Skip to main content

A handy library to manipulate dictionaries

Project description

Handy Dict

A library with some handy utils to deal with dictionaries that contain arrays

This file will become your README and also the index of your documentation.

Install

pip install handy-dict

How to use

Let's start with a dictionary

pikachu = {
  "name": "pikachu",
  "moves": [
    {
      "move": {
        "name": "mega-punch",
        "url": "https://pokeapi.co/api/v2/move/5/"
      }
    },
    {
      "move": {
        "name": "pay-day",
        "url": "https://pokeapi.co/api/v2/move/6/"
      }
    },
    {
      "move": {
        "name": "thunder-punch",
        "url": "https://pokeapi.co/api/v2/move/9/"
      }
    }
  ],
  "stats": [
    {
      "base_stat": 90,
      "effort": 2,
      "stat": {
        "name": "speed",
        "url": "https://pokeapi.co/api/v2/stat/6/"
      }
    },
    {
      "base_stat": 50,
      "effort": 0,
      "stat": {
        "name": "special-defense",
        "url": "https://pokeapi.co/api/v2/stat/5/"
      }
    }
  ]
}

apply_keyed

from handy_dict import apply_keyed

With handy-dict you can apply a function to the keys inside a dictionary, say you want to take name out of stat, go from something like this:

{
  "base_stat": 50,
  "effort": 0,
  "stat": {
    "name": "special-defense",
    "url": "https://pokeapi.co/api/v2/stat/5/"
  }
}

to this:

{
  "base_stat": 50,
  "name": "special-defense"
}

The function transform_stat is just a little helper that will transform the stats array in our pikachu dictionary and return a new array, that will replace the stats in a copy of the original dict:

def transform_stat(stat_array):
    return {
        stat["stat"]["name"]: stat["base_stat"] 
        for stat in stat_array 
    } 
modified_pikachu = apply_keyed(pikachu,["stats"], transform_stat)
modified_pikachu["stats"]
{'speed': 90, 'special-defense': 50}

return_keyed

from handy_dict import return_keyed

handy-dict also makes it easy to return multiple values from a dictionary, iterating through keys and arrays:

return_keyed(pikachu, ["moves","move","name"])
['mega-punch', 'pay-day', 'thunder-punch']

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

handy_dict-0.1.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

handy_dict-0.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file handy_dict-0.1.0.tar.gz.

File metadata

  • Download URL: handy_dict-0.1.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.10

File hashes

Hashes for handy_dict-0.1.0.tar.gz
Algorithm Hash digest
SHA256 65594dc4742ee4f41e5cb4b83161b85b2d5f7601c678aa185bac783b646b49cf
MD5 e95c61d7d5b74d1384f8b7fe61377f5f
BLAKE2b-256 93e8fd3f431fb6366a53d3257ae53f83035f769a388e07ca974c723cce8e4c40

See more details on using hashes here.

File details

Details for the file handy_dict-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: handy_dict-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.10

File hashes

Hashes for handy_dict-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a1ad3bf27c4107257d58318c0e8af8175cef39b261998c9eac40427191938c2e
MD5 986e18ba6298a17a1ae72a1c0f754b54
BLAKE2b-256 71d40a25db23fb5403d5cf4d50f511c07889dcc9652543c4932feb3a65343de0

See more details on using hashes here.

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