Introduction to Common Modules

Keywords: Python JSON encoding network

Introduction to common modules:

​ time,datetime
​ os,sys
​ hashlib,json,pickle,collections

time module:

  • Time module (time related): Encapsulates some methods for getting time in the form of timestamps and strings.

  • Three objects: timestamp, structured time object (9 fields), string [Key]

    • time.time(): Get the timestamp

    • time.gmtime([seconds]): Gets the formatted time object: consists of nine fields

    • time.localtime([seconds]): Gets the formatted time object: consists of nine fields

    • time.mktime(t): Time object - >Time stamp

    • time.strftime(format[,t]): Format the time object as a string

    • time.strptime(str,format): Converts a time string into a time object

      import time
      # Get Timestamp
      # Timestamp: The number of seconds that have elapsed since the first year of time (1970 1:00:00).
          print(time.time())#1558317680.919616
      
      # Gets the formatted time object: made up of nine fields.
           # The default parameter is the timestamp of the current system time.
           print(time.gmtime())# GMT  time.struct_time(tm_year=2019, tm_mon=5, tm_mday=20, tm_hour=2, tm_min=1, tm_sec=20, tm_wday=0, tm_yday=140, tm_isdst=0)
      
           # Gets the corresponding time object after one second of the first year of time
           print(time.gmtime(1))#time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=0)
      
           print(time.localtime())#time.struct_time(tm_year=2019, tm_mon=5, tm_mday=20, tm_hour=10, tm_min=1, tm_sec=20, tm_wday=0, tm_yday=140, tm_isdst=0)
      
      # Time Object--->Timestamp
           t1 = time.localtime()  #Time object
           t2 = time.mktime(t1)   #Get the corresponding timestamp
           print(t2)#1558318103.0
           print(time.time())#1558318103.9826832
      
      
      # Format Time Object to String
           s = time.strftime("%Y %m %d %H:%M:%S")
           print(s,type(s))#2019 05 20 10:05:17 <class 'str'>
      
      # Converting a Time String into a Time Object
           time_obj = time.strptime('2019 05 20','%Y %m %d')
           print(time_obj)#time.struct_time(tm_year=2019, tm_mon=5, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=140, tm_isdst=-1)
      
           time_obj = time.strptime('2019 05 20 12 30 55','%Y %m %d %H %M %S')
           print(time_obj)#time.struct_time(tm_year=2019, tm_mon=5, tm_mday=20, tm_hour=12, tm_min=30, tm_sec=55, tm_wday=0, tm_yday=140, tm_isdst=-1)
      
      #Pause the current program, sleep XXX seconds time.sleep(xxx)
           for i in range(5):
               print(time.strftime('%Y %m %d %H:%M:%S'))
               time.sleep(1)

