[cig-commits] r6258 - cs/pythia/trunk/pyre/inventory/properties

leif at geodynamics.org leif at geodynamics.org
Wed Mar 14 19:35:25 PDT 2007


Author: leif
Date: 2007-03-14 19:35:25 -0700 (Wed, 14 Mar 2007)
New Revision: 6258

Modified:
   cs/pythia/trunk/pyre/inventory/properties/InputFile.py
   cs/pythia/trunk/pyre/inventory/properties/OutputFile.py
Log:
Allow 'None' as a default for {Input,Output}File (instead of remapping
it to 'stdin' and 'stdout', respectively):

    import pyre.inventory as pyre
    winklerInputFile = pyre.InputFile("winklerInputFile", default=None)

This is a tiny change, but I have a lot to say about it :-) It makes
the Pyre framework more consistent: it allows an app to have optional
input/output files, in the same way one can have optional properties
of other types (int, str, etc...):

    if self.inventory.winklerInputFile is None:
        # no Winkler forces were given
    else:
        # read Winkler forces

As MGA himself wrote in a comment in Property.py: "None is a special
value; it means that a property is not set".  Indeed, the 'None'
technique already worked for ints, strs, etc.

When you set 'default' to 'None', it allows you to check whether or
not the corresponding property was set.  This is an invaluable feature
when you need it, esp. for input files.  Without it, one either needs
to force the user to set two options when one should suffice:

    pylith3dapp.py --readWinklerForces --winklerInputFile=foo.wink

or -- as PyLith v0.8 currently does -- use a string property instead
and have the very existence of the input file determine the outcome:

    pylith3dapp.py --winklerInputFile=foo.wink
    pylith3dapp.py  # defaults to ${inputFileRoot}.wink

Winker forces are used if the '.wink' file exists, otherwise it
silently fails.  This is especially evil, since the intent of the user
isn't clear (and therefore can't be verified).  This behavior bit Matt
recently for the "tractionInputFile", which works the same way
("tractest_1.traction" was missing -- "tractest" being a test which
tests traction! -- but instead of an error message, PyLith simply
exhibited different behavior).

Note that an empty string would not work:

    winklerInputFile = pyre.InputFile("winklerInputFile", default="")

Pyre automatically opens the file for you, and
"self.inventory.winklerInputFile" accesses the already-opened file
stream.  So it would be impossible to check whether the value
defaulted to the empty string -- it dies with an exception:

    IOError: [Errno 2] No such file or directory: ''

Empty strings could be handled as a special case; but that wouldn't
work for string properties.  Besides, empty strings are more
error-prone, esp. under Bash:

    pylith3dapp.py --winklerInputFile=$WINKLER

What if $WINKLER is not set?


Modified: cs/pythia/trunk/pyre/inventory/properties/InputFile.py
===================================================================
--- cs/pythia/trunk/pyre/inventory/properties/InputFile.py	2007-03-14 22:48:58 UTC (rev 6257)
+++ cs/pythia/trunk/pyre/inventory/properties/InputFile.py	2007-03-15 02:35:25 UTC (rev 6258)
@@ -19,10 +19,6 @@
 
 
     def __init__(self, name, default=None, meta=None, validator=None):
-        if default is None:
-            import sys
-            default = sys.stdin
-            
         Property.__init__(self, name, "file", default, meta, validator)
         return
 

Modified: cs/pythia/trunk/pyre/inventory/properties/OutputFile.py
===================================================================
--- cs/pythia/trunk/pyre/inventory/properties/OutputFile.py	2007-03-14 22:48:58 UTC (rev 6257)
+++ cs/pythia/trunk/pyre/inventory/properties/OutputFile.py	2007-03-15 02:35:25 UTC (rev 6258)
@@ -19,12 +19,7 @@
 
 
     def __init__(self, name, default=None, meta=None, validator=None):
-        if default is None:
-            import sys
-            default = sys.stdout
-            
         Property.__init__(self, name, "file", default, meta, validator)
-
         return
 
 



More information about the cig-commits mailing list