pyreflat

pyreflat - flatten python dict or json


Keywords
python, flatten, dict, json, serialize, serialization, persitence, config, pickel, flattening, persistence
Licenses
AGPL-3.0/GPL-3.0+
Install
pip install pyreflat==0.0.5

Documentation

Code style: black

pyreflat - flatten python dict support

transform a python dict to a flat structure and back

features:

  • 2-way transformation
    • from dict to flatten
    • from flaten to dict
  • 1-way transformation (new in version v0.0.3)
    • TerminalWriter
      • collects all terminal values in a flat list
    • AnynomusPathWriter
      • where path is stripped off any index information for latter seaching on a more abstract level see also test_search_sorted
  • supported types
    • bool
    • int
    • float
    • complex
    • list
    • set
    • tupel
    • dict
  • support for nested heterogeneous types in list
  • support for list in lists
  • no dependencies
  • pure python

how to use

refer to sample.py

code:

from pyreflat import FlatFile # new in version v0.0.2

fnam = "test-file.txt"
test_dict = { "a": 1 }

with FlatFile(fnam, "w", converter=ConvertUTF8) as f:
    f.write(test_dict, writer=TabFlatWriter)  # new in version v0.0.4

with FlatFile(fnam, converter=ConvertUTF8) as f:
    dic = f.read(reader=TabFlatReader) # new in version v0.0.4

how it works

with using techniques from compiler construction a stream of tokens are produced which can be saved into a simple file.

the tokens can later send to an interpreter for constructing the former dict again

use REPL to find out more:

toknizr = DictTokenizer(emitType=True)
toknizr.from_dict({"a":1,"b":2.0,"c":[1,2,3],"d":[ [1], [2,2], [3,3,3] ]})

alltokens = list(toknizr)  # iterator result as list

Platform

Tested on Python3, and Linux.

Development status

alpha state. the API or logical call flow might change without prior notice.

read CHANGELOG for latest, or upcoming news.

limitations

with FlatReader FlatWriter, and FlatFile dict key names can not contain special character sequences.

such as:

  • #k#,#i#,#s#,#t#,#c#, or #v#
  • #[k|i|s|c|v]# (normalzied)

use TabFlatWriter and TabFlatReader (new in version v0.0.4)

installation

available on pypi. install with:

python3 -m pip install pyreflat