Skip to main content

A very small and simple usage mechanism for Python threadlocals.

Project description

Build Status

A very small and simple usage mechanism for Python threadlocals.

This is an abstraction on type of threading.local that attempts to simply usage a bit and combat the common problem of accidentally overwriting values that may have been added by other portions of code. It also implements the in operator to test for inclusion and the bool() method to test whether the Chipmunk is holding anything.

Example Usage

Instantiating

The Chipmunk object is a sort of singleton that is instantiated upon import.

from chipmunk import Chipmunk #  Ready for use

Storing Data

Asking the Chipmunk to hold something can be done in one of three ways:

Setting Attributes Directly

Chipmunk.acorn = "Acorn"
Chipmunk.acorn_count = 5

With the store_data method

Chipmunk.store_data("acorn", "Acorn") Chipmunk.store_data("acorn_count", 5)

And with a context manager for short-term storage that removes the need for checking whether it is already holding something with the given name.

Chipmunk.nut = "acorn"

with Chipmunk.hold_this("nut", "walnut"):
    do_something_with_Chipmunk()

return Chipmunk.nut #  Returns "acorn"

If the Chipmunk is already holding something and you ask it to hold something else with the same name it will raise an AttributeError. If you want to replace an object permanently you must call either the delete_data or clear methods or use del Chipmunk.attr.

Chipmunk.nut = "acorn"
Chipmunk.nut = "walnut" #  AttributeError thrown

Chipmunk.nut = "acorn"
del Chipmunk.nut
Chipmunk.nut = "walnut" #  OK

Chipmunk.nut = "acorn"
Chipmunk.delete_data("nut")
Chipmunk.nut = "walnut" #  OK

Retrieving Data

When retrieving data from the Chipmunk a bool() check will tell you if it’s holding anything at all.

from chipmunk import Chipmunk
if Chipmunk: #  Conditional fails
    return "Not Empty"

Chipmunk.nut = "acorn"
if Chipmunk: #  Conditional succeeds
    return "Not Empty"

Testing whether the Chipmunk is holding something with a given name is as simple as an in check.

Chipmunk.nut = "acorn"
"nut" in Chipmunk #  True

Data can be accessed by doing an attribute lookup directly, using the get_data method, or getattr.

Chipmunk.nut = "acorn"

# These all return "acorn"
Chipmunk.nut
Chipmunk.get_data("nut")
getattr(Chipmunk, "nut")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chipmunk-1.0.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

chipmunk-1.0.1-py2.py3-none-any.whl (5.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file chipmunk-1.0.1.tar.gz.

File metadata

  • Download URL: chipmunk-1.0.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for chipmunk-1.0.1.tar.gz
Algorithm Hash digest
SHA256 59f13197d984ebd4d40e97fe1276328706033737fc149ceb85861db4e78833f2
MD5 af59a9c0d0d1fc93b32fe23f05ada41c
BLAKE2b-256 524ca359e6d322ee11c28bfe2766ee61c9147d376569509356c81196b7a8a3b7

See more details on using hashes here.

File details

Details for the file chipmunk-1.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for chipmunk-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 882d7e2a7dfba9f52cacfdc3c50e5aed60071c9bc174a93ef3db3da6d0452bad
MD5 26924f362d9ee0716787efa85afc2b86
BLAKE2b-256 4241507c5037cca0772a2a6645de8ad7fa1d5f4e09cfcaccb6a4f9647debd1ea

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page