Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/avilum/linqit

Extend python lists operations using .NET's LINQ syntax for clean and fast coding.
https://github.com/avilum/linqit

awesome batch-processing clean clean-code csharp development-tools dotnet efficiency filters lazy-loading linq metadata metaprogramming powerful python python3 pythonic robustness syntax

Last synced: about 1 month ago
JSON representation

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

Awesome Lists containing this project

README

        

# Linqit!
Extends python's list builtin with fun, robust functionality - .NET's Language Integrated Queries (Linq) and more.

Write clean code with powerful syntax.


```shell script
pip install linqit
```


Stop using loops, complex conditions, list comprehension and filters.

Doesn't it looks better?

```python
from linqit import List

# Go ahead and fill the list with whatever you want... like a list of objects.
programmers = List()
Avi = type("Avi", (), {})
Entrepreneur = type("Entrepreneur", ('talented'), {})
elon_musk = Entrepreneur(talented=True)

# Then play:
last_hot_pizza_slice = (
programmers.where(lambda e: e.experience > 15)
.except_for(elon_musk)
.of_type(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/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:
```
all
any
concat
contains
distinct
except_for
first
get_by_attr
intersect
last
select
skip
take
where
of_type
```
#### Properties:
```
sum
min
max
avg
sorted
```

## Deeper - Let's play with a list of people, a custom type.
```python
import List

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

def __repr__(self):
return f'Person(name="{self.name}", age={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
```python
people = people.where(lambda p: p.age > 23) # [, , ]
people.first() #
people.last() #
people.any(lambda p: p.name.lower().startswith('b')) # True
people.where(age=55) # []
people.skip(3).any() # False
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
```python
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.order_by(lambda t: t.age).last() #
```

## Let's make python more dynamic
```python
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
```

# Test Coverage
```python
➜ linqit git:(master) ✗ coverage report
Name Stmts Miss Cover
-----------------------------------------
linqit/__init__.py 2 0 100%
linqit/linq_list.py 101 11 89%
tests/__init__.py 0 0 100%
tests/test_list.py 203 0 100%
-----------------------------------------
TOTAL 306 11 96%
```