Re: How to get the previous line in a file?

2007-03-16 Thread dhn
On Mar 16, 5:10 pm, [EMAIL PROTECTED] wrote:
> On Mar 16, 3:51 pm, Shane Geiger <[EMAIL PROTECTED]> wrote:
>
>
>
> > lines = open('/tmp/foo.py',
> > "r").read().splitlines()
>
> > previous_line =
> > ''
>
> > for line in
> > lines:
>
> > if "foo" in
> > line:
>
> > print "found foo in the current line.  The previous line is:  "
> > +
> > previous_line
>
> > previous_line =
> > line
>
> > Qilong Ren wrote:
> > > Hi,all
>
> > > I am new to this list. And I am glade I am here.
> > > I have a question. I need to do some text processing. I need to read
> > > from a file line by line. If some line is met with some condition, the
> > > previous line needs some modification. How to get the info of the
> > > previous line?
>
> > > Thanks!
> > > Qilong
>
> > > 
> > > Never miss an email again!
> > > Yahoo! Toolbar
> > > 
> > > alerts you the instant new Mail arrives. Check it out.
> > > 
>
> > --
> > Shane Geiger
> > IT Director
> > National Council on Economic Education
> > [EMAIL PROTECTED]  |  402-438-8958  |  http://www.ncee.net
>
> > Leading the Campaign for Economic and Financial Literacy
>
> >  sgeiger.vcf
> > 1KDownload
>
> Hi,
>
> You should be able to use the file object's seek and tell methods. Or,
> since you're using a list, you could use a counter and when the text
> is found, you can print the previous line by subtracting one from the
> counter.
>
> counter = 0
> for line in lines:
>if 'foo' == line:
>   print 'found line. the previous line is: ' %s (lines[counter-1])
>counter += 1
>
> # Or something like that. Experiment!
>
> Mike

A more pythonic solution makes use of enumerate().

for number, line in enumerate(lines):
if line == 'foo':
print 'found line. the previous line is: %s' % lines[number -
1]

-- 
http://mail.python.org/mailman/listinfo/python-list


Point-feature labeling in matplotlib

2006-05-17 Thread dhn
I am trying to automate the generation of scatterplots in matplotlib.
The problem of where to put the labels is known as 'point-feature
labeling' (a main application is in cartography, where cities and such
are known as 'point features'). In general the problem is not easy.
Some references:

http://www.merl.com/reports/docs/TR96-04.pdf
http://www.eecs.harvard.edu/shieber/Biblio/Papers/tog-final.pdf

If anyone knows of any canned python algorithms to do this, please
direct me to them. If not, I will need to implement one.

My problem is not that hard, as I will not have more than 20 labels to
place. As I see it, the steps that need to be taken are:

1. locate the points
2. compute the size of the text boxes for the labels
3. minimize a loss function reflecting one of the algorithms discussed
in the papers cited to determine the optimal placement of the labels

At the moment I was wondering if any matplotlib experts (I am a bit of
a novice) could suggest a good way to accomplish 2. I have tried to do
something like

In [1]: from pylab import *
In [2]: plot([0.5],[0.5],'bo')
In [3]: axis([0,1,0,1])
In [4]: t = text(0.50,0.51,'Test')
In [5]: t._get_layout(t._renderer)[1][0][1]
Out[5]: (20.0, 6.0)

Then I have tried to use t._transform to convert these dimensions back
into axis coordinates. Unfortunately this method seems to require that
the plot be rendered to be able to get the dimensions, which is not too
efficient. I'm sure there is a better way to do this; does anyone have
any ideas?

-- 
http://mail.python.org/mailman/listinfo/python-list