Python

From Network Security Wiki


Basics

The Zen of Python

  • Writing programs that actually do what they are supposed to do is just one component of being a good Python programmer.
  • It's also important to write clean code that is easily understood, even weeks after you've written it.
1.  Beautiful is better than ugly.
2.  Explicit is better than implicit.(It is best to spell out exactly what your code is doing. This is why adding a numeric string to an integer requires explicit conversion, rather than having it happen behind the scenes, as it does in other languages.)
3.  Simple is better than complex.
4.  Complex is better than complicated.
5.  Flat is better than nested.(Heavily nested structures (lists of lists, of lists, and on and on…) should be avoided.)
6.  Sparse is better than dense.
7.  Readability counts.
8.  Special cases aren't special enough to break the rules.
9.  Although practicality beats purity.
10. Errors should never pass silently. (In general, when an error occurs, you should output some sort of error message, rather than ignoring it.)
11. Unless explicitly silenced.
12. In the face of ambiguity, refuse the temptation to guess.
13. There should be one-- and preferably only one --obvious way to do it.(References and contradicts the Perl language philosophy that there should be more than one way to do it.)
14. Although that way may not be obvious at first unless you're Dutch.
15. Now is better than never.
16. Although never is often better than *right* now.
17. If the implementation is hard to explain, it's a bad idea.
18. If the implementation is easy to explain, it may be a good idea.
19. Namespaces are one honking great idea -- let's do more of those!
  • The 20th principle is a matter of opinion, but our interpretation is that the blank line means "Use whitespace".
  • Use the following code to access the Zen of Python:
import this

Python Enhancement Proposals

  • PEP are suggestions for improvements to the language, made by experienced Python developers.
  • PEP 8 is a style guide on the subject of writing readable code:
Modules should have short, all-lowercase names
Class names should be in the CapWords style
Most variables and function names should be lowercase_with_underscores
Constants (variables that never change value) should be CAPS_WITH_UNDERSCORES
Names that would clash with Python keywords (such as 'class' or 'if') should have a trailing underscore.
Using spaces around operators and after commas to increase readability. 
Lines shouldn't be longer than 80 characters
'from module import *' should be avoided
There should only be one statement per line
Use spaces, rather than tabs, to indent.
However, to some extent, this is a matter of personal preference.
If you use spaces, only use 4 per line.
It's more important to choose one and stick to it.
  • The most important advice in the PEP is to ignore it when it makes sense to do so.
  • Don't bother with following PEP suggestions when it would cause your code to be less readable; inconsistent with the surrounding code; or not backwards compatible.

__main__

  • Most Python code is either a module to be imported, or a script that does something.
  • However, sometimes it is useful to make a file that can be both imported as a module and run as a script.
  • To do this, place script code inside if __name__ == "__main__"
  • This ensures that it won't be run if the file is imported.
  • When the Python interpreter reads a source file, it executes all of the code it finds in the file.
  • Before executing the code, it defines a few special variables.
  • For example, if the Python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "__main__".
  • If this file is being imported from another module, __name__ will be set to the module's name.
def function():
   print("This is a module function")

if __name__=="__main__":
   print("This is a script")

>>>
This is a script
>>>

Components

Variables

Declaring Variables:

list=[1,2,3]
string="Hello"
int=23


Basic argument specifiers:

%s                      - String (or any object with a string representation, like numbers)
%d                      - Integers
%f                      - Floating point numbers
%.<number of digits>f   - Floating point numbers with a fixed amount of digits to the right of the dot.
%x/%X                   - Integers in hex representation (lowercase/uppercase)


Indentation is Mandatory in python:

num=12
if num>5:
 print("Number is greater than 5")
 if num<20:
   print("Number is lesser than 20")
print("Program Ended")


Operator Precedence from highest to lowest order:

Statements

IF & ELIF statements:

num=9
if num==5:
 print("Number is 5")
elif num ==11:
 print("Number is 11")
elif num==7:
 print("Number is 7")
else:
 print("Number isn't 5,11,7")


AND returns true only if both are true

>>> 1 == 1 and 2==2
True
>>> 1 == 1 and 2==3
False
>>> 1 != 1 and 2==2
False
>>> 1 == 1 and 2==2
True

