Re: [Tutor] Capture keyboard input even without python in focus

2006-04-01 Thread francois schnell
Hello,On windows OS I'm using the nice pyhook module:"The pyHook library wraps the low-level mouse and keyboard hooks in the Windows Hooking API for use in Python applications. "
See tutorial here:http://www.cs.unc.edu/~parente/tech/tr08.shtmlfrancois 
On 01/04/06, Ars <[EMAIL PROTECTED]
> wrote:







What commands could capture input from the keyboard 
(and the mouse while we're at it) even when the running python program isn't the 
program in focus? Sorta like a keylogger, though that's not my goal (wouldn't be 
very secret with a python console running anyways lol.)
 
-Jack

___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] BeautifulSoup - getting cells without new line characters

2006-04-01 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
> Kent Johnson writes: 
> 
> 
>>[EMAIL PROTECTED] wrote: 
>>
>>
>>>List of states:
>>>http://en.wikipedia.org/wiki/U.S._state  
>>>
>>>: soup = BeautifulSoup(html)
>>>: # Get the second table (list of states).
>>>: table = soup.first('table').findNext('table')
>>>: print table  
>>>
>>>...
>>>
>>>WY
>>>Wyo.
>>>Wyoming
>>>Cheyenne
>>>Cheyenne
>src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Flag_of_Wyomin 
>>>g.svg/45px-Flag_of_Wyoming.svg.png" width="45" alt="" height="30" 
>>>longdesc="/wiki/Image:Flag_of_Wyoming.svg" />
>>>
>>>  
>>>
>>>Of each row (tr), I want to get the cells (td): 1,3,4 
>>>(postal,state,capital). But cells 3 and 4 have anchors. 
>>
>>So dig into the cells and get the data from the anchor. 
>>
>>cells = row('td')
>>cells[0].string
>>cells[2]('a').string
>>cells[3]('a').string 
>>
>>Kent 
>>
>>___
>>Tutor maillist  -  Tutor@python.org
>>http://mail.python.org/mailman/listinfo/tutor
> 
> 
> for row in table('tr'):
>cells = row('td')
>print cells[0] 
> 
> IndexError: list index out of range 

It works for me:


In [1]: from BeautifulSoup import BeautifulSoup as bs

In [2]: soup=bs('''
...: WY
...: Wyo.
...: Wyoming
...: Cheyenne
...: Cheyenne
...: http://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Flag_of_Wyomin
...: g.svg/45px-Flag_of_Wyoming.svg.png" width="45" alt="" height="30"
...: longdesc="/wiki/Image:Flag_of_Wyoming.svg" />
...: 
...:  '''
...:
...:
...:
...: )

In [18]: rows=soup('tr')

In [19]: rows
Out[19]:
[
WY
Wyo.
Wyoming
Cheyenne
Cheyenne
http://upload.

g.svg/45px-Flag_of_Wyoming.svg.png" width="45" alt="" height="30" 
longdesc="/wiki/Image:Flag_
]

In [21]: cells=rows[0]('td')

In [22]: cells
Out[22]:
[WY,
  Wyo.,
  Wyoming,
  Cheyenne,
  Cheyenne,
  http://upload
n
g.svg/45px-Flag_of_Wyoming.svg.png" width="45" alt="" height="30" 
longdesc="/wiki/Image:Flag_

In [23]: cells[0].string
Out[23]: 'WY'

In [24]: cells[2].a.string
Out[24]: 'Wyoming'

In [25]: cells[3].a.string


Kent

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


[Tutor] Python, VB math simple problem

2006-04-01 Thread Mr X






Hi looking for help with what should be a fairly simple Python problem, relating to VB inter-operability.

Got a great response from a fellow named Matt at [EMAIL PROTECTED], pointed me in some good directions - some areas, concerns still foggy on, the below thread is included any feedback on this simple dilemma

would be very appreciated.



Thanks,



D



thread follows below;





 

To: 	[EMAIL PROTECTED]	   

Subject: 	Problem with Python math functions and VB	   

Date: 	3/30/2006 9:39:28 PM	   

 Download Message Display Headers Printer Friendly 			 

Previous | Next 



Wondering if you might either know how to solve the following.



I've a background in Visual Basic, and am using an old version, 4.0, it compiles to a smaller executable which I prefer. I find myself in an odd situation, needing a very simple yet powerful capability of Python for a VB app

Im working on.



Simply, a large 300 digit number is divided by a smaller number ranging from 1 to 3 digits. I.e;



This large 300 digit number is generated as a string from the VB app, and I want to input this somehow

from the VB app directly to Python for simple math operations.



Where; x = 300 digit number, y = divisor ( say '37')





x / 37



I want to divide x by y but I want the remainder of this division to at least 3 or 4 decimal places, so my Python script at the command line;



x %y /y. = z



So now I want to take the resultant, the full number plus its remainder, and I want to round this number up

to the next highest number and divide it by the same constant;



z rounded up to next highest number (never the lowest)



so



z /y = z Long



Remove the 'L' at the end, round up the last digit of z = Z



Then;



Z %y /y. = a



Then I want the last five digits of z (not Z) and a truncated at the end, so the last digit before

the decimal point and the four digits past the decimal point printed to a text file.



I want to be able to open the text file with the VB app and use this data as inputs.

==



Ok, so here is my dilemma, I know VERY litle about Python and a fair bit about VB.



Ideally, I'd love to be able to simply have some extremely small executable that just accepts inputs

does the calculations above and then spits out the outputs. If it were possible to write some

simple lines of math code in Python and then compile these scripts in Python to a Windows

compatible executable,that would be fantastic.



If I could simply have my VB app, 'call' the name of the tiny Python executable, and then the Python executable

just automatically looked for a named text file (created by the VB app) and extracted the 300 digit number from this, then performed the calcs, then spit this data out as a new text file name it created, which I could then use the VB app to open and read from, THAT would be ideal.



However, I don't know if Python can compile scripts to an exe? If it can how could I find out how to do this?



If it doesn't, how could I get VB to directly pass commands to the Python command line and then automatically

extract the outputs? Shelling out from VB to Python would be tough to the command line I think, since the Python command line uses the 'Edit / Mark, Paste' approach to inserting, copy inputs, outputs and this would be virtually untenable, as far as I can tell to automate in a VB shell out routine.



So basically, how the heck can I access Pythons ability to perform simple calculations on very large numbers, easily, from within VB 4.0 ? There must be a way, it seems like such a simple think to do, especially since the actual math operations are so simple, straight forward, and would never change.



Any ideas?







--

Matthew,  thanks for your response. 



<-Original Message-> 

>From: Matthew Dixon Cowles

>Sent: 3/31/2006 9:41:18 AM

>To: [EMAIL PROTECTED]

>Cc: [EMAIL PROTECTED]

>Subject: Re: [Python-Help] Problem with Python math functions and VB



>I'm sure that there's a way to do that, but I'm not familiar with 

>Visual Basic and I don't know what inter-process communication 

>facilities it offers. 



Is there a person or group you might direct me to that has worked with this 

inter-process communication between VB and Python?



>I don't think that Python is going to be able to do that for you out 

>of the box. Hundreds of digits of floating-point precision is a lot. 



Could you explain that a bit more, sorry Im not sure what you mean

by 'out of the box' ?  If I run the Python command line screen in windows

and manually type out a very large number, say 180 digits; where 'X' = very large number;



X %37 /37.



returns what Im after, value wise.  but of course I don't want to do this manually each time

for every dividend.



>You might find that one of the Python modules that let you use an 

>extended-precision library would do what you want. GMPY is one: 



>http://gmpy.sourceforge.net/ 


Re: [Tutor] BeautifulSoup - getting cells without new line characters

2006-04-01 Thread jonasmg
Kent Johnson writes: 

> [EMAIL PROTECTED] wrote:
>> Kent Johnson writes:  
>> 
>> 
>>>[EMAIL PROTECTED] wrote:  
>>>
>>>
List of states:
http://en.wikipedia.org/wiki/U.S._state   

: soup = BeautifulSoup(html)
: # Get the second table (list of states).
: table = soup.first('table').findNext('table')
: print table   

...

WY
Wyo.
Wyoming
Cheyenne
Cheyenne
>>>src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Flag_of_Wyomin
 
g.svg/45px-Flag_of_Wyoming.svg.png" width="45" alt="" height="30" 
longdesc="/wiki/Image:Flag_of_Wyoming.svg" />

   

Of each row (tr), I want to get the cells (td): 1,3,4 
(postal,state,capital). But cells 3 and 4 have anchors. 
>>>
>>>So dig into the cells and get the data from the anchor.  
>>>
>>>cells = row('td')
>>>cells[0].string
>>>cells[2]('a').string
>>>cells[3]('a').string  
>>>
>>>Kent  
>>>
>>>___
>>>Tutor maillist  -  Tutor@python.org
>>>http://mail.python.org/mailman/listinfo/tutor
>>  
>> 
>> for row in table('tr'):
>>cells = row('td')
>>print cells[0]  
>> 
>> IndexError: list index out of range 
> 
> It works for me: 
> 
> 
> In [1]: from BeautifulSoup import BeautifulSoup as bs 
> 
> In [2]: soup=bs('''
> ...: WY
> ...: Wyo.
> ...: Wyoming
> ...: Cheyenne
> ...: Cheyenne
> ...:  title=""> ...: 
> src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Flag_of_Wyomin
> ...: g.svg/45px-Flag_of_Wyoming.svg.png" width="45" alt="" height="30"
> ...: longdesc="/wiki/Image:Flag_of_Wyoming.svg" />
> ...: 
> ...:  '''
> ...:
> ...:
> ...:
> ...: ) 
> 
> In [18]: rows=soup('tr') 
> 
> In [19]: rows
> Out[19]:
> [
> WY
> Wyo.
> Wyoming
> Cheyenne
> Cheyenne
>  title="">http://upload. 
> 
> g.svg/45px-Flag_of_Wyoming.svg.png" width="45" alt="" height="30" 
> longdesc="/wiki/Image:Flag_
> ] 
> 
> In [21]: cells=rows[0]('td') 
> 
> In [22]: cells
> Out[22]:
> [WY,
>   Wyo.,
>   Wyoming,
>   Cheyenne,
>   Cheyenne,
>title="">http://upload
> n
> g.svg/45px-Flag_of_Wyoming.svg.png" width="45" alt="" height="30" 
> longdesc="/wiki/Image:Flag_ 
> 
> In [23]: cells[0].string
> Out[23]: 'WY' 
> 
> In [24]: cells[2].a.string
> Out[24]: 'Wyoming' 
> 
> In [25]: cells[3].a.string 
> 
> 
> Kent 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

Yes, ok. But so, it is only possible get data from a row (rows[0]) 

cells=rows[0]('td') 

And I want get data from all rows. I have trying with several 'for' setences 
but i can not. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] BeautifulSoup - getting cells without new line characters

2006-04-01 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
> Yes, ok. But so, it is only possible get data from a row (rows[0]) 
> 
> cells=rows[0]('td') 
> 
> And I want get data from all rows. I have trying with several 'for' setences 
> but i can not. 

Can you show us what you tried?

Have you read a Python tutorial? It seems like some of the things you 
are struggling with might be addressed in general Python material.

Kent

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


Re: [Tutor] Python, VB math simple problem

2006-04-01 Thread Kent Johnson
Mr X wrote:
> 
> 
> Hi looking for help with what should be a fairly simple Python problem, 
> relating to VB inter-operability.
> Got a great response from a fellow named Matt at [EMAIL PROTECTED], 
> pointed me in some good directions - some areas, concerns still foggy 
> on, the below thread is included any feedback on this simple dilemma
> would be very appreciated.

You can use py2exe to make an executable from a python program. Or the 
VB program could invoke a command line like "python prog.py data.txt" 
without needing to use py2exe. You can also write COM servers in Python.

Kent

> 
> Thanks,
> 
> D
> 
> thread follows below;
> 
> 
> 
> To: [EMAIL PROTECTED]
> Subject: Problem with Python math functions and VB
> Date: 3/30/2006 9:39:28 PM
> Download Message Display Headers Printer Friendly
> Previous | Next
> 
> Wondering if you might either know how to solve the following.
> 
> I've a background in Visual Basic, and am using an old version, 4.0, it 
> compiles to a smaller executable which I prefer. I find myself in an odd 
> situation, needing a very simple yet powerful capability of Python for a 
> VB app
> Im working on.
> 
> Simply, a large 300 digit number is divided by a smaller number ranging 
> from 1 to 3 digits. I.e;
> 
> This large 300 digit number is generated as a string from the VB app, 
> and I want to input this somehow
> from the VB app directly to Python for simple math operations.
> 
> Where; x = 300 digit number, y = divisor ( say '37')
> 
> 
> x / 37
> 
> I want to divide x by y but I want the remainder of this division to at 
> least 3 or 4 decimal places, so my Python script at the command line;
> 
> x %y /y. = z
> 
> So now I want to take the resultant, the full number plus its remainder, 
> and I want to round this number up
> to the next highest number and divide it by the same constant;
> 
> z rounded up to next highest number (never the lowest)
> 
> so
> 
> z /y = z Long
> 
> Remove the 'L' at the end, round up the last digit of z = Z
> 
> Then;
> 
> Z %y /y. = a
> 
> Then I want the last five digits of z (not Z) and a truncated at the 
> end, so the last digit before
> the decimal point and the four digits past the decimal point printed to 
> a text file.
> 
> I want to be able to open the text file with the VB app and use this 
> data as inputs.
> ==
> 
> Ok, so here is my dilemma, I know VERY litle about Python and a fair bit 
> about VB.
> 
> Ideally, I'd love to be able to simply have some extremely small 
> executable that just accepts inputs
> does the calculations above and then spits out the outputs. If it were 
> possible to write some
> simple lines of math code in Python and then compile these scripts in 
> Python to a Windows
> compatible executable,that would be fantastic.
> 
> If I could simply have my VB app, 'call' the name of the tiny Python 
> executable, and then the Python executable
> just automatically looked for a named text file (created by the VB app) 
> and extracted the 300 digit number from this, then performed the calcs, 
> then spit this data out as a new text file name it created, which I 
> could then use the VB app to open and read from, THAT would be ideal.
> 
> However, I don't know if Python can compile scripts to an exe? If it can 
> how could I find out how to do this?
> 
> If it doesn't, how could I get VB to directly pass commands to the 
> Python command line and then automatically
> extract the outputs? Shelling out from VB to Python would be tough to 
> the command line I think, since the Python command line uses the 'Edit / 
> Mark, Paste' approach to inserting, copy inputs, outputs and this would 
> be virtually untenable, as far as I can tell to automate in a VB shell 
> out routine.
> 
> So basically, how the heck can I access Pythons ability to perform 
> simple calculations on very large numbers, easily, from within VB 4.0 ? 
> There must be a way, it seems like such a simple think to do, especially 
> since the actual math operations are so simple, straight forward, and 
> would never change.
> 
> Any ideas?
> 
> 
> 
> --
> Matthew, thanks for your response.
> 
> <-Original Message->
>  >From: Matthew Dixon Cowles
>  >Sent: 3/31/2006 9:41:18 AM
>  >To: [EMAIL PROTECTED]
>  >Cc: [EMAIL PROTECTED]
>  >Subject: Re: [Python-Help] Problem with Python math functions and VB
> 
>  >I'm sure that there's a way to do that, but I'm not familiar with
>  >Visual Basic and I don't know what inter-process communication
>  >facilities it offers.
> 
> Is there a person or group you might direct me to that has worked with this
> inter-process communication between VB and Python?
> 
>  >I don't think that Python is going to be able to do that for you out
>  >of the box. Hundreds of digits of floating-point precision is a lot.
> 
> Could you explain that a bit more, sorry Im not sure what you mean
> by 'out of the box' ? If I run the Python command line screen in windows
> a

Re: [Tutor] i need help please read (fwd)

2006-04-01 Thread Alan Gauld
Tom,

I'm still struggling, but based on what Danny Yoo deduced I'll assume
you are talking anbout a dialog box or window of some sort rather
than drawing a graphical box?

> ok ill tell you want im doing i want to make a box using python that if 
> you
> put the box over a number on the computer screen it pops up inside the box

Do you mean a bit like the magnifier applet that ships with Windows?
It magnifies whatever is underneath it on the main screen? If so thats
a really tough task. It is possible in Python but it requires an in-depth
knowledge of how Windows works - much more than I have!

> and the number can be altered in the box and however you alter it on the 
> box
> it turns into that or show what you altered in place of the number you put
> it over get what im saying

And modifying the number underneath is much much harder again.
It might not even be possible! You can alter it in your own program but to 
make
that take effect on the underlying application would require that you find 
out
what application was diswplaying the number and then find a way to write to
that application. These are very deep and difficult programming tasks!

Its theoretically possible in Python, but its very difficult. Even in C++ it
would be a real challenge.

Alan G.
Just back from a week's vacation in Spain. 

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


Re: [Tutor] BeautifulSoup - getting cells without new line characters

2006-04-01 Thread jonasmg
Kent Johnson writes: 

> [EMAIL PROTECTED] wrote:
>> Yes, ok. But so, it is only possible get data from a row (rows[0])  
>> 
>> cells=rows[0]('td')  
>> 
>> And I want get data from all rows. I have trying with several 'for' setences 
>> but i can not. 
> 
> Can you show us what you tried? 
> 
> Have you read a Python tutorial? It seems like some of the things you 
> are struggling with might be addressed in general Python material. 
> 
> Kent 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
 

You consider a thing about me.
If I ask something it is because I cannot find the solution. I do not it by 
whim. 

Yes, I have read tutorials about python, and I have looked for this problem 
in this mail list using a web searcher as alltheweb, and I have even looked 
for in the google groups about python. 

* for rows in table('tr'): print rows('td') 

it fails when i'm going to get data of each cell using:
for rows in table('tr'): print rows('td')[0] 

* for rows in table('tr'):
   for cell in rows('td'):
  print cell 

The same, using print cell[0] 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Doctest, object references and the use of ellipses

2006-04-01 Thread Don Taylor
Hi:

I am trying to use Doctest and am having trouble using the ellipsis 
feature when trying to match an object reference.

Here is the code:

 def add_change_listener(self, listener):
 '''

 Returns list of listeners just for testing.
 >>> def mock_listener():
 ...pass
 >>> model = Model()
 >>> model.add_change_listener(mock_listener) #doctest: +ELLIPSIS
 []

 '''

 self.listeners.append(listener)
 return self.listeners

This is what I get back:

Trying:
 model.add_change_listener(mock_listener) #doctest: +ELLIPSIS
Expecting:
 []
**
File "D:\ProgrammingProjects\MVCExperiments\src\mvcmodel.py", line 14, 
in __main__.Model.add_change_listener
Failed example:
 model.add_change_listener(mock_listener) #doctest: +ELLIPSIS
Expected:
 []
Got:
 []

As far as I can tell from the Doctest documentation this test should 
have passed.

Any help on what I am doing wrong would be much appreciated.

Thanks,

Don.

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


Re: [Tutor] Doctest, object references and the use of ellipses

2006-04-01 Thread Kent Johnson
Don Taylor wrote:
> Hi:
> 
> I am trying to use Doctest and am having trouble using the ellipsis 
> feature when trying to match an object reference.

What version of Python are you using? The ELLIPSIS comment was added in 
Python 2.4.

Kent

> 
> Here is the code:
> 
>  def add_change_listener(self, listener):
>  '''
> 
>  Returns list of listeners just for testing.
>  >>> def mock_listener():
>  ...pass
>  >>> model = Model()
>  >>> model.add_change_listener(mock_listener) #doctest: +ELLIPSIS
>  []
> 
>  '''
> 
>  self.listeners.append(listener)
>  return self.listeners
> 
> This is what I get back:
> 
> Trying:
>  model.add_change_listener(mock_listener) #doctest: +ELLIPSIS
> Expecting:
>  []
> **
> File "D:\ProgrammingProjects\MVCExperiments\src\mvcmodel.py", line 14, 
> in __main__.Model.add_change_listener
> Failed example:
>  model.add_change_listener(mock_listener) #doctest: +ELLIPSIS
> Expected:
>  []
> Got:
>  []
> 
> As far as I can tell from the Doctest documentation this test should 
> have passed.
> 
> Any help on what I am doing wrong would be much appreciated.
> 
> Thanks,
> 
> Don.
> 
> ___
> 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] Python tutor

