cengal.data_generation.id_generator.versions.v_1.id_generator

 1#!/usr/bin/env python
 2# coding=utf-8
 3
 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
 5# 
 6# Licensed under the Apache License, Version 2.0 (the "License");
 7# you may not use this file except in compliance with the License.
 8# You may obtain a copy of the License at
 9# 
10#     http://www.apache.org/licenses/LICENSE-2.0
11# 
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18from cengal.modules_management.alternative_import import alt_import
19from enum import Enum
20UUID_PRESENT = True
21with alt_import('uuid') as uuid:
22    if not uuid:
23        UUID_PRESENT = False
24
25"""
26Module Docstring
27Docstrings: http://www.python.org/dev/peps/pep-0257/
28"""
29
30__author__ = "ButenkoMS <gtalk@butenkoms.space>"
31__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
32__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
33__license__ = "Apache License, Version 2.0"
34__version__ = "4.4.1"
35__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
36__email__ = "gtalk@butenkoms.space"
37# __status__ = "Prototype"
38__status__ = "Development"
39# __status__ = "Production"
40
41
42class GeneratorType(Enum):
43    integer = 0
44    guid_string = 1
45    reusable_integer = 2
46
47
48class IDGenerator:
49    def __init__(self, generator_type=GeneratorType.integer):
50        self.counter = 0
51        self.type = generator_type
52        self.get_new_id = None
53        if GeneratorType.integer == self.type:
54            self.get_new_id = self._get_new_id__int
55        elif GeneratorType.guid_string == self.type:
56            if UUID_PRESENT:
57                self.get_new_id = self._get_new_id__uuid
58            else:
59                self.get_new_id = self._get_new_id__uuid_fake
60        elif GeneratorType.reusable_integer == self.type:
61            raise NotImplementedError
62        else:
63            raise RuntimeError('Wrong `generator_type` value')
64
65    def _get_new_id__int(self):
66        current_counter = self.counter
67        self.counter += 1
68        return current_counter
69
70    def _get_new_id__uuid(self):
71        seq = self.counter
72        self.counter += 1
73        current_counter = uuid.uuid1(clock_seq=seq).hex
74        return current_counter
75
76    def _get_new_id__uuid_fake(self):
77        current_counter = self.counter
78        self.counter += 1
79        return str(current_counter)
80
81    def remove_id(self, id_to_be_removed):
82        if GeneratorType.integer == self.type:
83            pass
84        elif GeneratorType.guid_string == self.type:
85            pass
86        elif GeneratorType.reusable_integer == self.type:
87            raise NotImplementedError
88        else:
89            raise RuntimeError('Wrong `self.type` value')
90
91    def clear(self):
92        self.counter = 0
93
94    def __call__(self):
95        return self.get_new_id()
UUID_PRESENT = True
class GeneratorType(enum.Enum):
43class GeneratorType(Enum):
44    integer = 0
45    guid_string = 1
46    reusable_integer = 2

An enumeration.

integer = <GeneratorType.integer: 0>
guid_string = <GeneratorType.guid_string: 1>
reusable_integer = <GeneratorType.reusable_integer: 2>
Inherited Members
enum.Enum
name
value
class IDGenerator:
49class IDGenerator:
50    def __init__(self, generator_type=GeneratorType.integer):
51        self.counter = 0
52        self.type = generator_type
53        self.get_new_id = None
54        if GeneratorType.integer == self.type:
55            self.get_new_id = self._get_new_id__int
56        elif GeneratorType.guid_string == self.type:
57            if UUID_PRESENT:
58                self.get_new_id = self._get_new_id__uuid
59            else:
60                self.get_new_id = self._get_new_id__uuid_fake
61        elif GeneratorType.reusable_integer == self.type:
62            raise NotImplementedError
63        else:
64            raise RuntimeError('Wrong `generator_type` value')
65
66    def _get_new_id__int(self):
67        current_counter = self.counter
68        self.counter += 1
69        return current_counter
70
71    def _get_new_id__uuid(self):
72        seq = self.counter
73        self.counter += 1
74        current_counter = uuid.uuid1(clock_seq=seq).hex
75        return current_counter
76
77    def _get_new_id__uuid_fake(self):
78        current_counter = self.counter
79        self.counter += 1
80        return str(current_counter)
81
82    def remove_id(self, id_to_be_removed):
83        if GeneratorType.integer == self.type:
84            pass
85        elif GeneratorType.guid_string == self.type:
86            pass
87        elif GeneratorType.reusable_integer == self.type:
88            raise NotImplementedError
89        else:
90            raise RuntimeError('Wrong `self.type` value')
91
92    def clear(self):
93        self.counter = 0
94
95    def __call__(self):
96        return self.get_new_id()
IDGenerator(generator_type=<GeneratorType.integer: 0>)
50    def __init__(self, generator_type=GeneratorType.integer):
51        self.counter = 0
52        self.type = generator_type
53        self.get_new_id = None
54        if GeneratorType.integer == self.type:
55            self.get_new_id = self._get_new_id__int
56        elif GeneratorType.guid_string == self.type:
57            if UUID_PRESENT:
58                self.get_new_id = self._get_new_id__uuid
59            else:
60                self.get_new_id = self._get_new_id__uuid_fake
61        elif GeneratorType.reusable_integer == self.type:
62            raise NotImplementedError
63        else:
64            raise RuntimeError('Wrong `generator_type` value')
counter
type
get_new_id
def remove_id(self, id_to_be_removed):
82    def remove_id(self, id_to_be_removed):
83        if GeneratorType.integer == self.type:
84            pass
85        elif GeneratorType.guid_string == self.type:
86            pass
87        elif GeneratorType.reusable_integer == self.type:
88            raise NotImplementedError
89        else:
90            raise RuntimeError('Wrong `self.type` value')
def clear(self):
92    def clear(self):
93        self.counter = 0