zoukankan      html  css  js  c++  java
  • 函数一个用法


    '''
    handle all the database interactions
    '''
    import json, time, os
    from conf import settings


    def file_db_handle(conn_params):
    '''
    parse the db file path
    :param conn_params: the db connection params set in settings
    :return:
    '''
    print('file db:', conn_params)
    # db_path ='%s/%s' %(conn_params['path'],conn_params['name'])
    return file_execute #3返回给外部程序,一个函数名。外部程序就可以进行调用,这个函数


    def db_handler(): #1外部程序最先调用这个
    '''
    connect to db
    :param conn_parms: the db connection params set in settings
    :return:a
    '''
    conn_params = settings.DATABASE
    if conn_params['engine'] == 'file_storage':
    return file_db_handle(conn_params) #2返回值是另一个函数的执行结果
    elif conn_params['engine'] == 'mysql':
    pass # todo


    def file_execute(sql, **kwargs):
    """
    对sql语句进行解析并执行
    :param sql: eg. select * from accounts where account=1344
    :param kwargs:
    :return:
    """
    conn_params = settings.DATABASE
    db_path = '%s/%s' % (conn_params['path'], conn_params['name'])

    print(sql, db_path)
    sql_list = sql.split("where")
    print(sql_list)
    if sql_list[0].startswith("select") and len(sql_list) > 1: # has where clause
    column, val = sql_list[1].strip().split("=")

    if column == 'account':
    account_file = "%s/%s.json" % (db_path, val)
    print(account_file)
    if os.path.isfile(account_file):
    with open(account_file, 'r') as f:
    account_data = json.load(f)
    return account_data
    else:
    exit("33[31;1mAccount [%s] does not exist!33[0m" % val)

    elif sql_list[0].startswith("update") and len(sql_list) > 1: # has where clause
    column, val = sql_list[1].strip().split("=")
    if column == 'account':
    account_file = "%s/%s.json" % (db_path, val)
    # print(account_file)
    if os.path.isfile(account_file):
    account_data = kwargs.get("account_data")
    with open(account_file, 'w') as f:
    acc_data = json.dump(account_data, f)
    return True
  • 相关阅读:
    ldap集成jenkins
    自动发现实现url+响应时间监控
    5秒跳转
    String的使用
    数字货币转换为中文货币
    字符串的使用(string,StringBuffer,StringBuilder)
    修饰符
    类的继承,抽象类,接口,方法重写和重载
    类的使用
    java中的输入流(Scanner),数据类型,运算符,switch,数组的用法
  • 原文地址:https://www.cnblogs.com/chengege/p/10259003.html
Copyright © 2011-2022 走看看