Re: [Tutor] IDLE/phythonWin -- Who's On First? (Abbott and Costello)

2009-02-12 Thread ALAN GAULD
There used to be a lot of problems running Tkinter 
programs inside IDLE but most of these have been 
solved since about v2.3. However it does still 
through up issues, so the simple solution it to 
always test GUI programs outside of the IDE.


Another reason why for serious programming 
I tend to favour the 3 window setup...

Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/






From: Wayne Watson 
To: Alan Gauld ; "tutor@python.org" 

Sent: Thursday, 12 February, 2009 5:09:01 AM
Subject: Re: [Tutor] IDLE/phythonWin -- Who's On First? (Abbott and Costello)

 This appears to be an old problem. 
.
However, the question remains. What to do about it? reboot? I killed
about a dozen pythonwin.exe task, but to no avail. 

Wayne Watson wrote: 
Using WinMerge, I found a difference between the two that shouldn't
have been, but it didn't solve the problem.

dialog.rateVar.get() versus dialog.rateVar.get

There was another like it with get(), but it wouldn't have been
executed in my test run. 

I suspect something else like that lurks in the code.

Wayne Watson wrote: 
I ran it w/o using either IDLE/pyWin, and it worked without any
messages, as shown by the console window. Isn't this the same as using
the console? 

Outside of the two, my guess is that some subtle was made to the code,
accidentally, and one tolerates it and the other doesn't. I'm going to
do a WinMerge compare between the current py and a slightly older one. 

Alan Gauld wrote: 

"Wayne Watson"  wrote 


Signature.htmlMy program in IDLE bombed
with: 
== 
Exception in Tkinter callback 
Traceback (most recent call last): 
   dialog = OperationalSettingsDialog( self.master, set_loc_dict ) 
   tkSimpleDialog.Dialog.__init__(self, parent) 
   self.wait_visibility() # window needs to be visible for the grab 


But runs fine in pythonWin performing the
same entry operation. 

Lets eliminate some variables by avoiding any IDEs. 

Does it work OK when run from a DOS console? 
Or if you just double click the file in Windows explorer? 

If it does that means its some kind of interaction between the program 
and IDLE. This is not unusual since IDLE is itself a Tkinter program 
and although recent versions are much better behaved with Tkinter 
it can still cause some strangeness. 

If you still get an error running outside the IDE then we can analyze 
it more closely. For that we will need the code, at least the function 
that calls the dialog. And if its a subclass the dialog class too. 



-- 
Signature.html 
   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)
 
The Richard Feynman
Problem-Solving
Algorithm:
  (1) write down the problem;
  (2) think very hard;
  (3) write down the answer.

Web Page: 



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

-- 
Signature.html 
   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)
 
The Richard Feynman
Problem-Solving
Algorithm:
  (1) write down the problem;
  (2) think very hard;
  (3) write down the answer.

Web Page: 



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

-- 
Signature.html 
   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)
 
The Richard Feynman Problem-Solving
Algorithm:
  (1) write down the problem;
  (2) think very hard;
  (3) write down the answer.

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


Re: [Tutor] Program to report if file was modified today

2009-02-12 Thread Alan Gauld


"David"  wrote

I really only want it to report when a file has been modified so 
this seems to work,



#!/usr/bin/python
import sys
import os
import time

def mod():
"""Find files modified today, given a file path."""
latest = 0
now = time.strftime('%Y-%m-%d', time.localtime())
dir = os.path.dirname(sys.argv[1])
for fname in os.listdir(dir):
if fname.endswith('.py'):
modtime = os.stat(os.path.join(dir, fname)).st_mtime
if modtime > latest:
latest = modtime


Why do you modify latest to whatever the mod time of the
file is? Surely you want to always compare mod time with now?

out = time.strftime('%Y-%m-%d', 
time.localtime(latest))

if out == now:
print fname, "has changed today. "
else:
pass


You don't need the else:pass bit. It does nothing.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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


Re: [Tutor] [tutor] IDLE

2009-02-12 Thread Alan Gauld


