Org It (TCS-iQMS-166) Python Programming Standards and Guidelines PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Document Number: TCS-iQMS-166

Python Programming Standards and Guidelines

Version 1.0

TCS House, Raveline Street, Fort Mumbai 400 001 India


April 2018
Python Programming Standards and Guidelines v1.0

Notice
© 2018 Tata Consultancy Services
This is a controlled document. Unauthorised access, copying, replication or
usage for a purpose other than for which it is intended, are prohibited.

This document must not be copied in whole or in parts by any means,


without the written authorisation of the Global Head – Delivery Excellence,
TCS.

TCS Internal ii
Python Programming Standards and Guidelines v1.0

TCS Internal iii


Python Programming Standards and Guidelines v1.0

Document Revision List

Document Name: Python Programming Standards and Guidelines


Document Version: 1.0
Document No.: TCS-iQMS-166

Release Notice Reference

Revision Revision Revision Page Rationale for Change Action Taken


No. Date Description No.

1.0 Apr-2018 Initial Release

TCS Internal iv
Python Programming Standards and Guidelines

About this Document

Introduction and Purpose


The purpose of this document is to provide the coding standards, guidelines and Best
Practices to be followed for Python Programming.

Intended Audience
This document is intended for Python programmers and Reviewers who develop and review
Python code and deploy them in Production environment.

Organisation of the Document


Information in this document has been organised as follows:

Chapter Description

Chapter 1 Introduction
Chapter 2 Python Identifier and Naming Standards
Chapter 3 Syntax
Chapter 4 Comments

Chapter 5 Imports

Chapter 6 Packages

Chapter 7 Nested/ Local/ Inner classes and Functions

Chapter 8 Conditional expressions

Chapter 9 Global Variables

Chapter 10 List Comprehensions

Chapter 11 Generators

Chapter 12 Lambda Functions

Chapter 13 Default Iterators and Operators

Chapter 14 Exceptions

Chapter 15 Default Argument Values

Chapter 16 Zen of Python

Chapter 17 Idioms

Chapter 18 Conventions

Chapter 19 Lexical Scoping


TCS Internal 5
Python Programming Standards and Guidelines

Chapter Description

Chapter 20 Power features

Chapter 21 Deprecated Language features

Chapter 22 Threading

Chapter 23 True and False Evaluations

Chapter 24 Packages

iQMS Document
This document is a part of the Integrated Quality Management System (iQMS) of TCS.

TCS Internal 6
Python Programming Standards and Guidelines

Abbreviations and Acronyms

Abbreviation/ Description
Acronym

iQMS Integrated Quality Management System


TCS Tata Consultancy Services

TCS Internal 7
Python Programming Standards and Guidelines

Contents

 INTRODUCTION ......................................................................................................... 10

 PYTHON IDENTIFIER AND NAMING STANDARDS ................................................................ 11


2.1 File Name .............................................................................................................................. 11
2.2 Mandatory Rules for naming an identifier ............................................................................. 11
2.3 Conventions used while naming an identifier ........................................................................ 11
2.4 Reserved Words ................................................................................................................... 12

 SYNTAX ...................................................................................................................... 13
3.1 Spacing ................................................................................................................................. 13
3.2 Colon ..................................................................................................................................... 13
3.3 Indentation............................................................................................................................. 13
3.4 Line Length............................................................................................................................ 14

 COMMENT STANDARDS ........................................................................................... 15


4.1 Block Comments ................................................................................................................... 15
4.2 Inline Comments ................................................................................................................... 15
4.3 Documentation Strings .......................................................................................................... 16

5. IMPORTS .......................................................................................................................... 17

6. PACKAGES: ...................................................................................................................... 18

7. NESTED/ LOCAL/ INNER CLASSES AND FUNCTIONS............................................................. 19

8. CONDITIONAL EXPRESSIONS ............................................................................................. 20

9. GLOBAL VARIABLES ......................................................................................................... 21

10. LIST COMPREHENSIONS .................................................................................................. 22

11. GENERATORS ................................................................................................................. 23

12. LAMBDA FUNCTIONS ....................................................................................................... 24

13. DEFAULT ITERATORS AND OPERATORS............................................................................ 25

14. EXCEPTIONS ................................................................................................................... 26

TCS Internal 8
Python Programming Standards and Guidelines

15. DEFAULT ARGUMENT VALUES ......................................................................................... 28

16. ZEN OF PYTHON.............................................................................................................. 29

17. IDIOMS ........................................................................................................................... 30

18. CONVENTIONS................................................................................................................. 31

19. LEXICAL SCOPING .......................................................................................................... 34

20. POWER FEATURES .......................................................................................................... 35

21. DEPRECATED LANGUAGE FEATURES ............................................................................... 36

22. THREADING .................................................................................................................... 37

23. TRUE AND FALSE EVALUATIONS ...................................................................................... 38