OR returns true if either of both arguments are true; false if both are false.

>>> 1 != 1 or 2==2
True
>>> 1 == 1 or 2==2
True
>>> 1 == 1 or 2==3
True


Lists

words=["Hello","World","!"]
print(words[0])
print(words[1])
print(words[2])

Mixed List:

number=3
things=['string',0,[1,2,number],4,56]
print(things[1])
print(things[2])
print(things[2][2])


List Operators:

nums = [1,2,3]
print(nums+[4,5,6])
print(nums*3)

in operator:

>>> words=['spam',"egg"]
>>> print("spam" in words)
True
>>> print("tomato" in words)
False
>>> print (not "tomato" in words)
True
>>> print ("tomato" not in words)
True

Append to the list:

nums=[1,2,3]
nums.append(4)
print(nums)

Len function:

  1. Unlike append, len is a normal function rather than a method.
  2. Therefore it is written before the list it is being called on, without a dot.
nums=[1,3,5,2,4]
print(len(nums))


Insert Method is similar to append, but allows to add at any position in the list

words=["Python","fun"]
index=1
words.insert(index,"is")
print(words)

Tuple

  • Tuples are immutable which means you cannot update or change the values of tuple elements.
  • You are able to take portions of existing tuples to create new tuples.
  • Tuples cannot be changed unlike Lists.
  • They use parentheses.
  • Declaring tuples:
tup1 = ()
tup2 = (50,)
tup3 = ('physics', 'chemistry', 1997, 2000)
tup4 = (1, 2, 3, 4, 5 )
tup5 = "a", "b", "c", "d"
  • Printing tuples:
tup1[0]
print "tup2[1:5]: ", tup2[1:5]
  • Adding tuples:
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
tup3 = tup1 + tup2
print (tup3)
(12, 34.56, 'abc', 'xyz')
  • Following action is not valid for tuples
  tup1[0] = 100 

Dictionary

  • All keys are unique, If you update item with same keys, Value will get replaced.
  • How a dictionary looks like:
{'abc': '43f9', 'sample': 'fc9e'}
  • Declaring a Dictionary:
x = {}
  • Adding Entry:
x.update({"abc": "112"})
  • Another example for adding Entry:
for file in fullpath(dir):
       filesize = os.path.getsize(file)
       print(file, filesize)
       x.update({file: filesize})
  • Dictionary Comprehension:
sq = {x: x*x for x in range(10)}
print(sq)
for i in sq:
   print(sq[i])
  • Printing Dictionary:
>>> x.items()
dict_items([('a', 12), ('b', 14), ('c', 16), ('d', 18)])
  • Printing items from dictionary:
for k,v in x.items():
   print(k,v)
  • Printing specific entries:
x['a']
x[4]
  • Length of Dictionary:
len(x)

Dictionary Advanced

  • How To Create a Dictionary in Python
example_dict = {}
example_dict = {1: 'mango', 2: 'pawpaw'}
example_dict = {'fruit': 'mango', 1: [4, 6, 8]}
example_dict = dict({1:'mango', 2:'pawpaw'})
example_dict = dict([(1,'mango'), (2,'pawpaw')])
example_dict = {1: {'student1' : 'Nicholas', 'student2' : 'John', 'student3' : 'Mercy'},
        2: {'course1' : 'Computer Science', 'course2' : 'Mathematics', 'course3' : 'Accounting'}}
  • How To Access Elements of a Python Dictionary
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
example_dict["model"]
example_dict = {'Name': 'Mercy', 'Age': 23, 'Course': 'Accounting'}
print("Student Name:", example_dict['Name'])
print("Course:", example_dict['Course'])
print("Age:", example_dict['Age'])
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
x = example_dict.get("model")
print(x)
  • How To Add Elements to a Python Dictionary
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
example_dict["Capacity"] = "1800CC"
print(example_dict)
example_dict = {}
example_dict[0] = 'Apples'
example_dict[2] = 'Mangoes'
example_dict[3] = 20
print("\n3 elements have been added: ")
print(example_dict)
  • How To Update Dictionary Elements
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
example_dict["year"] = 2014
print(example_dict)
  • How To Remove Dictionary Elements
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
del example_dict["year"]
print(example_dict)
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
example_dict.pop("year")
print(example_dict)
  • The popitem() method removes the last item inserted into the dictionary, without needing to specify the key.
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
example_dict.popitem()
print(example_dict)
  • Delete the entire dictionary
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
del example_dict
print(example_dict)
  • You might need to remove all dictionary elements but not the dictionary itself.
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
example_dict.clear()
print(example_dict)
  • Other Common Dictionary Methods in Python