"WM."  wrote

IDLE also now works perfectly, whether I open into IDLE or edit 
window.

Below is a copy/paste:

IDLE 2.6
>>> i = 9
>>> while i > 2:
print i
i -= 1


The problem occurs when you go to a second level block:


if 42 > 66:

   print 'false'
else:
   print 'true'

Whjat happens is a beginner will try to align the else with the if as 
shown

in the text books etc anmd get an error from IDLE. Whereas if IDLE
provided the ... secondary prompt it would look like:


if 42 > 66:

...print 'false'
...else:
...print 'true'

Which matches the text books.

Alan G 



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


Re: [Tutor] Keeping Dictonary Entries Ordered

2009-02-12 Thread Alan Gauld

"Wayne Watson"  wrote

config_names = {"start_time : '18:00:00', 'gray_scale' : True, 
"long": 120.00}


If I iterate over it, the entries will appear in any order, as 
opposed to

what I see above. However, in the config file, I'd like to keep them
in the order above.


Thats tricky! Dictionaries are not sorted and do not retain insertion 
order.

If you can define a sort order you can use sorted:


d = {1:2, 3:4, 8:9, 5:7}
for n in d: print n

...
8
1
3
5

for n in sorted(d): print n

...
1
3
5
8




But you need to have an order that will work with sorted().
Its not just the order you add items to the dict. To store an
arbitrary order I suspect you would need to maintain a
secondary list with the keys in the order of insertion.
The most reliable way to dop that would be to subclass
dict. Somebody may already have done that so its worth
a google...

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


Re: [Tutor] Keeping Dictonary Entries Ordered

2009-02-12 Thread Alan Gauld


"Eric Dorsey"  wrote 


config_names

{'start_time': '18:00:00', 'gray_scale': True, 'long': 120.0}

for i, x in config_names.items():

... print i, x
...
start_time 18:00:00
gray_scale True
long 120.0


Thats pretty much a happy coincidence, there is no guarantee 
that it will work like that for all dictionaries:



d = {1:2,3:4,8:9,5:7}
for i,x in d.items(): print i,x
... 
8 9

1 2
3 4
5 7

Alan G.

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


Re: [Tutor] Keeping Dictonary Entries Ordered

2009-02-12 Thread Senthil Kumaran
On Thu, Feb 12, 2009 at 2:55 PM, Alan Gauld  wrote:
> But you need to have an order that will work with sorted().
> Its not just the order you add items to the dict.


And yes algorithmically, there is No Requirement or I should say a
good Use Case for a Dict to have sorted keys. More we work with
dictionaries. we will understand this rationale better.


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


Re: [Tutor] Keeping Dictonary Entries Ordered

2009-02-12 Thread Kent Johnson
On Thu, Feb 12, 2009 at 4:25 AM, Alan Gauld  wrote:

> Thats tricky! Dictionaries are not sorted and do not retain insertion order.

> But you need to have an order that will work with sorted().
> Its not just the order you add items to the dict. To store an
> arbitrary order I suspect you would need to maintain a
> secondary list with the keys in the order of insertion.
> The most reliable way to dop that would be to subclass
> dict. Somebody may already have done that so its worth
> a google...

In fact this is planned for inclusion in Python 2.7:
http://www.python.org/dev/peps/pep-0372/

The PEP lists several implementations including one with the specified API:
http://dev.pocoo.org/hg/sandbox/raw-file/tip/odict.py

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


Re: [Tutor] IDLE/phythonWin -- Who's On First? (Abbott and Costello)

2009-02-12 Thread Wayne Watson




That's a good reason.

I'm off to a XP Pro group to see how to break out of this, and restore
order to IDLE.

ALAN GAULD wrote:

  
  There
used to be a lot of problems running Tkinter 
programs inside IDLE but most of these have been 
solved since about v2.3. However it does still 
through up issues, so the simple solution it to 
always test GUI programs outside of the IDE.
  
