changeset 14:3a1f04f33feb

various generics
author Jeff Hammel <k0scist@gmail.com>
date Fri, 19 Sep 2014 16:47:04 -0700
parents 805d358c4d74
children 90edb741397d
files numerics/filters.py numerics/formatting.py numerics/gather.py numerics/parse_date.py
diffstat 4 files changed, 193 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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])
+
--- /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])
--- /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()
--- /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()