print(len(example_dict))
  • copy() method returns a copy of the existing dictionary.
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
x = example_dict.copy()
print(x)
  • items() Method returns an iterable object
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
for k, v in example_dict.items():
  print(k, v)
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
x = example_dict.items()
print(x)
example_dict["model"] = "Mark X"
print(x)
  • fromkeys() Method returns a dictionary having specified keys and values:
The value for the required keys parameter is iterable and it specifies the keys for the new dictionary. The value for the value parameter is optional and it specifies the default value for all the keys. The default value for this is None.
Suppose we need to create a dictionary of three keys all with the same value, say 25:
name = ('John', 'Nicholas', 'Mercy')
age = 25
example_dict = dict.fromkeys(name, age)
print(example_dict)
name = ('John', 'Nicholas', 'Mercy')
example_dict = dict.fromkeys(name)
print(example_dict)
  • keys() Method also returns an iterable object.
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
x = example_dict.keys()
print(x)
  • Often times this method is used to iterate through each key in your dictionary:
example_dict = {
  "Company": "Toyota",
  "model": "Premio",
  "year": 2012
}
for k in example_dict.keys():
  print(k)

List/Arrays

  • Declaring Arrays:
x = [1,2,4,6,8,9]
x = [i for i in range(10)]
x = [i*i for i in range(10)]
  • Appending values:
x = []
for i in range(100):
  x.append(i)
  • Printing specific values:
print(x[20])

Loops

While Loop runs more than once, till condition is true, once condition is false, next section of code is executed:

i=1
while i<=500:
 print(i)
 i=i+1

print("Finished !")

Infinity while loop: condition always remain True:

while 1==1:
 print('In a loop..!!')

Break the infinity Loop:

i=0
while 1==1:
 print(i)
 i=i+1
 if i>=25:
  print('Breaking!')
  break

print("Finished!")


Continue this jumps back to top of the loop, rather than stopping it

i=0
while True:
 i=i+1
 if i==10:
  print('Skip 10')
  continue
 if i==25:
  print("Breaking")
  break
 print(i)
print("Finished!")


While Loop
words = ["hello","world","spam","egg"]
counter = 0
max_index = len(words)-1

while counter <= max_index:
 word = words[counter]
 print(word + "!")
 counter = counter + 1
Same code can be created using FOR Loop but fewer lines
words = ["hello","world","spam","egg"]
for word in words:
 print(word + "!")

Loop can be combined with Range objects(No need to call List on range objects in For Loop as it is not indexed):

for i in range(5):
 print("hello")

Functions

def myfunc():
 print("spam")
 print("spam")
 print("spam")

myfunc()

Arguments

def printe(word):
 print(word + "!")

printe("spam")
printe("world")
printe("egg")


Arguments: Functions can have more that 1 argument:

def print_twice(x,y):
 print(x+y)
 print(x+y)

print_twice(5,8)

Function arguments can be used as variables inside function, not outside:

def incr(x):
 x += 1
 print(x)

incr(7)
print(x)

Return: int & str functions return a value which can be used later: Return cannot be used outside of a function.

def max(x,y):
 if x>=y:
  return x
 else:
  return y

print(max(4,7))
z = max(8,5)
print(z)
def short(x,y):
 if len(x) <= len(y):
   return x
 else:
   return y

After using return in a function, it stops executing immediately:

def add(x,y):
 total = x + y
 return total
 print("This wont be printed")

print(add(4,5))

Lambda Function

>>> mul = lambda x: x**2 + 2*x-5
>>> mul(5)
30

>>> add = lambda y: a + b
>>> a=6
>>> b=5
>>> add (10)
11

