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.

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(lambdax:x*2)

Will be equal to:

>>>Queryable(instance).select(lambdax: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:

Adding methods to built-ins

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):