Re: [Tutor] conventions for establishing and saving default values for variables

2010-06-16 Thread R. Alan Monroe
> On Tue, Jun 15, 2010 at 2:27 PM, Pete O'Connell  
> wrote:
>> Hi I was wondering if anyone could give me some insight as to the best way
>> to get and save variables from a user the first time a script is opened. For
>> example if the script prompts something like "What is the path to the
>> folder?" and the result is held in a variable called thePath, what is the
>> best way to have that variable saved for all subsequent uses of the script
>> by the same user.
>> Pete

> In UNIX-likes, It would be a configuration file in their home
> directory, I suppose. Under windows, probably the registry. There's
> the _winreg and configparser modules.

Consider using the %USERPROFILE% environment variable rather than the
registry.

Alan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] conventions for establishing and saving default valuesfor variables

2010-06-16 Thread Alan Gauld


"R. Alan Monroe"  wrote


directory, I suppose. Under windows, probably the registry. There's
the _winreg and configparser modules.


Consider using the %USERPROFILE% environment variable rather than 
the

registry.


How would that work? That is just a single variable that points
to the users Settings folder. Effectively their home directory in
unix terms, so you could store the config file there. But
you couldn't store the kind of data you would store in the
registry?

I'm confused.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] conventions for establishing and saving default values for variables

2010-06-16 Thread Jeff Johnson

Pete O'Connell wrote:
Hi I was wondering if anyone could give me some insight as to the best 
way to get and save variables from a user the first time a script is 
opened. For example if the script prompts something like "What is the 
path to the folder?" and the result is held in a variable called 
thePath, what is the best way to have that variable saved for all 
subsequent uses of the script by the same user.

Pete


  


I will send you my python script that reads and writes to a windows 
style .ini file if you want me to.



--
Jeff

---

Jeff Johnson
j...@dcsoftware.com



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to model objects aimed to persistence?

2010-06-16 Thread Knacktus

Hi everyone,

within a python application I can easily model object association with 
simple references, e.g.:


#
class FavoritMovies(object):
def __init__(self, movies):
self.movies = movies

class Movie(object):
def __init__(self, name, actor):
self.name = name
self.actor = actor

gladiator = Movie("Gladiator", "Russel Crowe")
my_favorit_movies = FavoritMovies([gladiator])

kung_fu_panda = Movie("Kung Fu Panda", "Jack Black")
your_favorit_movies = FavoritMovies([gladiator, kung_fu_panda])
##

So far, so good. But what is best practise to prepare this data for 
general persistence? It should be usable for serialisation to xml or 
storing to an RDBMS or ObjectDatabase ...


I guess I have to incorporate some kind of id for every object and use 
this as reference with some kind of look-up dictionary, but I wouldn't 
like it and hope that there're some other sweet pythonic solutions?


Cheers,

Jan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to model objects aimed to persistence?

2010-06-16 Thread Mark Lawrence

On 16/06/2010 21:39, Knacktus wrote:

Hi everyone,

within a python application I can easily model object association with
simple references, e.g.:

#
class FavoritMovies(object):
def __init__(self, movies):
self.movies = movies

class Movie(object):
def __init__(self, name, actor):
self.name = name
self.actor = actor

gladiator = Movie("Gladiator", "Russel Crowe")
my_favorit_movies = FavoritMovies([gladiator])

kung_fu_panda = Movie("Kung Fu Panda", "Jack Black")
your_favorit_movies = FavoritMovies([gladiator, kung_fu_panda])
##

So far, so good. But what is best practise to prepare this data for
general persistence? It should be usable for serialisation to xml or
storing to an RDBMS or ObjectDatabase ...

I guess I have to incorporate some kind of id for every object and use
this as reference with some kind of look-up dictionary, but I wouldn't
like it and hope that there're some other sweet pythonic solutions?

Cheers,

Jan
___
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



Hi Jan,

I guess you're looking for something like the shelve or pickle modules.
http://docs.python.org/library/shelve.html
http://docs.python.org/library/pickle.html

HTH.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] conventions for establishing and saving default values for variables

2010-06-16 Thread Steven D'Aprano
On Thu, 17 Jun 2010 03:44:58 am Jeff Johnson wrote:

> I will send you my python script that reads and writes to a windows
> style .ini file if you want me to.

How is your script different from the standard ConfigParser module?


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] conventions for establishing and saving default valuesfor variables

