This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
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
Release History

Release History

1.0.0

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.0.6

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.0.5

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.0.4

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.0.3

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.0.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.0.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
sentinels-1.0.0.tar.gz (4.1 kB) Copy SHA256 Checksum SHA256 Source Aug 30, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting