A utility data structure for python! This enpowers the power of dicts to the next level!
This module is inspired from discordjs collections
$ pip install python-collection
from Collection import Collection
mycol = Collection()
mycol.set('key', 'value')
print(mycol.get('key')) # Prints 'value'!
col.dict # The data stored in dict
col.size # The size of the dict len(self.dict)
col.keys # Returns the array of keys of dict
col.values # Returns the array of values of dict
col.items # Returns the items of values of dict [(key, value), ...]
col.set(key, value) # Sets a value to the dict
col.get(key) # Returns the value of the key, if no value then returns None
col.clear() # Similar to dict.clear()
col.has(key) # Returns a boolean wheater the key has its value!
col.first() # Returns the first value of the dict
col.first(2) # Returns the array of values of the dict from first to second
col.last() # Returns the last value of the dict
col.last(2) # Returns the array of values of the dict from last to second last
col.random() # Returns a random value of the dict
col.random(2) # Returns a array of 2 random values of dict
# Similar to the above functions there are methods to get the keys of it
col.firstkey() # Returns the first key of the dict
col.firstkey(2) # Returns the array of keys of the dict from first to second
col.lastkey() # Returns the last key of the dict
col.lastkey(2) # Returns the array of keys of the dict from last to second last
col.randomkey() # Returns a random key of the dict
col.randomkey(2) # Returns a array of 2 random keys of dict
Extend the collection
col1 = Collection()
col2 = Collection()
col1.set('key', 'value')
col2.extend(col1)
print(col2.get('key')) # Will return value
Will work with dicts too
col = Collection()
col.extend({ 'key': 'value' })
print(col.get('key')) # Will return value
Will return a duplicate collection of the current collection
print(col.clone())
Convert the collection dict to object
col = Collection()
col.set('key', 'value')
obj = col.to_object()
print(obj.key) # Returns value
Find an item in the items of dict
def find(key, value):
return key == 'key'
print(col.find(find)) # Returns an item (key, value)
Verifies if the callback satisfies any of the items in the dict
def some(key, value):
return key == 'key'
print(col.some(some)) # Returns boolean stating the existence of the key which satisfies the callback
Find an item in the items of dict
def find(key, value):
return key == 'key'
print(col.find(find)) # Returns an item (key, value)
Find array of items which satisfies the callback
def findmany(key, value):
return key == 'key'
print(col.findmany(findmany)) # Returns an array of items (key, value)
Removes the item of the dict which satisfies the callback
def sweep(key, value):
return key == 'key'
print(col.sweep(sweep)) # Returns nothing
Similar to sweep but instead returns a new duplicate collection and filters it
def sweep(key, value):
return key == 'key'
print(col.sweep(sweep)) # Returns a new collection
Removes the item of the dict which satisfies the callback
def sweep(key, value):
return key == 'key'
print(col.sweep(sweep)) # Returns an item (key, value)
Maps the values of the dict and returns a duplicate collection
col.set(1, 5) # Current value = { 1: 5 }
def map(key, value):
return value + 1
print(col.map(map)) # Returns an collection
# Collection with value { 1: 6 }
Will run the function on each item
def foreach(key, value):
print(key)
print(col.foreach(foreach)) # Returns an item (key, value)
Will return a duplicate collection by concatting the both collection
col1 = Collection()
col2 = Collection()
col1.set('key', 'value')
print(col2.concat(col1)) # Will return value
Will work with dicts too
col = Collection()
print(col.extend({ 'key': 'value' })) # Will print value