24. PACKAGES ..................................................................................................................... 39

REFERENCE ......................................................................................................................... 43

GLOSSARY ........................................................................................................................... 44

FEEDBACK FORM ............................................................................................................. 45

TCS Internal 9
Python Programming Standards and Guidelines

 INTRODUCTION

This document describes the coding standards, naming conventions and guidelines to be
followed when designing and implementing framework/ scripts using Python programming.
This document helps to ensure consistency across the code, resulting in increased usability
and maintainability of the code.

Importance of coding standards


Coding standards are important because they lead to greater consistency and maintainability
of code. Consistency leads to the code that is easier to understand, which in turn results in
easy development, review and maintenance.

TCS Internal 10
Python Programming Standards and Guidelines

 PYTHON IDENTIFIER AND NAMING STANDARDS

A Python identifier is a name used to identify a variable, function, class, module or other
object.

2.1 File Name

The file name should be meaningful and should end with “.py”.

# Correct
Help_script.py
Hello_world.py

# Incorrect
Abcd.py
No123.py

2.2 Mandatory Rules for naming an identifier


 An identifier must start with a letter ( A-Z, a-z) or an underscore (_)
 First character may be followed by zero or more letters, digits (0-9) or underscore (_)
 No special symbols like @, $, % are allowed inside an identifier
 Keywords(True, False, break, continue etc.) cannot be used as identifiers

Examples:

Valid identifier ( Emp_tcs, _salary, var1 )

Non-valid identifier ( Emp@tcs, 2ndcar, $var1 )

2.3 Conventions used while naming an identifier

Types Description Example


Class names CapWords/CamelCase convention class Student, class MyClass
Private Single leading underscore _private_variable
Specific meaning to Two leading underscores __private_variable
interpreter, Mangles
names to avoid name
clashes with name
defined by
subclasses
Language- Ends with two trailing underscores special_variable__
defined special name

__double_leading_underscore: This is about syntax rather than a convention. Double


underscore will mangle the attribute names of a class to avoid conflicts of attribute
names between classes. (so-called “mangling” that means that the compiler or
interpreter modify the variables or function names with some rules, not use as it is).

TCS Internal 11
Python Programming Standards and Guidelines

The mangling rule of Python is adding the “_ClassName” to front of attribute names are
declared with double underscore.

That is, if you write method named “__method” in a class, the name will be mangled in
“_ClassName__method” form.

2.4 Reserved Words

The following are the list of reserved words in Python. These words may not be used as any
identifier name.

And assert break class continue


Def del elif else expect
Exec finally for from global
If import in is lambda
Not or pass print raise
Return try while with yield

The above keywords may get altered in different versions of python. Some extra might get
added or some might be removed. We can always get the list of keywords in current version
by typing the following in prompt.

>>> import keyword


>>>print(keyword.kwlist)

TCS Internal 12
Python Programming Standards and Guidelines

 SYNTAX

3.1 Spacing

Spaces are the preferred indentation method.

Tabs or 4 spaces should be used solely to remain consistent with code that is already
indented with tabs.

Python 3 disallows mixing the use of tabs and spaces for indentation.

Python 2 code indented with a mixture of tabs and spaces should be converted to using
spaces exclusively.

When invoking the Python 2 command line interpreter with the -t option, it issues warnings
about code that illegally mixes tabs and spaces. When using –tt option, these warnings
become errors. These options are highly recommended.

3.2 Colon
Colon “ : “ using for Function, Loops, Conditions, Class defining as like curly braces in other
programming language.

Function defining by using colon


def addition():
a…
b…

def subtraction():
a…
b…

Condition defining using colon


If (a == b ) :
A…
b…
elif ( …. ) :
CC…
DD…

Class defining using colon


Class subject:

3.3 Indentation
 Use four (4) spaces to indent the code
 Never use tabs or mix of tabs and spaces
 Fixing line length (80 Columns) prevents lots of nesting and very long functions
 It is suggested to have Indents of 4 spaces at minimum; though 8 Spaces are ideal

TCS Internal 13
Python Programming Standards and Guidelines

3.4 Line Length


Keep the lines less than 80 characters. This is the amount that will fit comfortably on a
printed page at a reasonable size. If it does not fit in then it is probably an indication that
some of the work should be encapsulated as a separate function.

TCS Internal 14
Python Programming Standards and Guidelines

 COMMENT STANDARDS
Comments should be added to increase the readability of code. Comments that contradict
the code are worse than no comments. Always make a priority of keeping the comments up-
to-date when the code changes.

Comments should be complete sentences. If a comment is a phrase or sentence, its first


word should be capitalized, unless it is an identifier that begins with a lower case letter.

If a comment is short, the period at the end can be omitted. Block comments generally
consist of one or more paragraphs built out of complete sentences and each sentence
should end with a period.

You should use two spaces after a sentence-ending period.

Every change to the framework/ scripts should be documented in modification history. A


modification history should contain the following:

 Name of the person who changed the code:


 Date of change:
 Version:
 Changed function/ event:
 Change description:

4.1 Block Comments


Block comments generally apply to some (or all) code that follows them and are indented to
the same level as that code. Each line of a block comment starts with a # and a single space
(unless it is indented text inside the comment).
Paragraphs inside a block comment are separated by a line containing a single #.
# Increment my x value by one
x = x + 1

4.2 Inline Comments


Use inline comments sparingly.

An inline comment is a comment on the same line as a statement. Inline comments should
be separated by at least two spaces from the statement. They should start with a # and a
single space.

Inline comments are unnecessary and in fact distracting if they state the obvious.

x = x + 1 # Increment x

TCS Internal 15
Python Programming Standards and Guidelines

4.3 Documentation Strings

Conventions for writing good documentation strings (i.e. "docstrings").

Write docstrings for all public modules, functions, classes, and methods. Docstrings are not
necessary for non-public methods, but you should have a comment that describes what the
method does. This comment should appear after the def line.

Most importantly, the """ that ends a multiline docstring should be on a line by itself, e.g.:
"""Return a foobang
Optional plotz says to frobnicate the bizbaz first.
"""

For one liner docstrings, keep the closing """ on the same line.

"""Return a foobang Optional plotz says to frobnicate the bizbaz first. """

TCS Internal 16
Python Programming Standards and Guidelines

5. IMPORTS
The imports are used only for packages and modules. The imports allows the reusability
mechanism for sharing the code across modules.

The imports can be defined as follows:

import pack_or_module – for importing modules and packages

from pack_or_module import y : where pack_or_module is the package or module


and y is the module name without prefix

from pack_or_module import y as z : if two modules named y are to be imported or y is


long name

Example. If you want to import package urllib2 used for reading the data from urls we write it
as

import urllib2

For example the module sound.filters.equalizer may be imported as follows

from sound.filters import equalizer

TCS Internal 17
Python Programming Standards and Guidelines

6. PACKAGES:
The code base can be divided into clean and efficient modules using python packages. The
packages can be reused for sharing the code between different python programs. Like a
directory contains sub directories and files, a python package can contain sub packages and
modules.

To import the module, we need to use the full path name of the module. The advantage of
specifying full path name avoids conflicts in module names and makes it easier to find the
modules. It makes harder to deploy code because you have to replicate package hierarchy.

Imports should be as follows:

import sound.filters.equalizer (reference code with complete name)

from sound.filters import equalizer (reference code with just module name
and is preferred)

TCS Internal 18
Python Programming Standards and Guidelines

7. Nested/ Local/ Inner Classes and Functions


Nested/ Local/ Inner classes and functions are acceptable. A class can be defined inside of
a method, function or a class. A function can be defined inside a method or a function.
Nested functions have read-only access to variables defined in enclosing scopes.

The advantage is that they allow definition of utility classes and functions that are only used
for a very limited scope. The disadvantages are nested or local classes cannot be pickled.

The below is the example of inner class: (https://pythonspot.com/inner-classes/)

class Human:

def __init__(self):
self.name = 'Guido'
self.head = self.Head()

class Head:
def talk(self):
return 'talking...'

if __name__ == '__main__':
guido = Human()
print guido.name
print guido.head.talk()

TCS Internal 19
Python Programming Standards and Guidelines

8. Conditional Expressions
Conditional Expressions are acceptable for one liners. Conditional expressions are
mechanisms that provide a shorter syntax for if statements.

For example: x = 1 if cond else 2.

The advantage is that they are short and more convenient than if statements. The
disadvantage is that it may be hard to read and the condition may be difficult to locate if the
expression is long.

TCS Internal 20
Python Programming Standards and Guidelines

9. GLOBAL VARIABLES
Variables that are declared at module level are called global variables. Avoid use of global
variables. The advantage of using global variable is that they are occasionally useful. The
disadvantage is that it has potential to change the module behaviour during the import
because assignments to module-level variables are done when the module is imported. In
favour of class variables, avoid the global variables.

Some exceptions are as follows:

 default options for scripts


 module level constants. For example: PI = 3.14159

It is sometimes useful for globals to cache values needed or returned by functions. If


needed, globals should be made internal to the module and accessed through public module
level functions.

TCS Internal 21
Python Programming Standards and Guidelines

10. LIST COMPREHENSIONS


Use List comprehensions for simple cases. It can be used to construct lists in a very natural
way like a mathematician is used to do.

For example:

S= [x**2 for x in range(10)]


v= [2**i for i in range(13)]

The advantage is that the Simple list comprehensions can be clearer and simpler than other
list creation techniques. Generator expressions can be very efficient, since they avoid the
creation of a list entirely. The disadvantage is that complicated list comprehensions or
generator expressions can be hard to read.

Each portion must fit on one line: mapping expression, for clause, filter expression.

Multiple for clauses or filter expressions are not permitted, it is recommended to use loops in
such cases.

TCS Internal 22
Python Programming Standards and Guidelines

11. GENERATORS
A generator function returns an iterator that yields a value each time it executes a yield
statement. After it yields a value, the runtime state of the generator function is suspended
until the next value is needed.

The advantage is that it results in simple code. The state of the local variables and control
flow are preserved for each call. A generator uses less memory than the function call that
creates an entire list of values at once.

Use “Yields” rather than return.

The below Fibonacci series is an example of generator.

def fib(max):
a, b = 0, 1
while a < max:
yield a
a, b = b, a + b

TCS Internal 23
Python Programming Standards and Guidelines

12. LAMBDA FUNCTIONS


Lambda functions defines anonymous functions in an expression as opposed to statement.
They are often used to define callbacks or operators for higher-order functions like map()
and filter().This is similar to lambda calculus which is the backbone of functional
programming. Python programs written in functional style usually won't go to the extreme of
avoiding all I/O or all assignments; instead, they will provide a functional appearing interface
but will use non-functional features internally.

The advantage is that they are cconvenient..

The disadvantage is that it is difficult to read and debug than local functions. The lack of
names means stack traces are more difficult to understand. Expressiveness is limited
because the function may only contain an expression.

If the code inside the lambda function is any longer than 60–80 characters, it's probably
better to define it as a regular (nested) function. For common operations like multiplication,
use the functions from the operator module instead of lambda functions.

For example, prefer operator.mul to lambda x,y: x * y.

The below example illustrates the lambda function.

double = lambda x: x * 2
print(double(5))

TCS Internal 24
Python Programming Standards and Guidelines

13. DEFAULT ITERATORS AND OPERATORS


Use default iterators and operators for types that support them, like lists, dictionaries and
files. They are defined as Container types like dictionaries and lists. Define default iterators
and membership test operators ("in" and "not in").

The advantage is that the default iterators and operators are simple and efficient. They
express the operation directly, without extra method calls. A function that uses default
operators is generic. It can be used with any type that supports the operation.

The type of object can not tell by reading the method names (e.g. has_key() means a
dictionary). This is also an advantage.

Use default iterators and operators for types that support them, like lists, dictionaries, and
files. The built-in types define iterator methods, too. Prefer these methods to methods that
return lists, except that you should not mutate a container while iterating over it.

Yes: for key in adict: ...


if key not in adict: ...
if obj in alist: ...
for line in afile: ...
for k, v in dict.iteritems(): …

No: for key in adict.keys(): ...


if not adict.has_key(key): ...
for line in afile.readlines(): ...

TCS Internal 25
Python Programming Standards and Guidelines

14. EXCEPTIONS
Exceptions are allowed but must be used carefully. Exceptions are events that can modify
the flow of control through a program.

Exceptions are a means of breaking out of the normal flow of control of a code block to
handle errors or other exceptional conditions.

Below are some common exceptions errors in Python:

IOError
If the file cannot be opened

ImportError
If python cannot find the module

ValueError
Raised when a built-in operation or function receives an argument that has the right type but
an inappropriate value

KeyboardInterrupt
Raised when the user hits the interrupt key (normally Control-C or Delete)

EOFError
Raised when one of the built-in functions (input() or raw_input()) hits an end-of-file condition
(EOF) without reading any data

Exceptions are handled as below.

try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: Can't find file or read data"
else:
print "Written content in the file successfully"
fh.close()

try:
print 1/0
except ZeroDivisionError:
print "You can't divide by zero."

The advantage is that the control flow of normal operation code is not cluttered by error-
handling code. It also allows the control flow to skip multiple frames when a certain condition
occurs, e.g., returning from N nested functions in one step instead of having to carry-through
error codes.

The disadvantage is that it may cause the control flow to be confusing. It is easy to miss
error cases when making library calls.

TCS Internal 26
Python Programming Standards and Guidelines

Exceptions must follow certain conditions.

1. Raise Exceptions as below:


raise MyException('Error Message') or raise MyException

Do not use the two argument form (raise MyException, 'Error Message') or deprecated
string based exception (raise 'Error message')

2. Modules or packages should define their own domain specific base exception class,
which should inherit from the built-in Exception class. The base Exception for a
module should be called Error

class Error(Exception):
pass

3. Never use catch-all except: statements, or catch Exception or StandardError, unless


you are re-raising the exception or in the outermost block in your thread (and printing
an error message). Python is very tolerant in this regard and except: will really catch
everything including misspelled names, sys.exit() calls, Ctrl+C interrupts, unit test
failures and all kinds of other exceptions that you simply don't want to catch.

4. Minimize the amount of code in a try/ except block. The larger the body of the try, the
more likely that an exception will be raised by a line of code that you didn't expect to
raise an exception. In those cases, the try/ except block hides a real error.

5. Use the finally clause to execute code whether or not an exception is raised in the try
block. This is often useful for clean-up like closing a file.

6. When capturing an exception, use “as” rather than a comma. For example:

try:
raise Error
except Error as error:
pass

TCS Internal 27
Python Programming Standards and Guidelines

15. DEFAULT ARGUMENT VALUES


Default arguments are acceptable in most cases.

You can specify values for variables at the end of a function's parameter list,
e.g. def foo(a,b=0)

If foo is called with only one argument, then b is set to zero. If foo is called with two
arguments, then b has the value of the second argument.

The advantage is that often you have a function that uses lots of default values, but rarely
you want to override the defaults. Default argument values provide an easy way to do this,
without having to define lots of functions for the rare exceptions. Also, Python does not
support overloaded methods/ functions. Default arguments are an easy way of "faking" the
overloading behaviour.

The disadvantage is that default arguments are evaluated once at a module load time. This
may cause problems if the argument is a mutable object such as a list or a dictionary. If the
function modifies the object (like by appending an item to a list), the default value is
modified.

It is acceptable to use with the following caveat:

Do not use mutable objects as default values in the function or method definition.

Yes: def foo(a, b=None):


if b is None:
b = []

No: def foo(a, b=[]):


...
No: def foo(a, b=time.time()): # The time the module was loaded???
...
No: def foo(a, b=FLAGS.my_thing): # sys.argv has not yet been parsed...
...

TCS Internal 28
Python Programming Standards and Guidelines

16. ZEN OF PYTHON


Also known as PEP 20, the guiding principles for python design.

>>> import this


The Zen of Python, by Tim Peters

Beautiful is better than ugly.


Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules, although practicality beats purity.
Errors should never pass silently, unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one and preferably only one obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea, let's do more of those!

TCS Internal 29
Python Programming Standards and Guidelines

17. IDIOMS
Al Programming idiom is a way to write code

Unpacking: If you know the length of a list or tuple, you can assign names to its elements
with unpacking. For example, since enumerate() will provide a tuple of two elements for each
item in list:
for index, item in enumerate(some_list): # do something with index and item

a, b = b, a( Swapping the variables)


a,(b,c) = 1,(2,3) nested unpacking also works
In python 3, a new method of unpacking defined as below.
a, *rest = [1, 2, 3]
# a = 1, rest = [2, 3]
a, *middle, c = [1, 2, 3, 4]
# a = 1, middle = [2, 3], c = 4

Create an ignored variable:

filename = 'foobar.txt'
basename, __, ext = filename.rpartition('.')

Many Python style guides recommend the use of a single underscore “_” for throwaway
variables rather than the double underscore “__” . The issue is that “_” is commonly used as
an alias for the gettext() function and is also used at the interactive prompt to hold the value
of the last operation. Using a double underscore instead is just as clear and almost as
convenient and eliminates the risk of accidentally interfering with either of these other use
cases.

Create a length-N list of the same thing

Use the Python list * operator:


four_nones = [None] * 4

Create a length-N list of lists


four_lists = [[] for __ in xrange(4)]

Create a string from a list

letters = ['s', 'p', 'a', 'm']


word = ''.join(letters)

Searching for an item in a collection

s = set(['s', 'p', 'a', 'm'])


l = ['s', 'p', 'a', 'm']

def lookup_set(s):
return 's' in s

def lookup_list(l):
return 's' in l

TCS Internal 30
Python Programming Standards and Guidelines

18. CONVENTIONS
Check if variable equals a constant: You don’t need to explicitly compare a value to True or
None or 0. You can just add it to the if statement. See Truth Value Testing for a list which is
considered as false.

Bad:

if attr == True:
print 'True!'

if attr == None:
print 'attr is None!'

Good:

# Just check the value


if attr:
print 'attr is truth!'

# or check for the opposite


if not attr:
print 'attr is false!'

# or, since None is considered false, explicitly check for it


if attr is None:
print 'attr is None!'

Access a Dictionary Element:

Don’t use the dict.has_key() method. Instead, use x in d syntax or pass a default argument
to dict.get().

Bad:

d = {'hello': 'world'}
if d.has_key('hello'):
print d['hello'] # prints 'world'
else:
print 'default_value'

Good:

d = {'hello': 'world'}

print d.get('hello', 'default_value') # prints 'world'


print d.get('thingy', 'default_value') # prints 'default_value'

# Or:
if 'hello' in d:
print d['hello']

TCS Internal 31
Python Programming Standards and Guidelines

Short Ways to Manipulate Lists:

List comprehensions provide a powerful and concise way to work with lists. Also, the map()
and filter() functions can perform operations on lists using a different and more concise
syntax.

Bad:

# Filter elements greater than 4


a = [3, 4, 5]
b = []
for i in a:
if i > 4:
b.append(i)

Good:

a = [3, 4, 5]
b = [i for i in a if i > 4]
# Or:
b = filter(lambda x: x > 4, a)

Bad:

# Add three to all list members.


a = [3, 4, 5]
for i in range(len(a)):
a[i] += 3

Good:

a = [3, 4, 5]
a = [i + 3 for i in a]
# Or:
a = map(lambda i: i + 3, a)

Use enumerate() keep a count of your place in the list.

a = [3, 4, 5]
for i, item in enumerate(a):
print i, item
# prints
#03
#14
#25

The enumerate() function has better readability than handling a counter manually. Moreover,
it is better optimized for iterators.

TCS Internal 32
Python Programming Standards and Guidelines

Read From a File:

Use the “with open” syntax to read from files. This will automatically close files for you.

Bad:

f = open('file.txt')
a = f.read()
print a
f.close()

Good:

with open('file.txt') as f:
for line in f:
print line

The with statement is better because it will ensure you always close the file, even if an
exception is raised inside the “with” block.

Line Continuations:

When a logical line of code is longer than the accepted limit, you need to split it over multiple
physical lines. The Python interpreter will join consecutive lines if the last character of the
line is a backslash. This is helpful in some cases, but should usually be avoided because of
its fragility. A white space added to the end of the line, after the backslash, will break the
code and may have unexpected results.

A better solution is to use parentheses around your elements. Left with an unclosed
parenthesis on an end-of-line the Python interpreter will join the next line until the
parentheses are closed. The same behaviour holds for curly and square braces.

Bad:

my_very_big_string = """For a long time I used to go to bed early. Sometimes, \


when I had put out my candle, my eyes would close so quickly that I had not even \
time to say “I’m going to sleep.”"""

from some.deep.module.inside.a.module import a_nice_function, another_nice_function, \


yet_another_nice_function

Good:

my_very_big_string = (
"For a long time I used to go to bed early. Sometimes, "
"when I had put out my candle, my eyes would close so quickly "
"that I had not even time to say “I’m going to sleep.”"
)
from some.deep.module.inside.a.module import (
a_nice_function, another_nice_function, yet_another_nice_function)

However, more often than not, having to split a long logical line is a sign that you are trying
to do too many things at the same time, which may hinder readability.

TCS Internal 33
Python Programming Standards and Guidelines

19. LEXICAL SCOPING


Use of Lexical scoping is acceptable.

A nested Python function can refer to variables defined in enclosing functions, but cannot be
assigned to them. Variable bindings are resolved using lexical scoping that is based on the
static program text.

Any assignment to a name in a block will cause Python to treat all references to that name
as a local variable, even if the use precedes the assignment. If a global declaration occurs,
the name is treated as a global variable.

An example of the use of this feature:

def get_adder(summand1):
"""Returns a function that adds numbers to a given number."""
def adder(summand2):
return summand1 + summand2

return adder

The advantage is that it often results in clearer and more elegant code.

The disadvantage is that it can lead to confusing bugs.

The below, example is based on PEP-0227 .

i=4
def foo(x):
def bar():
print i,
# ...
# A bunch of code here
# ...
for i in x: # Ah, i *is* local to Foo, so this is what Bar sees
print i,
bar()

So foo([1, 2, 3]) will print 1 2 3 3, not 1 2 3 4

TCS Internal 34
Python Programming Standards and Guidelines

20. POWER FEATURES


It is recommended to avoid these features.

Python is extremely flexible language and gives you many fancy features such as
metaclasses, access to bytecode, on-the-fly compilation, dynamic inheritance, object
reparenting, import hacks, reflection, modification of system internals etc.

The advantage is that these are powerful language features which can make code very
compact.

The disadvantage is that it is tempting to use these features when they're not absolutely
necessary. It's harder to read, understand and debug code that's using these features. It
doesn't seem that way at first (to the original author), but when revisiting the code, it tends to
be more difficult than code that is longer but straight forward.

TCS Internal 35
Python Programming Standards and Guidelines

21. DEPRECATED LANGUAGE FEATURES


It is recommended to:
- Use string methods instead of the string module wherever possible
- Use function call syntax instead of apply
- Use list comprehensions and for loops instead of filter and map when the function
argument have an inline lambda
- Use for loops instead of reduce

Current versions of Python provide alternative constructs that people find generally
preferable.

Good:
words = foo.split(':')

[x[1] for x in my_list if x[2] == 5]

map(math.sqrt, data) # Ok. No inlined lambda expression.

fn(*args, **kwargs)

Bad:
words = string.split(foo, ':')

map(lambda x: x[1], filter(lambda x: x[2] == 5, my_list))

apply(fn, args, kwargs)

TCS Internal 36
Python Programming Standards and Guidelines

22. THREADING
Do not rely on the atomicity of built-in types.

While Python's built-in data types such as dictionaries appear to have atomic operations.
There are corner cases where they aren't atomic (e.g. if __hash__or__eq__are implemented
as Python methods) and their atomicity should not be relied upon. Also you should not rely
on atomic variable assignment (since this in turn depends on dictionaries).