Another reason why for serious programming 
I tend to favour the 3 window setup... 
  
  
Alan Gauld
Author of the Learn To Program website
  http://www.alan-g.me.uk/
  
  
  
  




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


Re: [Tutor] Keeping Dictonary Entries Ordered

2009-02-12 Thread spir
Le Thu, 12 Feb 2009 09:25:22 -,
"Alan Gauld"  a écrit :

> > config_names = {"start_time : '18:00:00', 'gray_scale' : True, 
> > "long": 120.00}
> >
> > If I iterate over it, the entries will appear in any order, as 
> > opposed to
> > what I see above. However, in the config file, I'd like to keep them
> > in the order above.  
> 
> Thats tricky! Dictionaries are not sorted and do not retain insertion 
> order.

Had to do something similar.
I just subtyped dict to add a separate list a keys. Initiated with the possible 
initial arguments, then updated by setattr. Very few code lines. Then Ordict 
had a simplissim method to return values (and pairs) the keylist's order.
The only issue was rather a semantic one: should a value change keep the 
keylist unchanged or move the key to the last position?

Denis

--
la vida e estranya
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program to report if file was modified today

2009-02-12 Thread Kent Johnson
On Wed, Feb 11, 2009 at 11:21 PM, bob gailer  wrote:
> David wrote:
>>
>> I get this error with the int(time.localtime())
>>  start_of_today = int(time.localtime())
>> TypeError: int() argument must be a string or a number, not
>> 'time.struct_time'
>
> Should have been start_of_today = int(time.time())

No, that rounds to the current second, not the current day:
In [5]: int(time.time())
Out[5]: 1234438429

In [6]: int(time.time())
Out[6]: 1234438432

In [7]: int(time.time())
Out[7]: 1234438433

Here are two ways to get the time at midnight in seconds since the
epoch. The first one gives local midnight, the second one is UTC:
import time
from datetime import date

Convert datetime.date.today() to seconds:
In [21]: time.mktime(date.today().timetuple())
Out[21]: 1234414800.0

Round down to the nearest whole day:
In [24]: now = int(time.time())

In [27]: now - (now % (60*60*24))
Out[27]: 1234396800

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


Re: [Tutor] Program to report if file was modified today

2009-02-12 Thread David
Kent Johnson wrote:
> On Wed, Feb 11, 2009 at 11:21 PM, bob gailer  wrote:
>   
>
Thanks Alan and Kent,
This works as expected;

#!/usr/bin/python
import sys
import os
import time

def mod():
"""Find files modified today, given a file path."""
now = time.strftime('%Y-%m-%d', time.localtime())
dir = os.path.dirname(sys.argv[1])
for fname in os.listdir(dir):
if fname.endswith('.py'):
modtime = os.stat(os.path.join(dir, fname)).st_mtime
out = time.strftime('%Y-%m-%d', time.localtime(modtime))
if out == now:
print fname, "has changed today. "
mod()

-david

-- 
powered by Gentoo/GNU Linux
http://linuxcrazy.com

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


Re: [Tutor] Program to report if file was modified today

2009-02-12 Thread David
David wrote:
> Kent Johnson wrote:
>   
>> On Wed, Feb 11, 2009 at 11:21 PM, bob gailer  wrote:
>>   
>>
>> 
> Thanks Alan and Kent,
> This works as expected;
> 
> #!/usr/bin/python
> import sys
> import os
> import time
>
> def mod():
> """Find files modified today, given a file path."""
> now = time.strftime('%Y-%m-%d', time.localtime())
> dir = os.path.dirname(sys.argv[1])
> for fname in os.listdir(dir):
> if fname.endswith('.py'):
> modtime = os.stat(os.path.join(dir, fname)).st_mtime
> out = time.strftime('%Y-%m-%d', time.localtime(modtime))
> if out == now:
> print fname, "has changed today. "
> mod()
> 
> -david
>
>   
Sorry and Bob :)

-- 
powered by Gentoo/GNU Linux
http://linuxcrazy.com

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


Re: [Tutor] How to foreach over a dynamic number of levels

