All Projects → borzunov → dontasq

borzunov / dontasq

Licence: MIT license
⚡🐍 Extends built-in Python collections with LINQ-style methods

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to dontasq

Mongodb.entities
A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management
Stars: ✭ 204 (+558.06%)
Mutual labels:  linq
Masuit.Tools
该仓库为 https://github.com/ldqk/Masuit.Tools 的镜像仓库,代码更新存在较大的延迟。建议前往源仓库:https://github.com/ldqk/Masuit.Tools
Stars: ✭ 755 (+2335.48%)
Mutual labels:  linq
Kendo.DynamicLinqCore
KendoNET.DynamicLinq implements server paging, filtering, sorting, grouping, and aggregating to Kendo UI via Dynamic Linq for .Net Core App(1.x ~ 3.x).
Stars: ✭ 36 (+16.13%)
Mutual labels:  linq
Go Linq
.NET LINQ capabilities in Go
Stars: ✭ 2,791 (+8903.23%)
Mutual labels:  linq
Typescript.net
A JavaScript-Friendly .NET Based TypeScript Library (Moved)
Stars: ✭ 245 (+690.32%)
Mutual labels:  linq
Linq.ts
linq for typescript
Stars: ✭ 33 (+6.45%)
Mutual labels:  linq
Extralinq
Useful extension methods for .NET sequence and collection types.
Stars: ✭ 202 (+551.61%)
Mutual labels:  linq
multiplex.js
LINQ for JavaScript.
Stars: ✭ 79 (+154.84%)
Mutual labels:  linq
PocoDynamo
C# .NET Typed POCO Client for AWS Dynamo DB
Stars: ✭ 39 (+25.81%)
Mutual labels:  linq
Linq.Expression.Optimizer
System.Linq.Expression expressions optimizer. http://thorium.github.io/Linq.Expression.Optimizer
Stars: ✭ 81 (+161.29%)
Mutual labels:  linq
Linqit
Extend python lists with .NET's LINQ syntax for clean and fast coding. Also known as PINQ.
Stars: ✭ 222 (+616.13%)
Mutual labels:  linq
Morelinq
Extensions to LINQ to Objects
Stars: ✭ 2,833 (+9038.71%)
Mutual labels:  linq
LinqBuilder
LinqBuilder is an advanced implementation of the specification pattern specifically targeting LINQ query generation.
Stars: ✭ 34 (+9.68%)
Mutual labels:  linq
Linqpadless
LINQPad Queries without LINQPad
Stars: ✭ 218 (+603.23%)
Mutual labels:  linq
ObservableComputations
Cross-platform .NET library for computations whose arguments and results are objects that implement INotifyPropertyChanged and INotifyCollectionChanged (ObservableCollection) interfaces.
Stars: ✭ 94 (+203.23%)
Mutual labels:  linq
Linqaf
Low allocation re-implementation of LINQ-to-Objects
Stars: ✭ 203 (+554.84%)
Mutual labels:  linq
OLGA
an Ontology SDK
Stars: ✭ 36 (+16.13%)
Mutual labels:  linq
linq-fns
👴 LINQ for Javascript, written by TypeScript
Stars: ✭ 74 (+138.71%)
Mutual labels:  linq
linq2db.LINQPad
linq2db.LINQPad is a driver for LINQPad.
Stars: ✭ 65 (+109.68%)
Mutual labels:  linq
python-linq-samples
Python Linq Examples: Comparision of C# Linq functional programming to Python
Stars: ✭ 69 (+122.58%)
Mutual labels:  linq

dontasq

Extend built-in Python collections with LINQ-for-objects style methods

Description

The library extends built-in Python collections with methods from Robert Smallshire's asq. Adding methods to built-ins isn't officially allowed, but it's possible to do this in CPython (both 2.x and 3.x) using a hack described in the corresponding section below.

For example:

>>> import dontasq
>>>
>>> [1, 2, 3].select_many(lambda x: (x, x ** 2)).to_tuple()
(1, 1, 2, 4, 3, 9)
>>> 'oh brave new world'.split() \
...                     .where(lambda word: len(word) >= 5) \
...                     .select(str.capitalize) \
...                     .to_list()
['Brave', 'World']

In some cases, this style helps to write functional-esque code that is more clear than code with map, filter and generator expressions: there's no confusion with brackets, and methods are applied in the natural order.

Warning! dontasq uses undocumented CPython features. It's not guaranteed that this features will be maintained in the future Python versions.

Details

During import, dontasq looks for classes in the built-ins namespace, collections and itertools modules. If a class is an iterable and is not a metaclass, the library will append all public methods of asq.queryables.Queryable to it in such a way that a method call:

>>> instance.select(lambda x: x * 2)

Will be equal to:

>>> Queryable(instance).select(lambda x: x * 2)

For example, the methods will be added to list, str, collections.OrderedDict, and itertools.count. You can find a list of all Queryable methods and their description in asq documentation.

If a class already contains an attribute with a coinciding name (e.g. str.join and list.count), this attribute won't be replaced.

Of course, you're able to import other asq modules when using dontasq:

>>> import dontasq
>>> from asq.predicates import *
>>>
>>> words = ['banana', 'receive', 'believe', 'ticket', 'deceive']
>>> words.where(contains_('ei')).to_list()
['receive', 'deceive']

If you want to patch classes from another library, you can use methods dontasq.patch_type and dontasq.patch_module:

>>> import bintrees
>>> import dontasq
>>>
>>> dontasq.patch_type(bintrees.AVLTree)
>>>
>>> dictionary = {1: 'Anton', 2: 'James', 3: 'Olivia'}
>>> bintrees.AVLTree(dictionary).select(lambda x: x * 2).to_list()
[2, 4, 6]

You can find other examples in "tests" directory.

Adding methods to built-ins

The following approach is found in this question on StackOverflow.

Officially, you can get only a protected (read-only) instance of built-ins' __dict__. The trick is that in CPython this instance contains a reference to an original (modifiable) dictionary that can be tracked with gc.get_referents function.

For example, we can add select method to built-in list (unlike dontasq, it's non-lazy in this example):

>>> import gc
>>> gc.get_referents(vars(list))[0]['select'] = lambda self, func: list(map(func, self))
>>>
>>> [1, 2, 3].select(lambda x: x * 2)
[2, 4, 6]

Another possible way is to use forbiddenfruit library that interacts with ctypes.pythonapi module. The both approaches stably work on both Python 2 and 3, but restricted to CPython only.

Installation

You can install the library using pip:

sudo pip install dontasq

Or install a previously downloaded and extracted package:

sudo python setup.py install

Authors

Copyright (c) 2015 Alexander Borzunov

The library name suggested by Robert Smallshire (an author of asq).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].