Sunday, January 31, 2010

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

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

comp.lang.python@googlegroups.com

Today's topics:

* Can't get sys.stdin.readlines() to work - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/6fb92ef62afc438a?hl=en
* Python and Ruby - 12 messages, 6 authors
http://groups.google.com/group/comp.lang.python/t/dfe4f6c60032755e?hl=en
* PEP 3147 - new .pyc format - 2 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/7a0d8230a5907885?hl=en
* Keyboard input - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/9f18e65916b8c115?hl=en
* Slow down while creating a big list and iterating over it - 1 messages, 1
author
http://groups.google.com/group/comp.lang.python/t/327544bd08947046?hl=en
* create a string of variable lenght - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/ea070cfc61ccfd9c?hl=en
* ftp.storlines error - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/f67e054132789d17?hl=en
* gmtime - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/ffc89a42b4e893f6?hl=en
* recv_into(bytearray) complains about a "pinned buffer" - 1 messages, 1
author
http://groups.google.com/group/comp.lang.python/t/33a3eaafe2948d0c?hl=en
* iglob performance no better than glob - 2 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/d9a8617ec85e926d?hl=en

==============================================================================
TOPIC: Can't get sys.stdin.readlines() to work
http://groups.google.com/group/comp.lang.python/t/6fb92ef62afc438a?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Jan 31 2010 2:24 pm
From: tinnews@isbd.co.uk


Richard Thomas <chardster@gmail.com> wrote:
> On Jan 31, 6:15 pm, tinn...@isbd.co.uk wrote:
> > I'm trying to read some data from standard input, what I'm actually
> > trying to do is process some date pasted in using the mouse cut and
> > paste on a Linux box (xubuntu 9.10) in a terminal window.
> >
> > First attempts failed so I'm now trying the trivial:-
> >
> >     import sys
> >     data = sys.stdin.readlines()
> >     print "Counted", len(data), "lines."
> >
> > When I run this and try to paste something into the terminal window I
> > get the following errors:-
> >
> >     /home/chris/bin/m2r.py: line 2: syntax error near unexpected token `('
> >     /home/chris/bin/m2r.py: line 2: `data = sys.stdin.readlines()'
> >
> > It does exactly the same if I try:-
> >
> >     cat | m2r.py
> >
> > and then paste something into the window.
> >
> > So - what on earth am I doing wrong?
> >
> > --
> > Chris Green
>
> You haven't put a shebang line at the top.
>
> #!/usr/bin/env python
> import sys
> data = sys.stdin.readlines()
> ...

Urk!!! Silly me. That catches me out every few months! :-)

--
Chris Green


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

== 1 of 12 ==
Date: Sun, Jan 31 2010 2:36 pm
From: Steven D'Aprano


On Sun, 31 Jan 2010 04:28:41 -0800, Ed Keith wrote:

> In most functional languages you just name a function to access it and
> you do it ALL the time.
>
> for example, in if you have a function 'f' which takes two parameters to
> call the function and get the result you use:
>
> f 2 3
>
> If you want the function itself you use:
>
> f

How do you call a function of no arguments?

--
Steven


== 2 of 12 ==
Date: Sun, Jan 31 2010 2:37 pm
From: Steven D'Aprano


On Sun, 31 Jan 2010 14:47:08 -0600, John Bokma wrote:

> An editor can correct the indenting of the braces example but can't with
> this one.
>
> if x:
> if y:
> foo()
> else:
> bar()
>
> While braces might be considered redundant they are not when for one
> reason or another formatting is lost or done incorrectly.

I've heard this argument before, and I don't buy it. Why should we expect
the editor to correct malformed code?

Would you expect your editor to correct this malformed code?

result = sin(x+)y

Why should broken indentation be held to a higher standard than any other
breakage in code?

--
Steven


== 3 of 12 ==
Date: Sun, Jan 31 2010 3:40 pm
From: Chris Rebert


On Sun, Jan 31, 2010 at 2:36 PM, Steven D'Aprano
<steve@remove-this-cybersource.com.au> wrote:
> On Sun, 31 Jan 2010 04:28:41 -0800, Ed Keith wrote:
>> In most functional languages you just name a function to access it and
>> you do it ALL the time.
>>
>> for example, in if you have a function 'f' which takes two parameters to
>> call the function and get the result you use:
>>
>>  f 2 3
>>
>> If you want the function itself you use:
>>
>>    f
>
> How do you call a function of no arguments?

