def test_no_file_pantry(self): self.assertFalse(os.path.exists(self.filename + 'new')) with pantry(self.filename + 'new') as p: p['Test'] = True # write to new pantry with pantry(self.filename + 'new') as p: self.assertEqual(p['Test'], True)
def test_no_file_pantry(self): self.assertFalse(os.path.exists(self.filename+'new')) with pantry(self.filename+'new') as p: p['Test'] = True # write to new pantry with pantry(self.filename+'new') as p: self.assertEqual(p['Test'], True)
def test_read_from_pantry(self): with pantry(self.filename) as p: self.assertEqual(isinstance(p, dict), True) # pantry object should be dict p['Test'] = True # write to pantry with pantry(self.filename) as q: self.assertEqual(q['Test'], True) # Should be True q['SecondTest'] = 4 # determined to be random enough with pantry(self.filename) as r: self.assertEqual(r['Test'], True) # previous write self.assertEqual(r['SecondTest'], 4) # second write
def upload_local(pf, bucket): with pantry(pf) as db: while db['local']: im = db['local'].pop(0) upload_image(im['filename'], bucket) db['images'].append(im) db['uploaded'] += 1
def test_pantry_custom_class_defs(self): p = pantry.open(self.filename) p.db = CustomPantry(1, 2, 3, 4, 5) self.assertEqual(sum(p.db.args), sum((1, 2, 3, 4, 5))) p.close() with pantry(self.filename) as q: self.assertEqual(q.do_math(), sum((1, 2, 3, 4, 5)))
def initialize_pantry(pf): if os.path.isfile(pf): return with pantry(pf) as db: db['local'] = list() db['images'] = list() db['captured'] = 0 db['uploaded'] = 0
def test_immediate_save(self): db = {'a': 1, 'b': 2} pantry.store(self.filename, db) p = pantry.open(self.filename) self.assertEqual(p.db['a'], 1) p.db['b'] = 3 p.close() with pantry(self.filename) as db: self.assertEqual(db['b'], 3)
def on_first_run(): # list of four letter Q words... because. wordbank = 'QAID QATS QINS QOPH QUAD QUAG QUAI QUAT QUAY ' wordbank += 'QUEP QUEY QUID QUIM QUIN QUIP QUIT QUIZ QUOD QUOP' schema = ('result', 'cached_until') # setting up our pantry with pantry('demo.pk') as db: for word in wordbank.split(): api_result = magic_api(word) db[word] = dict(zip(schema, api_result))
def update(): with pantry('demo.pk') as db: for k, v in sorted(db.items(), key=lambda x: x[1]['cached_until']): if v['cached_until'] < datetime.now(): print('{} is being updated'.format(k)) result, cached_until = magic_api(k) v['result'] = result v['cached_until'] = cached_until else: time_left = (v['cached_until'] - datetime.now()).seconds print('{} has {} seconds until it needs updated'.format(k, time_left))
def update(debug=None): """ Updates the user's google calendar with any entry in the lazydb without an id set. """ with pantry('data/lazydb.pk') as db: for k, v in db.items(): if not v['id']: if debug: print "Adding new event...", result = add_event(v) db[k]['id'] = result['id'] if debug: print "Success", result['id']
def update(): with pantry('demo.pk') as db: for k, v in sorted(db.items(), key=lambda x: x[1]['cached_until']): if v['cached_until'] < datetime.now(): print('{} is being updated'.format(k)) result, cached_until = magic_api(k) v['result'] = result v['cached_until'] = cached_until else: time_left = (v['cached_until'] - datetime.now()).seconds print('{} has {} seconds until it needs updated'.format( k, time_left))
def capture(pf): c = PiCamera() c.resolution = (1920, 1080) now = datetime.now() filename = now.strftime("%F.%T") c.iso = 200 sleep(2) c.shutter_speed = c.exposure_speed c.exposure_mode = 'off' g = c.awb_gains c.awb_mode = 'off' c.awb_gains = g sleep(2) c.capture("images/{}.png".format(filename)) data = {'filename': '{}.png'.format(filename), 'datetime': now} with pantry(pf) as db: db['local'].append(data) db['captured'] += 1
def update_schedule(self): """ Updates the lazydb object with newly detected shifts to avoid adding already existing shifts to the calendar """ now = datetime.now() with pantry('data/lazydb.pk') as db: for k, v in db.items(): if v['start'] < now: if self.DEBUG: print "Old event removed", v['start'] del db[k] for k, v in self.schedule.items(): if k not in db.keys(): if self.DEBUG: print "New event inserted", v['start'] db[k] = v else: if self.DEBUG: print "Event exists", db[k]['start']
def test_write_to_pantry(self): with pantry(self.filename) as p: self.assertEqual(isinstance(p, dict), True) # pantry object should be dict p['Test'] = True # write to pantry object