File operation
Generally, we need to close the file in time after the file operation is completed, but using the with context method, the program will close automatically internally. We only need to focus on the file operation
There are generally three methods for file operation w (read) r (write) a (append the original content of the file)
Single line write: write
with open("log.txt","w") as a :
a.write("hello world")
Output results:
log.txt
hello world
Multiline writing: using wirelines
with open("log.txt","w") as d: d.writelines(''' jahcjlahl anlal aklna akln ''')
File reading, file reading, to print out
# with open("log.txt","r") as r: # Read all the contents of the file can only be read once if it is read for the second time or empty # print(r.read()) # Read the first line of the file readline() # print(r.readline()) # Read file by line readlines() # print(r.readlines())
Document contents are appended with a
with open("log.txt","w") as a : a.write("hello world")
sys actual combat
The use of sys is often accompanied by paths. You should import sys and os libraries before performing sys operations
When writing code at work, when we import it correctly, but prompt that we can't find the module. We can use sys to solve it
The solution is:
1. Find the path to sys and the path to the prompt module
2. Splice the paths of the modules together
3. Add to sys.path
4. Import all contents of the module
5. Call function
import sys
import os
# Gets the upper level of the current path base_dir=os.path .dirname(os.path.dirname(__file__)) print(base_dir) # take day12 Path and sys Path splicing day12Path=os.path.join(base_dir,"day12") print(day12Path) # Add to sys.path inside sys.path.append(day12Path)
Import all contents of the module
from login import * func()
Output results:
D:\code\practice
D:\code\practice\day13
hello world
hashilib actual combat
It is mainly used for encryption of open api Generally, time stamps are used to make the results different every time
Import before operation
hashilib Library
urllib in parse Library
time library
Encryption idea of open api:
1. Sort the request parameters according to the key
2. Process the request parameters as key = value & key = value
3. Encrypt md5 the request parameters (the encrypted data type must be bytes)
4. Send the encrypted sign to the server as the request header,
The service layer compares. If it is consistent, it can request. If it is inconsistent, it will reject the request
import hashlib from urllib import parse import time def sign(): # Sort request parameters according to key To sort dict1={"name:":"wuya","age":28,"address":"xian","time":time.time()} data=dict(sorted(dict1.items(),key=lambda item:item[0])) data=parse.urlencode(data) m=hashlib.md5() m.update(data.encode("utf-8")) return m.hexdigest() print(sign())
Output results:
2f414f60abd347f48e7dd083a64b6e2c It's different every time
Exception handling: in the process of program execution, there will be exceptions, and design test cases
The normal function points and abnormal function points of the tested function points need to be considered
try:
expect:
else:
finally
Execution sequence:
1. If try is executed normally, the code will execute the logic of else, and then execute finally
2. If try executes abnormally, the code will execute the logic of expect, and then execute finally
3. Exception is generally used, which can capture almost all program error information
4.finally can always be executed to
In general, exception handling needs to be considered when writing programs
def func(a,b): try: print(a/b) except Exception as e: print(e.args) else: print("try I can't be executed until I execute correctly") finally: print("Whether implemented or not try") func(4,2) func(1,0)
Output results:
2.0 try I can't be executed until I execute correctly Whether implemented or not try ('division by zero',) Whether implemented or not try
object-oriented:
object-oriented:
1. The keyword used in the definition of class is class
2. The first letter of the class must be uppercase
3. At present, the classes studied are all new classes
4. The base class (ancestor) of all classes is object, so they need to be inherited
5. Three characteristics of object-oriented:
encapsulation
inherit
polymorphic
6. In OOP programming, if you want to call the methods inside, you first need to instantiate the class, and the instantiated object can call the methods in the class
class Person(object): def __init__(self,name,age,sex): self.name=name self.age=age self.sex=sex def show(self): print("my name is {0},my age is {1},my sex is {2}".format(self.name,self.age,self.sex)) # def show(self): # print("hello class") # A process of instantiating a class whose object is obj obj=Person(name="wuya",age=28,sex="nan") obj.show()