From 88d3c3d9e2b4c5ab6112bc76dbcd8c04244cb177 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sun, 6 Jun 2021 02:09:26 +0200 Subject: [PATCH] Move find() and friends to reusable module. --- fulbank/util.py | 37 +++++++++++++++++++++++++++++++++++++ netbank | 41 ++++------------------------------------- 2 files changed, 41 insertions(+), 37 deletions(-) create mode 100644 fulbank/util.py diff --git a/fulbank/util.py b/fulbank/util.py new file mode 100644 index 0000000..eaf1249 --- /dev/null +++ b/fulbank/util.py @@ -0,0 +1,37 @@ +import operator + +def pfxmatch(pfx, item): + return str(item)[:len(pfx)] == pfx + +def ipfxmatch(pfx, item): + return str(item).upper()[:len(pfx)] == pfx.upper() + +class ambiguous(LookupError): + def __init__(self, a, b): + super().__init__("ambigous match: %s and %s" % (a, b)) + self.a = a + self.b = b + +def find(seq, *, item=None, test=None, match=None, key=None, default=LookupError): + if key is None: + key = lambda o: o + if match is None and item is not None: + match = lambda o: test(item, o) + if test is None: + test = operator.eq + found = None + for thing in seq: + if match(key(thing)): + if found is None: + found = thing + else: + if default is LookupError: + raise ambiguous(key(found), key(thing)) + else: + return default + if found is not None: + return found + if default is LookupError: + raise LookupError() + else: + return default diff --git a/netbank b/netbank index a1520ca..988b105 100755 --- a/netbank +++ b/netbank @@ -1,44 +1,11 @@ #!/usr/bin/python3 -import sys, os, getopt, pwd, operator -from fulbank import auth, data +import sys, os, getopt, pwd +from fulbank import auth, data, util sessname = data.defaultsess() sess = None -def pfxmatch(pfx, item): - return str(item)[:len(pfx)] == pfx - -class ambiguous(LookupError): - def __init__(self, a, b): - super().__init__("ambigous match: %s and %s" % (a, b)) - self.a = a - self.b = b - -def find(seq, *, item=None, test=None, match=None, key=None, default=LookupError): - if key is None: - key = lambda o: o - if match is None and item is not None: - match = lambda o: test(item, o) - if test is None: - test = operator.eq - found = None - for thing in seq: - if match(key(thing)): - if found is None: - found = thing - else: - if default is LookupError: - raise ambiguous(key(found), key(thing)) - else: - return default - if found is not None: - return found - if default is LookupError: - raise LookupError() - else: - return default - def usage(out): out.write("usage: netbank [-h] [-s SESSION-ID] COMMAND [ARGS...]\n") @@ -111,8 +78,8 @@ def cmd_lstxn(cmd, args): sys.stderr.write("usage: lstxn [-n NUM] ACCOUNT\n") sys.exit(1) try: - acct = find(sess.accounts, item=args[0], key=lambda acct: acct.number, test=pfxmatch) - except ambiguous as exc: + acct = util.find(sess.accounts, item=args[0], key=lambda acct: acct.number, test=util.pfxmatch) + except util.ambiguous as exc: sys.stderr.write("netbank: %s: ambiguous match between %s and %s\n" % (args[0], exc.a, exc.b)) sys.exit(1) except LookupError: -- 2.11.0