本文整理汇总了Python中tinydb.TinyDB.insert_multiple方法的典型用法代码示例。如果您正苦于以下问题:Python TinyDB.insert_multiple方法的具体用法?Python TinyDB.insert_multiple怎么用?Python TinyDB.insert_multiple使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tinydb.TinyDB
的用法示例。
在下文中一共展示了TinyDB.insert_multiple方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
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
示例2: db_smartcache
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
def db_smartcache():
db_ = TinyDB(storage=MemoryStorage)
db_.purge_tables()
db_.table_class = SmartCacheTable
db_ = db_.table('_default')
db_.insert_multiple({'int': 1, 'char': c} for c in 'abc')
return db_
示例3: mock_db
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
def mock_db(mocker):
credentials = [
{'login': 'foo', 'name': 'bar', 'fullname': '[email protected]',
'password': '', 'comment': ''},
{'login': 'foa', 'name': 'bazzy', 'fullname': '[email protected]',
'password': '', 'comment': ''},
{'login': 'spam', 'name': 'egg', 'fullname': '[email protected]',
'password': '', 'comment': ''},
]
database = TinyDB(storage=MemoryStorage)
database.insert_multiple(credentials)
MockDB = mock.MagicMock(return_value=database)
mocker.patch('passpie.cli.Database', MockDB)
return database
示例4: test_query_cache
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
def test_query_cache():
db = TinyDB(storage=MemoryStorage)
db.insert_multiple([
{'name': 'foo', 'value': 42},
{'name': 'bar', 'value': -1337}
])
query = where('value') > 0
results = db.search(query)
assert len(results) == 1
# Modify the db instance to not return any results when
# bypassing the query cache
db._table_cache[TinyDB.DEFAULT_TABLE]._read = lambda: {}
# Make sure we got an independent copy of the result list
results.extend([1])
assert db.search(query) == [{'name': 'foo', 'value': 42}]
示例5: write_to_cache_file
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
def write_to_cache_file(
data,
cache_path,
type_id=0,
region_id=0,
logger=logging.getLogger(PROGNAME)
):
"""save data to tinyDB
Args:
data (:obj:`pandas.DataFrame`): data to write out
cache_path (str): path to cache file
type_id (int, optional): EVE Online type_id
region_id (int, optional): EVE Online region_id
logger (:obj:`logging.logger`): logging handle
Returns:
None
"""
## get DB ##
logger.info('Writing data to cache')
tdb = TinyDB(cache_path)
date_min = data['date'].min()
logger.debug(date_min)
## clean out existing entries ##
if (type_id and region_id):
logger.info('--Removing old cache entries')
tdb.remove(
(Query().region_id == region_id) &
(Query().type_id == type_id) &
(Query().date >= date_min)
)
caching_data_str = data.to_json(
date_format='iso',
orient='records'
)
cache_data = json.loads(caching_data_str)
logger.info('--Writing to cache file')
tdb.insert_multiple(cache_data)
示例6: database
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
def database():
"""Initialise a TinyDB database at ./db.json and extends article model
to include read_count and popularity."""
global db
if os.path.isfile("db.json"):
db = TinyDB("db.json")
return None
req = requests.get("https://s3.eu-central-1.amazonaws.com/mm-dmu/arbetsprov/article_data.json")
articles = req.json()["articles"]
for article in articles:
article["read_count"] = 0.0
article["popularity"] = 0.0
db = TinyDB("db.json")
db.insert_multiple(articles)
update_all_articles_popularity()
示例7: test_cutom_mapping_type_with_json
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [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: TinyDB
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
reviews_db = TinyDB('data/reviews.json')
# Load key from yaml file
with open('key.yaml', 'r') as f:
key = yaml.load(f.read())['google-key']
# load language parameter
with open('config.yaml', 'r') as f:
language = yaml.load(f.read())['language']
# template url used to send requests
url_template = ('https://maps.googleapis.com/maps/api/place/details/json?'
'key={key}&placeid={placeid}&language={language}')
# load details for every place
for i in xrange(1, len(places_db)):
# build dic with params to send to the API
params = {'key': key,
'placeid': places_db.get(eid=i)['place_id'],
'language': language}
# send request
res = requests.get(url_template.format(**params))
# obtain reviews if any
try:
reviews = json.loads(res.content)['result']['reviews']
except Exception, e:
print 'Place does not have reviews, skipping...'
else:
# insert reviews in json file
reviews_db.insert_multiple(reviews)
示例9: db
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
def db():
db_ = TinyDB(storage=MemoryStorage)
db_.purge_tables()
db_.insert_multiple({'int': 1, 'char': c} for c in 'abc')
return db_
示例10: db
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
def db():
db = TinyDB(storage=MemoryStorage)
db.insert_multiple({'int': 1, 'char': c} for c in 'abc')
return db
示例11: Database
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
class Database(unittest.TestCase):
DB_NAME = 'test_database.json'
def setUp(self):
self.db = TinyDB(self.DB_NAME)
self.db.insert_multiple([
{"skills": 'python', "github": 'https://github.com/kennethreitz', "time": '0', "points": '',
"email": '[email protected]', "username": 'kennethreitz', 'slack_id': 'U123456789'},
{"skills": 'Duchess', "github": 'https://bitbucket.org/madhatter', "time": '5', "points": '1871',
"email": '[email protected]', "username": 'cheshirecat', 'slack_id': 'U18651871'}
])
self.User = Query()
database.db = self.db
database.User = self.User
def tearDown(self):
self.db.close()
import os
os.remove(self.DB_NAME)
def test_insert_user(self):
self.assertDictEqual(
json.loads(database.insert_user(email='[email protected]', skills='magic, rabbits'))['response'],
dict(skills='magic, rabbits', github='', time='', email='[email protected]', username='', slack_id='', points='0')
)
def test_update_user(self):
self.assertDictEqual(
json.loads(database.update_user(params={'skills': 'requests'}, slack_id='U123456789'))['response']['value'],
{"skills": 'requests', "github": 'https://github.com/kennethreitz', "time": '0', "points": '',
"email": '[email protected]', "username": 'kennethreitz', 'slack_id': 'U123456789'}
)
self.assertDictEqual(
json.loads(database.update_user(params={'username': 'kz'}, slack_id='U123456789'))['response']['value'],
{"skills": 'requests', "github": 'https://github.com/kennethreitz', "time": '0', "points": '',
"email": '[email protected]', "username": 'kz', 'slack_id': 'U123456789'}
)
self.assertEqual(
json.loads(database.update_user(params={'skills': 'django'}, slack_id='nonExisting'))['response']['value'],
None
)
def test_get_user(self):
self.assertDictEqual(
json.loads(database.get_user(slack_id='U18651871'))['response'],
{"skills": 'Duchess', "github": 'https://bitbucket.org/madhatter', "time": '5', "points": '1871',
"email": '[email protected]', "username": 'cheshirecat', 'slack_id': 'U18651871'}
)
self.assertDictEqual(
json.loads(database.get_user(slack_id='U123456789', email='[email protected]'))['response'],
{"skills": 'python', "github": 'https://github.com/kennethreitz', "time": '0', "points": '',
"email": '[email protected]', "username": 'kennethreitz', 'slack_id': 'U123456789'}
)
self.assertDictEqual(
json.loads(database.get_user(email='[email protected]'))['response'],
{'Error': 'User not found'}
)
def test_get_all_users(self):
self.assertListEqual(
json.loads(database.get_all_users())['response'],
[
{"skills": 'python', "github": 'https://github.com/kennethreitz', "time": '0', "points": '',
"email": '[email protected]', "username": 'kennethreitz', 'slack_id': 'U123456789'},
{"skills": 'Duchess', "github": 'https://bitbucket.org/madhatter', "time": '5', "points": '1871',
"email": '[email protected]', "username": 'cheshirecat', 'slack_id': 'U18651871'}
]
)
def test_delete_user(self):
self.assertEqual(
json.loads(database.delete_user(slack_id='U123456789', email='[email protected]', username='cheshirecat'))['response']['id'],
'U123456789'
)
self.assertEqual(
json.loads(database.delete_user(email='[email protected]', username='cheshirecat'))['response']['id'],
'cheshirecat'
)
示例12: open
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
from tinydb import TinyDB
# Load key from yaml file
with open('key.yaml', 'r') as f:
key = yaml.load(f.read())['google-key']
# load configuration parameters
with open('config.yaml', 'r') as f:
config = yaml.load(f.read())
# Google maps API template
url_template = ('https://maps.googleapis.com/maps/api/place/radarsearch/json'
'?key={key}&location={location}&radius={radius}&types={types}')
db = TinyDB('data/places.json')
# generate a request for every type of place
for a_type in config['types'].split('|'):
# Parameters to be included in the request
params = {'location': config['location'],
'key': key,
'radius': config['radius'],
'types': a_type}
# Make the request
res = requests.get(url_template.format(**params))
places = json.loads(res.content)['results']
# Save results in a json file
db.insert_multiple(places)
示例13: db
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
def db():
db = TinyDB(storage=MemoryStorage).table()
db.insert_multiple([{"k": i} for i in [4, 2, 1, 3, 5]])
return db
示例14: multi_db
# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import insert_multiple [as 别名]
def multi_db():
db = TinyDB(storage=MemoryStorage).table()
pairs = [[1, 2], [2, 3], [2, 4], [3, 4], [3, 5]]
shuffle(pairs)
db.insert_multiple([{"k": k, "t": t} for k, t in pairs])
return db