def register(name, password, account=15000): user_dic = { 'name': name, 'password': password, 'locked': False, 'account': account, 'creidt': account, 'bankflow': [] } db_handler.update(user_dic) common.get_logger(print('用户%s注册成功' % name))
def transfer_interface(from_name, to_user, money): # 拿到转账给谁的用户字典 to_user_dict = db_handles.select(to_user) # 判断一下用户是否存在 if not to_user_dict: return False, '改用户不存在' # 在的话: 在获取用户的字典 from_user_dict = db_handles.select(from_name) # 判断金额是否大于转账的金额 # 小于返回 记得是一一对应的哈 返回与接收是一致的 if not from_user_dict["balance"] > money: return False, '穷鬼自己养不活还给别人转钱' # 大于大于直接扣钱 from_user_dict['balance'] -= money # 收到放加钱 to_user_dict['balance'] += money # 生成一个对象 logger_obj = common.get_logger('用户转账功能') # 保存好 db_handles.save(to_user_dict) db_handles.save(from_user_dict) # 记录日志 logger_obj.debug('用户:%s 转账给%s 金额:%d¥' % (from_user_dict['name'], to_user_dict['name'], money)) return True, '用户:%s 转账给%s 金额:%d¥' % (from_user_dict['name'], to_user_dict['name'], money)
def withdraw_interface(name, money): # 获取当前用户 user_dict = db_handles.select(name) # 判断余额 # 手续费: # 取现总额度:money2 = money+money*0.05 money1 = money * 0.05 money2 = money + money * 0.05 if not user_dict['balance'] > money2: return False, '小伙子没有钱了' user_dict['balance'] -= money logger_obj = common.get_logger('用户取现功能') # 导入函数就阶段就定义号了 所以也可以放到导入函数下面 info = '用户%s 取现金额:%d 手续费:%d' % (user_dict['name'], money, money1) logger_obj.warning(info) # user_dict["flow"].append(info) # 拿到用户流水的值 user_dict["flow"].append(info) # 保存用户操作记录 db_handles.save(user_dict) # print(user_dict, '--------------') # # print(info, type(info)) # None <class 'NoneType'> type:None """ {'name': 'aaa', 'pwd': '85f4573d52ae1d38ac175e4afcd89b74', 'balance': 12590, 'flow': [None, None, None, None, None, None, None], 'shopping_car': {}} -------------- None <class 'NoneType'> None """ return True, info
def admin_change_user_interface(user, balance): user_dic = db_handler.select(user) user_dic["balance"] = int(balance) db_handler.save(user_dic) logger = get_logger("admin_user") logger.info("修改用户:%s 额度为 %s " % (user, balance)) return True, "修改用户:%s 额度为 %s " % (user, balance)
def admin_close_user_interface(user): dbfile = os.path.join(db_path, "%s.json" % user) if not os.path.exists(dbfile): return False, "需要冻结的用户:%s 不存在" % user os.rename(dbfile, dbfile+".close") logger = get_logger("admin_user") logger.info("冻结用户:%s 成功" % user) return True, "冻结用户:%s 成功" % user
def init(log_name): ''' Function for initializing main logger, databse connector objects, darknet network, and temp file Input: log name Return: return True if all global variables are generated successfully, otherwise return False ''' global main_logger, oracle_db, mysql_db main_logger = False oracle_db = False mysql_db = False main_logger = common.get_logger(log_name) # initialize darknet yolo_handler.init() # initialize openpose openpose_handler.init() # connect to Oracle database main_logger.info('Connecting to Oracle database') oracle_conn_rt = common.connect_db( 'oracle') # conn_rt = [ True/False, log_info, database object ] if oracle_conn_rt[0] == False: main_logger.error(oracle_conn_rt[1]) else: main_logger.info(oracle_conn_rt[1]) oracle_db = oracle_conn_rt[2] # connect to Mysql database main_logger.info('Connecting to Mysql database') mysql_conn_rt = common.connect_db('mysql') if mysql_conn_rt[0] == False: main_logger.error(mysql_conn_rt[1]) else: main_logger.info(mysql_conn_rt[1]) mysql_db = mysql_conn_rt[2] # create a temp file to record image filenames # which needs to be saved # save_tmp=open('tmp/pict.sav', 'a+') # common.file_check(save_tmp, main_logger, 'Temp file tmp/pict.sav create failed!', 10) # main_logger.info('temp file {0} generate successfully'.format(save_tmp)) if main_logger != False and oracle_db != False and mysql_db != False: main_logger.info('Initializting process successfully') return True else: main_logger.error('Initializting process Failed!') if oracle_db != False: common.close_db(oracle_db, 'oracle') if mysql_db != False: common.close_db(mysql_db, 'mysql') return False
def admin_login_interface(user, pwd): admin_dic = db_handler.select(user) logger = get_logger("admin_user") if pwd == admin_dic["password"]: logger.info("登录成功") return True, "登录成功" logger.info("登录失败") return False, "登录失败"
def login_interface(name, pwd): user_dict = db_handles.select(name) if not user_dict: return False, '用户名不存在' # 调用公共功能层的登陆密码加密和文件中的密码进行对比判断是否一致 new_md5 = common.get_md5(pwd) # common 加密返回值 变量接收一下即可 if new_md5 == user_dict['pwd']: # 输入的加密密码是否和系统存的加密密码一致 logger_obj = common.get_logger('用户登录功能') logger_obj.info('尊敬的%s用户您已成功登录' % name) return True, '%s登录成功' % user_dict['name'] else: return False, '输入系统密码与系统密码不一致'
def see_balane(): """ 查看余额 :return: """ username_path = os.path.join(DB_PATH, dic_login_flag["username"] + ".json") with open(username_path, 'r', encoding='utf-8') as fp: dic_info = json.load(fp) print("*******************") print(f"你的用户余额为:\n{dic_info['money']}") print("*******************") #把查看的日志写进log下的日志文件夹,但是我写进了db里面。 logger = get_logger(dic_login_flag["username"]) logger.info('查看账户余额') # 记录该文件的运行状态
def save_money(): """ 存钱 :return: """ dic_info = os.path.join(DB_PATH, dic_login_flag["username"] + '.json') money = input("请输入你存入的金额:") if money.isdecimal(): with open(dic_info, 'r', encoding='utf-8') as fp: dic = json.load(fp) dic["money"] = int(money) + int(dic["money"]) with open(dic_info, 'w', encoding='utf-8') as f: json.dump(dic, f) print("存钱成功!") logger = get_logger(dic_login_flag["username"]) logger.info(f'存入账户{money}¥') # 记录该文件的运行状态
def admin_login(): while True: user = "******" pwd = input("请输入管理员密码:").strip() flag, msg = user_interface.admin_login_interface(user, pwd) if flag: print(msg) res = admin_func() if not res: logger = common.get_logger("admin_user") logger.info("管理员已退出") print("管理员已退出") break else: print(msg)
def transfer_accounts(): """ 转账 :return: """ passive_user = input("请输入被转账方的用户名:") print("*******************") list_json = os.listdir(DB_PATH) own_json = os.path.join(DB_PATH, dic_login_flag["username"] + '.json') #被转账的用户是否存在,同时被转账的用户不应该自己 if passive_user + '.json' in list_json and passive_user != dic_login_flag[ "username"]: passive_path = os.path.join(DB_PATH, passive_user + '.json') with open(own_json, 'r', encoding='utf-8') as fp: dic = json.load(fp) while True: turn_money = input("输入转账金额:") if turn_money.isdecimal(): #自己的前是否够 if int(turn_money) <= int(dic["money"]): count_money = 0 #把钱加到对方账户同时减少自己账户的钱 with open(passive_path, 'r', encoding='utf-8') as f: passive_dic = json.load(f) count_money = int( passive_dic["money"]) + int(turn_money) passive_dic["money"] = count_money with open(passive_path, 'w', encoding='utf-8') as f1: json.dump(passive_dic, f1) with open(own_json, 'w', encoding='utf-8') as f2: dic["money"] = int(dic["money"]) - int(turn_money) # print (dic) json.dump(dic, f2) print("转账成功。") #记录日志 logger = get_logger(dic_login_flag["username"]) logger.info( f'向{passive_user}成功转入{turn_money}¥') # 记录该文件的运行状态 break else: print("金额不足。") else: print("输入的字符未识别。") else: print("被转账的用户不存在!")
def repay_interface(name, money): # 获取用户 user_dict = db_handles.select(name) # 直接操作 user_dict['balance'] -= money # 保存一下 logger_obj = common.get_logger('用户还款功能') # 拼写 用户流水、日志记录内容 # 将拼写的用户信息添加到flow 中 user_dict['flow'].append('返款%s成功' % money) # print(user_dict) # 把保存用户信息 db_handles.save(user_dict) # 生成日志记录 信息 logger_obj.error('返款%s成功' % money) return True,'返款%s成功' % money
def register_interface(name, pwd, balance=15000): user_dict = db_handles.select(name) if user_dict: return False, '用户已存在' # 对用户新注册的密码进行加密 存入文件 new_md5 = common.get_md5(pwd) user_dict = { "name": name, "pwd": new_md5, "balance": balance, "flow": [], "shopping_car": {} } db_handles.save(user_dict) logger_obj = common.get_logger('用户注册功能') logger_obj.debug('尊敬的用户[%s] 您已成功注册我们ATM网站' % user_dict['name']) return True, '尊敬的用户[%s] 您已成功注册我们ATM网站' % user_dict['name']
def init(log_name): ''' Function for initializing main logger and databse connector objects Input: log name Return: return True if all global variables are generated successfully, otherwise return False ''' print('get_video initializing') global main_logger, oracle_db, mysql_db main_logger = False oracle_db = False mysql_db = False main_logger = common.get_logger(log_name) main_logger.info('Connecting to Oracle database') oracle_conn_rt = common.connect_db( 'oracle') # conn_rt = [ True/False, log_info, database object ] if oracle_conn_rt[0] == False: main_logger.error(oracle_conn_rt[1]) else: main_logger.info(oracle_conn_rt[1]) oracle_db = oracle_conn_rt[2] main_logger.info('Connecting to Mysql database') mysql_conn_rt = common.connect_db('mysql') if mysql_conn_rt[0] == False: main_logger.error(mysql_conn_rt[1]) else: main_logger.info(mysql_conn_rt[1]) mysql_db = mysql_conn_rt[2] if main_logger != False and oracle_db != False and mysql_db != False: main_logger.info('Initializting process successfully') return True else: main_logger.error('Initializting process Failed!') if oracle_db != False: common.close_db(oracle_db, 'oracle') if mysql_db != False: common.close_db(mysql_db, 'mysql') return False
# 银行接口 from db import db_handle from lib import common log = common.get_logger('bank') # 提现接口 def withdrow_interface(user_name, money): user_dic = db_handle.select(user_name) balance = float(user_dic['balance']) money_and_fee = float(money) * 1.05 # 检测余额是否充足 if money_and_fee <= balance: # 更新余额 balance -= money_and_fee user_dic['balance'] = balance # 记录流水 flow = '用户 [{name}] 提现 [{money}] 手续费 [{fee}] 余额 [{balance}]]'.\ format(name=user_name, money = money, fee = money_and_fee - float(money), balance = balance) user_dic['flow'].append(flow) log.info(flow) db_handle.save(user_dic) return True, flow else: return False, '用户余额不足, 请重新输入!' # 转账接口 def tranfer_interface(user_name, dst_user, money):
# -*- encoding: utf-8 -*- import datetime from lib import common from db import db_handler logger = common.get_logger('user') def get_user_info_api(name): data = db_handler.file_handler_read(name) if data: logger.info('获取用户%s信息成功!' % name) else: logger.info('用户%s信息不存在!' % name) return data def register_user_api(name, password, credit_limit=15000): user_info = { 'name': name, 'password': password, 'role': 'user', 'balance': 0, 'credit_limit': credit_limit, 'credit_balance': credit_limit, 'detailed_list': [], 'shopping_cart': {}, 'bill': 0 } if db_handler.file_handler_write(user_info):
# -*- encoding: utf-8 -*- from lib import common logger = common.get_logger('common') def login(): print('\033[32m登陆\033[0m')
from db import db_handler from lib.common import get_logger logger1 = get_logger('bank') def tixian_interface(name, money): user_dic = db_handler.select(name) money1 = money * 1.05 money2 = money * 0.05 if user_dic['balance'] < money1: return False, '余额不足,提现失败' else: user_dic['balance'] -= money1 info = '用户%s提现%s成功,手续费%s元' % (name, money, money2) logger1.info(info) user_dic['flow'].append(info) db_handler.save(user_dic) return True, info def huankuan_interface(name, money): user_dic = db_handler.select(name) user_dic['balance'] = +money info = '用户%s还款%s成功' % (name, money) logger1.info(info) user_dic['flow'].append(info) db_handler.save(user_dic) return info def zhuanzhang_interface(name, to_user, to_money):
# -*- encoding: utf-8 -*- from lib import common from db import modules logger = common.get_logger('teacher_api') def login(name, password): teacher = modules.Teacher.get_obj_by_name(name) if not teacher: return False, '老师%s不存在!' % name if password == teacher.password: logger.info('老师%s登陆成功!' % name) return True, '老师%s登陆成功!' % name else: logger.warning('老师%s登陆失败!' % name) return False, '老师%s登陆失败!' % name def get_teacher_info(name): logger.info('老师%s获取个人信息!' % name) return modules.Teacher.get_obj_by_name(name) def get_course_info(name, course): logger.info('老师%s获取教授课程%s信息!' % (name, course)) return modules.Course.get_obj_by_name(course)
from conf import settings from lib import common import time logger = common.get_logger(__name__) current_user = { 'user': None, 'login_time': None, 'timeout': int(settings.LOGIN_TIMEOUT) } def auth(func): def wrapper(*args, **kwargs): if current_user['user']: interval = time.time() - current_user['login_time'] if interval < current_user['timeout']: return func(*args, **kwargs) count = 0 while (True): if count == 3: logger.info('禁止登陆!') break name = input('name>>: ') password = input('password>>: ') db = common.conn_db() if db.get(name): if password == db.get(name).get('password'): logger.info('登录成功') current_user['user'] = name
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/8/15 14:59 # @Author : zx-long # @File : teacher.py from db import db_handler from conf import settings from lib import common from core import src teac_logger = common.get_logger('teac') class Teacher: def __init__(self, name, id, psd, school_id): self.name = name self.id = id self.psd = psd self.school_id = school_id self.courses = [] def show_courses(self): print('========查看您的授课========') for k, item in enumerate(self.courses, 1): print(k, item) # 教师选课 def add_course(self, option): sch = db_handler.select(settings.sch_DB, self.school_id) for item in self.courses: if option == item:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/8/15 14:59 # @Author : zx-long # @File : manager.py from tool import school, teacher, courses from db import db_handler from conf import settings from lib import common mng_logger = common.get_logger('mng') class Manager: def __init__(self, name, id, psd): self.name = name self.id = id self.psd = psd def create_school(self, name, school_id): sch = db_handler.select(settings.sch_DB, school_id) if sch: return False, 'id已被其他学校占用' else: sch_obj = school.School(name, school_id) db_handler.save(settings.sch_DB, sch_obj) mng_logger.info('管理员-id:%s 创建了学校,学校id:%s,名称:%s' % (self.id, school_id, name)) return True, '学校创建成功' def create_teacher(
# -*- encoding: utf-8 -*- from lib import common from db import modules logger = common.get_logger('admin_api') def login(name, password): admin = modules.Admin.get_obj_by_name(name) if not admin: return False, '管理员%s不存在!' % name if password == admin.password: return True, '管理员%s登陆成功!' % name else: return False, '管理员%s密码错误!' % name def register(name, password): admin = modules.Admin.get_obj_by_name(name) if admin: return False, '管理员%s已存在!' % name modules.Admin(name, password) if modules.Admin.get_obj_by_name(name): return True, '管理员%s注册成功!' % name else: return False, '管理员%s注册失败!' % name def create_school(admin_name, name, address): school = modules.School.get_obj_by_name(name)
from db import models from lib import common admin_log = common.get_logger('admin') def register_interface(name, pwd): admin_obj = models.Admin.get_obj_from_name(name) if admin_obj: return False, '用户已存在' models.Admin(name, pwd) admin_log.info('%s 用户注册成功' % name) return True, '%s 用户注册成功' % name def create_school_interface(admin_name, name, address): school_obj = models.School.get_obj_from_name(name) if school_obj: return False, '学校已存在' admin_obj = models.Admin.get_obj_from_name(admin_name) admin_obj.create_school(name, address) admin_log.info('%s 创建了学校 %s' % (admin_name, name)) return True, '%s 学校创建成功' % name def create_teacher_interface(admin_name, name): teacher_obj = models.Teacher.get_obj_from_name(name) if teacher_obj: return False, '该老师已存在' admin_obj = models.Admin.get_obj_from_name(admin_name)
#coding:utf-8 import sys, os os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' #os.environ.pop('CUDA_VISIBLE_DEVICES') #os.environ["CUDA_VISIBLE_DEVICES"]="1" import lib.common as common import time import pandas as pd import lib.lkj_lib as LKJLIB import datetime os_sep = os.path.sep gpu_idx = 1 yolo_logger = common.get_logger('yolo') def init(): ''' Initializing function Input: None return: None ''' yolo_logger.info('Initializing') # get darkent parameters global dn, net, meta dn_path, mdl_cfg, weights, meta_f = common.get_yolo_config() cellphone_weights = './yolov3_32025000.weights' cellphone_cfg = './lyc_cellphone.cfg'
def admin_add_user_interface(user, pwd): flag, msg = register_interface(user, pwd) logger = get_logger("admin_user") logger.info("添加用户:%s 成功" % user) return flag, msg
from lib import common #要拿到logger对象 print(__name__) logger1 = common.get_logger( __name__) # core.src在日志配置字典中没有定义,最终匹配到默认的''logger对象。 logger2 = common.get_logger('collect') # collect是在日志配置字典中定义的 current_user = {'user': None} def auth(func): def wrapper(*args, **kwargs): if current_user['user']: return func(*args, **kwargs) name = input('username>>: ').strip() password = input('password>>: ').strip() db_obj = common.conn_db() #连接数据库,拿到数据库对象 if db_obj.get(name) and password == db_obj.get(name).get('password'): logger1.info('登录成功') logger2.info('登录成功') current_user['user'] = name return func(*args, **kwargs) else: logger1.error('登录失败') logger2.error('登录失败') return wrapper
import logging import os import torch import torchvision from torch.utils.data import SubsetRandomSampler, Sampler, Subset from torchvision.transforms import transforms from sklearn.model_selection import StratifiedShuffleSplit from lib.augmentations import * from lib.common import get_logger from lib.samplers import DistributedStratifiedSampler from lib.samplers import StratifiedSampler logger = get_logger('Fast AutoAugment') logger.setLevel(logging.INFO) def get_dataloaders(dataset, batch, dataroot, split=0.0, split_idx=0, horovod=False, aug="default", cutout=0, K=10): if 'cifar' in dataset: transform_train = transforms.Compose([ transforms.RandomCrop(32, padding=4),
#coding:utf-8 from db import db_handler from lib import common user_loger = common.get_logger('User') def get_userinfo_interface(name): return db_handler.select(name) def user_pwd_interface(name,pwd,balance=15000): user_dic = { 'name':name, 'password':pwd, 'locked':False, 'account':balance, 'limit':balance, 'flow_log':[] } user_loger.info('%s用户注册成功!' % name) db_handler.update(user_dic) def user_locked_interface(name): user_dic = db_handler.select(name) user_dic['locked'] = True db_handler.update(user_dic)