Skip to main content

utility set of handling dict

Project description

https://travis-ci.org/podhmo/dictknife.svg?branch=master

features

  • deepmerge

  • deepequal

  • walker

  • diff

deepmerge

from dictknife import deepmerge
d0 = {
    "a": {
        "x": 1
    },
    "b": {
        "y": 10
    },
}
d1 = {
    "a": {
        "x": 1
    },
    "b": {
        "z": 10
    },
    "c": 100
}
actual = deepmerge(d0, d1)
expected = {
    "a": {
        "x": 1
    },
    "b": {
        "y": 10,
        "z": 10
    },
    "c": 100
}
assert actual == expected

walker

using LooseDictWalkingIterator example.

import json
import pprint
from dictknife import LooseDictWalkingIterator

# from: https://github.com/BigstickCarpet/json-schema-ref-parser
d = json.loads("""
{
  "definitions": {
    "person": {
      "$ref": "schemas/people/Bruce-Wayne.json"
    },
    "place": {
      "$ref": "schemas/places.yaml#/definitions/Gotham-City"
    },
    "thing": {
      "$ref": "http://wayne-enterprises.com/things/batmobile"
    },
    "color": {
      "$ref": "#/definitions/thing/properties/colors/black-as-the-night"
    }
  }
}
""")

refs = []

iterator = LooseDictWalkingIterator(["$ref"])
for path, sd in iterator.iterate(d):
    refs.append((path[:], sd["$ref"]))

pprint.pprint(refs)

output

[(['definitions', 'color', '$ref'],
  '#/definitions/thing/properties/colors/black-as-the-night'),
 (['definitions', 'place', '$ref'],
  'schemas/places.yaml#/definitions/Gotham-City'),
 (['definitions', 'thing', '$ref'],
  'http://wayne-enterprises.com/things/batmobile'),
 (['definitions', 'person', '$ref'], 'schemas/people/Bruce-Wayne.json')]

todo: description about chains and operator and context,…

command

install dictknife via pip install dictknife[command] .

  • concat

  • transform

  • diff

TODO: gentle introduction

concat

$ dicknife concat a.yaml b.yaml c.json

transform

$ dicknife transform --function misc/transform.py:lift --src src/01transform/properties.yaml --config '{"name": "person"}'
# or
$ dictknife transform --code 'lambda d,**kwargs: {"definitions": {"person": d}}' --src src/01transform/properties.yaml

diff

$ dictknife diff a.yaml b.yaml
$ dictknife diff --sort-keys a.yaml b.yaml
--- src/02diff/left.yaml
+++ src/02diff/right.json
@@ -1,13 +1,12 @@
 {
-  "Person": {
+  "person": {
     "properties": {
-      "Name": {
+      "name": {
         "type": "string",
-        "description": "someone's name"
+        "description": "Someone's name"
       },
-      "Age": {
-        "type": "integer",
-        "description": "age"
+      "age": {
+        "type": "integer"
       }
     }
   }

0.3

  • command extras pip install dictknife[command]

  • adding dictknife-concat command

  • adding dictknife-transform command

  • adding dictknife-diff command

0.2

  • external iterator is recommened. (using LooseDictWalkingIterator instead of LooseDictWalker)

0.1

  • yaml/json load extras (pip install dictknife[load])

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

dictknife-0.4.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dictknife-0.4.0-py2.py3-none-any.whl (23.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file dictknife-0.4.0.tar.gz.

File metadata

  • Download URL: dictknife-0.4.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for dictknife-0.4.0.tar.gz
Algorithm Hash digest
SHA256 4dfa3a26d0fd998e7b3d549f90b808f65ba41940bff4828e37cfd2902bbc436b
MD5 24a2c264ca95c8847ac9e64c610dcd37
BLAKE2b-256 deee35797185ddbe451fd298a3af528968cb27db40063090c6448f786b8785c7

See more details on using hashes here.

File details

Details for the file dictknife-0.4.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for dictknife-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a0722488b161e5b8e75554bdd903e895b8245c234edb15015e5d04840bd082e1
MD5 72668970c9b6061284e1ff380594b08f
BLAKE2b-256 9160d154e3c952c738d3b6d15df96a8dc53386b282a251e35df8fc69ccf05978

See more details on using hashes here.

Supported by

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