本文整理汇总了Python中BTrees.IIBTree.IITreeSet.insert方法的典型用法代码示例。如果您正苦于以下问题:Python IITreeSet.insert方法的具体用法?Python IITreeSet.insert怎么用?Python IITreeSet.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BTrees.IIBTree.IITreeSet
的用法示例。
在下文中一共展示了IITreeSet.insert方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getPositions
# 需要导入模块: from BTrees.IIBTree import IITreeSet [as 别名]
# 或者: from BTrees.IIBTree.IITreeSet import insert [as 别名]
def getPositions(self, docid, wordid):
""" return a sequence of positions of occurrences of wordid within
a document given by its docid.
"""
encoded_wid = encode((wordid,))
encoded_document = self._doc2wid[docid].get()
positions = IITreeSet()
for pos, wid in enumerate(decode(encoded_document)):
if wid == wordid:
positions.insert(pos)
return positions
示例2: group
# 需要导入模块: from BTrees.IIBTree import IITreeSet [as 别名]
# 或者: from BTrees.IIBTree.IITreeSet import insert [as 别名]
def group(self, seq):
sortIndex = self._sortIndex; sortReverse = self._sortReverse
ns = len(seq); ni = len(sortIndex)
if ns >= 0.1 * ni:
# result large compared to index -- sort via index
handled = IISet(); hn = 0
_load = getattr(sortIndex, '_load', None)
if _load is None:
# not an optimized index
items = sortIndex.items()
_load = lambda (x1, x2): x2
if sortReverse: items.reverse()
elif sortReverse:
gRO = getattr(sortIndex, 'getReverseOrder', None)
items = gRO and gRO()
if items is None:
items = list(sortIndex._index.keys()); items.reverse()
else: items = sortIndex._index.keys()
for i in items:
ids = intersection(seq, _load(i))
if ids:
handled.update(ids); hn += len(ids)
yield i, ids
if hn != len(seq): yield None, difference(seq, handled)
else:
# result relatively small -- sort via result
m = OOBTree()
keyFor = getattr(sortIndex, 'keyForDocument', None)
# work around "nogopip" bug: it defines "keyForDocument" as an integer
if not callable(keyFor):
# this will fail, when the index neither defines a reasonable
# "keyForDocument" nor "documentToKeyMap". In this case,
# the index cannot be used for sorting.
keyFor = lambda doc, map=sortIndex.documentToKeyMap(): map[doc]
noValue = IITreeSet()
for doc in seq.keys():
try: k = keyFor(doc)
except KeyError: noValue.insert(doc); continue
l = m.get(k)
if l is None: l = m[k] = IITreeSet()
l.insert(doc)
items = m.items()
if sortReverse: items = list(items); items.reverse()
for i in items: yield i
if noValue: yield None, noValue
示例3: group
# 需要导入模块: from BTrees.IIBTree import IITreeSet [as 别名]
# 或者: from BTrees.IIBTree.IITreeSet import insert [as 别名]
def group(self, seq):
sortIndex = self._sortIndex; sortReverse = self._sortReverse
ns = len(seq); ni = len(sortIndex)
if ns >= 0.1 * ni:
# result large compared to index -- sort via index
handled = IISet(); hn = 0
_load = getattr(sortIndex, '_load', None)
if _load is None:
# not an optimized index
items = sortIndex.items()
_load = lambda (x1, x2): x2
if sortReverse: items.reverse()
elif sortReverse:
gRO = getattr(sortIndex, 'getReverseOrder', None)
items = gRO and gRO()
if items is None:
items = list(sortIndex._index.keys()); items.reverse()
else: items = sortIndex._index.keys()
for i in items:
ids = intersection(seq, _load(i))
if ids:
handled.update(ids); hn += len(ids)
yield i, ids
if hn != len(seq): yield None, difference(seq, handled)
else:
# result relatively small -- sort via result
keyFor = sortIndex.keyForDocument; m = OOBTree()
noValue = IITreeSet()
for doc in seq.keys():
try: k = keyFor(doc)
except KeyError: noValue.insert(doc); continue
l = m.get(k)
if l is None: l = m[k] = IITreeSet()
l.insert(doc)
items = m.items()
if sortReverse: items = list(items); items.reverse()
for i in items: yield i
if noValue: yield None, noValue
示例4: BooleanIndex
# 需要导入模块: from BTrees.IIBTree import IITreeSet [as 别名]
# 或者: from BTrees.IIBTree.IITreeSet import insert [as 别名]
class BooleanIndex(UnIndex):
"""Index for booleans
self._index = set([documentId1, documentId2])
self._unindex = {documentId:[True/False]}
self._length is the length of the unindex
self._index_length is the length of the index
False doesn't have actual entries in _index.
"""
meta_type = "BooleanIndex"
manage_options = (
{'label': 'Settings',
'action': 'manage_main'},
{'label': 'Browse',
'action': 'manage_browse'},
)
query_options = ["query"]
manage = manage_main = DTMLFile('dtml/manageBooleanIndex', globals())
manage_main._setName('manage_main')
manage_browse = DTMLFile('../dtml/browseIndex', globals())
_index_value = 1
_index_length = None
def clear(self):
self._index = IITreeSet()
self._index_length = BTrees.Length.Length()
self._index_value = 1
self._unindex = IIBTree()
self._length = BTrees.Length.Length()
if self._counter is None:
self._counter = BTrees.Length.Length()
else:
self._increment_counter()
def histogram(self):
"""Return a mapping which provides a histogram of the number of
elements found at each point in the index.
"""
histogram = {}
indexed = bool(self._index_value)
histogram[indexed] = self._index_length.value
histogram[not indexed] = self._length.value - self._index_length.value
return histogram
def _invert_index(self, documentId=None):
self._index_value = indexed = int(not self._index_value)
self._index.clear()
length = 0
for rid, value in self._unindex.iteritems():
if value == indexed:
self._index.add(rid)
length += 1
# documentId is the rid of the currently processed object that
# triggered the invert. in the case of unindexing, the rid hasn't
# been removed from the unindex yet. While indexing, the rid will
# be added to the index and unindex after this method is done
if documentId is not None:
self._index.remove(documentId)
length -= 1
self._index_length = BTrees.Length.Length(length)
def insertForwardIndexEntry(self, entry, documentId):
"""If the value matches the indexed one, insert into treeset
"""
# When we get the first entry, decide to index the opposite of what
# we got, as indexing zero items is fewer than one.
if self._length.value == 0:
self._index_value = int(not bool(entry))
# if the added entry value is index value, insert it into index
if bool(entry) is bool(self._index_value):
self._index_length.change(1)
self._index.insert(documentId)
# insert value into global unindex (before computing index invert)
self._unindex[documentId] = entry
self._length.change(1)
# is the index (after adding the current entry) larger than 60%
# of the total length? than switch the indexed value
if bool(entry) is bool(self._index_value):
if (self._index_length.value) >= ((self._length.value) * 0.6):
self._invert_index()
def removeForwardIndexEntry(self, entry, documentId, check=True):
"""Take the entry provided and remove any reference to documentId
in its entry in the index.
"""
if bool(entry) is bool(self._index_value):
try:
self._index.remove(documentId)
self._index_length.change(-1)
except ConflictError:
#.........这里部分代码省略.........
示例5: MessageStorage
# 需要导入模块: from BTrees.IIBTree import IITreeSet [as 别名]
# 或者: from BTrees.IIBTree.IITreeSet import insert [as 别名]
class MessageStorage(Persistent, Location):
interface.implements(IMessageStorage)
notify = True
principalId = None
def __init__(self, principalId):
self.index = OIBTree()
self.messages = IOBTree()
self.services = OOBTree()
self.readstatus = IITreeSet()
self.principalId = principalId
self._next = Length(1)
@Lazy
def readstatus(self):
self.readstatus = IITreeSet()
return self.readstatus
@property
def principal(self):
try:
return getUtility(IAuthentication).getPrincipal(self.principalId)
except:
return None
@property
def unread(self):
unread = 0
for serviceId in self.services.keys():
service = self.getService(serviceId)
unread = unread + service.unread()
return unread
def getMessage(self, messageId):
return self.messages.get(messageId)
def getServiceIds(self):
return list(self.services.keys())
def getService(self, serviceId):
service = self.services.get(serviceId)
if not IMessageService.providedBy(service):
factory = getUtility(IMessageServiceFactory, serviceId)
service = factory(self)
self.services[serviceId] = service
return service
def create(self, serviceId, **data):
""" create and append message to storage """
id = self._next()
self._next.change(1)
service = self.getService(serviceId)
msg = service.create(**data)
date = datetime.now(ITZInfo(self.principal, pytz.utc))
while date in self.index:
date = date + timedelta
msg.__id__ = id
msg.__date__ = date
self.index[date] = id
self.messages[id] = msg
self.readstatus.insert(id)
service.append(msg)
event.notify(MessageCreatedEvent(msg, self))
return id
def remove(self, messageId):
message = self.messages.get(messageId)
if message is None:
return
else:
self.clearReadStatus(message)
del self.index[message.__date__]
del self.messages[message.__id__]
for serviceId in self.services.keys():
service = self.getService(serviceId)
service.remove(message)
event.notify(MessageRemovedEvent(message, self))
def readStatus(self, message):
return message.__id__ in self.readstatus
def clearReadStatus(self, message):
if message.__id__ not in self.readstatus:
return
#.........这里部分代码省略.........
示例6: DateRangeIndex
# 需要导入模块: from BTrees.IIBTree import IITreeSet [as 别名]
# 或者: from BTrees.IIBTree.IITreeSet import insert [as 别名]
#.........这里部分代码省略.........
def index_object( self, documentId, obj, threshold=None ):
"""
Index an object:
- 'documentId' is the integer ID of the document
- 'obj' is the object to be indexed
- ignore threshold
"""
if self._since_field is None:
return 0
since = getattr( obj, self._since_field, None )
if callable( since ):
since = since()
since = self._convertDateTime( since )
until = getattr( obj, self._until_field, None )
if callable( until ):
until = until()
until = self._convertDateTime( until )
datum = ( since, until )
old_datum = self._unindex.get( documentId, None )
if datum == old_datum: # No change? bail out!
return 0
if old_datum is not None:
old_since, old_until = old_datum
self._removeForwardIndexEntry( old_since, old_until, documentId )
self._insertForwardIndexEntry( since, until, documentId )
self._unindex[ documentId ] = datum
return 1
def unindex_object( self, documentId ):
"""
Remove the object corresponding to 'documentId' from the index.
"""
datum = self._unindex.get( documentId, None )
if datum is None:
return
since, until = datum
self._removeForwardIndexEntry( since, until, documentId )
del self._unindex[ documentId ]
def uniqueValues( self, name=None, withLengths=0 ):
"""
Return a list of unique values for 'name'.
If 'withLengths' is true, return a sequence of tuples, in
the form '( value, length )'.
"""
if not name in ( self._since_field, self._until_field ):
return []
if name == self._since_field:
t1 = self._since
t2 = self._since_only
示例7: DateRangeIndex
# 需要导入模块: from BTrees.IIBTree import IITreeSet [as 别名]
# 或者: from BTrees.IIBTree.IITreeSet import insert [as 别名]
#.........这里部分代码省略.........
return self._unindex.get(documentId, default)
def index_object(self, documentId, obj, threshold=None):
"""Index an object:
- 'documentId' is the integer ID of the document
- 'obj' is the object to be indexed
- ignore threshold
"""
if self._since_field is None:
return 0
since = getattr(obj, self._since_field, None)
if safe_callable(since):
since = since()
since = self._convertDateTime(since)
until = getattr(obj, self._until_field, None)
if safe_callable(until):
until = until()
until = self._convertDateTime(until)
datum = (since, until)
old_datum = self._unindex.get(documentId, None)
if datum == old_datum: # No change? bail out!
return 0
self._increment_counter()
if old_datum is not None:
old_since, old_until = old_datum
self._removeForwardIndexEntry(old_since, old_until, documentId)
self._insertForwardIndexEntry(since, until, documentId)
self._unindex[documentId] = datum
return 1
def unindex_object(self, documentId):
"""Remove the object corresponding to 'documentId' from the index.
"""
datum = self._unindex.get(documentId, None)
if datum is None:
return
self._increment_counter()
since, until = datum
self._removeForwardIndexEntry(since, until, documentId)
del self._unindex[documentId]
def uniqueValues(self, name=None, withLengths=0):
"""Return a sequence of unique values for 'name'.
If 'withLengths' is true, return a sequence of tuples, in
the form '(value, length)'.
"""
if name not in (self._since_field, self._until_field):
raise StopIteration
if name == self._since_field:
sets = (self._since, self._since_only)
else:
sets = (self._until, self._until_only)
示例8: DateRangeIndex
# 需要导入模块: from BTrees.IIBTree import IITreeSet [as 别名]
# 或者: from BTrees.IIBTree.IITreeSet import insert [as 别名]
#.........这里部分代码省略.........
identified by 'documentId'. Return 'default' if not found.
"""
return self._unindex.get(documentId, default)
def index_object(self, documentId, obj, threshold=None):
"""Index an object:
- 'documentId' is the integer ID of the document
- 'obj' is the object to be indexed
- ignore threshold
"""
if self._since_field is None:
return 0
since = getattr(obj, self._since_field, None)
if safe_callable(since):
since = since()
since = self._convertDateTime(since)
until = getattr(obj, self._until_field, None)
if safe_callable(until):
until = until()
until = self._convertDateTime(until)
datum = (since, until)
old_datum = self._unindex.get(documentId, None)
if datum == old_datum: # No change? bail out!
return 0
if old_datum is not None:
old_since, old_until = old_datum
self._removeForwardIndexEntry(old_since, old_until, documentId)
self._insertForwardIndexEntry(since, until, documentId)
self._unindex[documentId] = datum
return 1
def unindex_object(self, documentId):
"""Remove the object corresponding to 'documentId' from the index.
"""
datum = self._unindex.get(documentId, None)
if datum is None:
return
since, until = datum
self._removeForwardIndexEntry(since, until, documentId)
del self._unindex[documentId]
def uniqueValues(self, name=None, withLengths=0):
"""Return a list of unique values for 'name'.
If 'withLengths' is true, return a sequence of tuples, in
the form '(value, length)'.
"""
if not name in (self._since_field, self._until_field):
return []
if name == self._since_field:
t1 = self._since
t2 = self._since_only
else:
t1 = self._until
t2 = self._until_only
result = []
示例9: index_object
# 需要导入模块: from BTrees.IIBTree import IITreeSet [as 别名]
# 或者: from BTrees.IIBTree.IITreeSet import insert [as 别名]
def index_object(self, documentId, obj, threshold=None):
"""Index an object.
- ``documentId`` is the integer ID of the document.
- ``obj`` is the object to be indexed.
- ``threshold`` is the number of words to process between committing
subtransactions. If None, subtransactions are disabled.
For each name in ``getIndexSourceNames``, try to get the named
attribute from ``obj``.
- If the object does not have the attribute, do not add it to the
index for that name.
- If the attribute is a callable, call it to get the value. If
calling it raises an AttributeError, do not add it to the index.
for that name.
"""
# Clear the data structures before indexing the object. This will ensure
# we don't leave any stale data behind when an object gets reindexed.
self.unindex_object(documentId)
### 1. Get the values.
start = self._getattr(self.start_attr, obj)
end = self._getattr(self.end_attr, obj)
if start is None:
# Ignore calls if the obj does not have the start field.
return False
if end is None:
# Singular event
end = start
recurrence = self._getattr(self.recurrence_attr, obj)
if not recurrence:
rule = None
elif isinstance(recurrence, basestring):
# XXX trap and log errors
rule = rrule.rrulestr(recurrence, dtstart=start)
elif isinstance(recurrence, rrule.rrulebase):
rule = recurrence
else:
#XXX Log error
rule = None
# Strip out times from the recurrence:
if rule is not None:
sync_timezone(rule, start.tzinfo)
### 2. Make them into what should be indexed.
# XXX Naive events are not comparable to timezoned events, so we convert
# everything to utctimetuple(). This means naive events are assumed to
# be GMT, but we can live with that at the moment.
start_value = start.utctimetuple()
end_value = end.utctimetuple()
# The end value should be the end of the recurrence, if any:
if rule is not None:
if is_open_ended(rule):
# This recurrence is open ended
end_value = None
else:
duration = end - start
allrecs = [x for x in rule._iter()]
if allrecs:
last = allrecs[-1] + duration
else:
# Real data may have invalud recurrence rules,
# which end before the start for example.
# Then we end up here.
last = end
end_value = last.utctimetuple()
### 3. Store everything in the indexes:
row = self._start2uid.get(start_value, None)
if row is None:
row = IITreeSet((documentId,))
self._start2uid[start_value] = row
else:
row.insert(documentId)
row = self._end2uid.get(end_value, None)
if row is None:
row = IITreeSet((documentId,))
self._end2uid[end_value] = row
else:
row.insert(documentId)
self._uid2start[documentId] = start_value
self._uid2recurrence[documentId] = rule
self._uid2end[documentId] = end_value
self._uid2duration[documentId] = end - start
return True