Thursday, October 10, 2013

comp.lang.python - 26 new messages in 11 topics - digest

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

comp.lang.python@googlegroups.com

Today's topics:

* UnicodeEncodeError: SOLVED - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/39960869dabdc2e5?hl=en
* öpcaö variable refrenced before assignment - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.python/t/f666067b26ec9740?hl=en
* Cookie gets changed when hit comes from a referrer - 10 messages, 6 authors
http://groups.google.com/group/comp.lang.python/t/cfa500a0029ed49c?hl=en
* datetime.timedelta.replace? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/b414b8511c71a1f8?hl=en
* Receive packet using socket - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/f4211c0bdf331fdf?hl=en
* super in Python 3 and variadic arguments - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/8d88f3f1fbaff513?hl=en
* Code golf challenge: XKCD 936 passwords - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/62194e48e062f4f9?hl=en
* Applying 4x4 transformation to 3-element vector with numpy - 1 messages, 1
author
http://groups.google.com/group/comp.lang.python/t/7fc89830bf619c45?hl=en
* Re for Apache log file format - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/7daaed832e3364d3?hl=en
* PROPOSAL: PyCons in Africa - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/545d371a081fbf5c?hl=en
* class-private names and the Zen of Python - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/7c936ee0b3017744?hl=en

==============================================================================
TOPIC: UnicodeEncodeError: SOLVED
http://groups.google.com/group/comp.lang.python/t/39960869dabdc2e5?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Oct 9 2013 7:41 am
From: Walter Hurry


Many thanks to those prepared to forgive my transgression in the
'Goodbye' thread. I mentioned there that I was puzzled by a
UnicodeEncodeError, and said I would rise it as a separate thread.

However, via this link, I was able to resolve the issue myself:

http://stackoverflow.com/questions/3224268/python-unicode-encode-error

Nevertheless, thanks again for the kind words.





==============================================================================
TOPIC: öpcaö variable refrenced before assignment
http://groups.google.com/group/comp.lang.python/t/f666067b26ec9740?hl=en
==============================================================================

== 1 of 4 ==
Date: Wed, Oct 9 2013 7:41 am
From: Chris Angelico


On Thu, Oct 10, 2013 at 1:20 AM, <markotaht@gmail.com> wrote:
> def koguarv_ridu failis(f):
> for i, l in enumerate(f):
> pass
> return i+1

This will throw the exception you're seeing (by the way, it helps a
LOT to actually copy and paste the full error, including the traceback
- fortunately I can work this one out without) if the enumerate()
doesn't yield any results. The whole loop gets skipped, nothing gets
assigned to i. But the real question is: why are you not getting
anything to enumerate?

> def palgad(f4):
> palgad = 0
> while True:
> f4r = f4.readline()
> if f4r == "":
> break
> palgad += int(f4r[f4r.find(";")+1:])
> return palgad
>
> def kuu_keskmine(palgad, f):
> return palgad/koguarv_ridu_failis(f)

And this would be why. Your first function is consuming the whole file
(up to a blank line, but I'm guessing your file doesn't have any), and
there's nothing left for ridu to read.

But first, a word on naming. You've used the name palgad in four distinct ways:

1) The function introduced in 'def palgad(f4)'
2) A local variable inside #1, which accumulates the returned integer
3) A local variable inside keskmine, which happens to be passed the
value that #1 returned
4) The file name, palgad.txt

This is not a problem to the interpreter, as they're quite separate,
but your first three senses are very confusing to a human.

So. You have a major problem here in that you're calculating a number
and then trying to divide it by the number of lines. There's a much
MUCH simpler, cleaner, _and_ safer way to do that: just count up the
lines at the same time as you calculate palgad. I'll let you do the
specifics, but that's what I would advise you to explore :)

Best of luck!

ChrisA




== 2 of 4 ==
Date: Wed, Oct 9 2013 7:52 am
From: Jussi Piitulainen


markotaht@gmail.com writes:

