Skip to main content

Dotted notation parser with pattern matching

Project description

Dotted

Sometimes you want to fetch data from a deeply nested data structure. Dotted notation helps you do that.

Let's say you have a dictionary containing a dictionary containing a list and you wish to fetch the ith value from that nested list.

>>> import dotted
>>> d = {'hi': {'there': [1, 2, 3]}}
>>> dotted.get(d, 'hi.there[1]')
2

Grammar

Dotted notation looks similar to python. Both dot fields and slot fields, e.g. brackets, call __getitem__ internally. A dot field expects to see a dictionary-like object. A slot field is biased towards sequences (like lists, tuples, and strs) but can act on dicts as well. Dotted also supports slicing notation as well as transforms discussed below.

Patterns

You can use dotted for pattern matching. You can match to wildcards or regular expressions.

>>> import dotted
>>> d = {'hi': {'there': [1, 2, 3]}, 'bye': {'there': [4, 5, 6]}}
>>> dotted.get(d, '*.there[2]')
(3, 6)
>>> dotted.get(d, '/h.*/.*')
([1, 2, 3],)

Dotted will return all values that match the pattern(s).

Slicing

Slicing is also supported. Dotted slicing works like python slicing and all that entails.

>>> import dotted
>>> d = {'hi': {'there': [1, 2, 3]}, 'bye': {'there': [4, 5, 6]}}
>>> dotted.get(d, 'hi.there[::2]')
[1, 3]
>>> dotted.get(d, '*.there[1:]')
([2, 3], [5, 6])

The '+' operator

Both slots and slices support the '+' operator which refers to the end of sequence. You may append an item or slice to the end a sequence.

>>> import dotted
>>> d = {'hi': {'there': [1, 2, 3]}, 'bye': {'there': [4, 5, 6]}}
>>> dotted.update(d, '*.there[+]', 8)
{'hi': {'there': [1, 2, 3, 8]}, 'bye': {'there': [4, 5, 6, 8]}}
>>> dotted.update(d, '*.there[+:]', [999])
{'hi': {'there': [1, 2, 3, 8, 999]}, 'bye': {'there': [4, 5, 6, 8, 999]}}

Transforms

You can optionally add transforms to the end of dotted notation. These will be applied on get and update. Transforms are separated by the | operator and multiple may be chained together. Transforms may be parameterized using the : operator.

>>> import dotted
>>> d = [1, '2', 3]
>>> dotted.get(d, '[1]')
'2'
>>> dotted.get(d, '[1]|int')
2
>>> dotted.get(d, '[0]|str:number=%d')
'number=1'

You may register new transforms via either register or the @transform decorator. Look at transforms.py for preregistered.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

dotted-notation-0.0.23.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

dotted_notation-0.0.23-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file dotted-notation-0.0.23.tar.gz.

File metadata

  • Download URL: dotted-notation-0.0.23.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for dotted-notation-0.0.23.tar.gz
Algorithm Hash digest
SHA256 95b706bdce27eb3ddb2c43ccb677ada99df3f488edd66b0a1f73eccfec20d4c5
MD5 ed72e131c9b5c9994a9cfe4509223c4a
BLAKE2b-256 d42669f7dee6de4fe3f0027ce768b6ab9da3aa1d73025592747ecd7e996b6850

See more details on using hashes here.

File details

Details for the file dotted_notation-0.0.23-py3-none-any.whl.

File metadata

  • Download URL: dotted_notation-0.0.23-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for dotted_notation-0.0.23-py3-none-any.whl
Algorithm Hash digest
SHA256 422768a9abf8f3fa5c42608f30b7dbe361b8fc38044f93df0968b2772af3c83a
MD5 7eba2cff25dd5594e042dcb0dd55ea18
BLAKE2b-256 84856b608e59f43b273e7e1d5ebbb556e44ff917704e035e5f6b368be33bc5b0

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