[Python-checkins] r53850 - sandbox/trunk/pep362/pep362.py

brett.cannon python-checkins at python.org
Thu Feb 22 01:03:01 CET 2007


Author: brett.cannon
Date: Thu Feb 22 01:02:57 2007
New Revision: 53850

Modified:
   sandbox/trunk/pep362/pep362.py
Log:
Add Parameter class that was accidentally skipped in initial copy from local
branch work.


Modified: sandbox/trunk/pep362/pep362.py
==============================================================================
--- sandbox/trunk/pep362/pep362.py	(original)
+++ sandbox/trunk/pep362/pep362.py	Thu Feb 22 01:02:57 2007
@@ -3,6 +3,57 @@
     determine if a binding of arguments to parameters is possible."""
     pass
 
+
+class Parameter(object):
+
+    """Represent a parameter in a function signature."""
+
+    def __init__(self, name, position, has_default, *args):
+        self.name = name
+        self.position = position
+        if not has_default:
+            self.has_default = False
+        else:
+            self.has_default = True
+            if len(args) != 1:
+                raise ValueError("Parameter requires a default value to be "
+                                    "specified when has_default has been set "
+                                    "to True")
+            self.default_value = args[0]
+
+    @classmethod
+    def __tuple2param(self, tuple_):
+        if not isinstance(tuple_, tuple):
+            return str(tuple_)
+        elif len(tuple_) == 1:
+            return "(" + str(tuple_[0]) +",)"
+        else:
+            return ('(' +
+                    ', '.join(self.__tuple2param(x) for  x in tuple_) +
+                    ')')
+
+    def __str__(self):
+        """Return the string representation of the parameter as it would look
+        in a function's signature."""
+        if isinstance(self.name, tuple):
+            result = self.__tuple2param(self.name)
+        else:
+            result = self.name
+        if self.has_default:
+            result+= "=" + str(self.default_value)
+        return result
+
+    def __repr__(self):
+        """Return the string required to create an equivalent instance of this
+        parameter."""
+        result = "%s(%r, %r, %r" % (self.__class__.__name__,
+                                    self.name, self.position, self.has_default)
+        if self.has_default:
+            result +=", %r" % self.default_value
+        result += ")"
+        return result
+
+
 class Signature(object):
 
     """Object to represent the signature of a function/method."""
@@ -152,7 +203,7 @@
         return bindings
 
 
-def getsignature(func):
+def signature(func):
     """Return a Signature object for the function or method.
 
     If possible, return the existing value stored in __signature__.  If that


More information about the Python-checkins mailing list