changeset 181:4f474ceb9beb

almost works
author Jeff Hammel <k0scist@gmail.com>
date Tue, 09 Aug 2016 15:16:03 -0700
parents 69543d62ae7a
children a4a665aa8569
files numerics/__init__.py numerics/cli.py numerics/main.py numerics/split_table.py
diffstat 4 files changed, 33 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/numerics/__init__.py	Tue Aug 09 14:34:31 2016 -0700
+++ b/numerics/__init__.py	Tue Aug 09 15:16:03 2016 -0700
@@ -1,3 +1,4 @@
-#
-from main import *
+"""
+personal experiments in plotting
+"""
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/numerics/cli.py	Tue Aug 09 15:16:03 2016 -0700
@@ -0,0 +1,13 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# imports
+import os
+
+def ensure_dir(directory):
+    """ensure a directory exists"""
+    if os.path.exists(directory):
+        assert os.path.isdir(directory)
+        return directory
+    os.makedirs(directory)
+    return directory
--- a/numerics/main.py	Tue Aug 09 14:34:31 2016 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,53 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-"""
-personal experiments in plotting
-"""
-
-# imports
-import argparse
-import os
-import subprocess
-import sys
-
-# module globals
-__all__ = ['main', 'Parser']
-here = os.path.dirname(os.path.realpath(__file__))
-string = (str, unicode)
-
-def ensure_dir(directory):
-    """ensure a directory exists"""
-    if os.path.exists(directory):
-        assert os.path.isdir(directory)
-        return directory
-    os.makedirs(directory)
-    return directory
-
-
-class Parser(argparse.ArgumentParser):
-    """CLI option parser"""
-    def __init__(self, **kwargs):
-        kwargs.setdefault('description', __doc__)
-        argparse.ArgumentParser.__init__(self, **kwargs)
-        self.options = None
-
-    def parse_args(self, *args, **kw):
-        options = argparse.ArgumentParser.parse_args(self, *args, **kw)
-        self.validate(options)
-        self.options = options
-        return options
-
-    def validate(self, options):
-        """validate options"""
-
-def main(args=sys.argv[1:]):
-    """CLI"""
-
-    # parse command line options
-    parser = Parser()
-    options = parser.parse_args(args)
-
-if __name__ == '__main__':
-    main()
-
--- a/numerics/split_table.py	Tue Aug 09 14:34:31 2016 -0700
+++ b/numerics/split_table.py	Tue Aug 09 15:16:03 2016 -0700
@@ -10,6 +10,7 @@
 import chunk
 import sys
 import table
+from cli import ensure_dir
 
 class SplitTableParser(table.TableParser):
 
@@ -17,13 +18,15 @@
         self.add_argument('input', type=argparse.FileType('r'),
                           help="input CSV file")
         self.add_argument('-o', '--output', dest='output',
-                          type=argparse.FileType('w'), default=sys.stdout,
-                          help="output file to write to, or stdout by default")
+                          type=ensure_dir,
+                          help="output directory to write to, or stdout by default")
         self.add_argument('-c', '--column', dest='columns', nargs='+',
                           help="column names to output")
         self.add_argument('-v', '--verbose', dest='verbose',
                           action='store_true', default=False,
                           help="be verbose")
+        self.add_argument('--name', '--column-name', dest='column_name',
+                          help="column name to use for filename")
 
 
 def main(args=sys.argv[1:]):
@@ -37,9 +40,20 @@
     data = parser.read_table()
 
     header = data[0].keys()
+    assert header
 
+    if options.column_name:
+        assert options.column_name in header
+    else:
+        column_name = header[0]
+
+    # output
+    # right now, only one at a time
+    filename_template = '{index}-{column}.csv'
     for index, row in enumerate(data):
-        print '{index}'.format(index=index)
+
+        filename = filename_template.format(index=index, column=row[column_name])
+        print filename
 
         output_data = [list(header)]