> fail4 = "palgad.txt"
>
> f4 = open(fail4, "r")
>
> def koguarv_ridu failis(f):
> for i, l in enumerate(f):
> pass
> return i+1
>
> def palgad(f4):
> palgad = 0
> while True:
> f4r = f4.readline()
> if f4r == "":
> break
> palgad += int(f4r[f4r.find(";")+1:])
> return palgad
>
> def kuu_keskmine(palgad, f):
> return palgad/koguarv_ridu_failis(f)
>
> print(kuu_keskmine(palgad(f4), f4))
>
>
> Why does it give me local variable "i" referenced before assignment
> in koguarv_ridu_failis(f) on the return i+1 line

Because palgad(f4) consumed f, the loop in koguarv_ridu_failis is not
executed even once.

> But if i do directly koguarv_ridu_failis(f4) then i get correct
> antswer.

Try to do just koguarv_ridu_failis(f4) twice. You'll get the same
error on the second attempt.




== 3 of 4 ==
Date: Wed, Oct 9 2013 8:15 am
From: markotaht@gmail.com


So i got it working, by saving palgad in a variable, before printing it and i count the lines into a global variable. Ty




== 4 of 4 ==
Date: Wed, Oct 9 2013 8:37 am
From: Mark Lawrence



On 09/10/2013 16:15, markotaht@gmail.com wrote:
> So i got it working, by saving palgad in a variable, before printing it and i count the lines into a global variable. Ty
>

You are hereby placed in detention for one hour this evening. You will
spend the whole hour writing repeatedly "I must remember to place things
in context when replying to the Python main mailing list/news group".
Do you understand this?

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence






==============================================================================
TOPIC: Cookie gets changed when hit comes from a referrer
http://groups.google.com/group/comp.lang.python/t/cfa500a0029ed49c?hl=en
==============================================================================

== 1 of 10 ==
Date: Wed, Oct 9 2013 7:43 am
From: Denis McMahon


On Wed, 09 Oct 2013 01:52:44 +0300, Νίκος Αλεξόπουλος wrote:

> Στις 8/10/2013 10:29 μμ, ο/η Denis McMahon έγραψε:

>> Have you checked the cookie jar in the browser to see what value the
>> cookie has? Is that the value you think it should have? Note that
>> checking the cookie jar is a browser topic, not a python topic, so if
>> you don't know how to do that you're going to have to find the right
>> place to ask, WHICH IS NOT HERE!

>> Ideally you need to check what the server thinks it's setting the
>> cooking to, what the browser thinks it received as the cookie, and what
>> the server gets back afterwards to work out where the error is
>> happening.

> Is there something i can try to isolate the problem and make it work?
> By whole counters project is based on cookie handling now....

See those last two paragraphs there that you quoted. You should have read
them.

--
Denis McMahon, denismfmcmahon@gmail.com




== 2 of 10 ==
Date: Wed, Oct 9 2013 7:45 am
From: Denis McMahon


On Wed, 09 Oct 2013 11:24:35 +0300, Νίκος Αλεξόπουλος wrote:

> Please someone esle try to reproduce the problem by just using cgi and
> not mod_wsgi.

I have no intention of reconfiguring my web server just to prove that
your code isn't working. We already know that your code isn't working.

--
Denis McMahon, denismfmcmahon@gmail.com




== 3 of 10 ==
Date: Wed, Oct 9 2013 8:00 am
From: Νίκος Αλεξόπουλος


Στις 9/10/2013 5:43 μμ, ο/η Denis McMahon έγραψε:
> On Wed, 09 Oct 2013 01:52:44 +0300, Νίκος Αλεξόπουλος wrote:
>
>> Στις 8/10/2013 10:29 μμ, ο/η Denis McMahon έγραψε:
>
>>> Have you checked the cookie jar in the browser to see what value the
>>> cookie has? Is that the value you think it should have? Note that
>>> checking the cookie jar is a browser topic, not a python topic, so if
>>> you don't know how to do that you're going to have to find the right
>>> place to ask, WHICH IS NOT HERE!
>
>>> Ideally you need to check what the server thinks it's setting the
>>> cooking to, what the browser thinks it received as the cookie, and what
>>> the server gets back afterwards to work out where the error is
>>> happening.
>
>> Is there something i can try to isolate the problem and make it work?
>> By whole counters project is based on cookie handling now....
>
> See those last two paragraphs there that you quoted. You should have read
> them.
>
ok so then tell me where i should ask this.

