Re: IMAP4_SSL, libgmail, GMail and corporate firewall/proxy

2011-02-16 Thread Chris Rebert
On Wed, Feb 16, 2011 at 10:44 PM, Malcolm Greene  wrote:
> Andrea,
>
> What type of result do you get trying port 993 ?

i.e. The port Google explicitly says to use for IMAP in Gmail's help docs.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: toy list processing problem: collect similar terms

2011-02-16 Thread WJ
Xah Lee wrote:

> here's a interesting toy list processing problem.
> 
> I have a list of lists, where each sublist is labelled by
> a number. I need to collect together the contents of all sublists
> sharing
> the same label. So if I have the list
> 
> ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> r) (5 s t))
> 
> where the first element of each sublist is the label, I need to
> produce:
> 
> output:
> ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
> 

Solving without hash-tables or "group-by".

Using Guile:

First, sort the groups by the numbers.

(stable-sort groups (lambda(a b)(< (car a) (car b

((0 a b) (1 c d) (1 i j) (2 e f) (2 k l) (2 o p) (3 g h)
 (4 m n) (4 q r) (5 s t))

Next, flatten the list.

(append-map identity step1)

(0 a b 1 c d 1 i j 2 e f 2 k l 2 o p 3 g h 4 m n 4 q r 5 s t)

Remove duplicate numbers.

(delete-duplicates step2)

(0 a b 1 c d i j 2 e f k l o p 3 g h 4 m n q r 5 s t)

We now need a very useful function called "scan".

;; Yields sublists of contiguous items that satisfy func.
(define (scan func lst)
  (let ((tail (find-tail func lst)))
(if tail
  (cons (take-while func tail)
(scan func (drop-while func tail)))
  '(

(scan symbol? step3)

((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to gain root privileges

2011-02-16 Thread Dan Stromberg
On Wed, Feb 16, 2011 at 6:59 PM, Adam Skutt  wrote:
> On Feb 16, 9:00 pm, Dan Stromberg  wrote:
>> So yeah, whether you use perl or anything else invoked with #!, you're
>> pretty much better off with sudo, or a tiny C wrapper that's so simple
>> it's hard to get wrong.
>
> UNIX makes this almost impossible

Please give a source that includes justifying detail.  Then you have the
right to ask me if there are well understood ways around each of the issues
identified.

> unless your wrapper is cooperative
> with whatever process invokes it

Isn't this what we were discussing?  I know I was.  Other than you and I
both suggesting sudo as an alternative, that is.

> , which is itself a security risk.  I
> advise anyone seriously considering this route to take a long, hard
> look at just what contortions sudo goes through in order to achieve
> this safety.  A correct suid program is neither tiny nor simple.

Actually, they can be.  Here's one, as a very easy-to-find example:

$ ls -al
cmd started 2011 Wed Feb 16 10:19:21 PM
total 8
drwxr-xr-x  2 dstromberg dstromberg 4096 2011-02-16 22:19 .
drwxr-xr-x 61 dstromberg dstromberg 4096 2011-02-16 22:18 ..
-rwsr-xr-x  1 root   dstromberg0 2011-02-16 22:19
correct-and-simple-setuid
benchbox-dstromberg:~/src/correct-and-simple-setuid i686-pc-linux-gnu 12006
- above cmd done 2011 Wed Feb 16 10:19 PM

$ ./correct-and-simple-setuid
cmd started 2011 Wed Feb 16 10:19:24 PM
benchbox-dstromberg:~/src/correct-and-simple-setuid i686-pc-linux-gnu 12006
- above cmd done 2011 Wed Feb 16 10:19 PM

$ echo $?
cmd started 2011 Wed Feb 16 10:19:26 PM
0
benchbox-dstromberg:~/src/correct-and-simple-setuid i686-pc-linux-gnu 12006
- above cmd done 2011 Wed Feb 16 10:19 PM

It's a correct, setuid replacement for /bin/true.  It's 0 bytes long.  Now,
I expect you to exploit it, since I claim that it's both tiny and simple.
IOW, it's a reductio ad absurdum.

sudo is an example of something that _isn't_ closely matched with what it's
running (beyond a little text in a config file), not something that _is_.
 You're engaging in a category error there.

In fact, sudo has a _lot_ of extra complexity to achieve a degree of
genericity.

A better example is apache suexec - the helper portion of which is  428
physical SLOC, and it's more complex than most need to be because it keeps
many env vars and prunes out others.

> Passing things through sudo(1) is really the only sensible route these
> days but even that can be fraught with peril.

Wait - I thought you just said that it was almost impossible unless the
setuid wrapper was well matched with the program in question.

Oh, you did.

Or was writing sudo almost impossible?  Well, no, it may have some expert
knowledge built into it, but I believe it was just a mechanical process of
translating a surmountable number of bugtraq discussions into code.

>  For something as simple
> as, 'Write to a normally restricted area' it's probably no more secure
> than an ACL (and potentially way less if you screw up the sudo
> configuration).

ACL's aren't at all unreasonable, though I believe they'll likely yield a
superset of the same problems one would see with a sticky directory.  But
then, neither are setgid and setuid unreasonable - with setgid being
preferred over setuid.

>> However, perl's taint feature would be useful
>> irrespective when writing privileged code; it removes some of the
>> skill required.
>
> I don't really think so.  It doesn't help prevent, for example,
> someone redirecting stdout to /etc/shadow and then running your
> command.

Quantifier error: You've given an example of why taint isn't perfect, not a
proof of why it's useless.  I said it would be useful, not that it was 100%
trouble free.

Moreover, I'd like to see your code demonstrating the ability to make a
simple setuid wrapper that doesn't even open a file, append to /etc/shadow.
If you'd like, I'll write the wrapper for you.

Note that >> is performed by the shell, not the wrapper.  If that
unprivileged shell (or unprivileged C program, or whatever) can >>
/etc/shadow, then you've got bigger problems to worry about.

Oh, did you mean the _subprogram_ could be exploited?  Why didn't you say
so?  And why is this an attribute of the wrapper, and not an attribute of
the subprogram?  IOW, you appear to be reasoning that because the
_subprogram_ the wrapper invokes /might/ be flawed, the teensy setuid
_wrapper_ /definitely/is/inherently/ broken.  This, assuming you're really
suggesting this, is both a category error and a quantifier error.

>  Besides, I'm not even remotely convinced that 'removing
> skill' is a good idea.

Fine.  Forget Python.  Forget C.  Forget assembly language.  Forget machine
language.  Forget firmware programming.  Forget plugboards.  Forget GUI's.
Forget curses.  Forget command line.  Forget punch cards.  Forget toggle
switches.  In fact, forget electricity where it might remove skill.  Since
removing skill is a bad idea, I expect that you'll greatly pr

Re: IMAP4_SSL, libgmail, GMail and corporate firewall/proxy

2011-02-16 Thread Malcolm Greene
Andrea,

What type of result do you get trying port 993 ?

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


Re: IMAP4_SSL, libgmail, GMail and corporate firewall/proxy

2011-02-16 Thread Andrea Gavana
Hi,

On 17 February 2011 10:28, BJ Swope wrote:
> Imap is not on port 443.  IIRC, it's late and I'm to lazy to even google it
> right now, but it's port 143 isn't it.

I have tried that, and I get the following with imaplib:

Traceback (most recent call last):
  File "D:\MyProjects\gmail.py", line 17, in 
m = imaplib.IMAP4_SSL('imap.gmail.com', 143)
  File "C:\Python26\lib\imaplib.py", line 1138, in __init__
IMAP4.__init__(self, host, port)
  File "C:\Python26\lib\imaplib.py", line 163, in __init__
self.open(host, port)
  File "C:\Python26\lib\imaplib.py", line 1149, in open
self.sock = socket.create_connection((host, port))
  File "C:\Python26\lib\socket.py", line 514, in create_connection
raise error, msg
socket.error: [Errno 10061] No connection could be made because the
target machine actively refused it


And this one with libgmail:

Traceback (most recent call last):
  File "D:\MyProjects\gmail2.py", line 8, in 
ga.login()
  File "C:\Python26\lib\site-packages\libgmail.py", line 305, in login
pageData = self._retrievePage(req)
  File "C:\Python26\lib\site-packages\libgmail.py", line 348, in _retrievePage
resp = self.opener.open(req)
  File "C:\Python26\lib\site-packages\mechanize\_opener.py", line 193, in open
response = urlopen(self, req, data)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 344, in _open
'_open', req)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 332, in _call_chain
result = func(*args)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 1171, in https_open
return self.do_open(conn_factory, req)
  File "C:\Python26\lib\site-packages\gmail_transport.py", line 143, in do_open
return ClientCookie.HTTPSHandler.do_open(self,
ProxyHTTPSConnection.new_auth(self.proxy, self.proxy_user,
self.proxy_passwd), req)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 1118, in do_open
raise URLError(err)
urllib2.URLError: 

:-( :-(

Thank you for your answer.


> On Wed, Feb 16, 2011 at 11:58 PM, Andrea Gavana 
> wrote:
>>
>> Hi All,
>>
>>    I apologize in advance if I'm going to write very stupid things,
>> my expertise in http/socket/imap stuff is very close to zero. I'm
>> using Python 2.6.5 on Windows XP SP3.
>>
>> I am trying to access my GMail account from my office, and it appears
>> our company's firewall is blocking all SMTP/POP3/IMAP attempts or
>> there is simply something I don't understand. My first try was to use
>> imaplib as follows:
>>
>>
>> import imaplib
>>
>> m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
>> m.login(username, password)
>>
>>
>> And I get the following:
>>
>>
>> Traceback (most recent call last):
>>  File "D:\MyProjects\gmail.py", line 17, in 
>>    m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
>>  File "C:\Python26\lib\imaplib.py", line 1138, in __init__
>>    IMAP4.__init__(self, host, port)
>>  File "C:\Python26\lib\imaplib.py", line 163, in __init__
>>    self.open(host, port)
>>  File "C:\Python26\lib\imaplib.py", line 1150, in open
>>    self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
>>  File "C:\Python26\lib\ssl.py", line 350, in wrap_socket
>>    suppress_ragged_eofs=suppress_ragged_eofs)
>>  File "C:\Python26\lib\ssl.py", line 118, in __init__
>>    self.do_handshake()
>>  File "C:\Python26\lib\ssl.py", line 293, in do_handshake
>>    self._sslobj.do_handshake()
>> ssl.SSLError: [Errno 8] _ssl.c:480: EOF occurred in violation of protocol
>>
>>
>> OK. Then I googled back and forth for a possible solution, and I got a
>> hint that (maybe) the problem could be related to proxy
>> authentication. And I tried this solution (which uses SocksiPy) I
>> found on the web (username2 and password2 are the user name and
>> password for our company's proxy):
>>
>> import socks
>> import socket
>>
>> proxy_ip = "10.100.100.20"   # Your proxy IP/DNS here
>>
>> socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy_ip, 8080, True,
>> username2, password2)
>> socket.socket = socks.socksocket
>>
>> import imaplib
>>
>> m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
>> m.login(username, password)
>>
>>
>> This solution just hangs forever at the line:
>>
>> m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
>>
>> And it never returns (it never gets to the m.login() stuff).
>>
>>
>> Then I tried with libgmail, giving it the option of setting the proxy
>> name and port:
>>
>> import libgmail
>>
>> # Connect from behind a proxy www.myproxy.org:3128 using
>> # proxy authentication user = 'john', password = 'proxy4321'
>> libgmail.PROXY_URL = username2:password2@my_company_proxy:443'  #
>> Define the proxy
>>
>> ga = libgmail.GmailAccount(username, password)
>> ga.login()
>>
>>
>> And I got this at first:
>>
>> Traceback (most recent call last):
>>  File "D:\MyProjects\gmail2.py", line 8, in 
>>    ga.login()
>>  File "C:\Python26\lib\site-packages\libgmail.py", line 305, in login
>>    pageData = self._retrievePage(req)
>>

Re: IMAP4_SSL, libgmail, GMail and corporate firewall/proxy

2011-02-16 Thread BJ Swope
Imap is not on port 443.  IIRC, it's late and I'm to lazy to even google it
right now, but it's port 143 isn't it.



On Wed, Feb 16, 2011 at 11:58 PM, Andrea Gavana wrote:

> Hi All,
>
>I apologize in advance if I'm going to write very stupid things,
> my expertise in http/socket/imap stuff is very close to zero. I'm
> using Python 2.6.5 on Windows XP SP3.
>
> I am trying to access my GMail account from my office, and it appears
> our company's firewall is blocking all SMTP/POP3/IMAP attempts or
> there is simply something I don't understand. My first try was to use
> imaplib as follows:
>
>
> import imaplib
>
> m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
> m.login(username, password)
>
>
> And I get the following:
>
>
> Traceback (most recent call last):
>  File "D:\MyProjects\gmail.py", line 17, in 
>m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
>  File "C:\Python26\lib\imaplib.py", line 1138, in __init__
>IMAP4.__init__(self, host, port)
>  File "C:\Python26\lib\imaplib.py", line 163, in __init__
>self.open(host, port)
>  File "C:\Python26\lib\imaplib.py", line 1150, in open
>self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
>  File "C:\Python26\lib\ssl.py", line 350, in wrap_socket
>suppress_ragged_eofs=suppress_ragged_eofs)
>  File "C:\Python26\lib\ssl.py", line 118, in __init__
>self.do_handshake()
>  File "C:\Python26\lib\ssl.py", line 293, in do_handshake
>self._sslobj.do_handshake()
> ssl.SSLError: [Errno 8] _ssl.c:480: EOF occurred in violation of protocol
>
>
> OK. Then I googled back and forth for a possible solution, and I got a
> hint that (maybe) the problem could be related to proxy
> authentication. And I tried this solution (which uses SocksiPy) I
> found on the web (username2 and password2 are the user name and
> password for our company's proxy):
>
> import socks
> import socket
>
> proxy_ip = "10.100.100.20"   # Your proxy IP/DNS here
>
> socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy_ip, 8080, True,
> username2, password2)
> socket.socket = socks.socksocket
>
> import imaplib
>
> m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
> m.login(username, password)
>
>
> This solution just hangs forever at the line:
>
> m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
>
> And it never returns (it never gets to the m.login() stuff).
>
>
> Then I tried with libgmail, giving it the option of setting the proxy
> name and port:
>
> import libgmail
>
> # Connect from behind a proxy www.myproxy.org:3128 using
> # proxy authentication user = 'john', password = 'proxy4321'
> libgmail.PROXY_URL = username2:password2@my_company_proxy:443'  #
> Define the proxy
>
> ga = libgmail.GmailAccount(username, password)
> ga.login()
>
>
> And I got this at first:
>
> Traceback (most recent call last):
>  File "D:\MyProjects\gmail2.py", line 8, in 
>ga.login()
>  File "C:\Python26\lib\site-packages\libgmail.py", line 305, in login
>pageData = self._retrievePage(req)
>  File "C:\Python26\lib\site-packages\libgmail.py", line 340, in
> _retrievePage
>req = ClientCookie.Request(urlOrRequest)
>  File "C:\Python26\lib\site-packages\mechanize\_request.py", line 31,
> in __init__
>if not _rfc3986.is_clean_uri(url):
>  File "C:\Python26\lib\site-packages\mechanize\_rfc3986.py", line 63,
> in is_clean_uri
>return not bool(BAD_URI_CHARS_RE.search(uri))
> TypeError: expected string or buffer
>
>
> Then I brutally hacked into mechanize here and there and I was able to
> fix all non-internet related errors. And when I try the libgmail
> solution above now I get:
>
> Traceback (most recent call last):
>  File "D:\MyProjects\gmail2.py", line 8, in 
>ga.login()
>  File "C:\Python26\lib\site-packages\libgmail.py", line 305, in login
>pageData = self._retrievePage(req)
>  File "C:\Python26\lib\site-packages\libgmail.py", line 348, in
> _retrievePage
>resp = self.opener.open(req)
>  File "C:\Python26\lib\site-packages\mechanize\_opener.py", line 193, in
> open
>response = urlopen(self, req, data)
>  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
> line 344, in _open
>'_open', req)
>  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
> line 332, in _call_chain
>result = func(*args)
>  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
> line 1171, in https_open
>return self.do_open(conn_factory, req)
>  File "C:\Python26\lib\site-packages\gmail_transport.py", line 145, in
> do_open
>return ClientCookie.HTTPSHandler.do_open(self,
> ProxyHTTPSConnection.new_auth(self.proxy, self.proxy_user,
> self.proxy_passwd), req)
>  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
> line 1118, in do_open
>raise URLError(err)
> urllib2.URLError:  Forbidden ( The ISA Server denied the specified Uniform Resource
> Locator (URL).  )>
>
>
> Just in case, I have tried also with port 80, with similar results:
>
> Traceback (most recent call last):
>  File "D:\MyProjects\gmail2.py", line 8,

Re: Best way to gain root privileges

2011-02-16 Thread Dan Stromberg
On Wed, Feb 16, 2011 at 6:10 PM, GSO  wrote:
>> pretty much better off with sudo, or a tiny C wrapper that's so simple
>> it's hard to get wrong.  However, perl's taint feature would be useful
>
> This snippet is about as tiny as it gets in C I think:

Well, it could be tinier really, and actually, this isn't that bad.

> #include 
>
> int main (int argc, char ** argv) {
> int err;
>
> char *newenv[] = { NULL };
>
> if ((err = execle("/usr/bin/pauseme", "pauseme", NULL, newenv)) < 0 ) {
>        exit(err);
>        }
>
> return 0; // never reached!
> }
>
> http://linuxgazette.net/67/tag/20.html
>
> But even this is considered to be risky.

Some people just like to make security sound impossible; they think it
makes them look smarter or something.  Then again, this is Linux
Gazette, which usually caters to relative Linux novices, so maybe such
stern warnings are appropriate (for a while - sometimes novices don't
stay novices forever).

If you empty the environment, don't spawn a subshell, don't scan the
path, avoid reading or writing undefined memory, and carefully check
your error returns, you're generally in good shape with something as
simple as a wrapper.  Yes, simple.

The signal vulnerability thing appears to be mostly about exploiting
overcomplicated signal handlers in the child process (EG, don't use
any system calls in signal handlers - not to be confused with
system(3)), and the file descriptor thing ISTR is mostly an issue when
someone calls open() without correctly checking for an error return -
but something like Python would typically traceback - it wouldn't just
pretend things were fine.

However, if you still think C wrappers are too complex, I believe
there's nothing stopping _you_ from using sudo.
-- 
http://mail.python.org/mailman/listinfo/python-list


IMAP4_SSL, libgmail, GMail and corporate firewall/proxy

2011-02-16 Thread Andrea Gavana
Hi All,

I apologize in advance if I'm going to write very stupid things,
my expertise in http/socket/imap stuff is very close to zero. I'm
using Python 2.6.5 on Windows XP SP3.

I am trying to access my GMail account from my office, and it appears
our company's firewall is blocking all SMTP/POP3/IMAP attempts or
there is simply something I don't understand. My first try was to use
imaplib as follows:


import imaplib

m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
m.login(username, password)


And I get the following:


Traceback (most recent call last):
  File "D:\MyProjects\gmail.py", line 17, in 
m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
  File "C:\Python26\lib\imaplib.py", line 1138, in __init__
IMAP4.__init__(self, host, port)
  File "C:\Python26\lib\imaplib.py", line 163, in __init__
self.open(host, port)
  File "C:\Python26\lib\imaplib.py", line 1150, in open
self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
  File "C:\Python26\lib\ssl.py", line 350, in wrap_socket
suppress_ragged_eofs=suppress_ragged_eofs)
  File "C:\Python26\lib\ssl.py", line 118, in __init__
self.do_handshake()
  File "C:\Python26\lib\ssl.py", line 293, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [Errno 8] _ssl.c:480: EOF occurred in violation of protocol


OK. Then I googled back and forth for a possible solution, and I got a
hint that (maybe) the problem could be related to proxy
authentication. And I tried this solution (which uses SocksiPy) I
found on the web (username2 and password2 are the user name and
password for our company's proxy):

import socks
import socket

proxy_ip = "10.100.100.20"   # Your proxy IP/DNS here

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, proxy_ip, 8080, True,
username2, password2)
socket.socket = socks.socksocket

import imaplib

m = imaplib.IMAP4_SSL('imap.gmail.com', 443)
m.login(username, password)


This solution just hangs forever at the line:

m = imaplib.IMAP4_SSL('imap.gmail.com', 443)

And it never returns (it never gets to the m.login() stuff).


Then I tried with libgmail, giving it the option of setting the proxy
name and port:

import libgmail

# Connect from behind a proxy www.myproxy.org:3128 using
# proxy authentication user = 'john', password = 'proxy4321'
libgmail.PROXY_URL = username2:password2@my_company_proxy:443'  #
Define the proxy

ga = libgmail.GmailAccount(username, password)
ga.login()


And I got this at first:

Traceback (most recent call last):
  File "D:\MyProjects\gmail2.py", line 8, in 
ga.login()
  File "C:\Python26\lib\site-packages\libgmail.py", line 305, in login
pageData = self._retrievePage(req)
  File "C:\Python26\lib\site-packages\libgmail.py", line 340, in _retrievePage
req = ClientCookie.Request(urlOrRequest)
  File "C:\Python26\lib\site-packages\mechanize\_request.py", line 31,
in __init__
if not _rfc3986.is_clean_uri(url):
  File "C:\Python26\lib\site-packages\mechanize\_rfc3986.py", line 63,
in is_clean_uri
return not bool(BAD_URI_CHARS_RE.search(uri))
TypeError: expected string or buffer


Then I brutally hacked into mechanize here and there and I was able to
fix all non-internet related errors. And when I try the libgmail
solution above now I get:

Traceback (most recent call last):
  File "D:\MyProjects\gmail2.py", line 8, in 
ga.login()
  File "C:\Python26\lib\site-packages\libgmail.py", line 305, in login
pageData = self._retrievePage(req)
  File "C:\Python26\lib\site-packages\libgmail.py", line 348, in _retrievePage
resp = self.opener.open(req)
  File "C:\Python26\lib\site-packages\mechanize\_opener.py", line 193, in open
response = urlopen(self, req, data)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 344, in _open
'_open', req)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 332, in _call_chain
result = func(*args)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 1171, in https_open
return self.do_open(conn_factory, req)
  File "C:\Python26\lib\site-packages\gmail_transport.py", line 145, in do_open
return ClientCookie.HTTPSHandler.do_open(self,
ProxyHTTPSConnection.new_auth(self.proxy, self.proxy_user,
self.proxy_passwd), req)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 1118, in do_open
raise URLError(err)
urllib2.URLError: 


Just in case, I have tried also with port 80, with similar results:

Traceback (most recent call last):
  File "D:\MyProjects\gmail2.py", line 8, in 
ga.login()
  File "C:\Python26\lib\site-packages\libgmail.py", line 305, in login
pageData = self._retrievePage(req)
  File "C:\Python26\lib\site-packages\libgmail.py", line 348, in _retrievePage
resp = self.opener.open(req)
  File "C:\Python26\lib\site-packages\mechanize\_opener.py", line 193, in open
response = urlopen(self, req, data)
  File "C:\Python26\lib\site-packages\mechanize\_urllib2_fork.py",
line 344, in _open
'_open', 

Re: Is this a bug of str.join?

2011-02-16 Thread Cameron Simpson
On 16Feb2011 12:01, Terry Reedy  wrote:
| On 2/16/2011 1:32 AM, fireinice wrote:
| >I'm sorry, I found it should be the terminal width caused visual
| >problem, please kindly ignore this post.
| 
| For future reference, the way to get more info about what is really
| in a string is to print its repr(), or even its list() version.
| Len() is also helpful. On Windows, both the Command Prompt window
| and IDLE refuse to expand tabs, so
| 
| >>> s='\t'.join(('a','b','c'))
| >>> str(s)
| 'a\tb\tc'
| >>> repr(s)
| "'a\\tb\\tc'"
| >>> len(s)
| 5
| >>> list(s)
| ['a', '\t', 'b', '\t', 'c']

And on UNIX, a common way to inspect program output at the character
level is like this:

  python my_python-program | od -c

possibly sent off into a temp file. That will let you see what character
are actually escaping from the program (versus terry's suggestion, which
is good for looking at the characters before they escape).

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

I'm not making any of this up you know. - Anna Russell
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method chaining on decorator got SyntaxError

2011-02-16 Thread alex23
Makoto Kuwata  wrote:
> I'm sad about this restriction because:
>
>     @recipe.product('*.html').ingreds('$(1).rst')
>     def file_html(c):
>         # do something
>
> is enough simple and more readable than:
>
>     @recipe.product('*.html')
>     @recipe.ingreds('$(1).rst')
>     def file_html(c):
>         # do something
>
> But I'll follow the Python's philosophy.

Personally, I'd find this approach a lot clearer:

rst2html = recipe.product('*.html').ingred('$(1).rst')

@rst2html
def file_html(c):
   # etc

It allows for easier reuse of your decorators and it labels it in a
way that I can understand from the name as opposed to having to
decipher the decorator method chain to work out its intent.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to gain root privileges

2011-02-16 Thread GSO
I essentially don't want to take a risk with a home CCTV prog., so
unless I can persuade a highly skilled Unix programmer to write a
wrapper (which I can't), then I think I'm best sticking with sudo.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to gain root privileges

2011-02-16 Thread GSO
>
> Passing things through sudo(1) is really the only sensible route these
> days but even that can be fraught with peril.  For something as simple
> as, 'Write to a normally restricted area' it's probably no more secure
> than an ACL (and potentially way less if you screw up the sudo
> configuration).
>

OK, so I'm heading towards sudo then, aiming to make sure I don't
screw up the configuration.  This is a home CCTV application, so I
want things as secure as possible.  A setgid wrapper would require the
kind of skilled programming that I couldn't do myself in order to keep
things at a high level of security, but sudo I can handle.

There is also policykit http://live.gnome.org/PolicyKit which I
mentioned in the initial post I think - not sure if this python lib
can be used to do what I need though...
https://fedorahosted.org/python-slip/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to gain root privileges

2011-02-16 Thread Nobody
On Thu, 17 Feb 2011 01:47:10 +0100, Alexander Kapps wrote:

>> Having said that I'm possibly arriving at the conclusion that a quick
>> perl script might be the simplest/easiest and most secure option - I
>> read perl includes code to safely run suid perl scripts - will dig out
>> my perl tomes.
> 
> Not sure, but Perl is just another "scripting language" (hate that 
> term) and you cannot have scripts be SUID.

Perl installations may include a setuid version of the Perl interpreter
under the name "sperl", which effectively allows you to have setuid Perl
scripts.

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


Re: Best way to gain root privileges

2011-02-16 Thread Adam Skutt
On Feb 16, 9:00 pm, Dan Stromberg  wrote:
> So yeah, whether you use perl or anything else invoked with #!, you're
> pretty much better off with sudo, or a tiny C wrapper that's so simple
> it's hard to get wrong.

UNIX makes this almost impossible unless your wrapper is cooperative
with whatever process invokes it, which is itself a security risk.  I
advise anyone seriously considering this route to take a long, hard
look at just what contortions sudo goes through in order to achieve
this safety.  A correct suid program is neither tiny nor simple.

Passing things through sudo(1) is really the only sensible route these
days but even that can be fraught with peril.  For something as simple
as, 'Write to a normally restricted area' it's probably no more secure
than an ACL (and potentially way less if you screw up the sudo
configuration).

> However, perl's taint feature would be useful
> irrespective when writing privileged code; it removes some of the
> skill required.

I don't really think so.  It doesn't help prevent, for example,
someone redirecting stdout to /etc/shadow and then running your
command.  Besides, I'm not even remotely convinced that 'removing
skill' is a good idea.

It especially doesn't help you very much when the whole point of your
script is just a wrapper to elevate privileges (execute another
programs) or copy files about.

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


Re: Method chaining on decorator got SyntaxError

2011-02-16 Thread Makoto Kuwata
On Thu, Feb 17, 2011 at 11:40 AM, MRAB  wrote:
> On 17/02/2011 01:55, Makoto Kuwata wrote:
>>
>> Thank you MRAB,
>>
>> On Thu, Feb 17, 2011 at 8:49 AM, MRAB  wrote:
>>>
>>> You may want to read the discussion at:
>>>
>>>
>>> https://groups.google.com/group/python-ideas/browse_thread/thread/1eebf486969c39a1/?hl=en
>>> --
>>
>> I can't figure out what is the point or conclusion of that discussion.
>> Is there any technical reason? Or Ideological reason?
>>
> Ideological.
>
> Keep it simple.
>
> "Readability, not succinctness, is what's Pythonic." -- Mike Meyer

Thank you, MRAB.
I'm sad about this restriction because:

@recipe.product('*.html').ingreds('$(1).rst')
def file_html(c):
# do something

is enough simple and more readable than:

@recipe.product('*.html')
@recipe.ingreds('$(1).rst')
def file_html(c):
# do something

But I'll follow the Python's philosophy.

--
regards,
makoto kuwata
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to gain root privileges

2011-02-16 Thread Adam Skutt
On Feb 16, 8:40 pm, GSO  wrote:
> Apols for being a nuisance.  I'm normally if anything a web programmer.
>
> It looks like there are set-id functions in the os module.  Further I
> don't actually need root privileges, just write access to a directory
> that a user ordinarily does not have write access to (and preferably
> not read).

So give them that instead, preferably via ACL. Reliably denying read
access may be difficult, however.  Chances are pretty good that any
solution you create won't be any more secure than this, though.

>  So a call to os.setegid(egid) with a group created for the
> program's use alone would do this then. (Unless this is bad technique
> security wise otherwise, as a uid 0 seteuid call would be considered;
> but surely what I am thinking of doing is not a security risk.)

Except in order to do this you need to be root, of course, or make the
users members of that group anyway (in which case, just use the damn
ACL).

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


Re: Method chaining on decorator got SyntaxError

2011-02-16 Thread MRAB

On 17/02/2011 01:55, Makoto Kuwata wrote:

Thank you MRAB,

On Thu, Feb 17, 2011 at 8:49 AM, MRAB  wrote:

You may want to read the discussion at:

https://groups.google.com/group/python-ideas/browse_thread/thread/1eebf486969c39a1/?hl=en
--


I can't figure out what is the point or conclusion of that discussion.
Is there any technical reason? Or Ideological reason?


Ideological.

Keep it simple.

"Readability, not succinctness, is what's Pythonic." -- Mike Meyer
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to handle sockets - easily?

2011-02-16 Thread William Ahern
Bubba  wrote:
> William Ahern's log on stardate 16 vlj 2011

> /snip

> > I think that there's an asynchronous all-Python MySQL library, but
> > I'm not sure. Maybe one day I can open source my asynchronous MySQL C
> > library. (I always recommend people to use PostgreSQL, though; which
> > is superior in almost every way, especially the C client library and
> > the wire protocol.) 

> I have no particular problem with using PgSQL.

> Which of these would you recommend? http://wiki.postgresql.org/wiki/Python

I don't use Python so I couldn't recommend one over another. I just took an
interest in your earlier post because I saw MySQL and asynchronous mentioned
together. It instantly occurred to me that there might be a conflict there,
and quickly reading the Python MySQLdb manual page confirmed this because it
seemed clear that it was just binding the C client API.

The PostgreSQL C client API supports asynchronous I/O, and the API and
protocol support interleaved requests over a single connection. But it's up
to the Python module writer to expose that functionality, and expose it in a
convenient and stylistically proper manner. Even if all the modules expose
the functionality, as I don't work in Python I couldn't say which does it
better.

Also, of course, there's no substitute for testing. For your particular
needs in your particular environment MySQL may work more performantly even
with its faults and the kludgy workarounds necessary. I was just warning
that you can't let a single client connection hold onto a MySQL connection
object for the duration of the client connection's lifetime, and probably
don't want the SQL requests to occur on the same thread as the client
handlers; at least without understanding the consequences.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to gain root privileges

2011-02-16 Thread GSO
> pretty much better off with sudo, or a tiny C wrapper that's so simple
> it's hard to get wrong.  However, perl's taint feature would be useful

This snippet is about as tiny as it gets in C I think:

#include 

int main (int argc, char ** argv) {
int err;

char *newenv[] = { NULL };

if ((err = execle("/usr/bin/pauseme", "pauseme", NULL, newenv)) < 0 ) {
exit(err);
}

return 0; // never reached!
}

http://linuxgazette.net/67/tag/20.html

But even this is considered to be risky.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to gain root privileges

2011-02-16 Thread Dan Stromberg
On Wed, Feb 16, 2011 at 4:47 PM, Alexander Kapps  wrote:
> On 17.02.2011 01:00, GSO wrote:
>> Having said that I'm possibly arriving at the conclusion that a quick
>> perl script might be the simplest/easiest and most secure option - I
>> read perl includes code to safely run suid perl scripts - will dig out
>> my perl tomes.
>
> Not sure, but Perl is just another "scripting language" (hate that term) and
> you cannot have scripts be SUID.
>
> I have almost no experiences with Perl, but I really doubt, that the general
> problem would be solved with it.

It depends on what OS you're on:

#1 On some OS's, setuid #! is an instant root - the script doesn't
even need to run.  In fact, the problem hinges on the script not
running.

#2 On others, setuid #! is safe.  Or rather, safe if you write your
script pretty carefully.

#3 On still others, setuid #! doesn't change your access at all; it's
just run under the uid that started the script.

The perl folk have attempted to make #2 safer with their "taint" stuff
(think "dataflow for user inputs to avoid invoking subshells with user
inputted data").  There's not a lot they could do about #1 - perl
doesn't really enter the picture there.  And for #3, you still need a
wrapper of some sort, otherwise your script doesn't change users.

So yeah, whether you use perl or anything else invoked with #!, you're
pretty much better off with sudo, or a tiny C wrapper that's so simple
it's hard to get wrong.  However, perl's taint feature would be useful
irrespective when writing privileged code; it removes some of the
skill required.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method chaining on decorator got SyntaxError

2011-02-16 Thread Makoto Kuwata
Thank you MRAB,

On Thu, Feb 17, 2011 at 8:49 AM, MRAB  wrote:
> You may want to read the discussion at:
>
> https://groups.google.com/group/python-ideas/browse_thread/thread/1eebf486969c39a1/?hl=en
> --

I can't figure out what is the point or conclusion of that discussion.
Is there any technical reason? Or Ideological reason?

--
regards,
makoto kuwata
-- 
http://mail.python.org/mailman/listinfo/python-list


Best way to gain root privileges

2011-02-16 Thread GSO
Apols for being a nuisance.  I'm normally if anything a web programmer.

It looks like there are set-id functions in the os module.  Further I
don't actually need root privileges, just write access to a directory
that a user ordinarily does not have write access to (and preferably
not read).  So a call to os.setegid(egid) with a group created for the
program's use alone would do this then.  (Unless this is bad technique
security wise otherwise, as a uid 0 seteuid call would be considered;
but surely what I am thinking of doing is not a security risk.)

> I have almost no experiences with Perl, but I really doubt, that the general
> problem would be solved with it.
>

Quoting from the article linked to by Steven D'Aprano:

"If you are new to secure programming, I recommend either sudo or a
Perl script. SUID Perl scripts have built-in protection to prevent
programmers from making the mistakes addressed in this article."

Perl has something called 'tainted mode' built in, which for example
will prevent what it judges as untrustworthy data being appended to
the end of the passwd file.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to handle sockets - easily?

2011-02-16 Thread Jean-Paul Calderone
On Feb 16, 1:59 pm, William Ahern 
wrote:
> Bubba  wrote:
>
> 
>
>
>
>
>
>
>
>
>
> > import asyncore
> > import socket
> > import string
> > import MySQLdb
> > import sys
> 
> >     def __init__(self, host, port):
> >         asyncore.dispatcher.__init__(self)
> >         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
> >         self.set_reuse_addr()
> >         self.bind((host, port))
> >         self.listen(5)
>
> >     def handle_accept(self):
> >         pair = self.accept()
> >         if pair is None:
> >             pass
> >         else:
> >             sock, addr = pair
> >             print 'Incoming connection from %s' % repr(addr)
> >             handler = Handler(sock)
>
> > server = Server('', 2020)
> > asyncore.loop()
>
> 
> > I do, however, have some more questions (thus crosspost to
> > comp.lang.python) - how many connections can this server handle without
> > a problem? I'm using Asyncore module, as it can be seen.
> > Is it necessary, due to the fact that it should serve more than
> > thousand devices that send data every 10 seconds, to do threading (I
> > believe that is already done with Asyncore for sockets, but what about
> > SQL?)
>
> The MySQL C library is not asynchronous. Each request does blocking I/O. If
> the MySQLdb module is a wrapper for the MySQL C library--and it seems it
> is--then you will want to use threads (not coroutines or generators). For
> all I know your accept handler is threaded already. MySQL itself almost
> certainly can't handle tens of thousands of simultaneous requests. The
> backend connection and query handler is simply-threaded as well, which means
> for every connection your talking 2 * "tens of thousands" threads. I didn't
> read over your code much, but the only way to get around this would be to
> handle your socket I/O asynchronously; but I don't enough about Python to
> get the mixed behavior you'd want.
>
> I think that there's an asynchronous all-Python MySQL library, but I'm not
> sure. Maybe one day I can open source my asynchronous MySQL C library. (I
> always recommend people to use PostgreSQL, though; which is superior in
> almost every way, especially the C client library and the wire protocol.)

There's the very new .
There's also
.

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


Re: Python Newbie needs some context

2011-02-16 Thread Fred Marshall

On 2/16/2011 11:45 AM, Grant Edwards wrote:
 Thanks for the advice!

Is it the intent to generate code with wxGlade and then rather
"import" that code into an Eclipse project context?  Or, should
one expect to be able to create hooks (e.g. for Tools) in Eclipse
that will do that?  If so, how?

Thanks,

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


Re: Best way to gain root privileges

2011-02-16 Thread Alexander Kapps

On 17.02.2011 01:00, GSO wrote:

OK, thanks for the tips.

gksu* does not seem to be included with RHEL6 Desktop (though there is
a package called beesu)


On RHEL try consolehelper/userhelper instead which need additional 
configuration.



The philosophy at the end of the day I think
is do your own thing so a hacker cannot download the code you used.


Nonsense. :-)
Real crackers don't need to download your source and home-brewed 
solutions are almost always the wrong solution for security issues 
(compare: better write your own cryptographic algorithm or use 
existing ones, even those who are open source?)


If public accessible source code would be a security risk, then 
Linux would be *the* most vulnerable OS ever.


Anyway, if you're really that much concerned about security, than 
drop the whole idea and do not let non-admins perform that job. Or, 
see if SElinux can help.



Having said that I'm possibly arriving at the conclusion that a quick
perl script might be the simplest/easiest and most secure option - I
read perl includes code to safely run suid perl scripts - will dig out
my perl tomes.


Not sure, but Perl is just another "scripting language" (hate that 
term) and you cannot have scripts be SUID.


I have almost no experiences with Perl, but I really doubt, that the 
general problem would be solved with it.


Perhaps explaining your exact situation would help finding a fitting 
solution.



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


Re: How to use Python well?

2011-02-16 Thread Steven D'Aprano
On Thu, 17 Feb 2011 10:12:52 +1100, Ben Finney wrote:

> Terry Reedy  writes:
> 
>> The most import thing is automated tests.
> 
> Steven D'Aprano  writes:
> 
>> The most important thing is structured programming and modularization.
> 
> 
> Steel-cage death match. FIGHT!

To the death? No, to the pain!

http://en.wikiquote.org/wiki/The_Princess_Bride



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


Re: Best way to gain root privileges

2011-02-16 Thread GSO
OK, thanks for the tips.

gksu* does not seem to be included with RHEL6 Desktop (though there is
a package called beesu), and besides which it appears gksu is
deprecated[1].  Either way c wrapper or sudo approach it is a tactical
decision, and the former is probably a better option with the problem
I have (though I will at the end of the day probably use both).  I
googled c wrapper and there are a ton of issues, type of c system call
to use, closing/reopening file handles, etc.  Whole books have been
written on the subject.  The philosophy at the end of the day I think
is do your own thing so a hacker cannot download the code you used.
[1] http://live.gnome.org/gksu

Having said that I'm possibly arriving at the conclusion that a quick
perl script might be the simplest/easiest and most secure option - I
read perl includes code to safely run suid perl scripts - will dig out
my perl tomes.


G.

On 16 February 2011 22:45, Emile van Sebille  wrote:
> On 2/16/2011 1:26 PM GSO said...
>>
>> I'm sure this question is as old as time, but what is the best way to
>> gain root privileges?  (Am using Python 2.6.5, pygtk2 v2.16, Gtk
>> v2.18.9, on RHEL6.)
>>
>
> have root's password?
>
> Emile
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Method chaining on decorator got SyntaxError

2011-02-16 Thread MRAB

On 16/02/2011 23:25, Makoto Kuwata wrote:

Hi,

I have a question about decorator.
I tried the following example and got Syntax Error.

 class deco(object):
 def __init__(self, name):
 self._name = name
 def foo(self, value):
 self._foo = value
 return self
 def __call__(self, func):
 func._deco = self
 return func

 ## ok
 @deco('aaa')
 def f1(): pass

 ## Syntax Error
 @deco('aaa').foo('bbb')  # SyntaxError: invalid syntax
 def f2(): pass

The above code shows that Python doesn't allow method chain
on decorator syntax.
Why does this limitation exist?
I want to chain methods as a certain DSL, just like:

 @recipe().product('*.html').ingreds('$(1).rst')
 def file_html(c):
 system(c%"rst2html.py $(ingred)>  $(product)")

If you know the reason of the restriction, let me know it.


You may want to read the discussion at:

https://groups.google.com/group/python-ideas/browse_thread/thread/1eebf486969c39a1/?hl=en
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with giant font sizes in tkinter

2011-02-16 Thread Rhodri James
On Tue, 15 Feb 2011 07:06:35 -, Steven D'Aprano  
 wrote:



On Sun, 13 Feb 2011 23:42:06 +, Rhodri James wrote:


On Fri, 11 Feb 2011 02:08:01 -, Steven D'Aprano
 wrote:


On Thu, 10 Feb 2011 15:48:47 +, Cousin Stanley wrote:


Steven D'Aprano wrote:


I have a tkinter application under Python 2.6 which is shows text in
a giant font, about twenty(?) times larger than expected.

[...]

In this case I think you're missing a hyphen.  Try

titlefont = '-Adobe-Helvetica-Bold-R-Normal--*-180-*'


Thanks for the suggestion, but xlsfonts returns the same eight font files
regardless of whether the hyphen is there or not. (I wouldn't expect
otherwise, since the font matching treats * as a text wildcard and will
let it cross field boundaries.)


Bother.  It was a lovely theory, but I should have read the man page first.

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Method chaining on decorator got SyntaxError

2011-02-16 Thread Makoto Kuwata
Hi,

I have a question about decorator.
I tried the following example and got Syntax Error.

class deco(object):
def __init__(self, name):
self._name = name
def foo(self, value):
self._foo = value
return self
def __call__(self, func):
func._deco = self
return func

## ok
@deco('aaa')
def f1(): pass

## Syntax Error
@deco('aaa').foo('bbb')  # SyntaxError: invalid syntax
def f2(): pass

The above code shows that Python doesn't allow method chain
on decorator syntax.
Why does this limitation exist?
I want to chain methods as a certain DSL, just like:

@recipe().product('*.html').ingreds('$(1).rst')
def file_html(c):
system(c%"rst2html.py $(ingred) > $(product)")

If you know the reason of the restriction, let me know it.

--
regards,
makoto kuwata
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to use Python well?

2011-02-16 Thread Roy Smith
In article 
,
 snorble  wrote:

> I use Python a lot, but not well. I usually start by writing a small
> script, no classes or modules.

One anti-pattern that I see in my own code is starting out thinking, 
"this is just a little script, I doesn't need any real structure".  That 
almost always turns out to be wrong, but by the time I start to realize 
I'm writing spaghetti code, it's so temping to say, "I don't have the 
time to refactor this now, I'll just hack on it a bit more".  Which, of 
course, makes it even harder to unravel later.

The first step is to break up a monolithic script into a few functions.  
I encourage myself to do that from the git-go by keeping a template 
around:

#!/usr/bin/env python

def main():
pass

if __name__ == '__main__':
main()

and I use that whenever I start a new script.  That at least gets me off 
on the right foot.

The next step is to turn my collection of functions (with the inevitable 
collection of global variables that lets them communicate) into a class 
with methods and instance variables.  I can't tell you how many times 
I've started out saying, "this isn't going to be complicated enough to 
justify making it a class".  Inevitably, I'm wrong.

Finally, the next layer of stupid mistake I often make is to start out 
saying, "This isn't going to be complicated enough to justify writing 
unit tests".  Inevitably, I'm wrong about that too.

So far, none of the above is at all specific to Python,  It's equally 
true in any language.

Now, for some Python-specific advice; you can write Fortran in any 
language.  What that means is it's one thing to translate some existing 
script into Python and make it work, but it's another to actually take 
advantage of some of Python's biggest strengths.  Learn to be 
comfortable with list comprehensions, generator expressions, and 
iterators in general.  Learn about Python's advanced data structures 
such as sets, defaultdicts, and named tuples.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to use Python well?

2011-02-16 Thread Ben Finney
Terry Reedy  writes:

> The most import thing is automated tests.

Steven D'Aprano  writes:

> The most important thing is structured programming and modularization. 


Steel-cage death match. FIGHT!

-- 
 \ “[W]e are still the first generation of users, and for all that |
  `\  we may have invented the net, we still don't really get it.” |
_o__)   —Douglas Adams |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to use Python well?

2011-02-16 Thread Steven D'Aprano
On Wed, 16 Feb 2011 10:35:28 -0800, snorble wrote:

> I use Python a lot, but not well. I usually start by writing a small
> script, no classes or modules. Then I add more content to the loops, and
> repeat. It's a bit of a trial and error learning phase, making sure I'm
> using the third party modules correctly, and so on. I end up with a
> working script, but by the end it looks messy, unorganized, and feels
> hacked together. I feel like in order to reuse it or expand it in the
> future, I need to take what I learned and rewrite it from scratch.
[...]
> Or maybe I'm looking for is best practices for how to organize the
> structure of a Python program. I love Python and I just want to be able
> to use it well.

I don't think best practice for writing Python is that much different 
from best practice for other languages. The main difference is that 
Python is multi-paradigm: you can mix procedural, functional and object-
oriented code in the one program.

You should read about bottom-up and top-down programming. You'll probably 
end up doing some of both, but mostly top-down.

The most important thing is structured programming and modularization. 
The debate over structured programming was won so decisively that people 
have forgotten that there was ever an argument to be made for spaghetti 
code!

You can learn a lot (and lose a lot of time!) reading the c2.com wiki at 
http://c2.com/cgi/wiki. These may be useful:

http://c2.com/cgi/wiki?StructuredProgramming
http://c2.com/cgi/wiki?WhatIsRefactoring


Break your code up into small pieces, whether you use functions or 
classes doesn't really matter, although for small scripts functions are 
simpler and have less mental overhead. Instead of writing one giant 
monolithic block of code that does twenty things, write one function for 
each thing, and then one extra main function to call them. This 
encourages code reuse, ease of testing, and simplifies maintenance.


I find that I've learned more from "things to avoid" than from "things to 
do". Something about reading about disasters makes it really clear why 
you shouldn't do it that way :)

Avoid:

* GOTO, but Python doesn't have that :)

* Copy-and-paste programming. If you want to do almost the same thing 
twice, don't copy the relevant code, paste it, and make a small 
modification to it. Write one or more functions that handle the common 
code, and call the function.

* Functions that do unrelated things. Functions should do one thing, 
although that "thing" can get quite complicated.

* Don't sweep errors under the rug. Don't catch exceptions unless you can 
do something about them. "Just ignore it, I'm sure it's not important" is 
rarely appropriate.


See also: 
http://c2.com/cgi/fullSearch?search=CategoryDevelopmentAntiPattern



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


Please post Chuck Missler Bible audio/video

2011-02-16 Thread Keith Anthony
Anything from Chuck Missler .  I've lost my collection.
Thanks.


-- 
- --- -- -
Posted with NewsLeecher v4.0 Final
Web @ http://www.newsleecher.com/?usenet
--- -  -- -

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


Re: Best way to gain root privileges

2011-02-16 Thread Emile van Sebille

On 2/16/2011 1:26 PM GSO said...

I'm sure this question is as old as time, but what is the best way to
gain root privileges?  (Am using Python 2.6.5, pygtk2 v2.16, Gtk
v2.18.9, on RHEL6.)



have root's password?

Emile


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


Re: Best way to gain root privileges

2011-02-16 Thread Alexander Kapps

On 16.02.2011 23:02, Ian Kelly wrote:

On Wed, Feb 16, 2011 at 2:29 PM, Daniel Mahoney  wrote:

On Wed, 16 Feb 2011 21:26:26 +, GSO wrote:


I'm sure this question is as old as time, but what is the best way to
gain root privileges?  (Am using Python 2.6.5, pygtk2 v2.16, Gtk
v2.18.9, on RHEL6.)


Gain root privileges for a script? Write a c wrapper to call the script,
chown it (the wrapper) to root, and set it (the wrapper) suid.


Or for better security, write a shell script that execs the Python
script via sudo.



This is what I occasionally use (in a non security critical 
environment anyway):


#!/bin/sh

SUDO=gksudo
[ -z "$DISPLAY" ] && SUDO=sudo
[ "$(id -u)" != 0 ] && exec $SUDO $0 $@

# your_root_privileges_requiring_commands_here


Don't ask me for any security issues. Properly setting up a 
dedicated user group and grant dedicated sudo privileges is probably 
a better way.



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


Re: How to use Python well?

2011-02-16 Thread Terry Reedy

On 2/16/2011 1:35 PM, snorble wrote:

I use Python a lot, but not well. I usually start by writing a small
script, no classes or modules. Then I add more content to the loops,
and repeat. It's a bit of a trial and error learning phase, making
sure I'm using the third party modules correctly, and so on. I end up
with a working script, but by the end it looks messy, unorganized, and
feels hacked together. I feel like in order to reuse it or expand it
in the future, I need to take what I learned and rewrite it from
scratch.


Not a completely bad idea, except for the 'from scratch' part. Parts of 
code that work may just need reorganizing.


The most import thing is automated tests. They should grow with the 
code. Tests are like having a safety net.



If I peeked over a Python expert's shoulder while they developed
something new, how would their habits differ? Do they start with
classes from the start?


Depends on whether the particular purpose needs user-defined classes or 
is fine with functions using built-in classes.


--
Terry Jan Reedy

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


Re: [newbie/2.5.1.1] Computing value of a word?

2011-02-16 Thread Chris Colbert
On Wed, Feb 16, 2011 at 4:27 AM, Chris Rebert  wrote:

> On Wed, Feb 16, 2011 at 1:17 AM, Gilles Ganault  wrote:
> > Hello,
> >
> > For a game, I need to go through a wordlist, and for each word,
> > compute its value, ie. a=1, b=2, etc.
> >
> > So for instance, NewYork = 14 + 5 + 23 + 25 + 15 + 18 + 11 = 111.
> >
> > Before I write the obvious While loop to go through each line in the
> > input text file, I was wondering if Python didn't already have some
> > function to perform this type of computation.
>
> A = ord('a') - 1
> for line in your_file:
>word = line.strip().lower()
>score = sum(ord(letter)-A for letter in word)
>
>
Or a one-liner (import not included):

In [26]: import numpy as np

In [27]: (np.frombuffer(buffer('NewYork'.lower()), dtype='uint8') -
96).sum()
Out[27]: 111
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to gain root privileges

2011-02-16 Thread Alister Ware
On Wed, 16 Feb 2011 21:26:26 +, GSO wrote:

> I'm sure this question is as old as time, but what is the best way to
> gain root privileges?  (Am using Python 2.6.5, pygtk2 v2.16, Gtk
> v2.18.9, on RHEL6.)
> 
> Ta,
> 
> 
> G.
> 
> gmotion
> PyGTK desktop GUI for Motion (software motion detector)
> http://code.google.com/p/gmotion/

The easiest way to gain root privileges is to be come sysadmin

Credit to the fortune application & the original anonymous poster

(sorry Couldn't resist that one)

-- 
Weinberg's Principle:
An expert is a person who avoids the small errors while
sweeping on to the grand fallacy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Problems of Symbol Congestion in Computer Languages

2011-02-16 Thread Xah Lee
might be interesting.

〈Problems of Symbol Congestion in Computer Languages (ASCII Jam;
Unicode; Fortress)〉
http://xahlee.org/comp/comp_lang_unicode.html

--
Problems of Symbol Congestion in Computer Languages (ASCII Jam;
Unicode; Fortress)

Xah Lee, 2011-02-05, 2011-02-15

Vast majority of computer languages use ASCII as its character set.
This means, it jams multitude of operators into about 20 symbols.
Often, a symbol has multiple meanings depending on contex. Also, a
sequence of chars are used as a single symbol as a workaround for lack
of symbols. Even for languages that use Unicode as its char set (e.g.
Java, XML), often still use the ~20 ASCII symbols for all its
operators. The only exceptions i know of are Mathematica, Fortress,
APL. This page gives some examples of problems created by symbol
congestion.

---
Symbol Congestion Workarounds


Multiple Meanings of a Symbol

Here are some common examples of a symbol that has multiple meanings
depending on context:

In Java, [ ] is a delimiter for array, also a delimiter for getting a
element of array, also as part of the syntax for declaring a array
type.

In Java and many other langs, ( ) is used for expression grouping,
also as delimiter for arguments of a function call, also as delimiters
for parameters of a function's declaration.

In Perl and many other langs, : is used as a separator in a ternary
expression e.g. (test ? "yes" : "no"), also as a namespace separator
(e.g. use Data::Dumper;).

In URL, / is used as path separators, but also as indicator of
protocol. e.g. http://example.org/comp/unicode.html

In Python and many others, < is used for “less than” boolean operator,
but also as a alignment flag in its “format” method, also as a
delimiter of named group in regex, and also as part of char in other
operators that are made of 2 chars, e.g.: << <= <<= <>.


Examples of Multip-Char Operators

Here are some common examples of operators that are made of multiple
characters: || && == <= != ** =+ =* := ++ -- :: // /* (* …

---
Fortress & Unicode

The language designer Guy Steele recently gave a very interesting
talk. See: Guy Steele on Parallel Programing. In it, he showed code
snippets of his language Fortress, which freely uses Unicode as
operators.

For example, list delimiters are not the typical curly bracket {1,2,3}
or square bracket [1,2,3], but the unicode angle bracket ⟨1,2,3⟩.
(See: Matching Brackets in Unicode.) It also uses the circle plus ⊕ as
operator. (See: Math Symbols in Unicode.)

---
Problems of Symbol Congestion

I really appreciate such use of unicode. The tradition of sticking to
the 95 chars in ASCII of 1960s is extremely limiting. It creates
complex problems manifested in:

* String Escape mechanism (C's backslash \n, \/, …, widely
adopted.)
* Complex delimiters for strings. (Python's triple quotes and
perl's variable delimiters q() q[] q{} m//, and heredoc. (See: Strings
in Perl and Python ◇ Heredoc mechanism in PHP and Perl.)
* Crazy leaning toothpicks syndrome, especially bad in emacs
regex.
* Complexities in character representation (See: Emacs's Key
Notations Explained (/r, ^M, C-m, RET, , M-, meta) ◇ HTML
entities problems. See: HTML Entities, Ampersand, Unicode, Semantics.)
* URL Percent Encoding problems and complexities: Javascript
Encode URL, Escape String

All these problems occur because we are jamming so many meanings into
about 20 symbols in ASCII.

See also:

* Computer Language Design: Strings Syntax
* HTML6: Your JSON and SXML Simplified

Most of today's languages do not support unicode in function or
variable names, so you can forget about using unicode in variable
names (e.g. α=3) or function names (e.g. “lambda” as “λ” or “function”
as “ƒ”), or defining your own operators (e.g. “⊕”).

However, there are a few languages i know that do support unicode in
function or variable names. Some of these allow you to define your own
operators. However, they may not allow unicode for the operator
symbol. See: Unicode Support in Ruby, Perl, Python, javascript, Java,
Emacs Lisp, Mathematica.

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


Re: How to use Python well?

2011-02-16 Thread Dan Stromberg
On Wed, Feb 16, 2011 at 10:35 AM, snorble  wrote:
> I use Python a lot, but not well. I usually start by writing a small
> script, no classes or modules. Then I add more content to the loops,
> and repeat. It's a bit of a trial and error learning phase, making
> sure I'm using the third party modules correctly, and so on. I end up
> with a working script, but by the end it looks messy, unorganized, and
> feels hacked together. I feel like in order to reuse it or expand it
> in the future, I need to take what I learned and rewrite it from
> scratch.

To some extent, writing code well just comes from practice with
programming, and practice with a language.

Exploratory programming is pretty normal (though some still insist on
having a complete design before starting on coding), but I find that
having lots of automated tests helps make exploratory programming more
practical.  You may or may not want to read a bit about agile
programming: http://en.wikipedia.org/wiki/Agile_software_development

A rewrite once in a while is not the end of the world, unless your
management decides it is (then it's just a pain to live without the
rewrite).  ^_^  Oh, and if you use modules and classes and even just
functions to limit the impact of one detail on another (each design
decision probably should be wrapped up into its own scope somehow),
you'll find that rewrites of Portions of your code are pretty viable -
without having changes need to cascade through one's codebase.

Just saying "I want this to read clearly, not run marginally faster"
helps, as does using tools like pylint, pychecker, pyflakes and/or
pep8 (pylint probably obviates the pep8 script, but pychecker or
pyflakes likely work well in combination with pep8 - so far I've only
used pylint).

Also, pymetrics is nice for its McCabe Complexity statistic - if the #
gets too high, simplify - this often means subdividing large functions
or methods into a larger number of smaller functions or methods.  But
pylint and perhaps others have a warning if your code blocks get too
long - that almost gives the same benefit as McCabe.

You may find that http://rope.sourceforge.net/ helps with your
refactoring, though I have yet to try rope - I just use vim and n.n.n.
 I hear that some IDE's support refactoring well - pycharm might be a
good example.

IOW, using some automated tools should give you lots of nearly
immediate feedback and assistance in your goal.

Yes, some people do start with classes at the outset.  Just think of a
class as a jack in the box - something with an external view, and a
different, hidden, internal view.  When you see a need for something
like a jack in a box in your code (two different views of what's going
on, to limit detail getting scattered more broadly than necessary),
consider using a class or perhaps a generator.  And yeah, sometimes
functions are enough - hey, some functional programming languages have
no other means of limiting the impact of details, and there is still
some really good functional code out there.

Finally, look over someone else's code now and then for ideas; that's
a great way to learn.  You don't necessarily have to hover over
someone's shoulder to learn from them - fortunately we live in a world
with symbolic language :).  Make sure the copyright on the code won't
bite you though.

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


Re: Best way to gain root privileges

2011-02-16 Thread Ian Kelly
On Wed, Feb 16, 2011 at 2:29 PM, Daniel Mahoney  wrote:
> On Wed, 16 Feb 2011 21:26:26 +, GSO wrote:
>
>> I'm sure this question is as old as time, but what is the best way to
>> gain root privileges?  (Am using Python 2.6.5, pygtk2 v2.16, Gtk
>> v2.18.9, on RHEL6.)
>
> Gain root privileges for a script? Write a c wrapper to call the script,
> chown it (the wrapper) to root, and set it (the wrapper) suid.

Or for better security, write a shell script that execs the Python
script via sudo.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to gain root privileges

2011-02-16 Thread Steven D'Aprano
On Wed, 16 Feb 2011 15:29:53 -0600, Daniel Mahoney wrote:

> On Wed, 16 Feb 2011 21:26:26 +, GSO wrote:
> 
>> I'm sure this question is as old as time, but what is the best way to
>> gain root privileges?  (Am using Python 2.6.5, pygtk2 v2.16, Gtk
>> v2.18.9, on RHEL6.)
> 
> Gain root privileges for a script? Write a c wrapper to call the script,
> chown it (the wrapper) to root, and set it (the wrapper) suid.

Further to this:

http://www.theillien.com/Sys_Admin_v12/html/v10/i06/a1.htm



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


Re: unicode shutil.copy() changes a file name during copy?

2011-02-16 Thread dave
thanks to your hint about drive format, i contacted MacDrive and they
confirmed it was an incorrect setting, i have since fixed the setting
and all is working!

not a python bug!

thanks for the replies.

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


Re: Editor/IDE with Python coverage support?

2011-02-16 Thread Dan Stromberg
I use vim, but I've considered switching to pycharm.  I don't know if
pycharm does this or not.

Anyway, coverage.py will produce an HTML report describing how well
your automated tests cover your code - I like to add it into my
Makefile's default rule, and then view the HTML once in a while using
a browser.

On Wed, Feb 16, 2011 at 11:50 AM, Matt Chaput  wrote:
> Are there any editors/IDEs with good support for line-coloring from Python 
> test coverage results? (I normally use Eclipse + PyDev but PyDev's current 
> coverage support isn't much better than nothing.)
>
> Thanks,
>
> Matt
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to gain root privileges

2011-02-16 Thread Daniel Mahoney
On Wed, 16 Feb 2011 21:26:26 +, GSO wrote:

> I'm sure this question is as old as time, but what is the best way to
> gain root privileges?  (Am using Python 2.6.5, pygtk2 v2.16, Gtk
> v2.18.9, on RHEL6.)

Gain root privileges for a script? Write a c wrapper to call the script, 
chown it (the wrapper) to root, and set it (the wrapper) suid.
-- 
http://mail.python.org/mailman/listinfo/python-list


Best way to gain root privileges

2011-02-16 Thread GSO
I'm sure this question is as old as time, but what is the best way to
gain root privileges?  (Am using Python 2.6.5, pygtk2 v2.16, Gtk
v2.18.9, on RHEL6.)

Ta,


G.

gmotion
PyGTK desktop GUI for Motion (software motion detector)
http://code.google.com/p/gmotion/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode shutil.copy() changes a file name during copy?

2011-02-16 Thread dave
ah! an interesting point! hmm yes when i'm running parallels, both the
source and destination are sortof "Network Drive"'s, they're actually
my native mac drives (file system reported as "PrlSF").  In this
situation all works well.  the reported getfilesystemencoding() is
'mbcs' which is a convenient lie according to the python docs: "On
Windows NT+, file names are Unicode natively, so no conversion is
performed. getfilesystemencoding() still returns 'mbcs', as this is
the encoding that applications should use when they explicitly want to
convert Unicode strings to byte strings that are equivalent when used
as file names".

when i run natively, the drive is NOT a network drive, it is my mac
drive supported by "MacDrive" and the file system is reported as
"HFSJ".  it seems the getfilesystemencoding() should take a drive
letter as a parameter cuz the encoding can be different per drive?
maybe?  but now i think perhaps the problem is with MacDrive?  but it
works flawlessly with all other software. hmmm.
-- 
http://mail.python.org/mailman/listinfo/python-list


interfacing python with emacs

2011-02-16 Thread Andrea Crotti
I decided that I finally want to get auto-completion and other really cool 
things for my python/emacs environment.

Rope also looks really great and being able to refactor easily would really be 
a dream :) (no more excuses from java developers then)
http://rope.sourceforge.net/

Pymacs is already there and working, but it gives me many concerns, so I would 
like to rewrite something simple and small.

Should not be too hard, I just need a server on the python side and a small 
protocol.
So here it comes the first question, how would you make them communicate?

In Pymacs was just read and write from stdin/out, but maybe there are better 
ways.
Probably a pipe would be more suited, and maybe even sockets would be nice.

But considering how it should be used maybe it doesn't help so much.

And what about the protocol? What could be a minimal useful set of operations 
to do?
In the beginning I'll just evaluate python code and get the output, but 
eventually I want to be able to call python functions as they were elisp 
functions, which is the main goal.

As soon as there is something working I'll publish it on github, so that anyone 
interested can help/criticize...
Thanks,
Andrea
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode shutil.copy() changes a file name during copy?

2011-02-16 Thread dave
ah! an interesting point! hmm yes when i'm running parallels, both the
source and destination are sortof "Network Drive"'s, they're actually
my native mac drives (file system reported as "PrlSF").  In this
situation all works well.  the reported getfilesystemencoding() is
'mbcs' which is a convenient lie according to the python docs: "On
Windows NT+, file names are Unicode natively, so no conversion is
performed. getfilesystemencoding() still returns 'mbcs', as this is
the encoding that applications should use when they explicitly want to
convert Unicode strings to byte strings that are equivalent when used
as file names".

when i run natively, the drive is NOT a network drive, it is my mac
drive supported by "MacDrive" and the file system is reported as
"HFSJ".  it seems the getfilesystemencoding() should take a drive
letter as a parameter cuz the encoding can be different per drive?
maybe?  but now i think perhaps the problem is with MacDrive?  but it
works flawlessly with all other software. hmmm.
-- 
http://mail.python.org/mailman/listinfo/python-list


Another MySQLdb Q

2011-02-16 Thread Victor Subervi
Hi;
I have this code:
  db = MySQLdb.connect(host, user, passwd, db)
  cursor= db.cursor()
  cursor.execute(sql, id)
  db.commit()

It throws no errors and gives every indication that it executes the command.
I've printed out the command and the id and executed it successfully in
mysql...but not in python from the script. Everything else works in MySQLdb.
What could be the problem?
TIA,
Beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Newbie needs some context

2011-02-16 Thread Grant Edwards
On 2011-02-16, Fred Marshall  wrote:

> I can already program in a few languages 

That should make things easy.

> (but not C++)

That should help even more

> and, since Python comes to highly recommended, I figured to venture
> into it.
>
> I'm used to using an IDE.
>
> So, after some web browsing and reading, I did the following:
>
> Installed Python
> Installed EasyEclipse
> Installed wxPython
> Installed wxGlade
>
> My objective is to develop some relatively simple GUI applications.

I wouldn't start with GUI development _unless_ you're already pretty
experienced with the particular GUI framework (e.g. wxWindows &
Glade).

> Since I'm still on a steep learning curve with all these things I'm
> clearly missing some of the structural context and wonder where would
> be really good places to read about:
>
> 1) Is it the intent to generate code with wxGlade and then rather 
>"import" that code into an Eclipse project context?  Or, should
>one expect to be able to create hooks (e.g. for Tools) in Eclipse
>that will do that?  If so, how?
>
> 2) I'm finding the Eclipse terminology re: projects, folders, etc.
>etc.  rather obscure.  Where can I learn about "good practice" and
>these things.  I know what cvs is but won't likely be using it.
>That is, which item in the hierarchy is best used for what?

If I were you I wouldn't try to learn Python, wxWindows, wxGlade, and
Eclipse all at the same time.  Both wxWindows and Eclipse are huge,
complex beasts.  Add a new language plus wxGlade on top of that, and
you're in for a long, hard, frustrating slog.

I'd pick a simple IDE or syntax aware editor and learn Python first
(without GUI stuff).

Once you're comfortable with Python and the IDE, then try to learn
wxWindows and wxGlade.

If you really want to jump into GUI stuff right away, tkinter is much
easier to get started with.

-- 
Grant Edwards   grant.b.edwardsYow! I like the way ONLY
  at   their mouths move ...  They
  gmail.comlook like DYING OYSTERS
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: logging module -- better timestamp accuracy on Windows

2011-02-16 Thread Brian Curtin
On Wed, Feb 16, 2011 at 12:23, benhoyt  wrote:

>
> > AFAIK, the Windows performance counter has long-term accuracy issues,
> > so neither is perfect. Preferably we should have a timer with the long-
> > term accuracy of time.time and the short-term accuracy of time.clock.
>
> Thanks for the tip -- yes, I hadn't thought about that, but you're right,
> QueryPerformanceCounter (and hence time.clock) veers away from the system
> time, and it's non-trivial to fix. See also:
>
> http://msdn.microsoft.com/en-us/magazine/cc163996.aspx


This is what http://pypi.python.org/pypi/timer uses, although it doesn't go
as far as using the final result of the article, but an implementation from
Figure 2, which was "Good Enough" (TM).
-- 
http://mail.python.org/mailman/listinfo/python-list


Editor/IDE with Python coverage support?

2011-02-16 Thread Matt Chaput
Are there any editors/IDEs with good support for line-coloring from Python test 
coverage results? (I normally use Eclipse + PyDev but PyDev's current coverage 
support isn't much better than nothing.)

Thanks,

Matt

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


Re: How to handle sockets - easily?

2011-02-16 Thread Bubba
William Ahern's log on stardate 16 vlj 2011

/snip

> I think that there's an asynchronous all-Python MySQL library, but
> I'm not sure. Maybe one day I can open source my asynchronous MySQL C
> library. (I always recommend people to use PostgreSQL, though; which
> is superior in almost every way, especially the C client library and
> the wire protocol.) 

I have no particular problem with using PgSQL.

Which of these would you recommend? http://wiki.postgresql.org/wiki/Python

-- 
"If you lie to the compiler,
it will get its revenge."
Henry Spencer
2.718281828459045235360287471352662497757247093699959574966967627.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Python Newbie needs some context

2011-02-16 Thread Fred Marshall
I can already program in a few languages (but not C++) and, since Python 
comes to highly recommended, I figured to venture into it.


I'm used to using an IDE.

So, after some web browsing and reading, I did the following:

Installed Python
Installed EasyEclipse
Installed wxPython
Installed wxGlade

My objective is to develop some relatively simple GUI applications.

Since I'm still on a steep learning curve with all these things I'm 
clearly missing some of the structural context and wonder where would be 
really good places to read about:


1) Is it the intent to generate code with wxGlade and then rather 
"import" that code into an Eclipse project context?  Or, should one 
expect to be able to create hooks (e.g. for Tools) in Eclipse that will 
do that?  If so, how?


2) I'm finding the Eclipse terminology re: projects, folders, etc. etc. 
rather obscure.  Where can I learn about "good practice" and these 
things.  I know what cvs is but won't likely be using it.  That is, 
which item in the hierarchy is best used for what?


Thanks,

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


Re: unicode shutil.copy() changes a file name during copy?

2011-02-16 Thread Jerry Hill
On Wed, Feb 16, 2011 at 1:28 PM, dave  wrote:
> figure it out.  it's very easy to test it yourself, just make a file
> with the section symbol in it, then try to copy it with python 2.7.1

This works fine for me on Windows XP and python 2.6.4 on an NTFS
formatted drive.  Are either your source or destination network
drives?  What does sys.getfilesystemencoding() say the encoding of
your filesystem is?

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


Re: How to use Python well?

2011-02-16 Thread Kurt Smith
On Wed, Feb 16, 2011 at 12:35 PM, snorble  wrote:
> I use Python a lot, but not well. I usually start by writing a small
> script, no classes or modules. Then I add more content to the loops,
> and repeat. It's a bit of a trial and error learning phase, making
> sure I'm using the third party modules correctly, and so on. I end up
> with a working script, but by the end it looks messy, unorganized, and
> feels hacked together. I feel like in order to reuse it or expand it
> in the future, I need to take what I learned and rewrite it from
> scratch.
>
> If I peeked over a Python expert's shoulder while they developed
> something new, how would their habits differ? Do they start with
> classes from the start?
>
> I guess I'm looking for something similar to "Large Scale C++ Software
> Design" for Python. Or even just a walkthrough of someone competent
> writing something from scratch. I'm not necessarily looking for a
> finished product that is well written. I'm more interested in, "I have
> an idea for a script/program, and here is how I get from point A to
> point B."
>
> Or maybe I'm looking for is best practices for how to organize the
> structure of a Python program. I love Python and I just want to be
> able to use it well.

Try this:

http://www.refactoring.com/

Not a silver bullet, but a good place to start.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to handle sockets - easily?

2011-02-16 Thread William Ahern
Bubba  wrote:

> import asyncore
> import socket
> import string
> import MySQLdb
> import sys

> def __init__(self, host, port):
> asyncore.dispatcher.__init__(self)
> self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
> self.set_reuse_addr()
> self.bind((host, port))
> self.listen(5)
>
> def handle_accept(self):
> pair = self.accept()
> if pair is None:
> pass
> else:
> sock, addr = pair
> print 'Incoming connection from %s' % repr(addr)
> handler = Handler(sock)
>
> server = Server('', 2020)
> asyncore.loop()
>

> I do, however, have some more questions (thus crosspost to
> comp.lang.python) - how many connections can this server handle without
> a problem? I'm using Asyncore module, as it can be seen. 

> Is it necessary, due to the fact that it should serve more than
> thousand devices that send data every 10 seconds, to do threading (I
> believe that is already done with Asyncore for sockets, but what about
> SQL?) 

The MySQL C library is not asynchronous. Each request does blocking I/O. If
the MySQLdb module is a wrapper for the MySQL C library--and it seems it
is--then you will want to use threads (not coroutines or generators). For
all I know your accept handler is threaded already. MySQL itself almost
certainly can't handle tens of thousands of simultaneous requests. The
backend connection and query handler is simply-threaded as well, which means
for every connection your talking 2 * "tens of thousands" threads. I didn't
read over your code much, but the only way to get around this would be to
handle your socket I/O asynchronously; but I don't enough about Python to
get the mixed behavior you'd want.

I think that there's an asynchronous all-Python MySQL library, but I'm not
sure. Maybe one day I can open source my asynchronous MySQL C library. (I
always recommend people to use PostgreSQL, though; which is superior in
almost every way, especially the C client library and the wire protocol.)
-- 
http://mail.python.org/mailman/listinfo/python-list


How to use Python well?

2011-02-16 Thread snorble
I use Python a lot, but not well. I usually start by writing a small
script, no classes or modules. Then I add more content to the loops,
and repeat. It's a bit of a trial and error learning phase, making
sure I'm using the third party modules correctly, and so on. I end up
with a working script, but by the end it looks messy, unorganized, and
feels hacked together. I feel like in order to reuse it or expand it
in the future, I need to take what I learned and rewrite it from
scratch.

If I peeked over a Python expert's shoulder while they developed
something new, how would their habits differ? Do they start with
classes from the start?

I guess I'm looking for something similar to "Large Scale C++ Software
Design" for Python. Or even just a walkthrough of someone competent
writing something from scratch. I'm not necessarily looking for a
finished product that is well written. I'm more interested in, "I have
an idea for a script/program, and here is how I get from point A to
point B."

Or maybe I'm looking for is best practices for how to organize the
structure of a Python program. I love Python and I just want to be
able to use it well.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: logging module -- better timestamp accuracy on Windows

2011-02-16 Thread benhoyt

> For example, are you assuming that your clock() call in logging is
> the very first call made?

Yes, we were making that assumption (the time.clock() call in the import of our 
log module), which was true in our code, but I can see where it's not a good 
thing to assume generally.

> Also, IIUC the resolution of clock() is < 1 usec, but
> as logging only prints to the nearest msec, won't you lose much of the
> benefit of the increased resolution? ...
> Or are you saying that the times should be formatted/printed to
> microsecond accuracy?

No, millisecond is fine. The 0.56ms example I gave in response to Rick was a 
really bad example -- It's more like when it's 5.6 ms that it's a problem for 
us, because the request time is saying 5.6ms, but the log timestamps within and 
at the end of that request are identical.

Anyway, as sturlamolden mentioned, time.clock() has long-term accuracy issues 
(gets out of sync with time.time()), so that's not really a good solution.

So the way it is is non-ideal, but that's more a fact of life due to the 
Windows time functions than anything else. It's not trivial to solve, so we're 
going to leave it for now, and just use time.clock() to time individual pieces 
of code when we need more accuracy...

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


Re: unicode shutil.copy() changes a file name during copy?

2011-02-16 Thread dave
i don't see an "active python 3.2" i see "3.1" and "Many 3rd-party
modules and extensions that you may depend upon may not yet be
available for Python 3. As a result you may want to continue to use
Python 2 for the time being".  and i depend on some 3rd party modules.

no i really think it's something like:

string = string.decode('latin_1').endocde('utf8') or something.  i
just don't know what's expected.  i've tried various flavors but can't
figure it out.  it's very easy to test it yourself, just make a file
with the section symbol in it, then try to copy it with python 2.7.1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: logging module -- better timestamp accuracy on Windows

2011-02-16 Thread benhoyt

> AFAIK, the Windows performance counter has long-term accuracy issues,
> so neither is perfect. Preferably we should have a timer with the long-
> term accuracy of time.time and the short-term accuracy of time.clock.

Thanks for the tip -- yes, I hadn't thought about that, but you're right, 
QueryPerformanceCounter (and hence time.clock) veers away from the system time, 
and it's non-trivial to fix. See also:

http://msdn.microsoft.com/en-us/magazine/cc163996.aspx

http://social.msdn.microsoft.com/forums/en-US/windowsgeneraldevelopmentissues/thread/a8b2c286-1133-4827-97be-61e27687ff5d

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


Re: How to handle sockets - easily?

2011-02-16 Thread Bubba
Richard Kettlewell's log on stardate 10 vlj 2011

> Rewrites can help but they can also go badly wrong.  Rewriting into a
> language you don't yet know seems especially likely to fall into the
> latter category!

Indeed.

However, it seems to be quite "doable", as it took me from 0 knowledge
of Python to come up with a solution that does only part of the thing
(it's missing CRC check for GPS data and NMEA checksum, user control
for changing device setup on the fly via web and few more details). 

Here's the code (ATTN: may wrap):

#! /usr/bin/env python

import asyncore
import socket
import string
import MySQLdb
import sys

class Handler(asyncore.dispatcher_with_send):

def handle_read(self):
data = self.recv(128) #payload size

data = string.lstrip(data,"\n") #sometimes a 0xOA stays
data_len = len (data) #get payload length
trackerID_hex = data[4:11]  #tracker ID
trackerID = ""  #create empty string
for i in trackerID_hex:
  trackerID += str('%02X'%ord(i)) #convert hex integer to string and 
append to trackerID
trackerID = string.rstrip(trackerID,"F")  #remove stuffed F's from 
tracker ID
checksum = ""
checksum_hex = data[data_len-4:data_len-1]  #second part of command 
payload, checksum
for i in checksum_hex:
  checksum += str('%02X'%ord(i))  #convert hex integer to string and 
append to trackerID
GPSdata = data[13:data_len-4] #GPRMC - 
hhmmss.dd,S,xxmm.,,yyymm.,,s.s,h.h,ddmmyy
l = list()
l = string.split(GPSdata, ",")
n = len(l)
if n > 1: #hack! devices can mess the output, so just discard the data 
and go to another sample
  GPStime = l[0]
  GPStime = GPStime[0:2] + ":" + GPStime[2:4] + ":" + GPStime[4:6]
  GPSstatus = l[1]
  GPSlatitude = l[2]
  GPSlatitude = float(GPSlatitude[0:2]) + float(GPSlatitude[2:10]) / 60
  GPSNS = l[3]
  GPSlongitude = l[4]
  GPSlongitude = float(GPSlongitude[0:3]) + float(GPSlongitude[3:11]) / 
60
  GPSEW = l[5]
  try:
GPSspeed = float(l[6])
  except ValueError:
GPSspeed = 0
  GPSspeed *=  1.852
  try:
GPSheading = float(l[7])
  except ValueError:
GPSheading = 0
  GPSdate = l[8]
  GPSdate = "20" + GPSdate[4:6] + "-" + GPSdate[2:4] + "-" + 
GPSdate[0:2]
  try:
conn = MySQLdb.connect (host = "localhost", user = "", passwd = "", 
db = "") #:p
  except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
  cursor = conn.cursor()
  query = "INSERT INTO data (trackerID, GPStime, GPSstatus, 
GPSlatitude, GPSNS, GPSlongitude, GPSEW, GPSspeed, GPSheading, GPSdate) VALUES 
('" + trackerID + "', '" + GPStime + "', '" + GPSstatus + "', '" + 
str(GPSlatitude) + "', '" + GPSNS + "', '" + str(GPSlongitude) + "', '" + GPSEW 
+ "', '" + str(GPSspeed) + "', '" + str(GPSheading) + "', '" + GPSdate + "')"
  cursor.execute (query)
  cursor.close ()
  conn.commit()
  conn.close ()

class Server(asyncore.dispatcher):

def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind((host, port))
self.listen(5)

def handle_accept(self):
pair = self.accept()
if pair is None:
pass
else:
sock, addr = pair
print 'Incoming connection from %s' % repr(addr)
handler = Handler(sock)

server = Server('', 2020)
asyncore.loop()

C code did not parse data, but rather put the whole payload to SQL
database, which would be parsed afterwards. This code takes almost
twice LOC less that C code. 

I do, however, have some more questions (thus crosspost to
comp.lang.python) - how many connections can this server handle without
a problem? I'm using Asyncore module, as it can be seen. 

Is it necessary, due to the fact that it should serve more than
thousand devices that send data every 10 seconds, to do threading (I
believe that is already done with Asyncore for sockets, but what about
SQL?) 

Any other general code suggestions (since this is the first time I
write anything in Python)? 

TIA!

-- 
"If you lie to the compiler,
it will get its revenge."
Henry Spencer
2.718281828459045235360287471352662497757247093699959574966967627.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode shutil.copy() changes a file name during copy?

2011-02-16 Thread Terry Reedy

On 2/15/2011 11:50 PM, dave wrote:

i'm on windows, using active python 2.7.1

i've written a script to copy a folder of files to dest folder..

one if the files in this folder has the section symbol (§, '\x15') as
part of the file name
shutil.copy(src_file, dst_file) "can't find the file specified" when
it does the os.chmod() part, can't find dest file,
cuz the file got copied with "_" in place of the section symbol.
gar.


What happens if you try this with 3.2?

--
Terry Jan Reedy


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


OT: Need Interactivity With the Browser

2011-02-16 Thread Victor Subervi
Hi;
I would like to build a component where the user can go to a page, say he'd
like to upload so many ("x") number of photos, click a button and without
leaving the page have three sets of upload widgets pop up. I think this is
done with Json; however, I can't find much info or tutorials on the same
that help me know if Json is my tool. (After spending a half hour studying
it online I'm still at a loss as to how it helps or what id does, just that
it's somewhat similar to XML) And I cannot find a discussion list on the
subject, either. Please advise.
TIA,
Beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: logging module -- better timestamp accuracy on Windows

2011-02-16 Thread Brian Curtin
On Wed, Feb 16, 2011 at 09:34, sturlamolden  wrote:

> On 16 Feb, 15:30, benhoyt  wrote:
>
> > It seems to me that the logging module should use a millisecond-accurate
> timestamp (time.clock) on Windows, just like the "timeit" module does.
>
> AFAIK, the Windows performance counter has long-term accuracy issues,
> so neither is perfect. Preferably we should have a timer with the long-
> term accuracy of time.time and the short-term accuracy of time.clock.
>
> Sturla


I just uploaded a timer extension I wrote a while back which tries to solve
some of this: http://pypi.python.org/pypi/timer (supports Python 2 and 3)

It takes an idea from MSDN Magazine about creating reliable and accurate
high resolution timers and converts it into somewhat of a threading.Timer
replacement. It accepts a duration in microseconds and can be stopped,
giving you the current elapsed microseconds. I should add an infinite
argument so it can be used more like a stopwatch, rather than requiring a
duration.

It appears to be fairly accurate so far, but I wouldn't count lives on it
just yet.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this a bug of str.join?

2011-02-16 Thread Terry Reedy

On 2/16/2011 1:32 AM, fireinice wrote:

On Feb 16, 1:24 am, fireinice  wrote:

Hi, all
I'm just working around to generate some fake file for parsing. and I
notice some weired thing happen.
 time = str(random.randint(1000, ))
 s_id = str(random.randint(1000, ))
 p_id = str(random.randint(1000, ))
 a_id = str(random.randint(1000, ))
 s = "test"
 a = [time, s_id, p_id, a_id, s]
 print '\t'.join(a)

the output is:
31079035823210326101282916386924719897196119318 
1780339444980186test

you can notice that there is no tab between a_id and s
if I switch a_id and p_id, it still happen, but if I delete one of
ids, the problem gone.
I tried this with python2.6 from debian source and python2.3 which I
compiled from source. the result are both the same.
What happened to str.join?
thanks


I'm sorry, I found it should be the terminal width caused visual
problem, please kindly ignore this post.


For future reference, the way to get more info about what is really in a 
string is to print its repr(), or even its list() version. Len() is also 
helpful. On Windows, both the Command Prompt window and IDLE refuse to 
expand tabs, so


>>> s='\t'.join(('a','b','c'))
>>> str(s)
'a\tb\tc'
>>> repr(s)
"'a\\tb\\tc'"
>>> len(s)
5
>>> list(s)
['a', '\t', 'b', '\t', 'c']

--
Terry Jan Reedy

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


Re: logging module -- better timestamp accuracy on Windows

2011-02-16 Thread sturlamolden
On 16 Feb, 15:30, benhoyt  wrote:

> It seems to me that the logging module should use a millisecond-accurate 
> timestamp (time.clock) on Windows, just like the "timeit" module does.

AFAIK, the Windows performance counter has long-term accuracy issues,
so neither is perfect. Preferably we should have a timer with the long-
term accuracy of time.time and the short-term accuracy of time.clock.

Sturla

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


Re: logging module -- better timestamp accuracy on Windows

2011-02-16 Thread Vinay Sajip
On Feb 16, 2:30 pm, benhoyt  wrote:

> It seems to me that the logging module should use a millisecond-accurate 
> timestamp (time.clock) on Windows,
> just like the "timeit" module does.

It's not an unreasonable request, though I don't think logging should
be used to time things accurately. I'm also not sure about the exact
form the solution might take. For example, are you assuming that your
clock() call in logging is the very first call made? If it's not, then
wouldn't this throw your calculations off? Or have I misunderstood how
clock() works? Also, IIUC the resolution of clock() is < 1 usec, but
as logging only prints to the nearest msec, won't you lose much of the
benefit of the increased resolution? In your above example your
request processing took 0.56 msec, so at best you would see a 1 msec
difference between start and finish times in the log - that doesn't
seem like it would be good enough; plus, if the process took less than
0.5 msec, then you might see identical start and finish times in the
log. Or are you saying that the times should be formatted/printed to
microsecond accuracy?

Of course 3.2 is at rc3, and branches for 2.x are closed except for
security fixes, so I'm not sure when this could go in to an official
release ...

Thanks & regards,

Vinay Sajip

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


Re: logging module -- better timestamp accuracy on Windows

2011-02-16 Thread benhoyt

> A simpler solution would be to caclulate the time it takes to the handle
> the request using time.clock() and include it in the log message.
> Something like:

Thanks, Ross. Actually, we are doing exactly that already -- it's how we 
noticed the timestamp issue in the first place. However, that doesn't fix it 
when we have multiple logged events that we want to calculate time deltas 
between, such as:

2011-02-15T10:11:12.123 Starting request
2011-02-15T10:11:12.123 Doing stuff
2011-02-15T10:11:12.123 Filtering stuff
2011-02-15T10:11:12.123 Rendering template
2011-02-15T10:11:12.123 Request complete, took 0.56 ms

It seems to me that the logging module should use a millisecond-accurate 
timestamp (time.clock) on Windows, just like the "timeit" module does.

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


Re: Another related OO Python ?

2011-02-16 Thread Eric Brunel
In article 
<6849fd3f-5116-4b35-b274-dc76ae39f...@a11g2000pro.googlegroups.com>,
 RJB  wrote:

> On Feb 16, 12:48 am, Eric Brunel 
> wrote:
> > In article ,
> >  Doug Epling  wrote:
> >
> > > hey, does anyone find the UML useful during Python development of larger
> > > projects?
> >
> > Well, UML being very Java/C++ oriented, I found out that Python idioms
> > were really difficult to represent in the diagrams. So I'm using it to a
> > very small extent and for documentation only, just to give an idea about
> > how classes are organized. For the rest, and IMHO, it's really too
> > impractical to be of any use.
> 
> Which of the 13 diagrams have tried and rejected?-)

Diagrams that aren't too bound to the language like e.g the deployment 
diagram can still be used, of course. I was mainly talking about the 
class diagram, which is still the central point of a model. But I even 
found sequence diagrams quite hard to write for Python, unless they are 
very simplistic ones.
-- 
http://mail.python.org/mailman/listinfo/python-list


70% [* SPAM *] Re: shelve trying to import bsddb

2011-02-16 Thread DPalao
El Wednesday February 16 2011, DPalao escribió:
> Dear all,
> I'm trying to use shelve to store some data, but sheve itself tries to
> import
> 
> bsddb, which results in:
> >   File "/usr/lib64/python2.6/shelve.py", line 239, in open
> >   
> > return DbfilenameShelf(filename, flag, protocol, writeback)
> >   
> >   File "/usr/lib64/python2.6/shelve.py", line 223, in __init__
> >   
> > Shelf.__init__(self, anydbm.open(filename, flag), protocol,
> > writeback)
> >   
> >   File "/usr/lib64/python2.6/anydbm.py", line 82, in open
> >   
> > mod = __import__(result)
> >   
> >   File "/usr/lib64/python2.6/dbhash.py", line 8, in 
> >   
> > import bsddb
> > 
> > ImportError: No module named bsddb
> 
> I know that the new bsddb3 should be used, so what can I do?
> TIA,
> 
> D

I solved the issue. I just compiled python (btw, I'm using 2.6.6) with support 
for "berkdb", which apparently disappeared in a recent update (I'm using 
gentoo).

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


Re: Map vs. List Comprehensions (was "lint warnings")

2011-02-16 Thread Steven D'Aprano
On Tue, 15 Feb 2011 18:55:50 -0500, Gerald Britton wrote:

> So, what's the feeling out there?  Go with map and the operators or
> stick with the list comps?

Stick to whatever feels and reads better at the time.

Unless you have profiled your code, and determined that the map or list 
comp was the bottleneck, who cares if you save half a nanosecond per 
iteration?

map clearly loses badly in the old-time idiom:

map(lambda x: x+1, seq)

or similar. That's the only time I'd just flat out say, avoid map in 
favour of a list comprehension. Otherwise, the performance difference is 
likely to be trivial, as is the difference in length of code. Use 
whichever you like.

I personally appreciate the ability to treat map as a primitive ("map 
this function over this data") rather than caring about the mechanics of 
iteration ("iterate over data, applying this function to each element in 
turn"), so I often use map. But I use list comps even more often.



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


Archiving Modules

2011-02-16 Thread peter
I am writing a small Tkinter utility to control archive files in
multiple formats (mainly for my own amusement and education).
Basically it presents the user with two adjacent listboxes, one with
the contents of the target directory and one with the contents of the
archive. By clicking buttons labelled '<' and '>' the user can copy
files to and from the archive.  The actual archiving functionality
derives from the modules zipfile and tarfile.

It all seems to be working fine, but I have two residual queries.
Firstly for the sake of completeness I would like to include .rar
archives, but there doesn't seem to be an equivalent rarfile module.
I use both Windows and Linux on various machines, so need a cross
platform solution which does not depend on external modules. The only
one I have found is at http://pypi.python.org/pypi/rarfile/1.0, but it
seems this does rely on an external module.  Is there anything out
there?

Secondly, I found that when extracting zip files, the date stamps were
all reset to the date/time of extraction, whereas for tar files they
retained their original values. This second behaviour seems more
logical, and I have simulated it for the zipfiles by using
zipfile.getinfo to read the original date then os.utime to rewrite
it.  It seems rather messy - have I missed something simple like a
flag setting within zipfile?

Peter


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


Re: Another related OO Python ?

2011-02-16 Thread RJB
On Feb 16, 12:48 am, Eric Brunel 
wrote:
> In article ,
>  Doug Epling  wrote:
>
> > hey, does anyone find the UML useful during Python development of larger
> > projects?
>
> Well, UML being very Java/C++ oriented, I found out that Python idioms
> were really difficult to represent in the diagrams. So I'm using it to a
> very small extent and for documentation only, just to give an idea about
> how classes are organized. For the rest, and IMHO, it's really too
> impractical to be of any use.

Which of the 13 diagrams have tried and rejected?-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: interleave string

2011-02-16 Thread Steven D'Aprano
On Tue, 15 Feb 2011 19:39:30 -0800, alex23 wrote:

> Andrea Crotti  wrote:
>> At the moment I have this ugly inliner
>>         interleaved = ':'.join(orig[x:x+2] for x in range(0,
>>         len(orig), 2))
> 
> I actually prefer this over every other solution to date.

Agreed. To me, it's the simplest, least contrived way to solve the 
problem. 


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


70% [* SPAM *] shelve trying to import bsddb

2011-02-16 Thread DPalao
Dear all,
I'm trying to use shelve to store some data, but sheve itself tries to import 
bsddb, which results in:

>   File "/usr/lib64/python2.6/shelve.py", line 239, in open
> return DbfilenameShelf(filename, flag, protocol, writeback)
>   File "/usr/lib64/python2.6/shelve.py", line 223, in __init__
> Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
>   File "/usr/lib64/python2.6/anydbm.py", line 82, in open
> mod = __import__(result)
>   File "/usr/lib64/python2.6/dbhash.py", line 8, in 
> import bsddb
> ImportError: No module named bsddb

I know that the new bsddb3 should be used, so what can I do?
TIA,

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


Re: return an object of a different class

2011-02-16 Thread Steven D'Aprano
On Tue, 15 Feb 2011 22:17:13 -0700, spam wrote:

> I didn't explain my problem, chose a terrible example. This is more what
> I'm trying to do:
[snip "thingy" class]


No, your first example was better. This one is terrible -- it's so 
generic it's meaningless. 

In any case, you don't explain why it has to be a class, rather than a 
factory function.


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


Re: return an object of a different class

2011-02-16 Thread Steven D'Aprano
On Tue, 15 Feb 2011 19:23:39 -0700, spam wrote:

> How can I do something like this in python:
> 
> #!/usr/bin/python3.1
> 
> class MyNumbers:
>def __init__(self, n):
>  self.original_value = n
>  if n <= 100:
>self = SmallNumers(self)
>  else:
>self = BigNumbers(self)

(1) self is just a local variable, it isn't privileged in any way. 
Assigning to the name "self" doesn't magically change the instance, so 
that can't work.

(2) By the time the __init__ method is called, the instance has been 
created. __init__ is the initializer, you need the constructor. Something 
like this:


# Untested.
class MyNumbers:
def __new__(cls, n):
if n <= 100:
instance = SmallNumbers(n)
else:
instance = BigNumbers(n)
return instance



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


hot hot and hot indian actresses

2011-02-16 Thread Ashraf Ali
Hello friends how r u all? what about indian hot girls? just visit
www.hotpictures-glaxi.blogspot.com

www.onlinetv-glaxi.blogspot.com

www.onlineradio-glaxi.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Displaying SVG in tkinter using cairo and rsvg

2011-02-16 Thread Arndt Roger Schneider

Martin P. Hellwig schrieb:

On 02/16/11 09:04, Arndt Roger Schneider wrote:


[snip]

tkpath does not seem to come standard with Python's tk version when I 
looked into it a couple of years ago, but maybe it has now?



tk canvas and tkpath share the same interface, the first tkpath was
a plugin into the tk canvas. A tkinter wrapper class will
be a simple subclass of tk canvas introducing the new item types:
path, ppolygone, polyline, circle, elipsis, pimage, prect, ptext, group 
and for tkpath 0.3 three

additional messages for: style, gradient and distance, that's all
~50 lines of code.


Is there anyting else You want to know about svg?



No not really :-), I just wanted to display a SVG in tkinter with the 
minimal amount of external dependencies, since I have achieved that I 
thought I share my experience, so that the next time someone google 
tkinter and display svg it will return something that (well at least of 
the time of this writing) worked.




Well CAIRO is sort of a shifting target...
--currently I am stuck with PPC and new CAIRO versions cannot longer
being built on it anymore :-(--

CAIRO can be real pain on non-X11-linux platforms. ImageMagick
is simpler than CAIRO cross-platform wise.

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


Re: Displaying SVG in tkinter using cairo and rsvg

2011-02-16 Thread Martin P. Hellwig

On 02/16/11 09:04, Arndt Roger Schneider wrote:


raster images from SVG:
There are multiple methods to convert a scalable vector graphic
into a bitmap.
In addition to cairo, librsvg and rsvg imageMagick contains a
vector graphic format similar to svg--gradients and transparency
are problematic for this approach, but its a while since I had
looked into it...

My product Jeszra imports svg into Tk(using tkpath
http://jeszra.sourceforge.net/jeszra/Jeszra_TechnicalNotes.html#d0e10279
), preserving it as a vector graphics.
There are, of course, limitations to what can be preserved in Tk:


tkpath does not seem to come standard with Python's tk version when I 
looked into it a couple of years ago, but maybe it has now?




Is there anyting else You want to know about svg?



No not really :-), I just wanted to display a SVG in tkinter with the 
minimal amount of external dependencies, since I have achieved that I 
thought I share my experience, so that the next time someone google 
tkinter and display svg it will return something that (well at least of 
the time of this writing) worked.


Thanks for the info though.

--
mph




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


[ANN] New package: SpaceFuncs (2D, 3D, ND geometric modeling, optimization, solving)

2011-02-16 Thread dmitrey
Hi all,
I'm glad to inform you about new, 4th OpenOpt Suite module:

SpaceFuncs - a tool for 2D, 3D, N-dimensional geometric modeling with
possibilities of parametrized calculations, numerical optimization and
solving systems of geometrical equations.

For details see its home page
http://openopt.org/SpaceFuncs
and documentation
http://openopt.org/SpaceFuncsDoc

The module is written in Python + NumPy, requires FuncDesigner (and
OpenOpt, DerApproximator for some operations). It has completely free
license: BSD.

Also, you can try it online via our Sage-server
http://sage.openopt.org/welcome

Regards,
Dmitrey.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [newbie/2.5.1.1] Computing value of a word?

2011-02-16 Thread Chris Rebert
On Wed, Feb 16, 2011 at 1:17 AM, Gilles Ganault  wrote:
> Hello,
>
> For a game, I need to go through a wordlist, and for each word,
> compute its value, ie. a=1, b=2, etc.
>
> So for instance, NewYork = 14 + 5 + 23 + 25 + 15 + 18 + 11 = 111.
>
> Before I write the obvious While loop to go through each line in the
> input text file, I was wondering if Python didn't already have some
> function to perform this type of computation.

A = ord('a') - 1
for line in your_file:
word = line.strip().lower()
score = sum(ord(letter)-A for letter in word)

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


[newbie/2.5.1.1] Computing value of a word?

2011-02-16 Thread Gilles Ganault
Hello,

For a game, I need to go through a wordlist, and for each word,
compute its value, ie. a=1, b=2, etc.

So for instance, NewYork = 14 + 5 + 23 + 25 + 15 + 18 + 11 = 111.

Before I write the obvious While loop to go through each line in the
input text file, I was wondering if Python didn't already have some
function to perform this type of computation.

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


Re: return an object of a different class

2011-02-16 Thread Karim

On 02/16/2011 06:05 AM, Richard Thomas wrote:

On Feb 16, 2:23 am, s...@uce.gov wrote:

How can I do something like this in python:

#!/usr/bin/python3.1

class MyNumbers:
def __init__(self, n):
  self.original_value = n
  if n<= 100:
self = SmallNumers(self)
  else:
self = BigNumbers(self)

class SmallNumbers:
def __init__(self, n):
  self.size = 'small'

class BigNumbers:
def __init__(self, n):
  self.size = 'big'

t = MyNumbers(200)

When I do type(t) it says MyNumbers, while I'd want it to be BigNumbers,
because BigNumbers and SmallNumbers will have different methods etc...

Do I need to use metaclasses?

Thanks.
--
Yves.  http://www.SollerS.ca/
http://blog.zioup.org/

If you don't want to use a factory function I believe you can do this:

class MyNumber(object):
 def __new__(cls, n):
 if n<= 100:
 cls = SmallNumbers
 else:
 cls = BigNumbers
 return object.__new__(cls, n)
 ...

Chard.


Very beautiful code great alternative to factory method!
To memorize this pythonic way.

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


Re: Displaying SVG in tkinter using cairo and rsvg

2011-02-16 Thread Arndt Roger Schneider

Martin P. Hellwig schrieb:

Hi all,

Information on using tkinter for displaying an svg image seems a bit low 
spread on the Internet. I recently played around with pygame and svg and 
realized, hold on this can be done with tk too. So I thought I post a 
little example for future generations :-) (and also have stored at 
http://dcuktec.googlecode.com/hg/source/examples/cairo_rsvg_tkinter.py).


So here it is if you are interested:

[snip]

raster images from SVG:
There are multiple methods to convert a scalable vector graphic
into a bitmap.
In addition to cairo, librsvg and rsvg imageMagick contains a
vector graphic format similar to svg--gradients and transparency
are problematic for this approach, but its a while since I had
looked into it...

My product Jeszra imports svg into Tk(using tkpath
http://jeszra.sourceforge.net/jeszra/Jeszra_TechnicalNotes.html#d0e10279
), preserving it as a vector graphics.
There are, of course, limitations to what can be preserved in Tk:

http://jeszra.sourceforge.net/jeszra/SVG_Import.html

The other way is much simpler to convert a Tk graphics into
svg, which is also implemented in Jeszra.
All svg graphics on http://jeszra.sourceforge.net and 
http://gestaltitems.sourceforge.net are generated by Jeszra from

Tk (there are some hundred graphics)...

The generator API is open and a draft documentation is online at:
http://jeszra.sourceforge.net/api/ch01s04.html
http://jeszra.sourceforge.net/api/index.html

Jeszra API Concerning svg:
http://jeszra.sourceforge.net/api/ch04.html
http://jeszra.sourceforge.net/api/ch05.html
http://jeszra.sourceforge.net/api/ch06.html
http://jeszra.sourceforge.net/api/ch07.html
http://jeszra.sourceforge.net/api/ch08.html

Here is an overview about Jeszra, SVG and Tk:
http://jeszra.sourceforge.net/api/pictures/overview.svg


The svg on those page gets on-demand converted into flash,
for the internet explorer.

Is there anyting else You want to know about svg?

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


Re: How does IDLE do it?

2011-02-16 Thread Eric Brunel
In article ,
 "Richard D. Moores"  wrote:

> I recently wrote some code that prints information about the 'jukugo'
> used in Japanese newspaper articles. A jukugo is a Japanese word
> written with at least 2 kanji. An example of a 2-kanji jukugo is 危機
> (kiki -- crisis). I found that I could not use my usual IDE to render
> the Japanese correctly in either the code or the output. But IDLE
> (version 3.1.2; Windows Vista) does a beautiful job! See screen shots
> 
> and
> .
> (The whole script plus output is at
> .)
> 
> I'd like to know how the IDLE developers did this. How can IDLE not
> have a problem with Japanese using Courier New, Calibri, even Fences
> or Windings! (For Wingdings, see
> .)

IDLE doesn't do anything, tk does. When a character is not available in a given 
font,
tk looks up for one containing it by itself and uses it automatically.

> Thanks,
> 
> Dick Moores
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another related OO Python ?

2011-02-16 Thread Eric Brunel
In article ,
 Doug Epling  wrote:

> hey, does anyone find the UML useful during Python development of larger 
> projects?

Well, UML being very Java/C++ oriented, I found out that Python idioms 
were really difficult to represent in the diagrams. So I'm using it to a 
very small extent and for documentation only, just to give an idea about 
how classes are organized. For the rest, and IMHO, it's really too 
impractical to be of any use.
-- 
http://mail.python.org/mailman/listinfo/python-list