SHARE
TWEET

Shenpen

a guest Feb 11th, 2009 140 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #"Project" name: PyNQ
  2. #IDEA:
  3. #LiNQ in C# is cool
  4. #Python doesn't quite need it as it has generatior expressions, sum(), len(), sorted() and itertools.groupby()
  5. #so let's just wrap a nice SQL-like interface over them in order to make them easier to use
  6.  
  7. #Problem: all these lambdas suck. Any better ways to do it?
  8.  
  9. #TODOS:
  10. #DISTINCT (just a special case of GroupBy)
  11. #make Select accept multiple parameters
  12.  
  13. #UPDATE 11/02/09 : better version with GroupBy() and less lambdas
  14.  
  15. from itertools import groupby
  16.  
  17. class From(object):
  18.     def __init__(self, lst):
  19.         self.lst = (x for x in lst)
  20.         self.Sum = self.SimpleSum
  21.         self.Count = self.SimpleCount
  22.    
  23.     def Where(self, func):
  24.         self.lst = ( x for x in self.lst if func(x))
  25.         return self
  26.    
  27.     def Select(self, func=lambda x: x):
  28.         self.lst = (func(x) for x in self.lst)
  29.         return self
  30.  
  31.     def SimpleCount(self, func=lambda x: x):
  32.         return len([func(x) for x in self.lst])
  33.  
  34.     def SimpleSum(self,func=lambda x: x):
  35.         return sum(func(x) for x in self.lst)
  36.    
  37.     def OrderBy(self, func):
  38.         self.lst= sorted(self.lst, lambda x, y: cmp(func(x), func(y)) )
  39.         return self
  40.    
  41.     def GroupBy(self, func):
  42.         self.lst = groupby(sorted(self.lst,key=func),func)
  43.         self.Sum = self.GroupedSum
  44.         self.Count = self.GroupedCount
  45.         return self
  46.    
  47.     def GroupedSum(self, func):
  48.         self.lst = ( (key, sum(func(x) for x in groups) ) for key, groups in self.lst)
  49.         return self
  50.    
  51.     def GroupedCount(self, func=lambda x: x):
  52.         self.lst = ( (key, len([ func(x) for x in groups] )) for key, groups in self.lst)
  53.         return self
  54.        
  55.        
  56.     def __iter__(self):
  57.         return self.lst
  58.  
  59. #EXAMPLE
  60.    
  61. sales =     (('Scotland', 'Edinburgh', 20000),
  62.              ('Scotland', 'Glasgow', 12500),
  63.              ('England', 'London', 90000),
  64.              ('Wales', 'Cardiff', 29700),
  65.              ('Wales', 'Bangor', 12800),
  66.              ('England', 'London', 90000),
  67.              ('England', 'London', 90000),                
  68.              ('England', 'Manchester', 45600),            
  69.              ('England', 'Liverpool', 29700))
  70.  
  71. from operator import itemgetter as column
  72.  
  73. #SELECT City, SUM(Sales)) FROM sales WHERE Country<>"Wales" GROUP BY City
  74. myLst = From(sales)\
  75.         .Where(lambda col: col[0] != 'Wales')\
  76.         .GroupBy(column(1))\
  77.         .Sum(column(2))
  78.  
  79. for x in myLst: print x
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top