--
What is now proved was at first only imagined! & WebHost
<http://superhost.gr>




== 4 of 10 ==
Date: Wed, Oct 9 2013 8:20 am
From: Joel Goldstick


On Wed, Oct 9, 2013 at 11:00 AM, Νίκος Αλεξόπουλος
<nikos.gr33k@gmail.com> wrote:
> Στις 9/10/2013 5:43 μμ, ο/η Denis McMahon έγραψε:
>
>> On Wed, 09 Oct 2013 01:52:44 +0300, Νίκος Αλεξόπουλος wrote:
>>
>>> Στις 8/10/2013 10:29 μμ, ο/η Denis McMahon έγραψε:
>>
>>
>>>> Have you checked the cookie jar in the browser to see what value the
>>>> cookie has? Is that the value you think it should have? Note that
>>>> checking the cookie jar is a browser topic, not a python topic, so if
>>>> you don't know how to do that you're going to have to find the right
>>>> place to ask, WHICH IS NOT HERE!
>>
>>
>>>> Ideally you need to check what the server thinks it's setting the
>>>> cooking to, what the browser thinks it received as the cookie, and what
>>>> the server gets back afterwards to work out where the error is
>>>> happening.
>>
>>
>>> Is there something i can try to isolate the problem and make it work?
>>> By whole counters project is based on cookie handling now....
>>
>>
>> See those last two paragraphs there that you quoted. You should have read
>> them.
>>
> ok so then tell me where i should ask this.
>


I already told you where to learn about cookies. Try that first.
People will be nicer to your questions if you show you are willing to
come prepared. As to where to ask -- perhaps there is an http mailing
list. Your code can't work if you don't know how cookies work.
--
Joel Goldstick
http://joelgoldstick.com




== 5 of 10 ==
Date: Wed, Oct 9 2013 8:39 am
From: Mark Lawrence



On 09/10/2013 16:00, Νίκος Αλεξόπουλος wrote:

> ok so then tell me where i should ask this.
>

Google, bing, duckduckgo, ask, yahoo ...

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence





== 6 of 10 ==
Date: Wed, Oct 9 2013 11:06 am
From: Denis McMahon


On Wed, 09 Oct 2013 18:00:28 +0300, Νίκος Αλεξόπουλος wrote:

> Στις 9/10/2013 5:43 μμ, ο/η Denis McMahon έγραψε:
>> On Wed, 09 Oct 2013 01:52:44 +0300, Νίκος Αλεξόπουλος wrote:
>>
>>> Στις 8/10/2013 10:29 μμ, ο/η Denis McMahon έγραψε:
>>
>>>> Have you checked the cookie jar in the browser to see what value the
>>>> cookie has? Is that the value you think it should have? Note that
>>>> checking the cookie jar is a browser topic, not a python topic, so if
>>>> you don't know how to do that you're going to have to find the right
>>>> place to ask, WHICH IS NOT HERE!

>>>> Ideally you need to check what the server thinks it's setting the
>>>> cooking to, what the browser thinks it received as the cookie, and
>>>> what the server gets back afterwards to work out where the error is
>>>> happening.

>>> Is there something i can try to isolate the problem and make it work?
>>> By whole counters project is based on cookie handling now....

>> See those last two paragraphs there that you quoted. You should have
>> read them.

> ok so then tell me where i should ask this.

In those two paragraphs I have told you what you need to do to isolate
where your problem is occurring. If you don't know how to do that, then I
commend to you the wealth of information that may be discovered using
search engines. However, as we keep telling you, this group is not the
right place to ask questions such as:

a) How do I see what's in my web browser's cookie jar?
b) How do I make my web server log the cookies it sends and receives?

For (a) you want information about your browser, not about python.
For (b) you want information about your web server, not about python.

Find the relevant forums and ask in them.

--
Denis McMahon, denismfmcmahon@gmail.com




