Re: [Tutor] is there any Python code for spatial tessellation?

2005-10-10 Thread János Juhász


Dear Danny,

take a look to http://public.kitware.com/VTK/
I have seen visualization for voronoi and delaunay in that package.

I have seen also good programming samples mainly in C for calculating
perpedincular and so on at:

 http://www.graphicsgems.org/   


I know it is about 10 years old, but very usable for this topic.

VTK was very good and easy, ( when I downloaded the correct binaries :) )


Yours sincerely,
__
János Juhász


> Message: 7
> Date: Sun, 9 Oct 2005 22:22:29 -0700
> From: Shi Mu <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] is there any Python code for spatial
> tessellation?
> To: Danny Yoo <[EMAIL PROTECTED]>
> Cc: Tutor 
> Message-ID:
> <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-2022-JP

> There are four points with coordinates:
> 2,3;4,9;1,6;3,10.
> How to use Python to draw one perpendicular bisector between (2,3) and
(4,9);
> the other perpendicular bisector between (1,6)?(3,10);
> then, makes the output like:
> l1 a b c
> l2 a b c
> (Note: l indicates the perpendicular bisector with equation ax + by = c.)
> Plus the intersection coordinates of the two perpendicular bisectors:
> x,y

>
> On 10/8/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
> >
> >
> > On Sat, 8 Oct 2005, Shi Mu wrote:
> >
> > > is there any Python code for spatial tessellation?
> >
> > Are you looking for code to generate voronoi diagrams?
> >
> >http://en.wikipedia.org/wiki/Voronoi_diagram
> >
> > From initial Google searches, it appears that there is a package called
> > Qhull that pepole use to do Voronoi tesselations.
> >
> >http://www.qhull.org/
> >
> > and Chris Myers has written a module around Qhull:
> >
> >http://www.tc.cornell.edu/~myers/PyXL/
> >
> >
> > Otherwise, I don't think we at Tutor can help you that much; you may
want
> > to ask on a more math-oriented forum.  Good luck to you!
> >
> >
> >

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


Re: [Tutor] is there any Python code for spatial tessellation?

2005-10-10 Thread Duncan Gibson
This is off-topic for Python: I've seen this on the FLTK mailing list,
but you might find some of the links under this page useful:

http://myweb.tiscali.co.uk/oaktree/nshea/tesselsphere/tesselsphere_index.html

  "OpenGL spherical subdivision utility. Currently employs particle
   and geodesic modules. GUI morphers can split Delaunay and Voronoi
   hulls to create new cells in the lattice. Additionally, morphers
   can target individual cells to split or stellate. Can be used to
   generate vertices for geodesic spheres, pollen, radiolaria, viruses
   and other polyhedra. Saves VRML 1.0 and POV-Ray inc file."

Disclaimer:
   This isn't my field of expertise.
   I haven't used this software.
   Don't ask me any questions about it.

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


Re: [Tutor] Regex help

2005-10-10 Thread Bill Burns
>> I'm looking to get the size (width, length) of a PDF file. Every pdf
>> file has a 'tag' (in the file) that looks similar to this
>>
>> Example #1
>> MediaBox [0 0 612 792]
>>
>> or this
>>
>> Example #2
>> MediaBox [ 0 0 612 792 ]
>>
>> I figured a regex might be a good way to get this data but the
>> whitespace (or no whitespace) after the left bracket has me stumped.
>>
>> If I do this
>>
>> pattern = re.compile('MediaBox \[\d+ \d+ \d+ \d+')
>>
>> I can find the MediaBox in Example #1 but I have to do this
>>
>> pattern = re.compile('MediaBox \[ \d+ \d+ \d+ \d+')
>>
>> to find it for Example #2.
>>
>> How can I make *one* regex that will match both cases?
> 
> 
> pattern = re.compile('MediaBox \[ *\d+ \d+ \d+ \d+')

Bob,

Thanks that works perfectly!

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


Re: [Tutor] line question

2005-10-10 Thread Alan Gauld
Hi,

Can you describe how you would solve the problem without
Python first of all? Just so we understand what you want to do
and how much is a Python problem and how much a basic
understanding issue. Often once you think through how to
do it without Python the programming becomes easier.

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



- Original Message - 
From: "Shi Mu" <[EMAIL PROTECTED]>
To: 
Sent: Monday, October 10, 2005 6:24 AM
Subject: [Tutor] line question


> There are four points with coordinates:
> 2,3;4,9;1,6;3,10.
> How to use Python to draw one perpendicular bisector between (2,3) and 
> (4,9);
> the other perpendicular bisector between (1,6)和(3,10);
> then, makes the output like:
> l1 a b c
> l2 a b c
> (Note: l indicates the perpendicular bisector with equation ax + by = c.)
> Plus the intersection coordinates of the two perpendicular bisectors:
> x,y
>
> 

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


Re: [Tutor] Regex help

2005-10-10 Thread Bill Burns
[Andrew]
> If the format is consistent enough,  you might get away with something like:
> 
>  >>> p = re.compile('MediaBox \[ ?\d+ \d+ (\d+) (\d+) ?\]')
>  >>> print p.search(s).groups()
> ('612', '792')
> 
> The important bits being:  ? means "0 or 1 occurences", and you can use 
> parentheses to group matches, and they get put into the tuple returned 
> by the .groups() function. So you can match and extract what you want in 
> one go.
> 
> http://www.amk.ca/python/howto/regex/ is a fairly gentle introduction to 
> regular expressions in Python if you want to learn more.
> 
> Having said all that, usually you would use a library of some sort to 
> access header information, although I'm not sure what Python has for PDF 
> support, and if that's -all- the information you need, and the -only- 
> variation you'll see, regex probably won't be too bad :)
> 

Thanks, Andrew!

Yes, the format is consistent (I believe the whitespace I mentioned is
the only difference you may find).

I'll take a look at your use of group matches tonight, looks like a
really easy way to return the two numbers I need.

Yeah, I was hoping to find a python PDF library that could do this, but
things seem a little sparse in this area. The only info I need is the
PDF size and it's consistently located (and tagged) in the MediaBox so I
figured it was a good way to get the data.

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


[Tutor] and-or precedence

2005-10-10 Thread Krishna
>>> 1 or 2 and 3
1

Why does the above expression return 1? As per my understanding of
boolean operations, this is what should have happaned:

1 or 2 => 1 and then
1 and 3 => 3

The library reference also suggests that 'or' has higher priority than 'and'.
http://docs.python.org/lib/boolean.html

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


Re: [Tutor] and-or precedence

2005-10-10 Thread paul brian
 has a higher priority than  (as shown on the page you
referenced -its "ascending priority"). Perhaps that could be clearer.

I find that brackets always make life easier in these cases (a rare
statement in the Python world :-), and on a personal note I would
always always comment heavily boolean operations that are not
immediately obvious. it saves brain ache later on.

>>> ((1 or 2) and 3)
3

However 1 or 2 and 3 is <1> if  is evaluated first.

cheers


On 10/10/05, Krishna <[EMAIL PROTECTED]> wrote:
> >>> 1 or 2 and 3
> 1
>
> Why does the above expression return 1? As per my understanding of
> boolean operations, this is what should have happaned:
>
> 1 or 2 => 1 and then
> 1 and 3 => 3
>
> The library reference also suggests that 'or' has higher priority than 'and'.
> http://docs.python.org/lib/boolean.html
>
> Thanks
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
--
Paul Brian
m. 07875 074 534
t. 0208 352 1741
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Stopping function after given time

2005-10-10 Thread Raduz
Hi all
Simple question: Is it possible to stop a running function after certain 
predefined time? 

Right now I have some home-made bash scripts for recording shows from net 
broadcasted ogg stream, that use wget for stream downloading, and two "at" 
jobs - one for first script (which prepares directory it's going to save to, 
dumps info about it's own PID, and execs wget for actual download), second for 
another script (which reads PID info dumped by the first script, uses it to 
kill wget and performs some clean-up tasks). 
I would like to convert this routine to Python using pycurl module, or maybe 
even standard urllib.urlretrieve. Problem is, downloaded file is a stream 
without end, so I have to stop the downloader manually, and I would prefer to 
drop the "two scripts" approach - no more PID saving and process killing. I 
want one clean package, which will start the download, let it run for some 
time, and then stop it correctly. I just don't know how to time the download. 
Can anybody give some sugestions, please? Is the threading way to go? Thanks.

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


Re: [Tutor] line question

2005-10-10 Thread Daniel Watkins
> There are four points with coordinates: 
> 2,3;4,9;1,6;3,10. 
> How to use Python to draw one perpendicular bisector between (2,3) and
> (4,9); 
> the other perpendicular bisector between (1,6)和(3,10); 
> then, makes the output like: 
> l1 a b c 
> l2 a b c 
> (Note: l indicates the perpendicular bisector with equation ax + by =
> c.) 
> Plus the intersection coordinates of the two perpendicular bisectors: 
> x,y 

What code do you have thus far?

Dan

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


Re: [Tutor] Stopping function after given time

2005-10-10 Thread Johan Geldenhuys
You can use the Timer in threading. Look at what is done in the attached 
script. You can basically use three functions to start the timer, stop 
the timer and do what ever it is to be done upon expiring.


There maybe easier ways, but this will work. You can call the 
start_timer with a time in seconds and then it will terminate and there 
you can call a different function that does the rest of your stuff.


Johan.

Raduz wrote:


Hi all
Simple question: Is it possible to stop a running function after certain 
predefined time? 

Right now I have some home-made bash scripts for recording shows from net 
broadcasted ogg stream, that use wget for stream downloading, and two "at" 
jobs - one for first script (which prepares directory it's going to save to, 
dumps info about it's own PID, and execs wget for actual download), second for 
another script (which reads PID info dumped by the first script, uses it to 
kill wget and performs some clean-up tasks). 
I would like to convert this routine to Python using pycurl module, or maybe 
even standard urllib.urlretrieve. Problem is, downloaded file is a stream 
without end, so I have to stop the downloader manually, and I would prefer to 
drop the "two scripts" approach - no more PID saving and process killing. I 
want one clean package, which will start the download, let it run for some 
time, and then stop it correctly. I just don't know how to time the download. 
Can anybody give some sugestions, please? Is the threading way to go? Thanks.


 

"""
  This module is helpful when something must be done on a timer.
  It is for testing purposes but the main class can be used in other applications
  with modifications to the timerExpired function. You can put your own stuff in there
  that must happen when the timer has expired.
  
  It is used by executing it from the shell with arguments -t, -s and optional -x.
  argument -t: timer duration in seconds.
  argument -s: choice to stop the timer after a default 5 seconds,
  optional -x: if -s was 'y' and an delay in seconds before the timer must be stopped.
  
  
  Author: J Geldenhuys
  AccessTel
  Date  : 2005-08-11
  
"""

# The threading module is used that has the code to start
# and stop the timer instance, so, why not use it?
from threading import Timer

# time module is imported to sleep a while after timer is started 
# before stopping it.
import time

# Main class to get timer instance going
class timer:
  
  # start timer with n seconds and go to self.timerExpired
  def startTimer(self, n):
self.t = Timer(n, self.timerExpired)
print 'Timer started for %d seconds'% n

#start the timer instance
self.t.start()

  # Called if argument is true to stop it.
  def stopTimer(self):

# First look if the timer is active
if self.t.isAlive():
print 'Timer found active'

#Stop the timer instance
self.t.cancel()
print 'Timer stopped'

# If not active, do nothing
else:
print 'Timer inactive'

  # Put code in here to execute after timer has expired.
  def timerExpired(self):
print 'Timer expired, go to do what you want'

# Help the user understand how to use this module
def printUsageAndExit():
print
print "Usage: %s -t[time-out] "\
  "-s[sleep before stopping, 'y' or 'n'] \r\noptional -x[seconds to wait before stopping]\r\n\r\n"\
  "Example: timertest.py -t 10 -s y -x 6\r\n" % sys.argv[0]
print
sys.exit(1)


if __name__ == '__main__':

import sys, getopt, string

l = (len(sys.argv))

if l <= 4:
   printUsageAndExit()
arg3 = int('5')
optlist, args = getopt.getopt(sys.argv[1:], "t:s:x:")
for opt in optlist:

if opt[0] == '-t':
   if not (opt[1].isdigit()):
  printUsageAndExit()
   arg1 = int(opt[1])
   
elif opt[0] == '-s':
   arg2 = (opt[1])
  
   #printUsageAndExit()
   
   
elif opt[0] == '-x':
   if not (opt[1].isdigit()):
  printUsageAndExit()
   arg3 = int(opt[1])
   

x = timer()
if arg2 == 'y':
   x.startTimer(arg1)
   time.sleep(arg3)
   print 'You chose to stop the timer after %s with option %s'%(str(arg3), arg2)
   x.stopTimer()
   
if arg2 == 'n':
   x.startTimer(arg1)
   
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] and-or precedence

2005-10-10 Thread ZIYAD A. M. AL-BATLY
On Mon, 2005-10-10 at 17:58 +0530, Krishna wrote:
> >>> 1 or 2 and 3
> 1
> 
> Why does the above expression return 1? As per my understanding of
> boolean operations, this is what should have happaned:
> 
> 1 or 2 => 1 and then
> 1 and 3 => 3
> 
> The library reference also suggests that 'or' has higher priority than 'and'.
> http://docs.python.org/lib/boolean.html
> 
> Thanks
This is called "lazy evaluation"¹ and is used in a lot in computer
world.

Here's how it work (I'll use your example with Python):
What Python sees is this: "1 or (2 and 3)" (because of the "Left to
Right" evaluation rule).

Now, Python evaluate that first step: 1 or (something else).  What's the
outcome of that evaluation?  Remember, Python tries hard so that the
output is True.  "1" in this case is True.  For that reason Python
doesn't have an obligation to continue as it got what it wants, a
True.  
No matter what the outcome of "2 and 3" is, the result is always True.
So, it stops right there (yes, it doesn't even evaluate "2 and 3" at
all!).

To illustrate:
>>> 0 or 2 and 3
3

Applying what I said above:  0 or (something else) is not guaranteed to
be True since 0 is False and there's a possibility (or "hope") that
"something else" will evaluate to True, and that's the reason Python
will continue to evaluate "2 and 3" which will give us "3".

The same thing applies when using "and" instead of "or":
>>> 1 and 2 or 3
2
"1 and 2" is always "2" which is True.  Python stops right there.

>>> 0 and 2 or 3
3
In this example, what Python sees is (0 and 2) or (3).  "0 and 2" is "0"
which is False, but since there's hope because of the "or", Python
continues to evaluate the rest.  (It will be like: "0 or 3"


I'm very bad at explaining and to tip-it-off, English isn't my native
language, but I hope it was clear enough and it was helpful for you.
Ziyad.

References:
 1. http://en.wikipedia.org/wiki/Lazy_evaluation

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


[Tutor] New image with Tkinter?

2005-10-10 Thread Joseph Quigley
Hi,
I've written an image reader that uses the PIL module.
I have a variable that uses os.listdir('mydir') to make a list of all
the pictures in the folder... here's what I'm talking about:

pics = os.listdir(imgDir)
pics.remove('license.txt')
pics.remove('gacor.py')
class Data:
    pic = 0
print "There are %s saved images in the image folder." % len(pics)

def newPic():
    Data.pic += 1

def quitProg():
    raise SystemExit

def Dsp_Image():
    root = Tk()
    root.title("GaCoR Image Browser")
    app = Frame(root)
    app.grid()
    img = Image.open('%s%s' % (imgDir, pics[Data.pic]))
    imgPrep = ImageTk.PhotoImage(img)
    imgShow = Label(app, image=imgPrep).grid()
    info = Label(app, text="Displaying %s" % pics[Data.pic]).grid()
    next = Button(app, text="Next Image", command=newPic).grid()
    close = Button(app, text="Close", command=quitProg).grid()
    print Data.pic
    app.mainloop()


Dsp_Image()


The image won't refresh when I click on 'Next'! Any help would be appreciated.
Joe
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Stopping function after given time

2005-10-10 Thread Oliver Maunder
>Simple question: Is it possible to stop a running function after certain>predefined time?
>>I would like to convert this routine to Python using pycurl module, or maybe>even standard urllib.urlretrieve. 

If you use urllib.urlretrieve, you can pass in a callback
function that gets called after every few blocks are downloaded. In
that function, you could check how  much time has passed since the
download started, and quit the download if necessary. You should be
able to do this without threads. I'm not sure how you stop a download
once it's started though!

Olly

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


Re: [Tutor] is there any Python code for spatial tessellation?

2005-10-10 Thread Danny Yoo


On Sun, 9 Oct 2005, Shi Mu wrote:

> There are four points with coordinates:
> 2,3;4,9;1,6;3,10.
> How to use Python to draw one perpendicular bisector between (2,3) and (4,9);
> the other perpendicular bisector between (1,6)$BOB(B(3,10);
> then, makes the output like:
> l1 a b c
> l2 a b c
> (Note: l indicates the perpendicular bisector with equation ax + by = c.)
> Plus the intersection coordinates of the two perpendicular bisectors:
> x,y


Hi Shi Mu,

I am having a hard time understanding the relationship between your first
slightly scary question --- spatial tesselation --- with the second, much
more basic question --- perpendicular bisectors.

I'm not getting any good sense of what you know.  And the core of your
question has nothing to do with Python: it's more an elementary
algebra/geometry homework assignment.  Frankly speaking, for the kind of
geometry questions you've asked so far, I believe you should already know
how do that.

You haven't said anything about what you've tried, or what difficulties
you're running into.  As far as I can tell, you haven't put in any effort
into anything except for repeating a problem statement.

See:

http://www.catb.org/~esr/faqs/smart-questions.html#homework

We're not going to do your homework.  Show us what you're really having
trouble with, and if it's programming related, we will try to help point
things out.  But other than that, you really have to do your own work.

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


Re: [Tutor] code improvement for beginner ?

2005-10-10 Thread lmac
Danny Yoo wrote:
> 
> On Sat, 8 Oct 2005, lmac wrote:
> 
> 
>>Ok. Here we go. Wanted to start my page long ago. Now is the right time.
>>
>>http://daderoid.freewebspace24.de/python/python1.html
> 
> 
> Hi lmac,
> 
> I'll pick out some stuff that I see; I'm sure others will be happy to give
> comments too.  I'll try to make sure that all the criticism I give is
> constructive in nature, and if you have questions on any of it, please
> feel free to ask about it.
> 
> 
> I'll concentrate on the imgreg() function first.  The declaration of
> 'images' as a global variable looks a little weird.  I do see that
> 'imgreg' feeds into 'images'.  Not a major issue so far, but you might
> want to see if it's possible to do without the global, and explicitly pass
> in 'images' as another parameter to imgreg.  Globals just bother me on
> principle.  *grin*
> 
The thing is i want to download the images after i got the pages. So i
thought i use an global var that it is in scope at the end of the script.
> 
> You may want to document what 'patt' and 'search' are meant to be.  A
> comment at the top of imgreg, like:
> 
>  """imgreg searches for a pattern 'patt' within the text 'search'.  If
>  a match exists, adds it to the set of images, and returns 1.  Else,
>  returns 0."""
> 
> will help a lot.  Documenting the intent of a function is important,
> because people are forgetful.  No programming language can prevent memory
> loss:  what we should try to do is to compensate for our forgetfulness.
> *grin*
> 
> 
> Looking at pageimgs(): I'm not sure what 't' means in the open statement:
> 
>   f = open(filename, "rt")
> 
> and I think that 't' might be a typo: I'm surprised that Python doesn't
> complain.  Can anyone confirm this?  I think you may have tried to do "r+"
> mode, but even then, you probably don't: you're just reading from the
> file, and don't need to write back to it.
> 
> 
> Looking further into pageimgs(): again, I get nervous about globals.  The
> use of the 'r1' global variable is mysterious.  I had to hunt around to
> figure out what it was it near the middle of the program.
> 
> If anything, I'd recommend naming your global variables with more meaning.
> A name like 'image_regex_patterns' will work better than 'r1'.  Also, it
> looks like pageimgs() is hardcoded to assume 'r1' has three regular
> expressions in it, as it calls imgreg three times for each pattern in r1.
> 
>   if imgreg(r1[0],a) == 1:
>   continue
>   if imgreg(r1[1],a) == 1:
>   continue
>   imgreg(r1[2],a)
> 
> and that looks peculiar.  Because this snippet of code is also
> copy-and-pasted around line 106, it appears to be a specific kind of
> conceptual task that you're doing to register images.
> 
> I think that the use of 'r1' and 'imgreg' should be intrinsically tied.
> I'd recommend revising imgreg() so that when we register images, we don't
> have to worry that we've called it on all the regular expressions in r1.
> That is, let imgreg worry about it, not clients: have imgreg go through
> r1[0:3] by itself.
> 
> 
> If we incorporate these changes, the result might look something like
> this:
> 
> ###
> image_regex_patterns = map(re.compile,
>[r'http://\w+.\w+.\w+/i.+.gif',
> r'http://\w+.\w+.\w+/i.+.jpg',
> r'http://\w+.\w+.\w+/i.+.png'])
This one is very good. I stumbled sometimes over map() but didn't know
how to use it. This makes it easier.
> def imgreg(search):
> """Given a search text, looks for image urls for registration.  If
> a new one can be found, returns 1.  Otherwise, returns 0.
> 
> Possible bug: does not register all images in the search text, but only
> the first new one it can find.
> """
> for r in image_regex_patterns:
> z = r.search(search)
> if z != None:
> x = z.group(0)
> if x not in images:
> images.append(x)
> return 1
> return 0
> ###
The purpose for storing the images in an global list was to download all
images after the pages were saved and don't download an image
again if it was already saved and downloaded on disk. So with
this list i have an good overview.
> 
> Does this make sense?
> 
Yes. Thats good.
> The point of this restructuring is to allow you to add more image types
> without too much pain, since there's no more hardcoded array indexing
> against r1.  It also simplifies to calls to imgreg from:
> 
> if imgreg(r1[0],a) == 1:
> continue
> if imgreg(r1[1],a) == 1:
> continue
> imgreg(r1[2],a)
> 
> to the simpler:
> 
> imgreg(a)
> 
> 
> I think I'll stop there and look at the program again later.  *grin* Hope
> this helps!
> 
> 
I made some significant changes ;-)

http://daderoid.freewebspace24.de/python/python

[Tutor] Please look at my wordFrequency.py

2005-10-10 Thread Dick Moores
Script is at:


Example text file for input:

 
(142 kb)
(from )

Example output in file:

(40 kb)

(Execution took about 30 sec. with my computer.)

I worked on this a LONG time for something I expected to just be an easy 
and possibly useful exercise. Three times I started completely over with 
a new approach. Had a lot of trouble removing exactly the characters I 
didn't want to appear in the output. Wished I knew how to debug other 
than just by using a lot of print statements.

Specifically, I'm hoping for comments on or help with:
1) How to debug. I'm using v2.4, IDLE on Win XP.
2) I've tried to put in remarks that will help most anyone to understand 
what the code is doing. Have I succeeded?
3) No modularization. Couldn't see a reason to do so. Is there one or two?
Specifically, what sections should become modules, if any?
4) Variable names. I gave up on making them self-explanatory. Instead, I 
put in some remarks near the top of the script (lines 6-10) that I hope 
do the job. Do they? In the code, does the "L to newL to L to newL to L" 
kind of thing remain puzzling?

(lines 6-10)
# meaning of short variable names:
#   S is a string
#   c is a character of a string
#   L, F are lists
#   e is an element of a list

5) Ideally, abbreviations that end in a period, such as U.N., e.g., i.e., 
viz. op. cit., Mr. (Am. E.), etc., should not be stripped of their final 
periods (whereas other words that end a sentence SHOULD be stripped). I 
tried making and using a Python list of these, but it was too tough to 
write the code to use it. Any ideas? (I can live very easily without a 
solution to point 5, because if the output shows there are 10 "e.g"s, 
I'll just assume, and I think safely, that there actually are 10 "e.g."s. 
But I am curious, Pythonically.)

Thanks very much in advance, tutors.

Dick Moores
[EMAIL PROTECTED]



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


Re: [Tutor] Listing all of an instances variables

2005-10-10 Thread Kent Johnson
Matt Williams wrote:
> Dear List,
> 
> I'm stuck on trying to write a generic 'report' function:
> 
> Class SomeClass:
>def __init__(self,a,b):
>   self.a = a
>   self.b = b
> 
>def report(self):
>   for i in dir(self):
>   print self.i
> 
> 
> 
> 
> This is where I run into problems: How do I return all of the variables
> in an instance?

If you are trying to show the data attributes of an instance then Karl's 
suggestion of self.__dict__.items() is probably the best. dir() attempts to 
show all accessible attributes, which may be instance variables, class methods, 
class attributes, etc. For example:

 >>> class F(object):
 ...   def __init__(self):
 ... self.data = 1
 ...   def show(self):
 ... print 'data =', self.data
 ...
 >>> f=F()
 >>> dir(f)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', 
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__'
, '__repr__', '__setattr__', '__str__', '__weakref__', 'data', 'show']
 >>> f.__dict__
{'data': 1}

In this case f.__dict__ is probably what you want, not dir(f). However it's 
easy to make an example where __dict__ doesn't contain all accessible 
attributes, for example in a class that defines __slots__:
 >>> class G(object):
 ...   __slots__ = ['data']
 ...   def __init__(self):
 ... self.data = 1
 ...
 >>> g=G()
 >>> g.__dict__
Traceback (most recent call last):
  File "", line 1, in ?
AttributeError: 'G' object has no attribute '__dict__'
 >>> dir(g)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', 
'__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__'
, '__setattr__', '__slots__', '__str__', 'data']
 >>> g.__slots__
['data']
 >>>

A class that defines __getattr__ or __getattribute__ could have attributes that 
don't appear in dir(), __dict__ or __slots__. So in general I think what you 
want is hard. For common cases __dict__ will work.

Kent





























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


Re: [Tutor] handling of tabular data

2005-10-10 Thread Danny Yoo


On Sun, 9 Oct 2005, [ISO-8859-1] Frank Hoffs�mmer wrote:

> thanks for your answers looks like SQL is the ticket for such
> problems... because my data is updated quite often, it feels like an
> overhead for me to store the data persistently, because then I would
> have to manage updates to individual rows and columns right now, I
> simply construct the latest version of my data table from my source
> data, whenever the script is run this is also the reason why I am using
> python in the first place: to extract the data of interest from other
> (non-SQL) sources and then create that data table as the basis for my
> queries. does python have a simple build in SQL engine that would allow
> for the creation of a throw-away, in-memory table that I could then
> query with SQL?

Hi Frank,

Try looking at:

http://www.sqlite.org/

It should be fairly easy to install, and comes with Python bindings:

http://initd.org/tracker/pysqlite

The tutorial shows how to build an in-memory database (Example 2 of
Chapter 2):

http://initd.org/pub/software/pysqlite/doc/usage-guide.html


Best of wishes!

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


Re: [Tutor] New image with Tkinter?

2005-10-10 Thread Michael Lange
On Mon, 10 Oct 2005 11:36:22 -0600
Joseph Quigley <[EMAIL PROTECTED]> wrote:

> Hi,
> I've written an image reader that uses the PIL module.
> I have a variable that uses os.listdir('mydir') to make a list of all the
> pictures in the folder... here's what I'm talking about:
(...)
> The image won't refresh when I click on 'Next'! Any help would be
> appreciated.
> Joe
> 

Hi Joe,

the image won't refresh until you explicitely tell Tk to do so, so you would 
have to do some more
in newPic(); btw, it looks overly complicated to me to use a "Data" class where 
a simple variable
will do the trick, so I would suggest to change the code like this:

pics = os.listdir(imgDir)
pics.remove('license.txt')
pics.remove('gacor.py')
print "There are %s saved images in the image folder." % len(pics)
pic = 0

root = Tk()
root.title("GaCoR Image Browser")
app = Frame(root)
app.grid()

imgPrep = ImageTk.PhotoImage(file=os.path.join(imgDir, pics[pic]))
imgShow = Label(app, image=imgPrep).grid()
info = Label(app, text="Displaying %s" % pics[Data.pic])
info.grid()

def newPic():
global pic
pic = pic + 1
imgPrep.configure(file=os.path.join(imgDir, pics[pic]))

Button(app, text="Next Image", command=newPic).grid()
Button(app, text="Close", command=quitProg).grid()
app.mainloop()

I hope this helps

Michael

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


Re: [Tutor] New image with Tkinter?

2005-10-10 Thread Alan Gauld
def newPic():
Data.pic += 1

def Dsp_Image():
   ..
   imgPrep = ImageTk.PhotoImage(img)
   imgShow = Label(app, image=imgPrep).grid()
   ...
   next = Button(app, text="Next Image", command=newPic).grid()
   app.mainloop()

Dsp_Image()

> The image won't refresh when I click on 'Next'!
> Any help would be appreciated.

You never ask it to refresh. The GUI displays the image that you pass to it,
you then call newPic which only increments the data counter but does
nothing to change the image being displayed.

You need to make your Photoimage object visible to newPic
(by making it global say?) and then in newPic acyually update
the image being displayed. visible

BTW you seem to be callingmainloop within the DSpImage function,
thats probably a bad idea, mainloop should only be called once
which means you could never call DspImage a second time should
you ever need to...

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] code improvement for beginner ?

2005-10-10 Thread Kent Johnson
Roel Schroeven wrote:
> Danny Yoo wrote:
> 
>>Looking at pageimgs(): I'm not sure what 't' means in the open statement:
>>
>>  f = open(filename, "rt")
> 
> It's not a typo. 't' opens the file in text mode. 

Are you sure? Is that documented anywhere?

Text mode is the default, you have to specify the 'b' if you want binary mode. 
And open() seems to accept any mode quite happily:

 >>> f=open('build.xml', 'rt')
 >>> f

 >>> f.close()
 >>> f=open('build.xml', 'rabcd')
 >>> f


Kent

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


Re: [Tutor] Please look at my wordFrequency.py

2005-10-10 Thread Alan Gauld
> didn't want to appear in the output. Wished I knew how to debug other than 
> just by using a lot of print statements.

When you need to get to the nitty gritty you need to use a realdebugger.

The best GUI one is probably the one in Pythonwin.
The standard one is PDB and the tutorial on it is OK.

I find the most useful approach is to set a breakpoint on the function
I'm interested in (or a control point within that - a loop or significant
branch say) then set a watch point on any variables I'm interested in.
Then step through the function remembering that 'step' will step into
any functions called by the function under test and 'next' will step
over the called functions, ie:

def foo():
   x = aFunction()
   x = another()
   return x

to step through foo will require 3 'next's but could involve dozens of
'steps' depending on how long 'aFunction' and 'another' are...

> 1) How to debug. I'm using v2.4, IDLE on Win XP.

IDLE's debugger isn't pretty but it does work, try it.
Use the context menus and mouse to set a break point,
then just step through using the step and next buttons.

IDLE debugger displays the variable values as you go...

If you can't get it to work you know where to ask for help :-)

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





> 2) I've tried to put in remarks that will help most anyone to understand 
> what the code is doing. Have I succeeded?
> 3) No modularization. Couldn't see a reason to do so. Is there one or two?
> Specifically, what sections should become modules, if any?
> 4) Variable names. I gave up on making them self-explanatory. Instead, I 
> put in some remarks near the top of the script (lines 6-10) that I hope do 
> the job. Do they? In the code, does the "L to newL to L to newL to L" kind 
> of thing remain puzzling?
>
> (lines 6-10)
> # meaning of short variable names:
> #   S is a string
> #   c is a character of a string
> #   L, F are lists
> #   e is an element of a list
>
> 5) Ideally, abbreviations that end in a period, such as U.N., e.g., i.e., 
> viz. op. cit., Mr. (Am. E.), etc., should not be stripped of their final 
> periods (whereas other words that end a sentence SHOULD be stripped). I 
> tried making and using a Python list of these, but it was too tough to 
> write the code to use it. Any ideas? (I can live very easily without a 
> solution to point 5, because if the output shows there are 10 "e.g"s, I'll 
> just assume, and I think safely, that there actually are 10 "e.g."s. But I 
> am curious, Pythonically.)
>
> Thanks very much in advance, tutors.
>
> Dick Moores
> [EMAIL PROTECTED]
>
>
>
>
> 

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


[Tutor] Undocumented 't' mode flag for open() [Was: Re: code improvement for beginner ?]

2005-10-10 Thread Danny Yoo


On Sun, 9 Oct 2005, Roel Schroeven wrote:

> Danny Yoo wrote:
> > Looking at pageimgs(): I'm not sure what 't' means in the open statement:
> >
> > f = open(filename, "rt")
> >
> > and I think that 't' might be a typo: I'm surprised that Python doesn't
> > complain.  Can anyone confirm this?  I think you may have tried to do "r+"
> > mode, but even then, you probably don't: you're just reading from the
> > file, and don't need to write back to it.
>
> It's not a typo. 't' opens the file in text mode. It doesn't make any
> difference on Unix, but it does on Windows.

Hi Roel,

Ah, ok, thank you!  Even so, I don't see this 't' option documented in the
reference docs though, although now I see a reference to this on:

http://mail.python.org/pipermail/python-list/2004-September/241444.html

This is odd that it's not documented in the reference manual.

>From the thread on python-list, it sounded like they left it at depending
on the user to know that 't' is a Windows-specific thing.  But that just
feels off to me: Python's open(), I think, should at least generalize it
so that 't' is explicitely defined in the documentation.


... Wow.  open() actually does seem like a VERY thin wrapper around the
underlying platforms fopen().  For example, on my Linux 2.6 system:

##
>>> open("/etc/passwd", "rfoobar!")

##

So if the underlying fopen() is permissive, we don't see errors.  But I
really think Python should be more strict than the platform.  I'll put it
on my TODO to check to see if this is registered as a bug in SF.


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


Re: [Tutor] handling of tabular data

2005-10-10 Thread Alan Gauld
> overhead for me to store the data persistently, because then I would
> have to manage updates to individual rows and columns right now, I
> simply construct the latest version of my data table from my source
> data, whenever the script is run this is also the reason why I am using
> python in the first place: to extract the data of interest from other
> (non-SQL) sources and then create that data table as the basis for my
> queries. does python have a simple build in SQL engine that would allow
> for the creation of a throw-away, in-memory table that I could then
> query with SQL?

You could look at the database topic in my tutor which both gives 
an intro to SQL and how to use it from Python with the sqlite database.

It really is pretty easy once you adjust to the SQL mindset.

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] Please look at my wordFrequency.py

2005-10-10 Thread R. Alan Monroe
> IDLE's debugger isn't pretty but it does work, try it.
> Use the context menus and mouse to set a break point,
> then just step through using the step and next buttons.

Has anyone else experienced this? I tried debugging in IDLE with the
source checkbox enabled, but when the sourcecode window lost focus as
I clicked back on the debugger window to hit the step button, the
highlight indicating the current line of source was no longer visible.
It's probably a fluke of Windows XP's GUI theme system, but I was
curious if anyone else had seen it.

Alan

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


Re: [Tutor] Regex help

2005-10-10 Thread Danny Yoo


On Mon, 10 Oct 2005, Bill Burns wrote:

> I'm looking to get the size (width, length) of a PDF file.


Hi Bill,

Just as a side note: you may want to look into using the 'pdfinfo' utility
that comes as part of the xpdf package:

http://www.foolabs.com/xpdf/

For example:

###
[EMAIL PROTECTED] ~]$ pdfinfo 05-lexparse.pdf
Producer:   Acrobat Distiller Command 3.0 for Solaris 2.3 and later
(SPARC)
CreationDate:   Tue Jul  1 18:36:35 1913
Tagged: no
Pages:  12
Encrypted:  no
Page size:  612 x 792 pts (letter)
File size:  191874 bytes
Optimized:  no
PDF version:1.2
###



> Every pdf file has a 'tag' (in the file) that looks similar to this
>
> Example #1
> MediaBox [0 0 612 792]
>
> or this
>
> Example #2
> MediaBox [ 0 0 612 792 ]
>
> I figured a regex might be a good way to get this data but the
> whitespace (or no whitespace) after the left bracket has me stumped.


I think you might want to look for the whitespace metacharacter '\s'.
Also, you can consider using '*' to qualify a previous pattern: it stands
for "zero or more of the pattern."  For example:

#
>>> re.search("a*b", "aab")
<_sre.SRE_Match object at 0x403ae250>
>>> re.search("a*b", "ab")
<_sre.SRE_Match object at 0x403ae138>
>>> re.search("a*b", "b")
<_sre.SRE_Match object at 0x403ae250>
>>> re.search("a*b", "")
>>>
#

In comparison:


#
>>> re.search("a+b", "aab")
<_sre.SRE_Match object at 0x403ae138>
>>> re.search("a+b", "ab")
<_sre.SRE_Match object at 0x403ae250>
>>> re.search("a+b", "b")
>>>
#


Good luck to you!

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


Re: [Tutor] Please look at my wordFrequency.py

2005-10-10 Thread John Fouhy
On 11/10/05, Dick Moores <[EMAIL PROTECTED]> wrote:
> I worked on this a LONG time for something I expected to just be an easy
> and possibly useful exercise. Three times I started completely over with
> a new approach. Had a lot of trouble removing exactly the characters I
> didn't want to appear in the output. Wished I knew how to debug other
> than just by using a lot of print statements.

I like pdb to debug, but I don't know how that works with IDLE.. (I
don't use IDLE)

Some comments:


textAsString = input.read()

S = ""
for c in textAsString:
if c == "\n":
S += ' '
else:
S += c


You could write this more concisely as:

S = textAsString.replace('\n', ' ')


# At this point, each element ("word" in code below) of L is
# a string containing a real word such as "dog",
# where "dog" may be prefixed and/or suffixed by strings of
# non-alphanumeric characters. So, for example, word could be "'dog?!".
# The following code first strips these prefixed or suffixed non-alphanumeric
# characters and then finds any words with dashes ("--") or forward
slashes ("/"),
# such as in "and/or". These then become 2 or more words without the
# dashes or slashes.


What about using regular expressions?

re.sub('\W+', ' ') will replace all non-alphanumeric characters with a
single ' '.  By the looks of things, the only difference is that if
you had something like 'foo.bar' or 'foo&bar', your code would leave
that as one word, whereas using the regex would convert it into two
words.

If you want to keep the meaning of your code intact, you could still
use a regex to do it.  Something like (untested)
re.sub('\b\W+|\W+\b|-+|/+', ' ') might work.


# Remove all empty elements of L, if any
while "" in L:
L.remove("")

for e in saveRemovedForLaterL:
L.append(e)

F = []

for word in L:
k = L.count(word)
if (k,word) not in F:
F.append((k,word))


There are a lot of hidden loops in here:

1. '' in L
This will look at every element of L, until it finds "" or it gets to the end.
2. L.count(word)
This will also look at every element of L.

If you combine your loops into one, you should be able to save a lot of time.

eg:

for e in saveRemovedForLaterL:
L.append(e)

counts = {}
for word in L:
if not word:  # This skips empty words.
continue
try:
counts[word] += 1
except KeyError:
counts[word] = 1
F = [(count, word) for word, count in counts.iteritems()]

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


Re: [Tutor] Please look at my wordFrequency.py

2005-10-10 Thread Alan Gauld
> Has anyone else experienced this? I tried debugging in IDLE with the
> source checkbox enabled, but when the sourcecode window lost focus as
> I clicked back on the debugger window to hit the step button, the
> highlight indicating the current line of source was no longer visible.
> It's probably a fluke of Windows XP's GUI theme system, but I was
> curious if anyone else had seen it.

Yes I get that too, I just watch the current source line being displayed 
in the debugger window.

But frankly IDLEs debugger sucks! I prefer to use text mode pdb 
for most things!! Which is a pretty bad inditement...

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


Re: [Tutor] Please look at my wordFrequency.py

2005-10-10 Thread Kent Johnson
Dick Moores wrote:
 > Specifically, I'm hoping for comments on or help with:
> 1) How to debug. I'm using v2.4, IDLE on Win XP.

I have been using winpdb recently, it is a pretty decent standalone Python 
debugger for Windows. Start it with the -t switch so you don't need the Python 
Cryptographic Toolkit.
http://www.digitalpeers.com/pythondebugger/

Kent

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


Re: [Tutor] Please look at my wordFrequency.py

2005-10-10 Thread Danny Yoo

> 3) No modularization. Couldn't see a reason to do so. Is there one or
> two? Specifically, what sections should become modules, if any?


Hi Dick,

My recommendation is to practice using and writing functions.  For
example, this block here is a good candidate to box off as a named
function:

###
F = []
for word in L:
k = L.count(word)
if (k,word) not in F:
F.append((k,word))
# F is a list of duples (k, word), where k is the frequency of word
F.sort()
###


That block can be treated as a function that takes in a list of words, and
returns a list of duples.

##
def doFrequencyCalculation(L):
# F is a list of duples (k, word), where k is the frequency of word
F = []
for word in L:
k = L.count(word)
if (k,word) not in F:
F.append((k,word))
F.sort()
return F
##

Here, we explicitly say that frequence calculation depends on having a
list L of words, and that the result should be a list of duples.

Functions allow us to "scope" variables into different kinds of roles.
We'd say in doFrequenceCalculation that 'word' and 'k' are "temporary"
variables.  If we write programs using functions, we can express that
"temporary"  role as a real part of the program.

As it stands, in the original code, we have to watch that 'word' or 'k'
isn't being used by anyone else: like all the other variables in your
program, they're all global and given equal status.  You're using
lowercase as an informal way to remind yourself that those variables are
meant to be temporary, but that approach can be error prone.


Conceptually, your program appears to work in four stages:

 1.  Read lines of a file
 2.  Extract list of words from the lines
 3.  Do histogram frequency count on those lines
 4.  Report frequency results

and it would be great if we could see this program flow as an explicit
sequence of function calls:

##
## Pseudocode
def program():
L = readLinesInFile()
W = extractWordsFromLines(L)
F = doFrequencyCalculation(W)
reportFrequencyResults(F)
##


This allows one to see the overall program plan without having to read the
whole program.  It also allows you to do some experimentation later on by
letting you plugging in different functions in the overall program() plan.

As a concrete example: once we have a program in this form, it's easy to
swap in a different algorithm for doing frequency calculation:

##
def program():
L = readLinesInFile()
W = extractWordsFromLines(L)
## F = doFrequencyCalculation(W)
F = doFrequencyCalculationWithDifferentAlgorithm(W)
reportFrequencyResults(F)
##


Does this make sense?  Please feel free to ask more questions about this.

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


Re: [Tutor] Tutor Digest, Vol 20, Issue 37

2005-10-10 Thread Joseph Quigley
Michael Wrote:

>Hi Joe,
>
>the image won't refresh until you explicitely tell Tk to do so, so you would 
>have to do some more
>in newPic(); btw, it looks overly complicated to me to use a "Data" class 
>where a simple variable
>will do the trick, so I would suggest to change the code like this:
>
>pics = os.listdir(imgDir)
>pics.remove('license.txt')
>pics.remove('gacor.py')
>print "There are %s saved images in the image folder." % len(pics)
>pic = 0
>
>root = Tk()
>root.title("GaCoR Image Browser")
>app = Frame(root)
>app.grid()
>
>imgPrep = ImageTk.PhotoImage(file=os.path.join(imgDir, pics[pic]))
>imgShow = Label(app, image=imgPrep).grid()
>info = Label(app, text="Displaying %s" % pics[Data.pic])
>info.grid()
>
>def newPic():
>global pic
>pic = pic + 1
>imgPrep.configure(file=os.path.join(imgDir, pics[pic]))
>
>Button(app, text="Next Image", command=newPic).grid()
>Button(app, text="Close", command=quitProg).grid()
>app.mainloop()
>
>I hope this helps
>
>Michael
>
>
>  
>
Hi,

