linqit

by avilum

avilum / linqit

Extends python lists with LINQ functionality for clean, fast coding.

129 Stars 3 Forks Last release: Not found Apache License 2.0 32 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

Linqit!

Extends python's list builtin with fun, robust functionality - .NET's Language Integrated Queries (Linq) and more.
Write clean code with powerful syntax.

Stop using loops, complex conditions, list comperhension and filters.
Doesn't it looks better?
```python programmers = List() Avi = type('Avi', (), {})

Go ahead and fill the list with whatever you want... like a list of objects.

Then play:

lasthotpizzaslice = programmers.where(lambda e:e.experience > 15) .exceptfor(elonmusk) .oftype(Avi) .take(3) # [, , ] .select(lambda avi:avi.lunch) # [, , ] .where(lambda p:p.is_hot() and p.origin != 'Pizza Hut'). .last() # .slices.last() #

What do you think?

We all use multiple aggregations in our code, while multiple filters/comprehentions are not pythonic at all.
The whole idea is is to use it for nested, multiple filters/modifications :).
Some of the methods might look rediculous for a single call, comparing to the regular python syntax.
Here are some use cases:

Methods:

all any concat contains distinct exceptfor first getbyattr intersect last select skip take where oftype ```

Properties:

sum
min
max
avg
sorted

Deeper - Let's play with a list of people, a custom type.

import List

class Person(): def init(self, name, age): self.name = name self.age = age

def __repr__(self):
    return '<person name="{}" age="{}">'.format(self.name, self.age)

Creating a list of people

avi, bill, bob, harry = Person('Avi', 23), Person('Bill', 41), Person('Bob', 77), Person('Harry', 55)

people = List(avi, bill, bob, harry)

Use LINQ selections, write cleaner code

old_people = people.where(lambda p: p.age > 23) # It's a joke! :) [, , ]
old_people.first()                                              # 
old_people.last()                                               # 
old_people.any(lambda p: p.name.lower().startswith('b'))        # True
old_people.where(lambda p: p.age == 55)                         # []
old_people.skip(3).any()                                        # False
old_people.skip(2).first()                                      # 

Isn't it better than "for", "if", "else", "filter", "map" and list comprehensions in the middle of your code?

More selections

new_kids_in_town = [Person('Chris', 18), Person('Danny', 16), Person('John', 17)]
people += new_kids_in_town # Also works: people = people.concat(new_kids_in_town)

teenagers = people.where(lambda p: 20 >= p.age >= 13) danny = teenagers.first(lambda t: t.name == 'Danny') # oldest_teen = teenagers.last() #

Let's make python more dynamic

names = people.name                                             # ['Avi', 'Bill', 'Bob', 'Harry', 'Chris', 'John']
ages = people.age                                               # [23, 41, 77, 55, 18, 17]
teenagers_names = teenagers.name                                # ['Chris', 'Danny', 'John']
teenagers_names.take(2).except_for(lambda n: n == 'Danny')      # ['Chris']
teenagers.age.min                                               # 16
teenagers.age.avg                                               # 17
teenagers.age.max                                               # 18

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.