Friday, February 12, 2010

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

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

comp.lang.python@googlegroups.com

Today's topics:

* Sorting a list of lists - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/700f9f95ffb50989?hl=en
* Please help with MemoryError - 11 messages, 9 authors
http://groups.google.com/group/comp.lang.python/t/6ed1981bc9821443?hl=en
* PAPER PRESENTATIONS AND SEMIMAR TOPICS. - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/42c1bbefe506edef?hl=en
* search entire drive say c: - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/15afbc4c17b0336a?hl=en
* Replace various regex - 5 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/0a6e1403d46a4ab5?hl=en
* Modifying Class Object - 4 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/fd36962c4970ac48?hl=en
* A silly question on file opening - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/44d20613038ccd74?hl=en
* Python version of perl's "if (-T ..)" and "if (-B ...)"? - 1 messages, 1
author
http://groups.google.com/group/comp.lang.python/t/8816441f153588dd?hl=en

==============================================================================
TOPIC: Sorting a list of lists
http://groups.google.com/group/comp.lang.python/t/700f9f95ffb50989?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Feb 12 2010 10:32 am
From: John Posner


On 2/12/2010 12:45 PM, R (Chandra) Chandrasekhar wrote:
> Dear Folks,
>
> I have lines of values like so:
>
> 14, [25, 105, 104]
> 10, [107, 106, 162]
> 21, [26, 116, 165]
>
> I need to sort them in two ways:
>
> (a) By the numeric value of the first column; and
>
> (b) by the sum of the elements of the second item in each list, which is
> a list in itself.
>
> At present, I have appended each line into a list L so that I for teh
> above minimal data, I have a list of lists thus:
>
> [[14, [25, 105, 104]], [10, [107, 106, 162]], [21, [26, 116, 165]]]
>
> I have tried using
>
> (a) sorted(L, key = lambda x:(x[0]))

Just plain "sorted(L)" will work, because Python knows how to compare
your [N, [N,N,N]]-format values:

>>> [14, [25, 105, 104]] < (14, [25, 105, 103]]
False
>>> [14, [25, 105, 104]] < (14, [25, 105, 105]]
True
>>>

>
> and
>
> (b) sorted(L, key = lambda x:(sum(x[1])))

That looks good.

