Skip to main content

Easy access to elements of collection/object structures

Project description

QuickPath

QuickPath is a package, which provides functions for easy access the elements of collection/object structures.

Motivating example

animals = [
  {
    'name': 'Wombat', 
    'avg_properties': {
      'height': {'value': 66, 'unit': 'cm'}, 
      'length':{'value': 108, 'unit': 'cm'},
      'weight': {'value': 27, 'unit': 'kg'}
    }
  },
  {
    'name': 'Duck', 
    'avg_properties': {
      'height': {'value': 62, 'unit': 'cm'}, 
      'weight': {'value': 1, 'unit': 'kg'}
    }
  },
  {
    'name': 'Dog', 
    'max_properties': {
      'height': {'value': 95, 'unit': 'cm'}, 
      'weight': {'value': 105, 'unit': 'kg'}
    }
  },
]

Let's query that above structure:

for animal in animals:
  print(animal["name"], 
        'average length', 
        animal["avg_properties"]["length"]["value"])

This code will abort with error as no the Duck has no length key. We have to add one more check.

for animal in animals:
  print(animal["name"], 
        'average length', 
        animal["avg_properties"]["length"]["value"] 
          if "length" in animal["avg_properties"] 
          else '-')

This improved code will still fail as Dog has only max_property key, we have to handle this situation too.

for animal in animals:
  if "avg_properties" in animal 
      and "length" in animal["avg_properties"]:
    print(animal["name"], 
          'average length', 
          animal["avg_properties"]["length"]["value"])
  else:
    print(animal["name"], 
          'avarage length', 
          "-")

The above scenarios can be simplified by quickpath:

from quickimport import getpath
for animal in animals:
  print(animal["name"], 
        'average length', 
        getpath(animal, 
                ("avg_properties", "length", "value"), 
                default='-'))

Alternatively, the keys can be represented as a single string:

from quickimport import getpaths
for animal in animals:
  print(animal["name"], 
        'average length', 
        getpaths(animal, 
                "avg_properties.length.value"), 
                default='-'))

Separator can be changed to any alternative characters:

from quickimport import getpaths
for animal in animals:
  print(animal["name"], 
        'average length', 
        getpaths(animal, 
                "avg_properties/length/value"), 
                default='-', 
                sep='/')

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

quickpath-0.9.3.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

quickpath-0.9.3-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file quickpath-0.9.3.tar.gz.

File metadata

  • Download URL: quickpath-0.9.3.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for quickpath-0.9.3.tar.gz
Algorithm Hash digest
SHA256 aaf6f2690dfbbcb1c9f101e2602d1fe64bdb5d0113f009dcc05923d93a3cbba4
MD5 9606a0c232b6e4ba14a24d6e2355bdbf
BLAKE2b-256 aac16241b5d7f33ca25b7ab95fcfbfaba14bfe72b059dfd9e3ca2352acf85aaa

See more details on using hashes here.

File details

Details for the file quickpath-0.9.3-py3-none-any.whl.

File metadata

  • Download URL: quickpath-0.9.3-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for quickpath-0.9.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d7d2150de72c1ac778346f622acb4ed1bba6784adce52d6b8a8799d4ef5c167f
MD5 a46ca45178421f5de1c9dffddfee5253
BLAKE2b-256 f8ec82ba38bda1b6d04d2d83a7c47951e1013d7c5ae91714cb361e4b31781fbc

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