def test_select(self): database = Database() sql = 'SELECT "Hello World!"' rows = database.execute(sql).fetchall() self.assertEqual(len(rows), 1) self.assertEqual(len(rows[0]), 1) self.assertEqual(rows[0][0], 'Hello World!')
def test_users_select(self): try: database = Database() sql = 'SELECT * FROM Users' database.execute(sql).fetchall() except sqlite3.OperationalError: self.fail()
def test_create_user(self): try: database = Database() database.add_user('username', 'password') sql = 'SELECT COUNT(*) FROM Users WHERE Name="username"' row = database.execute(sql).fetchone() self.assertEqual(row[0], 1) except sqlite3.IntegrityError as e: self.assertEqual(str(e), 'UNIQUE constraint failed: Users.Name')
def writeDatabase(self, outPath): db = Database(outPath, clear=True) db.insert_many("location", self.locationRows, True) self.addLocationParts(db) #names = [name.lstrip(".,'-_") for name in self.firstNames] names = [{"name":key, "lower":key.lower(), "length":len(key), "total":self.firstNames[key]} for key in sorted(self.firstNames)] db.insert_many("first_name", names, True) print "Indexing" for column in ("name", "lower", "level"): db.db["location"].create_index([column]) for column in ("name", "lower", "length"): db.db["first_name"].create_index([column])
def __init__(self, dataPath): self.dataPath = dataPath dbPath = os.path.join(dataPath, "yelp", "yelp.sqlite") if not os.path.exists(dbPath): raise Exception("No Yelp database at " + dbPath) self.db = Database(dbPath) self.parts = {"middle":{}, "first":{}, "last":{}, "single":{}} for row in self.db.db.query("select * from part where total > 10;"): pos = row["position"] text = row["token"] if text not in self.parts[pos]: self.parts[pos][text] = [] self.parts[pos][text].append(row["category"]) self.locations = {} for row in self.db.db["location"].all(): name = row["lower"] if row["lower"] not in self.locations: self.locations[name] = [] self.locations[name].append(row) self.names = set([x["lower"] for x in self.db.db.query("select * from first_name where total > 10;")]) for name in ("the",): self.names.remove(name)
def run_game(auto, map_idx, brain_module, infile=''): if auto not in ("simple", "advanced"): print("Invalid auto mode") return (None, None, None, None) map_list = [Map0, Map1, Map2, Map3, Map4, Map5] if map_idx not in range(len(map_list)): print("Invalid map index") return (None, None, None, None) os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (500, 30) walls, checkpoints, finish_line, rocks, car_origin, hud_pos = map_list[ map_idx] car = copy.copy(car_origin) lidar = LiDAR() control = Control() database = Database(lidar, control, car) game = Game(walls, checkpoints, finish_line, rocks, car, database, hud_pos=hud_pos, close_at_end=True) if auto == "simple" and infile is None: print("No input file to run evaluation") return (None, None, None, None) if auto == "advanced" and brain_module is None: print("No brain module to run evaluation") return (None, None, None, None) ### suppress stdout from game brain stdout = sys.stdout if platform.system() == 'Windows': dev_null = open('nul', 'w') else: dev_null = open('/dev/null', 'w') sys.stdout = dev_null brain = brain_module.Brain( database) if auto == "advanced" else TimeEventBrain(database, infile) print("Loaded brain module: {}".format(brain)) brain_thread = threading.Thread(target=brain.run, args=( g_sync_cv, g_brain_cv, )) brain_thread.start() res = game.runAuto(cv=g_sync_cv, bcv=g_brain_cv) brain_thread.join() sys.stdout = stdout return res
def __createDatabaseObject(self): connection = mock() when(sqlite3).connect(ANY).thenReturn(connection) cursor = mock() when(connection).cursor().thenReturn(cursor) when(cursor).execute(ANY) return Database(), cursor
def test_username_taken(self): try: database = Database() database.add_user('username', 'password') database.add_user('username', 'asdf') except sqlite3.IntegrityError: self.fail()
def __init__(self): if len(sys.argv) < 4: logger.error("Usage: {} IN.csv Func.py [0-1 Algorithm] [*.log]" .format(sys.argv[0])) sys.exit(1) self.schedule = [] self.input_file = sys.argv[1] self.load_function_file = sys.argv[2] try: self.load_module = importlib.import_module(self.load_function_file .replace('.py', '')) # for name in dir(self.load_module): # print(getattr(self.load_module, name)) logger.info("Load function loaded") except ImportError: logger.error("Import error") self.algorithm_type = sys.argv[3] self.log_files = sys.argv[4:] self.input_analytics = [] self.schedule = [] self.database = Database("data.p")
def main(auto, map_idx): map_list = [Map0, Map1, Map2, Map3, Map4, Map5] if map_idx not in range(len(map_list)): print("Invalid map index") return os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (500, 30) walls, checkpoints, finish_line, rocks, car, hud_pos = map_list[map_idx] lidar = LiDAR() control = Control() database = Database(lidar, control, car) game = Game(walls, checkpoints, finish_line, rocks, car, database, hud_pos=hud_pos) brain_thread = None if auto is not None: brain = Brain(database) if auto == "advanced" else TimeEventBrain(database) brain_thread = threading.Thread(target=brain.run, args=(g_sync_cv, g_brain_cv,)) brain_thread.start() res_win = None # win condition status res_time = None # total time res_dist = None # total distance res_ckpt = None # checkpoints if auto: res_win, res_time, res_dist, res_ckpt = game.runAuto(cv=g_sync_cv, bcv=g_brain_cv) else: res_win, res_time, res_dist, res_ckpt = game.runManual() if brain_thread is not None: brain_thread.join() if res_win is not None: if res_win: print("Congrats! You won") else: print("Too bad! You lose") print("### REPORT ###") print("Running time: {:.3f}".format(res_time / 1000.0)) print("Running dist: {:.1f}".format(res_dist)) if res_ckpt.items(): print("Checkpoints:") for k, v in res_ckpt.items(): print("- {} : time {:.03f} - dist: {:.1f}".format(k, v["time"] / 1000.0, v["distance"])) else: print("Exit") pygame.quit() return 0
def writeDatabase(self, outPath): db = Database(outPath, clear=True) db.insert_many("location", self.locationRows, True) self.addLocationParts(db) #names = [name.lstrip(".,'-_") for name in self.firstNames] names = [{ "name": key, "lower": key.lower(), "length": len(key), "total": self.firstNames[key] } for key in sorted(self.firstNames)] db.insert_many("first_name", names, True) print "Indexing" for column in ("name", "lower", "level"): db.db["location"].create_index([column]) for column in ("name", "lower", "length"): db.db["first_name"].create_index([column])
def db_parse_to_json(ip): DM = DistanceMatrix() interface = Database() interface.connectToDatabase(ip) r_addresses = interface.getResterauntAddresses() d_addresses = interface.getDonationCenterAddresses() for i in r_addresses: print(type(i[0])) DM.push(i[0], identification=i[0], d_type='o') for i in d_addresses: DM.push(i[0], identification=i[0], d_type='d') DM.to_json_file("./.cache_money/graph.json")
def test_users_if_exists(self): database = Database() sql = 'SELECT COUNT(name) FROM sqlite_master WHERE name="Users"' row = database.execute(sql).fetchone() self.assertEqual(row[0], 1)
def get_remote_projects(): remote = Database.get_all("projects") return [Project.create_from_entry(x) for x in remote]
class Cplayer: base_alphabet = '23456789abcdefghijkmnpqrstuvwxyz' def __init__(self): self.database = Database() def add_one_name_and_value(self, inlist, name, value): one_in = {} one_in['name'] = name one_in['value'] = value inlist.append(one_in) def base62_encode(self, number, alphabet=base_alphabet): # the number must 'int' if not isinstance(number, int): return "error", "number must int." if number == 0: return 'success', '0' arr = [] base = len(alphabet) while number: rem = number % base number = number // base arr.append(alphabet[rem]) arr.reverse() return ''.join(arr) def GetFileList(self, FindPath): FileList = [] FileNames = os.listdir(FindPath) if (len(FileNames) > 0): for fn in FileNames: fullfilename = "/images/player/head/%s" % fn FileList.append(fullfilename) return FileList def main(self): try: heads = self.GetFileList('/root/biubiuServer2/images/player/head') file = open("new_players.txt") cnt = 1060 while 1: name = file.readline() if not name: break name = name.strip('\n') cnt += 1 #print name + "cnt:%s" %cnt # get params player_params_list = [] self.add_one_name_and_value(player_params_list, 'id', cnt) self.add_one_name_and_value(player_params_list, 'name', name) now = time.time() now *= 1000000000 now = int(now) + cnt account = self.base62_encode(now, self.base_alphabet) self.add_one_name_and_value(player_params_list, 'account', account) self.add_one_name_and_value(player_params_list, 'accountID', cnt) self.add_one_name_and_value(player_params_list, 'gold', random.randint(200, 20000)) self.add_one_name_and_value(player_params_list, 'level', 1) self.add_one_name_and_value(player_params_list, 'exp', random.randint(200, 20000)) self.add_one_name_and_value( player_params_list, 'headurl', heads[random.randint(0, len(heads) - 1)]) #print json.dumps(player_params_list) # create account account_params_list = [] self.add_one_name_and_value(account_params_list, 'id', cnt) self.add_one_name_and_value(account_params_list, 'name', account) #self.database.db_do_insert_commond('account',account_params_list) # create player #self.database.db_do_insert_commond('player',player_params_list) #self.database.db_do_insert_commond('player2',player_params_list) # update logo sql = "select id,headurl from player2 where id=%s;" % (cnt) ret, msg, data = self.database.db_do_select_commond(sql) print data for one in data: print one[0], one[1] if isinstance(one[1], basestring): if "http://" not in one[1]: sql2 = "update player2 set headurl='http://115.159.31.18%s' where id = %s" % ( one[1], one[0]) print sql2 #self.database.db_do_update_commond(sql2) break time.sleep(0.1) except Exception, ex: print "ERROR:%s" % (ex)
def test_password_verify(self): database = Database() database.add_user('username', 'password') self.assertTrue(database.password_verify('username', 'password'))
def test_init(self): database = Database() self.assertIsInstance(database, Database)
def test_validate_password(self): database = Database() self.assertTrue(database.validate_password('password')) self.assertFalse(database.validate_password(''))
from src.Database import Database import os.path databaseName = "todolist_test.db" tableName = "todo" database = Database() def setup_function(function): database.createDatabase(databaseName) database.createTable(tableName) def teardown_function(function): database.removeDatabase(databaseName) def test_createDatabase(): database.createDatabase(databaseName) assert os.path.exists(databaseName) == True def test_removeDatabase(): database.createDatabase(databaseName) assert os.path.exists(databaseName) == True database.removeDatabase(databaseName) assert os.path.exists(databaseName) == False def test_insertTodo():
import importlib from src.Reddit import Reddit from src.Database import Database VERSION = "1.4.0" # Version of SOTW-bot (DO NOT CHANGE) # Set rbot default settings className = "RedditBot" fileName = "Settings" # Set rbot settings based on CLI argument if (len(sys.argv) > 1): className = sys.argv[ 1] # Capture 2nd argument as Class to use for rbot if (len(sys.argv) > 2): fileName = sys.argv[ 2] # Capture 3rd argument as file name for rbot rbot = getattr(importlib.import_module(fileName), className) # import rbot # Select song, add it to history, and get its SOTW number db = Database(rbot.database) # Instantiate and connect to the database song = db.randomSong() # Randomly select new song SOTWNumber = str(db.getSongOfTheWeekNumber() + 1) # Incriment SOTW number if not hasattr(rbot, "historyEnabled") or rbot.historyEnabled: db.addSongToHistory( song) # Add song to History and incriment times won del db # Close the database # Reddit if not hasattr(rbot, "postingEnabled") or rbot.postingEnabled: reddit = Reddit(rbot, VERSION) # Instantiate and setup praw reddit.post(SOTWNumber, song) # Post SOTW to Reddit
class Scheduler: """The list of analytics""" analytics = defaultdict(Analytic) def __init__(self): if len(sys.argv) < 4: logger.error("Usage: {} IN.csv Func.py [0-1 Algorithm] [*.log]" .format(sys.argv[0])) sys.exit(1) self.schedule = [] self.input_file = sys.argv[1] self.load_function_file = sys.argv[2] try: self.load_module = importlib.import_module(self.load_function_file .replace('.py', '')) # for name in dir(self.load_module): # print(getattr(self.load_module, name)) logger.info("Load function loaded") except ImportError: logger.error("Import error") self.algorithm_type = sys.argv[3] self.log_files = sys.argv[4:] self.input_analytics = [] self.schedule = [] self.database = Database("data.p") def main(self): # Loads existing data data = self.database.load_data() if data is not None: self.analytics = data # Read new logs and merge them into each analytic [ self.analytics[id].merge_run(run) for id, run in LogParser.parse_log_files(self.log_files) ] # Assign the id of the dictionary to the analytic for id in self.analytics.keys(): self.analytics[id].analytic_id = id # Saves the loaded analytic data self.database.save_data(self.analytics) # Load CSV of jobs to get frequency self.load_analytic_list(self.input_file) # Call scheduler if (self.algorithm_type == '0'): logger.info("***Random Scheduling has been chosen") self.scheduling_algorithm = RandomScheduling(7 * 24 * 60 * 60) else: logger.info("***Block Scheduling has been chosen") self.scheduling_algorithm = BlockingAlgorithm(7 * 24 * 60 * 60) # TODO: Map the analytics with something along the lines of: # [self.analytics[i] for i in self.input_analytics] (filter by input) self.schedule = self.scheduling_algorithm \ .build_schedule(self.analytics.values(), self.load_module) logger.info("Built schedule") logger.debug("Schedule: {}".format(self.schedule)) # Create schedule CSV self.schedule = self.scheduling_algorithm.schedule self.write_schedule() # TODO: Remove random save print("Max: {}".format(max(self.scheduling_algorithm.current_load))) np.save("schedule.npy", self.scheduling_algorithm.current_load) def load_analytic_list(self, file_path): """ Loads in the CSV of analytics. Arguments: file_path: the relative path of the .csv list of analytics that will be scheduled """ with open(file_path) as csv_file: reader = csv.reader(csv_file, delimiter=',') for row in reader: self.analytics[row[0]].run_frequency = row[2] logger.info("Analytic ID:" + row[0] + " Frequency: " + row[1]) def write_schedule(self): """ Writes the schedule to a CSV file named schedule.csv. """ with open('schedule.csv', 'w') as csv_file: schedule_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) for analytic in self.schedule: schedule_writer.writerow(list(analytic))
def set_db(db_name, ext='.db'): Database.current = Database(db_name, ext) Database.current.conn = sql.connect(db_name + ext) Database.current.c = conn.cursor()
def test_validate_username(self): database = Database() self.assertTrue(database.validate_username('username')) self.assertFalse(database.validate_username('')) self.assertFalse(database.validate_username('a' * 500))
def __init__(self): from src.Messengers.Telegram import TelegramMessenger self.messenger = TelegramMessenger(self, config.TOKEN) from src.Database import Database self.database = Database(self)
def main(): database = Database() database.shell()
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys sys.path.append("..") import json from src.Database import Database db = Database() # cmd: update guild2 set last_exp=exp; ret, msg = db.db_do_update_commond('update guild2 set last_exp=exp;') print ret print msg
def test_generate_salt(self): database = Database() salt_1 = database.generate_salt() salt_2 = database.generate_salt() self.assertNotEqual(salt_1, salt_2)
def mission_end(self): if self.db.flag.system_stop: end = True else: end = False check = self.control.crosswalk_mission_end if end or check: return True else: return False if __name__ == "__main__": db = Database(gps=False, imu=False) db.start() time.sleep(1) path = Path(db=db) control = Control(db=db, path=path) default = DefaultMission(db=db, control=control, path=path) narrow = NarrowMission(db=db, control=control, path=path) uturn = UturnMission(db=db, control=control, path=path) targetcar = TargetCarMission(db=db, control=control, path=path) crosswalk = CrosswalkMission(db=db, control=control, path=path) parking = ParkingMission(db=db, control=control, path=path) sample = SampleMission(db=db, control=control, path=path) sample.main()
def test_password_hash(self): database = Database() password_hash_1, salt_1 = database.password_hash('password') password_hash_2, salt_2 = database.password_hash('password') self.assertNotEqual(password_hash_1, password_hash_2) self.assertNotEqual(salt_1, salt_2)
class Cplayer: base_alphabet = '23456789abcdefghijkmnpqrstuvwxyz' def __init__(self): self.database = Database() def add_one_name_and_value(self,inlist, name, value): one_in = {} one_in['name'] = name one_in['value'] = value inlist.append(one_in) def base62_encode(self, number, alphabet=base_alphabet): # the number must 'int' if not isinstance( number, int): return "error","number must int." if number == 0: return 'success', '0' arr = [] base = len(alphabet) while number: rem = number % base number = number // base arr.append(alphabet[rem]) arr.reverse() return ''.join(arr) def GetFileList(self, FindPath): FileList=[] FileNames=os.listdir(FindPath) if (len(FileNames)>0): for fn in FileNames: fullfilename="/images/player/head/%s" %fn FileList.append(fullfilename) return FileList def main(self): try: heads = self.GetFileList('/root/biubiuServer2/images/player/head') file = open("new_players.txt") cnt = 1060 while 1: name = file.readline() if not name: break name=name.strip('\n') cnt += 1 #print name + "cnt:%s" %cnt # get params player_params_list = [] self.add_one_name_and_value(player_params_list, 'id', cnt) self.add_one_name_and_value(player_params_list, 'name', name) now = time.time() now *= 1000000000 now = int(now) + cnt account = self.base62_encode(now, self.base_alphabet) self.add_one_name_and_value(player_params_list, 'account', account) self.add_one_name_and_value(player_params_list, 'accountID', cnt) self.add_one_name_and_value(player_params_list, 'gold', random.randint(200, 20000)) self.add_one_name_and_value(player_params_list, 'level', 1) self.add_one_name_and_value(player_params_list, 'exp', random.randint(200, 20000)) self.add_one_name_and_value(player_params_list, 'headurl', heads[random.randint(0, len(heads)-1)]) #print json.dumps(player_params_list) # create account account_params_list = [] self.add_one_name_and_value(account_params_list, 'id', cnt) self.add_one_name_and_value(account_params_list, 'name', account) #self.database.db_do_insert_commond('account',account_params_list) # create player #self.database.db_do_insert_commond('player',player_params_list) #self.database.db_do_insert_commond('player2',player_params_list) # update logo sql = "select id,headurl from player2 where id=%s;" %(cnt) ret,msg,data = self.database.db_do_select_commond(sql) print data for one in data: print one[0],one[1] if isinstance(one[1],basestring): if "http://" not in one[1] : sql2 = "update player2 set headurl='http://115.159.31.18%s' where id = %s" %(one[1], one[0]) print sql2 #self.database.db_do_update_commond(sql2) break; time.sleep(0.1) except Exception,ex: print "ERROR:%s" %(ex)
def __init__(self): self.database = Database()
def main(): db = Database(gps=False, imu=False) db.start() yolo = YOLO(db=db) yolo.start() path = Path(db=db) control = Control(db=db, path=path) # 미션 매니저 생성 mission_manager = MissionManager(db=db) # 수행할 미션 순서 mission_manager.mission_keys =\ ["TrafficLight", "Default2", "Narrow", "U-Turn", "Target_Car", "Default", "Parking", "CrossWalk"] # 시작 미션 할당(일반적인 경우 0번째 미션부터 시작) mission_manager.mission_idx = 0 mission_manager.current_mission_key = mission_manager.mission_keys[ mission_manager.mission_idx] # 수행할 미션들을 생성 trafficlight_mission = TrafficLightMission(db=db, control=control, path=path) narrow_mission = NarrowMission(db=db, control=control, path=path) uturn_mission = UturnMission(db=db, control=control, path=path) target_mission = TargetCarMission(db=db, control=control, path=path) default_mission = DefaultMission(db=db, control=control, path=path) default_mission2 = DefaultMission2(db=db, control=control, path=path) parking_mission = ParkingMission(db=db, control=control, path=path) crosswalk_mission = CrosswalkMission(db=db, control=control, path=path) # 미션 매니저에 수행할 미션들을 추가. mission_manager.add_mission(key="TrafficLight", mission=trafficlight_mission) mission_manager.add_mission(key="Narrow", mission=narrow_mission) mission_manager.add_mission(key="U-Turn", mission=uturn_mission) mission_manager.add_mission(key="Target_Car", mission=target_mission) mission_manager.add_mission(key="Default", mission=default_mission) mission_manager.add_mission(key="Default2", mission=default_mission2) mission_manager.add_mission(key="Parking", mission=parking_mission) mission_manager.add_mission(key="CrossWalk", mission=crosswalk_mission) mission_manager.start() while True: if db.flag.system_stop: break else: try: print(mission_manager.current_mission_key) print(db.mission) time.sleep(1) except KeyboardInterrupt: print("Keyboard Interrupt detected!") db.flag.system_stop = True break yolo.join() mission_manager.join() db.join() return 0