本文整理汇总了Python中tinydb.TinyDB类的典型用法代码示例。如果您正苦于以下问题:Python TinyDB类的具体用法?Python TinyDB怎么用?Python TinyDB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TinyDB类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_statistic
def update_statistic():
db = TinyDB("data/db.json")
rows = db.all()
rows.sort(key=lambda x: int(x['rid']), reverse=True)
levels = {1: [], 2: [], 3: []}
for row in rows:
levels[row['level']].append(row)
out = []
header = '|%-35s|%-40s|%-6s|%-10s|%5s|%10s|' % ("round", "problem", "solved", "ave_pts",
"rate", "submission")
for k, v in levels.items():
out.append('# LEVEL %d' % k)
out.append('-' * len(header))
out.append(header)
out.append('-' * len(header))
v.sort(key=lambda x: difficulty(x), reverse=True)
for i in v:
out.append('|%-35s|%-40s|%-6s|%-10.2lf|%-4.3lf|%10s|' % (i['round'], i['name'], i['solved'],
i['average_pts'], i['correct_rate'], i['submissions']))
out.append('*' * len(header))
with open("data/statistic.txt", 'w') as f:
for i in out:
f.write(i + '\n')
print '>>>> data/statistic.txt has been updated.'
示例2: AddUser
def AddUser(self, username, chatid):
my_pokemon = [0] * 152 # Matching arr index to pokemon index (0 is disregarded)
db = TinyDB('users.json')
db.insert({'username': username, 'chatid': chatid, 'pokemon': my_pokemon})
pass # RETURN: check bool
示例3: save_customer
def save_customer(self, customer):
if not self.validate(customer):
return None
db = TinyDB('db/db.json')
return db.insert(customer)
示例4: test_serialisation_of_numpy_ndarray
def test_serialisation_of_numpy_ndarray(tmpdir):
from sacred.observers.tinydb_hashfs import NdArraySerializer
from tinydb_serialization import SerializationMiddleware
import numpy as np
# Setup Serialisation object for non list/dict objects
serialization_store = SerializationMiddleware()
serialization_store.register_serializer(NdArraySerializer(), 'TinyArray')
db = TinyDB(os.path.join(tmpdir.strpath, 'metadata.json'),
storage=serialization_store)
eye_mat = np.eye(3)
ones_array = np.ones(5)
document = {
'foo': 'bar',
'some_array': eye_mat,
'nested': {
'ones': ones_array
}
}
db.insert(document)
returned_doc = db.all()[0]
assert returned_doc['foo'] == 'bar'
assert (returned_doc['some_array'] == eye_mat).all()
assert (returned_doc['nested']['ones'] == ones_array).all()
示例5: check_prediction_cache
def check_prediction_cache(
region_id,
type_id,
cache_path=CACHE_PATH,
db_filename='prophet.json'
):
"""check tinyDB for cached predictions
Args:
region_id (int): EVE Online region ID
type_id (int): EVE Online type ID
cache_path (str): path to caches
db_filename (str): name of tinydb
Returns:
pandas.DataFrame: cached prediction
"""
utc_today = datetime.utcnow().strftime('%Y-%m-%d')
prediction_db = TinyDB(path.join(cache_path, db_filename))
raw_data = prediction_db.search(
(Query().cache_date == utc_today) &
(Query().region_id == region_id) &
(Query().type_id == type_id)
)
prediction_db.close()
if raw_data:
panda_data = pd.read_json(raw_data[0]['prediction'])
return panda_data
else:
return None
示例6: insert_test
def insert_test(db_file='db.json'):
db = TinyDB(db_file)
db.insert({
'name': 'Aman Verma',
'items': 1,
'contact': 7890701597
})
示例7: __init__
def __init__(self, path):
root_dir = os.path.abspath(path)
if not os.path.exists(root_dir):
raise IOError('Path does not exist: %s' % path)
fs = HashFS(os.path.join(root_dir, 'hashfs'), depth=3,
width=2, algorithm='md5')
# Setup Serialisation for non list/dict objects
serialization_store = SerializationMiddleware()
serialization_store.register_serializer(DateTimeSerializer(),
'TinyDate')
serialization_store.register_serializer(FileSerializer(fs),
'TinyFile')
if opt.has_numpy:
serialization_store.register_serializer(NdArraySerializer(),
'TinyArray')
if opt.has_pandas:
serialization_store.register_serializer(DataFrameSerializer(),
'TinyDataFrame')
serialization_store.register_serializer(SeriesSerializer(),
'TinySeries')
db = TinyDB(os.path.join(root_dir, 'metadata.json'),
storage=serialization_store)
self.db = db
self.runs = db.table('runs')
self.fs = fs
示例8: test_serialisation_of_pandas_dataframe
def test_serialisation_of_pandas_dataframe(tmpdir):
from sacred.observers.tinydb_hashfs import (DataFrameSerializer,
SeriesSerializer)
from tinydb_serialization import SerializationMiddleware
import numpy as np
import pandas as pd
# Setup Serialisation object for non list/dict objects
serialization_store = SerializationMiddleware()
serialization_store.register_serializer(DataFrameSerializer(),
'TinyDataFrame')
serialization_store.register_serializer(SeriesSerializer(),
'TinySeries')
db = TinyDB(os.path.join(tmpdir.strpath, 'metadata.json'),
storage=serialization_store)
df = pd.DataFrame(np.eye(3), columns=list('ABC'))
series = pd.Series(np.ones(5))
document = {
'foo': 'bar',
'some_dataframe': df,
'nested': {
'ones': series
}
}
db.insert(document)
returned_doc = db.all()[0]
assert returned_doc['foo'] == 'bar'
assert (returned_doc['some_dataframe'] == df).all().all()
assert (returned_doc['nested']['ones'] == series).all()
示例9: index
def index():
form = SearchForm()
query = request.args.get('query', '').strip()
db = TinyDB(recipyGui.config.get('tinydb'))
if not query:
runs = db.all()
else:
# Search run outputs using the query string
runs = db.search(
where('outputs').any(lambda x: listsearch(query, x)) |
where('inputs').any(lambda x: listsearch(query, x)) |
where('script').search(query) |
where('notes').search(query) |
where('unique_id').search(query))
runs = sorted(runs, key = lambda x: parse(x['date'].replace('{TinyDate}:', '')) if x['date'] is not None else x['eid'], reverse=True)
run_ids = []
for run in runs:
if 'notes' in run.keys():
run['notes'] = str(escape(run['notes']))
run_ids.append(run.eid)
db.close()
return render_template('list.html', runs=runs, query=query, form=form,
run_ids=str(run_ids),
dbfile=recipyGui.config.get('tinydb'))
示例10: test_json_readwrite
def test_json_readwrite(tmpdir):
"""
Regression test for issue #1
"""
path = str(tmpdir.join('test.db'))
# Create TinyDB instance
db = TinyDB(path, storage=JSONStorage)
item = {'name': 'A very long entry'}
item2 = {'name': 'A short one'}
get = lambda s: db.get(where('name') == s)
db.insert(item)
assert get('A very long entry') == item
db.remove(where('name') == 'A very long entry')
assert get('A very long entry') is None
db.insert(item2)
assert get('A short one') == item2
db.remove(where('name') == 'A short one')
assert get('A short one') is None
示例11: dragon_greet
def dragon_greet():
print("_______________________________________________________________\n")
time = datetime.datetime.now().time()
global user_full_name
global user_prefix
global config_file
command = "getent passwd $LOGNAME | cut -d: -f5 | cut -d, -f1"
user_full_name = os.popen(command).read()
user_full_name = user_full_name[:-1] # .decode("utf8")
home = expanduser("~")
config_file = TinyDB(home + '/.dragonfire_config.json')
callme_config = config_file.search(Query().datatype == 'callme')
if callme_config:
user_prefix = callme_config[0]['title']
else:
gender_config = config_file.search(Query().datatype == 'gender')
if gender_config:
user_prefix = GENDER_PREFIX[gender_config[0]['gender']]
else:
gender = Classifier.gender(user_full_name.split(' ', 1)[0])
config_file.insert({'datatype': 'gender', 'gender': gender})
user_prefix = GENDER_PREFIX[gender]
if time < datetime.time(12):
time_of_day = "morning"
elif datetime.time(12) < time < datetime.time(18):
time_of_day = "afternoon"
else:
time_of_day = "evening"
userin.execute(["echo"], "To activate say 'Dragonfire!' or 'Wake Up!'")
userin.say(" ".join(["Good", time_of_day, user_prefix]))
示例12: __init__
class pcDB:
def __init__(self,table="default"):
#'/path/to/db.json'
path=''
self.table=table
self.db = TinyDB(path).table(table)
def insert(self,_dict):
'''
:return:
'''
self.db.insert(_dict)
# db.insert({'int': 1, 'char': 'a'})
# db.insert({'int': 1, 'char': 'b'})
pass
def getAll(self):
'''
not param just get all data
:return:
'''
return self.db.all()
#db.search()
pass
pass
#
# from tinydb.storages import JSONStorage
# from tinydb.middlewares import CachingMiddleware
# db = TinyDB('/path/to/db.json', storage=CachingMiddleware(JSONStorage))
示例13: get_coffees
def get_coffees(active):
name = ''
name_link = ''
nav_links = []
description = ''
db = TinyDB('db/coffee.json')
coffee = db.all()
count = 0
coffee_id = 0
for i in coffee:
if active == -1 and count == 0:
nav_links.append(("/"+str(i['id']),"active",i['name']))
name = i['name']
description = i['description']
coffee_id = i['id']
name_link = '/'+str(i['id'])
elif active == -1 and count > 0:
nav_links.append(("/"+str(i['id']),"",i['name']))
elif active == i['id']:
nav_links.append(("/"+str(i['id']),"active",i['name']))
name = i['name']
description = i['description']
coffee_id = i['id']
name_link = '/'+str(i['id'])
else:
nav_links.append(("/"+str(i['id']),"",i['name']))
count = count+1
for i in nav_links:
print i
print name
print name_link
return nav_links, name, name_link, description, coffee_id
示例14: test_process
def test_process(tmpdir, capsys, side_effect, success):
db_fn = tmpdir.join('database.json')
log_dir = tmpdir.mkdir('log')
db = TinyDB(db_fn.strpath)
process_me = '/my/path/A12345'
accession_number = op.basename(process_me)
paths2process = {process_me: 42}
with patch('subprocess.Popen') as mocked_popen:
stdout = b"INFO: PROCESSING STARTS: {'just': 'a test'}"
mocked_popen_instance = mocked_popen.return_value
mocked_popen_instance.side_effect = side_effect
mocked_popen_instance.communicate.return_value = (stdout, )
# set return value for wait
mocked_popen_instance.wait.return_value = 1 - success
# mock also communicate to get the supposed stdout
process(paths2process, db, wait=-30, logdir=log_dir.strpath)
out, err = capsys.readouterr()
log_fn = log_dir.join(accession_number + '.log')
mocked_popen.assert_called_once()
assert log_fn.check()
assert log_fn.read() == stdout.decode('utf-8')
assert db_fn.check()
# dictionary should be empty
assert not paths2process
assert out == 'Time to process {0}\n'.format(process_me)
# check what we have in the database
Path = Query()
query = db.get(Path.input_path == process_me)
assert len(db) == 1
assert query
assert query['success'] == success
assert query['accession_number'] == op.basename(process_me)
assert query['just'] == 'a test'
示例15: setUp
def setUp(self):
with open(os.path.join(BASE_DIR, 'tests', 'fixtures', 'test.json')) as f:
self.j = json.load(f)
self.database_name = os.path.join(os.getcwd(), 'test.db')
db = TinyDB(self.database_name)
db.insert_multiple(self.j)
db.close