new-words

diff new-words.py @ 53:f583256b7ab1

-p key support in new-words.py
author Igor Chubin <igor@chub.in>
date Mon Oct 31 20:21:20 2011 +0200 (2011-10-31)
parents 74e05d4436ee
children e25de9ea9184
line diff
     1.1 --- a/new-words.py	Sun May 01 20:25:55 2011 +0200
     1.2 +++ b/new-words.py	Mon Oct 31 20:21:20 2011 +0200
     1.3 @@ -496,10 +496,54 @@
     1.4          for line in lines:
     1.5              f.write(line)
     1.6  
     1.7 +def parse_parts_description(parts_description):
     1.8 +    """
     1.9 +    Returns triad (start, stop, step)
    1.10 +    basing on parts_description string.
    1.11 +     from-to/step
    1.12 +     from+delta/step
    1.13 +    """
    1.14 +    def incorrect_parts_description(pd):
    1.15 +        raise ValueError("Parts description must be in format: num[[+-]num]/num; this [%s] is incorrect" % pd)
    1.16 +
    1.17 +    try:
    1.18 +        (a, step) = parts_description.split("/", 1)
    1.19 +        step = int(step)
    1.20 +        start = 0
    1.21 +        stop = 0
    1.22 +        if '-' in a:
    1.23 +            (start, stop) = a.split("-", 1)
    1.24 +            start = int(start)
    1.25 +            stop = int(stop)
    1.26 +        elif '+' in a:
    1.27 +            (start, stop) = a.split("+", 1)
    1.28 +            start = int(start)
    1.29 +            stop = int(stop)
    1.30 +        else:
    1.31 +            start = int(a)
    1.32 +            stop = start + 1
    1.33 +        return (start, stop, step)
    1.34 +
    1.35 +    except:
    1.36 +        raise ValueError("Parts description must be in format: num[[+-]num]/num; this [%s] is incorrect" % pd)
    1.37 +
    1.38 +
    1.39 +def take_part(lines, part_description = None):
    1.40 +    if part_description == None:
    1.41 +        return lines
    1.42 +    (start, stop, step) = parse_parts_description(part_description)
    1.43 +    n = len(lines)
    1.44 +    part_size = (1.0*n) / step
    1.45 +    result = []
    1.46 +    for i in range(n):
    1.47 +        if part_size * i >= start and part_size * i <= stop:
    1.48 +            result += lines[i]
    1.49 +    return result
    1.50 +
    1.51  def filter_get_words_group_words_add_stat(args):
    1.52      vocabulary = load_vocabulary()
    1.53      notes = load_notes(notes_filenames())
    1.54 -    lines = readlines_from_stdin()
    1.55 +    lines = take_part(readlines_from_stdin(), config.get('pages', ''))
    1.56      group_by = [1]
    1.57  
    1.58      if 'GROUP_WORDS_BY_TWO' in os.environ and os.environ['GROUP_WORDS_BY_TWO'] == 'YES':