Skip to main content

a simpler and easier way to manipulate lists of dictionaries

Project description

PLOD - Brief Introduction

PLOD is a simpler and easier way to manipulate lists of dictionaries. PLOD stands for Pythonic Lists of Dictionaries.

Why Lists of Dictionaries? Why PLOD?

Increasingly, software is passing more complex data stores between machines and between processes. Examples include RESTful XML, JSON/MongoDB, Google protobuf, RabbitMQ, etc.

These data stores can include lists of collections, and each collection can have many attributes/values. In Python, these are often internally represented as a list containing dictionaries. For example, if you needed to represent a list of fruits available for purchase:

fruits = [
    {"name": "bannana", "color": "yellow", "qty": 9,  "sizes": [2, 2.4, 3]},
    {"name": "cherry",                     "qty": 40, "sizes": [3, 2, 9]},
    {"name": "lime",    "color": "green",  "qty": 2,  "sizes": [2]},
]

One could, of course, pass such a structure into a SQL database such as MySQL or PostgreSQL for manipulation. But that can be overkill for a small amount of temporary data, especially when the infrastructure requirements are light and response time is critical. In that case, manipulating such a list might make more sense to do in-memory within Python itself.

If it is simple enough, one could do so directly using Python. For example:

abundant_fruit = [f for f in fruits if f['qty']>5]

But, if the program you are writing does such manipulations regularly, and those manipulations are somewhat more complex, then PLOD might be worth using. To mimic the previous example, this time with PLOD:

from PLOD import PLOD
abundant_fruit = PLOD(fruit).gt('qty',5).returnList()

Or a more complex example:

from PLOD import PLOD
my_fruit = PLOD(fruit).sort("color").contains("sizes", [3]).renumber("id", insert=True).returnList()

Here the list is sorted by color (missing colors at the top), filtered to entries with a size 3, and renumbered with a new key called “id”.

Installation

Install _PLOD_ using pip:

pip install PLOD

How to Use

In general, one simply:

  1. Creates an instance of the PLOD class.

  2. Chains together methods of the class to manipulate the list.

  3. Uses a “return” method to get the results you want.

For example, to sort a list:

from PLOD import PLOD
my_list = PLOD(fruits).sort("qty").returnList()

Or, to get a string with comma-separated values with a filter:

from PLOD import PLOD
csv = PLOD(fruits).gt('qty', 1).returnCSV(keys=['name', 'sizes'])

For more detailed information, please visit the Documentation and Library Reference

Other Resources

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

PLOD-0.1.5.zip (12.9 kB view details)

Uploaded Source

File details

Details for the file PLOD-0.1.5.zip.

File metadata

  • Download URL: PLOD-0.1.5.zip
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PLOD-0.1.5.zip
Algorithm Hash digest
SHA256 b1aa3c9b9ddff314e4c3fc621d2cd7ea72270046dfb3ac062cf2e7d6111f4b0b
MD5 fed3d63f403168d87e4000e09c2de832
BLAKE2b-256 19e6198964c2551765842a1f6650ca46d3a1e302c1cfe2cac0018964a1fc842b

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