[Tutor] 2 Combo Boxes! What was I thinking?

2006-05-04 Thread John CORRY








Hi,

 

I have set up a GUI which has amongst other widgets two
combo boxes.  I am using:

 

PythonCard version:
0.8.1

wxPython version:
2.6.1.0

Python version: 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]

Platform: win32

Glade 2

 

I started by setting up comboboxentry2 to accept two
settings in its drop down list.  (open + closed). 
This worked fined.  Code
below:

 

self.wTree = gtk.glade.XML ("phonelog.glade",
"window1")

    dic={"on_window1_destroy"
: self.quit, }

    self.wTree.signal_autoconnect (dic)

    

   
combo1 = self.wTree.get_widget("comboboxentry2")

    

    

   
combo1.append_text("Open")

   
combo1.append_text("Closed")

   
combo1.set_active(0)

    combo1.connect("changed", self.callback2, combo1)

    

My problems started when I went to set up comboboxentry3.  I used the following code:

 

 

self.wTree = gtk.glade.XML ("phonelog.glade",
"window1")

    dic={"on_window1_destroy"
: self.quit, }

    self.wTree.signal_autoconnect (dic)

    

    

   
combo3 = self.wTree.get_widget("comboboxentry3")

    combo3.connect("changed", self.callback3,
"comboboxentry3")

    

   
combo3.append_text ("Mon")

   
combo3.append_text ("Tue")

    

   
combo1 = self.wTree.get_widget("comboboxentry2")    

   
combo1.append_text("Open")

   
combo1.append_text("Closed")

   
combo1.set_active(0)

    combo1.connect("changed", self.callback2, combo1)

    

 

  I got the
following error:  

 

Gtkwarning: gtk_combo_box_append_text: assertion GTK_IS_LIST_STORE(Combobox->Priv->Model)

Failed

Combo3.append_text(“Mon”)

Gtkwarning: gtk_combo_box_append_text: assertion GTK_IS_LIST_STORE(Combobox->Priv->Model)

Failed

Combo3.append_text(“Tue”)

 

I then tried the code:

 

self.wTree = gtk.glade.XML ("phonelog.glade",
"window1")

    dic={"on_window1_destroy"
: self.quit, }

    self.wTree.signal_autoconnect (dic)

    

    

   
combo3 = self.wTree.get_widget("comboboxentry3")

    combo3.connect("changed", self.callback3,
"comboboxentry3")

    

   
combo3.child.append_text ("Mon")

   
combo3.child.append_text ("Tue")

    

   
combo1 = self.wTree.get_widget("comboboxentry2")    

   
combo1.append_text("Open")

   
combo1.append_text("Closed")

   
combo1.set_active(0)

    combo1.connect("changed", self.callback2, combo1)

 

 I get the
following error message:

 

DeprecationWarning: use GtkEditable.insert_text

Combo3.child.append_text(“Mon”)

DeprecationWarning: use GtkEditable.insert_text

Combo3.child.append_text(“Tue”)

 

The combobox3 is populated on the GUI but instead of being a
drop down list it appears as a string on one line and looks like this:

MonTue

 

I have to confess I am not really sure what I am doing.  I just guessed at putting the child
command in.  It gets me close but
not close enough. 
Can anyone tell me what I am doing wrong or explain what happens when
you put two combo boxes on the one GUI?

 

Any help greatly appreciated.

 

Thanks,

 

John.

 






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


[Tutor] UI developing

2006-05-04 Thread Alfonso
Hi,

I'm starting with python on linux. I would like to make gtk user 
interfaces. Wich is the best option to do that? gtk-bindings, glade? If 
I want to port a program with gtk interface to windows, what should I use?

Thank you very much for your attention.


__ 
LLama Gratis a cualquier PC del Mundo. 
Llamadas a fijos y móviles desde 1 céntimo por minuto. 
http://es.voice.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Memory Management etc

2006-05-04 Thread Philip Smith



Hi
 
I use Activestate Python (2.4.3) in a Windows 32 
bit environment.
 
I have a problem with a large programme that uses a 
lot of memory (numerous large arrays which are dynamically extended).  I 
keep getting unpredictable crashes (on a machine with 1/2 GB memory) whereas on 
my laptop (1 GB memory) it seems OK.
 
The portion of the code which crashes varies a bit 
but curiously is NEVER a part where any memory allocation is 
happening.
 
There are no debugger messages it just crashes (and 
reboots the machine more often than not).
 
I have so far tried:
 
Using the gc module:  can't seem to locate any 
circular references and garbage collection works OK.
 
Surrounding all memory allocation code with 
Try...Except but no MemoryError ever gets reported.
 
Running under various versions of Python (standard 
distrib from Python Org, Stackless Python etc)
 
This kind of crash is classically a stack/heap 
collision problem (and/or segmentation fault) but if I monitor memory usage 
during execution (with Windows Monitor) I usually have 200 or so MB free at the 
point of crash.
 
I have to say I have noticed (the programme is 
basically a batch-factoring programme for integers) that no matter how I tune gc 
I can't get it to reliably free memory in between factoring each 
integer.
 
Because this is a development programme (and for 
performance reasons) I use global variables some of which refer to the large 
arrays.
 
My questions are:
 
1)    Does the mere fact that a 
function cites a variable as global create a reference which prevents it being 
collected by gc?
2)    I notice that in Unix I would 
have access to a resource module which might help - is there nothing similar in 
Python for Windows?
3)    Is there any way to allocate a 
pool of available memory to my programme at the outset from which I can allocate 
my large arrays?
4)    Is there any module / function 
which would enable me to access the BYTE size of Python objects such as arrays 
etc or to track memory usage from within Python.
5)    Does anyone have ANY 
suggestions please?
 
I'm keen to solve this because I would like to make 
my programme generally available - in every other respect its near complete and 
massively outperforms the only other comparable pure python module (nzmath) 
which does the same job.
 
Thanks in anticipation.
 
Phil
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] counting number of inputs (EARLIER VERSION SENT ACCIDENTLY)

2006-05-04 Thread MICHELLE EVANS
This is exactly what I am trying to do.  I am so confused with trying to
write this.  I am not very familiar with any of the functions.  I keep
reading my book and reading my book, and none of it seems to make sense
anymore.  I can write extremely simple functions, but when I need to use
more than one in a code, I'm lost!

Thanks

- Original Message - 
From: "Marc Poulin" <[EMAIL PROTECTED]>
To: 
Sent: Thursday, May 04, 2006 12:08 AM
Subject: Re: [Tutor] counting number of inputs (EARLIER VERSION SENT
ACCIDENTLY)


