Python Code Snippets
To Concat multiple values
temp_list = []
for values in li_tags:
temp_list.append(values)
concat = '#'.join(temp_list)
Default Encoding/Encoding
-*Style-1*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
-*Style-2*-
chunk = chunk.decode('unicode-escape')
chunk = chunk.encode('ascii', 'ignore')
-*Style-3*-
# -*- coding: utf-8 -*-
Extracting from URL/URL Parsing
URL = 'https://sites.google.com/view/way2learnings/python-snippet/?brd=google'
parsed = urlparse.urlparse(URL)
if "brd" in str(url_data):
target_brand=urlparse.parse_qs(parsed.query)['brd'][0]
Batch Install or Uninstall Using PIP (Python)
create a list of libs (pip freeze > file.txt)
To Uninstall (pip uninstall -y -r file.txt)
To Install (pip install -r file.txt)
Calculating Program RunTime
import time
start = int (time.time() * 1000)
<code>
end = int (time.time() * 1000)
print (end - start) / 1000
To Clean \n \t (Meta tags)
data = re.sub('\s+',' ',data)
data = re.sub('\n|\t|^\s+\|\s+$','',data)
data = data.strip()
File Modes
r : Open text file for reading. The stream is positioned at the beginning of the file
r+ : Open for reading and writing. The stream is positioned at the beginning of the file
w : Truncate to zero length or create text file for writing. The stream is positioned at the beginning of the file
w+ : Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file
a : Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the current end of file
a+ : Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the current end of file
Python code Debugging
We can set breakpoints in our Python script with the help of the <pdb> module. Please follow the below example.
import pdb
pdb.set_trace()
We can specify <pdb.set_trace()> anywhere in the script and set a breakpoint there. It’s extremely convenient.
Difference B/W Dict and Set
# There is only a difference of ':' in both
my_dict = {i: i * i for i in xrange(10)}
my_set = {i * i for i in xrange(10)}
print my_dict
print my_set
>>{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
>>set([0, 1, 4, 81, 64, 9, 16, 49, 25, 36])
Collections : Named Tuples
import collections
Point = collections.namedtuple('Point', ['x', 'y'])
p = Point(x=1.0, y=2.0)
print p.y
>>2.0
List Compare Using Lambda
>>> import collections
>>> compare = lambda x, y: collections.Counter(x) == collections.Counter(y)
>>> compare([1,2,3], [1,2,3,3])
False
>>> compare([1,2,3], [1,2,3])
True
Converting Py into EXE
To make in single exe : pyinstaller --onefile <your_script_name>.py
Regular : python your_script.py
String Operations
sample_string ='sample@gmail.com'
Replace() : sample_string.replace('gmail','ymail')
Split() : sample_string.split('@') --> ['sample','ymail.com']
rpartition() : sample_string.rpartition('@') --> ('sample','@','ymail.com')
Sort Operations
a = [1,8,9,6,7,3,8,1,2]
print sorted(a)
#Duplicates : [1, 1, 2, 3, 6, 7, 8, 8, 9]
print sorted(set(a))
#Unique Only: [1, 2, 3, 6, 7, 8, 9]
a = [(1, 2), (4, 1), (9, 10), (13, -3)]
a.sort(key=lambda x: x[1])
print(a)
# Output: [(13, -3), (4, 1), (1, 2), (9, 10)]
Parallel Sort
data = zip(list1, list2)
data.sort()
list1, list2 = map(lambda t: list(t), zip(*data))
To get Current User Name Using pwd/getpass
try:
import pwd
except ImportError:
import getpass
pwd = None
def current_user():
if pwd:
return pwd.getpwuid(os.geteuid()).pw_name
else:
return getpass.getuser()
user_name = current_user()
File Size in Human readable format
def sizeof_fmt(num, suffix='o'):
"""Readable file size
:param num: Bytes value
:type num: int
:param suffix: Unit suffix (optionnal) default = o
:type suffix: str
:rtype: str
"""
for unit in ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1024.0:
return "%3.1f %s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
Get Image Size Using IO Operation
from io import BytesIO
img_file = BytesIO()
image.save(img_file, 'png')
image_file_size = img_file.tell()
CSV Writer and Reader
#Writer
results_tsv=open("sample.tsv","wb")
headers = ['Name','Age']
csvwriter = csv.DictWriter(results_tsv, delimiter='\t',fieldnames=headers)
csvwriter.writerow(dict((fn,fn) for fn in headers))
csvwriter.writerow({'Name':'dhamo','age':27})
#Reader
file_name = 'sample.tsv'
with open(file_name) as file_pointer:
for line in csv.DictReader(file_pointer, dialect="excel-tab"):
print line.get('Name')
Python Decorator Function
def our_decorator(func):
def function_wrapper(x,y):
print "Unfortunately i called first "
print "Subtraction : {} + {} = {}".format(x,y,x-y)
# func(x,y)
return function_wrapper
@our_decorator
def addition(x,y):
print "Addition : {} + {} = {}".format(x,y,x+y)
addition(6,2)
Factorial Calculation Using Python Decorator
factorial_nos = []
def argument_test_natural_number(fact):
def helper(x):
if type(x) == int and x > 0:
#actual function call post checking
return fact(x)
else:
raise Exception("Argument is not an integer")
return helper
#Actual function
@argument_test_natural_number
def factorial(n):
if n == 1: val = 1
else: val = n * factorial(n-1)
factorial_nos.append(val)
return val
factorial(3)
print factorial_nos
For Else Statement
data_list =['a','b','c']
for val in data_list:
if val=="aA":
print "Match Found!"
break
else:
print "No Match Found!"
Assign Value with Condition Using Next()
Next(): The next() function returns the next item from the iterator
my_list =['two','three','four']
i = next((elem for elem in my_list if elem == 'two'), None)