pyfunctools package
Pyfunctools
Pyfunctools is a module that provides functions, methods and classes that help in the creation of projects in python, bringing functional and object-oriented programming methods.
- class pyfunctools.Array(*args, default=None)
Bases:
object
Class that has common methods in arrays that are not present in the builtin list class.
- Raises
NotImplementedError – Error thrown when a None value is passed in the constructor
Examples
>>> #Create an array/list >>> Array() <Array values=[] > >>> Array(4) <Array values=[None, None, None, None] > >>> Array(1, 2) <Array values=[1, 2] >
- append(x: any)
Append a new item with value x to the end of the Array.
- Parameters
x (any) – object to add
Examples
>>> array = Array(1, 2, 3) >>> array.append(4) >>> array.to_list() [1, 2, 3, 4]
- chunk(size: int = 1) List[list]
Divides the Array object into sublists.
- Parameters
size (int, optional) – Chunk size. Defaults to 1
- Returns
A new list containing the chunks of the Array object
- Return type
list
Examples
>>> array = Aray([1, 2, 3, 4]) >>> array.chunk() [[1], [2], [3], [4]] >>> array.chunk(2) [[1, 2], [3, 4]] >>> array.chunk(3) [[1, 2, 3], [4, 5, 6]]
- concat(*args)
Concatenate into Array
- Returns
The Array instance being changed
- Return type
Examples
>>> a = Array([1, 2]) >>> a.concat(3) <Array values=[1, 2, 3] > >>> a.concat([4, 5], [6, 7]) <Array values=[1, 2, 3, 4, 5, 6, 7] > >>> #Turning into list type >>> a.to_list() [1, 2, 3, 4, 5, 6, 7]
- count(x: any) int
Return the number of times x appears in the array.
Examples
>>> Array(1, 2, 3).count(1) 1 >>> Array(2, 0, 2, 2).count(2) 3 >>> Array(2, 0, 0, 2).count(1) 0
- fill(fill_with: any)
Replaces/fill the values in the array without changing the len
- Returns
The Array instance being changed
- Return type
Examples
>>> a = Array([1, 2]) >>> a.fill('a') <Array values=['a', 'a'] > >>> #Turning into list type >>> a.to_list() ['a', 'a']
- filter(func) list
Method to filter a Aray item
- Parameters
func (function) – The callback function takes an item and index, and must return a boolean
Examples
>>> array = Array(1, 2, 3, 4) >>> array.filter(lambda item, index: item % 2 == 0) [2, 4]
- forEach(func)
Calls a function for each item, passing the item itself and the index
- Parameters
func (function) – Callback function
Examples
>>> array = Array(1, 2) >>> array.forEach(lambda item, index: print(f'{item}, {index}')) 1, 0 2, 1
- includes(item: any) bool
Test if an item exists in this Array
- Parameters
item (any) – Item to test
Examples
>>> array = Array(1, 2, 3) >>> array.includes(4) False >>> array.includes(1) True
- index_of(obj: any) int
The method returns the first index at which a given element can be found in the array, or -1 if it is not present.
- Parameters
obj (any) – Element to locate in the array.
- Returns
-1 if not exists in the array
- Return type
int
Examples
>>> array = Array(1, 2, 3) >>> array.index_of(4) -1 >>> array.index_of(1) 0
- static is_array(arr: any) bool
Test an object and return true if it is an instance of Array
Examples
>>> array = Array() >>> Array.is_list(array) True >>> Array.is_list({}) False
- is_empty() bool
Check if Array is empty
Examples
>>> Array().is_empty() True >>> Array(1, 2).is_empty() False
- static is_list(arr: any) bool
Test an object and return true if it is an instance of list
Examples
>>> Array.is_list([]) True >>> Array.is_list({}) False
- map(func) list
Function to create a new list based on callback function return
- Parameters
func (function) – A callback function that will be executed every iteration and should return something for reduce assemble new list.
Examples
>>> array = Array([1, 2, 3, 4]) >>> array.map(lambda item, index: item if item % 2 == 0 else None) [2, 4]
- pop(pos: int = - 1) any
Removes the element at the specified position.
- Parameters
pos (int, -1) – Position of the element to be removed and returned.
Examples
>>> array = Array(1, 2, 3) >>> array.pop() 3 >>> array.pop(0) 1 >>> array <Array values=[2] >
- reduce(func, initial=[])
Function to create a new object based on callback function return
- Parameters
func (function) – A callback function that will be executed every iteration and should return something for reduce assemble new object
initial (any, []) – Initial return value.
- Raises
NotImplementedError – func not defined or equal to None.
Examples
>>> array = Array([1, 2, 3, 4, 5, 6]) >>> def func(accumulator, item, index): ... if item % 2 == 0: ... return accumulator.append(item) ... return ... >>> array.reduce(func) [2, 4, 6]
Note
if the callback function never returns anything, reduce will return the initial value itself
- repetitions() dict
Parses and returns all repetitions in the array.
- Returns
A dictionary of type item: int(repetitions)
- Return type
dict
Examples
>>> Array(*'Pyfunctools').repetitions() {"P": 1, "y": 1, "f": 1, "u": 1, "n": 1, "c": 1, "t": 1, "o": 2, "l": 1, "s": 1} >>> Array(*'Python').repetitions() {"P": 1, "y": 1, "t": 1, "h": 1, "o": 1, "n": 1}
- reverse()
Reverses the sorting order of the elements.
Examples
>>> array = Array(1, 2, 3) >>> array.reverse() >>> array <Array values=[3, 2, 1] >
- shift()
Removes the first element from an array and returns that removed element.
Examples
>>> array = Array(1, 2, 3) >>> array.shift() 1 >>> array <Array values=[2, 3] > >>> array.shift() 2 >>> array <Array values=[3] >
- to_list() list
Convert the array object to the builtin type list.
Note
If you prefer you can use list compression on the Array instance
- unshift(*args)
Adds one or more elements to the beginning of an array and returns a array modified.
- pyfunctools.get_version(release: bool = False)
Get simple version or full version/release of pyfunc
- Parameters
release (bool, False) – if true, return full version of package
Examples
>>> get_version() '0.1' >>> get_version(True) '0.1.0'