Skip to main content

A thread safe implementation of the SortedDict.

Project description

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

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

contradict-2.0.1.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

contradict-2.0.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file contradict-2.0.1.tar.gz.

File metadata

  • Download URL: contradict-2.0.1.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for contradict-2.0.1.tar.gz
Algorithm Hash digest
SHA256 001d4acbc09ebc689cb14e1f384ca322b5e64b8300cc8e5cf4af5e8706de31e6
MD5 3ae1b7046aa05f41db2e33f343614c9f
BLAKE2b-256 2d41c6a1cfd099b7a8c812430fe9729a0446f790bbafa2da32d99c1831ca465c

See more details on using hashes here.

File details

Details for the file contradict-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: contradict-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for contradict-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1cffb1767c324cefebb372fea752ba89f31fcb4e68c027d139cd8bac1ef70bb1
MD5 4afe05cc42f0313865f8448d7cd7b391
BLAKE2b-256 c3678537ac579a6aeccd2b50cbe1ea1b498cbe1995bcef4cca95cb5a79ce19a9

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