datetime module:

  • datetime module: Date time module, which encapsulates some classes related to date and time.

    • date: requires three parameters: year, month and day

    • time: time, minutes, seconds as required

    • datetime: requires six parameters: year, month, day, hour, minute, second.

    • timedelta: It takes a time period. It can be days, seconds, microseconds.

      • Timdelta can perform mathematical operations with three classes: datetime.time, datetime.datetime, datetime.timedelta
    • Getting these types of objects is mainly used for mathematical operations with time periods.

      import datetime
      
      #date class
           d = datetime.date(2019,5,20)
          #Get the properties of the date object
           print(d)#2019-05-20
           print(d.year)#2019
           print(d.month)#5
           print(d.day)#20
      
      #time class:
           t = datetime.time(10,11,55)
           print(t)#10:11:55
          #Get the properties of time
           print(t.hour)#10
           print(t.minute)#11
           print(t.second)#55
      
      #datetime class:
           dt = datetime.datetime(2019,5,20,10,11,46)
           print(dt)#2019-05-20 10:11:46
      
      #timedelta class: the amount of change in time
           td = datetime.timedelta(days=1)
           print(td,type(td))#1 day, 0:00:00 <class 'datetime.timedelta'>
      
      ##Participate in mathematical operations:
      # To create a time object, you can only perform mathematical operations with the following three categories: date,datetime,timedelta
           //Test: (date,datetime,timedelta are operations on timedelta respectively, datetime and date cannot be operations)
           td = datetime.timedelta(days=1)
           d = datetime.date(2010,10,10)
           res = d - td
           print(res)#2010-10-09
      
      
      #Result type of timedelta and time period operation: consistent with another operand
           td2 = datetime.date(2010,10,10)
           td = datetime.timedelta(days=1)
           res = d + td
           print(res,type(res))#2010-10-11 <class 'datetime.date'>
      
           d = datetime.datetime(2010,10,10,10,10,10)
           td = datetime.timedelta(days=1)
           res = d + td
           print(res,type(res))#2010-10-11 10:10:10 <class 'datetime.datetime'>
      
           d = datetime.timedelta(seconds=20)
           td = datetime.timedelta(days=1)
           res = d + td
           print(type(res),res)#<class 'datetime.timedelta'> 1 day, 0:00:20
      
      
      # The calculation of the amount of time change produces rounding.
           t = datetime.datetime(2010,12,31,23,59,58)
           td = datetime.timedelta(seconds=3)
           res = t + td
           print(res)#2011-01-01 00:00:01
      
           t = datetime.datetime(2010,10,10,10,10,00)
           td = datetime.timedelta(seconds=3)
           res = t - td
           print(res)#2010-10-10 10:09:57
      
      # Exercise: Calculate the number of days in February of a year.
      # General algorithm: Is it a leap year based on the year. Yes: 29 days, No: 28?
      
      # Use the datetime module. First create March 1 of the specified year. Then move it forward one day.
           year = int(input("Please enter a year:"))
           d = datetime.date(year,3,1) # Create date object for specified year
           td = datetime.timedelta(days=1) # Create a time period of the day
           res = d - td
           print(res.day)#Display 28 or 29 depending on the year entered
      

os module:

  • os module: Operating system related operations are encapsulated in this module, mainly file deletion, directory deletion, rename and so on.

    import os
    #Related to file operations: rename, delete
    
    #Rename:
       os.rename('a.txt','b.txt')
    
    #Delete:
       os.remove('b.txt')
    
    #Delete directory, must be empty
       os.rmdir('aa')
    
    #Recursively delete empty folders
       os.removedirs('aa')
    
    #Use shutil module to delete directories with content (use with caution)
         import shutil
         shutil.rmtree('aa')
    
    #Path-related operations are encapsulated in another sub-module: os.path
    
        #Returns the parent directory part of a path (does not determine if the path exists)
             res = os.path.dirname(r'd:/aaa/bbb/ccc/a.txt')  #Path does not exist, no error will occur
             print(res)#d:/aaa/bbb/ccc
    
        #Returns the last content of the path specified by the path. If it is only a drive letter or a string ending with a path separator, it returns empty; otherwise, it returns the last content of the path.
             res = os.path.basename(r'd:/aaa/bbb/ccc.txt')  #Path does not exist, no error will occur
             print(res)#ccc.txt
             res = os.path.basename(r'd:/aaa/bbb/ccc')
             print(res)#ccc
    
        #Returns a tuple, the second element represents the last part of the content, and the first element represents the remaining content.
          //If it is only a drive letter or a string ending with a path separator, the second element is empty.Otherwise, the second element is the content of the last part.If the path does not contain a path separator, the first element is empty.
             res = os.path.split(r'd:/aa/bb/cc/a.txt')  #Path does not exist, no error will occur
             print(res)#('d:/aa/bb/cc', 'a.txt')
    
        #Stitching Path
             path = os.path.join('aaa','bbb','ccc','a.txt')
             print(path)#aaa\bbb\ccc\a.txt
             path = os.path.join('d:\\''aaa','bbb','ccc','a.txt')
             print(path)#d:\aaa\bbb\ccc\a.txt
    
    
        #Return the absolute path of a path
             //If the parameter path is relative, the combination string of the current path and the parameter path is returned as a result.
             //If the parameter path is already an absolute path, the parameter is returned directly.
             //If the parameter path starts with / the current drive character and the parameter path are joined to form a string to return.
    
        //Note: This method simply returns a stitched string without checking whether the file represented by the string exists.
    
         #If the path is / begins, the default is under the current drive letter
             res = os.path.abspath(r'/a/b/c')
             print(res)#D:\a\b\c
         #Default current path if it does not start/start
             res = os.path.abspath(r'a/b/c')
             print(res)#D:\python22\day16\a\b\c
    
             res = os.path.abspath('aa')
             print(res)#D:\python22\day16\aa
    
    
         #Judgement function:
         #Determine if it is an absolute path?
             print(os.path.isabs('d:/a.txt'))#True path does not exist, no error will occur
             print(os.path.isabs('a.txt'))#False
    
         #Determine if it is a directory?
             print(os.path.isdir('d:/aaa.txt'))#The False aaa.txt folder does not exist or aaa.txt is a file
             print(os.path.isdir('d:/aaa.txt'))#The True aaa.txt folder exists
    
         #Determine if the path really exists.
             print(os.path.exists('d:/a.txt'))#False a.txt does not exist
             print(os.path.exists('d:/s22/aaa.txt'))#The presence of True aaa.txt
             print(os.path.exists('d:/s22'))#True
    
         #Determine if it is a file?
             print(os.path.isfile('d:/aaaa.txt'))#False file does not exist
             print(os.path.isfile('d:/s22'))#False is a directory case
             print(os.path.isfile('d:/s22/aaa.txt'))#True
    

