Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Extend python lists operations using .NET's LINQ syntax for clean and fast coding.

License

NotificationsYou must be signed in to change notification settings

avilum/linqit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

pip install linqit


Stop using loops, complex conditions, list comprehension and filters.
Doesn't it looks better?

fromlinqitimportList# Go ahead and fill the list with whatever you want... like a list of <Programmer> objects.programmers=List()Avi=type("Avi", (), {})Entrepreneur=type("Entrepreneur", ('talented'), {})elon_musk=Entrepreneur(talented=True)# Then play:last_hot_pizza_slice= (programmers.where(lambdae:e.experience>15)    .except_for(elon_musk)    .of_type(Avi)    .take(3)# [<Avi>, <Avi>, <Avi>]    .select(lambdaavi:avi.lunch)# [<Pizza>, <Pizza>, <Pizza>]    .where(lambdap:p.is_hot()andp.origin!="Pizza Hut")    .last()# <Pizza>    .slices.last()# <PizzaSlice>)# What do you think?

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

Methods:

allanyconcatcontainsdistinctexcept_forfirstget_by_attrintersectlastselectskiptakewhereof_type

Properties:

summinmaxavgsorted

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

importListclassPerson():def__init__(self,name,age):self.name=nameself.age=agedef__repr__(self):returnf'Person(name="{self.name}", age={self.age})')# Creating a list of peopleavi,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

people=people.where(lambdap:p.age>23)# [<Person name="Bill" age="41">, <Person name="Bob" age="77">, <Person name="Harry" age="55">]people.first()# <Person name="Bill" age="41">people.last()# <Person name="Harry" age="55">people.any(lambdap:p.name.lower().startswith('b'))# Truepeople.where(age=55)# [<Person name="Harry" age="55">]people.skip(3).any()# Falsepeople.skip(2).first()# <Person name="Harry" age="55"># 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(lambdap:20>=p.age>=13)danny=teenagers.first(lambdat:t.name=='Danny')# <Person name="Danny" age="16">oldest_teen=teenagers.order_by(lambdat:t.age).last()# <Person name="John" age="17">

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(lambdan:n=='Danny')# ['Chris']teenagers.age.min# 16teenagers.age.avg# 17teenagers.age.max# 18

Test Coverage

linqitgit:(master) ✗coveragereportNameStmtsMissCover-----------------------------------------linqit/__init__.py20100%linqit/linq_list.py1011189%tests/__init__.py00100%tests/test_list.py2030100%-----------------------------------------TOTAL3061196%

[8]ページ先頭

©2009-2025 Movatter.jp