Jython 2.5 Beta1 Released!

2009-01-09 Thread Frank Wierzbicki
On behalf of the Jython development team, I'm pleased to announce that
Jython 2.5b1 is available for download:
http://downloads.sourceforge.net/jython/jython_installer-2.5b1.jar.
See the installation instructions here:
http://www.jython.org/Project/installation.html.

Jython 2.5 Beta1 continues a code cooling period where the number of
new features should significantly slow as we concentrate on
solidifying Jython 2.5 for an eventual release.  I would guess that we
will put out about two more betas before we start pushing out release
candidates, hopefully in February.

This is a beta release so be careful.

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

Support the Python Software Foundation:
http://www.python.org/psf/donations.html


Open Technology Group, Inc. announces Django GeoDjango MVC Framework Training

2009-01-09 Thread Chander Ganesan
Morrisville, NC (PRWEB) January 9, 2009 -- Open Technology Group, Inc. 
announces Django  GeoDjango MVC Framework Training


The Open Technology Group (OTG), a leader in the development and 
delivery of training solutions centered about Open Source technologies, 
released the latest in its set of Open Source Django training 
(http://www.otg-nc.com/django-training) courses. A powerful 
Model-View-Controller (MVC) framework written in Python, Django provides 
web developers with an intuitive framework to develop web-based 
applications. Programmers attending our Django training will learn how 
to develop, deploy, maintain, and administer Django applications, said 
Chander Ganesan, President, This hands-on course teaches web developers 
all they need to know to leverage the powerful features and 
functionality of the Django Framework. Students attending this training 
will learn - from start to finish - how to develop web applications 
using Django through a series of instructor-led lectures and hands on 
exercises, culminating in a complete, working web application.


OTG also announced a Django training course dedicated to Geospatial 
Professionals, Leveraging GeoDjango 
(http://www.otg-nc.com/geodjango-training).  GeoDjango builds upon the 
Django MVC framework by adding integrated support for Geospatial 
databases (including PostGIS and Oracle Spatial), as well as the GEOS 
and GDAL API's. This 2 day course teaches students how to use 
PostgreSQL, PostGIS, and GeoDjango to build powerful spatially enabled 
applications using the GeoDjango  Django framework.


OTG also provides training for Python, PostGIS, OpenLayers, 
JavaScript/AJAX, MySQL, and PostgreSQL - technologies that complement 
both Django and GeoDjango.


Both new classes are available today for both public enrollment and 
customized, affordable, on-site delivery to small groups (3 or more 
students) worldwide.


About Open Technology Group, Inc.
Founded in 2004 and headquartered in Morrisville, NC, the Open 
Technology Group, Inc. (OTG) has established itself as the leading 
provider of training solutions centered about Open Source software and 
solutions. With its comprehensive library of in-house developed 
intellectual property, OTG is able to deliver comprehensive, customized, 
and structured training covering a wide range of software solutions. The 
Open Technology Group offers affordable customized on-site technology 
training worldwide, as well as public-enrollment courses delivered in 
over 10 locations, nationwide. For more information, and a complete 
course catalog, visit us online at 
http://www.otg-nc.com/python-training, or contact us at 877-258-8987

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

   Support the Python Software Foundation:
   http://www.python.org/psf/donations.html


[ANNOUNCE] MyHDL 0.6 released

2009-01-09 Thread Jan Decaluwe

I'm happy to announce the release of MyHDL 0.6.

MyHDL is a Python package for using Python as a hardware
description language.

The highlight of this release is conversion to VHDL, in
addition to the existing Verilog capability. Furthermore,
the convertible subset has been broadened substantially
beyond synthesizable logic, to support test bench conversion.

For a complete overview, see:
   http://www.myhdl.org/doku.php/overview

To check whether MyHDL can be useful to you, please read:
   http://www.myhdl.org/doku.php/why

To find out the details of what's new in this release:
   http://www.myhdl.org/doc/0.6/whatsnew/0.6.html

You can download the release from SourceForge:
   http://sourceforge.net/project/showfiles.php?group_id=91207

Best regards,

Jan Decaluwe

--
Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com
Python as a hardware description language:
http://www.myhdl.org




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

   Support the Python Software Foundation:
   http://www.python.org/psf/donations.html


Re: BadZipfile file is not a zip file

2009-01-09 Thread Steven D'Aprano
On Thu, 08 Jan 2009 16:47:39 -0800, webcomm wrote:

 The error...
...
 BadZipfile: File is not a zip file
 
 When I look at data.zip in Windows, it appears to be a valid zip file. 
 I am able to uncompress it in Windows XP, and can also uncompress it
 with 7-Zip.  It looks like zipfile is not able to read a table of
 contents in the zip file.  That's not a concept I'm familiar with.

No, ZipFile can read table of contents:

Help on method printdir in module zipfile:

printdir(self) unbound zipfile.ZipFile method
Print a table of contents for the zip file.



In my experience, zip files originating from Windows sometimes have 
garbage at the end of the file. WinZip just ignores the garbage, but 
other tools sometimes don't -- if I recall correctly, Linux unzip 
successfully unzips the file but then complains that the file was 
corrupt. It's possible that you're running into a similar problem.


 data.zip is created in this script...
 
 decoded = base64.b64decode(datum)
 f = open('data.zip', 'wb')
 f.write(decoded)
 f.close()
 file = zipfile.ZipFile('data.zip', r)
 
 datum is a base64 encoded zip file.  Again, I am able to open data.zip
 as if it's a valid zip file.  Maybe there is something wrong with the
 approach I've taken to writing the data to data.zip?  I'm not sure if it
 matters, but the zipped data is Unicode.


The full signature of ZipFile is:

ZipFile(file, mode=r, compression=ZIP_STORED, allowZip64=True)

Try passing compression=zipfile.ZIP_DEFLATED and/or allowZip64=False and 
see if that makes any difference.

The zip format does support alternative compression methods, it's 
possible that this particular file uses a different sort of compression 
which Python doesn't deal with.


 What would cause a zip file to not have a table of contents?

What makes you think it doesn't have one?


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


Re: ulimit stack size and python threads

2009-01-09 Thread Martin v. Löwis
 Always crashing because I asked the OS to please not allow a process
 to grow too big is what I call overloading the meaning of ulimit -s.

Please trust that there is no explicit code in the Python interpreter
that tests whether the stack size is 4GB, and then produces an explicit
crash.

 It's quite surprising. Not to mention the poor error message.

AFAICT, you didn't even *report* yet what the error message is,
so I don't know whether it is poor.

Regards,
Martin

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


Re: ulimit stack size and python threads

2009-01-09 Thread Martin v. Löwis
 I see. I should be blaming the default behavior of pthreads. 

You shouldn't blame anybody. Instead, you should sit down and study
the problem in detail, until you fully understand it. Then you should
start contributing fixes. Never ever should you spread blame.

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


Encrypted Logging in python

2009-01-09 Thread koranthala
   I was wondering if there is a mechanism to encrypt logging
automatically in python.
   The issue is as follows:
(a) An application (after py2exe) will go as executable and there
is no need for the user to know that it is written in python. If an
exception occurs and it is logged, then the user can understand it is
written in python.
(b) A security threat. If an exception occurs, the code is seen by
the user - and possibly be misused.

   Base64 encoding somewhat helps - which is supported by logging
module - but even that is not very secure. If there can be an option -
wherein we send in the password and the logging is encrypted - it
might be better.
   I would have loved to provide the code, but I am completely tied up
at the moment and wont be able to help for another month.

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


Re: threading in PyQt vs threading in standard library

2009-01-09 Thread Phil Thompson
On Fri, 9 Jan 2009 15:15:28 +0800, Steven Woody narkewo...@gmail.com
wrote:
 Hi,
 
 I am considering using PyQt for GUI programs, and I notices that both
 of them include threading supports, so which one should I pick up?
 Similar also applies 'socket'.

I'd recommend using the PyQt versions of both.

A significant advantage of PyQt's sockets is that they are integrated with
the event loop. This means you don't need threads to handle networking.

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


Re: BadZipfile file is not a zip file

2009-01-09 Thread Martin v. Löwis
 What would cause a zip file to not have a table of contents?

AFAICT, _EndRecData is failing to find the end of zipfile structure in
the file. You might want debug through it to see where it looks, and how
it decides that this structure is not present in the file. Towards
22 bytes before the end of the file, the bytes PK\005\006 should appear.
If they don't appear, you don't have a zipfile. If they appear, but
elsewhere towards the end of the file, there might be a bug in the
zip file module (or, more likely, the zip file uses an optional zip
feature which the module doesn't implement).

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


Re: Replying to list messages

2009-01-09 Thread Steven D'Aprano
On Thu, 08 Jan 2009 10:42:13 -0800, Paul McNett wrote:

 Ben Finney wrote:
 Paul McNett p...@ulmcnett.com writes:
 But arguing about this here isn't going to change anything: opinions
 differ just like tabs/spaces and bottom-post/top-post.
 
 In cases like this, one side can simply be wrong :-)
 
 Best of luck getting your programs behaving as you want them to!
 
 BTW, I agree with you that in an ideal, pure world mailing lists
 wouldn't munge the reply-to field, but when 80% of the people use email
 clients that don't support reply-list, the practical thing to do as a
 list admin that wants to avoid having to explain over and over again
 that your client software is broken is to simply swallow some pride
 and munge the reply-to. Now, 99% of the users are happy, and the
 remaining 1% are elite enough to understand how to get around any
 problems this caused.

The only problem this leads to is people who hit send without checking 
where they are sending to are likely to be embarrassed when they send 
personal posts to the entire mailing list.

As far as I'm concerned, hitting send without looking to see who you are 
sending to is akin to turning into a busy road without looking to see if 
there are any cars coming: any accident that happens is YOUR fault, not 
the fault of the road, car, mail client or mailing list.

It boggles my brain that [insert sweeping generalisation here] the people 
who are most vehement about blaming the mailing list software are usually 
also the least understanding when (l)users click OK to dialogs without 
reading what the dialog says first.



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


Re: BadZipfile file is not a zip file

2009-01-09 Thread Carl Banks
On Jan 9, 2:16 am, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:
 On Thu, 08 Jan 2009 16:47:39 -0800, webcomm wrote:
  The error...
 ...
  BadZipfile: File is not a zip file

  When I look at data.zip in Windows, it appears to be a valid zip file.
  I am able to uncompress it in Windows XP, and can also uncompress it
  with 7-Zip.  It looks like zipfile is not able to read a table of
  contents in the zip file.  That's not a concept I'm familiar with.

 No, ZipFile can read table of contents:

     Help on method printdir in module zipfile:

     printdir(self) unbound zipfile.ZipFile method
         Print a table of contents for the zip file.

 In my experience, zip files originating from Windows sometimes have
 garbage at the end of the file. WinZip just ignores the garbage, but
 other tools sometimes don't -- if I recall correctly, Linux unzip
 successfully unzips the file but then complains that the file was
 corrupt. It's possible that you're running into a similar problem.


The zipfile format is kind of brain dead, you can't tell where the end
of the file is supposed to be by looking at the header.  If the end of
file hasn't yet been reached there could be more data.  To make
matters worse, somehow zip files came to have text comments simply
appended to the end of them.  (Probably this was for the benefit of
people who would cat them to the terminal.)

Anyway, if you see something that doesn't adhere to the zipfile
format, you don't have any foolproof way to know if it's because the
file is corrupted or if it's just an appended comment.

Most zipfile readers use a heuristic to distinguish.  Python's zipfile
module just assumes it's corrupted.

The following post from a while back gives a solution that tries to
snip the comment off so that zipfile module can handle it.  It might
help you out.

http://groups.google.com/group/comp.lang.python/msg/c2008e48368c6543


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


Re: drive a desktop app from python?

2009-01-09 Thread James Stroud

Tim Arnold wrote:
Hi, I don't even know what to google for on this one. I need to drive a 
commercial desktop app (on windows xp) since the app doesn't have a batch 
interface.  It's intended to analyze one file at a time and display a 
report.


I can get the thing to write out the report an html browser, but I have 
thousands of files I need it to analyze every night.


Is there any lib or recipe(s) for doing something like this via python?


You are a little thin on details here. My only advice at this point is 
to check out os.system, which is the simplest option. From there you can 
move to the popen2 module for greater control, but if you are talking 
about a gui app, you may run into hang-ups.


A quick recipe would be the following pretend application that 
recursively descends from the current directory calling the utility 
named dosapp on every file it finds that ends with jpg (what 
dosapp does is left to your imagination):




import os

def doit(suffix, adir, filenames):
  for afile in filenames:
if afile.endswith(suffix):
  pathname = os.path.join(adir, afile)
  os.system('dosapp %s' % pathname)


top = '.'
suffix = '.jpg'

os.path.walk(top, doit, suffix)


Read the docs on os.path.walk, of course.

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best practice in organize classes into modules

2009-01-09 Thread Bruno Desthuilliers

Steven Woody a écrit :

On Fri, Jan 9, 2009 at 1:02 PM, James Mills
prolo...@shortcircuit.net.au wrote:

On Fri, Jan 9, 2009 at 2:57 PM, Steven Woody narkewo...@gmail.com wrote:

In C++/Java, people usually put one class into one file.  What's the
suggestion on this topic in Python?  I so much interesting this
especially when exception classes also involved.

Normally i group related functionality into the one module.


Will that lead to too large source file size? 


When the case happens, then you can safely refactor the module into a 
package with submodules, and use the package's __init__.py to make it a 
facade for the submodules so the refactoring is transparent for client code.


But given Python's expressivity and metaprogramming features, it's 
usually easy to avoid cruft and boilerplate and keep the code short.



Is there a
recommendation on max lines of a python source?


Not really - use your own judgement, mostly. As far as I'm concerned, I 
start worrying about this when a module grows bigger than 1Kloc, but I 
seldom have this problem.


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


Re: Best practice in organize classes into modules

2009-01-09 Thread Steven Woody
On Fri, Jan 9, 2009 at 4:54 PM, Bruno Desthuilliers
bruno.42.desthuilli...@websiteburo.invalid wrote:
 Steven Woody a écrit :

 On Fri, Jan 9, 2009 at 1:02 PM, James Mills
 prolo...@shortcircuit.net.au wrote:

 On Fri, Jan 9, 2009 at 2:57 PM, Steven Woody narkewo...@gmail.com
 wrote:

 In C++/Java, people usually put one class into one file.  What's the
 suggestion on this topic in Python?  I so much interesting this
 especially when exception classes also involved.

 Normally i group related functionality into the one module.

 Will that lead to too large source file size?

 When the case happens, then you can safely refactor the module into a
 package with submodules, and use the package's __init__.py to make it a
 facade for the submodules so the refactoring is transparent for client code.

really a smart idea.  Did you mean putting some 'import statement in
__init__.py and use the old module name as the new package name?
Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-09 Thread Marc 'BlackJack' Rintsch
On Fri, 09 Jan 2009 04:04:41 +0100, Johannes Bauer wrote:

 I've first tried Python. Please don't beat me, it's slow as hell and
 probably a horrible solution:
 
 #!/usr/bin/python
 import sys
 import os
 
 f = open(sys.argv[1], r)

Mode should be 'rb'.

 filesize = os.stat(sys.argv[1])[6]

`os.path.getsize()` is a little bit more readable.

 width = 1024
 height = 1024
 pixels = width * height
 blocksize = filesize / width / height
 
 print(Filesize   : %d % (filesize)) print(Image size : %dx%d
 % (width, height)) print(Bytes per Pixel: %d % (blocksize))

Why parentheses around ``print``\s argument?  In Python 3 ``print`` is 
a statement and not a function.

 picture = { }
 havepixels = 0
 while True:
   data = f.read(blocksize)
   if len(data) = 0: break

if data:
break

is enough.

   datamap = { }
   for i in range(len(data)):
   datamap[ord(data[i])] = datamap.get(data[i], 0) + 1

Here you are creating a list full of integers to use them as index into 
`data` (twice) instead of iterating directly over the elements in 
`data`.  And you are calling `ord()` for *every* byte in the file 
although you just need it for one value in each block.  If it's possible 
to write the raw PGM format this conversion wouldn't be necessary at all.

For the `datamap` a `collections.defaultdict()` might be faster.

   maxchr = None
   maxcnt = None
   for (char, count) in datamap.items():
   if (maxcnt is None) or (count  maxcnt):
   maxcnt = count
   maxchr = char

Untested:

maxchr = max((i, c) for c, i in datamap.iteritems())[1]

   most = maxchr

Why?

   posx = havepixels % width
   posy = havepixels / width

posx, posy = divmod(havepixels, width)

Don't know if this is faster though.

   havepixels += 1
   if (havepixels % 1024) == 0:
   print(Progresss %s: %.1f%% % (sys.argv[1], 100.0 * 
havepixels /
   pixels))
 
   picture[(posx, posy)] = most

Why are you using a dictionary as 2d array?  In the C code you simply 
write the values sequentially, why can't you just use a flat list and 
append here?

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Re: Default __nonzero__ impl doesn't throw a TypeError exception

2009-01-09 Thread Bruno Desthuilliers

Bruno Desthuilliers a écrit :

Sergey Kishchenko a écrit :

(snip)

#prints Ouch!
f=Foo()
if f:
print Ouch!

So, default __nonzero__ impl is to return True.


Yes. It's clearly documented FWIW.


To be more exact: there's no default __nonzero__. The boolean value of 
an object is eval'd this way:


If the object is None (special cased by the interpreter AFAICT), it is 
false.
Else if the object implements __nonzero__, it has the boolean value 
returned by __nonzero__.
Else if the object implements __len__, it has the boolean value of its 
length.

Else if is true.


I think, this
behaviour conflicts with 'Explicit is better than implicit'


Why so ? It *is* explicit that the default for an object is to have a 
true value in a boolean context.


I meant explicit because documented, of course.



and
'Practicality beats purity'


Quite on the contrary. From a practical POV, the default truth values of 
Python objects are most of the time what you practically want in a 
boolean context - that is, any non-None object, non-empty sequence and 
non-zero numeric objects are true. __nonzero__ is here for the *very 
few* corner cases where this is not the sensible default.




statements. I think, throwing a TypeError
exception would be better.  It will result in more explicit code with
fewer errors.



As a last note wrt/ explicitness and practicality: Implementing your 
proposition, one would have to either put each and every boolean 
expression in a try/except block or explicitly define __nonzero__ for 
each and any class - most of the time (about 99.% I'd say) 
implementing it as to return True - and expect that *everybody* does so.


So yes, from a purity POV, it might look more explicit. But this 
would certainly not be practical at all.


OTHO, since boolean algebra only knows two values, it's clear that what 
is not false is by definition true. So having a default value (true) and 
a way to override it (__len__ and __nonzero__) is perhaps less pure, 
but really as explicit and much more practical.


My 2 cents...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6 fails on compiling Bug report

2009-01-09 Thread googler . 1 . webmaster
hm... any ideas?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6 fails on compiling Bug report

2009-01-09 Thread David Cournapeau
On Fri, Jan 9, 2009 at 6:18 PM,  googler.1.webmas...@spamgourmet.com wrote:
 hm... any ideas?

Posting the config.log file would be a first step to give more information,

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


Re: Implementing file reading in C/Python

2009-01-09 Thread James Mills
On Fri, Jan 9, 2009 at 7:15 PM, Marc 'BlackJack' Rintsch bj_...@gmx.net wrote:
 print(Filesize   : %d % (filesize)) print(Image size : %dx%d
 % (width, height)) print(Bytes per Pixel: %d % (blocksize))

 Why parentheses around ``print``\s argument?  In Python 3 ``print`` is
 a statement and not a function.

Not true as of 2.6+ and 3.0+

print is now a function.

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


Re: BadZipfile file is not a zip file

2009-01-09 Thread John Machin
On Jan 9, 7:16 pm, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:
 On Thu, 08 Jan 2009 16:47:39 -0800, webcomm wrote:
  The error...
 ...
  BadZipfile: File is not a zip file

  When I look at data.zip in Windows, it appears to be a valid zip file.
  I am able to uncompress it in Windows XP, and can also uncompress it
  with 7-Zip.  It looks like zipfile is not able to read a table of
  contents in the zip file.  That's not a concept I'm familiar with.

 No, ZipFile can read table of contents:

     Help on method printdir in module zipfile:

     printdir(self) unbound zipfile.ZipFile method
         Print a table of contents for the zip file.

 In my experience, zip files originating from Windows sometimes have
 garbage at the end of the file. WinZip just ignores the garbage, but
 other tools sometimes don't -- if I recall correctly, Linux unzip
 successfully unzips the file but then complains that the file was
 corrupt. It's possible that you're running into a similar problem.

  data.zip is created in this script...

      decoded = base64.b64decode(datum)
      f = open('data.zip', 'wb')
      f.write(decoded)
      f.close()
      file = zipfile.ZipFile('data.zip', r)

  datum is a base64 encoded zip file.  Again, I am able to open data.zip
  as if it's a valid zip file.  Maybe there is something wrong with the
  approach I've taken to writing the data to data.zip?  I'm not sure if it
  matters, but the zipped data is Unicode.

 The full signature of ZipFile is:

 ZipFile(file, mode=r, compression=ZIP_STORED, allowZip64=True)

 Try passing compression=zipfile.ZIP_DEFLATED and/or allowZip64=False and
 see if that makes any difference.

compression is irrelevant when reading. The compression method used
is stored on a per-file basis, not on a per-archive basis, and it
hasn't got anywhere near per-file details when that exception is
raised. allowZip64 has not been used either.


 The zip format does support alternative compression methods, it's
 possible that this particular file uses a different sort of compression
 which Python doesn't deal with.

  What would cause a zip file to not have a table of contents?

 What makes you think it doesn't have one?

 --
 Steven

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


Re: drive a desktop app from python?

2009-01-09 Thread Ant
On Jan 8, 9:06 pm, Tim Arnold tim.arn...@sas.com wrote:
 Is there any lib or recipe(s) for doing something like this via python?

Look into the PyWin32 extension module. It gives access to Windows
internals including the COM interface. You'll need to do some research
into how to automate the GUI you're using from other sources (there
are often VBA examples on the web that can be modified).

http://pywin32.sourceforge.net/

I've successfully used this in the past for automating MS Excel.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-09 Thread Marc 'BlackJack' Rintsch
On Fri, 09 Jan 2009 19:33:53 +1000, James Mills wrote:

 On Fri, Jan 9, 2009 at 7:15 PM, Marc 'BlackJack' Rintsch
 bj_...@gmx.net wrote:

 Why parentheses around ``print``\s argument?  In Python 3 ``print``
 is a statement and not a function.
 
 Not true as of 2.6+ and 3.0+
 
 print is now a function.

Please read again what I wrote.

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-09 Thread James Mills
On Fri, Jan 9, 2009 at 7:41 PM, Marc 'BlackJack' Rintsch bj_...@gmx.net wrote:
 Please read again what I wrote.

Lol I thought 3 was a smiley! :)

Sorry!

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


Re: Implementing file reading in C/Python

2009-01-09 Thread Marc 'BlackJack' Rintsch
On Fri, 09 Jan 2009 04:04:41 +0100, Johannes Bauer wrote:

   datamap = { }
   for i in range(len(data)):
   datamap[ord(data[i])] = datamap.get(data[i], 0) + 1

Here is an error by the way:  You call `ord()` just on the left side of 
the ``=``, so all keys in the dictionary are mapped to ones after the 
loop which gives a pretty boring PGM.  :-)

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-09 Thread Steven D'Aprano
On Thu, 08 Jan 2009 18:33:50 +, Mark Wooding wrote:

 [Steven's message hasn't reached my server, so I'll reply to it here.
 Sorry if this is confusing.]
 
 Aaron Brady castiro...@gmail.com wrote:
 On Jan 8, 1:45 am, Steven D'Aprano
 ste...@remove.this.cybersource.com.au wrote:
  On Wed, 07 Jan 2009 10:17:55 +, Mark Wooding wrote:
 
   The `they're just objects' model is very simple, but gets tied up
   in knots explaining things.  The `it's all references' model is
   only a little more complicated, but explains everything.
 
  But it *over* explains, because it implies things that everybody
  knows about references in other languages that aren't true for
  Python.
 
 I addressed this elsewhere.  Summary: `pass-by-reference' is a different
 thing to `all you manipulate are references': 

