Skip to main content

Python data manipulation made braindead

Project description

Python data manipulation made braindead.

Installation

pip install manipulator

Usage

manipulator mainly exposes three functions, get, update, and set. get retrieves data, update transforms it based on its form, and set transforms it by simply resetting. Transformations can be applied in place or on a copy. The default is in place, because copying is expensive. If you want a copy of the data, set the keyword argument in_place to True.

It uses a query “language” not unlike CSS, albeit much, much simpler. The only two entities in this “language” are IDs—denoted by #—and Classes—denoted by .. IDs are unique, whereas Classes are collections of all leaf values that conform.

A few motivating examples (a more exhaustive list can be found in the test directory):

import manipulator

manipulator.get({"k": [1, [2], 3]}, "#k #1 #0")
# => 2 (note how list indices are coerced into integers)

manipulator.get([{"k": "v"}, {"k": "v2", "k2": "v3"}], ".k")
# => ["v", "v2"]

manipulator.get([{"k": "v"},
                 {"k": {
                    "a": [{"k": 10}, {"k": 11}]
                  }
                 }], ".k #1 #a .k")
# => [10, 11]

manipulator.set({"k": [1, [2], 3]}, "#k #1 #0", 3)
# => {"k": [1, [3], 3]} (in place)

manipulator.set({"k": [1, [2], 3]}, "#k #1 #0", 3, in_place=False)
# => {"k": [1, [3], 3]} (will create a copy of the data)

manipulator.set([{"k": "v"},
                 {
                   "k": {
                     "a": [{"k": 10}, {"k": 11}]
                    }
                 }], ".k #1 #a .k", 100)
# => [{"k": "v"}, {"k": {"a": [{"k": 100}, {"k": 100}]}}]

manipulator.update({"k": [1, [2], 3]}, "#k #1 #0", lambda x: x-1)
# => {"k": [1, [1], 3]} (in place, use in_place=False to copy)

manipulator.update([{"k": "v"},
                    {
                      "k": {
                        "a": [{"k": 10}, {"k": 11}]
                      }
                    }], ".k #1 #a .k", lambda x: x+1)
# => [{"k": "v"}, {"k": {"a": [{"k": 11}, {"k": 12}]}}]

That is all.

Have fun!

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

manipulator-0.0.2.tar.gz (2.9 kB view details)

Uploaded Source

File details

Details for the file manipulator-0.0.2.tar.gz.

File metadata

  • Download URL: manipulator-0.0.2.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for manipulator-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5231ffa162b345c55f89883ac6f2550aa0dc81dd696a432b2454d98fc318ee51
MD5 d03559a8b1fcaa4adef278671490a4ee
BLAKE2b-256 db976b9624fc20a24c4bb40fee8fb1beaf4ae47f6e744b07ea3894b2ec85eff3

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