Skip to content

Latest commit

 

History

History
87 lines (65 loc) · 3.56 KB

cheatsheet.md

File metadata and controls

87 lines (65 loc) · 3.56 KB

Cheatsheet

Definition

Constant.define_set creates & returns a subclass for your set of constants.

# Form 1  (cname only)
Color = Constant.define_set( "Color", "RED", "GREEN", "BLUE" )

# Form 2  (cname + value)
Color = Constant.define_set( "Color", RED="ff0000", GREEN="00ff00", BLUE="0000ff" )

# Form 3  (cname + arbitrary attributes)
Color = Constant.define_set(
    "Color",
    RED   = dict( hex="ff0000", like=True  ),
    GREEN = dict( hex="00ff00", like=True  ),
    BLUE  = dict( hex="0000ff", like=False ),
)

Class API

Color.RED                               # constants are attached by cname
...

Color.all                               # -> [ Color.RED, ... ]
Color.as_cnames( filter=None )          # -> [ "RED", ... ]

# The next three support names "cname_pretty" and "ordinal" as if they were attributes.
Color.pick(        name, filter=None )  # list of values for named attribute
                                        # (raises AttributeError if missing)
Color.as_dicts(  *names, filter=None )  # list of dicts  generated by as_dict(  *names )
Color.as_tuples( *names, filter=None )  # list of tuples generated by as_tuple( *names )

Color.choices( filter=None )            # list of ( cname, cname_pretty )

Color.max_length                        # length of longest cname

Color.filter(    filter )               # list of objects where filter( obj ) is True
Color.select(  **kwargs )               # list of objects matching kwargs (ANDed)
Color.get(     **kwargs )               # single object matching kwargs (ANDed)
                                        # raises ValueError if matches != 1

Color.__contains__( item )              # ex: `Color.RED in Color`  -or-  `"RED" in Color`
Color.__getitem(    item )              # ex: `Color[ Color.RED ]`  -or-  `Color[ "RED" ]`
                                        # returns None if item not in set
Color.__iter__(          )              # ex: `for color in Color:`
Color.__len__(           )              # ex: len( Color ) -> count of objects

# You generally shouldn't need to use this.
Color.populate( *args, **kwargs )       # create & register multiple constants
                                        # used by Constant.define_set
                                        # supports all three forms

Instance API

c = Color.RED

c.cname
c.cname_pretty                  # titlecase + "_" -> " "  ("FOO_BAR" -> "Foo Bar")
c.ordinal                       # definition order  (1 .. len)

c.value                         # only if Form 2, or explicitly with Form 3
c.<attribute>                   # any attribute specified during creation

# The next three support names "cname_pretty" and "ordinal" as if they were attributes.
c.getattr_inclusive( name )     # raises AttributeError if attribute not found
c.as_dict(  *names )            # dict with all attributes, or a subset if *names
c.as_tuple( *names )            # tuple with all attribute values in original order,
                                # or a subset if *names  (in the same order as *names);
                                # if Form 1 and not *names, defaults to:  ( cname, ordinal )

c.__repr__(     )               # cname
c.__str__(      )               # cname
c.__len__(      )               # length of cname:  len( c )

c.__eq__( other )               # compare by cname    (also __ne__)
c.__lt__( other )               # compare by ordinal  (also __le__, __gt__, __ge__)

c.__hash__(     )               # hash( cname ), to support use as dict key
c.__index__(    )               # ordinal, to support use in slice expressions