You know, I've written a fair bit of Python code over the years, and I've 
never manipulated a reference *once*. Ints, strings, floats, lists, 
tuples... but references? Never.

I'm pretty sure that no other pure-Python coder has manipulated 
references either. They've manipulated objects. Whatever the VM does 
under the hood is another story.

If you insist on talking about implementations, then at least get it 
right: in Python, like every other programming language, all you do is 
flip bits. It's *all* bit flipping.

That's why we should try to keep the different layers of explanation 
separate, without conflating them. Python programmers don't actually flip 
bits, and neither do they manipulate references. Python programmers don't 
have access to bits, or references. What they have access to is objects.

(Of course, there are ways to get under the hood if you really want to.)


 Python does pass-by-value,
 but the things it passes -- by value -- are references.

If you're going to misuse pass-by-value to describe what Python does, 
*everything* is pass-by-value where the value is foo, for some foo. You 
can't have anything but pass-by-value with current computer technology, 
because computers don't actually move arguments, they only copy bytes. So 
pass-by-value becomes a meaningless term, because it describes every 
computer language imaginable, including hypothetical ones using calling 
conventions not yet invented, and therefore explains nothing.


 (The `pass-by-*' notions are confusingly named anyway.  Pass-by-name
 doesn't actually involve names at all.)

You might find them confusing, but I don't. What I find confusing is that 
people insist on misusing terminology invented for describing one type of 
behaviour in order to use it for a completely different type of behaviour 
just because of certain similarities under the hood.



  Of course it's not literally true that everybody knows that you can
  use references to implement a swap(x, y) procedure. But people coming
  from a C or Pascal background tend to assume that everything is like
  C/Pascal, and there are a lot of them. If C was a rare, unfamiliar
  language, my opposition to using the term reference would be a lot
  milder. Maybe in another five years?
 
 I agree with the comment about Pascal, but C is actually pretty similar
 to Python here.  C only does pass-by-value.

Except for arrays.


 If you want a function to
 modify your variable, you have to pass a pointer value which points to
 it.

Yes, because the variable is copied before the function sees it. So if 
you pass a struct, and modify one of the struct's fields, the caller 
doesn't see the change.

Now try that with Python, and you'll see completely different behaviour. 
(You'll have to use something *like* a struct, because Python doesn't 
have them. Try an object with attributes.)

In other words... C is call-by-value, and (according to you) Python is 
call-by-value, but they behaviour differently.


 Python has no pointer values, so you need a different hack.  The
 hack usually involves lists.  (Though it's easier in the main to return
 compound data objects like tuples.  I don't suppose that a proposal for
 true multiple return values would go down well here.  No, didn't think
 so...)

Out of curiosity, what makes Python returning tuples less true than 
true multiple return values, and what can you do with TMRVs that you 
can't do with tuples?


  Okay, the abstraction has leaked again... are the paperweights
  references to the objects, or the names we've bound objects to? I'm
  confused...
 
 They're the references to the objects.  You don't bind names to objects.

Amazing. It sure feels like it to me.

x = 23

There's a name, and an object, and I've bound the name to the object so I 
can refer to the object 23 by the name x.


  You bind names slots in which you store references.

I'm pretty sure I don't. I'd have noticed.



You may have missed my last question:

  How do we deal with anonymous objects in your model?



 What I am pretty sure of is that references are going to have to enter
 the picture at some point, because other models get too complicated.

Well, I dare 

Re: BadZipfile file is not a zip file

2009-01-09 Thread Steven D'Aprano
On Fri, 09 Jan 2009 00:46:27 -0800, Carl Banks wrote:

 On Jan 9, 2:16 am, Steven D'Aprano st...@remove-this-
 cybersource.com.au wrote:
 On Thu, 08 Jan 2009 16:47:39 -0800, webcomm wrote:
  The error...
 ...
  BadZipfile: File is not a zip file

  When I look at data.zip in Windows, it appears to be a valid zip
  file. I am able to uncompress it in Windows XP, and can also
  uncompress it with 7-Zip.  It looks like zipfile is not able to read
  a table of contents in the zip file.  That's not a concept I'm
  familiar with.

 No, ZipFile can read table of contents:

     Help on method printdir in module zipfile:

     printdir(self) unbound zipfile.ZipFile method
         Print a table of contents for the zip file.

 In my experience, zip files originating from Windows sometimes have
 garbage at the end of the file. WinZip just ignores the garbage, but
 other tools sometimes don't -- if I recall correctly, Linux unzip
 successfully unzips the file but then complains that the file was
 corrupt. It's possible that you're running into a similar problem.
 
 
 The zipfile format is kind of brain dead, you can't tell where the end
 of the file is supposed to be by looking at the header.  If the end of
 file hasn't yet been reached there could be more data.  To make matters
 worse, somehow zip files came to have text comments simply appended to
 the end of them.  (Probably this was for the benefit of people who would
 cat them to the terminal.)
 
 Anyway, if you see something that doesn't adhere to the zipfile format,
 you don't have any foolproof way to know if it's because the file is
 corrupted or if it's just an appended comment.

Yes, this has lead to a nice little attack vector, using hostile Java 
classes inside JAR files (a variant of ZIP).

http://www.infoworld.com/article/08/08/01/
A_photo_that_can_steal_your_online_credentials_1.html

or http://snipurl.com/9oh0e



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


Fatal Python error: ceval: tstate mix-up

2009-01-09 Thread Laszlo Nagy
After upgrading my system, a program started to throw this error, and 
make a core dump:


Fatal Python error: ceval: tstate mix-up

Kernel log says:

Jan  9 05:06:49 shopzeus kernel: pid 89184 (python), uid 1024: exited on 
signal 6 (core dumped)



I found out that this can happen only when executing Python code without 
holding the GIL. My program is written entriely in Python, but uses some 
third part libraries:


py25-json
py25-psycopg2

Here is Python version:

Python 2.5.2 (r252:60911, Nov 17 2008, 22:19:20)
[GCC 4.2.1 20070719  [FreeBSD]] on freebsd7

(OS is FreeBSD 7.0 amd64 stable on dual quad core xeon 5420, it that 
matters...)


I have the code dump, but I do not know how to debug that. What should I do?

Thanks,

  Laszlo


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


Re: Implementing file reading in C/Python

2009-01-09 Thread Steven D'Aprano
On Fri, 09 Jan 2009 19:33:53 +1000, James Mills wrote:

 On Fri, Jan 9, 2009 at 7:15 PM, Marc 'BlackJack' Rintsch
 bj_...@gmx.net wrote:
 print(Filesize   : %d % (filesize)) print(Image size :
 %dx%d % (width, height)) print(Bytes per Pixel: %d % (blocksize))

 Why parentheses around ``print``\s argument?  In Python 3 ``print``
 is a statement and not a function.
 
 Not true as of 2.6+ and 3.0+
 
 print is now a function.


Not so. print is still a statement in 2.6.

$ python2.6
Python 2.6.1 (r261:67515, Dec 24 2008, 00:33:13)
[GCC 4.1.2 20070502 (Red Hat 4.1.2-12)] on linux2
Type help, copyright, credits or license for more information.
 
 print 23
23





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


Re: Implementing file reading in C/Python

2009-01-09 Thread Steven D'Aprano
On Fri, 09 Jan 2009 09:15:20 +, Marc 'BlackJack' Rintsch wrote:

 picture = { }
 havepixels = 0
 while True:
  data = f.read(blocksize)
  if len(data) = 0: break
 
 if data:
 break
 
 is enough.


You've reversed the sense of the test. The OP exits the loop when data is 
empty, you exit the loop when it *isn't* empty.



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


Re: Encrypted Logging in python

2009-01-09 Thread Steven D'Aprano
On Fri, 09 Jan 2009 00:21:09 -0800, koranthala wrote:

 I was wondering if there is a mechanism to encrypt logging automatically
 in python.
The issue is as follows:
 (a) An application (after py2exe) will go as executable and there
 is no need for the user to know that it is written in python. If an
 exception occurs and it is logged, then the user can understand it is
 written in python.
 (b) A security threat. If an exception occurs, the code is seen by
 the user - and possibly be misused.

Security by obscurity is not security. If your application isn't secure 
against people who know what language is written in, then it isn't secure.




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


Re: Implementing file reading in C/Python

2009-01-09 Thread Marc 'BlackJack' Rintsch
On Fri, 09 Jan 2009 04:04:41 +0100, Johannes Bauer wrote:

 As this was horribly slow (20 Minutes for a 2GB file) I coded the whole
 thing in C also:

Yours took ~37 minutes for 2 GiB here.  This just ~15 minutes:

#!/usr/bin/env python
from __future__ import division, with_statement
import os
import sys
from collections import defaultdict
from functools import partial
from itertools import imap


def iter_max_values(blocks, block_count):
for i, block in enumerate(blocks):
histogram = defaultdict(int)
for byte in block:
histogram[byte] += 1

yield max((count, byte)
  for value, count in histogram.iteritems())[1]

if i % 1024 == 0:
print 'Progresss: %.1f%%' % (100 * i / block_count)


def write_pgm(filename, width, height, pixel_values):
with open(filename, 'w') as pgm_file:
pgm_file.write('P2\n'
'# CREATOR: Crappyass Python Script\n'
'%d %d\n'
'255\n' % (width, height))
pgm_file.writelines('%d\n' % value for value in pixel_values)


def main():
filename = sys.argv[1]
filesize = os.path.getsize(filename)

width = 1024
height = 1024
pixels = width * height
blocksize = filesize // width // height

print 'Filesize   : %d' % filesize
print 'Image size : %dx%d' % (width, height)
print 'Bytes per Pixel: %d' % blocksize

with open(filename, 'rb') as data_file:
blocks = iter(partial(data_file.read, blocksize), '')
pixel_values = imap(ord, iter_max_values(blocks, pixels))
write_pgm(filename + '.pgm', width, height, pixel_values)


if __name__ == '__main__':
main()


Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best practice in organize classes into modules

2009-01-09 Thread Bruno Desthuilliers

Steven Woody a écrit :

On Fri, Jan 9, 2009 at 4:54 PM, Bruno Desthuilliers
bruno.42.desthuilli...@websiteburo.invalid wrote:

Steven Woody a écrit :

On Fri, Jan 9, 2009 at 1:02 PM, James Mills
prolo...@shortcircuit.net.au wrote:

On Fri, Jan 9, 2009 at 2:57 PM, Steven Woody narkewo...@gmail.com
wrote:

In C++/Java, people usually put one class into one file.  What's the
suggestion on this topic in Python?  I so much interesting this
especially when exception classes also involved.

Normally i group related functionality into the one module.

Will that lead to too large source file size?

When the case happens, then you can safely refactor the module into a
package with submodules, and use the package's __init__.py to make it a
facade for the submodules so the refactoring is transparent for client code.


really a smart idea.


Mostly a common Python idiom.


 Did you mean putting some 'import statement in
__init__.py and use the old module name as the new package name?


Yes, you guessed.

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


Re: Implementing file reading in C/Python

2009-01-09 Thread Steve Holden
Marc 'BlackJack' Rintsch wrote:
 On Fri, 09 Jan 2009 04:04:41 +0100, Johannes Bauer wrote:
[...]
 print(Filesize   : %d % (filesize)) print(Image size : %dx%d
 % (width, height)) print(Bytes per Pixel: %d % (blocksize))
 
 Why parentheses around ``print``\s argument?  In Python 3 ``print`` is 
 a statement and not a function.
 
Portability?

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: BadZipfile file is not a zip file

2009-01-09 Thread John Machin
On Jan 9, 7:46 pm, Carl Banks pavlovevide...@gmail.com wrote:
 On Jan 9, 2:16 am, Steven D'Aprano st...@remove-this-





 cybersource.com.au wrote:
  On Thu, 08 Jan 2009 16:47:39 -0800, webcomm wrote:
   The error...
  ...
   BadZipfile: File is not a zip file

   When I look at data.zip in Windows, it appears to be a valid zip file.
   I am able to uncompress it in Windows XP, and can also uncompress it
   with 7-Zip.  It looks like zipfile is not able to read a table of
   contents in the zip file.  That's not a concept I'm familiar with.

  No, ZipFile can read table of contents:

      Help on method printdir in module zipfile:

      printdir(self) unbound zipfile.ZipFile method
          Print a table of contents for the zip file.

  In my experience, zip files originating from Windows sometimes have
  garbage at the end of the file. WinZip just ignores the garbage, but
  other tools sometimes don't -- if I recall correctly, Linux unzip
  successfully unzips the file but then complains that the file was
  corrupt. It's possible that you're running into a similar problem.

 The zipfile format is kind of brain dead, you can't tell where the end
 of the file is supposed to be by looking at the header.  If the end of
 file hasn't yet been reached there could be more data.  To make
 matters worse, somehow zip files came to have text comments simply
 appended to the end of them.  (Probably this was for the benefit of
 people who would cat them to the terminal.)

 Anyway, if you see something that doesn't adhere to the zipfile
 format, you don't have any foolproof way to know if it's because the
 file is corrupted or if it's just an appended comment.

 Most zipfile readers use a heuristic to distinguish.  Python's zipfile
 module just assumes it's corrupted.

 The following post from a while back gives a solution that tries to
 snip the comment off so that zipfile module can handle it.  It might
 help you out.

 http://groups.google.com/group/comp.lang.python/msg/c2008e48368c6543

And here's a little gadget that might help the diagnostic effort; it
shows the archive size and the position of all the magic PKnn
markers. In a normal uncommented archive, EndArchive_pos + 22 ==
archive_size.
8---
# usage: python zip_susser.py name_of_archive.zip
import sys
grimoire = [
(FileHeader,  PK\003\004), # magic number for file
header
(CentralDir,  PK\001\002), # magic number for central
directory
(EndArchive,  PK\005\006), # magic number for end of
archive record
(EndArchive64,PK\x06\x06), # magic token for Zip64
header
(EndArchive64Locator, PK\x06\x07), # magic token for locator
header
]
f = open(sys.argv[1], 'rb')
buff = f.read()
f.close()
blen = len(buff)
print archive size is, blen
for magic_name, magic in grimoire:
pos = 0
while pos  blen:
pos = buff.find(magic, pos)
if pos  0:
break
print %s at %d % (magic_name, pos)
pos += 4
8---

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


Re: Implementing file reading in C/Python

2009-01-09 Thread Steve Holden
Steven D'Aprano wrote:
 On Fri, 09 Jan 2009 19:33:53 +1000, James Mills wrote:
 
 On Fri, Jan 9, 2009 at 7:15 PM, Marc 'BlackJack' Rintsch
 bj_...@gmx.net wrote:
 print(Filesize   : %d % (filesize)) print(Image size :
 %dx%d % (width, height)) print(Bytes per Pixel: %d % (blocksize))
 Why parentheses around ``print``\s argument?  In Python 3 ``print``
 is a statement and not a function.
 Not true as of 2.6+ and 3.0+

 print is now a function.
 
 
 Not so. print is still a statement in 2.6.
 
 $ python2.6
 Python 2.6.1 (r261:67515, Dec 24 2008, 00:33:13)
 [GCC 4.1.2 20070502 (Red Hat 4.1.2-12)] on linux2
 Type help, copyright, credits or license for more information.
 print 23
 23
 
C:\Users\sholden\python26\python
Python 2.6 (r26:66721, Oct  2 2008, 11:35:03) [MSC v.1500 32 bit
(Intel)] on win32
Type help, copyright, credits or license for more information.
 print
built-in function print


OK, I confess I missed out

 from __future__ import print_function

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Line completion with custom commands

2009-01-09 Thread gu


Hi, my Python program can be launched with a range of different options 
(or subcommands) like:


$ myProgram doSomething
$ myProgram doSomethingElse
$ myProgram nowDoSomethingDifferent

I want it to use auto-completion with so that if i type myProgram d it 
returns myProgram doSomething and if i type myProgram n it renders 
myProgram nowDoSomethingDifferent. This is similar to the average use 
of the module rlcompleter, but it does not pick possible completion 
options from the filesystem (or from history) but from a custom set of 
strings (that correspond to the available options for my program)


Any idea on how to implement this?

I'm aware of the variable PYTHONSTARTUP (that should point to a file I 
don't know how to write).


As a working example, django-admin (from the django package) has the 
same exact feature i'm looking for

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


Re: [Python-Dev] compiling python2.5 on linux under wine

2009-01-09 Thread Luke Kenneth Casson Leighton
On Thu, Jan 8, 2009 at 9:07 PM, Martin v. Löwis mar...@v.loewis.de wrote:
  i'd just ... much rather be completely independent of proprietary
 software when it comes to building free software.

 I guess my question is then: why do you want to use Windows in the
 first place?

 ha ha :)  the same question was asked when i started the nt domains
reverse-engineering for samba, in 1996.  the answer is: i don't.  but
there are a lot of users and developers who feel that they don't have
a choice.  or haven't been given one.

 so if it's possible for me, as one of the under 1% of computer users
i.e. linux to compile stuff that will work on the over 95% of
computers used by everyone else i.e. windows _and_ i get to stick to
free software principles, that's gotta be good.

 take pywebkit-gtk as an example.

 the first-level (and some of the second-level) dependencies for
pywebkit-gtk are roughly as follows:

 * libstdc++
 * cairo, pango, gdk, fontconfig, gtk
 * libxml2 (which is dodgy)
 * libxslt1 (which is so dodgy and dependent on incompatible versions
of libxml2 it can't be compiled on win32)
 * libicu38
 * libcurl
 * libssl
 * webkit
 * python2.5
 * python-gobect
 * python-gtk

 that's a *big* ing list that comes in at a whopping 40mb of
_binaries_.  webkit itself comes in at 10mb alone.

 libicu38 fails _miserably_ to cross-compile with mingw32.  i was damn
lucky to have beaten it into submission: it took two days and i
couldn't run any of the tests, but actually managed to get at least
some .libs, .dlls and .a's out of the mess.

  libxslt1 and libxml2 have compile errors in mutually incompatible
versions on win32, plus, unfortunately, the versions that _do_ compile
correctly (really old versions like libxslt-1.12 + libxml2-18 or
something) are not the ones that can be used on webkit!

 i had to get the source code for gcc (4.4) because when linking
webkit against the MSVC-compiled libicu38 gcc actually segfaulted (!).
 and that was tracked down to exception handling across process /
thread boundaries in libstdc++-6 which had only literally been
fixed/patched a few days before i started the monster-compile-process.

 i tried hunting down python-gobject and python-gtk for win32, but
there is a dependency needed before you get to that: python25.lib.
as i mentioned previously i tried hunting down a .lib for python25 but
of course that would be useless unless i also have a libtool-compiled
.a so there wasn't any point.

 so, all the hard work that i did cross-compiling up webkit for win32
was completely wasted because python itself could not be compiled on
linux for a win32 platform.

hence my interest in making sure that it can be.

_then_ i can go back and revisit the monster compile process and
finally come up with the goods, on win32, on the gobject-based
DOM-model manipulation stuff i've added to pywebkit-gtk.  i've got
linux covered, i've got macosx covered.  win32 is the last one.

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


Re: Implementing file reading in C/Python

2009-01-09 Thread mk

Johannes Bauer wrote:


Which takes about 40 seconds. I want the niceness of Python but a little
more speed than I'm getting (I'd settle for factor 2 or 3 slower, but
factor 30 is just too much).


This probably doesn't contribute much, but have you tried using Python 
profiler? You might have *something* wrong that eats up a lot of time in 
the code.


The factor of 30 indeed does not seem right -- I have done somewhat 
similar stuff (calculating Levenshtein distance [edit distance] on words 
read from very large files), coded the same algorithm in pure Python and 
C++ (using linked lists in C++) and Python version was 2.5 times slower.


Regards,
mk


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


Re: Line completion with custom commands

2009-01-09 Thread Steve Holden
gu wrote:
 
 Hi, my Python program can be launched with a range of different options
 (or subcommands) like:
 
 $ myProgram doSomething
 $ myProgram doSomethingElse
 $ myProgram nowDoSomethingDifferent
 
 I want it to use auto-completion with so that if i type myProgram d it
 returns myProgram doSomething and if i type myProgram n it renders
 myProgram nowDoSomethingDifferent. This is similar to the average use
 of the module rlcompleter, but it does not pick possible completion
 options from the filesystem (or from history) but from a custom set of
 strings (that correspond to the available options for my program)
 
 Any idea on how to implement this?
 
 I'm aware of the variable PYTHONSTARTUP (that should point to a file I
 don't know how to write).
 
 As a working example, django-admin (from the django package) has the
 same exact feature i'm looking for

The issue here is that Python doesn't get control until afer you've hit
RETURN on the command line. so nothing you can do in your program or
interpreter setup will have any effect on how the command shell behaves.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: figuring week of the day....

2009-01-09 Thread Tim Chase

Tim Chase wrote:

tekion wrote:

Is there a module where you could figure week of the day, like where
it starts and end. I need to do this for a whole year. Thanks.


the monthcalendar() call returns the whole month's calendar which 
may be more what you want for the big-picture.


And if you want a whole year's worth, you can get pretty close with:

  import itertools as i
  import calendar as c
  for month in range(1,13):
for week in c.monthcalendar(2009, month):
  print repr(w)

You don't detail how you want the month-boundaries to behave, so 
this gives calendar's default behavior of filling in zeros on 
month-boundaries, so November through the 1st week in Dec 2009 
comes back as


  ...
  [0, 0, 0, 0, 0, 0, 1],
  [2, 3, 4, 5, 6, 7, 8],
  [9, 10, 11, 12, 13, 14, 15],
  [16, 17, 18, 19, 20, 21, 22],
  [23, 24, 25, 26, 27, 28, 29],
  [30, 0, 0, 0, 0, 0, 0],
  [0, 1, 2, 3, 4, 5, 6],
  ...

rather than

  ...
  [26, 27, 28, 29, 30, 31, 1],
  [2, 3, 4, 5, 6, 7, 8],
  [9, 10, 11, 12, 13, 14, 15],
  [16, 17, 18, 19, 20, 21, 22],
  [23, 24, 25, 26, 27, 28, 29],
  [30, 1, 2, 3, 4, 5, 6],
  ...

-tkc



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


Re: Line completion with custom commands

2009-01-09 Thread gu

Steve Holden wrote:

gu wrote:

Hi, my Python program can be launched with a range of different options
(or subcommands) like:

$ myProgram doSomething
$ myProgram doSomethingElse
$ myProgram nowDoSomethingDifferent

I want it to use auto-completion with so that if i type myProgram d it
returns myProgram doSomething and if i type myProgram n it renders
myProgram nowDoSomethingDifferent. This is similar to the average use
of the module rlcompleter, but it does not pick possible completion
options from the filesystem (or from history) but from a custom set of
strings (that correspond to the available options for my program)

Any idea on how to implement this?

I'm aware of the variable PYTHONSTARTUP (that should point to a file I
don't know how to write).

As a working example, django-admin (from the django package) has the
same exact feature i'm looking for


The issue here is that Python doesn't get control until afer you've hit
RETURN on the command line. so nothing you can do in your program or
interpreter setup will have any effect on how the command shell behaves.

regards
 Steve


I see, but how does django-admin work, then?

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


Re: Line completion with custom commands

2009-01-09 Thread Marco Mariani

gu wrote:


I see, but how does django-admin work, then?


from bash:

complete -W doSomething doSomethingElse doSomethingDifferent myProgram
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-09 Thread Aaron Brady
On Jan 9, 4:01 am, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:
 On Thu, 08 Jan 2009 18:33:50 +, Mark Wooding wrote:
  [Steven's message hasn't reached my server, so I'll reply to it here.
  Sorry if this is confusing.]

  Aaron Brady castiro...@gmail.com wrote:
  On Jan 8, 1:45 am, Steven D'Aprano
  ste...@remove.this.cybersource.com.au wrote:
   On Wed, 07 Jan 2009 10:17:55 +, Mark Wooding wrote:

The `they're just objects' model is very simple, but gets tied up
in knots explaining things.  The `it's all references' model is
only a little more complicated, but explains everything.

   But it *over* explains, because it implies things that everybody
   knows about references in other languages that aren't true for
   Python.

  I addressed this elsewhere.  Summary: `pass-by-reference' is a different
  thing to `all you manipulate are references':

 You know, I've written a fair bit of Python code over the years, and I've
 never manipulated a reference *once*. Ints, strings, floats, lists,
 tuples... but references? Never.
snip
 That's why we should try to keep the different layers of explanation
 separate, without conflating them. Python programmers don't actually flip
 bits, and neither do they manipulate references. Python programmers don't
 have access to bits, or references. What they have access to is objects.
snip
   How do we deal with anonymous objects in your model?
  What I am pretty sure of is that references are going to have to enter
  the picture at some point, because other models get too complicated.

 Well, I dare say that at *some* point all models are insufficient. The
 map is not the territory, and there's always something that gets left
 out. But I think your model with strings is more complicated: robots,
 sticky Blu-Tack, string that you can't touch or see, and so forth.
 Compared to that, TARDIS technology enabling objects to be in two places
 at once is remarkably straightforward. Despite it being physically
 unrealistic, it's logically simple.

 --
 Steven

Possible compromise.  You can think of functions as mutation-only.
You pass the object, and it gets a new (additional) name.  The old
name doesn't go in.  /compromise

Regardless, IMO, references don't add any explanatory power; they just
make you feel cocky that you know what they are.

M: If 'fun()' returned a reference, you would be able to assign to it.
m: You can't assign to it.
C: It doesn't return a reference.

-- Why can't I assign to a function call?
-- Python variables are references only.
-- Ok, why can't I assign to a function call?
-- [Explanation Steven is trying to give.]

In other words, Mark and Joe, cut to the chase.  You're shirking.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Line completion with custom commands

2009-01-09 Thread Steve Holden
gu wrote:
 Steve Holden wrote:
 gu wrote:
 Hi, my Python program can be launched with a range of different options
 (or subcommands) like:

 $ myProgram doSomething
 $ myProgram doSomethingElse
 $ myProgram nowDoSomethingDifferent

 I want it to use auto-completion with so that if i type myProgram d it
 returns myProgram doSomething and if i type myProgram n it renders
 myProgram nowDoSomethingDifferent. This is similar to the average use
 of the module rlcompleter, but it does not pick possible completion
 options from the filesystem (or from history) but from a custom set of
 strings (that correspond to the available options for my program)

 Any idea on how to implement this?

 I'm aware of the variable PYTHONSTARTUP (that should point to a file I
 don't know how to write).

 As a working example, django-admin (from the django package) has the
 same exact feature i'm looking for

 The issue here is that Python doesn't get control until afer you've hit
 RETURN on the command line. so nothing you can do in your program or
 interpreter setup will have any effect on how the command shell behaves.

 regards
  Steve
 
 I see, but how does django-admin work, then?
 
Probably the issue you has was not knowing the name of the feature. When
I asked Google to tell me about django-admin command completion it said:

  http://code.djangoproject.com/ticket/1240

as the first hit. As you will see, this is done by configuring the shell
to know about the specific commands it has to deal with. Enjoy!

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Line completion with custom commands

2009-01-09 Thread gu

Marco Mariani wrote:

gu wrote:


I see, but how does django-admin work, then?


from bash:

complete -W doSomething doSomethingElse doSomethingDifferent myProgram


This worked like a charm, thank you so much. Is this available for bash 
only or any shell?

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


Re: [Help] The pywinauto Can't select the MDI's menu using the MenuItems() which return [].

2009-01-09 Thread Korobase
2008/12/22 Simon Brunning si...@brunningonline.net

 2008/12/21  gagsl-...@yahoo.com.ar:
  The code below opens the Choose Font dialog on my Spanish Windows
 version:
 
  py from pywinauto.application import Application
  py app = Application.start(Notepad.exe)

 Notepad's menus are build with MFC. Word's menus are not. Trust me,
 give it up. For automating Word, COM (with Mark Hammond's excellent
 Python/COM bridge) is the only way to go.


Yeah.
But the GuiLib program also don't work.
You can dowload sample from http://www.beyondata.com/
and use the pywinauto to test it.
I want to know how to do the GuiLib's automation.




 --
 Cheers,
 Simon B.
 si...@brunningonline.net
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
OpenBookProject-开放图书计划邮件列表
详情: http://groups.google.com/group/OpenBookProject
维基: http://wiki.woodpecker.org.cn/
--
http://mail.python.org/mailman/listinfo/python-list


Re: drive a desktop app from python?

2009-01-09 Thread zugn...@gmail.com
 Is there any lib or recipe(s) for doing something like this via python?


I've used pywinauto to do something similar, and found it good.

http://pywinauto.openqa.org/


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


Re: Unexpected scientific notation

2009-01-09 Thread Mark Dickinson
On Jan 8, 1:00 am, Paul McNett p...@ulmcnett.com wrote:
 It displays '3E+1' instead of '30.0'.

 As I can't reproduce I'm looking for an idea brainstorm of what could be 
 causing
 this. What would be choosing to display such a normal number in scientific 
 notation?

 Ideas?

[I thought I replied to this earlier, but the post isn't showing up.
So here it is again.]

I suspect it's your use of the Decimal normalize() method that's
causing
this.  Trailing zeros on Decimal instances are significant, so
Decimal('30.0'), Decimal('30') and Decimal('3E+1') are considered
distinct (though they all have the same value).  The normalize method
strips all trailing zeros, turning Decimal('30.0') into Decimal('3E
+1').

One way to get around this is to add 0 after normalizing: this will
make sure that scientific notation is used only for very large
or small numbers, as usual.

Python 2.7a0 (trunk:68298:68318, Jan  6 2009, 10:39:14)
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Type help, copyright, credits or license for more information.
 from decimal import Decimal
 0 + Decimal('3E1')
Decimal('30')
 Decimal('0.0') + Decimal('3E1')
Decimal('30.0')

Adding 0 also has the side-effect of turning a negative zero
into a positive zero, but I suspect that this isn't going to
worry you much.  :)

You might also want to look at the Decimal.quantize method.

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


Re: Fatal Python error: ceval: tstate mix-up

2009-01-09 Thread Laszlo Nagy

I could start gdb python python.core but don't know what it means.
Unfortunately, there are no debugging symbols.


%gdb /usr/local/bin/python python.core
GNU gdb 6.1.1 [FreeBSD]
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type show copying to see the conditions.
There is absolutely no warranty for GDB.  Type show warranty for details.
This GDB was configured as amd64-marcel-freebsd...(no debugging
symbols found)...
Core was generated by `python'.
Program terminated with signal 6, Aborted.
Reading symbols from /lib/libutil.so.7...(no debugging symbols
found)...done.
Loaded symbols for /lib/libutil.so.7
Reading symbols from /lib/libm.so.5...(no debugging symbols found)...done.
Loaded symbols for /lib/libm.so.5
Reading symbols from /lib/libthr.so.3...(no debugging symbols found)...done.
Loaded symbols for /lib/libthr.so.3
Reading symbols from /lib/libc.so.7...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.7
Reading symbols from /usr/local/lib/python2.5/lib-dynload/time.so...(no
debugging symbols found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/time.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/collections.so...(no debugging
symbols found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/collections.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/datetime.so...(no debugging symbols
found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/datetime.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/cStringIO.so...(no debugging
symbols found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/cStringIO.so
Reading symbols from /usr/local/lib/python2.5/lib-dynload/strop.so...(no
debugging symbols found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/strop.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/operator.so...(no debugging symbols
found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/operator.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/_struct.so...(no debugging symbols
found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/_struct.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/binascii.so...(no debugging symbols
found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/binascii.so
Reading symbols from
/usr/local/lib/python2.5/site-packages/sgmlop.so...(no debugging symbols
found)...done.
Loaded symbols for /usr/local/lib/python2.5/site-packages/sgmlop.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/pyexpat.so...(no debugging symbols
found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/pyexpat.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/_socket.so...(no debugging symbols
found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/_socket.so
Reading symbols from /usr/local/lib/python2.5/lib-dynload/_ssl.so...(no
debugging symbols found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/_ssl.so
Reading symbols from /usr/lib/libssl.so.5...(no debugging symbols
found)...done.
Loaded symbols for /usr/lib/libssl.so.5
Reading symbols from /lib/libcrypto.so.5...(no debugging symbols
found)...done.
Loaded symbols for /lib/libcrypto.so.5
Reading symbols from /usr/local/lib/python2.5/lib-dynload/math.so...(no
debugging symbols found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/math.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/_random.so...(no debugging symbols
found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/_random.so
Reading symbols from /usr/local/lib/python2.5/lib-dynload/fcntl.so...(no
debugging symbols found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/fcntl.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/select.so...(no debugging symbols
found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/select.so
Reading symbols from
/usr/local/lib/python2.5/site-packages/OpenSSL/rand.so...(no debugging
symbols found)...done.
Loaded symbols for /usr/local/lib/python2.5/site-packages/OpenSSL/rand.so
Reading symbols from
/usr/local/lib/python2.5/site-packages/OpenSSL/crypto.so...(no debugging
symbols found)...done.
Loaded symbols for /usr/local/lib/python2.5/site-packages/OpenSSL/crypto.so
Reading symbols from
/usr/local/lib/python2.5/site-packages/OpenSSL/SSL.so...(no debugging
symbols found)...done.
Loaded symbols for /usr/local/lib/python2.5/site-packages/OpenSSL/SSL.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/cPickle.so...(no debugging symbols
found)...done.
Loaded symbols for /usr/local/lib/python2.5/lib-dynload/cPickle.so
Reading symbols from
/usr/local/lib/python2.5/lib-dynload/_weakref.so...(no debugging symbols
found)...done.
Loaded symbols 

pySerial - accessing GSM module failed

2009-01-09 Thread Dave Dave
Hello all,
I'm newbie in the serial buissness and I beed some Help. I'm a student at my
last year and I got final assaignment.
My goal is to comunicate with SIM free, GSM Module through computer. I want
to simulate SIM card by receiving and transferring data from my code.

In order to understand how to comunicate with the GSM module I took an
existing module which gut RS232 connector and connected it to the computer.
I ran my python code:
class MySerial():
 def __init__ (self,port,baundrate,)
  self.s = serial.Serial(port,baundreate,)

 def read(self,size=1):
   txt = self.s.read(size)

def write(self,text=''):txt = self.s.write(text)

def main():
  obj = MySerial('COM1')
  obj.write('ATI')
   sleep(1)
  obj.read(10)

the code run and I send the comman 'ATI' which ask the GSM Module if
everything is OK.
the read line returs 'ATI'. echo to my write function.

Whenever I run this comman ('ATI') through hyperterminal I gut the result :
232 OK

Does any one got an Idea why my code isn't getting the same answer from the
GSM Module?
thanks
dave
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-09 Thread Johannes Bauer
Marc 'BlackJack' Rintsch schrieb:

 f = open(sys.argv[1], r)
 
 Mode should be 'rb'.

Check.

 filesize = os.stat(sys.argv[1])[6]
 
 `os.path.getsize()` is a little bit more readable.

Check.

 print(Filesize   : %d % (filesize)) print(Image size : %dx%d
 % (width, height)) print(Bytes per Pixel: %d % (blocksize))
 
 Why parentheses around ``print``\s argument?  In Python 3 ``print`` is 
 a statement and not a function.

I write all new code to work under Python3.0. Actually I develop on
Python 3.0 but the code is currently deployed onto 2.6.

 picture = { }
 havepixels = 0
 while True:
  data = f.read(blocksize)
  if len(data) = 0: break
 
 if data:
 break
 
 is enough.
 
  datamap = { }
  for i in range(len(data)):
  datamap[ord(data[i])] = datamap.get(data[i], 0) + 1
 
 Here you are creating a list full of integers to use them as index into 
 `data` (twice) instead of iterating directly over the elements in 
 `data`.  And you are calling `ord()` for *every* byte in the file 
 although you just need it for one value in each block.  If it's possible 
 to write the raw PGM format this conversion wouldn't be necessary at all.

OK, those two are just stupid, you're right. I changed it to:

datamap = { }
for i in data:
datamap[i] = datamap.get(i, 0) + 1

array = sorted([(b, a) for (a, b) in datamap.items()], reverse=True)
most = ord(array[0][1])
pic.write(%d\n % (most))


 For the `datamap` a `collections.defaultdict()` might be faster.

Tried that, not much of a change.

  maxchr = None
  maxcnt = None
  for (char, count) in datamap.items():
  if (maxcnt is None) or (count  maxcnt):
  maxcnt = count
  maxchr = char
 
 Untested:
 
 maxchr = max((i, c) for c, i in datamap.iteritems())[1]

This is nice, I use it - the sort thing was a workaround anyways.

  most = maxchr
 
 Why?

I don't really know anymore :-\

  posx = havepixels % width
  posy = havepixels / width
 
 posx, posy = divmod(havepixels, width)

That's a nice one.

 Why are you using a dictionary as 2d array?  In the C code you simply 
 write the values sequentially, why can't you just use a flat list and 
 append here?

Yup, I changed the Python code to behave the same way the C code did -
however overall it's not much of an improvement: Takes about 15 minutes
to execute (still factor 23).

Thanks for all your pointers!

Kind regards,
Johannes

-- 
Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie.
 -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
 48d8bf1d$0$7510$54022...@news.sunrise.ch
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-09 Thread Johannes Bauer
James Mills schrieb:

 What does this little tool do anyway ?
 It's very interesting the images it creates
 out of files. What is this called ?

It has no particular name. I was toying around with the Princeton Cold
Boot Attack (http://citp.princeton.edu/memory/). In particular I was
interested in how much memory is erased when I would (on my system)
enable the slow POST (which counts through all RAM three times).

I downloaded the provided utitilities, dumped my system memory via PXE
boot onto another system after resetting it hard in the middle of a
running Linux session. I did sync, though. Praise all journaling
filesystems.

As a 2GB file is not really of much use for telling where something is
and where isn't, I thought of that picture coloring. In a 1024x1024
picture a pixel is 2048 bytes with 2GB of RAM, so exactly half a page.
This is sufficiently high resolution to detect what's in there.

 I'm curious :) I haven't had much tiem to
 optimize it yet - I'll try to when I get home from work.

Thanks for your effort, I appreciate it... hope my work leads to some
meaningful results. Currently it looks (*cough* if there aren't bugs in
my picture code) as if my PC would reset the whole RAM anyways, although
I do not have any ECC. Strange.

Kind regards,
Johannes

-- 
Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie.
 -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
 48d8bf1d$0$7510$54022...@news.sunrise.ch
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-09 Thread Johannes Bauer
Marc 'BlackJack' Rintsch schrieb:
 On Fri, 09 Jan 2009 04:04:41 +0100, Johannes Bauer wrote:
 
 As this was horribly slow (20 Minutes for a 2GB file) I coded the whole
 thing in C also:
 
 Yours took ~37 minutes for 2 GiB here.  This just ~15 minutes:

Ah, ok... when implementing your suggestions int he other post, I did
not get such a drastic performance increase. I really will have a look
at it and try to locate where I'm wasting the time.

Thanks a lot,
Kind regards,
Johannes

-- 
Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie.
 -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
 48d8bf1d$0$7510$54022...@news.sunrise.ch
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-09 Thread Johannes Bauer
mk schrieb:
 Johannes Bauer wrote:
 
 Which takes about 40 seconds. I want the niceness of Python but a little
 more speed than I'm getting (I'd settle for factor 2 or 3 slower, but
 factor 30 is just too much).
 
 This probably doesn't contribute much, but have you tried using Python
 profiler? You might have *something* wrong that eats up a lot of time in
 the code.

No - and I've not known there was a profiler yet have found anything
meaningful (there seems to be an profiling C interface, but that won't
get me anywhere). Is that a seperate tool or something? Could you
provide a link?

 The factor of 30 indeed does not seem right -- I have done somewhat
 similar stuff (calculating Levenshtein distance [edit distance] on words
 read from very large files), coded the same algorithm in pure Python and
 C++ (using linked lists in C++) and Python version was 2.5 times slower.

Yup, that was about what I had expected (and what I could well live
with, it's a tradeoff).

Thanks,
Kind regards,
Johannes

-- 
Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie.
 -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
 48d8bf1d$0$7510$54022...@news.sunrise.ch
--
http://mail.python.org/mailman/listinfo/python-list


Re: Force exception on attribute write access only one object

2009-01-09 Thread Thomas Guettler
Hi Peter and others,

your idea was good, but it does not work with Django ORM Models:

Traceback (most recent call last):
  File /localhome/modw/django/core/handlers/base.py, line 87, in get_response
response = callback(request, *callback_args, **callback_kwargs)
  File /localhome/modw/foo/views/filter.py, line 473, in add
return edit(request, 'add')
  File /localhome/modw/foo/views/filter.py, line 493, in edit
filter=form.save()
  File /localhome/modw/foo/views/filter.py, line 457, in save
action=form.save()
  File /localhome/modw/django/forms/models.py, line 315, in save
if self.instance.pk is None:
  File /localhome/modw/django/db/models/base.py, line 292, in _get_pk_val
return getattr(self, meta.pk.attname)
AttributeError: 'MyAction' object has no attribute 'filter_action_ptr_id'


Peter Otten schrieb:
 Thomas Guettler wrote:
 
 for debugging I want to raise an exception if an attribute is
 changed on an object. Since it is only for debugging I don't want
 to change the integer attribute to a property.
 
 Why?
  
 This should raise an exception:

 myobj.foo=1

 Background:
 Somewhere this value gets changed. But I don't now where.
 
 If you change your mind:
 
 class A(object):
 def __init__(self): 
 self.foo = 42
 
 a = A()
 b = A()
 
 class B(A):
 @property
 def foo(self):
 return self.__dict__[foo]
 
 b.__class__ = B
 
 a.foo = whatever
 print b.foo
 b.foo = whatever
 
 Peter


-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
--
http://mail.python.org/mailman/listinfo/python-list


Re: Encrypted Logging in python

2009-01-09 Thread koranthala
On Jan 9, 3:16 pm, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:
 On Fri, 09 Jan 2009 00:21:09 -0800, koranthala wrote:
  I was wondering if there is a mechanism to encrypt logging automatically
  in python.
     The issue is as follows:
      (a) An application (after py2exe) will go as executable and there
  is no need for the user to know that it is written in python. If an
  exception occurs and it is logged, then the user can understand it is
  written in python.
      (b) A security threat. If an exception occurs, the code is seen by
  the user - and possibly be misused.

 Security by obscurity is not security. If your application isn't secure
 against people who know what language is written in, then it isn't secure.

 --
 Steven

I understand that completely.
My point is that even though I can try to make the application
completely secure - I can never be sure of that. Especially if your
company is a very small one - and might not be able to have the best
programmers around. So, another layer of security - even security
through obscurity - can give that bit extra time in which the bugs in
the system can be ironed out.

Also, what I am asking is a generic option in logging - which can help
the adoption of the logging framework in even closed source systems.
It is not just about security - just that a closed source company
might be much more comfortable in using the system if crypt is there.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Fatal Python error: ceval: tstate mix-up

2009-01-09 Thread Laszlo Nagy
Meanwhile I'm trying to turn off threads in that program one by one. I 
just got this new type of error:


Fatal Python error: PyThreadState_Delete: invalid tstate



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


win32gui

2009-01-09 Thread Gandalf
Hi, everyone
I'm searching the win32gui lib to find a way to get the text under the
user cursor.
so far I managed to find only the controller ID which under the cursor
this way

cursorID = win32gui.WindowFromPoint(win32gui.GetCursorPos())

their is function called GetWindowText I tried to use but it doesn't
work in most of the time

someone know a way to do this?

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


Re: win32gui

2009-01-09 Thread TheSeeker
On Jan 9, 7:34 am, Gandalf goldn...@gmail.com wrote:
 Hi, everyone
 I'm searching the win32gui lib to find a way to get the text under the
 user cursor.
 so far I managed to find only the controller ID which under the cursor
 this way

 cursorID = win32gui.WindowFromPoint(win32gui.GetCursorPos())

 their is function called GetWindowText I tried to use but it doesn't
 work in most of the time

 someone know a way to do this?

 thank you!

One app that might be of help with your endeavors is called AutoIt
(http://www.autoitscript.com/autoit3/)

This suite of apps has a nice window browser, which shows most (if mot
all) information available from a Windows window. Sometimes not all
text in a window is available, depending on the window. It even comes
with a COM component, so it can be scripted from Python...

No affiliation, other than a contented user.

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


Re: Encrypted Logging in python

2009-01-09 Thread pruebauno
On Jan 9, 8:02 am, koranth...@gmail.com wrote:
 Also, what I am asking is a generic option in logging - which can help
 the adoption of the logging framework in even closed source systems.
 It is not just about security - just that a closed source company
 might be much more comfortable in using the system if crypt is there.

Python is an open source project. Many people that read this list
don't like closed source code too much and are not willing to invest
time to work in features like this. You might get lucky and somebody
that is interested in the topic might give you some tips.
--
http://mail.python.org/mailman/listinfo/python-list


Python Apache Handler

2009-01-09 Thread Scooter
Does anyone have any good examples, or links thereto for using python
as an Apache handler? And I should qualify all of this by saying I'm a
python newbie, and while having experience with Apache, I've never
done anything outside whats in the box .

What I'm looking for is how one might use Python not from the CGI/
presentation side but more on the backend...i.e. for each page Apache
serves up examine the request and update some headers, or add a cookie
to the response. Or possibly use Python for writing a custom Apache
logger. I've searched the web but typically end up with pages for
mod_python and writing CGI scripts.

And if you feel this is better posted in an Apache group vs. here, let
me apologize up front.

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


Re: Python Imaging Library and textmate

2009-01-09 Thread Philip Semanchuk


On Jan 9, 2009, at 2:49 AM, bilgin arslan wrote:


Hello,
I am a beginner in python and I am trying to create image files that  
contain

lines from a text file.
I am trying to do this with PIL, which seems like a suitable tool. I  
have a

copy of TextMate(1.5.8) and I run Macosx 10.5.6

The problem is, even though I installed PIL correctly (I get no  
errors from
import Image in IDLE or in terminal), I can't get TextMate to  
import the

module.
I keep getting ImportError: No module named Image error.
I tried reloading TextMate's bundles but that did not work either.


I use TextMate but I don't know what you mean when you say you can't  
get TM to import a module. Can you give a little more context?



There was a similar thread in this list with textmate but I can't  
understand

how that issue was solved in the end.


link?



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


Re: win32gui

2009-01-09 Thread Gandalf
thank you I'm checking autoit documentation...
--
http://mail.python.org/mailman/listinfo/python-list


Re: BadZipfile file is not a zip file

2009-01-09 Thread webcomm
On Jan 9, 3:16 am, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:
 The full signature of ZipFile is:

 ZipFile(file, mode=r, compression=ZIP_STORED, allowZip64=True)

 Try passing compression=zipfile.ZIP_DEFLATED and/or allowZip64=False and
 see if that makes any difference.

Those arguments didn't make a difference in my case.

 The zip format does support alternative compression methods, it's
 possible that this particular file uses a different sort of compression
 which Python doesn't deal with.

  What would cause a zip file to not have a table of contents?

 What makes you think it doesn't have one?

Because when I search for the file is not a zip file error in
zipfile.py, there is a function that checks for a table of contents.
Tho it looks like there are other ideas in this thread about what
might cause that error... I'll keep reading...

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


Re: BadZipfile file is not a zip file

2009-01-09 Thread webcomm
On Jan 9, 3:46 am, Carl Banks pavlovevide...@gmail.com wrote:
 The zipfile format is kind of brain dead, you can't tell where the end
 of the file is supposed to be by looking at the header.  If the end of
 file hasn't yet been reached there could be more data.  To make
 matters worse, somehow zip files came to have text comments simply
 appended to the end of them.  (Probably this was for the benefit of
 people who would cat them to the terminal.)

 Anyway, if you see something that doesn't adhere to the zipfile
 format, you don't have any foolproof way to know if it's because the
 file is corrupted or if it's just an appended comment.

 Most zipfile readers use a heuristic to distinguish.  Python's zipfile
 module just assumes it's corrupted.

 The following post from a while back gives a solution that tries to
 snip the comment off so that zipfile module can handle it.  It might
 help you out.

 http://groups.google.com/group/comp.lang.python/msg/c2008e48368c6543

 Carl Banks

Thanks Carl.  I tried Scott's getzip() function yesterday... I
stumbled upon it in my searches.  It didn't seem to help in my case,
though it did produce a different error:  ValueError, substring not
found.  Not sure what that means.
--
http://mail.python.org/mailman/listinfo/python-list


struct unpack to pre-allocated array?

2009-01-09 Thread Rich Henry
Is there any way to struct.unpack or struct.unpack_from into an existing
array.array or similar structure? I am unpacking file data in a loop and i
was hoping to find something that performs better than simply unpacking into
a new tuple each iteration.

Thanks in advance,
Rich
--
http://mail.python.org/mailman/listinfo/python-list


Re: BadZipfile file is not a zip file

2009-01-09 Thread Chris Mellon
On Fri, Jan 9, 2009 at 9:05 AM, webcomm rya...@gmail.com wrote:
 On Jan 9, 3:46 am, Carl Banks pavlovevide...@gmail.com wrote:
 The zipfile format is kind of brain dead, you can't tell where the end
 of the file is supposed to be by looking at the header.  If the end of
 file hasn't yet been reached there could be more data.  To make
 matters worse, somehow zip files came to have text comments simply
 appended to the end of them.  (Probably this was for the benefit of
 people who would cat them to the terminal.)

 Anyway, if you see something that doesn't adhere to the zipfile
 format, you don't have any foolproof way to know if it's because the
 file is corrupted or if it's just an appended comment.

 Most zipfile readers use a heuristic to distinguish.  Python's zipfile
 module just assumes it's corrupted.

 The following post from a while back gives a solution that tries to
 snip the comment off so that zipfile module can handle it.  It might
 help you out.

 http://groups.google.com/group/comp.lang.python/msg/c2008e48368c6543

 Carl Banks

 Thanks Carl.  I tried Scott's getzip() function yesterday... I
 stumbled upon it in my searches.  It didn't seem to help in my case,
 though it did produce a different error:  ValueError, substring not
 found.  Not sure what that means.
 --
 http://mail.python.org/mailman/listinfo/python-list


This is a ticket about another issue or 2 with invalid zipfiles that
the zipfile module won't load, but that other tools will compensate
for:

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


Re: Implementing file reading in C/Python

2009-01-09 Thread pruebauno
On Jan 9, 8:48 am, Johannes Bauer dfnsonfsdu...@gmx.de wrote:
 No - and I've not known there was a profiler yet have found anything
 meaningful (there seems to be an profiling C interface, but that won't
 get me anywhere). Is that a seperate tool or something? Could you
 provide a link?
 Thanks,
 Kind regards,
 Johannes

It is part of the python standard library:

http://docs.python.org/library/profile.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-09 Thread Joe Strout

ru...@yahoo.com wrote:


  a = array (1,2,3)
  b = a
  a[1] = 4
  print b

C, C++, VBA, Fortran, Perl:  1, 2, 3
Python:  1, 4, 3

You are mistaken


I don't think so.
See http://groups.google.com/group/comp.lang.python/msg/f99d5a0d8f869b96
The code I quoted there was tested.
In the C/C++ case, array-to-pointer coercion confuses
the issue so I embedded the array in a struct which
is more like an object.


No, that's cheating (IMHO).  Structs used directly (rather than via
pointers) are the odd beast, and while they're certainly possible in C,
they are far from the normal idiom.  And certainly embedding an array in
a struct just to force it to be copied, avoiding the standard reference
semantics, is an arbitrary trick.

I never claimed that you *couldn't* have copy semantics in C; you can do
almost anything you want in C (or C++).  But the *normal* usage of an
array is via a pointer, in which case the semantics are exactly the same
as in Python, Java, REALbasic, .NET, etc.


(Keep in mind
my point was not to show the behavior of arrays, but to
show that several common languages *do not* use Python's
*all* names are references model -- though of course
this does not preclude their having some Python-like
assignments since they all have some way of doing
references.)


Ah.  OK then, I guess I missed you're point.  You're absolutely right;
many languages have both reference types and value types.  Python is a
bit unusual in that it has only reference types.  I would have picked a
different example to illustrate that, but it's true nonetheless.

Best,
- Joe



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


Re: sftp with no password from python

2009-01-09 Thread loial
On 8 Jan, 18:49, Mike Hjorleifsson mhjorleifs...@gmail.com wrote:
 On Jan 8, 10:39 am, loial jldunn2...@googlemail.com wrote:

  Is it possible to usesftpwithout a password from python?

 Yes you can use keys you preestablish between the server and client so
 you dont need passwords, i do this on all my servers then lock off the
 ability to accept passwords at all, this way no one can dictionary
 attack my ssh servers.

Thanks...do you have any python examples of using sftp?

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


Re: BadZipfile file is not a zip file

2009-01-09 Thread webcomm
On Jan 9, 5:42 am, John Machin sjmac...@lexicon.net wrote:
 And here's a little gadget that might help the diagnostic effort; it
 shows the archive size and the position of all the magic PKnn
 markers. In a normal uncommented archive, EndArchive_pos + 22 ==
 archive_size.

I ran the diagnostic gadget...

archive size is 69888
FileHeader at 0
CentralDir at 43796
EndArchive at 43846


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


Re: BadZipfile file is not a zip file

2009-01-09 Thread webcomm
On Jan 9, 10:14 am, Chris Mellon arka...@gmail.com wrote:
 This is a ticket about another issue or 2 with invalid zipfiles that
 the zipfile module won't load, but that other tools will compensate
 for:

 http://bugs.python.org/issue1757072

Hmm.  That's interesting.  Are there other tools I can use in a python
script that are more forgiving?  I am using the zipfile module only
because it seems to be the most widely used.  Are other options in
python likely to be just as unforgiving?  Guess I'll look and see...

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


Re: Implementing file reading in C/Python

2009-01-09 Thread MRAB

Marc 'BlackJack' Rintsch wrote:

On Fri, 09 Jan 2009 04:04:41 +0100, Johannes Bauer wrote:


As this was horribly slow (20 Minutes for a 2GB file) I coded the whole
thing in C also:


Yours took ~37 minutes for 2 GiB here.  This just ~15 minutes:

#!/usr/bin/env python
from __future__ import division, with_statement
import os
import sys
from collections import defaultdict
from functools import partial
from itertools import imap


def iter_max_values(blocks, block_count):
for i, block in enumerate(blocks):
histogram = defaultdict(int)
for byte in block:
histogram[byte] += 1

yield max((count, byte)

  for value, count in histogram.iteritems())[1]


[snip]
Would it be faster if histogram was a list initialised to [0] * 256?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Extending Python with C or C++

2009-01-09 Thread Nick Craig-Wood
Thomas Heller thel...@python.net wrote:
  Nick Craig-Wood schrieb:
  Thomas Heller thel...@python.net wrote:
   Nick Craig-Wood schrieb:
   Interesting - I didn't know about h2xml and xml2py before and I've
   done lots of ctypes wrapping!  Something to help with the initial
   drudge work of converting the structures would be very helpful.
   
   ( http://pypi.python.org/pypi/ctypeslib/ )
   
   I gave it a quick go and it worked fine.  I had to edit the XML in one
   place to make it acceptable (removing a u from a hex number).
  
   If you are using a recent version of gccxml, then you should use the
   ctypeslib-gccxml-0.9 branch of ctypeslib instead of the trunk. (I should
   really merge the gccxml-0.9 branch into the trunk;-)
  
   If you are already using the branch and the XML file is not accepted,
   then could you please provide a short C code snippet that reproduces
   the problem so that I can fix it?
  
  I'm using gccxml from debian testing 0.9.0+cvs20080525-1 which I guess
  doesn't have the code from the branch in.
 
  I meant the branch in the repository where ctypeslib lives in.
  Anyway, it doesn't matter anymore since I merged that branch
  into the ctypeslib trunk.
 
  Now the problem with the 'u' suffix that you mentioned should be fixed, and
  I also made a quick change so that the sized integer types from stdint.h are 
 generated
  correctly. So, please update your ctypeslib installation and trey again;-)

I gave it a go and I can report success on both counts!

Well done and thank you.
-- 
Nick Craig-Wood n...@craig-wood.com -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-09 Thread rurpy
On Jan 9, 6:48 am, Johannes Bauer dfnsonfsdu...@gmx.de wrote:
 mk schrieb:
  The factor of 30 indeed does not seem right -- I have done somewhat
  similar stuff (calculating Levenshtein distance [edit distance] on words
  read from very large files), coded the same algorithm in pure Python and
  C++ (using linked lists in C++) and Python version was 2.5 times slower.

 Yup, that was about what I had expected (and what I could well live
 with, it's a tradeoff).

The rule-of-thumb I use is that Python is generally 5 to 50 times
slower than C.  It is considered blasphemy to say it in this group,
but Python is slow.  It does of course have many compensating
advantages
that make using it advantageous when runtime speed is not of primary
importance.
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-09 Thread Joe Strout

Aaron Brady wrote:


Possible compromise.  You can think of functions as mutation-only.
You pass the object, and it gets a new (additional) name.  The old
name doesn't go in.  /compromise


That's correct.  The reference itself is passed in, not the variable (or 
expression) that held or generated the reference in the calling code.


This is no different from, in C, passing an integer:

void foo(int bar) {
   bar = 42;
}
int baz = 0;
foo(baz);

This doesn't change baz because, speaking precisely, baz wasn't passed 
to foo -- only the *value* of baz (i.e. 0) was passed to foo.  Within 
foo, that value was stored in bar.  Assigning a new value to bar does 
not affect foo.


It's the exact same thing when the value happens to be a reference to an 
object:


typedef SomeClass* SomeClassPtr;
void foo(SomeClassPtr bar) {
   bar = new SomeClass();
}
SomeClassPtr baz = NULL;
foo(baz);

Again, we're not passing baz into foo; we're passing the *value* of baz 
(i.e. NULL) into foo.  That value is stored in bar within foo, and when 
we assign a new value (a reference to a freshly minted SomeClass object) 
into bar, of course it doesn't affect baz.



Regardless, IMO, references don't add any explanatory power; they just
make you feel cocky that you know what they are.


Nonsense.  They are the simple and clear explanation of what's going on.


M: If 'fun()' returned a reference, you would be able to assign to it.
m: You can't assign to it.
C: It doesn't return a reference.


C is false because M is false (at least, in the way I believe you mean 
it).  Or, more precisely, both M and m are nonsensical; what does it 
mean to assign to a reference?  It makes no sense.  What would it mean 
to assign to 42?


You don't assign to references any more than you assign to integers or 
assign to strings or assign to None.  Those are all values, and you 
don't assign to values -- you assign to variables.  Assign means to 
give a new value to a variable, i.e. to let (or cause) a variable to 
have a new value.  (Ye olde BASIC even used the LET keyword to indicate 
this, e.g. LET X = 42.)  Speaking casually, we don't always make this 
distinction, but I think precision is needed here.


So, I see two ways to make sense of your argument:

M1: If 'fun()' returned a variable, you would be able to assign to it.
m1: You can't assign to it.
C1: It doesn't return a variable.

This is true.  (Technically, instead of variable, we should say LValue 
here -- there are things slightly more complex than simple variables 
that can serve as the left-hand side of an assignment.  So replace 
variable with lvalue above if you prefer.)


Or, the other way some may see it is:

M2: If 'fun()' returned a reference, you might be able to mutate the 
object that refers to.

m2: You can sometimes mutate the object it refers to.
C2: 'fun()' returns a reference.

This is true (though the logic is flawed, but fixable).


-- Why can't I assign to a function call?
-- Python variables are references only.
-- Ok, why can't I assign to a function call?


You're right, Python variables are references only has nothing to do 
with it.  In a language where the only data type were integer, you 
wouldn't be able to assign to a function call either.


You can't assign to a function call because a function call is not 
itself an lvalue, and it doesn't return an lvalue.  That's just not 
something you can get for free from the type model; it's an extra 
feature that would have to be built into the language somehow, and 
Python doesn't have it.


If assignment were an operator rather than a statement, and could be 
overridden in a class via some specially-named method -- or, for that 
matter, if the assignment statement looked for such a method when it 
finds an object reference on the left-hand side -- then any object could 
be an lvalue, and you COULD (in some cases) assign to the result of a 
function.  But it's not and it doesn't.


Best,
- Joe


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


Re: Python 2.6 fails on compiling Bug report

2009-01-09 Thread googler . 1 . webmaster
Hi!

I didn't wanted to post 11.000 Lines here, so I uploaded it here:

http://rapidshare.com/files/181425216/config.log.html

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


Re: Creating new instances of subclasses.

2009-01-09 Thread J. Cliff Dyer
Thanks for the solutions everyone!  I'm not sure which I'll end up
using, but I think I've got a better grasp of the problem now.  

Cool stuff.

Cheers,
Cliff

On Thu, 2009-01-08 at 06:52 -0800, Paul McGuire wrote:
 On Jan 7, 12:00 pm, Paul McGuire pt...@austin.rr.com wrote:
  On Jan 7, 10:38 am, J. Cliff Dyer j...@unc.edu wrote:
 
   I want to be able to create an object of a certain subclass, depending
   on the argument given to the class constructor.
 
   I have three fields, and one might need to be a StringField, one an
   IntegerField, and the last a ListField.  But I'd like my class to
   delegate to the proper subclass automatically, so I can just do:
 
f1 = Field('abc')
f2 = Field('123')
f3 = Field('D,E,F')
 
  O-O is not always the solution to every problem.  Since inheritance is
  getting in your way, try using a class-level factory method.  Instead
  of using the Field constructor, use a staticmethod of Field, something
  like:
 
  @staticmethod
  def make_Field(a)
  if is_list(a):
  return ListField(a)
  elif is_integer(a):
  return IntegerField(a)
  else:
  return StringField(a)
 
  and then get rid of all those __new__ methods, too.
 
  -- Paul
 
 After looking this over a bit more, I decided I didn't like make_Field
 having to know the criteria for creating the different subclasses, but
 wanted to put the smarts into the subclasses themselves.  Here is an
 excerpt that shows this working:
 
 class Field(object):
 def __init__(self, input):
 super(Field, self).__init__(input)
 self.data = input
 
 @staticmethod
 def make_Field(a):
 subs = (ListField, IntegerField, StringField)
 ret = None
 for cls in subs:
 try:
 ret = cls(a)
 except TypeError:
 continue
 else:
 break
 return ret
 
 class IntegerField(Field):
 def __new__(cls, a):
 if not is_integer(a):
 raise TypeError()
 return Field.__new__(cls, a)
 
 ...
 ListField has a similar __new__ method, and StringField just creates
 the object, with no validation.
 
 make_Field still has to know what order to list the subclasses in
 (StringField is the most permissive, and so must come last in the list
 of subclasses), but the specific type tests are moved into the
 subclasses, which is a more appropriate place I think.
 
 -- Paul
 --
 http://mail.python.org/mailman/listinfo/python-list
 

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


Re: why cannot assign to function call

2009-01-09 Thread Mark Wooding
ru...@yahoo.com ru...@yahoo.com wrote:

 As a side comment (because it always bugs me when I read this, even
 though I read it in very authoritative sources), ISTM that C passes
 everything by value except arrays; they are passed by reference (by
 passing a pointer to the array by value.)  Admittedly, the close
 relationship between arrays and pointers makes it easy conflate them.

Arrays are distinctly second-class citizens in C.

Basically, in C, arrays aren't passed at all but there's some syntactic
sugar so you can squint and con yourself that they're passed by
reference.

If you try to pass an array, the array name immediately decays to a
pointer, and the pointer gets passed instead -- by value.  The
corresponding function parameter must be a pointer to an approrpriate
kind of thing, though you're allowed to write []s to confuse yourself if
you like -- T D[] in a function parameter declaration means precisely
the same as T *D -- to the extent that D has type T **D and so on.

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Guaranteeing an n-byte data type?

2009-01-09 Thread Evan Jones
Hello all,

I'm trying to use sockets to implement a pre-defined network protocol that
requires that I send messages of exactly a certain number of bytes. In
Python, integer values are represented as 4 bytes each (AFAIK.) However I
don't want to always send 4 bytes: sometimes I want to send one byte, or 11
bytes, or 33 bytes, or any other permutation that's not a multiple of 4. It
seems to make the most sense to use one-byte data members and concatenate
them before sending.

This is reasonably easy in C (thanks to the uint8_t data type), but with
Python I'm not sure how I'd implement it. The send() method in the socket
module will take any kind of data, but you can't specify the number of bytes
you want to send, so there's no guarantee as to how many you're actually
sending (particularly if you're sending a value that's regarded in Python as
a long integer - who knows how that data is actually represented in memory
behind the scenes!)

Perhaps since I'm trying to perform low-level operations, Python is simply
the wrong tool for this job. However, I'd very much like to write this
implementation in Python for sake of quick implementation and testing.

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


Re: why cannot assign to function call

2009-01-09 Thread Steve Holden
Joe Strout wrote:
 Aaron Brady wrote:
 
 Possible compromise.  You can think of functions as mutation-only.
 You pass the object, and it gets a new (additional) name.  The old
 name doesn't go in.  /compromise
 
 That's correct.  The reference itself is passed in, not the variable (or
 expression) that held or generated the reference in the calling code.
 
 This is no different from, in C, passing an integer:
 
 void foo(int bar) {
bar = 42;
 }
 int baz = 0;
 foo(baz);
 
 This doesn't change baz because, speaking precisely, baz wasn't passed
 to foo -- only the *value* of baz (i.e. 0) was passed to foo.  Within
 foo, that value was stored in bar.  Assigning a new value to bar does
 not affect foo.
 
 It's the exact same thing when the value happens to be a reference to an
 object:
 
 typedef SomeClass* SomeClassPtr;
 void foo(SomeClassPtr bar) {
bar = new SomeClass();
 }
 SomeClassPtr baz = NULL;
 foo(baz);
 
 Again, we're not passing baz into foo; we're passing the *value* of baz
 (i.e. NULL) into foo.  That value is stored in bar within foo, and when
 we assign a new value (a reference to a freshly minted SomeClass object)
 into bar, of course it doesn't affect baz.
 
 Regardless, IMO, references don't add any explanatory power; they just
 make you feel cocky that you know what they are.
 
 Nonsense.  They are the simple and clear explanation of what's going on.
 
 M: If 'fun()' returned a reference, you would be able to assign to it.
 m: You can't assign to it.
 C: It doesn't return a reference.
 
 C is false because M is false (at least, in the way I believe you mean
 it).  Or, more precisely, both M and m are nonsensical; what does it
 mean to assign to a reference?  It makes no sense.  What would it mean
 to assign to 42?
 
 You don't assign to references any more than you assign to integers or
 assign to strings or assign to None.  Those are all values, and you
 don't assign to values -- you assign to variables.  Assign means to
 give a new value to a variable, i.e. to let (or cause) a variable to
 have a new value.  (Ye olde BASIC even used the LET keyword to indicate
 this, e.g. LET X = 42.)  Speaking casually, we don't always make this
 distinction, but I think precision is needed here.
 
 So, I see two ways to make sense of your argument:
 
 M1: If 'fun()' returned a variable, you would be able to assign to it.
 m1: You can't assign to it.
 C1: It doesn't return a variable.
 
 This is true.  (Technically, instead of variable, we should say LValue
 here -- there are things slightly more complex than simple variables
 that can serve as the left-hand side of an assignment.  So replace
 variable with lvalue above if you prefer.)
 
 Or, the other way some may see it is:
 
 M2: If 'fun()' returned a reference, you might be able to mutate the
 object that refers to.
 m2: You can sometimes mutate the object it refers to.
 C2: 'fun()' returns a reference.
 
 This is true (though the logic is flawed, but fixable).
 
 -- Why can't I assign to a function call?
 -- Python variables are references only.
 -- Ok, why can't I assign to a function call?
 
 You're right, Python variables are references only has nothing to do
 with it.  In a language where the only data type were integer, you
 wouldn't be able to assign to a function call either.
 
 You can't assign to a function call because a function call is not
 itself an lvalue, and it doesn't return an lvalue.  That's just not
 something you can get for free from the type model; it's an extra
 feature that would have to be built into the language somehow, and
 Python doesn't have it.
 
 If assignment were an operator rather than a statement, and could be
 overridden in a class via some specially-named method -- or, for that
 matter, if the assignment statement looked for such a method when it
 finds an object reference on the left-hand side -- then any object could
 be an lvalue, and you COULD (in some cases) assign to the result of a
 function.  But it's not and it doesn't.
 
Pretty soon you guys will have me believing Python doesn't work ...

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Guaranteeing an n-byte data type?

2009-01-09 Thread MRAB

Evan Jones wrote:

Hello all,

I'm trying to use sockets to implement a pre-defined network protocol 
that requires that I send messages of exactly a certain number of bytes. 
In Python, integer values are represented as 4 bytes each (AFAIK.) 
However I don't want to always send 4 bytes: sometimes I want to send 
one byte, or 11 bytes, or 33 bytes, or any other permutation that's not 
a multiple of 4. It seems to make the most sense to use one-byte data 
members and concatenate them before sending.


This is reasonably easy in C (thanks to the uint8_t data type), but with 
Python I'm not sure how I'd implement it. The send() method in the 
socket module will take any kind of data, but you can't specify the 
number of bytes you want to send, so there's no guarantee as to how many 
you're actually sending (particularly if you're sending a value that's 
regarded in Python as a long integer - who knows how that data is 
actually represented in memory behind the scenes!)


Perhaps since I'm trying to perform low-level operations, Python is 
simply the wrong tool for this job. However, I'd very much like to write 
this implementation in Python for sake of quick implementation and testing.


The send() method takes a bytestring argument (class str in Python 
2.x, class bytes in Python 3.x), which is a string of 8-bit characters 
(1 byte per character). Just build your bytestring and then send it 
(actually it's better to use sendall() because send() isn't guaranteed 
to send all the bytes in one call).

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


Re: why cannot assign to function call

2009-01-09 Thread Steven D'Aprano
On Fri, 09 Jan 2009 08:30:46 -0700, Joe Strout wrote:

 That's correct.  The reference itself is passed in, not the variable (or
 expression) that held or generated the reference in the calling code.
 
 This is no different from, in C, passing an integer:
 
 void foo(int bar) {
 bar = 42;
 }
 int baz = 0;
 foo(baz);
 
 This doesn't change baz because, speaking precisely, baz wasn't passed
 to foo -- only the *value* of baz (i.e. 0) was passed to foo.

We can check whether bar and baz are the same thing, by printing their 
addresses:

#includestdio.h
void foo(int bar) {
  printf(\n The address of bar is %p , bar);
  bar = 42;
}

int main() {
int baz = 0;
printf(\n The address of baz is %p , baz);
foo(baz);
return 0;
}


When I do this, I get:

 The address of baz is 0xbfa72870
 The address of bar is 0xbfa72850

So we can agree that baz and bar are different entities. Now, would 
anyone like to take a small wager on what the equivalent code in CPython 
would print?


def foo(bar):
  print  The address of bar is %s  % id(bar)
  bar = 42

baz = 0
print  The address of baz is %s  % id(baz)
foo(baz)


When I execute this, I get

 The address of baz is 143599468
 The address of bar is 143599468

Python doesn't do the same thing as C. It actually passes the same value 
to the function, without copying it.

Why oh why do you keep insisting that Python is no different from C?


Let's try one more. In C:

#include stdio.h
#include stdlib.h

struct record {
  int x;
};

void mutate(struct record bar) {
  printf(\n The address of bar is %p , bar);
  bar.x = 0;
  printf(\n Inside: %d , bar.x);
}

struct record baz;

int main(void) {
  baz.x = 1;
  printf(\n The address of baz is %p , baz);
  printf(\n Before: %d , baz.x);
  mutate(baz);
  printf(\n After: %d , baz.x);
  return 0;
}


gives output: 

 The address of baz is 0x80496fc
 Before: 1
 The address of bar is 0xbfb8f980
 Inside: 0
 After: 1


We can clearly see that baz and bar are different entities, and changes 
made to bar inside the function don't affect baz.

A Python equivalent:

class struct: pass

def mutate(bar):
  print  The address of bar is %d  % id(bar)
  bar.x = 0;
  print  Inside: %d  % bar.x

baz = struct()
baz.x = 1
print  The address of baz is %d  % id(baz)
print  Before: %d  % baz.x
mutate(baz)
print  After: %d  % baz.x


which gives output:

 The address of baz is 3085537420
 Before: 1
 The address of bar is 3085537420
 Inside: 0
 After: 0



Why oh why do you keep insisting that Python is no different from C?


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


Re: Guaranteeing an n-byte data type?

2009-01-09 Thread Steve Holden
Evan Jones wrote:
 Hello all,
 
 I'm trying to use sockets to implement a pre-defined network protocol
 that requires that I send messages of exactly a certain number of bytes.
 In Python, integer values are represented as 4 bytes each (AFAIK.)
 However I don't want to always send 4 bytes: sometimes I want to send
 one byte, or 11 bytes, or 33 bytes, or any other permutation that's not
 a multiple of 4. It seems to make the most sense to use one-byte data
 members and concatenate them before sending.
 
 This is reasonably easy in C (thanks to the uint8_t data type), but with
 Python I'm not sure how I'd implement it. The send() method in the
 socket module will take any kind of data, but you can't specify the
 number of bytes you want to send, so there's no guarantee as to how many
 you're actually sending (particularly if you're sending a value that's
 regarded in Python as a long integer - who knows how that data is
 actually represented in memory behind the scenes!)
 
 Perhaps since I'm trying to perform low-level operations, Python is
 simply the wrong tool for this job. However, I'd very much like to write
 this implementation in Python for sake of quick implementation and testing.
 
In Python 2 you'd use an 8-bit string - though as you surmise you need
access to the conversions to and from the primitive data types. This is
provided  by the struct module.

In Python 3 you'll use a bytes object; in 2.6 and onwards the bytes
syntax can be used to refer to regular strings, to make portability
easier (in Python 3, strings are Unicode by default).

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Guaranteeing an n-byte data type?

2009-01-09 Thread Grant Edwards
Evan Jones wrote:

 I'm trying to use sockets to implement a pre-defined network
 protocol that requires that I send messages of exactly a
 certain number of bytes.  In Python, integer values are
 represented as 4 bytes each (AFAIK.) However I don't want to
 always send 4 bytes: sometimes I want to send one byte, or 11
 bytes, or 33 bytes, or any other permutation that's not a
 multiple of 4. It seems to make the most sense to use one-byte
 data members and concatenate them before sending.

In addition to previous suggestions, you should look at the
struct and ctypes modules in the standard Python library.
Both allow you to pack/unpack data to/from data structures with
arbitrary layout and endianness.

-- 
Grant Edwards   grante Yow! I'm meditating on
  at   the FORMALDEHYDE and the
   visi.comASBESTOS leaking into my
   PERSONAL SPACE!!
--
http://mail.python.org/mailman/listinfo/python-list


Re: BadZipfile file is not a zip file

2009-01-09 Thread webcomm
On Jan 9, 10:14 am, Chris Mellon arka...@gmail.com wrote:
 This is a ticket about another issue or 2 with invalid zipfiles that
 the zipfile module won't load, but that other tools will compensate
 for:

 http://bugs.python.org/issue1757072

Looks like I just need to do this to unzip with unix...

from os import popen
popen(unzip data.zip)

That works for me.  No idea why I didn't think of that earlier.  I'm
new to python but should have realized I could run unix commands with
python.  I had blinders on.  Now I just need to get rid of some bad
characters in the unzipped file.  I'll start a new thread if I need
help with that...

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


Re: Implementing file reading in C/Python

2009-01-09 Thread Grant Edwards
On 2009-01-09, Johannes Bauer dfnsonfsdu...@gmx.de wrote:

 I've come from C/C++ and am now trying to code some Python because I
 absolutely love the language. However I still have trouble getting
 Python code to run efficiently. Right now I have a easy task: Get a
 file,

If I were you, I'd try mmap()ing the file instead of reading it
into string objects one chunk at a time.

-- 
Grant Edwards   grante Yow! I'm DESPONDENT ... I
  at   hope there's something
   visi.comDEEP-FRIED under this
   miniature DOMED STADIUM ...
--
http://mail.python.org/mailman/listinfo/python-list


Re: BadZipfile file is not a zip file

2009-01-09 Thread Wesley Brooks
I missed the begining of this thread and so appologise if I'm repeating what
someone else has said!

I had a very similar problem with this error and it turned out it was due to
me moving a file across a socket connection and either not reading it or
writing it in the binary mode, ie open(filename, 'rb') or write(filename,
'wb'). This didn't make any difference on the linux machines (where the
error didn't occur) but did on the windows machines and fixed the problem.

Cheers,

Wes

On 09/01/2009, webcomm rya...@gmail.com wrote:

 On Jan 9, 10:14 am, Chris Mellon arka...@gmail.com wrote:
  This is a ticket about another issue or 2 with invalid zipfiles that
  the zipfile module won't load, but that other tools will compensate
  for:
 
  http://bugs.python.org/issue1757072

 Looks like I just need to do this to unzip with unix...

 from os import popen
 popen(unzip data.zip)

 That works for me.  No idea why I didn't think of that earlier.  I'm
 new to python but should have realized I could run unix commands with
 python.  I had blinders on.  Now I just need to get rid of some bad
 characters in the unzipped file.  I'll start a new thread if I need
 help with that...

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

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


Re: Implementing file reading in C/Python

2009-01-09 Thread bearophileHUGS
Johannes Bauer, I was about to start writing a faster version. I think
with some care and Psyco you can go about as 5 times slower than C or
something like that.

To do that you need to use almost the same code for the C version,
with a list of 256 ints for the frequencies, not using max() but a
manual loop, not using itertools or generators, maybe splitting code
in two functions to allow Psyco to optimize better, maybe using
another array(...) for the frequences too. The data can be read into
an array.array(B), and so on.

But I think all this work is a waste of time. I like Python, but that
C code, after some cleaning and polishing looks fine for this job. Of
course there are other languages that may give you a little nicer code
for this program, like D, and there may be ways to use numpy too to
speed up the computation of the mode, but they don't look so much
important this time.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected scientific notation

2009-01-09 Thread Paul McNett

Mark Dickinson wrote:

On Jan 8, 1:00 am, Paul McNett p...@ulmcnett.com wrote:

It displays '3E+1' instead of '30.0'.

As I can't reproduce I'm looking for an idea brainstorm of what could be causing
this. What would be choosing to display such a normal number in scientific 
notation?

Ideas?


[I thought I replied to this earlier, but the post isn't showing up.
So here it is again.]


LOL!


I suspect it's your use of the Decimal normalize() method that's
causing
this.  Trailing zeros on Decimal instances are significant, so
Decimal('30.0'), Decimal('30') and Decimal('3E+1') are considered
distinct (though they all have the same value).  The normalize method
strips all trailing zeros, turning Decimal('30.0') into Decimal('3E
+1').

One way to get around this is to add 0 after normalizing: this will
make sure that scientific notation is used only for very large
or small numbers, as usual.


Thank you for the insight. I believe the problem is with my use of normalize(), but I 
still can't figure out why I can't reproduce the issue in my running app. But I can 
see in the interpreter that printing Decimal(30.0).normalize() results in 3E+1.


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


Re: Python Imaging Library and textmate

2009-01-09 Thread bilgin arslan
Hi Philip,
I tried to install PIL with the directions given and it seemed to be ok.

When I tried it with IDLE, import Image did not give an error and as
far as I checked it seemed to be working.
However, importing Image module in TextMate gives an error, saying
that the module cannot be found for some strange reason.

The code simply contains the line *import Image* in both cases


On Jan 9, 2009, at 2:49 AM, bilgin arslan wrote:

* Hello,
** I am a beginner in python and I am trying to create image files that
** contain
** lines from a text file.
** I am trying to do this with PIL, which seems like a suitable tool. I
** have a
** copy of TextMate(1.5.8) and I run Macosx 10.5.6
**
** The problem is, even though I installed PIL correctly (I get no
** errors from
** import Image in IDLE or in terminal), I can't get TextMate to
** import the
** module.
** I keep getting ImportError: No module named Image error.
** I tried reloading TextMate's bundles but that did not work either.
*
I use TextMate but I don't know what you mean when you say you can't
get TM to import a module. Can you give a little more context?


* There was a similar thread in this list with textmate but I can't
** understand
** how that issue was solved in the end.
*
link?



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


Problem with -3 switch

2009-01-09 Thread Aivar Annamaa

Hi

I'm getting started with Python and in order to get good habits for 
Python 3, i'd like to run my Python 2.6.1 with Python 3 warning mode.


When i run
python -3

and execute statement
 print 4

then i expect to see a warning because i've understood that this 
statement is not valid in Python 3


however no warning appears.

Have is misunderstood something?
I'm running Python on Windows Vista.

thanks in advance!
Aivar
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with -3 switch

2009-01-09 Thread Chris Rebert
On Fri, Jan 9, 2009 at 9:17 AM, Aivar Annamaa concat_na...@hotmail.com wrote:
 Hi

 I'm getting started with Python and in order to get good habits for Python
 3, i'd like to run my Python 2.6.1 with Python 3 warning mode.

 When i run
 python -3

 and execute statement
 print 4

 then i expect to see a warning because i've understood that this statement
 is not valid in Python 3

 however no warning appears.

 Have is misunderstood something?

As was recently pointed out in a nearly identical thread, the -3
switch only points out problems that the 2to3 converter tool can't
automatically fix. Changing print to print() on the other hand is
easily fixed by 2to3.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Apache Handler

2009-01-09 Thread Gerhard Häring
Scooter wrote:
 Does anyone have any good examples, or links thereto for using python
 as an Apache handler? And I should qualify all of this by saying I'm a
 python newbie, and while having experience with Apache, I've never
 done anything outside whats in the box .
 
 What I'm looking for is how one might use Python not from the CGI/
 presentation side but more on the backend...i.e. for each page Apache
 serves up examine the request and update some headers, or add a cookie
 to the response. 

I vaguely remembered that mod_python can do such things. Looking again
it seems to be the case:
http://www.modpython.org/live/current/doc-html/pyapi-filter.html

 Or possibly use Python for writing a custom Apache
 logger. [...]

Maybe http://www.modpython.org/live/current/doc-html/dir-handlers-plh.html
The documentation about this is a joke, though.

Remember that you can write custom Apache loggers quite easily with
piped logs: http://httpd.apache.org/docs/2.2/logs.html

This looks roughly like this:

CustomLog |/path/to/my_script.py common

## begin my_script.py ##
import sys

for line in sys.stdin:
# do stuff
## end ##

-- Gerhard

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


Re: Unexpected scientific notation

2009-01-09 Thread Mark Dickinson
On Jan 9, 5:16 pm, Paul McNett p...@ulmcnett.com wrote:
 Thank you for the insight. I believe the problem is with my use of 
 normalize(), but I
 still can't figure out why I can't reproduce the issue in my running app.

Me neither.  In particular, I can't see how it could this output could
come out
of a locale.format call.  Is it possible that your user is somehow
seeing a
direct str() or %s of a Decimal instance, rather than something
that's been
through a format method?

Mark

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


Re: Unexpected scientific notation

2009-01-09 Thread Paul McNett

Paul McNett wrote:

Mark Dickinson wrote:

On Jan 8, 1:00 am, Paul McNett p...@ulmcnett.com wrote:

It displays '3E+1' instead of '30.0'.

As I can't reproduce I'm looking for an idea brainstorm of what could 
be causing
this. What would be choosing to display such a normal number in 
scientific notation?


Ideas?


[I thought I replied to this earlier, but the post isn't showing up.
So here it is again.]


LOL!


I'll clarify my LOL: Mark initially replied to me directly, to which I responded 
directly. Because he replied directly, I kept my response offline, too, not knowing 
if he had a special reason to discuss this offline instead of in public.


Then he replied to the list, so I made a different response to the list.

Anyway, thanks again Mark for your guidance.

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


Re: Problem with -3 switch

2009-01-09 Thread Aivar Annamaa

As was recently pointed out in a nearly identical thread, the -3
switch only points out problems that the 2to3 converter tool can't
automatically fix. Changing print to print() on the other hand is
easily fixed by 2to3.

Cheers,
Chris



I see.
So i gotta keep my own discipline with print() then :)

thanks!
A
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-09 Thread Sion Arrowsmith
Grant Edwards  inva...@invalid wrote:
On 2009-01-09, Johannes Bauer dfnsonfsdu...@gmx.de wrote:
 I've come from C/C++ and am now trying to code some Python because I
 absolutely love the language. However I still have trouble getting
 Python code to run efficiently. Right now I have a easy task: Get a
 file,
If I were you, I'd try mmap()ing the file instead of reading it
into string objects one chunk at a time.

You've snipped the bit further on in that sentence where the OP
says that the file of interest is 2GB. Do you still want to try
mmap'ing it?

-- 
\S -- si...@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
   Frankly I have no feelings towards penguins one way or the other
-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
--
http://mail.python.org/mailman/listinfo/python-list


Re: Encrypted Logging in python

2009-01-09 Thread Gerhard Häring
koranth...@gmail.com wrote:
I was wondering if there is a mechanism to encrypt logging
 automatically in python.

Python's standard library doesn't include any strong symmetric
ciphers. But if you include for example a cryptographic module for AES,
for example, it should be easy (I guess 10 lines of code, yes, the issue
always is *which* 10 lines) to write a custom logger that encrypts using
a hardcoded key.

As others have said, this is not really secure, so you could just as
well use something stupid like rot13 or base64 instead.

The issue is as follows:
 (a) An application (after py2exe) will go as executable and there
 is no need for the user to know that it is written in python. If an
 exception occurs and it is logged, then the user can understand it is
 written in python.

In 99.326 % of all cases, the answer is: so what?

 (b) A security threat. If an exception occurs, the code is seen by
 the user - and possibly be misused.

Simply make the user not see the exception, but use a fallback exception
handler that does whatever you want to. Write to a log file. Or write to
an encrypted log file if you still think that helps.

Base64 encoding somewhat helps - which is supported by logging
 module - but even that is not very secure. If there can be an option -
 wherein we send in the password and the logging is encrypted - it
 might be better. [...]

As I said before, that should be trivial to program if you look up the
documentation about the logging module. Just subclass FileHandler. And
make sure your class is then used. That's probably the hardest part ;-)

That all being said, I have one final advise: Your time is probably much
better spent on *real* issues.

-- Gerhard

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


Re: Python Imaging Library and textmate

2009-01-09 Thread Philip Semanchuk


On Jan 9, 2009, at 12:19 PM, bilgin arslan wrote:


Hi Philip,
I tried to install PIL with the directions given and it seemed to be  
ok.


When I tried it with IDLE, import Image did not give an error and as
far as I checked it seemed to be working.
However, importing Image module in TextMate gives an error, saying
that the module cannot be found for some strange reason.



I am still unclear on how you're importing Python modules via  
TextMate. It's a text editor, so even though I've used it for a couple  
of years, it's not at all obvious to me how you'd use it to import a  
Python module. I think I can help you, but to do so I need you to tell  
me *exactly* what menu items you're clicking on, e.g. Bundle/Python/ 
Run Script...



The code simply contains the line *import Image* in both cases


From what I can see in the PIL documentation, this wouldn't work.  
You'd need either this:

   from PIL import Image
or this:
   import PIL.Image

What am I missing?






On Jan 9, 2009, at 2:49 AM, bilgin arslan wrote:


* Hello,
** I am a beginner in python and I am trying to create image files  
that

** contain
** lines from a text file.
** I am trying to do this with PIL, which seems like a suitable  
tool. I

** have a
** copy of TextMate(1.5.8) and I run Macosx 10.5.6
**
** The problem is, even though I installed PIL correctly (I get no
** errors from
** import Image in IDLE or in terminal), I can't get TextMate to
** import the
** module.
** I keep getting ImportError: No module named Image error.
** I tried reloading TextMate's bundles but that did not work either.
*
I use TextMate but I don't know what you mean when you say you can't
get TM to import a module. Can you give a little more context?



* There was a similar thread in this list with textmate but I can't

** understand
** how that issue was solved in the end.
*
link?



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


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


  1   2   3   >