nocasedict - A case-insensitive ordered dictionary for Python

Overview

nocasedict - A case-insensitive ordered dictionary for Python

Version on Pypi Actions status Docs build status (master) Test coverage (master)

Overview

Class NocaseDict is a case-insensitive ordered dictionary that preserves the original lexical case of its keys.

Example:

$ python
>>> from nocasedict import NocaseDict

>>> dict1 = NocaseDict({'Alpha': 1, 'Beta': 2})

>>> dict1['ALPHA']  # Lookup by key is case-insensitive
1

>>> print(dict1)  # Keys are returned with the original lexical case
NocaseDict({'Alpha': 1, 'Beta': 2})

The NocaseDict class supports the functionality of the built-in dict class of Python 3.8 on all Python versions it supports with the following exceptions (and the case-insensitivity of course):

  • The iter..(), view..() and has_key() methods are only present on Python 2, consistent with the built-in dict class.
  • The keys(), values() and items() methods return a list on Python 2 and a dictionary view on Python 3, consistent with the built-in dict class.

Functionality can be added using mixin classes:

  • HashableMixin mixin class: Adds case-insensitive hashability.
  • KeyableByMixin mixin generator function: Adds ability to get the key from an attribute of the value object.

Why yet another case-insensitive dictionary: We found that all previously existing case-insensitive dictionary packages on Pypi either had flaws, were not well maintained, or did not support the Python versions we needed.

Installation

To install the latest released version of the nocasedict package into your active Python environment:

$ pip install nocasedict

This will also install any prerequisite Python packages.

For more details and alternative ways to install, see Installation.

Documentation

Change History

Contributing

For information on how to contribute to the nocasedict project, see Contributing.

License

The nocasedict project is provided under the GNU Lesser General Public License (LGPL) version 2.1, or (at your option) any later version.

