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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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