Skip to main content

Lightweight wrapper for creating namedtuples.

Project description

https://travis-ci.org/brennv/namedtupled.svg?branch=master https://img.shields.io/badge/python-2.7%2C%203.4%2C%203.5-blue.svg https://img.shields.io/codecov/c/github/brennv/namedtupled.svg

namedtupled

namedtuples are immutable, performant and classy. namedtupled is a lightweight wrapper for recursively creating namedtuples from nested dicts, lists, json and yaml. Thanks to hangtwenty.

Installation

pip install namedtupled

Getting started

import namedtupled

data = {'binks': {'says': 'meow'}}
cat = namedtupled.map(data)

cat  # NT(binks=NT(says='meow'))

cat.binks.says  # 'meow'

Usage

Create namedtuples with methods: map, json, yaml, zip, env and helper method ignore.

Optionally name namedtuples by passing a ‘name’ argument to any method, the default name is simply ‘NT’.

data = {'binks': {'says': 'meow'}}
cat = namedtupled.map(data, name='Cat')

cat  # Cat(binks=NT(says='meow'))

map()

Recursively convert mappings like nested dicts, or lists of dicts, into to namedtuples.

args: mapping, name=‘NT’

From dict:

data = {'binks': {'says': 'meow'}}
cat = namedtupled.map(data)

cat.binks.says  # 'meow'

From list:

data = [{'id': 'binks', 'says': 'meow'}, {'id': 'cedric', 'says': 'prrr'}]
cats = namedtupled.map(data)

cats[1].says  # 'prrr'

json()

Map namedtuples from json data.

args: data=None, path=None, name=‘NT’

Inline:

data = """{"binks": {"says": "meow"}}"""
cat = namedtupled.json(data)

cat.binks.says  # 'meow'

Or specify path for a json file:

cat = namedtupled.json(path='cat.json')

cat.binks.says  # 'meow'

yaml()

Map namedtuples from yaml data.

args: data=None, path=None, name=‘NT’

Inline:

data = """
binks:
  says: meow
"""
cat = namedtupled.yaml(data)

cat.binks.says  # 'meow'

Or specify path for a yaml file:

cat = namedtupled.yaml(path='cat.yaml')

cat.binks.says  # 'meow'

zip()

Map namedtuples given a pair of key, value lists.

args: keys=[], values=[], name=‘NT’

Example:

keys, values = ['id', 'says'], ['binks', 'prrr']
cat = namedtupled.zip(keys, values)

cat.says  # 'prrr'

env()

Returns a namedtuple from a list of environment variables. If not found in shell, gets input with input or getpass.

args: keys=[], name=‘NT’, use_getpass=False

In shell:

export USERNAME="binks"
export APIKEY="c4tnip!"

Then in python:

variables = ['USERNAME', 'APIKEY']
env = namedtupled.env(variables)

env.USERNAME  # 'binks'

ignore()

Use ignore to prevent a mapping from being converted to a namedtuple.

args: mapping

Example usage:

data = {'binks': namedtupled.ignore({'says': 'meow'})}
cat = namedtupled.map(data)

cat.binks  # {'says': 'meow'}

Development

PRs welcome, tests run with:

pip install pytest pytest-cov pytest-datafiles
python -m pytest --cov=namedtupled/ tests

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

namedtupled-0.1.3.tar.gz (3.9 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