>
> Michelle:
>
> Are you familiar with writing functions?
> Here I've created a function named getInputs.
>
> I've also created a few test cases to verify that (a)
> my understanding of the problem is correct, and (b) my
> solution is correct.
>
> It's important to think about how your program is
> supposed to behave in different situations. Do you
> think these 3 tests are enough to prove that the code
> is correct?
>
> #
> ## start of code   ##
> #
> def getInputs():
> """
> Description:
>Collect numbers entered by the user (up to a
> maximum of 5 values) and
>store them in the listOfValues.
>
>Stop collecting numbers if the user enters -1
> or if 5 numbers have been collected.
>
>If the user entered -1, the -1 is NOT returned
> as part of the list.
> """
> listOfValues = [] ## this list holds the values
> entered by the user
>
> for i in range(5):
> newValue = int(raw_input('Enter a number [-1
> to exit]:'))
> if newValue == -1:
> # Return right now with whatever is
> currently in the list.
> return listOfValues
> else:
> # Add this new value to the list and keep
> looping.
> listOfValues.append(newValue)
>
> ## If we got this far, it means the user did not
> enter a -1 so
> ## the list contains 5 values.
> return listOfValues
>
> """
> Here are a few test cases to verify the logic of my
> code.
>
> Test Case 1:
>INPUTS:
>   first entered value: -1
>RESULT:
>   function returns empty list
>
> Test Case 2:
>INPUTS:
>   first entered value: 1
>   second entered value: 2
>   third entered value: -1
>RESULT:
>   returned list contains [1,2]
>
> Test Case 3:
>INPUTS:
>   first entered value: 1
>   second entered value: 2
>   third entered value: 3
>   fourth entered value: 4
>   fifth entered value: 5
>RESULT:
>   returned list contains [1,2,3,4,5]
> """
> if __name__ == "__main__":
> print getInputs()
>
> ###
> ## end of code   ##
> ###
>
>
> --- Python <[EMAIL PROTECTED]> wrote:
>
> > On Wed, 2006-05-03 at 15:33 -0400, MICHELLE EVANS
> > wrote:
> > > OK, I've tried a different approach to this.
> > > How do I get this to stop by using -1?
> > > I do not want this to print until either 5 inputs
> > have been entered or -1
> > > has been entered.  See below:
> > >
> >
> > use a "for block" rather than a "while block" to
> > have a normal limit of
> > 5 repetitions:
> >
> > for x in range(5):
> >
> > will repeat 5 times with x running from 0 to 4.
> > x is ignored - unless some use for it does turn up.
> >
> > the break statement allows you to terminate a block,
> > so
> >
> > if number == -1: break
> >
> > will end the for block.
> >
> >
> > Now, one of the cute features in Python is the else
> > clause that goes
> > with the for and while blocks.  The else block is
> > executed when there is
> > no break.  So the skeleton for your program can look
> > something like
> >
> > for x in range(5):
> > # get inputs and break on -1
> > else:
> > # no break so just process the inputs
> >
> > Good luck.
> >
> > > # Add number of per hour
> > > numbers = []
> > > stop = None
> > > while stop != "-1":
> > > number = int(raw_input("Run number(-1 to end)
> > : "))
> > > numbers.append(number)
> > > print
> > > for number in numbers:
> > > print number
> > >
> > >
> > >
> > >
> > > - Original Message - 
> > > From: "Python" <[EMAIL PROTECTED]>
> > > To: "MICHELLE EVANS" <[EMAIL PROTECTED]>
> > > Cc: "Tutor Python" 
> > > Sent: Wednesday, May 03, 2006 12:18 PM
> > > Subject: Re: [Tutor] counting number of inputs
> > (EARLIER VERSION SENT
> > > ACCIDENTLY)
> > >
> > >
> > > > (Tip: Best to use reply-to-all when responding
> > to an email on the list)
> > > > On Tue, 2006-05-02 at 21:34 -0400, MICHELLE
> > EVANS wrote:
> > > > > number1 = int(raw_input("Run number 1 (-1 to
> > end) : "))
> > > > > number2 = int(raw_input("Run number 2 (-1 to
> > end) : "))
> > > > > number3 = int(raw_input("Run number 3 (-1 to
> > end) : "))
> > > > > number4 = int(raw_input("Run number 4 (-1 to
> > end) : "))
> > > > > number5 = int(raw_input("Run number 5 (-1 to
> > end) : "))
> > > > Good.  You collect the string from raw_input and
> > convert it to an
> > > > integer.
> > > >
> > > > This will p

Re: [Tutor] Memory Management etc

2006-05-04 Thread Kent Johnson
Philip Smith wrote:
> Hi
>  
> I use Activestate Python (2.4.3) in a Windows 32 bit environment.
>  
> I have a problem with a large programme that uses a lot of memory 
> (numerous large arrays which are dynamically extended).  I keep getting 
> unpredictable crashes (on a machine with 1/2 GB memory) whereas on my 
> laptop (1 GB memory) it seems OK.

I'm way out of my expertise here but I'll give it a try...

Is your program pure Python or are you using C extensions (other than 
the ones in the standard library)? If it is pure Python I think this 
would be considered a Python bug and would interest the Python developers.

> The portion of the code which crashes varies a bit but curiously is 
> NEVER a part where any memory allocation is happening.

Can you post any code? If you can make a short test program that shows 
the problem that will dramatically increase your chances of getting 
useful help.

> I have to say I have noticed (the programme is basically a 
> batch-factoring programme for integers) that no matter how I tune gc I 
> can't get it to reliably free memory in between factoring each integer.

I don't think Python will ever release memory back to the OS. This has 
changed in Python 2.5, you might be interested in trying the alpha release:
http://docs.python.org/dev/whatsnew/section-other.html
http://www.python.org/download/releases/2.5/

>  
> Because this is a development programme (and for performance reasons) I 
> use global variables some of which refer to the large arrays.
>  
> My questions are:
>  
> 1)Does the mere fact that a function cites a variable as global 
> create a reference which prevents it being collected by gc?

You mean a 'global' statement without actually using the named variable? 
I don't know if this creates a reference, but if so, the reference 
should go out of scope when the function exits.

> 5)Does anyone have ANY suggestions please?

These links might be interesting:
http://tinyurl.com/pszzh
http://evanjones.ca/python-memory.html
http://pysizer.8325.org/

Asking on comp.lang.python will give you access to many more people 
familiar with Python internals than you will find on this list.
>  
> I'm keen to solve this because I would like to make my programme 
> generally available - in every other respect its near complete and 
> massively outperforms the only other comparable pure python module 
> (nzmath) which does the same job.
>  
> Thanks in anticipation.
>  
> Phil
> 
> 
> 
> 
> ___
> 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] sockets

2006-05-04 Thread Matt Richardson
I need to send some data, 2 strings and a list, to a remote computer. 
After thinking about it some last night, it wouldn't be hard to just 
send it all as a string and then parse it on the receiving end.

I'm writing a program for work (and for a class project, so no answers!) 
that will provide some info on the network location of a laptop.  The 
client will gather IP address, MAC address, and a traceroute dump (the 
list mentioned above), then send this off to a super simple server that 
receives the data and puts it in a database.  We've had a few laptops 
'disappear' either through theft or people taking them home to do 'work 
from home' or whatever.  Makes annual inventory a huge pain.

Matt

-- 
Matt Richardson
IT Consultant
College of Arts and Letters
CSU San Bernardino
(909)537-7598

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


Re: [Tutor] sockets

2006-05-04 Thread Kent Johnson
Matt Richardson wrote:
> I need to send some data, 2 strings and a list, to a remote computer. 
> After thinking about it some last night, it wouldn't be hard to just 
> send it all as a string and then parse it on the receiving end.
> 
> I'm writing a program for work (and for a class project, so no answers!) 
> that will provide some info on the network location of a laptop.  The 
> client will gather IP address, MAC address, and a traceroute dump (the 
> list mentioned above), then send this off to a super simple server that 
> receives the data and puts it in a database.  We've had a few laptops 
> 'disappear' either through theft or people taking them home to do 'work 
> from home' or whatever.  Makes annual inventory a huge pain.

This would be very easy to do with XML-RPC. On the server side, writ a 
function that takes three parameters - the IP address, MAC address, and 
traceroute dump - and saves them to a database. Use SimpleXMLRPCServer 
to expose the function. On the client side, gather the data and use 
xmlrpclib to call the remote function. Easy. Since this function will 
presumably be exposed on the public internet you need to worry about 
security; you should use some kind of authorization. A really simple 
solution would be to add username and password arguments to the function 
you expose.

You could do all this with the socket library but XML-RPC takes care of 
all the details of connections, etc as well as marshalling and 
unmarshalling the data.

Kent

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


[Tutor] Python challenge

2006-05-04 Thread David Holland
I looked at this and got stuck on the first one :-  http://www.pythonchallenge.com/pc/def/0.html  It says try changing the url.  I assumed that it either meant 2*38 or 2 to the power of 38 but I can't see how to put either of those in the url.What have I missed ?Thanks in advance.David Holland  
		Yahoo! Photos – NEW, now offering a quality print service from just 7p a photo.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python challenge

2006-05-04 Thread Jason Massey
David,What answer did you get?  One of the things to rember on the challenge is to take your answer and put ".html" (no qutoes) on the end of it in the url.So for the first problem the url would be:
http://www.pythonchallenge.com/pc/def/.htmlOn 5/4/06, David Holland <
[EMAIL PROTECTED]> wrote:
I looked at this and got stuck on the first one :-  http://www.pythonchallenge.com/pc/def/0.html
  It says try changing the url.  I assumed that it either meant 2*38 or 2 to the power of 38 but I can't see how to put either of those in the url.What have I missed ?Thanks in advance.
David Holland  
		Yahoo! Photos
 – NEW, now offering a 
quality print service from just 7p a photo.
___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] Argument check

2006-05-04 Thread Gregor Lingl
Hi all,

I've a class with a lot of methods, for which I'd like to check argument types. 
To show you, what I mean, I've written a prototype I'm not really content with.
It goes like this (a bit revised in order to have short codelines (*mail* ;-)):

class BadArgError(Exception):
 def __init__(self, fname, var, typ, val):
 self.fname = fname
 self.var = var
 self.typ = typ
 self.val = val
 def __str__(self):
 return """
 %s: argument %s must be %s!
 %s of %s given.""" % ( self.fname, self.var,
 self.typ, repr(self.val), type(self.val))

def checkargs(fun,locs,*typelist):
 varnames=fun.im_func.func_code.co_varnames[1:]
 fn = fun.__name__
 for var, typ in zip(varnames, typelist):
 ok = isinstance(locs[var],typ)
 if not ok:
 raise BadArgError(fn, var, typ, locs[var])

## classe for testing:

class Z(object):
 pass

class A(object):
 def f(self,x,y):
 checkargs(self.f, locals(), int, str)
 def g(self,a,b,c):
 checkargs(self.g, locals(), (int,float), Z, tuple)


BadArgError works like this (interactive session):

 >>> bae = BadArgError("myfun", "myvar", str, 3)
 >>> raise bae

Traceback (most recent call last):
   File "", line 1, in -toplevel-
 raise bae
BadArgError:
 myfun: argument myvar must be !
 3 of  given.

You see, I want to the error-message to display the name of the function, which 
got the bad argument, the name of the parameter and it's expected type as well 
as the wrong argument.

Examples for it's use:

 >>> z=Z()
 >>> a=A()
 >>> a.f(1,"a")   #ok
 >>> a.f(1,2)

Traceback (most recent call last):
   File "", line 1, in -toplevel-
   ...
 raise BadArgError(fn, var, typ, locs[var])
BadArgError:
 f: argument y must be !
 2 of  given.
 >>> a.g(.5, z, ())#ok
 >>> a.g(.5, a, ())

Traceback (most recent call last):
   File "", line 1, in -toplevel-
   ...
 raise BadArgError(fn, var, typ, locs[var])
BadArgError:
 g: argument b must be !
 <__main__.A object at 0x00C92F70> of  given.

(One could easily tidy up this output - that's *not* the problem)

I feel it to be cumbersome and ugly, to have to pass the function (method) and 
the locals() dictionary to every call of checkargs.

I'd very much prefer a usage of checkargs similar to:

class A(object):
 def f(self,x,y):
 checkargs(int, str)
 def g(self,a,b,c):
 checkargs((int,float), Z, tuple)

but then chackargs had to get information about its caller (name, locals) from 
elsewhere (the callstack?). I don't know if this is possible at all, not to say 
how to accomplish it.

Do you have some hints?
Where could I find code-examples which solve similar problems?
Would you recommend a different approach?

Regards,

Gregor





-- 
Gregor Lingl
Reisnerstrasse 3/19
A-1030 Wien

Telefon: +43 1 713 33 98
Mobil:   +43 664 140 35 27

Website: python4kids.net

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


Re: [Tutor] Memory Management etc

2006-05-04 Thread Danny Yoo
> I have a problem with a large programme that uses a lot of memory 
> (numerous large arrays which are dynamically extended).  I keep getting 
> unpredictable crashes (on a machine with 1/2 GB memory) whereas on my 
> laptop (1 GB memory) it seems OK.

Hi Philip,

Can you be more specific about what you mean by "crash"?  Does the whole 
operating system freeze, or do you get an error message from Python, or 
...?

The reason I ask is because what you see may not necessarily have to do 
with Python --- there may be lower-level issues such as physical hardware, 
for example.  So more information on symptoms will be very helpful.


> There are no debugger messages it just crashes (and reboots the machine 
> more often than not).

Ok, if I understand what you're saying: are you saying that the machine 
physically reboots without user prompting?  If so, that's almost certainly 
NOT Python then.  System reboot means that even your operating system is 
having difficulty keeping the machine usable.  The most likely explanation 
in this circumstance is that the physical hardware is defective.

I'd recommend running diagnostics like a RAM checker.  But try running 
your program on another machine as another data point to support the 
possibility that perhaps the hardware, and not the software is the issue.


> I have to say I have noticed (the programme is basically a 
> batch-factoring programme for integers) that no matter how I tune gc I 
> can't get it to reliably free memory in between factoring each integer.

How are you measuring this?

Note that Python does not necessarily give allocated memory back to the 
operating system: it keeps a memory pool that it reuses for performance 
reasons.

Is the amount of memory you're using at least bounded?


> Because this is a development programme (and for performance reasons) I 
> use global variables some of which refer to the large arrays.

Global variables don't necessarily make programs fast.  I would strongly 
discourage this kind of ad-hoc performance optimization.  Don't guess: let 
the machine tell you where the program is slow. If you really want to make 
your program fast, use a profiler.

Also note that parameter passing from one function to another is a 
constant-time operation: no object values are being copied.  So the cost 
assumptions you're making about passing large arrays around functions may 
not be correct.



> 1)  Does the mere fact that a function cites a variable as global create 
> a reference which prevents it being collected by gc?

This isn't a contributing factor.  But global variable values at the 
toplevel don't die: that's the point about global variables, because they 
always have at least one reference to them and they're always accessible 
to the outside.


> 3)  Is there any way to allocate a pool of available memory to my 
> programme at the outset from which I can allocate my large arrays?

Practially everything in Python is done at runtime, not compile time.  At 
program startup, I suppose you can preallocate some arrays and keep a pool 
of them for your usage.

But are you finding this to be a significant factor in your program, 
though?  Again, before you go ahead with optimization, I'd strongly 
recommend using a profiler to do a principled analysis of the hotspots of 
your program.  Have you looked at the Python Profiler yet?



> I'm keen to solve this because I would like to make my programme 
> generally available - in every other respect its near complete and 
> massively outperforms the only other comparable pure python module 
> (nzmath) which does the same job.

If you would like a code review, and if the program is short enough, I'm 
sure people here would be happy to give some pointers.

Good luck to you!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Argument check

2006-05-04 Thread Kent Johnson
Gregor Lingl wrote:
> Hi all,
> 
> I've a class with a lot of methods, for which I'd like to check argument 
> types. 

You're not the first person to want to do this. You might be interested 
in these:
http://www.artima.com/weblogs/viewpost.jsp?thread=155123
http://www.python.org/dev/peps/pep-0246/

including the references in both cases.

> but then chackargs had to get information about its caller (name, locals) 
> from 
> elsewhere (the callstack?). I don't know if this is possible at all, not to 
> say 
> how to accomplish it.

See this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062

Kent

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


Re: [Tutor] Argument check

2006-05-04 Thread Ewald Ertl
Hi Gregor,

Maybe the module traceback could help you here.

It has same functions to extract the actual stack of the program.

HTH Ewald

Gregor Lingl wrote:
> Hi all,
> 
> I've a class with a lot of methods, for which I'd like to check argument 
> types. 
> To show you, what I mean, I've written a prototype I'm not really content 
> with.
> It goes like this (a bit revised in order to have short codelines (*mail* 
> ;-)):
> 
> class BadArgError(Exception):
>  def __init__(self, fname, var, typ, val):
>  self.fname = fname
>  self.var = var
>  self.typ = typ
>  self.val = val
>  def __str__(self):
>  return """
>  %s: argument %s must be %s!
>  %s of %s given.""" % ( self.fname, self.var,
>  self.typ, repr(self.val), type(self.val))
> 
> def checkargs(fun,locs,*typelist):
>  varnames=fun.im_func.func_code.co_varnames[1:]
>  fn = fun.__name__
>  for var, typ in zip(varnames, typelist):
>  ok = isinstance(locs[var],typ)
>  if not ok:
>  raise BadArgError(fn, var, typ, locs[var])
> 
> ## classe for testing:
> 
> class Z(object):
>  pass
> 
> class A(object):
>  def f(self,x,y):
>  checkargs(self.f, locals(), int, str)
>  def g(self,a,b,c):
>  checkargs(self.g, locals(), (int,float), Z, tuple)
> 
> 
> BadArgError works like this (interactive session):
> 
>  >>> bae = BadArgError("myfun", "myvar", str, 3)
>  >>> raise bae
> 
> Traceback (most recent call last):
>File "", line 1, in -toplevel-
>  raise bae
> BadArgError:
>  myfun: argument myvar must be !
>  3 of  given.
> 
> You see, I want to the error-message to display the name of the function, 
> which 
> got the bad argument, the name of the parameter and it's expected type as 
> well 
> as the wrong argument.
> 
> Examples for it's use:
> 
>  >>> z=Z()
>  >>> a=A()
>  >>> a.f(1,"a")   #ok
>  >>> a.f(1,2)
> 
> Traceback (most recent call last):
>File "", line 1, in -toplevel-
>...
>  raise BadArgError(fn, var, typ, locs[var])
> BadArgError:
>  f: argument y must be !
>  2 of  given.
>  >>> a.g(.5, z, ())#ok
>  >>> a.g(.5, a, ())
> 
> Traceback (most recent call last):
>File "", line 1, in -toplevel-
>...
>  raise BadArgError(fn, var, typ, locs[var])
> BadArgError:
>  g: argument b must be !
>  <__main__.A object at 0x00C92F70> of  given.
> 
> (One could easily tidy up this output - that's *not* the problem)
> 
> I feel it to be cumbersome and ugly, to have to pass the function (method) 
> and 
> the locals() dictionary to every call of checkargs.
> 
> I'd very much prefer a usage of checkargs similar to:
> 
> class A(object):
>  def f(self,x,y):
>  checkargs(int, str)
>  def g(self,a,b,c):
>  checkargs((int,float), Z, tuple)
> 
> but then chackargs had to get information about its caller (name, locals) 
> from 
> elsewhere (the callstack?). I don't know if this is possible at all, not to 
> say 
> how to accomplish it.
> 
> Do you have some hints?
> Where could I find code-examples which solve similar problems?
> Would you recommend a different approach?
> 
> Regards,
> 
> Gregor
> 
> 
> 
> 
> 


-- 
Ing. Ewald Ertl HartterGruppe   Phone : 
+43-3352-33085-558
trinomic Projektmanagement & Informationstechnik GmbH   Fax   : 
+43-3352-33085-600
Wiener Straße 41mailto:[EMAIL PROTECTED]
A-7400 Oberwart http://www.trinomic.com mailto:[EMAIL PROTECTED]

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


Re: [Tutor] sockets

2006-05-04 Thread Matt Richardson
Kent Johnson wrote:

> 
> This would be very easy to do with XML-RPC. On the server side, writ a 
> function that takes three parameters - the IP address, MAC address, and 
> traceroute dump - and saves them to a database. Use SimpleXMLRPCServer 
> to expose the function. On the client side, gather the data and use 
> xmlrpclib to call the remote function. Easy. Since this function will 
> presumably be exposed on the public internet you need to worry about 
> security; you should use some kind of authorization. A really simple 
> solution would be to add username and password arguments to the function 
> you expose.


I thought that might be overkill after quickly glancing at it in 
'Foundations of Python Network Programming', but I think you might have 
just convinced me that it is actually the easier route.  My original 
thought was that it could be just a simple string, sent via UDP, that 
would happen after networking was established but before log in.  I had 
done something simpler before using a bash script and sendmail, but I 
really don't want my inbox plugged up with a bunch of 'phone home' 
messages :)

Matt


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


Re: [Tutor] Memory Management etc

2006-05-04 Thread Alan Gauld
> I use Activestate Python (2.4.3) in a Windows 32 bit environment.
>
> I have a problem with a large programme that uses a lot of memory
> (numerous large arrays which are dynamically extended).
> I keep getting unpredictable crashes (on a machine with 1/2 GB 
> memory)
> whereas on my laptop (1 GB memory) it seems OK.

How are you running it?
Double clicjk in explorer?
>From within the IDE?
or using

C:\> python foo.py

If the latter try using the -i option (and if you use a lot of 
docstrings -OO)

Otherwise without seeing the code its hard to say, but it doesn't
sound like memory probnlems to me - that should just make it
run slowly in virtual memory.

HTH,

Alan G.


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


[Tutor] HELP: using popen2/popen3

2006-05-04 Thread Jerome Jabson
Hello,

I'm trying to start a shell script from my python
script using os.spawnlp. But I'm not getting msgs from
stderr. I thought that I could use popen2/popen3 to
replace os.spawnlp but I'm having problems with syntax
and how to do this. Can someone point me in the right
direction? Here's my original script (w/o using
popen2/popen3):


import os
import sys


 Definition 


def set_up():
   print "=== Starting Setup ==="
   os.chdir('/home/qauser/jerome')
   os.spawnlp(os.P_WAIT,
'/home/qauser/jerome/qaSetup.sh')
   print "=== Setup  Complete ==="


##  Main  ##


set_up()


I was trying to replace the os.spawnlp with:

sout, sin, serr =
os.open('/home/qauser/jerome/qaSetup.sh')
sout.readlines()
serr.readlines()
sout.close()
serr.close()
sin.close()

Many thanks,
Jerome

__
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] sockets

2006-05-04 Thread Hugo González Monteverde
Matt Richardson wrote:
> I need to send some data, 2 strings and a list, to a remote computer. 
> After thinking about it some last night, it wouldn't be hard to just 
> send it all as a string and then parse it on the receiving end.

Well if the excercise itself is not parsing a string, you can just use 
pickle to serialize your objects. Look up the documentation for the 
pickle module. You can transform many objects into byte streams and then 
send them over the network.

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


Re: [Tutor] Memory Management etc

2006-05-04 Thread Philip Smith
Hi

Thanks for response.  Tried this both in IDE and DOS window including under 
pdb.  No docstrings used at present.  -i option won't help either as the 
machine generally reboots - python reports no problems at all.

The reason I think it is memory is that the amount of memory available to 
the programme is the only variable - I'm using two identical 
hardware/software environments apart from this.  Neither machine ever 
crashes on anything else.  The only extraordinary thing about the programme 
(apart from performance) is the huge amount of dynamic data it uses.

Thanks anyway

Phil

- Original Message - 
From: "Alan Gauld" <[EMAIL PROTECTED]>
To: "Philip Smith" <[EMAIL PROTECTED]>; 
Sent: Thursday, May 04, 2006 11:48 PM
Subject: Re: [Tutor] Memory Management etc


>> I use Activestate Python (2.4.3) in a Windows 32 bit environment.
>>
>> I have a problem with a large programme that uses a lot of memory
>> (numerous large arrays which are dynamically extended).
>> I keep getting unpredictable crashes (on a machine with 1/2 GB memory)
>> whereas on my laptop (1 GB memory) it seems OK.
>
> How are you running it?
> Double clicjk in explorer?
> From within the IDE?
> or using
>
> C:\> python foo.py
>
> If the latter try using the -i option (and if you use a lot of 
> docstrings -OO)
>
> Otherwise without seeing the code its hard to say, but it doesn't
> sound like memory probnlems to me - that should just make it
> run slowly in virtual memory.
>
> HTH,
>
> Alan G.
>
>
> 


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


Re: [Tutor] sockets

2006-05-04 Thread János Juhász
Hi Matt,

the traceroute can be done from the client side or eigther from the server 
side.
The two ones should give the same result with reverse order.
In this case you can do the tracerouting from the server side when the 
client ask it.
I am just thinking about a simple finger deamon, that can do the job for 
you.
You can ask the deamon, with a simple finger request.
Finger.exe is part of th MS system so

c:\>finger [EMAIL PROTECTED]

seems to be enough in the login process.


This is the simplest fingerdeamon:

import SocketServer, os, string

class FingerHandler(SocketServer.StreamRequestHandler):
def handle(self):
username = self.rfile.readline(512)
username = string.strip(username)
# Just do your job here
# Do the traceroute, and save the result
 
if __name__ == '__main__':
server = SocketServer.TCPServer( ('', 79), FingerHandler)
server.serve_forever()



Matt wrote ---
Date: Thu, 04 May 2006 09:23:38 -0700
From: Matt Richardson <[EMAIL PROTECTED]>
Subject: Re: [Tutor] sockets
To: Tutor@python.org
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

I need to send some data, 2 strings and a list, to a remote computer.
After thinking about it some last night, it wouldn't be hard to just
send it all as a string and then parse it on the receiving end.

I'm writing a program for work (and for a class project, so no answers!)
that will provide some info on the network location of a laptop.  The
client will gather IP address, MAC address, and a traceroute dump (the
list mentioned above), then send this off to a super simple server that
receives the data and puts it in a database.  We've had a few laptops
'disappear' either through theft or people taking them home to do 'work
from home' or whatever.  Makes annual inventory a huge pain.

Matt




Yours sincerely, 
__
Janos Juhasz 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Memory Management etc

2006-05-04 Thread Philip Smith
Hi

Thanks for response.

I'm pretty sure for one reason and another that hardware is not at fault.

I use the windows monitor to track memory usage.  I must say I hadn't 
realised that Python doesn't release memory back to the OS - someone else 
advised me to try the alpha 2.5 version which cures this problem - I have 
but regretably the same problem keeps occurring.

The programme is about 600 lines and I'd be loth to let anyone have it in 
its current form until I understand this problem.

Interestingly I have programmed this in 3 different ways - as a class, as a 
modular programme and (currently) as a single file application (using global 
variables instead of parameter passing purely because in some cases I was 
passing and returning up to 20 parameters which seemed clumsy).

I have profiled the programme to death and there is no doubt that using 
globals is faster (this is a time-critical application).  However I may 
translate the current version back into modular or class form and see if the 
problem vanishes.

Regards

Phil

- Original Message - 
From: "Danny Yoo" <[EMAIL PROTECTED]>
To: "Philip Smith" <[EMAIL PROTECTED]>
Cc: 
Sent: Thursday, May 04, 2006 6:31 PM
Subject: Re: [Tutor] Memory Management etc


>> I have a problem with a large programme that uses a lot of memory 
>> (numerous large arrays which are dynamically extended).  I keep getting 
>> unpredictable crashes (on a machine with 1/2 GB memory) whereas on my 
>> laptop (1 GB memory) it seems OK.
>
> Hi Philip,
>
> Can you be more specific about what you mean by "crash"?  Does the whole 
> operating system freeze, or do you get an error message from Python, or 
> ...?
>
> The reason I ask is because what you see may not necessarily have to do 
> with Python --- there may be lower-level issues such as physical hardware, 
> for example.  So more information on symptoms will be very helpful.
>
>
>> There are no debugger messages it just crashes (and reboots the machine 
>> more often than not).
>
> Ok, if I understand what you're saying: are you saying that the machine 
> physically reboots without user prompting?  If so, that's almost certainly 
> NOT Python then.  System reboot means that even your operating system is 
> having difficulty keeping the machine usable.  The most likely explanation 
> in this circumstance is that the physical hardware is defective.
>
> I'd recommend running diagnostics like a RAM checker.  But try running 
> your program on another machine as another data point to support the 
> possibility that perhaps the hardware, and not the software is the issue.
>
>
>> I have to say I have noticed (the programme is basically a 
>> batch-factoring programme for integers) that no matter how I tune gc I 
>> can't get it to reliably free memory in between factoring each integer.
>
> How are you measuring this?
>
> Note that Python does not necessarily give allocated memory back to the 
> operating system: it keeps a memory pool that it reuses for performance 
> reasons.
>
> Is the amount of memory you're using at least bounded?
>
>
>> Because this is a development programme (and for performance reasons) I 
>> use global variables some of which refer to the large arrays.
>
> Global variables don't necessarily make programs fast.  I would strongly 
> discourage this kind of ad-hoc performance optimization.  Don't guess: let 
> the machine tell you where the program is slow. If you really want to make 
> your program fast, use a profiler.
>
> Also note that parameter passing from one function to another is a 
> constant-time operation: no object values are being copied.  So the cost 
> assumptions you're making about passing large arrays around functions may 
> not be correct.
>
>
>
>> 1)  Does the mere fact that a function cites a variable as global create 
>> a reference which prevents it being collected by gc?
>
> This isn't a contributing factor.  But global variable values at the 
> toplevel don't die: that's the point about global variables, because they 
> always have at least one reference to them and they're always accessible 
> to the outside.
>
>
>> 3)  Is there any way to allocate a pool of available memory to my 
>> programme at the outset from which I can allocate my large arrays?
>
> Practially everything in Python is done at runtime, not compile time.  At 
> program startup, I suppose you can preallocate some arrays and keep a pool 
> of them for your usage.
>
> But are you finding this to be a significant factor in your program, 
> though?  Again, before you go ahead with optimization, I'd strongly 
> recommend using a profiler to do a principled analysis of the hotspots of 
> your program.  Have you looked at the Python Profiler yet?
>
>
>
>> I'm keen to solve this because I would like to make my programme 
>> generally available - in every other respect its near complete and 
>> massively outperforms the only other comparable pure python module 
>> (nzmath) which does the same job.
>
> If you wou

