Skip to main content

Python Implementation of Java TreeMap/TreeSet

Project description

pytreemap Build Status codecov

Python implementation of the Java TreeMap/Tree.

Installation

Install with pip:

pip install pytreemap

Documentation

Click here to access the documentation

Basic Usage

This demo aims to show you the basic operations available in this package. Consult the documentation for more details.

Import and instantiate

>>> from pytreemap import TreeMap
>>> tm = TreeMap()

Insert key-value mappings

>>> tm[5] = 'Python is great!'
>>> print(tm)
{5=Python is great!}
>>> tm[10] = 'Java is also nice!'
>>> print(tm)
{5=Python is great!, 10=Java is also nice!}
>>> tm.put(-1,  'We love them both!')
>>> print(tm)
{-1=We love them both!, 5=Python is great!, 10=Java is also nice!}

Search for keys

>>> tm[5]
'Python is great!'
>>> tm[2]
KeyError: 'key not found'
>>> tm.get(2)  # No error is raised

Delete key-value mappings

>>> del tm[10]
>>> print(tm)
{-1=We love them both!, 5=Python is great!}
>>> del tm[2]
KeyError: 'key not found'
>>> tm.remove(2)  # No error is raised

Check whether some keys exist

>>> 2 in tm
False
>>> -1 in tm
True
>>> tm.contains_key(-1)
True

Iterate over keys/values/entries

>>> [key for key in tm]
[-1, 5]
>>> [key for key in tm.key_set()]
[-1, 5]
>>> [value for value in tm.values()]
['We love them both!', 'Python is great!']
>>> [entry for entry in tm.entry_set()]
[-1=We love them both!, 5=Python is great!]

Testing

Most of the tests from Java that concerned TreeMap are passed. Check out the tests/ directory for more details.

Benchmarks

All benchmarks are done on a laptop with Intel Core i7-7700HQ CPU and 16GB of RAM.

Since this package is an implementation of the Java TreeMap, the benchmarks are focused on comparing the performance between this package and Java’s TreeMap.

This package is currently written in pure Python and it should come at no surprise that it is much slower than Java, especially when the size of the tree is large.

A Cython version is in the works.

Benchmark procedure:

  1. Prepare n entries with distinct keys. (n ranges from 1000 to 60000 with 1000 interval.)

  2. Insert/Remove/Search them into the map in random order and record the completion time.

  3. Repeat step 1-2 two more times and average the result.

Here is result using Java TreeMap:

And here is the result using pytreemap:

Overlay the plots together, we can see that pytreemap is ~30x slower:

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

pytreemap-0.5.1.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

pytreemap-0.5.1-py3-none-any.whl (32.6 kB view details)

Uploaded Python 3

File details

Details for the file pytreemap-0.5.1.tar.gz.

File metadata

  • Download URL: pytreemap-0.5.1.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/29.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.4 tqdm/4.60.0 importlib-metadata/4.0.1 keyring/23.0.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.4

File hashes

Hashes for pytreemap-0.5.1.tar.gz
Algorithm Hash digest
SHA256 24f02ba10dba21d79b10c207967ed3dae043f98bc940d7295411e48753c21467
MD5 d50ba6e438364f18f0665db5ca1fad37
BLAKE2b-256 3df19373351d9f0656f3b61f14455a999461fb1109e6ef5a86497822f841b4c9

See more details on using hashes here.

File details

Details for the file pytreemap-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: pytreemap-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 32.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/29.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.4 tqdm/4.60.0 importlib-metadata/4.0.1 keyring/23.0.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.4

File hashes

Hashes for pytreemap-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f36e25e7681dc081bae182b3802ce86ee77b13d60d5a3ee41ca3d72ce0435d78
MD5 4feef7ee1a584334c5f6cdc8ceee68cb
BLAKE2b-256 99a8f98e673b87f557a76fdea9da52affe7ab41cf6906e3b4480587f78b14a28

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