Re: [Tutor] Writing to XML file with minidom

2005-08-31 Thread Johan Geldenhuys




Kent,
Thanks for the tip. I can write the changed data back to my xml file. One snag that I found is that the des encryption that I used for the data that is written back, it is not parsed correctly when the file is read again with the new data in it. There is non-printable characters or non-ascii chars in that gives errors from expat when the contents is parsed.
I had to use a different encryption algorithm. I am going to do some tests on it now.

Johan
 On Tue, 2005-08-30 at 09:47 -0400, Kent Johnson wrote:


Johan Geldenhuys wrote:
 That means that I have to compile the whole file from scratch in Python, 
 minidom.
 I am not that good, yet, but will try.

No, not if I understand what you are trying to do - the xmlDocument you have is all the data from the file, just write it back out using the code I posted before.

 will it be easier to search for the string that I look for in the file 
 (readlines) and then just write the pieces back again?

That depends a lot on the data. If you can reliably find what you want by looking at a line at a time, that is a simple approach. But you are almost there with the minidom. Really, just add my three lines of code to what you already have. (Maybe for prudence use a different file name.)

Kent

 
 Johan
 On Tue, 2005-08-30 at 07:40 -0400, Kent Johnson wrote:
 
Johan Geldenhuys wrote:
 Thanks for he help, so far.
 
 I am still having some questions on writing my new string back to the 
 xml file after I found what I was looking for and changed it.
 
 Extracts:
 
 xmlDocument = minidom.parse(file_name) # open existing file for parsing
 main = xmlDocument.getElementsByTagName('Config')
 main.getElementsByTagName('Connection')
 configSection = mainSection[0]
 
 for node in configSection: #Here I get the NamedNodeMap info
 password = node.getAttribute(password)
 # Do stuff to the password and I have 'newPass'
node.removeAttribute('password') # I take out my old attribute 
 and it's value
node.setAttribute('password', newPass)
 
 
 At this stage I have my new attribute and it's new value, but how do I 
 write that to my file in the same place?
 I have to get a 'writer', how do I do this?

The minidom docs say that DOM objects have a writexml() method:
writexml(writer[,indent=[,addindent=[,newl=]]])
Write XML to the writer object. The writer should have a write() method which matches that of the file object interface.

This is saying that 'writer' should act like a file object. So it can just be a file object obtained by calling open(). In other words something like
f = open(file_name, 'w')
xmlDocument.writexml(f)
f.close()

should do it. If you have non-ascii characters in your XML you should use codecs.open() instead of plain open() and encode your XML as desired (probably utf-8).

 Do I have to write all the data back or can I just replace the pieces I 
 changed?

You have to write it all back.

Kent

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

 

___
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] Listbox help

2005-08-31 Thread David Holland
I can work out how to use most Tkinter widgets.  Eg 
buttons like :-
   def create_widgets(self):
#create GUI
   Button(self, text = x, command =
self.y).grid(row = 3, column = 3, columnspan = 3)

However I am not sure how to use Listboxes.  What
would be the similar syntax to create a listbox.

Thanks in advance.

David



___ 
To help you stay safe and secure online, we've developed the all new Yahoo! 
Security Centre. http://uk.security.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Listbox help

2005-08-31 Thread Kent Johnson
David Holland wrote:
 I can work out how to use most Tkinter widgets.  Eg 
 buttons like :-
def create_widgets(self):
 #create GUI
Button(self, text = x, command =
 self.y).grid(row = 3, column = 3, columnspan = 3)
 
 However I am not sure how to use Listboxes.  What
 would be the similar syntax to create a listbox.

This page has a complete example of using a listbox:
http://effbot.org/zone/tkinter-scrollbar-patterns.htm

And this page has some useful snippets:
http://www.pythonware.com/library/tkinter/introduction/x5453-patterns.htm

Kent

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


Re: [Tutor] Writing to XML file with minidom

2005-08-31 Thread Travis Spencer
On 8/30/05, Johan Geldenhuys [EMAIL PROTECTED] wrote:
 One snag that I found is that the des encryption that I used for the data 
 that is 
 written back, it is not parsed correctly when the file is read again with the 
 new 
 data in it. There is non-printable characters or non-ascii chars in that 
 gives errors
 from expat when the contents is parsed.
 I had to use a different encryption algorithm. I am going to do some tests on 
 it 
 now.

Put the cyphertext in a CDATA section, so the parser knows to ignore
its contents:

?xml version=1.0?
root
cyphertext![CDATA[
^KI^[?+?6?
]]
/cyphertext
/root

-- 

Regards,

Travis Spencer 

P.S. Please don't send HTML e-mails.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] PYTHON????

2005-08-31 Thread luckygoodluck



Dear Python,
How does a calculator multiply? I want to 
create a computer software that can multiply. How do I program the computer to 
multiply?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Closing SimpleXMLRPCServer properly

2005-08-31 Thread lawrence wang
I have a SimpleXMLRPCServer, which I've tweaked thusly: 

class StoppableXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
 def serve_forever(self):
  to stop this server: register a function in the class
  that uses it which sets server.stop to True.
  self.stop = False
  while not self.stop:
   self.handle_request()

Here's the code where I start the server...

  try:
   self.server.serve_forever()
  finally:
   self.server.server_close()
   self.log('server closed')

>From another thread, I set the server's stop attribute to False, so the
server stops running. It exits the try block, runs server_close(), then
I get the message 'server closed'...

...but when I try to use the port that the server's bound to again, it
takes a very long time (while i try to use the port, catch the
exception, sleep, try again) until it becomes free. Is there something
else I need to call to ensure that the port is released cleanly? Is
this an OS-specific thing out of my control? (I'm running Debian Linux.)

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


Re: [Tutor] Writing to XML file with minidom

2005-08-31 Thread Danny Yoo

  One snag that I found is that the des encryption that I used for the
  data that is written back, it is not parsed correctly when the file is
  read again with the new data in it. There is non-printable characters
  or non-ascii chars in that gives errors from expat when the contents
  is parsed. I had to use a different encryption algorithm. I am going
  to do some tests on it now.

 Put the cyphertext in a CDATA section, so the parser knows to ignore
 its contents:

 ?xml version=1.0?
 root
 cyphertext![CDATA[
 ^KI^[?+?6?
 ]]
 /cyphertext
 /root


Hi Travis,


Putting pure binary bytes in an XML file has a flaw: the issue is that the
binary bytes themselves might contain characters that could be interpreted
as XML!  Even if we wrap the content in CDATA, there's nothing that really
stops the bytes from containing the characters ]] to prematurely close
off the CDATA tag.

To get around this, we can use a technique called ascii-armor to wrap
protection around the troublesome binary text.

http://en.wikipedia.org/wiki/ASCII_armor

Python comes with a common ascii-armoring algorithm called base64, and
it's actually very easy to use it.  Let's do a quick example.


Let's say we have some binary unprintable bytes, like this:

##
 someBytes = open('/usr/bin/ls').read(8)
 someBytes
'\x7fELF\x01\x01\x01\x00'
##

(Hey, look, an Elf!  *grin*)

Anyway, this ELF will probably pass through email poorly, because the
bytes surrounding it are pretty weird.  But we can apply base64 encoding
on those bytes:

##
 encodedBytes = someBytes.encode('base64')
 encodedBytes
'f0VMRgEBAQA=\n'
##

And now it's in a form that should pass cleanly through.  Decoding it is
also a fairly easy task:

##
 encodedBytes.decode('base64')
'\x7fELF\x01\x01\x01\x00'
##

And now we've got our ELF back.


Hope this helps!

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


Re: [Tutor] PYTHON????

2005-08-31 Thread Danny Yoo


On Mon, 29 Aug 2005 [EMAIL PROTECTED] wrote:

  How does a calculator multiply? I want to create a computer software
 that can multiply. How do I program the computer to multiply?

Hello,

Have you had a chance to look at:

http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

There are a few tutorial there that should help you get started.  Almost
every one of them talks about how to get the computer to do basic
arithmetic.

If you have questions about the tutorials, please feel free to ask here.

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


Re: [Tutor] Popen and Prompting

2005-08-31 Thread Danny Yoo


On Tue, 30 Aug 2005, Faulconer, Steven M. wrote:

 Been digging through the web and the archives for the tutor list and
 can't seem to locate an answer to my question. It is probably out there,
 but my searching skills are failing me. I recently wrote a GUI for
 several command line programs using Tkinter. Everything is working quite
 well, until I ran into one program that actually prompts for some input.

Hi Steven,

This is a common problem.  The Unix folks have traditionally used a
program called Expect to automate talking with interactive external
processes. There's a Python equivalent to Expect: take a look at the
'pexpect' module.  Here you go:

http://pexpect.sourceforge.net/

The documentation on that page has a few examples that should help you get
started.  Best of wishes to you!

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


Re: [Tutor] Closing SimpleXMLRPCServer properly

2005-08-31 Thread Danny Yoo
 class StoppableXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
 def serve_forever(self):
 to stop this server: register a function in the class
 that uses it which sets server.stop to True.
 self.stop = False
 while not self.stop:
 self.handle_request()

 Here's the code where I start the server...

 try:
 self.server.serve_forever()
 finally:
 self.server.server_close()
 self.log('server closed')

 From another thread, I set the server's stop attribute to False, so the
 server stops running. It exits the try block, runs server_close(), then I
 get the message 'server closed'...

 ...but when I try to use the port that the server's bound to again, it
 takes a very long time (while i try to use the port, catch the
 exception, sleep, try again) until it becomes free. Is there something
 else I need to call to ensure that the port is released cleanly? Is this
 an OS-specific thing out of my control? (I'm running Debian Linux.)


Hi Lawrence,

It's TCP specific.  When the server shuts down, the port is in a TIME_WAIT
state that causes the port to wait until things are cleanly shut down.

For more information on TIME_WAIT, see:

http://www.developerweb.net/sock-faq/detail.php?id=13


Anyway, you can force the issue, get the server to reuse the address, by
setting the allow_reuse_address attribute on your server.

##
class StoppableXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
Override of TIME_WAIT
allow_reuse_address = True

def serve_forever(self):
self.stop = False
while not self.stop:
self.handle_request()
##

See:

http://www.python.org/doc/lib/module-SocketServer.html

for a brief mention of allow_reuse_address.


One of these days, I have to read Richard Stevens's book on TCP to better
understand what exactly is going on.  I have to admit that I don't
understand the TCP model quite well yet.


Best of wishes to you!

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


Re: [Tutor] Writing to XML file with minidom

2005-08-31 Thread Travis Spencer
On 8/31/05, Danny Yoo [EMAIL PROTECTED] wrote:
 Hi Travis,

Hey Danny,

 Putting pure binary bytes in an XML file has a flaw: the issue is that the
 binary bytes themselves might contain characters that could be interpreted
 as XML!  Even if we wrap the content in CDATA, there's nothing that really
 stops the bytes from containing the characters ]] to prematurely close
 off the CDATA tag.

