Python coding basic tips (Based on my experience)

Variable names – It is always advisable to have Variable names as descriptive rather than single char.

Ex: t = 300 , total_time_elapsed = 300 (Right one)

Spacing between methods and functions – It is always advisable to have one space between each method inside the class, and two spaces between functions

Module names – It is better to have all small cases in the module name.

Ex: Session.py, session.py (Right one)

Dedicated py file all Globals – It is better to keep all common variables or constants in a single py file.

Comments and doc-string – It would be nice to have comments (shorter) and doc-string (for functions) for each block of code.

Grouping modules – It would be nice if you group all standard module one and other third-party modules one.

Having utility module – Having a separate module for utilities like windows file paths, Linux paths, and system-related stuff.

Star in imports – It is always recommended not to use “*” in from imports, until and unless really required.

Ex: from mymodule import *, from mymodule import specific (Right one)

Script execution entrypoint – It is advisable to have script execution entry point (if __name__ == ‘__main__’).

Python Interview questions – Part1

• Write a program to execute command on remote machine and get the out of command
• Best way to read bigger size files > 10GB
• Explain Python Global interpreter lock
• Validate file copy by calculating its hash (hashlib module)
• Write a program to find URLs in a text file and verify if the URL is valid
• Write a Python program to check if the input python module/package is already installed, upgrade the module/package if already present, install the module if not present (using pip)
• Find the maximum nested depth of curly braces
Wrongly ordered braces should return -1
Example:
Input: {a+b} * {b+c}. Output: 1
Input: {a + {c + {e / 9} – d} * 4} Output: 3
Input: {a – c *{s} Output: -1

Python client module/wrapper for Terraform interaction:

Please find the below Python wrapper/module for interacting with terraform and it is an alternative to CLI.

https://pypi.org/project/python-terraform/

https://github.com/beelit94/python-terraform/blob/master/README.md

 

Example:

from python_terraform import Terraform

tf = Terraform()

tf.init(reconfigure=True)

tf.apply(skip_plan=True)

tf.apply()