Skip to main content

Case insensitive derivable dictionary

Project description

Case insensitive derivable dictionary.

License

Copyright © 2013 Thomas Gläßle t_glaessle@gmx.de

This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the COPYING file for more details.

This program is free software. It comes without any warranty, to the extent permitted by applicable law.

Global namespace

  • def build_diciti(base): derive case insensitive dictionary class

  • def Dicti(obj): create ci-dict instance using obj’s class as base

  • class dicti = build_dicti(dict) standard ci-dict

  • class odicti = build_odicti(OrderedDict) ordered ci-dict

Overview

Entries in a dicti can be accessed case invariantly:

keys = ['Hello', 'beautiful', 'world!']
values = [1, 2, 3]
z = list(zip(keys, values))
i = dicti(z)
assert "WorLD!" in i and "universe" not in i
assert i.get('hEllo') == 1

However, the in calls like .keys() or .items() the keys are returned as in their original case:

assert "Hello" in list(i.keys())

dicti can be “derived” from another custom dictionary extension using it as the underlying dictionary to gain additional properties like order preservation:

from collections import OrderedDict
odicti = build_dicti(OrderedDict)
oi = odicti(z)
assert list(oi.keys()) == keys

The equality comparison preserves the semantics of the base type and reflexitivity as best as possible. This has impact on the transitivity of the comparison operator:

rz = list(zip(reversed(keys), reversed(values)))
roi = odicti(rz)
assert roi == i and i == oi
assert oi != roi and roi != oi  # NOT transitive!
assert oi == i and i == oi      # reflexive

Be careful with reflexitivity when comparing to non-dicti types and even more so if both operands are not subclasses of each other. Here it is important to know about coercion rules. o == oli actually calls oli.__eq__(o) if oli is of a subclass of the type of of o. See:

http://docs.python.org/2/reference/datamodel.html#coercion-rules

>>> o = OrderedDict(oi)
>>> oli = Dicti(oi.lower_dict())
>>> assert oli == o and o == oli    # reflexive (coercion rules)
>>> print(o.__eq__(oli))            # dependends on OrderedDict.__eq__
False

Note that dicti is the type corresponding to builtins.dict:

assert build_dicti(dict) is dicti

The method Dicti is convenient for creating case insensitive dictionaries from a given object automatically using the objects type as the underlying dictionary type.

assert oi == Dicti(o)
assert type(oi) is type(Dicti(o))

The subclassing approach works well with “badly” written code as in json that checks for isinstance(dict):

import json
assert oi == json.loads(json.dumps(oi), object_pairs_hook=odicti)

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

pydicti-0.0.2.tar.gz (5.6 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for pydicti-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c51efda9abd40521db966715febcd76fd119cf1040d4f9d46ee44c1e842c3b72
MD5 2c3cafd3e0c5213cb02d696d5bc23337
BLAKE2b-256 ae1a988cf840d138739a61a70b4d745f6dadd35e5fe952b1352345706ba717eb

See more details on using hashes here.

Supported by

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