[Python-checkins] r64937 - in sandbox/trunk/ttk-gsoc/src: 2.x/ttk.py 3.x/ttk.py

guilherme.polo python-checkins at python.org
Mon Jul 14 01:11:52 CEST 2008


Author: guilherme.polo
Date: Mon Jul 14 01:11:52 2008
New Revision: 64937

Log:
Moved most of _dict_from_tcltuple's code to a public function named
tclobjs_to_py since it may be useful outside the ttk module (e.g. the dict
returned from widget.configure());

Version is 0.1.3 now.


Modified:
   sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
   sandbox/trunk/ttk-gsoc/src/3.x/ttk.py

Modified: sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	Mon Jul 14 01:11:52 2008
@@ -12,7 +12,7 @@
 of the widgets appearance lies at Themes.
 """
 
-__version__ = "0.1.2"
+__version__ = "0.1.3"
 
 __author__ = "Guilherme Polo <ggpolo at gmail.com>"
 
@@ -21,7 +21,9 @@
            "PanedWindow", "Progressbar", "Radiobutton", "Scale", "Scrollbar",
            "Separator", "Sizegrip", "Style", "Treeview",
            # Extensions
-           "LabeledScale", "OptionMenu"]
+           "LabeledScale", "OptionMenu",
+           # functions
+           "tclobjs_to_py"]
 
 import Tkinter
 
@@ -251,36 +253,13 @@
     be removed.
 
     ttuple is expected to contain an even number of elements."""
-    opts = []
     opt_start = 1 if cut_minus else 0
 
+    retdict = {}
     for opt, val in zip(iter(ttuple[::2]), iter(ttuple[1::2])):
-        if isinstance(val, basestring):
-            try:
-                if ' ' in val: # could be the padding option
-                    val = map(int, val.split())
-                elif val.isdigit():
-                    val = int(val)
-            except ValueError: # leave val untouched for now
-                pass
+        retdict[str(opt)[opt_start:]] = val
 
-        elif val and hasattr(val, '__len__') and hasattr(val[0], 'typename'):
-            if val[0].typename == 'StateSpec':
-                val = _list_from_statespec(val)
-            else: # could be padding
-                val = map(str, val)
-                try:
-                    val = map(int, val)
-                except ValueError:
-                    pass
-
-        elif hasattr(val, 'typename'):
-            # some other Tcl object
-            val = str(val)
-
-        opts.append((str(opt)[opt_start:], val))
-
-    return dict(opts)
+    return tclobjs_to_py(retdict)
 
 def _list_from_statespec(stuple):
     """Construct a list from the given statespec tuple according to the
@@ -341,6 +320,44 @@
 
     return _dict_from_tcltuple(res)
 
+def _convert_stringval(value):
+    """Converts a value, that may possibly represents a sequence,
+    hopefully, to a more appropriate Python object."""
+    try:
+        value = int(value)
+    except (ValueError, TypeError):
+        if ' ' in value:
+            value = map(str, value)
+            try:
+                value = map(int, value)
+            except ValueError:
+                # this value needs no conversion apparently
+                pass
+
+    return value
+
+def tclobjs_to_py(adict):
+    """Returns adict with its values converted from Tcl objects to Python
+    objects."""
+    for opt, val in adict.iteritems():
+        if isinstance(val, basestring):
+            val = _convert_stringval(val)
+
+        elif val and hasattr(val, '__len__'):
+            if hasattr(val[0], 'typename') and val[0].typename == 'StateSpec':
+                val = _list_from_statespec(val)
+            else:
+                # converts a sequence that possibly has Tcl objects, or not,
+                # to a better representation
+                val = map(_convert_stringval, map(str, val))
+
+        elif hasattr(val, 'typename'): # some other Tcl object
+            val = str(val)
+
+        adict[opt] = val
+
+    return adict
+
 
 class Style(object):
     """Manipulate style database."""

Modified: sandbox/trunk/ttk-gsoc/src/3.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	Mon Jul 14 01:11:52 2008
@@ -12,7 +12,7 @@
 of the widgets appearance lies at Themes.
 """
 
-__version__ = "0.1.2"
+__version__ = "0.1.3"
 
 __author__ = "Guilherme Polo <ggpolo at gmail.com>"
 
@@ -21,7 +21,9 @@
            "PanedWindow", "Progressbar", "Radiobutton", "Scale", "Scrollbar",
            "Separator", "Sizegrip", "Style", "Treeview",
            # Extensions
-           "LabeledScale", "OptionMenu"]
+           "LabeledScale", "OptionMenu",
+           # functions
+           "tclobjs_to_py"]
 
 import tkinter
 
@@ -251,36 +253,13 @@
     be removed.
 
     ttuple is expected to contain an even number of elements."""
-    opts = []
     opt_start = 1 if cut_minus else 0
 
+    retdict = {}
     for opt, val in zip(iter(ttuple[::2]), iter(ttuple[1::2])):
-        if isinstance(val, str):
-            try:
-                if ' ' in val: # could be the padding option
-                    val = list(map(int, val.split()))
-                elif val.isdigit():
-                    val = int(val)
-            except ValueError: # leave val untouched for now
-                pass
+        retdict[str(opt)[opt_start:]] = val
 
-        elif val and hasattr(val, '__len__') and hasattr(val[0], 'typename'):
-            if val[0].typename == 'StateSpec':
-                val = _list_from_statespec(val)
-            else: # could be padding
-                val = list(map(str, val))
-                try:
-                    val = list(map(int, val))
-                except ValueError:
-                    pass
-
-        elif hasattr(val, 'typename'):
-            # some other Tcl object
-            val = str(val)
-
-        opts.append((str(opt)[opt_start:], val))
-
-    return dict(opts)
+    return tclobjs_to_py(retdict)
 
 def _list_from_statespec(stuple):
     """Construct a list from the given statespec tuple according to the
@@ -341,6 +320,44 @@
 
     return _dict_from_tcltuple(res)
 
+def _convert_stringval(value):
+    """Converts a value, that may possibly represents a sequence,
+    hopefully, to a more appropriate Python object."""
+    try:
+        value = int(value)
+    except (ValueError, TypeError):
+        if ' ' in value:
+            value = list(map(str, value))
+            try:
+                value = list(map(int, value))
+            except ValueError:
+                # this value needs no conversion apparently
+                pass
+
+    return value
+
+def tclobjs_to_py(adict):
+    """Returns adict with its values converted from Tcl objects to Python
+    objects."""
+    for opt, val in adict.items():
+        if isinstance(val, str):
+            val = _convert_stringval(val)
+
+        elif val and hasattr(val, '__len__'):
+            if hasattr(val[0], 'typename') and val[0].typename == 'StateSpec':
+                val = _list_from_statespec(val)
+            else:
+                # converts a sequence that possibly has Tcl objects, or not,
+                # to a better representation
+                val = list(map(_convert_stringval, map(str, val)))
+
+        elif hasattr(val, 'typename'): # some other Tcl object
+            val = str(val)
+
+        adict[opt] = val
+
+    return adict
+
 
 class Style(object):
     """Manipulate style database."""


More information about the Python-checkins mailing list