counter: allow arbitrary line/region id

pull/28/head
Konstantin Baierer 4 years ago
parent fffa207658
commit 56b688befe

@ -19,13 +19,25 @@ class EynollahIdCounter():
def set(self, name, val): def set(self, name, val):
self._counter[name] = val self._counter[name] = val
def region_id(self, region_idx=None):
if not region_idx:
region_idx = self._counter['region']
return REGION_ID_TEMPLATE % region_idx
def line_id(self, region_idx=None, line_idx=None):
if not region_idx:
region_idx = self._counter['region']
if not line_idx:
line_idx = self._counter['line']
return LINE_ID_TEMPLATE % (region_idx, line_idx)
@property @property
def next_region_id(self): def next_region_id(self):
self.inc('region') self.inc('region')
self.set('line', 0) self.set('line', 0)
return REGION_ID_TEMPLATE % self._counter['region'] return self.region_id()
@property @property
def next_line_id(self): def next_line_id(self):
self.inc('line') self.inc('line')
return LINE_ID_TEMPLATE % (self._counter['region'], self._counter['line']) return self.line_id()

@ -8,6 +8,8 @@ def test_counter_string():
assert c.next_line_id == 'region_0002_line_0001' assert c.next_line_id == 'region_0002_line_0001'
assert c.next_region_id == 'region_0003' assert c.next_region_id == 'region_0003'
assert c.next_line_id == 'region_0003_line_0001' assert c.next_line_id == 'region_0003_line_0001'
assert c.region_id(999) == 'region_0999'
assert c.line_id(999, 888) == 'region_0999_line_0888'
def test_counter_init(): def test_counter_init():
c = EynollahIdCounter(region_idx=2) c = EynollahIdCounter(region_idx=2)

Loading…
Cancel
Save