RE: Is there a simple way to parse this string ?

2007-12-19 Thread James Newton
>I need to translate the following string
>a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
>
>into the following list or tuple
>b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]

>Is there a simple way to to this.
>Stef Mientki


>>> a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
>>> b = eval(a)
>>> b
((0, 0, 0, 255), (192, 192, 192, 255), True, 8)
>>> c = list(eval(a))
>>> c
[(0, 0, 0, 255), (192, 192, 192, 255), True, 8]
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Globals or objects?

2008-02-21 Thread James Newton
Duncan Booth wrote:
> The easiest way in Python to implement a singleton is just to
> use a module: all modules are singletons and there is a
> defined mechanism (import) for accessing them.

Hi Duncan,

I'm intrigued by this idea.

Could you give a bare-bones demonstration of it that the relative newbie
that I am can understand?

Thanks in advance,

James
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Globals or objects? (is: module as singleton)

2008-02-21 Thread James Newton
Mel wrote:
>> James Newton wrote:
>> Could you give a bare-bones demonstration of [implementing a
singleton
>> by using a module]?
>
> I had a club-membership application that ran for several years. 
> Default pathnames, etc. for the particular year came from a module 
> called thisyear.py:
> #=
> '''Values used to access this years trakkers files.
> $Id: thisyear.py,v 1.2 2006/08/26 16:30:23 mwilson Exp $
> '''
>
> memberpath = '2006-7/20062007.txt' # path to this years membership CSV
> dirname = '2006-7' # directory name for this year
> #=
> Programs that needed to use the comma-separated-value membership base 
> would import thisyear, and pass thisyear.memberpath when creating the 
> CSV reader object.  Etc.

Hi Mel,

So you were using thisyear.py as a preferences file: making it a module
was a shortcut for reading in the file and parsing its contents.  I like
it.

Would there be any circumstances where the singleton module would
include functions and objects?  In particular, would this system work if
the application needed to change a value (such as a counter)?

Suppose your application wanted to save the changed value, so that the
next session started using the new value.  Could you simply write out a
new copy of the thisyear.py file?  Or would this lead to version issues?
Would a previously-imported version of the module be stored in a
different place in RAM than a module that was imported after the change
was made?

Perhaps my real question is about how to visualize a module: what makes
an imported module different from an instance?

Thanks for your insights,

James
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Globals or objects? (is: module as singleton)

2008-02-21 Thread James Newton
Duncan Booth wrote: 
> you can create additional module instances (by calling new.module)

Hi Duncan,

Could you provide a scenario where this would be useful (and the best
practice)?

> What you get with a module is support for locating a specific module
> and ensuring that you don't get duplicate copies of a named module.

So if I were to execute the following pseudo-code, the second 'import'
would simply point at the module (instance) imported the first time:

import mymodule
changeContentsOf("mymodule.py") #on the hard disk
import mymodule

The values, functions and classes available in mymodule would only
change if I were to restart the application.

> Regarding your question about saving the values: what you would
> usually do would be to store the values in a separate configuration
> file and the module would load them on startup and then rewrite the
> configuration file when you call a save function...

That's what I would normally do, too.

Thanks for your help,

James
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Last 4 Letters of String

2008-02-21 Thread James Newton
>>> "string whose last four letters are abcd"[-4:]

'abcd'

 



From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On
Behalf Of Robert Rawlins - Think Blue
Sent: 21 February 2008 11:36
To: python-list@python.org
Subject: Last 4 Letters of String

 

Hello Guys,

 

I'm looking for a function which will give me the last 4 characters of a
given string. I'm sure it's a very simple task but I couldn't find
anything of it.

 

Any ideas?

 

Rob

-- 
http://mail.python.org/mailman/listinfo/python-list