Comments
  • Make NocaseDict derived from dict

    Make NocaseDict derived from dict

    Currently, NocaseDict is derived from object, but it should ideally be derived from dict. The issue is that it does not want to inherit the dict data, at least not the current implementation. This can probably be handled though with some changes in the implementation.

    DISCUSSION: Do we want NocaseDict to be derived from dict, for type checking?

    COMMENT/KS: It is confusing when it is not inherited and probably not very pythonic. Thought it over last night and we should not be calling it a dict if it does not inherit from dict.

    type: enhancement area: code resolution: fixed 
    opened by andy-maier 3
  • Remove temporary disabling of pylint issue R0801 (similar lines)

    Remove temporary disabling of pylint issue R0801 (similar lines)

    PR #79 (targeted for 1.0.3) disabled pylint issue R0801 temporarily.

    The circumvention should be transitioned into a final solution once pylint issue https://github.com/PyCQA/pylint/issues/4118 is addressed.

    area: code type: cleanup resolution: fixed 
    opened by andy-maier 2
  • Weekly CI run of master branch on full set of environments

    Weekly CI run of master branch on full set of environments

    This PR is used to run the CI tests on the master branch, on the full set of OS / Python / package level combinations.

    The test is scheduled to run on a weekly basis.

    Do not merge this PR!!

    opened by andy-maier 2
  • Clarify what the rules are for implementing __sizeof__()

    Clarify what the rules are for implementing __sizeof__()

    The sys.getsizeof(obj) function returns the memory size of obj in Bytes. It does that by calling __sizeof__() on the object and adding the GC overhead if the object is GC-managed.

    The rules for whether a user-defined class like NocaseDict has to implement __sizeof__() are not documented in the Python docs.

    Here is a comparison between dict and NocaseDict that suggests that not implementing __sizeof__() is incorrect. On the other hand, dictionaries are referencing their key and value objects, so if multiple dictionaries reference the same objects it would not make too much sense to attribute their sizes to the dictionaries.

    import sys
    from nocasedict import NocaseDict
    nd = NocaseDict()
    d = dict()
    print("len dict NocaseDict")
    for x in range(0, 25):
        print(x, sys.getsizeof(d), sys.getsizeof(nd))
        key = 'key' + str(x)
        nd[key] = x
        d[key] = x
    

    resulting in:

    len dict NocaseDict
    0 232 48
    1 232 48
    2 232 48
    3 232 48
    4 232 48
    5 232 48
    6 360 48
    7 360 48
    8 360 48
    9 360 48
    10 360 48
    11 640 48
    12 640 48
    13 640 48
    14 640 48
    15 640 48
    16 640 48
    17 640 48
    18 640 48
    19 640 48
    20 640 48
    21 640 48
    22 1176 48
    23 1176 48
    24 1176 48
    25 1176 48
    
    resolution: invalid 
    opened by andy-maier 2
  • Hashable or not

    Hashable or not

    The current NocaseDict code supports a __hash__() method that calculates a hash value from the set of tuples of lower cased dict key and dict item value. That makes it a hashable object. Hashable objects can be used as members in sets or as keys in mappings (dicts).

    NocaseDict objects are mutable, but the hash value is calculated under the assumption that the object does not change its value while used in a set or as a key, i.e. it is calculated just once for a particular object. For this reason, the mutable standard types in Python (e.g. dict) are not hashable. Changing the value while in a set or used as a key can have strange effects (there are forum threads full of that).

    Right now, we document that the NocaseDict objects that are used in a set or as a mapping key must not change while being used that way. However, there is no enforcement about that.

    Pywbem is using NocaseDict objects in sets (I believe, need to double check).

    DISCUSSION: Should we continue supporting NocaseDict objects being hashable, or should we remove that functionality because it is considered too dangerous for the general user.

    area: code type: cleanup resolution: fixed 
    opened by andy-maier 2
  • Use of obj.name in NocaseDict() init

    Use of obj.name in NocaseDict() init

    The current NocaseDict init method supports an iterable of pywbem CIM objects whose name attribute is used as dict key. That ability is very convenient for pywbem and needs to be retained.

    Options are:

    • NocaseDict continues to support the functionality, but in a cleaned up way. It would be an additional functionality on top of what the standard Python dict supports.
    • NocaseDict no longer supports it, and the pywbem code handles it. That is possible because NocaseDict objects so far were not created by pywbem users.

    COMMENT/ks: Actually it is used in pywbemcli but only in tests. The tests can be changed.; it is used to generate keybindings for test_instances.py and scopes for test_qualdecl.py

    DISCUSSION

    area: code type: cleanup resolution: fixed 
    opened by andy-maier 2
  • Stronger typing

    Stronger typing

    It would be nice to have stronger typing when using the package. Currently, Mypy does not find any types when importing from nocasedict:

    image

    A good first step would be to add the py.typed marker, and ensure that all function arguments and return values have type hints. The project could benefit from Mypy linting itself, but that is not required.

    opened by rudolfbyker 0
  • Consider using `casefold` instead of `lower`

    Consider using `casefold` instead of `lower`

    I am comparing various implementations of case-insensitive dictionaries in Python, and have found that this one uses str.lower() rather than str.casefold(). See e.g. pydicti, which uses casefold() is available, and falls back to lower() if not available. If both fail, the key is used verbatim.

    The advantage of casefold is that is has better unicode support than lower.

    opened by rudolfbyker 0
Releases(1.0.4)
Owner
PyWBEM Projects
Organization for the PyWBEM projects (e.g. PyWBEM Client)
PyWBEM Projects
An esoteric data type built entirely of NaNs.

NaNsAreNumbers An esoteric data type built entirely of NaNs. Installation pip install nans_are_numbers Explanation A floating point number is just co

Travis Hoppe 72 Jan 01, 2023
A mutable set that remembers the order of its entries. One of Python's missing data types.

An OrderedSet is a mutable data structure that is a hybrid of a list and a set. It remembers the order of its entries, and every entry has an index nu

Elia Robyn Lake (Robyn Speer) 173 Nov 28, 2022
A mutable set that remembers the order of its entries. One of Python's missing data types.

An OrderedSet is a mutable data structure that is a hybrid of a list and a set. It remembers the order of its entries, and every entry has an index number that can be looked up.

Elia Robyn Lake (Robyn Speer) 173 Nov 28, 2022
Svector (pronounced Swag-tor) provides extension methods to pyrsistent data structures

