JSON in Python

Table of Contents What is JSON Definition Deserialize JSON in Python Serialize Python Data to JSON What is JSON Definition JSON is Javascript Object Notation. JSON is a light weight data exchange format. JSON string is a string that satisfies JSON format. {"name": "Isaac Zhou"} {'name': 'Isaac Zhou'} The above is a typical JSON string. JSON has several benefits: easy to read easy to analyze efficient with the network transaction It can transfer data across different languages [Read More]

Regex: General

Table of Contents Regex: General What is regex? Using Built-in Python Function Use Regex module Special char vs. Regular char Using Regex Special string char General char Counts match fixed length match a ranged length Greedy vs. Non-Greedy Search Greedy Non-greedy Matching Times Matching 0 or infinite times Use * Matching 1 or infinite times, Use + Matching 1 or 0 time, use ? [Read More]

Regex: Search and Match

Table of Contents search Check the returned value match Check the returned value match only once group There are two more methods in regex: search and match, however I don’t think they are as handy as .sub or .findall. Assume we have a string s=“A8C3721D86” search Check the returned value .search returns a re object, search will search for the whole string until the pattern fails [Read More]

Regex: Sub

Table of Contents Introducing re.sub the power of re.sub function as a parameter For regex, there are more than just find. Here I will discuss replacement Introducing re.sub re.sub matches first then replace the matched string import re lang = "PythonC#PHPC#C#C#C#" # replace C# with GO r = re.sub("C#", "GO", lang, 0) r 'PythonGOPHPGOGOGOGO' The 4th parameter 0 means to replace C# infinitly. If we change 0 to 1, C# will be replaced only once [Read More]

Init Method

__init__ method define __init__ function class User(): name = "" pwd = "" def __init__(self): print("user") def sign_up(self): print("User Sign Up") __init__ will be invoked automatically when instantiating a class user = User() # __init__ is seldom called this way user.__init__() user user user.sign_up() User Sign Up Only return None __init__ function can only return a None class Org(): def __init__(self): return "This is Org" org = Org() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-06eab282d464> in <module>() 3 return "This is Org" 4 ----> 5 org = Org() TypeError: __init__() should return None, not 'str' How to take parameters __init__ function can take parameters, for example __init__ will be invoked automatically # This is the wrong way to define the class class Org(): name = "" org_type = "" country = "" def __init__(self, name, org_type, country): # initialize the object attribute name = name org_type = org_type country = country print("{} was successfully created! [Read More]

Class Definition

Object Oriented Programming (OOP) We need to write meaningful OOP codes, merely having classes doesn’t mean it’s oop. Defining a class Class: defined by class ClassName class Student(): pass Define variables, functions/methods To use a class, we need to instantiate it first See examples below # define a class class Student(): name = "" age = 0 def print_file(): print("name: {} \n age: {}". [Read More]

Class and Object

Class and Object Class: a mapping between computer programs and the entities from real/thinking realm. It’s a blueprint that defines a set of attributes that will characterize any object that is instantiated from this class. Class encapsulates data (variables) and operations (methods) on those data A good class design depends on behavior (method) and attribute (variables). Behaviors need to be relevant with the class For example, for the class definition below, method print_file shouldn’t belong to Student, rather it should belong to Printer [Read More]


Everything is an Object In other languages, function is just an executable codes, but in Python everything is an object. So function is a type of object. In python, a function is an object, moreover, a function can be an argument in another function or a function can be a result returned from another function def test(): pass a = test print(type(a)) <class 'function'> 1.1 What is a closure? [Read More]


Decorator 1.1 Modifying the function decorator is more of a design pattern for exmaple # f1 prints a simple sentence def f1(): print("This is a function") Now, f1 doesn’t fit my need, I want to print out time as well import time def f1_old(): print(time.time()) print("This is a function") # it prints out a Unix timestamp and sentence f1_old() 1549295628.544087 This is a function 1. [Read More]

Decorator Side Effects

Decorator Side Effects 1.1 Decorator might change the function name In this post, we will deep-dive the side effects of decorators. recall our example before, print Unix Time everytime we run the function import time def decorator(func): def wrapper(): print(time.time()) func() return wrapper @decorator def f1(): print("This is a function") f1() 1549311820.0338516 This is a function # Now if f1 is defined without decorator, I want to print out f1's name import time def decorator(func): def wrapper(): print(time. [Read More]