May-01-2017, 02:36 AM
How do you guys do method chaining? Everything I've tried to break method calls across multiple lines looks pretty ugly.
Or do you guys just not do chaining at all?
Or do you guys just not do chaining at all?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class query( object ): def __init__( self , source): self .filters = [] self .source = source def where( self , filter ): self .filters.append( filter ) return self def get( self ): all_filters = lambda x: all (func(x) for func in self .filters) return list ( filter (all_filters, self .source)) things = query( range ( 1000 ) ).where( lambda x: x % 2 = = 0 ).where( lambda x: x % 3 = = 0 ).where( lambda x: x % 4 = = 0 ).where( lambda x: x % 5 = = 0 ) #things = query(range(1000)).where( # lambda x: x % 2 == 0).where( # lambda x: x % 3 == 0).where( # lambda x: x % 4 == 0).where( # lambda x: x % 5 == 0) print (things.get()) |
Output:[0, 60, 120, 180, 240, 300, 360, 420, 480, 540, 600, 660, 720, 780, 840, 900, 960]