Nothing says "DO NOT USE" like a poison value.
Project description
pypoison
Nothing says "DO NOT USE" like a poison value.
Sometimes you need a value to represent absence of a value.
However, sometimes you want to make absolutely sure this value is not used in any way, shape, form, or anything whatsoever.
In these cases, values like None
, float('NaN')
, or False
don't really fit, because you can still "touch" these values.
That's what poison values are for. Because nothing says "DO NOT USE" like a poison value.
This module provides an object that overrides all methods to raise an exception.
Table of Contents
Install
Just pip install pypoison
. (As soon as I have uploaded it to PyPI.)
Or just copy pypoison/__init__.py
to your project as pypoison.py
.
There are no dependencies.
Usage
Just use the value returned by pypoison.get_poison()
when you need a poison value.
The object is always the same object.
Custom behavior
By default, all accesses raise a ValueError
with a human-readable
explanation which attribute/method was accessed.
If you prefer otherwise, you can call pypoison.set_exception(my_exception)
,
and the poison value now raises the object my_exception
whenever anything is done with the poison value.
For extra fanciness, you can call pypoison.set_handler(my_fn)
,
and the poison value will raise whatever my_fn(method_name)
returns.
To reset to the default behavior, you can call pypoison.set_exception(None)
Examples
>>> 1 in pypoison.get_poison()
ValueError: Tried to access __contains__ on poison value.
>>> pypoison.get_poison() / 1
ValueError: Tried to access __truediv__ on poison value.
>>> 1 + Foo()
ValueError: Tried to access __radd__ on poison value.
>>> pypoison.get_poison() * 4
ValueError: Tried to access __mul__ on poison value.
>>> pypoison.get_poison() % 3
ValueError: Tried to access __mod__ on poison value.
>>> 3 % pypoison.get_poison()
ValueError: Tried to access __rmod__ on poison value.
>>> '{}'.format(pypoison.get_poison())
ValueError: Tried to access __format__ on poison value.
>>> '{!r}'.format(pypoison.get_poison())
ValueError: Tried to access __repr__ on poison value.
>>> list(pypoison.get_poison())
ValueError: Tried to access __iter__ on poison value.
>>> help(pypoison.get_poison())
ValueError: Tried to access __getattribute__ on poison value.
>>> pypoison.get_poison() == 42
ValueError: Tried to access __eq__ on poison value.
Caveat
Serious
Heavy use of the inspect
module might fiddle around with the poison object,
however safeguards are in place to make sure that this is not easy.
Passive use, such as is
or storing it in a passive container (e.g. a list)
cannot be prevented. However, this is intentionl. This module wants to
provide a poisonous placeholder value, and not an actively malicious value.
I am not sure how to handle callable()
, id()
, and type()
.
Logically-null usage cannot be detected, such as in pypoison.get_poison() in []
or True or pypoison.get_poison()
or even max([pypoison.get_poison()])
.
This module may cause bugs in your debugger/IDE/editor. Or at least unexpected behavior.
Do not store the poison value in a global. Many tools, e.g. unittest, walk the global variables and inspect them.
Non-serious
The amount of black magic might poison your appreciation of Python.
Pythonidae are always nonvenomous; this module makes it poisonous!
TODOs
- Do weird stuff with it
Contribute
Feel free to dive in! Open an issue or submit PRs.
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
File details
Details for the file pypoison-0.0.1.tar.gz
.
File metadata
- Download URL: pypoison-0.0.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0040b42f488bcea8011151049113affea65f9d67534db107e1fc20bf215be84d |
|
MD5 | 7b44748539e5d33eae827cb22f96fbe9 |
|
BLAKE2b-256 | 6bca95207ab48789aedc87943c3ea4fd1a09e84805152ff5a9fbd1a677b432d2 |
File details
Details for the file pypoison-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: pypoison-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32fecfdf0986b1ba1f49cea6ed39825338499181dd8a3e340e748a5421e6bfe9 |
|
MD5 | 1f79f956e4dd52a8cfadfa393410f38a |
|
BLAKE2b-256 | 89d50d985b5cfdeb2f38527416499e53cc53a04311f72ff3a338e98d6394fe8e |