def __or__(self, other):
return self.union(other)
python类__or__()的实例源码
def union(self, other):
if isinstance(other, BitSet):
return self._logic(self.copy(), operator.__or__, other)
b = self.copy()
b.update(other)
return b
def column_like(name, patterns, default):
""" patterns: dict[label, list[match_expr]]"""
# start with the pyspark.sql.functions
op = F
for label in patterns:
cond = reduce(
operator.__or__,
[F.col(name).like(pat) for pat in patterns[label]]
)
op = op.when(cond, label)
return op.otherwise(default)
def __or__(self, rhs):
return op_binary(self, rhs, operator.__or__)
def __ror__(self, lhs):
return op_binary(lhs, self, operator.__or__)
def __ior__(self, v):
return self.op_binary_inplace(v, operator.__or__)
def __or__(self, other):
return self.union(other)
def union(self, other):
if isinstance(other, BitSet):
return self._logic(self.copy(), operator.__or__, other)
b = self.copy()
b.update(other)
return b
def __or__(self, other):
return self.union(other)
def union(self, other):
if isinstance(other, BitSet):
return self._logic(self.copy(), operator.__or__, other)
b = self.copy()
b.update(other)
return b
def __or__(self, other):
with self._lock:
return operator.__or__(self.__wrapped__, other)
def __ror__(self, other):
with self._lock:
return operator.__or__(other, self.__wrapped__)
# inplace operations
def _handle_search_param(self):
search_param = self.params.get('search')
if not search_param:
return
fields = [f.name for f in self.model._meta.get_fields() if not f.is_relation]
q_list = [Q(**{'%s__icontains' % f : search_param}) for f in fields]
related = [f for f in self.model._meta.get_fields() if isinstance(f, ForeignKey)]
for rel in related:
model = rel.related_model
r_fields = [f.name for f in model._meta.get_fields() if not f.is_relation]
q_list += [Q(**{'%s__%s__icontains' % (rel.name, r_field): search_param}) for r_field in r_fields]
search_filter = reduce(OR, q_list)
return search_filter
def __or__(self, other):
return self.union(other)
def union(self, other):
if isinstance(other, BitSet):
return self._logic(self.copy(), operator.__or__, other)
b = self.copy()
b.update(other)
return b
def intersection(self, geometry):
try:
geoms = geometry.geoms
except AttributeError:
return set(self._index.intersection(geometry.bounds))
else:
return reduce(operator.__or__, (self.intersection(geom) for geom in geoms), set())
def __or__(self, other):
"""
Public method of OR operator: a|b. It call or_() internal method.
If or_() returns None: RegexOr object is used (and otherwise,
use or_() result).
"""
# Try to optimize (a|b)
new_regex = self.or_(other)
if new_regex:
return new_regex
# Else use (a|b)
return RegexOr((self, other))
def join(cls, regex):
"""
>>> RegexOr.join( (RegexString('a'), RegexString('b'), RegexString('c')) )
<RegexRange '[a-c]'>
"""
return _join(operator.__or__, regex)
def merge(old, new):
prims = ImmutableTreeList.merge(old.prims, new.prims, operator.__and__)
arrs = ImmutableTreeList.merge(old.arrs, new.arrs, arrays.merge)
tainted = ImmutableTreeList.merge(old.tainted, new.tainted, operator.__or__)
if prims is old.prims and arrs is old.arrs and tainted is old.tainted:
return old
return TypeInfo(prims, arrs, tainted)
def _update_spent(self):
today = date.today()
first_month_day = date(today.year, today.month, 1)
spent = Record.objects.filter(user=self.user,
transaction_type='EXP',
created_at__gte=first_month_day)
tags_list = []
for tag in self.get_tags_list():
tags_list.append(Q(tags=getattr(Record.tags, tag)))
if self.tags_type == 'INCL':
spent = spent.filter(reduce(OR, tags_list))
if self.tags_type == 'EXCL':
spent = spent.exclude(reduce(OR, tags_list))
spent = spent.aggregate(spent=Sum('amount'))
self.__spent = spent['spent'] if spent['spent'] else 0
def __or__(self, other):
return self.union(other)
def union(self, other):
if isinstance(other, BitSet):
return self._logic(self.copy(), operator.__or__, other)
b = self.copy()
b.update(other)
return b
def test_or(self):
self._test_incompatible_types_fail(operator.__or__)
self.assertEqual(no_flags | no_flags, no_flags)
self.assertEqual(no_flags | all_flags, all_flags)
self.assertEqual(no_flags | f0, f0)
self.assertEqual(no_flags | f1, f1)
self.assertEqual(no_flags | f2, f2)
self.assertEqual(no_flags | f01, f01)
self.assertEqual(no_flags | f02, f02)
self.assertEqual(no_flags | f12, f12)
self.assertEqual(f0 | no_flags, f0)
self.assertEqual(f0 | all_flags, all_flags)
self.assertEqual(f0 | f0, f0)
self.assertEqual(f0 | f1, f01)
self.assertEqual(f0 | f2, f02)
self.assertEqual(f0 | f01, f01)
self.assertEqual(f0 | f02, f02)
self.assertEqual(f0 | f12, all_flags)
self.assertEqual(f01 | no_flags, f01)
self.assertEqual(f01 | all_flags, all_flags)
self.assertEqual(f01 | f0, f01)
self.assertEqual(f01 | f1, f01)
self.assertEqual(f01 | f2, all_flags)
self.assertEqual(f01 | f01, f01)
self.assertEqual(f01 | f02, all_flags)
self.assertEqual(f01 | f12, all_flags)
self.assertEqual(all_flags | no_flags, all_flags)
self.assertEqual(all_flags | all_flags, all_flags)
self.assertEqual(all_flags | f0, all_flags)
self.assertEqual(all_flags | f1, all_flags)
self.assertEqual(all_flags | f2, all_flags)
self.assertEqual(all_flags | f01, all_flags)
self.assertEqual(all_flags | f02, all_flags)
self.assertEqual(all_flags | f12, all_flags)
def can_control_read(self, reg):
flag_bits = CGC_FLAG_PAGE >> 12
# shouldn't unset any already good bits
orig_bits = self.orig_regs[reg] >> 12
orig_matching_bits = (~(flag_bits ^ orig_bits)) & 0xfffff
curr_best_matches = []
for i in self.byte_analysis:
ast_vals = [x["AST"] for x in self.byte_analysis[i].reg_vals.values()]
for a in ast_vals:
bits = a >> 12
matching_bits = ~(flag_bits ^ bits) & 0xfffff
if matching_bits & orig_matching_bits != orig_matching_bits:
continue
else:
is_better_than_curr = True
for b in list(curr_best_matches):
matching_bits_b = ~(flag_bits ^ b) & 0xfffff
if matching_bits & matching_bits_b == matching_bits:
is_better_than_curr = False
elif matching_bits & matching_bits_b == matching_bits_b:
curr_best_matches.remove(b)
if is_better_than_curr:
curr_best_matches.append(bits)
# verify it can be pointed at flag page
all_bits = reduce(operator.__or__, [~(x ^ flag_bits) & 0xfffff for x in curr_best_matches])
if bin(all_bits).count("1") < 20:
return False
match_dict = defaultdict(set)
# now get all bytes that match each best
for i in self.byte_analysis:
for b in self.byte_analysis[i].reg_vals:
a = self.byte_analysis[i].reg_vals[b][reg]
bits = a >> 12
if bits in curr_best_matches:
match_dict[bits].add((i, b))
return True
def can_control_read(self, reg):
flag_bits = CGC_FLAG_PAGE >> 12
# shouldn't unset any already good bits
orig_bits = self.orig_regs[reg] >> 12
orig_matching_bits = (~(flag_bits ^ orig_bits)) & 0xfffff
curr_best_matches = []
for i in self.byte_analysis:
ast_vals = [x["AST"] for x in self.byte_analysis[i].reg_vals.values()]
for a in ast_vals:
bits = a >> 12
matching_bits = ~(flag_bits ^ bits) & 0xfffff
if matching_bits & orig_matching_bits != orig_matching_bits:
continue
else:
is_better_than_curr = True
for b in list(curr_best_matches):
matching_bits_b = ~(flag_bits ^ b) & 0xfffff
if matching_bits & matching_bits_b == matching_bits:
is_better_than_curr = False
elif matching_bits & matching_bits_b == matching_bits_b:
curr_best_matches.remove(b)
if is_better_than_curr:
curr_best_matches.append(bits)
# verify it can be pointed at flag page
if len(curr_best_matches) > 0:
all_bits = reduce(operator.__or__, [~(x ^ flag_bits) & 0xfffff for x in curr_best_matches])
else:
all_bits = 0
if bin(all_bits).count("1") < 20:
return False
match_dict = defaultdict(set)
# now get all bytes that match each best
for i in self.byte_analysis:
for b in self.byte_analysis[i].reg_vals:
a = self.byte_analysis[i].reg_vals[b][reg]
bits = a >> 12
if bits in curr_best_matches:
match_dict[bits].add((i, b))
return True
def _items(self, cart_status, category=None):
''' Aggregates the items that this user has purchased.
Arguments:
cart_status (int or Iterable(int)): etc
category (Optional[models.inventory.Category]): the category
of items to restrict to.
Returns:
[ProductAndQuantity, ...]: A list of product-quantity pairs,
aggregating like products from across multiple invoices.
'''
if not isinstance(cart_status, Iterable):
cart_status = [cart_status]
status_query = (
Q(productitem__cart__status=status) for status in cart_status
)
in_cart = Q(productitem__cart__user=self.user)
in_cart = in_cart & reduce(operator.__or__, status_query)
quantities_in_cart = When(
in_cart,
then="productitem__quantity",
)
quantities_or_zero = Case(
quantities_in_cart,
default=Value(0),
)
products = inventory.Product.objects
if category:
products = products.filter(category=category)
products = products.select_related("category")
products = products.annotate(quantity=Sum(quantities_or_zero))
products = products.filter(quantity__gt=0)
out = []
for prod in products:
out.append(ProductAndQuantity(prod, prod.quantity))
return out