Unfortunately I get this error:

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.3/lib-tk/Tkinter.py", line 1345, in __call__
return self.func(*args)
File "Programming/Gacor/tmp.py", line 83, in newPic
imgPrep.configure(file=os.path.join(imgDir, pics[pic]))
AttributeError: PhotoImage instance has no attribute 'configure'

I think I should mention that I'm completely in the dark when it comes 
to GUI programming (and I don't have much time to learn it... or do I?)

Thanks for the help so far,
Joe
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 20, Issue 37

2005-10-10 Thread John Fouhy
On 11/10/05, Joseph Quigley <[EMAIL PROTECTED]> wrote:
> I think I should mention that I'm completely in the dark when it comes
> to GUI programming (and I don't have much time to learn it... or do I?)

You're likely to have more success with your programming if you do
learn what you're doing :-)

I recommend Thinking in Tkinter: http://www.ferg.org/thinking_in_tkinter/

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


Re: [Tutor] code improvement for beginner ?

2005-10-10 Thread Danny Yoo


> > The point of this restructuring is to allow you to add more image
> > types without too much pain, since there's no more hardcoded array
> > indexing against r1.  It also simplifies to calls to imgreg from:
> >
> > if imgreg(r1[0],a) == 1:
> > continue
> > if imgreg(r1[1],a) == 1:
> > continue
> > imgreg(r1[2],a)
> >
> > to the simpler:
> >
> > imgreg(a)
>
> Yes. Thats good.


Check your code: pageimgs() is calling imgreg three times.



> The problem with downloading the images is this:
>
> -
> http://images.nfl.com/images/globalnav-shadow-gray.gif
> Traceback (most recent call last):
>   File "/home/internet/bin/nflgrab.py", line 167, in ?
> urllib.urlretrieve(img,img[f:])
>   File "/usr/lib/python2.3/urllib.py", line 83, in urlretrieve
> return _urlopener.retrieve(url, filename, reporthook, data)
>   File "/usr/lib/python2.3/urllib.py", line 216, in retrieve
> tfp = open(filename, 'wb')
> IOError: [Errno 13] Permission denied: '/globalnav-shadow-gray.gif'

One bug is that Python is trying to write those image files to the root
directory.  Your operating system's file system is saying that it won't
allow you to write files to that location.

urllib.urlretrieve saves those files with the path given in the second
parameter:

urllib.urlretrieve(img, img[f:])
^^^

You may want to change this so that it stores those files in a particular
directory.  Something like:

urllib.urlretrieve(img, os.path.join("/tmp",
 img[f+1:]))

may work better.  The main idea is that you should explicitly control
where the files are being downloaded to.


Another bug: you will probably still run into problems because 'img' must
be a URL, and each 'img' is instead a line that contains a URL.  The
difference is between having a string like:

http://python.org.

and:

This line contains a url to the python web site: http://python.org.

and images, as far as I can tell, is storing a list of the lines, not the
image urls.  So you may want to make appropriate changes to imgreg() so
that it maintains a list of image urls.

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


Re: [Tutor] Regex help

2005-10-10 Thread Bill Burns
> On Mon, 10 Oct 2005, Bill Burns wrote:
> 
> 
>>I'm looking to get the size (width, length) of a PDF file.
> 


> Hi Bill,
> 
> Just as a side note: you may want to look into using the 'pdfinfo' utility
> that comes as part of the xpdf package:
> 
> http://www.foolabs.com/xpdf/
> 
> For example:
> 
> ###
> [EMAIL PROTECTED] ~]$ pdfinfo 05-lexparse.pdf
> Producer:   Acrobat Distiller Command 3.0 for Solaris 2.3 and later
> (SPARC)
> CreationDate:   Tue Jul  1 18:36:35 1913
> Tagged: no
> Pages:  12
> Encrypted:  no
> Page size:  612 x 792 pts (letter)
> File size:  191874 bytes
> Optimized:  no
> PDF version:1.2
> ###
> 
> 
> 
> 
>>Every pdf file has a 'tag' (in the file) that looks similar to this
>>
>>Example #1
>>MediaBox [0 0 612 792]
>>
>>or this
>>
>>Example #2
>>MediaBox [ 0 0 612 792 ]
>>
>>I figured a regex might be a good way to get this data but the
>>whitespace (or no whitespace) after the left bracket has me stumped.
> 
> 
> 
> I think you might want to look for the whitespace metacharacter '\s'.
> Also, you can consider using '*' to qualify a previous pattern: it stands
> for "zero or more of the pattern."  For example:
> 
> #
> 
re.search("a*b", "aab")
> 
> <_sre.SRE_Match object at 0x403ae250>
> 
re.search("a*b", "ab")
> 
> <_sre.SRE_Match object at 0x403ae138>
> 
re.search("a*b", "b")
> 
> <_sre.SRE_Match object at 0x403ae250>
> 
re.search("a*b", "")

> 
> #
> 
> In comparison:
> 
> 
> #
> 
re.search("a+b", "aab")
> 
> <_sre.SRE_Match object at 0x403ae138>
> 
re.search("a+b", "ab")
> 
> <_sre.SRE_Match object at 0x403ae250>
> 
re.search("a+b", "b")

> 
> #
> 

Danny,

Thank you for the information.

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


Re: [Tutor] AttributeError: 'str' object has no attribute 'geturl'

2005-10-10 Thread Javier Ruere
Joseph Quigley wrote:
> Ok, new version (sorry to bug you).
> This time I've edited the program so that you can only download todays 
> (it won't work for any other date). Now I can connect to the server but 
> it sticks on "Downloading image!"
> 
> Thanks for your prevoius help.
> Joe

  You are welcome. This time I would like to help you but the code is 
incomplete (import error for Image) and I have never used urllib2 so I don't 
really know what to do.
  Again, try to debug it with pdb. Place "import pdb; pdb.set_trace()" where 
you want the break point and see what's going on.

Javier

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