# HG changeset patch # User Igor Chubin # Date 1320085280 -7200 # Node ID f583256b7ab150bcd0d54855b0e07ca8d22b2288 # Parent abd4080ee5839e2b51b6849221d0cd1da0fb8ff1 -p key support in new-words.py diff -r abd4080ee583 -r f583256b7ab1 new-words.py --- a/new-words.py Sun May 01 20:27:36 2011 +0200 +++ b/new-words.py Mon Oct 31 20:21:20 2011 +0200 @@ -496,10 +496,54 @@ for line in lines: f.write(line) +def parse_parts_description(parts_description): + """ + Returns triad (start, stop, step) + basing on parts_description string. + from-to/step + from+delta/step + """ + def incorrect_parts_description(pd): + raise ValueError("Parts description must be in format: num[[+-]num]/num; this [%s] is incorrect" % pd) + + try: + (a, step) = parts_description.split("/", 1) + step = int(step) + start = 0 + stop = 0 + if '-' in a: + (start, stop) = a.split("-", 1) + start = int(start) + stop = int(stop) + elif '+' in a: + (start, stop) = a.split("+", 1) + start = int(start) + stop = int(stop) + else: + start = int(a) + stop = start + 1 + return (start, stop, step) + + except: + raise ValueError("Parts description must be in format: num[[+-]num]/num; this [%s] is incorrect" % pd) + + +def take_part(lines, part_description = None): + if part_description == None: + return lines + (start, stop, step) = parse_parts_description(part_description) + n = len(lines) + part_size = (1.0*n) / step + result = [] + for i in range(n): + if part_size * i >= start and part_size * i <= stop: + result += lines[i] + return result + def filter_get_words_group_words_add_stat(args): vocabulary = load_vocabulary() notes = load_notes(notes_filenames()) - lines = readlines_from_stdin() + lines = take_part(readlines_from_stdin(), config.get('pages', '')) group_by = [1] if 'GROUP_WORDS_BY_TWO' in os.environ and os.environ['GROUP_WORDS_BY_TWO'] == 'YES':