async def daily(ctx): user = ctx.message.author db = TinyDB(path_db) usr = Query() docs = db.search(usr['ids'] == user.id) trs = [1000, 150, 150, 200, 100, 50, 250, 500, 200, 200, 150, 100, 69420] for doc in docs: tim = doc['daily'] if time.time() - tim > 86400: tr = random.choice(trs) embed = discord.Embed(title='**Tear Dispenser**', description=f'You cried {tr} tears.\n\ Storing them in the vaults of tears.Spend them wisely...💦\nSpend them wisely...', colour=discord.Color.blue()) embed.set_footer(text='😭') await ctx.send(embed=embed) if tr == 69420: await ctx.send('Damn, that\'s some epic sad-ness...') for doc in docs: doc['credits'] += tr doc['daily'] = time.time() db.write_back(docs) else: embed = discord.Embed( title='**Tear Dispenser**', description=f"You can't cry rn. Let your eyes hydrate.\n\ Wait for like {round((86400 - time.time()+tim)//3600)} hours or something.", colour=discord.Color.blue()) embed.set_footer(text='😭') await ctx.send(embed=embed)
def new_entry(): print('-------------------------------------------------------------') print('Opening database for new entry...') name = input('Enter name of student-') nof = input("Enter name of student's father-") nom = input("Enter name of student's mother-") dob = input('Enter Date Of Birth of the student-') y = input('Enter Year of Birth...') bg = input("Enter student's Blood-group-") cl = input('Enter class in which the student is being enrolled in. -') ph = input('Enter phone no.-') doa = datetime.date(datetime.now()) ids = customid() admn = entryno() + 1 db = TinyDB(path) now = datetime.now() age = now.year - int(y) try: db.insert({'Admission no.':admn,'Name':name,'id':ids,"Father's Name":nof,"Mother's Name":nom,\ 'Date of Birth':dob,'Age':age,'Blood Group':bg,'Class':cl,'Date of Admission':str(doa),'Phone':ph}) print('Added the student to database...') except Exception as e: print('A minor error occured...') print(e) db2 = TinyDB(path2) m = Query() if not db2.contains(m['Admission no.']): db2.insert({'Admission no.': 0}) else: docs = db2.search(m['Admission no.']) for doc in docs: doc['Admission no.'] += 1 db2.write_back(docs)
async def level_up(user, channel): db = TinyDB(path_db) usr = Query() docs = db.search(usr['ids'] == user.id) for doc in docs: lvl_start = doc['level'] experience = doc['experience'] x = 0 cnt = 0 while (x < experience): cnt += 1 x = (x * 2) + 10 if (experience == x): lvl_end = cnt else: lvl_end = lvl_start if lvl_start < lvl_end: for doc in docs: doc['level'] = lvl_end db.write_back(docs) db = TinyDB(path_db) usr = Query() docs = db.search(usr['ids'] == user.id) ls = lvl_end * 150 for doc in docs: doc['credits'] += ls db.write_back(docs) embed = discord.Embed( title=f'{user} has leveled up to {lvl_end}.', description=f'You have been given {ls} tears for your active-ness.\n\ Saving {ls} tears in your vault of tears.', color=discord.Color.teal()) embed.set_footer(text='😭') await channel.send(embed=embed)
class ForexNotifyDB: def __init__(self, db_path="notify.json"): self.__db = TinyDB(db_path) self.__query = Query() def __del__(self): self.__db.close() def add(self, user_id, cond, price): """ Insert/Update record to database :param user_id: :param cond(str): Currency to notify. :param price(float): Currency price to notify. :return Return True if insert or update record successfully """ if type(price) is not float or type(cond) is not str: print("Cannot insert the record to database") return False if self.__db is None: print("Database cannot be opened.") return False query_data = self.__db.search((self.__query.user_id == user_id) & (self.__query.cond == cond)) if len(query_data) > 0: for data in query_data: data["price"] = price self.__db.write_back(query_data) else: self.__db.insert({ "user_id": user_id, "cond": cond, "price": price }) return True def remove(self, user_id, cond): """ Remove record from opened database. :param user_id :param cond(str): Currency to notify. """ if self.__db is not None: self.__db.remove((self.__query.user_id == user_id) & (self.__query.cond == cond)) def get_all_data(self): """ Return all records from opened database. """ if self.__db is not None: return self.__db.all() return None def clear(self): for record in self.get_all_data(): self.__db.remove(self.__query.user_id == record["user_id"])
async def rep(ctx, member: discord.Member): user = ctx.message.author if member in ctx.message.guild.members: await update_data(member) user2 = member.id if user.id != user2: db = TinyDB(path_db) usr = Query() docs = db.search(usr['ids'] == user.id) for doc in docs: tim = doc['reptime'] if time.time() - tim > 3600: for doc in docs: doc['reptime'] = time.time() db.write_back(docs) embed = discord.Embed( title='**Successful**', description= f'{user} has successfully repped {member.mention}\n⬆️', colour=discord.Color.green()) embed.set_footer( text='Cry, cry, let the emotions flow through you...😭') await ctx.send(embed=embed) db = TinyDB(path_db) usr = Query() docs2 = db.search(usr['ids'] == user2) for do in docs2: do['rep'] += 1 db.write_back(docs2) else: embed = discord.Embed(title='**Unsuccessful!!!**', description=f"You can't rep rn.\n\ Wait for like {round((3600 - time.time()+tim)//60)} minutes or something.", colour=discord.Color.red()) embed.set_footer( text='Cry, cry, let the emotions flow through you...😭') await ctx.send(embed=embed) else: embed = discord.Embed( title='**Unsuccessful!!!**', description='You can\'t rep yourself, ya dummy...', colour=discord.Color.red()) embed.set_footer( text='Cry, cry, let the emotions flow through you...😭') await ctx.send(embed=embed) else: embed = discord.Embed( title='**Unsuccessful!!!**', description='Mention someone that exists, ya dummy...', colour=discord.Color.red()) embed.set_footer( text='Cry, cry, let the emotions flow through you...😭') await ctx.send(embed=embed)
async def transfer(ctx, amount, member: discord.Member): user = ctx.message.author if amount.isdigit(): amount = int(amount) if amount == 'all': db = TinyDB(path_db) usr = Query() docs = db.search(usr['ids'] == user.id) for doc in docs: trp = doc['credits'] amount = trp if member in ctx.message.guild.members: await update_data(member) user2 = member.id db = TinyDB(path_db) usr = Query() docs = db.search(usr['ids'] == user.id) for i in docs: bal = i['credits'] if bal > amount: for doc in docs: doc['credits'] -= amount db.write_back(docs) db = TinyDB(path_db) usr = Query() docs2 = db.search(usr['ids'] == user2) for doc2 in docs2: doc2['credits'] += amount db.write_back(docs2) embed = discord.Embed( title='**Succesful Transaction!!!**', description= f'{amount} tears have been transferred to {member}...', colour=discord.Color.blurple()) embed.set_footer( text='Cry, cry, let the emotions flow through you...😭') await ctx.send(embed=embed) else: embed = discord.Embed( title='**Unsuccesful Transaction!!!**', description='Insufficient tears. Cry more,,,,,:/', colour=discord.Color.blurple()) embed.set_footer( text='Cry, cry, let the emotions flow through you...😭') await ctx.send(embed=embed) else: embed = discord.Embed( title='**Unsuccesful Transaction!!!**', description='Mention someone that exists, ya dummy...', colour=discord.Color.blurple()) embed.set_footer( text='Cry, cry, let the emotions flow through you...😭') await ctx.send(embed=embed)
async def add_experience(message, user, exp): db = TinyDB(path_db) usr = Query() docs = db.search(usr['ids'] == user.id) try: for doc in docs: doc['experience'] += exp db.write_back(docs) except: return await level_up(message.author, message.channel)
def sendLoanPayment(_self, _loanTransactionID, _amount): db = TinyDB('./pendingLoanTransaction.db') loan = Query() try: checkUserLoan = db.search(loan.loanRequestID == _loanTransactionID) newBalance = checkUserLoan['borrowerParsedAmountOfLoan'] - _amount checkUserLoan['borrowerParsedAmountOfLoan'] = newBalance db.write_back(checkUserLoan) return ({'status': True}) except: return ({'status': False})
def addNewWalletBalance(_self, _newBalance): new_port = newPort date = datetime.now() timeStamp = date.strftime("%c") db = TinyDB(f"./databaseTransaction/{new_port}walletBalance.db") transactions = Query() allBalance = db.all() _newBalances = ast.literal_eval(_newBalance) _newAddresses = [] _currentAddresses = [] for address in _newBalances: if 'senderAddress' in address: _newAddresses.append(address['senderAddress']) for address in allBalance: if 'senderAddress' in address: _currentAddresses.append(address['senderAddress']) existingAddress = (set(_newAddresses) & set(_currentAddresses)) strExistingAddress = str(existingAddress) strExisting = strExistingAddress[1:-1] for balance in _newBalances: if allBalance != []: for oldBalance in allBalance: balanceData = db.search( transactions.senderAddress == balance['senderAddress']) search = db.search( transactions.senderAddress == balance['senderAddress']) if len(balance) > 0 and search != []: lastSearch = search[-1] lastSearchDict = lastSearch['balance'] lastSearch['balance'] = float(balance['balance']) db.write_back(search) else: print('newTrans on else') db.insert(balance) else: print('newTransaction') db.insert(balance)
def add_gradient(db: TinyDB): logging.debug("Calculate Gradient for items") gradients = [] items = db.search(~where('gradient-s').exists()) for item in items: logging.debug("#: {}\tdoc id: {}".format(len(gradients), item.doc_id)) data_object = DataObject.from_dict(item) data = data_object.data['cgmValue'].values d1 = data[630:690] d2 = data[635:695] max_gradient = max(d2 - d1) logging.debug("max gradient {}".format(max_gradient)) item['gradient-s'] = max_gradient gradients.append(max_gradient) db.write_back([item]) db.storage.flush() logging.info("Added gradient to {} items".format(len(items)))
async def cry(ctx): user = ctx.message.author db = TinyDB(path_db) usr = Query() docs = db.search(usr['ids'] == user.id) trs = [5, 5, 10, 10, 10, 10, 20, 25, 69, 0] for doc in docs: tim = doc['hourly'] if time.time() - tim > 3600: tr = random.choice(trs) if tr > 1: embed = discord.Embed(title='**Timely Tears**', description=f'You cried {tr} tears.\n\ Storing them in the vaults of tears.Spend them wisely...💦\nSpend them wisely...', colour=discord.Color.blue()) embed.set_footer(text='😭') await ctx.send(embed=embed) else: tr2 = [ 'You were not sad', 'You were surprisingly too happy to cry', 'You cried so much already that the tears are not coming out', 'You really tried but you could not cry', 'The tears are not coming out...' ] l = random.choice(tr2) embed = discord.Embed(title='**Tear Dispenser**', description=f"You can't cry rn.{l}", colour=discord.Color.blue()) embed.set_footer(text='😭') embed.add_field(name=f'Try again after like an hour.', value='oof', inline=False) await ctx.send(embed=embed) for doc in docs: doc['credits'] += tr doc['hourly'] = time.time() db.write_back(docs) else: embed = discord.Embed( title='**Tear Dispenser**', description=f"You can't cry rn. Let your eyes hydrate.\n\ Wait for like {round((3600 - time.time()+tim)//60)} minutes or something.", colour=discord.Color.blue()) embed.set_footer(text='😭') await ctx.send(embed=embed)
def get_users_tickets(): db = TinyDB(data_file) tickets = [] docs = db.all() for q in docs: q = Munch(q) ticket = Munch(get_tickets(q.plate, q.serial)) if ticket and ticket.status == 0 and ticket.adeudo: if len(ticket.adeudo) != q.ticketcount: q.notified = False if not notified and q.update: for bill in ticket.adeudo: bill = Munch(bill) #{'baselegal': , 'cantidad': 1, 'concepto': 44229, 'descripcion': , 'periodo': 2018, 'porcentaje': 100.0, 'idrow': 1, 'fechavenc': '', 'Folio': '113|272542007', 'Fecha': 1528952400000, 'numReq': 0, 'Tipo': 'M', 'ImpBase': 806.0, 'Beneficiario': 71947, 'Importe': 806.0, 'FotoInf': ''} tickets.append({ 'description': bill.descripcion, 'percentage': bill.porcentaje, 'number': bill.Folio, 'type': bill.Tipo, 'tax': bill.ImpBase, 'due': bill.Importe }) from sparkpost import SparkPost sparky = SparkPost(os.environ['SPARKPOST_API_KEY']) response = sparky.transmissions.send( use_sandbox=True, recipients=[q.email], html= '<html><body><p>Se encontraron nuevas multas, puedes consultarlas en la página oficial de la Secretaría de Movilidad.</p></body></html>', from_email='*****@*****.**', subject='Se ha encontrado una nueva multa') q.notified = True q.ticketcount = len(ticket.adeudo) db.write_back(docs) return tickets
def test_cutom_mapping_type_with_json(tmpdir): from tinydb.database import Mapping class CustomDocument(Mapping): def __init__(self, data): self.data = data def __getitem__(self, key): return self.data[key] def __iter__(self): return iter(self.data) def __len__(self): return len(self.data) # Insert db = TinyDB(str(tmpdir.join('test.db'))) db.purge() db.insert(CustomDocument({'int': 1, 'char': 'a'})) assert db.count(where('int') == 1) == 1 # Insert multiple db.insert_multiple([ CustomDocument({ 'int': 2, 'char': 'a' }), CustomDocument({ 'int': 3, 'char': 'a' }) ]) assert db.count(where('int') == 1) == 1 assert db.count(where('int') == 2) == 1 assert db.count(where('int') == 3) == 1 # Write back doc_id = db.get(where('int') == 3).doc_id db.write_back([CustomDocument({'int': 4, 'char': 'a'})], [doc_id]) assert db.count(where('int') == 3) == 0 assert db.count(where('int') == 4) == 1
def test_cutom_mapping_type_with_json(tmpdir): from tinydb.database import Mapping class CustomDocument(Mapping): def __init__(self, data): self.data = data def __getitem__(self, key): return self.data[key] def __iter__(self): return iter(self.data) def __len__(self): return len(self.data) # Insert db = TinyDB(str(tmpdir.join('test.db'))) db.purge() db.insert(CustomDocument({'int': 1, 'char': 'a'})) assert db.count(where('int') == 1) == 1 # Insert multiple db.insert_multiple([ CustomDocument({'int': 2, 'char': 'a'}), CustomDocument({'int': 3, 'char': 'a'}) ]) assert db.count(where('int') == 1) == 1 assert db.count(where('int') == 2) == 1 assert db.count(where('int') == 3) == 1 # Write back doc_id = db.get(where('int') == 3).doc_id db.write_back([CustomDocument({'int': 4, 'char': 'a'})], [doc_id]) assert db.count(where('int') == 3) == 0 assert db.count(where('int') == 4) == 1
def main(): logger.info("Start Main!") create_plots: bool = False # Select True if you want a plot for every prediction window # SET USER DATA user_data = UserData(bginitial=100.0, cratio=5, idur=4, inputeeffect=None, sensf=41, simlength=13, predictionlength=180, stats=None) # Get Database db = TinyDB(db_path, storage=CachingMiddleware(JSONStorage)) logging.info("length of db: {}".format(len(db))) logging.info("Valid examples: {}".format( len(db.search(where('valid') == True)))) logging.info("With result: {}".format( len(db.search(where('result').exists())))) with_result = db.search(where('result').exists()) lstm_result = list( filter( lambda x: any( list(map(lambda y: "LSTM" in y['predictor'], x['result']))), with_result)) logging.info("lstm res {}".format(len(lstm_result))) lstm_cleaned = list(map(clean_lstm, lstm_result)) db.write_back(lstm_cleaned) db.storage.flush() with_result = db.search(where('result').exists()) lstm_result = list( filter( lambda x: any( list(map(lambda y: "LSTM" in y['predictor'], x['result']))), with_result)) logging.info("lstm res {}".format(len(lstm_result))) exit() all = db.search(where('valid') == True) s = pd.Series(list(map(lambda x: x['id'], all))) list( map( lambda id: print("id {} has {} items".format( id, len(list(filter(lambda x: x['id'] == id, all))))), s.unique())) exit() get_arima_order_summary(db) exit() with_result = db.search(where('result').exists()) arima_result = list( filter( lambda x: any( list( map(lambda y: y['predictor'] == 'Arima Predictor', x[ 'result']))), with_result)) logging.info("arima results: {}".format(len(arima_result))) logging.info("length of db: {}".format(len(db))) #all = db.all() outliers = list( filter( lambda item: any( list( map(lambda result: abs(result['errors'][0]) > 100, item[ 'result']))), with_result)) logging.info("number of outliers: {}".format(len(outliers))) list(map(plot, outliers)) exit() logging.info("results with optimizer: {} ".format( len( list( filter( lambda x: any( list( map(lambda y: 'Optimizer' in y['predictor'], x[ 'result']))), with_result))))) for item in with_result: item['result'] = list( filter(lambda x: 'Optimizer' not in x['predictor'], item['result'])) db.write_back([item]) db.storage.flush() exit() logging.info("with result: {}".format(len(with_result))) results = list(map(lambda x: x['result'], with_result)) seven = list(filter(lambda x: len(x) == 7, results)) logging.info("with 7 results: {}".format(len(seven))) le = pd.Series(list(map(len, with_result))) logging.info(le.describe()) exit() # Filter Items with LSMT Result lstm_result = list(filter(check_lstm, with_result)) logging.info("number of results with lstm {}".format(len(lstm_result))) for item in lstm_result: item['result'] = list( filter(lambda x: x['predictor'] != 'LSTM Predictor', item['result'])) db.write_back([item]) db.storage.flush() exit() # with_result = db.search(where('doc_id') in list(range(19650, 19700))) # res = list(map(lambda x: db.get(doc_id=x),range(19600,19700))) # res = list(filter(lambda x: (x is not None), res)) # logging.debug(len(res)) #list(map(plot, res)) # doc_ids = list(map(lambda x: x.doc_id, res)) # db.remove(doc_ids=doc_ids) # db.storage.flush() # logging.info("length of db: {}".format(len(db))) # exit() # exit() outliers = list( filter(lambda x: x['result'][0]['errors'][0] > 75, with_result)) logging.debug(len(outliers)) # logging.debug(doc_ids) list(map(plot, outliers)) logging.info('end') exit() for item in db: print(item) for x in item: print(x) exit()
def edit(): print('Welcome to the data editor menu...') print('First you must select the student whose data you want to edit') print('-------------------------------------------------------------') print( 'Do you have an argument by which you can search for a particular student?' ) inp = input('If you want to search by Name, press1\n\ If you want to search by Admission No., press2\n\ If you want to search by id, press3\n\ If you want to search by Name of Father, press4\n\ If you want to search by Name of Mother, press5\n\ If you want to search by Phone no., press6\n\ If you want to return to previous menu, press7.\n>') db = TinyDB(path) usr = Query() inp = int(inp) print('-------------------------------------------------------------') if inp == 1: nm = input('Enter name of Student-') try: docs = db.search(usr['Name'] == nm.lower()) for doc in docs: print(json.dumps(doc, sort_keys=True, indent=4)) except: print('Argument not found in database') return elif inp == 2: inu = int(input('Enter Admission No. of Student-')) try: docs = db.search(usr['Admission no.'] == inu) for doc in docs: print(json.dumps(doc, sort_keys=True, indent=4)) except: print('Argument not found in database') return elif inp == 3: id = input("Enter Student's ID-") try: docs = db.search(usr['id'] == id) for doc in docs: print(json.dumps(doc, sort_keys=True, indent=4)) except: print('Argument not found in database') return elif inp == 4: nof = input("Enter name of Student's Father-") try: docs = db.search(usr["Father's Name"] == nof.lower()) for doc in docs: print(json.dumps(doc, sort_keys=True, indent=4)) except: print('Argument not found in database') return elif inp == 5: nom = input("Enter name of Student's Mother-") try: docs = db.search(usr["Mother's Name"] == nom) for doc in docs: print(json.dumps(doc, sort_keys=True, indent=4)) except: print('Argument not found in database') return elif inp == 6: phon = input('Enter Phone No.-') try: docs = db.search(usr['Phone'] == phon) for doc in docs: print(json.dumps(doc, sort_keys=True, indent=4)) except: print('Argument not found in database') return elif inp == 7: interface() else: print('Please enter a valid no.\nTerminating...') edit() if len(docs) > 1: print( 'There are multiple Entries inside the Database refering to that person\'s details' ) print( 'Consider Identifying the student from the data given above or using other parameters.\n\ If you find them, notice their ID and TYPE it in to obtain access OR type "db.reload" to use different parameter' ) inp = input('>') if inp == 'db.reload': edit() else: try: db = TinyDB(path) usr = Query() docs = db.search(usr['id'] == inp) for doc in docs: print(json.dumps(doc, sort_keys=True, indent=4)) dbs = doc print( '-------------------------------------------------------------' ) print( 'Now that you have identified entry, you can begin editing it.' ) print( 'The column name will start display. Add the addition you want to make to respective entry against it\ and then press ENTER.\n*If you don\'t want to edit any specific entry, press 0' ) for i in dbs: k = input(f'{i}: ({dbs[i]})-') if k != '0': print('Editing database...') for doc in docs: doc[i] = k db.write_back(docs) except: print('Argument not found in database') return else: dbs = doc print('-------------------------------------------------------------') print('Now that you have found entry, you can begin editing it.') print( 'The column name will start display. Add the addition you want to make to respective entry\ against it and then press ENTER.\n*If you don\'t want to edit any specific entry, press 0' ) for i in dbs: k = input(f'{i}: ({dbs[i]})-') if k != '0': print('Editing database...') for doc in docs: doc[i] = k db.write_back(docs)
class Database: def __init__(self): if os.path.exists('db.json'): print('DB exists') self.db = TinyDB('db.json') else: with open('propertiesData.json') as jd: data = json.load(jd) self.db = TinyDB('db.json') for item in data: self.db.insert(item) def incrementLanded(self, landed): Space = Query() prop = self.db.search(Space.Name == landed) for doc in prop: doc['Landed'] += 1 self.db.write_back(prop) def purgeDocuments(self): self.db.purge() os.remove('db.json') def createGraph(self): name = [] timesLanded = [] costs = [] rents = [] self.removeDataPoints() # Removes All non-properties prop = self.db.all() for doc in prop: name.append(doc['Name']) timesLanded.append(doc['Landed']) costs.append(doc['Cost']) rents.append(doc['Rent']) timesLanded = self.convertToPercent(timesLanded) _graph = Graph(name, timesLanded, costs, rents) #_graph.barChart() _graph.lineChart() def convertToPercent(self, values): timesLanded = [] total = 0 for value in values: total += value for num in values: percent = (num / total) * 100 timesLanded.append(round(percent, 2)) return timesLanded def removeDataPoints(self): Place = Query() self.db.remove(Place.Name == 'Chance') self.db.remove(Place.Name == 'GO') self.db.remove(Place.Name == 'Community Chest') self.db.remove(Place.Name == 'Jail') self.db.remove(Place.Name == 'Free Parking') self.db.remove(Place.Name == 'GO TO Jail') self.db.remove(Place.Name == 'Income Tax') self.db.remove(Place.Name == 'Luxury Tax')
from datetime import datetime import pytz from tinydb import TinyDB, Query db = TinyDB('event_db.json') local_tz = pytz.utc records = db.all() for entry in records: timestamp = int( pytz.utc.localize( datetime.strptime(entry['Timestamp'], '%Y-%m-%d %H:%M:%S')).timestamp()) entry['timestamp'] = timestamp db.write_back(records)
#Variavéis que irão conter o caminhos para as imagens media = [] #Iterar pelos itens não postados for post in nao_foi_postado: #Retornar apenas os 10 primeiros itens if (index <= 5): if (post['source'] == 'Aleteia'): #caso dê algum erro com este item o robô pula para o próximo try: media_post = {} spider_aleteia(post['link'], index) titles = db.search(Query().titulo == post['titulo']) for title in titles: title['postado'] = 'sim' db.write_back(titles) print('Criando postagem Aleteia...') create_post(str('Images/imagem-' + str(index) + '.jpg'), post['titulo'], post['source'], str(index)) print('Postagem Aleteia Criada') #create_stories(str('Images/stories-' + str(index) + '.jpg'), post['titulo'], post['source'], str(index)) #upload_stories('Images/stories-'+str(index)+'.jpg') media_post['type'] = 'photo' media_post['file'] = 'Images/post-' + str(index) + '.jpg' media.append(media_post) captionText = captionText + str( '\u2063\n' + "-------" + '\u2063\n' + str(index) + '-Postado por ' + post['source'] + '\u2063\n' + " " + '\u2063\n' + post['titulo'] + '\u2063\n' + " " + '\u2063\n' + 'Link: ' + post['link'] + '\u2063\n') index = index + 1
def rolling(db: TinyDB, user_data: UserData): check_directories() predictionWindow = PredictionWindow() results = [] prediction_carb_optimized = [] i = 0 loop_start = datetime.now() # create random iterator over valid items without a result elements = db.search(where('result').exists() & (where('valid') == True)) elements = list(filter(check_time, elements)) #elements = list(filter(lambda x: any(list(map(lambda y: abs(y['errors'][0]) > 70, x['result']))), elements)) random.shuffle(elements) logging.info("number of unprocessed items {}".format(len(elements))) last_save = 0 for item in elements: # Break out of loop if enough results or it takes too long if len(results) >= config.PREDICTION_CONFIG['max_number_of_results'] or \ (datetime.now() - loop_start).seconds > config.PREDICTION_CONFIG['runtime_in_minutes'] * 60: break logger.info("#:{} \t #R:{}\tdoc_id: {}".format(i, len(results), item.doc_id)) # Get element data_object = DataObject.from_dict(item) predictionWindow.startTime = data_object.start_time logger.info(predictionWindow.startTime.isoformat()) predictionWindow.endTime = data_object.end_time # select data for this window predictionWindow.data = data_object.data predictionWindow.data_long = data_object.data_long predictionWindow.events = pd.concat([ data_object.carb_events, data_object.basal_events, data_object.bolus_events ]) predictionWindow.userData = user_data predictionWindow.plot = config.PREDICTION_CONFIG['create_plots'] predictionWindow.features_90 = data_object.features_90 # prediction_carb_optimized.append(checkOptimizer.check(predictionWindow)) if checkData.check_window(predictionWindow.data, user_data): # Call to Predictors res, order, features = check.check_and_plot(predictionWindow, item) # Write result back into db if res is not None: results.append(res) if 'result' in item: item['result'] = item['result'] + res db.write_back([item]) else: db.update({'result': res}, doc_ids=[item.doc_id]) if features is not None: if 'features-90' in item: item['features-90'] = features db.write_back([item]) else: db.update({'features-90': features}, doc_ids=[item.doc_id]) if order is not None: if 'features' in item: item['features'] = order db.write_back([item]) else: db.update({'features': order}, doc_ids=[item.doc_id]) if len(results) > 10 + last_save: last_save = len(results) db.storage.flush() db.storage.flush() logger.info("length of result {}".format(len(results))) # save all prediction carb optimized values to a json file to_file(prediction_carb_optimized) return results
def addValidatedTransaction(self, _transaction, _senderAddress, _recipientAddress, _balance, _totalAmount, _amount): new_port = newPort db2 = TinyDB(f"./databaseTransaction/{new_port}walletBalance.db") db3 = TinyDB(f"./databaseTransaction/{new_port}blockChain.db") db = TinyDB(f"./databaseTransaction/{new_port}pendingTransaction.db") date = datetime.now() timeStamp = date.strftime("%c") transaction = Query() userBalance = db2.search(transaction.senderAddress == _senderAddress) receiverBalance = db2.search( transaction.senderAddress == _recipientAddress) if userBalance != [] or len(userBalance) > 1: userBalanceDict = userBalance[-1] if receiverBalance != [] or len(receiverBalance) > 1: receiverBalanceDict = receiverBalance[-1] userBalanceDict['balance'] = float( userBalanceDict['balance']) - float(_totalAmount) receiverBalanceDict['balance'] = float( receiverBalanceDict['balance']) + float(_amount) # print(receiverNewBalance) db2.write_back(userBalance) db.insert({ 'timeStamp': timeStamp, 'transaction': _transaction }) data2 = str("0") startingHash = sha256(data2.encode('utf8')).hexdigest() db3.insert({ 'index': 0, 'previousHash': str(startingHash), 'currentBlock': "" }) db2.write_back(receiverBalance) return jsonify({'status': 'pending transaction'}) else: db2.insert({ 'senderAddress': _senderAddress, 'balance': _balance, 'timeStamp': timeStamp }) db2.insert({ 'senderAddress': _recipientAddress, 'balance': _amount, 'timeStamp': timeStamp }) db.insert({ 'timeStamp': timeStamp, 'transaction': _transaction }) return jsonify({'status': 'pending new transaction '}) else: db2.insert({ 'senderAddress': _senderAddress, 'balance': _balance, 'timeStamp': timeStamp }) db2.insert({ 'senderAddress': _recipientAddress, 'balance': _amount, 'timeStamp': timeStamp }) db.insert({'timeStamp': timeStamp, 'transaction': _transaction}) return jsonify({'status': 'invalid'})
class DBwrapper(object): def __init__(self, filename): self.db = TinyDB(filename) def getFileAtPath(self, path): File = Query() file = self.db.get(File.path == path) return file def pathExist(self, path): File = Query() exists = self.db.contains(File.path == path) return exists def getFilesInFolder(self, path): Files = Query() files = self.db.search(Files.parent == path) return files def addFile(self, path, info, chunks=[]): parent = ''.join(path[1:].split('/')[:-1]) document = { 'path': path, 'parent': parent, 'info': info, "chunks": chunks } if not self.pathExist(path): self.db.insert(document) return True else: return False def removeChunks(self, path): file = self.getFileAtPath(path) file['chunks'] = [] self.db.write_back([file]) return True def addChunk(self, path, chunk): file = self.getFileAtPath(path) file['chunks'].append(chunk) self.db.write_back([file]) return True def getChunks(self, path): file = self.getFileAtPath(path) return file['chunks'] def getInfo(self, path): file = self.getFileAtPath(path) return file['info'] def setInfo(self, path, info): file = self.getFileAtPath(path) file['info'] = info self.db.write_back([file]) return True def remove(self, path): file = self.getFileAtPath(path) self.db.remove(where('path') == path) return True
class MemeChainDB(object): def __init__(self, db_path): self._db = TinyDB(db_path) def add_meme(self, ipfs_id, hashlink, txid, block, imgformat, author, status): self._db.insert({ "ipfs_id": ipfs_id, "hashlink": hashlink, "txid": txid, "block": block, "imgformat": imgformat, "author": author, "status": status }) def remove_meme(self, ipfs_id): self._db.remove(Query().ipfs_id == ipfs_id) def get_memechain_height(self): return len(self._db) def update_meme(self, ipfs_id, block): memes = self._db.search(Query().ipfs_id == ipfs_id) print(memes) for meme in memes: meme["block"] = block meme["status"] = "confirm" self._db.write_back(memes) def search_by_block(self, block): """ Get a meme entry using block number as the search parameter block - Int """ return self._db.search(Query().block == block) def search_by_memechain_height(self, height): """ Get a meme entry using height as the search parameter height - Float (0 is genesis) """ if int(height) == 0: return None else: if int(height) > self.get_memechain_height(): return None else: return dict({'meme_height': int(height)}, **Index(self._db)[int(height) - 1]) def search_by_ipfs_id(self, ipfs_id): """ Get a meme entry using its IPFS ID as the search parameter ipfs_id - String """ try: return dict( {'meme_height': self.get_meme_height_by_ipfs_id(ipfs_id)}, **self._db.get(Query().ipfs_id == ipfs_id)) except ValueError as e: return self._db.get(Query().ipfs_id == ipfs_id) def search_by_txid(self, txid): """ Get a meme entry using its Kekcoin txid as the search parameter txid - String """ return self._db.get(Query().txid == txid) def search_by_author(self, author): """ Get a meme entry using its Kekcoin author as the search parameter txid - String """ return self._db.search(Query().author == author) def get_prev_block_memes(self): """ Get an array with the memes in the previous kekcoin block """ memechain_height = self.get_memechain_height() if memechain_height == 0: return None else: last_meme = self.search_by_memechain_height(memechain_height) return self._db.search(Query().block == last_meme["block"]) def get_last_meme(self): return Index(self._db)[-1] def get_all_memes(self): return self._db.search(Query().ipfs_id.exists()) def get_meme_height_by_ipfs_id(self, ipfs_id): return Index(self._db).return_index( self._db.get(Query().ipfs_id == ipfs_id)) + 1