Skip to main content

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 isn’t 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)

So the methods will be added to classes such as list, str, collections.OrderedDict, or 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).

Metadata

Release files for dontasq 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dontasq 0.1
File Size Uploaded Provenance
dontasq-0.1.tar.gz 5.5 kB Without provenance Details

Release files / dontasq-0.1.tar.gz

Download URL dontasq-0.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
584145809416bf30b07edcbb197d51605bfdb6c360bb48dee04f924636f9ad3e
BLAKE2b-256 checksum
How to use checksums
c0afd1a093fd0846696c0284beee105b2f9d0ea97e2758a24f44e7b7317aff2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for dontasq 0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page