def _check_updates(self): response = get("https://api.ratesapi.io/api/latest") if response.status_code == 200: storage = Storage(storage_name) latest_api_date = json.loads(response.text)["date"] logging.debug(f"API date: {latest_api_date}") rates = storage.get_dict() latest_storage_date = rates.get("last_update") logging.debug(f"Storage date: {latest_storage_date}") if latest_storage_date: if ( latest_storage_date < latest_api_date or [ currency for currency, rates in rates["currencies"].items() if rates["rates"].keys() != supported_currencies.keys() ] or rates["currencies"].keys() != supported_currencies.keys() ): return "update" else: return "ok" else: return "empty" else: return "failed"
def initGameCallBack(self): if isinstance(self.interFrame, Frame): self.interFrame.destroy() self.timer.destroy() self.counter.destroy() if isinstance(self.gameFrame, Frame): self.gameFrame.destroy() storage = Storage() self.mines = storage.get("mines") h = storage.get("height") w = storage.get("width") self.isGameStarted = False self.tiles = [[j for j in range(w)] for i in range(h)] self.bombs = set() self.bombsPrediction = set() self.tilesAmount = w * h self.board = Board(w, h, self.mines) self.root.bind_class('Label', '<Button-1>', self.openTileEvent) self.root.bind_class('Label', '<Button-2>', self.flagTileEvent) self.root.bind('<KeyPress>', self.keyboardEvent) self.code = "" self.__createFrame() self.__initInterface()
def getScheduleStorage(load, pv, RATEOPTION): st = Storage(df=load, RATE=RATEOPTION) load_t = preproc_df(load) pv_t = preproc_df(pv) r_nbc = st.RATE["NBC"] r_on_m = preproc_demand_rate_m(st, "ONPEAK") r_mid_m = preproc_demand_rate_m(st, "MIDPEAK") r_off_m = preproc_demand_rate_m(st, "OFFPEAK") c_whole = 0.05 PCS_max = 500.0 CAP_ESS = 1000.0 SOC_min = 0.0 SOC_max = 1.0 CC = st.RATE["CC"] ef_c = 0.96 ef_d = (1.0 / 0.96) solver = build_solve_milp(st, load_t=load_t, pv_t=pv_t, \ cost_t=preproc_cost(st), \ r_nbc=r_nbc, r_on_m=r_on_m, r_mid_m=r_mid_m, r_off_m=r_off_m, \ c_whole=c_whole, PCS_max=PCS_max, \ ef_c=ef_c, ef_d=ef_d, CAP_ess=CAP_ESS, SOC_min=SOC_min, SOC_max=SOC_max, CC=CC)
def __init__(self): bot = commands.Bot(command_prefix='.') self.storage = Storage() self.channel_controller = ChannelConstroller(DiscordMessageBus(bot), self.storage.unique_tag) self.bot = attach_commands(bot, self.storage, self.channel_controller) self.looper = Looper(bot, self.storage, self.channel_controller)
def test_extract_subs(self): tmp_dir = tempfile.mkdtemp() file = os.path.join(tmp_dir, 'fragment.mkv') shutil.copyfile('fragment.mkv', file) with Storage(':memory:') as storage: app_run_config = AppRunConfig(tmp_dir, [languages.get(part1=x) for x in ['ru', 'en', 'fr']], [(languages.get(part1='ru'), languages.get(part1='en'))], ".*", {}, False, False) extract_subs = ExtractSubs(app_run_config, storage) extract_subs.scan_files() video_files = storage.get_all_video_files() self.assertEqual(1, len(video_files)) fragment_file = video_files[0] subtitles = storage.get_all_subtitles_by_video_file_id(fragment_file['id']) self.assertEqual(5, len(subtitles)) subtitles_file_names = set(os.path.basename(subtitle['full_path']) for subtitle in subtitles) self.assertEqual({'fragment_eng_SDH.srt', 'fragment_fra.srt', 'fragment.rus_eng.ass', 'fragment_rus_Forced.srt', 'fragment_rus.srt'}, subtitles_file_names) # 1 - cause forced subtitles is empty, file is empty, can't merge self.assertEqual(1, len(storage.get_all_merged_subtitles_by_video_file_id(fragment_file['id']))) shutil.rmtree(tmp_dir, ignore_errors=True)
def test_delete(self): ''' Test Storage.delete basic functionality (without ttl). :return: ''' storage = Storage() self.assertEqual(0, storage.delete([])) keys_to_set = { '1': 'hello', '2': 'bye', '3': [1, 2, 'three'], '4': { 1: 'one', 2: 'two' } } keys_to_delete = ['2', '4', '60'] for key in keys_to_set: storage.set(key, keys_to_set[key]) num_deleted = storage.delete(keys_to_delete) true_num_deleted = 0 for key in keys_to_delete: if key in keys_to_set: keys_to_set.pop(key) true_num_deleted += 1 self.assertEqual(true_num_deleted, num_deleted, 'Num of deleted keys is wrong.') self.assertEqual(keys_to_set, storage._keys_dict, 'Remaining keys are wrong')
def main(): data = Data([1, 2, 3, 4, 5], "lol") print(data) storage = Storage() storage.add(data) storage.add(Data([1, 2, 3, 4, 5], "kek")) print(storage.getAll())
def test_keys_failure(self): """ Test Storage.keys command for failure :return: """ storage = Storage() storage._keys_dict = {'1': 'one', 'abc': '1'} self.assertRaises(StoragePatternError, storage.keys, 'ab[cd')
def test_read_not_exists(self): work_dir = SetupTest.ensure_clean_work_dir() storage_path = os.path.join(work_dir, 'file_must_not_exists.yaml') self.assertFalse(os.path.exists(storage_path)) p = Storage() p.set_file(storage_path) p.load()
def setUp(self) -> None: self.storage = Storage() self.gc = StorageGarbageCollector(self.storage) self.now = time.time() def fake_time(): return self.now self.fake_time = fake_time
def __init__(self, config: Config): self.config = config self.storage = Storage(config) self.health_code_map = { 1: "success", 0: "failure", -1: "unresponsive", -2: "unreachable", -3: "unknown errs", }
def __init__(self, name): Device.__init__(self, name) CheckCyclicTask.__init__(self) # default config values self._eep = self.DEFAULT_EEP.clone() self._storage_max_age = None # type: Optional[int] # age in seconds self._storage = Storage()
def test_set_moe_rewrite(self): """ Test if moe is deleted when rewriting key with moe to key without moe. :return: """ storage = Storage() storage.set('1', 1, 5) storage.set('1', 2) self.assertEqual(False, '1' in storage._moe_dict, "Moe for key '1' should be reset.")
def __init__(self, config=None): configurable.Configurable.__init__(self, config) self._event_handlers = { 'key.detach': KeyDetach(self), 'key.invalidate': KeyInvalidate(self) } self._society = Society(self, self.config.g('society.members', []), self.config.g('society.announceInterval', 60)) self._storage = Storage(self) self._time_start = time.time()
def test_search_for_key(tmpdir, keyword, emotion): db_path = str(tmpdir.mkdir("database")) db_name = "tmp_db" stor = Storage(db_name, db_path, "tmp_table") stor.create_new_entry("tmp_table", "'dissapointing'", "'sad'") stor.create_new_entry("tmp_table", "'provoked'", "'angry'") stor.create_new_entry("tmp_table", "'inspired'", "'happy'") res = stor.search_for_keyword(db_path, db_name, "tmp_table", keyword) assert res == [(keyword, emotion)]
def test_set_with_get(self): """ Test Storage.set with 'get' option :return: """ storage = Storage() storage.set('1', 1) self.assertEqual(1, storage.set('1', 2, get=True), "Should return previous value") self.assertEqual(2, storage.get('1'), 'Should get new value') self.assertEqual(None, storage.set('2', 1, get=True), "Should return None as there was no key '2'")
def __init__(self, storage=None): """ :param storage: Storage object or None for creating it automatically :param gc: enable garbage collector in createt Storage :param file_prefix: prefix for file names for storage saving/loading, set None to disable saving/loading """ if storage is None: storage = Storage() self.storage = storage self.astonished = False
def convert(self, pretiffy=False): if self.input_currency and self.output_currency: if len(self.input_currency) > 1: return ( "Too many input currency options! Please insert a currency code/symbol matching the unique currency.", 400, ) else: input_dict = { "amount": self.amount, "currency": self.input_currency[0] } storage = Storage(storage_name) rates = storage.get_dict() try: rates = rates["currencies"][ self.input_currency[0]]["rates"] logging.info(rates) except KeyError: rates = {} if rates: output_dict = { currency: round(rates[currency] * self.amount, 2) if currency in rates else None for currency in self.output_currency } return ( json.dumps( { "input": input_dict, "output": output_dict }, indent=4, sort_keys=True, ) if pretiffy else { "input": input_dict, "output": output_dict }, 200, ) else: return ( "Missing currency exchange rates. Please try to update the storage.", 400, ) else: return ( "Unsupported currency symbol/code! Please check inserted values with supported values help and try again!", 400, )
def main(args): """Coordinate the skill. Main function called in IBM Cloud Functions""" ### Helper Functions ### def do_cleanup(paths: list) -> None: """Cleanup the leftover paths Args: paths: a list of paths to clean up """ for path in paths: os.remove(path) def get_config(config_file_path: str) -> dict: """Load the skill config Args: config_file_path: location of config file """ config = {} with open(config_file_path) as config_file: config = json.load(config_file) return config ### Main Function ### # Get args request_body = args config_file = args.get('config', './config.json') config = get_config(config_file) # # Build pieces storage = Storage(config, request_body) action = Action(config) # # Download --> ACTION --> Upload file_data = storage.file_data # {name: "", id: 0000} file_path = storage.download_file() # Run Action action.do_action(file_path, file_data['id']) # Upload/Update new file results = action.push2storage(storage) # Clean up local files do_cleanup([file_path]) # Return status return {"status": "success"}
def test_keys_ttl(self): """ Test Storage.keys with ttl :return: """ self.now = time.time() with patch('time.time', self.fake_time): storage = Storage() storage.set('1', 'one', self.now + 5) storage.set('2', 'two') storage.set('3', 'three', self.now + 10) self.now += 6 self.assertEqual(['2', '3'], storage.keys('*')) self.assertEqual(['2', '3'], list(storage._keys_dict.keys()))
def storage(request) -> Storage: storage_type: Literal['sql', 'json'] = request.param # We can conveniently sneak in a test case right here with pytest.raises(RuntimeError): # This should fail as we have not previously # initialized the storage # for any of the supported storage types Storage() store = Storage(storage_type) store.db.create() yield store store.db.destroy() # We also need to reset the __interned cache after each run # Double underscore variables get namespace shifted # so we need to do a force lookup to find their new name attr_name = "__interned" cache_attr = [attr for attr in dir(Storage) if attr_name in attr][0] setattr(Storage, cache_attr, {})
def test_get_all(tmpdir): db_path = str(tmpdir.mkdir("database")) db_name = "tmp_db" stor = Storage(db_name, db_path, "tmp_table") stor.create_new_entry("tmp_table", "'dissapointing'", "'sad'") stor.create_new_entry("tmp_table", "'provoked'", "'angry'") stor.create_new_entry("tmp_table", "'inspired'", "'happy'") stor.create_new_entry("tmp_table", "'offended'", "'angry'") stor.create_new_entry("tmp_table", "'pleased'", "'happy'") res = stor.get_all("tmp_table") assert res == [('dissapointing', 'sad'), ('provoked', 'angry'), ('inspired', 'happy'), ('offended', 'angry'), ('pleased', 'happy')]
def test_roundtrip(self): work_dir = SetupTest.ensure_clean_work_dir() storage_path = os.path.join(work_dir, 'storage_test.yaml') self.assertFalse(os.path.exists(storage_path)) p = Storage() p.set_file(storage_path) p.load() data1 = {"abc": 123, "456": "rr", 'list': [1, 2, 3, "str"]} p.set("data1", data1) data2 = datetime.datetime(2018, 12, 3, 13, 7, 45, tzinfo=tzoffset(None, 3600)) p.set("data2", data2) data3 = 123 p.set("data3", data3) p.save() self.assertTrue(os.path.exists(storage_path)) p = Storage() p.set_file(storage_path) p.load() comp1 = p.get("data1") self.assertEqual(comp1, data1) comp2 = p.get("data2") self.assertEqual(comp2, data2) comp3 = p.get("data3") self.assertEqual(comp3, data3)
def test_error_write_permission(self): path = "/usr/bin/not_allowed_to_write_into" self.assertFalse(os.path.exists(path)) p = Storage() p.set_file(path) p.load() data1 = 123 p.set("data1", data1) with self.assertRaises(StorageException): p.save() self.assertFalse(os.path.exists(path))
def test_create_new_entry(tmpdir, keyword, emotion): db_path = str(tmpdir.mkdir("database")) db_name = "tmp_db" stor = Storage(db_name, db_path, "tmp_table") kw = "'"+keyword+"'" emo = "'"+emotion+"'" stor.create_new_entry("tmp_table", kw, emo) conn = sqlite3.connect(os_path.join(db_path, db_name)) cur = conn.cursor() # where keyword = ? and emotion = ? , (keyword, emotion) entriesMade = cur.execute("select * from tmp_table").fetchall() conn.close() assert entriesMade == [(keyword, emotion)]
def test_error_read_wrong_format(self): work_dir = SetupTest.ensure_clean_work_dir() storage_path = os.path.join(work_dir, 'error_wrong_format.yaml') self.assertFalse(os.path.exists(storage_path)) byte_data = [120, 3, 255, 0, 100] with open(storage_path, "wb") as stream: pickle.dump(byte_data, stream) self.assertTrue(os.path.isfile(storage_path)) p = Storage() p.set_file(storage_path) with self.assertRaises(StorageException): p.load()
def test_set_moe(self): """ Test Storage.set_moe method :return: """ self.now = time.time() with patch('time.time', self.fake_time): storage = Storage() self.assertRaises(StorageKeyError, storage.set_moe, 1, {'moe': 2}) storage.set(1, 'one') storage.set(2, 'two', 2) self.assertEqual(None, storage.set_moe(1, 2)) self.assertEqual(None, storage.set_moe(2, None)) self.now += 3 self.assertRaises(StorageKeyError, storage.get, 1) self.assertEqual('two', storage.get(2))
def test_get_ttl(self): """ Test Storage.get with ttl. :return: """ self.now = time.time() with patch('time.time', self.fake_time): storage = Storage() keys_to_set = { '1': 'hello', '2': 'bye', '3': [1, 2, 'three'], '4': { 1: 'one', 2: 'two' } } moes = {'1': time.time() + 5, '4': time.time() + 10} for key in keys_to_set.keys(): storage.set(key, keys_to_set[key], moes.get(key)) # test at moment t self.assertEqual(keys_to_set['1'], storage.get('1'), "Key '1' should still exist.") # test at moment t+6, one key should expire self.now += 6 keys_to_set.pop('1') moes.pop('1') self.assertRaises(StorageKeyError, storage.get, '1') self.assertEqual(keys_to_set['4'], storage.get('4'), "Key '4' should still exist.") self.assertEqual(keys_to_set, storage._keys_dict, "Remaining keys are wrong") self.assertEqual(moes, storage._moe_dict, "Remaining moes are wrong") # test at moment t+11 self.now += 5 keys_to_set.pop('4') moes.pop('4') self.assertRaises(StorageKeyError, storage.get, '1') self.assertRaises(StorageKeyError, storage.get, '4') self.assertEqual(keys_to_set, storage._keys_dict, "Remaining keys are wrong") self.assertEqual(moes, storage._moe_dict, "Remaining moes are wrong")
def test(): load = dao.getRawLoad() pv = dao.getRawPV() # es = dao.getRawES() # net = dao.getRawNet() # for test # load = load.loc[load.index[[i for i in range(0, 31)]]] # pv = pv.loc[pv.index[[i for i in range(0, 31)]]] # es = dao.getRawES().drop(es.index[[i for i in range(31, 365)]]) # net = dao.getRawNet().drop(net.index[[i for i in range(31, 365)]]) st = Storage(df=load, RATE=rate.TOU8_OPTION_R) # cost_t = preproc_cost(st) load_t = preproc_df(load) pv_t = preproc_df(pv) r_nbc = st.RATE["NBC"] r_on_m = preproc_demand_rate_m(st, "ONPEAK") r_mid_m = preproc_demand_rate_m(st, "MIDPEAK") r_off_m = preproc_demand_rate_m(st, "OFFPEAK") c_whole = 0.05 PCS_max = 500.0 CAP_ESS = 1000.0 SOC_min = 0.0 SOC_max = 1.0 CC = st.RATE["CC"] ef_c = 0.96 ef_d = (1.0 / 0.96) #Î dfnet, dfsoc, dfess = build_solve_milp(st, load_t=load_t, pv_t=pv_t,\ cost_t=preproc_cost(st),\ r_nbc=r_nbc, r_on_m=r_on_m, r_mid_m=r_mid_m, r_off_m=r_off_m,\ c_whole=c_whole, PCS_max=PCS_max,\ ef_c=ef_c, ef_d=ef_d, CAP_ess=CAP_ESS, SOC_min=SOC_min, SOC_max=SOC_max, CC=CC) print("hi")
def test_set(self): """ Test Storage.set basic functionality. :return: """ storage = Storage() items_to_add = { 'test_s': 'hello', 'test_list': [1, 2, 'three'], 'test_dict': { 1: 'one', 2: 'two' } } moes = {'test_list': time.time() + 5} for key in items_to_add.keys(): storage.set(key, items_to_add[key], moes.get(key)) self.assertEqual(items_to_add, storage._keys_dict, 'Added keys are wrong.') self.assertEqual(moes, storage._moe_dict, 'Added moes are wrong.')