Skip to main content

Understreck

https://img.shields.io/pypi/v/understreck.svg https://travis-ci.com/cfarvidson/understreck.svg?branch=master Documentation Status Code style: black downloads

A collection of nice utility functions for python

Features

  • Perform a safe get on a nested dictionary with the nested_get function

  • Split a list into chunks

  • Filter a list of dictionaries

  • Strip indents from multiline strings

Examples

Get example:

import understreck as _

test_dictionary = {
    "foo": {
        "second_level": {"third_level": "it works", "third_level_sibling": False},
        "second_level_list": ["Hello", "World", {"planet": "Earth"}, ["Hello", "World", {"planet": "jupiter"}, ]],
    }
}

# Using dot delimited strings
result = _.get(test_dictionary, "foo.second_level.third_level")  # result == "it works"
result = _.get(test_dictionary, "foo.second_level.DOES_NOT_EXIST")  # result == None

# Using a list or tuple
result = _.get(test_dictionary, ["foo", "second_level", "third_level"])  # result == "it works"
result = _.get(test_dictionary, ["foo", "second_level", "DOES_NOT_EXIST"])  # result == None

# Getting elements in list

result = _.get(test_dictionary, "foo.second_level_list[0]")  # result == "Hello"
result = _.get(test_dictionary, "foo.second_level_list[1]")  # result == "World"
result = _.get(test_dictionary, "foo.second_level_list[2].planet")  # result == "Earth"

# Getting a property in a nested list
nested_list = {
                "foo": {
                    "bar": [
                        "x", [
                            "first", "second", {"name": "Hello World"}
                        ]
                    ]
                }
              }

result = _.get(nested_list, "foo.bar[1][2].name")  # result == "Hello World"

Chunks example:

import understreck as _

to_chunk = ["one", "two", "three", "four", "five"]
result = _.chunks.split(to_chunk, 2)  # result == [["one", "two", "three"], ["four", "five"]]

Filter example:

import understreck as _

users = [
        {"user": "barney", "age": 36, "active": True},
        {"user": "fred", "age": 40, "active": False},
    ]

# Using a lambda function
result = _.filter(users, lambda x: not x.get("active"))  # result == [{"user": "fred", "age": 40, "active": False}]

# Using partial dictionary
result = _.filter(users, {"age": 36, "active": True})  # result == [{"user": "barney", "age": 36, "active": True}]

# Using a list with a property name and value
result = _.filter(users, ["active", False])  # result == [{"user": "fred", "age": 40, "active": False}]

# Using a list with a property name. The value must be truthy.
result = _.filter(users, ["active"])  # result == [{"user": "barney", "age": 36, "active": True}]

Strip indents example:

import understreck as _

def some_function():
    to_strip = """This is a
    multi-line
    string"""

    _.strip(to_strip)  # "This is a\nmulti-line\nstring"

Credits

I have to credit the Lodash project for inspiration!

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

Unreleased

1.0.0 (2020-03-03)

  • Get elements from nested lists

0.5.1 (2019-11-09)

  • Updates dev dependencies

0.5.0 (2019-05-03)

  • Updates dev dependencies

  • Moves to the new repository

0.4.0 (2019-05-03)

  • Adds Understreck.strip that strips indents

0.3.0 (2019-05-03)

  • Adds Understreck.filter with inspired by https://lodash.com/docs/4.17.11#filter

  • Updated the travis button URL

  • Replaced nested_get with get in the README. (nested_get still works)

  • Black formatting

0.2.1 (2018-12-10)

  • Add Understreck.chunks

0.2.0 (2018-11-12)

  • Add get as an alias for nested get

0.1.1 (2018-11-02)

  • Update the readme

0.1.0 (2018-11-02)

  • First release on PyPI.

Metadata

Release files for understreck 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for understreck 1.0.0
File Size Uploaded Provenance Actions
understreck-1.0.0.tar.gz 27.3 kB Without provenance Details for understreck-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for understreck 1.0.0
File Interpreter ABI Platform Actions
understreck-1.0.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for understreck-1.0.0-py2.py3-none-any.whl

Total release size: 34.5 kB

Release files / understreck-1.0.0.tar.gz

Download URL understreck-1.0.0.tar.gz
Size 27.3 kB
Tags Source
SHA-256 checksum
How to use checksums
639241d07131376812788a47aa85c1f466222875c24fbcfdba73302a118fb23e
BLAKE2b-256 checksum
How to use checksums
3a8ade81ecf22a63a90fe3255f800c7cc948a9bad613439e1c8c3e3fa6f469bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.1

Release files / understreck-1.0.0-py2.py3-none-any.whl

Download URL understreck-1.0.0-py2.py3-none-any.whl
Size 7.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
81efea9205a47e549388cd0c71c084ff06b8be0fcc929e5db1408ea097d13595
BLAKE2b-256 checksum
How to use checksums
f2a8f6fde6a70b096a0b4938c2a13e14a1ce51e7ae22689a26204e2fc0e2c02d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.1

Security for understreck 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page