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

2010-01-09 Thread Nobody
On Wed, 06 Jan 2010 11:39:37 -0800, Steven K. Wong wrote:

> Suppose now all the prog1.poll() calls/loop are replaced by a single
> prog1.wait(). Without the explicit prog1.stdout.close(), prog1.wait()
> will not return, so the calling process still hangs. Because calling
> prog1.wait() means that the calling process will naturally never read
> prog1.stdout, I would argue that prog1.wait() should close the pipe
> before actually waiting for prog1 to exit. Makes sense?

prog1.stdout might be being read by a different thread.

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


Re: PIL how to display multiple images side by side

2010-01-09 Thread Alf P. Steinbach

* Lie Ryan:

On 1/9/2010 8:43 AM, suresh.amritapuri wrote:

Hi,

In PIL, how to display multiple images in say m rows and n colums when
I have m*n images.

suresh


Tkinter has PhotoImage widget and PIL has support for this widget:
http://www.pythonware.com/library/pil/handbook/imagetk.htm


Maybe I've misunderstood something (in that case glad to learn!), but I believe 
PhotoImage is not a widget, and that a PhotoImage has to be presented in e.g. a 
Label widget or some other widget that's able to display images.



Cheers,

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


Re: PIL how to display multiple images side by side

2010-01-09 Thread Lie Ryan

On 1/9/2010 8:43 AM, suresh.amritapuri wrote:

Hi,

In PIL, how to display multiple images in say m rows and n colums when
I have m*n images.

suresh


Tkinter has PhotoImage widget and PIL has support for this widget:
http://www.pythonware.com/library/pil/handbook/imagetk.htm


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


Re: Prepend to logging message

2010-01-09 Thread Ishwor Gurung
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-information-to-your-logging-output
--
Regards
Ishwor Gurung
Key id:0xa98db35e
Key fingerprint:FBEF 0D69 6DE1 C72B A5A8  35FE 5A9B F3BB 4E5E 17B5
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Office Word and Python (Win XP)

2010-01-09 Thread David Monaghan
On Sat, 9 Jan 2010 11:18:12 -0800 (PST), "3lvss0...@gmail.com"
<3lvss0...@gmail.com> wrote:

>Dennis Lee Bieber: Im not familiar with python, also Im not
>programmer.

What you want to do isn't complicated, but it isn't simple either, unless
you're familiar with VBA/VBS. I approach these problems by first getting the
VBA code by recording a macro within Word. I then convert it to VB Script,
which is a learning process in itself. Converting that script to Python com
is another learning process and then putting the whole thing together with
file finding and saving is another job.

When you've done that, you won't feel able to say you're not a programmer -
and you should feel familiar with Python, too.

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


Prepend to logging message

2010-01-09 Thread 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())
--


[1] http://docs.python.org/library/logging.html#logging.LoggerAdapter
-- 
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-09 Thread casevh
On Jan 9, 3:10 pm, pdlem...@earthlink.net wrote:
> On Sat, 9 Jan 2010 13:27:07 -0800 (PST), casevh 
> wrote:
> >Did you recompile Python 3.1.1 after installing libreadline5-dev?
>
> >(From the Python 3.1.1 directory. Your options to configure may vary.)
>
> >make distclean
> >./configure --prefix=/usr/local --with-computed-gotos --with-wide-
> >unicode
> >make
> >make altinstall
>
> >casevh
>
> Thanks so much for your help . . . but I'm going backwards, probably
> due to my inadequate knowledge of Linux.
>
> Ran the above vebatim, except had to do      sudo make install    .
> Python recompiled and the system appears to be in same path/dir as
> before :  /home/dave/python31/Python-3.1.1
> Called up interactive prompt  >>>  with  $ python3 , as before.
>

It looks like you now have two copies of Python 3.1.1 installed.
That's probably a side-effect of my instructions. I'll got through
each instruction individually.

It looks like your Python source code is in "/home/dave/python31/
Python-3.1.1". The commnad "make distclean" should remove the results
of the prior configuration so to won't need to extract the source code
again.

The command "./configure" accepts several options that control how the
source code is configured. To see all the available options, use the
command "./configure --help". The first option - "--prefix=/usr/local"
- identifies the location where Python 3.1.1 will be installed. The "/
usr/local" directory is a common location for user-compiled programs.
If no location is specified, many applications assume "/usr/local".
The option - "--with-computed-gotos" - is just a compiler options that
produces a slightly faster interpreter. The final option - "--with-
wide-unicode" - identifies the proper Unicode format to use. (I looks
like this option may have been split via line wrapping on my earlier
email. I think it is causing the UCS4 error.)

The command "make" compiles Python 3.1.1.

The command "sudo make altinstall" installs Python 3.1.1 under the "/
usr/local" directory. The option "altinstall" installs Python 3.1.1
and leaves its name as "python3.1". It should be located in "/usr/
local/bin/python3.1".

Your own programs (Ackermann.py) should be kept somewhere in your home
directory, for example "/home/dave/code". Typing "python3.1" will look
in the current directory first, and then search the path where it
should find "/usr/local/bin/python3.1".

What will you need to do fix this?

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.

Don't give up; we can fix this.

casevh
> Problems :
>     1. prompt keys remain fouled up as before.
>     2. will not import  random  module, either from prompt or
>             from any of my prewritten modules.  Get ImportError
>            /usr/local/lib/Python3.1/lib-dynload/_collections.so :
>             undefined symbol : PyUnicode UCS4_FromString
>                 Some other modules will import : math , sys, os
>     3. Some of my pre-existing modules will not run : have
>           not checked them all, but apparently those with random.
>     4. Attempted to read my modules with gedit.  Pulls them
>           up but refuses to save :          could not save file
>           /usr/local/lib/python3.1/dlmodules/Ackermann.py
>                You do not have permission necessary to save file.
>           This is same path my modules were in before the
>           recompile and the dir I made for them. Had no trouble
>           saving them previously.
>
> If no fix available, I'll reinstall from the Python-3.1.1 that
> I extracted from the tarball , and go back to XP for a while  : (
>
> Dave WB3DWE       pdlem...@earthlink.net

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


Re: Microsoft Office Word and Python (Win XP)

2010-01-09 Thread 3lvss0...@gmail.com
Marco did you also make the HD files to it worked for you? Because I
cannot even imagine how only three lines would do everything - find
correct folder (path) of the files, find maximum number, move the
entire text,... In this 3 lines is not stated that we are talking
about the files that start with "HD" so I wonder how it worked for
you. Nothing was moved on my PC. I have done some research about the
line "app = Dispatch("Word.Application")" and saw this:

http://www.programmingforums.org/post105986.html

The script described here is different comparing to what I want to do
but I might be able to use some ideas - of course, since Im not a
programmer, I need to study the functions before. On the link the user
is trying to replace parts (predefinited or not - i don't know) of
the .doc files - this is not moving/copying entire text but the topic
is still about writing something into .doc through python.
Unfortunatelly for me, his script doesn't need to search for
correct .doc file.

So I kept searching and came to this:

http://www.daniweb.com/forums/thread129924.html

This task is very close to what I want but still different. If we try
to compare;

"I am a text" (his task) = already exsisting (if any) text in my HDX
file where X is the highest number (my task)
"Hello" (his task) = whole text inside .doc with always the same name
on the same location (my task)

then thats it. I believe just those two differences exsist.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Link to module Stack

2010-01-09 Thread Dave Angel

Steven D'Aprano wrote:

On Sat, 09 Jan 2010 05:56:36 -0500, Dave Angel wrote:

  

"InnerInterpreterError" is the most inappropriate exception name I've
ever seen. It has nothing to do with the interpreter, it's a stack
error.


  

It has everything to do with the (Forth) interpreter.  Exceptions can
readily be named according to their application -- it's not always about
Python.  Anyway, Forth has an inner-interpreter and an
outer-interpreter, and the name will make sense to a Forth programmer.



Pardon me, but I *am* a Forth programmer. Or was, it's been many years, 
and I'm rusty. I guess this is a difference of terminology: what you're 
calling an inner interpreter and an outer interpreter I know of as the 
Forth engine and the (text) interpreter. Gforth refers to them as such, 
so did Leo Brodie's Forth books, and the (ancient) Macintosh Forth 
compiler "Mach 2".


  
I'm pretty sure FIG-Forth called them an inner interpreter and outer 
interpreter, but I don't remember other sources.  FIG-Forth was my first 
Forth system, gotten on an 8" diskette.  The inner interpreter was  
LOADSW, JMP AX, I believe, as it was an indirected threaded interpreter 
implementation.




  

or even better, without the extra local var:

def pop (self):
if len(self.__heap) == 0:
raise InnerInterpreterError, "stack underflow"
return self.__heap.pop(1)



pop(1)? I don't think so.

  

That was a typo;  I meant  pop().  And of course others have improved on 
my remarks anyway.


DaveA
--
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-09 Thread pdlemper
On Sat, 9 Jan 2010 13:27:07 -0800 (PST), casevh 
wrote:

>
>Did you recompile Python 3.1.1 after installing libreadline5-dev?
>
>(From the Python 3.1.1 directory. Your options to configure may vary.)
>
>make distclean
>./configure --prefix=/usr/local --with-computed-gotos --with-wide-
>unicode
>make
>make altinstall
>
>casevh

Thanks so much for your help . . . but I'm going backwards, probably
due to my inadequate knowledge of Linux.

Ran the above vebatim, except had to do  sudo make install.
Python recompiled and the system appears to be in same path/dir as
before :  /home/dave/python31/Python-3.1.1
Called up interactive prompt  >>>  with  $ python3 , as before.

Problems :
1. prompt keys remain fouled up as before.
2. will not import  random  module, either from prompt or
from any of my prewritten modules.  Get ImportError 
   /usr/local/lib/Python3.1/lib-dynload/_collections.so :
undefined symbol : PyUnicode UCS4_FromString
Some other modules will import : math , sys, os
3. Some of my pre-existing modules will not run : have
  not checked them all, but apparently those with random.
4. Attempted to read my modules with gedit.  Pulls them
  up but refuses to save :  could not save file
  /usr/local/lib/python3.1/dlmodules/Ackermann.py
   You do not have permission necessary to save file.
  This is same path my modules were in before the
  recompile and the dir I made for them. Had no trouble 
  saving them previously.

If no fix available, I'll reinstall from the Python-3.1.1 that 
I extracted from the tarball , and go back to XP for a while  : (

Dave WB3DWE   pdlem...@earthlink.net 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python-list Digest, Vol 76, Issue 97

2010-01-09 Thread Mitchell L Model

On Jan 8, 2010, at 7:35:39 PM EST, Terry Reedy  wrote:


On 1/8/2010 12:02 PM, Mitchell L Model wrote:



On further reflection, I will add that
what appears to be happening is that during import both the global  
and
local dictionaries are set to a copy of the globals() from the  
importing

scope and that copy becomes the value of the module's __dict__ once
import has completed successfully.


I have no idea why you think that. The module dict starts empty  
except for __name__, __file__, and perhaps a couple of other  
'hidden' items. It is not a copy and has nothing to do with  
importing scopes.


Why I think -- or, rather, thought -- that was because of some  
defective experiments I ran. It was purely a delusion. Thank you for  
correcting it.




> and that copy becomes the value of the module's __dict__ once
> import has completed successfully.

That new dict becomes  .


Because exec leaves locals() and globals() distinct,


Not necessarily.

In 3.x, at least,
exec(s)
executes s in the current scope. If this is top level, where locals  
is globals, then same should be true within exec.


Yes. To simplify some of my ramblings and incorporate the points you  
and others have made, and to once again acknowledge Python's elegance,  
an important observation which I bet even a lot of serious Python  
programs don't realize (or at least not consciously) is that:

globals() is locals()
in the following contexts:
the interpreter top level
	the top level of a module (though as you point out, starts out as a  
very bare dictionary during import)

a string being exec'd when the call to exec includes
no dictionary argument(s)
one dictionary argument
the same dictionary as both the second and third arguments
The identity does not hold for:
	a string being exec'd when a different dictionary is provided as the  
second and third arguments to exec
	inside anything that creates a scope: a function definition, class  
definition, etc.


Did I get all that right? Are there any other contexts that should be  
included in these?




d = {}
exec(s, d)

In 3.x, at least, d will also be used as locals.


Yes, talking about 3.x.



exec(s, d, d)

Again, globals and locals are not distinct.

It would seem that in 3.x, the only way for exec to have distinct  
globals and locals is to call exec(s) where they are distinct or to  
pass distince globals and locals.


Apparently so. To clarify "where they are distinct", that would mean  
from a context in which they were already distinct, which is not the  
case if exec is called from the top level, but is the case if called  
from within, say, a function, as my code does.





Some of the issues of this thread are discussed in Language  
Reference 4.1, Naming and Binding. I suppose it could be clearer  
that it is, but the addition of nonlocal scope complicated things.



I pretty much have that section memorized and reread it at least  
monthly. It's part of what I meant by starting my original comments by  
saying that I thought I understood all of this. Thank you (and others)  
for helping clarify exactly what's going on. As with so many things in  
Python, it is not always easy to keep one's preconceptions, delusions,  
and experiences with other languages out of the way of its simplicity,  
even if one is a very experienced and knowledgeable Python programmer.


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


Re: Append to an Excel file

2010-01-09 Thread Niels L. Ellegaard
pp  writes:

> On Jan 9, 1:47 am, Jason Scheirer  wrote:
>> On Jan 9, 12:30 am, pp  wrote:
>>
>> > Hi All,
>>
>> > How do I add a line to an existing file. This should append to the
>> > existing data in the excel file, which was saved previously.
>>
>> > Thanks,
>> > PP
>>
>> http://pypi.python.org/pypi/xlwt
>
> Hi Jason and all,
>
> Thanks
>
> I have seen this.. my question is there a  way to append to a excel
> file which has been closed. Any specific modes which can be added to
> the sheet so that it adds a line to the data which was return in some
> earlier running of the program.

I may be wrong, but I think that you have to do the following

1) Use xlrd to read the file. This creates an xlrd.Book
2) Use xlutils to transform the xlrd.Book into a xlwt.WorkBook
3) Edit the xlwt.WorkBook
4) Save the xlwt.WorkBook

