def parse_report(val): """ PORTED FROM LOGISTICS: Takes a product report string, such as "zi 10 co 20 la 30", and parses it into a list of tuples of (code, quantity): >>> parse_report("zi 10 co 20 la 30") [('zi', 10), ('co', 20), ('la', 30)] Properly handles arbitrary whitespace: >>> parse_report("zi10 co20 la30") [('zi', 10), ('co', 20), ('la', 30)] Properly deals with Os being used for 0s: >>> parse_report("zi1O co2O la3O") [('zi', 10), ('co', 20), ('la', 30)] Properly handles extra spam in the string: >>> parse_report("randomextradata zi1O co2O la3O randomextradata") [('zi', 10), ('co', 20), ('la', 30)] """ if six.PY3: maketrans = str.maketrans if isinstance(val, bytes): val = val.decode('utf-8') return [ (x[0], int(x[1].replace(' ', '').translate(maketrans("lLO", "110")))) for x in findall( "\s*(?P<code>[A-Za-z]{%(minchars)d,%(maxchars)d})\s*" "(?P<quantity>[+-]?[ ]*[0-9%(numeric_letters)s]+)\s*" % { "minchars": 2, "maxchars": 4, "numeric_letters": "lLO" }, val) ] else: from strop import maketrans if isinstance(val, six.text_type): val = val.encode('utf-8') return [ (x[0], int(x[1].translate(maketrans("lLO", "110")))) for x in findall( "\s*(?P<code>[A-Za-z]{%(minchars)d,%(maxchars)d})\s*" "(?P<quantity>[+-]?[ ]*[0-9%(numeric_letters)s]+)\s*" % { "minchars": 2, "maxchars": 4, "numeric_letters": "lLO" }, val) ]
def parse_report(val): """ PORTED FROM LOGISTICS: Takes a product report string, such as "zi 10 co 20 la 30", and parses it into a list of tuples of (code, quantity): >>> parse_report("zi 10 co 20 la 30") [('zi', 10), ('co', 20), ('la', 30)] Properly handles arbitrary whitespace: >>> parse_report("zi10 co20 la30") [('zi', 10), ('co', 20), ('la', 30)] Properly deals with Os being used for 0s: >>> parse_report("zi1O co2O la3O") [('zi', 10), ('co', 20), ('la', 30)] Properly handles extra spam in the string: >>> parse_report("randomextradata zi1O co2O la3O randomextradata") [('zi', 10), ('co', 20), ('la', 30)] """ if six.PY3: maketrans = str.maketrans if isinstance(val, bytes): val = val.decode('utf-8') return [(x[0], int(x[1].replace(' ', '').translate(maketrans("lLO", "110")))) for x in findall( "\s*(?P<code>[A-Za-z]{%(minchars)d,%(maxchars)d})\s*" "(?P<quantity>[+-]?[ ]*[0-9%(numeric_letters)s]+)\s*" % { "minchars": 2, "maxchars": 4, "numeric_letters": "lLO" }, val)] else: from strop import maketrans if isinstance(val, six.text_type): val = val.encode('utf-8') return [(x[0], int(x[1].translate(maketrans("lLO", "110")))) for x in findall( "\s*(?P<code>[A-Za-z]{%(minchars)d,%(maxchars)d})\s*" "(?P<quantity>[+-]?[ ]*[0-9%(numeric_letters)s]+)\s*" % { "minchars": 2, "maxchars": 4, "numeric_letters": "lLO" }, val)]
def parse_report(val): """ PORTED FROM LOGISTICS: Takes a product report string, such as "zi 10 co 20 la 30", and parses it into a list of tuples of (code, quantity): >>> parse_report("zi 10 co 20 la 30") [('zi', 10), ('co', 20), ('la', 30)] Properly handles arbitrary whitespace: >>> parse_report("zi10 co20 la30") [('zi', 10), ('co', 20), ('la', 30)] Properly deals with Os being used for 0s: >>> parse_report("zi1O co2O la3O") [('zi', 10), ('co', 20), ('la', 30)] Properly handles extra spam in the string: >>> parse_report("randomextradata zi1O co2O la3O randomextradata") [('zi', 10), ('co', 20), ('la', 30)] """ def _cleanup(s): return unicode(s).encode('utf-8') return [(x[0], int(x[1].translate(maketrans("lLO", "110")))) for x in findall( "\s*(?P<code>[A-Za-z]{%(minchars)d,%(maxchars)d})\s*" "(?P<quantity>[+-]?[ ]*[0-9%(numeric_letters)s]+)\s*" % { "minchars": 2, "maxchars": 4, "numeric_letters": "lLO" }, _cleanup(val))]
def parse_report(val): """ PORTED FROM LOGISTICS: Takes a product report string, such as "zi 10 co 20 la 30", and parses it into a list of tuples of (code, quantity): >>> parse_report("zi 10 co 20 la 30") [('zi', 10), ('co', 20), ('la', 30)] Properly handles arbitrary whitespace: >>> parse_report("zi10 co20 la30") [('zi', 10), ('co', 20), ('la', 30)] Properly deals with Os being used for 0s: >>> parse_report("zi1O co2O la3O") [('zi', 10), ('co', 20), ('la', 30)] Properly handles extra spam in the string: >>> parse_report("randomextradata zi1O co2O la3O randomextradata") [('zi', 10), ('co', 20), ('la', 30)] """ def _cleanup(s): return unicode(s).encode('utf-8') return [ (x[0], int(x[1].translate(maketrans("lLO", "110")))) for x in findall( "\s*(?P<code>[A-Za-z]{%(minchars)d,%(maxchars)d})\s*" "(?P<quantity>[+-]?[ ]*[0-9%(numeric_letters)s]+)\s*" % { "minchars": 2, "maxchars": 4, "numeric_letters": "lLO" }, _cleanup(val)) ]
def test_maketrans(self): self.assertTrue(strop.maketrans("abc", "xyz") == transtable) self.assertRaises(ValueError, strop.maketrans, "abc", "xyzq")
import warnings