sys module:

  • sys module: operations related to python interpreter

    import sys
    #Gets the parameter after the script that runs as command line: sys.argv[x]
         print('Script name:',sys.argv[0])   #Script name: D:\python22\day16\tt16.py
         print('First parameter:',sys.argv[1])  #First parameter: hello
         print('Second parameter:',sys.argv[2])  #Second parameter: world
         print(type(sys.argv[1]))  #<class 'str'>
         print(type(sys.argv[2]))  #<class 'str'>
    
    # Find the path to the module when the interpreter executes: sys.path
        #sys.path: The system looks for the path to the module.
        print(sys.path)
    
    #sys.modules: Returns the system loaded modules, returned as a dictionary.
      print(sys.modules)
    
    

hashlib module:

  • hashlib module: encapsulates some classes for encryption.

    • The purpose of encryption is to judge and verify, not to decrypt.Encrypt one data and then compare the result of another data encryption with that of the first one.If the results are the same, the original text is the same. If not, the original text is different.
    • Characteristic:
      • The result of encrypting a large piece of data into different blocks, encrypting them separately, and summarizing them is consistent with that of encrypting the whole data directly.
      • One-way encryption, irreversible.
      • A small change in the original data will result in a very different result,'avalanche'effect.
  • Three steps to encrypt a data:

    For example: md5 encryption algorithm:

    1. Get an encrypted object
    2. Encrypt using update of encrypted object, update method can be called many times
    3. Encryption results or digest methods are usually obtained through hex digest().

    import hashlib
    # Get an encrypted object
       m = hashlib.md5()
    # Encrypt using update of encrypted object
         m.update('abc Chinese'.encode('utf-8'))  #?????
         m.update('def'.encode('utf-8'))
    # Get encrypted results through hexdigest
         res = m.hexdigest()
         res1 = m.digest()   ###????
         print(res)#2f1b6e294e72d25ae196fe4ac2d27de6
         print(res1)#b'/\x1bn)Nr\xd2Z\xe1\x96\xfeJ\xc2\xd2}\xe6'
    
    
    
    # Different encryption algorithms (different encrypted objects) are actually different lengths of the encryption results. The longer the length, the more time-consuming.
         print(len(hashlib.md5().hexdigest()))#32
         print(len(hashlib.sha224().hexdigest()))#56
         print(len(hashlib.sha256().hexdigest()))#64
    
    # When creating an encrypted object, you can specify a parameter called salt.
         m = hashlib.md5(b'abc')
         print(m.hexdigest())#900150983cd24fb0d6963f7d28e17f72
    
         m = hashlib.md5()
         m.update(b'abc')
         print(m.hexdigest())#900150983cd24fb0d6963f7d28e17f72
    
         m = hashlib.md5()
         m.update(b'abc')
         m.update(b'def')
         print(m.hexdigest())#e80b5017098950fc58aad83c8c14978e
    
    
    #Register for login:
         def get_md5(username,password):
             m = hashlib.md5(username[::-1].encode('utf-8'))#Salt, invert the username, and convert it to binary bytes
             # m.update(username.encode('utf-8'))
             m.update(password.encode('utf-8'))
             ret = m.hexdigest()
             return ret
    
         def regist(username,password):
             res = get_md5(username,password)#encryption
             with open('login',encoding='utf-8',mode='r') as f:
    
    
    
    
                    ???????
    
    
    
    
    

