def whosinclass(): return t.reduceby(lambda s: 'present' if s[1] else 'absent' if s[2] else 'nothere', lambda acc,x: acc + [x[0]], [[s.name, s.in_class, datetime.datetime.today().strftime("%Y-%m-%d") in s.absences] for s in data['students'].values()], [])
def compute_up(t, seq, **kwargs): if ((isinstance(t.apply, Reduction) and type(t.apply) in binops) or (isinstance(t.apply, Summary) and builtins.all(type(val) in binops for val in t.apply.values))): grouper, binop, combiner, initial = reduce_by_funcs(t) d = reduceby(grouper, binop, seq, initial) else: grouper = rrowfunc(t.grouper, t._child) groups = groupby(grouper, seq) d = dict((k, compute(t.apply, {t._child: v})) for k, v in groups.items()) if isscalar(t.grouper.dshape.measure): keyfunc = lambda x: (x,) else: keyfunc = identity if isscalar(t.apply.dshape.measure): valfunc = lambda x: (x,) else: valfunc = identity return tuple(keyfunc(k) + valfunc(v) for k, v in d.items())
def compute_one(t, seq, **kwargs): grouper = rrowfunc(t.grouper, t.child) if (isinstance(t.apply, Reduction) and type(t.apply) in binops): binop, initial = binops[type(t.apply)] applier = rrowfunc(t.apply.child, t.child) def binop2(acc, x): return binop(acc, applier(x)) d = reduceby(grouper, binop2, seq, initial) else: groups = groupby(grouper, seq) d = dict((k, compute(t.apply, {t.child: v})) for k, v in groups.items()) if t.grouper.iscolumn: return d.items() else: return tuple(k + (v,) for k, v in d.items())
def match_protocols(self, remote_capabilities: List[Tuple[bytes, int]]): """Match the sub-protocols supported by this Peer with the given remote capabilities. Every sub-protocol and remote-capability are defined by a protocol name and version. This method will get the match with the highest version for every protocol, sort them in ascending alphabetical order and add a Protocol instance for the protocol with that name/version to this peer's list of enabled sub protocols. Each Protocol instance will also have a cmd ID offset, defined as the offset of the previous item (0 for the base protocol) plus the protocol's cmd length (i.e. number of commands). """ matching_capabilities = set(self.capabilities).intersection(remote_capabilities) higher_matching = reduceby( key=operator.itemgetter(0), binop=lambda a, b: a if a[1] > b[1] else b, seq=matching_capabilities) sub_protocols_by_name_and_version = dict( ((klass.name, klass.version), klass) for klass in self._supported_sub_protocols) offset = self.base_protocol.cmd_length for name, version in sorted(higher_matching.values()): proto_klass = sub_protocols_by_name_and_version[(name, version)] self.enabled_sub_protocols.append(proto_klass(self, offset)) offset += proto_klass.cmd_length
def compute(t, seq): parent = compute(t.parent, seq) if (isinstance(t.apply, Reduction) and type(t.apply) in binops): binop, initial = binops[type(t.apply)] applier = rowfunc(t.apply.parent) grouper = rowfunc(t.grouper) def binop2(acc, x): return binop(acc, applier(x)) d = reduceby(grouper, binop2, parent, initial) else: grouper = rowfunc(t.grouper) groups = groupby(grouper, parent) d = dict((k, compute(t.apply, v)) for k, v in groups.items()) if t.grouper.iscolumn: return d.items() else: return tuple(k + (v,) for k, v in d.items())
def reduce_by(self, key, op): return fdict(cytoolz.reduceby(key, op, self))
# like `get(3)` or `lambda x: x[3]`, and a binary operator like # `add` or `lesser = lambda acc, x: acc if acc < x else x`. # It successively applies the key function to each item in succession, # accumulating running totals for each key by combining each new value # with the previous using the binary operator. # It can’t accept full reduction operations like `sum` or `min` as # these require access to the entire group at once. Here's an example: def iseven(n): return n % 2 == 0 def add(x, y): return x + y print(reduceby(iseven, add, [1, 2, 3, 4])) # {False: 4, True: 6} # The even numbers are added together (2 + 4 = 6) into group True, and # the odd numbers are added together (1 + 3 = 4) into group False. # Here is the solution for our accounts example that # adds up the balances for each group: binop = lambda total, acc: total + acc[2] print(reduceby(get(3), binop, accounts), 0) # {'M': 400, 'F': 400} # SEMI-STREAMING `JOIN` # We register multiple datasets together with `join`. # Consider a second dataset storing addresses by ID addresses = [ (1, '123 Main Street'), # id, address (2, '5 Adams Way'), (5, '34 Rue St Michel')