Re: [Tutor] better resolution on time.sleep()?

2005-05-24 Thread Alan G

> I'm running an application that has a polling loop to check a serial
port
> for certain signals, and on my laptop I can get about 6700 samples
per
> second, which (of course) consumes 100% CPU; which may impact
battery life.

Consuming 100% CPU won't make much difference, the CPU is running all
the time anyway (when its not asleep). What will consume batteries
much
faster is if you are accessing hardware such as disk or modem or other
power hungry devices.

> Anyone know of:
>
> 1) a working millisleep/microsleep implementation for python 2.2+?

I've never tried in Python but can you use select()?
I've used it in C for short duration intervals.

Just a thought.

Alan G


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] ASCII characters

2005-05-24 Thread John Carmona
I need to print all the ASCII characters within a string, how would I 
delimit for example to print the first 100 only? Many thanks
JC


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ASCII characters

2005-05-24 Thread Kent Johnson
John Carmona wrote:
> I need to print all the ASCII characters within a string, how would I 
> delimit for example to print the first 100 only? Many thanks

A string is a sequence and supports sequence operations including slices. So
   s[:100]
gives the first 100 chars of a string.

You might be interested in the textwrap modulue.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [HELP]win32 shutdown, restart, logoff

2005-05-24 Thread Shidai Liu
On 5/23/05, Tony Meyer <[EMAIL PROTECTED]> wrote:
> I have the local machine's administration privilege.[...]> When I run the command as you point out, I got the following> messages>> (5, 'InitiateSystemShutdown', 'Access is denied.')
The process itself needs to have the privilege.  This message has completeinstructions:=Tony.MeyerThanks for the link. But I still get 'Access is denied'.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Date picker

2005-05-24 Thread Alberto Troiano

Hey everyone

