Custom types used throughout the library

class Member[source]

Member(*args)

Annotation only type to be used within @enumify.

Used to denote when a member will be of type str, with the value being its member name in lowercase.

from fastcore.test import ExceptionExpected

with ExceptionExpected(TypeError, "Member is a documentation type, cannot be instantiated"):
    t = Member()

with ExceptionExpected(TypeError, "Member is a documentation type, cannot be instantiated"):
    t = Mem()

class Documented[source]

Documented(*args)

Annotation only type to be used within @enumify.

Used to denote when a member will have a docstring as its last assert.

from fastcore.test import ExceptionExpected

with ExceptionExpected(TypeError, "Documented is a documentation type, cannot be instantiated"):
    t = Documented()

with ExceptionExpected(TypeError, "Documented is a documentation type, cannot be instantiated"):
    t = Doc()

FunctionalEnum[source]

Enum = []

An Enum class implementing __ne__, __eq__, and __str__ to compare self.value.

Compatible with the functional API.

_da = [["zero", 0], ["one", 1]]

_d = FunctionalEnum("test_enum", _da)
test_eq(hasattr(_d, "zero"), True)
test_eq(str(_d.zero), "0")
test_eq(_d.zero == 0, True)

test_eq(hasattr(_d, "one"), True)
test_eq(str(_d.one), "1")
test_eq(_d.one == 1, True)

DocumentedEnum[source]

Enum = []

An Enum capabile of having its members have docstrings.

Inherits FunctionalEnum to allow for logic comparison via ==, !=, and string representation str() of self.value.

_da = [["addition", ("Sum of two numbers", "addition")], ["subtraction", ("Some documentation")], ["multiplication", (None, "multiplication")]]

_d = DocumentedEnum("test_enum", _da)
test_eq(hasattr(_d, "addition"), True)
test_eq(str(_d.addition), "addition")
test_eq(_d.addition.__doc__, "Sum of two numbers")
test_eq(_d.addition == "addition", True)

test_eq(str(_d.subtraction), str(None))
test_eq(_d.subtraction.__doc__, "Some documentation")
test_eq(_d.subtraction != "addition", True)

test_eq(str(_d.multiplication), "multiplication")
test_eq(_d.multiplication.__doc__, "An enumeration.")
test_eq(_d.multiplication != "addition", True)

multiassert[source]

multiassert(type, types:list=[])

Runs == on all types

If any are True, returns True.

enumify

enumify[source]

enumify()

A decorator to turn cls into an Enum class with member values as property names, and potentially with documentation

Should be documented with the Member type with the following annotation:

from .typing import Member
@enumify
class MyClass:
  NAME:Member["Some documented enum value"]
  name_two:Member # An undocumented enum value
  name_three:Member["Some documentation"] = "some value"

Can also use the shorthand Mem type

@enumify
class DaysOfWeek:
    MONDAY:tuple[Member,Documented] = "First day of the week"
    TUESDAY:Member
    WEDNESDAY:Documented = "Wed", "Third day of the week"
    THURSDAY:Documented = 0, "Fourth day of the week"
    
test_eq(DaysOfWeek.MONDAY, "monday")
test_eq(DaysOfWeek.MONDAY.__doc__, "First day of the week")
test_eq(DaysOfWeek.TUESDAY, "tuesday")
test_eq(DaysOfWeek.TUESDAY.__doc__, "An enumeration.")
test_eq(DaysOfWeek.WEDNESDAY, "Wed")
test_eq(DaysOfWeek.WEDNESDAY.__doc__, "Third day of the week")
test_eq(DaysOfWeek.THURSDAY, 0)
test_eq(DaysOfWeek.THURSDAY.__doc__, "Fourth day of the week")
@enumify
class DaysOfWeek:
    MONDAY:tuple[Mem,Doc] = "First day of the week"
    TUESDAY:Mem
    WEDNESDAY:Doc = "Wed", "Third day of the week"
    THURSDAY:Doc = 0, "Fourth day of the week"
    
test_eq(DaysOfWeek.MONDAY, "monday")
test_eq(DaysOfWeek.MONDAY.__doc__, "First day of the week")
test_eq(DaysOfWeek.TUESDAY, "tuesday")
test_eq(DaysOfWeek.TUESDAY.__doc__, "An enumeration.")
test_eq(DaysOfWeek.WEDNESDAY, "Wed")
test_eq(DaysOfWeek.WEDNESDAY.__doc__, "Third day of the week")
test_eq(DaysOfWeek.THURSDAY, 0)
test_eq(DaysOfWeek.THURSDAY.__doc__, "Fourth day of the week")