2006-04-01 Thread Alan Gauld

"Noufal Ibrahim" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Greetings all,

Greetings,

> the reader learns stuff). Another example that comes to mind is the
> tcltutor program to learn TCL. It contains an instruction window, a
> code window and an output window. The user is told something, they try
> it and the output is visible. I personally used it when I was learning
> TCL.

The tcltutor is fabulous and I've often toyed with the idea of trying to
build a Pythonic version. I particularly like the way you can toggle from
newbie view to expert view and get the same feature described in
friendly English prose through to the bare man pages.

> The python tutorial is great and probably all one needs to learn the
> language but I think a more interactive program to teach it might be

I tried at one stage producing JavaScripted versions of the code in my
tutor where you could step through the code with the active line being
highlighted in colour - like a debugger. But after struggling for ages to 
get
one short example to work it seemed too much like hard work!

> Do you all think it'll be a worthwhile project?

I think it would be a great project.

The other one in similar vein is to do a demo program of the Tkinter
(and the Tix widgets too) similar to the Tcl/Tk demo program. Again
I've had it on my list of 'things to do' for several years now!

Alan G.
(Back from a weeks vacation in sunny Spain! :-)



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


Re: [Tutor] BeautifulSoup - getting cells without new line characters

2006-04-01 Thread Danny Yoo


