Advanced python techniques
Here are a few tricks in Python which will really make your work easier. This article is recommended for advanced users.
Python is full of amazing functions and tricks. Inevitably you think Wow! You are cool, Python! We joined together a few functions we really like. We hope that you’ll find them useful and say Wow! I didn’t know it.
The source code is at GitHub, if you have ideas how to improve it – welcome!
A generator is an object that creates a sequence of values. It can be used as an iterator, which means you can use it with the operator “for” or to use the function “next” to get the next value. However, you can only once reselect the values.
The generator can be created with the function with the key word “yield”. To apply to the generator function, object-generator is created.
For simple cases, you can create the generator using the utterance-generator. Unlike the list, the values are evaluated “on the fly”, and not griming the memory for a one-time evaluation.
Collections are a module in a standard library implementing containers for alternative data types.
For example, the counter is a range of items stored as the keys and their counters as the dictionary’s meanings:
Defauldict sub class dict that allows using factory’s function to bring automatically new value in case the key is missing.
Defauldict can be used to create a tree data structure!
Itertools is a module of a standard library that allows creating iterators for the efficient cycle.
For example, premulations let generate all the possible ways to organize the methods of arranging the set of items?
Similarly, combinations creates all the possible ways to choose the items from the collection, so (unlike the premulations) the order doesn’t matter:
itertools also contains additional functions, like the chain that accepts the iterated and creates a new iterator which returns the data elements in turn, as single consistency:
Compression/Decompression in Python>
* – is the operator for decompression or a splat-operator. It’s very useful for convenient conversions by moving from lists or tuples as a single variable or arguments and vice versa.
When the arguments in your function have already been in the list or the tuple, you can decompress them with the help of *args if it’s a list or ** kwargs, if it’s dict.
A reverse operation is also possible: you can define the function that will compress all the arguments in one tuple and all the key words into one dict.
Decorator is the function to accept the function as a parameter and to return the function.
For example, the code below contains cash-function as a decorator to store the Fibonacci numbers which have already been calculated.
The Functools module provides us with a few decorators, such as lru_cach capable to do the same that we’ve just done: storage. It saves recent calls to save the time when the function is applied to with the same arguments.
Context managers are basically used for proper resource management. Often they are used to open a file: open(‘workfile’, ‘r’) as f:. However, the majority of developers don’t know how they really work and how to create their own ones.
In fact, the context manager it’s a class to implement the methods _enter_and_exit_.
In simple cases, the function generator with the single yield can be used, using the @contextmanager decorator.