File: README.rst

package info (click to toggle)
python-sentinels 1.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 128 kB
  • sloc: python: 63; sh: 7; makefile: 3
file content (75 lines) | stat: -rw-r--r-- 2,325 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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