2010-06-16 Thread Steven D'Aprano
On Thu, 17 Jun 2010 03:25:03 am Alan Gauld wrote:
> "R. Alan Monroe"  wrote
>
> >> directory, I suppose. Under windows, probably the registry.
> >> There's the _winreg and configparser modules.
> >
> > Consider using the %USERPROFILE% environment variable rather than
> > the
> > registry.
>
> How would that work? That is just a single variable that points
> to the users Settings folder. Effectively their home directory in
> unix terms, so you could store the config file there. But
> you couldn't store the kind of data you would store in the
> registry?
>
> I'm confused.

Environment variables are not permanent storage. They only exist in RAM 
unless you take steps to recreate them from permanent storage. Under 
Linux, for example, environment variables are recreated each time you 
log in after being read from a config file, such as .bashrc. My .bashrc 
includes:

export PYTHONPATH=/home/steve/python/
export PYTHONSTARTUP=/home/steve/python/startup.py

So even if you wrote the values you cared about to one or more 
environment variable, they would disappear as soon as the user logged 
out or rebooted.

It gets worse... Python includes the command os.putenv to set 
environment variables, but they are only set for subprocesses of the 
Python process that sets them. So for example, I can do this:

>>> os.putenv('myenvvar', 'value')
>>> os.system('echo $myenvvar')
value
0

but if I turn to another shell (not a subshell) it doesn't exist:

[st...@sylar ~]$ echo $myenvvar

[st...@sylar ~]$


See also this:
http://code.activestate.com/recipes/159462-how-to-set-environment-variables/


So unless I've missed something exceedingly subtle, writing to 
environment variables is not a solution to the problem of saving 
default values. It's not even part of a solution.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to model objects aimed to persistence?

2010-06-16 Thread Steven D'Aprano
On Thu, 17 Jun 2010 06:39:44 am Knacktus wrote:

> So far, so good. But what is best practise to prepare this data for
> general persistence? It should be usable for serialisation to xml or
> storing to an RDBMS or ObjectDatabase ...

Oh wow, deja vu... this is nearly the same question as just asked a day 
or so ago, in the thread "conventions for establishing and saving 
default values for variables". The answer I give will be more or less 
the same as was already given in that thread:

Use ConfigParser for ini-style config files.

Use pickle or shelve for serialising arbitrary Python objects.

You probably shouldn't use marshal, as that's awfully limited and really 
only designed for serialising Python byte code.

You can roll your own XML solution using the various XML modules in the 
standard library, or use plistlib, an XML-based storage format borrowed 
from Mac OS X.

Or one of the other standard serialisers like JSON or YAML, although 
YAML is not in the standard library yet.

Or you can use any one of many different databases, although that's 
probably massive overkill.


> I guess I have to incorporate some kind of id for every object and
> use this as reference with some kind of look-up dictionary, 

"Have to"? Certainly not. If your data doesn't need an ID field, there's 
no need to add one just for serialisation. It would be a pretty bizarre 
serialiser that couldn't handle this:

{key: value}

but could handle this:

({id: key}, {key: value})



> but I 
> wouldn't like it and hope that there're some other sweet pythonic
> solutions?

There's a few... :)



By the way, looking at your class name:

class FavoritMovies(object):

I can forgive the American (mis)spelling of Favo(u)rite, but why have 
you dropped the E off the end of "favorite"? I'd almost think your 
keyboard was broken, but no, you have E in Movies and object.

Deliberate misspellings in class and variable names will cost you *far* 
more in debugging time when you forget to misspell the words than they 
will save you in typing. Trust me on this.


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to model objects aimed to persistence?

2010-06-16 Thread Alan Gauld
"Steven D'Aprano"  wrote 

Deliberate misspellings in class and variable names 
will cost you *far* more in debugging time when you 
forget to misspell the words than they will save you 
in typing. Trust me on this.


I don't normally do "me too" postings but this is such 
a big deal that I just have to concur with Steven. It doesn't 
matter too much if you abbreviate local variables but 
anything that might be exposed at the module level 
and visible across imports should be spelled in full. 
It will only cause grief and pain later, if not to you 
then to anyone who imports your code...


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter - master attribute

2010-06-16 Thread Jim Byrnes

ALAN GAULD wrote:




I still am having trouble understanding the use of "master" in
Tkinter. I think the problem is I can't find any reference that explains the
concept around master,


If you read the GUI topic in my tutorial it explains the concept
of a containment tree that is common to ost GUI frameworks
including Tkinter. All widgets belong to a parent or master
widget until you get to some kind of root or main window that
is the master odf all its sub widgets.


I'll make it my next stop.


When you delete a window you delete the master and it deletes
all its children. The children delete their children, and so on until
all the widgets making up the window are deleted. Thats how GUIs work.
Similarly if you add a widget to a window you must tell the new
widget where within the containment tree it sits, you tell it who
its master is. Usually the widget will register itself with its master.