It's not really a function in that case, it's just a named constant.
(Recall that functions don't/can't have side-effects.)

Cheers,
Chris
--
http://blog.rebertia.com


== 4 of 12 ==
Date: Sun, Jan 31 2010 4:14 pm
From: Arnaud Delobelle


Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> writes:

> On Sun, 31 Jan 2010 04:28:41 -0800, Ed Keith wrote:
>
>> In most functional languages you just name a function to access it and
>> you do it ALL the time.
>>
>> for example, in if you have a function 'f' which takes two parameters to
>> call the function and get the result you use:
>>
>> f 2 3
>>
>> If you want the function itself you use:
>>
>> f
>
> How do you call a function of no arguments?

In a functional language, a function of no arguments will always return
the same value. So, from a non-functional point of vue, f is both the
function and its value.

--
Arnaud


== 5 of 12 ==
Date: Sun, Jan 31 2010 4:25 pm
From: Steven D'Aprano


On Sun, 31 Jan 2010 15:40:36 -0800, Chris Rebert wrote:

> On Sun, Jan 31, 2010 at 2:36 PM, Steven D'Aprano
> <steve@remove-this-cybersource.com.au> wrote:
>> On Sun, 31 Jan 2010 04:28:41 -0800, Ed Keith wrote:
>>> In most functional languages you just name a function to access it and
>>> you do it ALL the time.
>>>
>>> for example, in if you have a function 'f' which takes two parameters
>>> to call the function and get the result you use:
>>>
>>>  f 2 3
>>>
>>> If you want the function itself you use:
>>>
>>>    f
>>
>> How do you call a function of no arguments?
>
> It's not really a function in that case, it's just a named constant.
> (Recall that functions don't/can't have side-effects.)


>>> time.time(), random.random()
(1264983502.7505889, 0.29974255140479633)
>>> time.time(), random.random()
(1264983505.9283719, 0.74207867411026329)


They don't look terribly constant to me.


There is a difference between a function that does "give me whatever
value is specified by a fixed description" and a function that does "give
me a fixed value".

--
Steven


== 6 of 12 ==
Date: Sun, Jan 31 2010 4:27 pm
From: Ed Keith


--- On Sun, 1/31/10, Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> wrote:

> From: Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au>
> Subject: Re: Python and Ruby
> To: python-list@python.org
> Date: Sunday, January 31, 2010, 5:36 PM
> On Sun, 31 Jan 2010 04:28:41 -0800,
> Ed Keith wrote:
>
> > In most functional languages you just name a function
> to access it and
> > you do it ALL the time.
> >
> > for example, in if you have a function 'f' which takes
> two parameters to
> > call the function and get the result you use:
> >
> >  f 2 3
> >
> > If you want the function itself you use:
> >
> >    f
>
> How do you call a function of no arguments?
>
>

In a 'pure' functional language a function with no arguments is, by definition, a constant. This is because a 'pure' function will always return the same result whenever given the same arguments. so if it has no argument it always returns a constant value.

-EdK

Ed Keith
e_d_k@yahoo.com

Blog: edkeith.blogspot.com


== 7 of 12 ==
Date: Sun, Jan 31 2010 4:47 pm
From: John Bokma


Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> writes:

> On Sun, 31 Jan 2010 14:47:08 -0600, John Bokma wrote:
>
>> An editor can correct the indenting of the braces example but can't with
>> this one.
>>
>> if x:
>> if y:
>> foo()
>> else:
>> bar()
>>
>> While braces might be considered redundant they are not when for one
>> reason or another formatting is lost or done incorrectly.
>
> I've heard this argument before, and I don't buy it. Why should we expect
> the editor to correct malformed code?

Or a prettyfier. It doesn't matter. The point is that with braces there
*is* redundancy that be used to fix the code.

> Would you expect your editor to correct this malformed code?
>
> result = sin(x+)y

Nice straw man.

Let me repeat again: I am ok with how Python works. To be honest I think
it's cleaner compared to using {}. But in there are real life examples
in which Python code will break where code with braces will survive.

--
John Bokma j3b

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


== 8 of 12 ==
Date: Sun, Jan 31 2010 4:50 pm
From: Chris Rebert


On Sun, Jan 31, 2010 at 4:25 PM, Steven D'Aprano
<steven@remove.this.cybersource.com.au> wrote:
> On Sun, 31 Jan 2010 15:40:36 -0800, Chris Rebert wrote:
>> On Sun, Jan 31, 2010 at 2:36 PM, Steven D'Aprano
>> <steve@remove-this-cybersource.com.au> wrote:
>>> On Sun, 31 Jan 2010 04:28:41 -0800, Ed Keith wrote:
>>>> In most functional languages you just name a function to access it and
>>>> you do it ALL the time.
>>>>
>>>> for example, in if you have a function 'f' which takes two parameters
>>>> to call the function and get the result you use:
>>>>
>>>>  f 2 3
>>>>
>>>> If you want the function itself you use:
>>>>
>>>>    f
>>>
>>> How do you call a function of no arguments?
>>
>> It's not really a function in that case, it's just a named constant.
>> (Recall that functions don't/can't have side-effects.)
>
>
>>>> time.time(), random.random()
> (1264983502.7505889, 0.29974255140479633)
>>>> time.time(), random.random()
> (1264983505.9283719, 0.74207867411026329)
>
>
> They don't look terribly constant to me.

Those aren't functions in the pure functional programming sense; which
is unsurprising since Python isn't a [pure] functional language.
They both involve side-effects. time() does I/O to the clock chip to
see what time it is, and random() uses and changes a global seed value
variable (which, in a double-whammy, takes its initial value from
time()).

Pure functions must be referentially transparent
[http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)],
and as you've demonstrated, neither of those Python functions qualify.

Cheers,
Chris
--
http://blog.rebertia.com


== 9 of 12 ==
Date: Sun, Jan 31 2010 4:53 pm
From: John Bokma


Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> writes:

> On Sun, 31 Jan 2010 14:47:08 -0600, John Bokma wrote:
>
>> An editor can correct the indenting of the braces example but can't with
>> this one.
>>
>> if x:
>> if y:
>> foo()
>> else:
>> bar()
>>
>> While braces might be considered redundant they are not when for one
>> reason or another formatting is lost or done incorrectly.
>
> I've heard this argument before, and I don't buy it. Why should we expect
> the editor to correct malformed code?

I do expect my editor to assist me in coding. In Emacs I have to do some
effort to enter the broken C code in the earlier post, and when I
reformat the code, it will be lined out correctly. I can't do that with
the above example, because it's correctly formatted.

You don't have to buy my argument, I am not selling it.

While in the past I wrote that an editor can't make you that more
productive I want to take that back, on the record. Since I've switched
to Emacs the editor has saved me several times from minor issues. Either
because it refused to indent correctly thanks to a missing closing }, ), ]
or other error. With the correct mode in Emacs one gets, in my
experience, immediate feedback when making mistakes one otherwise find
during the run/compiling phase.

Note that I am also not selling Emacs. It's free after all.

--
John Bokma j3b

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


== 10 of 12 ==
Date: Sun, Jan 31 2010 5:22 pm
From: Steven D'Aprano


On Sun, 31 Jan 2010 16:50:50 -0800, Chris Rebert wrote:

>>>> How do you call a function of no arguments?
>>>
>>> It's not really a function in that case, it's just a named constant.
>>> (Recall that functions don't/can't have side-effects.)
>>
>>
>>>>> time.time(), random.random()
>> (1264983502.7505889, 0.29974255140479633)
>>>>> time.time(), random.random()
>> (1264983505.9283719, 0.74207867411026329)
>>
>>
>> They don't look terribly constant to me.
>
> Those aren't functions in the pure functional programming sense; which
> is unsurprising since Python isn't a [pure] functional language. They
> both involve side-effects. time() does I/O to the clock chip to see what
> time it is, and random() uses and changes a global seed value variable
> (which, in a double-whammy, takes its initial value from time()).

Yes, but these tasks -- get the time, get a (pseudo) random number -- are
not unique to Python. Surely even Lisp and Haskell code will sometimes
need to know the time. Whether they are "pure functions" (functions in
the mathematical sense) or impure, they're still functions in some sense.
How do you deal with such impure functions?

--
Steven


== 11 of 12 ==
Date: Sun, Jan 31 2010 5:31 pm
From: Ed Keith


--- On Sun, 1/31/10, Steven D'Aprano <steven@REMOVE.THIS.cybersource.com.au> wrote:

> From: Steven D'Aprano <steven@REMOVE.THIS.cybersource.com.au>
> Subject: Re: Python and Ruby
> To: python-list@python.org
> Date: Sunday, January 31, 2010, 8:22 PM
> On Sun, 31 Jan 2010 16:50:50 -0800,
> Chris Rebert wrote:
>
> >>>> How do you call a function of no
> arguments?
> >>>
> >>> It's not really a function in that case, it's
> just a named constant.
> >>> (Recall that functions don't/can't have
> side-effects.)
> >>
> >>
> >>>>> time.time(), random.random()
> >> (1264983502.7505889, 0.29974255140479633)
> >>>>> time.time(), random.random()
> >> (1264983505.9283719, 0.74207867411026329)
> >>
> >>
> >> They don't look terribly constant to me.
> >
> > Those aren't functions in the pure functional
> programming sense; which
> > is unsurprising since Python isn't a [pure] functional
> language. They
> > both involve side-effects. time() does I/O to the
> clock chip to see what
> > time it is, and random() uses and changes a global
> seed value variable
> > (which, in a double-whammy, takes its initial value
> from time()).
>
> Yes, but these tasks -- get the time, get a (pseudo) random
> number -- are
> not unique to Python. Surely even Lisp and Haskell code
> will sometimes
> need to know the time. Whether they are "pure functions"
> (functions in
> the mathematical sense) or impure, they're still functions
> in some sense.
> How do you deal with such impure functions?
>
>
>

