Skip to main content

Dict operation utilities including iteration, getter/setter makers, and attribute access

Project description

k3dict

Action-CI Documentation Status Package

It provides with several dict operation functions.

k3dict is a component of pykit3 project: a python3 toolkit set.

k3dict

It provides with several dict operation functions.

Status

This library is considered production ready.

Install

pip install k3dict

Synopsis

import k3dict

mydict = {'a':
              {'a.a': 'v-a.a',
               'a.b': {'a.b.a': 'v-a.b.a'},
               'a.c': {'a.c.a': {'a.c.a.a': 'v-a.c.a.a'}}
               }
          }

# depth-first iterative the dict
for rst in k3dict.depth_iter(mydict):
    print(rst)

# output:
#     (['a', 'a.c', 'a.c.a', 'a.c.a.a'], 'v-a.c.a.a')
#     (['a', 'a.b', 'a.b.a'], 'v-a.b.a')
#     (['a', 'a.a'], 'v-a.a')

for rst in k3dict.breadth_iter(mydict):
    print(rst)

# output:
#     (['a'],                            {'a.c': {'a.c.a': {'a.c.a.a': 'v-a.c.a.a'}}, 'a.b': {'a.b.a': 'v-a.b.a'}
#                                           , 'a.a': 'v-a.a'})
#     (['a', 'a.a'],                     'v-a.a')
#     (['a', 'a.b'],                     {'a.b.a': 'v-a.b.a'})
#     (['a', 'a.b', 'a.b.a'],            'v-a.b.a')
#     (['a', 'a.c'],                     {'a.c.a': {'a.c.a.a': 'v-a.c.a.a'}})
#     (['a', 'a.c', 'a.c.a'],            {'a.c.a.a': 'v-a.c.a.a'})
#     (['a', 'a.c', 'a.c.a', 'a.c.a.a'], 'v-a.c.a.a')
#

records = [
    {"event": 'log in',
     "time": {"hour": 10, "minute": 30, }, },

    {"event": 'post a blog',
     "time": {"hour": 10, "minute": 40, }, },

    {"time": {"hour": 11, "minute": 20, }, },

    {"event": 'log out',
     "time": {"hour": 11, "minute": 20, }, },
]

get_event = k3dict.make_getter('event', default="NOTHING DONE")
get_time = k3dict.make_getter('time.$field')

for record in records:

    ev = get_event(record)

    tm = "%d:%d" % (get_time(record, {"field": "hour"}),
                    get_time(record, {"field": "minute"}))

    print("{ev:<12}   at {tm}".format(ev=ev, tm=tm))

# output:
# log in         at 10:30
# post a blog    at 10:40
# NOTHING DONE   at 11:20
# log out        at 11:20

Author

Zhang Yanpo (张炎泼) drdr.xp@gmail.com

Copyright and License

The MIT License (MIT)

Copyright (c) 2015 Zhang Yanpo (张炎泼) drdr.xp@gmail.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

k3dict-0.1.9.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

k3dict-0.1.9-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file k3dict-0.1.9.tar.gz.

File metadata

  • Download URL: k3dict-0.1.9.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for k3dict-0.1.9.tar.gz
Algorithm Hash digest
SHA256 940c68f5aa58d0cc3a3273ae3c977e7203c6032c78a7787fee9500f415cf2c86
MD5 7b59d3f25b4ca32a3ac7d99cd9269a43
BLAKE2b-256 02a18cf6e1cbedfee5964b810cf0fa3c0ffaa5d5ab58b68f8def66e2f8024ce2

See more details on using hashes here.

File details

Details for the file k3dict-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: k3dict-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for k3dict-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 6373c5a1427e840b59e19c1d8ec3a7645af957f460d82365a0274c0d108e2ed6
MD5 026fb678ffed7575c4439bc3d3965a0c
BLAKE2b-256 79b1d49b893eac7b0892f67549a9854f5f5fe5c0584df683ea1a32e5ce5035a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page