def testShortUid(self): key = "susodigital" domain = "alfjaofjafjapdfja.com" encoded = StrUtility.encrypt_XOR(key=key, plaintext=domain) print(encoded) decoded = StrUtility.decrypt_XOR(key, encoded) print(decoded)
def testShortUid(self): key = "susodigital" domain = "alfjaofjafjapdfja.com" encoded = StrUtility.encrypt_XOR(key=key, plaintext=domain) print(encoded) decoded = StrUtility.decrypt_XOR(key, encoded) print(decoded)
def __init__(self, table: str, creation_query: str, insert_query: str = "", offset=0, databaseType: str = SiteSource.Seed, db_addr: str = None, db_filter: DBFilterInterface = None): if databaseType is None: raise ValueError("database type cannot be None") if db_addr is None: self.db_addr = SiteSource.get_default_address(databaseType) else: self.db_addr = db_addr self.tab = table self.encoded_tab = StrUtility.make_valid_table_name(table) self.db_type = databaseType if (self.db_addr is None or len(self.db_addr) == 0) and len(creation_query) > 0: self.db = sqlite3.connect(":memory:") else: self.db = sqlite3.connect(self.db_addr) self.cur = self.db.cursor() self.cur.execute(creation_query) self.db.commit() self.insert_query = insert_query self.offset = offset self.db_filter = db_filter self.output_format_tuple = False self.get_lock = threading.RLock()
def save_sub_category(self, category_table: str, sub_category_list: [SubCategoryStruct]): try: category_table = StrUtility.make_valid_table_name(category_table) self.cur.execute("CREATE TABLE IF NOT EXISTS \'{0:s}\'(SUB_CATEGORY TEXT, COUNT INTEGER, " "PRIMARY KEY(SUB_CATEGORY));".format(category_table,)) self.cur.executemany(u"INSERT OR REPLACE INTO \'{0:s}\' " u"(SUB_CATEGORY, COUNT) VALUES ( ?, ?);".format(category_table), [x.to_tuple() for x in sub_category_list]) self.db.commit() except Exception as ex: print(ex)
def save_to_table(self, category: str, data: []): try: category = StrUtility.make_valid_table_name(category) converted = [self.convert_input(x) for x in data] self.cur.execute(self.creation_query.format(category,)) self.cur.executemany(self.insert_query.format(category,), converted) self.db.commit() except Exception as ex: print("error write to table:", category, " count:", len(data)) print(ex) pass
def get_total(self, category: str, **kvargs): count = 0 try: category = StrUtility.make_valid_table_name(category) parameters_str = self.convert_filter(kvargs) query = "SELECT COUNT(*) FROM \'{0:s}\' " + parameters_str + ";" self.cur.execute(query.format(category, )) count = self.cur.fetchone()[0] except Exception as ex: print(ex) finally: return count
def save_to_table(self, category: str, data: []): try: category = StrUtility.make_valid_table_name(category) converted = [self.convert_input(x) for x in data] self.cur.execute(self.creation_query.format(category, )) self.cur.executemany(self.insert_query.format(category, ), converted) self.db.commit() except Exception as ex: print("error write to table:", category, " count:", len(data)) print(ex) pass
def get_sub_category(self, category_table: str) -> [SubCategoryStruct]: results = [] try: category_table = StrUtility.make_valid_table_name(category_table) self.cur.execute("SELECT * FROM {0:s};".format(category_table,)) temp = self.cur.fetchall() for sub_category, count in temp: results.append(SubCategoryStruct(CategoryManager.decode_sub_category(sub_category, False), count)) except Exception as ex: print(ex) finally: return results
def get_total(self, category: str, **kvargs): count = 0 try: category = StrUtility.make_valid_table_name(category) parameters_str = self.convert_filter(kvargs) query = "SELECT COUNT(*) FROM \'{0:s}\' "+parameters_str+";" self.cur.execute(query.format(category,)) count = self.cur.fetchone()[0] except Exception as ex: print(ex) finally: return count
def get_from_table(self, category: str, index: int, length: int, filter_dict: dict=None, reverse_read=True, random_read=True) -> []: output = [] reverse_read_clause = "DESC " if reverse_read else "" random_read_clause = "RANDOM()" if random_read else "rowid" try: category = StrUtility.make_valid_table_name(category) self.cur.execute(u"SELECT * FROM \'{0:s}\' {1:s} " u"ORDER BY {2:s} {3:s}LIMIT {4:d} OFFSET {5:d};" .format(category, CategorySiteDBInterface.convert_filter(filter_dict), random_read_clause, reverse_read_clause, length, index)) output = [self.convert_output(x) for x in self.cur.fetchall()] except Exception as ex: print(ex) finally: return output
def get_from_table(self, category: str, index: int, length: int, filter_dict: dict = None, reverse_read=True, random_read=True) -> []: output = [] reverse_read_clause = "DESC " if reverse_read else "" random_read_clause = "RANDOM()" if random_read else "rowid" try: category = StrUtility.make_valid_table_name(category) self.cur.execute( u"SELECT * FROM \'{0:s}\' {1:s} " u"ORDER BY {2:s} {3:s}LIMIT {4:d} OFFSET {5:d};".format( category, CategorySiteDBInterface.convert_filter(filter_dict), random_read_clause, reverse_read_clause, length, index)) output = [self.convert_output(x) for x in self.cur.fetchall()] except Exception as ex: print(ex) finally: return output
def __init__(self, table: str, creation_query :str, insert_query: str="", offset=0, databaseType: str=SiteSource.Seed, db_addr: str=None, db_filter: DBFilterInterface=None): if databaseType is None: raise ValueError("database type cannot be None") if db_addr is None: self.db_addr = SiteSource.get_default_address(databaseType) else: self.db_addr = db_addr self.tab = table self.encoded_tab = StrUtility.make_valid_table_name(table) self.db_type = databaseType if (self.db_addr is None or len(self.db_addr) == 0) and len(creation_query) > 0: self.db = sqlite3.connect(":memory:") else: self.db = sqlite3.connect(self.db_addr) self.cur = self.db.cursor() self.cur.execute(creation_query) self.db.commit() self.insert_query = insert_query self.offset = offset self.db_filter = db_filter self.output_format_tuple = False self.get_lock = threading.RLock()
def testEscape(self): target_strs = ["women's toy", "2015/03/04 marketing"] for item in target_strs: print("before:", item, " after:", StrUtility.make_valid_table_name(item))
def testEscape(self): target_strs = ["women's toy", "2015/03/04 marketing"] for item in target_strs: print("before:", item, " after:", StrUtility.make_valid_table_name(item))