4. Python Tricks

Some useful Python tricks for my reference.

4.1 f-string

f-strings or format-strings are very useful strings. They are similar to C
snprint() with extra thought.
Let us see how to use f-string for debug prints. (f-string are also used heavily
for string substitution, but we will not discuss that)

  • using f-string to print a variable. We don'd need to type var = {var} to
    print a var. Just {var = } sufficient. Let us see in detail.
my_str = 'hello world'
# both of the following code lines print the same output.
print(f' {my_str = }') # I use this trick heavily as it is a lot clearner, crisp  
print(f' my_str = {my_str}')  

The same trick can be used to print expressions too.
If there is a = inside { }, f-string understands that it is an expression.
It prints the expression as is and its computed value (both at the same time!)!.

my_int = 100  
print(f'{my_int+100 = }')  

4.2 Virtual Environment

Virtual Env helps us to install any 3rd party Python libs without touching
system wide library locations. We can call this sandboxing Python lib
installation. This free us from

  • asking permission of sys admin to install any Python libs
  • worrying disturbing the whole system!

Below, we will see how to create this sandbox! Note: some people use .venv as folder name, but I like explicit name like sandbox.

python3 -m venv sandbox # this creates a sandbox
source sandbox/bin/activate # this activate sandbox
# on microsoft windows, I use git-bash and do following  
# source sandbox/Scripts/activate  
# we can use pip command to install any 3rd party Python libs
# at any moment to deactive sandbox, just use Ctlr+D

4.3 Using pip

Once you activate virtual environment sandbox, we can install/un-install any
python module like below example. pip installs pkgs inside your site-packages
folder. For any import of modules, Python first searches in the site-packages
folder. Thus we don't need to keep setting PYTHONPATH environment variable.

First ensure, we have latest pip pkg.

python3 -m ensurepip --upgrade
# directly from standard pypy server
python3 -m pip install pexpect  
# to un install a pkg  
python3 -m pip uninstall pexpect  
# installing from a particular git repo directly  
python -m pip install git+ssh://<repo_url>@<tag_commit_branch>  
# install all pkgs listed in this file!  
python -m pip install -r <path_to_requirements.txt_file>   
# to list all we have  
python -m pip list   

!!! danger "Do not pip this way!" Do not use command like below, because, we don't know to which python
version the pip points

Always use pip as show above, not as below.
~~pip install pexpect~~
~~pip pip uninstall pexpect~~

4.4 Terminology ##

  • A python script is a python file that can be executed on shell
  • A python sourced code files is called module. This can be imported into other modules.
  • A folder containing 1 or more python source files is called package

4.5 Making our package pip installable

It is very good idea to make our pkg as pip installable. We can directly
install the pkg from github/gitlab repo.
Place a file called setup.py in the main folder of your to-be pkg.

# content of setup.py
""" setup.py to mke pip installable"""
import os
from distutils.core import setup
def read(fname):
    """ read """
    return open(os.path.join(os.path.dirname(__file__), fname), encoding='utf-8').read()

setup(
    name='<pkg name>',
    version='1.0',
    author='<author>',
    description='blah',
    long_description=read('README.md'),
    url='github/gitlab repo url',
    py_modules=[],
    # include top directory
    package_dir={
        'dir_name': '',
    },
    #include all directories that you want to include
    packages=[
        'folder',
        'folder.file1',
        'folder.file2',
    ],
)

4.6 Reg. expression

use raw string for this. raw strings do not process the python escape sequence like \n, \t, \ etc. It just passes to regular expression engine as it is written. Further, I use re python module module for RE processing

4.7 co-op tasking

4.8 pexpect

4.9 Polymorphism

4.10 Type hints

a.k.a Type Annotation, Type Signature

All Variables in python are not statically typed, but they are dynamically typed. They are as strongly typed as any C/C++ variables. This may look odd, but true.

In C, variable type has to be defined in source file. During run-time, its type remains same forever. In Python, type of a variable keeps changing based on type of the value it points (you assign)

In python too, we can mention the variable type in source code also. However, Python runtime ignore all type info you convey in source code. Type hint is just for developer to reinforce the idea of type that variable suppose to carry. Type hint also helps Lint/IDE/Static Analysis tools to indicate possible problems.

So moral of story : Python don't care of type hints we provide. It is just for IDE/lint/ human readers of src code. I use type hints when : - If I am very sure that a variable's type is fixed. - If I can convey better meaning for function signature etc

Ref: Official Python Type Hints

4.11 Linting

# installation of linting tools
python -m pip install -r wheel pexpect multimethod junit_xml
python -m pip install pylint flake8

# to run lint on your folder
python3 -m pylint --max-line-length 120 <folder_path>
python3 -m flake8 --max-line-length 120 <folder_path>

# now fix them each. But VS code provide GUI interface for this!
# it is a lot handy.

4.12 Unicode

- https://www.asmeurer.com/python-unicode-variable-names/
- https://www.py4u.net/discuss/215789

4.13 Ref