Skip to main content

Function to get value from unknown structure

Project description

la-deep-get

Function to get value that is deep inside another structure.

install

pip install la-deep-get

syntax

dget(structure, *args, default=None)

structure - Structure to go through
*args - Each field to be passed as key
default - The value to return in case it fail to access any of the keys (default is None)

example

Let's say that you have dictionary with product information and you want all active big images.

product = {
    "name": "really cool product",
    "images": {
        "active: {
            "small": [
                "https://www.smallimage.com",
                "https://www.smallimage.com",
                "https://www.smallimage.com",
            ],
            "big": [
                "https://www.bigimage.com",
                "https://www.bigimage.com",
                "https://www.bigimage.com",
            ]
        },
        "disabled": {
            "small": [
                "https://www.notcool.com",
                "https://www.notcool.com",
                "https://www.notcool.com",
            ],
            "big": [
                "https://www.notcool.com",
                "https://www.notcool.com",
                "https://www.notcool.com",
            ]
        }
    }
}

The easiest way would be to use try except and set a default value in case of errors.

try:
    big_images = product["images"]["active"]["big"]
except:
    big_images = []

Or you could use multiple gets.

big_images = product.get("images", {}).get("active", {}).get("big", [])

Either way can be quite annoying to write and complicated. If the structure gets bigger and bigger (like a json from a complicated website), using dget may simplify this situation.

big_images = dget(product, "images", "active", "big", default=[])

usage

All that you need to know is that you will navigate like normally do with dictionaries or lists (using square brackets) but it will not raise exceptions if one field doesn't exist along the way.

list_ = [
    [1,2,3],
    [4,5,6],
    [7,8,9],
    [
        [10, 11, 12],
        [13, 14, 15],
        [16, 17, 18],
    ]
]

# Same as: list_[3][1][2]
value = dget(list_, 3, 1, 2)

print(value) # 15
list_ = {
    "hello": {
        "annoing": {
            "dictionary": "here"
        }
    }
}

# Same as: list_["hello"]["annoing"]["dictionary"]
value = dget(list_, "hello", "annoing", "dictionary")

print(value) # "here"

It really doesn't care what strcuture it is.

list_ = {
    "first": {
        5: [
            "nice"
        ]
    }
}

# Same as: list_["first"][5][0]
value = dget(list_, "first", 5, 0)

print(value) # "nice"

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

la-deep-get-0.0.3.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

la_deep_get-0.0.3-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file la-deep-get-0.0.3.tar.gz.

File metadata

  • Download URL: la-deep-get-0.0.3.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

File hashes

Hashes for la-deep-get-0.0.3.tar.gz
Algorithm Hash digest
SHA256 30f4537343c7dfe95516f923403165fd9ef8606e33babdd67a549102c76c3145
MD5 9a1d77aa55e7eca2f5fff99a247afcd0
BLAKE2b-256 3ac5c69960e924873044aa34a447b12c4c99af6458b3618bc6a58a78e98c39d9

See more details on using hashes here.

Provenance

File details

Details for the file la_deep_get-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: la_deep_get-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1

File hashes

Hashes for la_deep_get-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1442170ee44909b057c157b267d471cdd8ddbac24aa1d6ea9f2e643333f4b739
MD5 471afb8745f9e6c070d4abff23cdd93c
BLAKE2b-256 e66b973fe3a829aa9ec345eff8ed0122f9ceeaee00e7db5735df67aa66bb2ba9

See more details on using hashes here.

Provenance

Supported by

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