# HG changeset patch # User Jeff Hammel # Date 1411170424 25200 # Node ID 3a1f04f33feb196710f4ca4b5f5c243230f2d5c4 # Parent 805d358c4d746509502db4302c0a52d6c2b5bc57 various generics diff -r 805d358c4d74 -r 3a1f04f33feb numerics/filters.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/numerics/filters.py Fri Sep 19 16:47:04 2014 -0700 @@ -0,0 +1,25 @@ +""" +filter functions for stats +""" + +__all__ = ['mean', 'array_mean', 'median'] + +def mean(data): + return sum(data)/float(len(data)) + +def array_mean(data): + if not data: + return [] + lengths = [len(i) for i in data] + if len(set(lengths)) != 1: + raise AssertionError("Different lengths to array_mean: {}".format(' '.join(lengths))) + return [mean(i) for i in zip(*data)] + +def median(data): + length = len(data) + index = length/2 + if length % 2: + return data[index] + else: + return 0.5*(data[index-1] + data[index]) + diff -r 805d358c4d74 -r 3a1f04f33feb numerics/formatting.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/numerics/formatting.py Fri Sep 19 16:47:04 2014 -0700 @@ -0,0 +1,29 @@ +""" +formatting tools for tabular data +""" + +__all__ = ['format_cols', 'format_table'] + +def format_cols(rows, header=None, right_align=()): + if not rows: + return [] + if isinstance(rows[0], dict): + header = header or rows[0].keys() + rows = [[row[h] for h in header] for row in rows] + if header: + rows.insert(0, header) + rows.insert(1, ['-'*len(i) for i in header]) + assert len(set([len(row) for row in rows])) == 1 + rows = [[str(col).strip() for col in row] + for row in rows] + lengths = [max([len(row[i]) for row in rows]) + for i in range(len(rows[0]))] + rows = [[(' '*(length-len(col)) + col) if index in right_align else (col + ' '*(length-len(col))) + for index, (col, length) in enumerate(zip(row, lengths))] + for row in rows] + return rows + +def format_table(rows, header=None, right_align=(), joiner=' '): + """format a table for printing""" + rows = format_cols(rows, header=header, right_align=right_align) + return '\n'.join([joiner.join([str(col) for col in row]).rstrip() for row in rows]) diff -r 805d358c4d74 -r 3a1f04f33feb numerics/gather.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/numerics/gather.py Fri Sep 19 16:47:04 2014 -0700 @@ -0,0 +1,71 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +gather files for processing +""" + +import argparse +import os +import subprocess +import sys + +__all__ = ['find', 'main'] +string = (str, unicode) + +def find(directories, filenames=None, exts=None): + """gather files from directories""" + + + retval = [] + if isinstance(directories, string): + directories = [directories] + + directories = [os.path.abspath(d) for d in directories] + + for d in directories: + + if not os.path.exists(d): + continue + if not os.path.isdir(d): + retval.append(d) + continue + + for item in sorted(os.listdir(d)): + path = os.path.join(d, item) + + if os.path.isdir(path): + retval.extend(find(path, filenames=filenames, exts=exts)) + else: + if filenames: + if item in filenames: + retval.append(path) + elif exts: + for ext in exts: + if path.endswith(ext): + retval.append(path) + break + else: + retval.append(path) + + return retval + + +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('path', nargs='+', help="paths") + parser.add_argument('-e', '--ext', dest='exts', + nargs='+', help='extensions') + options = parser.parse_args(args) + + # gather + paths = find(options.path, exts=options.exts) + + # scatter + print ('\n'.join(paths)) + +if __name__ == '__main__': + main() diff -r 805d358c4d74 -r 3a1f04f33feb numerics/parse_date.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/numerics/parse_date.py Fri Sep 19 16:47:04 2014 -0700 @@ -0,0 +1,68 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +parse date +""" + +import argparse +import datetime +import sys +import time +from .formatting import format_table +from dateutil.parser import parse + +__all__ = ['main', 'parse_date', 'epoch2local', 'epoch2utc', 'is_dst', 'timezone'] + +def is_dst(localtime=None): + """returns if daylight savings time is in effect locally""" + return time.localtime(localtime).tm_isdst > 0 + +def timezone(localtime=None): + """returns name of local timezone""" + return time.tzname[int(is_dst(localtime))] + +def epoch2local(datestamp): + """convert epoch to local time""" + return datetime.datetime.fromtimestamp(float(datestamp)) + +def epoch2utc(datestamp): + """convert epoch to UTC""" + return datetime.datetime.utcfromtimestamp(float(datestamp)) + + +def parse_date(datestamp): + """returns seconds since epoch from the supplied date""" + + try: + # already epoch timestamp + return float(datestamp) + except ValueError: + pass + + # parse the string + parsed_date = parse(datestamp) + + # convert this to seconds since epoch + return time.mktime(parsed_date.timetuple()) + + +def main(args=sys.argv[1:]): + + # parse command line + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('date', nargs='+', + help="local date to parse") + options = parser.parse_args(args) + + # parse each date + epochs = [parse_date(d) for d in options.date] + + # display results + header = ['epoch', 'local', 'UTC'] + print (format_table([[d, '{} {}'.format(epoch2local(d), timezone(d)), epoch2utc(d)] for d in epochs], + header=header, joiner='|')) + + +if __name__ == '__main__': + main()