== 7 of 10 ==
Date: Wed, Oct 9 2013 11:06 am
From: Denis McMahon


On Wed, 09 Oct 2013 18:00:28 +0300, Νίκος Αλεξόπουλος wrote:

> Στις 9/10/2013 5:43 μμ, ο/η Denis McMahon έγραψε:
>> On Wed, 09 Oct 2013 01:52:44 +0300, Νίκος Αλεξόπουλος wrote:
>>
>>> Στις 8/10/2013 10:29 μμ, ο/η Denis McMahon έγραψε:
>>
>>>> Have you checked the cookie jar in the browser to see what value the
>>>> cookie has? Is that the value you think it should have? Note that
>>>> checking the cookie jar is a browser topic, not a python topic, so if
>>>> you don't know how to do that you're going to have to find the right
>>>> place to ask, WHICH IS NOT HERE!

>>>> Ideally you need to check what the server thinks it's setting the
>>>> cooking to, what the browser thinks it received as the cookie, and
>>>> what the server gets back afterwards to work out where the error is
>>>> happening.

>>> Is there something i can try to isolate the problem and make it work?
>>> By whole counters project is based on cookie handling now....

>> See those last two paragraphs there that you quoted. You should have
>> read them.

> ok so then tell me where i should ask this.

In those two paragraphs I have told you what you need to do to isolate
where your problem is occurring. If you don't know how to do that, then I
commend to you the wealth of information that may be discovered using
search engines. However, as we keep telling you, this group is not the
right place to ask questions such as:

a) How do I see what's in my web browser's cookie jar?
b) How do I make my web server log the cookies it sends and receives?

For (a) you want information about your browser, not about python.
For (b) you want information about your web server, not about python.

Find the relevant forums and ask in them.

--
Denis McMahon, denismfmcmahon@gmail.com




== 8 of 10 ==
Date: Wed, Oct 9 2013 11:28 am
From: Mark Lawrence



On 09/10/2013 19:06, Denis McMahon wrote:
>
> Find the relevant forums and ask in them.
>

Why am I thinking of this
http://en.wikipedia.org/wiki/There%27s_a_Hole_in_My_Bucket ?

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence





== 9 of 10 ==
Date: Wed, Oct 9 2013 11:36 am
From: Piet van Oostrum


Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> writes:

>
> # initialize cookie and retrieve cookie from clients browser
> cookie = cookies.SimpleCookie( os.environ['HTTP_COOKIE'] )
>
> if cookie.get('ID') is not None:
> cookieID = cookie['ID'].value
> else:
> cookieID = random.randrange(0, 9999)
> cookie['ID'] = cookieID
> cookie['ID']['domain'] = ".superhost.gr"
> cookie['ID']['path'] = '/'
> cookie["ID"]["expires"] = 60*60*24*365 # this cookie will expire in a year
>
As Ian already has told you (but apparently you didn't pay attention to), your expires is wrong. So if your cookies disappear you should get this right first.

from datetime import datetime, timedelta
expiretime = datetime.utcnow() + timedelta(days=365)

cookie["ID"]["expires"] = expiretime.strftime("%a, %d %b %Y %H:%M:%S GMT")
--
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]




== 10 of 10 ==
Date: Wed, Oct 9 2013 12:26 pm
From: Tim Chase


On 2013-10-09 19:28, Mark Lawrence wrote:
> On 09/10/2013 19:06, Denis McMahon wrote:
>> Find the relevant forums and ask in them.
>
> Why am I thinking of this
> http://en.wikipedia.org/wiki/There%27s_a_Hole_in_My_Bucket ?

There's a bug in my program, dear newsgroup, dear newsgroup,
There's a bug in my program, dear newsgroup a bug.

Then fix it, dear Nikos, dear Nikos, dear Nikos,
Then fix it, dear Nikos, dear Nikos, fix it.

With what shall I fix it, dear newsgroup, dear newsgroup,
With what shall I fix it, dear newsgroup, with what?

The traceback, dear Nikos, dear Nikos, dear Nikos,
The traceback, dear Nikos, dear Nikos, traceback.

The problem's not Python, dear newsgroup, dear newsgroup,
The problem's not Python, dear newsgroup, what now?

Try Google, dear Nikos, dear Nikos, dear Nikos,
Try Google, dear Nikos, dear Nikos, Google.

But what shall I query, dear newsgroup, dear newsgroup,
But what shall I query, dear newsgroup, but what?

Some keywords, dear Nikos, dear Nikos, dear Nikos,
Some keywords, dear Nikos, dear Nikos, keywords.

I know it's off-topic, dear newsgroup, dear newsgroup,
I know it's off-topic, dear newsgroup, but help...


I can see the similarity ;-)

-tkc











==============================================================================
TOPIC: datetime.timedelta.replace?
http://groups.google.com/group/comp.lang.python/t/b414b8511c71a1f8?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Oct 9 2013 8:15 am
From: Skip Montanaro


Datetime objects have a replace method, but timedelta objects don't.
If I take the diff of two datetimes and want to zero out the
microseconds field, is there some way to do it more cleanly than this?

delta = dt1 - dt2
zero_delta = datetime.timedelta(days=delta.days, seconds=delta.seconds)

I guess that's not bad, but replace() seems cleaner (or at least more
congruent with datetime objects).

Skip





==============================================================================
TOPIC: Receive packet using socket
http://groups.google.com/group/comp.lang.python/t/f4211c0bdf331fdf?hl=en
==============================================================================

== 1 of 3 ==
Date: Wed, Oct 9 2013 8:37 am
From: tspiegelman@amplify.com


Hey all,

I am trying to use socket to send / receive a packet (want to recreate some functionality of hping3 and port it to windows and mac as a tcp ping). I am having some problems with the recv functionality of socket. Below is the script I am using. I get an ack from the server (used wireshark to ensure it was working) when I run this, but the script doesn't see the ack for some reason and the script exits with this error or a timeout:

Traceback (most recent call last):
File "./tcpgcmtesttristedit.py", line 21, in <module>
s.recv(1024)
socket.error: [Errno 104] Connection reset by peer

Here is the script:

import socket
import time

numberofpackets = int(raw_input("How many packets should be sent?\n> "))
hostname = 'mtalk.google.com'
port = 5228

for i in range(numberofpackets):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(4)
s.connect((hostname, port))
s.send('data')
start_time = time.time()
s.recv(24)
print time.time() - start_time
s.close()


Any help would be much appreciated. Thanks, Tom.




== 2 of 3 ==
Date: Wed, Oct 9 2013 9:07 am
From: tspiegelman@amplify.com


BTW what I am trying to accomplish is easily done in hping3 using this command:
hping3 mtalk.google.com -S -p 5228

I just want those same kind of results using python so I can make an exe out of it.

On Wednesday, October 9, 2013 11:37:39 AM UTC-4, tspie...@amplify.com wrote:
> Hey all,
>
>
>
> I am trying to use socket to send / receive a packet (want to recreate some functionality of hping3 and port it to windows and mac as a tcp ping). I am having some problems with the recv functionality of socket. Below is the script I am using. I get an ack from the server (used wireshark to ensure it was working) when I run this, but the script doesn't see the ack for some reason and the script exits with this error or a timeout:
>
>
>
> Traceback (most recent call last):
>
> File "./tcpgcmtesttristedit.py", line 21, in <module>
>
> s.recv(1024)
>
> socket.error: [Errno 104] Connection reset by peer
>
>
>
> Here is the script:
>
>
>
> import socket
>
> import time
>
>
>
> numberofpackets = int(raw_input("How many packets should be sent?\n> "))
>
> hostname = 'mtalk.google.com'
>
> port = 5228
>
>
>
> for i in range(numberofpackets):
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>
> s.settimeout(4)
>
> s.connect((hostname, port))
>
> s.send('data')
>
> start_time = time.time()
>
> s.recv(24)
>
> print time.time() - start_time
>
> s.close()
>
>
>
>
>
> Any help would be much appreciated. Thanks, Tom.





== 3 of 3 ==
Date: Wed, Oct 9 2013 10:03 am
From: Nobody


On Wed, 09 Oct 2013 08:37:39 -0700, tspiegelman wrote:

> I am trying to use socket to send / receive a packet (want to recreate
> some functionality of hping3 and port it to windows and mac as a tcp
> ping). I am having some problems with the recv functionality of socket.
> Below is the script I am using. I get an ack from the server (used
> wireshark to ensure it was working) when I run this, but the script
> doesn't see the ack for some reason and the script exits with this error
> or a timeout:

> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

If you're trying to send/receive individual packets, you need to use
SOCK_RAW (which normally requires root/administrator privilege).

> s.recv(24)

Alternatively, if you use a normal TCP stream socket, the receiver must
consume all data which is sent to it, and only close the socket after the
writer has closed its end. Depending upon the application-layer protocol,
this may involve some form of QUIT command, or a half-close using
the .shutdown() method.






==============================================================================
TOPIC: super in Python 3 and variadic arguments
http://groups.google.com/group/comp.lang.python/t/8d88f3f1fbaff513?hl=en
==============================================================================

== 1 of 2 ==
Date: Wed, Oct 9 2013 8:44 am
From: Marco Buttu


Given this class:

>>> class A:
... def afoo(*args):
... print(args)

in Python 3 we can write the following class:

>>> class B(A):
... def bfoo(*args):
... super(B, args[0]).afoo(*args[1:])
...
>>> B().bfoo(1, 2, 3)
(<__main__.B object at 0x7f5b3bde48d0>, 1, 2, 3)


without giving arguments to super, in this way:

>>> class B(A):
... def bfoo(self, *args):
... super().afoo(*args)
...
>>> B().bfoo(1, 2, 3)
(<__main__.B object at 0x7f5b3bdea0d0>, 1, 2, 3)

But it does not work in this case:

>>> class B(A):
... def bfoo(*args):
... super().afoo(*args[1:])
...
>>> B().bfoo(1, 2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in bfoo
RuntimeError: super(): no arguments

How come?
--
Marco Buttu




== 2 of 2 ==
Date: Wed, Oct 9 2013 9:47 am
From: Ned Batchelder


On 10/9/13 11:44 AM, Marco Buttu wrote:
> Given this class:
>
> >>> class A:
> ... def afoo(*args):
> ... print(args)
>
> in Python 3 we can write the following class:
>
> >>> class B(A):
> ... def bfoo(*args):
> ... super(B, args[0]).afoo(*args[1:])
> ...
> >>> B().bfoo(1, 2, 3)
> (<__main__.B object at 0x7f5b3bde48d0>, 1, 2, 3)
>
>
> without giving arguments to super, in this way:
>
> >>> class B(A):
> ... def bfoo(self, *args):
> ... super().afoo(*args)
> ...
> >>> B().bfoo(1, 2, 3)
> (<__main__.B object at 0x7f5b3bdea0d0>, 1, 2, 3)
>
> But it does not work in this case:
>
> >>> class B(A):
> ... def bfoo(*args):
> ... super().afoo(*args[1:])
> ...
> >>> B().bfoo(1, 2, 3)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 3, in bfoo
> RuntimeError: super(): no arguments
>
> How come?

The no-args super() call inspects the calling environment to determine
the class and self. "self" is the first local name stored in
frame.f_code.co_localsplus, but *args doesn't put "args" into that entry
of the code object. Basically, super() is looking for the first regular
argument in the function.

--Ned.







==============================================================================
TOPIC: Code golf challenge: XKCD 936 passwords
http://groups.google.com/group/comp.lang.python/t/62194e48e062f4f9?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Oct 9 2013 9:07 am
From: Nick Cash


>> # Python-2, sorry
>> import os
>> print list(set(open('/usr/share/dict/words')))[os.getpid():][:4]

> So that steps by your pid?

Not really. It seems to rely on list(set(...)) kinda randomizing order... which is definitely not safe without hash randomization.

But this brings up an interesting concept... if we can assume Python 2.7 with the -R flag, or Python 3.3+, I think we do get true pseudo-randomization out of list(set(...))... which means we can trim some!

print(list(set(open('/usr/share/dict/words')))[:4])

No module imports needed! Although it's not as pretty as the original post, but neither was Roy's.

-Nick Cash





==============================================================================
TOPIC: Applying 4x4 transformation to 3-element vector with numpy
http://groups.google.com/group/comp.lang.python/t/7fc89830bf619c45?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Oct 9 2013 9:44 am
From: Nobody


On Tue, 08 Oct 2013 23:10:16 -0700, John Nagle wrote:

> I only need affine transformations. This is just moving
> the coordinate system of a point, not perspective rendering. I have to do
> this for a lot of points, and I'm hoping numpy has some way to do this
> without generating extra garbage on the way in and the way out.

In which case, Christian's answer is correct.

For an affine transformation, the bottom row of the 4x4 matrix will be
[0 0 0 1], so you have:

[x'] [a b c d] [x] [ax+by+cz+d]
[y'] = [e f g h] [y] = [ex+fy+gz+h]
[z'] [i j k l] [z] [ix+jy+kz+l]
[1 ] [0 0 0 1] [1] [1 ]

=>

[x'] [ax+by+cz+d] [a b c] [x] [d]
[y'] = [ex+fy+gz+h] = [e f g] [y] + [h]
[z'] [ix+jy+kz+l] [i j k] [z] [l]

IOW:
xyz_ = m[:3,:3] * xyz + m[:3,3:]

(where xyz is a column vector or 3xN array)







==============================================================================
TOPIC: Re for Apache log file format
http://groups.google.com/group/comp.lang.python/t/7daaed832e3364d3?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Oct 9 2013 10:33 am
From: Piet van Oostrum


Sam Giraffe <sam@giraffetech.biz> writes:

> Hi,
>
> I am trying to split up the re pattern for Apache log file format and seem to be having some
> trouble in getting Python to understand multi-line pattern:
>
> #!/usr/bin/python
>
> import re
>
> #this is a single line
> string = '192.168.122.3 - - [29/Sep/2013:03:52:33 -0700] "GET / HTTP/1.0" 302 276 "-" "check_http/
> v1.4.16 (nagios-plugins 1.4.16)"'
>
> #trying to break up the pattern match for easy to read code
> pattern = re.compile(r'(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+'
>                      r'(?P<ident>\-)\s+'
>                      r'(?P<username>\-)\s+'
>                      r'(?P<TZ>\[(.*?)\])\s+'
>                      r'(?P<url>\"(.*?)\")\s+'
>                      r'(?P<httpcode>\d{3})\s+'
>                      r'(?P<size>\d+)\s+'
>                      r'(?P<referrer>\"\")\s+'
>                      r'(?P<agent>\((.*?)\))')
>
> match = re.search(pattern, string)
>
> if match:
>     print match.group('ip')
> else:
>     print 'not found'
>
> The python interpreter is skipping to the 'math = re.search' and then the 'if' statement right
> after it looks at the <ip>, instead of moving onto <ident> and so on.

Although you have written the regexp as a sequence of lines, in reality it is a single string, and therefore pdb will do only a single step, and not go into its "parts", which really are not parts.
>
> mybox:~ user$ python -m pdb /Users/user/Documents/Python/apache.py
>> /Users/user/Documents/Python/apache.py(3)<module>()
> -> import re
> (Pdb) n
>> /Users/user/Documents/Python/apache.py(5)<module>()
> -> string = '192.168.122.3 - - [29/Sep/2013:03:52:33 -0700] "GET / HTTP/1.0" 302 276 "-"
> "check_http/v1.4.16 (nagios-plugins 1.4.16)"'
> (Pdb) n
>> /Users/user/Documents/Python/apache.py(7)<module>()
> -> pattern = re.compile(r'(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+'
> (Pdb) n
>> /Users/user/Documents/Python/apache.py(17)<module>()
> -> match = re.search(pattern, string)
> (Pdb)

Also as Andreas has noted the r'(?P<referrer>\"\")\s+' part is wrong. It should probably be
r'(?P<referrer>\".*?\")\s+'

And the r'(?P<agent>\((.*?)\))') will also not match as there is text outside the (). Should probably also be
r'(?P<agent>\".*?\")') or something like it.
--
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]





==============================================================================
TOPIC: PROPOSAL: PyCons in Africa
http://groups.google.com/group/comp.lang.python/t/545d371a081fbf5c?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Oct 9 2013 12:33 pm
From: real-not-anti-spam-address@apple-juice.co.uk (D.M. Procida)


[reposted; the previous one didn't seem to make it out!]

I have a written a first draft outlining a proposal for a PyCon in a
sub-Saharan African nation where there has never been one.

<http://pycons-in-africa.readthedocs.org>

There's an email list for people interested in becoming involved in the
idea: <http://groups.google.com/group/pycons-in-africa>.

I'm seeking support for the proposal, from all parties who'd need to be
involved, from Python user groups in African cities where a PyCon might
be viable, to people who'd want to take part and the wider Python
community.

If anyone would like to add themselves to the list of potential
collaborators on this, please do so using GitHub:
<https://github.com/evildmp/pycons-in-africa/blob/master/docs/collaborat
ors.rst>. If you can't do that, just ask me to add the information.

There's a wiki on GitHub too:
<https://github.com/evildmp/pycons-in-africa/wiki>.

I'm also seeking critical opinions on this, so if you think it's a poor
idea, or poorly conceived, or that there would be better ways to achieve
the aims in the proposal, or something key that's missing from it, I
would like to hear why you think that, so I can address any problems.

Thanks,

Daniele





==============================================================================
TOPIC: class-private names and the Zen of Python
http://groups.google.com/group/comp.lang.python/t/7c936ee0b3017744?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Oct 9 2013 12:34 pm
From: Charles Hixson


On 10/08/2013 06:24 AM, Steven D'Aprano wrote:
> On Tue, 08 Oct 2013 12:13:48 +0200, Marco Buttu wrote:
>
>> In the following case:
>>
>> >>> class Foo:
>> ... _Foo__a = 100
>> ... __a = 33
>> ...
>> >>> Foo._Foo__a
>> 33
>>
>> I think this behavior, for a user who does not know the convention,
>> could be a surprise.
> Yes, you are correct. It surprised me, and I've been using Python for
> more than 15 years, and I know the convention of double-underscore name-
> mangling.
>
>
>> Should be raising an exception (in order to inform
>> the user the transformation of the name __a have been replaced an
>> existing name) a possible --explicit-- alternative?
> No, I don't think so. That would slow down class creation, for no real
> benefit. Except for the name-mangling part, this is no different from:
>
> class Spam:
> x = 23
> x = 42
>
> If anything, something like PyLint or PyChecker could warn about it. But
> the language itself is fine like it is.
>
>
>> Another question is: where is the place in which this transformation
>> occurs? Is it at the parser level, before the dictionary attribute is
>> gave as argument to the metaclass?
> Good question!
>
> I don't have a full answer, but I have a part answer: it occurs before
> the metaclass sees the namespace:
>
> py> class Meta(type):
> ... def __new__(meta, name, bases, namespace):
> ... print(namespace)
> ... return super().__new__(meta, name, bases, namespace)
> ...
> py>
> py> class Test(metaclass=Meta):
> ... __test = 'foo'
> ...
> {'__module__': '__main__', '_Test__test': 'foo', '__qualname__': 'Test'}
>
>
> so I think it is done by the parser.
Unless one wanted to add an error checking mode to the interpreter, not
a totally bad idea, I agree that this is a job for external tools. But
perhaps that same external tool should be referenced in the
documentation. As it is I'm not clear that PyLint and/or PyChecker are
kept current with the compiler/interpreter version, and I rather suspect
that they aren't. (This won't normally show up, as changes that they
would catch happen quite rarely, but...)

OTOH, neither one really fits in as, say, an included module...they're
more like idle, which now that I look does have a "check module" run
option. Perhaps that invokes one of them. I note that Idle, itself, is
barely mentioned in the documentation. Perhaps there needs to be a
"tools" section.


--
Charles Hixson





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

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