2009-02-12 Thread Alexander Daychilde (Gmail)
First, thank you VERY much for your help! That's amazingly much easier than
I thought it would be... I was considering looping through and generating
nested for loops, then exec'ing the whole mess.. UGH, and security risk, to
boot...

Couple of questions:

> Make a list containing all the steps:
> steps = [step1, step2, step3]
> 
> Now you need to convert all steps to lists. This loop builds a new
> list from the original step list, wrapping any bare strings in lists:
> 
> step_lists = []
> for step in steps:
> if isinstance(step, basestring):
> step = [step]
> step_lists.append(step)
> 
> Now what you want is the Cartesian product of all the lists. Python
> has a function (new in Python 2.6) in the itertools library module
> that will do this:

I'm stuck on 2.5.2 because of the framework I'm driving... Does that
preclude this solution?

> from itertools import product
> for model_set in product(*step_lists):
> print model_set
> 
> (The * in the argument list means, take the elements of step_list and
> use them as the arguments to product())
> 
> The output is
> ('step1model-a', 'step2model-a', 'step3model-a')
> ('step1model-a', 'step2model-a', 'step3model-b')
> ('step1model-b', 'step2model-a', 'step3model-a')
> ('step1model-b', 'step2model-a', 'step3model-b')
> ('step1model-c', 'step2model-a', 'step3model-a')
> ('step1model-c', 'step2model-a', 'step3model-b')

I hate to ask such a n00b question, but I am still learning python, and I
must just be missing how to do this: How can I reference the key name as
well?

Basically, what I'm imputing is more like this:

date = {2008-01-01:2008-01-30} # I'll parse that into 30 days
model1 = some_model1
model2 = {some_model2;othermodel2} # I'll parse into a real list
[...]
somekey = somevalue

What I'm outputting is a series of INI files, one for each run. In this
case, that's 30 days times the two models, but since any key/value pair can
be an array, it may end up being hundreds or thousands (or millions) of
runs...

For each run, I output something like this:

date = 2008-01-01
model1 = some_model1
model2 = some_model2
[...]
somekey = somevalue


So I have to be able to regurgitate the key name -- and  I'm not sure how to
address that... Like in PHP, I'd use a variable variable.

(I feel like this is a st00pid n00b question, and if so, I do apologize, but
I'm just... missing it somewhere... I'm honestly trying to improve my coding
skills, but you know how it is - you don't know something until you learn
it... and python is in some ways refreshing and awesome, and in some ways
just blowing my mind...)

> HTH
> Kent

Very much, indeed! :)

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


[Tutor] ConfigParser re-read fails

2009-02-12 Thread Timo

Hello all,

I use a file for my program that I read with ConfigParser. I append all 
sections of this file into a treeview (using pyGTK). The user can add, 
edit or remove sections, options and values. These I write back to the 
file and then update the treeview. The problem is that it doesn't 
update, the values are still the same. How can I re-read the 
configuration file?


This is (very simple) what I do:

conf = ConfigParser.RawConfigParser()
configFile = 'config.cfg'
conf.read(configFile)

fill_treeview()

def fill_treeview()
   # Do the treeview stuff here

def button_clicked()
   write_config(section, option, value)

   conf.read(configFile)  # It needs to be re-read here

   fill_treeview()

def write_config(section, option, value)
  if not conf.has_section(section):
  conf.add_section(section)

   conf.set(section, key, value)

   conf.write(open(configFile, 'w'))


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


Re: [Tutor] How to foreach over a dynamic number of levels

2009-02-12 Thread Kent Johnson
On Thu, Feb 12, 2009 at 1:04 PM, Alexander Daychilde (Gmail)
 wrote:

>> Now what you want is the Cartesian product of all the lists. Python
>> has a function (new in Python 2.6) in the itertools library module
>> that will do this:
>
> I'm stuck on 2.5.2 because of the framework I'm driving... Does that
> preclude this solution?

The docs for itertools.product() give a pure-Python equivalent, just
paste that into your code:
http://docs.python.org/library/itertools.html#itertools.product

