Programming Error Type 1
1.1 syntax errors, such as quotation marks do not come in pairs, fewer commas, etc., the system will throw Syntaxerror
1.2 semantic errors, such as division by 0
1.3 logical error
2 Exception Handling
2.1 Example
= F Open ( 'test.txt') the try: reached, f.read () the except : Print ( 'file operation exception') the finally : f.close ()
2.2 finally is typically used where resources need to be released, such as the example file operations, there is the operation of the database
2.3 raise for manually throw an exception
2.4 except use to specify exception handling, such as
except Exception Type AS 1 Example 1: Processing 1 except 2 AS 2 Example type of exception handling 2
2.5 else for all normal handling of
3 Test
3.1 Test Procedure
3.1.1 into the test unit
import unittest
3.1.2 To test introducing or Class
from module import method
3.1.3 create a test class
class TestMethod(unittest.testcase)
3.1.4 establish testing methods, convention, this method always start with test, introduced in the method unittest module comes methods
DEF test_method_uu (): Variable = Method () . Self built-in method (variable, the actual value)
3.1.5 Run the test program
if __name__ == ‘__main__’: unittest.main()
3.2 Both functions
3.2.1 setUp () function, the initialization code for preparing
3.2.2 tearDown () function, for performing a cleaning work, such as involving a file, database operation class or function
4 common assertion methods
4.1 assertEqual()
4.2 assertIn()
4.3 assertAlmostEqual()
4.4 assertIs()
4.5 assertGreater()
4.6 assertIsInstance()
4.7 assertTrue()
4.8 assertIsNone()
5 formatted values
Two expressions value formatted
5.1 '{: .2f}'. Format (variable)
5.2 f '{variables: .2f}'
6 random process
6.1 random.choice (sequence) of a number taken from a random sequence
6.2 random.sample (sequence number) a number taken from a random sequence number
6.3 random.shuffle (sequence) disrupted sequence
(Value 1, value 2) generating a random integer within the range of 6.4 random.randint
6.5 random.random () generates a random float
6.6 random.getrandbits (value) to generate random numbers to develop bit digits
7 Date and Time
7.1 import module import datetime
7.2 three main ways: date, time, datetime
7.3 format conversion
7.3.1 String Variable Time
s=’2018-7-6’
t=datetime.datetime.strptime(s,’Y%-m%-d%’)
7.3.2 Time String Variable
now = datetime.datetime.now()
txt = now.strftime(‘Y%/m%/d%’)
7.4 time difference timedelta
time1=datetime.datetime(2008,8,8,20,8) time2=datetime.datetime.now() diff=time1-time2
diff is the timedelta type, comprising two days and attributes seconds