Skip to main content

Contra[dict]

Description

Contradict provides a thread-safe implementation of the SortedDict. A SortedDict is a dict which can be traversed in ascending or descending order. This is different from the OrderedDict which can be traversed in the order in which the elements were added. The collection type exposes APIs to perform the following operationsget, set, contains, items, keys and values.

Object Usage

For adding an object to the SortedDict, the object needs to implement it's comparator logic and a hash function.

class GitRepo:
    def __init__(self, url, stars, forks):
        self.url = url
        self.stars = stars
        self.forks = forks

    # The object needs to implement the following methods
    def __eq__(self, other):
        return self.name == other.name and self.stars == other.stars and self.forks == other.forks

    def __ne__(self, other):
        return self.name != other.name or self.stars != other.stars or self.forks != other.forks

    def __lt__(self, other):
        # The two repos are first compared based on stars then forks and then sorted lexically based on url
        if self.stars == other.stars and self.forks == other.forks return self.url < other.url

    def __le__(self, other):
        if self.stars == other.stars and self.forks == other.forks return self.url <= other.url

    def __gt__(self, other):
        if self.stars == other.stars and self.forks == other.forks return self.url > other.url

    def __ge__(self, other):
        if self.stars == other.stars and self.forks == other.forks return self.url >= other.url
    # Use any unique attribute to hash
    def __hash__(self):
        return hash(self.url)


Dependencies

Python 3

Installation

pip install --upgrade contradict

Usage

from contradict.sorted_dict import SortedDict


sorted_dict = SortedDict()           # Creates an empty SortedDict
sorted_dict.set(key, value)          # Sets a key, value pair
sorted_dict.get(key)                 # Returns the value, if key is present or None
sorted_dict.items()                  # Returns an interator to the key, value pairs in asc order
sorted_dict.keys()                   # Returns an interator to the keys in asc order
sorted_dict.values()                 # Returns an interator to the values in asc order of keys
sorted_dict.items(reverse=True)      # Returns an interator to the key, value pairs in desc order
sorted_dict.keys(reverse=True)       # Returns an interator to the keys in desc order
sorted_dict.values(reverse=True)     # Returns an interator to the values in desc order of keys

License

MIT

Changelog

2.0.1
  • Fixed the README
2.0.0
  • Added thread safety and fixed a bug with reverse sorting
1.0.2
  • Updated README

Metadata

Release files for contradict 2.0.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 contradict 2.0.1
File Size Uploaded
contradict-2.0.1.tar.gz 2.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for contradict 2.0.1
File Interpreter ABI Platform
contradict-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.6 kB

Release files / contradict-2.0.1.tar.gz

Download URL contradict-2.0.1.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
001d4acbc09ebc689cb14e1f384ca322b5e64b8300cc8e5cf4af5e8706de31e6
BLAKE2b-256 checksum
How to use checksums
2d41c6a1cfd099b7a8c812430fe9729a0446f790bbafa2da32d99c1831ca465c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.5

Release files / contradict-2.0.1-py3-none-any.whl

Download URL contradict-2.0.1-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1cffb1767c324cefebb372fea752ba89f31fcb4e68c027d139cd8bac1ef70bb1
BLAKE2b-256 checksum
How to use checksums
c3678537ac579a6aeccd2b50cbe1ea1b498cbe1995bcef4cca95cb5a79ce19a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.5

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

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