You pass it a monad (http://en.wikipedia.org/wiki/Monad_(functional_programming)).

-EdK

Ed Keith
e_d_k@yahoo.com

Blog: edkeith.blogspot.com


== 12 of 12 ==
Date: Sun, Jan 31 2010 5:33 pm
From: Steven D'Aprano


On Sun, 31 Jan 2010 18:47:42 -0600, John Bokma wrote:

> Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> writes:
>
>> On Sun, 31 Jan 2010 14:47:08 -0600, John Bokma wrote:
>>
>>> An editor can correct the indenting of the braces example but can't
>>> with this one.
>>>
>>> if x:
>>> if y:
>>> foo()
>>> else:
>>> bar()
>>>
>>> While braces might be considered redundant they are not when for one
>>> reason or another formatting is lost or done incorrectly.
>>
>> I've heard this argument before, and I don't buy it. Why should we
>> expect the editor to correct malformed code?
>
> Or a prettyfier. It doesn't matter. The point is that with braces there
> *is* redundancy that be used to fix the code.

Prettyfiers are significant in languages that allow braces (or begin/end
tokens) and indentation to go out of sync. Since that can't happen with
Python, it's not a problem that needs solving. Prettyfiers exist to work
around a limitation of languages where indentation is not significant.

Arguing that an advantage of braces is that prettyfiers can work with
them easily is a silly argument. That's like saying an advantage of horse-
drawn buggies over cars is that it will go faster when you hit the horse
with a whip. That's true, but you only need the whip because of the lack
of accelerator pedal.

A much better argument would be that an advantage of significant
indentation is that you no longer need a prettyfier, and a second
advantage is a major reduction in flame wars over coding styles.

>> Would you expect your editor to correct this malformed code?
>>
>> result = sin(x+)y
>
> Nice straw man.


It's not a straw man. It's a serious argument. There is an infinite
number of problems with malformed code that your editor can't fix, and
your prettifier can't deal with. Why should we care if indentation is one
more?

There are tools out there (such as some web forum software) that corrupt
whitespace. Those tools are broken, and if you (generic you), as a
developer, are relying on those tools to code with, then shame on you.
And if you're the creator of such broken tools, then shame on you more.
You wouldn't arbitrarily decide to remove leading "E"s from the user's
text, or trailing full stops, so why do you arbitrarily remove whitespace?

> Let me repeat again: I am ok with how Python works. To be honest I think
> it's cleaner compared to using {}. But in there are real life examples
> in which Python code will break where code with braces will survive.

Yes. So what? They are rare and insignificant in practice. For every line
of code that you get via a webforum that mangles indentation, you get ten
thousand lines of code from some place that doesn't.

So long as code is written by and for human beings, the benefit of
significant indentation is 100% practical, and the practical benefit of
braces will remain insignificant.

--
Steven

==============================================================================
TOPIC: PEP 3147 - new .pyc format
http://groups.google.com/group/comp.lang.python/t/7a0d8230a5907885?hl=en
==============================================================================

== 1 of 2 ==
Date: Sun, Jan 31 2010 2:37 pm
From: Steven D'Aprano


On Sun, 31 Jan 2010 09:06:18 -0600, John Bokma wrote:

> Based on the magic numbers I've seen so far it looks like that not an
> option. They increment with every minor change.

They increment with every *incompatible* change to the marshal format,
not every change to the compiler.

> So to me, at this moment
> (and maybe it's my ignorance) it looks like a made up example to justify
> what to me still looks like a bad decision.

Of course it's a made-up example. But with Python now entering a period
where there is a moratorium on changes to the language, is it really so
difficult to imagine that the marshal format will settle down for a while
even as the standard library goes through upgrades?

--
Steven


== 2 of 2 ==
Date: Sun, Jan 31 2010 2:40 pm
From: Steven D'Aprano


On Sun, 31 Jan 2010 14:10:34 -0800, Dennis Lee Bieber wrote:

> Ugh... That would mean that for an application using, say 20
> files,
> one now has 20 subdirectories for what, in a lot of cases, will contain
> just one file each (and since I doubt older Python's will be modified to
> support this scheme, it will only be applicable to 3.x, and maybe a
> 2.7?)

If you only use one version of Python, then don't run it with the -R
switch.

Have you read the PEP? It is quite explicit that the default behaviour
of .pyc files will remain unchanged, that to get the proposed behaviour
you have to specifically ask for it.

--
Steven

==============================================================================
TOPIC: Keyboard input
http://groups.google.com/group/comp.lang.python/t/9f18e65916b8c115?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Jan 31 2010 2:40 pm
From: Steven D'Aprano


On Sun, 31 Jan 2010 14:10:34 -0800, Dennis Lee Bieber wrote:

> On Sun, 31 Jan 2010 11:51:46 +0000, "Mr.SpOOn" <mr.spoon21@gmail.com>
> declaimed the following in gmane.comp.python.general:
>
>> 2010/1/29 Gabriel Genellina <gagsl-py2@yahoo.com.ar>:
>> >
>> > That's strange. If you're using Linux, make sure you have the
>> > readline package installed.
>>
>> I'm using Linux. Ubuntu. I checked on synaptic and I have
>> readline-common. Do you mean something else?
>>
> But was your Python /built/ using readline?


How do you ensure that it is?

--
Steven

==============================================================================
TOPIC: Slow down while creating a big list and iterating over it
http://groups.google.com/group/comp.lang.python/t/327544bd08947046?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Jan 31 2010 2:42 pm
From: MRAB


marc magrans de abril wrote:
> Hi!
>
> ...I have found a good enough solution, although it only works if the
> number of patterns (clusters) is not very big:
> def classify(f):
> THERESHOLD=0.1
>
> patterns={}
> for l in enumerate(f):
> found = False
> for p,c in patterns.items():
> if dist(l,p) < THERESHOLD:
> found=True
> patterns[p] = c +1
>
> if not found:
> patterns[l] = 1
>
> return patterns
>
> This algorithm is O(n*np*m^2). Where n is the number of logs, np the
> number of patterns, and m is the log length (i.e. m^2 is the distance
> cost). So it's way better O(n^2*m^2) and I can run it for some hours
> to get back the results.
>
> I wonder if there is a single threaded/process clustering algorithm
> than runs in O(n)?
>
Your original code used the first entry in the remaining logs for each
pattern, but your new code stores the patterns in a dict, which is
unordered, so you might get different results.

But that doesn't matter, because your new code increments the count when
it has found a match, and then continues looking, so it might match and
increment more than once.

Finally, your original code treated it as a match if distance <=
threshold but your new code treats it as a match if distance <
threshold.

patterns = []
for x in logs:
for index, (pat, count) in enumerate(patterns):
if dist(pat, x) <= THRESHOLD:
patterns[index] = pat, count + 1
break
else:
# Didn't break out of the loop, therefore no match.
patterns.append((x, 1))

==============================================================================
TOPIC: create a string of variable lenght
http://groups.google.com/group/comp.lang.python/t/ea070cfc61ccfd9c?hl=en
==============================================================================

== 1 of 2 ==
Date: Sun, Jan 31 2010 2:49 pm
From: MRAB


Tracubik wrote:
> Il Sun, 31 Jan 2010 13:46:16 +0100, Günther Dietrich ha
> scritto:
>
>> Maybe you might solve this if you decode your string to unicode.
>> Example:
>>
>> |>>> euro = "€"
>> |>>> len(euro)
>> |3
>> |>>> u_euro = euro.decode('utf_8')
>> |>>> len(u_euro)
>> |1
>>
>> Adapt the encoding ('utf_8' in my example) to whatever you use.
>>
>> Or create the unicode string directly:
>>
>> |>>> u_euro = u'€'
>> |>>> len(u_euro)
>> |1
>>
>>
>>
>> Best regards,
>>
>> Günther
>
> thank you, your two solution is really interesting.
> is there a possible to set unicode encoding by default for my python
> scripts?
> i've tried inserting
> # -*- coding: utf-8 -*-
>
> at the beginning of my script but doesn't solve the problem

That tells Python which encoding the file is using, but you still need
to save the file in that encoding.


== 2 of 2 ==
Date: Sun, Jan 31 2010 4:54 pm
From: Benjamin Kaplan


On Sun, Jan 31, 2010 at 5:12 PM, Tracubik <affdfsdfdsfsd@b.com> wrote:
> Il Sun, 31 Jan 2010 13:46:16 +0100, Günther Dietrich ha
> scritto:
>
>> Maybe you might solve this if you decode your string to unicode.
>> Example:
>>
>> |>>> euro = "€"
>> |>>> len(euro)
>> |3
>> |>>> u_euro = euro.decode('utf_8')
>> |>>> len(u_euro)
>> |1
>>
>> Adapt the encoding ('utf_8' in my example) to whatever you use.
>>
>> Or create the unicode string directly:
>>
>> |>>> u_euro = u'€'
>> |>>> len(u_euro)
>> |1
>>
>>
>>
>> Best regards,
>>
>> Günther
>
> thank you, your two solution is really interesting.
> is there a possible to set unicode encoding by default for my python
> scripts?
> i've tried inserting
> # -*- coding: utf-8 -*-
>
> at the beginning of my script but doesn't solve the problem


First of all, if you haven't read this before, please do. It will make
this much clearer.
http://www.joelonsoftware.com/articles/Unicode.html

To reiterate: UTF-8 IS NOT UNICODE!!!!

In Python 2, '*' signifies a byte string. It is read as a sequence of
bytes and interpreted as a sequence of bytes When Python encounters
the sequence 0x27 0xe2 0x82 0xac 0x27 in the code (the UTF-8 bytes for
'€') it interprets it as 3 bytes between the two quotes. It doesn't
care about characters or anything like that. u'*' signifies a Unicode
string. Python will attempt to convert the sequence of bytes into a
sequence of characters. It can use any encoding for that: cp1252,
utf-8, MacRoman, ISO-8859-15. UTF-8 isn't special, it's just one of
the few encodings capable of storing all of the possible Unicode
characters.

What the line at the top says is that the file should be read using
UTF-8. Byte strings are still just sequences of bytes- this doesn't
affect them. But any Unicode string will be decoded using UTF-8. IF
python looks at the above sequence of bytes as a Unicode string, it
views the 3 bytes as a single character. When you ask for it's length,
it returns the number of characters.

Solution to your problem: in addition to keeping the #-*- coding ...
line, go with Günther's advice and use Unicode strings.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

==============================================================================
TOPIC: ftp.storlines error
http://groups.google.com/group/comp.lang.python/t/f67e054132789d17?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Jan 31 2010 3:06 pm
From: Mik0b0


On Feb 1, 12:19 am, Chris Rebert <c...@rebertia.com> wrote:
> On Sun, Jan 31, 2010 at 2:07 PM, Mik0b0 <new...@gmail.com> wrote:
> > Good day/night/etc.
> > I am rather a newb in Python (learning Python 3). I am trying to
> > create a small script for FTP file uploads  on my home network. The
> > script looks like this:
>
> > from ftplib import FTP
> > ftp=FTP('10.0.0.1')
> > ftp.login('mike','*****')
> > directory='/var/www/blabla/'
> > ftp.cwd(directory)
> > ftp.retrlines('LIST')
> > print('<- - - - - - - - - >')
> > file_to_change='test'
> > file=1
> > file=open(file_to_change,'w')
>
> Here you open the file in (w)rite mode. Also, don't call it `file` as
> that shadows the name of the built-in type.
>
> > text='test'
> > file.write(text)
>
> And indeed, here you've written something to it.
>
> > ftp.storlines('STOR ' + file_to_change,file)
>
> storlines() needs a file opened in (r)ead mode however, hence the
> error. Obviously, it needs to read the contents of the file in order
> to send it over the network.
> Either do the writing separately, close the file, and then open it
> again in read mode, or open it in one of the modes that allows for
> both reading and writing [see help(open) for details].
>
> Cheers,
> Chris
> --http://blog.rebertia.com
>
> > ftp.retrlines('LIST')
> > file.close()
>
> > The output is like this:
> > Traceback (most recent call last):
> >  File "ftp.py", line 13, in <module>
> >    ftp.storlines('STOR ' + file_to_change,i)
> >  File "/usr/lib/python3.1/ftplib.py", line 474, in storlines
> >    buf = fp.readline()
> > IOError: not readable
>
> > What is wrong?

Thanks Chris,
the final version looks like this and it works:

from ftplib import FTP
ftp=FTP('10.0.0.1')
ftp.login('mike','******')
directory='/var/www/blabla/'
ftp.cwd(directory)
ftp.retrlines('LIST')
print('<- - - - - - - - - >')
file_to_change='test.php'
i=1
i=open(file_to_change,'w')
text='test'
i.write(text)
i.close()
i=open(file_to_change,'rb')
ftp.storbinary('STOR ' + file_to_change,i)
ftp.retrlines('LIST')
i.close()

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

== 1 of 2 ==
Date: Sun, Jan 31 2010 4:01 pm
From: gazza


On Jan 31, 3:27 pm, gazza <burslem2...@yahoo.com> wrote:
> Hi,
>
> I am trying to discover how to obtain the correct time of say CST/
> America and EST/America in python?
>
> Any help on this would be appreciated.
>
> Thanks,
> Garyc

I found some information. Someone suggested I use the pytz library?

Cheers,
Garyc


== 2 of 2 ==
Date: Sun, Jan 31 2010 5:19 pm
From: pograph


On Jan 31, 4:01 pm, gazza <burslem2...@yahoo.com> wrote:
> On Jan 31, 3:27 pm, gazza <burslem2...@yahoo.com> wrote:
>
> > Hi,
>
> > I am trying to discover how to obtain the correct time of say CST/
> > America and EST/America in python?
>
> > Any help on this would be appreciated.
>
> > Thanks,
> > Garyc
>
> I found some information. Someone suggested I use the pytz library?
>
> Cheers,
> Garyc

tz = pytz.timezone('US/Pacific')
t = datetime.datetime.now(tz)

==============================================================================
TOPIC: recv_into(bytearray) complains about a "pinned buffer"
http://groups.google.com/group/comp.lang.python/t/33a3eaafe2948d0c?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Jan 31 2010 4:04 pm
From: Antoine Pitrou

Hello Andrew,

> I don't even know what a "pinned buffer" means, and searching python.org
> isn't helpful.
>
> Using a bytearray in Python 3.1.1 *does* work:
> [...]

Agreed, the error message is cryptic.
The problem is that socket.recv_into() in 2.6 doesn't recognize the new
buffer API which is needed to accept bytearray objects.
(it does in 3.1, because the old buffer API doesn't exist anymore there)

You could open an issue on the bug tracker for this.

Thank you

Antoine.


==============================================================================
TOPIC: iglob performance no better than glob
http://groups.google.com/group/comp.lang.python/t/d9a8617ec85e926d?hl=en
==============================================================================

== 1 of 2 ==
Date: Sun, Jan 31 2010 4:22 pm
From: Kyp


On Jan 31, 1:06 pm, John Bokma <j...@castleamber.com> wrote:
> Kyp <k...@stsci.edu> writes:
> > Is there a way to get the first X # of files from a dir with lots of
> > files, that does not take a long time to run?
>
> Assuming Linux: what does time
>
>  ls thedir | head
>
> give?
>
> with thedir the name of the actual dir
about 3 seconds.

3.086u 0.201s 0:03.32 98.7% 0+0k 0+0io 0pf+0w

>
> Also how many is many files?
over 100K (I know I should not do that, but it's a temp dir holding
files to be transferred)
thanx, mark

== 2 of 2 ==
Date: Sun, Jan 31 2010 4:23 pm
From: Kyp


On Jan 31, 2:44 pm, Peter Otten <__pete...@web.de> wrote:
> Kyp wrote:
> > I have a dir with a large # of files that I need to perform operations
> > on, but only needing to access a subset of the files, i.e. the first
> > 100 files.
>
> > Using glob is very slow, so I ran across iglob, which returns an
> > iterator, which seemed just like what I wanted. I could iterate over
> > the files that I wanted, not having to read the entire dir.
>
> > So the iglob was faster, but accessing the first file took about the
> > same time as glob.glob.
>
> > Here's some code to compare glob vs. iglob performance,  it outputs
> > the time before/after a glob.iglob('*.*') files.next() sequence and a
> > glob.glob('*.*') sequence.
>
> > #!/usr/bin/env python
>
> > import glob,time
> > print '\nTest of glob.iglob'
> > print 'before       iglob:', time.asctime()
> > files = glob.iglob('*.*')
> > print 'after        iglob:',time.asctime()
> > print files.next()
> > print 'after files.next():', time.asctime()
>
> > print '\nTest of glob.glob'
> > print 'before        glob:', time.asctime()
> > files = glob.glob('*.*')
> > print 'after         glob:',time.asctime()
>
> > Here are the results:
>
> > Test of glob.iglob
> > before       iglob: Sun Jan 31 11:09:08 2010
> > after        iglob: Sun Jan 31 11:09:08 2010
> > foo.bar
> > after files.next(): Sun Jan 31 11:09:59 2010
>
> > Test of glob.glob
> > before        glob: Sun Jan 31 11:09:59 2010
> > after         glob: Sun Jan 31 11:10:51 2010
>
> > The results are about the same for the 2 approaches, both took about
> > 51 seconds. Am I doing something wrong with iglob?
>
> No, but iglob() being lazy is pointless in your case because it uses
> os.listdir() and fnmatch.filter() underneath which both read the whole
> directory before returning anything.
>
> > Is there a way to get the first X # of files from a dir with lots of
> > files, that does not take a long time to run?
>
> Here's my attempt. It turned out to be more work than expected, so I cut a
> few corners. It's Linux-only "works on my machine" code, but may give you
> some hints on how to proceed.
>
> from ctypes import *
> import fnmatch
> import glob
> import os
> import re
> from itertools import ifilter, imap
>
> class dirent(Structure):
>     "works on my machine ;)"
>     _fields_ = [
>         ("d_ino", c_long),
>         ("d_off", c_long),
>         ("d_reclen", c_ushort),
>         ("d_type", c_ubyte),
>         ("d_name", c_char*256)]
>
> direntp = POINTER(dirent)
>
> LIBC = "libc.so.6"
> cdll.LoadLibrary(LIBC)
> libc = CDLL(LIBC)
> libc.readdir.restype = direntp
>
> def diriter(dir):
>     "lazy partial replacement for os.listdir()"
>     # errors? what errors?
>     dirp = libc.opendir(dir)
>     if not dirp:
>         return
>     try:
>         while True:
>             ep = libc.readdir(dirp)
>             if not ep:
>                 break
>             yield ep.contents.d_name
>     finally:
>         libc.closedir(dirp)
>
> def filter(names, pattern):
>     "lazy partial replacement for fnmatch.filter()"
>     import posixpath
>
>     pattern = os.path.normcase(pattern)
>     r = fnmatch.translate(pattern)
>     r = re.compile(r)
>
>     if os.path is not posixpath:
>         names = imap(os.path.normcase, names)
>
>     return ifilter(r.match, names)
>
> def globiter(path):
>     "lazy partial replacement for glob.glob()"
>     dir, filename = os.path.split(path)
>     if glob.has_magic(dir):
>         raise ValueError("wildcards in directory not supported")
>     return filter(diriter(dir), filename)
>
> if __name__ == "__main__":
>     import sys
>     [pattern] = sys.argv[1:]
>     for name in globiter(pattern):
>         print name
>
> Peter

I'll give it a try, thanx for the reply.
mark


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

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