Skip to main content
https://travis-ci.org/TriOptima/tri.token.svg?branch=master https://codecov.io/github/TriOptima/tri.token/coverage.svg?branch=master

tri.token

tri.token provides enriched enum functionality. tri.token enum structures are declared using:

  • TokenAttribute: overridable attribute definitions with support for dynamic values.

  • Token: holds TokenAttributes objects.

  • TokenContainer: holds Token objects.

In other words: a Token is an enum which has TokenInstance members. Token instances are declared within TokenContainer(s).

Basic usage

from tri_token import Token, TokenAttribute, TokenContainer, PRESENT


class Taste(Token):
    name = TokenAttribute()
    display_name = TokenAttribute(value=lambda name, **_: name.upper() + '!!')
    opinion = TokenAttribute()


class Tastes(TokenContainer):
    vanilla = Taste()
    pecan_nut = Taste(display_name="pecan nutz", opinion="Tasty")


# A TokenContainer is a collection of Token objects.
assert Tastes.vanilla in Tastes

# The order of Token objects in a TokenContainer is by order of declaration.
assert list(Tastes) == [Tastes.vanilla, Tastes.pecan_nut]
assert list(Tastes) != [Tastes.pecan_nut, Tastes.vanilla]

# Magic for 'name' TokenAttribute. It is set automatically from the token declaration within it's container.
assert Tastes.vanilla.name == "vanilla"

# A TokenAttribute will have a None value if not set during Token instantiation.
assert Tastes.vanilla.opinion is None

# A TokenAttribute can have a dynamic value, derived from the invocation to the callable
# set as 'value' in the TokenAttribute definition
# (see declaration of 'display_name' TokenAttribute further up in the code).

# The real value of the token attribute will be the return value of an invocation to said callable.
# The invocation will receive the values of all other token attributes passed as keyword arguments.
assert Tastes.vanilla.display_name == "VANILLA!!"

# TokenAttribute dynamic value behavior is overridden/not used if value is set explicitly during Token instantiation.
assert Tastes.pecan_nut.display_name == "pecan nutz"

# A TokenContainer can be rendered as csv, excel, rst etc
assert """\
+--------------+---------+
| display_name | opinion |
+==============+=========+
|  VANILLA!!   |         |
+--------------+---------+
|  pecan nutz  |  Tasty  |
+--------------+---------+\
""" == Tastes.to_rst(['display_name', 'opinion'])

Optional token attributes

# A TokenAttribute may be declared as having optional dynamic values.
# That is, we want these dynamic attributes to be evaluated sometimes, but not always.
# In the example below, we want some superheroes to have homes, but not others.

SUPERHERO_HOMES = {'superman': 'Fortress of Solitude',
                   'batman': 'Batcave'}


class Superhero(Token):
    name = TokenAttribute()
    home = TokenAttribute(optional_value=lambda name, **_: SUPERHERO_HOMES[name])


# The PRESENT special value is used during Token instantiation to decide what
# optional token attributes should be evaluated.
class Superheroes(TokenContainer):
    batman = Superhero(home=PRESENT)
    hawkman = Superhero()
    wonder_woman = Superhero(home='Themyscira')

# Batman has a home, but poor Hawkman does not.
assert Superheroes.batman.home == 'Batcave'
assert Superheroes.hawkman.home is None

# Just as with dynamic attributes, the logic for TokenAttribute optional dynamic values is overridden
if value is set explicitly during Token instantiation.
assert Superheroes.wonder_woman.home = 'Themyscira'

# As a shortcut, PRESENT for specific optional token attributes may be assigned to
# variables, and used in declarations, for enhanced readability.
# This is useful when one has tokens with many attributes declared using dynamic values,
# but we don't want all of them to be evaluated in all tokens.
home = PRESENT('home')

class Superheroes(TokenContainer):
    batman = Superhero(home)
    hawkman = Superhero()

# Again, Batman has a home, but poor Hawkman does not.
assert Superheroes.batman.home == 'Batcave'
assert Superheroes.hawkman.home is None

TokenAttribute inheritance

class FooToken(Token):
    foo = TokenAttribute(value=lambda **kwargs: 'foo_value')

class BarToken(Token):
    bar = TokenAttribute()

class FieToken(FooToken, BarToken):
    fie = TokenAttribute()

class FooBarFieTokenContainer(TokenContainer):
    t = FieToken(fie=3)

assert dict(FooBarFieTokenContainer.t) == {'foo': 'foo_value', 'bar': None, 'name': 't', 'fie': 3}

TokenAttribute container inheritance

class MyToken(Token):

    name = TokenAttribute()
    stuff = TokenAttribute()


class MyTokens(TokenContainer):

    foo = MyToken(stuff='Hello')
    bar = MyToken(stuff='World')

assert MyTokens.foo in MyTokens

class MoreTokens(MyTokens):
    boink = MyToken(stuff='Other Stuff')

assert MyTokens.foo in MoreTokens

assert list(MoreTokens) == [MyTokens.foo, MyTokens.bar, MoreTokens.boink]
assert MoreTokens.foo is MyTokens.foo

For more tri.token examples, please have a look at the contents of tests/test_tokens.py in the installation directory.

Running tests

You need tox installed then just make test.

License

BSD

Documentation

http://tritoken.readthedocs.org.

Release files for tri.token 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tri.token 4.0.0
File Size Uploaded
tri.token-4.0.0.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tri.token 4.0.0
File Interpreter ABI Platform
tri.token-4.0.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 18.6 kB

Release files / tri.token-4.0.0.tar.gz

Download URL tri.token-4.0.0.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
17ab78a63ac2efbfd99ce63d8e04aad0de312f01ca91a3a4ca9ad80e54be8516
BLAKE2b-256 checksum
How to use checksums
bb2f90a8b665507ca872c0cc8d0224ba6025af5c2b408e8a032a1a68c347698b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.11

Release files / tri.token-4.0.0-py2.py3-none-any.whl

Download URL tri.token-4.0.0-py2.py3-none-any.whl
Size 8.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
56c491bed5996567130ca4e58bd70c81362e1ed17e9888883d7e64055aacf15d
BLAKE2b-256 checksum
How to use checksums
8562b2b7257d281cd0ebc5f03c1c02edf9ca90a4a0821f7394a352eb01ac539d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.11

Release history Release notifications | RSS feed

This release

4.0.0 This release

2 release files

3.3.0

1 release file

3.2.0

1 release file

3.1.1

1 release file

3.1.0

1 release file

3.0.1

1 release file

3.0.0

1 release file

1.1.0

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page