[Tutor] OT, Tcl & Python

2005-07-16 Thread Luis N
Hi,

I was wondering if someone knowledgeable of both Tcl and Python could
suggest whether it would be a good or a bad idea to write a Python/Tk
application, with the motive to rewrite the application in Tcl/Tk once
completed. My reason for considering this route is that I have never
written a single line of Tcl code nor coded a Tk application in the
past. My motivation is the greater ease of deployment across systems
that Tcl seems to offer, with Starkits and Starpacks,
http://www.equi4.com/starkit.html Tcl also appears useful to learn, for
writing scripts in tclsh, etc.  

I've experimented with py2exe in the past, which seems fine for
Windows, although I have never tried py2app, and this approach seems
cumbersome. A typical GUI app is approximately 5 MB in python,
distributed as a collection of files in a folder, whereas a Tcl
Starpack is a compact 1 MB, distributed as a single file executable. 

Sincerely,


Luis 


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


[Tutor] is there an online exam or coarse i can take to get a certificate

2005-07-16 Thread Mustafa Abbasi
is there an online exam or coarse i can take to get a certificate in python.
preferrably cheap and online because i live in pakistan and online exams are my only hope.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Closing BaseHTTPServer...

2005-07-16 Thread lawrence wang
Here's some more detail about how I've got things set up. I left a
bunch of things out of my original email; sorry about that, I was
rushed for time. I don't have access to the verbatim code right now,
but here's the gist. I've subclassed threading.Thread like this:

class ServerThread(threading.Thread):
def __init__(self):
self.httpd = BaseHTTPServer()

def run(self):
self.httpd.serve_forever()

def close(self):
self.httpd.server_close()
del self.httpd

I was expecting server_close() to stop serve_forever(), but this
doesn't seem to be the case, since the threads still stick around
after I run close() (that is, the extra python instances still show up
in the list of processes). When I try to use the port again after
closing, I get socket.error,'address already in use' or something like
that.

I'm running Python 2.4 on Debian Linux, btw.

On 7/15/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
> 
> 
> > I've been using BaseHTTPServer (and subclassing BaseHTTPRequestHandler,
> > of course) for a project at work. However, I can't seem to close my
> > connections completely once I'm done with the server. I've tried:
> >
> > server.server_close()
> > del server
> >
> > but when I try to use the same port again, it complains that it's
> > already bound; what's more, the interpreter hangs when I try to exit.
> > Thanks in advance for any help you can offer!
> 
> 
> Hi Lawrence,
> 
> I'm not exactly sure if this is the issue you're running into; if you can
> show us code, that'll help.  Are you sure nothing's running as a daemon
> afterwards?  You may want to try the unix utility 'telnet' and just make
> double-check that the server port is closed.
> 
> 
> If everything is truly closed, then it still usually takes a moment
> between restarts before the socket is available for use again. If we want
> to force the issue, we can use the socket.SO_REUSEADDR attribute.  For a
> general idea of what common problem is, see:
> 
> http://www.unixguide.net/network/socketfaq/4.5.shtml
> 
> 
> Concretely, before binding the server's socket to a port, we may need to
> set a few socket parameters to reuse a port that's still in cooldown.  If
> we have a socket object that hasn't been bound yet, then we can do
> something like this:
> 
> ##
> socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
> socket.bind(server_address)
> ##
> 
> We shouldn't have to do this either if we're using BaseHTTPServer, since
> the mechanism for calling SO_REUSEADDR already exists in
> SocketServer.TCPServer.server_bind.
> 
> 
> The thing that bothers me is that this should already be happening for
> you, since by default, allow_reuse_address is set to true for subclasses
> of HTTPServer.  At least, this appears to be true in Python 2.3, according
> to this code snippet in the BaseHTTPServer code:
> 
> ### BaseHTTPServer.py ###
> class HTTPServer(SocketServer.TCPServer):
> 
> allow_reuse_address = 1# Seems to make sense in testing environment
> 
> def server_bind(self):
> """Override server_bind to store the server name."""
> SocketServer.TCPServer.server_bind(self)
> host, port = self.socket.getsockname()[:2]
> self.server_name = socket.getfqdn(host)
> self.server_port = port
> ##
> 
> 
> So I think we need some more information before we nail down what's really
> happening.  Show us some error messages, and some code, and we might be
> able to get a better idea of the situation.
> 
> 
> Good luck!
> 
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] PLEASE HELP ON PYTHON