Serialization: Converts data in memory into bytes or strings for easy storage or network transmission.?????
In-Memory Data--->Byte String/String: Serialization
Byte String/String--> In-Memory Data: Deserialization

json module:

  • json module:

    • JavaScript Object Notation:java scripting object markup language. It has become a simple data exchange format.
    • Serialization: The process of converting other data formats into json strings.
    • Deserialization: The process of converting a json string to another data type.
    • Methods involved:
      • json.dumps(obj): Converts obj to a json string and returns it to memory.
      • json.dump(obj,fp): Converts obj to a json string and saves it in a file pointed to by fp.
      • json.loads(s): Converts an in-memory json string to a corresponding data type object
      • json.load(f): Read the json string from the file and convert it back to the original data type.
  • Be careful:

    • json cannot serialize all data types: for example, set collections.

    • The tuple data type is serialized by json and becomes a list data type.

    • json files are usually written and read at once, but they can be implemented by the file itself: one line stores a serialized json string, and when deserializing, one line deserializes.

      import json
      ##Serialization:
      
       # json.dumps: Converts data to a string for storage or network transmission.
      
           s = json.dumps([1,2,3]) # Converts the specified object to a string in json format
           print(type(s))#<class 'str'>
           print(s)#[1, 2, 3]
      
           s = json.dumps((1,2,3))#  After tuple serialization, it becomes a list
           print(s)#[1, 2, 3]
      
           res = json.dumps(10)
           print(res,type(res))#10 <class 'str'>
      
           res = json.dumps({'name':'alex','age':88})
           print(res,type(res))#{"name": "alex", "age": 88} <class 'str'>
      
           res = json.dumps(set('abc'))
           print(res)#TypeError: Object of type 'set' is not JSON serializable
      
      
       # json.dump
          #Write json results to a file
           with open('a.txt',encoding='utf-8',mode='a') as f:
               json.dump([1,2,3],f)
      
      
      #Deserialization:
          #json.loads
               res = json.dumps([1,2,3])
               lst = json.loads(res)   ## Deserialize
               print(lst,type(lst))#[1, 2, 3] <class 'list'>
      
              # Tuples become lists
               res = json.dumps((1,2,3))
               lst = json.loads(res)           # Deserialize
               print(lst,type(lst))#[1, 2, 3] <class 'list'>
      
          #json.load
          #Deserialize from file:
               with open('a.txt',encoding='utf-8') as f:
                   res = json.load(f)
               print(res,type(res))#[1, 2, 3] <class 'list'>
      
      
      
      # json files are usually written and read at once. Another way is to write and read multiple times.
          # Objects that need to be serialized. Write a json string to a file after multiple serializations using the file write method
               with open('json.txt',encoding='utf-8',mode='a') as f:
                   f.write(json.dumps([1,2,3]) + '\n')   #Only dumps can be used at this time, not dump
                   f.write(json.dumps([4,5,6]) + '\n')
      
          #  Deserialize the json string
               with open('json.txt',encoding='utf-8',mode='r') as f:
                   res1 = json.loads(f.readline().strip())
                   print(res1)
                   res2 = json.loads(f.readline().strip())
                   print(res2)
                   # [1, 2, 3]
                   # [4, 5, 6]
      
          #Use circular improvements:
               with open('json.txt',encoding='utf-8',mode='r') as f:
                   for line in f:
                       res = json.loads(line.strip())  #Only loads can be used at this time, not load
                       print(res)
              #  [1, 2, 3]
              #  [4, 5, 6]
      
      