>>> add2 = lambda a,b: a + b
>>> add2(10,3)
13

>>> rem = lambda a,b: a%b
>>> add (10,3)
1


Functions as an Object: they can be assigned & reassigned to variables.

def multiply(x,y):
 return x*y

a = 4
b = 7
operation = multiply
print(operation(a,b))
def shout(word):
 return word + "!"
speak = shout
output = speak("shout")
print(output)


Function can be used as arguments of other functions:

def add(x,y):
 return x + y

def twice(func(x,y)):
 return func(func(x,y), func(x,y))

a=6
b=12

print(twice(add,a,b))
def square(x):
 return x*x

def test(func,x):
 print(func(x))

test(square,42)

Comments

#This is a comment.
print("Test") #This is also a comment.
#print("Test 2")

Docstrings:

def excl(word):
 """
 Print a word
 with exclamation!
 """
 print(word + "!")

shout("spam")

Examples

Entering variables inside quotation marks:

os.system("""osascript -e 'display notification "{}" with title "{}" sound name "{}"'""".format(text, title, sound))

Modules

Modules are piece of code written by others to fulfil common tasks.

Print 5 random numbers in range 1 to 8:

import random
for i in range(5):
 value = random.randint(1,8)
 print(value)
import math
num = 10
print(math.sqrt(num))


Import certain functions only from module:

from math import pi
print(pi)
from math import pi,sqrt
from math import *            # not recommended, can confuse the variables in code with variables external module.
from math import sqrt as square_root
print(square_root(100))


3 types of Modules:

Standard Library -> string, re, datetime, math, random, os, multiprocessing, subprocess, socket, email, json, doctest, unitest, pdb, argparse, sys
Installed from external sources
Manually written


Python Package Index

PyPI is used to install 3rd party modules.

sudo apt-get install python-pip
pip install library_name
pip install paramiko


List modules:

pip list
pip freeze
help('modules')

Common Packages

paramiko          SSH
ipaddr            IP addressing

Slack

Installation:

pip install slackclient

Simple function:

from slackclient import SlackClient

 def slack(message, channel, icon):
     sc = SlackClient("xoxp-353395......6a42822")
     sc.api_call('chat.postMessage', channel=channel, text=message, username='MyBot', icon_emoji=icon)

 slack("Hello", "@aman", ':scroll:')
 slack("Hello", "team-alerts", ':scroll:')

Function with Attachment:

text1 = 'Case - 73416 - Severity 1 (Critical) - xyz Inc -  System crashed three times'
text2 = 'Case - 73411 - Severity 2 (High) - zyx Ltd -  System stuck in initializing state'
def slack_message(message, email, channel):
    token = 'xoxp-3533423224-303453435575-359136310-95d593463sfs32529057e311tre434822'
    sc = SlackClient(token)
    colors = 0
    sev = message.split('-')[2]
    if '1' in sev:
        colors = 'danger'
    elif '2' in sev:
        colors = '#e5505a'
    elif '3' in sev:
        colors = '#ffee00'
    elif '4' in sev:
        colors = '#a4ef02'
    elif '5' in sev:
        colors = 'good'
    attachments = []
    attachments.append({
                'title': 'Case: ' + message.split('-')[1],
                'text': 'Issue: ' + message.split('-')[4] + '\n' + 'Customer:' + message.split('-')[3] + '\n' + 'Urgency: '+ sev,
                'color': colors,
                'footer': 'Env: ' + email,
                'ts': 123456789
            })
    formatted_result = ({
        'title': 'Execution Results',
        'attachments': attachments,
        'as_user': 'false'
        })
    response = sc.api_call('chat.postMessage', channel=channel,
                text='New Case in Queue', **formatted_result, username='My Bot',
                icon_emoji=':brief_case:')
    if not response['ok']:
        log('Slack Error: {}'.format(response['error']))
slack_message(text1, email_from, 'team-channel')

Slack v2

  • Simple Example
import slack

def slack_message(message):
    token = 'xoxp-3533423224-303453435575-359136310-95d593463sfs32529057e311tre434822'
    sc = slack.WebClient(token, timeout=30)
    sc.chat_postMessage(
        channel='@aman',
        username='My Bot',
        icon_emoji=':robot_face:',
        text=message)

