Is there a better way to get colour from tuple-converted-to-string?

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?

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?

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'>

Michael

···

On Fri, 03 Oct 2014 19:16:35 +0200, Nathan McCorkle <nmz787@gmail.com> wrote:

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.

···

On Friday, October 3, 2014 10:38:37 AM UTC-7, Michael Ross wrote:

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’>

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.

···

On Fri, 03 Oct 2014 19:42:03 +0200, Nathan McCorkle <nmz787@gmail.com> wrote:

On Friday, October 3, 2014 10:38:37 AM UTC-7, Michael Ross wrote:

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.

Assuming no error handling:
def stringTo4Tuple(self, st):
return tuple(int(k) for k in st.strip(‘()’).split(‘,’))

···

Nathan McCorkle wrote:

    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

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

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

···

On Fri, Oct 3, 2014 at 11:28 AM, Tim Roberts timr@probo.com wrote:

Nathan McCorkle wrote:

    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

-- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Assuming no error handling:



def stringTo4Tuple(self, st):

    return tuple(int(k)  for k in st.strip('()').split(','))

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+unsubscribe@googlegroups.com.

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

Chris.Barker@noaa.gov

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.

···

On Friday, October 3, 2014 3:28:57 PM UTC-7, Chris Barker wrote:

I"d say this a good time to dump configobj and use something smarter, like json.

Nathan McCorkle wrote:

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...

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