soft

A collection of soft objects.

Every soft object has a value that changes every time it is retrieved according to defined chance profiles. This value can be retrieved with the SoftObject ‘s get() method.

>>> blurry_float = SoftFloat([(-1, 2), (3, 5)])
>>> blurry_float.get()                                         
1.925674784815838
>>> blurry_float.get()                                         
1.120389067727415
>>> blurry_float.get()                                         
1.30418962132812
class blur.soft.SoftObject

An abstract base class for SoftObject ‘s.

Direct instances of SoftObject should not be created; instead, the appropriate subclass should be used.

Every SoftObject represents a stochastic blurry object whose value is determined with the get() method.

This is an abstract method and should not be called. Subclasses of SoftObject must override and implement this.

Direct instances of SoftObject should not be created; instead, the appropriate subclass should be used.

get()

Retrieve a value of this SoftObject.

This is an abstract method and should not be called. Subclasses of SoftObject must override and implement this.

class blur.soft.SoftOptions(options)

One of many objects with corresponding weights.

Parameters:options (list) – a list of options where each option is a tuple of form (Any, float) corresponding to (outcome, weight). Outcome values may be of any type. Weights 0 or less will have no chance to be retrieved by get()

Example

>>> options = SoftOptions([('option one', 2),
...                        ('option two', 5),
...                        ('option three', 8)])
>>> options.get()                                  
'option three'
classmethod with_uniform_weights(options, weight=1)

Initialize from a list of options, assigning uniform weights.

Parameters:
  • options (list) – The list of options of any type this object can return with the get() method.
  • weight (float or int) – The weight to be assigned to every option. Regardless of what this is, the probability of each option will be the same. In almost all cases this can be ignored. The only case for explicitly setting this is if you need to modify the weights after creation with specific requirements.
Returns:

SoftOptions – A newly constructed instance

Example

>>> blurry_object = SoftOptions.with_uniform_weights(
...     ['option one', 'option two', 'option three'])
>>> blurry_object.options
[('option one', 1), ('option two', 1), ('option three', 1)]
classmethod with_random_weights(options)

Initialize from a list of options with random weights.

The weights assigned to each object are uniformally random integers between 1 and len(options)

Parameters:options (list) – The list of options of any type this object can return with the get() method.
Returns:SoftOptions – A newly constructed instance
options

list – a list of options where each option is a tuple of form (Any, float or int) corresponding to (outcome, weight). Outcome values may be of any type. Weights 0 or less will have no chance to be retrieved by get()

get()

Get one of the options within the probability space of the object.

Returns:Any – An item from self.options.
class blur.soft.SoftBool(prob_true)

A stochastic bool defined by a probability to be True.

Parameters:prob_true (float) – The probability that get() returns True where prob_true <= 0 is always False and prob_true >= 1 is always True.
prob_true

float or int – The probability that get() returns True where prob_true <= 0 is always False and prob_true >= 1 is always True.

get()

Get either True or False depending on self.prob_true.

Returns:boolTrue or False depending on self.prob_true.
class blur.soft.SoftFloat(weights)

A stochastic float value defined by a list of weights.

Parameters:weights (list) – the list of weights where each weight is a tuple of form (int or float, int or float) corresponding to (outcome, strength). These weights represent the stochastic value of this SoftFloat.
classmethod bounded_uniform(lowest, highest, weight_interval=None)

Initialize with a uniform distribution between two values.

If no weight_interval is passed, this weight distribution will just consist of [(lowest, 1), (highest, 1)]. If specified, weights (still with uniform weight distribution) will be added every weight_interval. Use this if you intend to modify the weights in any complex way after initialization.

Parameters:
  • lowest (float or int) –
  • highest (float or int) –
  • weight_interval (int) –
Returns:

SoftFloat – A newly constructed instance.

weights

list – the list of weights where each weight is a tuple of form (int or float, int or float) corresponding to (outcome, strength). These weights represent the stochastic value of this SoftFloat.

