Python Primer

Here is the collection of information on python and howTos.

How to switch between python versions

To manage different versions of python, we will use update-alternatives. (man page: https://documentation.suse.com/es-es/sles/15-SP1/html/SLES-all/cha-update-alternative.html) to configure and be able to easily switch between version.

Step 1: Let’s take example of Python2 and Python3. First, lets find out python versions. Execute following command

ls /usr/bin/python*

Step 2: find out if the alternatives is configured in your system. To find out execute following command

sudo update-alternatives --list python

If you are getting “error: no alternatives for python” , then you will need to configure the alternatives.

Step 3: Let’s configure Python alternatives for python2 and python3. Execute the following commands

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2

Step 4: Check python version. Execute following command

python --version

#this will show your python version.

Step 4: If you want to switch and see what is configured, execute following command

sudo update-alternatives --configure python

The above command will show your options and what is currently configured

Generator Expression

Generator expressions return an iterator that computes the values as necessary, not needing to materialize all the values at once. This means that list comprehensions aren’t useful if you’re working with iterators that return an infinite stream or a very large amount of data. Generator expressions are preferable in these situations.

List Expression

With a list comprehension, you get back a Python list, a list containing the resulting lines but not an iterator.

Generators

Generators are a special class of functions that simplify the task of writing iterators. Regular functions compute a value and return it, but generators return an iterator that returns a stream of values.

Python and Luigi

Python Logging

Python logging has evolved over the years and the logging module is very rich and comparable to log4j logging. However, the documentation is great but examples are very skinny. Having said that the logging module is well implemented.

The logging module does not support YAML configuration file. I found out painfully. However, the configuration file default layout is very straight forward and works well. Had I known that the YAML is not supported, I would not have waisted my time.

If you are interested in YAML, you will need to write your own YAML parser and then provide the Dict to the logging configurator.

Reference: