Python —PEP8 and Packaging

A(The) Beautiful Soul
4 min readApr 2, 2019

--

# The Zen of Python
import this

# Python Enhancement Proposals (PEP)
# PEP 8 -> Style Guide on Writing Readable Code.
# Modules -> short, all-lowercase names
# Class names -> CapWords style
# Most variables and function names -> lowercase_with_underscores
# Constants -> CAPS_WITH_UNDERSCORES
# Names that clash with Python Keywords (‘class’ or ‘if’) -> Use a Trailing Underscore
# Use Spaces around Operators and after Commas.
# Lines shouldn’t be longer than 80 characters.
# ‘from module import *’ should be avoided.
# Only write one statement per line.
# Ignore PEP if it makes sense to do so.
# PEP 20 -> The Zen of Python
# PEP 257 -> Style Conventions for Docstrings.

def func1(first, second = 3, *args, **kwargs): #args is just a convention and you can use any other name.
#args return a tuple.
#The default value should be given after the named arguments without default value.
#kwargs stand for ‘keyword arguments’ and it stores the values in the form of a dictionary.
b = (2, 4, 1)
x, y, z = b #Tuple Unpacking
x, y = y, x #Tuple Unpacking because y, x on the right forms a tuple (y, x)
p, q, *r, s, t = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10 11] #Here r takes the values [3, 4, 5, 6, 7, 8, 9]
print(first + second)
print(args)

func1(7, 2, 2, 4, 9, 3, s = 4, a = 8)

# Ternery Operator
#Applications: Conditional Expressions
a = 1
b = 2 if a >= 1 else 3

for i in range(5):
if i == 10:
break
else:
print(“Not broken 1”) #This will get printed as break is not executed.

for i in range(15):
if i == 10:
break
else:
print(“Not broken 2”) #This will not get printed as break is executed.

try:
print(1)
except ZeroDivisionError:
print(2)
else:
print(3)

try:
print(1/0)
except ZeroDivisionError:
print(4)
else:
print(5)

#output is
# 1
# 3
# 4

# __main__
def func1():
print(“This is a module function”)
if __name__ == “__main__”:
print(“This is a Script”)
#The python interpreter before executing the code defines a few special variables.
#Eg: If the Python Interpreter is running the module (the source file) as the main program then it sets the special variable as:
# __name__ == “__main__”
#If this file is imported from another module then:
# __name__ == “that_module’s_name”

#If we save the above code as a file called func_main.py then we can import it to another script as a module using the name func_main.
import func_main
func_main.func1()

#output is
#This is a module function

#### Major 3rd-Party Libraries.
# Python has a module ‘urllib’ to programmatically access websites but it is cumbersome to use.
# Third party library requests make it much easier to use HTTP request.
#Web Frameworks
#Django
#CherryPy
#Flask

#Web Scrapper
#BeautifulSoup
#This leads to better results than using your own scrapper with regular expressions.

#Matplotlib
#To plot graphs
#SciPy
#NumPy
#Multidimensional array Operations.

#Game Development
#Panda3D
#To Build 3D games with Python.
#Pygame
#To Build 2D games with Python.

#### Packaging

# Directory_name/
# LICENSE.txt
# README.txt
# setup.py
# Inner_Directory_name
# __init__.py
# Script_file1.py
# Script_file2.py
# Script_file3.py
# ..

#### setup.py file
#This contains information necessary to assemble the package so it can be uploaded to PyPI and installed with pip.

from distutils.core import setup
setup(
name = ‘FastDL’,
version = ‘0.1dev’,
packages = [‘Inner_Directory_name’,],
license = ‘MIT’,
long_description = open(‘README.txt’).read(),
)

#Source Distribution is the Human readable verion which is the raw, uncompiled code.
#Binary Distribution is the Computer readable version which is the compiled code.

# Packaging Modules for use by other Python Programmers.
# Upload the setup.py file directly to PyPI.
# Or use the command line to create a binary distribution (an executable installer).
#To build a source distribution, use the command line to navigate to the directory containing setup.py, and run the command
#python setup.py sdist
#To build a Binary Distribution
#python setup.py bdist
#python setup.py bdist_wininst (for Windows)
#Use
#python setup.py register
#Followed by
#python setup.py sdist upload (to upload a package)
#Finally, install a package with
#python setup.py install

# Packaging Modules for use by other computer users.
# Linux users.
#Python is already installed and are able to run scripts as they are.
# Windows users.
#Convert into Executable format using:
#Py2exe
# Mac Users.
#Convert into Executable format using:
#Py2app
#PyInstaller
#Cx_Freeze

# — — — — — — — — — — — — — — — — — — — — — — —

--

--