Use the Queue module's Queue data type as the preferred way to communicate data
between threads. Otherwise, use the threading module and its locking primitives. Learn
about the proper use of condition variables so you can use threading.Condition instead of
using lower-level locks.

Example: (Threading) (https://www.saltycrane.com/blog/2008/09/simplistic-python-thread-


example/)
import time
from threading import Thread

def myfunc(i):
print "sleeping 5 sec from thread %d" % i
time.sleep(5)
print "finished sleeping from thread %d" % i

for i in range(10):
t = Thread(target=myfunc, args=(i,))
t.start()

TCS Internal 37
Python Programming Standards and Guidelines

23. TRUE AND FALSE EVALUATIONS


Use the "implicit" false if it is possible. Python evaluates certain values as false when in a
Boolean context. A quick rule of thumb is that all empty values are considered false so 0,
None, [], {}, ''; all evaluate as false in a Boolean context.

The advantage is Conditions using Python Booleans are easier to read and less error-prone.
In most cases, they're also faster.

The disadvantage is that it may look strange to C/C++ developers.


Use the "implicit" false if at all possible, e.g., if foo:rather than if foo != []:.

There are few caveats that you should know:


Never use == or != to compare singletons like None. Use is or is not.
Beware of writing if x: when you really mean if x is not None e.g., when testing whether a
variable or argument that defaults to None was set to some other value. The other value
might be a value that's false in a Boolean context.

Never compare a Boolean variable to False using ==. Use if not x: instead. If you need to
distinguish False from None then chain the expressions, such as if not x and x is not None:.

For sequences (strings, lists, tuples), use the fact that empty sequences are false, so if not
seq: or if seq: is preferable to if len(seq): or if not len(seq):

When handling integers, implicit false may involve more risk than benefit (like accidentally
handling None as 0). You may compare a value which is known to be an integer (and is not
the result of len()) against the integer 0.

Good:
if not users:
print 'no users'
if foo == 0:
self.handle_zero()
if i % 10 == 0:
self.handle_multiple_of_ten()

Bad:
if len(users) == 0:
print 'no users'

if foo is not None and not foo:


self.handle_zero()
if not i % 10:
self.handle_multiple_of_ten()

TCS Internal 38
Python Programming Standards and Guidelines

24. PACKAGES
The below is the list of widely used Python packages and the list can go on.

1. NumPy:
The most fundamental package, around which the scientific computation stack is built, is
numpy (stands for Numerical Python). It provides an abundance of useful features for
operations on n-arrays and matrices in Python
Example:
import numpy as np
cvalues = [20.1, 20.8, 21.9, 22.5, 22.7, 22.3, 21.8, 21.2, 20.9, 20.1]
C = np.array(cvalues)
print(C)
print(C * 9 / 5 + 32)

2. SciPy:
SciPy is a library of software for engineering and science It provides efficient numerical
routines as numerical integration, optimization and many others via its specific sub-modules
Example:
from scipy import linalg
arr = np.array([[1, 2],[3, 4]])
linalg.det(arr)

3. Pandas:
Pandas is a Python package designed to do work with “labeled” and “relational” data simple
and intuitive. Pandas is a perfect tool for data wrangling.
Example:
import pandas as pd
df = pd.DataFrame(np.random.randn(10, 4))
pieces = [df[:3], df[3:7], df[7:]]
pd.concat(pieces)

4. Matplotlib:
Another SciPy Stack core package and another Python Library that is tailored for the
generation of simple and powerful visualizations with ease is Matplotlib
Example:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(C)
plt.show()

TCS Internal 39
Python Programming Standards and Guidelines

5. Seaborn:
Seaborn is mostly focused on the visualization of statistical models; such visualizations
include heat maps, those that summarize the data but still depict the overall distributions
Example:
import seaborn as sns
sns.set(style="ticks")

# Load the example dataset for Anscombe's quartet


df = sns.load_dataset("anscombe")

# Show the results of a linear regression within each dataset


sns.lmplot(x="x", y="y", col="dataset", hue="dataset", data=df,
col_wrap=2, ci=None, palette="muted", size=4,
scatter_kws={"s": 50, "alpha": 1})

6. Bokeh:
Another great visualization library is Bokeh, which is aimed at interactive visualizations. In
contrast to the previous library, this one is independent of Matplotlib. The main focus of
Bokeh, as we already mentioned, is interactivity and it makes its presentation via modern
browsers in the style of Data-Driven Documents (d3.js).
Example
#Import library
from bokeh.charts import Bar, output_file, show #use output_notebook to visualize it in notebook
# prepare data (dummy data)
data = {"y": [1, 2, 3, 4, 5]}
# Output to Line.HTML
output_file("lines.html", title="line plot example") #put output_notebook() for notebook
# create a new line chat with a title and axis labels
p = Bar(data, title="Line Chart Example", xlabel='x', ylabel='values', width=400, height=400)
# show the results
show(p)