I've been searching (with no luck by the way) for a date picker combo or 
something like it
What I need to do is validate a date field (better if it's already done) and 
I want to know if anyone knows about a code to do that (a made code, don't 
mean you to do my work). I was hoping to find a calendar combo box as in 
Visual Basic DropDown Calendar

Thanks in advanced

Alberto


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Filtering Spreadsheet Data

2005-05-24 Thread Mike Hansen
> Subject:
> [Tutor] Filtering Spreadsheet Data
> From:
> Luke Jordan <[EMAIL PROTECTED]>
> Date:
> Mon, 23 May 2005 15:30:33 -0500
> To:
> tutor@python.org
> 
> To:
> tutor@python.org
> 
> 
> Hi All,
> 
> I have several frighteningly cumbersome reports to review at my new
> job. I would like to write a python program to help me with my
> analysis. The goal of the program is to filter out information that
> doesn't meet certain requirements and print  relevant results back to
> a legible report that I can do detailed research and analysis on. The
> reports come to me in Excel format.
> 
> I have a solid understanding of basic programming.
> 
> Any guidance/advice/starting points would be greatly appreciated.
> 
> Thanks!
> 
> Luke
> 

Excel has some nice database-like queries itself. Take a look at Advanced 
Filter 
in Help. You can essentially query a worksheet and even send the results to a 
different worksheet. I'd imagine that once you got the query working, you could 
automate it using VBA or Python.

Mike
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Filtering Spreadsheet Data

2005-05-24 Thread Max Noel

On May 24, 2005, at 14:22, Mike Hansen wrote:

> Excel has some nice database-like queries itself. Take a look at  
> Advanced Filter
> in Help. You can essentially query a worksheet and even send the  
> results to a
> different worksheet. I'd imagine that once you got the query  
> working, you could
> automate it using VBA or Python.
>
> Mike

 If you want to do it in Python, the easiest way is to export the  
spreadsheet as CSV in Excel, then use the csv module to do your  
processing. If needed, convert back to XLS afterward.

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting  
and sweating as you run through my corridors... How can you challenge  
a perfect, immortal machine?"

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python won't play wav file

2005-05-24 Thread Joseph Quigley
I can't get:

import wave as w

def manipulate():
 manipulate = raw_input("Type 'help' for help\nManipulate>> ")
 if manipulate == "play":
 w.tell()
 elif manipulate == "back()":
 main_menu()

def open():
 while True:
 file_name = raw_input("Open: ")
 if file_name == "back()":
 break
 try:
 w.open(file_name, 'r')
 except IOError:
 print "Non-existant file."
 w.open(file_name, 'r')
 manipulate()

def main_menu():
 main_menu = raw_input("Type 'help' for help\n>> ")
 if main_menu == "open":
 open()
 elif main_menu == "help":
 import help
 help.main()

main_menu()

... to play a .wav music file. I tried w.play() but got an error that 
module has no attribute 'play'.
What do I use to get it to play?
Thanks,
JQ

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Sorting a directory

2005-05-24 Thread Jonas Melian
Hi all,

I have to working with the files lines in this directory:

:::
dir_locales = "/usr/share/i18n/locales/"

for line in fileinput.input(glob.glob(os.path.join\
(dir_locales, "*_[A-Z][A-Z]"))):
...

:::

The problem is that the directory otuput is:
aa_DJ
aa_ER
aa_ET
af_ZA
...

and i would sort this files by the 2 last letters, so:
aa_DJ
so_DJ
aa_ER
byn_ER
gez_ER
ti_ER
tig_ER
...

I have not foun a lot of info about sort

Thanks!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting a directory

2005-05-24 Thread Kent Johnson
Jonas Melian wrote:
> Hi all,
> 
> I have to working with the files lines in this directory:
> 
> :::
> dir_locales = "/usr/share/i18n/locales/"
> 
> for line in fileinput.input(glob.glob(os.path.join\
> (dir_locales, "*_[A-Z][A-Z]"))):
> ...
> 
> :::
> 
> The problem is that the directory otuput is:
> aa_DJ
> aa_ER
> aa_ET
> af_ZA
> ...
> 
> and i would sort this files by the 2 last letters, so:
> aa_DJ
> so_DJ
> aa_ER
> byn_ER
> gez_ER
> ti_ER
> tig_ER
> ...
> 
> I have not foun a lot of info about sort

The sort() method of a mutable sequence is very powerful. Docs for it are here:
http://docs.python.org/lib/typesseq-mutable.html

The key= parameter of sort() allows you to provide a function which, given a 
member of the sequence 
being sorted, returns the sort key for that item. In your case you can define a 
simple helper 
function that returns the last two characters of a string:

def lastTwoChars(s):
   return s[-2:]

then sort like this:

dir_locales = "/usr/share/i18n/locales/"

files = glob.glob(os.path.join(dir_locales, "*_[A-Z][A-Z]"))
files.sort(key=lastTwoChars)# Note: NOT key=lastTwoChars()

for line in fileinput.input(files):
   ...

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting a directory

2005-05-24 Thread Jonas Melian
Kent Johnson wrote:
>>and i would sort this files by the 2 last letters, so:
> 
> The sort() method of a mutable sequence is very powerful. Docs for it are 
> here:
> http://docs.python.org/lib/typesseq-mutable.html
> 
> The key= parameter of sort() allows you to provide a function which, given a 
> member of the sequence 
> being sorted, returns the sort key for that item. In your case you can define 
> a simple helper 
> function that returns the last two characters of a string:
> 
> def lastTwoChars(s):
>return s[-2:]
> 
> then sort like this:
> 
> dir_locales = "/usr/share/i18n/locales/"
> 
> files = glob.glob(os.path.join(dir_locales, "*_[A-Z][A-Z]"))
> files.sort(key=lastTwoChars)  # Note: NOT key=lastTwoChars()
> 
> for line in fileinput.input(files):
>...
> 

Thanks for this information.

I get:
::
files.sort(key=lastTwoChars)
TypeError: sort() takes no keyword arguments
::

Could it be by the Python version? I have Python 2.3.5
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting a directory

2005-05-24 Thread Kent Johnson
Jonas Melian wrote:
> Kent Johnson wrote:
>>files = glob.glob(os.path.join(dir_locales, "*_[A-Z][A-Z]"))
>>files.sort(key=lastTwoChars)  # Note: NOT key=lastTwoChars()
> 
> I get:
> ::
> files.sort(key=lastTwoChars)
> TypeError: sort() takes no keyword arguments
> ::
> 
> Could it be by the Python version? I have Python 2.3.5

Yes, the key= parameter is new in Python 2.4. For Python 2.3 you can define a 
comparison function, e.g.

def cmpLastTwoChars(a, b):
   return cmp(a[-2:], b[-2:])

files.sort(cmpLastTwoChars)

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] passing variables between frames?

2005-05-24 Thread Jeff Peery
ok, thanks. that makes sense; however I do not know how to pass a variable from my parent frame to the dialog.  I have tried this before unsuccessfully. could you provide a quick example of how to pass a variable to a dialog from a frame. I am not sure how this works. thanks.
 
JeffKent Johnson <[EMAIL PROTECTED]> wrote:
Jeff Peery wrote:> Hello, I am having trouble with passing variables between frames (Frame1 > and Dialog1). I'm using wxpython to write an application. when I run my > application the parent frame appears and within the parent frame I can > assign values to variables. I then have a button that launches a child > dialog. I want to access the values from the parent frame in the > dialog. for example say I had a listbox in the parent frame and I > choose the first entry 'a' from the list. I then launch the child > dialog and I want to put the value of the selection from the list box > into a text field - when I lauch the child dialog a text field appears > with the value '0'. From within Frame1 I can use the getselection() > function to get the listbox selection. although in the child dialog I > !
 tried to
 use Frame1.listBox.getselection() to get the selection from > Frame1. this doesn't work. not sure what to do here? any ideas? thanks.It's hard to know specifically what is broken without seeing code or error messages. My guess is that the variable Frame1 is not in scope in the dialog box code. But I would suggest a different approach...- Make the dialog box code independent of the values of variables in Frame1. Generally for me this means packaging up the dialog box in a function or class that is passed the values it needs to use and returns some result to the user. A very simple example is a dialog box that presents an error message; you could make a function showError(msg) that doesn't know anything about its caller.- In the event handler for the button in Frame1, gather the values needed by the dialog, launch it, get the result and handle it.This style keeps your dialog code independent of the rest of the pro!
 gram. An
 immediate benefit is that you can write a simple test driver that exercises the dialog without having to create any infrastructure. (This is the way I create most dialog boxes. I run it standalone until I am happy with its appearance and function, then I integrate it into the rest of the app.) Another long-term benefit is the dialogs are reusable; you may not see a need for this but in the long run some of them will probably have more than one use.HTH,Kent___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting a directory