get()

Get a float value in the probability space of the object.

Returns:float – A value between the lowest and highest outcomes in self.weights
class blur.soft.SoftInt(weights)

A stochastic int value defined by a list of weights.

Has the exact same functionality as SoftFloat, except that get() returns int values

Parameters:weights (list) – the list of weights where each weight is a tuple of form (int or float, int or float) corresponding to (outcome, strength). These weights represent the stochastic value of this SoftFloat.
get()

Get an int value in the probability space of the object.

Returns: int

bounded_uniform(lowest, highest, weight_interval=None)

Initialize with a uniform distribution between two values.

If no weight_interval is passed, this weight distribution will just consist of [(lowest, 1), (highest, 1)]. If specified, weights (still with uniform weight distribution) will be added every weight_interval. Use this if you intend to modify the weights in any complex way after initialization.

Parameters:
  • lowest (float or int) –
  • highest (float or int) –
  • weight_interval (int) –
Returns:

SoftFloat – A newly constructed instance.

weights

list – the list of weights where each weight is a tuple of form (int or float, int or float) corresponding to (outcome, strength). These weights represent the stochastic value of this SoftFloat.

class blur.soft.SoftColor(red, green, blue)

An RGB color whose individual channels can be SoftInt objects.

SoftColor.get() returns an (r, g, b) tuple o integers. To get a hexadecimal color value, use get_as_hex().

>>> color = SoftColor(234,                           # static red
...                   124,                           # static green
...                   SoftInt([(0, 10), (40, 20)]))  # soft blue
>>> rgb = color.get()
>>> rgb                                                
(234, 124, 32)
# Conveniently convert the value to hex
>>> SoftColor.rgb_to_hex(rgb)                          
'#EA7C20'
# Generate a new value directly as hex
>>> some_soft_color.get_as_hex()                       
'#EA7C20'
Parameters:
  • red (int or SoftInt or tuple(args for SoftInt) –
  • green (int or SoftInt or tuple(args for SoftInt) –
  • blue (int or SoftInt or tuple(args for SoftInt) –
Raises:

TypeError – if invalid types are passed in args

Notes

When initializing the soft color channels using the convenience option to pass a tuple of args for for SoftInt.__init__(), keep in mind that when creating 1-length tuples in Python you need to add a comma after the first element, or Python will ignore the parentheses.

color = SoftColor(([(0, 1), (255, 10)],),
                  ([(0, 1), (255, 10)],),
                  ([(0, 1), (255, 10)],))
red

SoftInt or int – The red channel of the RGB color

green

SoftInt or int – The green channel of the RGB color

blue

SoftInt or int – The blue channel of the RGB color

classmethod rgb_to_hex(color)

Convert an (r, g, b) color tuple to a hexadecimal string.

Alphabetical characters in the output will be capitalized.

Parameters:color (tuple) – An rgb color tuple of form: (int, int, int)

Returns: string

Example

>>> SoftColor.rgb_to_hex((0, 0, 0))
'#000000'
>>> SoftColor.rgb_to_hex((255, 255, 255))
'#FFFFFF'
get()

Get an rgb color tuple according to the probability distribution.

Returns:tuple (int, int, int) – A (red, green, blue) tuple.

Example

>>> color = SoftColor(([(0, 1), (255, 10)],),
...                   ([(0, 1), (255, 10)],),
...                   ([(0, 1), (255, 10)],))
>>> color.get()                                    
(234, 201, 243)
get_as_hex()

Get a hexademical color according to the probability distribution.

Equivalent to SoftColor.rgb_to_hex(self.get())

Returns:str – A hexademical color string

Example

>>> color = SoftColor(([(0, 1), (255, 10)],),
...                   ([(0, 1), (255, 10)],),
...                   ([(0, 1), (255, 10)],))
>>> color.get_as_hex()                             
'#C8EABB'