Svector Svector (pronounced Swag-tor) provides extension methods to pyrsistent data structures. Easily chain your methods confidently with tons of add

James Chua 5 Dec 09, 2022
This repo represents all we learned and are learning in Data Structure course.

DataStructure Journey This repo represents all we learned and are learning in Data Structure course which is based on CLRS book and is being taught by

Aprime Afr (Alireza Afroozi) 3 Jan 22, 2022
This repo is all about different data structures and algorithms..

Data Structure and Algorithm : Want to learn data strutrues and algorithms ??? Then Stop thinking more and start to learn today. This repo will help y

Priyanka Kothari 7 Jul 10, 2022
This repository is for adding codes of data structures and algorithms, leetCode, hackerrank etc solutions in different languages

DSA-Code-Snippet This repository is for adding codes of data structures and algorithms, leetCode, hackerrank etc solutions in different languages Cont

DSCSRMNCR 3 Oct 22, 2021
Data Structure With Python

Data-Structure-With-Python- Python programs also include in this repo Stack A stack is a linear data structure that stores items in a Last-In/First-Ou

Sumit Nautiyal 2 Jan 09, 2022
A Python library for electronic structure pre/post-processing

PyProcar PyProcar is a robust, open-source Python library used for pre- and post-processing of the electronic structure data coming from DFT calculati

Romero Group 124 Dec 07, 2022
Array is a functional mutable sequence inheriting from Python's built-in list.

funct.Array Array is a functional mutable sequence inheriting from Python's built-in list. Array provides 100+ higher-order methods and more functiona

182 Nov 21, 2022
Python Data Structures and Algorithms

No non-sense and no BS repo for how data structure code should be in Python - simple and elegant.

Prabhu Pant 1.9k Jan 08, 2023
Supporting information (calculation outputs, structures)

Supporting information (calculation outputs, structures)

Eric Berquist 2 Feb 02, 2022
Al-Quran dengan Terjemahan Indonesia

Al-Quran Rofi Al-Quran dengan Terjemahan / Tafsir Jalalayn Instalasi Al-Quran Rofi untuk Archlinux untuk pengguna distro Archlinux dengan paket manage

Nestero 4 Dec 20, 2021
Integrating C Buffer Data Into the instruction of `.text` segment instead of on `.data`, `.rodata` to avoid copy.

gcc-bufdata-integrating2text Integrating C Buffer Data Into the instruction of .text segment instead of on .data, .rodata to avoid copy. Usage In your

Jack Ren 1 Jan 31, 2022
Final Project for Practical Python Programming and Algorithms for Data Analysis

Final Project for Practical Python Programming and Algorithms for Data Analysis (PHW2781L, Summer 2020) Redlining, Race-Exclusive Deed Restriction Lan

Aislyn Schalck 1 Jan 27, 2022
A Python dictionary implementation designed to act as an in-memory cache for FaaS environments

faas-cache-dict A Python dictionary implementation designed to act as an in-memory cache for FaaS environments. Formally you would describe this a mem

Juan 3 Dec 13, 2022
🔬 Fixed struct serialization system, using Python 3.9 annotated type hints

py-struct Fixed-size struct serialization, using Python 3.9 annotated type hints This was originally uploaded as a Gist because it's not intended as a

Alba Mendez 4 Jan 14, 2022
A simple tutorial to use tree-sitter to parse code into ASTs

A simple tutorial to use py-tree-sitter to parse code into ASTs. To understand what is tree-sitter, see https://github.com/tree-sitter/tree-sitter. Tr

Nghi D. Q. Bui 7 Sep 17, 2022
CLASSIX is a fast and explainable clustering algorithm based on sorting

CLASSIX Fast and explainable clustering based on sorting CLASSIX is a fast and explainable clustering algorithm based on sorting. Here are a few highl

69 Jan 06, 2023
This Repository consists of my solutions in Python 3 to various problems in Data Structures and Algorithms

Problems and it's solutions. Problem solving, a great Speed comes with a good Accuracy. The more Accurate you can write code, the more Speed you will

SAMIR PAUL 1.3k Jan 01, 2023