pip install sidis
import sidis
from sidis import *
Sidis supports extensible typecasting between iterables and non-iterables, letting you do intuitive conversions such as:
cast(0,list)
cast([0.9,1.1],int)
or define your own rules:
mycast = Caster()
mycast[list][float] = lambda t: sum(t)
mycast([0.9,1.1],float)
Sidis lets you sort python objects by the result of maps over those objects, and provides convenient conversion functions.
from sidis import sort, convert
Convert the elements of a list to binary arrays, and sort by the length of the array:
sort([0,10,3,5],by=convert,key=lambda t:len(t[-1]))
from sidis import pipe #like functools.partial, but also allows for typecasting of input and output data
Now let's convert to hex:
sort([0.9,10.5,3.1,5.5],by=pipe(convert,to=hex,otype=int)) #convert the elements `otype` into integers, then hex
a=[1]
get(a,0) #get 0th level or attribute of a
[give(a,0,a[0]+1) for i in range(10)] #and can assign arbitrary objects without writing entire loops!
a
and can apply this level of control to more complex datastructures:
import networkx as nx
g=nx.DiGraph() #create a ring with a loop, 0->1->2->0->0
g.add_edges_from([(0,1),(1,2),(2,0)])
g.add_edges_from([(0,0)])
g.in_degree()
from sidis import get
get(g,'in_degree',0) #get in-degree of node 0
and use this to sort with more abstract methods:
sort(g.nodes,by=g.in_degree)
sort(g.nodes,by=g.edges,key=lambda t:len(list(t[-1])))
or even give attributes to objects:
from sidis import give
give(g,'nodes',0,newattr='for fun')
g.nodes[0]['newattr']
[give(g,'edges',e,weight=np.random.rand(1)) for e in g.edges]
sort(g.edges,'weight')
sidis also extends mapping using maps
, which lets you pass in an object and a series of functions and evaluate those functions independently or sequentially over the input to the desired depth:
f1=lambda t:t
f2=lambda t:t+1
f3=lambda t:t+2
maps(0,f1,f2,f3) #apply them individually
maps(0,f1,f2,f3,depth=-1) #apply them sequentially
maps([0,1],f1,f2,f3,depth=1) #apply f1 to 0, then return f2(f1(0)) and f2(f1(0)), then repeat for 1
from sidis import depth,flatten,unflatten,fill,Template,RNG
depth([[0]])
depth({'a':0,'b':{'c':1,'d':3}})
flatten({'a':0,'b':{'c':1,'d':3}})
unflatten({'a': 0, 'b,c': 1, 'b,d': 3})
fill([[1],[2,3]],fillwith=1000,mask=False)
Template('''
fill out your _name
and provide {0} ZIP _iter, lambda t: 'embedded iterable functions!'
''',_name='name and information',_iter=range(1))
rng=RNG(0) #globally stable
rng.random(0,1,shape=(2,2))