https://secure.simplistix.co.uk/svn/xlutils/trunk/xlutils/docs/copy.txt

  Niels



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


Re: [Python-Dev] [RELEASED] Python 2.7 alpha 2

2010-01-09 Thread Jan Kaliszewski

09-01-2010 o 22:34:28 Jack Diederich  wrote:


On Sat, Jan 9, 2010 at 2:53 PM, Jan Kaliszewski 


I have a question: are class decorator planned to be backported from  
3.x?



Eh? Class decorators have been in the 2.x series since 2.6.


Oops, I overlooked the fact :) Thank you!

*j

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Office Word and Python (Win XP)

2010-01-09 Thread Marco Nawijn
On Jan 9, 8:18 pm, "3lvss0...@gmail.com" <3lvss0...@gmail.com> wrote:
> Marco Nawijn: I have had installed pywin32 already. The three lines
> that you mentoined don't do this
I checked at my own computer and it works fine.
> also what did you mean with "doc =
> app.Documents.Open("c:\\example.doc")". Which document should I open
> with this line?
This was just meant as an example on how to open a Word document from
within python. This would be the basis for copying/appending from your
source
document to your target document (e.g. HD10.doc).
> It shouldn't be opened anything. I was asking about
> the script as automated process. If you know how could i do this?
Well it is a python script. So you can run it as an automated process.
You should just set app.Visible=False so the Word user interface
component
is not shown.

>
> Terry Reedy: I have never mentoined copying files but moving the whole
> text from, always the same (same name, same path), .doc file with.
> However copying (=moving) text to correct .doc file would be good yes.
> I know command prompt and its copy function but this way it wouldn't
> work because I would have to define the target file - the file INTO
> which I want to move the text. But I will never know the file name
> (target file). The only thing I know is:
> - the file is one of .doc files that start with "HD"
> - whole name of those .doc file is always HDX.doc where X is a number
> and I need to move the text into the file with maximum X (the most
> high number)
> - all the HD files will be always in the same path (same folder) but I
> would like to use the path inside the code (it might be obvious that I
> have to) because on PC and laptop, I have two different usernames and
> since HD files are located inside Documents And Settings, I have to
> use two copies of the script - one for PC, one for laptop.
>
> Dennis Lee Bieber: Im not familiar with python, also Im not
> programmer. Thats why Im not able to do so when people tell me "do
> this then use XYZ function which will give you ZYX from what you can
> do that and you will get result". Im still willing to learn but there
> are thousands of python tutorials and the one for exsactly this topic
> probably doesn't exsist. The .doc extension is required, so I cannot
> use .txt because I need the HD files in .doc.

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


Re: [Python-Dev] [RELEASED] Python 2.7 alpha 2

2010-01-09 Thread Jack Diederich
On Sat, Jan 9, 2010 at 2:53 PM, Jan Kaliszewski  wrote:
> Hello,
>
> I have a question: are class decorator planned to be backported from 3.x?
>
Eh? Class decorators have been in the 2.x series since 2.6.

If you want to know more about class decorators check out this talk
from PyCon 2009  http://pycon.blip.tv/file/1949345 by a very handsome
and well beloved man, and come to pycon 2010 where there will be a
couple more talks on the subject by slightly more homely but none the
less competent speakers.

-Jack
-- 
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-09 Thread casevh
On Jan 9, 10:06 am, Dave WB3DWE wrote:
> On Jan 6 I inquired how to "fix" the 3.1.1 interactive terminal
> in Ubuntu Linux.   Left arrow yields ^[[D , etc.
>
> casevh helped by suggesting "libreadline5-dev" be installed.
> Did so with Synaptic Package Manager.
> The behavior of the Python 3.3.1 terminal is unchanged but
> the 2.6.2 terminal is corrected.
>
> Is there any way to fix the 3.1.1 terminal command line ?
>
> Thanks,     Dave WB3DWE      pdlem...@earthlink.net

Did you recompile Python 3.1.1 after installing libreadline5-dev?

(From the Python 3.1.1 directory. Your options to configure may vary.)

make distclean
./configure --prefix=/usr/local --with-computed-gotos --with-wide-
unicode
make
make altinstall

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


Re: lightweight encryption of text file

2010-01-09 Thread Paul Rubin
Robert Kern  writes:
> Are you on a 64-bit platform? Unfortunately, array's integer typecodes
> are platform-specific, but p3.py requires a 32-bit integer ...

Oh yes, thanks, I never did get around to dealing with 64 bit platforms.
I also notice that some of the unit test functions use print statements,
which won't compile under Python 3.0.  I will try to release a version
in the next few days with fixes for both of these issues.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lightweight encryption of text file

2010-01-09 Thread Paul Rubin
Daniel Fetchinson  writes:
> I have a plain text file which I would like to protect in a very
> simple minded, yet for my purposes sufficient, way. 

For encrypting strings, use this module:

   http://nightsong.com/phr/crypto/p3.py

Obviously this is limited to strings that fit in memory, which
might be a problem with large files.   Some day I might get
around to adding a streaming interface to it.

The "file" command will not recognize the ciphertext as encrypted
data.  It will just say "data".  

If you want to be more serious, use pgp or gpg with the -c option
(password-based encryption).  I think "file" does recognize the pgp
file format as encrypted data (RFC 2440).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Link to module Stack

2010-01-09 Thread Steve Holden
Steven D'Aprano wrote:
> On Sat, 09 Jan 2010 05:56:36 -0500, Dave Angel wrote:
> 
>>> "InnerInterpreterError" is the most inappropriate exception name I've
>>> ever seen. It has nothing to do with the interpreter, it's a stack
>>> error.
>>>
>>>
>> It has everything to do with the (Forth) interpreter.  Exceptions can
>> readily be named according to their application -- it's not always about
>> Python.  Anyway, Forth has an inner-interpreter and an
>> outer-interpreter, and the name will make sense to a Forth programmer.
> 
> Pardon me, but I *am* a Forth programmer. Or was, it's been many years, 
> and I'm rusty. I guess this is a difference of terminology: what you're 
> calling an inner interpreter and an outer interpreter I know of as the 
> Forth engine and the (text) interpreter. Gforth refers to them as such, 
> so did Leo Brodie's Forth books, and the (ancient) Macintosh Forth 
> compiler "Mach 2".
> 
> But in any case... a stack is an general-purpose data structure, and the 
> error message shouldn't be coupled so tightly to one use. That would be 
> like this (made-up) example:
> 
 1/0
> Traceback (most recent call last):
>   File "", line 1, in 
> GraphicsApplicationError: too few pixels to calculate average
> 
> 
> Ridiculous, yes?
> 
> 
> Yes, Forth uses a stack (technically two, a parameter stack and a return 
> stack, and some implementations include a third, floating point, stack). 
> Virtually all languages use stacks in their implementation, and Python 
> byte-code is also stack-based.
> 
> 
 result = self.__heap[-1]
 del self.__heap[-1]
 
 
>>> That is better written as result = self.__heap.pop().
>>>
>>>
>>>
>> or even better, without the extra local var:
>>
>> def pop (self):
>> if len(self.__heap) == 0:

Since self.__heap is a list, the canonical Python for the above test
would, of course, be the much simpler

if not self.__heap

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-09 Thread Paul Rubin
Daniel Fetchinson  writes:
>>http://www.nightsong.com/phr/crypto/p3.py
>
> Thanks a lot, currently I'm having trouble using this code on python
> 2.6 but probably some small tweaking will fix it.

Yikes, this is the first I've heard of such a problem.  I will 
look into it.  Thanks.  (Also thanks to Robert for the recommendation).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Something More Elegant

2010-01-09 Thread Stephen Hansen
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 :) 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.

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.

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.
>

Your first goal needs to be in the layout of your tables, yes. Instead of
having multiple "Packages" tables that vary, have a single Packages table,
with an additional column that determines what kind of package it is (this
information used to be in the name of the table). This may seem like
everything's jumbled together, but it works. Put an index on that column if
you need to: don't worry about if that one table might have thousands or
tens of thousands of records. Databases are -designed- to handle that, and
handle it well and faster.

Do the same for any other table which has various names-- CategoriesPackages
seems to be another one. Segment the data by adding columns and indexes when
needed, and not by breaking it out into different tables: if two tables have
approximately the same columns, they belong in one table, with a column to
simply differentiate between the two.

These steps are taking your database towards the ultimate goal of
normalization-- a laudable goal, but I won't go into that in detail. It
takes a book.

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 user

Re: PIL show() not working for 2nd pic

2010-01-09 Thread Cousin Stanley

> I am using PIL for image processing in ubuntu 9.04. When i give two
> im.show() commands for two different images, the second image is not
> displayed (eye of gnome is the display program). It says no such file
> or directory. Any ideas?

  Suresh  

I also had problems with  show()  when using eye of gnome
as the image viewer with your code but no problems using 
the  display  viewer from the imagemagick package 

  im.show( command = 'eog' )#problems
  im.show( command = 'display' )# no problems

# --

#!/usr/bin/python

'''
NewsGroup  comp.lang.python
Subject .. PIL show() not working for 2nd pic
Date . 2010-01-07
Post_By .. suresh.amritapuri
Edit_By .. Stanley C. Kitching
'''

import math
import Image

list_source = [
'image/beach.tif' ,
'image/colors.tif' ]

list_target = [ ]

def neg( x ) :
return 255 - 1 - x

def logtr( x ) :
y = math.log( 1 + x , 10 )
print y
return y * 100

for this_image in list_source :
im_source  = Image.open( this_image )
im_neg = im_source.point( neg )
im_logtr   = im_source.point( logtr )

list_target.append( im_source )
list_target.append( im_neg )
list_target.append( im_logtr )

for this_image in list_target :
this_image.show( command = 'display' )

# --

-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona

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


Re: [Python-Dev] [RELEASED] Python 2.7 alpha 2

2010-01-09 Thread Jan Kaliszewski

Hello,

I have a question: are class decorator planned to be backported from 3.x?

All the best,
*j
--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Scripting (was Re: Python books, literature etc)

2010-01-09 Thread bartc


"Peter"  wrote in message 
news:mailman.661.1262978839.28905.python-l...@python.org...



Sounds good.

Regarding the book's title: is it just me, or are Python programmers
in general put off when people call it "scripting"?

I won't attempt a strict definition of the term "scripting language",
but it seems like non-programmers use it to mean "less scary than what
you might think of as programming", while programmers interpret it as
"not useful as a general-purpose language".


It took me a while to take "scripting" seriously. I grew up with Pascal 
and Eiffel and I found it difficult to appreciate dynamic typing and 
scripting. The author Langtangen is explaining in detail why he considers 
scripting useful, in particular he provides an automatic test suite to run 
different language versions ( perl, python, c, c++) of the same program to 
compare performance. The results are amazing, in that some of the examples 
run faster than the C++ version.


I think if you can get Python to run fast (compared to compiled languages), 
then that's scripting (ie. just using it to sequence lots of built-in 
functions and operations).


If it runs a lot slower than those other languages, then you're probably 
doing some programming.


And with programs where the runtime is not significant, it could be 
either...


--
Bartc 


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


Re: Microsoft Office Word and Python (Win XP)

2010-01-09 Thread 3lvss0...@gmail.com
Marco Nawijn: I have had installed pywin32 already. The three lines
that you mentoined don't do this, also what did you mean with "doc =
app.Documents.Open("c:\\example.doc")". Which document should I open
with this line? It shouldn't be opened anything. I was asking about
the script as automated process. If you know how could i do this?

Terry Reedy: I have never mentoined copying files but moving the whole
text from, always the same (same name, same path), .doc file with.
However copying (=moving) text to correct .doc file would be good yes.
I know command prompt and its copy function but this way it wouldn't
work because I would have to define the target file - the file INTO
which I want to move the text. But I will never know the file name
(target file). The only thing I know is:
- the file is one of .doc files that start with "HD"
- whole name of those .doc file is always HDX.doc where X is a number
and I need to move the text into the file with maximum X (the most
high number)
- all the HD files will be always in the same path (same folder) but I
would like to use the path inside the code (it might be obvious that I
have to) because on PC and laptop, I have two different usernames and
since HD files are located inside Documents And Settings, I have to
use two copies of the script - one for PC, one for laptop.

Dennis Lee Bieber: Im not familiar with python, also Im not
programmer. Thats why Im not able to do so when people tell me "do
this then use XYZ function which will give you ZYX from what you can
do that and you will get result". Im still willing to learn but there
are thousands of python tutorials and the one for exsactly this topic
probably doesn't exsist. The .doc extension is required, so I cannot
use .txt because I need the HD files in .doc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lightweight encryption of text file

2010-01-09 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':
>>
>> [fetchin...@fetch ~]$ file encrypted.data
>> encrypted.data: data
> [...]
>
> This is probably not what you want, but it is very simple and doesn't
> import any module:) I am not qualified to say how easy it is to discover
> the message without the password.
>
> def str2int(txt):
> return reduce(lambda n, c: n*255 + ord(c), txt, 0)
>
> def int2str(n):
> chars = []
> while n:
> n, o = divmod(n, 255)
> chars.append(chr(o))
> return ''.join(reversed(chars))
>
> def encrypt(txt, pwd):
> return int2str(str2int(txt)*str2int(pwd))
>
> def decrypt(txt, pwd):
> return int2str(str2int(txt)/str2int(pwd))
>
> def test(txt, pwd):
> encrypted_txt = encrypt(txt, pwd)
> decrypted_txt = decrypt(encrypted_txt, pwd)
> print "text:%r" % txt
> print "encrypted:%r" % encrypted_txt
> print "decrypted:%r" % decrypted_txt
>
>
 test("This encryption scheme is definitely unbreakable.", "secret")
> text:'This encryption scheme is definitely unbreakable.'
> encrypted:'&2\xa5\xd4\x17i+E\x01k\xfa\x94\xf80\xa8\x8f\xea.w\x128\xf1\xd9\x0f9\xf2t\xc9\r`\x90%\xd6\xf3~\x1f\x00%u&\x8a\xe4\xe0\xa7\xb8\xb0ec)S>\xcb\xf2>\xec'
> decrypted:'This encryption scheme is definitely unbreakable.'

Thanks, this looks pretty simple too, I will go with either Steven's
or with your solution.

Cheers,
Daniel



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


Re: Something More Elegant

2010-01-09 Thread Victor Subervi
On Sat, Jan 9, 2010 at 1:00 PM, Dennis Lee Bieber wrote:

> On Sat, 9 Jan 2010 10:28:31 -0500, Victor Subervi
>  declaimed the following in
> gmane.comp.python.general:
>
> > On Sat, Jan 9, 2010 at 10:14 AM, Iuri  wrote:
> >
> > > And you should use cursor.fetchall() instead of cursor in list
> > > comprehension:
> > >
> > > packageIDs = [itm[0] for itm in cursor.fetchall()]
> > >
> >
> > Now, someone else on this list told me the other. Can you explain the
> > difference?
>
> Since a (one-liner) list comprehension is going to result in
> processing all the data, it may be faster to fetch all the data at the
> start. Depending upon the implementation of the database API, iterating
> over a cursor object to retrieve one record at a time may result in a
> hit on the database engine (some engines may hold the data in the server
> and only transmit it record by record unless explicitly asked for all of
> it -- and if the database server is on a different machine that could
> make for a lot of slow network traffic).
>
>Also, you need to take the database locking system into account --
> until you not only read all the data, but commit the transaction (even a
> read-only transaction), the engine may lock any other user from
> accessing those records (and some engines may lock the entire table, not
> just records).
>
>Iterating over a cursor may be useful if: 1) you have a really
> massive database and your query is not filtering the data to manageable
> levels (better done by looping over the query itself using limit and
> offset features to control how many records are returned in a batch); 2)
> it is a single user/single access database where locking data won't have
> an impact; 3) you need to use the data in each record to update or
> otherwise perform some other query on the database (though unless this
> is done within the same transaction you might still have locking
> issues).
>
>
Wow! Thanks. I'll digest this over the weekend.
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Easy Q

2010-01-09 Thread Victor Subervi
On Sat, Jan 9, 2010 at 11:56 AM, Gary Herron wrote:

> Victor Subervi wrote:
>
>> Hi;
>> I have a string.join statement on a variable that comes from a
>> cgi.FieldStorage().getlist. The variable may be a list or a single value. I
>> need to treat it differently depending on which it is. How can I distinguish
>> it? len(var) will obviously give me the length of the string if it's a
>> string and the length of the list if it's a list.
>> TIA,
>> beno
>>
>
> Like this:
>
> if isinstance(var, list):
>  ... join ...
> else:
>  ... ??? ...
>

Right.. Thanks!
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2010-01-09 Thread Dave WB3DWE
On Jan 6 I inquired how to "fix" the 3.1.1 interactive terminal
in Ubuntu Linux.   Left arrow yields ^[[D , etc. 

casevh helped by suggesting "libreadline5-dev" be installed.
Did so with Synaptic Package Manager.
The behavior of the Python 3.3.1 terminal is unchanged but
the 2.6.2 terminal is corrected.

Is there any way to fix the 3.1.1 terminal command line ?

Thanks, Dave WB3DWE  pdlem...@earthlink.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Easy Q

2010-01-09 Thread Dave Angel

Victor Subervi wrote:

Hi;
I have a string.join statement on a variable that comes from a
cgi.FieldStorage().getlist. The variable may be a list or a single value. I
need to treat it differently depending on which it is. How can I distinguish
it? len(var) will obviously give me the length of the string if it's a
string and the length of the list if it's a list.
TIA,
beno

  
If you have to do an explicit type check of your variable, use an if 
statement, and the isinstance() built-in function.  Note that you can 
check for multiple types, like list and tuple, in the same function call.



DaveA

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


Re: Microsoft Office Word and Python (Win XP)

2010-01-09 Thread Terry Reedy

On 1/9/2010 10:12 AM, 3lvss0...@gmail.com wrote:


I need the script that moves (only moves, not change or delete!)
entire (100% of the text) text from one .doc file to another.


If you want to copy files without modification, use the OS copy command. 
You can use the subprocess module to do that from Python.


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


Re: Microsoft Office Word and Python (Win XP)

2010-01-09 Thread Marco Nawijn
On Jan 9, 4:12 pm, "3lvss0...@gmail.com" <3lvss0...@gmail.com> wrote:
> Hi.
> Im very new with python. I have got some answer on my issue to use
> interop or COM ''plugins'' to access MS Word through python but i
> don't even know what those two ''plugins'' are so I cannot use them.
> What I want to do is the following:
>
> I need the script that moves (only moves, not change or delete!)
> entire (100% of the text) text from one .doc file to another. But its
> not so easy as it sounds. The target .doc file is not the only one but
> can be many of them. All the target .doc files are always in the same
> folder (same path) but all of them don't have the same name. The .doc
> file FROM where I want to move entire text is only one, always in the
> same folder (same path) and always with the same file name.
> Names of the target are only similar but as I have said before, not
> the same. Here is the point of whole script:
> Target .doc files have the names:
> HD1.doc
> HD2.doc
> HD3.doc
> HD4.doc
> and so on
>
> What I would like to have is moved the entire (but really all of the
> text, must be 100% all) text into the .doc file with the highest ( ! )
> number. The target .doc files will always start with ''HD'' and always
> be similar to above examples.
> It is possible that the doc file (target file) is only one, so only
> HD1.doc. Therefore ''1'' is the maximum number and the text is moved
> into this file.
> Sometimes the target file is empty but usually won't be. If it won't
> be then the text should be moved to the end of the text, into first
> new line (no empty lines inbetween).
> So for example in the target file which has the maximum number in its
> name is the following text:
>
> a
> b
> c
>
> In the file from which I want to move the text is:
>
> d
>
> This means I need in the target file this:
>
> a
> b
> c
> d
>
> Could someone tell me please how to do this?
>
> Thank you.

Hi,

I will try to head you in the right direction with the Python/MS.Word
link.

First of all, you need to install the win32 extension. See
http://sourceforge.net/projects/pywin32/

Once you have this installed you can instantiate a MS.Word application
like
this (code untested):

>>> from win32com.client import Dispatch
>>> app = Dispatch("Word.Application")
>>> app.Visible = True

The code so-far is more or less equivalent to opening Word without
opening
a document (normally Word will start with an empty document).

To open a document do something like the following.
>>> doc = app.Documents.Open("c:\\example.doc")

Further builtin Python modules that could be helpfull are:
   glob->  for searching files matching a pattern
   os, os.path ->  for path related functionality like stripping
directory
   names from a complete path

Take a look at the online documentation for more information
http://docs.python.org/modindex.html

Good luck and let us know the result.

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


Direct use of bytearray buffers with ctypes ?

2010-01-09 Thread Pakal
Hello

I'm having big trouble wrapping the win32 ReadFile() function with
ctypes.

I wanted to allow the user to give a bytearray to this function, so
that the writable buffer of this bytearray is directly used to receive
the data from the file ; thus, no temporary copy in a separate ctype
buffer would be required.

However, I've found no way to provide ReadFile with a pointer to the
inner buffer of the bytearray object. I get misc. TypeErrors and
ValueErrors all the time, by trying to use the from_buffer() method or
other ctypes functions.

Is that actually posisble to expose with ctypes the internals of a
python object to the system, or is that unsupported at the moment ?
-- 
http://mail.python.org/mailman/listinfo/python-list


[RELEASED] Python 2.7 alpha 2

2010-01-09 Thread Benjamin Peterson
On behalf of the Python development team, I'm gleeful to announce the second
alpha release of Python 2.7.

Python 2.7 is scheduled to be the last major version in the 2.x series.  It
includes many features that were first released in Python 3.1.  The faster io
module, the new nested with statement syntax, improved float repr, and the
memoryview object have been backported from 3.1. Other features include an
ordered dictionary implementation, unittests improvements, and support for ttk
Tile in Tkinter.  For a more extensive list of changes in 2.7, see
http://doc.python.org/dev/whatsnew/2.7.html or Misc/NEWS in the Python
distribution.

To download Python 2.7 visit:

 http://www.python.org/download/releases/2.7/

Please note that this is a development release, intended as a preview of new
features for the community, and is thus not suitable for production use.

The 2.7 documentation can be found at:

 http://docs.python.org/2.7

Please consider trying Python 2.7 with your code and reporting any bugs you may
notice to:

 http://bugs.python.org


Have fun!

--
Benjamin Peterson
2.7 Release Manager
benjamin at python.org
(on behalf of the entire python-dev team and 2.7's contributors)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Easy Q

2010-01-09 Thread Gary Herron

Victor Subervi wrote:

Hi;
I have a string.join statement on a variable that comes from a 
cgi.FieldStorage().getlist. The variable may be a list or a single 
value. I need to treat it differently depending on which it is. How 
can I distinguish it? len(var) will obviously give me the length of 
the string if it's a string and the length of the list if it's a list.

TIA,
beno


Like this:

if isinstance(var, list):
 ... join ...
else:
 ... ??? ...

Gary Herron

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


Re: Easy Q

2010-01-09 Thread MRAB

Victor Subervi wrote:

Hi;
I have a string.join statement on a variable that comes from a 
cgi.FieldStorage().getlist. The variable may be a list or a single 
value. I need to treat it differently depending on which it is. How can 
I distinguish it? len(var) will obviously give me the length of the 
string if it's a string and the length of the list if it's a list.



1. string.join isn't a statement, it's a function, and why are you using
it anyway? Strings have a .join method:

>>> ", ".join(["first", "second", "third"])
'first, second, third'

2. Are you sure cgi.FieldStorage().getlist sometimes returns a single
value instead of a list? If that's the case then it's a very odd name
for the method!
--
http://mail.python.org/mailman/listinfo/python-list


Re: table from csv file

2010-01-09 Thread J
On Sat, Jan 9, 2010 at 05:26, Jon Clements  wrote:

>> reader = open('C:/test.txt','rb')
>> data = csv.DictReader(reader,restval='000',restkey='Misc')
>
> [snip]
>
> DictReader works, but what use to bug me was the fact you couldn't
> then output the cols in the 'correct' order afterwards, so you had
> to store the header row anyway to re-order the rows...
> (although admittedly this doesn't affect the OP's question).
>
> However, I see that 2.6+ offers .fieldnames on DictReader objects.

At a guess, and I stipulate that because I am just learning about all
this, I'd say that's because DictReader returns a dictionary for each
row retrieved...  It DOES take the first row and make those the dict
keys, but after that, it just returns a Dict for each rows and
dictionaries seem to be randomly ordered.

It may not be that great if you're doing  something that needs order,
but all he's doing is pulling arbitrary columns from a file and
printing as a table, so that works great in this case...

It was quite interesting messing around with that yesterday.  I saw
the question, and it looked interesting, so I went and learned and
applied ;-)  I'm rather smugly satisfied with myself ...

Cheers,

Jeff

-- 

Samuel Goldwyn  - "I'm willing to admit that I may not always be
right, but I am never wrong." -
http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get many places of pi from Machin's Equation?

2010-01-09 Thread Richard D. Moores
On Sat, Jan 9, 2010 at 07:57, Mark Dickinson  wrote:
> On Jan 9, 11:31 am, "Richard D. Moores"  wrote:
>> Machin's Equation is
>>
>> 4 arctan (1/5) - arctan(1/239) = pi/4
>> [...]
>>
>> Is there a way in Python 3.1 to calculate pi to greater accuracy using
>> Machin's Equation? Even to an arbitrary number of places?
>
> Here's some crude code (no error bounds,  possibility of infinite
> loops, ...) that computes pi to 1000 places using Machin's formula and
> the decimal module.  The last few digits will be bogus, and should be
> ignored.
>
> from decimal import Decimal, getcontext
>
> def atan(x):
>    # reductions
>    reductions = 0
>    while 100*abs(x) > 1:
>        reductions += 1
>        x /= 1 + (1+x*x).sqrt()
>
>    # Taylor series
>    sum = 0
>    xpow = x
>    x2 = x*x
>    k = 1
>    while True:
>        term = xpow/k
>        oldsum = sum
>        sum += term
>        if sum == oldsum:
>            break
>        k += 2
>        xpow *= -x2
>
>    return sum * 2**reductions
>
> getcontext().prec = 1000
> one = Decimal(1)
> print(16*atan(one/5) - 4*atan(one/239))
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Great! Done in Python 3 with arctan and the decimal module. And the
first 997 digits were accurate.

Just what I was after.

I don't believe the Chudnovsky algorithm has been mentioned. It isn't
what I wanted, but it is amazing. ()

Thanks, everyone!

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


Re: Link to module Stack

2010-01-09 Thread Steven D'Aprano
On Sat, 09 Jan 2010 05:56:36 -0500, Dave Angel wrote:

>> "InnerInterpreterError" is the most inappropriate exception name I've
>> ever seen. It has nothing to do with the interpreter, it's a stack
>> error.
>>
>>
> It has everything to do with the (Forth) interpreter.  Exceptions can
> readily be named according to their application -- it's not always about
> Python.  Anyway, Forth has an inner-interpreter and an
> outer-interpreter, and the name will make sense to a Forth programmer.

Pardon me, but I *am* a Forth programmer. Or was, it's been many years, 
and I'm rusty. I guess this is a difference of terminology: what you're 
calling an inner interpreter and an outer interpreter I know of as the 
Forth engine and the (text) interpreter. Gforth refers to them as such, 
so did Leo Brodie's Forth books, and the (ancient) Macintosh Forth 
compiler "Mach 2".

But in any case... a stack is an general-purpose data structure, and the 
error message shouldn't be coupled so tightly to one use. That would be 
like this (made-up) example:

>>> 1/0
Traceback (most recent call last):
  File "", line 1, in 
GraphicsApplicationError: too few pixels to calculate average


Ridiculous, yes?


Yes, Forth uses a stack (technically two, a parameter stack and a return 
stack, and some implementations include a third, floating point, stack). 
Virtually all languages use stacks in their implementation, and Python 
byte-code is also stack-based.


>>> result = self.__heap[-1]
>>> del self.__heap[-1]
>>> 
>>> 
>> That is better written as result = self.__heap.pop().
>>
>>
>>
> or even better, without the extra local var:
> 
> def pop (self):
> if len(self.__heap) == 0:
> raise InnerInterpreterError, "stack underflow"
> return self.__heap.pop(1)

pop(1)? I don't think so.

>>> L = list('abcdef')
>>> L.pop(1)
'b'
>>> L
['a', 'c', 'd', 'e', 'f']


You probably meant pop(-1), but that's unnecessary because pop defaults 
to popping from the end of the list.



> P.S. - I'm puzzled why the OP even put this message here.  There's no
> question posted with it.

Me too.



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


Easy Q

2010-01-09 Thread Victor Subervi
Hi;
I have a string.join statement on a variable that comes from a
cgi.FieldStorage().getlist. The variable may be a list or a single value. I
need to treat it differently depending on which it is. How can I distinguish
it? len(var) will obviously give me the length of the string if it's a
string and the length of the list if it's a list.
TIA,
beno

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


Re: How to get many places of pi from Machin's Equation?

2010-01-09 Thread Mark Dickinson
On Jan 9, 11:31 am, "Richard D. Moores"  wrote:
> Machin's Equation is
>
> 4 arctan (1/5) - arctan(1/239) = pi/4
> [...]
>
> Is there a way in Python 3.1 to calculate pi to greater accuracy using
> Machin's Equation? Even to an arbitrary number of places?

Here's some crude code (no error bounds,  possibility of infinite
loops, ...) that computes pi to 1000 places using Machin's formula and
the decimal module.  The last few digits will be bogus, and should be
ignored.

from decimal import Decimal, getcontext

def atan(x):
# reductions
reductions = 0
while 100*abs(x) > 1:
reductions += 1
x /= 1 + (1+x*x).sqrt()

# Taylor series
sum = 0
xpow = x
x2 = x*x
k = 1
while True:
term = xpow/k
oldsum = sum
sum += term
if sum == oldsum:
break
k += 2
xpow *= -x2

return sum * 2**reductions

getcontext().prec = 1000
one = Decimal(1)
print(16*atan(one/5) - 4*atan(one/239))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scripting (was Re: Python books, literature etc)

2010-01-09 Thread McColgst
Just to kind of get back on topic:

Before buying a book or making a terribly large investment, OP should
consider the fact that Python 3 is out and gaining some popularity.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get many places of pi from Machin's Equation?

2010-01-09 Thread Mark Dickinson
On Jan 9, 11:31 am, "Richard D. Moores"  wrote:
> Is there a way in Python 3.1 to calculate pi to greater accuracy using
> Machin's Equation? Even to an arbitrary number of places?

There's no arbitrary-precision version of atan included with Python.
You could write your own (e.g., based on argument reduction + Taylor
series) for use with the decimal module, or you could use one of the
various 3rd party arbitrary-precision arithmetic packages that do
provide atan.

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


Re: Something More Elegant

2010-01-09 Thread Victor Subervi
On Sat, Jan 9, 2010 at 10:14 AM, Iuri  wrote:

> And you should use cursor.fetchall() instead of cursor in list
> comprehension:
>
> packageIDs = [itm[0] for itm in cursor.fetchall()]
>

Now, someone else on this list told me the other. Can you explain the
difference?
TIA,
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Porblem with xlutils/xlrd/xlwt

2010-01-09 Thread John Machin
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.

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


Re: Something More Elegant

2010-01-09 Thread Victor Subervi
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. 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.
TIA,
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Microsoft Office Word and Python (Win XP)

2010-01-09 Thread 3lvss0...@gmail.com
Hi.
Im very new with python. I have got some answer on my issue to use
interop or COM ''plugins'' to access MS Word through python but i
don't even know what those two ''plugins'' are so I cannot use them.
What I want to do is the following:

I need the script that moves (only moves, not change or delete!)
entire (100% of the text) text from one .doc file to another. But its
not so easy as it sounds. The target .doc file is not the only one but
can be many of them. All the target .doc files are always in the same
folder (same path) but all of them don't have the same name. The .doc
file FROM where I want to move entire text is only one, always in the
same folder (same path) and always with the same file name.
Names of the target are only similar but as I have said before, not
the same. Here is the point of whole script:
Target .doc files have the names:
HD1.doc
HD2.doc
HD3.doc
HD4.doc
and so on

What I would like to have is moved the entire (but really all of the
text, must be 100% all) text into the .doc file with the highest ( ! )
number. The target .doc files will always start with ''HD'' and always
be similar to above examples.
It is possible that the doc file (target file) is only one, so only
HD1.doc. Therefore ''1'' is the maximum number and the text is moved
into this file.
Sometimes the target file is empty but usually won't be. If it won't
be then the text should be moved to the end of the text, into first
new line (no empty lines inbetween).
So for example in the target file which has the maximum number in its
name is the following text:

a
b
c

In the file from which I want to move the text is:

d

This means I need in the target file this:

a
b
c
d

Could someone tell me please how to do this?

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


Re: Something More Elegant

2010-01-09 Thread Iuri
And you should use cursor.fetchall() instead of cursor in list
comprehension:

packageIDs = [itm[0] for itm in cursor.fetchall()]

On Sat, Jan 9, 2010 at 1:01 PM, Gabriel Genellina wrote:

> En Sat, 09 Jan 2010 11:01:25 -0300, Victor Subervi <
> victorsube...@gmail.com> escribió:
>
>> On Sat, Jan 9, 2010 at 8:39 AM, Tim Chase > >wrote:
>>
>>  It would also help if you didn't pass the categoryID as a
>>> string-formatted
>>> value, but as a proper parameter, something like
>>>
>>>  sql = "... where c.categoryid=?" % (store, store)
>>>  cursor.execute(sql, (category_id,))
>>>
>>>
>> I now have the following:
>>
>>  sql = 'select distinct p.ID from %sPackages p join
>> %sCategoriesPackages c where c.CategoryID=?;' % (store, store)
>>  cursor.execute(sql, (categoryID,))
>>  packageIDs = [itm[0] for itm in cursor]
>>
>> It threw this error:
>>
>> TypeError: not all arguments converted during string formatting
>>  args = ('not all arguments converted during string formatting',)
>>
>>  You'd have to check the place-holder character for your particular
>>> back-end:
>>>
>>>  >>> import  as db
>>>  >>> print db.paramstyle
>>>
>>> Printed "format". What's that mean? I use MySQLdb
>>>
>>
> That means, MySQLdb uses %s as a placeholder for parameter substitution --
> same as Python when doing string interpolation. Unfortunately this will
> confuse things. In your code above, the ? near the end should become %s --
> but you don't want THAT %s to be interpreted by Python at that time, instead
> it must remain as a literal %s until the cursor.execute line. You have to
> escape the % by doubling it: %%s
>
>   sql = 'select distinct p.ID from %sPackages p join
> %sCategoriesPackages c where c.CategoryID=%%s;' % (store, store)
>
>   cursor.execute(sql, (categoryID,))
>   packageIDs = [itm[0] for itm in cursor]
>
> --
> Gabriel Genellina
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: restructuredText editor ?

2010-01-09 Thread Peter

On 01/09/2010 03:32 AM, Florian Diesch wrote:

Peter  writes:

   

What editor do people out there use to edit .rst files for
sphinx-python documentation ?
 

Emacs with ReST mode and  YASnippet


Florian
   
Great, works very well and thanks for mentionning YASnippets ( useful 
for many types of documents )


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


Re: Something More Elegant

2010-01-09 Thread Gabriel Genellina
En Sat, 09 Jan 2010 11:01:25 -0300, Victor Subervi  
 escribió:
On Sat, Jan 9, 2010 at 8:39 AM, Tim Chase  
wrote:


It would also help if you didn't pass the categoryID as a  
string-formatted

value, but as a proper parameter, something like

 sql = "... where c.categoryid=?" % (store, store)
 cursor.execute(sql, (category_id,))



I now have the following:

  sql = 'select distinct p.ID from %sPackages p join
%sCategoriesPackages c where c.CategoryID=?;' % (store, store)
  cursor.execute(sql, (categoryID,))
  packageIDs = [itm[0] for itm in cursor]

It threw this error:

TypeError: not all arguments converted during string formatting
  args = ('not all arguments converted during string formatting',)


You'd have to check the place-holder character for your particular
back-end:

 >>> import  as db
 >>> print db.paramstyle

Printed "format". What's that mean? I use MySQLdb


That means, MySQLdb uses %s as a placeholder for parameter substitution --  
same as Python when doing string interpolation. Unfortunately this will  
confuse things. In your code above, the ? near the end should become %s --  
but you don't want THAT %s to be interpreted by Python at that time,  
instead it must remain as a literal %s until the cursor.execute line. You  
have to escape the % by doubling it: %%s


   sql = 'select distinct p.ID from %sPackages p join  
%sCategoriesPackages c where c.CategoryID=%%s;' % (store, store)

   cursor.execute(sql, (categoryID,))
   packageIDs = [itm[0] for itm in cursor]

--
Gabriel Genellina

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


Re: How to get many places of pi from Machin's Equation?

2010-01-09 Thread Gabriel Genellina
En Sat, 09 Jan 2010 08:31:49 -0300, Richard D. Moores   
escribió:



Is there a way in Python 3.1 to calculate pi to greater accuracy using
Machin's Equation? Even to an arbitrary number of places?


You may be interested in Demo/scripts/pi.py in the source distribution. It  
can generate pi with infinite precision, limited by available memory only.
And this thread from last month:  
http://groups.google.com/group/comp.lang.python/t/e37bb8c59f2e5582/


--
Gabriel Genellina

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


Re: Something More Elegant

2010-01-09 Thread Steve Holden
Victor Subervi wrote:
> On Sat, Jan 9, 2010 at 8:39 AM, Tim Chase  > wrote:
> 
> Victor Subervi wrote:
> 
> Hi;
> The following code works fine. I would like you to suggest
> something more
> simple and elegant:
> 
>  sql = 'select p.ID from %sPackages p join
> %sCategoriesPackages c where
> c.CategoryID=%s;' % (store, store, categoryID)
>  cursor.execute(sql)
>  tmp = [itm[0] for itm in cursor]
>  packageIDs = []
>  for t in tmp:
>if t not in packageIDs:
>  packageIDs.append(t)
> 
> 
> You mean like
> 
>  sql = "select distinct p.ID from ..." % (...)
> 
> 
> Oh, that's good!
>  
> 
>  # 
>  cursor.execute(sql)
>  package_ids = [row[0] for row in cursor.fetchall()]
> 
> It would also help if you didn't pass the categoryID as a
> string-formatted value, but as a proper parameter, something like
> 
>  sql = "... where c.categoryid=?" % (store, store)
>  cursor.execute(sql, (category_id,))
> 
> 
> I now have the following:
> 
>   sql = 'select distinct p.ID from %sPackages p join
> %sCategoriesPackages c where c.CategoryID=?;' % (store, store)
>   cursor.execute(sql, (categoryID,))
>   packageIDs = [itm[0] for itm in cursor]
> 
> It threw this error:
> 
>  /var/www/html/angrynates.com/christians/cart/display.py
> 
>   141 print '\n'
>   142   cursor.close()
>   143   bottom()
>   144
>   145 display()
> display = 
>  /var/www/html/angrynates.com/christians/cart/display.py
>  in display()
>   109   categoryID = cursor.fetchone()[0]
>   110   sql = 'select distinct p.ID from %sPackages p join
> %sCategoriesPackages c where c.CategoryID=?;' % (store, store)
>   111   cursor.execute(sql, (categoryID,))
>   112   packageIDs = [itm[0] for itm in cursor]
>   113   for pid in packageIDs:
> global cursor = , cursor.execute =  method Cursor.execute of >, sql = 'select
> distinct p.ID from productsPackages p join productsCategoriesPackages c
> where c.CategoryID=?;', categoryID = 1L
>  /usr/lib64/python2.4/site-packages/MySQLdb/cursors.py in
> execute(self=, query='select distinct
> p.ID from productsPackages p join productsCategoriesPackages c where
> c.CategoryID=?;', args=(1L,))
>   146 query = query.encode(charset)
>   147 if args is not None:
>   148 query = query % db.literal(args)
>   149 try:
>   150 r = self._query(query)
> query = 'select distinct p.ID from productsPackages p join
> productsCategoriesPackages c where c.CategoryID=?;', db =  0x2b79db9dc470 to Connection>, db.literal =  Connection.literal of <_mysql.connection open to 'localhost' at
> 142be8b0>>, args = (1L,)
> 
> TypeError: not all arguments converted during string formatting
>   args = ('not all arguments converted during string formatting',) 
> 
> 
> This helps prevent SQL-injection attacks (assuming you have full
> control over the value of "store"...otherwise, as you've been
> advised, if the remote user has control over the value in "store",
> you're asking to be exploited).  
> 
> 
> They have control over it. I pass it in the url. Please advise.
>  
> 
> You'd have to check the place-holder character for your particular
> back-end:
> 
>  >>> import  as db
>  >>> print db.paramstyle
> 
> Printed "format". What's that mean? I use MySQLdb
> TIA,
> beno
> 
Given that you actually started this thread by asking a good question
that showed you had done some independent work, I'll bite.

The problem is something that was discussed in one of your other
numerous threads by John Machin and me. The issue is the
parameterization of (i.e. sticking variable bits into) SQL queries.

When you write

curs.execute("some sql query with %s and %s in it", (data1, data2))

the second argument to execute is supposed to contain data values. This
allows the SQL engine to do the preparatory work for a query once, and
then use the same "prepared query" then next time it's executed. The
preparation involves scanning the SQL query to make sure the syntax is
correct, validating the table and column names, and developing a "query
execution plan" that is a sequence of internal operations the database
performs to get you the answer you want. (SQL, unlike Python, is a
"declarative" language - rather than telling it what to do you describe
the results you want to see and the engine works out how to provide it).

Of course, if different tables are used for different queries then there
is no hope that the same execution plan can be used for them. For this
reason most database processors (and this certainly includes the one you
are using) don't allow you to parameterize anything in SQL statement
other than data values. So

Re: How to get many places of pi from Machin's Equation?

2010-01-09 Thread John Machin
On Jan 9, 10:31 pm, "Richard D. Moores"  wrote:
> Machin's Equation is
>
> 4 arctan (1/5) - arctan(1/239) = pi/4
>
> Using Python 3.1 and the math module:
>
>
>
> >>> from math import atan, pi
> >>> pi
> 3.141592653589793
> >>> (4*atan(.2) - atan(1/239))*4
> 3.1415926535897936
> >>> (4*atan(.2) - atan(1/239))*4 == pi
> False
> >>> abs((4*atan(.2) - atan(1/239))*4) - pi < .01
> False
> >>> abs((4*atan(.2) - atan(1/239))*4) - pi < .0001
> False
> >>> abs((4*atan(.2) - atan(1/239))*4) - pi < .001
> True
>
> Is there a way in Python 3.1 to calculate pi to greater accuracy using
> Machin's Equation? Even to an arbitrary number of places?

Considering that my namesake calculated pi to 100 decimal places with
the computational equipment available in 1706 (i.e. not much), I'd bet
you London to a brick that Python (any version from 0.1 onwards) could
be used to simulate his calculations to any reasonable number of
places. So my answers to your questions are yes and yes.

Suggestion: search_the_fantastic_web("machin pi python")
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another Screwy Problem

2010-01-09 Thread Victor Subervi
On Sat, Jan 9, 2010 at 9:07 AM, J. Cliff Dyer  wrote:

> On Sat, 2010-01-09 at 07:59 -0500, Victor Subervi wrote:
> > On Fri, Jan 8, 2010 at 4:44 PM, J. Clifford Dyer
> >  wrote:
> > Victor Subervi wrote:
> > > Hi;
> > > I have this line of code:
> > >  sql = 'select Name, Price from %sPackages where ID=%s;' %
> > (store, pid)
> > > which prints to this:
> > >  select Name, Price from productsPackages where ID=1;
> > > which when I enter it into the MySQL interpreter gives me
> > this:
> > > mysql> select Name, Price from productsPackages where ID=1;
> > > +--++
> > > | Name | Price  |
> > > +--++
> > > | pkg  | 123.45 |
> > > +--++
> > > 1 row in set (0.00 sec)
> > >
> > > exactly what I expect. However, in my script for some reason
> > it returns
> > > this:
> > > ((1,),)
> >
> >
> >
> > First, got your other email. I thought I had executed the statement.
> > Oops. Works fine now. Sorry.
> >
> > First, never use string formatting to pass parameters to your
> > database.  Read the MySQLdb documentation (or sqlite, or
> > psycopg2) documentation for reasons why, and how to do it
> > right.
> >
> > The only thing I found, which collaborates with something someone else
> > taught me on this list about entering binary data, is that one must
> > pass the parameters in the execute statement. Is that what you mean?
> > If so, I find that for all purposes thus far other than binary data,
> > the way I've been doing it seems to work just fine. I would prefer to
> > keep doing it that way, because I find putting a print statement
> > between the sql= line and the execute statement gives me a good
> > opportunity to review the sql statement and catch errors. Is this not
> > good practice?
> >
> >
> > Thanks.
> > beno
>
> This is a horrendous practice.  You leave yourself vulnerable not only
> to attacks, but to simple absent-mindedness as well.  Using parameters
> in your execute statement will handle all necessary quoting for you,
> which eliminates the possibility of a bad query sneaking in.  For more
> information, as I mentioned, look up SQL injection.  Also, read this:
> http://xkcd.com/327/
>
> Thanks :)
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another Screwy Problem

2010-01-09 Thread J. Cliff Dyer
On Sat, 2010-01-09 at 07:59 -0500, Victor Subervi wrote:
> On Fri, Jan 8, 2010 at 4:44 PM, J. Clifford Dyer
>  wrote:
> Victor Subervi wrote:
> > Hi;
> > I have this line of code:
> >  sql = 'select Name, Price from %sPackages where ID=%s;' %
> (store, pid)
> > which prints to this:
> >  select Name, Price from productsPackages where ID=1;
> > which when I enter it into the MySQL interpreter gives me
> this:
> > mysql> select Name, Price from productsPackages where ID=1;
> > +--++
> > | Name | Price  |
> > +--++
> > | pkg  | 123.45 |
> > +--++
> > 1 row in set (0.00 sec)
> >
> > exactly what I expect. However, in my script for some reason
> it returns
> > this:
> > ((1,),)
> 
> 
> 
> First, got your other email. I thought I had executed the statement.
> Oops. Works fine now. Sorry.
>  
> First, never use string formatting to pass parameters to your
> database.  Read the MySQLdb documentation (or sqlite, or
> psycopg2) documentation for reasons why, and how to do it
> right.
> 
> The only thing I found, which collaborates with something someone else
> taught me on this list about entering binary data, is that one must
> pass the parameters in the execute statement. Is that what you mean?
> If so, I find that for all purposes thus far other than binary data,
> the way I've been doing it seems to work just fine. I would prefer to
> keep doing it that way, because I find putting a print statement
> between the sql= line and the execute statement gives me a good
> opportunity to review the sql statement and catch errors. Is this not
> good practice?
> 
> 
> Thanks.
> beno

This is a horrendous practice.  You leave yourself vulnerable not only
to attacks, but to simple absent-mindedness as well.  Using parameters
in your execute statement will handle all necessary quoting for you,
which eliminates the possibility of a bad query sneaking in.  For more
information, as I mentioned, look up SQL injection.  Also, read this:
http://xkcd.com/327/

Cheers,
Cliff



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


Re: Something More Elegant

2010-01-09 Thread Victor Subervi
On Sat, Jan 9, 2010 at 8:39 AM, Tim Chase wrote:

> Victor Subervi wrote:
>
>> Hi;
>> The following code works fine. I would like you to suggest something more
>> simple and elegant:
>>
>>  sql = 'select p.ID from %sPackages p join %sCategoriesPackages c
>> where
>> c.CategoryID=%s;' % (store, store, categoryID)
>>  cursor.execute(sql)
>>  tmp = [itm[0] for itm in cursor]
>>  packageIDs = []
>>  for t in tmp:
>>if t not in packageIDs:
>>  packageIDs.append(t)
>>
>
> You mean like
>
>  sql = "select distinct p.ID from ..." % (...)
>

Oh, that's good!


>  # 
>  cursor.execute(sql)
>  package_ids = [row[0] for row in cursor.fetchall()]
>
> It would also help if you didn't pass the categoryID as a string-formatted
> value, but as a proper parameter, something like
>
>  sql = "... where c.categoryid=?" % (store, store)
>  cursor.execute(sql, (category_id,))
>

I now have the following:

  sql = 'select distinct p.ID from %sPackages p join
%sCategoriesPackages c where c.CategoryID=?;' % (store, store)
  cursor.execute(sql, (categoryID,))
  packageIDs = [itm[0] for itm in cursor]

It threw this error:

 /var/www/html/angrynates.com/christians/cart/display.py
  141 print '\n'
  142   cursor.close()
  143   bottom()
  144
  145 display()
display = 
 /var/www/html/angrynates.com/christians/cart/display.py in display()
  109   categoryID = cursor.fetchone()[0]
  110   sql = 'select distinct p.ID from %sPackages p join
%sCategoriesPackages c where c.CategoryID=?;' % (store, store)
  111   cursor.execute(sql, (categoryID,))
  112   packageIDs = [itm[0] for itm in cursor]
  113   for pid in packageIDs:
global cursor = , cursor.execute = >, sql = 'select
distinct p.ID from productsPackages p join productsCategoriesPackages c
where c.CategoryID=?;', categoryID = 1L
 /usr/lib64/python2.4/site-packages/MySQLdb/cursors.py in
execute(self=, query='select distinct p.ID
from productsPackages p join productsCategoriesPackages c where
c.CategoryID=?;', args=(1L,))
  146 query = query.encode(charset)
  147 if args is not None:
  148 query = query % db.literal(args)
  149 try:
  150 r = self._query(query)
query = 'select distinct p.ID from productsPackages p join
productsCategoriesPackages c where c.CategoryID=?;', db = , db.literal = >, args = (1L,)

TypeError: not all arguments converted during string formatting
  args = ('not all arguments converted during string formatting',)


> This helps prevent SQL-injection attacks (assuming you have full control
> over the value of "store"...otherwise, as you've been advised, if the remote
> user has control over the value in "store", you're asking to be exploited).
>


They have control over it. I pass it in the url. Please advise.


> You'd have to check the place-holder character for your particular
> back-end:
>
>  >>> import  as db
>  >>> print db.paramstyle
>
> Printed "format". What's that mean? I use MySQLdb
TIA,
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Something More Elegant

2010-01-09 Thread Tim Chase

Victor Subervi wrote:

Hi;
The following code works fine. I would like you to suggest something more
simple and elegant:

  sql = 'select p.ID from %sPackages p join %sCategoriesPackages c where
c.CategoryID=%s;' % (store, store, categoryID)
  cursor.execute(sql)
  tmp = [itm[0] for itm in cursor]
  packageIDs = []
  for t in tmp:
if t not in packageIDs:
  packageIDs.append(t)


You mean like

  sql = "select distinct p.ID from ..." % (...)
  # 
  cursor.execute(sql)
  package_ids = [row[0] for row in cursor.fetchall()]

It would also help if you didn't pass the categoryID as a 
string-formatted value, but as a proper parameter, something like


  sql = "... where c.categoryid=?" % (store, store)
  cursor.execute(sql, (category_id,))

This helps prevent SQL-injection attacks (assuming you have full 
control over the value of "store"...otherwise, as you've been 
advised, if the remote user has control over the value in 
"store", you're asking to be exploited).  You'd have to check the 
place-holder character for your particular back-end:


  >>> import  as db
  >>> print db.paramstyle

should tell you whether to use "?", "%s", or some other notation.



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


Re: Something More Elegant

2010-01-09 Thread Iuri
Your code select some ids from database and list distinct ids in packageIDs.
You can use SELECT DISTINCT in your SQL statement.



On Sat, Jan 9, 2010 at 11:23 AM, Victor Subervi wrote:

> Hi;
> The following code works fine. I would like you to suggest something more
> simple and elegant:
>
>   sql = 'select p.ID from %sPackages p join %sCategoriesPackages c
> where c.CategoryID=%s;' % (store, store, categoryID)
>   cursor.execute(sql)
>   tmp = [itm[0] for itm in cursor]
>   packageIDs = []
>   for t in tmp:
> if t not in packageIDs:
>   packageIDs.append(t)
>
> TIA,
> beno
>
> --
> The Logos has come to bear
> http://logos.13gems.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Something More Elegant

2010-01-09 Thread Victor Subervi
Hi;
The following code works fine. I would like you to suggest something more
simple and elegant:

  sql = 'select p.ID from %sPackages p join %sCategoriesPackages c where
c.CategoryID=%s;' % (store, store, categoryID)
  cursor.execute(sql)
  tmp = [itm[0] for itm in cursor]
  packageIDs = []
  for t in tmp:
if t not in packageIDs:
  packageIDs.append(t)

TIA,
beno

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


Re: creating tar file and streaming it over HTTP?

2010-01-09 Thread pbienst
OK, thanks to the feedback from everyone I got the PUT from a client
to the WSGI server working.

I'm now trying to go the other way around: use a tar stream in one of
the functions in the WSGI server in order to send files to the client.
Problem is that the WSGI specs expects an iterator as return value for
streaming, whereas TarFile needs to write to a file obj.

Is there any way I can get these two to work together?

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


Re: Append to an Excel file

2010-01-09 Thread Steve Holden
pp wrote:
> On Jan 9, 1:47 am, Jason Scheirer  wrote:
>> On Jan 9, 12:30 am, pp  wrote:
>>
>>> Hi All,
>>> How do I add a line to an existing file. This should append to the
>>> existing data in the excel file, which was saved previously.
>>> Thanks,
>>> PP
>> http://pypi.python.org/pypi/xlwt
> 
> Hi Jason and all,
> 
> Thanks
> 
> I have seen this.. my question is there a  way to append to a excel
> file which has been closed. Any specific modes which can be added to
> the sheet so that it adds a line to the data which was return in some
> earlier running of the program.
> 
If you are talking about an XLS file and not a CSV then it's a highly
structured object, and you can't just "stick bits on the end" with any
expectation that Excel will know what to do with the new data.  The most
likely outcomes would be Excel either complaining the file format is
invalid or ignoring the extra data.

If it's a CSV file, then f = open(file, "a") should do 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: lightweight encryption of text file

2010-01-09 Thread Arnaud Delobelle
Daniel Fetchinson  writes:

> 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
[...]

This is probably not what you want, but it is very simple and doesn't
import any module:) I am not qualified to say how easy it is to discover
the message without the password.

def str2int(txt):
return reduce(lambda n, c: n*255 + ord(c), txt, 0)

def int2str(n):
chars = []
while n:
n, o = divmod(n, 255)
chars.append(chr(o))
return ''.join(reversed(chars))

def encrypt(txt, pwd):
return int2str(str2int(txt)*str2int(pwd))

def decrypt(txt, pwd):
return int2str(str2int(txt)/str2int(pwd))

def test(txt, pwd):
encrypted_txt = encrypt(txt, pwd)
decrypted_txt = decrypt(encrypted_txt, pwd)
print "text:%r" % txt
print "encrypted:%r" % encrypted_txt
print "decrypted:%r" % decrypted_txt


>>> test("This encryption scheme is definitely unbreakable.", "secret")
text:'This encryption scheme is definitely unbreakable.'
encrypted:'&2\xa5\xd4\x17i+E\x01k\xfa\x94\xf80\xa8\x8f\xea.w\x128\xf1\xd9\x0f9\xf2t\xc9\r`\x90%\xd6\xf3~\x1f\x00%u&\x8a\xe4\xe0\xa7\xb8\xb0ec)S>\xcb\xf2>\xec'
decrypted:'This encryption scheme is definitely unbreakable.'

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


Re: Standardized interpreter speed evaluation tool

2010-01-09 Thread Steve Holden
Chris Rebert wrote:
> On Fri, Jan 8, 2010 at 2:25 AM, alexru  wrote:
>> Is there any standardized interpreter speed evaluation tool? Say I
>> made few changes in interpreter code and want to know if those changes
>> made python any better, which test should I use?
> 
> Although apparently undocumented, test.pystone is some sort of
> interpreter benchmark.
> 
It's undocumented because it's not considered a representative
benchmark.Sure, you can use it to get *some* idea of relative
performance, but a single program is a very poor tool for such a complex
topic a comparing implementations of a dynamic language.

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: Accessing python from a network share in windows 7

2010-01-09 Thread Anssi Saari
aj  writes:

> I access python from a network share. This works fine on XP but on
> windows 7 it throws the following error:
>
> Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit
> (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
 import random
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "t:\win32\python-2.6.1\lib\random.py", line 871, in 
> _inst = Random()
>   File "t:\win32\python-2.6.1\lib\random.py", line 96, in __init__
> self.seed(x)
>   File "t:\win32\python-2.6.1\lib\random.py", line 110, in seed
> a = long(_hexlify(_urandom(16)), 16)
> WindowsError: [Error 127] The specified procedure could not be found
>
> Is there some security policy that I need to enable/disable to use
> python from a network on windows 7?

Well, there was just a complaint about this sort of thing on a local
newsgroup here. Specifically, someone was trying to execute a Windows
program from a share and every time Windows 7 pops up a warning window
saying that the program is maybe from the evil interwebby and are you
really sure you actually want to run it. So probably that's the
command line version of same.

Solution is apparently specifying your server to be in the local
intranet, in IE's security settings. Apparently there is a non-working
autodetection for what is a local intranet, so specifying the server
IP address by hand in the advanced settigns is the working solution.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Link to module Stack

2010-01-09 Thread Duncan Booth
Dave Angel  wrote:

> or even better, without the extra local var:
> 
> def pop (self):
> if len(self.__heap) == 0:
> raise InnerInterpreterError, "stack underflow"
> return self.__heap.pop(1)

pop(1)?

Anyway if would be simpler and almost certainly faster to not bother 
checking before the pop:

def pop(self):
try:
return self.__heap.pop()
except IndexError:
raise InnerInterpreterError, "stack underflow"

and if performance mattered the OP might even consider pre-binding the pop 
method in __init__:

self.__pop = self.__heap.pop

but that's probably premature optimisation.
  
> P.S. - I'm puzzled why the OP even put this message here.  There's no 
> question posted with it.

Me too. It's a repost of something from 2004. Bizarre.
-- 
http://mail.python.org/mailman/listinfo/python-list


How to get many places of pi from Machin's Equation?

2010-01-09 Thread Richard D. Moores
Machin's Equation is

4 arctan (1/5) - arctan(1/239) = pi/4

Using Python 3.1 and the math module:

>>> from math import atan, pi
>>> pi
3.141592653589793
>>> (4*atan(.2) - atan(1/239))*4
3.1415926535897936
>>> (4*atan(.2) - atan(1/239))*4 == pi
False
>>> abs((4*atan(.2) - atan(1/239))*4) - pi < .01
False
>>> abs((4*atan(.2) - atan(1/239))*4) - pi < .0001
False
>>> abs((4*atan(.2) - atan(1/239))*4) - pi < .001
True
>>>

Is there a way in Python 3.1 to calculate pi to greater accuracy using
Machin's Equation? Even to an arbitrary number of places?

Thanks,

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


Re: lightweight encryption of text file

2010-01-09 Thread Dave Angel



Anthra Norell wrote:
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.
>
> I'm fully aware of the security implications of this loose
> specification, but for my purposes this would be a good solution.
>
> What would be the simplest way to achieve this using preferably stock
> python without 3rd party modules? If a not too complex 3rd part
> module made it really simple that would be acceptable too.




Daniel,

Here's what looks like another thread veering off into package-ology, 
leaving a stumped OP behind.


"Don't use a random generator for encryption purposes!" warns the 
manual, of which fact I was reminded in no uncertain terms on this 
forum a few years ago when I proposed the following little routine in 
response to a post very similar to yours. One critic challenged me to 
encode my credit card data and post it. Which I did. Upon which 
another critic conjured up the horror vision of gigahertzes hacking my 
pathetic little effort to pieces as I was reading his message. Of the 
well-meaning kind, he urged me to put an immediate stop to this 
foolishness. I didn't.


No unplanned expenditures ensued.

Or to quote ... I forget who: Fools and innovators are people who 
don't care much about what one is not supposed to do.


So, take or leave what follows for what it is worth or not worth, I am 
confident it works and would serve your purpose, which, as I 
understand, is not to write a text book on cryptology.


Regards

Frederic


The problem I'd have with this approach (not studied in detail), is that 
there's no reason that next year's Python must use the same random 
number generator, or the same shuffle algorithm.  So in order to assure 
that "encrypted" archives will be recoverable, one should store with 
them the CPython implementation, in source form, just in case that's 
needed to reconstruct things.


DaveA

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


Re: Porblem with xlutils/xlrd/xlwt

2010-01-09 Thread pp
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:
>
> > > > Whenever i run the code below I get the following error:
>
> > > > AttributeError: 'Book' object has no attribute 'on_demand'
> > > > WARNING: Failure executing file: 
>
> > > > Why is it so??
>
> > > > from xlrd import open_workbook
> > > > from xlwt import easyxf
> > > > from xlutils.copy import copy
> > > > rb =  open_workbook('source.xls',formatting_info=True)
> > > > rs =  rb.sheet_by_index(0)
> > > > wb =  copy(rb)
> > > > ws =  wb.get_sheet(0)
> > > > plain = easyxf('')
> > > > for i,cell in enumerate(rs.col(2)):
> > > >      if not i:
> > > >          continue
> > > >      ws.write(i,2,cell.value,plain)
> > > > for i,cell in enumerate(rs.col(4)):
> > > >      if not i:
> > > >          continue
> > > >      ws.write(i,4,cell.value-1000)
> > > > wb.save('output.xls')
>
> > > I suspect your version of xlrd is not up to date (although I thought
> > > on_demand was ages ago!).
> > > Make sure all the tools are the latest versions 
> > > fromhttp://www.python-excel.org
>
> > > There's also a dedicated Google Group for the xl* products listed on
> > > that page.
>
> > > hth
> > > Jon.
>
> > yeah all my versions are latest fromhttp://www.python-excel.org.
> > just checked!!
> > 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'
Thanks ..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Link to module Stack

2010-01-09 Thread Dave Angel

Steven D'Aprano wrote:

On Sat, 09 Jan 2010 01:07:39 -0800, kzagradskiy wrote:

  

class Stack:
def __init__(self):
self.__heap = []



A "heap" has a technical meaning in programming. To describe the 
internals of a stack as "heap" will be disconcerting and confusing to 
anyone who knows about stacks and heaps.



  

def push (self, word):
self.__heap.append (word)
def pop (self):
if len(self.__heap) == 0:
raise InnerInterpreterError, "stack underflow"



"InnerInterpreterError" is the most inappropriate exception name I've 
ever seen. It has nothing to do with the interpreter, it's a stack error.


  
It has everything to do with the (Forth) interpreter.  Exceptions can 
readily be named according to their application -- it's not always about 
Python.  Anyway, Forth has an inner-interpreter and an 
outer-interpreter, and the name will make sense to a Forth programmer.

result = self.__heap[-1]
del self.__heap[-1]



That is better written as result = self.__heap.pop().


  

or even better, without the extra local var:

   def pop (self):
   if len(self.__heap) == 0:
   raise InnerInterpreterError, "stack underflow"
   return self.__heap.pop(1)

P.S. - I'm puzzled why the OP even put this message here.  There's no 
question posted with it.


DaveA

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


Re: Porblem with xlutils/xlrd/xlwt

2010-01-09 Thread Jon Clements
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:
>
> > > Whenever i run the code below I get the following error:
>
> > > AttributeError: 'Book' object has no attribute 'on_demand'
> > > WARNING: Failure executing file: 
>
> > > Why is it so??
>
> > > from xlrd import open_workbook
> > > from xlwt import easyxf
> > > from xlutils.copy import copy
> > > rb =  open_workbook('source.xls',formatting_info=True)
> > > rs =  rb.sheet_by_index(0)
> > > wb =  copy(rb)
> > > ws =  wb.get_sheet(0)
> > > plain = easyxf('')
> > > for i,cell in enumerate(rs.col(2)):
> > >      if not i:
> > >          continue
> > >      ws.write(i,2,cell.value,plain)
> > > for i,cell in enumerate(rs.col(4)):
> > >      if not i:
> > >          continue
> > >      ws.write(i,4,cell.value-1000)
> > > wb.save('output.xls')
>
> > I suspect your version of xlrd is not up to date (although I thought
> > on_demand was ages ago!).
> > Make sure all the tools are the latest versions 
> > fromhttp://www.python-excel.org
>
> > There's also a dedicated Google Group for the xl* products listed on
> > that page.
>
> > hth
> > Jon.
>
> yeah all my versions are latest fromhttp://www.python-excel.org.
> just checked!!
> what could be the problem?

Does rb = xlrd.open_workbook('somesheet.xls', on_demand=True) work by
itself?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lightweight encryption of text file

2010-01-09 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':
>  >
>  > [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.
>  >
>  > I'm fully aware of the security implications of this loose
>  > specification, but for my purposes this would be a good solution.
>  >
>  > What would be the simplest way to achieve this using preferably stock
>  > python without 3rd party modules? If a not too complex 3rd part
>  > module made it really simple that would be acceptable too.
>
>
>
>
> Daniel,
>
> Here's what looks like another thread veering off into package-ology,
> leaving a stumped OP behind.
>
> "Don't use a random generator for encryption purposes!" warns the
> manual, of which fact I was reminded in no uncertain terms on this forum
> a few years ago when I proposed the following little routine in response
> to a post very similar to yours. One critic challenged me to encode my
> credit card data and post it. Which I did. Upon which another critic
> conjured up the horror vision of gigahertzes hacking my pathetic little
> effort to pieces as I was reading his message. Of the well-meaning kind,
> he urged me to put an immediate stop to this foolishness. I didn't.
>
> No unplanned expenditures ensued.
>
> Or to quote ... I forget who: Fools and innovators are people who don't
> care much about what one is not supposed to do.
>
> So, take or leave what follows for what it is worth or not worth, I am
> confident it works and would serve your purpose, which, as I understand,
> is not to write a text book on cryptology.
>
> Regards
>
> Frederic
>
>
> ##
>
>
> import random
>
>
> def crypt_string (string, key, floor = 0, size_of_set = 255):
>
> # key is a number. The sign of that number controls which way the
> process
> # goes. If the number is positive, the result is an encryption. A
> negative
> # number orders a decryption.
>
>if key == 0: return string   # No processing
>
>import random
>
>MAX_CHUNK_SIZE  = 32
>MIN_CHUNK_SIZE  = 16
>
>def process_sequence (sequence):
>   s = ''
>   for c in sequence:
>  r = random.randint (0, size_of_set - 1)
>  s += chr (((ord (c) - floor + r * sign) % size_of_set) + floor)
>   return s
>
>def shuffle_sequence (sequence):
>   random.shuffle (sequence)
>   return sequence
>
>sign = (key > 0) * 2 - 1
>random.seed (key * sign)
>
>s = ''
>
>if sign > 0:# forward
>
>   i = 0
>   while i < len (string):
>  random_size_of_chunk = random.randint (MIN_CHUNK_SIZE,
> MAX_CHUNK_SIZE)
>  clear_chunk_shuffled = shuffle_sequence (list (string
> [i:i+random_size_of_chunk]))
>  code_chunk_shuffled = process_sequence (clear_chunk_shuffled)
>  s += code_chunk_shuffled
>  i += len (code_chunk_shuffled)
>
>else:   # backward
>
>   i = 0
>   while i < len (string):
>  random_size_of_chunk = random.randint (MIN_CHUNK_SIZE,
> MAX_CHUNK_SIZE)
>  code_chunk_shuffled = list (string [i:i+random_size_of_chunk])
>  real_size_of_chunk = len (code_chunk_shuffled)
>  unshuffling_template = shuffle_sequence (range
> (real_size_of_chunk))  # 1. same ...
>  clear_chunk_shuffled = process_sequence
> (code_chunk_shuffled) # 2. ... order
>  clear_chunk = real_size_of_chunk * [None]
>  for ii in range (real_size_of_chunk):
> clear_chunk [unshuffling_template[ii]] =
> clear_chunk_shuffled [ii]
>  s += ''.join (clear_chunk)
>  i += real_size_of_chunk
>
>return s
>
>
>
> def _crypt_file (in_file, out_file, key):
>
>BUFFER_SIZE = 1024
>while 1:
>   s = in_file.read (BUFFER_SIZE)
>   if s == '': break
>   out_file.write (crypt_string (s, key))
>
>
> def crypt_file (in_file_name, out_file_name, key):
>
> in_file = open (in_file_name, 'rb')
> out_file = open (out_file_name, 'wb')
> _crypt_file (in_file, out_file, key)
> out_file.close ()

Thanks a lot!

Your description is a good fit for my purposes, indeed I don't plan on
writing a text book on encryption :) Also, I don't plan on encrypting
credit card numbers either, all I need is that a file doesn't look
obviously full of ascii characters but something that is generic data.
And since it will not be an entire system, with lots of files of this
type, only we are talking about a single file, there is no incentive
to decipher my algorithm.

Re: lightweight encryption of text file

2010-01-09 Thread Daniel Fetchinson
On 1/9/10, Steven D'Aprano  wrote:
> 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':
>>
>> [fetchin...@fetch ~]$ file encrypted.data
>> encrypted.data: data
>
> If that is your sole requirement,

No, that was not my sole requirement, I also wrote:

"""
and the effort required to convert the file back to the original text
file without the password would be equivalent to guessing the
password.
"""

> then the addition of a single non-text
> byte (say, a null) anywhere in the file would be sufficient to have file
> identify it as data.

Yes, but this would not satisfy the other requirement quoted above.
One could read the file without an effort that is equivalent to
guessing a random password.

> You say "encrypt/convert" -- does this mean that you
> don't care if people can read the text in a hex editor, so long as file
> identifies it as data?

I do care. See the quote above :)

> Would something like a binary Vigenere Cipher be sufficient?
>
>
> # Untested
> def encrypt(plaintext, password):
> cipher = []
> for i, c in enumerate(plaintext):
> shift = password[i % len(password)]
> shift = ord(shift)
> cipher.append((ord(c) + shift) % 256)
> return ''.join([chr(n) for n in cipher])
>
> def decrypt(ciphertext, password):
> plain = []
> for i, c in enumerate(ciphertext):
> shift = password[i % len(password)]
> shift = ord(shift)
> plain.append((256 + ord(c) - shift) % 256)
> return ''.join([chr(n) for n in plain])

Thanks, this looks simple enough and probably sufficient for my purposes!
I'll see if I'll use this or Paul Rubin's p3.py.

> Is it acceptable if there is a chance (small, possibly vanishingly small)
> that file will identify it as text? As far as I know, even the
> heavyweight "serious" encryption algorithms don't *guarantee* that the
> cipher text will include non-text bytes.

Hmmm, that's a good point, but actually it doesn't matter if 'file'
identifies it as text with a very small probability.

>> and the effort required to convert the file back to the original text
>> file without the password would be equivalent to guessing the password.
>
> If you seriously mean that, then "lightweight encryption" won't do the
> job, because it is vulnerable to frequency analysis, which is easier than
> guessing the password. You need proper, heavy-weight encryption.

Well, probably you are right and I should have been more clear. What
typically people call obfuscation is sufficient for me, as long as the
obfuscation involves a password, something that your solution seems to
do.


>> I'm fully aware of the security implications of this loose
>> specification, but for my purposes this would be a good solution.
>
> Can you explain what your objection to real encryption is?

Not much really, I simply don't want to overkill, that's all. First
and foremost I wouldn't want to introduce a dependency on a 3rd party
module and also wouldn't want to read documentation of a complex API
when all I need are two functions: encrypt( text, password) and
decrypt( text, password ). I really like your solution because that's
all it does.

> The problem is that, as I see it, you've assumed a solution rather than
> state what your requirements are. I'm *guessing* that you are more
> concerned of having to learn to use a complex API,

Well, that's sort of true about learning a complex API :) But it's
also true that I'm not storing anything really valuable in the file
but still wouldn't want to leave it lying around in plain text. In
case I lose the laptop with the file I seriously doubt anybody who
finds it will go through each and every file and try to find what's in
it, even though they look like data files and there is no hint what so
ever that any one of them contains encrypted info. If they see a text
file, well, that can give them ideas, so let's encrypt a little bit.
So basically that's the story, granted, it's not a full specification
or anything like that, it's a description of a vague situation but
that's really all I have :)

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


Re: Porblem with xlutils/xlrd/xlwt

2010-01-09 Thread pp
On Jan 9, 3:42 am, Jon Clements  wrote:
> On Jan 9, 10:24 am, pp  wrote:
>
>
>
> > Whenever i run the code below I get the following error:
>
> > AttributeError: 'Book' object has no attribute 'on_demand'
> > WARNING: Failure executing file: 
>
> > Why is it so??
>
> > from xlrd import open_workbook
> > from xlwt import easyxf
> > from xlutils.copy import copy
> > rb =  open_workbook('source.xls',formatting_info=True)
> > rs =  rb.sheet_by_index(0)
> > wb =  copy(rb)
> > ws =  wb.get_sheet(0)
> > plain = easyxf('')
> > for i,cell in enumerate(rs.col(2)):
> >      if not i:
> >          continue
> >      ws.write(i,2,cell.value,plain)
> > for i,cell in enumerate(rs.col(4)):
> >      if not i:
> >          continue
> >      ws.write(i,4,cell.value-1000)
> > wb.save('output.xls')
>
> I suspect your version of xlrd is not up to date (although I thought
> on_demand was ages ago!).
> Make sure all the tools are the latest versions 
> fromhttp://www.python-excel.org
>
> There's also a dedicated Google Group for the xl* products listed on
> that page.
>
> hth
> Jon.

yeah all my versions are latest from http://www.python-excel.org .
just checked!!
what could be the problem?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Porblem with xlutils/xlrd/xlwt

2010-01-09 Thread Jon Clements
On Jan 9, 10:24 am, pp  wrote:
> Whenever i run the code below I get the following error:
>
> AttributeError: 'Book' object has no attribute 'on_demand'
> WARNING: Failure executing file: 
>
> Why is it so??
>
> from xlrd import open_workbook
> from xlwt import easyxf
> from xlutils.copy import copy
> rb =  open_workbook('source.xls',formatting_info=True)
> rs =  rb.sheet_by_index(0)
> wb =  copy(rb)
> ws =  wb.get_sheet(0)
> plain = easyxf('')
> for i,cell in enumerate(rs.col(2)):
>      if not i:
>          continue
>      ws.write(i,2,cell.value,plain)
> for i,cell in enumerate(rs.col(4)):
>      if not i:
>          continue
>      ws.write(i,4,cell.value-1000)
> wb.save('output.xls')

I suspect your version of xlrd is not up to date (although I thought
on_demand was ages ago!).
Make sure all the tools are the latest versions from http://www.python-excel.org

There's also a dedicated Google Group for the xl* products listed on
that page.

hth
Jon.

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


Re: table from csv file

2010-01-09 Thread Jon Clements
On Jan 8, 8:31 pm, J  wrote:
> On Fri, Jan 8, 2010 at 13:55, Jon Clements  wrote:
> > On Jan 8, 5:59 pm, marlowe  wrote:
> >> I am trying to create a table in python from a csv file where I input
> >> which columns I would like to see, and the table only shows those
> >> columns. I have attached an example of the csv file i am using, and
> >> some of the code I have written. I am having trouble converting
> >> variables between lists, dictionaries and tuples. Is there a name for
> >> what I am attempting to do? any help to get me on the right track with
> >> this is appreciated.
>
> >> test.csv
>
> I had to edit that and comma delimit it, because cut and paste gave me
> random numbers/types of whitespace...
>
> [code snipped]
>
>
>
> > This might be a useful starting point (I'm guessing this is what
> > you're after...)
>
> > Let's assume your 'CSV' file is tab separated as it's certainly not
> > comma separated :)
>
> > import csv
> > csvin = csv.reader(open('test.csv'), delimiter='\t')
> > header = dict( (val.strip(),idx) for idx, val in enumerate(next
> > (csvin)) )
>
> > We can use header as a column name->column index lookup eg header
> > ['Open'] == 1
>
> > from operator import itemgetter
> > wanted = ['Open', 'Close'] # Although you'll want to use raw_input and
> > split on ','
> > getcols = itemgetter(*[header[col] for col in wanted])
>
> > getcols is a helper function that'll return a tuple of the columns in
> > the requested order...
>
> > for row in csvin:
> >    print getcols(row)
>
> > Loop over the rest of the file and output the required columns.
>
> As someone who knows just enough to be dangerous... what about this:
>
> import csv
>
> reader = open('C:/test.txt','rb')
> data = csv.DictReader(reader,restval='000',restkey='Misc')

[snip]

DictReader works, but what use to bug me was the fact you couldn't
then output the cols in the 'correct' order afterwards, so you had
to store the header row anyway to re-order the rows...
(although admittedly this doesn't affect the OP's question).

However, I see that 2.6+ offers .fieldnames on DictReader objects.

Cheers,

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


Re: lightweight encryption of text file

2010-01-09 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':
>
> [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.
>
> I'm fully aware of the security implications of this loose
> specification, but for my purposes this would be a good solution.
>
> 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.

 Paul Rubin's p3.py algorithm is probably the most straightforward way to
 meet
 these requirements. It's not a standard crypto algorithm by any means,
 but
 Paul
 knows his stuff and has devised it with these deployment restrictions in
 mind.

 http://www.nightsong.com/phr/crypto/p3.py
>>>
>>> Thanks a lot, currently I'm having trouble using this code on python
>>> 2.6 but probably some small tweaking will fix it.
>>
>> Actually, it also doesn't work with python 2.5 and currently I don't
>> have access to anything older. array.array raises a
>>
>> ValueError: string length not a multiple of item size
>>
>> Does anyone recall a change to array.array?
>>
>> The full traceback is
>>
>> Traceback (most recent call last):
>>File "p3.py", line 163, in
>>  _test()
>>File "p3.py", line 143, in _test
>>  c1 = e(plain,key)
>>File "p3.py", line 69, in p3_encrypt
>>  xkey = _expand_key(k_enc, n+4)
>>File "p3.py", line 41, in _expand_key
>>  return array ('L', j)
>> ValueError: string length not a multiple of item size
>
> Are you on a 64-bit platform? Unfortunately, array's integer typecodes are
> platform-specific, but p3.py requires a 32-bit integer and was written on a
> 32-bit platform. It's reasonably straightforward to fix. Put this bit of
> (untested) code at the top of the file and replace occurrences of 'L' with
> uint32:
>
> # Find the typecode of a 32-bit unsigned integer.
> for typecode in 'IL':
>  if len(array(typecode, [0]).tostring()) == 4:
>  uint32 = typecode
>  break
> else:
>  raise RuntimeError("Neither 'I' nor 'L' are unsigned 32-bit integers.")

Thanks!
That was exactly the problem, I'm on a 64 bit machine and your fix
seems to work indeed.

Cheers,
Daniel


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


Re: Clarifications on compiling for Windows

2010-01-09 Thread Martin v. Loewis
> First of all, is the Windows FAQ fairly up to date?

Fairly, yes.

> Should people be
> referring to section 6 if they are going to build an application with
> an embedded Python interpreter?

I think that's very selective in its view of problems - why would I
be using SWIG, for example? (yet there are three issues dedicated to
SWIG in this section)

pythonNN.dll is not in \windows\system, but in system32 or winsxs.

pythonNN.lib is *not* a static library, but (as the text then notes)
an import library. So even if you link with pythonNN.lib, you *still*
need pythonNN.dll at run-time (what is discussed as a drawback of
dynamic linking). Of course, it might be possible to build a static
library out of Python (which then still might be called pythonNN.lib).

IMO, it should be possible to link Python into the executable (at
the expense of not supporting dynamic loading of extension modules
anymore).

> If I understand correctly, compiled extensions for Python on Windows
> should match the compiler that was used to build the interpreter
> itself? Is there a list somewhere that shows which version of msvc was
> used to compile the recent Python binaries?

See PCbuild/readme.txt of the respective Python release.

2.3:  VC 6
2.4, 2.5: VC 7.1 / VS .NET 2003
2.6, 3.1: VC 9 / VS 2008

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


Porblem with xlutils/xlrd/xlwt

2010-01-09 Thread pp
Whenever i run the code below I get the following error:

AttributeError: 'Book' object has no attribute 'on_demand'
WARNING: Failure executing file: 

Why is it so??

from xlrd import open_workbook
from xlwt import easyxf
from xlutils.copy import copy
rb =  open_workbook('source.xls',formatting_info=True)
rs =  rb.sheet_by_index(0)
wb =  copy(rb)
ws =  wb.get_sheet(0)
plain = easyxf('')
for i,cell in enumerate(rs.col(2)):
 if not i:
 continue
 ws.write(i,2,cell.value,plain)
for i,cell in enumerate(rs.col(4)):
 if not i:
 continue
 ws.write(i,4,cell.value-1000)
wb.save('output.xls')

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


Re: lightweight encryption of text file

2010-01-09 Thread Anthra Norell

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.
>
> I'm fully aware of the security implications of this loose
> specification, but for my purposes this would be a good solution.
>
> What would be the simplest way to achieve this using preferably stock
> python without 3rd party modules? If a not too complex 3rd part
> module made it really simple that would be acceptable too.




Daniel,

Here's what looks like another thread veering off into package-ology, 
leaving a stumped OP behind.


"Don't use a random generator for encryption purposes!" warns the 
manual, of which fact I was reminded in no uncertain terms on this forum 
a few years ago when I proposed the following little routine in response 
to a post very similar to yours. One critic challenged me to encode my 
credit card data and post it. Which I did. Upon which another critic 
conjured up the horror vision of gigahertzes hacking my pathetic little 
effort to pieces as I was reading his message. Of the well-meaning kind, 
he urged me to put an immediate stop to this foolishness. I didn't.


No unplanned expenditures ensued.

Or to quote ... I forget who: Fools and innovators are people who don't 
care much about what one is not supposed to do.


So, take or leave what follows for what it is worth or not worth, I am 
confident it works and would serve your purpose, which, as I understand, 
is not to write a text book on cryptology.


Regards

Frederic


##


import random


def crypt_string (string, key, floor = 0, size_of_set = 255):

   # key is a number. The sign of that number controls which way the 
process
   # goes. If the number is positive, the result is an encryption. A 
negative

   # number orders a decryption.

  if key == 0: return string   # No processing


  import random

  MAX_CHUNK_SIZE  = 32
  MIN_CHUNK_SIZE  = 16

  def process_sequence (sequence):
 s = ''
 for c in sequence:
r = random.randint (0, size_of_set - 1)
s += chr (((ord (c) - floor + r * sign) % size_of_set) + floor)
 return s

  def shuffle_sequence (sequence):
 random.shuffle (sequence)
 return sequence

  sign = (key > 0) * 2 - 1
  random.seed (key * sign)

  s = ''

  if sign > 0:# forward

 i = 0
 while i < len (string):
random_size_of_chunk = random.randint (MIN_CHUNK_SIZE, 
MAX_CHUNK_SIZE)
clear_chunk_shuffled = shuffle_sequence (list (string 
[i:i+random_size_of_chunk]))

code_chunk_shuffled = process_sequence (clear_chunk_shuffled)
s += code_chunk_shuffled
i += len (code_chunk_shuffled)

  else:   # backward

 i = 0
 while i < len (string):
random_size_of_chunk = random.randint (MIN_CHUNK_SIZE, 
MAX_CHUNK_SIZE)

code_chunk_shuffled = list (string [i:i+random_size_of_chunk])
real_size_of_chunk = len (code_chunk_shuffled)
unshuffling_template = shuffle_sequence (range 
(real_size_of_chunk))  # 1. same ...
clear_chunk_shuffled = process_sequence 
(code_chunk_shuffled) # 2. ... order

clear_chunk = real_size_of_chunk * [None]
for ii in range (real_size_of_chunk):
   clear_chunk [unshuffling_template[ii]] = 
clear_chunk_shuffled [ii]

s += ''.join (clear_chunk)
i += real_size_of_chunk

  return s



def _crypt_file (in_file, out_file, key):

  BUFFER_SIZE = 1024
  while 1:
 s = in_file.read (BUFFER_SIZE)
 if s == '': break
 out_file.write (crypt_string (s, key))


def crypt_file (in_file_name, out_file_name, key):

   in_file = open (in_file_name, 'rb')
   out_file = open (out_file_name, 'wb')
   _crypt_file (in_file, out_file, key)
   out_file.close ()

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


Re: Link to module Stack

2010-01-09 Thread Steven D'Aprano
On Sat, 09 Jan 2010 01:07:39 -0800, kzagradskiy wrote:

> class Stack:
> def __init__(self):
> self.__heap = []

A "heap" has a technical meaning in programming. To describe the 
internals of a stack as "heap" will be disconcerting and confusing to 
anyone who knows about stacks and heaps.


> def push (self, word):
> self.__heap.append (word)
> def pop (self):
> if len(self.__heap) == 0:
> raise InnerInterpreterError, "stack underflow"

"InnerInterpreterError" is the most inappropriate exception name I've 
ever seen. It has nothing to do with the interpreter, it's a stack error.

> result = self.__heap[-1]
> del self.__heap[-1]

That is better written as result = self.__heap.pop().


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


Re: C Module's '1.#INF' changes to 'inf' at Python

2010-01-09 Thread Mark Dickinson
On Jan 8, 3:36 pm, Robert Kern  wrote:
> On 2010-01-08 07:48 AM, CELEN Erman wrote:
> > My problem is that I’ve noticed a strange behavior in Python while
> > handling FPEs on Windows after switching compilers (msvc8 to msvc9) and
> > I am trying to find out how Python handles INF values to figure out
> > where the problem might be.
> > [...]
>
> Python 2.6 changed the string representations of the special floating point
> values inf and nan. Previously, the string representation was left up to the C
> runtime, so they differed between platforms. Python 2.6 normalized the string
> representation across all platforms. The underlying values are the same. What
> version of Python are you using?

In addition to this, for good or ill Python 2.6 also standardized
exceptional behaviour for the math module functions:  log10(0.0) and
sqrt(-1) used to produce different results across implementations, but
now both should consistently produce ValueError regardless of the
platform.  This is achieved by dealing directly with special cases in
the input before delegating to the relevant libm function; this of
course has an associated performance cost.  There are some notes on
the (intended) current behaviour at the top of the Modules/
mathmodule.c file:

http://svn.python.org/view/*checkout*/python/branches/release26-maint/Modules/mathmodule.c

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


Link to module Stack

2010-01-09 Thread kzagradskiy
Link to module Stack:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/e6a0668bb2be9a8e/64cb44a120baeca2?lnk=gst&q=stack+module#64cb44a120baeca2

Here's the stack module for py4th.
nick
---
#!/usr/Util/bin/python
#
# @(#)stack.py  1.1
#
#   stack.py
#  generic stack class.
class Stack:
def __init__(self):
self.__heap = []
def push (self, word):
self.__heap.append (word)
def pop (self):
if len(self.__heap) == 0:
raise InnerInterpreterError, "stack underflow"
result = self.__heap[-1]
del self.__heap[-1]
return result
def __repr__(self):
return `self.__heap`
def __str__(self):
return `self.__heap`
def flush (self):
self.__heap = []
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Append to an Excel file

2010-01-09 Thread pp
On Jan 9, 1:47 am, Jason Scheirer  wrote:
> On Jan 9, 12:30 am, pp  wrote:
>
> > Hi All,
>
> > How do I add a line to an existing file. This should append to the
> > existing data in the excel file, which was saved previously.
>
> > Thanks,
> > PP
>
> http://pypi.python.org/pypi/xlwt

Hi Jason and all,

Thanks

I have seen this.. my question is there a  way to append to a excel
file which has been closed. Any specific modes which can be added to
the sheet so that it adds a line to the data which was return in some
earlier running of the program.


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


Re: Clarifications on compiling for Windows

2010-01-09 Thread Jason Scheirer
On Jan 7, 10:51 pm, Mensanator  wrote:
> On Jan 8, 12:19 am, peteshinners  wrote:
>
>
>
>
>
>
>
> > My presentation for Pycon is coming together, but I need to make sure
> > my information about compiling Python and Python extensions for
> > Windows is correct. I'm really only experienced with this on the Linux
> > side of things.
>
> > First of all, is the Windows FAQ fairly up to date? Should people be
> > referring to section 6 if they are going to build an application with
> > an embedded Python 
> > interpreter?http://www.python.org/doc/faq/windows/#how-can-i-embed-python-into-a-...
>
> > If I understand correctly, compiled extensions for Python on Windows
> > should match the compiler that was used to build the interpreter
> > itself? Is there a list somewhere that shows which version of msvc was
> > used to compile the recent Python binaries?
>
> > Thank you for feedback. I definitely want to make sure I have this
> > correct before telling anybody else?
>
> You aren't going to try it?

At a high level: YES. Avoid FILE* and you are golden on Windows. Works
like a charm.

MSVC 2008 works for 2.6 with the least effort (I strongly recommend
having it installed as that's what the build uses). If you have VS2008
you will have no problem whatsoever with setup.py install, even with C
extensions. I'd like to verify the same with earlier versions of VS
but I can't. MinGW works, too, but with slightly more effort: there
are some command line arguments you have to issue setup.py to know how
to use/where the MinGW compiler is.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Append to an Excel file

2010-01-09 Thread Jason Scheirer
On Jan 9, 12:30 am, pp  wrote:
> Hi All,
>
> How do I add a line to an existing file. This should append to the
> existing data in the excel file, which was saved previously.
>
> Thanks,
> PP

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


Append to an Excel file

2010-01-09 Thread pp
Hi All,

How do I add a line to an existing file. This should append to the
existing data in the excel file, which was saved previously.

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