Re: Overuse of try/except/else?

2011-05-10 Thread Paul Probert

On 05/09/2011 07:40 PM, Kyle T. Jones wrote:


It has been hard for me to determine what would constitute overuse.

Cheers.
Well, for me the power of exceptions is that it lets me write much more 
concise code. For example, suppose I call a routine I wrote over and 
over, and I have to check for errors on each call. Then you have a long 
block of code like:

if err == 0:
  x1,err=somefunction(1)
if err == o:
  x2,err=somefunction(2)
...
...
but if somefunction just raises an exception on error, then you do
try:
  x1=somefunction(1)
  x2=somefunction(2)
  ...
  ...
except:
  blah blah

So for my uses, its handy to let things raise exceptions willy nilly in 
the lower level functions, and do the catching in the higher level function.


Paul Probert
--
http://mail.python.org/mailman/listinfo/python-list


Re: calculating on matrix indices

2006-02-17 Thread Paul Probert
Brian Blais wrote:
> Hello,
> 
> In my attempt to learn python, migrating from matlab, I have the following 
> problem. 
> Here is what I want to do, (with the wrong syntax):
> 
> from numpy import *
> 
> t=arange(0,20,.1)
> x=zeros(len(t),'f')
> 
> idx=(t>5)
> tau=5
> x[idx]=exp(-t[idx]/tau)  # <---this line is wrong (gives a TypeError)
> 
> #--
> 
> what is the best way to replace the wrong line with something that works: 
> replace all 
> of the values of x at the indices idx with exp(-t/tau) for values of t at 
> indices idx?
> 
> I do this all the time in matlab scripts, but I don't know that the pythonic 
> preferred method is.
> 
Brian,
First, let me warn you I only know the old Numeric, and I haven't made 
the jump to Numpy yet. I'm told the syntax is about the same though.
Using nonzero, put, and take, I can do what you want like this:

import Numeric as num
t=num.arange(0,20,.1)
x=num.zeros(len(t),'f')
idx=num.nonzero(t>5)
tau=5.
num.put(x,idx,num.exp(-num.take(t,idx)/tau))

Kinda messy compared to Matlab. Do you know about Octave 
(www.octave.org)? It tries to be compatible with Matlab syntax. Of 
course it lacks the other niceties of python.

Paul Probert


-- 
http://mail.python.org/mailman/listinfo/python-list


time.sleep(1) sometimes runs for 200 seconds under windows

2006-02-23 Thread Paul Probert
Hi,
   My app runs in a loop looking for changes in a database, and like a 
good boy I call time.sleep(1) inside the loop. Unfortunately this 
sometimes runs for 200 seconds or so, presumably while my OS is calling 
Bill Gates to tell him what I've been doing. This happens under NT4, 
W2k, and XP. What do people do to fix this? Thanks!

Paul Probert
University of Wisconsin

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: time.sleep(1) sometimes runs for 200 seconds under windows

2006-02-23 Thread Paul Probert
Claudio Grondi wrote:

>In similar situation I would start to blame the hardware for the 
>problem, so below a kind of checklist to go through:
>
>   1. have you changed any hardware?
>   2. have you installed new drivers?
>   3. have you connected via USB/Firewire/IDE or other interfaces/ports 
>etc. some new devices?
>   4. have you installed new BIOS?
>   5. is your RAM ok?
>   6. are you sure there is no CD/DVD in your CD/DVD drive?
>   7. are you sure there is no floppy disk in your floppy drive?
>   8. are you sure your CPU/motherboard/RAM is not overheating?
>
>Claudio
>  
>
Claudio,
  Thanks for the reply. I should have mentioned, this happens to just 
about every machine in our collection of about 20 machines. Each is of a 
different age, and the hardware is completely diverse. Each has either 
of NT4, win2k, or XP installed. They all belong to our domain

Paul Probert
University of Wisconsin

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: time.sleep(1) sometimes runs for 200 seconds under windows

2006-02-23 Thread Paul Probert
Benji York wrote:

>Paul Probert wrote:
> > Hi, My app runs in a loop looking for changes in a database, and like
> > a good boy I call time.sleep(1) inside the loop. Unfortunately this
> > sometimes runs for 200 seconds or so, presumably while my OS is
> > calling Bill Gates to tell him what I've been doing. This happens
> > under NT4, W2k, and XP. What do people do to fix this? Thanks!
>
> From the docs for time.sleep:
>
> The actual suspension time may be less than that requested because
> any caught signal will terminate the sleep() following execution of
> that signal's catching routine. Also, the suspension time may be
> longer than requested by an arbitrary amount because of the
> scheduling of other activity in the system.
>--
>Benji York
>  
>
Benji,
   Thanks, but yes, I had read that before posting. One would think, 
though, that the extra 200 seconds is a bit extreme to be blamable on 
system scheduling granularity.
   I'm starting to think we've got a worm of some kind. If so, its 
invisible to our Norton AV corporate.

Paul Probert
University of Wisconsin

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: time.sleep(1) sometimes runs for 200 seconds under windows

2006-02-23 Thread Paul Probert
Peter Hansen wrote:

>Are you saying that you believe the time.sleep(1) call is actually 
>blocking for 200 seconds?  Or just that your loop (and we can only guess 
>what it looks like) is the one taking that long?
>
>If the former, try something like putting "print 'before'" and "print 
>'after'" before and after the sleep, and observe what happens when you 
>run the program.  I'm fairly confident in saying there's no chance 
>you'll see the "before" sit for 200s before you see the "after" and that 
>your problem lies elsewhere, not with time.sleep(1).
>
>If the latter, um, obviously we can't help without more info.
>
>-Peter
>
>  
>
Yes, I'm doing this:
   .
  oldtime=time.time()
  time.sleep(1)
  newtime=time.time()
  dt=newtime-oldtime
   if dt > 2:
   print 'dt=',dt,' time=',time.strftime('%Y_%m_%d_%Hh_%Mm_%Ss')
Its happening roughly 4 times a day total on our 20 machines, ie about 
once every 5 days on a given machine.

Paul Probert
University of Wisconsin

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: time.sleep(1) sometimes runs for 200 seconds under windows

2006-02-24 Thread Paul Probert
Grant Edwards wrote:

>Time to dowload a linux CD then, eh?
>
>  
>
We are looking at that very seriously. The big hurdle is that we run a 
lot of laboratory hardware that has no support under linux. A world 
where there is no more Kazaa or sasser seems like it would be wonderful, 
though.

Paul Probert
University of Wisconsin

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: time.sleep(1) sometimes runs for 200 seconds under windows

2006-02-24 Thread Paul Probert
Dennis Lee Bieber wrote:

>On Thu, 23 Feb 2006 15:56:09 -0600, Paul Probert <[EMAIL PROTECTED]>
>declaimed the following in comp.lang.python:
>
>
>  
>
>>  Thanks for the reply. I should have mentioned, this happens to just 
>>about every machine in our collection of about 20 machines. Each is of a 
>>different age, and the hardware is completely diverse. Each has either 
>>of NT4, win2k, or XP installed. They all belong to our domain
>>
>>
>>
>   Any chance they all tend to have slow clocks, and are getting bitten
>by a semi-random NTP time update; my machine tends to run the NTP update
>at 7-day intervals (including time of day), counting from the last
>successful synchronization.
>  
>
This is now our leading suspect. All the affected machines have 
"abouttime.exe" running as a service, something our sysadmin put in a 
few years ago. We are currently disabling this and now waiting for the 
proof.
Thanks!

Paul Probert
University of Wisconsin

-- 
http://mail.python.org/mailman/listinfo/python-list


Problem solved: Re: time.sleep(1) sometimes runs for 200 seconds under windows

2006-02-24 Thread Paul Probert
Thanks everyone for your help.
   It was the "Abouttime.exe" program, a time synch utility. To get the 
problem, you have to run it as a service, and possibly it has to have 
trouble connecting to its time servers. It would cause time.sleep(1) to 
sometimes block for 200 seconds. Not "about" 200 seconds but always 
within 199 to 202 seconds. Very strange, but it is good to have the 
problem gone.

Enjoy the weekend!

Paul Probert
University of Wisconsin

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: wxPython gurus, please help

2009-05-24 Thread Paul Probert
Jive Dadson wrote:
>I have an application that opens an image file of the user's choice.
>   I have an exception handler for the case that the user selected a bad
> or unsupported image file.  My code is catching the exception, but
> unfortunately for me, after I exit the except-clause, wxPython is
> popping up its own message dialog from somewhere in deepest, darkest
> __core__.
> 
>How do I make it not do that?
> 
>Thankee much,
>Jive
In your line creating your app object do it like this to avoid
redirecting error output to a popup window:

app=wx.App(False)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: wxPython problem

2008-09-08 Thread Paul Probert

Peter Anderson wrote:
Stef Mientki said: In PyScripter, you should run wxPython in the plain 
remote machine (not the wxPython remote),and you should set "reset 
before run flag" or reset the remote machine each time yourself.


Stef,

Thanks for the help! It has taken several hours to find and install the 
correct version of Rpyc (which is required to run the remote Python 
engine but it now seems to be working fine. And the "Reinitialise the 
Python engine {Alt]+[F2]" does need to be done between script runs. 
Given all that, the script now runs multiple times in PyScripter. The 
script still only runs ONCE in IDLE but I can live with that.


In case others find this message from a search; I am using Python 2.5.2 
and it requires rpyc-2.60.zip from the Rpyc download page (see 
http://rpyc.wikispaces.com/ click on the "Download" link and make sure 
you select the "main" link under the "Packages" column at Sourceforge. 
You will be shown a list of Rpyc versions. For Python 2.5.2 choose 
Relese "2.60". This will save you the several hours its cost me.


Thanks again Stef.

Regards,
Peter
wxPython never runs well in a program running under Tk, such as IDLE. 
Something about competing GUI's. If you want a replacement, you can try 
pycrust or one of its brethren, which ship with wxpython.


Paul Probert
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python arrays and sting formatting options

2008-10-01 Thread Paul Probert
Grant Edwards wrote:
> On 2008-09-30, Peter Pearson <[EMAIL PROTECTED]> wrote:
>> On Tue, 30 Sep 2008 00:04:18 +0200, Ivan Rebori wrote:
>>> 1. Multi dimensional arrays - how do you load them in python
>>> For example, if I had:
>>> ---
>>> 1 2 3
>>> 4 5 6
>>> 7 8 9
>>>
>>> 10 11 12
>>> 13 14 15
>>> 16 17 18
>>> ---
>>> with "i" being the row number, "j" the column number, and "k" the ..
>>> uhmm, well, the "group" number, how would you load this ?
>>>
>>> If fortran90 you would just do:
>>>
>>> do 10 k=1,2
>>> do 20 i=1,3
>>>
>>> read(*,*)(a(i,j,k),j=1,3)
>>>
>>> 20 continue
>>> 10 continue
>>>
>>> How would the python equivalent go ?
> 
> You would drag yourself out of the 1960s, install numpy, and
> then do something like this:
> 
>a = read_array(open("filename.dat","r"))
> 
>> Since you're coming from the FORTRAN world (thank you for that
>> stroll down Memory Lane), you might be doing scientific
>> computations, and so might be interested in the SciPy package
>> (Google scipy), which gives you arrays and matrices. Don't
>> expect to be able to use it without learning some Python,
>> though.
> 
> If not full-up scipy (which provides all sorts of scientific
> and numerical-analysis stuff), then at least numpy (which
> provides the basic array/matrix operations:
> 
>   http://numpy.scipy.org/
> 
> Though the software is free, the documentation isn't.  You've
> got to buy the book if you want something to read.  IMO, it's
> definitely worth it, and a good way to support the project even
> if you don't really need something to keep your bookends apart.
clip ...
The book is free now, as of Aug 21, 08.
http://www.tramy.us/guidetoscipy.html

Paul Probert
--
http://mail.python.org/mailman/listinfo/python-list