Thursday, February 4, 2010

comp.lang.python - 25 new messages in 12 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* Building a multiline string - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/cb131609c7b065a9?hl=en
* Repeat an exception - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/6e7a96024dce9a2b?hl=en
* "australia scholarship" "australia scholarships 2010" "canada scholarships"
"canada scholarships for international students" "usa scholarships" "uno
scholarships" on http://escholarship-positions.blogspot.com/ - 1 messages, 1
author
http://groups.google.com/group/comp.lang.python/t/c4c1478b8938cf80?hl=en
* Common area of circles - 6 messages, 5 authors
http://groups.google.com/group/comp.lang.python/t/8cf2115734d16002?hl=en
* YAML (was: Python and Ruby) - 4 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/dfe4f6c60032755e?hl=en
* Refreshing of urllib.urlopen() - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/9f0ed7d1d836656a?hl=en
* read a process output with subprocess.Popen - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/ff58d1f8efb0bd23?hl=en
* Passing parameters in URL - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/52695ffb32fef94b?hl=en
* Selenium/SauceLabs OpenSpace at Pycon - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/259ee71b81205891?hl=en
* "germany scholarships" "europe scholarships" "europe scholarship 2010" "
spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.
com/ - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/6f254f8a7e6c4e77?hl=en
* how to run part of my python code as root - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/02fa0b92a095f503?hl=en
* Problem with __init__.py in Python3.1 - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/5740b72706867444?hl=en

==============================================================================
TOPIC: Building a multiline string
http://groups.google.com/group/comp.lang.python/t/cb131609c7b065a9?hl=en
==============================================================================

== 1 of 3 ==
Date: Thurs, Feb 4 2010 4:31 am
From: Steve Holden


