Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Creates simple sentinel objects which are the only instance of their own anonymous class. As a singleton, there is a guarantee that there will only ever be one instance: they can be safely used with pickle and cPickle alike, as well as being able to be used properly with copy.deepcopy(). In addition, a self-documenting __repr__ is provided for free!

Usage

Sentinels are singleton objects that typically represent some end or terminating condition. Some singletons already exist in Python, like None NotImplemented, and Ellipsis.

All that’s needed to create a sentinel is its name:

>>> import sentinel
>>> Nothing = sentinel.create('Nothing')
>>> Nothing
Nothing

This by itself is useful when other objects such as None, False, 0, -1, etc. are entirely valid values. For example, setting default values when all other values are valid with: dict.setdefault():

>>> MissingEntry = sentinel.create('MissingEntry')
>>> d = {'stdout': None, 'stdin': 0, 'EOF': -1}
>>> [d.setdefault(key, MissingEntry) for key in ('stdin', 'stdout', 'stderr')]
[0, None, MissingEntry]

Alternatively, using dict.get() when fetching values:

>>> d = {'stdout': None, 'stdin': 0, 'EOF': -1}
>>> d.get('stdout', MissingEntry)
None
>>> d.get('stdin', MissingEntry)
0
>>> d.get('stderr', MissingEntry)
MissingEntry

It’s known immediately which value was missing from the dictionary in a self-documenting manner.

Adding extra methods and class attributes

Sentinels may also inherit from base classes, or implement extra methods.

Consider a binary search tree with two kinds of nodes: interior nodes (Node) which contain some payload and leaves (Leaf), which simply terminate traversal.

To create singleton leaf which implements a search method and an is_leaf property, you may provide any extra class attributes in the cls_dict keyword argument. The following is a full example of both the singleton Leaf and its Node counterpart:

def _search_leaf(self, key):
    raise KeyError(key)

Leaf = sentinel.create('Leaf', cls_dict={
    'search': _search_leaf,
    'is_leaf': property(lambda self: True)
})

class Node(object):
    def __init__(self, key, payload, left=Leaf, right=Leaf):
        self.left = left
        self.right = right
        self.key = key
        self.payload = payload

    def search(self, key):
        if key < self.key:
            return self.left.search(key)
        elif key > self.key:
            return self.right.search(key)
        else:
            return self.payload

    is_leaf = property(lambda: false)

Example usage:

>>> tree = Node(2, 'bar', Node(1, 'foo'), Node(3, 'baz'))
>>> tree.search(1)
'foo'
>>> tree.search(4)
Traceback (most recent call last):
    ...
KeyError: 2

Contributing

This project uses Poetry. To contribute to the codebase, make sure to install poetry, With Poetry installed, clone then repo, then within the repo directory, install the developer dependencies:

$ poetry install

Next, I recommend you do all development tasks within the poetry shell:

$ poetry shell
(sentinel-nUnrocCf-py3.9) $ black .
(sentinel-nUnrocCf-py3.9) $ pytest

Download files

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

Source Distribution

sentinel-0.2.1a0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sentinel-0.2.1a0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file sentinel-0.2.1a0.tar.gz.

File metadata

  • Download URL: sentinel-0.2.1a0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1032-azure

File hashes

Hashes for sentinel-0.2.1a0.tar.gz
Algorithm Hash digest
SHA256 b77c156d12cbad13291a41547da0dc81f9dc53b62064e3f662e2e11e7a7b27b6
MD5 3c9a8c1290f58c87d74c22dbe21338e7
BLAKE2b-256 3f777d0cea8c0ae9c0b8b090255c2d6b728fd54aee5504847a48f11d5ff24033

See more details on using hashes here.

File details

Details for the file sentinel-0.2.1a0-py3-none-any.whl.

File metadata

  • Download URL: sentinel-0.2.1a0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.4.0-1032-azure

File hashes

Hashes for sentinel-0.2.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 85bca409b63018a5327eb8ce600d21258c6d2fd4527d32bdc76155daff48223f
MD5 c615b59d209c93a0110dbab70fe79186
BLAKE2b-256 e711acf4dee9f4cfaa6b78b31c983ced4f4580a8790f49e5e803f4e1964e7bba

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.0

2 files

0.3.0

2 files

This release

0.2.1a0 This release

2 files

0.2.0

2 files

0.1.2

2 files

0.1.1

1 file

0.1.0

1 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