Discussion:
Is there a better way to get colour from tuple-converted-to-string?
Nathan McCorkle
2014-10-03 17:16:35 UTC
Permalink
I am using configobj to save colour tuples (tuples with 4 values) to a
config file, this saves the tuple as a string like '(1,2,3,4)'

Upon loading this file with configobj on GUI startup, I've found I need to
convert the configobj data from a string (configobj doesn't give me a
Python tuple object), and I am doing it like so:
def stringTo4Tuple(self, st):
sp = st.split(',')
if len(sp)==4:
if sp[0][0]=='(' and sp[3][-1]==')':
try:
#chop off the '(' and convert to int
val1 = int(sp[0][1:])
val2 = int(sp[1])
val3 = int(sp[2])
#chop off the ')' and convert to int
val4 = int(sp[3][0:-1])
#return values as a tuple
return (val1, val2, val3, val4)
except ValueError:
return None


I'm wondering if there's a better way to do this... I might try converting
the colour tuple to a list before storing with configobj to see if it will
load it as a Python list, which would get around the string operations at
least.

Is there some magic string-tuple-to-colour function I'm not seeing in the
docs?
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Michael Ross
2014-10-03 17:38:25 UTC
Permalink
Post by Nathan McCorkle
I am using configobj to save colour tuples (tuples with 4 values) to a
config file, this saves the tuple as a string like '(1,2,3,4)'
Upon loading this file with configobj on GUI startup, I've found I need to
convert the configobj data from a string (configobj doesn't give me a
sp = st.split(',')
#chop off the '(' and convert to int
val1 = int(sp[0][1:])
val2 = int(sp[1])
val3 = int(sp[2])
#chop off the ')' and convert to int
val4 = int(sp[3][0:-1])
#return values as a tuple
return (val1, val2, val3, val4)
return None
I'm wondering if there's a better way to do this... I might try converting
the colour tuple to a list before storing with configobj to see if it will
load it as a Python list, which would get around the string operations at
least.
Is there some magic string-tuple-to-colour function I'm not seeing in the
docs?
eval?
Post by Nathan McCorkle
eval('(1,2,3,4)')
(1, 2, 3, 4)
Post by Nathan McCorkle
type(eval('(1,2,3,4)'))
<type 'tuple'>
Post by Nathan McCorkle
type(eval('(1,2,3,4)')[0])
<type 'int'>


Michael
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nathan McCorkle
2014-10-03 17:42:03 UTC
Permalink
Post by Michael Ross
eval?
eval('(1,2,3,4)')
(1, 2, 3, 4)
type(eval('(1,2,3,4)'))
<type 'tuple'>
type(eval('(1,2,3,4)')[0])
<type 'int'>
That was my first thought, but since the input is coming from a
user-editable file I wanted to avoid the potential security risk there.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Michael Ross
2014-10-03 17:49:18 UTC
Permalink
Post by Nathan McCorkle
Post by Michael Ross
eval?
eval('(1,2,3,4)')
(1, 2, 3, 4)
type(eval('(1,2,3,4)'))
<type 'tuple'>
type(eval('(1,2,3,4)')[0])
<type 'int'>
That was my first thought, but since the input is coming from a
user-editable file I wanted to avoid the potential security risk there.
eval( re.sub( '[^(),0-9]', '', st ) )

would put you on the save side, I think.
Max a user could do is make the resulting tuple have the wrong length.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Tim Roberts
2014-10-03 18:28:09 UTC
Permalink
Post by Nathan McCorkle
I am using configobj to save colour tuples (tuples with 4 values) to a
config file, this saves the tuple as a string like '(1,2,3,4)'
Upon loading this file with configobj on GUI startup, I've found I
need to convert the configobj data from a string (configobj doesn't
sp = st.split(',')
#chop off the '(' and convert to int
val1 = int(sp[0][1:])
val2 = int(sp[1])
val3 = int(sp[2])
#chop off the ')' and convert to int
val4 = int(sp[3][0:-1])
#return values as a tuple
return (val1, val2, val3, val4)
return None
Assuming no error handling:

def stringTo4Tuple(self, st):
return tuple(int(k) for k in st.strip('()').split(','))
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chris Barker
2014-10-03 22:28:12 UTC
Permalink
I"d say this a good time to dump configobj and use something smarter, like
json.

(I've been wanting a "pyson" for a while -- it would be a lot like json,
but understand pythonic things that json doesn't: list vs tuple, non-string
dict keys, etc).

You can get this pretty much out of the box with eval(), but as the OP
pointed out -- only if you can really trust your input.

-Chris
Post by Nathan McCorkle
I am using configobj to save colour tuples (tuples with 4 values) to a
config file, this saves the tuple as a string like '(1,2,3,4)'
Upon loading this file with configobj on GUI startup, I've found I need to
convert the configobj data from a string (configobj doesn't give me a
sp = st.split(',')
#chop off the '(' and convert to int
val1 = int(sp[0][1:])
val2 = int(sp[1])
val3 = int(sp[2])
#chop off the ')' and convert to int
val4 = int(sp[3][0:-1])
#return values as a tuple
return (val1, val2, val3, val4)
return None
return tuple(int(k) for k in st.strip('()').split(','))
--
Providenza & Boekelheide, Inc.
--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

***@noaa.gov
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nathan McCorkle
2014-10-04 05:01:21 UTC
Permalink
Post by Chris Barker
I"d say this a good time to dump configobj and use something smarter, like
json.
Hmm, we do already use JSON (and XML, and a bunch of internal custom
formats)... and I guess it wouldn't be much work to switch over at this
point since configobj looks pretty much like a dictionary in use anyway. It
looks like you only need to specify *indent* *= [some int] *to get
json.dumps to pretty-print, which is pretty much all I'd care about.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Robin Dunn
2014-10-07 07:01:31 UTC
Permalink
Post by Nathan McCorkle
I am using configobj to save colour tuples (tuples with 4 values) to a
config file, this saves the tuple as a string like '(1,2,3,4)'
Upon loading this file with configobj on GUI startup, I've found I need
to convert the configobj data from a string (configobj doesn't give me a
sp = st.split(',')
#chop off the '(' and convert to int
val1 = int(sp[0][1:])
val2 = int(sp[1])
val3 = int(sp[2])
#chop off the ')' and convert to int
val4 = int(sp[3][0:-1])
#return values as a tuple
return (val1, val2, val3, val4)
return None
I'm wondering if there's a better way to do this...
Save and restore the colour as a string and let it do its own
conversions. For example:

st = c1.GetAsString()
c2 = wx.NamedColour(st) # or just wx.Colour(st) in Phoenix
assert c1 == c2
--
Robin Dunn
Software Craftsman
http://wxPython.org
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...