Re: [Tutor] Bad time to get into Python?

2008-02-03 Thread Thomas Pani
Dotan Cohen wrote:
> However, with the coming of Python3 and the new syntax, is this a bad time to 
> start learning Python?
Not at all, I'd say. Changes will be fairly small, with the main changes
being:
 - print is replaced by a print() function
 - / will become the float division operator
 - changes towards iterators (e.g. range() doesn't return a list)
 - string filetype changes
There are lots of other changes, but most of them include removing
already deprecated idioms. [1] has a list of Python 3k changes.
There's also the 2to3 conversion tool which allows you to run lots of
conversion automated.

> That asked, I've heard that 2.6 can be configured to warn when using
> code that will not run in 3.x. Is this correct?
Yes, 2.6 will support a "Py3k warnings mode". It should also have many
of 3k's features already implemented, allowing to run both side-by-side
or via __future__. PEP 3000 ([2]) has more info on this.

> How is this done? I'd like to do it on a per-file basis, so that I will
> only need to run one version of python on this machine.
Don't know. But if your only using it for some home-coding, you would
just once do the conversion and then update to 3k.

> I want my own apps to throw errors,
> but not other python apps on this system. Is there some error-level
> code that I can run?
Not sure what you mean by that. Are you refering to exception-handling?

I'd say it's not a bad time to learn Python. There will be some major
changes in 3k, but as long as you don't have to maintain 2.6 and 3.0 in
parallel, conversion should be easy enough.

Cheers,
thomas pani

[1] http://docs.python.org/dev/3.0/whatsnew/3.0.html
[2] http://www.python.org/dev/peps/pep-3000/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] opening a pipe?

2008-02-12 Thread Thomas Pani
The subprocess module is what you're looking for.
Your example would look like

%<
import subprocess
p = subprocess.Popen('cat hi.txt', shell=True, stdout=subprocess.PIPE)
for line in p.stdout:
 print line
%<

I assume you know that you can just open a file for reading instead of 
piping cat.

thomas

James Hartley wrote:
> A Perl script can easily serve as a filter within a pipe as seen in
> the following:
> 
> use strict;
> use warnings;
> 
> open(IN, 'cat hello.txt |') or die 'unable to open file';
> while () {
> print;
> }
> close(IN);
> 
> Can I do the same within Python?  Thanks.
> 
> Jim
> ___
> 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] Question on multithreading

2008-02-24 Thread Thomas Pani
Hi,

Here are some thoughts:

 From the Python Library Reference: "If the subclass overrides the 
constructor, it must make sure to invoke the base class constructor 
(Thread.__init__()) before doing anything else to the thread." You'll 
have to do that for your GuiScript class.

You can't just stop a thread, you can only wait for it to finish using 
join(). Or you can set its deamon flag, so it will exit when no 
non-daemon threads are left.

Is there any reason for starting the GUI code in a separate thread? You 
could just leave it in the main thread.

For some general notes on this topic this might be helpful:
http://wiki.wxpython.org/LongRunningTasks

Cheers,
Thomas Pani

Varsha Purohit wrote:
> Hello,
> i have a gui program in wxpython where i am spawning two threads. 
> one for the mainloop of gui and other for some background tasks. I have 
> to stop the background running thread once its work is done. I am trying 
> to use the join method but it is giving me an error saying threads 
> cannot be joined as the thread has just been created... whereas at that 
> point the thread is done with its job. So i am confused whether i am 
> putting it correctly or not. also, is there any method called stop() to 
> stop the threadsAlso, i want to see if the thread is alive or not.. 
> but i donno where should i put the isAlive() method associated with the 
> thread.. i tried putting it in the code where thread does execution but 
> apparently it is not the correct place.
> 
> Here is the code...
> 
> class GuiScript(threading.Thread):
> def __init__(self):
> self.run()
> def run(self):
> app = wx.PySimpleApp()
> MainWindow().Show()
> app.MainLoop()
>
> class RunScript(threading.Thread):
> def run(self):
> imFile=test()
>
> and when the stop button is pressed the two threads should stop 
> executing... so i have written code like this...
> 
> def OnCloseWindow(self,event):
> GuiScript().Stop()
> RunScript().Stop()
> self.Destroy()
> 
> but seems stop() method is not existing... can anybody guide me pl
> 
> thanks,
>   
> 
> -- 
> Varsha Purohit,
> Graduate Student
> 
> 
> 
> 
> ___
> 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] Open a directory in the default file manager

2008-05-16 Thread Thomas Pani

Tim Michelsen wrote:
> Hello,
> is there any function/module that allows me to open a directory in the
> default file manager of a operating system?

On Windows you can use os.startfile().
On "pure" Unices there's no such thing as filetype associations
However, if you use a desktop environment, you can spawn xdg-open (from 
xdg-utils) from Python. This will autodetect gnome, kde and xfce and use 
their tools (gnome-open, kfmclient, exo-open).

I think on OS X/Darwin there's a similar utility called `open'.

You can use sys.platform to determine which system you're running on.

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


Re: [Tutor] listing classes

2008-05-20 Thread Thomas Pani

Hi,

dir(A) will essentially give you what you want (and a little more)

If you're only interested in classes, you can do something like:

import types
[ name for name in dir(A) if type(eval('A.'+name)) == types.ClassType ]

Thomas

Laureano Arcanio wrote:

Hi All,

I need to have a listing of all classes defined inside a class body,
something like this:

class A(object):
class B(object):
pass
class C(object):
pass(object):

and i need to get the classes to instantiate them.. something like this.

classes =[A,B]

Any ideas ? do i need meta classes or something ?

Thanks in advice.





___
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] Decimals 'not equal to themselves' (e.g. 0.2 equals 0.200000001)

2008-08-03 Thread Thomas Pani

CNiall wrote:
I want to make a simple script that calculates the n-th root of a given 
number (e.g. 4th root of 625--obviously five, but it's just an example 
:P), and because there is no nth-root function in Python I will do this 
with something like x**(1/n).


Side note: of course there are python built-in ways to do that. You just 
named one yourself:


In [6]: 625**(1.0/4)
Out[6]: 5.0

also:

In [9]: pow(625, 1.0/4)
Out[9]: 5.0

However, with some, but not all, decimals, they do not seem to 'equal 
themselves'.


As you can see, the last two decimals are very slightly inaccurate. 
However, it appears that when n in 1/n is a power of two, the decimal 
does not get 'thrown off'. How might I make Python recognise 0.2 as 0.2 
and not 0.20001?



You just can't store 0.1 as a binary floating point.
You might want to read:
http://www.network-theory.co.uk/docs/pytut/FloatingPointArithmeticIssuesandLimitations.html
http://www.network-theory.co.uk/docs/pytut/RepresentationError.html

The decimal module provides decimal floating point arithmetic:
http://docs.python.org/lib/module-decimal.html

like in:
In [1]: 0.2 * 2
Out[1]: 0.40002

In [2]: from decimal import Decimal

In [3]: Decimal('0.2') * 2
Out[3]: Decimal("0.4")

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