def read_file(self): """ 通过路径读取文件并且转换为list[Access] 如果路径不是文件,抛出异常 """ try: context = self.__file_check__("rb").read() decrypted = AES_Encrypt().decrypt_by_aes(context, self.password) access_list = [] for access in json.loads(decrypted): access_list.append( Access(access["tag"], access["username"], access["password"], access["url"])) except (IOError, ValueError, KeyError): raise return access_list
def __show__(self, source): result = "" if source: access_list = [Access("标签", "用户名", "密码", "网址")] if type(source) is list: access_list += source elif type(source) is Access: access_list.append(source) # 找到每个列最长的属性,用来确定列宽 columns = [0, 0, 0, 0] for access in access_list: loop = 0 # 每个半角空格作为一个长度单位,汉字的长度单位是两个,所以长度为字符串长度+汉字的个数 for v in access.__dict__.values(): length = len(v) + len(re.findall(u"[\u4e00-\u9fa5]", v)) if length > columns[loop]: columns[loop] = length loop += 1 columns = [i + 4 for i in columns] # 生成打印字符串 result = "" for access in access_list: for i in columns: result += "+" + "—" * i result += "+\n" loop = 0 for v in access.__dict__.values(): length = len(v) + len(re.findall(u"[\u4e00-\u9fa5]", v)) temp = (columns[loop] - length) / 2 left = int(temp) right = math.ceil(temp) result += "|" + " " * left + str(v) + " " * right loop += 1 result += "|\n" for i in columns: result += "+" + "—" * i result += "+\n" print(result)
self.__file_check__("wb").write(encrypted) except IOError as e: raise e def __file_check__(self, file_mode: str): try: file_obj = open(self.file_path, file_mode) except IOError as e: raise e return file_obj if __name__ == "__main__": converter = File_Converter(r"PasswordManger\test", "FuckYou") # 生成数据并写入文件 access_list = [ Access("tag{0}".format(i), "username{0}".format(i), "password{0}".format(i), "url{0}".format(i)) for i in range(1, 11) ] converter.write_file(access_list) # 读取数据并打印 access_list = converter.read_file() for access in access_list: print("tag:{tag},username:{username},password:{password},url:{url}". format(tag=access.tag, username=access.username, password=access.password, url=access.url))
def server_side_analytics(f, host: str, tracking_id: str): log = Access.from_file(f, host) for entry in log: hit.hit_from_access(entry)
import multiprocessing import threading import tensorflow as tf from Access import Access from Framework import ExplorerFramework NUMS_CPU = multiprocessing.cpu_count() state_size = 4 action_size = 2 tf.reset_default_graph() sess = tf.Session() with tf.device("/cpu:0"): A = Access(state_size, action_size) F_list = [] for i in range(NUMS_CPU): F_list.append(ExplorerFramework(A, 'W%i' % i, state_size, action_size)) COORD = tf.train.Coordinator() sess.run(tf.global_variables_initializer()) threads_list = [] for ac in F_list: job = lambda: ac.run(sess) t = threading.Thread(target=job) t.start() threads_list.append(t) COORD.join(threads_list)
""" test.py is intended to run tests of mostly Access.py to try to make sure it is working Currently it is being used for manual tests as needed """ from Access import Access from GoogleAnalyticsMeasurementProtocol import hit f = open("access.log") log = Access.from_file(f, "test.com") def test(access): return hit.hit_from_access(access, "UA-67487168-1") futures = [] for access in log[:100]: futures.append(test(access)) for future in futures: response = future.result() print(response.text)