TCS Internal 40
Python Programming Standards and Guidelines

7. Plotly
It is rather a web-based toolbox for building visualizations, exposing APIs to some
programming languages (Python among them)
Example:
import plotly.plotly as py
from plotly.graph_objs import *

trace0 = Scatter(
x=[1, 2, 3, 4],
y=[10, 15, 13, 17]
)
trace1 = Scatter(
x=[1, 2, 3, 4],
y=[16, 5, 11, 9]
)
data = Data([trace0, trace1])

py.plot(data, filename = 'basic-line')

8. sciKit-Learn
Scikits are additional packages of SciPy Stack designed for specific functionalities like image
processing and machine learning facilitation
Example: The below page for package sciKit-learn will provide various example for sciKit-
learn.
http://scikit-learn.org/

9. Theano:
Theano is a Python package that defines multi-dimensional arrays similar to NumPy, along
with math operations and expressions. The library is compiled, making it run efficiently on all
architectures. Originally developed by the Machine Learning group of Université de
Montréal, it is primarily used for the needs of Machine Learning
Example: The below page for package Theano will provide various example for theano.
http://deeplearning.net/software/theano/tutorial/index.html

10. TensorFlow:
Coming from developers at Google, it is an open-source library of data flow graphs
computations, which are sharpened for Machine Learning. It was designed to meet the high-
demand requirements of Google environment for training Neural Networks and is a
successor of DistBelief, a Machine Learning system, based on Neural Networks. However,
TensorFlow isn’t strictly for scientific use in borders of Google — it is general enough to use it
in a variety of real-world application.
Example: The below page for package Tensorflow will provide various example for
tensorflow
https://www.tensorflow.org/

11. Keras:
It is an open-source library for building Neural Networks at a high-level of the interface, and it
is written in Python. It is minimalistic and straightforward with high-level of extensibility. It
TCS Internal 41
Python Programming Standards and Guidelines

uses Theano or TensorFlow as its backends, but Microsoft makes its efforts now to integrate
CNTK (Microsoft’s Cognitive Toolkit) as a new back-end.
Example: The below page for package keras will provide various example for keras
https://keras.io/

12. NLTK:
The name of this suite of libraries stands for Natural Language Toolkit and, as the name
implies, it used for common tasks of symbolic and statistical Natural Language Processing.
NLTK was intended to facilitate teaching and research of NLP and the related fields
(Linguistics, Cognitive Science Artificial Intelligence, etc.) and it is being used with a focus on
this today.
Example: The below page for package NLTK will provide various example for NLTK
http://www.nltk.org/

13. Gensim:
It is an open-source library for Python that implements tools for work with vector space
modeling and topic modeling. The library designed to be efficient with large texts, not only in-
memory processing is possible. The efficiency is achieved by the using of NumPy data
structures and SciPy operations extensively. It is both efficient and easy to use
Example: The below package web page gives the gensim example
https://radimrehurek.com/gensim/tutorial.html

14. Scrapy:
Scrapy is a library for making crawling programs, also known as spider bots, for retrieval of
the structured data, such as contact info or URLs, from the web
Example: The below webpage gives python scrapy introduction and example
https://www.analyticsvidhya.com/blog/2017/07/web-scraping-in-python-using-scrapy/

15. Statsmodels:
As you have probably guessed from the name, statsmodels is a library for Python that
enables its users to conduct data exploration via the use of various methods of estimation of
statistical models and performing statistical assertions and analysis
Example:

import statsmodels.api as sm
import statsmodels.formula.api as smf
linreg = smf.ols(formula='Lottery ~ Literacy + Wealth + Region', data=df).fit()

TCS Internal 42
Python Programming Standards and Guidelines

Reference
For more information, please refer to the following sources:

Python Programming courses in iEVOLVE


- Python Programming
- Digital : Python
- Python for Everybody
- Programming for Everybody (Python)

TCS Internal 43
Python Programming Standards and Guidelines

Glossary
For the glossary of common iQMS terms, refer to the document Glossary of Common Terms
(TCS-iQMS-999).

TCS Internal 44
Python Programming Standards and Guidelines

FEEDBACK FORM

To: Head – Enterprise Quality Management


Tata Consultancy Services

Date:

Location:
From:

Project/Support Group/Other Functional Group:

________________________________________________________________________
Feedback details (Comments / Suggestions / Corrections / Queries)

(For associates to share their feedback, a feedback link is also available in iQMS Wiki)

(Please use additional sheets, if necessary)


For The Head – Enterprise Quality Management use

Action assigned to: Date assigned:


Details of action taken:

TCS Internal 45
END OF DOCUMENT

You might also like