Skip to main content

Easy way to create and manage hash lists

Project description

Description

This package is meant to store data in a smart way so when you are dealing with large amounts of data it will be faster to query the list and get an answer. For now the package supports only strings as data types but I am working to improve this and allowing for other data types, data structures and objects to be stored.

Methods

  • add(args*)
    • Can add multiple elements at a time into the hash list object
  • get_item_index(item)
    • Returns a tuple of form (key, index)
  • get_item_by_index(index)
    • Returns the item by providing the index (key, index)
  • remove_item_by_index(index=None)
    • Removes an item form the hashed list either by it's index. Note that the index should be a tuple of (key, index).
  • remove_item_by_name(item)
    • Removes the item by it's 'name'.
  • delete_key(key)
    • The method deletes a key and all the items associated with it. Be careful how you use this method in your code. The index argument expects an integer (int).
  • keys()
    • The method returns a list of all the keys in the hashed list
  • print()
    • Prints the hashed list object contents.

Usage

  • To get this module you can install it by using pip as follows:
pip install hashlist
  • If you have hashlist installed update it from time to time using this command:
pip install --upgrade hashlist
  • Once you have your module installed let's create an instance of the class and add some items and print the hash list:
from hashlist import hast_list

h_lst = hast_list()
h_lst.add('test', 'my', 'python', 'hashed', 'list')

'''
> Output:
{112: ['test', 'python'], 115: ['my'], 103: ['hashed'], 111: ['list']}
'''
  • Note that you can add 1 item at a time or multiple items.
  • The resulting hash list is a dictionary that has stored the words in a key:value pair
  • The key, for now, is calculated by adding the ASCII values of the letters from the word and dividing the sum to the length of the word:
def __key__(self, word):
    if word:
        key = sum([ord(letter) for letter in word])//len(word)
        return key
    raise Exception('Argument can not be None')
  • You can see all the public methods that the class offers by running this code:
from hashlist import hast_list

method_list = [method for method in dir(hash_list) if method.startswith('_') is False]
print(method_list)

'''
> Output:
> ['add', 'delete_index', 'get_item_by_index', 'get_item_index', 'indexes', 'print', 'remove_item_by_index', 'remove_item_by_name']
'''
  • Ok, great! Now let's use some of those methods. let's start with get_item_index() method.
  • get_item_index(index) will return a tuple of values. The first value in the tuple is the item's key and the second value will represent the position of the item in the array:
from hashlist import hast_list

h_lst = hast_list()
h_lst.add('test', 'my', 'python', 'hashed', 'list')
item = h_lst.get_item_index('my')
print(item)

'''
> Output:
>(115, 0)
'''
  • But what if you want to get the actual item (in this case the word). Well we can use the get_item_index() and get_item_by_index():
from hashlist import hast_list

h_lst = hast_list()
h_lst.add('test', 'my', 'python', 'hashed', 'list')
index= h_lst.get_item_index('my')
item = h_lst.get_item_by_index(index)
print(index)
print(item)
'''
> Output:
> (115, 0)
> my
'''
  • Now let's use the keys() method. This method return a list of all the keys that are in your hash list
from hashlist import hast_list

h_lst = hast_list()
h_lst.add('test', 'my', 'python', 'hashed', 'list')
all_keys = h_lst.keys()
print(all_keys)

'''
> Output:
> [112, 115, 103, 111]
'''
  • You can use the delete_key() method to delete a key from the hash list.
from hashlist import hast_list

h_lst = hast_list()
h_lst.add('test', 'my', 'python', 'hashed', 'list')
key = h_lst.keys()[0]
h_lst.print()
h_lst.delete_key(key)
h_lst.print()

'''
> Output:
> {112: ['test', 'python'], 115: ['my'], 103: ['hashed'], 111: ['list']}
> {115: ['my'], 103: ['hashed'], 111: ['list']}
'''
  • If you want to delete an item from the hash list but you don't know it's (key, index) you can use the remove_item_by_name()
from hashlist import hast_list

h_lst = hast_list()
h_lst.add('test', 'my', 'python', 'hashed', 'list')
h_lst.print() # hash list before removing an item
h_lst.remove_item_by_name('my')
h_lst.print() # hash list after removing an item

'''
> Output:
> {112: ['test', 'python'], 115: ['my'], 103: ['hashed'], 111: ['list']}
> {112: ['test', 'python'], 103: ['hashed'], 111: ['list']}
'''
  • If you can see the class automatically deletes the entire key if there is no value associate with it. In this case the word 'my' had the key 115 but because we removed the item the key has no value associate with it so the class deletes the key.
  • You can remove an item by using it's (key, index) by using remove_item_by_key() method:
from hashlist import hast_list

h_lst = hast_list()
h_lst.add('test', 'my', 'python', 'hashed', 'list')
h_lst.print() # hash list before removing an item
index = h_lst.get_item_index('test')
print(index)
h_lst.remove_item_by_index(index)
h_lst.print() # hash list after removing an item

'''
> Output:
> {112: ['test', 'python'], 115: ['my'], 103: ['hashed'], 111: ['list']}
> (112, 0)
> {112: ['python'], 115: ['my'], 103: ['hashed'], 111: ['list']}
'''

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

hashlist-0.0.8.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

hashlist-0.0.8-py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 3

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