2005-05-24 Thread Pujo Aji
you can sort listsource based on listkey

For example:
you have listsource = ['c','b','a']
and listkey = [2,3,1] # this will be used to sort listsource.

def sortbasedlist(sources,keys):
  #combine (key,sources)
  Kombin = [(keys[i],sources[i]) for i in range(len(sources))]
  Kombin.sort()
  return [source for (key,source) in Kombin]

main program:

  print sortbasedlist(listsource,listkey) # this will result ('a','c','b')

And for your problem you can do something like this:
# suppose files already found and can be considered as source
filesKey = [x[-2:] for x in files]
Result = sortbasedlist(files,filesKey)

good luck.
pujo



On 5/24/05, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Jonas Melian wrote:
> > Kent Johnson wrote:
> >>files = glob.glob(os.path.join(dir_locales, "*_[A-Z][A-Z]"))
> >>files.sort(key=lastTwoChars)  # Note: NOT key=lastTwoChars()
> >
> > I get:
> > ::
> > files.sort(key=lastTwoChars)
> > TypeError: sort() takes no keyword arguments
> > ::
> >
> > Could it be by the Python version? I have Python 2.3.5
> 
> Yes, the key= parameter is new in Python 2.4. For Python 2.3 you can define a 
> comparison function, e.g.
> 
> def cmpLastTwoChars(a, b):
>return cmp(a[-2:], b[-2:])
> 
> files.sort(cmpLastTwoChars)
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] strip is deprecated, so what do I use?

2005-05-24 Thread William O'Higgins
As the subject says, I was looking for an analog to chomp, and found
strip() and friends (rstrip() and lstrip()), but they are deprecated.
I'm happy to forgo their use in preparation for 3.0 (I figure we're
going to live the rest of our lives in the future, we might as well be
ready) but I need an alternative syntax today, and I haven't been able
to see what I should use instead.  Anyone have a suggestion?
-- 

yours,

William



signature.asc
Description: Digital signature
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Wizards in Tkinter

2005-05-24 Thread William O'Higgins
I am writing a small application that takes a user through a set of
steps - like a wizard.  What I need is an idea how I can start with a
window, have the use click "Next" and get another window.  My
understanding is that it is undesirable to have more than one mainloop
per program.  Thanks.
-- 

yours,

William



signature.asc
Description: Digital signature
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ASCII characters

2005-05-24 Thread John Carmona
Thanks Kent for the reply, I am actually having trouble to find the solution 
of the following exercise:

## Write a for loop that prints the ASCII code of each character in a string 
name S.##

I am ok with the for loop, put I don't know how get to print the ASCII code 
of each character with a string (I also know how to use the String).

Many thanks
JC


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] strip is deprecated, so what do I use?

2005-05-24 Thread William O'Higgins
On Tue, May 24, 2005 at 01:16:21PM -0400, Michael P. Reilly wrote:
>   On 5/24/05, William O'Higgins <[EMAIL PROTECTED]> wrote:
>
> As the subject says, I was looking for an analog to chomp, and found
> strip() and friends (rstrip() and lstrip()), but they are deprecated.
> I'm happy to forgo their use in preparation for 3.0 (I figure we're
> going to live the rest of our lives in the future, we might as well be
> ready) but I need an alternative syntax today, and I haven't been able
> to see what I should use instead.  Anyone have a suggestion?
>
>   William,
>
>   These were all changed to object-oriented syntax on the string value
>   itself.
> Old way  string.strip('   abcd   ')
> New way  '   abcd   '.strip()
>
>   It may take a little getting used to, but the functionality is the same
>   and it is more in keeping with how we should be treating the built-in data
>   values as objects just like we do with class instances.

Perfect, and it make a lot of sense.  Heck, I find this much more
readable in terms of nested calls.  Thank you.
-- 

yours,

William



signature.asc
Description: Digital signature
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ASCII characters

2005-05-24 Thread Kent Johnson
John Carmona wrote:
> Thanks Kent for the reply, I am actually having trouble to find the 
> solution of the following exercise:
> 
> ## Write a for loop that prints the ASCII code of each character in a 
> string name S.##
> 
> I am ok with the for loop, put I don't know how get to print the ASCII 
> code of each character with a string (I also know how to use the String).

You need the ord() function and maybe hex() also:
  >>> ord('s')
115
  >>> hex(ord('s'))
'0x73'

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ASCII characters

2005-05-24 Thread D. Hartley
I have a question: what is the "opposite" of hex()? (i.e., like ord
and chr).  If I have

'0x73', how can I get back to 115 or s?

Thanks!
~Denise

> You need the ord() function and maybe hex() also:
>  >>> ord('s')
> 115
>  >>> hex(ord('s'))
> '0x73'
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ASCII characters