> Basically, what I'm imputing is more like this:
>
> date = {2008-01-01:2008-01-30} # I'll parse that into 30 days
> model1 = some_model1
> model2 = {some_model2;othermodel2} # I'll parse into a real list
> [...]
> somekey = somevalue
>
> What I'm outputting is a series of INI files, one for each run. In this
> case, that's 30 days times the two models, but since any key/value pair can
> be an array, it may end up being hundreds or thousands (or millions) of
> runs...
>
> For each run, I output something like this:
>
> date = 2008-01-01
> model1 = some_model1
> model2 = some_model2
> [...]
> somekey = somevalue
>
>
> So I have to be able to regurgitate the key name -- and  I'm not sure how to
> address that... Like in PHP, I'd use a variable variable.

Maybe you should use a list of pairs (tuples) where the first element
is the name and the second is the list of models:
steps = [
('model1', ['some_model1']),
('model2', ['some_model2', 'othermodel2'])
# etc
]

You could perhaps modify the product() function to create the strings
you need. You might want to get a little practice working with lists
first...

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


[Tutor] Converting a numerical variable into a string variable

2009-02-12 Thread Andres Sanchez
Folks,

Please, give me a hand with this. Say I have a vector defined as 
x=[1,2,3,4,...,9]. I need to write string variables with the names 
G1BFG9BF. Thus, the first string variable would be 'G'+'1'+'BF', being the 
number 1 equal to x[0]. Now, using a for loop, I want to do something like: 
y[i]='G'+x[i-1]+'BF' , to create my string variables. Yes, the problem is that 
I am mixing numerical and string variables and Python reports an error when I 
try to do this. So, the question is: how do I do to convert the numerical 
variable x[0]=1 into a string variable '1', so I can do my 'G'+'1'+BF' thing?

Your thoughts are appreciated.

By the way, 'G'+'x[0]'+'BF' reports 'Gx[0]BF'  :)

Regards,

Andres


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


Re: [Tutor] Converting a numerical variable into a string variable

2009-02-12 Thread Sander Sweers
On Thu, Feb 12, 2009 at 21:06, Andres Sanchez  wrote:
> Please, give me a hand with this. Say I have a vector defined as
> x=[1,2,3,4,...,9]. I need to write string variables with the names
> G1BF...G9BF. Thus, the first string variable would be 'G'+'1'+'BF', being
> the number 1 equal to x[0]. Now, using a for loop, I want to do something
> like: y[i]='G'+x[i-1]+'BF' , to create my string variables. Yes, the problem
> is that I am mixing numerical and string variables and Python reports an
> error when I try to do this. So, the question is: how do I do to convert the
> numerical variable x[0]=1 into a string variable '1', so I can do my
> 'G'+'1'+BF' thing?

>>> x = 100
>>> x
100
>>> str(x)
'100'

You can use str() to convert the numbers to strings. To print what you
are looking for do something like this.

>>> x = [x for x in range(1,10)]
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> for var in x:
print 'G' + str(var) + 'BF'


G1BF
G2BF
G3BF
G4BF
G5BF
G6BF
G7BF
G8BF
G9BF

Or:

>>> for var in x:
print 'G%sBF' % var


G1BF
G2BF
G3BF
G4BF
G5BF
G6BF
G7BF
G8BF
G9BF

> By the way, 'G'+'x[0]'+'BF' reports 'Gx[0]BF'  :)

Correct, 'x[0]' is a string not the number you are looking for. See
above on how you can do this.

Greets
Sander

PS: personally I like the second one better :-)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converting a numerical variable into a string variable

2009-02-12 Thread Shawn Milochik
>>> for x in range(1,9):
print("G%dBF" % (x,))


G1BF
G2BF
G3BF
G4BF
G5BF
G6BF
G7BF
G8BF
>>>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] print function in python 2.6 and 3

2009-02-12 Thread Sander Sweers
Hello,

I see more people posting answers with the new print() function only
available in python 3 and in python 2.6 via an import from __future__.
Now I am not confused by this but I can understand that people very
new to python can get confused byt this. Especially people following a
guide online or a book which almost all use the old print statement.