> > Have you read a Python tutorial? It seems like some of the things you
> > are struggling with might be addressed in general Python material.
>
>
> You consider a thing about me. If I ask something it is because I cannot
> find the solution. I do not it by whim.

Hello Jonas,

Yes, but don't take Kent's question as a personal insult --- he's asking
because it looks like you're having trouble interpreting error messages or
considering border cases.

If you're doing HTML parsing, there's an unspoken assumption that you've
already mastered basic programming.  After reading the questions you're
asking, I agree with Kent; you're struggling with things that you should
have already covered in tutorials.

Out of curiosity, what tutorials have you looked at?  Give us links, and
we'll take a look and evaluate them for accuracy.  Some Python tutorials
are good, but some of them out there are quite bad too.  The ones linked
from:

http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

and:

http://wiki.python.org/moin/BeginnersGuide/Programmers

should be ok.



> * for rows in table('tr'): print rows('td')
>
> it fails when i'm going to get data of each cell using:
>
> for rows in table('tr'): print rows('td')[0]

Just to clarify: when you say it "fails", please try to be more specific.
What exactly does Python report as the error?


I see that you mention the error message here:

http://mail.python.org/pipermail/tutor/2006-March/046103.html

But are you looking at the error message and trying to understand what
it's saying?  It says that the cell doesn't have a zeroth element. And
this is probably true! I wouldn't disbelieve the computer on this one.
*grin*