2005-05-24 Thread Kent Johnson
D. Hartley wrote:
> I have a question: what is the "opposite" of hex()? (i.e., like ord
> and chr).  If I have
> 
> '0x73', how can I get back to 115 or s?

I don't know a really clean way to do this because '0x73' is not a legal input 
value for int().

The simplest way is to use eval():
  >>> eval('0x73')
115

but eval() is a security hole so if you are not 100% sure of where your data is 
coming from then 
this is probably a better solution (it strips off the '0x' then tells int() to 
convert base 16):
  >>> int('0x73'[2:], 16)
115

To go from the string '115' to the integer 115 you can use int() directly:
  >>> int('115')
115

To get back to a string use chr():
  >>> chr(115)
's'

All of these functions are documented here:
http://docs.python.org/lib/built-in-funcs.html

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ASCII characters

2005-05-24 Thread Danny Yoo


On Tue, 24 May 2005, Kent Johnson wrote:

> D. Hartley wrote:
> > I have a question: what is the "opposite" of hex()? (i.e., like ord
> > and chr).  If I have
> >
> > '0x73', how can I get back to 115 or s?
>
> I don't know a really clean way to do this because '0x73' is not a legal
> input value for int().


int() needs a little bit more hinting in this case: it can take in a
seceond parameter whose value is the "base".  For example:

##
>>> int('001010101001', 2)
681
>>> int('0x73', 16)
115
##


Bets of wishes!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python won't play wav file

2005-05-24 Thread Lee Harr
>I can't get:
>
>import wave as w
>


>... to play a .wav music file. I tried w.play() but got an error that
>module has no attribute 'play'.
>What do I use to get it to play?


Strangely... I do not believe the wave module is meant to actually
_play_ a wav file. It looks to me like it is only intended to read and
parse the file for meta information.

You could try passing the name of the file to another program
via os.system, or you could use pygame.

_
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python won't play wav file

2005-05-24 Thread Kent Johnson
Lee Harr wrote:
>>I can't get:
>>
>>import wave as w
>>
> 
> 
> 
>>... to play a .wav music file. I tried w.play() but got an error that
>>module has no attribute 'play'.
>>What do I use to get it to play?
> 
> 
> 
> Strangely... I do not believe the wave module is meant to actually
> _play_ a wav file. It looks to me like it is only intended to read and
> parse the file for meta information.

If you are on windows try the winsound module.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python won't play wav file

2005-05-24 Thread Max Noel

On May 24, 2005, at 02:49, Joseph Quigley wrote:

> ... to play a .wav music file. I tried w.play() but got an error that
> module has no attribute 'play'.
> What do I use to get it to play?
> Thanks,

 The wave module is meant to read, write and manipulate  
(transform, filter, whatever) WAV files, not to play them.
 Playing a sound is a highly OS-specific function; PyGame is a  
cross-platform library that enables this, among other things.

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting  
and sweating as you run through my corridors... How can you challenge  
a perfect, immortal machine?"

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Anyone doing the riddles?

2005-05-24 Thread D. Hartley
Hello all!

I was just thinking: if there are people on here who are still doing
the python riddles (or are interested in talking about them), I could
start an off-tutor thread so that we could chat about them without
clogging up the tutor mailing list for others?

If you are interested, drop me an email! (and what riddle are you on?
I just started 13!)

~Denise :)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Date picker

2005-05-24 Thread Lee Harr
>What I need to do is validate a date field (better if it's already done) 
>and
>I want to know if anyone knows about a code to do that (a made code, don't
>mean you to do my work). I was hoping to find a calendar combo box as in
>Visual Basic DropDown Calendar


For which GUI system?

py-qt has DateEdit

py-kde has KDateWidget or KDateTable

pygtk has gtk.Calendar

_
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Date picker

2005-05-24 Thread Alberto Troiano

Completeley forgot, :D

I'm working over Windows, Python 2.3., GUI Tkinter

Thanks in advanced

Alberto


http://graphics.hotmail.com/emvamp.gif"; 
width=12> Gaucho




>From: "Lee Harr" <[EMAIL PROTECTED]>
>To: tutor@python.org
>Subject: Re: [Tutor] Python Date picker
>Date: Wed, 25 May 2005 01:52:53 +0430
>
> >What I need to do is validate a date field (better if it's already done)
> >and
> >I want to know if anyone knows about a code to do that (a made code, 
>don't
> >mean you to do my work). I was hoping to find a calendar combo box as in
> >Visual Basic DropDown Calendar
>
>
>For which GUI system?
>
>py-qt has DateEdit
>
>py-kde has KDateWidget or KDateTable
>
>pygtk has gtk.Calendar
>
>_
>Express yourself instantly with MSN Messenger! Download today it's FREE!
>http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] xml

2005-05-24 Thread D. Hartley
And just in case anyone *isnt* working on the riddles:

anyone have a pointer to a *SIMPLE* intro to xml as used in python? I
looked in the library and there are about 11 xml-related modules.

Perhaps something 'remote'. ;)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: xml

2005-05-24 Thread Max Noel
(meh, forgot to click "reply-all" again)