Now what should be used and promoted on the list?

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


Re: [Tutor] print function in python 2.6 and 3

2009-02-12 Thread Alan Gauld


"Sander Sweers"  wrote


I see more people posting answers with the new print() function only
available in python 3 and in python 2.6 via an import from 
__future__.

Now I am not confused by this but I can understand that people very
new to python can get confused byt this. Especially people following 
a

guide online or a book which almost all use the old print statement.


Yes, it is unfiortunate but will settle down over the next year or so 
I guess.



Now what should be used and promoted on the list?


We can't shouldn't promote one over the other since the remit
of this list is to help newbies, regardless of the Python version.
We had similar issues when Python 2 introduced strings as
objects and string methods replaced the functions in the string
module - indeed we still get posts using string.foo...

The only thing I would strongly request is that posters specify
the Python version (both in questions and replies) . That's always
a good idea but especially during a time of change.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


[Tutor] Detecting wx

2009-02-12 Thread Ricardo Aráoz
There are a couple of utilities I want to be able to run from the
command window. Now, if I'm at the command window, or Idle, or other non
wx shell I want to establish a wx app. But if I'm in pythonWin, PyCrust,
or any other wx based shell then there is a wx event loop already
running and if I create other I'll be in trouble. My question is if
there is a way in which I can detect if there is a wx event loop running
in my environment?

TIA


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


Re: [Tutor] print function in python 2.6 and 3

2009-02-12 Thread Sander Sweers
On Thu, Feb 12, 2009 at 22:50, Alan Gauld  wrote:
>> Now what should be used and promoted on the list?
>
> We can't shouldn't promote one over the other since the remit
> of this list is to help newbies, regardless of the Python version.

OK, understood.

> The only thing I would strongly request is that posters specify
> the Python version (both in questions and replies) . That's always
> a good idea but especially during a time of change.

Maybe it is an idea to actively request the versoin if not immediatly obvious?

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


Re: [Tutor] Detecting wx

2009-02-12 Thread John Fouhy
2009/2/13 Ricardo Aráoz :
> There are a couple of utilities I want to be able to run from the
> command window. Now, if I'm at the command window, or Idle, or other non
> wx shell I want to establish a wx app. But if I'm in pythonWin, PyCrust,
> or any other wx based shell then there is a wx event loop already
> running and if I create other I'll be in trouble. My question is if
> there is a way in which I can detect if there is a wx event loop running
> in my environment?

You could try something like:

wx.CallAfter(lambda: None)

Here's what I get in a quick test with no app running:

>>> import wx
>>> wx.CallAfter(lambda: None)
Traceback (most recent call last):
  File "", line 1, in 
  File 
"//Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/wx-2.8-mac-unicode/wx/_core.py",
line 14359, in CallAfter
assert app is not None, 'No wx.App created yet'
AssertionError: No wx.App created yet

Whereas if I do the same thing from pycrust, nothing happens (that is,
presumably my lambda executes).

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


[Tutor] extracting phrases and their memberships from syntax trees

2009-02-12 Thread عماد نوفل
Dear Tutors,
I have syntax trees like the one below. I need to extract the membership
information like which adjective belongs to which noun phrase, and so on. In
short, I want to do something like this:
http://ilk.uvt.nl/team/sabine/chunklink/README.html
I already have the Perl script that does that, so I do not need a script. I
just want to be able to do this myself. My question is: what tools do I need
for this? Could you please give me pointers to where to start? I'll then try
to do it myself, and ask questions when I get stuck.
Thank you in anticipation
 with a syntax tree like this:

