本文整理汇总了Python中bson.timestamp.Timestamp方法的典型用法代码示例。如果您正苦于以下问题:Python timestamp.Timestamp方法的具体用法?Python timestamp.Timestamp怎么用?Python timestamp.Timestamp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bson.timestamp
的用法示例。
在下文中一共展示了timestamp.Timestamp方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dumps
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def dumps(obj, *args, **kwargs):
"""Helper function that wraps :func:`json.dumps`.
Recursive function that handles all BSON types including
:class:`~bson.binary.Binary` and :class:`~bson.code.Code`.
:Parameters:
- `json_options`: A :class:`JSONOptions` instance used to modify the
encoding of MongoDB Extended JSON types. Defaults to
:const:`DEFAULT_JSON_OPTIONS`.
.. versionchanged:: 3.4
Accepts optional parameter `json_options`. See :class:`JSONOptions`.
.. versionchanged:: 2.7
Preserves order when rendering SON, Timestamp, Code, Binary, and DBRef
instances.
"""
json_options = kwargs.pop("json_options", DEFAULT_JSON_OPTIONS)
return json.dumps(_json_convert(obj, json_options), *args, **kwargs)
示例2: get_consistent_end_ts
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def get_consistent_end_ts(self):
end_ts = None
bkp_end_ts = self.get_backup_end_max_ts()
for shard in self.tailed_oplogs:
instance = self.tailed_oplogs[shard]
if 'last_ts' in instance and instance['last_ts'] is not None:
last_ts = instance['last_ts']
if end_ts is None or last_ts < end_ts:
end_ts = last_ts
if last_ts < bkp_end_ts:
end_ts = bkp_end_ts
if end_ts is None:
# Happens when there were _no_ oplog changes since the backup
# end, i. e. when all tailed-oplogs are empty
end_ts = bkp_end_ts
return Timestamp(end_ts.time + 1, 0)
示例3: _get_timestamp
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def _get_timestamp(
data, position, as_class, tz_aware, uuid_subtype, compile_re):
inc, position = _get_int(data, position, unsigned=True)
timestamp, position = _get_int(data, position, unsigned=True)
return Timestamp(timestamp, inc), position
示例4: dumps
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def dumps(obj, *args, **kwargs):
"""Helper function that wraps :class:`json.dumps`.
Recursive function that handles all BSON types including
:class:`~bson.binary.Binary` and :class:`~bson.code.Code`.
.. versionchanged:: 2.7
Preserves order when rendering SON, Timestamp, Code, Binary, and DBRef
instances. (But not in Python 2.4.)
"""
if not json_lib:
raise Exception("No json library available")
return json.dumps(_json_convert(obj), *args, **kwargs)
示例5: advance_cluster_time
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def advance_cluster_time(self, cluster_time):
"""Update the cluster time for this session.
:Parameters:
- `cluster_time`: The
:data:`~pymongo.client_session.ClientSession.cluster_time` from
another `ClientSession` instance.
"""
if not isinstance(cluster_time, abc.Mapping):
raise TypeError(
"cluster_time must be a subclass of collections.Mapping")
if not isinstance(cluster_time.get("clusterTime"), Timestamp):
raise ValueError("Invalid cluster_time")
self._advance_cluster_time(cluster_time)
示例6: advance_operation_time
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def advance_operation_time(self, operation_time):
"""Update the operation time for this session.
:Parameters:
- `operation_time`: The
:data:`~pymongo.client_session.ClientSession.operation_time` from
another `ClientSession` instance.
"""
if not isinstance(operation_time, Timestamp):
raise TypeError("operation_time must be an instance "
"of bson.timestamp.Timestamp")
self._advance_operation_time(operation_time)
示例7: _get_timestamp
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def _get_timestamp(data, position, dummy0, dummy1, dummy2):
"""Decode a BSON timestamp to bson.timestamp.Timestamp."""
end = position + 8
inc, timestamp = _UNPACK_TIMESTAMP(data[position:end])
return Timestamp(timestamp, inc), end
示例8: _encode_timestamp
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def _encode_timestamp(name, value, dummy0, dummy1):
"""Encode bson.timestamp.Timestamp."""
return b"\x11" + name + _PACK_TIMESTAMP(value.inc, value.time)
示例9: stop
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def stop(self, kill=False, sleep_secs=3):
if not self.enabled():
return
logging.info("Stopping all oplog tailers")
for shard in self.shards:
replset = self.replsets[shard]
state = self.shards[shard]['state']
thread = self.shards[shard]['thread']
try:
uri = MongoUri(state.get('uri'))
except Exception, e:
raise OperationError(e)
if not kill:
# get current optime of replset primary to use a stop position
try:
timestamp = replset.primary_optime(True, True)
except Exception:
logging.warning("Could not get current optime from PRIMARY! Using now as a stop time")
timestamp = Timestamp(int(time()), 0)
# wait for replication to get in sync
while state.get('last_ts') and state.get('last_ts') < timestamp:
logging.info('Waiting for %s tailer to reach ts: %s, currrent: %s' % (uri, timestamp, state.get('last_ts')))
sleep(sleep_secs)
# set thread stop event
self.shards[shard]['stop'].set()
if kill:
thread.terminate()
sleep(1)
# wait for thread to stop
while thread.is_alive():
logging.info('Waiting for tailer %s to stop' % uri)
sleep(sleep_secs)
# gather state info
self._summary[shard] = state.get().copy()
示例10: dumps
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def dumps(obj, *args, **kwargs):
"""Helper function that wraps :class:`json.dumps`.
Recursive function that handles all BSON types including
:class:`~bson.binary.Binary` and :class:`~bson.code.Code`.
.. versionchanged:: 2.7
Preserves order when rendering SON, Timestamp, Code, Binary, and DBRef
instances.
"""
return json.dumps(_json_convert(obj), *args, **kwargs)
示例11: _get_timestamp
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def _get_timestamp(data, view, position, dummy0, dummy1, dummy2):
"""Decode a BSON timestamp to bson.timestamp.Timestamp."""
inc, timestamp = _UNPACK_TIMESTAMP_FROM(data, position)
return Timestamp(timestamp, inc), position + 8
示例12: test_timestamp
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def test_timestamp(self):
dct = {"ts": Timestamp(4, 13)}
res = bsonjs_dumps(dct)
self.assertEqual('{ "ts" : { "$timestamp" : { "t" : 4, "i" : 13 } } }',
res)
rtdct = bsonjs_loads(res)
self.assertEqual(dct, rtdct)
示例13: default
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def default(obj):
# We preserve key order when rendering SON, DBRef, etc. as JSON by
# returning a SON for those types instead of a dict. This works with
# the "json" standard library in Python 2.6+ and with simplejson
# 2.1.0+ in Python 2.5+, because those libraries iterate the SON
# using PyIter_Next. Python 2.4 must use simplejson 2.0.9 or older,
# and those versions of simplejson use the lower-level PyDict_Next,
# which bypasses SON's order-preserving iteration, so we lose key
# order in Python 2.4.
if isinstance(obj, ObjectId):
return {"$oid": str(obj)}
if isinstance(obj, DBRef):
return _json_convert(obj.as_doc())
if isinstance(obj, datetime.datetime):
# TODO share this code w/ bson.py?
if obj.utcoffset() is not None:
obj = obj - obj.utcoffset()
millis = int(calendar.timegm(obj.timetuple()) * 1000 +
obj.microsecond / 1000)
return {"$date": millis}
if isinstance(obj, (RE_TYPE, Regex)):
flags = ""
if obj.flags & re.IGNORECASE:
flags += "i"
if obj.flags & re.LOCALE:
flags += "l"
if obj.flags & re.MULTILINE:
flags += "m"
if obj.flags & re.DOTALL:
flags += "s"
if obj.flags & re.UNICODE:
flags += "u"
if obj.flags & re.VERBOSE:
flags += "x"
if isinstance(obj.pattern, str):
pattern = obj.pattern
else:
pattern = obj.pattern.decode('utf-8')
return SON([("$regex", pattern), ("$options", flags)])
if isinstance(obj, MinKey):
return {"$minKey": 1}
if isinstance(obj, MaxKey):
return {"$maxKey": 1}
if isinstance(obj, Timestamp):
return {"$timestamp": SON([("t", obj.time), ("i", obj.inc)])}
if isinstance(obj, Code):
return SON([('$code', str(obj)), ('$scope', obj.scope)])
if isinstance(obj, Binary):
return SON([
('$binary', base64.b64encode(obj).decode()),
('$type', "%02x" % obj.subtype)])
if PY3 and isinstance(obj, binary_type):
return SON([
('$binary', base64.b64encode(obj).decode()),
('$type', "00")])
if bson.has_uuid() and isinstance(obj, bson.uuid.UUID):
return {"$uuid": obj.hex}
raise TypeError("%r is not JSON serializable" % obj)
示例14: default
# 需要导入模块: from bson import timestamp [as 别名]
# 或者: from bson.timestamp import Timestamp [as 别名]
def default(obj):
# We preserve key order when rendering SON, DBRef, etc. as JSON by
# returning a SON for those types instead of a dict.
if isinstance(obj, ObjectId):
return {"$oid": str(obj)}
if isinstance(obj, DBRef):
return _json_convert(obj.as_doc())
if isinstance(obj, datetime.datetime):
# TODO share this code w/ bson.py?
if obj.utcoffset() is not None:
obj = obj - obj.utcoffset()
millis = int(calendar.timegm(obj.timetuple()) * 1000 +
obj.microsecond / 1000)
return {"$date": millis}
if isinstance(obj, (RE_TYPE, Regex)):
flags = ""
if obj.flags & re.IGNORECASE:
flags += "i"
if obj.flags & re.LOCALE:
flags += "l"
if obj.flags & re.MULTILINE:
flags += "m"
if obj.flags & re.DOTALL:
flags += "s"
if obj.flags & re.UNICODE:
flags += "u"
if obj.flags & re.VERBOSE:
flags += "x"
if isinstance(obj.pattern, text_type):
pattern = obj.pattern
else:
pattern = obj.pattern.decode('utf-8')
return SON([("$regex", pattern), ("$options", flags)])
if isinstance(obj, MinKey):
return {"$minKey": 1}
if isinstance(obj, MaxKey):
return {"$maxKey": 1}
if isinstance(obj, Timestamp):
return {"$timestamp": SON([("t", obj.time), ("i", obj.inc)])}
if isinstance(obj, Code):
return SON([('$code', str(obj)), ('$scope', obj.scope)])
if isinstance(obj, Binary):
return SON([
('$binary', base64.b64encode(obj).decode()),
('$type', "%02x" % obj.subtype)])
if PY3 and isinstance(obj, bytes):
return SON([
('$binary', base64.b64encode(obj).decode()),
('$type', "00")])
if isinstance(obj, uuid.UUID):
return {"$uuid": obj.hex}
raise TypeError("%r is not JSON serializable" % obj)