Skip to main content

Short getters for python

Project description

“Safe” attribute getter operators for python. Useful when exploring data in a REPL for debugging.

Usage

import k
name = k.name(user)

Works on object attributes and string dictionary keys. List indexes, too:

first_message = k.messages[0](user)

But if it encounters a list with a non-list key, it will map over the list:

names = k.name(users)
message_bodies = k.messages.body(users)  # list of lists [[body1, b2], [b3, b4], ..]

If you’d like you can flatten lists of lists:

mbs = k.messages(flatten=True).body(users)  # list [body1, b2, b3, b4, ..]

If you access a non-present attribute, instead of throwing an exception, it will just return None. This is helpful if objects have heterogeneous attributes.

coaches = k.coach(users)  # [coach1, None, coach2]

You can combine two k instances with + and it will merge them into a dictionary keyed by the path.

(k.first_name + k.last_name)(users)  # [{'first_name':'foo', 'last_name':'bar'}, ..]

k._ will return ‘self’, which can be helpful when doing combinations.

user = k._ + k.coach.id(user)

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

key-0.1.tar.gz (1.7 kB view hashes)

Uploaded Source

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