|
1 | 1 | # numdb.py - module for handling hierarchically organised numbers
|
2 | 2 | #
|
3 |
| -# Copyright (C) 2010-2019 Arthur de Jong |
| 3 | +# Copyright (C) 2010-2021 Arthur de Jong |
4 | 4 | #
|
5 | 5 | # This library is free software; you can redistribute it and/or
|
6 | 6 | # modify it under the terms of the GNU Lesser General Public
|
|
39 | 39 |
|
40 | 40 | To split the number and get properties for each part:
|
41 | 41 |
|
42 |
| ->>> dbfile.info('01006') == [ |
43 |
| -... ('0', {'prop1': 'foo'}), |
44 |
| -... ('100', {'prop2': 'bar'}), |
45 |
| -... ('6', {}), |
46 |
| -... ] |
47 |
| -True |
48 |
| ->>> dbfile.info('02006') == [ |
49 |
| -... ('0', {'prop1': 'foo'}), |
50 |
| -... ('200', {'prop2': 'bar', 'prop3': 'baz'}), |
51 |
| -... ('6', {}), |
52 |
| -... ] |
53 |
| -True |
54 |
| ->>> dbfile.info('03456') == [ |
55 |
| -... ('0', {'prop1': 'foo'}), |
56 |
| -... ('345', {'prop2': 'bar', 'prop3': 'baz'}), |
57 |
| -... ('6', {}), |
58 |
| -... ] |
59 |
| -True |
60 |
| ->>> dbfile.info('902006') == [ |
61 |
| -... ('90', {'prop1': 'booz'}), |
62 |
| -... ('20', {'prop2': 'foo'}), |
63 |
| -... ('06', {}), |
64 |
| -... ] |
65 |
| -True |
66 |
| ->>> dbfile.info('909856') == [ |
67 |
| -... ('90', {'prop1': 'booz'}), |
68 |
| -... ('985', {'prop2': 'fooz'}), |
69 |
| -... ('6', {}), |
70 |
| -... ] |
71 |
| -True |
72 |
| ->>> dbfile.info('9889') == [ |
73 |
| -... ('98', {'prop1': 'booz'}), |
74 |
| -... ('89', {'prop2': 'foo'}), |
75 |
| -... ] |
76 |
| -True |
77 |
| ->>> dbfile.info('633322') == [ |
78 |
| -... ('6', {'prop1': 'boo'}), |
79 |
| -... ('333', {'prop2': 'bar', 'prop3': 'baz', 'prop4': 'bla'}), |
80 |
| -... ('22', {}), |
81 |
| -... ] |
82 |
| -True |
| 42 | +>>> import pprint |
| 43 | +>>> pprint.pprint(dbfile.info('01006')) |
| 44 | +[('0', {'prop1': 'foo'}), ('100', {'prop2': 'bar'}), ('6', {})] |
| 45 | +>>> pprint.pprint(dbfile.info('02006')) |
| 46 | +[('0', {'prop1': 'foo'}), ('200', {'prop2': 'bar', 'prop3': 'baz'}), ('6', {})] |
| 47 | +>>> pprint.pprint(dbfile.info('03456')) |
| 48 | +[('0', {'prop1': 'foo'}), ('345', {'prop2': 'bar', 'prop3': 'baz'}), ('6', {})] |
| 49 | +>>> pprint.pprint(dbfile.info('902006')) |
| 50 | +[('90', {'prop1': 'booz'}), ('20', {'prop2': 'foo'}), ('06', {})] |
| 51 | +>>> pprint.pprint(dbfile.info('909856')) |
| 52 | +[('90', {'prop1': 'booz'}), ('985', {'prop2': 'fooz'}), ('6', {})] |
| 53 | +>>> pprint.pprint(dbfile.info('9889')) |
| 54 | +[('98', {'prop1': 'booz'}), ('89', {'prop2': 'foo'})] |
| 55 | +>>> pprint.pprint(dbfile.info('633322')) |
| 56 | +[('6', {'prop1': 'boo'}), ('333', {'prop2': 'bar', 'prop3': 'baz', 'prop4': 'bla'}), ('22', {})] |
83 | 57 |
|
84 | 58 | """
|
85 | 59 |
|
@@ -114,41 +88,27 @@ def __init__(self):
|
114 | 88 | """Construct an empty database."""
|
115 | 89 | self.prefixes = []
|
116 | 90 |
|
117 |
| - @staticmethod |
118 |
| - def _merge(results): |
119 |
| - """Merge the provided list of possible results into a single result |
120 |
| - list (this is a generator).""" |
121 |
| - # expand the results to all have the same length |
122 |
| - ml = max(len(x) for x in results) |
123 |
| - results = [x + (ml - len(x)) * [None] |
124 |
| - for x in results] |
125 |
| - # go over each part |
126 |
| - for parts in zip(*results): |
127 |
| - # regroup parts into parts list and properties list |
128 |
| - partlist, proplist = list(zip(*(x for x in parts if x))) |
129 |
| - part = min(partlist, key=len) |
130 |
| - props = {} |
131 |
| - for p in proplist: |
132 |
| - props.update(p) |
133 |
| - yield part, props |
134 |
| - |
135 | 91 | @staticmethod
|
136 | 92 | def _find(number, prefixes):
|
137 | 93 | """Lookup the specified number in the list of prefixes, this will
|
138 | 94 | return basically what info() should return but works recursively."""
|
139 | 95 | if not number:
|
140 | 96 | return []
|
141 |
| - results = [] |
142 |
| - if prefixes: |
143 |
| - for length, low, high, props, children in prefixes: |
144 |
| - if low <= number[:length] <= high and len(number) >= length: |
145 |
| - results.append([(number[:length], props)] + |
146 |
| - NumDB._find(number[length:], children)) |
147 |
| - # not-found fallback |
148 |
| - if not results: |
149 |
| - return [(number, {})] |
150 |
| - # merge the results into a single result |
151 |
| - return list(NumDB._merge(results)) |
| 97 | + part = number |
| 98 | + properties = {} |
| 99 | + next_prefixes = [] |
| 100 | + # go over prefixes and find matches |
| 101 | + for length, low, high, props, children in prefixes: |
| 102 | + if len(part) >= length and low <= part[:length] <= high: |
| 103 | + # only use information from the shortest match |
| 104 | + if length < len(part): |
| 105 | + part = part[:length] |
| 106 | + properties = {} |
| 107 | + next_prefixes = [] |
| 108 | + properties.update(props) |
| 109 | + next_prefixes.extend(children or []) |
| 110 | + # return first part and recursively find next matches |
| 111 | + return [(part, properties)] + NumDB._find(number[len(part):], next_prefixes) |
152 | 112 |
|
153 | 113 | def info(self, number):
|
154 | 114 | """Split the provided number in components and associate properties
|
|
0 commit comments