[^ 1]: A memo when I forget
A piece of data (eg, clerk) has a tree structure (department clerk) and has various attributes (eg, clerk, section chief, clerk). Explains how to set and use attributes in groups (eg departments and sections).
Define and use two types of information: a tree-structured ** configuration ** and an attribute (** name **).
import re, toml
toml._groupname_re = re.compile('^[A-Za-z0-9-Hmm--One-龥_*-]+$')
tml = toml.loads("""\
[Constitution.Part A]
Section L="X clerk,Y clerk"
Section M="Z clerk"
[Constitution.Part B]
Section N="W clerk"
[name.Part A]
Director="Mr. A"
[name.Part A.Section L]
Manager="Mr. L"
[name.Part A.Section L.X clerk]
Chief="Mr. X"
[name.Part A.Section L.Y clerk]
Chief="Mr. Y"
[name.Part A.Section M]
Manager="Mr. M"
[name.Part A.Section M.Z clerk]
Chief="Mr. Z"
[name.Part B]
Director="Mr. B"
[name.Part B.Section N]
Manager="Mr. N"
[name.Part B.Section N.W clerk]
Chief="Mr. W"
""")
tml['Constitution']
>>>
{'Part A': {'Section L': 'X clerk,Y clerk',
'Section M': 'Z clerk'},
'Part B': {'Section N': 'W clerk'}}
tml['name']
>>>
{'Part A': {'Section L': {'X clerk': {'Chief': 'Mr. X'},
'Y clerk': {'Chief': 'Mr. Y'},
'Manager': 'Mr. L'},
'Section M': {'Z clerk': {'Chief': 'Mr. Z'},
'Manager': 'Mr. M'},
'Director': 'Mr. A'},
'Part B': {'Section N': {'W clerk': {'Chief': 'Mr. W'},
'Manager': 'Mr. N'},
'Director': 'Mr. B'}}
Create configuration data using ortoolpy.MultiKeyDict. If you loop with for, you can access all data (all related). Note that MultiKeyDict is cached and cannot be updated. (Assuming that it is read first like the setting information and does not change)
from ortoolpy import MultiKeyDict
iskey = lambda x: x[-1] in 'Department clerk' # 'Department clerk'Change the key ending with to a new MultiKeyDict
conv = lambda x: ((s,None) for s in x.split(','))
Constitution= MultiKeyDict(tml['Constitution'], iskey=iskey, conv=conv, extend=True)
for ky in configuration:
print(ky)
>>>
('Part A', 'Section L', 'X clerk')
('Part A', 'Section L', 'Y clerk')
('Part A', 'Section M', 'Z clerk')
('Part B', 'Section N', 'W clerk')
Create attribute data (name) with MultiKeyDict and access using configuration data as a key.
name= MultiKeyDict(tml['name'], iskey=iskey)
for ky in configuration:
print(' '.join(ky))
for ky2,name in name[ky].items():
print(f' %s: %s'%(ky2[-1],name))
>>>
Department A, Section L, Section X
Director:Mr. A
Manager:Mr. L
Chief:Mr. X
Department A Section L Section Y Section
Director:Mr. A
Manager:Mr. L
Chief:Mr. Y
Department A M Section Z Section
Director:Mr. A
Manager:Mr. M
Chief:Mr. Z
Department B Section N Section W Section
Director:Mr. B
Manager:Mr. N
Chief:Mr. W
for ky in configuration:
print(' '.join(ky))
dc =name.get_list(ky, True)
print(f'Director: %s'%dc['Director'][0])
print(f'Manager: %s'%dc['Manager'][0])
print(f'Chief: %s'%dc['Chief'][0])
>>>
Department A, Section L, Section X
Director:Mr. A
Manager:Mr. L
Chief:Mr. X
Department A Section L Section Y Section
Director:Mr. A
Manager:Mr. L
Chief:Mr. Y
Department A M Section Z Section
Director:Mr. A
Manager:Mr. M
Chief:Mr. Z
Department B Section N Section W Section
Director:Mr. B
Manager:Mr. N
Chief:Mr. W
As shown below, the manager defined in [Name.A Department] is also "Name.A Department.L Section.X Section" or [Name.A Department.L Section.Y Section] "Manager =" Mr. A "". I was able to refer to it.
[Name.Part A] Director = "Mr. A"
In this way, you can get the attribute from the end data (participant) without worrying about where the attribute was defined in the tree structure.
that's all
Recommended Posts