2005-07-16 Thread Suranga Sarukkali



Hai, I (Suranga) started using Python2.4 Downloaded from the 
Original WebSite www.python.org a week ago 
but still I have not made any good progress on Programming and all that 
Tutorials available make it like hell to get started at all! whatever please 
tell me on [EMAIL PROTECTED] how to 
get started and start programming and since I never programmed before or haven't 
done any big Math Tests but a k-12 School still I'm a Student of 14 but I'm 
loving the name programming even, reply to me as soon as 
possible.
BEGIN:VCARD
VERSION:2.1
N:Sarukkali;Suranga;;Ms
FN:Suranga Sarukkali
NICKNAME:saruk
TEL;HOME;VOICE:0942610560
ADR;HOME:;;12B, 4th Cross Lane, Borupana Road,;Ratmalana,;WP,;10345;Sri Lanka
LABEL;HOME;ENCODING=QUOTED-PRINTABLE:12B, 4th Cross Lane, Borupana Road,=0D=0ARatmalana,, WP, 10345=0D=0ASri Lank=
a
X-WAB-GENDER:2
BDAY:19891011
EMAIL;PREF;INTERNET:[EMAIL PROTECTED]
REV:20050716T041838Z
END:VCARD
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Saving class data to a text file

2005-07-16 Thread Darryl Luff
Hi all. I'm looking at Python for the first time, and using it for an
app I'm writing to try it out. I normally use c or perl.

I have a class that has half a dozen or so data fields. It needs to be
able to save its data to a text file, in a format that's easy to edit
with other tools (vi, perl, etc). And needs to be able to load itself
back from disk.

I discovered that a dictionary can convert itself into a nice string
format that I can save directly to a file. So I thought I could put a
dictionary in the class to hold the data, and save the string
representation of the dictionary to the file. Each object instance is on
a single text line. I created get/set methods for each field to avoid
problems if I mis-type the dictionary keys.

Now I have a couple of questions:
- What is the recommended 'python' way to implement this class? and
- If what I'm doing is OK, is there an easy way to load a dictionary
from the string representation without parsing the string manually?


Thanks for your time!

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


Re: [Tutor] PLEASE HELP ON PYTHON

2005-07-16 Thread Brian van den Broek
Suranga Sarukkali said unto the world upon 16/07/2005 00:18:
> Hai, I (Suranga) started using Python2.4 Downloaded from the
> Original WebSite www.python.org a week ago but still I have not
> made any good progress on Programming and all that Tutorials
> available make it like hell to get started at all! whatever please
> tell me on [EMAIL PROTECTED] how to get started and start
> programming and since I never programmed before or haven't done any
> big Math Tests but a k-12 School still I'm a Student of 14 but I'm
> loving the name programming even, reply to me as soon as possible.


Hi Suranga,

welcome to this list and to Python!

I think you have found one of the very best resources for learning 
Python, so you are off to a good start. The way to use this list best, 
in my opinion, is to post specific questions. Questions with some code 
are even better. (It is hard for people to know what would help you 
without seeing what you don't understand.)

The people who do a lot of answering here are very patient -- they've 
put up with all sorts of questions from me :-)

One tutorial I think you might find useful is 
. It is aimed at high school 
students, but does not condescend. Why don't you try that from the 
beginning, and when stuck on something, post here asking about it?

Best of luck.

Brian vdB

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


Re: [Tutor] PLEASE HELP ON PYTHON

2005-07-16 Thread Rob Andrews
On 7/15/05, Suranga Sarukkali <[EMAIL PROTECTED]> wrote:
>  
> Hai, I (Suranga) started using Python2.4 Downloaded from the Original
> WebSite www.python.org a week ago but still I have not made any good
> progress on Programming and all that Tutorials available make it like hell
> to get started at all! whatever please tell me on [EMAIL PROTECTED] how to
> get started and start programming and since I never programmed before or
> haven't done any big Math Tests but a k-12 School still I'm a Student of 14
> but I'm loving the name programming even, reply to me as soon as possible. 

We'll be happy to help.

What sort of programming interests you? Since I'm one of the world's
most incompetent programmers and find that I can work out the
occasional python program, I'm sure you'll be just fine.

There are quite a few tutorials to help get you started. Have you
checked out: http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html
?

namaste,
Rob
-- 
Golf with an attitude:
http://www.ragingolf.com/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] cPickle.load()

2005-07-16 Thread David Jimenez
hello everybody,

I am trying to learn to use Python. Currently, I am
reading Michael Dawson's "Python Programming for the
Absolute Beginner." Right now, I am having the
following problem: I try to read all the pickles in a
file, but keep on getting the same error: EOFError.


This is what the code looks like:

import cPickle, shelve

print "Pickling lists."
variety=["sweet","hot","dill"]
shape=["whole","spear","chip"]
brand=["Claussen","Heinz","Vlassic"]
pickle_file=open("pickles1.dat","w")
cPickle.dump(variety,pickle_file)
cPickle.dump(shape,pickle_file)
cPickle.dump(brand,pickle_file)
pickle_file.close()

print "\nUnpickling lists."
pickle_file=open("pickles1.dat","rb")
for i in pickle_file:
i=cPickle.load(pickle_file)
print i
pickle_file.close()

This is what I keep on getting:
Pickling lists.

Unpickling lists.

Traceback (most recent call last):
  File "/Users/davidjimenez/Documents/trypickle", line
20, in -toplevel-
i=cPickle.load(pickle_file)
EOFError

Thank you,
David Jimenez






Start your day with Yahoo! - make it your home page 
http://www.yahoo.com/r/hs 
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] cPickle.load()

2005-07-16 Thread Terry Carroll
On Sat, 16 Jul 2005, David Jimenez wrote:

> [EOFError on pickle]

First, something that may not be a problem, but let's get it out of the 
way, anyway:

> pickle_file=open("pickles1.dat","w")
>. . .
> pickle_file=open("pickles1.dat","rb")

I notice that your write open does not specify binary mode, but your read 
open does.  If you're on an OS where binary mode matters (e.g., Windows), 
you should use it in both places, or the file you write out will probably 
be corrupted.


But here's your real problem (I'm adding line numbers in brackets for 
reference):


> for i in pickle_file:#1
> i=cPickle.load(pickle_file)  #2
> print i  #3
> pickle_file.close()  #4

On line #1, you're telling it to iterate over the pickel file.  That is, 
line #1 says, read a line from pickle_file, and assign it to i.

Then, in line #2, bearing in mind you've already taken the first line out 
of the file, you're doing a pickle load, starting part-way into the file.

I suspect that, because it's a binary file, your line #1 is putting the 
whole file contents into i, and nothing's left in the file to process.  
When you do the load, you hit EOF, hence the error.

It should be a big red flag to you that you're modifying variable i both 
in line #1 and line #2.  That never leads anywhere good.

What I would suggest is that you endlessly loop over a load call, and wrap 
it in a try/except combination to exit on EOF.  Here's an equivalent to 
the above four lines, that takes that approach:


=
try: 
while True:
i=cPickle.load(pickle_file)
print i
except EOFError:
pass

pickle_file.close()
=


Hope this helps.


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


Re: [Tutor] Saving class data to a text file

2005-07-16 Thread Javier Ruere
Darryl Luff wrote:
> [...] I created get/set methods for each field to avoid
> problems if I mis-type the dictionary keys.

 An automated way to do that:

class M(type):
def create_properties(cls, obj, *prop_names, **prop_names_values):
for pname in prop_names:
setattr(cls, pname, cls._create_property(pname))

for pname, value in prop_names_values.iteritems():
setattr(cls, pname, cls._create_property(pname, value))

def _create_property(cls, pname, init_value = None):
container_name = '__' + pname

def setter(self, value): setattr(self, container_name, value)
def getter(self):
if not hasattr(self, container_name):
return init_value
return getattr(self, container_name)

return property(getter, setter)

class A(object):
__metaclass__ = M

def __init__(self):
A.create_properties(self, 'prop1', 'prop2', prop3=8, **{'prop4':9})

a = A()
print a.prop1
a.prop1 = 3
print a.prop1
print a.prop3
a.prop3 = 'asdf'
print a.prop3
print a.prop4


> Now I have a couple of questions:
> - What is the recommended 'python' way to implement this class? and

  Don't know.

> - If what I'm doing is OK, is there an easy way to load a dictionary
> from the string representation without parsing the string manually?

  I wouldn't write the dictionary directly to a file, I would use ConfigParser.
  An easy but terribly insecure way to do what you ask is using eval (for ex.: 
eval(`{ 1:1 }`) ) but I do not recommend this.

Javier

PS: M was my first metaclass! :) If the same result can be achieved without one 
I would like to know the way.

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


[Tutor] Closing BaseHTTPServer...

2005-07-16 Thread Python
This sidesteps the issue of how to fix your threading code.
SocketServer has a ThreadingMixIn class.  You can create a threading
HTTPServer without coding the threading yourself.

(watch for typos - this is NOT from working code)

class ThreadingHTTPD(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
pass
srvr = ThreadingHTTPD( , )
srvr.serve_forever()

Credit for this goes to John Goerzen and his fine book "Foundations of
Python Network Programming"

-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] OT, Tcl & Python

2005-07-16 Thread Alan G
> I was wondering if someone knowledgeable of both Tcl and Python 
> could
> suggest whether it would be a good or a bad idea to write a 
> Python/Tk
> application, with the motive to rewrite the application in Tcl/Tk

Oooh that's a tough one, it would depend on the program.
Python is a very different language to Tcl and you would need
to write the code in a Tcl style - don't use an OOP style for
the GUI for example. In fact I'd avoid OOP all together since
Tcl OOP is significantly different to Python OOP and not even
part of standard Tcl.

Its much harder to translate a Python script to Tcl than to
translate Python to C++ or Delphi or even Perl because they
have different underlying philosophies. You could just translate
line by line but the end program wouldn't look like anything
a Tcl programmer would recognise!

> My reason for considering this route is that I have never written
> a single line of Tcl code nor coded a Tk application in the past.

You might like to take a quick run through the basics of my old
Python tutor which used Tcl as a comparison to Python. Its far
from comprehensive but would give a fairly good Python view
of Tcl.

http://www.freenetpages.co.uk/hp/alan.gauld/oldtutor/index.htm

> motivation is the greater ease of deployment across systems that Tcl 
> seems
> to offer, with Starkits and Starpacks

Tcl has several nice features there, but if you just deploy the
scripts its not much different to Python. And since I hate bloatware
I nearly always deploy the scripts and interpreter separately.

> also appears useful to learn, for writing scripts in tclsh, etc.

Tcl is a fun language with a wholeheap of interesting concepts new to
Python programmers. So its definitely worth looking at - and its
nearly always more compact than Python too. Its main limitations
are lack of suitability for large projects and performance. The
latter is relative and if you really need fast code use C!

> typical GUI app is approximately 5 MB in python,
> distributed as a collection of files in a folder,
> whereas a Tcl Starpack is a compact 1 MB

Thats because the Tk GUI is a fairly big DLL including the TCl
interpreter as well as Python! You are effectively distributing
Tcl and Python together

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] PLEASE HELP ON PYTHON

2005-07-16 Thread Alan G
Hello,

> Downloaded from the Original WebSite

