+ Regularization parameter configuration file of the database package + mysql

Configuration file read by the module configpaser

ConfigParser Import 
class readconfig: 
    "" " 
    finish the configuration file read 
    " "" 
    DEF __init __ (Self): 
        self.config = configparser.ConfigParser () 
        self.config.read (contants.global_file) # to load, Ltd. Free Join 
        Switch = Self. config.getboolean ( 'switch', 'ON') 
        IF switch: # switch is ON, the use of online configuration 
            self.config.read (contants.online_file, encoding = 'UTF-. 8') 
        the else: when the switch is off # , using the test configuration 
            self.config.read (contants.test_file, encoding = 'UTF-. 8') 
    DEF GET (Self, sectionTop, Option): 
        return self.config.get (sectionTop, Option) 

config = readconfig ()

 

Regular is by re module  

Dore DEF (Data): 
    "" " 
    1. regular expression to match the specified character data in the test 
    according to the specified character obtained value corresponding to the configuration file 
    3. Then replace 
    " "" 
    pattern = '# (. ? *) # '# regular expression matching among multiple groups or at most a single character 
    the while re.search (pattern, the Data): 
        Search = re.search (pattern, the Data) # from any position start looking, looking first returned Match object, if not find None 
        Group search.group = (. 1) to get the parameters of the # KEY 
        value = config.get_value ( 'Data', Group) # KEY taken according to the value of the configuration file which 
        "" " 
        remember to replace contents continue receiving Data 
        "" " 
        Data = the re.sub (pattern, value, Data, count =. 1) # find Alternatively, count the number of the replacement search 
        Print (Data) 
    return Data 

 
IF the __name__ == '__main__':  
    a = Dore ( '{" MobilePhone, ":"#user_mobile#","pwd":"#user_password#"}')
    print(a)

 

Database by pymsql

DoMysql class: 
    "" " 
    Initialization: 
    1. Obtain the connection information from the profile database 
    2. The establishment of the cursor 
    Method: 
    1. Perform sql statement, one or all of the data acquired 
    2. Turn off the cursor and the database 
    " "" 
    DEF the __init __ (Self) : 
        self.mysql = pymysql.connect (= Host config.get_value ( 'the testdb', 'Host'), 
                                     Port = int (config.get_value ( 'the testdb', 'Port')), 
                                     User config.get_value = ( 'the testdb ',' User '), 
                                     password = config.get_value (' the testdb ',' password '), 
                                     DB = config.get_value (' the testdb ',' DB '),
                                     charset=config.get_value('testdb', 'charset'))

        #self.cursor = self.mysql.cursor(cursor=pymysql.cursors.DictCursor)  # 查询的数据以键值对返回

        self.cursor = self.mysql.cursor()  # 值返回查询的结果

    def fetch_one(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchone()

    def fetch_all(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    def close(self):
        self.cursor.close()
        self.mysql.close()


if __name__ == '__main__':
    result = DoMysql().fetch_all('select id from loan where memberid=241;')
    print(result)

  

 

Guess you like

Origin www.cnblogs.com/XXQQ123/p/11539700.html