Re: [Tutor] Tutor Digest, Vol 27, Issue 12

2006-05-04 Thread Philip Smith
Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> --
>
> Message: 2
> Date: Thu, 04 May 2006 08:33:46 -0400
> From: Kent Johnson <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] Memory Management etc
> Cc: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Philip Smith wrote:
>> Hi
>>
>> I use Activestate Python (2.4.3) in a Windows 32 bit environment.
>>
>> I have a problem with a large programme that uses a lot of memory
>> (numerous large arrays which are dynamically extended).  I keep getting
>> unpredictable crashes (on a machine with 1/2 GB memory) whereas on my
>> laptop (1 GB memory) it seems OK.
>
> I'm way out of my expertise here but I'll give it a try...
>
> Is your program pure Python or are you using C extensions (other than
> the ones in the standard library)? If it is pure Python I think this
> would be considered a Python bug and would interest the Python developers.
>
>> The portion of the code which crashes varies a bit but curiously is
>> NEVER a part where any memory allocation is happening.
>
> Can you post any code? If you can make a short test program that shows
> the problem that will dramatically increase your chances of getting
> useful help.
>
>> I have to say I have noticed (the programme is basically a
>> batch-factoring programme for integers) that no matter how I tune gc I
>> can't get it to reliably free memory in between factoring each integer.
>
> I don't think Python will ever release memory back to the OS. This has
> changed in Python 2.5, you might be interested in trying the alpha 
> release:
> http://docs.python.org/dev/whatsnew/section-other.html
> http://www.python.org/download/releases/2.5/
>
>>
>> Because this is a development programme (and for performance reasons) I
>> use global variables some of which refer to the large arrays.
>>
>> My questions are:
>>
>> 1)Does the mere fact that a function cites a variable as global
>> create a reference which prevents it being collected by gc?
>
> You mean a 'global' statement without actually using the named variable?
> I don't know if this creates a reference, but if so, the reference
> should go out of scope when the function exits.
>
>> 5)Does anyone have ANY suggestions please?
>
> These links might be interesting:
> http://tinyurl.com/pszzh
> http://evanjones.ca/python-memory.html
> http://pysizer.8325.org/
>
> Asking on comp.lang.python will give you access to many more people
> familiar with Python internals than you will find on this list.
>>
>> I'm keen to solve this because I would like to make my programme
>> generally available - in every other respect its near complete and
>> massively outperforms the only other comparable pure python module
>> (nzmath) which does the same job.
>>
>> Thanks in anticipation.
>>
>> Phil
>>
>>
>> 
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>
>
>
>
> --
>
> Message: 3
> Date: Thu, 04 May 2006 09:23:38 -0700
> From: Matt Richardson <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] sockets
> To: Tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> I need to send some data, 2 strings and a list, to a remote computer.
> After thinking about it some last night, it wouldn't be hard to just
> send it all as a string and then parse it on the receiving end.
>
> I'm writing a program for work (and for a class project, so no answers!)
> that will provide some info on the network location of a laptop.  The
> client will gather IP address, MAC address, and a traceroute dump (the
> list mentioned above), then send this off to a super simple server that
> receives the data and puts it in a database.  We've had a few laptops
> 'disappear' either through theft or people taking them home to do 'work
> from home' or whatever.  Makes annual inventory a huge pain.
>
> Matt
>
> -- 
> Matt Richardson
> IT Consultant
> College of Arts and Letters
> CSU San Bernardino
> (909)537-7598
>
>
>
> --
>
> Message: 4
> Date: Thu,