>
> and get the anticipated results for (a) and (b0, at least with the above
> minimal data set.
>
> Is this a sensible way to go about the sorting, or are there better ways
> of doing it in Python?

You're doing fine.

>
> Also, I am baffled because the above fails obviously when len(L) is
> about a hundred and I can't figure out why.

Please cut-and-paste the exact error message (or other evidence of
"failure") into a message.


Tx,
John

==============================================================================
TOPIC: Please help with MemoryError
http://groups.google.com/group/comp.lang.python/t/6ed1981bc9821443?hl=en
==============================================================================

== 1 of 11 ==
Date: Fri, Feb 12 2010 10:45 am
From: John Posner


On 2/12/2010 12:14 PM, Steven D'Aprano wrote:
> On Fri, 12 Feb 2010 06:45:31 -0800, Jeremy wrote:
>
>> You also confirmed what I thought was true that all variables are passed
>> "by reference" so I don't need to worry about the data being copied
>> (unless I do that explicitly).
>
> No, but yes.
>
> No, variables are not passed by reference, but yes, you don't have to
> worry about them being copied.
>
> You have probably been mislead into thinking that there are only two
> calling conventions possible, "pass by value" and "pass by reference".
> That is incorrect. There are many different calling conventions, and
> different groups use the same names to mean radically different things.
>
> If a language passes variables by reference, you can write a "swap"
> function like this:
>
> def swap(a, b):
> a, b = b, a
>
> x = 1
> y = 2
> swap(x, y)
> assert (x == 2) and (y==1)
>
> But this does not work in Python, and cannot work without trickery. So
> Python absolutely is not "pass by reference".
>
> On the other hand, if a variable is passed by value, then a copy is made
> and you can do this:
>
> def append1(alist):
> alist.append(1) # modify the copy
> return alist
>
> x = []
> newlist = append1(x)
> assert x == [] # The old value still exists.
>
> But this also doesn't work in Python! So Python isn't "pass by value"
> either.
>
> What Python does is called "pass by sharing", or sometimes "pass by
> object reference". It is exactly the same as what (e.g.) Ruby and Java
> do, except that confusingly the Ruby people call it "pass by reference"
> and the Java people call it "pass by value", thus guaranteeing the
> maximum amount of confusion possible.
>
>
> More here:
> http://effbot.org/zone/call-by-object.htm
> http://en.wikipedia.org/wiki/Evaluation_strategy
>

Excellent writeup, Steve! You and Jeremy might be interested in a
message on "pass-by-XXX" from John Zelle, the author of the textbook
"Python Programming: An Introduction to Computer Science". [1] This was
part of a long thread in May 2008 on the Python edu-sig list -- nearly
as long as "Modifying Class Object", but with nowhere near the fireworks!

Tx,
John

[1] http://mail.python.org/pipermail/edu-sig/2008-May/008583.html

== 2 of 11 ==
Date: Fri, Feb 12 2010 12:07 pm
From: mk


John Posner wrote:

>> http://effbot.org/zone/call-by-object.htm
>> http://en.wikipedia.org/wiki/Evaluation_strategy

> [1] http://mail.python.org/pipermail/edu-sig/2008-May/008583.html

Hmm how about "call by label-value"?

That is, you change labels by assignment, but pass the value of the
label to a function. Since label value is passed, original label is not
changed (i.e. it's not call by reference).

However, an object referenced by label value can be changed in Python,
like in classic example of list label passed to a function and then this
list being modified in a function.

Regards,
mk

== 3 of 11 ==
Date: Fri, Feb 12 2010 12:22 pm
From: Mel


mk wrote:

> John Posner wrote:
>
>>> http://effbot.org/zone/call-by-object.htm
>>> http://en.wikipedia.org/wiki/Evaluation_strategy
>
>> [1] http://mail.python.org/pipermail/edu-sig/2008-May/008583.html
>
> Hmm how about "call by label-value"?

Nothing egregiously wrong with it.. maybe it's a bit wordy. It may be an
uphill fight getting it accepted into computer science the way Call by
value, Call by reference, Call by sharing, and Call by copy-restore have
been (<http://en.wikipedia.org/wiki/Evaluation_strategy>).

Mel.
>
> That is, you change labels by assignment, but pass the value of the
> label to a function. Since label value is passed, original label is not
> changed (i.e. it's not call by reference).
>
> However, an object referenced by label value can be changed in Python,
> like in classic example of list label passed to a function and then this
> list being modified in a function.
>
> Regards,
> mk


== 4 of 11 ==
Date: Fri, Feb 12 2010 12:59 pm
From: Christian Heimes


mk wrote:
> Hmm how about "call by label-value"?

Or "call by guido"? How do you like "call like a dutch"? :]

== 5 of 11 ==
Date: Fri, Feb 12 2010 1:03 pm
From: "Alf P. Steinbach"


* Christian Heimes:
> mk wrote:
>> Hmm how about "call by label-value"?
>
> Or "call by guido"? How do you like "call like a dutch"? :]

Just a note: it might be more clear to talk about "pass by XXX" than "call by XXX".

Unless you're talking about something else than argument passing.

The standard terminology is in my view fine.


Cheers & hth.,

- Alf


== 6 of 11 ==
Date: Fri, Feb 12 2010 12:33 pm
From: Ethan Furman


mk wrote:
> John Posner wrote:
>
>>> http://effbot.org/zone/call-by-object.htm
>>> http://en.wikipedia.org/wiki/Evaluation_strategy
>
>> [1] http://mail.python.org/pipermail/edu-sig/2008-May/008583.html
>
> Hmm how about "call by label-value"?
>
> That is, you change labels by assignment, but pass the value of the
> label to a function. Since label value is passed, original label is not
> changed (i.e. it's not call by reference).
>
> However, an object referenced by label value can be changed in Python,
> like in classic example of list label passed to a function and then this
> list being modified in a function.
>
> Regards,
> mk
>

Because "value" and "reference" are already well defined terms with very
definite meanings, I think using them in any way to describe Python's
model will lead to confusion.

Seems to me that "call by object", a term coined decades ago, and that
accurately defines the way that Python (the language) actually does it,
should be the term used.

My $0.02.

~Ethan~


== 7 of 11 ==
Date: Fri, Feb 12 2010 1:33 pm
From: Steven D'Aprano


On Fri, 12 Feb 2010 21:07:08 +0100, mk wrote:

> John Posner wrote:
>
>>> http://effbot.org/zone/call-by-object.htm
>>> http://en.wikipedia.org/wiki/Evaluation_strategy
>
>> [1] http://mail.python.org/pipermail/edu-sig/2008-May/008583.html
>
> Hmm how about "call by label-value"?

Python's calling convention already has an well-established name,
established over thirty years ago by the distinguished computer scientist
Barbara Liskov, namely call-by-sharing. It's also known as "call-by-
object-reference". It doesn't need yet another name.

--
Steven


== 8 of 11 ==
Date: Fri, Feb 12 2010 1:52 pm
From: Antoine Pitrou


Le Fri, 12 Feb 2010 17:14:57 +0000, Steven D'Aprano a écrit :
>
> What Python does is called "pass by sharing", or sometimes "pass by
> object reference". It is exactly the same as what (e.g.) Ruby and Java
> do, except that confusingly the Ruby people call it "pass by reference"
> and the Java people call it "pass by value", thus guaranteeing the
> maximum amount of confusion possible.

Well, I think the Ruby people got it right. Python *does* pass parameters
by reference. After all, we even speak about reference counting,
reference cycles, etc.

So I'm not sure what distinction you're making here.

== 9 of 11 ==
Date: Fri, Feb 12 2010 2:12 pm
From: "Alf P. Steinbach"


* Antoine Pitrou:
> Le Fri, 12 Feb 2010 17:14:57 +0000, Steven D'Aprano a écrit :
>> What Python does is called "pass by sharing", or sometimes "pass by
>> object reference". It is exactly the same as what (e.g.) Ruby and Java
>> do, except that confusingly the Ruby people call it "pass by reference"
>> and the Java people call it "pass by value", thus guaranteeing the
>> maximum amount of confusion possible.
>
> Well, I think the Ruby people got it right. Python *does* pass parameters
> by reference. After all, we even speak about reference counting,
> reference cycles, etc.
>
> So I'm not sure what distinction you're making here.

Steven talks about the standard meaning of "pass by reference".

Essentially that means that you can implement a routine "swap" such that

a = 1; b = 2; swap( a, b )

results in a == 2 and b == 1.

In Pascal that's 'var' parameters, in C there's no such, in C++ it's reference
type parameters using '&' as a type builder, in C# it's 'out' and 'ref'
parameters (I think C# has both the most practical and the most elegant
implementation, you can see at the call site whether it's by reference), so on.

To some degree "pass by reference" is however just a collection of special cases
with some similarity. For example, if one says that the guiding principle & main
criterion is that one should not have to indicate the pass by reference at the
call site, then that would exclude C#. But if one allows some indication at the
call site, then that would perhaps include C (applying the & address op), which
everyone agrees does not have call by reference.

So, to understand the conventional meaning of the term it is perhaps necessary
to have some knowledge of all the special cases, like here C# versus C.


Cheers & hth.,

- Alf


== 10 of 11 ==
Date: Fri, Feb 12 2010 2:10 pm
From: Steve Holden


Antoine Pitrou wrote:
> Le Fri, 12 Feb 2010 17:14:57 +0000, Steven D'Aprano a écrit :
>> What Python does is called "pass by sharing", or sometimes "pass by
>> object reference". It is exactly the same as what (e.g.) Ruby and Java
>> do, except that confusingly the Ruby people call it "pass by reference"
>> and the Java people call it "pass by value", thus guaranteeing the
>> maximum amount of confusion possible.
>
> Well, I think the Ruby people got it right. Python *does* pass parameters
> by reference. After all, we even speak about reference counting,
> reference cycles, etc.
>
> So I'm not sure what distinction you're making here.
>
He's distinguishing what Python does from the "call by reference" which
has been used since the days of Algol 60.

As has already been pointed out, if Python used call by reference then
the following code would run without raising an AssertionError:

def exchange(a, b):
a, b = b, a

x = 1
y = 2
exchange(x, y)
assert (x == 2 and y == 1)

Since function-local assignment always takes place in the function
call's local namespace Python does not, and cannot, work like this, and
hence the term "call by reference" is inapplicable to Python's semantics.

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/

== 11 of 11 ==
Date: Fri, Feb 12 2010 2:10 pm
From: Steve Holden


Antoine Pitrou wrote:
> Le Fri, 12 Feb 2010 17:14:57 +0000, Steven D'Aprano a écrit :
>> What Python does is called "pass by sharing", or sometimes "pass by
>> object reference". It is exactly the same as what (e.g.) Ruby and Java
>> do, except that confusingly the Ruby people call it "pass by reference"
>> and the Java people call it "pass by value", thus guaranteeing the
>> maximum amount of confusion possible.
>
> Well, I think the Ruby people got it right. Python *does* pass parameters
> by reference. After all, we even speak about reference counting,
> reference cycles, etc.
>
> So I'm not sure what distinction you're making here.
>
He's distinguishing what Python does from the "call by reference" which
has been used since the days of Algol 60.

As has already been pointed out, if Python used call by reference then
the following code would run without raising an AssertionError:

def exchange(a, b):
a, b = b, a

x = 1
y = 2
exchange(x, y)
assert (x == 2 and y == 1)

Since function-local assignment always takes place in the function
call's local namespace Python does not, and cannot, work like this, and
hence the term "call by reference" is inapplicable to Python's semantics.

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/

==============================================================================
TOPIC: PAPER PRESENTATIONS AND SEMIMAR TOPICS.
http://groups.google.com/group/comp.lang.python/t/42c1bbefe506edef?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Feb 12 2010 11:09 am
From: hot girl


PAPER PRESENTATIONS AND SEMIMAR TOPICS.
CHECK OUR VAST PAPER PRESENTATIONS AND SEMIMAR TOPICS INCLUDING
PROJECTS FOR FREE AT
http://presentationsandseminars.blogspot.com

==============================================================================
TOPIC: search entire drive say c:
http://groups.google.com/group/comp.lang.python/t/15afbc4c17b0336a?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Feb 12 2010 11:36 am
From: McColgst

>    ROOT = "c:\\"
>    fname = "abc.txt".lower()
>    for p, d, f in os.walk(ROOT):
>      for fn in f:
>        if fn.lower() == fname:
>          print os.path.join(p, f)
>          # break

I think you mean

print os.path.join(p,fn)

-sean

==============================================================================
TOPIC: Replace various regex
http://groups.google.com/group/comp.lang.python/t/0a6e1403d46a4ab5?hl=en
==============================================================================

== 1 of 5 ==
Date: Fri, Feb 12 2010 11:39 am
From: Martin


Hi,

I am trying to come up with a more generic scheme to match and replace
a series of regex, which look something like this...

19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft)
5.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft)

Ideally match the pattern to the right of the "!" sign (e.g. lai), I
would then like to be able to replace one or all of the corresponding
numbers on the line. So far I have a rather unsatisfactory solution,
any suggestions would be appreciated...

The file read in is an ascii file.

f = open(fname, 'r')
s = f.read()

if CANHT:
s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ !
canht_ft", CANHT, s)

where CANHT might be

CANHT = '115.01,16.38,0.79,1.26,1.00 ! canht_ft'

But this involves me passing the entire string.

Thanks.

Martin


== 2 of 5 ==
Date: Fri, Feb 12 2010 11:57 am
From: McColgst


On Feb 12, 2:39 pm, Martin <mdeka...@gmail.com> wrote:
> Hi,
>
> I am trying to come up with a more generic scheme to match and replace
> a series of regex, which look something like this...
>
> 19.01,16.38,0.79,1.26,1.00   !  canht_ft(1:npft)
> 5.0, 4.0, 2.0, 4.0, 1.0      !  lai(1:npft)
>
> Ideally match the pattern to the right of the "!" sign (e.g. lai), I
> would then like to be able to replace one or all of the corresponding
> numbers on the line. So far I have a rather unsatisfactory solution,
> any suggestions would be appreciated...
>
> The file read in is an ascii file.
>
> f = open(fname, 'r')
> s = f.read()
>
> if CANHT:
>     s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+   !
> canht_ft", CANHT, s)
>
> where CANHT might be
>
> CANHT = '115.01,16.38,0.79,1.26,1.00   !  canht_ft'
>
> But this involves me passing the entire string.
>
> Thanks.
>
> Martin

If I understand correctly, there are a couple ways to do it.
One is to use .split() and split by the '!' sign, given that you wont
have more than one '!' on a line. This will return a list of the words
split by the delimiter, in this case being '!', so you should get back
(19.01,16.38,0.79,1.26,1.00 , canht_ft(1:npft) ) and you can do
whatever replace functions you want using the list.

check out split: http://docs.python.org/library/stdtypes.html#str.split

Another, is in your regular expression, you can match the first part
or second part of the string by specifying where the '!' is,
if you want to match the part after the '!' I would do something like
r"[^! cahnt_ft]", or something similar (i'm not particularly up-to-
date with my regex syntax, but I think you get the idea.)

I hope I understood correctly, and I hope that helps.

-sean


== 3 of 5 ==
Date: Fri, Feb 12 2010 12:28 pm
From: Martin


On Feb 12, 7:57 pm, McColgst <mccol...@gmail.com> wrote:
> On Feb 12, 2:39 pm, Martin <mdeka...@gmail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > I am trying to come up with a more generic scheme to match and replace
> > a series of regex, which look something like this...
>
> > 19.01,16.38,0.79,1.26,1.00   !  canht_ft(1:npft)
> > 5.0, 4.0, 2.0, 4.0, 1.0      !  lai(1:npft)
>
> > Ideally match the pattern to the right of the "!" sign (e.g. lai), I
> > would then like to be able to replace one or all of the corresponding
> > numbers on the line. So far I have a rather unsatisfactory solution,
> > any suggestions would be appreciated...
>
> > The file read in is an ascii file.
>
> > f = open(fname, 'r')
> > s = f.read()
>
> > if CANHT:
> >     s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+   !
> > canht_ft", CANHT, s)
>
> > where CANHT might be
>
> > CANHT = '115.01,16.38,0.79,1.26,1.00   !  canht_ft'
>
> > But this involves me passing the entire string.
>
> > Thanks.
>
> > Martin
>
> If I understand correctly, there are a couple ways to do it.
> One is to use .split() and split by the '!' sign, given that you wont
> have more than one '!' on a line. This will return a list of the words
> split by the delimiter, in this case being '!', so you should get back
> (19.01,16.38,0.79,1.26,1.00  ,  canht_ft(1:npft) )  and you can do
> whatever replace functions you want using the list.
>
> check out split:http://docs.python.org/library/stdtypes.html#str.split
>
> Another, is in your regular expression, you can match the first part
> or second part of the string by specifying where the '!' is,
> if you want to match the part after the '!' I would do something like
> r"[^! cahnt_ft]", or something similar (i'm not particularly up-to-
> date with my regex syntax, but I think you get the idea.)
>
> I hope I understood correctly, and I hope that helps.
>
> -sean

Hi I like the second suggestion, so this wouldn't rely on me having to
match the numbers only the string canht for example but still allow me
to replace the whole line, is that what you mean?

I tried it and the expression seemed to replace the entire file, so
perhaps i am doing something wrong. But in principle I think that
might be a better scheme than my current one. i tried

if CANHT:
#s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ !
canht_ft", CANHT, s)
s = re.sub(r"[^! canht_ft]", CANHT, s)


== 4 of 5 ==
Date: Fri, Feb 12 2010 12:30 pm
From: MRAB


McColgst wrote:
> On Feb 12, 2:39 pm, Martin <mdeka...@gmail.com> wrote:
>> Hi,
>>
>> I am trying to come up with a more generic scheme to match and replace
>> a series of regex, which look something like this...
>>
>> 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft)
>> 5.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft)
>>
>> Ideally match the pattern to the right of the "!" sign (e.g. lai), I
>> would then like to be able to replace one or all of the corresponding
>> numbers on the line. So far I have a rather unsatisfactory solution,
>> any suggestions would be appreciated...
>>
>> The file read in is an ascii file.
>>
>> f = open(fname, 'r')
>> s = f.read()
>>
>> if CANHT:
>> s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ !
>> canht_ft", CANHT, s)
>>
>> where CANHT might be
>>
>> CANHT = '115.01,16.38,0.79,1.26,1.00 ! canht_ft'
>>
>> But this involves me passing the entire string.
>>
>> Thanks.
>>
>> Martin
>
> If I understand correctly, there are a couple ways to do it.
> One is to use .split() and split by the '!' sign, given that you wont
> have more than one '!' on a line. This will return a list of the words
> split by the delimiter, in this case being '!', so you should get back
> (19.01,16.38,0.79,1.26,1.00 , canht_ft(1:npft) ) and you can do
> whatever replace functions you want using the list.
>
> check out split: http://docs.python.org/library/stdtypes.html#str.split
>
The .split method is the best way if you process the file a line at a
time. The .split method, incidentally, accepts a maxcount argument so
that you can split a line no more than once.