path = '/mnt/cores/Customer/case_10682/'
option = 'Cloud'

slack_message(str("Path: "+path+'\n'+"Option: "+option))
  • Attachment
import slack

path = '/mnt/cores/Customer/case_10682/'
option = 'Cloud'

def slack_message():
    token = 'xoxp-3533423224-303453435575-359136310-95d593463sfs32529057e311tre434822'
    sc = slack.WebClient(token, timeout=30)
    attachments = []
    attachments.append({
        'title': 'Tool: ' + 'Tech Support',
        'text': 'Path: ' + path + '\n' + 'Option:' + option + '\n',
        'color': 'red',
        'footer': 'Sender: ' + 'eMail-ID',
        'ts': 123456789
    })

    formatted_result = ({
        'title': 'Execution Results',
        'attachments': attachments,
        'as_user': 'false'
        })

    response = sc.chat_postMessage(channel='@aman',
                text='New Case in Queue', **formatted_result, username='My Bot',
                icon_emoji=':brief_case:')

    if not response['ok']:
        print('Slack Error: {}'.format(response['error']))

slack_message()

Salesforce

Installation

pip3 install simple_salesforce

Find out Security Token:

https://<yoursalesforcehostname>/_ui/system/security/ResetApiTokenEdit?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DPersonalInfo&setupid=ResetApiToken

Example:

https://avi.my.salesforce.com/_ui/system/security/ResetApiTokenEdit?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DPersonalInfo&setupid=ResetApiToken    

Login:

sf = Salesforce(username="test", password="pwd@123", security_token="NSUgqlxewweWdK8bjBEsdsNs")

Query:

results = sf.query_all("SELECT CaseNumber,Subject,IsClosed FROM Case WHERE OwnerId = '00500000erefBz'")
result2 = sf.query_all("SELECT CaseNumber,Subject,IsClosed FROM Case WHERE CaseNumber = '6800'")

Output:

results['records'])

Exceptions

 ImportError: an import fails
 IndexError: list is indexed with an out of range  number
 NameError: an unknown variable is used
 SyntaxError: the code can't be parsed properly
 TypeError: a function is called on a value of inappropriate type
 ValueError: a function is called on value of correct type but with inappropriate value
 OSError:

Third party libraries has their own defined exceptions.

ZeroDivisionError:

 x = 7
 y = 0
 print(x/y)

Exception Handling

try:
 x = 7
 y = 0
 print(x/y)
 print("Calc Done")
except ZeroDivisionError:
 print("An Error Occured")
 print("due to zero division")

Handling multiple exceptionin a single except statement:

try:
 x = 10
 print(x + "Hello")
 print(x / 2)
except ZeroDivisionError:
 print("Division by Zero")
except(ValueError, TypeError):
 print("Error occured")

Catch all error(not recommended, they can catch unexpected errors and hide programming mistakes): Exception handling is quite useful when dealing with user input.

try:
 word = "spam"
 print(word / 0)
except:
 print("An error occured")

Finally: this code will always run, even if uncaught exception occurs.

try: 
 print("Hello")
 print(1/0)
except ZeroDivisionError:
 print("Div by Zero")
finally:
 print("This will always run")
try:
 print(1)
 print(10/0)
except ZeroDivisionError:
 print(unknown_var)
finally:
 print("This is executed last")

Raising Exceptions:

print(1)
raise ValueError
print(2)
name = "123"
raise NameError("Invalid name!")
x = input(":")
if float(x) < 0:
 raise ValueError("Negative!")

Raise statement can be used to raise whatever exception occurs:

try:
 x = 5/0
except:
 print("An error occured")
 raise

Traceback

import traceback

try:
    <something>
except:
    traceback.print_exc()
    pass

Assertions

Used for sanity checking, programmers use this at start of fuction to check for valid input and after function call to check for valid output.
Expression is tested, if result is false, an exception is raised.

print(1)
assert 2+2 == 4
print(2)
assert 1 + 1 == 3
print(3)

Assertons can take a second argument passed to AssertionError if assertion fails:
Assertions can be caught & handled like other exceptions using Try-Except statement.
If not handled, it can terminate the program.

