Flavored Booleans

For when different flavors of the truth matter.

final class pythonic_fp.booleans.flavored.FBool

Bases: SBool

Favored Booleans

When different flavors of the truth matter. Each FBool is an SBool subtype corresponding to a hashable flavor.

Warning

Combining FBool instances of different flavors with bitwise operators will result in a runtime ValueError exception.

static __new__(cls, witness: object, flavor: Hashable) → Self

new

Traditional singleton pattern but with a ClassVar dict to store the for truthy or falsy singleton for each hashable flavor.

param witness:

Determines truthiness of the FBool instance returned.

param flavor:

The flavor of FBool to created.

returns:

The truthy or falsy FBool instance of a particular flavor.

__init__(witness: object, flavor: Hashable) → None

initialize

Let the flavored boolean know its flavor.

param witness:

Determines truthiness of the FBool instance returned.

param flavor:

The flavor of FBool to created.

type flavor:

H: Hashable

returns:

The truthy or falsy FBool instance of a particular flavor.

raises ValueError:

If different flavors compared with bitwise operators.

__reduce__() → tuple[type[FBool], tuple[Hashable]]

reduce

Tell pickle and copy how to reconstruct the SBool singleton by calling SBool() rather than rebuilding the object directly.

  • reconstruction goes through __new__, so the flavor cache is consulted

  • unpickling returns the existing singleton of that flavor, or creates and caches it in a fresh interpreter

  • copy and deepcopy use the same protocol, so they return the singleton unchanged

returns:

The pair (SBool, ((self._flavor))), which pickle uses to rebuild the singleton.

Important

Without this, pickle would construct a duplicate SBool outside the instance dict, and is comparisons against it would silently fail.

__invert__() → FBool

bitwise invert operator

Inverts the value of an FBool.

returns:

The inverted value of the FBool.

__and__(other: int) → int

bitwise and operator

Overloading & to perform Boolean “and” if other is another FBool of the same flavor.

param other:

Any object derived from int.

returns:

Boolean “and” if other is an FBool of the same flavor, otherwise returns bitwise “and”.

raises:

ValueError if other is an FBool of a different flavor.

__or__(other: int) → int

bitwise or operator

Overloading | to perform Boolean “or” if other is another FBool of the same flavor.

param other:

Any object derived from int.

returns:

Boolean “or” if other is an FBool of the same flavor, otherwise returns bitwise “or”.

raises:

ValueError if other is an FBool of a different flavor.

__xor__(other: int) → int

bitwise xor operator

Overloading ^ to perform Boolean “xor” if other is another FBool of the same flavor.

param other:

Any object derived from int.

returns:

Boolean “xor” if other is an FBool of the same flavor, otherwise returns bitwise “or”.

raises:

ValueError if other is an FBool of a different flavor.

__repr__() → str

repr string

Create strings of the form

  • FBool(True, repr_flavor)

  • FBool(False, repr_flavor)

Where repr_flavor = repr(self.flavor())

returns:

A String to reproduce the flavored boolean.

__str__() → str

user string

Create strings of the form

  • FBool(True, str_flavor)

  • FBool(False, str_flavor)

Where str_flavor = str(self.flavor())

returns:

A String meaningful to an end user.

flavor() → Hashable

flavor

Get the flavor of the FBool, a hashable value.

returns:

The flavor of the FBool.

pythonic_fp.booleans.flavored.truthy(flavor: Hashable) → FBool

function truthy

Returns the truthy singleton FBool of a particular flavor.

param flavor:

Hashable value to determine which singleton flavor to return.

returns:

The truthy singleton of a particular flavor.

pythonic_fp.booleans.flavored.falsy(flavor: Hashable) → FBool

Function falsy

Returns the falsy singleton FBool of a particular flavor.

param flavor:

Hashable value to determine which singleton flavor to return.

returns:

The falsy singleton of a particular flavor.