[aA-zZ] Python quick reference

Sanjeev Rohila
3 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']

--

--