Begin forwarded message:

> From: Max Noel <[EMAIL PROTECTED]>
> Date: May 24, 2005 23:01:39 BDT
> To: "D. Hartley" <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] xml
>
>
>
> On May 24, 2005, at 22:59, D. Hartley wrote:
>
>
>> anyone have a pointer to a *SIMPLE* intro to xml as used in python? I
>> looked in the library and there are about 11 xml-related modules.
>>
>
> The most elegant way to use XML in Python is to use the  
> elementtree module (http://effbot.org/zone/element-index.htm). It's  
> pure goodness.
>
> -- Max
> maxnoel_fr at yahoo dot fr -- ICQ #85274019
> "Look at you hacker... A pathetic creature of meat and bone,  
> panting and sweating as you run through my corridors... How can you  
> challenge a perfect, immortal machine?"
>
>

-- 
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting  
and sweating as you run through my corridors... How can you challenge  
a perfect, immortal machine?"

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Wizards in Tkinter

2005-05-24 Thread jfouhy
Quoting William O'Higgins <[EMAIL PROTECTED]>:

> I am writing a small application that takes a user through a set of
> steps - like a wizard. What I need is an idea how I can start with a
> window, have the use click "Next" and get another window. My
> understanding is that it is undesirable to have more than one mainloop
> per program. Thanks.

If you want more than one window, you use Tkinter.Toplevel.  But I think this is
not what you want here.

A few options spring to mind..

You could build multiple frames, but only pack the one you are interested in.  
eg:

from Tkinter import *
tk = Tk()

page1 = Frame(tk)
Label(page1, text='This is page 1 of the wizard').pack()
page1.pack(side=TOP)

page2 = Frame(tk)
Label(page2, text='This is page 2 of the wizard.').pack()

page3 = Frame(tk)
Label(page3, text='This is page 3.  It has an entry widget too!').pack()
Entry(page3).pack()

pages = [page1, page2, page3]
current = page1
def move(dirn):
global current
idx = pages.index(current) + dirn
if not 0 <= idx < len(pages):
return
current.pack_forget()
current = pages[idx]
current.pack(side=TOP)

def next():
move(+1)

def prev():
move(-1)

Button(tk, text='Next', command=next).pack(side=BOTTOM)
Button(tk, text='Previous', command=prev).pack(side=BOTTOM)

--

Another option is to use Pmw.Notebook without the tabs (and with next/previous
buttons to change pages).  This could be a bit easier (and it will do stuff like
making the wizard stay the same size).

Thirdly, a google search turned up this: http://www.freshports.org/devel/wizard/

HTH.

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] xml

2005-05-24 Thread jfouhy
Quoting "D. Hartley" <[EMAIL PROTECTED]>:

> anyone have a pointer to a *SIMPLE* intro to xml as used in python? I
> looked in the library and there are about 11 xml-related modules.
> 
> Perhaps something 'remote'. ;)

Use ElementTree!

(google for it)

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python won't play wav file

2005-05-24 Thread Alan G
Joseph,

I don't know if this will help or not, but some observations:

> def manipulate():
>  manipulate = raw_input("Type 'help' for help\nManipulate>> ")
>  if manipulate == "play":
>  w.tell()
>  elif manipulate == "back()":
>  main_menu()

You expect the user to type 'play' or 'back()'
One response is a command the other a pseudo function call
complete with parentheses. Thats not a very consistent user
experience.

Also by calling main_menu from inside manipulate you are effectively
using recursion as a loop. Thats OK if the number of loops is
small but if this program were used over a long preiod you
will eventually run out of menory.

Its usually better to return a value and have a main control
loop somewhere that calls the functions. For example have your
main_menu function display the prompt, sanity check the input
and return a valifd response from the user. Then have a single
if/elif tree dispatch the appropriate function that returns
success or failure. Wrap the menu and if/elif tree inside a
loop that exits on error or command.

That way ypur program can run indefinitely without risk of
memory overflow.

def main_menu():

return choice

while True:
choice = main_menu()
if choice == 'foo':
   foo()
elif chooice == 'bar':
   bar()
# etc...
elif choice == 'exit'
   break
else: break


> ... to play a .wav music file. I tried w.play() but got an error
that
> module has no attribute 'play'.

Have you used dir(w) to see what it does offer? Even better what does
the documentation say? Have you tried help(w)?

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Wizards in Tkinter

2005-05-24 Thread Alan G
Your mail came as an attachment so no quoted text, but basically 
there are two approaches to your problem of a Wizard.

In Tkinter you normally use a Frame as your main window.
You can pack another Frame inside that frame with your content.
When you press next simply unpack frame 1 and pack frame 2.
Repeat for as many frames as your Wizard requires. (This
makes going back easy too...)

A second approach is to use TopLevel widgets which are 
normally used for modeless dialog boxes. These effctively 
have their own mainloop running in parallel wit the main 
window but in a controlled way. But creating lors of 
TopLevel "dialogs" is IMHO resource hungry and harder 
work than using multiple frames.

The Frame approach also has the advantage that you can, 
to a degree data drive the wizard by putting references 
to the Frames in a dictionary or list and stepping through
the sequence. That way you can easily change the sequence, 
reuse frames over again within the same wizard etc etc.

Hope that made sense,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] xml

2005-05-24 Thread Kent Johnson
D. Hartley wrote:
> And just in case anyone *isnt* working on the riddles:
> 
> anyone have a pointer to a *SIMPLE* intro to xml as used in python? I
> looked in the library and there are about 11 xml-related modules.

ElementTree is my choice too. It is simple and easy to use though a bit 
bare-bones. It has a C 
implementation that is very fast and can cope with very large files (e.g. 
25MB). For a broader 
perspective and many examples, check out Uche Ogbuji's articles on xml.com. He 
has written several 
survey articles of available tools.
http://www.xml.com/pub/au/84

In truth, I haven't found a Python XML library that IMO comes close to dom4j in 
power and ease of 
use. It's one of the reasons I still use Jython. lxml looks promising but it is 
not available on 
Windows so I haven't tried it.
http://www.dom4j.org
http://codespeak.net/lxml/

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] xml

2005-05-24 Thread D. Hartley
I looked at the page for ElementTree that Max sent out, but I can't
understand what it's even talking about. Looking through the python
modules it seems like I need xmlrpclib - I created a serverproxy
instance, which I want to use to talk to a server - to send it
information (in this case, a name), and return to me its response.  I
can follow the documentation far enough to create a ServerProxy
instance, but then don't know what to do with it (it starts talking
about the reserved system member, and so on).

I don't want to have to know everything about xml (yet), I just want
to be able to use a phonebook which processes information in xml
(forgive my probably erroneous wording of things).  It said: "The
returned instance is a proxy object with methods that can be used to
invoke corresponding RPC calls on the remote server. If the remote
server supports the introspection API, the proxy can also be used to
query the remote server for the methods it supports (service
discovery) and fetch other server-associated metadata." - and that
sounds like what I want. Perhaps I am wrong?

Also, when I read the ElementTree page, it talked about element
instances and adding "code to load SML files as trees of Element
objects, and save them back again" - as often happens to me (and, I
hope to other beginners), this explanation does not even tell me if it
does the same thing, or anything remotely related to what I am looking
for.

Is anyone familiar with xmlrpclib, or knows of documentation that is
easier to follow than that in the library about how to use its
methods?

Also, re: using passwords and usernames in urls, I saw in this library
the following:

