Handling of sparse tree-structured attributes (Python)

Introduction [^ 1]

[^ 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).

Overview

Define and use two types of information: a tree-structured ** configuration ** and an attribute (** name **).

Original data (TOML)

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'}}

Configuration data creation

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')

Attribute data confirmation

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

Another way

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

Handling of sparse tree-structured attributes (Python)
python> Handling of 2D arrays
Handling of python on mac
Handling of JSON files in Python
Introduction of Python
Python Error Handling
Basics of Python ①
Basics of python ①
Python exception handling
Copy of python
Python timezone handling
Python exception handling
Introduction of Python
How to specify attributes with Mock of python
[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)
[Data science memorandum] Handling of missing values ​​[python]
[Python] Chapter 02-06 <Supplement> Basics of Python programs (handling of numerical values)
[Python] Operation of enumerate
List of python modules
Handling yaml with python
Unification of Python environment
Copy of python preferences
Handling json in python
Basics of Python scraping basics
[python] behavior of argmax
Usage of Python locals ()
the zen of Python
Installation of Python 3.3 rc1
Python, about exception handling
Python decimal point handling
# 4 [python] Basics of functions
Hexadecimal handling in Python 3
Planar skeleton analysis with Python (4) Handling of forced displacement
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Various processing of Python
Notes on handling large amounts of data with python + pandas
Comparison of data frame handling in Python (pandas), R, Pig
[Python] Correct usage of map
Python exception handling (Python learning memo ⑥)
Handling of absolute paths of os.path.join
Towards the retirement of Python2
Summary of python file operations
Summary of Python3 list operations
Python --Quick start of logging
Recommendation of binpacking library of python
[python] Value of function object (?)
Handling of quotes in [bash]
Automatic update of Python module
Python --Check type of values
[Python] Etymology of python function names
Handling sparse matrices in Scipy
About the ease of Python
Static analysis of Python programs
About various encodings of Python 3
Equivalence of objects in Python
Introduction of activities applying Python