Oh, sure.  I didn't think that through, and if I had, I wouldn't have
know how to work around it.

 To get around this, we can use a technique called ascii-armor to wrap
 protection around the troublesome binary text.
 
 http://en.wikipedia.org/wiki/ASCII_armor

Brilliant.  I won't forget the term ascii-armor if I ever find
myself in Johan's shoes and I've forgotten the details.

 (Hey, look, an Elf!  *grin*)

HA!

 Hope this helps!

Tremendously.  Thanks, Danny!

-- 

Regards,

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


Re: [Tutor] Popen and Prompting

2005-08-31 Thread Faulconer, Steven M.
Danny,

Thanks for the response. We looked at using pexpect (I'm a UNIX guy at
heart), but we have variable command prompts, that may or may not show up,
with this program, so expect/pexpect would be difficult. John's suggestion
was to add some sort of unique ID as a good idea, but won't work in this
situation. I guess in the end, doing something with pexpect would be the way
to go if I needed to. This issue has been resolved in another way (per my
last message to the list).

Thanks again for the response,
Steven

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, August 31, 2005 3:06 PM
To: Faulconer, Steven M.
Cc: tutor@python.org
Subject: Re: [Tutor] Popen and Prompting



On Tue, 30 Aug 2005, Faulconer, Steven M. wrote:

 Been digging through the web and the archives for the tutor list and
 can't seem to locate an answer to my question. It is probably out there,
 but my searching skills are failing me. I recently wrote a GUI for
 several command line programs using Tkinter. Everything is working quite
 well, until I ran into one program that actually prompts for some input.

Hi Steven,

This is a common problem.  The Unix folks have traditionally used a
program called Expect to automate talking with interactive external
processes. There's a Python equivalent to Expect: take a look at the
'pexpect' module.  Here you go:

http://pexpect.sourceforge.net/

The documentation on that page has a few examples that should help you get
started.  Best of wishes to you!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tail -f problem

2005-08-31 Thread Alberto Troiano
Hey

I'm still working with this RADIUS system over Red Hat 9.0, mysql 3.23 and 
python 2.2.2

I ran to another problem.I have a file that is written by the RADIUS 
server (for those who don't know RADIUS is an authentication Service). The 
file contains the detail of the RADIUS, the server writes the STOP and START 
signals to this filebelow is the detail of the file.
What I need to do is a program that check when the file gets an input and 
analyze only the text that has entered...I thought about tail-f 
/var/log/radacct/max/detail but this thing opens a console and I won't end 
but how can I get the output of the command to python..
This is one of two solutionsthe other would be Upgrade the MySQL Server 
to version 4.1.11 which I don't have idea how to

file desc
---
Date
  User-Name
  NAS-IP-ADDRESS
  and a long list of variables

---
end of file desc

I know the second option has nothing to do with this forum but if anyone 
knows of any manual to upgrade MySQL over Linux Red HAt I would appreciate

Thanks in advanced

Alberto


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


Re: [Tutor] Tail -f problem

2005-08-31 Thread Nick Lunt
Hi Alberto,


 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] Behalf Of
 Alberto Troiano
 Sent: 31 August 2005 22:01
 To: tutor@python.org
 Subject: [Tutor] Tail -f problem


 Hey

I thought about tail-f
 /var/log/radacct/max/detail but this thing opens a console and I
 won't end
 but how can I get the output of the command to python..

Have a look here for a possible solution to your tail -f problem
http://twistedmatrix.com/projects/core/documentation/examples/filewatch.py


 I know the second option has nothing to do with this forum but if anyone
 knows of any manual to upgrade MySQL over Linux Red HAt I would appreciate

If your using a recent Redhat then try this as root (im on a windows box at
the moment so this is untested)

$ yum upgrade mysql mysql-server

Naturally this will only work if your current mysql install is an RPM.

Cheers
Nick .


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


Re: [Tutor] Tail -f problem

2005-08-31 Thread Alberto Troiano
Hey Nick

About the mysql upgrade (by the way thanks for the reply) I have downloaded 
the rpm fron mysql.com along with the installation manual..I will give it a 
try

The code you sent in the hyperlink looks neat...I will try it out since the 
page hasn't much documentation about this example in particular

Thanks a lot

Alberto

From: Nick Lunt [EMAIL PROTECTED]
To: Alberto Troiano [EMAIL PROTECTED], tutor@python.org
Subject: Re: [Tutor] Tail -f problem
Date: Wed, 31 Aug 2005 22:38:42 +0100

Hi Alberto,


  -Original Message-
  From: [EMAIL PROTECTED]
  [mailto:[EMAIL PROTECTED] Behalf Of
  Alberto Troiano
  Sent: 31 August 2005 22:01
  To: tutor@python.org
  Subject: [Tutor] Tail -f problem
 
 
  Hey
 
I thought about tail-f
  /var/log/radacct/max/detail but this thing opens a console and I
  won't end
  but how can I get the output of the command to python..

Have a look here for a possible solution to your tail -f problem
http://twistedmatrix.com/projects/core/documentation/examples/filewatch.py


  I know the second option has nothing to do with this forum but if anyone
  knows of any manual to upgrade MySQL over Linux Red HAt I would 
appreciate

If your using a recent Redhat then try this as root (im on a windows box at
the moment so this is untested)

$ yum upgrade mysql mysql-server

Naturally this will only work if your current mysql install is an RPM.

Cheers
Nick .


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


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