lallous wrote:
> Hello
>
> Maybe that's already documented, but it seems the parser accepts to
> build a long string w/o really using the first method:
>
> # Method1
> x = "line1" + \ # cannot use comments!
> "line2"+ \
> "line3"
>
> and instead using a list with one element like this:
>
> # Method2
> x = [
> "line1" # can use comments
> "line2"
> "line3"
> ][0]
>
> Or:
> # Method3
> x = (
> "line1" # can use comments
> "line2"
> "line3"
> )
>
> (Not that I don't want new lines in the strings)
>
> Now should I be using method 2 or 3 in production code?
>
I should have thought it was pretty obvious that method 2 creates a list
and then performs an indexing operation on it. These are completely
unnecessary operations, which are avoided in method 3 which is a simple
parenthesised expression.

So why anyone would want to adopt method 2, which is also mess clear as
source code, is beyond me.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

== 2 of 3 ==
Date: Thurs, Feb 4 2010 5:23 am
From: Marco Mariani


On 02/04/2010 12:34 PM, lallous wrote:

> Now should I be using method 2 or 3 in production code?

Another way... depending on what you are using the string for, of
course. If it's an HTML/XML/SQL/whatever piece of code:

>>>> from textwrap import dedent
>>>> sql = dedent("""
> ... SELECT *
> ... FROM table
> ... WHERE foo=bar
> ... """)
>>>>
>>>> print sql
>
> SELECT *
> FROM table
> WHERE foo=bar
>


And if you don't want the starting/ending newlines:

>>>> sql = dedent("""\
> ... SELECT *
> ... FROM table
> ... WHERE foo=bar\
> ... """)
>>>>
>>>> print sql
> SELECT *
> FROM table
> WHERE foo=bar
>>>>

I use this sometimes to keep both python and the embedded code readable
while preserving indentation.


== 3 of 3 ==
Date: Thurs, Feb 4 2010 10:23 am
From: Duncan Booth


lallous <lallous@lgwm.org> wrote:

> Now should I be using method 2 or 3 in production code?

Also Method1a:

# Method1a
x = ("line1" + # can use comments!
"line2"+
"line3")

Use Method1a or Method3 as you prefer: either of these generates a single
string constant. Method2 is dumb.

==============================================================================
TOPIC: Repeat an exception
http://groups.google.com/group/comp.lang.python/t/6e7a96024dce9a2b?hl=en
==============================================================================

== 1 of 2 ==
Date: Thurs, Feb 4 2010 4:59 am
From: Jean-Michel Pichavant


MRAB wrote:
> In other words:
>
> for attempt in range(2):
> try:
> spanish_field = translate(english_field, lang_to='es',
> lang_from='en')
> break
> except TranslationError:
> pass
> else:
> # Didn't break out of the loop, therefore not successful.
> print "Translation failed"

What the hell is this 'for else' loop !! :D First time I see this
statement for years.
I'd never thought I'd still learn something that basic.

My first impression is that the mechansim is not that obvious. MRAB's
need for a comment tends to confirm this.
I'll try to remember that anyway. Nice addition.

JM


== 2 of 2 ==
Date: Thurs, Feb 4 2010 8:36 am
From: MRAB


Jean-Michel Pichavant wrote:
> MRAB wrote:
>> In other words:
>>
>> for attempt in range(2):
>> try:
>> spanish_field = translate(english_field, lang_to='es',
>> lang_from='en')
>> break
>> except TranslationError:
>> pass
>> else:
>> # Didn't break out of the loop, therefore not successful.
>> print "Translation failed"
>
> What the hell is this 'for else' loop !! :D First time I see this
> statement for years.
> I'd never thought I'd still learn something that basic.
>
> My first impression is that the mechansim is not that obvious. MRAB's
> need for a comment tends to confirm this.
> I'll try to remember that anyway. Nice addition.
>
The comment is there for the OP. _I_ don't need it! :-)

==============================================================================
TOPIC: "australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/

http://groups.google.com/group/comp.lang.python/t/c4c1478b8938cf80?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Feb 4 2010 5:34 am
From: Naeem


"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/
"australia scholarship" "australia scholarships 2010" "canada
scholarships" "canada scholarships for international students" "usa
scholarships" "uno scholarships" on http://escholarship-positions.blogspot.com/

==============================================================================
TOPIC: Common area of circles
http://groups.google.com/group/comp.lang.python/t/8cf2115734d16002?hl=en
==============================================================================

== 1 of 6 ==
Date: Thurs, Feb 4 2010 6:01 am
From: Terry Reedy


On 2/4/2010 7:05 AM, Shashwat Anand wrote:
> I want to calculate areas.
> like for two circles (0, 0) and (0, 1) : the output is '1.228370'
>
> similarly my aim is to take 'n' co-ordinates, all of radius '1' and
> calculate the area common to all.
> The best I got was monte-carlo methods which is inefficient. Is there
> any other approach possible.

There is a method for calculating the area of a polygon by traversing
its boundary. I forget the formula, but you should be able to find it.
So *if* you can find the arcs that bound the area, you can approximate
each by a series of short lines and apply the polygon method.

Terry Jan Reedy

== 2 of 6 ==
Date: Thurs, Feb 4 2010 8:29 am
From: Gerard Flanagan

> On 2/4/2010 7:05 AM, Shashwat Anand wrote:
>
> I want to calculate areas.
> like for two circles (0, 0) and (0, 1) : the output is '1.228370'
>
> similarly my aim is to take 'n' co-ordinates, all of radius '1' and
> calculate the area common to all.
> The best I got was monte-carlo methods which is inefficient. Is
> there
> any other approach possible.
>
>

A brute force approach - create a grid of small squares and calculate
which squares are in all circles. I don't know whether it is any better
than monte-carlo: for two circles, delta=0.001 takes about a minute and
delta=0.0001 is still running after 30 minutes :-)

1.22
1.2281
1.22834799999


------------------------------------------------------------------
import math

class Circle:

def __init__(self, x, y, r=1):
self.x = float(x)
self.y = float(y)
self.r = float(r)

def contains(self, a, b):
return math.sqrt((self.x-a)**2 + (self.y-b)**2) <= self.r

class Grid:

def __init__(self, circles):
self.X1 = min(c.x-c.r for c in circles)
self.Y1 = max(c.y+c.r for c in circles)
self.X2 = max(c.x+c.r for c in circles)
self.Y2 = min(c.y-c.r for c in circles)
self.circles = circles

def iter_boxes(self, delta):
X2 = self.X2
Y2 = self.Y2
a = self.X1
while a < X2:
a += delta
b = self.Y1
while b > Y2:
b -= delta
#print a, b
yield a, b

def intersection(self, delta=0.1):
S = 0
s = delta**2 #box area
circles = self.circles
for a, b in self.iter_boxes(delta):
if all(c.contains(a, b) for c in circles):
S += s
return S


c = Circle(0, 1)

assert c.contains(0, 1)
assert c.contains(0, 1.5)
assert c.contains(math.sqrt(2)/2, math.sqrt(2)/2)
assert c.contains(0, 2)
assert not c.contains(0, 2.01)
assert not c.contains(0, 2.1)
assert not c.contains(0, 3)

circles = [
Circle(0, 0),
Circle(0, 1),
]

g = Grid(circles)

print '-'*30
print g.intersection()
print g.intersection(0.01)
print g.intersection(0.001)
print g.intersection(0.0001)

------------------------------------------------------------------

== 3 of 6 ==
Date: Thurs, Feb 4 2010 8:46 am
From: Gary Herron


Gerard Flanagan wrote:
>
>> On 2/4/2010 7:05 AM, Shashwat Anand wrote:
>>
>> I want to calculate areas.
>> like for two circles (0, 0) and (0, 1) : the output is
>> '1.228370'
>>
>> similarly my aim is to take 'n' co-ordinates, all of radius
>> '1' and
>> calculate the area common to all.
>> The best I got was monte-carlo methods which is inefficient. Is
>> there
>> any other approach possible.
>>
>>
>
> A brute force approach - create a grid of small squares and calculate
> which squares are in all circles. I don't know whether it is any
> better than monte-carlo:

That's just what the monte-carlo method is -- except the full family of
monte-carlo methods can be quite sophisticated, including being more
general (pseudo or quasi random points instead of a regular grid) and
can provide a theoretical basis for calculating the rate of convergence,
and so on.

Gary Herron

> for two circles, delta=0.001 takes about a minute and delta=0.0001 is
> still running after 30 minutes :-)
>
> 1.22
> 1.2281
> 1.22834799999
>
>
> ------------------------------------------------------------------
> import math
>
> class Circle:
>
> def __init__(self, x, y, r=1):
> self.x = float(x)
> self.y = float(y)
> self.r = float(r)
>
> def contains(self, a, b):
> return math.sqrt((self.x-a)**2 + (self.y-b)**2) <= self.r
>
> class Grid:
>
> def __init__(self, circles):
> self.X1 = min(c.x-c.r for c in circles)
> self.Y1 = max(c.y+c.r for c in circles)
> self.X2 = max(c.x+c.r for c in circles)
> self.Y2 = min(c.y-c.r for c in circles)
> self.circles = circles
>
> def iter_boxes(self, delta):
> X2 = self.X2
> Y2 = self.Y2
> a = self.X1
> while a < X2:
> a += delta
> b = self.Y1
> while b > Y2:
> b -= delta
> #print a, b
> yield a, b
>
> def intersection(self, delta=0.1):
> S = 0
> s = delta**2 #box area
> circles = self.circles
> for a, b in self.iter_boxes(delta):
> if all(c.contains(a, b) for c in circles):
> S += s
> return S
>
>
> c = Circle(0, 1)
>
> assert c.contains(0, 1)
> assert c.contains(0, 1.5)
> assert c.contains(math.sqrt(2)/2, math.sqrt(2)/2)
> assert c.contains(0, 2)
> assert not c.contains(0, 2.01)
> assert not c.contains(0, 2.1)
> assert not c.contains(0, 3)
>
> circles = [
> Circle(0, 0),
> Circle(0, 1),
> ]
>
> g = Grid(circles)
>
> print '-'*30
> print g.intersection()
> print g.intersection(0.01)
> print g.intersection(0.001)
> print g.intersection(0.0001)
>
> ------------------------------------------------------------------
>

== 4 of 6 ==
Date: Thurs, Feb 4 2010 9:24 am
From: Gerard Flanagan


Gary Herron wrote:
> Gerard Flanagan wrote:
>>
>> A brute force approach - create a grid of small squares and calculate
>> which squares are in all circles. I don't know whether it is any
>> better than monte-carlo:
>
> That's just what the monte-carlo method is -- except the full family of
> monte-carlo methods can be quite sophisticated, including being more
> general (pseudo or quasi random points instead of a regular grid) and
> can provide a theoretical basis for calculating the rate of convergence,
> and so on.
>

I would have said a Monte Carlo method necessarily involves random
sampling in some shape or form, no? (Casinos, Chance etc..) And as I've
previously understood Monte Carlo Integration, the technique involves
rather the ratio "hits/total sample" rather than the sum of small areas.
That's how I remember it, at least - it's been a while though.

Regards

G.F

== 5 of 6 ==
Date: Thurs, Feb 4 2010 9:27 am
From: Dave Angel


Shashwat Anand wrote:
> I want to calculate areas.
> like for two circles (0, 0) and (0, 1) : the output is '1.228370'
>
> similarly my aim is to take 'n' co-ordinates, all of radius '1' and
> calculate the area common to all.
> The best I got was monte-carlo methods which is inefficient. Is there any
> other approach possible.
>
>
>
You start with a list of circles, each having center (a,b), and radius
(r). As you stated the problem, r is always 1, but I'll keep the
algorithm general.

This is untested, and probably contains typos, at least.

I would do it with successively smaller squares. A square can be
described using center coordinates (x,y) , and length (s) of a side.
Each square can have three states: not included, partially included,
and fully included.

You build a list of squares, starting with one, the bounding square for
the first circle.

For each square in the list, you go through all the circles (each with
center a,b, and radius r), and for each circle decide if the square is
entirely within the circle, or partially. If it's fully included in all
of them, you add its area to the the accumulator, and drop it from your
list. if it's not included at all in one of them, you just drop it
from the list. (Note, be careful about modifying a list you're
iterating through -- make a copy)

After a pass through the list, the accumulator is a lower-bound for the
area, and the accumulator plus the areas of all the squares is an upper
bound. If these are close enough together, you terminate.

Otherwise you take the remaining squares in the list, split each into
four pieces, and end up with a list 4 times as long. And repeat the
loop, checking each square against each circle.


The only remaining question is how you decide for a given circle and a
given square which of the three states it's in. And it turns out you
can be pretty approximate in this, and it'll still converge. So you can
take a distance between (a,b) and (x,y), and compare it to r-s/2 and to
r+(s*0.707106781187). If it's greater than r+s*0.707106781187, no
intersection. If it's less than r-s/2, then the square's entirely inside.

Note that I used 0.707106781187 above, but you'd actually use sqr(0.5)
to whatever precision you needed. And if it's convenient to round it,
round it upwards; again it won't affect the result. I doubt if it
would matter in Python code, but you could even do the whole thing in
integers (avoiding the sqrt for distance calc), if you were careful
about the bounding formulas. That's premature optimization, however.

HTH,
DaveA

== 6 of 6 ==
Date: Thurs, Feb 4 2010 10:55 am
From: Mark Dickinson


On 2/4/2010 7:05 AM, Shashwat Anand wrote:
> I want to calculate areas.
> like for two circles (0, 0) and (0, 1) : the output is '1.228370'
>
> similarly my aim is to take 'n' co-ordinates, all of radius '1' and
> calculate the area common to all.
> The best I got was monte-carlo methods which is inefficient. Is there
> any other approach possible.

How much accuracy do you need? How fast does the algorithm need to
be? How many circles are typically involved? Is the intersection
necessarily non-empty for the configurations of circles that you use?
How much code are you prepared to write? How much mathematics do you
want to use?

Besides the Monte-Carlo and grid-based approaches already mentioned, I
see a couple of other possibilities:

(1) It should be entirely possible to do this analytically. The hard
part would be identifying the intersection points that form the
vertices of the intersection (and especially, doing this robustly in
the face of numerical errors and triple intersections or near-triple
intersections). Once you've got the vertices, it should be
straightforward to compute the area: compute the area of the polygon
given by the convex hull of the vertices, then add on the extra areas
for the segments bounded by the (straight) sides of the polygon and
the corresponding outer arcs.

(2) For a numerical approach that might be slightly better than the 2d
brute-force approaches described by others: make use of the fact that
the intersection is convex. Pick a point P in the intersection (if it
exists: finding such a point is an issue in itself). For each angle
t, 0 <= t <= 2*pi, define f(t) to be the distance from P to the
boundary of the region, in the direction of the angle t. We always
have 0 <= f(t) <= 1, so f(t) can be quickly and accurately computed
with a bisection search. Now find the definite integral of 0.5 *
(f(t))**2, as t goes from 0 to 2*pi, using your favourite quadrature
method (but avoid methods that would be very sensitive to continuity
of derivatives: f(t) will be continuous, but f'(t) will not).
Simpson's rule is probably good enough.

Good luck!

--
Mark

==============================================================================
TOPIC: YAML (was: Python and Ruby)
http://groups.google.com/group/comp.lang.python/t/dfe4f6c60032755e?hl=en
==============================================================================

== 1 of 4 ==
Date: Thurs, Feb 4 2010 6:57 am
From: Lou Pecora


In article <87eil1ddjp.fsf_-_@castleamber.com>,
John Bokma <john@castleamber.com> wrote:

> Lou Pecora <pecora@anvil.nrl.navy.mil> writes:
>
> > That's a pretty accurate description of how I transitioned to Python
> > from C and Fortran.
>
> Not C, but C++ (but there are also C implementations): YAML, see:
> http://code.google.com/p/yaml-cpp/wiki/HowToParseADocument
>
> I use YAML now and then with Perl for both reading/writing data and for
> debugging purposes (YAML is quite human readable, for programmers at least)
>
> Of course there is also YAML support for Python:
> http://pyyaml.org/.

Well, that looks a bit more complicated than I would like, but maybe
it's doing more stuff than I can grok. Here's what I needed and how I
did it in Python:

# Make some variables
x=1.234e-8
y=2
astr="An output string...whatever"
z=4.5+1j*1.3456 # a complex number

# Output them to a file already opened as fp
outlist=[x, y, astr, z]
repvars= repr(outlist)+"\n"
fp.write(repvars)

# Reading same list in:
instr=fp.readline()
inlist=eval(instr)
x1,y1,astr1,z1= inlist


That's what I needed. 3 lines to write or read a inhomogeneous
collection of variables. I can add more variables, shuffle the order,
whatever without messing with formatting, etc. That's pretty easy for me
and it's easy for anyone to see and understand what's being done. Not
trying to start an argument, just showing how the former messasge I was
replying to made a good point about Python's way of doing things and the
effort to shake off old habits from other languages.

--
-- Lou Pecora


== 2 of 4 ==
Date: Thurs, Feb 4 2010 6:59 am
From: Lou Pecora


In article <7x8wb9j4r2.fsf@ruckus.brouhaha.com>,
Paul Rubin <no.email@nospam.invalid> wrote:

> Lou Pecora <pecora@anvil.nrl.navy.mil> writes:
> > after much noodling around and reading it hit me that I could just put
> > all that output of different types of variables into a list, hit it
> > with a repr() function to get a string version, and write the string
> > to a file -- no formatting necessary-- three lines of code. Later
> > reading in the string version (no formatting necessary), and hitting
> > it with an eval() function returned all the values I originally had in
> > those variables. How simple, but beautiful.
>
> FYI: I do that too sometimes, but in general using repr/eval that way
> is poor style and not very reliable. It's better to use the pickle
> module, which is intended for that sort of thing, or the json module
> if your datatypes are suitable and you want to follow a semi-standard.

Yeah, I should look into pickle. Haven't messed with that. Most of
what I do is numerical calculations for my consumption/research so quick
and easy comes first. Thanks for the hint.

--
-- Lou Pecora


== 3 of 4 ==
Date: Thurs, Feb 4 2010 9:26 am
From: John Bokma


Marius Gedminas <mgedmin@gmail.com> writes:

> On Feb 4, 1:03 am, John Bokma <j...@castleamber.com> wrote:
>> Jonathan Gardner <jgard...@jonathangardner.net> writes:
>> > I can explain all of Python in an hour;
>>
>> OK, in that case I would say give it a go. Put it on YouTube, or write a
>> blog post about it (or post it here). I am sure you will help a lot of
>> people that way.
>
> Someone already did: "Advanced Python or Understanding Python"
> http://video.google.com/videoplay?docid=7760178035196894549
> (76 minutes).
>
> Worth watching.

Thanks, I will. And let you know if it succeeds at "explain all of
Python in 76 minutes". It's not a fair test, since I am not new to
Python, but let me see first ;-)

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development


== 4 of 4 ==
Date: Thurs, Feb 4 2010 9:49 am
From: John Bokma


Lou Pecora <pecora@anvil.nrl.navy.mil> writes:

> In article <87eil1ddjp.fsf_-_@castleamber.com>,
> John Bokma <john@castleamber.com> wrote:
>
>> Lou Pecora <pecora@anvil.nrl.navy.mil> writes:
>>
>> > That's a pretty accurate description of how I transitioned to Python
>> > from C and Fortran.
>>
>> Not C, but C++ (but there are also C implementations): YAML, see:
>> http://code.google.com/p/yaml-cpp/wiki/HowToParseADocument
>>
>> I use YAML now and then with Perl for both reading/writing data and for
>> debugging purposes (YAML is quite human readable, for programmers at least)
>>
>> Of course there is also YAML support for Python:
>> http://pyyaml.org/.
>
> Well, that looks a bit more complicated than I would like, but maybe
> it's doing more stuff than I can grok. Here's what I needed and how I
> did it in Python:
>
> # Make some variables
> x=1.234e-8
> y=2
> astr="An output string...whatever"
> z=4.5+1j*1.3456 # a complex number
>
> # Output them to a file already opened as fp
> outlist=[x, y, astr, z]
> repvars= repr(outlist)+"\n"
> fp.write(repvars)
>
> # Reading same list in:
> instr=fp.readline()
> inlist=eval(instr)
> x1,y1,astr1,z1= inlist
>
>
> That's what I needed. 3 lines to write or read a inhomogeneous
> collection of variables. I can add more variables, shuffle the order,
> whatever without messing with formatting, etc. That's pretty easy for me
> and it's easy for anyone to see and understand what's being done. Not
> trying to start an argument, just showing how the former messasge I was
> replying to made a good point about Python's way of doing things and the
> effort to shake off old habits from other languages.

My C++ is rusty to say the least, so I can't give you an example in C++,
and the C++ version will be longer than the Python version for
sure. I use YAML, YAML::Syck to be precise, now and then in Perl. In
Perl, if $data is a reference to an arbitrary (complex) datastructure:

DumpFile( 'filename', $data );

writes it out and

$data = LoadFile( 'filename' );

reads it back in.

Since YAML is to some extent human readable, I now and then use it for
debugging purposes over Data::Dumper, also because the output is more
compact, e.g.

die Dump $data;

Personally I think it's good to be aware of YAML, since it's supported
by several languages and it should in general be possible to exchange
the generated YAML between them.

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development

==============================================================================
TOPIC: Refreshing of urllib.urlopen()
http://groups.google.com/group/comp.lang.python/t/9f0ed7d1d836656a?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Feb 4 2010 8:46 am
From: Nobody


On Wed, 03 Feb 2010 21:33:08 -0600, Michael Gruenstaeudl wrote:

> I am fairly new to Python and need advice on the urllib.urlopen()
> function. The website I am trying to open automatically refreshes
> after 5 seconds and remains stable thereafter. With
> urllib.urlopen().read() I can only read the initial but not the
> refreshed page. How can I access the refreshed page via
> urlopen().read()? I have already tried to intermediate with
> time.sleep() before invoking .read() (see below), but this does not
> work.

In all probability, the server is instructing the browser to load a
different URL via either a Refresh: header or a <meta http-equiv="refresh">
tag in the page. You will have to retrieve that information then issue a
request for the new URL.

It might even be redirecting via JavaScript, in which case, you lose (it's
possible to handle this case, but it's difficult).


==============================================================================
TOPIC: read a process output with subprocess.Popen
http://groups.google.com/group/comp.lang.python/t/ff58d1f8efb0bd23?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Feb 4 2010 9:02 am
From: Nobody


On Thu, 04 Feb 2010 04:28:20 -0800, Ashok Prabhu wrote:

> I m trying a read the output of a process which is running
> continuously with subprocess.Popen. However the readline() method
> hangs for the process to finish. Please let me know if the following
> code can be made to work with subprocess.Popen with threads or queues.
> I tried a lot of methods but to no avail. It would be great if someone
> can make it work.

This is an issue with grep, not Python per se.

By default, stdout (in the C library) is line-buffered if it refers to a
TTY, and block-buffered otherwise (e.g. if it refers to a pipe). grep
doesn't change the default buffering, so when it's stdout is a pipe, its
output is written in 4K blocks.

If you only need this to work with GNU grep, you can use the
--line-buffered switch to force stdout to be flushed after each line.

If it needs to be portable, you can implement the grep part in Python
(i.e. read p1.stdout and ignore any lines which don't contain a given
string or which don't match a given regex).


==============================================================================
TOPIC: Passing parameters in URL
http://groups.google.com/group/comp.lang.python/t/52695ffb32fef94b?hl=en
==============================================================================

== 1 of 2 ==
Date: Thurs, Feb 4 2010 9:22 am
From: John Bokma


"Diez B. Roggisch" <deets@nospam.web.de> writes:

> Am 04.02.10 01:42, schrieb John Bokma:

[..]

>> Maybe you should think about what happens if someone posts:
>> <img src="http://example.com/item_delete?id=123"> to a popular forum...
>
> And the difference to posting
>
> from urrlib2 import open
> from urllib import encode
>
> open("http://example.com/item_delete", data=encode([("id", "123")]))
>
> to that same public "hacker" forum is exactly what?

Imagine that a user of example.com, logged in at example.com (i.e. with
a valid session ID in a cookie), visits the aforementioned (by me)
forum, and that he has an item 123. It will be deleted.

> If your webapp happens to allow item_delete to be called without
> authentication & authorization, then *that's* your problem.

You now understand that *with* a & a a GET request can be *still* harmful?

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development


== 2 of 2 ==
Date: Thurs, Feb 4 2010 10:07 am
From: "Diez B. Roggisch"


Am 04.02.10 18:22, schrieb John Bokma:
> "Diez B. Roggisch"<deets@nospam.web.de> writes:
>
>> Am 04.02.10 01:42, schrieb John Bokma:
>
> [..]
>
>>> Maybe you should think about what happens if someone posts:
>>> <img src="http://example.com/item_delete?id=123"> to a popular forum...
>>
>> And the difference to posting
>>
>> from urrlib2 import open
>> from urllib import encode
>>
>> open("http://example.com/item_delete", data=encode([("id", "123")]))
>>
>> to that same public "hacker" forum is exactly what?
>
> Imagine that a user of example.com, logged in at example.com (i.e. with
> a valid session ID in a cookie), visits the aforementioned (by me)
> forum, and that he has an item 123. It will be deleted.

The webapp must be actually preventing the processing of GET-requests
for the aciton in question. This isn't the case by default for many of
them, in fact at least e.g. TurboGears, as well as PHP offer you ways to
treat GET and POSTvars the exact same way. So unless the programmer is
aware of this potential problem, it won't help.

And in the same way one can embed a form with a post-action that leads
to the full http://example.com-url into an external page. So it is
equally as dangerous. Yes, links are easier, no doubt about that. But
POST doesn't magically make you safe from those kinds of attacks.

The only way to prevent this are short-lived sessions, or action-tokens
of some kind, as Paul mentioned before.

Diez

==============================================================================
TOPIC: Selenium/SauceLabs OpenSpace at Pycon
http://groups.google.com/group/comp.lang.python/t/259ee71b81205891?hl=en
==============================================================================

== 1 of 2 ==
Date: Thurs, Feb 4 2010 10:18 am
From: Raymond Hettinger


> >For those who are interested, the Sauce Labs team,
> >http://saucelabs.com/about/team, is hosting two free tutorial open
> >space sessions at Pycon in Atlanta.

[Aahz]
> Congrats on the new job!

Thanks. I'm really enjoying working with Jim Baker
and Frank Wierzbicki.


Raymond

== 2 of 2 ==
Date: Thurs, Feb 4 2010 11:48 am
From: Paul Rubin


aahz@pythoncraft.com (Aahz) writes:
> Raymond Hettinger <python@rcn.com> wrote:
>>For those who are interested, the Sauce Labs team,
>>http://saucelabs.com/about/team, is hosting two free tutorial open
>>space sessions at Pycon in Atlanta.
>
> Congrats on the new job!

Yes, cool! I don't recognize several of these logos, but maybe Sauce
can help:

http://aasi.ebm.fi/5742/browsers.jpg

;-)

==============================================================================
TOPIC: "germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.
com/
http://groups.google.com/group/comp.lang.python/t/6f254f8a7e6c4e77?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Feb 4 2010 10:47 am
From: saima81


"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/
"germany scholarships" "europe scholarships" "europe scholarship 2010"
"spain scholarship" "italy scholarship" "italy scholarships" "france
scholarships" "france scholarships" on http://escholarship-positions.blogspot.com/

==============================================================================
TOPIC: how to run part of my python code as root
http://groups.google.com/group/comp.lang.python/t/02fa0b92a095f503?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Feb 4 2010 11:05 am
From: Tomas Pelka


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hey,

is there possibility how to run part of my code (function for example)
as superuser.

Or only way how to do this is create a wrapper and run is with Popen
through sudo (but I have to configure sudo to run "whole" python as root).

Thanks for advice.

- --
Tom

Key fingerprint = 06C0 23C6 9EB7 0761 9807 65F4 7F6F 7EAB 496B 28AA
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAktrGoYACgkQf29+q0lrKKqaNACdEvfg+g0n3DzFr/7R33y2Nesy
hK8An3ZlpUEEibf0Q1wVET/KpXnsv/PO
=JKro
-----END PGP SIGNATURE-----

==============================================================================
TOPIC: Problem with __init__.py in Python3.1
http://groups.google.com/group/comp.lang.python/t/5740b72706867444?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Feb 4 2010 11:21 am
From: "Gabriel Genellina"


En Thu, 04 Feb 2010 12:40:41 -0300, Hidura <hidura@gmail.com> escribió:

> Thanks, I read it and try to put my code in the correct form, but now
> give
> me another error, inside the Writer Package says:
> "ValueError: Attempted relative import in non-package", if somebody
> knows a
> clue of how fix it i will glad to read opinions. [?]

You say that Writer is a package, but apparently Python disagrees. A
package is not just "a directory containing an __init__.py file", this is
half the truth. Python must be *aware* of such file, and this happens when
you import the package.

If you directly run a script from inside a package, Python does not know
that it belongs to a package, and treats it as a simple, lonely script. In
that case, relative imports won't work.

Put the "main" script (the one that you directly execute) outside the
package. It can be as small as this, if you want:

from some.package import main
main()

--
Gabriel Genellina

==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en

To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate