diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4367d18 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.gz \ No newline at end of file diff --git a/src/12/simple_parallel_programming/findrobots.py b/src/12/simple_parallel_programming/findrobots.py index 1dcd60c..cffab81 100644 --- a/src/12/simple_parallel_programming/findrobots.py +++ b/src/12/simple_parallel_programming/findrobots.py @@ -20,7 +20,7 @@ def find_all_robots(logdir): ''' Find all hosts across and entire sequence of files ''' - files = glob.glob(logdir+"/*.log.gz") + files = glob.glob(logdir+"/*.gz") all_robots = set() for robots in map(find_robots, files): all_robots.update(robots) diff --git a/src/12/simple_parallel_programming/findrobots_par.py b/src/12/simple_parallel_programming/findrobots_par.py index 3e7a588..e9e7eae 100644 --- a/src/12/simple_parallel_programming/findrobots_par.py +++ b/src/12/simple_parallel_programming/findrobots_par.py @@ -21,9 +21,9 @@ def find_all_robots(logdir): ''' Find all hosts across and entire sequence of files ''' - files = glob.glob(logdir+"/*.log.gz") + files = glob.glob(logdir+"/*.gz") all_robots = set() - with futures.ProcessPoolExecutor() as pool: + with futures.ProcessPoolExecutor(16) as pool: for robots in pool.map(find_robots, files): all_robots.update(robots) return all_robots diff --git a/src/2/writing_a_simple_recursive_descent_parser/example.py b/src/2/writing_a_simple_recursive_descent_parser/example.py index d401566..414e533 100644 --- a/src/2/writing_a_simple_recursive_descent_parser/example.py +++ b/src/2/writing_a_simple_recursive_descent_parser/example.py @@ -1,7 +1,45 @@ # example.py # # An example of writing a simple recursive descent parser - +# -*- coding: UTF-8 -*- +from __future__ import print_function +import re +import functools +PREFIX = '' +def trace(fn): + """A decorator that prints a function's name, its arguments, and its return + values each time the function is called. For example, + + @trace + def compute_something(x, y): + # function body + """ + @functools.wraps(fn) + def wrapped(*args, **kwds): + global PREFIX + reprs = [repr(e) for e in args] + reprs += [repr(k) + '=' + repr(v) for k, v in kwds.items()] + log('{0}({1})'.format(fn.__name__, ', '.join(reprs)) + ':') + PREFIX += ' ' + try: + result = fn(*args, **kwds) + PREFIX = PREFIX[:-4] + except Exception as e: + log(fn.__name__ + ' exited via exception') + PREFIX = PREFIX[:-4] + raise + # Here, print out the return value. + #log('{0}({1}) -> {2}'.format(fn.__name__, ', '.join(reprs), result)) + log('-> {0}'.format(result,)) + return result + return wrapped + +def log(message): + """Print an indented message (used with trace).""" + if type(message) is not str: + message = str(message) + print(PREFIX + re.sub('\n', '\n' + PREFIX, message)) + import re import collections @@ -38,6 +76,7 @@ class ExpressionEvaluator: (or raise a SyntaxError if it doesn't match). ''' + @trace def parse(self,text): self.tokens = generate_tokens(text) self.tok = None # Last symbol consumed