本文整理汇总了Python中django.utils.datastructures.OrderedSet类的典型用法代码示例。如果您正苦于以下问题:Python OrderedSet类的具体用法?Python OrderedSet怎么用?Python OrderedSet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OrderedSet类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_len
def test_len(self):
s = OrderedSet()
self.assertEqual(len(s), 0)
s.add(1)
s.add(2)
s.add(2)
self.assertEqual(len(s), 2)
示例2: navigation_event_ids_by_user
def navigation_event_ids_by_user(user, start_date=None, end_date=None):
database = NavigationEventAudit.get_db()
def _date_key(date):
return [date.year, date.month, date.day]
startkey = [user]
if start_date:
startkey.extend(_date_key(start_date))
endkey = [user]
if end_date:
end = end_date + timedelta(days=1)
endkey.extend(_date_key(end))
else:
endkey.append({})
ids = OrderedSet()
results = database.view(
'auditcare/urlpath_by_user_date',
startkey=startkey,
endkey=endkey,
reduce=False,
include_docs=False,
)
for row in results:
ids.add(row['id'])
return ids
示例3: merge
def merge(*lists):
"""
Merge lists while trying to keep the relative order of the elements.
Warn if the lists have the same elements in a different relative order.
For static assets it can be important to have them included in the DOM
in a certain order. In JavaScript you may not be able to reference a
global or in CSS you might want to override a style.
"""
dependency_graph = defaultdict(set)
all_items = OrderedSet()
for list_ in filter(None, lists):
head = list_[0]
# The first items depend on nothing but have to be part of the
# dependency graph to be included in the result.
dependency_graph.setdefault(head, set())
for item in list_:
all_items.add(item)
# No self dependencies
if head != item:
dependency_graph[item].add(head)
head = item
try:
return stable_topological_sort(all_items, dependency_graph)
except CyclicDependencyError:
warnings.warn(
'Detected duplicate Media files in an opposite order: {}'.format(
', '.join(repr(l) for l in lists)
), MediaOrderConflictWarning,
)
return list(all_items)
示例4: get_parent_list
def get_parent_list(self):
"""
Return all the ancestors of this model as a list ordered by MRO.
Useful for determining if something is an ancestor, regardless of lineage.
"""
result = OrderedSet(self.parents)
for parent in self.parents:
for ancestor in parent._meta.get_parent_list():
result.add(ancestor)
return list(result)
示例5: fields_for_document
def fields_for_document(document, fields=None, exclude=None, widgets=None,
formfield_callback=None,
field_generator=MongoFormFieldGenerator):
"""
Returns a ``OrderedSet`` containing form fields for the given model.
``fields`` is an optional list of field names. If provided, only the named
fields will be included in the returned fields.
``exclude`` is an optional list of field names. If provided, the named
fields will be excluded from the returned fields, even if they are listed
in the ``fields`` argument.
"""
field_list = []
ignored = []
if isinstance(field_generator, type):
field_generator = field_generator()
sorted_fields = sorted(document._fields.values(),
key=lambda field: field.creation_counter)
for f in sorted_fields:
if isinstance(f, ObjectIdField):
continue
if isinstance(f, ListField) and not (f.field.choices or isinstance(f.field, ReferenceField)):
continue
if fields is not None and not f.name in fields:
continue
if exclude and f.name in exclude:
continue
if widgets and f.name in widgets:
kwargs = {'widget': widgets[f.name]}
else:
kwargs = {}
if formfield_callback is None:
form_field = field_generator.generate(f, **kwargs)
elif not callable(formfield_callback):
raise TypeError('formfield_callback must be a function or callable')
else:
form_field = formfield_callback(f, **kwargs)
if form_field:
field_list.append((f.name, form_field))
else:
ignored.append(f.name)
field_dict = OrderedSet(field_list)
if fields:
field_dict = OrderedSet(
[(f, field_dict.get(f)) for f in fields
if ((not exclude) or (exclude and f not in exclude)) and (f not in ignored)]
)
return field_dict
示例6: test_bool
def test_bool(self):
# Refs #23664
s = OrderedSet()
self.assertFalse(s)
s.add(1)
self.assertTrue(s)