Re: Vigil, the eternal morally vigilant programming language

2013-01-08 Thread Terry Reedy

On 1/7/2013 11:01 AM, Alain Ketterlin wrote:


I just came across Vigil, an extension to python for serious software
engineers,


I hope that last part comes from a sense of humor.


at https://github.com/munificent/vigil and thought everybody
in this group would be interested (sorry if it has been announced
before).

 From README:

| Vigil is a very safe programming language, and an entry in the January
| 2013 PLT Games competition.
|
| Many programming languages claim to take testing, contracts and safety
| seriously, but only Vigil is truly vigilant about not allowing code
| that fails to pass programmatic specifications.


While the language is a joke (Procrustes would be a somewhat better 
name), the example illustrates the near uselessness of contract 
checking, at least for functions. The example fib(n) function 'swears' 
that the answer is a count (not negative). Ha, ha, very funny. A simple 
'return 0' would satisfy that useless contract*. A correct fib(n) 
function must actually calculate fib(n), and in practice, that is not 
possible to check. Even checking that sorted(iterable) is correct 
(outside a test situation) is harder than it might first seem#.


* In any of the many languages that use biased residue classes as a 
substitute for real integers, positive + positive = positive is not so 
trivial. So the function must return a negative to signal bad input or 
raise an exception either directly or by an input condition. The 
advantage of the directly exception is that the exception message can be 
tailored to the audience and situation. [Vigil would simply excise the 
calling function.]


# Given sorted(iterable) where iterable gets some number of comparable 
items from stdin, how is the contract checker to determine that the 
output is indeed a permutation of the unmutated input items, before 
checking that they are are actually sorted.


--
Terry Jan Reedy

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


Re: Problem with Unicode char in Python 3.3.0

2013-01-08 Thread Terry Reedy

On 1/7/2013 8:12 AM, Terry Reedy wrote:

On 1/7/2013 7:57 AM, Franck Ditter wrote:


<<< print('\U0001d11e')
Traceback (most recent call last):
   File "", line 1, in 
 print('\U0001d11e')
UnicodeEncodeError: 'UCS-2' codec can't encode character '\U0001d11e'
in position 0: Non-BMP character not supported in Tk


The message comes from printing to a tk text widget (the IDLE shell),
not from creating the 1 char string. c = '\U0001d11e' works fine. When
you have problems with creating and printing unicode, *separate*
creating from printing to see where the problem is. (I do not know if
the brand new tcl/tk 8.6 is any better.)

The windows console also chokes, but with a different message.

 >>> c='\U0001d11e'
 >>> print(c)
Traceback (most recent call last):
   File "", line 1, in 
   File "C:\Programs\Python33\lib\encodings\cp437.py", line 19, in encode
 return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001d11e'
in posit
ion 0: character maps to 

Yes, this is very annoying, especially in Win 7.


The above is in 3.3, in which '\U0001d11e' is actually translated to a 
length 1 string. In 3.2-, that literal is translated (on 3.2- narrow 
builds, as on Windows) to a length 2 string surrogate pair (in the BMP). 
On printing, the pair of surrogates got translated to a square box used 
for all characters for which the font does not have a glyph.  𝄞When cut 
and pasted, it shows in this mail composer as a weird music sign with 
peculiar behavior.

3 -s, 3 spaces, paste, 3 spaces, 3 -s, but it may disappear.
---   𝄞   ---
So 3.3 is the first Windows version to get the UnicodeEncodeError on 
printing.


--
Terry Jan Reedy


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


Re: test failed: test_urlwithfrag

2013-01-08 Thread Terry Reedy

On 1/7/2013 1:26 PM, Elli Lola wrote:


$ ./python -m test -v test_urlwithfrag

== CPython 3.3.0 (default, Jan 4 2013, 23:08:00) [GCC 4.6.3]
==   Linux-3.2.0-35-generic-pae-i686-with-debian-wheezy-sid little-endian
==   /home/me/Programme/Python/Python-3.3.0/build/test_python_30744
Testing with flags: sys.flags(debug=0, inspect=0, interactive=0,
optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0,
ignore_environment=0, verbose=0, bytes_warning=0, quiet=0,
hash_randomization=1)
[1/1] test_urlwithfrag
test test_urlwithfrag crashed -- Traceback (most recent call last):
   File "/home/me/Programme/Python/Python-3.3.0/Lib/test/regrtest.py",
line 1213, in runtest_inner
 the_package = __import__(abstest, globals(), locals(), [])
ImportError: No module named 'test.test_urlwithfrag'

1 test failed:
 test_urlwithfrag


Read the message I sent you before.

(To everyone else, I already explained that the error message means 
exactly what it says and listed the test_url files that do exist. No 
need to repeat a third time.)


--
Terry Jan Reedy

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


Re: [Offtopic] Line fitting [was Re: Numpy outlier removal]

2013-01-08 Thread Terry Reedy

On 1/7/2013 8:23 PM, Steven D'Aprano wrote:

On Mon, 07 Jan 2013 22:32:54 +, Oscar Benjamin wrote:


An example: Earlier today I was looking at some experimental data. A
simple model of the process underlying the experiment suggests that two
variables x and y will vary in direct proportion to one another and the
data broadly reflects this. However, at this stage there is some
non-normal variability in the data, caused by experimental difficulties.
A subset of the data appears to closely follow a well defined linear
pattern but there are outliers and the pattern breaks down in an
asymmetric way at larger x and y values. At some later time either the
sources of experimental variation will be reduced, or they will be
better understood but for now it is still useful to estimate the
constant of proportionality in order to check whether it seems
consistent with the observed values of z. With this particular dataset I
would have wasted a lot of time if I had tried to find a computational
method to match the line that to me was very visible so I chose the line
visually.



If you mean:

"I looked at the data, identified that the range a < x < b looks linear
and the range x > b does not, then used least squares (or some other
recognised, objective technique for fitting a line) to the data in that
linear range"

then I'm completely cool with that.


If both x and y are measured values, then regressing x on y and y on x 
with give different answers and both will be wrong in that *neither* 
will be the best answer for the relationship between them. Oscar did not 
specify whether either was an experimentally set input variable.



But that is not fitting a line by eye, which is what I am talking about.


With the line constrained to go through 0,0, a line eyeballed with a 
clear ruler could easily be better than either regression line, as a 
human will tend to minimize the deviations *perpendicular to the  line*, 
which is the proper thing to do (assuming both variables are measured in 
the same units).


--
Terry Jan Reedy

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


Re: Calculate Big Number

2013-01-08 Thread Gisle Vanem

"Steven D'Aprano"  wrote:


py> from timeit import Timer
py> t1 = Timer("(a**b)*(c**d)", setup="a,b,c,d = 10, 25, 2, 50")
py> min(t1.repeat(repeat=5, number=10))
0.5256571769714355

So that's about 5 microseconds on my (slow) computer.


That's pretty fast. So is there still a need for a GMP python-binding like
gmpy? http://code.google.com/p/gmpy/wiki/IntroductionToGmpy

GMP can include optimized assembler for the CPU you're using. But
I guess it needs more memory. Hence disk-swapping could be an issue
on performance.

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


How to call ltm function using rpy package in python

2013-01-08 Thread Mohit Khanna
I am trying the following code--

from rpy import *
r.library("ltm")

dat= #some data frame or matrix
r.ltm(r('dat~z1'))

error coming is---
RPy_RException: Error in eval(expr, envir, enclos) : object 'dat' not found

Please tell me the right way to call ltm function using rpy library
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get the selected text of the webpage in chrome through python ?

2013-01-08 Thread iMath
在 2013年1月8日星期二UTC+8下午12时20分28秒,iMath写道:
> How to get the selected text of the webpage in chrome through python ?

I need the code ,cuz  I am only familiar with Python ,so it would be better to 
give me the code written in Python .
You can also give me the code in other programming language   ,thanks in 
advance :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Newbe accessing MySQL, need something like perl's prepare_cached

2013-01-08 Thread Morten Guldager
'Aloha Friends!

Still quite new to python I'm trying to access a MySQL database. Being a
former perl programmer I recognize much of the semantics going on.

Create a database handle, compile a piece of SQL and put it into a cursor,
run the query and use the result. exactly the same flow as I am used to.
And it works just fine.

But as programs grow larger, it is sometimes becomes a burden to compile
those SQL's over and over again when they should be perfectly reusable.
 And implementing a "query cache" by myself seems not to be the right thing
in this batteries included environment.

Donno exactly what I'm looking for, but the perl equivalent is
called prepare_cached.

Oh, by the way, I'm using "import MySQLdb", hope that's not the most
outdated, inefficient and stupid choice for a MySQL library...

-- 
/Morten %-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what’s the difference between socket.send() and socket.sendall() ?

2013-01-08 Thread Philipp Hagemeister
socket.socket.send is a low-level method and basically just the
C/syscall method send(3) / send(2). It can send less bytes than you
requested.

socket.socket.sendall is a high-level Python-only method that sends the
entire buffer you pass or throws an exception. It does that by calling
send until everything has been sent or an error occurs.

If you're using TCP with blocking sockets and don't want to be bothered
by internals (this is the case for most simple network applications),
use sendall.

Otherwise, use send and make sure to read and process its return value.

- Philipp

On 01/07/2013 11:35 AM, iMath wrote:
> what’s the difference between socket.send() and socket.sendall() ?
> 
> It is so hard for me to tell the difference between them from the python doc
> 
> so what is the difference between them ?
> 
> and each one is suitable for which case ?
> 



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get the selected text of the webpage in chrome through python ?

2013-01-08 Thread Dave Angel
On 01/08/2013 07:38 AM, Dennis Lee Bieber wrote:
> On Mon, 7 Jan 2013 20:20:28 -0800 (PST), iMath 
> declaimed the following in gmane.comp.python.general:
>
>> How to get the selected text of the webpage in chrome through python ?
>   Chrome is a browser, is it not... If you want to get the text in
> Python, you have to write the equivalent of a browser -- something that
> parses the returned HTML message, extracting the parts you want.

To get a selection, you can either wait till it's copied to the
clipboard, and get it from there, or you can write a addon for the
application.  Since Chrome accepts addons, I suggest you research that
angle.  Probably mozilla.org is the place to start, and function

nsISelectionController::GetSelection() might be some place to examine.

You might find some hints in
http://stackoverflow.com/questions/2671474/range-selection-and-mozilla


if you are going to work with the clipboard, realize that it works quite
differently from one OS to another, and even from one desktop manager to
another.

-- 

DaveA

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


Re: How to get the selected text of the webpage in chrome through python ?

2013-01-08 Thread Bruno Dupuis
On Mon, Jan 07, 2013 at 08:20:28PM -0800, iMath wrote:
> How to get the selected text of the webpage in chrome through python ?

What you need is a way to get selected text from wherever it comes. The
way to do this depends on your graphical environment. If you use X, i'd make a
a quick and dirty call to xclip -o, although there must be a pure python
implementation which in turn depends on the python framework you play
with (gtk/qt/wx/tk/...).

Short answer is : it depends on your system, and it may be easier and more
portable if you use a graphical toolkit.

cheers.

-- 
Bruno Dupuis

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


Re: [Offtopic] Line fitting [was Re: Numpy outlier removal]

2013-01-08 Thread Oscar Benjamin
On 8 January 2013 01:23, Steven D'Aprano
 wrote:
> On Mon, 07 Jan 2013 22:32:54 +, Oscar Benjamin wrote:
>
> [...]
>> I also think it would
>> be highly foolish to go so far with refusing to eyeball data that you
>> would accept the output of some regression algorithm even when it
>> clearly looks wrong.
>
> I never said anything of the sort.
>
> I said, don't fit lines to data by eye. I didn't say not to sanity check
> your straight line fit is reasonable by eyeballing it.

I should have been a little clearer. That was the situation when I
decided to just use a (digital) ruler - although really it was more of
a visual bisection (1, 2, 1.5, 1.25...). The regression result was
clearly wrong (and also invalid for the reasons Terry has described).
Some of the problems were easily fixable and others were not. I could
have spent an hour getting the code to make the line go where I wanted
it to, or I could just fit the line visually in about 2 minutes.


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


Re: How to get the selected text of the webpage in chrome through python ?

2013-01-08 Thread Miki Tebeka
On Monday, January 7, 2013 8:20:28 PM UTC-8, iMath wrote:
> How to get the selected text of the webpage in chrome through python ?

You can probably use selenium to do that.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-08 Thread Thomas Rachel

Am 06.01.2013 15:30 schrieb Kurt Hansen:

Den 06/01/13 15.20, Chris Angelico wrote:

On Mon, Jan 7, 2013 at 1:03 AM, Kurt Hansen  wrote:

I'm sorry to bother you, Chris, but applying the snippet with your
code in
Gedit still just deletes the marked, tab-separated text in the editor.



Ah, whoops. That would be because I had a bug in the code (that's why
I commented that it was untested). Sorry about that! Here's a fixed
version:


[cut]>

Note that it's a single line:

output += '' + item +
' '

If your newsreader (or my poster) wraps it, you'll need to unwrap that
line, otherwise you'll get an IndentError.


Ahhh, I did'nt realize that. Now it works :-)


That version should work.


It certainly does. I'll keep it and use it until at better solution is
found.


That would be simple:

Replace

output += '' + item +
' '

with

if len(columns) >= 3:
output += ''
else:
output += ''
output += item + ' '

(untested as well; keep the indentation in mind!)


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


Re: How to modify this script?

2013-01-08 Thread Thomas Rachel

Am 07.01.2013 18:56 schrieb Gertjan Klein:


(Watch out for line wraps! I don't know how to stop Thunderbird from
inserting them.)


Do "insert as quotation" (in German Thunderbird: "Als Zitat einfügen"), 
or Strg-Shift-O. Then it gets inserted with a ">" before and in blue.


Just remove the > and the space after it; the "non-breaking property" is 
kept.


Example:

columns = line.split("\t");
if len(columns) == 1:
output += ('' % max_columns) + line + 
'\n'

continue

without and

columns = line.split("\t");
if len(columns) == 1:
output += ('' % max_columns) + line + 
'\n'

continue

with this feature.

HTH,

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


Re: ANN: PyDTLS

2013-01-08 Thread Guido van Rossum
This sounds exciting. Are you considering a Python 3 port? It might make a
nice demo of PEP 3156.

On Monday, January 7, 2013, rbit wrote:

> I would like to announce Datagram Transport Layer Security for
> Python. From the top of the project README:
>
> PyDTLS brings Datagram Transport Layer Security (DTLS - RFC 6347:
> http://tools.ietf.org/html/rfc6347) to the Python environment. In a
> nutshell, DTLS brings security (encryption, server authentication,
> user authentication, and message authentication) to UDP datagram
> payloads in a manner equivalent to what SSL/TLS does for TCP stream
> content.
>
> DTLS is now very easy to use in Python. If you're familiar with the
> ssl module in Python's standard library, you already know how. All it
> takes is passing a datagram/UDP socket to the *wrap_socket* function
> instead of a stream/TCP socket. Here's how one sets up the client side
> of a connection:
>
> import ssl
> from socket import socket, AF_INET, SOCK_DGRAM
> from dtls import do_patch
> do_patch()
> sock = ssl.wrap_socket(socket(AF_INET, SOCK_DGRAM))
> sock.connect(('foo.bar.com', 1234))
> sock.send('Hi there')
>
> The project is hosted at https://github.com/rbit/pydtls, and licensed
> under
> the Apache license 2.0. PyPI has packages. I can be reached
> at code AT liquibits DOT com for questions, feedback, etc.
>
> http://pypi.python.org/pypi/Dtls/0.1.0";>Dtls 0.1.0 -
>   Datagram Transport Layer Security for Python.  (07-Jan-13)
> --
> http://mail.python.org/mailman/listinfo/python-announce-list
>
> Support the Python Software Foundation:
> http://www.python.org/psf/donations/
>


-- 
--Guido van Rossum (python.org/~guido)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to download internet files by python ?

2013-01-08 Thread MRAB

On 2013-01-08 05:00, Rodrick Brown wrote:

On Mon, Jan 7, 2013 at 11:19 PM, iMath mailto:[email protected]>> wrote:

for example ,if I want to download this file ,how to implement the
download functionality by python ?

http://down.51voa.com/201208/se-ed-foreign-students-friends-16aug12.mp3

as for  download speed ,of course ,the fast ,the better ,so how to
implement it ?

It would be better to show me an example :) thanks !!!
--
http://mail.python.org/mailman/listinfo/python-list