Pile module:

  • Serialization: Converts all data types in Python to byte strings.

  • Deserialization: Converts a byte string to a data type in python.

  • Pile common scenarios: like json, write once, read once.

  • pickle: python-specific serialization module, consistent with json's method.

  • Json, comparison of pickles:

    • json:

      • 1. Not all data types can be serialized. The result is a string.
      • 2. You cannot serialize the same file more than once.
      • 3.json data can be cross-lingual
    • pickle:

      • 1. All python types can be serialized, resulting in byte strings.
      • 2. The same file can be serialized multiple times
      • 3. Can't cross language.
      import pickle
      
      #All python data types can be serialized
      
      # List serialization
           bys = pickle.dumps([1,2,3])
           print(bys,type(bys))#b'\x80\x03]q\x00(K\x01K\x02K\x03e.' <class 'bytes'>
      
      # Saved data type of tuple
           bys = pickle.dumps((1,2,3))#serialize
           print(bys,type(bys))#b'\x80\x03K\x01K\x02K\x03\x87q\x00.' <class 'bytes'>
           res = pickle.loads(bys)  #Deserialize
           print(res,type(res))#(1, 2, 3) <class 'tuple'>
      
      # Collection serialization, deserialization
           bys = pickle.dumps(set('abc'))
           print(bys,type(bys))#b'\x80\x03cbuiltins\nset\nq\x00]q\x01(X\x01\x00\x00\x00cq\x02X\x01\x00\x00\x00bq\x03X\x01\x00\x00\x00aq\x04e\x85q\x05Rq\x06.' <class 'bytes'>
           res = pickle.loads(bys)
           print(res,type(res))#{'c', 'b', 'a'} <class 'set'>
      
      
      # Write pickle serialization to file:
           with open('cc.txt',mode='wb') as f:  #Convert to Bytes
               pickle.dump([1,2,3],f)
      
      # Deserialize pickle data from a file
           with open('cc.txt',mode='rb') as f:   #byte
               res = pickle.load(f)
               print(res,type(res))#[1, 2, 3] <class 'list'>
      
      
      # Pick Le data multiple times to the same file
           with open('cc.txt',mode='ab') as f:
               pickle.dump([1, 2, 3], f)
               pickle.dump([1, 2, 3], f)
               pickle.dump([1, 2, 3], f)
               pickle.dump([1, 2, 3], f)
      
      # Deserialize pickle data multiple times from a file:
           with open('cc.txt',mode='rb') as f:
               for i in range(4):
                   res = pickle.load(f)
                   print(res)
          # [1, 2, 3]
          # [1, 2, 3]
          # [1, 2, 3]
          # [1, 2, 3]
      
      

collections module:

  • collections module:

    • namedtuple(): named tuple

    • defaultdict(): Default value dictionary.

    • Counter(): Counter#must be capitalized

      import collections
      
      # namedtuple()
           Rectangle = collections.namedtuple('Rectangle_class',['length','width'])
           r = Rectangle(10,6)
      
          # Accessing tuple elements through attributes
            print(r.length)#10
            print(r.width)#6
      
          # Accessing elements indexed
            print(r[0])#10
            print(r[1])#6
      
      #defaultdict:
       # How to create a dictionary:
           d = {'name':'alex','age':84}
           print(d)#{'name': 'alex', 'age': 84}
           d = dict([('name','alex'),('age',84)])
           print(d)#{'name': 'alex', 'age': 84}
           d = { k:v for k,v in [('name','alex'),('age',84)]}
           print(d)#{'name': 'alex', 'age': 84}
      
      # defaultdict()
           d = collections.defaultdict(int,name='alex',age=84)
           print(d['name'])#alex
           print(d['age'])#84
           print(d['addr'])#0     # {'addr':0} will also be added to the dictionary
           print(d)#defaultdict(<class 'int'>, {'name': 'alex', 'age': 84, 'addr': 0})
      
           d = collections.defaultdict(bool,name='alex',age=84)
           print(d['name'])#alex
           print(d['age'])#84
           print(d['addr'])  #False   # {'addr':False} will also be added
           print(d)# defaultdict(<class 'bool'>, {'name': 'alex', 'age': 84, 'addr': False})
      
      
      # Custom functions act as their first parameter, requiring them to have no parameters
           def f():
               return 'hello'
      
           d = collections.defaultdict(f,name='Andy',age=20)
           print(d['addr'])#hello
           print(d)# defaultdict(<function f at 0x000002A685A41E18>, {'name': 'Andy', 'age': 20, 'addr': 'hello'})
      
      
      # Counter: Counter
           c = collections.Counter('asdfgkoiunbbsgfawpag')
           print(c)#Counter({'a': 3, 'g': 3, 's': 2, 'f': 2, 'b': 2, 'd': 1, 'k': 1, 'o': 1, 'i': 1, 'u': 1, 'n': 1, 'w': 1, 'p': 1})
           print(c.most_common(3))#[('a', 3), ('g', 3), ('s', 2)]
      

Posted by Nuggit on Mon, 20 May 2019 09:55:18 -0700