TD elements probably won't have nested sub-elements.  But they may have
a 'string' attribute, though, which is what Kent's example used to pull
the text out of the TD.

But your reply to his message doesn't look like it even responds to Kent's
example.  It is unclear to us why you're not reading or understanding his
example, and just going off and doing something else.  If you don't
understand a reply, try asking a question about it: we'll be happy to
elaborate.  Try not to go off so quickly and ignore responses:  it gives
the impression that you don't care enough to read through things.



Anyway, the program snippet above makes assumptions, so let's get those
out of the way.  Concretely:

for rows in table('tr'):
print rows('td')[0]

makes an assumption that is not necessarely true:

* It assumes that each row has a td element.

Do you understand the border case here?  In particular:

* What if you hit a TR table row that does not have any TD columns?

>From viewing the wiki web page you gave as an example, I can see several
TR's in the page's content that do not have TD's, but only TH's.  I'm not
certain what BeautifulSoup will do in this situtation --- I suspect that
it'll return None --- but in any case, your code has to account for this
possibility.

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


Re: [Tutor] request for sugestions on fragement of code for generating truth-tables

2006-04-01 Thread Orri Ganel
Brian van den Broek wrote:

>Hi all,
>
>I've been too busy with life to do much Python of late. So, I am a bit
>rusty. :-(
>
>I've got some code that does what it needs to but I think I might be 
>overlooking a much smoother way to get what I need.
>
>The code takes a list of strings and returns a list of dictionaries, 
>where each dictionary uses the strings from the list as keys and 
>booleans as values. The dictionary list contains a dictionary 
>exhibiting each possible combination of booleans over the keys, where 
>these combinations are listed in a systematic way.
>
>This code works:
>
>def tva_dict_maker(atoms):
>
> tvas = []
> val = False
>
> for k in range(2**len(atoms)):
> tvas.append(dict())
>
> for i in range(len(atoms)):
> key = atoms[i]
>
> for j in range(2**len(atoms)):
> if j % ( len(tvas) / 2.0 ** (i+1) ) == 0:
> val = not val
> tvas[j][key]=val
>
> return tvas
>
>The output I desire is easier to see with a bit of processing (to 
>impose order on the dicts). So,
>
>def display_tvas(tvas):
> for i in tvas:
>   for j in sorted(i):
>   print "%s:%s\t" %(j, i[j]),
>   print
>
>Then, the output is like so:
>
> >>> atoms = ["a","b","c"]
> >>> tvas = tva_dict_maker(atoms)
> >>> display_tvas(tvas)
>a:True b:True  c:True  
>a:True b:True  c:False 
>a:True b:False c:True  
>a:True b:False c:False 
>a:Falseb:True  c:True  
>a:Falseb:True  c:False 
>a:Falseb:False c:True  
>a:Falseb:False c:False 
> >>>
>
>As desired :-)
>
>But, I can't shake the feeling I'm doing this in a harder than 
>necessary way :-|
>
>[This is in the context of writing a program to generate truth-tables 
>for sentential logic, so that I can easily create answered exercises 
>for a critical thinking course I've been teaching. (As it is for a 
>Philosophy class, the truth-values are listed in the opposite order 
>than in a CompSci context.)]
>
>Any suggestions would be welcome.
>
>Best to all,
>
>Brian vdB
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>  
>
What this shouts immediately to me, at least, is binary numbers and 
bool().  Just use your favorite binary conversion recipe, and count down 
from int(len(atoms)*"1",2), converting as you go.  And then you take the 
boolean value of each digit of the binary number.  If you need help, let 
me know as I've completed a working model.