"Both the HTTP and HTTPS transports support the URL syntax extension
for HTTP Basic Authentication: http://user:[EMAIL PROTECTED]:port/path. The
user:pass portion will be base64-encoded as an HTTP `Authorization'
header, and sent to the remote server as part of the connection
process when invoking an XML-RPC method. You only need to use this if
the remote server requires a Basic Authentication user and password."

I read that the user:pass was an undocumented feature of, I think,
urllib (I may be wrong on this one), and as such, I was wondering if
anyone knew how to format the url so it automatically passed in the
username/password, like:

http://myname:mypass/www.mysite.com

does not work - but I dont know a "host" or "port"  ?

Any ideas/suggestions/pointers would be appreciated! (sorry if I dont
understand some of the pointers you have already sent! some of the
documentation is not always user-friendly for beginners!)

Thanks again,
Denise




On 5/24/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Quoting "D. Hartley" <[EMAIL PROTECTED]>:
> 
> > anyone have a pointer to a *SIMPLE* intro to xml as used in python? I
> > looked in the library and there are about 11 xml-related modules.
> >
> > Perhaps something 'remote'. ;)
> 
> Use ElementTree!
> 
> (google for it)
> 
> --
> John.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Pickling in plain English

2005-05-24 Thread Tom Tucker
I am having trouble understanding the c|Pickle modules.  What does 
serializing and de-serializing objects mean? I have read the below
urls and I "think" I understand the process, but I can't visualize the
beneifts.  Can someone kindly explain pickling in lamens terms?

Thanks,

Tom


http://effbot.org/librarybook/cpickle.htm
http://www.python.org/doc/2.3.5/lib/module-cPickle.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pickling in plain English

2005-05-24 Thread jfouhy
Quoting Tom Tucker <[EMAIL PROTECTED]>:

> I am having trouble understanding the c|Pickle modules. What does 
> serializing and de-serializing objects mean? I have read the below
> urls and I "think" I understand the process, but I can't visualize the
> beneifts. Can someone kindly explain pickling in lamens terms?

It's actually astonishingly simple, once you get the hang of it.

example:

>>> arr = [1, 3, 5, 7]
>>> dct = {'foo':1, 'bar':2, 'baz':3}
>>> st = "re: your mail"
>>>
>>> import pickle
>>> f = file('dump', 'wb')
>>> pickle.dump((arr, dct, st), f)
>>> f.close()
>>> ^Z

[new instance of python]

>>> import pickle
>>> f = file('dump', 'r')
>>> res = pickle.load(f)
>>> res
([1, 3, 5, 7], {'bar': 2, 'foo': 1, 'baz': 3}, 're: your mail')

[and you can look at the file 'dump' on your HDD, if you want to]

Basically, pickle allows you to write any python object to disk and the load it
back again with very little effort.  This is important if you want your program
to be able to save state between instances.

You can pickle more complex objects; the only restriction is that pickle.load
must be able to find the module where the classes are defined.

[there are some other restrictions, but you can do a lot without worrying about
them]

Does this help?

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] xml

2005-05-24 Thread Danny Yoo


On Tue, 24 May 2005, D. Hartley wrote:

> I looked at the page for ElementTree that Max sent out, but I can't
> understand what it's even talking about.


Hello Denise,

ElementTree is a third-party module by the Effbot for handling some of the
drudgery that is XML parsing:

http://effbot.org/zone/element.htm

it makes XML documents look like a bunch of nested lists.  Let's work
through a small example with it; that may help to clear some confusion.


If we have something like a small HTML document:

##
>>> testtext = """
... hello world.  foo!
... """
##


then we can use ElementTree to get a data structure out of this string:

###
>>> from elementtree import ElementTree
>>> tree = ElementTree.fromstring(testtext)
###

'tree' here is our root node, and the tree itself has a single child, the
'body' of the text, which we can get at by just indexing it:

##
>>> len(tree)
1
>>> tree[0]

>>> tree[0].text
'hello world.  '
##



The body has some text, as well as a child (that italicized node):

##
>>> tree[0][0]

>>> tree[0][0].text
'foo!'
##


One reason why this whole parsing thing is nice is because we can ask the
tree things like: "Give me all the italicized nodes, anywhere in the
document."

##
>>> for italicNode in tree.findall('.//i'):
... print italicNode.text
...
foo!
##

No need to worry about regular expressions at all.  *grin*


We can also start mutating the tree and add more things.  For example,
let's add a "goodbye world" at the tail end of the body.

##
>>> tree[0].tail
>>> tree[0].tail = "goodbye!"
>>>
>>> ElementTree.tostring(tree)
'hello world.  foo!\ngoodbye!'
##


Does this make sense?



> Looking through the python modules it seems like I need xmlrpclib - I
> created a serverproxy instance, which I want to use to talk to a server

Out of curiosity, which server?

xmlrpclib is customized to talk to servers that speak the 'xmlrpc'
protocol:

http://www.xmlrpc.com/

so it might or might not be appropriate to use it, depending on what
you're trying to connect to.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pickling in plain English

2005-05-24 Thread Tom Cloyd
I just want to take a moment to express my deep appreciation for this  
List. As one who is learning Python as amateur, in my spare moments, and  
already getting it to do good work for me, these clear, beautifully worked  
descriptions of basic aspects of Python are simply an ongoing delight for  
me - and singularly useful to my study of Python. To those of you who give  
your time, thought, and experience here to those of us who need it, thank  
you so very much.

Tom Cloyd

On Tue, 24 May 2005 16:32:50 -0700, <[EMAIL PROTECTED]> wrote:

> Quoting Tom Tucker <[EMAIL PROTECTED]>:
>
>> I am having trouble understanding the c|Pickle modules. What does
>> serializing and de-serializing objects mean? I have read the below
>> urls and I "think" I understand the process, but I can't visualize the
>> beneifts. Can someone kindly explain pickling in lamens terms?
>
> It's actually astonishingly simple, once you get the hang of it.
>
> example:
>
 arr = [1, 3, 5, 7]
 dct = {'foo':1, 'bar':2, 'baz':3}
 st = "re: your mail"

 import pickle
 f = file('dump', 'wb')
 pickle.dump((arr, dct, st), f)
 f.close()
 ^Z
>
> [new instance of python]
>
 import pickle
 f = file('dump', 'r')
 res = pickle.load(f)
 res
> ([1, 3, 5, 7], {'bar': 2, 'foo': 1, 'baz': 3}, 're: your mail')
>
> [and you can look at the file 'dump' on your HDD, if you want to]
>
> Basically, pickle allows you to write any python object to disk and the  
> load it
> back again with very little effort.  This is important if you want your  
> program
> to be able to save state between instances.
>
> You can pickle more complex objects; the only restriction is that  
> pickle.load
> must be able to find the module where the classes are defined.
>
> [there are some other restrictions, but you can do a lot without  
> worrying about
> them]
>
> Does this help?
>



-- 

==
Tom Cloyd
Bellingham, Washington, U.S.A: (360) 920-1226
<< BestMindHealth.com >>
==

Using Opera's revolutionary e-mail client (program):  
http://www.opera.com/mail/

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pickling in plain English

2005-05-24 Thread Tom Tucker
John,
Thanks that did help.  Like usual, I was making it harder than necessary.  

Tom,
I concur! Well put!

On 5/24/05, Tom Cloyd <[EMAIL PROTECTED]> wrote:
> I just want to take a moment to express my deep appreciation for this
> List. As one who is learning Python as amateur, in my spare moments, and
> already getting it to do good work for me, these clear, beautifully worked
> descriptions of basic aspects of Python are simply an ongoing delight for
> me - and singularly useful to my study of Python. To those of you who give
> your time, thought, and experience here to those of us who need it, thank
> you so very much.
> 
> Tom Cloyd
> 
> On Tue, 24 May 2005 16:32:50 -0700, <[EMAIL PROTECTED]> wrote:
> 
> > Quoting Tom Tucker <[EMAIL PROTECTED]>:
> >
> >> I am having trouble understanding the c|Pickle modules. What does
> >> serializing and de-serializing objects mean? I have read the below
> >> urls and I "think" I understand the process, but I can't visualize the
> >> beneifts. Can someone kindly explain pickling in lamens terms?
> >
> > It's actually astonishingly simple, once you get the hang of it.
> >
> > example:
> >
>  arr = [1, 3, 5, 7]
>  dct = {'foo':1, 'bar':2, 'baz':3}
>  st = "re: your mail"
> 
>  import pickle
>  f = file('dump', 'wb')
>  pickle.dump((arr, dct, st), f)
>  f.close()
>  ^Z
> >
> > [new instance of python]
> >
>  import pickle
>  f = file('dump', 'r')
>  res = pickle.load(f)
>  res
> > ([1, 3, 5, 7], {'bar': 2, 'foo': 1, 'baz': 3}, 're: your mail')
> >
> > [and you can look at the file 'dump' on your HDD, if you want to]
> >
> > Basically, pickle allows you to write any python object to disk and the
> > load it
> > back again with very little effort.  This is important if you want your
> > program
> > to be able to save state between instances.
> >
> > You can pickle more complex objects; the only restriction is that
> > pickle.load
> > must be able to find the module where the classes are defined.
> >
> > [there are some other restrictions, but you can do a lot without
> > worrying about
> > them]
> >
> > Does this help?
> >
> 
> 
> 
> --
> 
> ==
> Tom Cloyd
> Bellingham, Washington, U.S.A: (360) 920-1226
> << BestMindHealth.com >>
> ==
> 
> Using Opera's revolutionary e-mail client (program):
> http://www.opera.com/mail/
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] passing variables between frames?

2005-05-24 Thread Kent Johnson
Jeff Peery wrote:
> ok, thanks. that makes sense; however I do not know how to pass a 
> variable from my parent frame to the dialog.  I have tried this before 
> unsuccessfully. could you provide a quick example of how to pass a 
> variable to a dialog from a frame. I am not sure how this works. thanks

Can you post your (maybe broken) dialog code with a placeholder for the 
variable you want?

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Covert numbers to hex fails

2005-05-24 Thread Tom Tucker
Good evening!  I am trying to pass a number variable and have it
converted to hex.  Any recommendations on how to achieve this?  Thank
you.

FAILS
--
>>> value = 1234567890
>>> hexoutput = hex('%d' % (value))
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: hex() argument can't be converted to hex
>>> 



WORKS
-
>>> hexoutput = hex(1234567890)
>>> print hexoutput
0x499602d2
>>>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] passing variables between frames?

2005-05-24 Thread Jeff Peery
actually I got it to work! thanks for the help.
 
JeffKent Johnson <[EMAIL PROTECTED]> wrote:
Jeff Peery wrote:> ok, thanks. that makes sense; however I do not know how to pass a > variable from my parent frame to the dialog. I have tried this before > unsuccessfully. could you provide a quick example of how to pass a > variable to a dialog from a frame. I am not sure how this works. thanksCan you post your (maybe broken) dialog code with a placeholder for the variable you want?Kent___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Covert numbers to hex fails

2005-05-24 Thread Tony Meyer
> FAILS
> --
> >>> value = 1234567890
> >>> hexoutput = hex('%d' % (value))
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: hex() argument can't be converted to hex

Just don't convert the number to a string, e.g:

>>> value = 1234567890
>>> hexoutput = hex(value)
>>> hexoutput
'0x499602d2'

=Tony.Meyer

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Covert numbers to hex fails

2005-05-24 Thread Tom Tucker
Thanks!I see the mistake.  

On 5/24/05, Tony Meyer <[EMAIL PROTECTED]> wrote:
> > FAILS
> > --
> > >>> value = 1234567890
> > >>> hexoutput = hex('%d' % (value))
> > Traceback (most recent call last):
> >   File "", line 1, in ?
> > TypeError: hex() argument can't be converted to hex
> 
> Just don't convert the number to a string, e.g:
> 
> >>> value = 1234567890
> >>> hexoutput = hex(value)
> >>> hexoutput
> '0x499602d2'
> 
> =Tony.Meyer
> 
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Covert numbers to hex fails

2005-05-24 Thread ZIYAD A. M. AL-BATLY
On Tue, 2005-05-24 at 22:56 -0400, Tom Tucker wrote:
> Good evening!  I am trying to pass a number variable and have it
> converted to hex.  Any recommendations on how to achieve this?  Thank
> you.
> 
> FAILS
> --
> >>> value = 1234567890
> >>> hexoutput = hex('%d' % (value))
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: hex() argument can't be converted to hex
> >>> 
> 
> 
> 
> WORKS
> -
> >>> hexoutput = hex(1234567890)
> >>> print hexoutput
> 0x499602d2
> >>>
Try one of those:
'%x' % value # Will output the hex value without the leading 0x
hex(value)   # Leading 0x will be printed

Ziyad.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor