class TinyDatabase: def __init__( self, table_name="database", ): self.table_name = table_name self.db = TinyDB(settings.CLI_CONFIG_FILE) self.table = self.db.table(self.table_name) def select(self, id: str) -> TinyDatabaseData: document = self.table.get(Query().id == id) return document def insert(self, data: BaseModel) -> int: doc_id = self.table.insert(data.dict()) return doc_id def update(self, id: str, data: BaseModel) -> List[int]: doc_id_list = self.table.upsert(data.dict(), Query().id == id) return doc_id_list def delete(self, id: str) -> bool: doc_id = self.table.remove(Query().id == id) if not doc_id: return False return True def drop(self): self.db.purge_table(self.table_name)
def insertDailyReport(table_name, contents): db = TinyDB('dailyReport.json') table = db.table(table_name) if len(table.all()): db.purge_table(table_name) table = db.table(table_name) table.insert(contents)
def doOperation(symbol, symbolIndex, symbolsCounter): codalRawDataDB = TinyDB(FilenameManager.get({'enum':FilenameManager.CodalRawData,'symbol':symbol['sy']})) soratMaliTableDataTable = codalRawDataDB.table('SoratHayeMali') soratMaliTableData = soratMaliTableDataTable.all() soratMaliTableDataLen = len(soratMaliTableData) CodalSoratMaliSheetIdDB = TinyDB(FilenameManager.get({'enum':FilenameManager.CodalSoratMaliSheetId})) SheetIDTable = CodalSoratMaliSheetIdDB.table('SheetID') SheetIDTableData = SheetIDTable.all() if not (SheetIDTableData): SheetIDTableData=set([]) else: SheetIDTableData=set(SheetIDTableData[0]['Data']) for sorateMali in soratMaliTableData: sheetIds = sorateMali['theSubjectRecord'] for sheetId in sheetIds: SheetIDTableData.add(sheetIds[sheetId]+" "+str(sheetId)) CodalSoratMaliSheetIdDB.purge_table('SheetID') SheetIDTable = CodalSoratMaliSheetIdDB.table('SheetID') listSheetIDTableData = list(SheetIDTableData) listSheetIDTableData.sort() SheetIDTable.insert({'Data':listSheetIDTableData}) CodalSoratMaliSheetIdDB.close()
class DatabaseHelper(metaclass=Singleton): def __init__(self): self._tinydb = TinyDB("UserData.json") #creates new table if it doesn't exist yet, or returns #existing or cached table def createTable(self, table_name): return self._tinydb.table(table_name) #remove table from database def removeTable(self, name: str): self._tinydb.purge_table(name) #returns list of all table names in database def getTables(self): return self._tinydb.tables() #insert new row values into a table, returns id def insert(self, table_name: str, row: dict): return self.createTable(table_name).insert(row) #get a row of values given table name, and field-value search def get(self, table_name: str, field: str, value): return self.createTable(table_name).get(where(field) == value) #update a field value based on table name def update(self, table_name: str, searchField: str, searchValue, updateField: str, updateValue): table = self.createTable(table_name) table.update({updateField: updateValue}, where(searchField) == searchValue)
def get_xueqiu_hold(cube_symbol, cube_weight): db = TinyDB('data/db_holding.json') print(cube_symbol) table = db.table(cube_symbol) db.purge_table(cube_symbol) req = urllib.request.Request( cube_hold_url + cube_symbol, headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0', 'cookie': cookie }) soup = urllib.request.urlopen(req).read().decode('utf-8') soup = BeautifulSoup(soup, 'lxml') script = soup.find('script', text=re.compile('SNB\.cubeInfo')) json_text = re.search(r'^\s*SNB\.cubeInfo\s*=\s*({.*?})\s*;\s*$', script.string, flags=re.DOTALL | re.MULTILINE).group(1) # json_text.append({'cube_symbol':cube_symbol}).append({'cube_weight':cube_weight}) data = json.loads(json_text) # data.update({'cube_symbol',cube_symbol}).update({'cube_weight',cube_weight}) # data["view_rebalancing"]["holdings"].append("cube_symbol : "+cube_symbol) # data["view_rebalancing"]["holdings"].append("cube_weight : "+cube_weight) # print(data["view_rebalancing"]["holdings"]) # data["view_rebalancing"]["holdings"] table.insert({"cube_symbol": data["view_rebalancing"]["holdings"]})
def save_results_to_tinydb(self, tinydbfname='PokeDataSim.json'): tic = time() tdb = TinyDB(tinydbfname) tdb.purge_table(self.simname) results_table = tdb.table(name=self.simname) results_table.insert_multiple(self.results) tdb.close() toc = time() self.dbg("db time: " + str(toc - tic) + 's')
def __init__(self, datapath): self.datapath = datapath db = TinyDB(self.datapath, indent=4) bwm_table = db.table(name="bwm") db.purge_table("_default") self.close(db)
def main(argv): # setup command line arguments argParser = argparse.ArgumentParser() argParser.add_argument("--insert", action="store_true") argParser.add_argument("--print", action="store_true") argParser.add_argument("--purgedb", action="store_true") argParser.add_argument("--searchname") argParser.add_argument("--searchage", type=int) args = argParser.parse_args() # init db db = TinyDB('db.json') people = db.table('people') if (args.insert): # insert new person (name, age) = getUserInput() people.insert({'name': name, 'age': age}) if (args.print): # print the contents of the people table dbSize = len(people) print("table has [{}] values".format(dbSize)) i = 0 for r in people: i = i + 1 name = r.get('name') age = r.get('age') print("{} - name: [{}] | age: [{}]".format(i, name, age)) if (args.purgedb): # purge database print("purging database...") db.purge_table('people') if (args.searchname): # search db for value Person = Query() res = people.search(Person.name == args.searchname) i = 0 for r in res: i = i + 1 name = r.get('name') age = r.get('age') print("{} - name: [{}] | age: [{}]".format(i, name, age)) if (args.searchage): # search db for people of given age Person = Query() res = people.search(Person.age == args.searchage) i = 0 for r in res: i = i + 1 name = r.get('name') age = r.get('age') print("{} - name: [{}] | age: [{}]".format(i, name, age))
def get_xueqiu_cube_list(category, count, orderby): url = cube_list_url + "?category=" + category + "&count=" + count + "&market=cn&profit=" + orderby data = request(url, cookie) jsonObj = json.loads(data.read()) db = TinyDB('data/db_cube.json') table = db.table("Cube") db.purge_table("Cube") for TopestCube in jsonObj["list"]: table.insert(TopestCube)
class ReferenceUpdater: ebi_file = '' db = '' def __init__(self): self.ebi_file = 'http://www.eibispace.de/dx/sked-b19.csv' self.db = TinyDB('reference.json') def build_schedule_table(self): self.db.purge_tables() schedule_table = self.db.table('schedule_table') with get(self.ebi_file) as reader: # with open('sked-b19.csv', encoding='latin-1') as reader: i = 0 for line in reader.text.split('\n'): # for line in reader.readlines(): # line = line.rstrip() if (i != 0): sched = line.split(';') # kHz:75;Time(UTC):93;Days:59;ITU:49;Station:201;Lng:49;Target:62;Remarks:135;P:35;Start:60;Stop:60; # 16.4;0000-2400;;NOR;JXN Marine Norway;;NEu;no;1;; # 17.2;0730-0830;24Dec;S;SAQ Grimeton;-CW;Eu;gr;6;1312;2412 # 18.2;0000-2400;;IND;VTX Indian Navy;;SAs;v;1;; schedule_table.insert({'frequency': sched[0], 'time': sched[1], 'source': sched[3], 'station': sched[4], 'target': sched[6]}) print(str(i) + ' rows inserted') i = i + 1 def build_reference_db(self): schedule_table = self.db.table('schedule_table') self.db.purge_table('stations') # DO we need to purge this? self.db.purge_table('sourcedestination') # Do we need to purge this? station_index = 0 sourcedestination_index = 0 station_table = self.db.table('stations') sourcedestination_table = self.db.table('sourcedestination') stations_box = [] sourcedestination_box = [] for schedule_row in schedule_table.all(): current_station_id = -1 if schedule_row['station'] not in stations_box: station_table.insert({'id': station_index, 'name': schedule_row['station']}) current_station_id = station_index station_index = station_index + 1 stations_box.append(schedule_row['station']) else: current_station_id = stations_box.index(schedule_row['station']) current_source_id = -1 if schedule_row['source'] not in sourcedestination_box: sourcedestination_table.insert({'id': sourcedestination_index, 'name': schedule_row['source']}) current_source_id = sourcedestination_index sourcedestination_index = sourcedestination_index + 1 sourcedestination_box.append(schedule_row['source']) else: current_source_id = sourcedestination_box.index(schedule_row['source'])
class Movie: def __init__(self,title=None,clasification=None,gender=None,rating=None,director=None,distribution=None): self.title = title self.clasification = clasification self.gender = gender self.rating = rating self.director = director self.distribution = distribution self.db = TinyDB('./db.json') def saveMovie(self): table = self.db.table('movies') table.insert({'title': self.title, 'clasification': self.clasification,'gender': self.gender, 'rating':self.rating,'director':self.director,'distribution':self.distribution}) return True def getAllMovies(self): table = self.db.table('movies') print(table.all()) def getMovie(self,index,value): if(self.searchMovie(index, value) > 0): table = self.db.table('movies') resp = table.search(where(index) == value) return resp else: return False def removeMovie(self,index,value): if(self.searchMovie(index, value) > 0): table = self.db.table('movies') table.remove(where(index) == value) return True else: return False def updateMovie(self,index,field,value,search): if(self.searchMovie(index, search)): table = self.db.table('movies') table.update({field:value}, where(index) == search) return True else: return False def purgeTable(self): self.db.purge_table('movies') def searchMovie(self,index,value): table = self.db.table('movies') return len(table.search(where(index) == value))
def init(device_id, password, owner_id, action_names): table = get_tinydb_table(path, 'device') table.upsert( { 'id': device_id, 'password': password, "owner_id": owner_id, "actions": action_names }, Query().id.exists()) db = TinyDB(path) db.purge_table('users') table = get_tinydb_table(path, 'users') table.insert({'integrity': init_integrity_data(), "id": int(owner_id)})
def getMofidAccountData(): securitySettingFilename = rootFolder + 'securitySetting.json' username = "******" + securitySettingFilename password = "******" + securitySettingFilename mofidAccountTable = "mofidAccount" try: db = TinyDB(securitySettingFilename) userTable = db.table(mofidAccountTable) user = userTable.all() username = user[0]["Username"] password = user[0]["Password"] db.close() except: print("Error in username and password Easy Trader.") db.purge_table(mofidAccountTable) userTable = db.table(mofidAccountTable) userTable.insert({"Username": username, "Password": password}) db.close() return {"username": username, "password": password}
class TinyRepository: def __init__(self, db_path, table_name): self.db_file_name = db_path self.db = TinyDB(db_path) self.table_name = table_name self.table = self.db.table(table_name) def clear(self): self.db.purge_table(self.table_name) def add(self, obj): dic = json_converter.to_dict(obj) return self.table.insert(dic) def get(self, doc_id): return json_converter.to_object(self.table.get(doc_id=doc_id)) def search(self, key, value=None, as_list=True): if isinstance(key, list): results = self.table.search( it(key[0]) == value[0] and it(key[1]) == value[1]) else: results = self.table.search( it(key)) if value is None else self.table.search( it(key) == value) if len(results) > 0: return [json_converter.to_object(r) for r in results ] if as_list else json_converter.to_object(results[0]) else: return None def update(self, obj): self.table.write_back([obj]) def delete(self, obj_id): self.table.remove(doc_ids=[obj_id]) def __del__(self): self.db.close()
class DbService: """ Class for communication with database. """ def __init__(self, datafile="data.json"): """ Instantiates DB """ self.db = TinyDB(datafile) self.add_recipes() def add_recipes(self) -> None: """ Adds recipes to database (for testing purposes) :return: """ recipes = [{ "name": "Fried Rice", "ingredients": ["rice", "onion", "garlic", "carrot", "egg", "peas", "soy sauce"] }, { "name": "Thai Street Pancakes", "ingredients": [ "wheat", "green onion", "garlic", "shrimp", "onion", "mushroom", "crab", "soy sauce", "sesame oil", "lemon juice" ] }] self.db.table('recipes').insert_multiple(recipes) def add_documents_to_table(self, tablename: str, documents: List) -> None: print("%d documents to be inserted" % (len(documents))) self.db.table(tablename).insert_multiple(documents) def purge_table(self, tablename): self.db.purge_table(tablename)
class Database(): """ A bunch of functions useful to interact with the database """ def __init__(self, databasePath): self.database = TinyDB(databasePath) def add_channel(self, channel): return self.database.table('channels').insert(channel.__dict__) def all_channels(self): list = self.database.table('channels').all() for channel in list: channel['ID'] = channel.doc_id return list def update_channel(self, id, channel): return self.database.table('channels').update(channel.__dict__, doc_ids=[id]) def remove_channel(self, id): return self.database.table('channels').remove(doc_ids=[id]) def get_channel(self, id): return self.database.table('channels').get(doc_id=id) def all_channels_sorted(self): channelsList = self.all_channels() return sorted(channelsList, key=lambda channel: channel['position']) def purge_channel_table(self): return self.database.purge_table('channels') def get_online_channels(self): list = [] for channel in self.all_channels_sorted(): if channel['checked']: list.append(channel) return list def get_offline_channels(self): list = [] for channel in self.all_channels_sorted(): if not channel['checked']: list.append(channel) return list def get_playlist_key(self): if len(self.database.table('playlist')) <= 0: # generate for the first time a key self.database.table('playlist').insert({'key': ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))}) return self.database.table('playlist').get(doc_id=1)
def test_purge_table(): db = TinyDB(storage=MemoryStorage) assert [TinyDB.DEFAULT_TABLE] == list(db.tables()) db.purge_table(TinyDB.DEFAULT_TABLE) assert [] == list(db.tables()) table_name = 'some-other-table' db = TinyDB(storage=MemoryStorage) db.table(table_name) assert set([TinyDB.DEFAULT_TABLE, table_name]) == db.tables() db.purge_table(table_name) assert set([TinyDB.DEFAULT_TABLE]) == db.tables() db.purge_table('non-existent-table-name') assert set([TinyDB.DEFAULT_TABLE]) == db.tables()
import time from smsService.ModemConnection import * from tinydb import TinyDB, Query from CommonDefs import * personal = "Personal" try: db = TinyDB(FilenameManager.get({'enum': FilenameManager.LoginData})) userTable = db.table(personal) user = userTable.all() ValidPhoneNumber = user[0]["PhoneNumber"] db.close() except: print("Error in username and password Easy Trader.") db.purge_table(personal) userTable = db.table(personal) userTable.insert({"PhoneNumber": "TypePhoneNumberHere"}) db.close() def onSmsReceived(sms): #sms.number, sms.time, sms.text #From: +98936XXX9014 #Time: 2020-01-23 08:04:11+03:30 if sms.number == ValidPhoneNumber: commands = sms.text.split('%') for command in commands: if command == 'گ': from Gold import updateGoldData goldPrice = updateGoldData() message = ""
class TinyRunDB(BaseDB): def __init__(self, conn_str): self.name = 'TinyRunDB' self.conn_str = conn_str self.default_table = 'linchpin' def _opendb(self): self.middleware = CachingMiddleware(JSONStorage) self.middleware.WRITE_CACHE_SIZE = 500 self.db = TinyDB(self.conn_str, storage=self.middleware, default_table=self.default_table) def __str__(self): if self.conn_str: return "{0} at {2}".format(self.name, self.conn_str) return "{0} at {1}".format(self.name, 'None') @property def schema(self): return self._schema @schema.setter def schema(self, schema): self._schema = dict() self._schema.update(schema) @usedb def init_table(self, table): t = self.db.table(name=table) return t.insert(self.schema) @usedb def update_record(self, table, run_id, key, value): t = self.db.table(name=table) tx_rec = t.get(eid=run_id).get("outputs", []) if len(tx_rec) > 0 and isinstance(value, list): # fetch the resources dict, index # by filtering them from outputs list res_list = [(idx, x) for idx, x in enumerate(tx_rec) if "resources" in x] if len(res_list) != 0: res_idx = res_list[0][0] resources = res_list[0][1] if "resources" in value[0]: de = defaultdict(list, resources["resources"]) for i, j in value[0]["resources"].items(): de[i].extend(j) de = {"resources": de} tx_rec[res_idx] = de return t.update(tinySet(key, [de]), eids=[run_id]) return t.update(add(key, value), eids=[run_id]) @usedb def get_tx_record(self, tx_id): t = self.db.table(name='linchpin') return t.get(eid=tx_id) @usedb def get_tx_records(self, tx_ids): txs = {} t = self.db.table(name='linchpin') for tx_id in tx_ids: txs[tx_id] = t.get(eid=tx_id) return txs @usedb def get_record(self, table, action='up', run_id=None): t = self.db.table(name=table) if not run_id: run_id = len(t.all()) if not run_id: return (None, 0) for rid in range(int(run_id), 0, -1): record = t.get(eid=int(rid)) if record and record['action'] == action: return (record, int(rid)) else: record = t.get(eid=int(run_id)) if record: return(record, int(run_id)) return (None, 0) @usedb def get_records(self, table, count=10): records = {} if table in self.db.tables(): t = self.db.table(name=table) if len(t.all()): start = len(t) if count == 'all': end = 0 else: end = start - count for i in xrange(start, end, -1): records[i] = t.get(doc_id=i) return records @usedb def get_tables(self): tables = self.db.tables() tables.remove(self.default_table) return tables def remove_record(self, table, key, value): pass def search(self, table, key=None): t = self.db.table(name=table) if key: return t.search(key) return t.all() def query(self, table, query): pass def purge(self, table=None): if table: return self.db.purge_table(table) return self.db.purge_tables() def _closedb(self): self.db.close()
def updateGroups(): requests.packages.urllib3.disable_warnings() db = TinyDB(FilenameManager.get({'enum': FilenameManager.Groups})) response = requests.get( 'http://www.tsetmc.com/Loader.aspx?ParTree=111C1213', verify=False) soup = BeautifulSoup(response.text, "lxml") tds = soup.find_all({'td'}) jsonGroupDatas = {} groupDict = {} for i in range(1, int(len(tds) / 2)): jsonGroupDatas[tds[1 + (2 * i)].text.strip()] = tds[0 + (2 * i)].text.strip() groupDict[(tds[0 + (2 * i)].text.strip())] = (tds[1 + (2 * i)].text.strip()) jsonGroupDatas = dictSorter(jsonGroupDatas) groupDict = dictSorter(groupDict) db.purge_table('Groups') groupTable = db.table('Groups') groupTable.insert(jsonGroupDatas) print('Groups Done') #---------- response = requests.get('https://search.codal.ir/api/search/v1/categories', verify=False) db.purge_table('Categories') categoriesTable = db.table('Categories') categoriesTable.insert_multiple(response.json()) print('Categories Done') #---------- response = requests.get('https://search.codal.ir/api/search/v1/companies', verify=False) jsonDatas = response.json() for jsonData in jsonDatas: code = jsonData["i"][:2] jsonData['Group'] = code if code in groupDict: jsonData['GroupName'] = groupDict[code] else: jsonData['GroupName'] = 'Unknown' db.purge_table('Symbols') symbolsTable = db.table('Symbols') symbolsTable.insert_multiple(jsonDatas) print('Symbols Done') #---------- symbolsTable = db.table('Symbols') symbols = symbolsTable.all() symbolGroupByGroupName = {} for symbol in symbols: if not (symbol['GroupName'] in symbolGroupByGroupName): symbolGroupByGroupName[symbol['GroupName']] = {} symbolGroupByGroupName[symbol['GroupName']][symbol['sy']] = symbol['n'] symbolGroupByGroupName = dictSorter(symbolGroupByGroupName) db.purge_table('SymbolGroupByGroupName') symbolsTable = db.table('SymbolGroupByGroupName') symbolsTable.insert(symbolGroupByGroupName) #---------- totalSymbolInGroup = {} for groupName in symbolGroupByGroupName: symbolInGroupCounter = len(symbolGroupByGroupName[groupName]) totalSymbolInGroup[groupName] = symbolInGroupCounter totalSymbolInGroup = dictSorter(totalSymbolInGroup) db.purge_table('totalSymbolInGroup') symbolsTable = db.table('totalSymbolInGroup') symbolsTable.insert(totalSymbolInGroup) #---------- print('Processing on Data is Done') db.close()
import sys sys.path.append('/usr/local/lib/python3.5/dist-packages') # TinyDBを使うためライブラリのインポート from tinydb import TinyDB, Query # データベースに接続 --- (※1) filepath = "test-tynydb.json" db = TinyDB(filepath) # 既存のデータがあれば破棄 --- (※2) db.purge_table('fruits') # テーブルを得る --- (※3) table = db.table('fruits') # データをデータベースに挿入 --- (※4) table.insert( {'name': 'Banana', 'price': 600} ) table.insert( {'name': 'Orange', 'price': 1200} ) table.insert( {'name': 'Mango', 'price': 840} ) # 全データを抽出して表示 --- (※5) print(table.all()) # 特定のデータを抽出して表示 # Orangeを検索 --- (※6) Item = Query() res = table.search(Item.name == 'Orange') print('Orange is ', res[0]['price']) # 値段が800円以上のものを抽出 --- (※7)
class Manager: def __init__(self, bot): self.bot = bot self.bot.remove_command('help') self.config = configparser.ConfigParser() self.config.read('config.ini') self.db = TinyDB(self.config['db']['path']) @commands.command(pass_context=True, no_pm=True) async def help(self, ctx, *args): author = ctx.message.author amention = author.mention has_privs = self.userIsAdmin(author) trigger = self.config['bot']['trigger'] msg = ( '{0} Below is a list of example commands. If you want to pass additional arguments to the commands that accept arguments, you might have to wrap quotes around those arguments. ' 'If you don\'t wrap the additinoal arguments in quotes, there\'s a chance I won\'t understand the command and I\'ll just ignore them and ask you for the information I need.\n\n' '{1} u <any insult> - Returns "no u"\n' '{1} suggestion <sugggestion> - Submits a suggestion for this bot.\n' '{1} add team "<tournament name>" "<team name>" "<@Members, @on, this, team>" - Joints your team to a tournament (Notice the quotes)' ) if has_privs: msg = msg + ( '\n\n As an admin, you also have the ability to do these commands:\n\n' '{1} add tournament "<tournament name>" - Creates a tournament\n' '{1} purge - Delete ALL tournament data. Do this at the end of TigerLan' ) await self.bot.send_message(ctx.message.channel, msg.format(amention, trigger)) @commands.command(pass_context=True, no_pm=True) async def test(self, ctx, *args): await self.bot.send_message( ctx.message.channel, '{} arguments: {}'.format(len(args), ', '.join(args))) @commands.command(pass_context=True, no_pm=True) async def u(self, ctx, *args): amention = ctx.message.author.mention await self.bot.send_message(ctx.message.channel, '{} no u'.format(amention)) @commands.command(pass_context=True, no_pm=True) async def purge(self, ctx): amention = ctx.message.author.mention await self.bot.send_message( ctx.message.channel, '{} about to kill all tournament data, are you sure? (y/n)'.format( amention)) answer = await self.bot.wait_for_message(timeout=30, author=ctx.message.author) content = answer.content if answer is not None else "" while answer is not None and content != "y" and content != "n": await self.bot.send_message( answer.channel, '{} Uhhh... I did not understand that. Please just say "y" or "n".' .format(amention)) answer = await self.bot.wait_for_message(timeout=30, author=answer.author) if answer is not None: content = answer.content if answer is None: await self.bot.send_message( ctx.message.channel, '{} Took too long to answer, action canceled'.format(amention)) elif answer.content == 'y': self.db.purge_table('_default') await self.bot.send_message( answer.channel, '{} tournaments killed!'.format(amention)) elif answer.content == 'n': await self.bot.send_message( answer.channel, '{} okay. I\'ll do nothing then.'.format(amention)) @commands.command(pass_context=True, no_pm=True) async def suggestion(self, ctx, *arg): suggestion = " ".join(arg) amention = ctx.message.author.mention if len(arg) == 0: await self.bot.send_message( ctx.message.channel, '{} what is your suggestion?'.format(amention)) suggestion = await self.bot.wait_for_message( timeout=30, author=ctx.message.author) suggestion = suggestion.content if suggestion is not None else "" if suggestion == "": await self.bot.send_message( ctx.message.channel, '{} no suggestion reported. Action canceled.'.format(amention)) return suggestions = self.db.table('suggestions') suggestions.insert({ 'userid': ctx.message.author.id, 'username': ctx.message.author.name, 'suggestion': suggestion }) await self.bot.send_message( ctx.message.channel, '{} suggestion noted. Thanks!'.format(amention)) def userIsAdmin(self, user): roles = [str(role) for role in user.roles] return 'Mods' in roles or 'Admins' in roles or '@everyone' in roles @commands.group(pass_context=True, no_pm=True) async def add(self, ctx): if ctx.invoked_subcommand is None: author = ctx.message.author amention = author.mention has_privs = self.userIsAdmin(author) msg = '{} No subcommand found! Please call ' if has_privs: msg = msg + 'either "add tournament" or ' msg = msg + '"add team". Alternatively call "' + self.config[ 'bot']['trigger'] + ' help" for a list of all commands!' await self.bot.send_message(ctx.message.channel, msg.format(amention)) @add.command(pass_context=True, no_pm=True) async def tournament(self, ctx, *arg): amention = ctx.message.author.mention if len(arg) == 1: self.db.insert({'tournament': arg[0], 'teams': []}) msg = '{} tournament ' + arg[0] + ' has been created!' await self.bot.send_message(ctx.message.channel, msg.format(amention)) return await self.bot.send_message( ctx.message.channel, '{} what is the name of the tournament?'.format(amention)) tournament = await self.bot.wait_for_message(timeout=30, author=ctx.message.author) if tournament is None: await self.bot.send_message( ctx.message.channel, '{} No input recieved! No tournament has been created.'.format( amention)) return tournament = tournament.content self.db.insert({'tournament': tournament, 'teams': []}) msg = '{} tournament ' + tournament + ' has been created!' await self.bot.send_message(ctx.message.channel, msg.format(amention)) @add.command(pass_context=True, no_pm=True) async def team(self, ctx, *arg): amention = ctx.message.author.mention tournament = None team = None members = None tournaments = self.db.all() tournament_names = [vals['tournament'] for vals in tournaments] if len(tournament_names) == 0: await self.bot.send_message( ctx.message.channel, '{} No tournaments have been created yet! Please talk to an admin or mod.' .format(amention)) return if len(arg) == 3: tournament = arg[0] team = arg[1] members = arg[2] members = amention + ", " + members if tournament not in tournament_names: msg = '{} That tournament has not been created yet. Please try that again. Here are the Tornaments you can join:' for val in tournament_names: msg = msg + '\n' + val msg = msg + '\nPlease try running that command again with one of these tournaments. Or contact an admin/mod for help.' await self.bot.send_message(ctx.message.channel, msg.format(amention)) return else: msg = '{} What tournament would you like to join? Here are your options:' for val in tournament_names: msg = msg + '\n' + val await self.bot.send_message(ctx.message.channel, msg.format(amention)) tournament = await self.bot.wait_for_message( timeout=30, author=ctx.message.author) if tournament is not None: tournament = tournament.content while tournament is not None and tournament not in tournament_names and tournament != 'stop': await self.bot.send_message(ctx.message.channel, \ '{} That tournament does not exist! Please try again or contact an admin/mod.\n Alternatively, type "stop" to cancel this command.'.format(amention)) tournament = await self.bot.wait_for_message( timeout=30, author=ctx.message.author) if tournament is not None: tournament = tournament.content if tournament is None: await self.bot.send_message( ctx.message.channel, '{} No input recieved. Action canceled. Please try again.'. format(amention)) return elif tournament == 'stop': await self.bot.send_message( ctx.message.channel, '{} Action canceled. Nothing was done!'.format(amention)) return await self.bot.send_message( ctx.message.channel, '{} What is the name of your team?'.format(amention)) team = await self.bot.wait_for_message(timeout=30, author=ctx.message.author) if team is None: await self.bot.send_message( ctx.message.channel, '{} No input recieved. Action canceled. Please try again.'. format(amention)) return team = team.content msg = '{} Who all is in your team? You can either @ them in discord or just provide a name. Don\'t write your own name down. I know who you are ;).' msg = msg + '\nExample: @Botson, @Botty, playerone, playertwo' await self.bot.send_message(ctx.message.channel, msg.format(amention)) members = await self.bot.wait_for_message( timeout=30, author=ctx.message.author) if members is None: await self.bot.send_message( ctx.message.channel, '{} No input recieved. Action canceled. Please try again.'. format(amention)) return members = members.content members = amention + ", " + members msg = '{} Alright, let me make sure I got that right...\n' msg = msg + 'You want to join the ' + tournament + ' tournament.\n' msg = msg + 'Your team name is ' + team + '.\n' msg = msg + 'Your team members are ' + members + '.\n' msg = msg + 'Is this information correct? (y/n)' await self.bot.send_message(ctx.message.channel, msg.format(amention)) answer = await self.bot.wait_for_message(timeout=30, author=ctx.message.author) if answer is not None: answer = answer.content while answer is not None and answer != 'y' and answer != 'n': await self.bot.send_message( answer.channel, '{} Uhhh... I did not understand that. Please just say "y" or "n".' .format(amention)) answer = await self.bot.wait_for_message(timeout=30, author=ctx.message.author) if answer is not None: answer = answer.content if answer is None: await self.bot.send_message( ctx.message.channel, '{} No input recieved. Action canceled. Please try again.'. format(amention)) return elif answer == 'n': await self.bot.send_message(ctx.message.channel, '{} Action canceled!'.format(amention)) return entry = Query() result = self.db.search(entry.tournament == tournament) teams = result[0]['teams'] teams.append({'team': team, 'players': members}) self.db.update(set('teams', teams), entry.tournament == tournament) msg = '{} I have you and your team down for the ' + tournament + ' tournament! Good luck!' await self.bot.send_message(ctx.message.channel, msg.format(amention))
class DataStore: ekse = None kelas = None kacamata = None db = None def __init__(self): self.db = TinyDB('db/database.json') self.ekse = self.db.table('eksekusi') self.kelas = self.db.table('kelas') self.kacamata = Query() #self.initialize() def initialize(self): self.db.purge_table('kelas') self.kelas.insert_multiple([ { "kelas": 0, "kacamata": "baca", "nama": "baca-00", "path": "static/img/class/0.png", "like": 0, "dislike": 0, "predicted": 0 }, { "kelas": 1, "kacamata": "baca", "nama": "baca-01", "path": "static/img/class/1.png", "like": 0, "dislike": 0, "predicted": 0 }, { "kelas": 2, "kacamata": "baca", "nama": "baca-02", "path": "static/img/class/2.png", "like": 0, "dislike": 0, "predicted": 0 }, { "kelas": 3, "kacamata": "baca", "nama": "baca-03", "path": "static/img/class/3.png", "like": 0, "dislike": 0, "predicted": 0 }, { "kelas": 4, "kacamata": "baca", "nama": "baca-04", "path": "static/img/class/4.png", "like": 0, "dislike": 0, "predicted": 0 }, { "kelas": 5, "kacamata": "baca", "nama": "baca-05", "path": "static/img/class/5.png", "like": 0, "dislike": 0, "predicted": 0 }, # -- baca-sun { "kelas": 0, "kacamata": "sung", "nama": "sung-00", "path": "static/img/clazz/0.png", "like": 0, "dislike": 0, "predicted": 0 }, { "kelas": 1, "kacamata": "sung", "nama": "sung-01", "path": "static/img/clazz/1.png", "like": 0, "dislike": 0, "predicted": 0 }, { "kelas": 2, "kacamata": "sung", "nama": "sung-02", "path": "static/img/clazz/2.png", "like": 0, "dislike": 0, "predicted": 0 }, { "kelas": 3, "kacamata": "sung", "nama": "sung-03", "path": "static/img/clazz/3.png", "like": 0, "dislike": 0, "predicted": 0 }, { "kelas": 4, "kacamata": "sung", "nama": "sung-04", "path": "static/img/clazz/4.png", "like": 0, "dislike": 0, "predicted": 0 }, { "kelas": 5, "kacamata": "sung", "nama": "sung-05", "path": "static/img/clazz/5.png", "like": 0, "dislike": 0, "predicted": 0 }, { "kelas": 6, "kacamata": "sung", "nama": "sung-06", "path": "static/img/clazz/6.png", "like": 0, "dislike": 0, "predicted": 0 } ]) def arsip(self, index, kelas, kelamin, kacamata, like): self.ekse.insert({ 'indeks_morfo': index, 'kelas': kelas, 'jenis_kelamin': kelamin, 'kacamata': kacamata, 'like': like }) return True, 'Berhasil insert data!' def like(self, kacamata, is_like): # print(kacamata) Kacamata = Query() ini = self.kelas.search((Kacamata.nama == kacamata))[0] # print(ini.doc_id) if (is_like == True): self.kelas.update({'like': ini['like'] + 1}, doc_ids=[ini.doc_id]) else: self.kelas.update({'dislike': ini['like'] + 1}, doc_ids=[ini.doc_id]) return True def all_arsip(self): return self.ekse.all() def clear_arsip(self): self.db.purge_table('eksekusi') return True def all_kacamata(self, split=False): if split: Kacamata = Query() return self.kelas.search( Kacamata.kacamata == 'baca'), self.kelas.search( Kacamata.kacamata == 'sung'), else: return self.kelas.all()
from tinydb import TinyDB, Query database_path = "tinydb.json" database = TinyDB(database_path) database.purge_table("items") table = database.table("items") table.insert({"name": "Banana", "price": 6000}) table.insert({"name": "Orange", "price": 12000}) table.insert({"name": "Mango", "price": 8400}) print(table.all()) item = Query() res = table.search(item.name == "Orange") print("Orange is ", res[0]["price"]) res = table.search(item.price >= 8000) for i in res: print("-", i["name"])
class TinyRunDB(BaseDB): def __init__(self, conn_str): self.name = 'TinyRunDB' self.conn_str = conn_str self.default_table = 'linchpin' def _opendb(self): self.middleware = CachingMiddleware(JSONStorage) self.middleware.WRITE_CACHE_SIZE = 500 self.db = TinyDB(self.conn_str, storage=self.middleware, default_table=self.default_table) def __str__(self): if self.conn_str: return "{0} at {2}".format(self.name, self.conn_str) return "{0} at {1}".format(self.name, 'None') @property def schema(self): return self._schema @schema.setter def schema(self, schema): self._schema = dict() self._schema.update(schema) @usedb def init_table(self, table): t = self.db.table(name=table) return t.insert(self.schema) @usedb def update_record(self, table, run_id, key, value): t = self.db.table(name=table) return t.update(add(key, value), eids=[run_id]) @usedb def get_record(self, table, action='up', run_id=None): t = self.db.table(name=table) if not run_id: run_id = len(t.all()) if not run_id: return (None, 0) for rid in range(int(run_id), 0, -1): record = t.get(eid=int(rid)) if record['action'] == action: return (record, int(rid)) return (None, 0) @usedb def get_records(self, table, count=10): records = {} if table in self.db.tables(): t = self.db.table(name=table) start = len(t) end = start - count for i in xrange(start, end, -1): records[i] = t.get(doc_id=i) return records @usedb def get_tables(self): tables = self.db.tables() tables.remove(self.default_table) return tables def remove_record(self, table, key, value): pass def search(self, table, key=None): t = self.db.table(name=table) if key: return t.search(key) return t.all() def query(self, table, query): pass def purge(self, table=None): if table: return self.db.purge_table(table) return self.db.purge_tables() def _closedb(self): self.db.close()
from tinydb import TinyDB db = TinyDB('database.json') db.purge_table('locations') locations = db.table('locations') locations.insert( { 'name': 'Bar', 'directions': ['list of directions'] } ) locations.insert( { 'name': 'McDonalds', 'directions': ['list of directions'] } ) locations.insert( { 'name': 'Cafe', 'directions': ['list of directions'] } ) locations.insert( { 'name': 'Car Park',
class TinyRunDB(BaseDB): def __init__(self, conn_str): self.name = 'TinyRunDB' self.conn_str = conn_str self.default_table = 'linchpin' def _opendb(self): self.middleware = CachingMiddleware(JSONStorage) self.middleware.WRITE_CACHE_SIZE = 500 self.db = TinyDB(self.conn_str, storage=self.middleware, default_table=self.default_table) def __str__(self): if self.conn_str: return "{0} at {2}".format(self.name, self.conn_str) return "{0} at {1}".format(self.name, 'None') @property def schema(self): return self._schema @schema.setter def schema(self, schema): self._schema = dict() self._schema.update(schema) @usedb def init_table(self, table): t = self.db.table(name=table) return t.insert(self.schema) @usedb def update_record(self, table, run_id, key, value): t = self.db.table(name=table) # get transaction record tx_rec = t.get(doc_id=run_id).get("outputs", []) if len(tx_rec) > 0 and isinstance(value, list): # fetch the resources dict, index # by filtering them from outputs list res_list = [(idx, x) for idx, x in enumerate(tx_rec) if "resources" in x] if len(res_list) != 0: res_idx = res_list[0][0] resources = res_list[0][1] if "resources" in list(value[0].keys()): de = resources["resources"] for i in value[0]["resources"]: de.append(i) de = {"resources": de} tx_rec[res_idx] = de res = t.update(tinySet(key, [de]), doc_ids=[run_id]) return res return t.update(add(key, value), doc_ids=[run_id]) @usedb def get_tx_record(self, tx_id): t = self.db.table(name='linchpin') return t.get(doc_id=tx_id) @usedb def get_tx_records(self, tx_ids): txs = {} t = self.db.table(name='linchpin') for tx_id in tx_ids: txs[tx_id] = t.get(doc_id=tx_id) return txs @usedb def get_record(self, table, action='up', run_id=None): t = self.db.table(name=table) if not run_id: run_id = len(t.all()) if not run_id: return (None, 0) for rid in range(int(run_id), 0, -1): record = t.get(eid=int(rid)) if record and record['action'] == action: return (record, int(rid)) else: record = t.get(eid=int(run_id)) if record: return (record, int(run_id)) return (None, 0) @usedb def get_records(self, table, count=10): records = {} if table in self.db.tables(): t = self.db.table(name=table) if len(t.all()): start = len(t) if count == 'all': end = 0 else: end = start - count for i in range(start, end, -1): records[i] = t.get(doc_id=i) return records @usedb def get_tables(self): tables = self.db.tables() tables.remove(self.default_table) return tables def remove_record(self, table, key, value): pass def search(self, table, key=None): t = self.db.table(name=table) if key: return t.search(key) return t.all() def query(self, table, query): pass def purge(self, table=None): if table: return self.db.purge_table(table) return self.db.purge_tables() def _closedb(self): self.db.close()
def fetch(projectId): db = TinyDB("project_dbs/" + object_id_to_directory_name(projectId) + ".json") from rules import rules from display_results import display_results from insert_entity import insert_entity, insert_subnet_entities from categories.firewalls import add_affected_instances, add_network_rules from categories.roles import insert_roles import categories.compute_engine from categories.service_account_keys import insert_service_account_keys import categories.service_accounts import categories.service_account_IAM_policy import categories.buckets from categories.addresses import insert_addresses # initialize the entity database tables for the project, so that running G-Scout more than once against the same project doesn't result in duplicated data # I did this as an explicit list of tables so that if future versions store data that should persist between runs, those aren't deleted. entity_table_names = ["Address", "Bucket", "Cluster", "Compute Engine", "Finding", "Firewall", "Instance Template", "Network", "Pub/Sub", "Role", "Rule", "Service Account", "Snapshot", "SQL Instance", "Subnet", "Topics"] for tn in entity_table_names: db.purge_table(tn) try: insert_entity(projectId, "compute", ["networks"], "Network") except Exception as e: print("Failed to fetch networks.") logging.exception("networks") try: insert_subnet_entities(projectId) except Exception as e: print("Failed to fetch subnets: %s" % (e)) logging.exception("subnets: %s" % (e)) try: insert_entity(projectId, "compute", ["firewalls"], "Firewall") except Exception as e: print("Failed to fetch firewalls.") logging.exception("firewalls") try: insert_roles(projectId, db) except Exception as e: print("Failed to fetch roles.") logging.exception("roles") try: insert_entity(projectId, "iam", ["projects","serviceAccounts"], "Service Account", "v1", "projects/", "accounts") categories.service_accounts.insert_sa_roles(projectId, db) except Exception as e: print("Failed to fetch service accounts.") logging.exception("service accounts") try: insert_service_account_keys(projectId, db) except Exception as e: print("Failed to fetch service account keys.") logging.exception("service account keys") try: categories.service_account_IAM_policy.insert_sa_policies(projectId, db) except Exception as e: print("Failed to fetch service account IAM policies.") logging.exception("service account IAM policies") try: insert_entity(projectId, "storage", ["buckets"], "Bucket") except Exception as e: print("Failed to fetch buckets.") logging.exception("buckets") try: categories.buckets.insert_acls(db) categories.buckets.insert_defacls(db) except Exception as e: print("Failed to fetch bucket ACLS/Default ACLS: %s" % (e)) logging.exception("bucket ACLS/Default ACLS: %s" % (e)) try: categories.compute_engine.insert_instances(projectId, db) insert_entity(projectId, "compute", ["instanceTemplates"], "Instance Template") categories.compute_engine.insert_instance_groups(projectId, db) except Exception as e: print("Failed to fetch compute engine instances.") logging.exception("compute engine instances") try: categories.compute_engine.add_member_instances(projectId, db) except Exception as e: print("Failed add member instances to compute engine instances.") logging.exception("compute engine instances") try: add_network_rules(projectId, db) add_affected_instances(projectId, db) except Exception as e: print("Failed to display instances/rules with instances.") try: insert_addresses(projectId, db) except Exception as e: print("Failed to fetch IP addresses") print(e) try: insert_entity(projectId, "compute", ["snapshots"], "Snapshot") except Exception as e: print("Failed to fetch instance snapshots.") logging.exception("snapshots") try: insert_entity(projectId, "sqladmin", ["instances"], "SQL Instance", "v1beta4") except Exception as e: print("Failed to fetch SQL instances.") logging.exception("SQL instances") try: insert_entity(projectId, "pubsub", ["projects", "topics"], "Topics", "v1", "projects/", "topics") insert_entity(projectId, "pubsub", ["projects", "subscriptions"], "Pub/Sub", "v1", "projects/", "subscriptions") except Exception as e: print("Failed to fetch Pub/Sub topics/subscriptions.") logging.exception("pub/sub") try: insert_entity(projectId, "container", ['projects','locations','clusters'],"Cluster","v1beta1","projects/","clusters","/locations/-") except: print("Failed to fetch clusters") logging.exception("GKE") try: rules(projectId) except Exception as e: logging.exception(e) display_results(db, projectId)
#테이블 데이터 전체 삽입1 with open('c:/section5/data/users.json','r') as infile: r = json.loads(infile.read()) for p in r: users.insert(p) #테이블 데이터 전체 삽입2 with open('c:/section5/data/todos.json','r') as infile: r = json.loads(infile.read()) for p in r: todos.insert(p) #전체 데이터 출력 print(users.all()) print(todos.all()) #테이블 목록 조회 print(db.tables()) #전체 데이터 삭제 users.purge() todos.purge() db.purge_table('users') db.purge_table('todos') db.purge_tables() db.close()
#!/usr/local/bin/python3 import yaml from tinydb import TinyDB, Query DB = TinyDB('database/data.db', default_table='players') Q = Query() DB.purge_table('monsters') TABLE = DB.table('monsters') with open('conf/monster.yaml', 'r') as yamlf: CONF = yaml.load(yamlf) for section in CONF: CD = dict(CONF[section]) CD.update({'name': section}) TABLE.upsert(CD, Q.name == section)
#!/usr/local/bin/python3 import yaml from tinydb import TinyDB, Query DB = TinyDB('database/data.db', default_table='players') Q = Query() DB.purge_table('skills') TABLE = DB.table('skills') with open('conf/skills.yaml', 'r') as yamlf: CONF = yaml.load(yamlf) for section in CONF: CD = dict(CONF[section]) CD.update({'name': section}) TABLE.upsert(CD, Q.name == section)