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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c2f64f754187c19e0a1a029b148b74cf58dd12ec27b4e19c0e5d6e22b5a9a86
|
|
| MD5 |
b26f0ee1a3e289e4a68f02a7de75984b
|
|
| BLAKE2b-256 |
6f9b07195878aa25fe6ed209ec74bc55ae3e3d263b60a489c6e73fdca3c8fe05
|
Provenance
The following attestation bundles were made for sentinels-1.1.1.tar.gz:
Publisher:
ci.yml on vmalloc/sentinels
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sentinels-1.1.1.tar.gz -
Subject digest:
3c2f64f754187c19e0a1a029b148b74cf58dd12ec27b4e19c0e5d6e22b5a9a86 - Sigstore transparency entry: 384016479
- Sigstore integration time:
-
Permalink:
vmalloc/sentinels@232ae4d7d2d885f02006153558ffb7ba479779ef -
Branch / Tag:
refs/tags/1.1.1 - Owner: https://github.com/vmalloc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@232ae4d7d2d885f02006153558ffb7ba479779ef -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
835d3b28f3b47f5284afa4bf2db6e00f2dc5f80f9923d4b7e7aeeeccf6146a11
|
|
| MD5 |
4bc1a377f55a711c27740f6d571c56f1
|
|
| BLAKE2b-256 |
4965dea992c6a97074f6d8ff9eab34741298cac2ce23e2b6c74fb7d08afdf85c
|
Provenance
The following attestation bundles were made for sentinels-1.1.1-py3-none-any.whl:
Publisher:
ci.yml on vmalloc/sentinels
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sentinels-1.1.1-py3-none-any.whl -
Subject digest:
835d3b28f3b47f5284afa4bf2db6e00f2dc5f80f9923d4b7e7aeeeccf6146a11 - Sigstore transparency entry: 384016494
- Sigstore integration time:
-
Permalink:
vmalloc/sentinels@232ae4d7d2d885f02006153558ffb7ba479779ef -
Branch / Tag:
refs/tags/1.1.1 - Owner: https://github.com/vmalloc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@232ae4d7d2d885f02006153558ffb7ba479779ef -
Trigger Event:
push
-
Statement type: