[Tutor] Still searching in html files

Kent Johnson kent37 at tds.net
Thu Jan 21 13:59:51 CET 2010


On Thu, Jan 21, 2010 at 4:03 AM, Paul Melvin
<paul at assured-networks.co.uk> wrote:

> The code is at http://python.codepad.org/S1ul2bh7 and the bit I would like
> some advice on is how to get the sorted data and where to put it.

Generally the code seems a bit disorganized, consider breaking it into
functions. There is a fair amount of deadwood you could delete. Also
it will be more readable if you move the initialization of variables
closer to where they are used.

> Currently I use a temporary list to store the data before writing it to
> another, is this OK? (lines 45 onwards)

for chunk in u.split(searchText):
    temp.append(chunk)

u.split() already returns a sequence, there is no need for this loop.
You could just write
temp = u.split(searchText)
though you might think of a more descriptive name.

for item in temp[1:]:
    temp2 = []
    temp2.append(findLink(item))
    temp2.append(findTitle(item))
    temp2.append(findSize(item))
    cleaned.append(temp2)

This could be written more concisely without temp2:
  cleaned.append( [ findLink(item), findTitle(item), findSize(item) ] )

or even put the whole thing into a single list comprehension (though
this is perhaps too dense):
cleaned = [ [ findLink(item), findTitle(item), findSize(item) ] for
item in u.split(searchText)[1:] ]

I think I would write it as
items = u.split(searchText)[1:]
cleaned = [ [ findLink(item), findTitle(item), findSize(item) ] for
item in items ]

> I want to expand the program to give some output which the user can interact
> with, e.g. ‘which of this information do you want me to get?’ and the user
> would choose.  Does anyone have any tips on this? Can I just use things like
> raw_input and lists?

That's fine, just separate out the data acquisition from the user
input and processing.

Kent


More information about the Tutor mailing list