Skip to main content

Unique temporary id generator for python

Project description

utempid

Unique temporary id generator for python

Usage

Getting it

To download utempid, simply use Pypi via pip.

$ pip install utempid

Using it

# import the generator class
from utempid import UtempidGenerator

# create the generator
gen = utempid.UtempidGenerator()

# get temporary ids
first_id = gen.get_id() # first_id = 0
second_id = gen.get_id() # second_id = 1
third_id = gen.get_id() # third_id = 2

# return ids when no longer needed
gen.return_id(second_id)

# track amount of temporary ids in use
print(gen.active_count) # 2

Example

UtempidGenerator can be used when creating temporary objects with unique ids. When an object is destroyed, the id is returned to the generator. This allows for an easy count of the live objects and creates ids that are limited by the maximum amount of live objects at any point.

# import the generator class
from utempid import UtempidGenerator

class TemporaryObject(object):

    gen = UtempidGenerator()

    def __init__(self):
        self.id = self.gen.get_id()

    @classmethod
    def count(cls):
        return cls.gen.active_count

    def __del__(self):
        self.gen.return_id(self.id)


def main():
    a = TemporaryObject() # a.id = 0
    b = TemporaryObject() # b.id = 1
    c = TemporaryObject() # c.id = 2

    print("count: ", TemporaryObject.count()) # 3
    del a # return id 0
    print("count: ", TemporaryObject.count()) # 2

    d = TemporaryObject() # d.id = 0

    print("count: ", TemporaryObject.count()) # 3
    b = 1 # return id 1
    print("count: ", TemporaryObject.count()) # 2

License

MIT License

Copyright (c) 2018 Joel Barmettler

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

utempid-1.0.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

utempid-1.0.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file utempid-1.0.0.tar.gz.

File metadata

  • Download URL: utempid-1.0.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.0 CPython/3.7.4

File hashes

Hashes for utempid-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d5465a34de36a989cbb18d30a06988a3cc16b4929200c1915a788465a87cfe2c
MD5 5e9945ead5ee6e26393e022b5acab9b0
BLAKE2b-256 f5d8ddef38c6c96bd3a59b9556d4ee45bd261e05cc23d829e8d9895a97b007dc

See more details on using hashes here.

File details

Details for the file utempid-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: utempid-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.0 CPython/3.7.4

File hashes

Hashes for utempid-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d2cef840a10f13d19fbe5a10c89a82c4d1b03a79cc2c6a129beac6d8840fcb9
MD5 68462dfbf1c4be5d1d2d4d643b072ffb
BLAKE2b-256 24ddd9e7046a51be7320f955f9150d2da2e094638b4a815645857ddd9ff385c5

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