[Tutor] Need info regd Singleton class implementation

2006-06-15 Thread Akanksha Govil
hi,I need to implement a singleton class in python.Please give me some refernce sites which can help me with the same.Also i want to know to call a destructor for a singleton class.ThanksAkanksha __Do You Yahoo!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need info regd Singleton class implementation

2006-06-15 Thread Andre Roberge
On 6/15/06, Akanksha Govil [EMAIL PROTECTED] wrote:
 hi,

 I need to implement a singleton class in python.
 Please give me some refernce sites which can help me with the same.

The online Python Cookbook is a good reference site.  Here's the
result from a search:
http://aspn.activestate.com/ASPN/search?query=singletonx=0y=0section=PYTHONCKBKtype=Subsection


I use the following from the printed edition of the Python Cookbook:

class Singleton(object):
From the 2nd edition of the Python cookbook.
   Ensures that only one instance is created per running script
def __new__(cls, *args, **kwargs):
if '_inst' not in vars(cls):
cls._inst = object.__new__(cls, *args, **kwargs)
return cls._inst

André


 Also i want to know to call a destructor for a singleton class.

 Thanks
 Akanksha



  __
 Do You Yahoo!?
 Tired of spam? Yahoo! Mail has the best spam protection around
 http://mail.yahoo.com
 ___
 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] Need info regd Singleton class implementation

2006-06-15 Thread Kent Johnson
Akanksha Govil wrote:
 I need to implement a singleton class in python.
 Please give me some refernce sites which can help me with the same.

Singletons are not used that much in Python, perhaps because the general 
culture of we're all adults here is biased against enforcing usage 
patterns.

If you want to have a master reference to an object you can just make it 
a module attribute. Then any client that needs access to the object just 
imports the module.

Why do you need a singleton? Is this homework?

 Also i want to know to call a destructor for a singleton class.

In Python, destructors (__del__() methods) are called on an object when 
it is garbage-collected. The details of when this happens vary between 
different Python implementations. In CPython (the standard Python from 
python.org), an object is garbage-collected when its reference count 
goes to 0 - when there are no names that refer to the object. So if you 
want the __del__() method of an object to be called, you have to ensure 
that there are no references to the object. In the case of a singleton, 
this would mean deleting or assigning None to the name that holds the 
master reference, and ensuring that no users of the singleton retain 
references to it.

Kent

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


[Tutor] Question about network server in python

2006-06-15 Thread Tino Dai
Hi there, I am wondering if somebody to could answer a question about sockets. I have a socket that is listening, and a client program connects to it. The client program transfers a name over, and then disconnects from the socket. Now, how that is done is using a 
socket.close() call to shut down the entire socket. My question is: Is there a way to have the socket close the connection, yet stay open for the next client that comes along and connects to the server? I have my already written code below for further documentation. Thanks!
while 1: s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) s.bind((self.ipAddr,self.port)) s.listen(5)  print The port is: , 
self.port  client,addr=s.accept() while 1: try: if addr[0] == self.remoteIpAddr: client.send(Connected to the server\n) 
 msg=client.recv(1024) msg=msg.strip() if msg in 'exit': s.close() #Is there a different way to write this?
 time.sleep(30) print exiting break if len(msg)  0:
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about network server in python

2006-06-15 Thread Peter Jessop
I think the problem here is the 'break' statement.
Does it not put you outside the while loop whereas in order to keep
the server socket open you need it to loop forever.

I also think that the s.accept should be inside the while loop.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about network server in python

2006-06-15 Thread Peter Jessop
import socket
host = ''
port = 57000
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((host,port))
s.listen(5)
while 1:
client,addr=s.accept()
client.send(Connected to the server\n)
#if someCondition:
 #   cliente.close()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about network server in python

2006-06-15 Thread Kent Johnson
Peter Jessop wrote:
 I think the problem here is the 'break' statement.
 Does it not put you outside the while loop whereas in order to keep
 the server socket open you need it to loop forever.
 
 I also think that the s.accept should be inside the while loop.

There are two loops, I think you missed the outer loop.

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


Re: [Tutor] Question about network server in python

