Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

scientific-dev/python-collection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Collection

A utility data structure for python! This enpowers the power of dicts to the next level!
This module is inspired from discordjs collections

Links

Installation

$ pip install python-collection

Usage

from Collection import Collection

mycol = Collection()

mycol.set('key', 'value')
print(mycol.get('key')) # Prints 'value'!

Properties

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), ...]

Basic methods

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!

Utility methods

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 

Extending a Collection

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

Cloning

Will return a duplicate collection of the current collection

print(col.clone())

To a object

Convert the collection dict to object

col = Collection()

col.set('key', 'value')
obj = col.to_object()

print(obj.key) # Returns value

List methods

Find

Find an item in the items of dict

def find(key, value):
    return key == 'key'

print(col.find(find)) # Returns an item (key, value)

Some

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

Find an item in the items of dict

def find(key, value):
    return key == 'key'

print(col.find(find)) # Returns an item (key, value)

FindMany

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)

Sweep

Removes the item of the dict which satisfies the callback

def sweep(key, value):
    return key == 'key'

print(col.sweep(sweep)) # Returns nothing

Filter

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

Sweep

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

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 }

ForEach

Will run the function on each item

def foreach(key, value):
    print(key)

print(col.foreach(foreach)) # Returns an item (key, value)

Concat

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

Releases

No releases published

Packages

No packages published