Package: micropy
My little lib of Python goodiesMicropy is auto-formatted usingyapf.Nice thingsdig()CSS selector like deep value grabbing from almost any object.>>>frommicropyimportdig>>>dig.xget((1,2,3),1)2>>>dig.xget({'foo':'bar'},'foo')'bar'>>>dig.dig({'foo':1,'bar':[1,2,3]},'bar.1')2>>>dig.dig({'foo':1,'bar':[1,{'baz':'jox'},3]},'bar.1.baz')'jox'>>>The difference betweendig.dig()andfuncy.get_in()is that you
can use shell-like blob patterns to get several values keyed by similar
names:>>>frommicropyimportdig>>>res=dig.dig({'foo':1,'foop':2},'f*')>>>res[foo=1:int,foop=2:int]>>># (textual representation of an indexable object)>>>res[0]foo=1:int>>>res[1]foop=2:int>>>Programmatic class creationProgrammatic creation of arbitrary named classes in module definition,
add methods using a decorator notation:>>>frommicropyimportlang>>>mystuff=(('Foo',1),('Bar',2))>>>forname,numinmystuff:locals()[name]=lang.mkclass(name,**{'num':num})>>>Foo<class'micropy.lang.Foo'>>>>Foo.num1>>>\...@Foo.classmethod...defmyclassmethod(cls,x):...returnx+1>>>Foo.myclassmethod(1)2>>>>>>\...@Foo.staticmethod...defmystaticmethod(x,y):...returnx+y>>>Foo.mystaticmethod(1,2)3>>>\...@Foo.method...defmymethod(self,x):...self.y=self.num+x...returnself.y>>>foo=Foo()>>>foo.mymethod(1)2>>>foo.y2>>>micropy module with developer convenience toolsThemicropy.microscopemodule contains utilities that aid
development. It has to ways to inspect live objects:Via ‘AbneuYAML’AbneuYAML is“Almost, but not entirely unlike YAML”. Objects dumped
to ‘AbneuYAML’ should be easy to get a visual overview of for humans.To dump any object:>>>frommicropyimportmicroscope>>>classCls:pass...>>>c=Cls()>>>c.foo,c.bar=1,2>>>c.sub=Cls()>>>c.sub.foo,c.sub.bar,c.sub.baz=3,4,[1,2]>>>encoded=microscope.abneuyaml(c)>>>print(encoded)#doctest: +ELLIPSIS<__main__.Clsobjectat0x...>:Clsfoo=1:intbar=2:intsub=<__main__.Clsobjectat0x...>:Clsfoo=3:intbar=4:intbaz=[1,2]:list>>>A simple way of creating small DSL’s using Python operator overloading.>>>frommicropyimportlang>>>\...classPipingExample(lang.Piping):...def__add__(self,value)->lang.Piping:...self.queue(lambdaa,b:a+b,value)...returnself...>>>simplest_pipe=PipingExample(10)>>>res=simplest_pipe+10+20>>>res()40>>>Mostly, you’ll want to use the pipe operator to define simple
composition:>>>frommicropyimportlang>>>incr=lambdax:x+1>>>showr="It is{}!".format>>>(lang.ComposePiping(5)>>incr>>incr>>showr)()'It is 7!'>>>‘Call by type’ convenience objects>>>frommicropyimportlang>>>foo=lang.Match({int:lambdax:x*100,str:lambdax:f'Hello{x}'})>>>foo(10)1000>>>foo('bar')'Hello bar'>>>Narrowable collectionsUses indexes to narrow collections to fewer values. You can narrow by
type, a predicate function or value equality. The return value is always
a newNarrowablederived type from the initial value. Therefore, you
can chain several narrowing operations in the same expression.Errors raised by the narrowing predicates are considered misses.Some examples:Narrow by type>>>frommicropy.primitivesimportnarrowable>>>narrowable((1,2,3,'foo','bar'))[int](1,2,3)>>>Narrow by callable>>>frommicropy.primitivesimportnarrowable>>>narrowable((1,2,3))[lambdax:x>1](2,3)>>>>>>narrowable((1,2,3,'foo','bar'))[int](1,2,3)>>>narrowable((1,2,3,'foo','bar'))[lambdax:x>1](2,3)>>># Note, swallows ValueError raised by 'foo' > 1 etc>>>Supress empty iterable objects>>>frommicropy.primitivesimportnarrowable>>>narrowable([[1],[2],[],[]])[lambdax:x[0]][[1],[2]]>>>Narrow using exact match>>>frommicropy.primitivesimportnarrowable>>>narrowable((1,2,3,'foo'))['foo']('foo',)>>>Narrow using a regexp>>>frommicropy.primitivesimportnarrowable>>>importre>>>narrowable(('foo','fom','jox',8,'fim'))[re.compile('fo.*').match]('foo','fom')>>>Combine>>>frommicropy.primitivesimportnarrowable>>>narrowable((1,2,3,'foo','bar'))[str]['foo']('foo',)>>>Go deeper>>>frommicropy.primitivesimportnarrowable>>>narrowable((1,2,3,(41,42,43)))[tuple][0][lambdax:x>41](42,43)>>>No matches foundIf no element matches, an empty version of the collection parameter will
be returned:>>>frommicropy.primitivesimportnarrowable>>>narrowable((1,2,3))[lambdax:x>3]()>>>