2006-06-15 Thread Kent Johnson
Tino Dai wrote:
 Hi there,
 
   I am wondering if somebody to could answer a question about 
 sockets. I have a socket that
 is listening, and a client program connects to it. The client program 
 transfers a name over, and then disconnects from the socket. Now, how 
 that is done is using a socket.close() call to shut down the entire 
 socket. My question is: Is there a way to have the socket close the 
 connection, yet stay open for the next client that comes along and 
 connects to the server? I have my already written code below for further 
 documentation. Thanks!
 
 while 1:
   s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
   s.bind((self.ipAddr,self.port))
   s.listen(5)
   print The port is: , self.port   
   client,addr=s.accept()
   while 1:
try:
if addr[0] == self.remoteIpAddr:
client.send(Connected to the server\n)   
msg=client.recv(1024)
msg=msg.strip()
if msg in 'exit':
 s.close()  #Is there a different 
 way to write this?

I think you want to close the client socket - client.close() - rather 
than the master socket.

You might be interested in the SocketServer library which helps to write 
simple socket servers such as this. One advantage of using SocketServer 
is it makes it trivial to convert your server to a threaded or forked 
server. Here are some examples:
http://www.amk.ca/python/simple/fingerd.py.html
http://examples.oreilly.com/pythonian/ see example 19-5

Kent

 time.sleep(30)
 print exiting
 break
if len(msg)  0:
 
 
 
 
 ___
 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] WinXP IDLE - open file : how-to change default directory ?

2006-06-15 Thread learner404
Hello,On WinXP IDLE will always 'open' in Python24 folder at startup.A beginner friend of mine hate that (personally I use SPE) and I tried 'quickly' to find a way to configure that for him.I found all the .cfg files in idlelib and also the .idlerc folder in the Documents and settings but I don't see how to give IDLE a default directory at startup (or kipping in memory the last one opened).
How to do that or is it just not possible ?Thanks learner404
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] WinXP IDLE - open file : how-to change default directory ?

2006-06-15 Thread learner404
On 15/06/06, Matthew Webber [EMAIL PROTECTED] wrote:
Try right-clicking on the shortcut, select properties, and change the startin value.MatthewScary, I didn't know that windows had that ...Thanks very much Matthew !
P.S. When posting to the list, please use plain text format.
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] OnBehalf Of learner404Sent: 15 June 2006 20:09
To: tutor@python.orgSubject: [Tutor] WinXP IDLE - open file : how-to change defaultdirectory ?Hello,On WinXP IDLE will always 'open' in Python24 folder at startup.
A beginner friend of mine hate that (personally I use SPE) and Itried 'quickly' to find a way to configure that for him.I found all the .cfg files in idlelib and also the .idlerc folder in
the Documents and settings but I don't see how to give IDLE a defaultdirectory at startup (or kipping in memory the last one opened).How to do that or is it just not possible ?Thanks
learner404
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XML: Expletive Deleted (OT)

2006-06-15 Thread Alan Gauld
Just picked this up after being out for most of the week...

Carroll, Barry [EMAIL PROTECTED] wrote in message

 One reason to for choosing a human-readable format is the desire to
 visually confirm the correctness of the stored data and format.

Thats a very dangerous asumption, how do you detect unprintable
characters, tabs instead of spaces, trailing spaces on a line etc etc.
Whole text representations are helpful you should never rely on the
human eye to validate a data file.

 can be invaluable when troubleshooting a bug involving stored data. 
 If
 there is a tool between the user and the data, one must then rely 
 upon
 the correctness of the tool to determine the correctness of the 
 data.

Or the correctness of the eye. I know which one i prefer - a tested 
tool.
The human eye is not a dta parser, but it flatters to deceive by being
nearly good enough.

 In a case like this, nothing beats the evidence of one's eyes, IMHO.

Almost anything beats the human eye IME :-)
Actually if you must use eyes do so on a hex dump of the file, that
is usually reliable enough if you can read hex...

 In their book, The Pragmatic Programmer: From Journeyman to Master
 (Addison Wesley Professional), Andrew Hunt and David Thomas give 
 another
 reason for storing data in human readable form:

The problem with most binary formats is that the context 
 necessary
to understand the data is separate from the data itself. You are
artificially divorcing the data from its meaning. The data may
as well be encrypted; it is absolutely meaningless without the
application logic to parse it. With plain text, however, you can
achieve a self-describing data stream that is independent of the
application that created it.

But at a very high risk. I do not dislike text files BTW and am not
suggesting that text should not be used but its parsing is best left
to machines, the eye is only a rough and unreliable guide.

And if your data volumes are high go with binary, you'll need tools
to parse a lot of data anyway, you might as well save the space!

The Hunt/Thomas book is excellent BTW - I recommend it highly.
Even though I disagree witrh several of their suggestions(*) I agree
with far more.

(*)They recommend sticking with one text editor whereas I use
about 5 or 6 on a regular basis depending on the job I'm doing and
the platform I'm working on. Emacs on X Windows for new files
but vim for quick fix ups, vim on Windows for most things,
ed or ex for text based email or over a phone line.

 This is an example of the resource balancing act that computer 
 people
 have been faced with since the beginning.  The most scarce/expensive
 resource dictates the program's/system's design.  In Alan's example 
 high
 speed bandwidth is the limiting resource.  A data transmission 
 method
 that fails to minimize use of that resource is therefore a bad 
 solution.

Unfortunately the software industry is full of people who by and large
don't understand networks so they just ignoire them. At least thats
my experience! SOA using SOAP/XML is probably the most inefficient
and unreliable set of data networking technologies you could possible
come up with. But the focus is on cutting developer cost because the
people inventing it are developers! In almost every sizewable project
the cost of development will be significantly less than the cost of
deployment - in most of my projects it usually works out something 
like:

development - 15%
deployment - 30%
support - 15%
training - 25%
documentation - 5%
management overhead - 10%

Saving 25% of development costs rediuced total cost by around 4% but
if that puts deployment costs up by 10% the net gain is only 1%!
And in XML case it often puts deployment costs up by 100%
- a net loss of  24%!!
Now those figures come from a typical project that I work on which
probably has a total budget of betwen $10-100 million. If your
budget is smaller, say less than $1 million then the balance may
well change. But over 50% of the IT industry works on projects
with  $1m budgets according to both Datamation and Infoweek.

[ The only SOA/XML book that addresses this side of XML usage
is the excellent SOA - A Field Guide by Peter Erls. Erls also
suggests some mitigating strategies to get round it.]

 So here's my off-topic question: Ajax is being touted as the 
 'best-known
 method' (BKM) for making dynamic browser-based applications, and XML 
 is
 the BKM for transferring data in Ajax land.  If XML is a bad idea 
 for
 network data-transfer, what medium should be used instead?

The example I gave of having to upgrade the sites network was actually
an early adopter of XML/Ajax architecture! There are lots of other 
data
formats around - some are even self describing (CSV and TLV are cases)
Others simply hold the definition in an accessible library so you only
have to transport it once - eg IDL and ASN.1 - or optionally compile 
it
into your code for maximum efficiency. ASN./1 is typically around 50

[Tutor] lambda and creating GUIs

2006-06-15 Thread Christopher Spears
I have been reading though the PyGTK tutorial.
Can anyone explain how lambda is being used in this
statement:

button.connect(clicked, lambda w: gtk.main_quit())

This code might put the statement in context:

# Create Quit button
   66   button = gtk.Button(Quit)
   67   
   68   # When the button is clicked, we call
the main_quit function
   69   # and the program exits
   70   button.connect(clicked, lambda w:
gtk.main_quit())
   71   
   72   # Insert the quit button into the both
lower quadrants of the table
   73   table.attach(button, 0, 2, 1, 2)
   74   
   75   button.show()

The complete script is located at
http://www.pygtk.org/pygtk2tutorial/sec-TablePackingExample.html.


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


Re: [Tutor] lambda and creating GUIs

2006-06-15 Thread John Fouhy
(resending to include Tutor -- sorry for the dupe, Christopher)

On 16/06/06, Christopher Spears [EMAIL PROTECTED] wrote:
 I have been reading though the PyGTK tutorial.
 Can anyone explain how lambda is being used in this
 statement:

 button.connect(clicked, lambda w: gtk.main_quit())


Do you understand what lambda does in general?

Does it help if I rewrite the code like this:

def onQuit(w):
   gtk.main_quit()
button.connect(clicked, onQuit)

?

Provided you get your indentation correct (def onQuit and
button.connect should be at the same level), and provided you aren't
using the name onQuit somewhere else in that method, this code
should be a dropin replacement for the line you quote.

--

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