Package: linq-t
Installpip install -U LinqAdditional: Some magic here:Mix Linq with
PipeHere is an example to get top 10 frequent pixels in a picture.fromlinqimportFlowimportnumpyasnpdefmost_frequent(arr:np.ndarray)->np.ndarray:returnFlow(arr.flatten())\.GroupBy(lambda_:_)\.Then(lambdax:x.items())\.Map(lambdak,v:(k,len(v)))\.Sorted(by=lambda_,v:-v)\.Take(10)\.Map(lambdak,_:k)\.ToList()\.Then(np.array).Unboxed()About LinqThe well-known EDSL in .NET,Language Integrated Query, in my
opinion, is one of the best design in .NET environment.Here is an example of C# Linq.// Calculate MSE loss.
/// <param name="Prediction"> the prediction of the neuron network</param>
/// <param name="Expected"> the expected target of the neuron network</param>Prediction.Zip(Expected,(pred,expected)=>Math.Square(pred-expected)).Average()It’s so human readable and it doesn’t cost much.Reference:Microsoft .NET general introduction =>LINQ: .NET
Language-Integrated
Query.Wikipedia =>Language Integrated
Query.And there are so many scenes very awkward to Python programmer, usingLinqmight help a lot.Awkward Scenes in Pythonseq1=range(100)seq2=range(100,200)zipped=zip(seq1,seq2)mapped=map(lambdaab:ab[0]/ab[1],zipped)grouped=dict();group_fn=lambdax:x//0.2foreinmapped:group_id=group_fn(e)ifgroup_idnotingrouped:grouped[group_id]=[e]continuegrouped[group_id].append(e)foreingrouped.items():print(e)The codes seems to be too long…Now we extract the functiongroup_by:defgroup_by(f,container):grouped=dict()foreincontainer:group_id=f(e)ifgroup_idnotingrouped:grouped[group_id]=[e]continuegrouped[group_id].append(e)returngroupedres=group_by(lambdax:x//0.2,map(lambdaab[0]/ab[1],zip(seq1,seq2)))Okay, it’s not at fault, however, it makes me upset —— why do I have to
write these ugly codes?Now, let us try Linq!fromlinqimportFlow,extension_stdseq=Flow(range(100))res=seq.Zip(range(100,200)).Map(lambdafst,snd:fst/snd).GroupBy(lambdanum:num//0.2).Unboxed()How doesLinq.pywork?There is a core class object,linq.core.flow.Flow, which just has
one memberstream.When you want to get a specific extension method fromFlowobject,
thetypeof itsstreammember will be used to search whether
the extension method exists.In other words, extension methods are binded with the type(precisely,{type.__module__}.{type.__name__}).classFlow:__slots__=['stream']def__init__(self,sequence):self.stream=sequencedef__getattr__(self,k):forclsinself.stream.__class__.__mro__:namespace=Extension['{}.{}'.format(cls.__module__,cls.__name__)]ifkinnamespace:returnpartial(namespace[k],self)raiseNameError("No extension method named `{}` for{}.".format(k,'{}.{}'.format(object.__module__,object.__name__)))def__str__(self):returnself.stream.__str__()def__repr__(self):returnself.__str__()Extension MethodHere are three methods for you to do so.Firstly, you can useextension_stdto add extension methods for
all Flow objects.Next, you useextension_class(cls: type)to add extension methods
for all Flow objects whose memberstream’s type is named{cls.__module}.{cls.__name__}.Finally, you can useextension_class(cls_name: str,of_module='builtins')to add
extension methods for all Flow objects whose memberstream’s type
is named is named{of_module}.{cls_name}.(This way to make extension methods is for theimplicit typesin
Python, each of which cannot be got except from its instances’ meta
member__class__.)@extension_std# For all Flow objectsdefAdd(self,i):returnFlow(self.stream+(i.streamifisinstance(i,Flow)elsei)))@extension_class(int)# Just for type `int`defAdd(self,i):returnFlow(self.stream+(i.streamifisinstance(i,Flow)elsei)))@extension_class_name('int',of_module=int.__module__)# Also for type `int`.defAdd(self,i):returnFlow(self.stream+(i.streamifisinstance(i,Flow)elsei)))Documents of Standard Extension MethodsNote: Docs haven’t been finished yet.General(can be used by all Flow objects)UnboxedSumEnumMapReduceThenEachAggregateZipSortedArgSortedGroupGroupByTakeTakeWhileDrop|SkipConcatToListToTupleToDictToSetAllAnyListExtendedExtendSortReversedReverseSetIntersectsUnionHow to ContributeDesign thestandard
libraryforLinq.py.Write documents for the standard library and tutorials about how to
useLinq.py.JoinLinqPy Roomto discuss
about any aspects ofLinq.py.Feel free to pull requests here.