put the word spam on a Button I found a reference that said you type the word
text followed by an equal sign followed by spam in quotes.


That's slightly different in that text is an attribute of the widget itself.
By assigning 'Spam' to the attribute you control the look of that particular
Button widget.


OK.  That was the best example I could come up with to convey my 
confusion concerning master.  I could look at a reference and see that 
in that context the word text had a special purpose, I couldn't do that 
with master.



class CanvasEventsDemo(canvasDraw.CanvasEventsDemo):
def __init__(self, parent=None):
 canvasDraw.CanvasEventsDemo.__init__(self, parent)

Here parent is being used instead of master. Thats quite common.


Its usage like this that tends to confuse me. If I change all 
occurrences of of parent to Xparent the program runs.  If I replace 
parent with master the program runs.  If I replace canvas.master with 
canvas.xmaster I get an error.



self.canvas.master.bind('', self.onMoveOvals)


Here the master attribute is being accessed. That is an attribute
of the canvas widget.


word master appears only twice in the entire script so it is not defined
somewhere else in the script.


It is an inherited attribute and is inherited by all widgets
although from where is a little bit mysterious - see below...


AttributeError: Canvas instance has no attribute
'masterx'

So the Canvas does not have a masterx attribute but does have
one called master.  Maybe the bottom line question is where can I look to
see a list of a widgets attributes?


You can use dir() or help().
However in Tkinter it is further complicated by the mapping of Tkinter
to the underlying Tk widgets. It is not always 1-1 and some attributes
are implemented by the Tk tookit rather than at the Tkinter level and
these do not always show up in dir(). Master appears to be one of these.

However, on digging a little deeper it seems there is a subtle distinction
in Tkinter between the containment hierarchy and the geometry manager
hierarchy. I confess that I've never noticed this before and have only
skimmed the material(Grayson's Tkinter book)  but it seems that master
refers to the GM hierarchy and parent to the containment one. In most
cases they will be the same but they can be different. But because of
this master seems to be implemented somewhere in the GM code
rather than the widget code...

In most cases you can ignore that - as I have been doing for the last 10 years! 
:-)
Just use master as an inherited attribute that is present in all widgets.


OK, good.  This is what I was looking for, an explicit statement of what 
master is.



Sorry to be so dense about this but I just don't get it yet.


You are not being dense but asking rather deep questions which
probably need someone who understands the Tkinter implementatioon
to answer. You probably don't really need to know the detail, just accept
that master will be the attribute and it will be there in each widget class
you use.


I really wasn't trying to delve deep into the innards of Tkinter, it 
just seemed like master was appearing like magic and I just wanted to 
nail it down so I could get on learning Python.



If I get the time I will try to track this down further now that you have
piqued my curiosity.

HTH,


It does, I finally feel like I have a handle on it. Thanks for your time 
and patience.


Regards,  Jim


Alan G.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] pickling/shelve classes

2010-06-16 Thread Payal
Hi all,
Can someone please help in this below?

class F(object) :
   ...: def __init__(self, amt) : self.amt = amt
   ...: def dis(self) : print 'Amount : ', self.amt
   ...: def add(self, na) :
   ...: self.amt += na
   ...: F.dis(self)

pickle.dumps(F)
gives PicklingError: Can't pickle : it's not found
as __main__.F

What is my mistake? I want to pickle and shelve classes as well as
instances.

With warm regards,
-Payal
-- 


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to model objects aimed to persistence?

2010-06-16 Thread Knacktus

Am 17.06.2010 02:17, schrieb Alan Gauld:

"Steven D'Aprano"  wrote

Deliberate misspellings in class and variable names will cost you
*far* more in debugging time when you forget to misspell the words
than they will save you in typing. Trust me on this.


Thanks for that hint. I can imagine the nightmares ... It was just a 
spelling mistake. English is not my native language and I sometime tend 
to skip trailing "e"s ;-).


But I think I need to clarify my question a bit. The point I'm unsure 
with is how to best model references in python for persistence when not 
using pickle or any dedicated python. Python doesn't even need names as 
reference to the objects location in memory. I could do something like this:


###
my_favourite_movies = FavouriteMovies([Movie("Gladiator", "Russel Crowe")])
###

When I want to rebuild the data from a xml file or database, my objects 
somehow need to know which references to other objects they hold.
I don't now wether I have to use explicit indices in my python code to 
make it "xml-ready" and if that's the case, how to do it smoothly. Or 
whether there's some other trick.


Cheers,

Jan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor