2021-03-02 15:24:56 +01:00
|
|
|
from collections import Counter
|
|
|
|
|
|
|
|
REGION_ID_TEMPLATE = 'region_%04d'
|
|
|
|
LINE_ID_TEMPLATE = 'region_%04d_line_%04d'
|
|
|
|
|
|
|
|
class EynollahIdCounter():
|
|
|
|
|
|
|
|
def __init__(self, region_idx=0, line_idx=0):
|
|
|
|
self._counter = Counter()
|
2023-08-18 02:52:49 +02:00
|
|
|
self._initial_region_idx = region_idx
|
|
|
|
self._initial_line_idx = line_idx
|
2021-03-12 18:39:27 +01:00
|
|
|
self.reset()
|
|
|
|
|
|
|
|
def reset(self):
|
2023-08-18 02:52:49 +02:00
|
|
|
self.set('region', self._initial_region_idx)
|
|
|
|
self.set('line', self._initial_line_idx)
|
2021-03-02 15:24:56 +01:00
|
|
|
|
|
|
|
def inc(self, name, val=1):
|
|
|
|
self._counter.update({name: val})
|
|
|
|
|
|
|
|
def get(self, name):
|
|
|
|
return self._counter[name]
|
|
|
|
|
|
|
|
def set(self, name, val):
|
|
|
|
self._counter[name] = val
|
|
|
|
|
2021-03-02 17:41:45 +01:00
|
|
|
def region_id(self, region_idx=None):
|
2021-03-11 19:44:42 +01:00
|
|
|
if region_idx is None:
|
2021-03-02 17:41:45 +01:00
|
|
|
region_idx = self._counter['region']
|
|
|
|
return REGION_ID_TEMPLATE % region_idx
|
|
|
|
|
|
|
|
def line_id(self, region_idx=None, line_idx=None):
|
2021-03-11 19:44:42 +01:00
|
|
|
if region_idx is None:
|
2021-03-02 17:41:45 +01:00
|
|
|
region_idx = self._counter['region']
|
2021-03-11 19:44:42 +01:00
|
|
|
if line_idx is None:
|
2021-03-02 17:41:45 +01:00
|
|
|
line_idx = self._counter['line']
|
|
|
|
return LINE_ID_TEMPLATE % (region_idx, line_idx)
|
|
|
|
|
2021-03-02 15:24:56 +01:00
|
|
|
@property
|
|
|
|
def next_region_id(self):
|
|
|
|
self.inc('region')
|
|
|
|
self.set('line', 0)
|
2021-03-02 17:41:45 +01:00
|
|
|
return self.region_id()
|
2021-03-02 15:24:56 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def next_line_id(self):
|
|
|
|
self.inc('line')
|
2021-03-02 17:41:45 +01:00
|
|
|
return self.line_id()
|