def test_summary_complex_arith_multiple(bank): expr = by(t.name, arith=(100 - t.amount * 2 / 30.0).sum(), other=t.amount.mean()) result = compute(expr, bank) reducer = lambda acc, x: (100 - x['amount'] * 2 / 30.0) + acc expected = reduceby('name', reducer, bank.find(), 0) mu = reduceby('name', lambda acc, x: acc + x['amount'], bank.find(), 0.0) values = list(mu.values()) items = expected.items() counts = groupby('name', bank.find()) items = [x + (float(v) / len(counts[x[0]]),) for x, v in zip(items, values)] assert set(result) == set(items)
def additive_combinez( duplicated_questions: Iterable[NextQuestionScore] ) -> Iterable[NextQuestionScore]: return toolz.reduceby(key=NextQuestionScore.get_question_id, binop=lambda q1, q2: NextQuestionScore( q1.question_id, q1.score + q2.score), seq=duplicated_questions).values()
def get_stats_by_host_class(self): all_nodes = self.get_nodes() nodes = filter(lambda node: self.matches_hostname_regex(node['hostname']), all_nodes) nodes_by_class = reduceby( lambda node: self.extract_host_class(node['hostname']), lambda x, y: x, nodes) return dict((host_class, self.get_stats_dict(sample_host)) for host_class, sample_host in nodes_by_class.items())
def get_stats_by_host_class(self): nodes = self.get_nodes() nodes_by_class = reduceby( lambda node: self.match_hostname(node['hostname']), lambda x, y: x, nodes) return dict((host_class, self.get_stats_dict(sample_host)) for host_class, sample_host in nodes_by_class.items())
def _get_item_groups(args): def get_tax_rate(item_tax_rate): try: tax_rates = json.loads(item_tax_rate) return sum([v for k, v in tax_rates.items()]) except TypeError: 0 def set_tax_and_total(row): tax_amount = ( get_tax_rate(row.get("item_tax_rate")) * row.get("net_amount") / 100 ) return merge( row, { "tax_amount": tax_amount, "grand_total": tax_amount + row.get("net_amount"), }, ) groups = reduceby( "item_group", lambda a, x: { "qty": a.get("qty") + x.get("qty"), "net_amount": a.get("net_amount") + x.get("net_amount"), "tax_amount": a.get("tax_amount") + x.get("tax_amount"), "grand_total": a.get("grand_total") + x.get("grand_total"), }, ( set_tax_and_total(x) for x in frappe.db.sql( """ SELECT sii.item_code, sii.item_group, sii.qty, sii.net_amount, sii.item_tax_rate FROM `tabSales Invoice Item` AS sii LEFT JOIN `tabSales Invoice` AS si ON si.name = sii.parent WHERE {clauses} """.format( clauses=_get_clauses(args) ), values=args, as_dict=1, ) ), {"qty": 0, "net_amount": 0, "tax_amount": 0, "grand_total": 0}, ) return [merge(v, {"item_group": k}) for k, v in groups.items()]
def _schedule(sl, obj): if obj.current_state == "queued": sl.data[obj.number]["current_state"] = "loading" Clock.schedule_once( lambda dt: sl.data[obj.number].__setitem__( "current_state", "ready"), 5, ) elif obj.current_state == "ready": sl.data[obj.number]["current_state"] = choice(["done", "error"]) names_by_state = toolz.reduceby("current_state", lambda x, y: x + [y["text"]], sl.data, list) obj.current_state = sl.data[obj.number]["current_state"] print(toolz.keyfilter(lambda x: x != "queued", names_by_state))
def fn(row): make_remark = compose( lambda x: ", ".join(x), partial(map, lambda x: "{mop}: {amount}".format(mop=x[0], amount=x[1])), lambda x: x.items(), lambda lines: reduceby( "mode_of_payment", lambda a, x: a + get("paid_amount", x, 0), lines, 0, ), lambda x: concatv( get(x.invoice_name, payments, []), get(x.order_name, payments, []) ), frappe._dict, ) return merge(row, {"remarks": make_remark(row)})
def _set_tax_amount(items, transaction_type): item_map = valmap( lambda values: reduceby("item_code", lambda a, x: a + x.get( "net_amount", 0), values, 0), groupby("invoice", items), ) def set_amount(item_code, tax_detail): return item_code, tax_detail[1] def make_tax_amount(tax): return compose( partial(itemmap, lambda x: set_amount(*x)), excepts(ValueError, json.loads, {}), )(tax.item_wise_tax_detail) tax_map = compose( partial(groupby, "invoice"), partial(map, lambda x: merge({"invoice": x.invoice}, make_tax_amount(x))), )(frappe.db.sql( """ SELECT parent AS invoice, item_wise_tax_detail FROM `tab{transaction_type} Taxes and Charges` WHERE parent in %(invoices)s """.format(transaction_type=transaction_type), values={"invoices": list(item_map.keys())}, as_dict=1, )) def fn(row_dict): row = frappe._dict(row_dict) net_amount = row.net_amount or 0 item_net = item_map.get(row.invoice, {}).get(row.item_code, 0) tax_weight = net_amount / item_net if item_net else 0 tax = sum_by(row.item_code, tax_map.get(row.invoice, [])) * tax_weight return merge(row_dict, {"tax": tax, "total": net_amount + tax}) return fn
def test_summary_complex_arith(bank): expr = by(t.name, arith=(100 - t.amount * 2 / 30.0).sum()) result = compute(expr, bank) reducer = lambda acc, x: (100 - x['amount'] * 2 / 30.0) + acc expected = reduceby('name', reducer, bank.find(), 0) assert set(result) == set(expected.items())
def _result(statements): key_function = lambda statement: tuple(key(statement)) return list(toolz.reduceby(key_function, Statement.merge, statements).values())
def _result(statements): def key_function(statement): return tuple(key(statement)) return list( toolz.reduceby(key_function, Statement.merge, statements).values())
def test_foldby(): c = b.foldby(iseven, lambda acc, x: acc + x, 0, lambda a, b: a + b, 0) assert set(c) == set(reduceby(iseven, lambda acc, x: acc + x, L, 0).items()) c = b.foldby(iseven, lambda acc, x: acc + x) assert set(c) == set(reduceby(iseven, lambda acc, x: acc + x, L, 0).items())
def read_addresses(address_file: str) -> Dict[str, list]: with open(address_file, 'r', newline='') as fin: return reduceby(get(TITLE_FIELD), lambda guests, row: guests + [row], csv.DictReader(fin), [])
def key_by(key, items): return reduceby(key, lambda a, x: merge(a, x), items, {})
def collect_retweeted_by(tweets): return reduceby(lambda x: x['id'], lambda acc, t: acc + t['ag_retweeted_by'], tweets, [])