py-enumerable

py-linq

py-linq is a little wrapper package that I wrote a few years ago for querying and transforming collections of Python objects. I found that the built-in Python querying and transforming tools were lacking. Specifically, the Python API made the code unreadable and difficult to understand. It made me long for the LINQ API’s available in .NET. So I decided to sit down and try to write it.

Getting started

To get started using py-linq, you will need to install it from PyPI.

pip install py-linq

To start querying and transforming your Python collection you will need to import Enumerable.

from py_linq import Enumerable

Enumerable

Enumerable is just the access point for the py-linq API. There are 2 ways to instantiate an Enumerable object.

my_collection = Enumerable()
my_collection = Enumerable(collection)

The collection class has to implement the __iter__ dunder. The default constructor Enumerable() is just Enumerable(collection) where collection is []. Enumerable itself is an iterable.

LINQ methods

The methods encapsulated by the Enumerable class can be either executing functions or non-executing. Executing functions will iterate over the collection when it is called. Non-executing functions will not iterate over the collections. These functions will be executed only when the collection does get iterated over.

Once you have created an Enumerable instance, the LINQ methods will become available to you. The methods implemented at this point are:

  1. select
  2. order_by
  3. order_by_descending
  4. skip
  5. take
  6. where
  7. select_many
  8. add
  9. concat
  10. join
  11. intersect
  12. except_
  13. to_list
  14. count
  15. sum
  16. min
  17. max
  18. avg
  19. median
  20. any
  21. element_at
  22. element_at_or_default
  23. first
  24. first_or_default
  25. last
  26. last_or_default
  27. contains
  28. group_by
  29. distinct
  30. group_join
  31. union
  32. all
  33. aggregate
  34. append
  35. prepend
  36. empty
  37. range
  38. repeat
  39. reverse
  40. skip_last
  41. skip_while
  42. take_last
  43. take_while
  44. zip
  45. default_if_empty
  46. single
  47. single_or_default