temp = -10
assert (temp >= 0),"Colder than absolute Zero!"

Exercise

Function that returns sum of all numbers from 0 till that number:

def sum(x):
 res=0
 for i in range(x):
 res+=1
 return res

What is highest nymber output by this code?

def nums(x):
 for i in range(x):
  print(i)
  return
nums(10)

What is the output of this code?

def func(x):
 res = 0
 for i in range(x):
  res += i
 return res

print(func(4))

What is the highest number printed by this code:

print(0)
assert "h" != "w"
print(1)
assert False
print(2)
assert True
print(3)

Running Unix Commands

os.system(deprecated)

import os
os.system("date")
 import os
 f = os.popen('date')
 now = f.read()
 print "Today is ", now


Subprocess

Basic usage:

import subprocess
subprocess.call("command1")
subprocess.call(["command1", "arg1", "arg2"])

Execute the date command:

import subprocess
subprocess.call("date")

Execute ls command with arguments:

import subprocess
subprocess.call(["ls", "-l", "/etc/resolv.conf"])

Execute date command:

import subprocess
p = subprocess.Popen("date", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print "Today is", output


import subprocess
p = subprocess.Popen(["ls", "-l", "/etc/resolv.conf"], stdout=subprocess.PIPE)
output, err = p.communicate()
print "*** Running ls -l command ***\n", output
import subprocess
p = subprocess.Popen(["ping", "-c", "10", "4.2.2.2"], stdout=subprocess.PIPE)
output, err = p.communicate()
print  output

The only problem with above code is that output, err = p.communicate() will block next statement till ping is completed i.e. you will not get real time output from the ping command.
So you can use the following code to get real time output:

import subprocess
cmdping = "ping -c4 4.2.2.2"
p = subprocess.Popen(cmdping, shell=True, stderr=subprocess.PIPE)
while True:
    out = p.stderr.read(1)
    if out == '' and p.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()

SSH

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect( '192.168.1.23', username = 'root', password = 'libreelec' )
stdin, stdout, stderr = ssh.exec_command( 'ls -al' )

for line in stdout:
    print('... ' + line.strip('\n'))
ssh.close()

Regex

Importing Library

import re

Methods:

re.match() => Matches Beginning

Here for 'dog' it matches:

>>> re.match(r'dog', 'dog cat dog')
<_sre.SRE_Match object at 0xb743e720<
>>> match = re.match(r'dog', 'dog cat dog')
>>> match.group(0)
'dog'

However for 'cat' it does not:

>>> re.match(r'cat', 'dog cat dog')
>>>
re.search() => Matches Anywhere
re.search(r'cat', 'dog cat dog')
 >>> match.group(0)
 'cat'

The search() method stops looking after it finds a match, so search()-ing for ‘dog’ finds the first occurrence:

 >>> match = re.search(r'dog', 'dog cat dog')
 >>> match.group(0)
 'dog'
re.findall() => All Matching Objects
>>> re.findall(r'dog', 'dog cat dog')
['dog', 'dog']
>>> re.findall(r'cat', 'dog cat dog')
['cat']
Substitute
s = "Example String"
replaced = re.sub('[ES]', 'a', s)
print(replaced)

Using Databases

Installing Modules for MySQL:

sudo apt-get install python-pip python-dev libmysqlclient-dev
pip install MySQL-python

Connecting to a DB on localhost(need to create a db 'testdb' beforehand):
Source: tutorialspoint.com

#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect("localhost","guest","guest123","testdb")

cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print "Database Version : %s " % data
db.close()


Creating Database:

#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect("localhost","guest","guest123","testdb")

cursor = db.cursor()
sql="""CREATE table book_tbl (
       book_id INT NOT NULL AUTO_INCREMENT,
       book_title VARCHAR(100) NOT NULL,
       book_author VARCHAR(100) NOT NULL,
       submission_date DATE,
       PRIMARY KEY( book_id ));"""

cursor.execute(sql)
db.close()

Writing to Database with User Input:

#!/usr/bin/python
import MySQLdb
a=int(input('Please enter the Book ID:\n'))
b=str(raw_input('Please enter the Book Title:\n'))
c=str(raw_input('Please enter the Book Author:\n'))
d=int(input('Please enter the Book Submission Date:\n'))

db = MySQLdb.connect("localhost","guest","guest123","testdb" )

cursor = db.cursor()

sql = "INSERT INTO book_tbl(book_id, \
       book_title, book_author, submission_date) \
       VALUES ('%d', '%s', '%s', '%d' )" % \
       (a, b, c, d )
try:
   cursor.execute(sql)
   db.commit()
   print("Database Updated.")
except:
   db.rollback()
   print("Error when saving to DB.\n")
db.close()

Read from Database:

#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect("localhost","guest","guest123","testdb" )

cursor = db.cursor()

sql = "SELECT * FROM book_tbl WHERE book_id > '%d'" % (10)

try:
  cursor.execute(sql)
  results = cursor.fetchall()
  for row in results:
      ID = row[0]
      Title = row[1]
      Author = row[2]
      Date = row[3]

      print "ID=%d,Title=%s,Author=%s,Date=%s" % (ID, Title, Author, Date )
except:
  print "Error: unable to fetch data"
db.close()

Pillow

  • Install in Python3
sudo apt-get install python3-pip
sudo apt-get install python3-dev python3-setuptools
sudo apt-get install libjpeg8-dev zlib1g-dev libwebp-dev tcl8.5-dev tk8.5-dev
sudo pip3 install Pillow
from PIL import Image

im = Image.open("sample.webp").convert("RGB")
im.save("test.jpg","jpeg")

OpenCV

Windows
  • Install NumPy
sudo pip3 install numpy
  • Download the relevant WHL file from:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv
  • Install OpenCV WHL file:
pip install opencv_python‑3.3.1‑cp36‑cp36m‑win32.whl
  • Verify installation:
import cv2
print(cv2.__version__)
Linux
sudo pip3 install opencv-contrib-python

Non Recursive files only list

Using Functions(file name only)
dir = sys.argv[1]

def files(path):
    for file in os.listdir(path):
        if os.path.isfile(os.path.join(path, file)):
            yield file

for file in files(dir):
    print(file)
Using Functions(full paths)
dir = sys.argv[1]

def fullpath(path):
    for file in os.listdir(path):
        if os.path.isfile(os.path.join(path, file)):
            yield (os.path.join(path, file))

for file in fullpath(dir):
    print(file)
Without using Functions
dir = sys.argv[1]

files = [f for f in os.listdir(dir) if os.path.isfile(os.path.join(dir, f))]

for file in files:
    print(file)

Performance Test

cProfile

  • cProfile is the classic profiling tool:
python3 -m cProfile myscript.py
        1003236 function calls in 2.163 CPU seconds
  Ordered by: standard name
  ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       1    0.000    0.000    2.163    2.163 <string>:1(<module>)
 1000014    1.039    0.000    1.821    0.000 mersenne.twister.py:19(extract_number)
    1603    0.766    0.000    0.782    0.000 mersenne.twister.py:33(generate_numbers)
  • ncalls is the number of times a function was called.
  • tottime is the total time spent in a function, excluding the time spent in sub-function calls.
  • percall is tottime / ncalls.
  • cumtime is the time spent in the function including the time spent in sub-function calls.
  • And the remaining data is as follows: filename:lineno(func_name).
  • In most cases, look at ncalls and tottime first.
  • In the above data, the large majority of the time spent by this program happens in extract_number.
  • Furthermore, we can see that extract_number is called many (1000014) times.
  • Anything can be done to speed up extract_number will significantly speed up the execution of this test code.
  • If it gains me a microsecond, then the gain will be multiplied by 1000014, resulting in a full second gain.
  • Then work on generate_numbers.
  • Gains there won't matter as much, but they may still be significant, and since that function burns another .7 seconds, there's some benefit to be had.
  • tottime number can sometimes be deceptive, in cases of recursion, for example.

Misc

Python 2 to Python 3 conversion tool
2to3 -w test.py
Print in the same position
print(data, end='\r')


References





{{#widget:DISQUS |id=networkm |uniqid=Python |url=https://aman.awiki.org/wiki/Python }}