Name | sentinels JSON |
Version |
1.0.0
JSON |
| download |
home_page | |
Summary | Various objects to denote special meanings in python |
upload_time | 2016-08-30 07:19:19 |
maintainer | |
docs_url | None |
author | Rotem Yaari |
requires_python | |
license | BSD |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
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
Raw data
{
"_id": null,
"home_page": "",
"name": "sentinels",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Rotem Yaari",
"author_email": "vmalloc@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/ac/b7/1af07a98390aba07da31807f3723e7bbd003d6441b4b3d67b20d97702b23/sentinels-1.0.0.tar.gz",
"platform": "UNKNOWN",
"description": "Overview\n--------\n\nThe sentinels module is a small utility providing the Sentinel class, along with useful instances.\n\nWhat are Sentinels?\n-------------------\n\nSentinels 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).\n\nWhy Do I Need Sentinels?\n------------------------\n\nLet's take *NOTHING* for example. This sentinel is automatically provided with the sentinels import::\n\n >>> from sentinels import NOTHING\n\nLet'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::\n\n >>> class MyDict(dict):\n ... def get_default_or_raise(self, key, default=None):\n ... if key not in self and default is None:\n ... raise KeyError(key)\n ... return self.get(key, default)\n\nOr even this::\n\n >>> class MyDict(dict):\n ... def get_default_or_raise(self, key, default=None):\n ... returned = self.get(key, default)\n ... if returned is None:\n ... raise KeyError(key)\n ... return returned\n\nBut 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!**\n\nThis is where NOTHING comes in handy::\n\n\n >>> class MyDict(dict):\n ... def get_default_or_raise(self, key, default=NOTHING):\n ... returned = self.get(key, default)\n ... if returned is NOTHING:\n ... raise KeyError(key)\n ... return returned\n\nAnd Tada!\n\nSemantics\n---------\n\nSentinels are always equal to themselves::\n\n >>> NOTHING == NOTHING\n True\n\nBut never to another object::\n\n >>> from sentinels import Sentinel\n >>> NOTHING == 2\n False\n >>> NOTHING == \"NOTHING\"\n False\n\nCopying sentinels returns the same object::\n\n >>> import copy\n >>> copy.deepcopy(NOTHING) is NOTHING\n True\n\nAnd of course also pickling/unpickling::\n\n >>> import pickle\n >>> NOTHING is pickle.loads(pickle.dumps(NOTHING))\n True\n",
"bugtrack_url": null,
"license": "BSD",
"summary": "Various objects to denote special meanings in python",
"version": "1.0.0",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "acb71af07a98390aba07da31807f3723e7bbd003d6441b4b3d67b20d97702b23",
"md5": "6b1327d94883cccbce8a18c1e7e835b1",
"sha256": "7be0704d7fe1925e397e92d18669ace2f619c92b5d4eb21a89f31e026f9ff4b1"
},
"downloads": -1,
"filename": "sentinels-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "6b1327d94883cccbce8a18c1e7e835b1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4074,
"upload_time": "2016-08-30T07:19:19",
"upload_time_iso_8601": "2016-08-30T07:19:19.963319Z",
"url": "https://files.pythonhosted.org/packages/ac/b7/1af07a98390aba07da31807f3723e7bbd003d6441b4b3d67b20d97702b23/sentinels-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2016-08-30 07:19:19",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "sentinels"
}