Skip to main content

A LINQ-like library for Python inspired by C# LINQ using itertools and more-itertools internally.

Project description

Linq Tool

Build Status PyPI Version License

Linq is a Python package that provides LINQ-like functionality for performing common operations on iterables. Inspired by LINQ from C#, this package allows you to easily manipulate collections in a functional style.

Features

  • Select: Project each element of a sequence into a new form.
  • Where: Filter elements based on a predicate.
  • GroupBy: Group elements by a key selector function.
  • OrderBy: Sort elements based on a key.
  • Distinct: Remove duplicate elements.
  • And many more!

Installation

You can install Linq via pip:

pip install linq-tool

Usage Examples

You might create a Linq instance and iterate throughout the object

linq = Linq([1, 2, 3])
for item in linq:
    print(item) # 1

The object is an iterable, if you need to use it as list, you need to invoke the to_list method

Select

from linq_tool import Linq

linq = Linq([1, 2, 3])
result = linq.select(lambda x: x * 2).to_list()
print(result)  # Output: [2, 4, 6]

Where

linq = Linq([1, 2, 3, 4])
result = linq.where(lambda x: x % 2 == 0).to_list()
print(result)  # Output: [2, 4]

GroupBy

linq = Linq(['apple', 'banana', 'apricot', 'blueberry'])
result = linq.group_by(lambda x: x[0]).to_list()
print(result)  # Output: [('a', ['apple', 'apricot']), ('b', ['banana', 'blueberry'])]

OrderBy

linq = Linq([{'name': 'apple', 'price': 5}, {'name': 'banana', 'price': 3}])
result = linq.order_by(lambda x: x['price']).to_list()
print(result)  # Output: [{'name': 'banana', 'price': 3}, {'name': 'apple', 'price': 5}]

Distinct

linq = Linq([1, 2, 2, 3, 4, 4])
result = linq.distinct().to_list()
print(result)  # Output: [1, 2, 3, 4]

Take

linq = Linq([1, 2, 2, 3, 4, 4])
result = linq.distinct().to_list()
print(result)  # Output: [1, 2, 3, 4]

Skip

linq = Linq([1, 2, 3, 4, 5])
result = linq.skip(2).to_list()
print(result)  # Output: [3, 4, 5]

FirstOrDefault

linq = Linq([1, 2, 3])
result = linq.first_or_default()
print(result)  # Output: 1

empty_linq = Linq([])
result = empty_linq.first_or_default(42)
print(result)  # Output: 42

LastOrDefault

linq = Linq([1, 2, 3])
result = linq.last_or_default()
print(result)  # Output: 3

empty_linq = Linq([])
result = empty_linq.last_or_default(42)
print(result)  # Output: 42

Any

linq = Linq([1, 2, 3])
result = linq.any(lambda x: x > 2)
print(result)  # Output: True

result = linq.any(lambda x: x > 3)
print(result)  # Output: False

All

linq = Linq([1, 2, 3])
result = linq.all(lambda x: x > 0)
print(result)  # Output: True

result = linq.all(lambda x: x > 1)
print(result)  # Output: False

Count

linq = Linq([1, 2, 3, 4])
result = linq.count()
print(result)  # Output: 4

TakeWhile

linq = Linq([1, 2, 3, 4, 5])
result = linq.take_while(lambda x: x < 4).to_list()
print(result)  # Output: [1, 2, 3]

SkipWhile

linq = Linq([1, 2, 3, 4, 5])
result = linq.skip_while(lambda x: x < 4).to_list()
print(result)  # Output: [4, 5]

SkipWhile

linq = Linq([1, 2, 3, 4, 5])
result = linq.skip_while(lambda x: x < 4).to_list()
print(result)  # Output: [4, 5]

ZipWith

linq = Linq([1, 2, 3])
result = linq.zip_with(['a', 'b', 'c']).to_list()
print(result)  # Output: [(1, 'a'), (2, 'b'), (3, 'c')]

ZipLongest

linq = Linq([1, 2, 3])
result = linq.zip_longest_with(['a', 'b'], fillvalue='x').to_list()
print(result)  # Output: [(1, 'a'), (2, 'b'), (3, 'x')]

Batch

linq = Linq([1, 2, 3, 4, 5, 6])
result = linq.batch(2).to_list()
print(result) # [(1, 2), (3, 4), (5, 6)]

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

linq_tool-0.1.14.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

linq_tool-0.1.14-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file linq_tool-0.1.14.tar.gz.

File metadata

  • Download URL: linq_tool-0.1.14.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for linq_tool-0.1.14.tar.gz
Algorithm Hash digest
SHA256 bc48d16929f2ea06a9b7116b974cdc410364f2cd71256bfa0052f2ff35a1e2c1
MD5 f81eaf64ae57524a96bb1948361b9e72
BLAKE2b-256 38fcca4399f572c4e515b9aab819ecde0ee126a6fb8895d49cef01f7046fce33

See more details on using hashes here.

File details

Details for the file linq_tool-0.1.14-py3-none-any.whl.

File metadata

  • Download URL: linq_tool-0.1.14-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for linq_tool-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 37ac0ac81a0337a7751633ba7396db4f32f8fa16f873cf13aa14e5321498cb90
MD5 981b7d6b8a36bfb1d56f8d5c0efd260a
BLAKE2b-256 498820e38c368d1bfe78bd49b1c09c4ab33cebf693e867486a88f1fc0de2fb96

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page