Advance Python Concept II
- Serialization
Python provides you a built-in JSON libraries to encode and decode JSON.
import jsonTher are two basic formats for JSON data; string or the object data structure. In object, elements can be add, list, search and remove as in lists and dictionaries. String format is use to pass data into another program or load into a data structure.
import json
json_string = json.dumps([1, 2, 3])
Python has a proprietary data serialization method called pickle. There is another alternative to it named cPickle which is faster.
import pickle
pickled_string = pickle.dumps([1, 3, a, s])
print(pickle.loads(pickled_string))2. Partial Functions
There is partial function from functions library. It allows you to derive a function with a parameter to a function with fewer parameters and fix value set for more limited function.
from functools import partial
def add(x, y):
return x +y
dbl = partial(add, 2)
print(dbl(4))3. Code Introspection
Code introspection is a program’s ability to examine the type, properties, structure, or values of an object and its code metadata at runtime. It lets developers inspect classes, functions and modules while an app runs to see what they do and contain.
It is commonly used for debugging — to inspect the unexpected change of state or values of the variables and object, frameworks and ORMS — to automatically map database fields to code properties.
Some popular tools in python are:- type(), dir(), help(), isinstance(). id(), callable() and so on.
4. Closures
Closure is a nested function that remembers and accesses variables from its outer function’s scope, even after the outer function has finished its execution. Nested function is another function which is inside a function.
def print_msg(number):
def printer():
print(number)
printer()
print_msg(9)Its major advantage is it can avoid use of global variables and provides some form of data hiding. It is use for data encapsulation, memory efficiency and function factories.
Get Leekhitame’s stories in your inbox
Join Medium for free to get updates from this writer.
5. Decorators
Decorators is a design pattern that allows you to modify or extend the behavior of a function or class without permanently changing its source code. Its common usages are logging, access control, caching and measuring execution time.
def changecase(func):
def myinner():
return func().upper()
return myinner
@changecase
def myfunction():
return "Hello Sally"
@changecase
def otherfunction():
return "I am speed!"
print(myfunction())
print(otherfunction())6. Map, Filter, Reduce
Map function returns a list.
my_list = ['zippy', 'yolk', 'maxine']
upper = list(map(str.upper, my_list))here, upper takes one argument so we passed one iterable to it.
Filter function returns boolean values and then passes each element in th iterable through the functions.
num = [34. 32, 90. 53, 80]
def isLessThanFifty(n):
return num < 50
lessNum = list(filter(isLessThanFifty, num))
print(lessNum)Reduce applies a function of two arguments cumulatively to the elements of an iterable, optionally its starts with an initial argument.
syntax: reduce(func, iterable[ , initial])
from functiontools import rduce
num = [4, 3, 2, 9, 7]
def custom_sum(first, second):
reutrn first + second
result = reduce(custom_sum, numbers)7. Parsing CSV Files
CSV — Comma Separated Value. A standard way to parse CSV files in python is by using built-in csv module.
import csv
with opn("data.csv", mode="r", encoding="utf-8") as file:
reader = csv.reader(file)
for row in reader:
print(row)Some common features are:
csv.writer
csv.writer
csv.field_size_limit etc.
That’s it. Now, you are I, we both know equal amount of python.
Enjoy Your Life
LA





