"append_const" action

Andrea 'fwyzard' Bocci <[email protected]> Sun, 07 Nov 2004 20:47:25 +0100
Newsgroups gmane.comp.python.optik.user
Organization AntaniX
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------030103080802060404050809
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi,
here's the background behind my proposal for "append_const", and sorry
for the late answer

I'm writing a program that takes a bunch of "items", each with many
properties, from an XML file and makes a combination of them, optimizing
  one or more properties.

To let the user specify what to optimize for, I use to syntaxes:
program --target property1 --target property2 ... xmlfile
or (for the most usefull)
program --property1 --property2 ... xmlfile

Since my program can (now) optimize sequentially for more than one
property, I want the use to be able to specify more than one property at
a time.

I did this with the "append" action to --target, and with "append_const"
action for the explicit --properties, both pointing at the same field.

Well this works :-)

As you suggested, I've also added a small testcase in test/test_optik.py.
If you think some more tests should be implemented, let me know, I'll
come out with something more complete.

I've attached a diff against Optik 1.5a2, both for lib/options.py and
test/test_optik.py.


Thanks for the great parser :-)
.Andrea.
-- 

Andrea 'fwyzard' Bocci - [email protected]
Free Software Foundation Associate Member

--------------030103080802060404050809
Content-Type: text/plain;
 name="append_const.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="append_const.diff"

diff -r -u3p optik-1.5a2/build/lib/optik/option.py optik/build/lib/optik/option.py
--- optik-1.5a2/build/lib/optik/option.py	2004-10-26 02:53:47.000000000 +0200
+++ optik/build/lib/optik/option.py	2004-11-07 20:12:05.000000000 +0100
@@ -97,6 +97,7 @@ class Option:
                "store_true",
                "store_false",
                "append",
+               "append_const",
                "count",
                "callback",
                "help",
@@ -110,6 +111,7 @@ class Option:
                      "store_true",
                      "store_false",
                      "append",
+                     "append_const",
                      "count")
 
     # The set of actions for which it makes sense to supply a value
@@ -123,6 +125,10 @@ class Option:
     ALWAYS_TYPED_ACTIONS = ("store",
                             "append")
 
+    # The set of actions which accept and *require* a "const" parameter
+    CONST_ACTIONS = ("store_const",
+                     "append_const")
+
     # The set of known types for option parsers.  Again, listed here for
     # constructor argument validation.
     TYPES = ("string", "int", "long", "float", "complex", "choice")
@@ -288,7 +294,7 @@ class Option:
                 self.dest = self._short_opts[0][1]
 
     def _check_const(self):
-        if self.action != "store_const" and self.const is not None:
+        if self.action not in self.CONST_ACTIONS and self.const is not None:
             raise OptionError(
                 "'const' must not be supplied for action %r" % self.action,
                 self)
@@ -395,6 +401,8 @@ class Option:
             setattr(values, dest, False)
         elif action == "append":
             values.ensure_value(dest, []).append(value)
+        elif action == "append_const":
+            values.ensure_value(dest, []).append(self.const)
         elif action == "count":
             setattr(values, dest, values.ensure_value(dest, 0) + 1)
         elif action == "callback":
diff -r -u3p optik-1.5a2/lib/option.py optik/lib/option.py
--- optik-1.5a2/lib/option.py	2004-10-26 02:53:47.000000000 +0200
+++ optik/lib/option.py	2004-11-07 20:16:52.000000000 +0100
@@ -97,6 +97,7 @@ class Option:
                "store_true",
                "store_false",
                "append",
+               "append_const",
                "count",
                "callback",
                "help",
@@ -110,6 +111,7 @@ class Option:
                      "store_true",
                      "store_false",
                      "append",
+                     "append_const",
                      "count")
 
     # The set of actions for which it makes sense to supply a value
@@ -123,6 +125,10 @@ class Option:
     ALWAYS_TYPED_ACTIONS = ("store",
                             "append")
 
+    # The set of actions which accept a "const" parameter.
+    CONST_ACTIONS = ("store_const",
+                     "append_const")
+
     # The set of known types for option parsers.  Again, listed here for
     # constructor argument validation.
     TYPES = ("string", "int", "long", "float", "complex", "choice")
@@ -288,7 +294,7 @@ class Option:
                 self.dest = self._short_opts[0][1]
 
     def _check_const(self):
-        if self.action != "store_const" and self.const is not None:
+        if self.action not in self.CONST_ACTIONS and self.const is not None:
             raise OptionError(
                 "'const' must not be supplied for action %r" % self.action,
                 self)
@@ -395,6 +401,8 @@ class Option:
             setattr(values, dest, False)
         elif action == "append":
             values.ensure_value(dest, []).append(value)
+        elif action == "append_const":
+            values.ensure_value(dest, []).append(self.const)
         elif action == "count":
             setattr(values, dest, values.ensure_value(dest, 0) + 1)
         elif action == "callback":
diff -r -u3p optik-1.5a2/test/test_optik.py optik/test/test_optik.py
--- optik-1.5a2/test/test_optik.py	2004-10-26 04:11:58.000000000 +0200
+++ optik/test/test_optik.py	2004-11-07 20:15:15.000000000 +0100
@@ -879,6 +879,8 @@ class TestMultipleArgsAppend(BaseTest):
                                type="float", dest="point")
         self.parser.add_option("-f", "--foo", action="append", nargs=2,
                                type="int", dest="foo")
+        self.parser.add_option("-z", "--zero", action="append_const",
+                               dest="foo", const=(0, 0))
 
     def test_nargs_append(self):
         self.assertParseOK(["-f", "4", "-3", "blah", "--foo", "1", "666"],
@@ -894,6 +896,11 @@ class TestMultipleArgsAppend(BaseTest):
                            {'point': None, 'foo':[(3, 4)]},
                            [])
 
+    def test_nargs_append_const(self):
+        self.assertParseOK(["--zero", "--foo", "3", "4", "-z"],
+                           {'point': None, 'foo':[(0, 0), (3, 4), (0, 0)]},
+                           [])
+
 class TestVersion(BaseTest):
     def test_version(self):
         self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE,

--------------030103080802060404050809--



-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click