#!/usr/bin/python
import urllib2
if __name__ == '__main__':

fileurl='http://down.51voa.com/201208/se-ed-foreign-students-friends-16aug12.mp3'

 mp3file = urllib2.urlopen(fileurl)
 with open('outfile.mp3','wb') as output:
 output.write(mp3file.read())


Why not just use urlretrieve?
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-08 Thread chaouche yacine
Well tell me how do you use this script in gedit, are you using it as a plugin 
? are you putting this code somewhere ? I'll try to do the same on my side and 
try to understand how it works.







 From: Kurt Hansen 
To: [email protected] 
Sent: Monday, January 7, 2013 5:42 PM
Subject: Re: How to modify this script?
 
Den 06/01/13 16.12, chaouche yacine skrev:
> I'm not confident this would run on gedit. It works on a python
> interpreter if you have a file named data.txt in the same directory
> containing your sample data.
>
> It surely has to do with how gedit works then, because the "$" sign
> isn't used in python, this business should be a gedit convention. And
> sorry, I can't help on that, I'm not a user of gedit myself. Fortunately
> others have answered and I beleive one of the solutions worked for you.

It does not seem to be the case :-(

Thank you for trying to help.

> 
> *From:* Kurt Hansen 
> *To:* [email protected]
> *Sent:* Sunday, January 6, 2013 3:21 PM
> *Subject:* Re: How to modify this script?
>
> Den 06/01/13 15.01, chaouche yacine wrote:
>  > Well, I'm not answering your question since I am rewriting the script,
>  > because I prefer it this way :)
>  >
>  > def addline(line):
>  >      return "%s\n" % line
> [cut]
>
> I surpose I shall put your code between $< and >?
>
>  > printed
>  >
>  >  >>> 
>  > Price table
>  > 1  Green apple  $1
>  > 5  Green apples  $4
>  > 10  Green apples  $7
>  > 
>  >  >>>
>
> Aha, so you tested it yourself?
>
> When running this in Gedit on four lines of tab-separated text the
> output is:
>
> %s\n" % line
>
> def addcolumn(item,nb_columns):
>      if nb_columns != 3:
>          return "%s" % (3 - nb_columns + 1, item)
>      return "%s" % item
>
> output = "\n"
> for line in file("data.txt"):
>      items = line.strip().split("\t")
>      columns = ""
>      for item in items :
>          columns += addcolumn(item,len(items))
>      output  += addline(columns)
>
>
> output += ""
> print output
>  >
> -- Venlig hilsen
> Kurt Hansen
> -- http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
Venlig hilsen
Kurt Hansen
-- 
http://mail.python.org/mailman/listinfo/python-list-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Offtopic] Line fitting [was Re: Numpy outlier removal]

2013-01-08 Thread Robert Kern

On 08/01/2013 06:35, Chris Angelico wrote:

On Tue, Jan 8, 2013 at 1:06 PM, Steven D'Aprano
 wrote:

given that weather patterns have been known to follow cycles at least
that long.


That is not a given. "Weather patterns" don't last for thirty years.
Perhaps you are talking about climate patterns?


Yes, that's what I meant. In any case, debate about global warming is
quite tangential to the point about statistical validity; it looks
quite significant to show a line going from the bottom of the graph to
the top, but sounds a lot less noteworthy when you see it as a
half-degree increase on about (I think?) 30 degrees, and even less
when you measure temperatures in absolute scale (Kelvin) and it's half
a degree in three hundred.


Why on Earth do you think that the distance from nominal surface temperatures to 
freezing much less absolute 0 is the right scale to compare global warming 
changes against? You need to compare against the size of global mean temperature 
changes that would cause large amounts of human suffering, and that scale is on 
the order of a *few* degrees, not hundreds. A change of half a degree over a few 
decades with no signs of slowing down *should* be alarming.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: How to modify this script?

2013-01-08 Thread Gertjan Klein

Thomas Rachel wrote:


Am 07.01.2013 18:56 schrieb Gertjan Klein:


(Watch out for line wraps! I don't know how to stop Thunderbird from
inserting them.)


Do "insert as quotation" (in German Thunderbird: "Als Zitat einfügen"),
or Strg-Shift-O. Then it gets inserted with a ">" before and in blue.

Just remove the > and the space after it; the "non-breaking property" is
kept.


Ah, I didn't think of that, thanks. Laborious but manageable. But:


Example:


Both your examples are wrapped, in the same way. (I checked the message 
source, it's not some setting on my end.) It appears that, although 
Thunderbirds editor remembers the "don't wrap" setting, wrapping still 
occurs before sending. (The wrapping-before-sending code uses a slightly 
different algorithm than the editor too, so messages often look 
different after sending. Thunderbirds editor is really bad.)


A last attempt: two (equal) quotes, the second one with the leading "> " 
removed:



  output += ('' % max_columns) + line + '\n'


  output += ('' % max_columns) + line + 
'\n'


I'm curious how this will come out.

Regards,
Gertjan.


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


Re: Calculate Big Number

2013-01-08 Thread casevh
On Tuesday, January 8, 2013 2:06:09 AM UTC-8, Gisle Vanem wrote:
> "Steven D'Aprano"  wrote:
> 
> > py> from timeit import Timer
> > py> t1 = Timer("(a**b)*(c**d)", setup="a,b,c,d = 10, 25, 2, 50")
> > py> min(t1.repeat(repeat=5, number=10))
> > 0.5256571769714355
> > 
> > So that's about 5 microseconds on my (slow) computer.
> 
> That's pretty fast. So is there still a need for a GMP python-binding like
> gmpy? http://code.google.com/p/gmpy/wiki/IntroductionToGmpy
> 
> GMP can include optimized assembler for the CPU you're using. But
> I guess it needs more memory. Hence disk-swapping could be an issue
> on performance.
> 
gmpy will be faster than Python as the numbers get larger. The cutover varies 
depending on the platform, but usually occurs between 50 and 100 digits.

casevh
> 
> --gv

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


Re: [Offtopic] Line fitting [was Re: Numpy outlier removal]

2013-01-08 Thread Maarten
On Tuesday, January 8, 2013 10:07:08 AM UTC+1, Terry Reedy wrote:

> With the line constrained to go through 0,0, a line eyeballed with a 
> clear ruler could easily be better than either regression line, as a 
> human will tend to minimize the deviations *perpendicular to the  line*, 
> which is the proper thing to do (assuming both variables are measured in 
> the same units).

In that case use an appropriate algorithm to perform the fit. ODR comes to 
mind. http://docs.scipy.org/doc/scipy/reference/odr.html

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


ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

2013-01-08 Thread Dave Cinege
Thesaurus: A different way to call a dictionary.

Thesaurus is a new a dictionary subclass which allows calling keys as
if they are class attributes and will search through nested objects
recursively when __getitem__ is called.

You will notice that the code is disgusting simple. However I have found that
this has completely changed the way I program in python. I've re-written some
exiting programs using Thesaurus, and often realized 15-30% code reduction.
Additionally I find the new code much easier to read.

If you find yourself programing with nested dictionaries often, fighting to 
generate output or command lines for external programs, or wish you had 
a dictionary that could act (sort of) like a class, Thesaurus may be for you.
#!/usr/bin/env python
"""
thesaurus.py		2012-09-13

Copyright (c) 2012 Dave Cinege
Licence: python, Copyright notice may not be altered.

Thesaurus: A different way to call a dictionary.

Thesaurus is a new a dictionary subclass which allows calling keys as
if they are class attributes and will search through nested objects
recursivly when __getitem__ is called.

You will notice that the code is disgusting simple. However I have found that
this has completely changed the way I program in python. I've re-written some
exiting programs using Thesaurus, and often relized 15-30% code reduction.
Additionally I find the new code much easier to read.

If you find yourself programing with nested dictionaries often, fighting to 
generate output or command lines for external programs, or wish you had 
a dictionary that could act (sort of) like a class, Thesaurus may be for you.

By example:
---
#!/usr/bin/env python

import thesaurus
thes = thesaurus.Thesaurus

# I like to create a master global object called 'g'.
# No more 'global' statements for me.
g = thes()

g.prog = thes()
g['prog']['VERSION'] = '1.0'	# I can do this like a normal nested dictionary
g.prog.VERSION = '1.0'		# But isn't this so much cleaner?
g.prog.NAME = 'Thesaurus'

class TestClass:
	no = 'Class'
	way = 'this'

def main ():

	L = thes()		# Some local varibles

	L.tc = TestClass()	# We can also recurse to class attributes

	L.l = list()		# And recurse to indices too!
	L.l.append('Some')
	L.l.append('objects')
	
	g.L = L		# Easily make these locals global
	
	# Here's the real magic. Creating output without a fight.
	print '''
		When programing python using %(prog.NAME)s, it is very
		easy to access your %(L.l.1)s.
		
		%(L.l.0)s people might say %(prog.NAME)s has no %(L.tc.no)s.
	'''.replace('\t','')[1:-1] % g

	del g.L		# Clean locals up out of global space

	# If I was using a storage class, I must use hasattr() or an ugly eval.
	# But we can still use a str for the key name and 'in' like on any
	# regular dictionary.
	if 'VERSION' in g.prog:
		print 'But I challenge them to write code %(tc.way)s clean without it!' % L


if __name__ == '__main__':
	main()

---

"""
__VERSION__ = 20120913

class Thesaurus (dict):
	def __getattr__(self, name):
		return self.__getitem__(name)
	def __setattr__(self, name, value):
		return dict.__setitem__(self, name, value)
	def __delattr__(self, name):
		return dict.__delitem__(self, name)
	def __getitem__(self, key):
		if '.' not in key:
			return dict.__getitem__(self, key)
		l = key.split('.')
		if isinstance(l[0], (dict, Thesaurus)):
			a = self.data
		else:
			a = self
		for i in range(len(l)):		# Walk keys recursivly
			try:
a = a[l[i]]		# Attempt key
			except:
try:
	a = getattr(a, l[i])	# Attempt attr
except:
	try:
		a = a[int(l[i])]	# Attempt indice
	except:
		raise KeyError(key + ' [%s]' % i)
		return a
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: License status of pycollada?

2013-01-08 Thread Jeff Terrace
Hi Gene,

I'm the maintainer of pycollada. No such paywall exists, and a login is not
required. I'm not sure how you came across that.

As Chris said, it's a standard BSD license. I'd be happy to help with
packaging, so feel free to contact me.

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


Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

2013-01-08 Thread Neal Becker
Did you intend to give anyone permission to use the code?  I see only a 
copyright notice, but no permissions.

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


Re: Searching through two logfiles in parallel?

2013-01-08 Thread darnold
i don't think in iterators (yet), so this is a bit wordy.
same basic idea, though: for each message (set of parameters), build a
list of transactions consisting of matching send/receive times.

mildly tested:


from datetime import datetime, timedelta

sendData = '''\
05:00:06 Message sent - Value A: 5.6, Value B: 6.2, Value C: 9.9
05:00:08 Message sent - Value A: 3.3, Value B: 4.3, Value C: 2.3
05:00:10 Message sent - Value A: 3.0, Value B: 0.4, Value C: 5.4
#orphan
05:00:14 Message sent - Value A: 1.0, Value B: 0.4, Value C: 5.4
07:00:14 Message sent - Value A: 1.0, Value B: 0.4, Value C: 5.4
'''

receiveData = '''\
05:00:09 Message received - Value A: 5.6, Value B: 6.2, Value C:
9.9
05:00:12 Message received - Value A: 3.3, Value B: 4.3, Value C:
2.3
05:00:15 Message received - Value A: 1.0, Value B: 0.4, Value C:
5.4
07:00:18 Message received - Value A: 1.0, Value B: 0.4, Value C:
5.4
07:00:30 Message received - Value A: 1.0, Value B: 0.4, Value C:
5.4   #orphan
07:00:30 Message received - Value A: 17.0, Value B: 0.4, Value C:
5.4  #orphan
'''

def parse(line):
timestamp, rest = line.split(' Message ')
action, params = rest.split(' - ' )
params = params.split('#')[0]
return timestamp.strip(), params.strip()

def isMatch(sendTime,receiveTime,maxDelta):
if sendTime is None:
return False

sendDT = datetime.strptime(sendTime,'%H:%M:%S')
receiveDT = datetime.strptime(receiveTime,'%H:%M:%S')
return receiveDT - sendDT <= maxDelta

results = {}

for line in sendData.split('\n'):
if not line.strip():
continue

timestamp, params = parse(line)
if params not in results:
results[params] = [{'sendTime': timestamp, 'receiveTime':
None}]
else:
results[params].append({'sendTime': timestamp, 'receiveTime':
None})

for line in receiveData.split('\n'):
if not line.strip():
continue

timestamp, params = parse(line)
if params not in results:
results[params] = [{'sendTime': None, 'receiveTime':
timestamp}]
else:
for tranNum, transaction in enumerate(results[params]):
if
isMatch(transaction['sendTime'],timestamp,timedelta(seconds=5)):
results[params][tranNum]['receiveTime'] = timestamp
break
else:
results[params].append({'sendTime': None, 'receiveTime':
timestamp})

for params in sorted(results):
print params
for transaction in results[params]:
print '\t%s' % transaction


>>>  RESTART 
>>>
Value A: 1.0, Value B: 0.4, Value C: 5.4
{'sendTime': '05:00:14', 'receiveTime': '05:00:15'}
{'sendTime': '07:00:14', 'receiveTime': '07:00:18'}
{'sendTime': None, 'receiveTime': '07:00:30'}
Value A: 17.0, Value B: 0.4, Value C: 5.4
{'sendTime': None, 'receiveTime': '07:00:30'}
Value A: 3.0, Value B: 0.4, Value C: 5.4
{'sendTime': '05:00:10', 'receiveTime': None}
Value A: 3.3, Value B: 4.3, Value C: 2.3
{'sendTime': '05:00:08', 'receiveTime': '05:00:12'}
Value A: 5.6, Value B: 6.2, Value C: 9.9
{'sendTime': '05:00:06', 'receiveTime': '05:00:09'}
>>>

HTH,
Don
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: License status of pycollada?

2013-01-08 Thread Gene Heskett
On Tuesday 08 January 2013 14:09:55 Jeff Terrace did opine:
Message additions Copyright Tuesday 08 January 2013 by Gene Heskett

> Hi Gene,
> 
> I'm the maintainer of pycollada. No such paywall exists, and a login is
> not required. I'm not sure how you came across that.
> 
Google search.

> As Chris said, it's a standard BSD license. I'd be happy to help with
> packaging, so feel free to contact me.

It doesn't seem to be that important at the moment.

I did find the src tarball later on another site, but it bails out because
something is missing,
gene@coyote:~/src/pycollada-0.4$ python setup.py
Traceback (most recent call last):
  File "setup.py", line 2, in 
from setuptools import find_packages, setup
ImportError: No module named setuptools

I should go look for setuptools.

matplotlib also fails to configure, numpy is a version too old on 10.04.4 
Ubuntu, with no update for it available for that old a Ubuntu.
 
10.04.4 LTS only has 1.3, so I am blocked there too.

ATM, no biggie.  Thanks for the email.  I'm going to go make some swarf 
freehand.

> Jeff


Cheers, Gene
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
My web page:  is up!
My views 

The possession of a book becomes a substitute for reading it.
-- Anthony Burgess
I was taught to respect my elders, but its getting 
harder and harder to find any...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

2013-01-08 Thread Ian Kelly
On Tue, Jan 8, 2013 at 12:16 PM, Neal Becker  wrote:
> Did you intend to give anyone permission to use the code?  I see only a
> copyright notice, but no permissions.

It also says "Licence: python, Copyright notice may not be altered."

Which suggests to me that the intent is that it be licensed under the
PSF License, although I wouldn't want to be the one testing that
inference in a courtroom.

Dave, this announcement looks identical to the one you posted a couple
months ago, and it also seems to include the earlier version of the
source.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Offtopic] Line fitting [was Re: Numpy outlier removal]

2013-01-08 Thread Chris Angelico
On Wed, Jan 9, 2013 at 2:55 AM, Robert Kern  wrote:
> On 08/01/2013 06:35, Chris Angelico wrote:
>> ... it looks
>> quite significant to show a line going from the bottom of the graph to
>> the top, but sounds a lot less noteworthy when you see it as a
>> half-degree increase on about (I think?) 30 degrees, and even less
>> when you measure temperatures in absolute scale (Kelvin) and it's half
>> a degree in three hundred.
>
> Why on Earth do you think that the distance from nominal surface
> temperatures to freezing much less absolute 0 is the right scale to compare
> global warming changes against? You need to compare against the size of
> global mean temperature changes that would cause large amounts of human
> suffering, and that scale is on the order of a *few* degrees, not hundreds.
> A change of half a degree over a few decades with no signs of slowing down
> *should* be alarming.

I didn't say what it should be; I gave three examples. And as I said,
this is not the forum to debate climate change; I was just using it as
an example of statistical reporting.

Three types of lies.

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


New version of PyGreSQL 4.1.1 - bug fixes

2013-01-08 Thread D'Arcy J.M. Cain
-
Release of PyGreSQL version 4.1.1
-

A few problems showed up with the 4.1 release so we are releasing a
quick bugfix version.

It is available at: http://pygresql.org/files/PyGreSQL-4.1.1.tgz.

If you are running NetBSD, look in the packages directory under
databases. There is also a package in the FreeBSD ports collection.

Please refer to `changelog.txt `_
for things that have changed in this version.

Please refer to `readme.txt `_
for general information.

This version has been built and unit tested on:
 - NetBSD
 - FreeBSD
 - openSUSE 12.2
 - Windows 7 with both MinGW and Visual Studio
 - PostgreSQL 8.4, 9.0 and 9.2 32 and 64bit
 - Python 2.5, 2.6 and 2.7 32 and 64bit

| D'Arcy J.M. Cain
| [email protected]


-- 
D'Arcy J.M. Cain
PyGreSQL Development Group
http://www.PyGreSQL.org IM:[email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


How to tell how many weeks apart two datetimes are?

2013-01-08 Thread Roy Smith
How do you tell how many weeks apart two datetimes (t1 and t2) are?
The "obvious" solution would be:

weeks = (t2 - t1) / timedelta(days=7)

but that doesn't appear to be allowed.  Is there some fundamental
reason why timedelta division not supported?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to tell how many weeks apart two datetimes are?

2013-01-08 Thread marduk


On Tue, Jan 8, 2013, at 04:22 PM, Roy Smith wrote:
> How do you tell how many weeks apart two datetimes (t1 and t2) are?
> The "obvious" solution would be:
> 
> weeks = (t2 - t1) / timedelta(days=7)
> 
> but that doesn't appear to be allowed.  Is there some fundamental
> reason why timedelta division not supported?
> -- 
> http://mail.python.org/mailman/listinfo/python-list

It works for python 3(.2):

>>> x = datetime.timedelta(days=666)
>>> week = datetime.timedelta(days=7)
>>> x / week
95.14285714285714
>>> halfday = datetime.timedelta(hours=12)
>>> x / halfday
1332.0
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to tell how many weeks apart two datetimes are?

2013-01-08 Thread Ian Kelly
On Tue, Jan 8, 2013 at 2:22 PM, Roy Smith  wrote:
> How do you tell how many weeks apart two datetimes (t1 and t2) are?
> The "obvious" solution would be:
>
> weeks = (t2 - t1) / timedelta(days=7)
>
> but that doesn't appear to be allowed.  Is there some fundamental
> reason why timedelta division not supported?

Seems to be supported in Python 3.3, but not in 2.7.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to tell how many weeks apart two datetimes are?

2013-01-08 Thread Ian Kelly
On Tue, Jan 8, 2013 at 2:33 PM, Ian Kelly  wrote:
> On Tue, Jan 8, 2013 at 2:22 PM, Roy Smith  wrote:
>> How do you tell how many weeks apart two datetimes (t1 and t2) are?
>> The "obvious" solution would be:
>>
>> weeks = (t2 - t1) / timedelta(days=7)
>>
>> but that doesn't appear to be allowed.  Is there some fundamental
>> reason why timedelta division not supported?
>
> Seems to be supported in Python 3.3, but not in 2.7.

>From the docs:

Changed in version 3.2: Floor division and true division of a
timedelta object by another timedelta object are now supported, as are
remainder operations and the divmod() function. True division and
multiplication of a timedelta object by a float object are now
supported.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to tell how many weeks apart two datetimes are?

2013-01-08 Thread MRAB

On 2013-01-08 21:22, Roy Smith wrote:

How do you tell how many weeks apart two datetimes (t1 and t2) are?
The "obvious" solution would be:

weeks = (t2 - t1) / timedelta(days=7)

but that doesn't appear to be allowed.  Is there some fundamental
reason why timedelta division not supported?


Try this:

weeks = (t2 - t1).days / 7

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


Re: How to tell how many weeks apart two datetimes are?

2013-01-08 Thread Oscar Benjamin
On 8 January 2013 22:50, MRAB  wrote:
> On 2013-01-08 21:22, Roy Smith wrote:
>>
>> How do you tell how many weeks apart two datetimes (t1 and t2) are?
>> The "obvious" solution would be:
>>
>> weeks = (t2 - t1) / timedelta(days=7)
>>
>> but that doesn't appear to be allowed.  Is there some fundamental
>> reason why timedelta division not supported?
>>
> Try this:
>
> weeks = (t2 - t1).days / 7

You beat me to it...

$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> dt1 = datetime.datetime.now()
>>> dt2 = dt1 - datetime.timedelta(days=8)
>>> (dt2 - dt1) / 7 > datetime.timedelta(days=14)
False


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


Re: [Offtopic] Line fitting [was Re: Numpy outlier removal]

2013-01-08 Thread Robert Kern

On 08/01/2013 20:14, Chris Angelico wrote:

On Wed, Jan 9, 2013 at 2:55 AM, Robert Kern  wrote:

On 08/01/2013 06:35, Chris Angelico wrote:

... it looks
quite significant to show a line going from the bottom of the graph to
the top, but sounds a lot less noteworthy when you see it as a
half-degree increase on about (I think?) 30 degrees, and even less
when you measure temperatures in absolute scale (Kelvin) and it's half
a degree in three hundred.


Why on Earth do you think that the distance from nominal surface
temperatures to freezing much less absolute 0 is the right scale to compare
global warming changes against? You need to compare against the size of
global mean temperature changes that would cause large amounts of human
suffering, and that scale is on the order of a *few* degrees, not hundreds.
A change of half a degree over a few decades with no signs of slowing down
*should* be alarming.


I didn't say what it should be;


Actually, you did. You stated that "a ~0.6 deg increase across ~30 years [is 
h]ardly statistically significant". Ignoring the confusion between statistical 
significance and practical significance (as external criteria like the 
difference between the nominal temp and absolute 0 or the right criteria that I 
mentioned has nothing to do with statistical significance), you made a positive 
claim that it wasn't significant.



I gave three examples.


You gave negligently incorrect ones. Whether your comments were on topic or not, 
you deserve to be called on them when they are wrong.



And as I said,
this is not the forum to debate climate change; I was just using it as
an example of statistical reporting.

Three types of lies.


FUD is a fourth.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Searching through two logfiles in parallel?

2013-01-08 Thread Oscar Benjamin
On 8 January 2013 19:16, darnold  wrote:
> i don't think in iterators (yet), so this is a bit wordy.
> same basic idea, though: for each message (set of parameters), build a
> list of transactions consisting of matching send/receive times.

The advantage of an iterator based solution is that we can avoid
loading all of both log files into memory.

[SNIP]
>
> results = {}
>
> for line in sendData.split('\n'):
> if not line.strip():
> continue
>
> timestamp, params = parse(line)
> if params not in results:
> results[params] = [{'sendTime': timestamp, 'receiveTime':
> None}]
> else:
> results[params].append({'sendTime': timestamp, 'receiveTime':
> None})
[SNIP]

This kind of logic is made a little easier (and more efficient) if you
use a collections.defaultdict instead of a dict since it saves needing
to check if the key is in the dict yet. Example:

>>> import collections
>>> results = collections.defaultdict(list)
>>> results
defaultdict(, {})
>>> results['asd'].append(1)
>>> results
defaultdict(, {'asd': [1]})
>>> results['asd'].append(2)
>>> results
defaultdict(, {'asd': [1, 2]})
>>> results['qwe'].append(3)
>>> results
defaultdict(, {'qwe': [3], 'asd': [1, 2]})


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


Best way to do this? List loop (matrix?) iteration

2013-01-08 Thread andydtaylor
Hi!

I might be missing the obvious, or I may have found something more complicated 
than the VBA I am used to. Could it be I need to use a maths library?

For a given list of k items I'd like to turn it into an k*k matrix of item 
pairs.

List_sample = ['a', 'b', 'c']

Output:

aa ab ac
ba bb bc
ca cb cc

I'd like to have 2 hooks into this process
1. I want the opportunity to use a value pair each time they are generated 
(because I need to send these to an api and get a number back to put into a 
temporary list or dictionary - still tbd).
2. I'd also like to know each time a row is completed so I can bank that 
temporary list to a database table. Else build one big list and do it at the 
end, I'm still figuring this out.

#Code I've tried:

   stn_count = len(stn_list_short)
   for rowcount in range (0, stn_count):
  for colcount in range (0, stn_count):
 print stn_list_long[rowcount] stn_list_long[colcount]

I've found itertools, tee, and product and felt I was getting warmer. I'm still 
looking, but any pointers would be appreciated!

Thanks,

Andy


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


Re: [Offtopic] Line fitting [was Re: Numpy outlier removal]

2013-01-08 Thread Steven D'Aprano
On Tue, 08 Jan 2013 04:07:08 -0500, Terry Reedy wrote:

>> But that is not fitting a line by eye, which is what I am talking
>> about.
> 
> With the line constrained to go through 0,0 a line eyeballed with a 
> clear ruler could easily be better than either regression line, as a
> human will tend to minimize the deviations *perpendicular to the line*,
> which is the proper thing to do (assuming both variables are measured
> in the same units).

It is conventional to talk about "residuals" rather than deviations.

And it could even more easily be worse than a regression line. And since 
eyeballing is entirely subjective and impossible to objectively verify, 
the line that you claim minimizes the residuals might be very different 
from the line that I claim minimizes the residuals, and no way to decide 
between the two claims.

In any case, there is a technique for working out ordinary least squares 
(OLS) linear regression using perpendicular offsets rather than vertical 
offsets:

http://mathworld.wolfram.com/LeastSquaresFittingPerpendicularOffsets.html

but in general, if you have to care about errors in the dependent 
variable, you're better off using a more powerful technique than just OLS.

The point I keep making, that everybody seems to be ignoring, is that 
eyeballing a line of best fit is subjective, unreliable and impossible to 
verify. How could I check that the line you say is the "best fit" 
actually *is* the *best fit* for the given data, given that you picked 
that line by eye? Chances are good that if you came back to the data a 
month later, you'd pick a different line!

As I have said, eyeballing a line is fine for rough back of the envelope 
type calculations, where you only care that you have a line pointing more 
or less in the right direction. But for anything where accuracy is 
required, line fitting by eye is down in the pits of things not to do, 
right next to "making up the answers you prefer".



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


Re: Best way to do this? List loop (matrix?) iteration

2013-01-08 Thread Mitya Sirenef

On Tue 08 Jan 2013 07:19:59 PM EST, [email protected] wrote:

Hi!

I might be missing the obvious, or I may have found something more complicated 
than the VBA I am used to. Could it be I need to use a maths library?

For a given list of k items I'd like to turn it into an k*k matrix of item 
pairs.

List_sample = ['a', 'b', 'c']

Output:

aa ab ac
ba bb bc
ca cb cc

I'd like to have 2 hooks into this process
1. I want the opportunity to use a value pair each time they are generated 
(because I need to send these to an api and get a number back to put into a 
temporary list or dictionary - still tbd).
2. I'd also like to know each time a row is completed so I can bank that 
temporary list to a database table. Else build one big list and do it at the 
end, I'm still figuring this out.

#Code I've tried:

stn_count = len(stn_list_short)
for rowcount in range (0, stn_count):
   for colcount in range (0, stn_count):
  print stn_list_long[rowcount] stn_list_long[colcount]

I've found itertools, tee, and product and felt I was getting warmer. I'm still 
looking, but any pointers would be appreciated!

Thanks,

Andy





You can use itertools.product("abc", repeat=2) together with itertools 
recipe grouper() from the same page:


http://docs.python.org/3.3/library/itertools.html?highlight=itertools#itertools


HTH, -m


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Specifying two log files with one configuration file

2013-01-08 Thread Vinay Sajip
Peter Steele  gmail.com> writes:

> I have been unable to get this to work. My current conf file looks like this:

Try with the following changes:

[logger_test]
level: DEBUG
handlers: test
propagate: 0
qualname: test

The qualname: test is what identifies the logger as the logger named 'test', and
propagate: 0 prevents the test message from being passed up to the root logger.

Regards,

Vinay Sajip

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


Re: Best way to do this? List loop (matrix?) iteration

2013-01-08 Thread Chris Angelico
On Wed, Jan 9, 2013 at 11:19 AM,   wrote:
>stn_count = len(stn_list_short)
>for rowcount in range (0, stn_count):
>   for colcount in range (0, stn_count):
>  print stn_list_long[rowcount] stn_list_long[colcount]

First off, you can iterate over the list directly:

for row in stn_list_short:
  for col in stn_list_short:
print row + col

(I'm not sure what your code was doing with the print line, because it
ought to have failed. Explicit concatenation will work.)

Secondly, you can make a list of all of those pairs with a compact
notation called a comprehension:

pairs = [row + col for row in stn_list_short for col in stn_list_short]

That covers your requirement #2, giving you a full list of all of
them. How big is k going to be? Storing the whole list in memory will
get a little awkward if you have very large k; on this system, I
started seeing performance issues with a thousand elements in the
list, but you could probably go to ten thousand (ie a hundred million
pairs) if you have a decent bit of RAM.

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


Re: [Offtopic] Line fitting [was Re: Numpy outlier removal]

2013-01-08 Thread Jason Friedman
> Statistical analysis is a huge science. So is lying. And I'm not sure
> most people can pick one from the other.

Chris, your sentence causes me to think of Mr. Twain's sentence, or at
least the one he popularized:
http://www.twainquotes.com/Statistics.html.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Offtopic] Line fitting [was Re: Numpy outlier removal]

2013-01-08 Thread Jason Friedman
> Statistical analysis is a huge science. So is lying. And I'm not sure
> most people can pick one from the other.

Chris, your sentence causes me to think of Mr. Twain's sentence, or at
least the one he popularized:
http://www.twainquotes.com/Statistics.html.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get the selected text of the webpage in chrome through python ?

2013-01-08 Thread iMath
在 2013年1月8日星期二UTC+8下午9时19分51秒,Bruno Dupuis写道:
> On Mon, Jan 07, 2013 at 08:20:28PM -0800, iMath wrote:
> 
> > How to get the selected text of the webpage in chrome through python ?
> 
> 
> 
> What you need is a way to get selected text from wherever it comes. The
> 
> way to do this depends on your graphical environment. If you use X, i'd make a
> 
> a quick and dirty call to xclip -o, although there must be a pure python
> 
> implementation which in turn depends on the python framework you play
> 
> with (gtk/qt/wx/tk/...).
> 
> 
> 
> Short answer is : it depends on your system, and it may be easier and more
> 
> portable if you use a graphical toolkit.
> 
> 
> 
> cheers.
> 
> 
> 
> -- 
> 
> Bruno Dupuis

I use it on WinXP , I also learned PyQt4 .
can u help ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get the selected text of the webpage in chrome through python ?

2013-01-08 Thread iMath
在 2013年1月8日星期二UTC+8下午9时11分30秒,Dave Angel写道:
> On 01/08/2013 07:38 AM, Dennis Lee Bieber wrote:
> 
> > On Mon, 7 Jan 2013 20:20:28 -0800 (PST), iMath 
> 
> > declaimed the following in gmane.comp.python.general:
> 
> >
> 
> >> How to get the selected text of the webpage in chrome through python ?
> 
> > Chrome is a browser, is it not... If you want to get the text in
> 
> > Python, you have to write the equivalent of a browser -- something that
> 
> > parses the returned HTML message, extracting the parts you want.
> 
> 
> 
> To get a selection, you can either wait till it's copied to the
> 
> clipboard, and get it from there, or you can write a addon for the
> 
> application.  Since Chrome accepts addons, I suggest you research that
> 
> angle.  Probably mozilla.org is the place to start, and function
> 
> 
> 
> nsISelectionController::GetSelection() might be some place to examine.
> 
> 
> 
> You might find some hints in
> 
> http://stackoverflow.com/questions/2671474/range-selection-and-mozilla
> 
> 
> 
> 
> 
> if you are going to work with the clipboard, realize that it works quite
> 
> differently from one OS to another, and even from one desktop manager to
> 
> another.
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

It seems need Javascript to work it on .
Unfortunately ,I am only familiar with python .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to implement mouse gesture by python or pyqt ?

2013-01-08 Thread iMath
在 2013年1月8日星期二UTC+8上午8时44分20秒,iMath写道:
> It would be better to give me some examples .thanks in advance !
> 
> 
> 
> P.S. which module or lib are needed ?

what I wanna perhaps like this:
when a right mouse button is pressed and we go down and right with a cursor. As 
in letter 'L'. Our mouse gesture will close the window.
I googled such gesture examples on PyQt4 ,but hard to find one ,so your help 
will be greatly appreciated !
thanks inadvance !
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: PyDTLS

2013-01-08 Thread rbit
Thank you. I will gladly port to Python 3 if there is interest from
the community.

Regarding PEP 3156: asynchronous use of unreliable network protocols
makes for an interesting use case. In particular, it forces
applications to deal with packet loss under some circumstances. One
such situation occurs during DTLS's handshaking phase: if no response
is received from the peer after some period of time, we must assume
that our most recent datagram has been lost, and so we need to
retransmit. The event loop interface as outlined in the PEP makes this
a bit difficult (as did the asyncore module). One possible way to make
things easier would be by adding two parameters to add_reader: a
callable to retrieve the current timeout, and a callable that is
invoked if that timeout expires before the descriptor becomes
readable. Each loop iteration would then collect all given timeouts,
and pass the minimum of that set to whatever polling facility it
invokes. If that timeout expires, the corresponding timeout handler
would be invoked prior to the next loop iteration.

The PEP also considers only stream transports when referring to
"transport." Datagram transports do not, for example, have the
property that calling t.write(b'abc'); t.write(b'def') is equivalent
to calling t.write(b'abcdef'). I'm not sure what sort of impact this
omission of datagram transports has for an implementation. Though I
would certainly like to see datagram transports be treated as
first-class citizens, despite not being nearly used as often as stream
transports. I would hope that an implementer of, say, RTP over UDP,
can tie into the same event loop as someone implementing a
single-process, single-threaded Web server.

Implementing DTLS as a tulip transport sounds interesting. Is the
tulip package available somewhere so that I can try it out?

Ray

On Tue, Jan 8, 2013 at 6:53 AM, Guido van Rossum  wrote:
> This sounds exciting. Are you considering a Python 3 port? It might make a
> nice demo of PEP 3156.
>
>
> On Monday, January 7, 2013, rbit wrote:
>>
>> I would like to announce Datagram Transport Layer Security for
>> Python. From the top of the project README:
>>
>> PyDTLS brings Datagram Transport Layer Security (DTLS - RFC 6347:
>> http://tools.ietf.org/html/rfc6347) to the Python environment. In a
>> nutshell, DTLS brings security (encryption, server authentication,
>> user authentication, and message authentication) to UDP datagram
>> payloads in a manner equivalent to what SSL/TLS does for TCP stream
>> content.
>>
>> DTLS is now very easy to use in Python. If you're familiar with the
>> ssl module in Python's standard library, you already know how. All it
>> takes is passing a datagram/UDP socket to the *wrap_socket* function
>> instead of a stream/TCP socket. Here's how one sets up the client side
>> of a connection:
>>
>> import ssl
>> from socket import socket, AF_INET, SOCK_DGRAM
>> from dtls import do_patch
>> do_patch()
>> sock = ssl.wrap_socket(socket(AF_INET, SOCK_DGRAM))
>> sock.connect(('foo.bar.com', 1234))
>> sock.send('Hi there')
>>
>> The project is hosted at https://github.com/rbit/pydtls, and licensed
>> under
>> the Apache license 2.0. PyPI has packages. I can be reached
>> at code AT liquibits DOT com for questions, feedback, etc.
>>
>> http://pypi.python.org/pypi/Dtls/0.1.0";>Dtls 0.1.0 -
>>   Datagram Transport Layer Security for Python.  (07-Jan-13)
>> --
>> http://mail.python.org/mailman/listinfo/python-announce-list
>>
>> Support the Python Software Foundation:
>> http://www.python.org/psf/donations/
>
>
>
> --
> --Guido van Rossum (python.org/~guido)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: PyDTLS

2013-01-08 Thread Guido van Rossum
On Tue, Jan 8, 2013 at 8:39 PM, rbit  wrote:
> Thank you. I will gladly port to Python 3 if there is interest from
> the community.

Python 3 is where it's at! :-)

> Regarding PEP 3156: asynchronous use of unreliable network protocols
> makes for an interesting use case. In particular, it forces
> applications to deal with packet loss under some circumstances.

But don't you have to deal with that when doing synchronous I/O as
well? It's a datagram protocol after all.

> One
> such situation occurs during DTLS's handshaking phase: if no response
> is received from the peer after some period of time, we must assume
> that our most recent datagram has been lost, and so we need to
> retransmit.

Is this something the transport can handle, or does the protocol (and
hence the application) need to be involved here?

> The event loop interface as outlined in the PEP makes this
> a bit difficult (as did the asyncore module). One possible way to make
> things easier would be by adding two parameters to add_reader: a
> callable to retrieve the current timeout, and a callable that is
> invoked if that timeout expires before the descriptor becomes
> readable. Each loop iteration would then collect all given timeouts,
> and pass the minimum of that set to whatever polling facility it
> invokes. If that timeout expires, the corresponding timeout handler
> would be invoked prior to the next loop iteration.

Hm, this would add a fair amount of complexity to the event loop. It's
true that I don't have the complete story for timeouts yet, but I am
hopeful that things like this can be implemented by using call_later()
with some callback that does the retransmit (and resets some internal
state), and cancelling that callback whenever a packet is received
(i.e. in the protocol's datagram_received() method).

> The PEP also considers only stream transports when referring to
> "transport." Datagram transports do not, for example, have the
> property that calling t.write(b'abc'); t.write(b'def') is equivalent
> to calling t.write(b'abcdef').

Yeah, obviously this invariant only applies to stream protocols. The
PEP currently doesn't really specify datagram support (it's just in
the Open Issues section).

> I'm not sure what sort of impact this
> omission of datagram transports has for an implementation. Though I
> would certainly like to see datagram transports be treated as
> first-class citizens, despite not being nearly used as often as stream
> transports. I would hope that an implementer of, say, RTP over UDP,
> can tie into the same event loop as someone implementing a
> single-process, single-threaded Web server.

Yeah, at the level of the eventloop proper (the APIs that deal with
callbacks, not futures, transports and protocols) datagrams won't be a
problem. There will have to be separate specifications for the
transport and protocol  interfaces used with datagrams.

> Implementing DTLS as a tulip transport sounds interesting. Is the
> tulip package available somewhere so that I can try it out?

Absolutely -- it is very much in flux, but you can check out the
latest source from http://code.google.com/p/tulip/source/checkout
using Mercurial.

--Guido

> Ray
>
> On Tue, Jan 8, 2013 at 6:53 AM, Guido van Rossum  wrote:
>> This sounds exciting. Are you considering a Python 3 port? It might make a
>> nice demo of PEP 3156.
>>
>>
>> On Monday, January 7, 2013, rbit wrote:
>>>
>>> I would like to announce Datagram Transport Layer Security for
>>> Python. From the top of the project README:
>>>
>>> PyDTLS brings Datagram Transport Layer Security (DTLS - RFC 6347:
>>> http://tools.ietf.org/html/rfc6347) to the Python environment. In a
>>> nutshell, DTLS brings security (encryption, server authentication,
>>> user authentication, and message authentication) to UDP datagram
>>> payloads in a manner equivalent to what SSL/TLS does for TCP stream
>>> content.
>>>
>>> DTLS is now very easy to use in Python. If you're familiar with the
>>> ssl module in Python's standard library, you already know how. All it
>>> takes is passing a datagram/UDP socket to the *wrap_socket* function
>>> instead of a stream/TCP socket. Here's how one sets up the client side
>>> of a connection:
>>>
>>> import ssl
>>> from socket import socket, AF_INET, SOCK_DGRAM
>>> from dtls import do_patch
>>> do_patch()
>>> sock = ssl.wrap_socket(socket(AF_INET, SOCK_DGRAM))
>>> sock.connect(('foo.bar.com', 1234))
>>> sock.send('Hi there')
>>>
>>> The project is hosted at https://github.com/rbit/pydtls, and licensed
>>> under
>>> the Apache license 2.0. PyPI has packages. I can be reached
>>> at code AT liquibits DOT com for questions, feedback, etc.
>>>
>>> http://pypi.python.org/pypi/Dtls/0.1.0";>Dtls 0.1.0 -
>>>   Datagram Transport Layer Security for Python.  (07-Jan-13)
>>> --
>>> http://mail.python.org/mailman/listinfo/python-announce-list
>>>
>>> Support the Python Software Foundation:
>>> http://www.python.org/psf/d

Re: ANN: PyDTLS

2013-01-08 Thread rbit
On Tue, Jan 8, 2013 at 9:09 PM, Guido van Rossum  wrote:
> But don't you have to deal with that when doing synchronous I/O as
> well? It's a datagram protocol after all.

No: when dealing with blocking sockets, the OpenSSL library activates its
own retransmission timers, and the application never becomes aware of
whether timeouts occurred. Since OpenSSL can't do this in the the case of
non-blocking sockets, it becomes the application's problem to call back
into OpenSSL at some point in the future (the very same time for which
OpenSSL would have set its own timeout had the socket been blocking).
OpenSSL exports functions DTLSv1_get_timeout and DTLSv1_handle_timeout to
applications to address this case. The timeouts start at one second, and
double for each encountered timeout, until the timeout ceiling of one
minutes is reached. I'm using the term "application" here for any code that
uses the OpenSSL library, but is not part of it.

>> One
>> such situation occurs during DTLS's handshaking phase: if no response
>> is received from the peer after some period of time, we must assume
>> that our most recent datagram has been lost, and so we need to
>> retransmit.
>
> Is this something the transport can handle, or does the protocol (and
> hence the application) need to be involved here?

Given my current understanding of the PEP, I think this can be handled in
the transport. Maybe there's some pitfall here that I can't quite see yet -
even more reason for me to try to implement it.

>> The event loop interface as outlined in the PEP makes this
>> a bit difficult (as did the asyncore module). One possible way to make
>> things easier would be by adding two parameters to add_reader: a
>> callable to retrieve the current timeout, and a callable that is
>> invoked if that timeout expires before the descriptor becomes
>> readable. Each loop iteration would then collect all given timeouts,
>> and pass the minimum of that set to whatever polling facility it
>> invokes. If that timeout expires, the corresponding timeout handler
>> would be invoked prior to the next loop iteration.
>
> Hm, this would add a fair amount of complexity to the event loop. It's
> true that I don't have the complete story for timeouts yet, but I am
> hopeful that things like this can be implemented by using call_later()
> with some callback that does the retransmit (and resets some internal
> state), and cancelling that callback whenever a packet is received
> (i.e. in the protocol's datagram_received() method).

Yes, ok, I can see how that could work, too. I thought that it might make
sense to centralize handling timeouts in the event loop in order to prevent
proliferation in the transports (since there are multiple event loop
implementations, perhaps a mix-in would be good?). I think one will want to
contain handshake (vs. application data) timeout handling at least to the
transport, though, and not let it spill over into various protocols. I'm
not sure yet where the right place is for cancelling a timeout callback.

>> Implementing DTLS as a tulip transport sounds interesting. Is the
>> tulip package available somewhere so that I can try it out?
>
> Absolutely -- it is very much in flux, but you can check out the
> latest source from http://code.google.com/p/tulip/source/checkout
> using Mercurial.

All right, thanks, I'll check it out.

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


Re: [Offtopic] Line fitting [was Re: Numpy outlier removal]

2013-01-08 Thread Steven D'Aprano
On Wed, 09 Jan 2013 07:14:51 +1100, Chris Angelico wrote:

> Three types of lies.

Oh, surely more than that.

White lies. 

Regular or garden variety lies.

Malicious lies.

Accidental or innocent lies.

FUD -- "fear, uncertainty, doubt".

Half-truths.

Lying by omission.

Exaggeration and understatement.

Propaganda.

Misinformation.

Disinformation.

Deceit by emphasis.

And manufactured doubt.

E.g. the decades long campaign by the tobacco companies to deny that 
tobacco products caused cancer, when their own scientists were telling 
them that they did. Having learnt how valuable falsehoods are, those same 
manufacturers of doubt went on to sell their services to those who wanted 
to deny that CFCs destroyed ozone, and that CO2 causes warming. 


The old saw about "lies, damned lies and statistics" reminds me very much 
of a quote from Homer Simpson:

"Pfff, facts, you can prove anything that's even remotely true with 
facts!"



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