Skip to main content

Various objects to denote special meanings in python

Project description

Overview

The sentinels module is a small utility providing the Sentinel class, along with useful instances.

What are Sentinels?

Sentinels are objects with special meanings. They can be thought of as singletons, but they service the need of having 'special' values in your code, that have special meanings (see example below).

Why Do I Need Sentinels?

Let's take NOTHING for example. This sentinel is automatically provided with the sentinels import:

>>> from sentinels import NOTHING

Let's say you're writing a wrapper around a Python dictionary, which supports a special kind of method, get_default_or_raise. This method behaves like get, but when it does not receive a default and the key does not exist, it raises a KeyError. How would you implement such a thing? The naive method is this:

>>> class MyDict(dict):
...     def get_default_or_raise(self, key, default=None):
...         if key not in self and default is None:
...             raise KeyError(key)
...         return self.get(key, default)

Or even this:

>>> class MyDict(dict):
...     def get_default_or_raise(self, key, default=None):
...         returned = self.get(key, default)
...         if returned is None:
...             raise KeyError(key)
...         return returned

But the problem with the above two pieces of code is the same -- when writing a general utility class, we don't know how it will be used later on. More importantly, None might be a perfectly valid dictionary value!

This is where NOTHING comes in handy:

>>> class MyDict(dict):
...     def get_default_or_raise(self, key, default=NOTHING):
...         returned = self.get(key, default)
...         if returned is NOTHING:
...             raise KeyError(key)
...         return returned

And Tada!

Semantics

Sentinels are always equal to themselves:

>>> NOTHING == NOTHING
True

But never to another object:

>>> from sentinels import Sentinel
>>> NOTHING == 2
False
>>> NOTHING == "NOTHING"
False

Copying sentinels returns the same object:

>>> import copy
>>> copy.deepcopy(NOTHING) is NOTHING
True

And of course also pickling/unpickling:

>>> import pickle
>>> NOTHING is pickle.loads(pickle.dumps(NOTHING))
True

Project details


Download files

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

Source Distribution

sentinels-1.1.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

sentinels-1.1.1-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file sentinels-1.1.1.tar.gz.

File metadata

  • Download URL: sentinels-1.1.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sentinels-1.1.1.tar.gz
Algorithm Hash digest
SHA256 3c2f64f754187c19e0a1a029b148b74cf58dd12ec27b4e19c0e5d6e22b5a9a86
MD5 b26f0ee1a3e289e4a68f02a7de75984b
BLAKE2b-256 6f9b07195878aa25fe6ed209ec74bc55ae3e3d263b60a489c6e73fdca3c8fe05

See more details on using hashes here.

Provenance

The following attestation bundles were made for sentinels-1.1.1.tar.gz:

Publisher: ci.yml on vmalloc/sentinels

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sentinels-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: sentinels-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sentinels-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 835d3b28f3b47f5284afa4bf2db6e00f2dc5f80f9923d4b7e7aeeeccf6146a11
MD5 4bc1a377f55a711c27740f6d571c56f1
BLAKE2b-256 4965dea992c6a97074f6d8ff9eab34741298cac2ce23e2b6c74fb7d08afdf85c

See more details on using hashes here.

Provenance

The following attestation bundles were made for sentinels-1.1.1-py3-none-any.whl:

Publisher: ci.yml on vmalloc/sentinels

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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