HTH,
Orri

-- 
Email: singingxduck AT gmail DOT com
AIM: singingxduck
Programming Python for the fun of it.

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


Re: [Tutor] Doctest, object references and the use of ellipses

2006-04-01 Thread Don Taylor
Kent Johnson wrote:
> Don Taylor wrote:
> 
>>Hi:
>>
>>I am trying to use Doctest and am having trouble using the ellipsis 
>>feature when trying to match an object reference.
> 
> 
> What version of Python are you using? The ELLIPSIS comment was added in 
> Python 2.4.
> 

I am using 2.4.2

Don.

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


Re: [Tutor] Apple Remote "Mouse"

2006-04-01 Thread Alan Gauld

"Johnston Jiaa" <[EMAIL PROTECTED]> wrote in message
> I recently bought a Macbook Pro from Apple.

I'm jealous already...

> As it comes with a remote, I thought it would be great to use it as
> a mouse when not in Front Row.  ...
> Is there any way to manipulate the cursor position on the screen using 
> Python?

This is really about programming Cocoa more than it is about Python.
You need to look at how you would do this in Objective C first then
translate that to Python using the Cocoa wrappers for MacPython.

It should all be feasible but I'd start looking on the Apple development
sites first - have you checked the fantastic programmer's pages on the
Apple site at:

http://developer.apple.com/

If you haven't already done so sogn up to the developers email list.
Not only do you get advance news of developments in Apple land
but they post new articles and how-tos every week or so.

Also worth keeping an eye on is O'Reilly's MacDevCenter

http://www.macdevcenter.com/

And finally Stepwise's Vermont recipes are great:

http://www.stepwise.com/

Look in particular at the starting Cocoa and Articles links.

For your purpose you will need to create mouse events and then
post them to the applications event queue using the postEvent
method. There are several factory methods available for creating events.

Warning: This is not a trivial task and will involve some fairly deep
reading about the Apple event model and the mouse event definitions
in particular. But it is doable.

HTH,


-- 
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




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


Re: [Tutor] Python tutor

2006-04-01 Thread Don Taylor
Noufal Ibrahim wrote:
> Greetings all,
>Are there any programs for python that offer an "interactive" tutorial?
> Something on the lines of the builtin emacs tutorial (which is


While it is not really what you had in mind, I have just discovered the 
Python Challenge - and it is a lot of fun.

http://www.pythonchallenge.com/

Don.

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


Re: [Tutor] how to get the content of an XML elements.

2006-04-01 Thread Alan Gauld

"Keo Sophon" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> Is there anyway to get the content of an XML elements. I am using xml.dom.

For true XML I think ElemTree (by Fred Lundh?)  is the best approach.
Try a Google search.

Kent uses it as I recall.

Alan G. 



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


Re: [Tutor] how to get the content of an XML elements.

2006-04-01 Thread Kent Johnson
Alan Gauld wrote:
> "Keo Sophon" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
> 
> 
>>Is there anyway to get the content of an XML elements. I am using xml.dom.
> 
> 
> For true XML I think ElemTree (by Fred Lundh?)  is the best approach.
> Try a Google search.

Yes, it's ElementTree though.

Keo, what do you mean, "get the content of an XML elements"?

Kent

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


Re: [Tutor] Python tutor

2006-04-01 Thread Don Taylor
Alan Gauld wrote:

> 
> I tried at one stage producing JavaScripted versions of the code in my
> tutor where you could step through the code with the active line being
> highlighted in colour - like a debugger. But after struggling for ages to 
> get
> one short example to work it seemed too much like hard work!
> 

I found this site the other day and I thought that it would not be too 
difficult to generalize this technique into a simple tool for authoring 
tutorials.

http://www.jorendorff.com/toys/

Don.

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


Re: [Tutor] BeautifulSoup - getting cells without new line characters

2006-04-01 Thread jonasmg
Danny Yoo writes: 

>  
> 
>> > Have you read a Python tutorial? It seems like some of the things you
>> > are struggling with might be addressed in general Python material. 
>>
>>
>> You consider a thing about me. If I ask something it is because I cannot
>> find the solution. I do not it by whim.
> 
> Hello Jonas, 
> 
> Yes, but don't take Kent's question as a personal insult --- he's asking
> because it looks like you're having trouble interpreting error messages or
> considering border cases. 
> 

Sorry Kent if my answer was very rude. I very was tired to try many things 
without no good result. 

> Anyway, the program snippet above makes assumptions, so let's get those
> out of the way.  Concretely: 
> 
> for rows in table('tr'):
> print rows('td')[0] 
> 
> makes an assumption that is not necessarely true: 
> 
> * It assumes that each row has a td element. 
> 
> Do you understand the border case here?  In particular: 
> 
> * What if you hit a TR table row that does not have any TD columns? 
> 

Danny, you give me the idea. The problem is that the first row has TH 
columns (not TD). So: 

for row in table('tr'):
   if row('td'):
   print row('td')[0].string 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] BeautifulSoup - getting cells without new line characters

2006-04-01 Thread jonasmg
And the solution to get the state and capital columns (where there are 
anchors): 

for row in table('tr'):
   for cell in row.fetch('a')[0:2]:
   print cell.string 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python tutor

2006-04-01 Thread Alan Gauld
>> I tried at one stage producing JavaScripted versions of the code in my
>
> I found this site the other day and I thought that it would not be too 
> difficult to generalize this technique into a simple tool for authoring 
> tutorials.
>
> http://www.jorendorff.com/toys/