> Another, is in your regular expression, you can match the first part
> or second part of the string by specifying where the '!' is,
> if you want to match the part after the '!' I would do something like
> r"[^! cahnt_ft]", or something similar (i'm not particularly up-to-
> date with my regex syntax, but I think you get the idea.)
>
The regex would be r"(?m)^[^!]*(!.*)" to capture the '!' and the rest of
the line.

> I hope I understood correctly, and I hope that helps.
>

== 5 of 5 ==
Date: Fri, Feb 12 2010 1:02 pm
From: Martin


On Feb 12, 8:30 pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
> McColgst wrote:
> > On Feb 12, 2:39 pm, Martin <mdeka...@gmail.com> wrote:
> >> Hi,
>
> >> I am trying to come up with a more generic scheme to match and replace
> >> a series of regex, which look something like this...
>
> >> 19.01,16.38,0.79,1.26,1.00   !  canht_ft(1:npft)
> >> 5.0, 4.0, 2.0, 4.0, 1.0      !  lai(1:npft)
>
> >> Ideally match the pattern to the right of the "!" sign (e.g. lai), I
> >> would then like to be able to replace one or all of the corresponding
> >> numbers on the line. So far I have a rather unsatisfactory solution,
> >> any suggestions would be appreciated...
>
> >> The file read in is an ascii file.
>
> >> f = open(fname, 'r')
> >> s = f.read()
>
> >> if CANHT:
> >>     s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+   !
> >> canht_ft", CANHT, s)
>
> >> where CANHT might be
>
> >> CANHT = '115.01,16.38,0.79,1.26,1.00   !  canht_ft'
>
> >> But this involves me passing the entire string.
>
> >> Thanks.
>
> >> Martin
>
> > If I understand correctly, there are a couple ways to do it.
> > One is to use .split() and split by the '!' sign, given that you wont
> > have more than one '!' on a line. This will return a list of the words
> > split by the delimiter, in this case being '!', so you should get back
> > (19.01,16.38,0.79,1.26,1.00  ,  canht_ft(1:npft) )  and you can do
> > whatever replace functions you want using the list.
>
> > check out split:http://docs.python.org/library/stdtypes.html#str.split
>
> The .split method is the best way if you process the file a line at a
> time. The .split method, incidentally, accepts a maxcount argument so
> that you can split a line no more than once.
>
> > Another, is in your regular expression, you can match the first part
> > or second part of the string by specifying where the '!' is,
> > if you want to match the part after the '!' I would do something like
> > r"[^! cahnt_ft]", or something similar (i'm not particularly up-to-
> > date with my regex syntax, but I think you get the idea.)
>
> The regex would be r"(?m)^[^!]*(!.*)" to capture the '!' and the rest of
> the line.
>
>
>
> > I hope I understood correctly, and I hope that helps.

I guess I could read the file a line at a time and try splitting it,
though I though it would be better to read it all once then search for
the various regex I need to match and replace?

I am not sure that regex helps, as that would match and replace every
line which had a "!". Perhaps if i explain more thoroughly?

So the input file looks something like this...

9*0.0 ! canopy(1:ntiles)
12.100 ! cs
0.0 ! gs
9*50.0 ! rgrain(1:ntiles)
0.749, 0.743, 0.754, 0.759 ! stheta(1:sm_levels)(top to bottom)
9*0.46 ! snow_tile(1:ntiles)
0.46 ! snow_grnd
276.78,277.46,278.99,282.48 ! t_soil(1:sm_levels)(top to bottom)
9*276.78 ! tstar_tile(1:ntiles)
19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft)
200.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft)

So for each of the strings following the "!" I may potentially want to
match them and replace some of the numbers. That is I might search for
the expression snow_grnd with the intention of substituting 0.46 for
another number. What i came up with was a way to match all the numbers
and pass the replacement string.

==============================================================================
TOPIC: Modifying Class Object
http://groups.google.com/group/comp.lang.python/t/fd36962c4970ac48?hl=en
==============================================================================

== 1 of 4 ==
Date: Fri, Feb 12 2010 12:22 pm
From: "Alf P. Steinbach"


* Michael Sparks:
> Hi Alf,
>
>
> Before I start, note we're talking about semantics, not
> implementation. That distinction is very important.

Yes.

It would seem to readers that posters here do not grasp and are unable to grasp
that distinction.

However, all those references to implementation aspects, persisting in the face
of corrections, have just, IMHO, been consistent attempts at misrepresentation.


> On Feb 11, 4:49 am, "Alf P. Steinbach" <al...@start.no> wrote:
>>> *The* standard general language independent definition?
>
> [ of pointer ]
>
>> Yes.
>>
>>> As defined where?
>> For example, as I used as reference in my first posting, the Java language spec.
>> But it has nothing specifically to do with Java. It is a basic concept in
>> computer science, that (most) CS students learn in their *first year*.
>>
>> E.g.
>>
>> <quote src="http://cslibrary.stanford.edu/106/">
>> A pointer stores a reference to something. Unfortunately there is no fixed term
>> for the thing that the pointer points to, and across different computer
>> languages there is a wide variety of things that pointers point to. We use the
>> term pointee for the thing that the pointer points to, and we stick to the basic
>> properties of the pointer/pointee relationship which are true in all languages.
>> The term "reference" means pretty much the same thing as "pointer" --
>> "reference" implies a more high-level discussion, while "pointer" implies the
>> traditional compiled language implementation of pointers as addresses. For the
>> basic pointer/pointee rules covered here, the terms are effectively equivalent.
>> </quote>
>
> This is where you have gone wrong. You have taken a first year
> undergraduate
> academic generalisation and assumed that it applies to The World.'

I'm sorry to disappoint, but no. It was a random Google hit to find a first-year
text that perhaps posters here could understand. Not insinuiting anything about
their mental prowess or lack thereof, but just something they might understand
given a repeatedly demonstrated inability to understand such basics, or even to
understand the concept of reference to a definition of a term.

Please do consider that my usage of the term "pointer" has always, in this
thread, including the first article I posted in this thread, been with explicit
reference to the Java language spec's meaning.

You may argue that those who wrote that language spec are confused about things
or whatever, but that's rather stretching it, wouldn't you say?


> In theory, there is no difference between practice and theory, but in practice
> there is (so the saying goes).
>
> The World however has another place for defining terms. That place is
> of highly
> varying quality, but generally a better place to correct semantics of
> terms.
> Who knows, eventually there may be a single commonly accepted
> viewpoint. (Which
> would bring a whole new level of pedantry of course )-:
>
> I am referring to Wikipedia here. (this is a vague attempt at humour,
> rather
> than an attempt to patronise which it may also come over as)

I understand.

You may note that that Wikipedia article refers to an article that I wrote about
pointers in C++.

So, this must be the most silly "argument by authority" ever offered.

I find that amusing and would have included a smiley except that I think that
that would incur further flames.

It's no criticism of you; I do appreciate your effort, thanks. And I reiterate
that the meaning of "pointer" I have used is the meaning in the Java language
spec. And that that's about semantics, not about implementation, and not about C
pointers or Pascal pointers or whatever else that some would rather wish it was.

[snip]

In the following that I snipped you had good discussion except for (as I could
see) a fallacy-based denial of references existing in Python. I don't think
there's any point in discussing that further, because it's evidently a religious
matter where rational arguments -- and I've tried a few -- don't reach, in
spite of the extreme triviality of the subject matter. Thanks for the effort at
non-flaming discussion, it *is* appreciated.


Cheers & hth.,

- Alf


== 2 of 4 ==
Date: Fri, Feb 12 2010 12:26 pm
From: "Alf P. Steinbach"


* Bruno Desthuilliers:
> Alf P. Steinbach a écrit :
> (snip)
>> This group has an extraordinary high level of flaming and personal
>> attacks
>
> Oh my...
>
> (snip remaining non-sense)
>
> Mr Steinbach, I bet you'll count this as another "flaming" and "personal
> attack", but nonetheless : you might have happier time if you were able
> to understand the distinction between disagreement and "personal attack".
>
> (now back to more interesting readings)

Yes, I do count this as a personal attack and flaming.

The litmus test for that is that it says something very negative about the
person you're debating with.

In addition, your statement about the earlier attacks on me, is untrue, and your
implication that it's only about attacks on me, is untrue. Both of which are
very misleading, by the way. I'm assuming that you're intentionally lying.


Cheers & hth.,

- Alf


== 3 of 4 ==
Date: Fri, Feb 12 2010 2:17 pm
From: Steve Holden


Alf P. Steinbach wrote:
> You may note that that Wikipedia article refers to an article that I
> wrote about pointers in C++.
>
It's a broken link, referring to a non-existent server.

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/

== 4 of 4 ==
Date: Fri, Feb 12 2010 2:27 pm
From: "Alf P. Steinbach"


* Steve Holden:
> Alf P. Steinbach wrote:
>> You may note that that Wikipedia article refers to an article that I
>> wrote about pointers in C++.
>>
> It's a broken link, referring to a non-existent server.

Yes, sorry.

It's been that way a long time, and for the same reason my C++ tutorial, the
only one linked to from the C++ FAQ, is also off-line.

However, if you're interested then copies of the "pointers" doc are likely to be
floating around (at one time some mention of it somewhere generated very high
traffic over a few weeks, people downloading the PDF, and then I almost reversed
my decision not to have ads...).


Cheers & hth.,

- Alf

==============================================================================
TOPIC: A silly question on file opening
http://groups.google.com/group/comp.lang.python/t/44d20613038ccd74?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Feb 12 2010 12:32 pm
From: joy99


On Feb 11, 1:57 am, Anthony Tolle <anthony.to...@gmail.com> wrote:
> On Feb 10, 3:42 pm,joy99<subhakolkata1...@gmail.com> wrote:
>
> > Dear Group,
> > [snip]
> > I tried to change the location to D:\file and as I saw in Python Docs
> > the file reading option is now "r+" so I changed the statement to
> >    file_open=open("D:\file","r+")
> > but it is still giving error.
>
> Only use "r+" if you need to also write to the file.  "r" is still
> good for opening for reading.
>
> Without seeing a traceback, I can only assume the error is caused by
> using a backslash in the path without escaping it.  Try either the
> following:
>
> file_open=open("D:\\file","r+")
>
> file_open=open(r"D:\file","r+")
>
> For an explanation, see the Python documentation:
>
> http://docs.python.org/reference/lexical_analysis.html#string-literals

Dear Group,

I am sorry I could not report. I was trying your solutions, I could
try only one or two, they are working. My problem got solved. Thank
you for your suggestions. But soon I find time, I would try all of
them, so that I can learn more and what I try to do in this room to
get better angles over any problem from experts like you. Thank you
for giving me your valuable time despite your busy schedule.

Wishing you happy day ahead,
Best Regards,
Subhabrata.

==============================================================================
TOPIC: Python version of perl's "if (-T ..)" and "if (-B ...)"?
http://groups.google.com/group/comp.lang.python/t/8816441f153588dd?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Feb 12 2010 12:41 pm
From: Ben Finney


aahz@pythoncraft.com (Aahz) writes:

> In article <mailman.2434.1265983307.28905.python-list@python.org>,
> Lloyd Zusman <ljz@asfast.com> wrote:
> >if (-T $filename) { print "file contains 'text' characters\n"; }
> >if (-B $filename) { print "file contains 'binary' characters\n"; }
>
> Assuming you're on a Unix-like system or can install Cygwin, the
> standard response is to use the "file" command. It's *much* more
> sophisticated.

Indeed, the 'file' command is an expected (though not standard) part of
most Unix systems, and its use frees us from the lies of filenames about
their contents.

The sophistication comes from an extensive database of heuristics —
filesystem attributes, "magic" content signatures, and parsing — that
are together known as the "magic database". This database is maintained
along with the 'file' program, and made accessible through a C library
from the same code base called 'magic'.

So, you don't actually need to use the 'file' command to access this
sophistication. Just about every program on a GNU system that wants to
display file types, such as the graphical file manager, will query the
'magic' library directly to get the file type.

The 'file' code base has for a while now also included a Python
interface to this library, importable as the module name 'magic'.
Unfortunately it isn't registered at PyPI as far as I can tell. (There
are several project using the name "magic" that implement something
similar, but they are nowhere near as sophisticated.)

On a Debian GNU/Linux system, you can install the 'python-magic' package
to get this installed. Otherwise, you can build it from the 'file' code
base <URL:http://www.darwinsys.com/file/>.

--
\ "I don't accept the currently fashionable assertion that any |
`\ view is automatically as worthy of respect as any equal and |
_o__) opposite view." —Douglas Adams |
Ben Finney


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

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