Did you get it installed OK? I'll assume you use Windows,
if another OS let us know which. Is there a Python menu
in your Start->Programs menu?

If you select Python GUI dows a window open called
Python Shell or similar? It should contain a prompt like

>>>

at which you can type Python commands.


> ...all that Tutorials available make it like hell to get started

Which tutorials have you used? The standard one that comes with Python
is intended for experienced programmers converting to Python. There
is a separate page of tutorials for non programmers (Including mine)?

Have you tried any of those ones? If so which?
They all have strengths and weaknesses.

If you are stuck at a particular biyt then ask questions here, the
more specific the better. Also if you can include any error messages
that Python presents that will help a lot. They may look meaningless
to a beginner but they actually contain a lot of useful information
once you learn how to read them!

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] Saving class data to a text file

2005-07-16 Thread Alan G
> I have a class that has half a dozen or so data fields. It needs to 
> be
> able to save its data to a text file, in a format that's easy to 
> edit
> with other tools (vi, perl, etc). And needs to be able to load 
> itself
> back from disk.

Take a look at the OOP topic in my tutor. It includes an examnple
of how to save/load objects to text files.

Other options include the pickle module but it isn't that user
friendly to edit in vi etc...

> Now I have a couple of questions:
> - What is the recommended 'python' way to implement this class?

The native python solution would be to use pickle or shelve modules.
If it must be human friendly too then the approach in my tutor
supplemented by overloading the __repr__() method would work.

> - If what I'm doing is OK, is there an easy way to load a dictionary
> from the string representation without parsing the string manually?

You might try eval() but it is full of security implications, rogue
code in your data could be evaluated and do who knows what kind of
mischief...

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


[Tutor] how to add python code to a webpage

2005-07-16 Thread Mustafa Abbasi
i want to create a simple page which has a form
and takes in persons date of birth and give out exact age.
how do i add python codeto an html page.
i don't have any dreamweaver or frontpage.
this is basically cuz some freinds advised me to create this isnce i am learning programming python..
so please help 
thanks .
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Saving class data to a text file

2005-07-16 Thread Jonathan Conrad


class OOO (object):
def __init__ (i, **a): i.__dict__ = a
def __repr__ (i):
a = i.__dict__
return "%s (%s)" % (type (i).__name__, ", ".join (
"%s=%r" % (o, a [o]) for o in sorted (a.iterkeys ())
))

class OO (object):
def __init__ (i, *o, **a):
if o:
p = i.__slots__
for x in xrange (len (o)):
setattr (i, p [x], o [x])
for x in a: setattr (i, x, a [x])
def __repr__ (i):
a = list ()
q = set ()
try: o = i.__slots__
except: pass
else:
j = 0
for x in o:
if x in q: break
try: a.append (repr (getattr (i, x)))
except: break
q.add (x)
j += 1
for j in xrange (j, len (o)):
x = o [j]
if x not in q:
try: a.append ("%s=%r" % (x, getattr (i, x)))
except: pass
q.add (x)
try:
for x, o in sorted (i.__dict__.iteritems ()):
if x not in q:
a.append ("%s=%r" % (x, o))
q.add (x)
except: pass
return "%s (%s)" % (type (i).__name__, ", ".join (a))

class RGB (OO):
__slots__ = ["r", "g", "b"]
def __str__ (i):
return "#%02X%02X%02X" % (i.r, i.g, i.b)

x = [
RGB (0, 0, 0, name = "black"),
RGB (255, 0, 0, name = "red"),
RGB (0, 255, 0, name = "green"),
RGB (0, 0, 255, name = "blue"),
RGB (0, 255, 255, name = "cyan"),
RGB (255, 0, 255, name = "magenta"),
RGB (255, 255, 0, name = "yellow"),
RGB (255, 255, 255, name = "white")
]

file ("marshal.txt", "w").write ("\n".join (
["[", ",\n".join (repr (o) for o in x), "]", ""]))

y = eval (file ("marshal.txt").read ())
print y
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor