Skip to main content

Simple "symbol" type, useful for enumerations or sentinels

Project description

Installing SymbolType (using "easy_install SymbolType" or "setup.py install") gives you access to the peak.util.symbols module, previously available only by installing the full PEAK toolkit. peak.util.symbols provides a Symbol type and two built-in symbols that are used by PEAK: NOT_FOUND and NOT_GIVEN. You can create your own symbol objects using the Symbol type, by giving it the symbol name and the name of the module where the symbol is being created:

>>> from peak.util.symbols import Symbol
>>> AN_EXAMPLE = Symbol('AN_EXAMPLE', __name__)

The resulting object’s repr() and str() forms are the same as the name you passed in:

>>> AN_EXAMPLE
AN_EXAMPLE

>>> str(AN_EXAMPLE)
'AN_EXAMPLE'

But symbols compare equal only to themselves; they are not equal to strings:

>>> AN_EXAMPLE == 'AN_EXAMPLE'
False

>>> AN_EXAMPLE == AN_EXAMPLE
True

A symbol’s __name__ and __module__ attributes are the original name and module used to create the symbol:

>>> from peak.util.symbols import NOT_FOUND

>>> NOT_FOUND.__name__
'NOT_FOUND'

>>> NOT_FOUND.__module__
'peak.util.symbols'

The reason that symbols want to know their defining module is that this allows them to be pickled and unpickled correctly:

>>> import pickle
>>> pickle.loads(pickle.dumps(NOT_FOUND))
NOT_FOUND

Specifically, it’s so that the result of unpickling a symbol is exactly the same object as the original symbol:

>>> pickle.loads(pickle.dumps(NOT_FOUND)) is NOT_FOUND
True

Note that this means the symbol must be defined at module level within its module, with the same name that’s passed in to it, or else pickle will not be able to find it when unpickling.

Last, but not least, symbol objects are immutable and cannot be changed in any way:

>>> AN_EXAMPLE.foo = "bar"
Traceback (most recent call last):
...
TypeError: Symbols are immutable

Mailing List

Please direct questions regarding this package to the PEAK mailing list; see http://www.eby-sarna.com/mailman/listinfo/PEAK/ for details.

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

SymbolType-1.0.zip (9.5 kB view details)

Uploaded Source

Built Distributions

SymbolType-1.0-py2.4.egg (4.1 kB view details)

Uploaded Source

SymbolType-1.0-py2.3.egg (7.8 kB view details)

Uploaded Source

File details

Details for the file SymbolType-1.0.zip.

File metadata

  • Download URL: SymbolType-1.0.zip
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for SymbolType-1.0.zip
Algorithm Hash digest
SHA256 bcc311398c7dbcc9cbde9fe68bba89b0efe45103410b5949f3acf484fcb14f39
MD5 4b9155cc908b249f029a518b8de09368
BLAKE2b-256 2b32777db70cba95a80fbc1669e72acb9f4c02584a49476ea26dfbef1902bac5

See more details on using hashes here.

File details

Details for the file SymbolType-1.0-py2.4.egg.

File metadata

  • Download URL: SymbolType-1.0-py2.4.egg
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for SymbolType-1.0-py2.4.egg
Algorithm Hash digest
SHA256 5726c3052e2165e7bab7aa9bc7c91d902835a1108a9164784c542f8cc19e72e1
MD5 9eb77a7d0fce6602ee46b7c5808aa37f
BLAKE2b-256 4d2260a01c69bcfe47a265fa86c1d00b4f654e4031a41f2af2330b1854faf812

See more details on using hashes here.

File details

Details for the file SymbolType-1.0-py2.3.egg.

File metadata

  • Download URL: SymbolType-1.0-py2.3.egg
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for SymbolType-1.0-py2.3.egg
Algorithm Hash digest
SHA256 2feb40190e7a4b8a01869bbbbbcbb0e571cc168d2001820be962bec09c8f104b
MD5 1e04236cdf29c6e143c219e1c7d009ed
BLAKE2b-256 3e17b7da26e970c959a22eaef29645756026da38915124294a68735cd1511825

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page