Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #"Project" name: PyNQ
- #IDEA:
- #LINQ in C# is cool
- #Python doesn't quite need it as it has generatior expressions, sum(), len(), sorted() and itertools.groupby()
- #so let's just wrap a nice SQL-like interface over them in order to make them more familiar
- #Problem: all these lambdas suck. Any better ways to do it?
- #TODOS/QUESTIONS:
- #1. is it a good idea?
- #2. can it be made to not suck (do something with all those lambdas)?
- #3. implement GroupBy()
- class From(object):
- def __init__(self, lst):
- self.lst = (x for x in lst)
- def Where(self, func):
- self.lst = ( x for x in self.lst if func(x))
- return self
- def Select(self, func=lambda x: x):
- self.lst = (func(x) for x in self.lst)
- return self
- def Count(self, func=lambda x: x):
- return len([func(x) for x in self.lst])
- def Sum(self,func=lambda x: x):
- return sum(func(x) for x in self.lst)
- def OrderBy(self, func):
- self.lst= sorted(self.lst, lambda x, y: func(x) - func(y))
- return self
- def __iter__(self):
- return self.lst
- #examples
- table = ((8, 2, 9), (4, 5, 6), (2, 1, 9))
- mySum = From(table)\
- .Where(lambda column: column[2]==9)\
- .Sum(lambda column: column[0])
- print mySum
- myLst = From(table) \
- .Where(lambda column: column[2]==9) \
- .OrderBy(lambda column: column[2]) \
- .Select(lambda column: column[0])
- for item in myLst: print item
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement