Re: subprocess.Popen does not close pipe in an error case

2010-01-10 Thread Nobody
On Wed, 06 Jan 2010 19:05:40 -0800, Steven K. Wong wrote:

> Well, the example code at
> http://www.python.org/ ... /subprocess.html#replacing-shell-pipeline
> has the same issue:

> Perhaps the doc can be improved to remind folks to close p1.stdout if
> the calling process doesn't need it, unless wait() is changed to close
> it and p1.wait() is called.
> 
> Am I making any sense here?

The docs should include the p1.stdout.close().

It isn't needed in the typical case, where p2 runs until EOF on stdin, but
(as you have noticed) it matters if p2 terminates prematurely.

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


Re: Ask how to use HTMLParser

2010-01-10 Thread Nobody
On Fri, 08 Jan 2010 11:44:48 +0800, Water Lin wrote:

> I am a new guy to use Python, but I want to parse a html page now. I
> tried to use HTMLParse. Here is my sample code:
> --
> from HTMLParser import HTMLParser

Note that HTMLParser only tokenises HTML; it doesn't actually *parse* it.
You just get a stream of tag, text, entity, text, tag, ..., not a parse
tree.

In particular, if an element has its start and/or end tags omitted, you
won't get any notification about the start and/or end of the element;
you have to figure that out yourself from the fact that you're getting a
tag which wouldn't be allowed outside or inside the element.

E.g. if the document has omitted  tags, if you get a  tag when
you are (or *thought* that you were) already within a paragraph, you can
infer the omitted  tag.

If you want an actual parser, look at BeautifulSoup. This also does
a good job of handling invalid HTML (which seems to be far more
common than genuine HTML).

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


Re: lightweight encryption of text file

2010-01-10 Thread Nobody
On Fri, 08 Jan 2010 20:14:51 +0100, Daniel Fetchinson wrote:

> I have a plain text file which I would like to protect in a very
> simple minded, yet for my purposes sufficient, way. I'd like to
> encrypt/convert it into a binary file in such a way that possession of
> a password allows anyone to convert it back into the original text
> file while not possessing the password one would only see the
> following with the standard linux utility 'file':

> What would be the simplest way to achieve this using preferably stock
> python without 3rd party modules? If a not too complex 3rd party
> module made it really simple that would be acceptable too.

RC4 (aka ArcFour) is quite trivial to implement, and better than inventing
your own cipher or using a Vignere:

import itertools

class arcfour:
def __init__(self, key):
self.s = range(256)
self.schedule(map(ord, key))
self.pad = self.prng()

def swap(self, i, j):
self.s[i], self.s[j] = self.s[j], self.s[i]

def schedule(self, key):
j = 0
for i, c in zip(xrange(256), itertools.cycle(key)):
j = (j + self.s[i] + c) % 256
self.swap(i, j)

def prng(self):
i = j = 0
while True:
i = (i + 1) % 256
j = (j + self.s[i]) % 256
self.swap(i, j)
yield self.s[(self.s[i] + self.s[j]) % 256]

def crypt(self, string):
chars = (chr(c ^ r) for c, r in zip(map(ord, string), self.pad))
return ''.join(chars)

I suggest that you don't use the password itself as the key, unless you're
sure that a low-entropy string won't be used. Instead, create an SHA hash
(see the sha and hashlib modules) of the password and use that.

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


Re: lightweight encryption of text file

2010-01-10 Thread Daniel Fetchinson
>> I have a plain text file which I would like to protect in a very
>> simple minded, yet for my purposes sufficient, way. I'd like to
>> encrypt/convert it into a binary file in such a way that possession of
>> a password allows anyone to convert it back into the original text
>> file while not possessing the password one would only see the
>> following with the standard linux utility 'file':
>
>> What would be the simplest way to achieve this using preferably stock
>> python without 3rd party modules? If a not too complex 3rd party
>> module made it really simple that would be acceptable too.
>
> RC4 (aka ArcFour) is quite trivial to implement, and better than inventing
> your own cipher or using a Vignere:
>
> import itertools
>
> class arcfour:
> def __init__(self, key):
>   self.s = range(256)
>   self.schedule(map(ord, key))
>   self.pad = self.prng()
>
> def swap(self, i, j):
>   self.s[i], self.s[j] = self.s[j], self.s[i]
>
> def schedule(self, key):
>   j = 0
>   for i, c in zip(xrange(256), itertools.cycle(key)):
>   j = (j + self.s[i] + c) % 256
>   self.swap(i, j)
>
> def prng(self):
>   i = j = 0
>   while True:
>   i = (i + 1) % 256
>   j = (j + self.s[i]) % 256
>   self.swap(i, j)
>   yield self.s[(self.s[i] + self.s[j]) % 256]
>
> def crypt(self, string):
>   chars = (chr(c ^ r) for c, r in zip(map(ord, string), self.pad))
>   return ''.join(chars)
>
> I suggest that you don't use the password itself as the key, unless you're
> sure that a low-entropy string won't be used. Instead, create an SHA hash
> (see the sha and hashlib modules) of the password and use that.

Thanks, this looks very simple too, but where is the decryption code?
Wikipedia seems to suggest that encryption and decryption are both the
same but running crypt on the output of crypt doesn't give back the
original string. So probably I'm misunderstanding something.

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
-- 
http://mail.python.org/mailman/listinfo/python-list


Computer Music Toolkit (CMT) ?

2010-01-10 Thread Peter Billam
Greetings. Is there a way to get at the Computer Music Toolkit (CMT)
  http://www.ladspa.org/cmt/
functionality from Python (Python3 in my case) ?

Googling is confusing because of www.cmt.com and cmt-graph and
openscientist.lal.in2p3.fr/v9/cmt.html and pyAMISecure_and_cmt
and so on...

Regards,  Peter

-- 
Peter Billam www.pjb.com.auwww.pjb.com.au/comp/contact.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Direct use of bytearray buffers with ctypes ?

2010-01-10 Thread Pakal

Oki, it seems I've found.
To directly use a bytearray buffer from ctypes, you must first create
a compatible ctypes type (I.E, a char array of same size), and only
then instanciate this new type with newtype.from_buffer
(bytearray_object).

The little danger is : you must NOT change the size of bytearray as
long as the ctypes array targets its inner buffer, else of cousre
memory reallocations occurs, and your ctypes array points to invalid
memory (awaiting a pretty segfault). But modifying chars or characters
ranges from the buffer, as well via the bytearray object than via the
ctypes array, is fine.

I don't think problems can occur if both sides are accessed
simultaneously, even though ctypes operations may release the GIL
while they execute... the only race condition is the access of a
memory segment, isn't it ? No crash shall occur with this...

IDLE 2.6.4
>>> import ctypes
>>> a = bytearray(3)
>>> mytype = ctypes.c_char * 3
>>> h = mytype.from_buffer(a)
>>> h
<__main__.c_char_Array_3 object at 0x02B06350>
>>> h[:] = "abc"
>>> a
bytearray(b'abc')
>>> h[0] = "k"
>>> a
bytearray(b'kbc')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Prepend to logging message

2010-01-10 Thread Joan Miller
On 10 ene, 03:27, Ishwor Gurung  wrote:
> Joan,
>
> 2010/1/10 Joan Miller :
>
>
>
> > How to prepend anything to a logging message? Is possible to do it
> > from the dictionary object (ExtraLog) or is there is that override
> > process() [1]?
>
> > --
> > class ExtraLog(object):
>
> >    def __getitem__(self, name):
> >        if name == 'foo':
> >            result = 'testing'
> >        return result
>
> >    def __iter__(self):
> >        keys = ['foo',]
> >        keys.extend(self.__dict__.keys())
> >        return iter(keys)
>
> > logger = logging.LoggerAdapter(logging.getLogger('foo'), ExtraLog())
> > --
>
> Yep. Just subclass LoggerAdapter and override process(..)
> Read 
> this:http://docs.python.org/library/logging.html#adding-contextual-informa...
> --
> Regards
> Ishwor Gurung
> Key id:0xa98db35e
> Key fingerprint:FBEF 0D69 6DE1 C72B A5A8  35FE 5A9B F3BB 4E5E 17B5

Any example to override process() ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Computer Music Toolkit (CMT) ?

2010-01-10 Thread Stefan Behnel

Peter Billam, 10.01.2010 10:15:

Greetings. Is there a way to get at the Computer Music Toolkit (CMT)
  http://www.ladspa.org/cmt/
functionality from Python (Python3 in my case) ?

Googling is confusing because of www.cmt.com and cmt-graph and
openscientist.lal.in2p3.fr/v9/cmt.html and pyAMISecure_and_cmt
and so on...


Just a note on the last paragraph: searching for acronyms is usually 
simplified by adding the spelled-out name to the search keywords. It's 
highly unlikely that someone would write a Python tool for a library or 
application without putting the complete name of the thing it's made for on 
the project homepage.


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


Re: Porblem with xlutils/xlrd/xlwt

2010-01-10 Thread pp
On Jan 9, 8:23 am, John Machin  wrote:
> On Jan 9, 9:56 pm, pp  wrote:
>
> > On Jan 9, 3:52 am, Jon Clements  wrote:
>
> > > On Jan 9, 10:44 am, pp  wrote:
>
> > > > On Jan 9, 3:42 am, Jon Clements  wrote:
>
> > > > > On Jan 9, 10:24 am, pp  wrote:
> > > > yeah all my versions are latest fromhttp://www.python-excel.org.
> > > > just checked!!
>
> How did you check?
>
> > > > what could be the problem?
>
> > > Does rb = xlrd.open_workbook('somesheet.xls', on_demand=True) work by
> > > itself?
>
> > Yes it does. The problem is with line: wb =  copy(rb)
> > here I am getting the error: AttributeError: 'Book' object has no
> > attribute 'on_demand'
>
> Please replace the first 4 lines of your script by these 6 lines:
>
> import xlrd
> assert xlrd.__VERSION__ == "0.7.1"
> from xlwt import easyxf
> from xlutils.copy import copy
> rb = xlrd.open_workbook(
>     'source.xls',formatting_info=True, on_demand=False)
>
> and run it again. Please copy all the output and paste it into your
> response.

This time when I ran the code sent by you I got the following
results:I am using ipython for running the code.

AssertionErrorTraceback (most recent call
last)

/home/parul/CODES/copy_1.py in ()
  1
> 2 import xlrd
  3 assert xlrd.__VERSION__ == "0.7.1"
  4 from xlwt import easyxf
  5 from xlutils.copy import copy
  6 rb = xlrd.open_workbook('source.xls',formatting_info=True,
on_demand=False)

AssertionError:
WARNING: Failure executing file: 

I used www.python-excel.org to get xlrd and xlwt .. so they are latest
versions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Prepend to logging message

2010-01-10 Thread Peter Otten
Joan Miller wrote:

> How to prepend anything to a logging message? Is possible to do it
> from the dictionary object (ExtraLog) or is there is that override
> process() [1]?
> 
> --
> class ExtraLog(object):
> 
> def __getitem__(self, name):
> if name == 'foo':
> result = 'testing'
> return result
> 
> def __iter__(self):
> keys = ['foo',]
> keys.extend(self.__dict__.keys())
> return iter(keys)

format = "foo=%(foo)s " + logging.BASIC_FORMAT
logging.basicConfig(format=format)
logger = logging.LoggerAdapter(logging.getLogger('foo'), ExtraLog())
logger.error("yadda")

Is that what you want?

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


(non-python) upgrading a MySQL database

2010-01-10 Thread Dikkie Dik
>> ... And I am interested in cleaning this up. I should probably
>> start with the matter of databases, since that's something I won't be able
>> to easily change once clients actually start entering data. Please share
>> with me any further concepts or questions to get me thinking how to redesign
>> the databases.


I maintain my (MySQL) databases in an agile fashion. That means I start
small, and let my databases evolve along with my applications. So the
schema changes a lot during development. I take into account that the
databases already contain data and that it must be preserved and
migrated. I wrote a howto on my upgrading method and it can be found here:

http://www.howtoforge.org/node/4833

Hope this is of any help.

Good luck,
Dikkie
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Porblem with xlutils/xlrd/xlwt

2010-01-10 Thread John Machin
On Jan 10, 8:51 pm, pp  wrote:
> On Jan 9, 8:23 am, John Machin  wrote:
>
>
>
> > On Jan 9, 9:56 pm, pp  wrote:
>
> > > On Jan 9, 3:52 am, Jon Clements  wrote:
>
> > > > On Jan 9, 10:44 am, pp  wrote:
>
> > > > > On Jan 9, 3:42 am, Jon Clements  wrote:
>
> > > > > > On Jan 9, 10:24 am, pp  wrote:
> > > > > yeah all my versions are latest fromhttp://www.python-excel.org.
> > > > > just checked!!
>
> > How did you check?

You didn't answer this question.

>
> > > > > what could be the problem?
>
> > > > Does rb = xlrd.open_workbook('somesheet.xls', on_demand=True) work by
> > > > itself?
>
> > > Yes it does. The problem is with line: wb =  copy(rb)
> > > here I am getting the error: AttributeError: 'Book' object has no
> > > attribute 'on_demand'
>
> > Please replace the first 4 lines of your script by these 6 lines:
>
> > import xlrd
> > assert xlrd.__VERSION__ == "0.7.1"
> > from xlwt import easyxf
> > from xlutils.copy import copy
> > rb = xlrd.open_workbook(
> >     'source.xls',formatting_info=True, on_demand=False)
>
> > and run it again. Please copy all the output and paste it into your
> > response.
>
> This time when I ran the code sent by you I got the following
> results:I am using ipython for running the code.
>
> AssertionError                            Traceback (most recent call
> last)
>
> /home/parul/CODES/copy_1.py in ()
>       1
> > 2 import xlrd
>       3 assert xlrd.__VERSION__ == "0.7.1"
>       4 from xlwt import easyxf
>       5 from xlutils.copy import copy
>       6 rb = xlrd.open_workbook('source.xls',formatting_info=True,
> on_demand=False)
>
> AssertionError:
> WARNING: Failure executing file: 
>

Your traceback appears to show an AssertionError from an import
statement. We could do without an extra layer of noise in the channel;
please consider giving ipython the flick (for debug purposes, at
least) and use Python to run your script from the shell prompt.

Change the second line to read:

print xlrd.__VERSION__

> I used www.python-excel.org to get xlrd and xlwt .. so they are latest
> versions.

Let's concentrate on xlrd. I presume that means that you clicked
on the xlrd Download link which took you to http://pypi.python.org/pypi/xlrd
from which you can download the latest version of the package. That
page has "xlrd 0.7.1" in a relatively large font at the top. You would
have been presented with options to download one of these

xlrd-0.7.1.tar.gz
xlrd-0.7.1.win32.exe
xlrd-0.7.1.zip

(each uploaded on 2009-06-01).

Which one did you download, and then what did you do with it?

Or perhaps you ignored those and read further down to "Download link"
which took you to an out-of-date page but you didn't notice the
"0.6.1" in large bold type at the top nor the "Page last updated on 11
June 2007" at the bottom nor the "0.6.1" in the name of the file that
you downloaded ... sorry about that; I've smacked the webmaster about
the chops :-)

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


Re: Prepend to logging message

2010-01-10 Thread Joan Miller
On 10 ene, 10:26, Peter Otten <__pete...@web.de> wrote:
> Joan Miller wrote:
> > How to prepend anything to a logging message? Is possible to do it
> > from the dictionary object (ExtraLog) or is there is that override
> > process() [1]?
>
> > --
> > class ExtraLog(object):
>
> >     def __getitem__(self, name):
> >         if name == 'foo':
> >             result = 'testing'
> >         return result
>
> >     def __iter__(self):
> >         keys = ['foo',]
> >         keys.extend(self.__dict__.keys())
> >         return iter(keys)
>
> format = "foo=%(foo)s " + logging.BASIC_FORMAT
> logging.basicConfig(format=format)
> logger = logging.LoggerAdapter(logging.getLogger('foo'), ExtraLog())
> logger.error("yadda")
>
> Is that what you want?
>
> Peter

I want that a message can be modified before of being logged. i.e. for
"yadda" I would that were preppend 2 spaces. (And I want not manage
that in the format to manage the indentation of all text)
-- 
http://mail.python.org/mailman/listinfo/python-list


getopt not raising exception

2010-01-10 Thread Matthew Lear
Hello,

I'm having problems getting getopt to function correctly. Basically, no
exception is being raised if no argument is passed to the code snippet
below. I've read the Python documentation and tried example code from
various sources which should cause an exception, only they don't. I've
also tried executing the code on different machines too but to no avail.
I'm sure I'm obviously doing something wrong but can't determine what.
Any help would be much appreciated indeed.

import sys, getopt

try:
opts, args = getopt.getopt(sys.argv, "h:", ["help"])
except getopt.GetoptError:
print "error"
sys.exit(2)

If no args are passed when the script is run there is no exception
raised. Why? Surely the "h:" means that this option must be passed?

Thanks,
--  Matt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getopt not raising exception

2010-01-10 Thread Matt Nordhoff
Matthew Lear wrote:
> Hello,
> 
> I'm having problems getting getopt to function correctly. Basically, no
> exception is being raised if no argument is passed to the code snippet
> below. I've read the Python documentation and tried example code from
> various sources which should cause an exception, only they don't. I've
> also tried executing the code on different machines too but to no avail.
> I'm sure I'm obviously doing something wrong but can't determine what.
> Any help would be much appreciated indeed.
> 
> import sys, getopt
> 
> try:
> opts, args = getopt.getopt(sys.argv, "h:", ["help"])
> except getopt.GetoptError:
> print "error"
> sys.exit(2)
> 
> If no args are passed when the script is run there is no exception
> raised. Why? Surely the "h:" means that this option must be passed?
> 
> Thanks,
> --  Matt

That's not what ":" means. It means that, *if* the option is passed, it
must be followed by an additional argument, e.g.:

foo.py -h something

If you require that -h be passed, it's not much of an option! You should
use a regular argument and check len(args) or somesuch. Or, if you must
keep it an "option", do something equivalent for that.

BTW: Checked out optparse? It's bigger and sexier!
-- 
Matt Nordhoff
-- 
http://mail.python.org/mailman/listinfo/python-list


integer and string compare, is that correct?

2010-01-10 Thread Hellmut Weber

Hi,
being a causal python user (who likes the language quite a lot)
it took me a while to realize the following:


l...@sylvester py_count $ python
Python 2.6.3 (r263:75183, Oct 26 2009, 12:34:23)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> max = '5'
>>> n = 5
>>> n >= max
False
>>> n + max
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>


Section 5.9 Comparison describes this.

Can someone give me examples of use cases

TIA

Hellmut

--
Dr. Hellmut Weber m...@hellmutweber.de
Degenfeldstraße 2 tel   +49-89-3081172
D-80803 München-Schwabing mobil +49-172-8450321
please: No DOCs, no PPTs. why: tinyurl.com/cbgq

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


Re: Prepend to logging message

2010-01-10 Thread Peter Otten
Joan Miller wrote:

> On 10 ene, 10:26, Peter Otten <__pete...@web.de> wrote:
>> Joan Miller wrote:
>> > How to prepend anything to a logging message? Is possible to do it
>> > from the dictionary object (ExtraLog) or is there is that override
>> > process() [1]?
>>
>> > --
>> > class ExtraLog(object):
>>
>> > def __getitem__(self, name):
>> > if name == 'foo':
>> > result = 'testing'
>> > return result
>>
>> > def __iter__(self):
>> > keys = ['foo',]
>> > keys.extend(self.__dict__.keys())
>> > return iter(keys)
>>
>> format = "foo=%(foo)s " + logging.BASIC_FORMAT
>> logging.basicConfig(format=format)
>> logger = logging.LoggerAdapter(logging.getLogger('foo'), ExtraLog())
>> logger.error("yadda")
>>
>> Is that what you want?
>>
>> Peter
> 
> I want that a message can be modified before of being logged. i.e. for
> "yadda" I would that were preppend 2 spaces. (And I want not manage
> that in the format to manage the indentation of all text)

Following Ishwor's advice:

import logging

class MyLoggerAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
return "message->" + msg.upper(), kwargs

logging.basicConfig()
logger = MyLoggerAdapter(logging.getLogger('foo'), {})
logger.error("yadda")

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


Re: integer and string compare, is that correct?

2010-01-10 Thread Chris Rebert
On Sun, Jan 10, 2010 at 4:26 AM, Hellmut Weber  wrote:
> Hi,
> being a causal python user (who likes the language quite a lot)
> it took me a while to realize the following:
>
>
> l...@sylvester py_count $ python
> Python 2.6.3 (r263:75183, Oct 26 2009, 12:34:23)
> [GCC 4.4.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 max = '5'
 n = 5
 n >= max
> False
 n + max
> Traceback (most recent call last):
>  File "", line 1, in 
> TypeError: unsupported operand type(s) for +: 'int' and 'str'

>
>
> Section 5.9 Comparison describes this.
>
> Can someone give me examples of use cases

The behavior of disparate types being comparable is deprecated and has
been removed in Python 3.0+; don't rely upon it. (The ordering used is
arbitrary but consistent)
IIRC, the feature existed in prior versions to make lists containing
mixed types sortable; this was found to be not all that useful and to
hide what are quite arguably errors.

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


Re: integer and string compare, is that correct?

2010-01-10 Thread Chris Rebert
On Sun, Jan 10, 2010 at 4:46 AM, Chris Rebert  wrote:

> The behavior of disparate types being comparable is deprecated and has
> been removed in Python 3.0+; don't rely upon it.

Clarification: Equality testing between disparate types still works
unaltered however.
By "comparable", I meant >, <, >=, <= comparisons.

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


Re: Prepend to logging message

2010-01-10 Thread Joan Miller
On 10 ene, 12:36, Peter Otten <__pete...@web.de> wrote:
> Joan Miller wrote:
> > On 10 ene, 10:26, Peter Otten <__pete...@web.de> wrote:
> >> Joan Miller wrote:
> >> > How to prepend anything to a logging message? Is possible to do it
> >> > from the dictionary object (ExtraLog) or is there is that override
> >> > process() [1]?
>
> >> > --
> >> > class ExtraLog(object):
>
> >> > def __getitem__(self, name):
> >> > if name == 'foo':
> >> > result = 'testing'
> >> > return result
>
> >> > def __iter__(self):
> >> > keys = ['foo',]
> >> > keys.extend(self.__dict__.keys())
> >> > return iter(keys)
>
> >> format = "foo=%(foo)s " + logging.BASIC_FORMAT
> >> logging.basicConfig(format=format)
> >> logger = logging.LoggerAdapter(logging.getLogger('foo'), ExtraLog())
> >> logger.error("yadda")
>
> >> Is that what you want?
>
> >> Peter
>
> > I want that a message can be modified before of being logged. i.e. for
> > "yadda" I would that were preppend 2 spaces. (And I want not manage
> > that in the format to manage the indentation of all text)
>
> Following Ishwor's advice:
>
> import logging
>
> class MyLoggerAdapter(logging.LoggerAdapter):
>     def process(self, msg, kwargs):
>         return "message->" + msg.upper(), kwargs
>
> logging.basicConfig()
> logger = MyLoggerAdapter(logging.getLogger('foo'), {})
> logger.error("yadda")
>
> Peter

Thanks!

I had to see the code to override it correctly [1]


class LoggerAdapter_(logging.LoggerAdapter):

def __init__(self, logger, extra):
self.logger = logger
self.extra = extra

def process(self, msg, kwargs):
kwargs["extra"] = self.extra
return "message->" + msg.upper(), kwargs


[1] 
http://bitbucket.org/mirror/python-trunk/src/tip/Lib/logging/__init__.py#cl-1264
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer and string compare, is that correct?

2010-01-10 Thread Peter Otten
Hellmut Weber wrote:

> being a causal python user (who likes the language quite a lot)
> it took me a while to realize the following:
> 
> 
> l...@sylvester py_count $ python
> Python 2.6.3 (r263:75183, Oct 26 2009, 12:34:23)
> [GCC 4.4.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> max = '5'
>  >>> n = 5
>  >>> n >= max
> False

> Section 5.9 Comparison describes this.
> 
> Can someone give me examples of use cases

The use cases for an order that works across types like int and str are weak 
to non-existent. Implementing it was considered a mistake and has been fixed 
in Python 3:

Python 3.1.1+ (r311:74480, Nov  2 2009, 15:45:00)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 5 > "5"
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: int() > str()

Checking for equality is still possible:

>>> 5 == "5"
False

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


"Advanced" Python programming book?

2010-01-10 Thread flow
I've just finished reading a sort of beginner Python book, and I know 
quite a bit now but I'm looking for a book that can teach me advanced 
aspects of Python - code optimisation, threading, etc.

Any recommendations?

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


Re: Prepend to logging message

2010-01-10 Thread Joan Miller
On 10 ene, 13:10, Joan Miller  wrote:
> On 10 ene, 12:36, Peter Otten <__pete...@web.de> wrote:
>
>
>
> > Joan Miller wrote:
> > > On 10 ene, 10:26, Peter Otten <__pete...@web.de> wrote:
> > >> Joan Miller wrote:
> > >> > How to prepend anything to a logging message? Is possible to do it
> > >> > from the dictionary object (ExtraLog) or is there is that override
> > >> > process() [1]?
>
> > >> > --
> > >> > class ExtraLog(object):
>
> > >> > def __getitem__(self, name):
> > >> > if name == 'foo':
> > >> > result = 'testing'
> > >> > return result
>
> > >> > def __iter__(self):
> > >> > keys = ['foo',]
> > >> > keys.extend(self.__dict__.keys())
> > >> > return iter(keys)
>
> > >> format = "foo=%(foo)s " + logging.BASIC_FORMAT
> > >> logging.basicConfig(format=format)
> > >> logger = logging.LoggerAdapter(logging.getLogger('foo'), ExtraLog())
> > >> logger.error("yadda")
>
> > >> Is that what you want?
>
> > >> Peter
>
> > > I want that a message can be modified before of being logged. i.e. for
> > > "yadda" I would that were preppend 2 spaces. (And I want not manage
> > > that in the format to manage the indentation of all text)
>
> > Following Ishwor's advice:
>
> > import logging
>
> > class MyLoggerAdapter(logging.LoggerAdapter):
> >     def process(self, msg, kwargs):
> >         return "message->" + msg.upper(), kwargs
>
> > logging.basicConfig()
> > logger = MyLoggerAdapter(logging.getLogger('foo'), {})
> > logger.error("yadda")
>
> > Peter
>
> Thanks!
>
> I had to see the code to override it correctly [1]
>
> 
> class LoggerAdapter_(logging.LoggerAdapter):
>
>     def __init__(self, logger, extra):
>         self.logger = logger
>         self.extra = extra
>
>     def process(self, msg, kwargs):
>         kwargs["extra"] = self.extra
>         return "message->" + msg.upper(), kwargs
> 
>
> [1]http://bitbucket.org/mirror/python-trunk/src/tip/Lib/logging/__init__...

Sorry! It isn't necessary to copy __init__().
-- 
http://mail.python.org/mailman/listinfo/python-list


"Advanced" Python programming book?

2010-01-10 Thread Ryniek90

> I've just finished reading a sort of beginner Python book, and I know 
> quite a bit now but I'm looking for a book that can teach me advanced 
> aspects of Python - code optimisation, threading, etc.
>
> Any recommendations?
>
> Cheers.
Check those link:
http://www.amazon.com/Beginning-Python-Visualization-Transformation-Professionals/dp/1430218436/ref=sr_1_11?ie=UTF8&s=books&qid=1263136036&sr=1-11

http://www.amazon.com/Gray-Hat-Python-Programming-Engineers/dp/1593271921/ref=sr_1_12?ie=UTF8&s=books&qid=1263136036&sr=1-12

http://www.amazon.com/Python-Cookbook-Alex-Martelli/dp/0596007973/ref=sr_1_4?ie=UTF8&s=books&qid=1263136036&sr=1-4

http://www.amazon.com/High-Performance-Python-Anthony-Lewis/dp/0596522088/ref=sr_1_14?ie=UTF8&s=books&qid=1263136082&sr=1-14

http://www.amazon.com/Natural-Language-Processing-Python-Steve/dp/0596516495/ref=sr_1_16?ie=UTF8&s=books&qid=1263136082&sr=1-16

http://www.amazon.com/Python-Scripting-Computational-Science-Engineering/dp/3540739157/ref=sr_1_17?ie=UTF8&s=books&qid=1263136082&sr=1-17

http://www.amazon.com/Expert-Python-Programming-practices-distributing/dp/184719494X/ref=sr_1_20?ie=UTF8&s=books&qid=1263136082&sr=1-20

http://www.djangobook.com/


Similar topics you can find in Google.
Check python technologies, like: PyCuda, SciPy, NumPy, PyGame, Django,
Pylons, Zope, TurboGears, Twisted, PyGTK, PyQt and so on.

Check this link: http://www.python.org/about/apps/  and this
http://wiki.python.org/moin/

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


Re: lightweight encryption of text file

2010-01-10 Thread Steven D'Aprano
On Sun, 10 Jan 2010 09:59:31 +0100, Daniel Fetchinson wrote:

> Thanks, this looks very simple too, but where is the decryption code?
> Wikipedia seems to suggest that encryption and decryption are both the
> same but running crypt on the output of crypt doesn't give back the
> original string. So probably I'm misunderstanding something.

Yes, the nature of a stream cipher :)

What you're probably doing is what I did, before I had my Aha!!! moment:


>>> arc = arcfour("password")
>>> plaintext = "attack at dawn"
>>> ciphertext = arc.crypt(plaintext)
>>> print plaintext; print ciphertext.encode("hex").upper()
attack at dawn
6371736C6E7C3F495C185629210B
>>> x = arc.crypt(ciphertext)
>>> assert x == plaintext
Traceback (most recent call last):
  File "", line 1, in 
AssertionError
>>> x
'\x16\xf7\xf1\xcc\xda\xb5\xe0\xbf\x0b\x13 bF\x8f'


So what's going on? Consider:

Because Arcfour uses xor for the encryption step, decryption is exactly 
the same. So you only need one method to do both.

But because Arcfour stores state, calling it twice in a row doesn't give 
the same result:

>>> arc.crypt("hello").encode("hex").upper()
'CAA48DE953'
>>> arc.crypt("hello").encode("hex").upper()
'03405412CA'


Arcfour is a stream cipher. When you call it twice on two different 
strings, that is logically equivalent to calling it once on a single long 
string made up of concatenating the two strings together. Each time you 
encrypt a single character, the internal state ("self.s") changes. To 
undo the change, you need the same state. The easiest way to do this is 
by creating a new instance:


>>> encrypter = arcfour("password")
>>> decrypter = arcfour("password")
>>> plaintext = "attack at dawn"
>>> ciphertext = encrypter.crypt(plaintext)
>>> assert decrypter.crypt(ciphertext) == plaintext
>>> 


So long as the two instances stay in lock-step (every time you use up a 
byte from the keystream in one, you do the same in the other) you can use 
one to decrypt the output of the other. It doesn't even really matter 
which one you use:

>>> x = decrypter.crypt("Nobody expects the Spanish Inquisition!!!")
>>> encrypter.crypt(x)
'Nobody expects the Spanish Inquisition!!!'


In summary: use the arcfour class to create a stream. If you are just 
encrypting, or just decrypting, you can use one stream, or as many 
streams as you like, using different keys. But to do both, you need two 
streams, initiated with the same key, and kept in lockstep.

The advantage of a stream cipher is that you can encrypt a text without 
needing all the text at once, and then decrypt it the same way:

>>> output = []
>>> output.append(encrypt.crypt("abcdefghi"))
>>> output.append(encrypt.crypt("jklmno"))
>>> output.append(encrypt.crypt("p"))
>>> output.append(encrypt.crypt("qrstuvwxyz"))
>>> output = ''.join(output)
>>>
>>> plain = []
>>> plain.append(decrypt.crypt(output[0:20]))
>>> plain.append(decrypt.crypt(output[20:24]))
>>> plain.append(decrypt.crypt(output[24:]))
>>> ''.join(plain)
'abcdefghijklmnopqrstuvwxyz'



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


[ANN] Spyder v1.0.2 released

2010-01-10 Thread Pierre Raybaut

Hi all,

I'm pleased to announce here that Spyder version 1.0.2 has been released:
http://packages.python.org/spyder

Previously known as Pydee, Spyder (Scientific PYthon Development
EnviRonment) is a free open-source Python development environment
providing MATLAB-like features in a simple and light-weighted
software, available for Windows XP/Vista/7, GNU/Linux and MacOS X:
   * advanced code editing features (code analysis, ...)
   * interactive console with MATLAB-like workpace (with GUI-based
list, dictionary, tuple, text and array editors -- screenshots:
http://packages.python.org/spyder/console.html#the-workspace) and
integrated matplotlib figures
   * external console to open an interpreter or run a script in a
separate process (with a global variable explorer providing the same
features as the interactive console's workspace)
   * code analysis with pyflakes and pylint
   * search in files features
   * documentation viewer: automatically retrieves docstrings or
source code of the function/class called in the interactive/external
console
   * integrated file/directories explorer
   * MATLAB-like path management
   ...and more!

Spyder is part of spyderlib, a Python module based on PyQt4 and
QScintilla2 which provides powerful console-related PyQt4 widgets.

Spyder v1.0.2 is a bugfix release:
* External console: subprocess python calls were using the external 
console's sitecustomize.py (instead of system sitecustomize.py)

* Added workaround for PyQt4 v4.6+ major bug with matplotlib
* Added option to customize the way matplotlib figures are embedded 
(docked or floating window)

* Matplotlib's "Option" dialog box is now supporting subplots
* Array editor now supports complex arrays
* Editor: replaced "Run selection or current line" option by "Run 
selection or current block" (without selection, this feature is similar 
to MATLAB's cell mode)

* ...and a lot of minor bugfixes.

- Pierre


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


Re: lightweight encryption of text file

2010-01-10 Thread Nobody
On Sun, 10 Jan 2010 09:59:31 +0100, Daniel Fetchinson wrote:

> Thanks, this looks very simple too, but where is the decryption code?
> Wikipedia seems to suggest that encryption and decryption are both the
> same but running crypt on the output of crypt doesn't give back the
> original string. So probably I'm misunderstanding something.

The encryption is stateful (it wouldn't be very good if it wasn't), so you
need to create and initialise a new arcfour instance for decryption; you
can't re-use the existing instance.

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


Re: lightweight encryption of text file

2010-01-10 Thread Nobody
On Sun, 10 Jan 2010 15:30:12 +, Steven D'Aprano wrote:

>> Thanks, this looks very simple too, but where is the decryption code?
>> Wikipedia seems to suggest that encryption and decryption are both the
>> same but running crypt on the output of crypt doesn't give back the
>> original string. So probably I'm misunderstanding something.
> 
> Yes, the nature of a stream cipher :)

It isn't limited to stream ciphers. You would have the same issue for
a block cipher with chaining (i.e. *not* ECB mode).


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


Re: integer and string compare, is that correct?

2010-01-10 Thread Nobody
Hellmut Weber wrote:

>> being a causal python user (who likes the language quite a lot)
>> it took me a while to realize the following:

>>  >>> max = '5'
>>  >>> n = 5
>>  >>> n >= max
>> False
> 
>> Section 5.9 Comparison describes this.
>> 
>> Can someone give me examples of use cases

Peter Otten wrote:

> The use cases for an order that works across types like int and str are weak 
> to non-existent. Implementing it was considered a mistake and has been fixed 
> in Python 3:

 5 > "5"
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: unorderable types: int() > str()

If you actually need to perform comparisons across types, you can rely
upon the fact that tuple comparisons are non-strict and use e.g.:

> a = 5
> b = '5'
> (type(a).__name__, a) < (type(b).__name__, b)
True
> (type(a).__name__, a) > (type(b).__name__, b)
False

The second elements will only be compared if the first elements are equal
(i.e. the values have the same type).

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


Re: "Advanced" Python programming book?

2010-01-10 Thread Tom Pacheco

Ryniek90 wrote:
I've just finished reading a sort of beginner Python book, and I know 
quite a bit now but I'm looking for a book that can teach me advanced 
aspects of Python - code optimisation, threading, etc.


Any recommendations?

Cheers.


Check those link:
http://www.amazon.com/Beginning-Python-Visualization-Transformation-Professionals/dp/1430218436/ref=sr_1_11?ie=UTF8&s=books&qid=1263136036&sr=1-11

http://www.amazon.com/Gray-Hat-Python-Programming-Engineers/dp/1593271921/ref=sr_1_12?ie=UTF8&s=books&qid=1263136036&sr=1-12

http://www.amazon.com/Python-Cookbook-Alex-Martelli/dp/0596007973/ref=sr_1_4?ie=UTF8&s=books&qid=1263136036&sr=1-4

http://www.amazon.com/High-Performance-Python-Anthony-Lewis/dp/0596522088/ref=sr_1_14?ie=UTF8&s=books&qid=1263136082&sr=1-14

http://www.amazon.com/Natural-Language-Processing-Python-Steve/dp/0596516495/ref=sr_1_16?ie=UTF8&s=books&qid=1263136082&sr=1-16

http://www.amazon.com/Python-Scripting-Computational-Science-Engineering/dp/3540739157/ref=sr_1_17?ie=UTF8&s=books&qid=1263136082&sr=1-17

http://www.amazon.com/Expert-Python-Programming-practices-distributing/dp/184719494X/ref=sr_1_20?ie=UTF8&s=books&qid=1263136082&sr=1-20

http://www.djangobook.com/


Similar topics you can find in Google.
Check python technologies, like: PyCuda, SciPy, NumPy, PyGame, Django,
Pylons, Zope, TurboGears, Twisted, PyGTK, PyQt and so on.

Check this link: http://www.python.org/about/apps/  and this
http://wiki.python.org/moin/

Cheers  :)
  

some good books.. i might add this to the list

http://www.amazon.com/Python-Essential-Reference-David-Beazley/dp/0672329786/ref=pd_sim_b_2






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


Re: lightweight encryption of text file

2010-01-10 Thread Daniel Fetchinson
>> Thanks, this looks very simple too, but where is the decryption code?
>> Wikipedia seems to suggest that encryption and decryption are both the
>> same but running crypt on the output of crypt doesn't give back the
>> original string. So probably I'm misunderstanding something.
>
> Yes, the nature of a stream cipher :)
>
> What you're probably doing is what I did, before I had my Aha!!! moment:
>
>
 arc = arcfour("password")
 plaintext = "attack at dawn"
 ciphertext = arc.crypt(plaintext)
 print plaintext; print ciphertext.encode("hex").upper()
> attack at dawn
> 6371736C6E7C3F495C185629210B
 x = arc.crypt(ciphertext)
 assert x == plaintext
> Traceback (most recent call last):
>   File "", line 1, in 
> AssertionError
 x
> '\x16\xf7\xf1\xcc\xda\xb5\xe0\xbf\x0b\x13 bF\x8f'
>
>
> So what's going on? Consider:
>
> Because Arcfour uses xor for the encryption step, decryption is exactly
> the same. So you only need one method to do both.
>
> But because Arcfour stores state, calling it twice in a row doesn't give
> the same result:
>
 arc.crypt("hello").encode("hex").upper()
> 'CAA48DE953'
 arc.crypt("hello").encode("hex").upper()
> '03405412CA'
>
>
> Arcfour is a stream cipher. When you call it twice on two different
> strings, that is logically equivalent to calling it once on a single long
> string made up of concatenating the two strings together. Each time you
> encrypt a single character, the internal state ("self.s") changes. To
> undo the change, you need the same state. The easiest way to do this is
> by creating a new instance:
>
>
 encrypter = arcfour("password")
 decrypter = arcfour("password")
 plaintext = "attack at dawn"
 ciphertext = encrypter.crypt(plaintext)
 assert decrypter.crypt(ciphertext) == plaintext

>
>
> So long as the two instances stay in lock-step (every time you use up a
> byte from the keystream in one, you do the same in the other) you can use
> one to decrypt the output of the other. It doesn't even really matter
> which one you use:
>
 x = decrypter.crypt("Nobody expects the Spanish Inquisition!!!")
 encrypter.crypt(x)
> 'Nobody expects the Spanish Inquisition!!!'
>
>
> In summary: use the arcfour class to create a stream. If you are just
> encrypting, or just decrypting, you can use one stream, or as many
> streams as you like, using different keys. But to do both, you need two
> streams, initiated with the same key, and kept in lockstep.
>
> The advantage of a stream cipher is that you can encrypt a text without
> needing all the text at once, and then decrypt it the same way:
>
 output = []
 output.append(encrypt.crypt("abcdefghi"))
 output.append(encrypt.crypt("jklmno"))
 output.append(encrypt.crypt("p"))
 output.append(encrypt.crypt("qrstuvwxyz"))
 output = ''.join(output)

 plain = []
 plain.append(decrypt.crypt(output[0:20]))
 plain.append(decrypt.crypt(output[20:24]))
 plain.append(decrypt.crypt(output[24:]))
 ''.join(plain)
> 'abcdefghijklmnopqrstuvwxyz'


Thanks Steven, this was very helpful!

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lightweight encryption of text file

2010-01-10 Thread Paul Rubin
Nobody  writes:
> RC4 (aka ArcFour) is quite trivial to implement, and better than inventing
> your own cipher or using a Vignere: ...

That's a cute implementation, but it has no authentication and doesn't
include any randomness, which means if you use the same key for two
inputs, there is a security failure (xor'ing the two ciphertexts reveals
the xor of the plaintexts).  It also looks rather slow.  I don't make
any guarantees about p3.py, but it has been reviewed by several experts
and appears to be reasonably sound for the type of casual use being
discussed here, and it is tuned for speed (given the implementation
constraints).  For more demanding purposes, you should use a more
serious library like one of the OpenSSL wrappers.
-- 
http://mail.python.org/mailman/listinfo/python-list


Academic Question

2010-01-10 Thread Victor Subervi
Hi;
The following code that works:

#! /usr/bin/python

import string
import cgitb; cgitb.enable()
import cgi
import MySQLdb
import sys,os
from sets import Set
import fpformat
cwd = os.getcwd()
sys.path.append(cwd)
from login import login
from particulars import ourOptions

form = cgi.FieldStorage()
store = form.getfirst('store')
cat = form.getfirst('cat')
id = form.getfirst('id')
patientID = form.getfirst('patientID')

try:
  browser = form.getfirst('browser', 'all')
except:
  browser = headers()

os.chdir('%s/..' % cwd)
sys.path.append(os.getcwd())
from templateFrame import top, bottom
os.chdir(cwd)

Why doesn't it work if I move the bottom imports to the top? The form values
get lost, even though I chdir to cwd.
TIA,
beno

-- 
The Logos has come to bear
http://logos.13gems.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Something More Elegant

2010-01-10 Thread Victor Subervi
On Sat, Jan 9, 2010 at 3:00 PM, Stephen Hansen wrote:

> On Sat, Jan 9, 2010 at 7:15 AM, Victor Subervi wrote:
>
>> On Sat, Jan 9, 2010 at 9:35 AM, Steve Holden  wrote:
>>
>>> But we are now in the realm of theory as far as you are concerned, since
>>> you have already stated several times that you aren't interested in
>>> correcting your design until after you have got the current mess into
>>> production.  So good luck with that.
>>>
>>
>> And if you were in my shoes, I'm sure you'd do the same thing.
>
>
> ... Really, no, he wouldn't :)
>

Let's not argue an academic point.


> We're not all just hobbyists here who only do work for random open source
> projects. A lot of us are professionals who actually do have clients,
> actually do have deadlines, actually do have an understanding for production
> requirements. Getting something into production as soon as possible is
> certainly an important goal in commercial work. But it is not the only goal.
> Proper database design is very important because if you don't do it, you'll
> actually end up usually wasting *more* time and effort then if you just bite
> the bullet and fix it now.
>

Clearly. That's why when it was pointed out to me, I jumped on it.


>
> Proper database design -- in particular in your case, not having multiple
> tables with various names that even need "%sThis" or "%sThat", and using
> parameterized queries to access those tables, is really important. It will
> save you time, it will save you effort, and it'll save you money-- because
> /not/ doing it is among other things, a major security risk. Getting code
> out fast and now is a noble goal in commercial projects, getting code out
> which is by design prone to attacks by hackers is negligence.
>

You're preaching to the choir ;) Read the above. The only exception to this
is putting all store stuff in the same table. The columns of each store
table are different and automatically created through what I believe are
called "mixins".

>
> Well, it *is* working now :)) And I am interested in cleaning this up. I
>> should probably start with the matter of databases, since that's something I
>> won't be able to easily change once clients actually start entering data.
>> Please share with me any further concepts or questions to get me thinking
>> how to redesign the databases.
>>
>
> The next thing to do is to re-do your SQL queries. You should never do
> string interpolation, e.g:
>
>  SQL="SELECT x FROM y WHERE z = %s" % (arg,)
>  cur.execute(SQL)
>
> If you have done the first step, your tables always have a set names so you
> never need to interpolate to do the queries-- and then at this point, under
> no circumstances ever, ever-- consider this a law that you will get fined
> for violation-- use string interpolation to generate your queries.
>
> Instead, do:
>
> cur.execute("SELECT x FROM y WHERE z = %s", (arg,))
>
> That looks really similar, but is lightyears away. Its very unfortunate
> that some database drivers use 'format' as the paramstyle because it
> confuses issues, but the two effects are very different. In one, Python is
> just munging together and creating a new string. In the other, the database
> driver is doing a few things. Its analyzing the query, its storing it (often
> caching it, which speeds up further executions of that query), etc, etc, and
> then finally its seeing that you are passing arguments into it, and it is
> -safely- binding those arguments into the expression; this prevents SQL
> Injection attacks. You can use interpolation and prevent injection if you
> -meticulously- check -every- string that comes from the user, and -never-
> trust it (even if that string was written out to a hidden  and
> legitimate users have no way to alter, because illegitimate users will alter
> it anyways). Or you can use parameterized queries and just avoid it, while
> getting plenty of other benefits as well.
>
>
> At work, we had a third-party package that we re-sold as part of our
> offering, and glancing over its source, I noticed something. It did, in
> essence to check login:
>
>  cur.execute("SELECT user_id FROM usertable WHERE username = '%s' AND
> password = '%s' % (username, password))
>
> I blinked, and emailed them to point out the problem. I suggested they log
> in as:
>
> Username = dummyuser
> Password = '; DROP usertable
>
> You see, when using interpolation, the string that got sent to the database
> was:
>
> SELECT user_id FROM usertable WHERE username = 'dummyuser' AND password
> = ''; DROP usertable
>
> And thus, from the login screen of this app-- I destroyed their
> environment.
>
> Its sort of important that you not put code out into production which is
> susceptible to such things. Your clients will not at all appreciate it if
> and when some hacker discovers it and destroys their site, loosing them
> money. But even beyond that, there are many other benefits to just doing
> this right. You want to, believe me. Now, before yo

Re: integer and string compare, is that correct?

2010-01-10 Thread Peter Otten
Somebody wrote:

> If you actually need to perform comparisons across types, you can rely
> upon the fact that tuple comparisons are non-strict and use e.g.:
> 
> > a = 5
> > b = '5'
> > (type(a).__name__, a) < (type(b).__name__, b)
> True
> > (type(a).__name__, a) > (type(b).__name__, b)
> False
> 
> The second elements will only be compared if the first elements are equal
> (i.e. the values have the same type).

The same type *name*. To play it safe you'll have to compare the id, too:

>>> items = []
>>> for i in range(2):
... class A: pass
... items.append(A())
...
>>> sorted(items, key=lambda a: (type(a).__name__, a))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: A() < A()
>>> sorted(items, key=lambda a: (type(a).__name__, id(type(a)), a))
[<__main__.A object at 0x14dfbd0>, <__main__.A object at 0x14dfc50>]

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


Re: problem with cheetah

2010-01-10 Thread Aahz
In article <0fd84b05-cf12-485b-a14e-608e47679...@s20g2000yqd.googlegroups.com>,
mlowicki   wrote:
>
>Hi!, i get such error when I try to install cheetah:

Probably better to ask on the Cheetah list:

https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur."  --Red Adair
-- 
http://mail.python.org/mailman/listinfo/python-list


Milliseconds in logging format

2010-01-10 Thread Joan Miller
How the logging '%(asctime)s' [1] specifier to gets the millisecond
portion of the time if there is not a directive to get it from the
time module [2] ?


"The date format string follows the requirements of strftime()"
[1] http://docs.python.org/library/logging.html#basic-example

[2] http://docs.python.org/library/time.html#time.strftime
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Academic Question

2010-01-10 Thread MRAB

Victor Subervi wrote:

Hi;
The following code that works:

#! /usr/bin/python

import string
import cgitb; cgitb.enable()
import cgi
import MySQLdb
import sys,os
from sets import Set
import fpformat
cwd = os.getcwd()
sys.path.append(cwd)
from login import login
from particulars import ourOptions

form = cgi.FieldStorage()
store = form.getfirst('store')
cat = form.getfirst('cat')
id = form.getfirst('id')
patientID = form.getfirst('patientID')

try:
  browser = form.getfirst('browser', 'all')
except:
  browser = headers()


A bare except, and if an exception _does_ occur, they'll be a NameError
because 'headers' isn't defined.




os.chdir('%s/..' % cwd)
sys.path.append(os.getcwd())
from templateFrame import top, bottom
os.chdir(cwd)

Why doesn't it work if I move the bottom imports to the top? The form 
values get lost, even though I chdir to cwd.



I try to avoid changing the directory.
--
http://mail.python.org/mailman/listinfo/python-list


Re: lightweight encryption of text file

2010-01-10 Thread geremy condra
Not sure why in the world you would homebrew something like this- a
small dependency isn't that bad, and aes can be pretty simple to use.
Might as well go for the industrial strength approach.

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


Re: Milliseconds in logging format

2010-01-10 Thread Irmen de Jong

On 10-1-2010 20:04, Joan Miller wrote:

How the logging '%(asctime)s' [1] specifier to gets the millisecond
portion of the time if there is not a directive to get it from the
time module [2] ?


"The date format string follows the requirements of strftime()"
[1] http://docs.python.org/library/logging.html#basic-example

[2] http://docs.python.org/library/time.html#time.strftime


It appends them to the string that strftime() returned.
If you set a custom datefmt, it will stop doing this and you'll have to
use %(msecs) in your log format to get them back.

See the source, Lib/logging/__init__.py around line 400. (Python 2.6.4)

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


"Bad file descriptor" in HTTPServer using Multiprocessing.

2010-01-10 Thread Adam Tauno Williams
I have a Python multiprocessing application where a master process
starts server sub-processes and communicates with them via Pipes;  that
works very well.  But one of the subprocesses, in turn, starts a
collection of HTTPServer 'workers' (almost exactly as demonstrated in
the docs).  This works perfectly so long as the subprocess that
starts the HTTPServer workers is the *first* process started by the
master process.  Otherwise the HTTPServer's serve_forever immediately
abends with: "(9, 'Bad file descriptor')"

I'm confused why the order of starting the processes matter, especially
given that the HTTPServer's are start in a subprocess of a subprocess.
The master doesn't do much of anything between starting each subprocess
(which it does in a loop;  identify all the subprocesses to start, and
start them.).

subprocess

..
self._httpd = HTTPServer((HTTP_HOST, HTTP_PORT), HTTPRequestHandler)
print 'HTTPServer created.'
for i in range(10):
p = multiprocessing.Process(target=serve_forever, 
args=(self._httpd, i, self))
self._workers.append(p)
p.start()

.
def serve_forever(server, i, control):
print 'coils.http starting HTTP worker #{0}'.format(i)
try:
server.serve_forever()
except KeyboardInterrupt:
pass
except Exception, e:
control.log.exception(e)
print 'coils.http worker #{0} abended with exception.'.format(i)
print e
return


-- 
OpenGroupware developer: awill...@whitemice.org

OpenGroupare & Cyrus IMAPd documenation @


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


How to use list type generated by SWIG?

2010-01-10 Thread Bryan
I am writing a small script to manage my ipod.  I am using the python
bindings for libgpod.  I have never used swig, or used python to
program against a c/c++ library.

I can get the library to find my ipod, and parse its DB format, but
I'm not sure how to interact with the types that some of the functions
return, specifically a Swig Glist (gtk's doubly-linked list)

import gpod
db = gpod.itdb_parse('/media/ipod', None)
print db.tracks
>>>

Glist is not iterable or subscriptable.  It contains a 'next' function
which always seems to return None after printing "swig/python detected
a memory leak of type 'GList *', no destructor found." to the console.

How can I interact with this list?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lightweight encryption of text file

2010-01-10 Thread Paul Rubin
geremy condra  writes:
> Not sure why in the world you would homebrew something like this- a
> small dependency isn't that bad, and aes can be pretty simple to use.
> Might as well go for the industrial strength approach.

In my experience, 1) small dependencies ARE that bad, since they mean
you have to develop and test on every platform that you want your code
to run on; 2) using a serious library requires quite a bit of knowledge
and decision-making which not everyone is equipped to do.  "AES" is not
so simple to use unless you know what you're doing in terms of modes,
nonces, etc.  Having supported this kind of package in a commercial
setting in the past, IMO, for the sort of (common) application in
question, it's best to keep things as simple as possible and supply a
single interface that provides encryption, authentication, and random
initialization all in one call.  The cost is a little bit of ciphertext
bloat, but it prevents all kinds of security failures frequently
overlooked by novices.

I'd like it a lot if the Python stdlib could include a serious
cryptography module.  That was rejected for regulatory reasons several
years ago, but maybe things are changing enough that the issue can be
revisited sometime.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Office Word and Python (Win XP)

2010-01-10 Thread 3lvss0...@gmail.com
so does anyone know how I could do this?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getopt not raising exception

2010-01-10 Thread Matthew Lear
Matt Nordhoff wrote:
 > BTW: Checked out optparse? It's bigger and sexier!

Thanks for the tip. Works a treat.
--  Matt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getopt not raising exception

2010-01-10 Thread Emmanuel Surleau
> Matt Nordhoff wrote:
>  > BTW: Checked out optparse? It's bigger and sexier!

If you're doing things with positional arguments, you should consider using 
argparse (http://argparse.googlecode.com/svn/tags/r101/doc/index.html). It's 
like optparse, but (much) better.

Cheers,

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


class viewer ?

2010-01-10 Thread Stef Mientki

hello,

I'd like to have a class viewer, something different from pydoc,
and I wonder if someone has made something similar.

from the given class, it's ancestors and it's derived classes,
I'ld like to get the following information in a tree like structure:

- the file were the class is definied
- the attributes, split in inherited / created / overriden
- the methodes, split in inherited / created / overriden
- the files were instances of the class are created
- and probably I forget a few

any suggestions ?

thanks,
Stef Mientki

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


setTextAlignment function Qtablewidget PyQt

2010-01-10 Thread Zabin
Hey!

I am trying to align contents of some table cells but find the code
below working for some fields and not for others. I am stuck as to why
this is happening. Help will be gretly appreciated!

setTextAlignment(QtCore.Qt.AlignVCenter)

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


sys.stdout vs. sys.stderr

2010-01-10 Thread Mitchell L Model
In Python 3.1 is there any difference in the buffering behavior of the  
initial sys.stdout and sys.stderr streams? They are both line_buffered  
and stdout doesn't seem to use a larger-grain buffering, so they seem  
to be identical with respect to buffering. Were they different at some  
earlier point in Python's evolution?

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


Re: setTextAlignment function Qtablewidget PyQt

2010-01-10 Thread Zabin
On Jan 11, 1:10 pm, Zabin  wrote:
> Hey!
>
> I am trying to align contents of some table cells but find the code
> below working for some fields and not for others. I am stuck as to why
> this is happening. Help will be gretly appreciated!
>
> setTextAlignment(QtCore.Qt.AlignVCenter)
>
> Cheers
> Zabin

Figured it out!
It was just the '\n' character being read with the line from the text
file that messed up the formatting.
just needed to use the line.strip() function before passing it to the
table as a table widget item.

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


Fractional Hours from datetime?

2010-01-10 Thread W. eWatson
Maybe there's a more elegant way to do this. I want to express the 
result of datetime.datetime.now() in fractional hours.


Here's one way.

dt=datetime.datetime.now()
xtup = dt.timetuple()
h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6
#  now is in fractions of an hour
--
http://mail.python.org/mailman/listinfo/python-list


Python Goldmine has been updated: http://preciseinfo.org/Convert/index_Convert_Python.html

2010-01-10 Thread tanix
Python Goldmine collection contains the extensive collection of articles
going back several years. It includes thousands of code
examples and expert discussions on all major topics.

The information is organized by relevant topics, covered
by the corresponding chapters.

The information was filtered with sophisticated filters and vast
majority of artilces with little relevance have been filtered out.

If you have any specific requests for some new chapters to be added
and it is of interest to others, please post your requests on this
thread.

If anyone feels he has above average level of competence, or can
reccommend someone who posts on this group, you may request to be
included in the expert chapters.

The Python Goldmine is at:

http://preciseinfo.org/Convert/index_Convert_Python.html

--
Programmer's Goldmine collections:

http://preciseinfo.org

Tens of thousands of code examples and expert discussions on
C++, MFC, VC, ATL, STL, templates, Java, Python, Javascript, PHP,
organized by major topics of language, tools, methods, techniques.

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


Re: Introspection

2010-01-10 Thread m...@infoserv.dk
Thanks Miki and Jason. I knew it could be done :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Manipulating pointers in C using ctypes

2010-01-10 Thread Daniel Platz
Thanks for valuable answers. Both solutions work and I understand my
mistake.

Best regards,

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


Re: Microsoft Office Word and Python (Win XP)

2010-01-10 Thread 3lvss0...@gmail.com
?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Office Word and Python (Win XP)

2010-01-10 Thread 3lvss0...@gmail.com
no idea :-(
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Computer Music Toolkit (CMT) ?

2010-01-10 Thread Terry Reedy

On 1/10/2010 4:15 AM, Peter Billam wrote:

Greetings. Is there a way to get at the Computer Music Toolkit (CMT)
   http://www.ladspa.org/cmt/
functionality from Python (Python3 in my case) ?


You can access compiled C shared libraries most easily via the ctypes 
module.


Searching Python CMT "Computer Music Toolkit"
(following Stefan's suggestion) returns a few hits that might be useful.

Searching PyPI for CMT does not turn up any relevant packages, so if you 
do develop a ctypes wrapping, consider contributing it.


Terry Jan Reedy

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


Re: Python Goldmine has been updated: http://preciseinfo.org/Convert/index_Convert_Python.html

2010-01-10 Thread Terry Reedy

On 1/8/2010 11:50 AM, tanix wrote:

Python Goldmine collection contains the extensive collection of articles
going back several years. It includes thousands of code
examples and expert discussions on all major topics.

The information is organized by relevant topics, covered
by the corresponding chapters.

The information was filtered with sophisticated filters and vast
majority of artilces with little relevance have been filtered out.

If you have any specific requests for some new chapters to be added
and it is of interest to others, please post your requests on this
thread.

If anyone feels he has above average level of competence, or can
reccommend someone who posts on this group, you may request to be
included in the expert chapters.

The Python Goldmine is at:

http://preciseinfo.org/Convert/index_Convert_Python.html

--
Programmer's Goldmine collections:

http://preciseinfo.org

Tens of thousands of code examples and expert discussions on
C++, MFC, VC, ATL, STL, templates, Java, Python, Javascript, PHP,
organized by major topics of language, tools, methods, techniques.


This site pops up spam windowns. One was blocked, one managed to bypass 
the popup blocker. Tnis is not friendly behaviour.


Some categories have 100s of entries. Better, I think, to use a search 
engine such as  Google with more specific search terms and a snippet of 
context for each result.


tjr



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


Re: Python Goldmine has been updated: http://preciseinfo.org/Convert/index_Convert_Python.html

2010-01-10 Thread Steve Holden
Terry Reedy wrote:
> On 1/8/2010 11:50 AM, tanix wrote:
>> Python Goldmine collection contains the extensive collection of articles
>> going back several years. It includes thousands of code
>> examples and expert discussions on all major topics.
>>
>> The information is organized by relevant topics, covered
>> by the corresponding chapters.
>>
>> The information was filtered with sophisticated filters and vast
>> majority of artilces with little relevance have been filtered out.
>>
>> If you have any specific requests for some new chapters to be added
>> and it is of interest to others, please post your requests on this
>> thread.
>>
>> If anyone feels he has above average level of competence, or can
>> reccommend someone who posts on this group, you may request to be
>> included in the expert chapters.
>>
>> The Python Goldmine is at:
>>
>> http://preciseinfo.org/Convert/index_Convert_Python.html
>>
>> -- 
>> Programmer's Goldmine collections:
>>
>> http://preciseinfo.org
>>
>> Tens of thousands of code examples and expert discussions on
>> C++, MFC, VC, ATL, STL, templates, Java, Python, Javascript, PHP,
>> organized by major topics of language, tools, methods, techniques.
> 
> This site pops up spam windowns. One was blocked, one managed to bypass
> the popup blocker. Tnis is not friendly behaviour.
> 
> Some categories have 100s of entries. Better, I think, to use a search
> engine such as  Google with more specific search terms and a snippet of
> context for each result.
> 
Because I habitually run the NoScript extension to Firefox the popups
didn't appear, but there didn't seem to be any original content on this
site. Google continues to be your friend.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Python Goldmine has been updated: http://preciseinfo.org/Convert/index_Convert_Python.html

2010-01-10 Thread Steve Holden
Steve Holden wrote:
[...]
> Because I habitually run the NoScript extension to Firefox the popups
> didn't appear, but there didn't seem to be any original content on this
> site. Google continues to be your friend.
> 
And dammit, why didn't I think to strip the links out instead of
creating yet one more link to it? Sorry ...

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: lightweight encryption of text file

2010-01-10 Thread Nobody
On Sun, 10 Jan 2010 08:54:51 -0800, Paul Rubin wrote:

> Nobody  writes:
>> RC4 (aka ArcFour) is quite trivial to implement, and better than inventing
>> your own cipher or using a Vignere: ...
> 
> That's a cute implementation, but it has no authentication and doesn't
> include any randomness, which means if you use the same key for two
> inputs, there is a security failure (xor'ing the two ciphertexts reveals
> the xor of the plaintexts).

Right. RC4 is a cipher, not a cryptosystem.

But, yeah, the OP needs to be aware of the difference (and probably isn't,
yet). So to take that a step further ...

The key passed to arcfour.schedule() shouldn't be re-used. If you need to
encrypt multiple files, use a different key for each. If you want to
encrypt multiple files with the same "password", generate a unique key by
hashing a combination of the password and a random salt (e.g. from
/dev/random), and prepend the salt to the beginning of the stream. To
decrypt, extract the salt from the stream to generate the key.

If you need to verify the data, append a hash of the ciphertext (a hash
of the plaintext would allow an attacker to confirm a guessed plaintext
or to confirm that two files contain the same plaintext). Stream ciphers
are vulnerable to replacement attacks:

(p1 xor r) xor (p1 xor p2) == (p2 xor r)

So if you can guess any part of the plaintext p1, you can replace it with
alternative plaintext p2 without needing to decrypt/encrypt or knowing
anything about the pad r.

Also, if this is for something important, I'd be concerned about how to
protect the key. That's hard enough to do in C, let alone in Python.

> It also looks rather slow.

Any kind of bulk binary data processing in pure Python is slow. The code
was written mainly for simplicity, e.g. using generators means that you
don't have to deal with buffer sizes. Replacing " % 256" with " & 255"
might be worthwhile.

> I don't make
> any guarantees about p3.py, but it has been reviewed by several experts
> and appears to be reasonably sound for the type of casual use being
> discussed here, and it is tuned for speed (given the implementation
> constraints).  For more demanding purposes, you should use a more
> serious library like one of the OpenSSL wrappers.

The OP specifically wanted to avoid third-party libraries.

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


Re: lightweight encryption of text file

2010-01-10 Thread Nobody
On Sun, 10 Jan 2010 12:26:05 -0800, Paul Rubin wrote:

> I'd like it a lot if the Python stdlib could include a serious
> cryptography module.

And I'd like a truckload of gold ;)

Right now, even asking for HTTPS support is too much to ask. Heck,
even asking for the fake HTTPS support to be identified as such is too
much, apparently.

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


Re: lightweight encryption of text file

2010-01-10 Thread Steve Holden
Nobody wrote:
> On Sun, 10 Jan 2010 12:26:05 -0800, Paul Rubin wrote:
> 
>> I'd like it a lot if the Python stdlib could include a serious
>> cryptography module.
> 
> And I'd like a truckload of gold ;)
> 
> Right now, even asking for HTTPS support is too much to ask. Heck,
> even asking for the fake HTTPS support to be identified as such is too
> much, apparently.
> 
No, Paul, nobody will complain if you *ask* ...

A question I've been asking myself quite a lot recently is how the PSF
could, were funding to be available, direct the development of Python in
specific directions, and what those directions should be. Unfortunately
there are probably as many answers as Python programmers in terms of the
priorities to be adopted.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: Fractional Hours from datetime?

2010-01-10 Thread Austyn
How about:

import time
arizona_utc_offset = -7.00
h = (time.time() / 3600 + arizona_utc_offset) % 24

dt.timetuple()[6] is the day of the week; struct tm_time doesn't
include a sub-second field.

On Jan 10, 10:28 am, "W. eWatson"  wrote:
> Maybe there's a more elegant way to do this. I want to express the
> result of datetime.datetime.now() in fractional hours.
>
> Here's one way.
>
> dt=datetime.datetime.now()
> xtup = dt.timetuple()
> h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6
> #  now is in fractions of an hour

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


Re: interactive terminal in Ubuntu Linux : libreadline5-dev works only in Python 2.6 not 3.1

2010-01-10 Thread Dave WB3DWE
On Sat, 9 Jan 2010 16:48:52 -0800 (PST), casevh 
wrote:

>On Jan 9, 3:10 pm, pdlem...@earthlink.net wrote:
>> On Sat, 9 Jan 2010 13:27:07 -0800 (PST), casevh 
>> wrote:
>
>1) Try the commands again. Make sure all the "./configure" options are
>on one line. Make sure to do "sudo make altinstall". (Don't use "sudo
>make install"; it will give your version of Python the name "python"
>and that can cause confusion on your system.)
>
>2) Move your applications to another directory.
>
>3) Try running "python3.1" while you are in that directory.
>
>If this doesn't work, report back on the error messages you receive.
>
Thanks casevh for your time & patience.
The ./configure . . .  was a "continuous" line , although it ran over to
next line even on 132 char wide terminal because of names of system &
dir.  I was careful with the spaces and hyphens.
In my reply the "make install" was a typo , I did run  make altinstall.

Moved all my code to pycode dir on my home directory.  Removed all
files from /usr/local/lib/python3.1/dlmodules and removed that dir. 

Twice ran the recompile :
make distclean
./configure --prefix=/usr/local --with-computed-gotos
--with-wide-unicode
<^ one space>
make
sudo make altinstall 
After each reinstall had same problems :  cannot import  random  or
any code using it, although random.py is in Lib :
Traceback
File "", line 1, in 
File "random.py", line 46, in 
import collections as _collections
File "collections.py", line 9 in 
from _collections import deque, default dict
ImportError: /usr/local/lib/python3.1/lib-dynload/collections.so:
undefined symbol: PyUnicodeUCS4_FromString

After second reinstall today I also found that a module importing
"time"  would not run.  Likewise could not import  time  at  >>> .
Same error, ending : undefined symbol: PyUnicode UCS4_FromString

And my original problem still there : fouled up keys in interactive
terminal. Seems minor now ; )   Should I try to remove everything
and reopen the tarball ?
Dave WB3DWE,  central Texas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fractional Hours from datetime?

2010-01-10 Thread Austyn
Here's an improvement in case you want your code to work outside of
Arizona:

from time import time, timezone
h = ((time() - timezone) / 3600) % 24

On Jan 10, 9:04 pm, Austyn  wrote:
> How about:
>
> import time
> arizona_utc_offset = -7.00
> h = (time.time() / 3600 + arizona_utc_offset) % 24
>
> dt.timetuple()[6] is the day of the week; struct tm_time doesn't
> include a sub-second field.
>
> On Jan 10, 10:28 am, "W. eWatson"  wrote:
>
>
>
> > Maybe there's a more elegant way to do this. I want to express the
> > result of datetime.datetime.now() in fractional hours.
>
> > Here's one way.
>
> > dt=datetime.datetime.now()
> > xtup = dt.timetuple()
> > h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6
> > #  now is in fractions of an hour

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


Re: lightweight encryption of text file

2010-01-10 Thread Paul Rubin
Steve Holden  writes:
>> Right now, even asking for HTTPS support is too much to ask. Heck,
>> even asking for the fake HTTPS support to be identified as such is too
>> much, apparently.
>> 
> No, Paul, nobody will complain if you *ask* ...

Er, that wasn't me...

> A question I've been asking myself quite a lot recently is how the PSF
> could, were funding to be available, direct the development of Python in
> specific directions,...

Crypto in the stdlib is not a matter of funding or (technical)
priorities.  It's a policy issue; the maintainers were (at least as of a
few years ago) concerned about legal restrictions on crypto in some
jurisdictions causing problems with distributing Python if it included
crypto.  I know that other systems (Java, Mozilla, etc) include crypto
but they may have had to jump through some hoops for that purpose.

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


Re: lightweight encryption of text file

2010-01-10 Thread Paul Rubin
Nobody  writes:

> But, yeah, the OP needs to be aware of the difference (and probably isn't,
> yet). So to take that a step further ...
> The key passed to arcfour.schedule() shouldn't be re-used
> If you need to verify the data, append a hash of the ciphertext ...
> If you want to encrypt multiple files with the same "password",
> generate a unique key by hashing a combination of the password and a
> random salt (e.g. from /dev/random)...

Yep, a whole lot of stuff that's easy to get wrong if it's left to the
user (for example, you mean "a MAC of the ciphertext", not "a hash of
the ciphertext"), and a lot of extra work even if the user gets it
right.  It's simplest for the library to provide a single interface that
does all of it.

>> It also looks rather slow.
>
> Any kind of bulk binary data processing in pure Python is slow. 

Well, slow is a relative term, but p3.py is about 5x faster than the
fastest pure-Python rc4 implementation that I compared it to.  Its
heavy lifting is done by the SHA and array modules, that are written in C.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py2exe "for loop" hangs in compiled program

2010-01-10 Thread Aahz
In article ,
p_tierchen  <1...@sms.at> wrote:
>
>the application is an interface to a sqlite database and stores image
>metadata (such as year, event, photographer, people on image etc.). i
>use pyqt4 for the interface and developed this application on a linux
>platform (python 2.5.4). friends of mine liked what i have done an want
>it for their windows computers. so i setup a test system (win XP and
>python 2.6.?). copying the code to this system and starting it: works
>as intended. however they complain python whats this, this is sooo
>complicated etc. so i took refuge in using py2exe, planing to give them
>a zip compressed archive ... testing this option i found out that on
>the surface all looks nice (GUI and functions etc) however: extending
>to "larger" datasets (>~50 images) some of the for loops stop after
>some time.

https://lists.sourceforge.net/lists/listinfo/py2exe-users
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur."  --Red Adair
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to validate the __init__ parameters

2010-01-10 Thread Aahz
In article ,
Jean-Michel Pichavant   wrote:
>
>class A:
>def __init__(self, foo = None, bar = None):
>if len(foo) > 5:
>   raise ValueError('foo cannot exceed 5 characters')

Bad Idea -- what happens when foo is None?
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur."  --Red Adair
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lightweight encryption of text file

2010-01-10 Thread Steve Holden
Paul Rubin wrote:
> Steve Holden  writes:
>>> Right now, even asking for HTTPS support is too much to ask. Heck,
>>> even asking for the fake HTTPS support to be identified as such is too
>>> much, apparently.
>>>
>> No, Paul, nobody will complain if you *ask* ...
> 
> Er, that wasn't me...
> 
Oh sorry, no more it was.

>> A question I've been asking myself quite a lot recently is how the PSF
>> could, were funding to be available, direct the development of Python in
>> specific directions,...
> 
> Crypto in the stdlib is not a matter of funding or (technical)
> priorities.  It's a policy issue; the maintainers were (at least as of a
> few years ago) concerned about legal restrictions on crypto in some
> jurisdictions causing problems with distributing Python if it included
> crypto.  I know that other systems (Java, Mozilla, etc) include crypto
> but they may have had to jump through some hoops for that purpose.
> 
There are administrative hoops to jump through still in the US to export
cryptographic code. Further, if the standard distribution were to
include it then the requirements of the US government do still come into
play, and it would be a pain to have to have people downloading the
software certify (for example) that they weren't intending to re-export
it to a "prohibited" country.

We can get around some of these issues by retaining the hosting on
mainland Europe rather than in the USA, but these issues do demand
careful study which apparently nobody really has time for at present.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: lightweight encryption of text file

2010-01-10 Thread Carl Banks
On Jan 8, 11:14 am, Daniel Fetchinson 
wrote:
> I have a plain text file which I would like to protect in a very
> simple minded, yet for my purposes sufficient, way. I'd like to
> encrypt/convert it into a binary file in such a way that possession of
> a password allows anyone to convert it back into the original text
> file while not possessing the password one would only see the
> following with the standard linux utility 'file':
>
> [fetchin...@fetch ~]$ file encrypted.data
> encrypted.data: data
>
> and the effort required to convert the file back to the original text
> file without the password would be equivalent to guessing the
> password.


gpg -c simpletextfile.txt -o simpletextfile.gpg

But I guess you can't depend on users to have gpg installed so you
have to roll out some unvetted Python tool.


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


Re: lightweight encryption of text file

2010-01-10 Thread geremy condra
On Sun, Jan 10, 2010 at 3:26 PM, Paul Rubin  wrote:
> geremy condra  writes:
>> Not sure why in the world you would homebrew something like this- a
>> small dependency isn't that bad, and aes can be pretty simple to use.
>> Might as well go for the industrial strength approach.
>
> In my experience, 1) small dependencies ARE that bad, since they mean
> you have to develop and test on every platform that you want your code
> to run on;

And having no dependencies frees you from the burden of testing
where your software will be deployed? I don't think so.

> 2) using a serious library requires quite a bit of knowledge
> and decision-making which not everyone is equipped to do.

Homebrewing is not a good solution to the problem of being
ignorant of modern cryptography.

> "AES" is not so simple to use unless you know what you're doing in
> terms of modes, nonces, etc.

Seems pretty simple to me- use AES 192, don't use ECB mode, and
use your library of choice's key strengthening utilities. Even blatantly
ignoring that advice would still probably give you better results than
homebrewing though, so I don't really see the issue here.

> Having supported this kind of package in a commercial
> setting in the past, IMO, for the sort of (common) application in
> question, it's best to keep things as simple as possible and supply a
> single interface that provides encryption, authentication, and random
> initialization all in one call.  The cost is a little bit of ciphertext
> bloat, but it prevents all kinds of security failures frequently
> overlooked by novices.
>
> I'd like it a lot if the Python stdlib could include a serious
> cryptography module.  That was rejected for regulatory reasons several
> years ago, but maybe things are changing enough that the issue can be
> revisited sometime.

I agree. I inquired about it not too long ago on python-ideas; little
serious discussion ensued.

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


Re: lightweight encryption of text file

2010-01-10 Thread Steve Holden
Carl Banks wrote:
> On Jan 8, 11:14 am, Daniel Fetchinson 
> wrote:
>> I have a plain text file which I would like to protect in a very
>> simple minded, yet for my purposes sufficient, way. I'd like to
>> encrypt/convert it into a binary file in such a way that possession of
>> a password allows anyone to convert it back into the original text
>> file while not possessing the password one would only see the
>> following with the standard linux utility 'file':
>>
>> [fetchin...@fetch ~]$ file encrypted.data
>> encrypted.data: data
>>
>> and the effort required to convert the file back to the original text
>> file without the password would be equivalent to guessing the
>> password.
> 
> 
> gpg -c simpletextfile.txt -o simpletextfile.gpg
> 
> But I guess you can't depend on users to have gpg installed so you
> have to roll out some unvetted Python tool.
> 
> 
The OP's statement of requirements would be pretty much satisfied by the
"crypt" utility. He can even run it under Cygwin on Windows if
necessary. Cryptographic sophistication (or even cryptographic security)
was not requested (and would not be provided anyway by most of the
suggested solutions).

If any real protection is required then an ad-hoc program is definitely
not the way to provide it.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: interactive terminal in Ubuntu Linux : libreadline5-dev works only in Python 2.6 not 3.1

2010-01-10 Thread casevh
On Jan 10, 8:16 pm, Dave WB3DWE wrote:
> On Sat, 9 Jan 2010 16:48:52 -0800 (PST), casevh 
> wrote:
>
> >On Jan 9, 3:10 pm, pdlem...@earthlink.net wrote:
> >> On Sat, 9 Jan 2010 13:27:07 -0800 (PST), casevh 
> >> wrote:
>
> >1) Try the commands again. Make sure all the "./configure" options are
> >on one line. Make sure to do "sudo make altinstall". (Don't use "sudo
> >make install"; it will give your version of Python the name "python"
> >and that can cause confusion on your system.)
>
> >2) Move your applications to another directory.
>
> >3) Try running "python3.1" while you are in that directory.
>
> >If this doesn't work, report back on the error messages you receive.
>
> Thanks casevh for your time & patience.
> The ./configure . . .  was a "continuous" line , although it ran over to
> next line even on 132 char wide terminal because of names of system &
> dir.  I was careful with the spaces and hyphens.
> In my reply the "make install" was a typo , I did run  make altinstall.
>
> Moved all my code to pycode dir on my home directory.  Removed all
> files from /usr/local/lib/python3.1/dlmodules and removed that dir.
>
> Twice ran the recompile :
>     make distclean
>     ./configure --prefix=/usr/local --with-computed-gotos
>             --with-wide-unicode
>         <^ one space>
>     make
>     sudo make altinstall
> After each reinstall had same problems :  cannot import  random  or
> any code using it, although random.py is in Lib :
>     Traceback
>         File "", line 1, in 
>         File "random.py", line 46, in 
>             import collections as _collections
>         File "collections.py", line 9 in 
>             from _collections import deque, default dict
>     ImportError: /usr/local/lib/python3.1/lib-dynload/collections.so:
>         undefined symbol: PyUnicodeUCS4_FromString
>
> After second reinstall today I also found that a module importing
>     "time"  would not run.  Likewise could not import  time  at  >>> .
> Same error, ending : undefined symbol: PyUnicode UCS4_FromString
>
> And my original problem still there : fouled up keys in interactive
> terminal. Seems minor now ; )   Should I try to remove everything
> and reopen the tarball ?
> Dave WB3DWE,  central Texas

Are you sure you are using the new version of python3.1 (the one
located in /usr/local/bin/)?

What is the result of "which python3.1"?

What happens if you run "/usr/local/bin/python3.1"?

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


how to duplicate array entries

2010-01-10 Thread Sebastian
Hi there,

I have an array  x=[1,2,3]

Is there an operator which I can use to get the result
[1,1,1,2,2,2,3,3,3] ?

I tried x*3, which resulted in [1,2,3,1,2,3,1,2,3]
I also tried [[b,b,b] for b in x] which led to [[1,2,3],[1,2,3],
[1,2,3]], but this isn't what I want either.

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


Re: how to duplicate array entries

2010-01-10 Thread Sebastian
On Jan 11, 4:21 pm, Sebastian  wrote:

> I also tried [[b,b,b] for b in x] which led to [[1,2,3],[1,2,3],
> [1,2,3]]

Sorry, I have to correct myself. The quoted line above resulted in
[[1,1,1],[2,2,2],[3,3,3]] of course!

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


Re: how to duplicate array entries

2010-01-10 Thread Chris Rebert
On Sun, Jan 10, 2010 at 10:21 PM, Sebastian  wrote:
> Hi there,
>
> I have an array  x=[1,2,3]
>
> Is there an operator which I can use to get the result
> [1,1,1,2,2,2,3,3,3] ?
>
> I tried x*3, which resulted in [1,2,3,1,2,3,1,2,3]
> I also tried [[b,b,b] for b in x] which led to [[1,2,3],[1,2,3],
> [1,2,3]], but this isn't what I want either.

from itertools import chain, repeat
n = 3
stretched = list(chain(*[repeat(item, n) for item in x]))

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


Re: how to duplicate array entries

2010-01-10 Thread Paul Rudin
Sebastian  writes:

> Hi there,
>
> I have an array  x=[1,2,3]

In python such an object is called a "list".

(In cpython it's implemented as an automatically resizable array.)

>
> Is there an operator which I can use to get the result
> [1,1,1,2,2,2,3,3,3] ?

There's no operator that will give you that directly - but there are
plenty of one-liners that will yield that list.
e.g:

>>> list(itertools.chain(*([x]*3 for x in [1,2,3])))
[1, 1, 1, 2, 2, 2, 3, 3, 3]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: integer and string compare, is that correct?

2010-01-10 Thread Dan Bishop
On Jan 10, 10:34 am, Nobody  wrote:
> Hellmut Weber wrote:
> >> being a causal python user (who likes the language quite a lot)
> >> it took me a while to realize the following:
> >>  >>> max = '5'
> >>  >>> n = 5
> >>  >>> n >= max
> >> False
>
> >> Section 5.9 Comparison describes this.
>
> >> Can someone give me examples of use cases
> Peter Otten wrote:
> > The use cases for an order that works across types like int and str are weak
> > to non-existent. Implementing it was considered a mistake and has been fixed
> > in Python 3:
>  5 > "5"
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > TypeError: unorderable types: int() > str()
>
> If you actually need to perform comparisons across types, you can rely
> upon the fact that tuple comparisons are non-strict and use e.g.:
>
>         > a = 5
>         > b = '5'
>         > (type(a).__name__, a) < (type(b).__name__, b)
>         True
>         > (type(a).__name__, a) > (type(b).__name__, b)
>         False
>
> The second elements will only be compared if the first elements are equal
> (i.e. the values have the same type).

But this method gives you 3.0 < 2 because 'float' < 'int'.  Probably
not what you want.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to duplicate array entries

2010-01-10 Thread Gary Herron

Paul Rudin wrote:

Sebastian  writes:

  

Hi there,

I have an array  x=[1,2,3]



In python such an object is called a "list".

(In cpython it's implemented as an automatically resizable array.)

  

Is there an operator which I can use to get the result
[1,1,1,2,2,2,3,3,3] ?



There's no operator that will give you that directly - but there are
plenty of one-liners that will yield that list.
e.g:

  

list(itertools.chain(*([x]*3 for x in [1,2,3])))


[1, 1, 1, 2, 2, 2, 3, 3, 3]
  


List comprehension also works nicely for this problem, and may be 
clearer to some.


>>> x = [1,2,3]
>>> print [ifor i in xfor k in range(3)]
[1, 1, 1, 2, 2, 2, 3, 3, 3]

Gary Herron



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


Re: how to duplicate array entries

2010-01-10 Thread Steven D'Aprano
On Sun, 10 Jan 2010 22:21:54 -0800, Sebastian wrote:

> Hi there,
> 
> I have an array  x=[1,2,3]

You have a list. Python has an array type, but you have to "import array" 
to use it.


> Is there an operator which I can use to get the result
> [1,1,1,2,2,2,3,3,3] ?

Not an operator, but you can do it easily with a function. Here's the 
simple version:

>>> def duplicate(items, count):
... L = []
... for item in items:
... L.extend([item]*count)
... return L
...
>>> duplicate([1,2,3], 3)
[1, 1, 1, 2, 2, 2, 3, 3, 3]



Here's a version which is short and simple enough to use in-line, but 
will be slow for large lists:


>>> x = [1,2,3]
>>> count = 3
>>> sum([[item]*count for item in x], [])
[1, 1, 1, 2, 2, 2, 3, 3, 3]


Finally, here's a nasty hack that you should never, ever, ever use for 
any reason except to win a bet:


>>> [locals()['_[1]'].extend([item]*(count-1)) or item for item in x]
[1, 1, 1, 2, 2, 2, 3, 3, 3]



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


Re: "Advanced" Python programming book?

2010-01-10 Thread Marco Salden
On Jan 10, 2:35 pm, flow  wrote:
> I've just finished reading a sort of beginner Python book, and I know
> quite a bit now but I'm looking for a book that can teach me advanced
> aspects of Python - code optimisation, threading, etc.
>
> Any recommendations?
>
> Cheers.

I like to add this one, which is nice to read just after reading the
"starters", covers all kinds of topics, and written by someone who
knows Python (I think at least).
http://oreilly.com/catalog/9780596009250/

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


Re: class viewer ?

2010-01-10 Thread Ben Finney
Stef Mientki  writes:

> from the given class, it's ancestors and it's derived classes,
> I'ld like to get the following information in a tree like structure:
>
> - the file were the class is definied
> - the attributes, split in inherited / created / overriden
> - the methodes, split in inherited / created / overriden
> - the files were instances of the class are created
> - and probably I forget a few
>
> any suggestions ?

The “Emacs Code Browser” http://ecb.sourceforge.net/> was seemingly
designed for languages like C++ and Java, but it serves well enough for
Python also.

I find it annoying to use, since it seems to assume mouse navigation and
has rather impenetrable (for me) documentation. But it does seem to do
much of what you're asking for, and a big plus is that it's not specific
to Python.

-- 
 \  “The most common way people give up their power is by thinking |
  `\   they don't have any.” —Alice Walker |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list