本文整理汇总了Python中bson.ObjectId方法的典型用法代码示例。如果您正苦于以下问题:Python bson.ObjectId方法的具体用法?Python bson.ObjectId怎么用?Python bson.ObjectId使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bson
的用法示例。
在下文中一共展示了bson.ObjectId方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _write_audit
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def _write_audit(self, user, message, changed_version):
"""
Creates an audit entry, which is much like a snapshot in that
it references versions and provides some history of the changes made.
"""
audit = {'_id': bson.ObjectId(),
'user': user,
'message': message,
'symbol': changed_version.symbol
}
orig_version = changed_version.orig_version.version
new_version = changed_version.new_version.version
audit['orig_v'] = orig_version
audit['new_v'] = new_version
# Update the versions to contain the audit
mongo_retry(self._versions.update_many)({'symbol': changed_version.symbol,
'version': {'$in': [orig_version, new_version]}
},
{'$addToSet': {'parent': audit['_id']}})
# Create the audit entry
mongo_retry(self._audit.insert_one)(audit)
示例2: test_list_version
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def test_list_version(library, fw_pointers_cfg):
with FwPointersCtx(fw_pointers_cfg):
assert len(list(library.list_versions(symbol))) == 0
dates = [None, None, None]
now = dt.utcnow().replace(tzinfo=mktz('UTC'))
for x in six.moves.xrange(len(dates)):
dates[x] = now - dtd(minutes=130 - x)
with patch("bson.ObjectId", return_value=bson.ObjectId.from_datetime(dates[x])):
library.write(symbol, ts1, prune_previous_version=False)
assert len(list(library.list_versions(symbol))) == 3
library.write(symbol, ts1, prune_previous_version=True)
assert len(list(library.list_versions(symbol))) >= 2
versions = list(library.list_versions(symbol))
for i, x in enumerate([4, 3]):
assert versions[i]['symbol'] == symbol
assert versions[i]['date'] >= dates[i]
assert versions[i]['version'] == x
示例3: test_list_version_latest_only
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def test_list_version_latest_only(library):
assert len(list(library.list_versions(symbol))) == 0
dates = [None, None, None]
now = dt.utcnow().replace(tzinfo=mktz('UTC'))
for x in six.moves.xrange(len(dates)):
dates[x] = now - dtd(minutes=20 - x)
with patch("bson.ObjectId", return_value=bson.ObjectId.from_datetime(dates[x])):
library.write(symbol, ts1, prune_previous_version=False)
assert len(list(library.list_versions(symbol))) == 3
library.write(symbol, ts1, prune_previous_version=True)
assert len(list(library.list_versions(symbol, latest_only=True))) == 1
versions = list(library.list_versions(symbol))
for i, x in enumerate([4, ]):
assert versions[i]['symbol'] == symbol
assert versions[i]['date'] >= dates[i]
assert versions[i]['version'] == x
示例4: test_prunes_multiple_versions
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def test_prunes_multiple_versions(library, fw_pointers_cfg):
with FwPointersCtx(fw_pointers_cfg):
coll = library._collection
a = [{'a': 'b'}]
c = [{'c': 'd'}]
# Create an ObjectId
now = dt.utcnow()
with patch("bson.ObjectId", return_value=bson.ObjectId.from_datetime(now - dtd(minutes=125))):
library.write(symbol, a, prune_previous_version=False)
with patch("bson.ObjectId", return_value=bson.ObjectId.from_datetime(now - dtd(minutes=122))):
library.write(symbol, c, prune_previous_version=False)
with patch("bson.ObjectId", return_value=bson.ObjectId.from_datetime(now - dtd(minutes=121))):
library.write(symbol, a, prune_previous_version=False)
with patch("bson.ObjectId", return_value=bson.ObjectId.from_datetime(now - dtd(minutes=119))):
library.write(symbol, c, prune_previous_version=False)
assert mongo_count(coll.versions) == 4
# Prunes all versions older than the most recent version that's older than 10 mins
library.write(symbol, a, prune_previous_version=True)
assert mongo_count(coll.versions) == 3
assert library.read(symbol, as_of=3).data == a
assert library.read(symbol, as_of=4).data == c
assert library.read(symbol, as_of=5).data == a
示例5: test_prunes_multiple_versions_ts
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def test_prunes_multiple_versions_ts(library, fw_pointers_cfg):
with FwPointersCtx(fw_pointers_cfg):
coll = library._collection
a = ts1
c = ts2
# Create an ObjectId
now = dt.utcnow()
with patch("bson.ObjectId", return_value=bson.ObjectId.from_datetime(now - dtd(minutes=125))):
library.write(symbol, a, prune_previous_version=False)
with patch("bson.ObjectId", return_value=bson.ObjectId.from_datetime(now - dtd(minutes=122))):
library.write(symbol, c, prune_previous_version=False)
with patch("bson.ObjectId", return_value=bson.ObjectId.from_datetime(now - dtd(minutes=121))):
library.write(symbol, a, prune_previous_version=False)
with patch("bson.ObjectId", return_value=bson.ObjectId.from_datetime(now - dtd(minutes=119))):
library.write(symbol, c, prune_previous_version=False)
assert mongo_count(coll.versions) == 4
# Prunes all versions older than the most recent version that's older than 10 mins
library.write(symbol, a, prune_previous_version=True)
assert mongo_count(coll.versions) == 3
assert_frame_equal(library.read(symbol, as_of=3).data, a)
assert_frame_equal(library.read(symbol, as_of=4).data, c)
assert_frame_equal(library.read(symbol, as_of=5).data, a)
示例6: test_list_symbols_newer_version_with_lower_id
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def test_list_symbols_newer_version_with_lower_id(library, fw_pointers_cfg):
with FwPointersCtx(fw_pointers_cfg):
now = struct.pack(">i", int(time.time()))
old_id = bson.ObjectId(now + b"\x00\x00\x00\x00\x00\x00\x00\x00")
new_id = bson.ObjectId(now + b"\x00\x00\x00\x00\x00\x00\x00\x01")
object_id_class = Mock()
object_id_class.from_datetime = bson.ObjectId.from_datetime
object_id_class.return_value = new_id
with patch("bson.ObjectId", object_id_class):
library.write(symbol, ts1)
library.snapshot('s1')
object_id_class.return_value = old_id
with patch("bson.ObjectId", object_id_class):
library.delete(symbol)
assert symbol not in library.list_symbols()
示例7: test_list_versions_localTime
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def test_list_versions_localTime():
# Object ID's are stored in UTC. We need to ensure that the returned times
# for versions are in the local TimeZone
vs = create_autospec(VersionStore, instance=True, _versions=Mock(), _snapshots=Mock())
mocked_snap_resp = [{'_id': 'abcde', 'name': 'snap'}]
vs._snapshots.find.return_value = mocked_snap_resp
vs._snapshots.find_one.return_value = mocked_snap_resp
date = dt(2013, 4, 1, 9, 0)
vs._versions.find.return_value = [{'_id': bson.ObjectId.from_datetime(date),
'symbol': 's',
'version': 10,
'metadata': None,
'parent': [mocked_snap_resp[0]['_id']]}]
version = list(VersionStore.list_versions(vs, "symbol"))[0]
local_date = date.replace(tzinfo=mktz("UTC"))
assert version == {'symbol': version['symbol'], 'version': version['version'],
# We return naive datetimes in 'default' time, which is London for us
'date': local_date,
'snapshots': ['snap'],
'deleted': False}
示例8: test_list_versions_no_snapshot
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def test_list_versions_no_snapshot():
vs = create_autospec(VersionStore, instance=True, _versions=Mock(), _snapshots=Mock())
vs._snapshots.find.return_value = []
vs._snapshots.find_one.return_value = []
date = dt(2013, 4, 1, 9, 0)
vs._versions.find.return_value = [{'_id': bson.ObjectId.from_datetime(date),
'symbol': 's',
'version': 10,
'metadata': None,
'parent': []}]
version = list(VersionStore.list_versions(vs, "symbol"))[0]
local_date = date.replace(tzinfo=mktz("UTC"))
assert version == {'symbol': version['symbol'],
'version': version['version'],
'date': local_date,
'snapshots': [],
'deleted': False}
示例9: test_prune_previous_versions_0_timeout
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def test_prune_previous_versions_0_timeout():
self = create_autospec(VersionStore, _versions=Mock())
self.name = sentinel.name
self._versions = create_autospec(Collection)
self._versions.with_options.return_value.find.__name__ = 'find'
self._versions.with_options.return_value.find.return_value = []
with patch('arctic.store.version_store.dt') as dt:
dt.utcnow.return_value = datetime.datetime(2013, 10, 1)
VersionStore._find_prunable_version_ids(self, sentinel.symbol, keep_mins=0)
assert self._versions.with_options.call_args_list == [call(read_preference=ReadPreference.PRIMARY)]
assert self._versions.with_options.return_value.find.call_args_list == [
call({'$or': [{'parent': {'$exists': False}},
{'parent': []}],
'symbol': sentinel.symbol,
'_id': {'$lt': bson.ObjectId('524a10810000000000000000')}},
sort=[('version', -1)],
skip=1,
projection={'FW_POINTERS_CONFIG': 1, '_id': 1, 'SEGMENT_SHAS': 1}
)]
示例10: test_write_metadata_with_previous_data
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def test_write_metadata_with_previous_data():
vs = _create_mock_versionstore()
expected_new_version = TPL_VERSION.copy()
expected_new_version.update({'_id': MOCK_OBJID,
'version': TPL_VERSION['version'] + 1,
'metadata': META_TO_WRITE})
expected_ret_val = VersionedItem(symbol=TEST_SYMBOL,
library=vs._arctic_lib.get_name(),
host=vs._arctic_lib.arctic.mongo_host,
version=TPL_VERSION['version'] + 1,
metadata=META_TO_WRITE,
data=None)
with patch('arctic.store.version_store.bson.ObjectId') as mock_objId,\
patch('arctic.store.version_store.mongo_retry') as mock_retry:
mock_objId.return_value = MOCK_OBJID
mock_retry.side_effect = lambda f: f
assert expected_ret_val == VersionStore.write_metadata(vs, symbol=TEST_SYMBOL, metadata=META_TO_WRITE)
assert vs._versions.insert_one.call_args_list == [call(expected_new_version)]
assert vs._versions.delete_one.called is False
assert vs.write.called is False
示例11: test_restore_last_version
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def test_restore_last_version():
vs = _create_mock_versionstore()
vs._version_nums.find_one.return_value = {'version': TPL_VERSION['version']}
vs._read_metadata.side_effect = [TPL_VERSION, TPL_VERSION]
with patch('arctic.store.version_store.bson.ObjectId') as mock_objId, \
patch('arctic.store.version_store.mongo_retry') as mock_retry:
mock_objId.return_value = MOCK_OBJID
mock_retry.side_effect = lambda f: f
ret_item = VersionStore.restore_version(vs, symbol=TEST_SYMBOL, as_of=TPL_VERSION['version'],
prune_previous_version=True)
assert ret_item.version == TPL_VERSION['version']
assert ret_item.metadata == TPL_VERSION.get('metadata')
assert vs._read_metadata.call_args_list == [call(TEST_SYMBOL, as_of=TPL_VERSION['version'])]
assert vs._version_nums.find_one.call_args_list == [call({'symbol': TEST_SYMBOL})]
assert not vs.read.called
assert not vs.write.called
示例12: from_id
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def from_id(cls, ctx, _id, meta_only=False):
if not isinstance(_id, ObjectId):
_id = ObjectId(_id)
if meta_only:
obj = await cls.data_coll(ctx).find_one({"_id": _id}, ['_id', 'name', 'owner', 'public'])
else:
obj = await cls.data_coll(ctx).find_one({"_id": _id})
if obj is None:
raise NoActiveBrew()
if not meta_only:
return cls.from_dict(obj)
return obj
# helpers
示例13: follow_user
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def follow_user(self, request):
"""Adds the current user as follower of the given user."""
username = request.match_info['username']
session = await get_session(request)
user_id = session.get('user_id')
if not user_id:
raise web.HTTPNotAuthorized()
whom_id = await db.get_user_id(self.mongo.user, username)
if whom_id is None:
raise web.HTTPFound()
await self.mongo.follower.update_many(
{'who_id': ObjectId(user_id)},
{'$push': {'whom_id': whom_id}}, upsert=True)
return redirect(request, 'user_timeline', username=username)
示例14: unfollow_user
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def unfollow_user(self, request):
"""Removes the current user as follower of the given user."""
username = request.match_info['username']
session = await get_session(request)
user_id = session.get('user_id')
if not user_id:
raise web.HTTPNotAuthorized()
whom_id = await db.get_user_id(self.mongo.user, username)
if whom_id is None:
raise web.HTTPFound()
await self.mongo.follower.update_many(
{'who_id': ObjectId(session['user_id'])},
{'$pull': {'whom_id': whom_id}})
return redirect(request, 'user_timeline', username=username)
示例15: add_message
# 需要导入模块: import bson [as 别名]
# 或者: from bson import ObjectId [as 别名]
def add_message(self, request):
"""Registers a new message for the user."""
session = await get_session(request)
user_id = session.get('user_id')
if not user_id:
raise web.HTTPNotAuthorized()
form = await request.post()
if form.get('text'):
user = await self.mongo.user.find_one(
{'_id': ObjectId(session['user_id'])},
{'email': 1, 'username': 1})
await self.mongo.message.insert_one(
{'author_id': ObjectId(user_id),
'email': user['email'],
'username': user['username'],
'text': form['text'],
'pub_date': datetime.datetime.utcnow()})
return redirect(request, 'timeline')