本文整理汇总了Python中tinydb.TinyDB.purge方法的典型用法代码示例。如果您正苦于以下问题:Python TinyDB.purge方法的具体用法?Python TinyDB.purge怎么用?Python TinyDB.purge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tinydb.TinyDB
的用法示例。
在下文中一共展示了TinyDB.purge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Test_005_Search_Not_existing_data_by_valid_query_Function
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
class Test_005_Search_Not_existing_data_by_valid_query_Function(unittest.TestCase):
def setUp(self):
self.db = TinyDB('db.json')
def tearDown(self):
self.db.purge()
self.db.all()
def test_simple_search_not_exist(self):
print("case 5 search Non-existing data by valid query")
result=self.db.search(where('Name') == 'Wendy')
self.assertEqual(result,[])
示例2: Test_007_Delete_Not_existing_data_by_valid_query_Function
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
class Test_007_Delete_Not_existing_data_by_valid_query_Function(unittest.TestCase):
def setUp(self):
self.db = TinyDB('db.json')
def tearDown(self):
self.db.purge()
self.db.all()
def test_simple_delete_not_exist(self):
print("case 7 Delete Non-existing data by valid query")
result=self.db.remove(where('Name') == 'Wendy')
self.assertEqual(result,None)
示例3: Test_006_Modify_Not_existing_data_by_valid_query_Function
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
class Test_006_Modify_Not_existing_data_by_valid_query_Function(unittest.TestCase):
def setUp(self):
self.db = TinyDB('db.json')
def tearDown(self):
self.db.purge()
self.db.all()
def test_simple_modify_not_exist(self):
print("case 6 modify Non-existing data by valid query")
result=self.db.update({'int': 10}, where('Name') == 'Wendy')
self.assertEqual(result,None)
示例4: Test_001_Insert_by_valid_query_Function
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
class Test_001_Insert_by_valid_query_Function(unittest.TestCase):
def setUp(self):
self.db = TinyDB('db.json')
def tearDown(self):
self.db.purge()
self.db.all()
def test_simple_insert_valid_exist(self):
print("case 1 insert data by valid query")
self.db.insert({'Name': 'Greg', 'Email': '[email protected]', 'int' : 1, 'char':1})
result=self.db.search(where('Name') == 'Greg')
self.assertEqual(result,[{'Name': 'Greg', 'Email': '[email protected]', 'int' : 1, 'char':1}])
示例5: Test_008_Insert_exits_data_Function
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
class Test_008_Insert_exits_data_Function(unittest.TestCase):
def setUp(self):
self.db = TinyDB('db.json')
def tearDown(self):
self.db.purge()
self.db.all()
def test_simple_insert_by_query(self):
print("case 8 can insert existing data")
self.db.insert({'Name': 'Yingyu Wu', 'Email': 'ywu23[email protected]', 'int' : 1, 'char':1})
self.db.insert({'Name': 'Yingyu Wu', 'Email': '[email protected]', 'int' : 1, 'char':1})
result_array = self.db.search(where('Name') == 'Yingyu Wu')
num = len(result_array)
#print (result_array)
#print("search one key,get %d result" %(num))
self.assertEqual(2,num)
示例6: __init__
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
class FolderManager:
def __init__(self, path):
self.db_file = os.path.join(path, CONF_DIR, FOLDER_DB_FN)
self._db = TinyDB(self.db_file)
def open_db(self):
self._db = TinyDB(self.db_file)
def close_db(self):
self._db.close()
def add_folder(self, file_name):
if not self.folder_exists(file_name):
entry = {'file_name': file_name}
self._db.insert(entry)
def get_all_entries(self):
return self._db.all()
def folder_exists(self, file_name):
""" checks if a folder has been added """
entries = self._db.search(where('file_name') == file_name)
if entries:
return True
else:
return False
def remove_element(self, file_name):
self._db.remove(where('file_name') == file_name)
def get_file_names(self):
""" returns all the file names of folders that the user has added """
file_names = []
for entry in self._db.all():
file_names.append(entry['file_name'])
return file_names
def get_folder_by_name(self, expected_name):
""" get documents by the specified property """
entry = self._db.get(where('file_name') == expected_name)
return entry
def clear_all(self):
self._db.purge()
示例7: test_cutom_mapping_type_with_json
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
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
示例8: TweetCollector
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
class TweetCollector(object):
def __init__(self, agencies, token, token_secret, consumer, consumer_secret):
self.agencies = agencies
self.access_token = token
self.access_token_secret = token_secret
self.consumer_key = consumer
self.consumer_secret = consumer_secret
self._refresh = 10 * 60
self._tweets = TinyDB('./tweets.json')
self._tweets.purge()
self._auth = tweepy.OAuthHandler(self.consumer_key, self.consumer_secret)
self._auth.set_access_token(self.access_token, self.access_token_secret)
self._api = tweepy.API(self._auth)
thread = threading.Thread(target=self.run, args=())
thread.daemon = True
thread.start()
def _status(self, handle):
try:
user = self._api.get_user(handle)
except tweepy.TweepError as e:
print('Tweeter server error ', e.response, ' for handle: ', handle)
return []
if hasattr(user, 'status'):
return user.status
else:
return []
@staticmethod
def _process(status):
clean_status = re.sub(r'\w+:\/{2}[\d\w-]+(\.[\d\w-]+)*(?:(?:\/[^\s/]*))*', '', status._json['text'],
flags=re.MULTILINE)
clean_status = re.sub('@[\w.]+', '', clean_status, flags=re.MULTILINE)
tokenized_docs = word_tokenize(clean_status)
regex = re.compile('[%s]' % re.escape(string.punctuation))
tokenized_docs_no_punctuation = []
for token in tokenized_docs:
new_token = regex.sub(u'', token)
if not new_token == u'':
if new_token not in stopwords.words('english') and new_token != 'RT' and new_token != '...':
tokenized_docs_no_punctuation.append(new_token)
status._json['tokens'] = tokenized_docs_no_punctuation
return status
def _single_execute(self):
print(datetime.now().time())
for agency in self.agencies.search(where('handle')):
tweeter_handle = agency['handle']
status = self._status(tweeter_handle)
if status:
status_p = self._process(status)
if not self._tweets.search(where('id') == status_p._json['id']) or not self._tweets.all():
self._tweets.insert(status_p._json)
else:
continue
def run(self):
while True:
self._single_execute()
time.sleep(self._refresh)
示例9: __init__
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
class DocumentManager:
def __init__(self, path):
self.db_file = os.path.join(path, CONF_DIR, DB_FN)
self._db = TinyDB(self.db_file)
def open_db(self):
self._db = TinyDB(self.db_file)
def close_db(self):
self._db.close()
def doc_exists(self, file_name, title):
entries = self._db.search((where('file_name') == file_name) & (where('name') == title))
if entries:
return True
else:
return False
def is_doc_new(self, file_name):
file_name_exists = self._db.search(where('file_name') == file_name)
if not file_name_exists:
return True
return False
def is_doc_modified(self, file_name, path):
entry = self._db.get(where('file_name') == file_name)
full_path = os.path.join(path, file_name)
last_modified = os.stat(full_path).st_mtime
if entry and entry['added'] < last_modified and entry['last_mod'] < last_modified:
return True
return False
def add_document(self, title, create_date, doc_id, sys_mtime, last_mod, file_name):
entry = {'name': title, 'added': create_date, 'id': doc_id,
'sys_last_mod': sys_mtime, 'last_mod': last_mod, 'file_name': file_name,
'downloaded': []}
self._db.insert(entry)
def update_document(self, field, new_val, doc_id):
if type(new_val) is list:
self._db.update(_update_entry_list(field, new_val), where('id') == doc_id)
else:
if type(new_val) is set:
new_val = list(new_val)
self._db.update({field: new_val}, where('id') == doc_id)
def get_doc_by_prop(self, prop, expected_value):
""" get documents by the specified property """
entry = self._db.get(where(prop) == expected_value)
return entry
def get_all_entries(self):
return self._db.all()
def get_doc_ids(self):
""" returns all the ids of documents that user has added """
doc_ids = []
for entry in self._db.all():
doc_ids.append(entry['id'])
return doc_ids
def remove_element(self, doc_id):
self._db.remove(where('id') == doc_id)
def clear_all(self):
self._db.purge()
示例10: seed
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
def seed():
db = TinyDB('db/dataset.json')
db.purge()
db.insert({'employee_id': 6, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 1, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 2, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 3, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 4, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 5, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 6, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 0, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 1, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 2, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 3, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 4, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 5, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 6, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 1, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 2, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 3, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 4, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 5, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 6, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 1, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 2, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 3, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 4, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 5, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 6, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 0, 'day_of_week':1, 'attend': 1})
db.insert({'employee_id': 1, 'day_of_week':1, 'attend': 0})
db.insert({'employee_id': 6, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 1, 'day_of_week':2, 'attend': 0})
db.insert({'employee_id': 2, 'day_of_week':2, 'attend': 0})
db.insert({'employee_id': 3, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 4, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 5, 'day_of_week':2, 'attend': 0})
db.insert({'employee_id': 6, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 1, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 2, 'day_of_week':2, 'attend': 0})
db.insert({'employee_id': 3, 'day_of_week':2, 'attend': 0})
db.insert({'employee_id': 4, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 5, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 6, 'day_of_week':2, 'attend': 0})
db.insert({'employee_id': 0, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 1, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 2, 'day_of_week':2, 'attend': 0})
db.insert({'employee_id': 3, 'day_of_week':2, 'attend': 0})
db.insert({'employee_id': 4, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 5, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 6, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':2, 'attend': 0})
db.insert({'employee_id': 1, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 2, 'day_of_week':2, 'attend': 0})
db.insert({'employee_id': 3, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 4, 'day_of_week':2, 'attend': 0})
db.insert({'employee_id': 5, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 6, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 1, 'day_of_week':2, 'attend': 1})
db.insert({'employee_id': 6, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 1, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 2, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 3, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 4, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 5, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 6, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 1, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 2, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 3, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 4, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 5, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 6, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 1, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 2, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 3, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 4, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 5, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 6, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 1, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 2, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 3, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 4, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 5, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 6, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 0, 'day_of_week':3, 'attend': 0})
db.insert({'employee_id': 1, 'day_of_week':3, 'attend': 1})
db.insert({'employee_id': 6, 'day_of_week':4, 'attend': 0})
db.insert({'employee_id': 0, 'day_of_week':4, 'attend': 0})
db.insert({'employee_id': 1, 'day_of_week':4, 'attend': 1})
db.insert({'employee_id': 2, 'day_of_week':4, 'attend': 1})
#.........这里部分代码省略.........
示例11: xlibris
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
#.........这里部分代码省略.........
bookData['Language'] = raw_input('Language: ')
bookData['Publisher'] = raw_input('Publisher: ')
bookData['Title'] = raw_input('Title: ')
bookData['Year'] = raw_input('Year: ')
bookData['Date Added'] = _today()
self.db.insert(bookData)
def search(self, keyword):
NewSearch = Query()
title = self.db.search(NewSearch.Title == keyword)
auth = self.db.search(NewSearch.Authors == keyword)
pub = self.db.search(NewSearch.Publisher == keyword)
isbn = self.db.search(NewSearch.ISBN == keyword)
ttable = [title, auth, pub, isbn]
for i in ttable:
if i:
print 'Matches Found for {} \n'.format(keyword)
print tabulate(_concat(i), headers='keys', tablefmt="fancy_grid")
def _blkadd(self, ISBNlist):
with tqdm(ISBNlist) as pbar:
for i in pbar:
pbar.set_description("Adding %s "%i)
self.add(i)
pbar.update(1/len(ISBNlist)*100)
def add_from_file(self, filename):
with open(filename, 'rb') as f:
raw = reader(f)
final = list(raw)
for i in range(len(final)):
final[i] = str(final[i][0])
self._blkadd(final)
print 'Done'
def change_title(self, isbn):
tmp = Query()
def change(field):
def transform(element):
element[field] = raw_input('Enter Title ')
return transform
title = self.db.search(tmp.ISBN == isbn)[0]
print 'Change title of :- {}'.format(title['Title'])
self.db.update(change('Title'), tmp.ISBN == isbn )
print 'Entry Updated'
def change_author(self, isbn):
tmp = Query()
def change(field):
def transform(element):
element[field] = raw_input('Enter Author ')
return transform
title = self.db.search(tmp.ISBN == isbn)[0]
print 'Change author of :- {}'.format(title['Title'])
self.db.update(change('Authors'), tmp.ISBN == isbn )
print 'Entry Updated'
def change_publisher(self, isbn):
tmp = Query()
def change(field):
def transform(element):
element[field] = raw_input('Enter Publisher ')
return transform
title = self.db.search(tmp.ISBN == isbn)[0]
print 'Change Publisher of :- {}'.format(title['Title'])
self.db.update(change('Publisher'), tmp.ISBN == isbn )
print 'Entry Updated'
def write_to_file(self, filename):
try:
data = tabulate(_concat(self.db.all()), headers='keys', tablefmt="simple")
except AttributeError:
print 'No database Connected'
f = open('xlibris/' + filename + '.txt', 'w')
f.write(data.encode('utf8'))
f.write('\n'.encode('utf8'))
f.write('--------------------'.encode('utf8'))
f.write('\n'.encode('utf8'))
f.write(self.count().encode('utf8'))
f.close()
print 'Written to {}'.format('xlibris/' + filename + '.txt')
def purge_current(self):
self.db.purge()
def remove(self, isbn):
tmp = Query()
resp = raw_input('Delete \n {} \n ? (y/n)'.format(tabulate(self.db.search(tmp.ISBN == isbn), headers='keys')))
resp = resp.lower()
if resp == 'y':
for i in ['Publisher', 'Title', 'Authors', 'Year', 'Date Added', 'Language', 'ISBN']:
self.db.update(delete(i), tmp.ISBN == isbn)
print 'Deleted'
elif resp == 'n':
print 'Spared'
def lookup(self, keyword):
data = _concat(self.db.all())
title = data.pop('Title')
auth = data.pop('Authors')
choices = title + auth
searchKey = process.extractBests(keyword, choices)
for i in searchKey:
if i[1] >= 90:
self.search(i[0])
def count(self):
listisbn = _concat(self.db.all())
listisbn = listisbn.pop('ISBN')
return "Total {} books.".format(len(listisbn))
示例12: save
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
def save(self, db_path):
db = TinyDB(db_path)
db.purge()
db.insert({'root': self._data_to_store()})
示例13: __init__
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
#.........这里部分代码省略.........
def __init__(self, db_filename=DEFAULT_DATABASE_FN, size_threshold=None):
"""
Initialize data store.
:param size_threshold: maximum storage size, in number of data objects
:param db_filename: name of the file where the data is stored
"""
self.size_threshold = size_threshold
path = Path(db_filename).parent
if not path.exists():
path.mkdir(parents=True)
self.db = TinyDB(db_filename + ".json")
self.stats = self.db.table('stats')
self.data = self.db.table('messages')
self.lock = RLock()
def add_object(self, data):
"""
Attempt to insert a data object into the store. If it does not exist, it gets initialized. Otherwise the
statistics are updated by increasing the receive count and the time of the last reception if the message has
not been flagged as deleted.
:param data: data object to store
"""
idx = hash_string(data)
now = int(time.time())
with self.lock:
Stats = Query()
res = self.stats.search(Stats.idx == idx)
if len(res) == 0:
self.data.insert({'idx': idx, 'content': data})
self.stats.insert({'idx': idx,
'first_seen': now,
'receive_count': 0,
'send_count': 0,
'last_received': None,
'last_sent': None,
'deleted': False})
log_debug("Data object created: {}".format(data))
else:
deleted = res[0]['deleted']
if deleted:
log_debug("Received deleted data object: {}".format(data))
self.stats.update({'last_received': now}, Stats.idx == idx)
self.stats.update(increment('receive_count'), Stats.idx == idx)
log_debug("Data object updated: {}".format(data))
def get_data(self):
"""
Retrieve the data objects sorted by increasing popularity, namely in increasing receive_count, then send_count
and finally the last time they were sent by the current aDTN node.
:return: data objects sorted by increasing popularity.
"""
with self.lock:
Stats = Query()
stats = self.stats.search(Stats.deleted == False)
res = sorted(stats, key=lambda x: (x['receive_count'], x['send_count'], x['last_sent']))[:10]
now = int(time.time())
objects = []
for r in res:
idx = r['idx']
Objects = Query()
obj = self.data.search(Objects.idx == idx)[0]['content']
objects.append(obj)
self.stats.update({'last_sent': now}, Objects.idx == idx)
self.stats.update(increment('send_count'), Objects.idx == idx)
return objects
def delete_data(self, object_id):
"""
Delete a data object given its ID.
:param object_id: ID of the data object to delete.
"""
with self.lock:
Stats = Query()
Message = Query()
res = self.stats.search(Stats.idx == object_id)
self.stats.update({'deleted': True}, Stats.idx == object_id)
record = self.data.get(Message.idx == object_id)
if record is not None:
self.data.remove(eids=[record.eid])
log_debug("Deleted message: {}".format(object_id))
else:
log_debug("No data to delete: {}".format(object_id))
def list_objects(self):
"""
Print a list of data objects preceded by its object ID.
"""
with self.lock:
objects = ms.data.all()
for obj in objects:
print("{}\t{}".format(obj['idx'], obj['content']))
def wipe(self):
"""
Empty the data store.
"""
with self.lock:
self.stats.purge()
self.data.purge()
self.db.purge()
示例14: TinyDbWrapper
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
class TinyDbWrapper(object):
def __init__(self, db_name, purge = False):
self.db = TinyDB(db_name)
if purge:
self.db.purge()
示例15: TinyDB
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import purge [as 别名]
# -*- coding: utf-8 -
from tinydb import TinyDB, Query
db = TinyDB('db/dataset_category.json')
db.purge()
db.insert({'text': 'ストーリーについて', 'label': 1})
db.insert({'text': 'あらすじについて知りたい', 'label': 1})
db.insert({'text': 'あらすじ', 'label': 1})
db.insert({'text': '話について', 'label': 1})
db.insert({'text': 'どんな話だっけ', 'label': 1})
db.insert({'text': 'どんなストーリーだったか', 'label': 1})
db.insert({'text': 'ストーリー', 'label': 1})
db.insert({'text': '各話のあらすじ', 'label': 1})
db.insert({'text': 'どんな話か', 'label': 1})
db.insert({'text': '登場人物', 'label': 2})
db.insert({'text': '人物について', 'label': 2})
db.insert({'text': 'キャラクター', 'label': 2})
db.insert({'text': 'キャラについて', 'label': 2})
db.insert({'text': '登場する人', 'label': 2})
db.insert({'text': '主人公', 'label': 2})
db.insert({'text': '人', 'label': 2})
db.insert({'text': '人について', 'label': 2})
db.insert({'text': 'キャラ', 'label': 2})
db.insert({'text': '登場人物について', 'label': 2})
db.insert({'text': 'どんな人', 'label': 2})
#print db.all()
db = TinyDB('db/dataset_answer.json')
db.purge()
db.insert({'category_id': 1, 'text': 'りょうについて', 'label': 1})