Yes, this is similar to something else I tried which was a collapsing editor
style presentation where you could collapse or expand functions. The problem
was that I found it difficult to get a version that worked properly on all
browsers and the amount of code to get it universal was too much for
me to tolerate in a web page! (I'm a minimalist when it comes to HTML!)

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


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


Re: [Tutor] Doctest, object references and the use of ellipses

2006-04-01 Thread Tim Peters
[Don Taylor]
> I am trying to use Doctest and am having trouble using the ellipsis
> feature when trying to match an object reference.
>
> Here is the code:
>
>  def add_change_listener(self, listener):
>  '''
>
>  Returns list of listeners just for testing.
>  >>> def mock_listener():
>  ...pass
>  >>> model = Model()
>  >>> model.add_change_listener(mock_listener) #doctest: +ELLIPSIS
>  []
>
>  '''
>
>  self.listeners.append(listener)
>  return self.listeners
>
> This is what I get back:
>
> Trying:
>  model.add_change_listener(mock_listener) #doctest: +ELLIPSIS
> Expecting:
>  []
> **
> File "D:\ProgrammingProjects\MVCExperiments\src\mvcmodel.py", line 14,
> in __main__.Model.add_change_listener
> Failed example:
>  model.add_change_listener(mock_listener) #doctest: +ELLIPSIS
> Expected:
>  []
> Got:
>  []

That "should work", provided there aren't differences in whitespace
that are invisible to us in this medium.  For example, if, in your
source file, there's actually a (one or more) trailing space on your
line of expected output, then it would _not_ match the actual output. 
Try adding +REPORT_NDIFF to your #doctest options; that will point out
all differences (including whitespace).
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Apple Remote "Mouse"

2006-04-01 Thread Johnston Jiaa
Hey Alan,I'm fairly new to programming and this is my first Apple computer.  It arrives on Monday, I've been waiting for ages.  Amazon took forever shipping it out.  Anyway, this will ultimately be a true learning experience -- I'll tell you how it turns out!  Many thanks for giving me these pointers, I'll check out all the sites and be immersed in all these Apple pages the next couple of weeks or so.Johnston Jiaa ([EMAIL PROTECTED])
		Blab-away for as little as 1¢/min. Make  PC-to-Phone Calls using Yahoo! Messenger with Voice.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Doctest, object references and the use of ellipses

2006-04-01 Thread Don Taylor
Tim Peters wrote:

> That "should work", provided there aren't differences in whitespace
> that are invisible to us in this medium.  For example, if, in your
> source file, there's actually a (one or more) trailing space on your
> line of expected output, then it would _not_ match the actual output. 
> Try adding +REPORT_NDIFF to your #doctest options; that will point out
> all differences (including whitespace).


Oh, thank you!

Yes, there was a trailing whitespace.  And yes I did read the manual 
that warned about this but I guess it did not register deep enough into 
my reptile brain (you know, the Python brain).

I will try not to do this again (and I probably won't).

Don.

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


Re: [Tutor] Doctest, object references and the use of ellipses

2006-04-01 Thread Tim Peters
[Tim Peters]
>> That "should work", provided there aren't differences in whitespace
>> that are invisible to us in this medium.  For example, if, in your
>> source file, there's actually a (one or more) trailing space on your
>> line of expected output, then it would _not_ match the actual output.
>> Try adding +REPORT_NDIFF to your #doctest options; that will point out
>> all differences (including whitespace).

[Don Taylor]
> Oh, thank you!

Cool -- glad it worked!

> Yes, there was a trailing whitespace.  And yes I did read the manual
> that warned about this but I guess it did not register deep enough into
> my reptile brain (you know, the Python brain).
>
> I will try not to do this again (and I probably won't).

I'll share a secret :-)  I work on Python development, and a few times
per week I run this from the root of a Python checkout (this is on
Windows, BTW):

python \Python24\Tools\Scripts\reindent.py -r .

reindent.py is in your distribution too.  It ensures (by rewriting
files as needed) that all .py files reachable from "." conform to core
Python's whitespace standards, which includes things like 4-space
indentation, no hard tab characters, and no trailing whitespace on any
lines.  All the .py files in a distribution are automatically kept
free of "whitespace surprises" this way.  You're allowed to run that
on your code too ;-)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] BeautifulSoup - getting cells without new line characters

2006-04-01 Thread Danny Yoo


> And the solution to get the state and capital columns (where there are
> anchors):
>
> for row in table('tr'):
>for cell in row.fetch('a')[0:2]:
>print cell.string

Hi Jonas,

That's good to hear!  So does everything work for you then?

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