def lambda_handler(event, session): import boto3 from pydblite import Base db = Base('/tmp/TechHub.pld') if db.exists(): db.open() else: db.create('name', 'quantity') db.open() dynamodb = boto3.client('dynamodb') print("[INTENT_HANDLER]") print("event.session.application.applicationId=" + event['session']['application']['applicationId']) logger.info('got event{}'.format(event)) if event['session']['new']: on_session_started({'requestId': event['request']['requestId']}, event['session']) if event['request']['type'] == "LaunchRequest": output = on_launch(event['request'], event['session']) elif event['request']['type'] == "IntentRequest": output = on_intent(event['request'], event['session'], db, dynamodb) elif event['request']['type'] == "SessionEndedRequest": output = on_session_ended(event['request'], event['session']) db.commit() return output
def run_class_configuration(request): if request.method == 'POST': db = Base('backendDB.pdl') if db.exists(): db.open() else: db.create('Type','Log', 'Run', 'Prefix','Rule','Threshold', 'TimeStamp', 'Status') configuration_json = json.loads(request.body) print configuration_json log = configuration_json["log"] prefix = configuration_json['prefix'] rule = configuration_json['rule'] threshold = configuration_json['threshold'] # Encode the file. encoding.encode(log, prefix) for encodingMethod in configuration_json['encoding']: for clustering in configuration_json['clustering']: for classification in configuration_json['classification']: django_rq.enqueue(tasks.classifierTask, log, prefix, encodingMethod, clustering, classification, rule, threshold) run = classification + '_' + encodingMethod + '_' + clustering records = [r for r in db if r['Run'] == run and r['Prefix'] == str(prefix) and r['Log'] == log and r['Rule'] == rule and r['Threshold'] == str(threshold)] print records if not records: db.insert("Classification", log, run, str(prefix), rule, str(threshold), time.strftime("%b %d %Y %H:%M:%S", time.localtime()), 'queued') else: db.update(records[0], TimeStamp=time.strftime("%b %d %Y %H:%M:%S", time.localtime()), Status= 'queued') db.commit() return HttpResponse("YOLO")
def open_db(): db = Base(DB_FILE) db.create('abbr', 'name', 'available', 'volume', 'buy', 'sell', 'date', mode="open") if not db.exists(): raise Exception('Database error') db.create_index('abbr') return db
def get_cursor(db_name): if db_name == 'weather_data': db = Base('storage/weather_data.pdl') if not db.exists(): db.create('low', 'tmw', 'high', 'temp', 'date', 'text', 'code', 'history', 'uniq_id', 'location', 'astronomy', 'atmosphere', 'country_name', 'created_date', 'location_name') elif db_name == 'locations': db = Base('storage/locations.pdl') if not db.exists(): db.create('uniq_id', 'location', 'created_date') else: raise Exception('Please Enter Valid Name!') cursor = db.open() return cursor
def getConfStatus(request): db = Base('backendDB.pdl') if db.exists(): db.open() records = []; for r in db: print r records.append(r) return HttpResponse(json.dumps(records), content_type="application/json")
def printdb(dbpath): #Prints the contents of a PyDBLite database to the console db = Base(dbpath) if db.exists(): db.open() retstr = "" for obj in db: retstr += str(obj) retstr += "\n" print retstr return retstr else: print "The database does not exist or is corrupt.\n"
def printdb(dbpath): # Prints the contents of a PyDBLite database to the console db = Base(dbpath) if db.exists(): db.open() retstr = "" for obj in db: retstr += str(obj) retstr += "\n" encoded = retstr.encode("utf-8", errors="ignore") print(encoded) else: print("The database does not exist or is corrupt.\n")
def get_all_record_data(): db = Base('nfc_auth_ok_rec.pdl') if not db.exists(): db.create('time', 'device', 'name') db.open() all_str = '[' i = 0 for r in db: obj = '{\"time\":'+str(r['time'])+\ ',\"name\":\"'+r['name']+\ '\",\"device\":\"'+r['device']+'\"}' all_str += obj if i < len(db) - 1: all_str += ',' i = i + 1 all_str += ']' return all_str
def load_repo(db_path): job_db = Base(os.path.join(db_path, 'PBS_job_database.pdl')) if job_db.exists(): job_db.open() print("PBS_database found. Loading...\n") all_items = list_queue() pbs_id = [] for line in all_items: pbs_id.append(str(re.search(r'\d+', line).group())) for ele in pbs_id: ele_dir = init_work_dir(ele) ele_id = re.findall(r'\d+', ele_dir)[-1] if len(job_db(PBS_id=ele)) == 0: job_db.insert(PBS_id=ele, work_dir=ele_dir, uniq_id=ele_id) else: job_db = new_repo(db_path) job_db.commit()
def run_configuration(request): if request.method == 'POST': db = Base('backendDB.pdl') # db.create('Type','Log', 'Run', 'Prefix','Rule','Threshold', 'TimeStamp', 'Status', mode="override") if db.exists(): db.open() else: db.create('Type','Log', 'Run', 'Prefix','Rule','Threshold', 'TimeStamp', 'Status') configuration_json = json.loads(request.body) print configuration_json log = configuration_json["log"] prefix = configuration_json['prefix'] # Encode the file. encoding.encode(log, prefix) for encodingMethod in configuration_json['encoding']: for clustering in configuration_json['clustering']: for regression in configuration_json['regression']: django_rq.enqueue(tasks.regressionTask, log, prefix, encodingMethod, clustering, regression) run = regression + '_' + encodingMethod + '_' + clustering records = [r for r in db if r['Run'] == run and r['Prefix'] == str(prefix) and r['Log'] == log] # for r in db: # if (r['Run'] == run) and (r['Prefix'] == str(prefix)) and (r['Log'] == log): # records.append(r) print records if not records: db.insert("Regression", log, run, str(prefix),"NaN","NaN", time.strftime("%b %d %Y %H:%M:%S", time.localtime()), 'queued') else: db.update(records[0], TimeStamp=time.strftime("%b %d %Y %H:%M:%S", time.localtime()), Status= 'queued') # if run in df['Run'].unique(): # df.loc[df.Run == run, 'TimeStamp'] = time.strftime("%b %d %Y %H:%M:%S", time.localtime()) # df.loc[df.Run == run, 'Status'] = "queued" # else: # df.loc[df.shape[0]] = [run, time.strftime("%b %d %Y %H:%M:%S", time.localtime()), 'queued'] # print df # print df['Run'] # df.to_csv('core_results_queue/' + log + '/' + str(prefix) + '/reg_queueStatus.csv', sep=',',header=writeHeader, mode='w+', index=False) db.commit() return HttpResponse("YOLO")
def initialize_database(): print("Initializing database!") db = Base('block_chain.pdl') if (db.exists()): print("Delete block chain database") db.delete db.create('index', 'timestamp', 'hash', 'previousHash', 'data', mode="override") genesisData = "It all begins here!" timestamp = datetime.now() index = 0 hash = calculateHash(index, "", timestamp, genesisData) genesisBlock = Block(index, str(timestamp), hash, None, genesisData) db.insert(genesisBlock.index, genesisBlock.timestamp, genesisBlock.hash, genesisBlock.previousHash, genesisBlock.data) db.commit()
def setup(args): if os.path.isfile(path): backup_path = path + '.bak' shutil.copy(path, backup_path) db = Base(path) if args.overwrite: while 1: confirm = input('Do you really want to overwrite the database ? (y/n) ') if confirm in ['y', 'n']: break if confirm == 'y': db.create('name', 'cost', 'date', 'tags', mode='override') print('Data base in {} has been overwritten!'.format(path)) else: if db.exists(): print('{} already exists and is a database. If you want to recreate' ' it, use the -o flag.'.format(path)) else: db.create('name', 'cost', 'date', 'tags', mode='open') print('Created database at {}!'.format(path))
def get_results_db(clear_cache=False, skip=[]): cache_file = 'cache/results.pdl' db = Base(cache_file) if clear_cache or not db.exists() or os.path.getmtime(cache_file) < os.path.getmtime(results_dir): warnings.warn('Rebuilding results cache...') columns = set() rows = [] p = pathlib.Path(results_dir) for config_file in p.glob('*.config'): with config_file.open() as config_fh: settings_hash = config_file.stem row = json.loads(config_fh.read()) if settings_hash in skip: continue row['hash'] = settings_hash tests_count = analyze.count(config_file.parent, settings_hash) row['iostat_cpu'], len_cpu_values = analyze.iostat_cpu(config_file.parent, settings_hash) row['iperf_result'], len_iperf_values = getattr(analyze, row['iperf_name'])(config_file.parent, settings_hash, row) if tests_count != len_cpu_values or tests_count != len_iperf_values: raise analyze.AnalysisException('For test {}, mismatch in cardinality of tests between count ({}), iostat ({}) and iperf ({})'.format(settings_hash, tests_count, len_cpu_values, len_iperf_values), settings_hash) if len_iperf_values > 0: min_fairness = row['iperf_result']['fairness'][0] - row['iperf_result']['fairness'][1] if min_fairness < (1 - 1 / (2 * row['parallelism'])): warnings.warn('For test {}, fairness has a critical value: {}.'.format(settings_hash, row['iperf_result']['fairness']), RuntimeWarning) columns = columns | set(row.keys()) rows.append(row) db.create(*columns, mode='override') for r in rows: db.insert(**r) db.commit() warnings.warn('Results cache built.') else: warnings.warn('Reusing results cache.') db.open() return db
from pathlib import Path from pydblite import Base if not Path('database').exists(): Path('database').mkdir() client = Base('database/client.pdl') if client.exists(): client.open() else: client.create('secret', 'redirect_uri', 'name') authorization_code = Base('database/authorization_code.pdl') if authorization_code.exists(): authorization_code.open() else: authorization_code.create('user_id', 'code', 'expire_time') token = Base('database/token.pdl') if token.exists(): token.open() else: token.create('user_id', 'access', 'expire_time', 'refresh') if len(client) == 0: client.insert(secret='test_secret', redirect_uri='http://example.com', name='app1') client.commit()
from tornado import gen from tornado_json.requesthandlers import APIHandler from tornado_json import schema from tornado_json.gen import coroutine from pydblite import Base db = Base('test.pdl') if db.exists(): db.open() else: db.create('body', 'guid') class post_data(APIHandler): @schema.validate( input_schema={ "type": "object", "properties": { "body": {"type": "string"}, "guid": {"type": "string"}, } }, input_example={ "body": "Very Important Post-It Note", "guid": "information", } ) def post(self): # `schema.validate` will JSON-decode `self.request.body` for us # and set self.body as the result, so we can use that here
class Inventory: def __init__(self, session): self.session = session self.client = session.resource("ec2") self.create_database() def create_database(self): self.db = Base("test.pdl") if not self.db.exists(): self.db.create("resource", "name", "env") self.db.open() def fetch_instance(self, id): try: instances = self.client.instances.filter(InstanceIds=[id]) for instance in instances: tags = {t["Key"]: t["Value"] for t in instance.tags} env = tags["Env"] if "Env" in tags else tags["Environment"] self.db.insert(resource=id, name=tags["Name"], env=env) except Exception as e: print(e) self.db.insert(resource=id, name=id, env="") def get_instance(self, id): instance = self.db(resource=id) if not instance: self.fetch_instance(id) instance = self.db(resource=id) return instance[0] if instance else None def fetch_network_interface(self, id): c = self.client.meta.client try: data = c.describe_network_interfaces(NetworkInterfaceIds=[id]) nif = data["NetworkInterfaces"][0] if "Attachment" in nif: instance = self.get_instance(nif["Attachment"]["InstanceId"]) if instance: self.db.insert(resource=id, name=instance["name"], env=instance["env"]) except Exception as e: print(e) self.db.insert(resource=id, name=id, env="") def add_group(self, id, name): self.db.insert(resource=id, name=name, env="") def get_network_interface(self, id): nif = self.db(resource=id) if not nif: self.fetch_network_interface(id) nif = self.db(resource=id) return nif[0] if nif else None def fetch_security_group(self, id): c = self.client.meta.client try: groups = c.describe_security_groups(GroupIds=[id]) for group in groups["SecurityGroups"]: self.db.insert(resource=id, name=group["GroupName"]) except Exception as e: print(e) def get_security_group(self, id): group = self.db(resource=id) if not group: self.fetch_security_group(id) group = self.db(resource=id) return group[0] if group else None def get_resource_name(self, id): resource = self.db(resource=id) return resource[0]["name"] if resource else None
from pydblite import Base import datetime import crawler import configure as conf dbFileNameSuffix = conf.db_filename idData = Base('id.' + dbFileNameSuffix) if idData.exists(): idData.open() else: idData.create('id', 'name', 'meterNo', 'campus', \ 'building', 'floor', 'room') usageData = Base('usage.' + dbFileNameSuffix) if usageData.exists(): usageData.open() else: usageData.create('id', 'date', 'usage', 'remain') def updateIdData(): #idData.execute('delete from IDDATA;') # Clear old data idData.delete(idData) data = crawler.getIdData() for i in data: idData.insert( i['id'], i['name'], i['meterNo'], i['campus'], \ i['building'], i['floor'], i['room']) idData.commit() return def updateUsageData(mids):
from pathlib import Path from pydblite import Base if not Path('db').exists(): Path('db').mkdir() client = Base('db/client.pdl') if client.exists(): client.open() else: client.create('secret', 'redirect_uri', 'name') authorization_code = Base('db/authorization_code.pdl') if authorization_code.exists(): authorization_code.open() else: authorization_code.create('user_id', 'code', 'expire_time') token = Base('db/token.pdl') if token.exists(): token.open() else: token.create('user_id', 'access', 'expire_time', 'refresh') user = Base('db/user.pdl') if user.exists(): user.open() else: user.create('login', 'password_hash', 'name', 'email') event = Base('db/event.pdl')
# -*- coding: utf-8 -*- from pydblite import Base ''' 序列化采用cPickle ''' db = Base("test.db", save_to_file=False) if db.exists(): db.open() else: db.create("name", "age") db.insert("bob", 10) index = db.insert(name="alice", age=20) print db[index] # 按照主键访问record record = db[1] db.update(record, name="dellian") #db.delete(record) # db.records (所有记录) # query for r in db("age") > 10: print r
def get_nfc_key_data_base(): db = Base('nfc_key.pdl') if not db.exists(): db.create('time', 'pwd') db.open() return db
from pydblite import Base db = Base('test.pydb') db.create('name', 'age', 'size') db.insert(name='homer', age=23, size=1.84) db.insert('homer', 23, 1.84) db.commit() db.exists()
def run_every_10_seconds(): print 'run_every_10_seconds' now = datetime.datetime.now() #record = db(user="") #now = now.replace(hour=20, minute=0, second=1, microsecond=0) print now; db = Base(schedule_table_name) if db.exists(): db.open() records = db.records ''' list_of_records = []; for record in records: list_of_records.append(records[record]); db.delete(list_of_records); db.commit(); ''' for record in records: schedule_time = records[record]['schedule_time'] user = records[record]['user'] chat_id = records[record]['chat_id'] enable = records[record]['enable'] print schedule_time,user,chat_id,enable if enable=='no': continue try: if schedule_time==AM8: today8am0min = now.replace(hour=8, minute=0, second=0, microsecond=0) today8am1min = now.replace(hour=8, minute=1, second=0, microsecond=0) if now >= today8am0min and now <= today8am1min: kik.send_messages([TextMessage(to=user,chat_id=chat_id,body="scheduled message 8AM")]) except: pass try: if schedule_time==PM8: print "In 8PM"; today8pm0min = now.replace(hour=20, minute=0, second=0, microsecond=0) today8pm1min = now.replace(hour=20, minute=1, second=0, microsecond=0) print today8pm0min; print today8pm0min; if now > today8pm0min and now < today8pm1min: print "Condition matched. Sending message to " + str(user); kik.send_messages([TextMessage(to=user,chat_id=chat_id,body="scheduled message 8PM")]) except Exception as e: print (e); pass try: if schedule_time==AM10: today10am0min = now.replace(hour=10, minute=0, second=0, microsecond=0) today10am1min = now.replace(hour=10, minute=1, second=0, microsecond=0) if now > today10am0min and now < today10am1min: kik.send_messages([TextMessage(to=user,chat_id=chat_id,body="scheduled message 10AM")]) except: pass try: if schedule_time==PM10: today10pm0min = now.replace(hour=22, minute=0, second=0, microsecond=0) today10pm1min = now.replace(hour=22, minute=1, second=0, microsecond=0) if now > today10pm0min and now < today10pm1min: kik.send_messages([TextMessage(to=user,chat_id=chat_id,body="scheduled message 10PM")]) except: pass try: if schedule_time=='Default': todaydefault0 = now.replace(hour=DEFAULT_TIME, minute=0, second=0, microsecond=0) todaydefault1 = now.replace(hour=DEFAULT_TIME, minute=1, second=0, microsecond=0) if now > todaydefault0 and now < todaydefault1: kik.send_messages([TextMessage(to=user,chat_id=chat_id,body="scheduled message default")]) except: pass
import db as iddb import configure as conf from pydblite import Base import logging import datetime pp = telegram.utils.request.Request(read_timeout=20) bot = telegram.Bot(token=conf.bot_token, request=pp, base_url=conf.base_url) updater = telegram.ext.Updater(bot=bot, use_context=True) log = logging.RootLogger(level=logging.DEBUG) logging.basicConfig(level=logging.WARNING) db = Base('bind.pdl') if not db.exists(): db.create('tg_user_id', 'meter_id') else: db.open() def handle_bind_event(update, context): log.info("Handle bind event.") uid = update.effective_user.id mids = "" for mid in context.args: if [i for i in ((db('tg_user_id') == uid) & (db('meter_id') == mid)) ] == []: db.insert(uid, mid) mids += '<pre>' + str(mid) + '</pre>\n' log.info("Insert new bind record:{} - {}".format(uid, mid))
from datetime import datetime from pathlib import Path from pydblite import Base if not Path('db').exists(): Path('db').mkdir() client = Base('db/client.pdl') if client.exists(): client.open() else: client.create('secret', 'redirect_uri', 'name') authorization_code = Base('db/authorization_code.pdl') if authorization_code.exists(): authorization_code.open() else: authorization_code.create('user_id', 'code', 'expire_time') token = Base('db/token.pdl') if token.exists(): token.open() else: token.create('user_id', 'access', 'expire_time', 'refresh') user = Base('db/user.pdl') if user.exists(): user.open() else: user.create('login', 'password_hash', 'name', 'email', 'phone')
from pathlib import Path from pydblite import Base if not Path('db').exists(): Path('db').mkdir() client = Base('db/client.pdl') if client.exists(): client.open() else: client.create('secret', 'redirect_uri', 'name') authorization_code = Base('db/authorization_code.pdl') if authorization_code.exists(): authorization_code.open() else: authorization_code.create('user_id', 'code', 'expire_time') token = Base('db/token.pdl') if token.exists(): token.open() else: token.create('user_id', 'access', 'expire_time', 'refresh') user = Base('db/user.pdl') if user.exists(): user.open() else: user.create('login', 'password_hash', 'name', 'email', 'phone') clothes = Base('db/clothes.pdl')
def open_db(self, db_name): _db = Base(manager.app_dir.joinpath('db', f'{db_name}.pdl')) if _db.exists(): return _db.open() raise TVShowsDBError(f'Couldn\'t connect to DB: {db_name}')
from pydblite import Base from pathlib import Path if not Path('database').exists(): Path('database').mkdir() client = Base('database/client.pdl') if client.exists(): client.open() else: client.create('client_id', 'client_secret', 'redirect_uri') auth_code = Base('database/auth_code.pdl') if auth_code.exists(): auth_code.open() else: auth_code.create('user_id', 'code', 'expired') token = Base('database/token.pdl') if token.exists(): token.open() else: token.create('user_id', 'access', 'expired', 'refresh') user = Base('database/user.pdl') if user.exists(): user.open() else: user.create('login', 'password', 'name', 'email', 'phone') item = Base('database/item1.pdl')
from pathlib import Path from pydblite import Base if not Path('db').exists(): Path('db').mkdir() """ Base for client's application on service via client_id """ client_base = Base('db/client_base.pdl') if client_base.exists(): client_base.open() else: client_base.create('secret', 'redirect_uri', 'name') """ Base for keeping authorization codes while oauth """ authorization_code = Base('db/authorization_code.pdl') if authorization_code.exists(): authorization_code.open() else: authorization_code.create('user_id', 'code', 'expire_time') """ Base for access_tokens for authorized users """ access_token = Base('db/access_token.pdl') if access_token.exists(): access_token.open() else: access_token.create('user_id', 'access', 'expire_time', 'refresh') """
from pathlib import Path from pydblite import Base if not Path("db").exists(): Path("db").mkdir() """ Base for client's application on service via client_id """ client_base = Base("db/client_base.pdl") if client_base.exists(): client_base.open() else: client_base.create("secret", "redirect_uri", "name") """ Base for keeping authorization codes while oauth """ authorization_code = Base("db/authorization_code.pdl") if authorization_code.exists(): authorization_code.open() else: authorization_code.create("user_id", "code", "expire_time") """ Base for access_tokens for authorized users """ access_token = Base("db/access_token.pdl") if access_token.exists(): access_token.open() else: access_token.create("user_id", "access", "expire_time", "refresh") """
class DataBase(): def __init__(self, mode): self.db = Base("./mlfv_hosts.pdl") self.db.create('ip', 'port', 'libs', 'cpu', 'mem', 'net', 'runs', mode=mode) def insert_reg(self, r): if (len(r) < 6): print("Bad insertion") return False if self.db.exists(): print self.db.insert(ip=r['ip'], port=r['port'], libs=r['libs'], cpu=r['cpu'], mem=r['mem'], net=r['net'], runs=0) self.db.commit() return True def remove_reg(self, r): if self.db.exists(): for i in self.db: if i['ip'] == r['ip'] and i['port'] == r['port']: self.db.delete(i) self.db.commit() print str(i['ip']) + " removed\n" return True return False def print_all(self): if self.db.exists(): for r in self.db: print r def get_less_runs(self): if self.db.exists(): less_runs = sys.maxsize for r in self.db: if (r['runs'] < less_runs): new_host = r less_runs = r['runs'] new_host['runs'] += 1 self.db.commit() return new_host def get_hosts_cpu_mem(self, cpu, mem): print "cpu", cpu, mem ret = [] if self.db.exists(): for r in (self.db("cpu") >= cpu) & (self.db("mem") >= mem): ret.append([ r['ip'], r['port'], r['libs'], r['cpu'], r['mem'], r['net'] ]) #ret = [r for r in self.db if r['cpu'] >= cpu and r['mem'] >= mem] return ret else: print("Error: no client!") return None def get_registers_values(self): if self.db.exists(): l = [] for r in self.db: l.append([ r['ip'], r['port'], r['libs'], r['cpu'], r['mem'], r['net'] ]) a = np.array(l, dtype=object) return a