[aA-zZ] Python quick reference

Sanjeev Rohila
4 min readNov 19, 2020

--

Disclaimer: Image copied from Google

Hello,
I am going to update this space every time I come across something new, productive, which improves the code readability or can optimize the python code. So keep watching this space.

Fallen in love with functools.partialmethod

Using functools.partialmethod we fix the arguments that are the same over most of the calls if a function is required to be called again and again.

We can pass the arguments that are required to be changed over repetitive calls. We of course need to take care of the ordering of the parameters otherwise can use keyword arguments.

# A normal function
def normal_function(a, b, c, d):
print(f"a:{a}, b:{b}, c:{c}, d:{d}")
# A partial function that calls normal_function with
# a as 4, b as 3 most of the time
>>>partial_function = functools.partial(normal_function, 4, 3)
# Calling partial_function with frequently changing
# value of last two arguments
>>> partial_function(2,1)
a:4, b:3, c:2, d:1
>>>
>>> partial_function(200,100)
a:4, b:3, c:200, d:100
## Fix last first 3 Params
>>> partial_function = functools.partial(normal_function, 4, 3, 2)
>>> partial_function(100)
a:4, b:3, c:2, d:100
>>>
>>> partial_function(10)
a:4, b:3, c:2, d:10

Let python say something to you

>>> import os
>>> os.system('say "Corona Virus is about to be finished!!"')

Using // in the pace of /

when an integer value is required

>>> 10/3
3.3333333333333335
>>> int(10/3)
3
>>> 10//3
3
>>>

The _ (underscore) in python

Stores the output of last statement executed on command prompt

>>> '_' in dir(__builtins__)
False
>>> 1+1
2
>>> _
2
>>> '_' in dir(__builtins__)
True
>>>

Could be used as a digit seperator, makes the code more readable

>>> i=1_000
>>> i
1000
>>> i=1_000_00
>>> i
100000
>>>

Choosing random strings

If we need to choose the random string, then we can use something like this

>>> import random
>>> random.choice(["Apple", "Orange", "Banana"])
'Banana'
>>> random.choice(["Apple", "Orange", "Banana"])
'Apple'
>>> random.choice(["Apple", "Orange", "Banana"])
'Orange'
>>> random.choice(["Apple", "Orange", "Banana"])
'Apple'

Check the iterable instance

Instead of checking the instance of every every iterable seperately, use of collections.abc.Iterable makes the code much more readable

>>> import collections
>>> l=list()
>>> d=dict()
>>> s=set()
>>> t=tuple()
>>> string="Python"
>>> n=5
>>> isinstance(l, collections.abc.Iterable)
True
>>> isinstance(d, collections.abc.Iterable)
True
>>> isinstance(s, collections.abc.Iterable)
True
>>> isinstance(t, collections.abc.Iterable)
True
>>> isinstance(string, collections.abc.Iterable)
>>> True
>>> isinstance(n, collections.abc.Iterable)
False
>>>

Clean Code #1

Here we are using the same context manager twice and without using extra indentation.

@contextlib.contextmanager
def sshclient(ip, username, private_key, ssh_timeout=600):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(
paramiko.AutoAddPolicy()
)
key_file = io.StringIO(private_key)
key = paramiko.RSAKey.from_private_key(key_file)
client.connect(
ip,
username=username,
pkey=key,
timeout=ssh_timeout
)
yield client
client.close()
# Now lets say we need two diffent linux server
with sshclient(ip1, username, private_key) as res1, sshclient(ip2, username, private_key) as res2:
# do something with res1
# something with res2

Check the builtins in python

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

How join works in threading

without join:
+---+---+------------------ main-thread
| |
| +........... child-thread(short)
+.................................. child-thread(long)

with join
+---+---+------------------***********+### main-thread
| | |
| +...........join() | child-thread(short)
+......................join()...... child-thread(long)

with join and daemon thread
+-+--+---+------------------***********+### parent-thread
| | | |
| | +...........join() | child-thread(short)
| +......................join()...... child-thread(long)
+,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, child-thread(long + daemonized)

'-' main-thread/parent-thread/main-program execution
'.' child-thread execution
'#' optional parent-thread execution after join()-blocked parent-thread could
continue
'*' main-thread 'sleeping' in join-method, waiting for child-thread to finish
',' daemonized thread - 'ignores' lifetime of other threads;
terminates when main-programs exits; is normally meant for
join-independent tasks

Using Paramiko

ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)

If you are using ssh keys, do:

k = paramiko.RSAKey.from_private_key_file(keyfilename)
# OR k = paramiko.DSSKey.from_private_key_file(keyfilename)

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=user, pkey=k)

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Sanjeev Rohila
Sanjeev Rohila

Written by Sanjeev Rohila

A Infra2Qe automation engineer

No responses yet

Write a response