( (S
(NP-SBJ-1
  (NP (NNP Rudolph) (NNP Agnew) )
  (, ,)
  (UCP
(ADJP
  (NP (CD 55) (NNS years) )
  (JJ old) )
(CC and)
(NP
  (NP (JJ former) (NN chairman) )
  (PP (IN of)
(NP (NNP Consolidated) (NNP Gold) (NNP Fields) (NNP PLC) 
  (, ,) )
(VP (VBD was)
  (VP (VBN named)
(S
  (NP-SBJ (-NONE- *-1) )
  (NP-PRD
(NP (DT a) (JJ nonexecutive) (NN director) )
(PP (IN of)
  (NP (DT this) (JJ British) (JJ industrial) (NN
conglomerate) ))
(. .) ))



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington

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


Re: [Tutor] Detecting wx

2009-02-12 Thread Alan Gauld


"Ricardo Aráoz"  wrote


wx shell I want to establish a wx app. But if I'm in pythonWin


I didn't think pythonwin was a wx application. I believe it's written
in pure Windows, using the pywin extensions on top of Scintilla.

But that aside I'd be interested in the answer to this one.

there is a way in which I can detect if there is a wx event loop 
running

in my environment?


I've never actually had any problems with wxPython from inside
any of the IDEs but I'm not sure I've actually tried because I
just habitually test all GUIs outside the IDE.

Alan G 



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


Re: [Tutor] IDLE/phythonWin -- Who's On First? (Abbott and Costello)

2009-02-12 Thread Alan Gauld


"ALAN GAULD"  wrote

solved since about v2.3. However it does still 
through up issues, 


Ahem! That should of course be "throw up issues"!

Sorry,

Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/



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


Re: [Tutor] Keeping Dictonary Entries Ordered

2009-02-12 Thread Eric Dorsey
 But you need to have an order that will work with sorted().
> Its not just the order you add items to the dict. To store an
> arbitrary order I suspect you would need to maintain a
> secondary list with the keys in the order of insertion.
> The most reliable way to dop that would be to subclass
> dict. Somebody may already have done that so its worth
> a google...
>
> HTH,
>

Alan, can you give a short snippet of what that would look like?  I was
trying to code out some idea of how you'd retain insertion order using
another dict or a list and didn't get anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping Dictonary Entries Ordered

2009-02-12 Thread John Fouhy
2009/2/13 Eric Dorsey :
> Alan, can you give a short snippet of what that would look like?  I was
> trying to code out some idea of how you'd retain insertion order using
> another dict or a list and didn't get anywhere.

Here's something basic:

class o_dict(dict):
def __init__(self, *args, **kw):
dict.__init__(self, *args, **kw)
self.__keylist = []

def __setitem__(self, key, val):
dict.__setitem__(self, key, val)
if key not in self.__keylist:
self.__keylist.append(key)

def __iter__(self):
return iter(self.__keylist)

It will do the right thing if you do 'for key in odict:', but not for
things like iteritems().  It will break if you delete keys from the
dictionary, and the 'key not in self.__keylist' test will get slow if
you have lots and lots of keys.  It will also not do what you want if
you initialise it as something like: o_dict(foo=1, bar=2)

All of which goes to show that you're probably better off looking for
an implementation online, since someone else is bound to have tied off
all the issues :-)

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


Re: [Tutor] extracting phrases and their memberships from syntax trees

2009-02-12 Thread A.T.Hofkamp

Emad Nawfal (عماد نوفل) wrote:

just want to be able to do this myself. My question is: what tools do I need
for this? Could you please give me pointers to where to start? I'll then try
to do it myself, and ask questions when I get stuck.


I'd start with parsing (reading) the tree to a generic abstract tree, and do 
the processing on the tree as a separate second step.


For your tree, it seems feasible to write a parser by hand (you'd need to read 
about recursive descendant parsers then). Another approach is to use a parser 
generator tool. There are many: http://wiki.python.org/moin/LanguageParsing


To give you a rough classification, LL(1) is the weakest parsing algorithm 
(but also the simplest to understand, comparable to recursive descendant), 
LALR(1) is 'normal', you can parse source code of many real programming 
languages with it (eg C and Python). GLR is the heavy-duty equipment but is 
also much slower and more memory-greedy.


Many people are fond of PyParsing. My personal favorite is PLY.


Enjoy your yourney!


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