Skip to main content

pydicti

Test Status Coverage Latest Version License

Installation

You can install the newest version of pydicti from PyPI:

pip install pydicti

Alternatively, you can just take the file pydicti.py and redistribute it with your application.

Overview

  • class dicti: default case insensitive dictionary type

  • class odicti: ordered case insensitive dictionary type

  • def build_dicti: create a case insensitive dictionary class

  • def Dicti: create a case insensitive copy of a dictionary

dicti

Objects of type dicti are dictionaries that feature case insensitive item access:

>>> d = dicti(Hello='foo', world='bar')
>>> d['heLLO']
'foo'
>>> 'WOrld' in d
True

Internally however, the keys retain their original case:

>>> sorted(d.keys())
['Hello', 'world']

odicti

The type odicti instanciates order-preserving case insensitive dictionaries:

>>> odicti(zip('abc', range(3)))
Dicti(OrderedDict([('a', 0), ('b', 1), ('c', 2)]))

build_dicti

With build_dicti you can create custom case insensitive dictionaries. This function is what is used to create the pydicti.dicti and pydicti.odicti types. Note that calling build_dicti several times with the same argument will result in identical types:

>>> build_dicti(dict) is dicti
True
>>> build_dicti(OrderedDict) is odicti
True

build_dicti uses subclassing to inherit the semantics of the given base dictionary type:

>>> issubclass(odicti, OrderedDict)
True

Dicti

The function Dicti is convenient for creating case insensitive copies of dictionary instances:

>>> o = OrderedDict(zip('abcdefg', range(7)))
>>> oi = Dicti(o)
>>> type(oi) is odicti
True

JSON

The subclassing approach allows to plug your dictionary instance into places where typechecking with isinstance is used, like in the json module:

>>> import json
>>> d == json.loads(json.dumps(d), object_hook=dicti)
True

You can use json.loads(s, object_pairs_hook=odicti) to deserialize ordered dictionaries.

Pitfalls

The equality comparison tries preserves the semantics of the base type as well as reflexitivity. This has impact on the transitivity of the comparison operator:

>>> i = dicti(oi)
>>> roi = odicti(reversed(list(oi.items())))
>>> roi == i and i == oi
True
>>> oi != roi and roi != oi  # NOT transitive!
True
>>> oi == i and i == oi      # reflexive
True

The coercion rules in python allow this to work pretty well when performing comparisons between types that are subclasses of each other. Be careful otherwise, however.

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.

CHANGELOG

1.2.0

Date: 25.02.2023

  • fix incorrect behaviour of __or__ and __ior__, see #8

1.2.0

Date: 15.12.2022

  • add type hints

1.1.6

Date: 04.11.2021

  • update the badges on the landing page

1.1.5

Date: 04.11.2021

  • maintenance release for testing automatic releases using GitHub Actions

1.1.4

Date: 17.10.2020

  • use str.casefold() on python3

  • make normalization function a parameter of build_dict, so that user-defined normalization functions can be passed

1.1.3

Date: 28.06.2019

  • avoid key recomputation in __setitem__

1.1.2

Date: 28.06.2019

  • leave item order invariant under assignment in odicti (#2)

  • leave key case invariant under assignment

1.1.1

Date: 25.03.2019

  • fix deprecated MutableMapping import (error on py38)

1.1.0

Date: 19.03.2019

  • drop py2.6 support

  • fix version number in long_description

1.0.0

Date: 19.03.2019

  • make str representation more dict-like

  • misc cleanup

0.0.6

Date: 08.09.2016

  • fix UnicodeDecodeError in setup when UTF-8 is not the default encoding

0.0.5

Date: 18.05.2016

  • fix pickling on py 3.5

  • the ‘name’ parameter to build_dicti can now be a qualname

0.0.4

Date: 01.02.2014

  • add coverage reports

  • use more extensive unit tests

  • add support for pickle

0.0.3

Date: 26.01.2014

  • add support for python26

  • make dependency on OrderedDict optional

  • migrate to setuptools in order to use testing commands

  • support ordereddict.OrderedDict as fallback

0.0.2

Date: 29.12.2013

  • fix dicti.pop

  • support deepcopy(dicti)

  • make nosetest automatically execute the doctests

Metadata

Release files for pydicti 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydicti 1.2.1
File Size Uploaded
pydicti-1.2.1.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydicti 1.2.1
File Interpreter ABI Platform
pydicti-1.2.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 20.0 kB

Release files / pydicti-1.2.1.tar.gz

Download URL pydicti-1.2.1.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
63e0756ff27a335e92916ff40be589a21a58894b3a93099edf0f3cbf695690dc
BLAKE2b-256 checksum
How to use checksums
f3c8b3f5457fada93d8d9fc2f0f50b5082ff1500f0b1c9457786925e0ad1fa1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / pydicti-1.2.1-py2.py3-none-any.whl

Download URL pydicti-1.2.1-py2.py3-none-any.whl
Size 9.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
1c8ea52ec71d4597087b7800043c645c3bad6d7b69d8a5c1b11f3b9351362614
BLAKE2b-256 checksum
How to use checksums
55edf724918683c445efe37b5d4dea3a69f9efd58112a98482577d7295a3da61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.2.0

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.0.6

2 release files

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page