Re: How to avoid certain directories when using os.walk?

2009-10-31 Thread Gabriel Genellina

En Fri, 30 Oct 2009 14:50:34 -0300, Sean DiZazzo half.ital...@gmail.com
escribió:

On Oct 29, 10:17 pm, Chris Rebert c...@rebertia.com wrote:

On Thu, Oct 29, 2009 at 9:53 PM, Peng Yu pengyu...@gmail.com wrote:



 I don't see a way to avoid walking over directories of certain names
 with os.walk. For example, I don't want os.walk return files whose
 path include '/backup/'. Is there a way to do so? Otherwise, maybe I
 will have to make my own program. Thank you!

Read the docs! (http://docs.python.org/library/os.html#os.walk):
They even include a specific code example of how to skip unwanted
subdirectories.


You will run into problems however if you want to delete from a tree
while ignoring certain named directories.  Directories must be empty
before they can be deleted, so you must use topdown=False, but to
prune a search you must use topdown=True.  At least I think that was
the problem I had with my deletion script a while back.


I'd say the problem is ill-defined in that case. You have to decide what
to do with those non-empty directories that are non-empty because you
explicitely skipped some subdirectories...

--
Gabriel Genellina

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


Re: How can module determine its own path?

2009-10-31 Thread Gabriel Genellina

En Fri, 30 Oct 2009 20:40:59 -0300, Stef Mientki stef.mien...@gmail.com
escribió:

Robert Kern wrote:

On 2009-10-30 12:19 PM, kj wrote:



How can a module determine the path of the file that defines it?
(Note that this is, in the general case, different from sys.argv[0].)


__file__


but for modules launched with execfile, __file__ doesn't exists.


Remember that execfile just executes the file contents, it does not create
a new module nor interacts with the module management.
modules launched with execfile has no meaning.

--
Gabriel Genellina

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


How to get output of command called by os.system()?

2009-10-31 Thread Peng Yu
I need to integrate shell program with python. I'm wondering if there
is a way get the output of the shell program called by os.system().
Thank you!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get output of command called by os.system()?

2009-10-31 Thread Chris Rebert
On Fri, Oct 30, 2009 at 11:09 PM, Peng Yu pengyu...@gmail.com wrote:
 I need to integrate shell program with python. I'm wondering if there
 is a way get the output of the shell program called by os.system().

You'd probably do better to use the `subprocess` module instead:
http://docs.python.org/library/subprocess.html

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


How to get the realpath of a symbolic link?

2009-10-31 Thread Peng Yu
Suppose that I have the following directory and files. I want to get
the canonical path of a file, a directory or a symbolic link.
For example, for 'b' below, I want to get its canonical path as
'/private/tmp/abspath/b'.

However, os.path.abspath('b') gives me '/private/tmp/abspath/b', but
os.path.abspath('/tmp/abspath/b') gives me '/tmp/abspath/b'.
The results are not the same. So, neither os.path.abspath or
os.path.realpath gives me what I want.

I'm wondering if there is a way to get the canonical path
'/private/tmp/abspath/b', no matter whether the argument is 'b' or
'/tmp/abspath/b'.

$./test.py
/private/tmp/abspath/a
/private/tmp/abspath/a
/private/tmp/abspath/a
/private/tmp/abspath/a
/private/tmp/abspath/a
/private/tmp/abspath/b
/tmp/abspath/a
/tmp/abspath/b
$cat test.py
#!/usr/bin/env python

import os.path

print os.path.realpath('a')
print os.path.realpath('b')
print os.path.realpath('/tmp/abspath/a')
print os.path.realpath('/tmp/abspath/b')

print os.path.abspath('a')
print os.path.abspath('b')
print os.path.abspath('/tmp/abspath/a')
print os.path.abspath('/tmp/abspath/b')
$pwd
/tmp/abspath
$gls -Rgtra
.:
total 8
-rw-r--r--  1 wheel   0 2009-10-31 01:52 a
lrwxr-xr-x  1 wheel   1 2009-10-31 01:52 b - a
-rwx--x--x  1 wheel 312 2009-10-31 01:54 test.py
drwx--  5 wheel 170 2009-10-31 01:54 .
drwxrwxrwt 23 wheel 782 2009-10-31 01:56 ..
$gls -lgtr /tmp
lrwxr-xr-x 1 admin 11 2009-05-21 04:28 /tmp - private/tmp
-- 
http://mail.python.org/mailman/listinfo/python-list


Why do you use python?

2009-10-31 Thread sk
What would be your answer if this question is asked to you in an
interview?

a modified version might be:
Where would you use python over C/C++/Java?

(because my resume says I know C/C++/Java)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do you use python?

2009-10-31 Thread Jaime Buelta
On Oct 31, 8:11 am, sk catchyouraak...@gmail.com wrote:
 What would be your answer if this question is asked to you in an
 interview?

 a modified version might be:
 Where would you use python over C/C++/Java?

 (because my resume says I know C/C++/Java)?

I also know C/C++/Java so...

I'd say that I can be much more productive in Python that in those
other languages.
I can do the same with , let's say, 5 lines of Python that with 15 or
20 lines of C/C++/Java (maybe a little less in Java), with much less
errors.
I can produce clear code, so it's easier to understand when other
person will look at it.
I can produce a prototype much faster
I can focus more on the problem and less on the coding...
I can make cross-platform code (well, also with Java)

I would use it as much as possible, but specially for:
* Complicated script-like actions, in spair of bash scripts, which
it's great for simple , OS related operations, but to manage workflow
it's sometimes confusing.
* Rapid prototyping. Check ideas.
* Tests automation

I try to use Python as often as possible, as my default language, as
I think I can't be more productive, which it's usually the most
important parameter in software development. Of course, if there is a
good reason not to use it, then it's not the adequate tool. But But I
tend to ask myself Is there a reason for not using Python? instead
of Is there a reason for using Python?
-- 
http://mail.python.org/mailman/listinfo/python-list


Feedback desired on reworked ch 1 progr. intro (now Python 3.x, Windows)

2009-10-31 Thread Alf P. Steinbach

Hi all!

After my earlier feedback request a lot of you responded with constructive 
criticism and suggestions.


As a result of that I've changed the text to be based on *Python 3.x* instead of 
2.6+, and chapter 1 Getting started has grown from 9 pages to a whopping 11 pages!


I would particularly like to thank the following persons (although of course I 
don't know whether any of them would recommend the old text, or the new text! 
:-) ), because they directly caused changes of the text:


Chris Rebert, in [comp.lang.python]
Ch 1
all batteries included - batteries included
OS/X - Mac OS X (with a space, not a slash)
implementation - distribution

Eric Brunel, in [comp.lang.python]
Ch 1
static type checking discussion yielding wrong impression

ba...@freeuk.com, in [comp.lang.python]
Ch 1
Unclear that Windows subsystems are not something to do with Python.

Ethan Furman, in private communication
Ch 1
Pointed out the renaming 2.6 Tkinter - 3.x tkinter

Jon Clements, in [comp.lang.python]
General
Info about Python 3.x that let me switch to 3.x

Plus, the person who suggested that I should mention the PSPad editor, but I 
forgot to note who that was (sorry).


I hope this new version of ch 1 is, well, better, addresses some of the concerns 
raised? g


url: http://preview.tinyurl.com/ProgrammingBookP3
Formats: PDF, text

Now starting on ch 2, which with Python 3.x should become a lot cleaner...


Cheers,

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


Re: self.__dict__ tricks

2009-10-31 Thread Hendrik van Rooyen
On Friday, 30 October 2009 17:28:47 MRAB wrote:

 Wouldn't it be clearer if they were called dromedaryCase and
 BactrianCase? :-)

Ogden Nash:

The Camel has a single hump-
The Dromedary, two;
Or the other way around-
I'm never sure. - Are You?

- Hendrik


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


Re: Sqlite3. Substitution of names in query.

2009-10-31 Thread Lawrence D'Oliveiro
In message mailman.2357.1256964121.2807.python-l...@python.org, Dennis Lee 
Bieber wrote:

 This way regular string interpolation operations (or whatever Python
 3.x has replaced it with) are safe to construct the SQL, leaving only
 user supplied (or program generated) data values to be passed via the
 DB-API parameter system -- so that they are properly escaped and
 rendered safe.

Mixing the two is another recipe for confusion and mistakes.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are *.pyd's universal?

2009-10-31 Thread Lawrence D'Oliveiro
In message 6e603d9c-2be0-449c-9c3c-
bab49e09e...@13g2000prl.googlegroups.com, Carl Banks wrote:

 It's not Python that's the issue.  The issue is that if you have a
 module with a .dll extension, other programs could accidentally try to
 load that module instead of the intended dll, if the module is in the
 current directory or system path.  Modules will sometimes find
 themselves on the path in Windows, so the fact that Windows performs a
 library search on the path is quite significant.

Why is it only Windows is prone to this problem?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are *.pyd's universal?

2009-10-31 Thread Albert Hopkins
On Sat, 2009-10-31 at 21:32 +1300, Lawrence D'Oliveiro wrote:
 Modules will sometimes find
  themselves on the path in Windows, so the fact that Windows performs
 a
  library search on the path is quite significant.
 
 Why is it only Windows is prone to this problem? 

I think as someone pointed out earlier, in Unix-like operating systems,
a regular library's file name starts with lib, e.g. libcrypt.so.  So
this would not conflict with Python's crypt.so.  But in Windows, they
would both be named crypt.dll, for example (I'm halfway guessing since I
don't have/use Windows).
 

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


Re: Web development with Python 3.1

2009-10-31 Thread Dotan Cohen
 notmm uses Python 2.6 and will probably work just fine with Python
 3000.


The only reference to notmm that I could find in Google was this thread!


 I am free, no matter what rules surround me. If I find them
 tolerable, I tolerate them; if I find them too obnoxious, I break
 them. I am free because I know that I alone am morally responsible for
 everything I do. -- Robert A. Heinlein


Heinlein said that? It's been a long time since I've read Heinlein,
and his quotes are as vivid as his books.

-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get output of command called by os.system()?

2009-10-31 Thread Rominsky
On Oct 30, 11:09 pm, Peng Yu pengyu...@gmail.com wrote:
 I need to integrate shell program with python. I'm wondering if there
 is a way get the output of the shell program called by os.system().
 Thank you!

popen should do what your after.  There are several modules that have
a popen method including os and subprocess.  It will allow you to make
a system call similar to os.system, but it gives you pipe access, like
an open file, to the standard output and standard error if you use
subprocess.Popen.  A simple example would be:

import subprocess
output = subprocess.Popen('pwd')
print('Present Working Directory is: ' + output.readline())

Hope that helps.

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


Re: datetime question

2009-10-31 Thread Albert Hopkins
On Sat, 2009-10-31 at 10:08 +1100, Ben Finney wrote:
 The ‘datetime’ module focusses on individual date+time values (and the
 periods between them, with the ‘timedelta’ type).
 
 For querying the properties of the calendar, use the ‘calendar’
 module.
 
 Yes, it would be nice if the ‘time’, ‘datetime’, and ‘calendar’
 modules
 were all much more unified and consumed a common set of primitive
 date+time types. It's a wart, and fixing it would (unfortunately)
 probably require backward-incompatible API changes.
 

But, supposedly, that's why we had Python3.

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


Re: Why do you use python?

2009-10-31 Thread Martin P. Hellwig

sk wrote:

What would be your answer if this question is asked to you in an
interview?

a modified version might be:
Where would you use python over C/C++/Java?

(because my resume says I know C/C++/Java)?


I would say where I can, where 'can' is depending on the problem, 
already implementations and requirements.


On the other hand, when I go to a restaurant I usually don't tell the 
chef which brand of knives he has to prepare my meal with, even though I 
prefer Globals knives for my own use.


--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: datetime question

2009-10-31 Thread Ben Finney
Albert Hopkins mar...@letterboxes.org writes:

 On Sat, 2009-10-31 at 10:08 +1100, Ben Finney wrote:
  Yes, it would be nice if the ‘time’, ‘datetime’, and ‘calendar’
  modules were all much more unified and consumed a common set of
  primitive date+time types. It's a wart, and fixing it would
  (unfortunately) probably require backward-incompatible API changes.

 But, supposedly, that's why we had Python3.

Fixing ‘time’, ‘datetime’, and ‘calendar’ was the reason for Python 3?
No, it wasn't.

Or perhaps you mean that any backward-incompatible change was a reason
to have Python 3? Even more firmly no. The extent of changes was
severely limited to make the transition from Python 2 to Python 3 as
painless as feasible, while still meeting the goals of Python 3.

-- 
 \“Odious ideas are not entitled to hide from criticism behind |
  `\  the human shield of their believers' feelings.” —Richard |
_o__) Stallman |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: datetime question

2009-10-31 Thread Albert Hopkins
On Sat, 2009-10-31 at 20:34 +1100, Ben Finney wrote:
 Fixing ‘time’, ‘datetime’, and ‘calendar’ was the reason for Python 3?
 No, it wasn't.
 
 Or perhaps you mean that any backward-incompatible change was a reason
 to have Python 3? Even more firmly no. The extent of changes was
 severely limited to make the transition from Python 2 to Python 3 as
 painless as feasible, while still meeting the goals of Python 3. 

No, I meant cleaning up the standard library in spite of
incompatibilities was one of the goals of Python3 (PEP 3108).
Personally I don't see anything wrong with the modules, but that was my
question to the person who said they should all be integrated but wasn't
because of incompatibilities.

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


Python 2.6.4: ./configure does not work

2009-10-31 Thread knipknap
Hi,

Running ./configure in the 2.6.4 sources produces the following error:

config.status: error: cannot find input file: Makefile.pre.in

Indeed, such a file is not contained anywhere in the Pakage. Also, I
found this note:

The Unix build and install process is explained clearly in the README
file contained in the distribution
(http://www.python.org/download/source/)

However, there isn't any README (or INSTALL) file contained in the
distribution.

s...@mach:/home/sab/Python-2.6.4 find . -name README
./Lib/lib2to3/tests/data/README
./Lib/distutils/README
./Lib/test/crashers/README

Any hints?

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


Re: Why do you use python?

2009-10-31 Thread Alf P. Steinbach

* sk:

[title Why do you use python?]
What would be your answer if this question is asked to you in an
interview?

a modified version might be:
Where would you use python over C/C++/Java?

(because my resume says I know C/C++/Java)?


The C++ FAQ addresses this question here:
url: http://www.parashift.com/c++-faq-lite/big-picture.html#faq-6.5


Cheers  hth.,

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


Re. Web development with Python 3.1

2009-10-31 Thread Rustom Mody
Rober Kern wrote
 But if you insist, you may be interested in Breve:

   http://pypi.python.org/pypi/Breve/

Thanks for that! Viva internal DSLs!
[Sorry -- cut my teeth on lisp]

Is there anything like this for xml?
Well I guess that is a slightly wrong (if not straight stupid) question.

Maybe something along this line:
Takes a spec as DTD or Relax-ng or somethn and produces something like Breve?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Working threads progress

2009-10-31 Thread mattia
Il Wed, 28 Oct 2009 20:04:45 -0700, ryles ha scritto:

 On Oct 28, 7:02 pm, mattia ger...@gmail.com wrote:
 Now, I would like to know the activity done (e.g. every two seconds) so
 I create another thread that checks the queue size (using .qsize()).
 Have you any suggestion to improve the code?
 
 It's not uncommon to pass each thread a second queue for output, which
 in your case might be tuples of (item, result). You can read from this
 queue in a single thread, and since the results now accumulate
 incrementally, you can easily collect more detailed status/progress
 information (if that's what you're looking for).

OK, but what do you mean by 'read from this queue'? I mean, I can have a 
variable initialized when the working queue is full (e.g. ready to be 
used) and then every time i use queue.get() decrement the variable (using 
a lock in order to prevent concurrent access). Is what you had in mind?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6.4: ./configure does not work

2009-10-31 Thread Albert Hopkins
On Sat, 2009-10-31 at 03:07 -0700, knipknap wrote:
 Hi,
 
 Running ./configure in the 2.6.4 sources produces the following error:
 
 config.status: error: cannot find input file: Makefile.pre.in
 
 Indeed, such a file is not contained anywhere in the Pakage. 

Which sources are you referring to?  Can you verify the checksums:

17dcac33e4f3adb69a57c2607b6de246  13322131  Python-2.6.4.tgz
fee5408634a54e721a93531aba37f8c1  11249486  Python-2.6.4.tar.bz2


 Also, I
 found this note:

 The Unix build and install process is explained clearly in the README
 file contained in the distribution
 (http://www.python.org/download/source/)
 

 However, there isn't any README (or INSTALL) file contained in the
 distribution.
 
 s...@mach:/home/sab/Python-2.6.4 find . -name README
 ./Lib/lib2to3/tests/data/README
 ./Lib/distutils/README
 ./Lib/test/crashers/README
 
 Any hints?

There is a README at the root of the tarball:

$ pwd
/home/marduk/Desktop/Python-2.6.4
$ head -1 README
This is Python version 2.6.4



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


Re: Python 2.6.4: ./configure does not work

2009-10-31 Thread knipknap
On 31 Okt., 11:40, Albert Hopkins mar...@letterboxes.org wrote:
 Which sources are you referring to?  Can you verify the checksums:

 17dcac33e4f3adb69a57c2607b6de246  13322131  Python-2.6.4.tgz
 fee5408634a54e721a93531aba37f8c1  11249486  Python-2.6.4.tar.bz2

 There is a README at the root of the tarball:

Huh, looks like the .bz2 package is broken (even though the md5 is
fine). The .gz works fine.

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


Re: Why do you use python?

2009-10-31 Thread Bruno Desthuilliers
sk a écrit :
 What would be your answer if this question is asked to you in an
 interview?
 
 a modified version might be:
 Where would you use python over C/C++/Java?
 

As far as I'm concerned, I'd put it the other way round : where would I
use C/C++/Java over Python ?-)

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


Re: Are *.pyd's universal?

2009-10-31 Thread Lawrence D'Oliveiro
In message mailman.2365.1256979069.2807.python-l...@python.org, Albert 
Hopkins wrote:

 On Sat, 2009-10-31 at 21:32 +1300, Lawrence D'Oliveiro wrote:

In message 6e603d9c-2be0-449c-9c3c-bab49e09e...@13g2000prl.googlegroups.com, 
Carl Banks wrote:

 Modules will sometimes find themselves on the path in Windows, so the
 fact that Windows performs a library search on the path is quite
 significant.
 
 Why is it only Windows is prone to this problem?
 
 I think as someone pointed out earlier, in Unix-like operating systems,
 a regular library's file name starts with lib, e.g. libcrypt.so.  So
 this would not conflict with Python's crypt.so.

I just checked my Debian installation:

l...@theon:~ find /lib /usr/lib -name \*.so -a -not -name lib\* -print | 
wc -l
2950
l...@theon:~ find /lib /usr/lib -name \*.so -print | wc -l
4708

So 63% of the shareable libraries on my system have names NOT beginning with 
lib.

Any better theories?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Aaaargh! global name 'eggz' is not defined

2009-10-31 Thread Alan Franzoni
On 10/29/09 9:48 PM, kj wrote:
 How can one check that a Python script is lexically correct?

You can use a pseudo-static analyzer like pyflakes, pylint or pydoctor.

Or, better, you can avoid wild imports, excessive local or global
namespace manipulation, and break you program in smaller parts and write
unit tests for them.

Typos are very common but should very easy to catch. If you're not
catching them until a very long run of your program, then your code
coverage is probably too low.

-- 
Alan Franzoni
contact me at pub...@[mysurname].eu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are *.pyd's universal?

2009-10-31 Thread Carl Banks
On Oct 31, 1:32 am, Lawrence D'Oliveiro l...@geek-
central.gen.new_zealand wrote:
 In message 6e603d9c-2be0-449c-9c3c-

 bab49e09e...@13g2000prl.googlegroups.com, Carl Banks wrote:
  It's not Python that's the issue.  The issue is that if you have a
  module with a .dll extension, other programs could accidentally try to
  load that module instead of the intended dll, if the module is in the
  current directory or system path.  Modules will sometimes find
  themselves on the path in Windows, so the fact that Windows performs a
  library search on the path is quite significant.

 Why is it only Windows is prone to this problem?

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


problem with read() write()

2009-10-31 Thread Zeynel
Hello,

I've been studying the official tutorial, so far it's been fun, but
today I ran into a problem with the write(). So, I open the file pw
and write hello and read:

f = open(pw, r+)
f.write(hello)
f.read()

But read() returns a bunch of what looks like meta code:

ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
\xa5\x02\x0b
\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'QUEUE'\np1\n
(S'exec' 

What am I doing wrong? Thank you.

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


Re: Are *.pyd's universal?

2009-10-31 Thread Carl Banks
On Oct 31, 1:32 am, Lawrence D'Oliveiro l...@geek-
central.gen.new_zealand wrote:
 In message 6e603d9c-2be0-449c-9c3c-

 bab49e09e...@13g2000prl.googlegroups.com, Carl Banks wrote:
  It's not Python that's the issue.  The issue is that if you have a
  module with a .dll extension, other programs could accidentally try to
  load that module instead of the intended dll, if the module is in the
  current directory or system path.  Modules will sometimes find
  themselves on the path in Windows, so the fact that Windows performs a
  library search on the path is quite significant.

 Why is it only Windows is prone to this problem?

Unix doesn't search the executable path or current directory when
loading libraries, which if you would have seen if you hadn't ignored
my original reply.


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


Re: problem with read() write()

2009-10-31 Thread Alf P. Steinbach

* Zeynel:

Hello,

I've been studying the official tutorial, so far it's been fun, but
today I ran into a problem with the write(). So, I open the file pw
and write hello and read:

f = open(pw, r+)
f.write(hello)
f.read()

But read() returns a bunch of what looks like meta code:

ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
\xa5\x02\x0b
\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'QUEUE'\np1\n
(S'exec' 

What am I doing wrong? Thank you.


After the 'write' the current position in the file is after the hello, so 
reading will read further content from there.


The following works (disclaimer: I'm utter newbie in Python, and didn't consult 
the documentation, and it's the first time I've seen the Python 'open'):



f = open(pw, r+)
f.write( hello )
f.seek( 0 )  # Go back to start of file
f.read()
f.close()


Cheers  hth.,

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


Re: Are *.pyd's universal?

2009-10-31 Thread Albert Hopkins
On Sat, 2009-10-31 at 23:58 +1300, Lawrence D'Oliveiro wrote:
 I just checked my Debian installation:
 
 l...@theon:~ find /lib /usr/lib -name \*.so -a -not -name lib\*
 -print | wc -l
 2950
 l...@theon:~ find /lib /usr/lib -name \*.so -print | wc -l
 4708
 
 So 63% of the shareable libraries on my system have names NOT
 beginning with lib.
 
 Any better theories? 

Those are likely not system (sharable) libraries (e.g. libcrypt). These
are probably plugins loaded by a specific program, for example PAM
modules, ImageMagick plugins, python modules, etc.  so since they are
not in your library path the do not stand getting accidentally loaded
(e.g. when a binary is linked against libcrypt.so).  These libraries are
loaded directly by the program using their exact path name and dlopen().

The issue with windows they were saying is that Windows will load the
library with that name if it is in your current directory.  So if you
happen to have a python library called CRYPT.DLL and you are in that
directory and try to run a program that loads CRYPT.DLL then you will be
loading the python module instead of the Windows one (I have no idea if
Windows has a CRYPT.DLL).

OTOH this doesn't happen in Linux because a) programs wanting the
system's crypt library are looking for libcrypt.so and b) Linux doesn't
look in your current directory (by default) for libraries.




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


Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:
 * Zeynel:





  Hello,

  I've been studying the official tutorial, so far it's been fun, but
  today I ran into a problem with the write(). So, I open the file pw
  and write hello and read:

  f = open(pw, r+)
  f.write(hello)
  f.read()

  But read() returns a bunch of what looks like meta code:

  ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
  \xa5\x02\x0b
  \x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 
  0'QUEUE'\np1\n
  (S'exec' 

  What am I doing wrong? Thank you.

 After the 'write' the current position in the file is after the hello, so
 reading will read further content from there.

 The following works (disclaimer: I'm utter newbie in Python, and didn't 
 consult
 the documentation, and it's the first time I've seen the Python 'open'):

 f = open(pw, r+)
 f.write( hello )
 f.seek( 0 )  # Go back to start of file
 f.read()
 f.close()

 Cheers  hth.,

 - Alf

Thanks, but it didn't work for me. I still get the meta file. Although
I see that hello is there.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do you use python?

2009-10-31 Thread Jaime Buelta
On Oct 31, 11:31 am, Alf P. Steinbach al...@start.no wrote:
 * sk:

  [title Why do you use python?]
  What would be your answer if this question is asked to you in an
  interview?

  a modified version might be:
  Where would you use python over C/C++/Java?

  (because my resume says I know C/C++/Java)?

 The C++ FAQ addresses this question here:
 url:http://www.parashift.com/c++-faq-lite/big-picture.html#faq-6.5

 Cheers  hth.,

 - Alf

Mphf

Even I have some agreement that the choice of a language it's more a
business-driven issue I REALLY don't like that the techie weenie
shouldn't be heard. Talks a guy that programmed a GUI on Motif using C
(plain old C) in 2003 and takes almost forever (one year and a half),
instead of using any other graphical toolkit because we can't afford
to expend time on learning new languages...
I think time to development (usually related to mystical bugs well
hidden in layers and layers of code) it's an important thing to take
into account.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-10-31 Thread Alf P. Steinbach

* Zeynel:

On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:

* Zeynel:






Hello,
I've been studying the official tutorial, so far it's been fun, but
today I ran into a problem with the write(). So, I open the file pw
and write hello and read:
f = open(pw, r+)
f.write(hello)
f.read()
But read() returns a bunch of what looks like meta code:
ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
\xa5\x02\x0b
\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 
0'QUEUE'\np1\n
(S'exec' 
What am I doing wrong? Thank you.

After the 'write' the current position in the file is after the hello, so
reading will read further content from there.

The following works (disclaimer: I'm utter newbie in Python, and didn't consult
the documentation, and it's the first time I've seen the Python 'open'):

f = open(pw, r+)
f.write( hello )
f.seek( 0 )  # Go back to start of file
f.read()
f.close()

Cheers  hth.,

- Alf


Thanks, but it didn't work for me. I still get the meta file. Although
I see that hello is there.


Just a thought: try w+ instead of r+.

Because if you do

  print( open.__doc__ )

as I recall it said something about w truncating the file?


Cheers  hth.,

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


Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 9:55 am, Alf P. Steinbach al...@start.no wrote:
 * Zeynel:





  On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:
  * Zeynel:

  Hello,
  I've been studying the official tutorial, so far it's been fun, but
  today I ran into a problem with the write(). So, I open the file pw
  and write hello and read:
  f = open(pw, r+)
  f.write(hello)
  f.read()
  But read() returns a bunch of what looks like meta code:
  ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
  \xa5\x02\x0b
  \x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
   0'QUEUE'\np1\n
  (S'exec' 
  What am I doing wrong? Thank you.
  After the 'write' the current position in the file is after the hello, so
  reading will read further content from there.

  The following works (disclaimer: I'm utter newbie in Python, and didn't 
  consult
  the documentation, and it's the first time I've seen the Python 'open'):

  f = open(pw, r+)
  f.write( hello )
  f.seek( 0 )  # Go back to start of file
  f.read()
  f.close()

  Cheers  hth.,

  - Alf

  Thanks, but it didn't work for me. I still get the meta file. Although
  I see that hello is there.

 Just a thought: try w+ instead of r+.

 Because if you do

    print( open.__doc__ )

 as I recall it said something about w truncating the file?

 Cheers  hth.,

 - Alf

No :) I still got the same thing.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: datetime question

2009-10-31 Thread Victor Subervi
Thanks, Rami, that will work.
V

On Sat, Oct 31, 2009 at 4:54 AM, Albert Hopkins mar...@letterboxes.orgwrote:

 On Sat, 2009-10-31 at 20:34 +1100, Ben Finney wrote:
  Fixing ‘time’, ‘datetime’, and ‘calendar’ was the reason for Python 3?
  No, it wasn't.
 
  Or perhaps you mean that any backward-incompatible change was a reason
  to have Python 3? Even more firmly no. The extent of changes was
  severely limited to make the transition from Python 2 to Python 3 as
  painless as feasible, while still meeting the goals of Python 3.

 No, I meant cleaning up the standard library in spite of
 incompatibilities was one of the goals of Python3 (PEP 3108).
 Personally I don't see anything wrong with the modules, but that was my
 question to the person who said they should all be integrated but wasn't
 because of incompatibilities.

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

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


Re: problem with read() write()

2009-10-31 Thread Alf P. Steinbach

* Zeynel:

On Oct 31, 9:55 am, Alf P. Steinbach al...@start.no wrote:

* Zeynel:






On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:

* Zeynel:

Hello,
I've been studying the official tutorial, so far it's been fun, but
today I ran into a problem with the write(). So, I open the file pw
and write hello and read:
f = open(pw, r+)
f.write(hello)
f.read()
But read() returns a bunch of what looks like meta code:
ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
\xa5\x02\x0b
\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 
0'QUEUE'\np1\n
(S'exec' 
What am I doing wrong? Thank you.

After the 'write' the current position in the file is after the hello, so
reading will read further content from there.
The following works (disclaimer: I'm utter newbie in Python, and didn't consult
the documentation, and it's the first time I've seen the Python 'open'):
f = open(pw, r+)
f.write( hello )
f.seek( 0 )  # Go back to start of file
f.read()
f.close()
Cheers  hth.,
- Alf

Thanks, but it didn't work for me. I still get the meta file. Although
I see that hello is there.

Just a thought: try w+ instead of r+.

Because if you do

   print( open.__doc__ )

as I recall it said something about w truncating the file?

Cheers  hth.,

- Alf


No :) I still got the same thing.


Hm. Now I had to look in the docs because I thought I'd given bad advice.

Doc of 'open' says:

  The mode 'w+' opens and truncates the file to 0 bytes, while 'r+' opens the
   file without truncation.

So with 'w+' the only way to get garbage is if 'read' reads beyond the end of 
file, or 'open' doesn't conform to the documentation.


Testing with Python 3.1.1 under Windows XP Pro:

example
 f = open( zilly, w+ )
 f.write( garbagegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage )
63
 f.close()
 f = open( zilly, r )
 f.read()
'garbagegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage'
 f.close()
 f.open( zilly, r+ )
Traceback (most recent call last):
  File stdin, line 1, in module
AttributeError: '_io.TextIOWrapper' object has no attribute 'open'
 open( zilly, r+ )
_io.TextIOWrapper name='zilly' encoding='cp1252'
 f = open( zilly, r+ )
 f.write( hello )
5
 f.seek( 0 )
0
 f.read()
'hellogegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage'
 f.close()
 f = open( zilly, w+ )
 f.write( hello )
5
 f.seek( 0 )
0
 f.read()
'hello'
 f.close()

/example

The w+ works here. Even if I made a typing mistake and apparently left the 
file open in the middle there.



Cheers  hth.,

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


Re: problem with read() write()

2009-10-31 Thread Dave Angel

Zeynel wrote:

On Oct 31, 9:55 am, Alf P. Steinbach al...@start.no wrote:
  

* Zeynel:







On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:
  

* Zeynel:


Hello,
I've been studying the official tutorial, so far it's been fun, but
today I ran into a problem with the write(). So, I open the file pw
and write hello and read:
f =pen(pw, r+)
f.write(hello)
f.read()
But read() returns a bunch of what looks like meta code:
ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
\xa5\x02\x0b
\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 
0'QUEUE'\np1\n
(S'exec' 
What am I doing wrong? Thank you.
  

After the 'write' the current position in the file is after the hello, so
reading will read further content from there.

The following works (disclaimer: I'm utter newbie in Python, and didn't consult

the documentation, and it's the first time I've seen the Python 'open'):

f =pen(pw, r+)

f.write( hello )
f.seek( 0 )  # Go back to start of file
f.read()
f.close()

Cheers  hth.,

- Alf


Thanks, but it didn't work for me. I still get the meta file. Although
I see that hello is there.
  

Just a thought: try w+ instead of r+.

Because if you do

   print( open.__doc__ )

as I recall it said something about w truncating the file?

Cheers  hth.,

- Alf



No :) I still got the same thing.

  
When you ran the program, the file already existed.  So you're mixing 
the old content and the new.  If you don't want that, then don't use r+. 

When testing, you should start with the system in a known state.  Try 
initializing (with a text editor for example) that file before trying 
the program.


And if it still gives you problems, show us the data in the file before 
running your code, your new code, and the results you get.   One thing 
that'd be useful is to actually assign the results of the read() 
function to a variable and print it.


DaveA

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


Scheduling used in the multiprocessing.Pool.map() function

2009-10-31 Thread rpg
Hi all,

I have been using the map() function in the multiprocessing module to
parallelize my tasks on a dual core CPU. My tasks are embarrassingly
parallel, shared nothing tasks. In one of my runs, I found that the
this function interleaves execution of two processes over a single
list.

So far so good. But the problem is that the last remnant job is
executed serially. I mean that it seems that the job scheduling is
essentially static, and the last piece does not execute in parallel.

Why can't there be a task-stealing scheduler in multiprocessing? Each
of my individual function call in map takes over half hour (Each
function call internally calls out to c++ code). This could be a very
useful addition to multiprocessing's utility.

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


import bug

2009-10-31 Thread kj



I'm running into an ugly bug, which, IMHO, is really a bug in the
design of Python's module import scheme.  Consider the following
directory structure:

ham
|-- __init__.py
|-- re.py
`-- spam.py

...with the following very simple files:

% head ham/*.py
== ham/__init__.py ==

== ham/re.py ==

== ham/spam.py ==
import inspect

I.e. only ham/spam.py is not empty, and it contains the single line
import inspect.

If I now run the innocent-looking ham/spam.py, I get the following
error:

% python26 ham/spam.py
Traceback (most recent call last):
  File ham/spam.py, line 1, in module
import inspect
  File /usr/local/python-2.6.1/lib/python2.6/inspect.py, line 35, in module
import string
  File /usr/local/python-2.6.1/lib/python2.6/string.py, line 122, in module
class Template:
  File /usr/local/python-2.6.1/lib/python2.6/string.py, line 116, in __init__
'delim' : _re.escape(cls.delimiter),
AttributeError: 'module' object has no attribute 'escape'

or, similarly,

% python3 ham/spam.py
Traceback (most recent call last):
  File ham/spam.py, line 1, in module
import inspect
  File /usr/local/python-3.0/lib/python3.0/inspect.py, line 36, in module
import string
  File /usr/local/python-3.0/lib/python3.0/string.py, line 104, in module
class Template(metaclass=_TemplateMetaclass):
  File /usr/local/python-3.0/lib/python3.0/string.py, line 98, in __init__
'delim' : _re.escape(cls.delimiter),
AttributeError: 'module' object has no attribute 'escape'

My sin appears to be having the (empty) file ham/re.py.  So Python
is confusing it with the re module of the standard library, and
using it when the inspect module tries to import re.

I've tried a lot of things to appease Python on this one, including
a liberal sprinkling of from __future__ import absolute_import
all over the place (except, of course, in inspect.py, which I don't
control), but to no avail.

I also pored over pp. 149-151 of Beazley's Python Essential Reference
(4th ed.) on anything that would shed light on this problem, and
again, nothing.

I give up: what's the trick?  (Of course, renaming ham/re.py is
hardly the trick.  It's rather Procrustes' Bed.)

BTW, it is hard for me to imagine of an argument that could convince
me that this is not a design bug, and a pretty ugly one at that.
But, as they say, hope springs eternal: is there a PEP on the
subject?  (I know that there's a PEP on absolute_import, but since
absolute_import appears to be absolutely ineffectual here, I figure
I must look elsewhere for enlightenment.)

TIA!

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


Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 10:40 am, Alf P. Steinbach al...@start.no wrote:
Thanks! This works. But I need to close the file before read and open
it again with r, otherwise I get the garbage again. Can you give me
the link where you got this in documentation:

The mode 'w+' opens and truncates the file to 0 bytes, while 'r+'
opens the
 file without truncation.

Only place i could find it was in this bug report: 
http://bugs.python.org/issue5061

 * Zeynel:





  On Oct 31, 9:55 am, Alf P. Steinbach al...@start.no wrote:
  * Zeynel:

  On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:
  * Zeynel:
  Hello,
  I've been studying the official tutorial, so far it's been fun, but
  today I ran into a problem with the write(). So, I open the file pw
  and write hello and read:
  f = open(pw, r+)
  f.write(hello)
  f.read()
  But read() returns a bunch of what looks like meta code:
  ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
  \xa5\x02\x0b
  \x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
   0'QUEUE'\np1\n
  (S'exec' 
  What am I doing wrong? Thank you.
  After the 'write' the current position in the file is after the hello, 
  so
  reading will read further content from there.
  The following works (disclaimer: I'm utter newbie in Python, and didn't 
  consult
  the documentation, and it's the first time I've seen the Python 'open'):
  f = open(pw, r+)
  f.write( hello )
  f.seek( 0 )  # Go back to start of file
  f.read()
  f.close()
  Cheers  hth.,
  - Alf
  Thanks, but it didn't work for me. I still get the meta file. Although
  I see that hello is there.
  Just a thought: try w+ instead of r+.

  Because if you do

     print( open.__doc__ )

  as I recall it said something about w truncating the file?

  Cheers  hth.,

  - Alf

  No :) I still got the same thing.

 Hm. Now I had to look in the docs because I thought I'd given bad advice.

 Doc of 'open' says:

    The mode 'w+' opens and truncates the file to 0 bytes, while 'r+' opens 
 the
     file without truncation.

 So with 'w+' the only way to get garbage is if 'read' reads beyond the end of
 file, or 'open' doesn't conform to the documentation.

 Testing with Python 3.1.1 under Windows XP Pro:

 example
   f = open( zilly, w+ )
   f.write( 
 garbagegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage )
 63
   f.close()
   f = open( zilly, r )
   f.read()
 'garbagegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage'
   f.close()
   f.open( zilly, r+ )
 Traceback (most recent call last):
    File stdin, line 1, in module
 AttributeError: '_io.TextIOWrapper' object has no attribute 'open'
   open( zilly, r+ )
 _io.TextIOWrapper name='zilly' encoding='cp1252'
   f = open( zilly, r+ )
   f.write( hello )
 5
   f.seek( 0 )
 0
   f.read()
 'hellogegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage'
   f.close()
   f = open( zilly, w+ )
   f.write( hello )
 5
   f.seek( 0 )
 0
   f.read()
 'hello'
   f.close()
  
 /example

 The w+ works here. Even if I made a typing mistake and apparently left the
 file open in the middle there.

 Cheers  hth.,

 - Alf

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


python os.path.exists failure

2009-10-31 Thread koranthala
Hi all,
   My code is as follows:

path = r'C:/Program Files/testfolder/2.3/test.txt'
if os.path.lexists(path):
print 'Path Exists'
else:
print 'No file found in path - %s' %path
print Popen(path, stdout=PIPE, shell=True).stdout.read()

The output comes as
No file found in path - C:/Program Files/testfolder/2.3/test.txt
but the test.txt file is opened.

The issue, I guess, is that the double quotes inside is failing the
check. But without the double quotes, Popen fails.
One solution, I can think is to check without double quotes, and then
using some code, put the double quotes back inside, but it looks quite
kludgy.

What is the usual solution to this?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import bug

2009-10-31 Thread Jon Clements
On Oct 31, 3:12 pm, kj no.em...@please.post wrote:
 I'm running into an ugly bug, which, IMHO, is really a bug in the
 design of Python's module import scheme.  Consider the following
 directory structure:

 ham
 |-- __init__.py
 |-- re.py
 `-- spam.py

 ...with the following very simple files:

 % head ham/*.py
 == ham/__init__.py ==

 == ham/re.py ==

 == ham/spam.py ==
 import inspect

 I.e. only ham/spam.py is not empty, and it contains the single line
 import inspect.

 If I now run the innocent-looking ham/spam.py, I get the following
 error:

 % python26 ham/spam.py
 Traceback (most recent call last):
   File ham/spam.py, line 1, in module
     import inspect
   File /usr/local/python-2.6.1/lib/python2.6/inspect.py, line 35, in 
 module
     import string
   File /usr/local/python-2.6.1/lib/python2.6/string.py, line 122, in 
 module
     class Template:
   File /usr/local/python-2.6.1/lib/python2.6/string.py, line 116, in 
 __init__
     'delim' : _re.escape(cls.delimiter),
 AttributeError: 'module' object has no attribute 'escape'

 or, similarly,

 % python3 ham/spam.py
 Traceback (most recent call last):
   File ham/spam.py, line 1, in module
     import inspect
   File /usr/local/python-3.0/lib/python3.0/inspect.py, line 36, in module
     import string
   File /usr/local/python-3.0/lib/python3.0/string.py, line 104, in module
     class Template(metaclass=_TemplateMetaclass):
   File /usr/local/python-3.0/lib/python3.0/string.py, line 98, in __init__
     'delim' : _re.escape(cls.delimiter),
 AttributeError: 'module' object has no attribute 'escape'

 My sin appears to be having the (empty) file ham/re.py.  So Python
 is confusing it with the re module of the standard library, and
 using it when the inspect module tries to import re.

 I've tried a lot of things to appease Python on this one, including
 a liberal sprinkling of from __future__ import absolute_import
 all over the place (except, of course, in inspect.py, which I don't
 control), but to no avail.

 I also pored over pp. 149-151 of Beazley's Python Essential Reference
 (4th ed.) on anything that would shed light on this problem, and
 again, nothing.

 I give up: what's the trick?  (Of course, renaming ham/re.py is
 hardly the trick.  It's rather Procrustes' Bed.)

 BTW, it is hard for me to imagine of an argument that could convince
 me that this is not a design bug, and a pretty ugly one at that.
 But, as they say, hope springs eternal: is there a PEP on the
 subject?  (I know that there's a PEP on absolute_import, but since
 absolute_import appears to be absolutely ineffectual here, I figure
 I must look elsewhere for enlightenment.)

 TIA!

 kynn

You can shift the location of the current directory further down the
search path.
Assuming sys.path[0] is ''...

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type help, copyright, credits or license for more information.
 import sys
 sys.path = sys.path[1:] + ['']
 import spam
 spam.__file__
'spam.pyc'

hth
Jon.
hth,

Jon.





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


Re: python os.path.exists failure

2009-10-31 Thread Benjamin Kaplan
On Sat, Oct 31, 2009 at 11:26 AM, koranthala koranth...@gmail.com wrote:
 Hi all,
   My code is as follows:

 path = r'C:/Program Files/testfolder/2.3/test.txt'
 if os.path.lexists(path):
    print 'Path Exists'
 else:
    print 'No file found in path - %s' %path
 print Popen(path, stdout=PIPE, shell=True).stdout.read()

 The output comes as
 No file found in path - C:/Program Files/testfolder/2.3/test.txt
 but the test.txt file is opened.

 The issue, I guess, is that the double quotes inside is failing the
 check. But without the double quotes, Popen fails.
 One solution, I can think is to check without double quotes, and then
 using some code, put the double quotes back inside, but it looks quite
 kludgy.


Just out of curiosity, does 'C:/Program FIles/' even work on the
Windows command line? The usual procedure is to put the entire path in
quotes. r'C:\Program Files\...'.


 What is the usual solution to this?
 --
 http://mail.python.org/mailman/listinfo/python-list

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


Re: import bug

2009-10-31 Thread Stefan Behnel
kj, 31.10.2009 16:12:
 My sin appears to be having the (empty) file ham/re.py.  So Python
 is confusing it with the re module of the standard library, and
 using it when the inspect module tries to import re.

1) it's a bad idea to name your own modules after modules in the stdlib
2) this has been fixed in Py3

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


PIL Image.fromarray( ... , mode=1 )

2009-10-31 Thread Hans Georg Schaathun
Does anyone know how to save two-tone images represented as
numpy arrays?  I handle grayscale images by converting to
PIL Image objects (mode=L) and then use the PIL save method,
but I cannot make this work with mode=1.

I have tried both boolean arrays and uint8 arrays (mod 2).
In both cases I get an image which is predominantly black,
with thin white stripes (possibly for every 8 pixels).
If, instead, multiply my (mod 2) image by 255, and then
convert with mode=L, I get the expected random-noise-looking
image.

Does anyone have any ideas?  What do I do wrong?  What is the
right/best way to save/convert two-tone images?
I have not managed to find proper documentation for the 
Image.fromarray() method; the docstring seems to be empty )-:

TIA
:-- Hans Georg
-- 
http://mail.python.org/mailman/listinfo/python-list


How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Peng Yu
I have the following files, which are in the directory 'test'. The
parent directory of 'test' is in $PYTHONPATH. I have 'from A import A'
and 'from B import B' in '__init__.py', because I want to use 'test.A'
and 'test.B' to refer to classes A and B rather than 'test.A.A' and
'test.B.B'.

$ll -g
total 24
-rw-r--r-- 1 staff  32 2009-10-31 10:41:47 __init__.py
-rw-r--r-- 1 staff 235 2009-10-31 10:45:24 __init__.pyc
-rw-r--r-- 1 staff 550 2009-10-31 10:45:24 B.pyc
-rw-r--r-- 1 staff 550 2009-10-31 10:45:24 A.pyc
-rw-r--r-- 1 staff  54 2009-10-31 10:46:03 A.py
-rw-r--r-- 1 staff  54 2009-10-31 10:46:14 B.py
$cat __init__.py
from A import A
from B import B
$cat A.py
class A:
  def __init__(self):
print '__init__ A'
$cat B.py
class B:
  def __init__(self):
print '__init__ B'


Then I have the following python files to call the modules. However,
because I have 'import A from A' in '__init__.py', I can not call
'test.A.A()' anymore. Even I only have 'import test.A', both modules
'A' and 'B' are imported. So 'import test.A' is essentially the same
as 'import test'.

I'm wondering if there is a way to make the following two things hold.
Thank you1
1. When I 'import test', I can refer to class A as 'test.A'.
2. When I 'import test.A', I can refer to class A as 'test.A.A' and
class B shall not be imported.



$cat fail.py
import test.A
test.A.A()
$python fail.py
Traceback (most recent call last):
  File fail.py, line 2, in module
test.A.A()
AttributeError: class A has no attribute 'A'
$cat main.py
import test
test.A()
test.B()
$python main.py
__init__ A
__init__ B
$cat fail2.py
import test.A
test.A()
test.B()
$python fail2.py
__init__ A
__init__ B
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sqlite3. Substitution of names in query.

2009-10-31 Thread Carsten Haese
Lawrence D'Oliveiro wrote:
 In message mailman.2357.1256964121.2807.python-l...@python.org, Dennis Lee 
 Bieber wrote:
 
 This way regular string interpolation operations (or whatever Python
 3.x has replaced it with) are safe to construct the SQL, leaving only
 user supplied (or program generated) data values to be passed via the
 DB-API parameter system -- so that they are properly escaped and
 rendered safe.
 
 Mixing the two is another recipe for confusion and mistakes.

Mixing the two is necessary. According to the SQL standard, parameters
can only take the place of literal values. Parameters can't take the
place of identifiers or keywords that make up the structure of the query.

So, you use string manipulation to build the structure of the query, and
then you use parameter binding to fill values into the query. They are
two different tools for two fundamentally different jobs. As long as you
understand what you're doing, there should be no confusion. (And if you
don't understand what you're doing, you shouldn't be doing it!)

--
Carsten Haese
http://informixdb.sourceforge.net

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


Re: self.__dict__ tricks

2009-10-31 Thread Tim Johnson
On 2009-10-31, Steven D'Aprano st...@remove-this-cybersource.com.au wrote:
 Idiomatic Python is to use CamelCase for classes.
   Can you point me to a discussion on Idiomatic Python, CamelCase and
   other matters?

... See PEP 8:

 http://www.python.org/dev/peps/pep-0008/
  Got it. Thanks.

 invalid parameter shouldn't raise the same error as failing an
 attribute look-up. That's misleading and confusing.

 What error class or other approach do you recommend?

 Unless you have a good reason for doing something different, do what 
 Python built-ins do:

 int('123', parrot=16)
 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: 'parrot' is an invalid keyword argument for this function
 Understood. I kyped that method from from an open source library module
 and have been using it ever since.

 I also should point out that your trick will fail if you are using
 __slots__.
   ??. Will research that. Elaborate if you wish.

 __slots__ are an optimization for making objects smaller than normal if 
 you have many millions of them:

 http://docs.python.org/reference/datamodel.html#slots
  Thanks.


   If the class grows - and I expect it will - I'd prefer to stick with
   the keywords approach. That approach also allows me to use a
   dictionary to initialize the object.

 You can still do that with named parameters.

...
 class Parrot:
 ... def __init__(self, name='Polly', colour='blue',

 p = Parrot(Sparky, 'white', Cockatoo)
 data = dict(colour='red', name='Fred', foo=1)
 p = Parrot(**data)  # raise an error with bad input
 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: __init__() got an unexpected keyword argument 'foo'
... OK. That makes sense. You have made a believer of me.

I really appreciate all the time you have taken with this.
Many programmers I know stay away from 'lists' such as this, because
they are afraid to show their ignorance. Me, I'm fearless, and I have
learned a lot that I might not have otherwise.

take care
-- 
Tim 
t...@johnsons-web.com
http://www.akwebsoft.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get the realpath of a symbolic link?

2009-10-31 Thread Emile van Sebille

On 10/31/2009 12:03 AM Peng Yu said...

Suppose that I have the following directory and files. I want to get
the canonical path of a file, a directory or a symbolic link.
For example, for 'b' below, I want to get its canonical path as
'/private/tmp/abspath/b'.


So, why isn't realpath working for you?  It looks like it is, and it 
works that way here:


 os.path.realpath('/home/emile/vmlinuz')
'/root/vmlinuz-2.4.7-10'


Emile

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


Re: import bug

2009-10-31 Thread kj
In 4aec591e$0$7629$9b4e6...@newsspool1.arcor-online.net Stefan Behnel 
stefan...@behnel.de writes:

kj, 31.10.2009 16:12:
 My sin appears to be having the (empty) file ham/re.py.  So Python
 is confusing it with the re module of the standard library, and
 using it when the inspect module tries to import re.

1) it's a bad idea to name your own modules after modules in the stdlib

Obviously, since it leads to the headaches this thread illustrates.
But there is nothing intrisically wrong with it.  The fact that it
is problematic in Python is a design bug, plain and simple.  There's
no rational basis for it, and represents an unreasonable demand on
module writers, since contrary to the tight control on reserved
Python keywords, there does not seem to be a similar control on
the names of stdlib modules.  What if, for example, in the future
it was decided that my_favorite_module name would become part of
the standard library?  This alone would cause code to break.

2) this has been fixed in Py3

In my post I illustrated that the failure occurs both with Python
2.6 *and* Python 3.0.  Did you have a particular version of Python
3 in mind?

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


Re: Are *.pyd's universal?

2009-10-31 Thread Carl Banks
On Oct 31, 6:49 am, Albert Hopkins mar...@letterboxes.org wrote:
 OTOH this doesn't happen in Linux because a) programs wanting the
 system's crypt library are looking for libcrypt.so and b) Linux doesn't
 look in your current directory (by default) for libraries.

One other thing is that linux binaries are usually linked against a
versioned .so file, so a random command would refernce libc.so.6
rather than libc.so.

I don't think that's necessarily the case for all Unix-like OSes,
though.


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


Re: How to get the realpath of a symbolic link?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 11:26 AM, Emile van Sebille em...@fenx.com wrote:
 On 10/31/2009 12:03 AM Peng Yu said...

 Suppose that I have the following directory and files. I want to get
 the canonical path of a file, a directory or a symbolic link.
 For example, for 'b' below, I want to get its canonical path as
 '/private/tmp/abspath/b'.

 So, why isn't realpath working for you?  It looks like it is, and it works
 that way here:

 os.path.realpath('/home/emile/vmlinuz')
 '/root/vmlinuz-2.4.7-10'

My definition of 'realpath' is different from the definition of
'os.path.realpath'. But I'm not short what term I should use to
describe. I use the following example to show what I want.

In my example in the original post,

'/tmp/abspath/b' is a symbolic link to '/tmp/abspath/a' and '/tmp' is
a symbolic link to '/private/tmp'.

Therefore, I want to get '/private/tmp/abspath/b', rather than
'/private/tmp/abspath/a', as the canonical path of 'b'.

If the argument is a symbolic link os.path.realpath will return the
actually target of the symbolic link. However, I want the path of the
symbolic link rather than the path of the target.

Hope this is clear.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import bug

2009-10-31 Thread Albert Hopkins
On Sat, 2009-10-31 at 16:27 +, kj wrote:
 2) this has been fixed in Py3
 
 In my post I illustrated that the failure occurs both with Python
 2.6 *and* Python 3.0.  Did you have a particular version of Python
 3 in mind? 

I was not able to reproduce with my python3:

$ head ham/*.py
== ham/__init__.py ==

== ham/re.py ==

== ham/spam.py ==
import inspect
$ python3 ham/spam.py
$ python3 --version
Python 3.1.1


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


Re: self.__dict__ tricks

2009-10-31 Thread MRAB

Hendrik van Rooyen wrote:

On Friday, 30 October 2009 17:28:47 MRAB wrote:


Wouldn't it be clearer if they were called dromedaryCase and
BactrianCase? :-)


Ogden Nash:

The Camel has a single hump-
The Dromedary, two;
Or the other way around-
I'm never sure. - Are You?


If you make the first letter a capital:

Dromedary starts with D, 1 bump, 1 hump.

Bactrian starts with B, 2 bumps, 2 humps.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Duncan Booth
Peng Yu pengyu...@gmail.com wrote:

 I'm wondering if there is a way to make the following two things hold.
 Thank you1
 1. When I 'import test', I can refer to class A as 'test.A'.
 2. When I 'import test.A', I can refer to class A as 'test.A.A' and
 class B shall not be imported.
 
No. Either import adds the name 'test' to the current namespace. That name 
in each case references the same thing.

Your simplest solution would be to give the sub-modules lowercase 
filenames, then you can do:

   import test
   test.A()

or

   import test.a
   test.a.A()

or even

   import test.a
   test.b.B()

It would probably be best though just to be consistent as to how you 
reference the classes: define a public interface for your package and stick 
to it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get the realpath of a symbolic link?

2009-10-31 Thread Emile van Sebille

On 10/31/2009 10:11 AM Peng Yu said...

 My definition of 'realpath' is different from the definition of
 'os.path.realpath'. But I'm not short what term I should use to
 describe. I use the following example to show what I want.

 In my example in the original post,

 '/tmp/abspath/b' is a symbolic link to '/tmp/abspath/a' and '/tmp' is
 a symbolic link to '/private/tmp'.

 Therefore, I want to get '/private/tmp/abspath/b', rather than
 '/private/tmp/abspath/a', as the canonical path of 'b'.


It still looks like it works here.  I've set up a similar structure and 
appear to get the results you're asking for using os.path.realpath.


# pwd
/home/emile
# ls -l
drwxr-xr-x 3 root root 4096 2009-10-31 10:25 private
lrwxrwxrwx 1 root root   11 2009-10-31 10:25 tmp - private/tmp

# pwd
/home/emile/tmp/abspath
# ls -l
-rw-r--r-- 1 root root 10 2009-10-31 10:25 a
lrwxrwxrwx 1 root root  1 2009-10-31 10:26 b - a

Python 2.6.3 (r263:75183, Oct 15 2009, 15:03:49) [GCC 4.3.2] on linux2
 import os
 os.path.realpath('/home/emile/tmp/a')
'/home/emile/private/tmp/a'
 os.path.realpath('/home/emile/tmp/b')
'/home/emile/private/tmp/b'


If the argument is a symbolic link os.path.realpath will return the
actually target of the symbolic link. 
However, I want the path of the

symbolic link rather than the path of the target.


Which is what I got above.



Hope this is clear.


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


Re: list comprehension problem

2009-10-31 Thread Terry Reedy

alex23 wrote:

Terry Reedy tjre...@udel.edu wrote:

alex23 wrote:

You're completely wrong. Immutability has nothing to do with identity,

...
 I'm honestly not getting your point here.

Let me try again, a bit differently.

I claim that the second statement, and therefor the first, can be seen 
as wrong. I also claim that (Python) programmers need to understand why.


In mathematics, we generally have immutable values whose 'identity' is 
their value. There is, for example, only one, immutable, empty set.


In informatics, and in particular in Python, in order to have 
mutability, we have objects with value and an identity that is separate 
from their value.  There can be, for example, multiple mutable empty 
sets. Identity is important because we must care about which empty set 
we add things to. 'Identity' is only needed because of 'mutability', so 
it is mistaken to say they have nothing to do with each other.


Ideally, from both a conceptual and space efficiency view, an 
implementation would allow only one copy for each value of immutable 
classes. This is what new programmers assume when they blithely use 'is' 
instead of '==' (as would usually be correct in math).


However, for time efficiency reasons, there is no unique copy guarantee, 
so one must use '==' instead of 'is', except in those few cases where 
there is a unique copy guarantee, either by the language spec or by 
one's own design, when one must use 'is' and not '=='.  Here 'must' 
means 'must to be generally assured of program correctness as intended'.


We obviously agree on this guideline.

Terry Jan Reedy

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


Re: How to get the realpath of a symbolic link?

2009-10-31 Thread Terry Reedy

Peng Yu wrote:

On Sat, Oct 31, 2009 at 11:26 AM, Emile van Sebille em...@fenx.com wrote:

On 10/31/2009 12:03 AM Peng Yu said...

Suppose that I have the following directory and files. I want to get
the canonical path of a file, a directory or a symbolic link.
For example, for 'b' below, I want to get its canonical path as
'/private/tmp/abspath/b'.

So, why isn't realpath working for you?  It looks like it is, and it works
that way here:


os.path.realpath('/home/emile/vmlinuz')

'/root/vmlinuz-2.4.7-10'


My definition of 'realpath' is different from the definition of
'os.path.realpath'. But I'm not short what term I should use to
describe. I use the following example to show what I want.

In my example in the original post,

'/tmp/abspath/b' is a symbolic link to '/tmp/abspath/a' and '/tmp' is
a symbolic link to '/private/tmp'.

Therefore, I want to get '/private/tmp/abspath/b', rather than
'/private/tmp/abspath/a', as the canonical path of 'b'.

If the argument is a symbolic link os.path.realpath will return the
actually target of the symbolic link. However, I want the path of the
symbolic link rather than the path of the target.

Hope this is clear.


I suspect that you will have to write your own code for your own 
function. os and os.path are written in Python, so look at the code for 
realpath and modify it for your modified definition.


Terry Jan Reedy

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


Re: problem with read() write()

2009-10-31 Thread Terry Reedy

Zeynel wrote:

On Oct 31, 10:40 am, Alf P. Steinbach al...@start.no wrote:
Thanks! This works. But I need to close the file before read and open
it again with r, otherwise I get the garbage again. Can you give me
the link where you got this in documentation:

The mode 'w+' opens and truncates the file to 0 bytes, while 'r+'
opens the

file without truncation.


Only place i could find it was in this bug report: 
http://bugs.python.org/issue5061


LibRef / builtin functions /open
The entry is a full page.

Get familiar with this section and the Built-in Types section.

Terry Jan Reedy

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


Re: OT: DARPA red balloon challenge

2009-10-31 Thread John Nagle

Adam N wrote:

All,

In case people hadn't heard, DARPA just announced what I think is the
coolest competition ever:

http://networkchallenge.darpa.mil/

On December 5, DARPA will raise 10 red weather balloons somewhere in
the US.  The first person to get the location of all 10 balloons and
submit them will be given $40k.


   You're thinking of doing this with people running around?  Nah.
The right way is to get some successive images of the earth from
Digital Globe.  They just launched their WorldView-2 satellite.

http://www.satimagingcorp.com/satellite-sensors/worldview-2.html

   They image the whole planet at 0.42m resolution in color every
two days.  Upload that to some cloud system and find all red dots
in the continental US that weren't there before the event.

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


Re: How to get the realpath of a symbolic link?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 1:10 PM, Emile van Sebille em...@fenx.com wrote:
 On 10/31/2009 10:11 AM Peng Yu said...

 My definition of 'realpath' is different from the definition of
 'os.path.realpath'. But I'm not short what term I should use to
 describe. I use the following example to show what I want.

 In my example in the original post,

 '/tmp/abspath/b' is a symbolic link to '/tmp/abspath/a' and '/tmp' is
 a symbolic link to '/private/tmp'.

 Therefore, I want to get '/private/tmp/abspath/b', rather than
 '/private/tmp/abspath/a', as the canonical path of 'b'.


 It still looks like it works here.  I've set up a similar structure and
 appear to get the results you're asking for using os.path.realpath.

 # pwd
 /home/emile
 # ls -l
 drwxr-xr-x 3 root root 4096 2009-10-31 10:25 private
 lrwxrwxrwx 1 root root   11 2009-10-31 10:25 tmp - private/tmp

 # pwd
 /home/emile/tmp/abspath
 # ls -l
 -rw-r--r-- 1 root root 10 2009-10-31 10:25 a
 lrwxrwxrwx 1 root root  1 2009-10-31 10:26 b - a

 Python 2.6.3 (r263:75183, Oct 15 2009, 15:03:49) [GCC 4.3.2] on linux2
 import os
 os.path.realpath('/home/emile/tmp/a')
 '/home/emile/private/tmp/a'
 os.path.realpath('/home/emile/tmp/b')
 '/home/emile/private/tmp/b'

 If the argument is a symbolic link os.path.realpath will return the
 actually target of the symbolic link. However, I want the path of the
 symbolic link rather than the path of the target.

 Which is what I got above.

I'm curious why we get different results. I tried on both linux and
mac. Both of them give me the same results.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python newbie data structures question

2009-10-31 Thread codingJoe
On Oct 29, 2:06 am, Chris Rebert c...@rebertia.com wrote:
 On Wed, Oct 28, 2009 at 11:31 PM, codingJoe tracy.monte...@gmail.com wrote:
  Hi all!

  I am trying to choose the right data structure to do a value lookup
  with multiple keys.

  I want to lookup data by:  key, key,{ values }

  My final product should be able to reference this datastructure from
  within a django template.

  Because my lookup needs only 80 values and will never change, it seems
  that I could just build some type of data structure or object.   My
  rudamentary approach is to do lots of looping, but there must be a
  better way to do this.

  A dictionary object only has (key, value).  The same pair of keys
  could return multiple values.
  Tuples might work, but I just don't understand them enough.

  the simple exampe is a quick lookup index the available sport by
  season that returns one or many value.     The values would be data
  structures and contain more than just the name, but also things like
  uniform color, team size.

              indoors       outdoors
             ---
  winter   |  bball         |  skiing     |
             |                 |  sledding  |
             |--
             |                  |  baseball |
  summer|  raquetball  |  soccer    |
             |                  |               |
             |
             |--

  Advice?  What data structure should I use?  And a simple newbie
  example please..

 Besides Alex's option of using tuples as dictionary keys:

 A. An SQL database
 (http://docs.python.org/library/sqlite3.html#module-sqlite3). Overkill
 for something as simple as your toy example, but in analogous but more
 complicated cases, it's one method to consider.

 B. Nested dictionaries. Particularly useful if you want to access the
 data by broader categories (e.g. all summer sports), although it only
 works for one axis of categories (i.e. season or doors-ness); if you
 want to access by the other axis, you have to code it yourself.
 Example:
 sports = {winter : {indoors:[bball], outdoors:[skiing,
 sledding]}, summer : {indoors:[raquetball],
 outdoors:[baseball,soccer]} }
 bball = sports[winter][indoors][0]
 baseball, soccer = sports[summer][outdoors]
 all_summer_sports = sum(sports[summer].values(), [])

 Cheers,
 Chris
 --http://blog.rebertia.com- Hide quoted text -

 - Show quoted text -


Nice,  both examples work well on the Python side.  Now I am trying to
print the values from a Django template.  I've nested them in a table
similar to the example above to be rendered in a web page.  How do I
do that?


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


Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 3:11 pm, Terry Reedy tjre...@udel.edu wrote:

Great, thanks.

 Zeynel wrote:
  On Oct 31, 10:40 am, Alf P. Steinbach al...@start.no wrote:
  Thanks! This works. But I need to close the file before read and open
  it again with r, otherwise I get the garbage again. Can you give me
  the link where you got this in documentation:

  The mode 'w+' opens and truncates the file to 0 bytes, while 'r+'
  opens the
      file without truncation.

  Only place i could find it was in this bug 
  report:http://bugs.python.org/issue5061

 LibRef / builtin functions /open
 The entry is a full page.

 Get familiar with this section and the Built-in Types section.

 Terry Jan Reedy

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


Re: How to get the realpath of a symbolic link?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 1:46 PM, Terry Reedy tjre...@udel.edu wrote:
 Peng Yu wrote:

 On Sat, Oct 31, 2009 at 11:26 AM, Emile van Sebille em...@fenx.com
 wrote:

 On 10/31/2009 12:03 AM Peng Yu said...

 Suppose that I have the following directory and files. I want to get
 the canonical path of a file, a directory or a symbolic link.
 For example, for 'b' below, I want to get its canonical path as
 '/private/tmp/abspath/b'.

 So, why isn't realpath working for you?  It looks like it is, and it
 works
 that way here:

 os.path.realpath('/home/emile/vmlinuz')

 '/root/vmlinuz-2.4.7-10'

 My definition of 'realpath' is different from the definition of
 'os.path.realpath'. But I'm not short what term I should use to
 describe. I use the following example to show what I want.

 In my example in the original post,

 '/tmp/abspath/b' is a symbolic link to '/tmp/abspath/a' and '/tmp' is
 a symbolic link to '/private/tmp'.

 Therefore, I want to get '/private/tmp/abspath/b', rather than
 '/private/tmp/abspath/a', as the canonical path of 'b'.

 If the argument is a symbolic link os.path.realpath will return the
 actually target of the symbolic link. However, I want the path of the
 symbolic link rather than the path of the target.

 Hope this is clear.

 I suspect that you will have to write your own code for your own function.
 os and os.path are written in Python, so look at the code for realpath and
 modify it for your modified definition.

I find the following two files that define realpath. But I don't find
'realpath' in os.py. I looked at 'os.py'. But I don't understand how
the function realpath is introduced in the name space in os.path.
Would you please let me know?

gfind . ! -path '*backup*' -name *.py -type f -exec grep -n def
realpath {} \; -printf %p\\n\\n
193:def realpath(path):
./macpath.py

345:def realpath(filename):
./posixpath.py
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6.4: ./configure does not work

2009-10-31 Thread Ned Deily
In article 
bdb4ce25-22ea-4e83-8e73-ae53cd125...@a31g2000yqn.googlegroups.com,
 knipknap knipk...@gmail.com wrote:
 Huh, looks like the .bz2 package is broken (even though the md5 is
 fine). The .gz works fine.

Hmm, the .bz2 from the official download page
  http://www.python.org/download/releases/2.6.4/
seems to have a perfectly good README and Makefile.pre.in.  Problem with 
your version of tar or ... ?

-- 
 Ned Deily,
 n...@acm.org

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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 12:47 PM, Duncan Booth
duncan.bo...@invalid.invalid wrote:
 Peng Yu pengyu...@gmail.com wrote:

 I'm wondering if there is a way to make the following two things hold.
 Thank you1
 1. When I 'import test', I can refer to class A as 'test.A'.
 2. When I 'import test.A', I can refer to class A as 'test.A.A' and
 class B shall not be imported.

 No. Either import adds the name 'test' to the current namespace. That name
 in each case references the same thing.

 Your simplest solution would be to give the sub-modules lowercase
 filenames, then you can do:

   import test
   test.A()

 or

   import test.a
   test.a.A()

 or even

   import test.a
   test.b.B()

 It would probably be best though just to be consistent as to how you
 reference the classes: define a public interface for your package and stick
 to it.

The original problem comes from the maintenance of the package. When A
and B are large classes, it is better to put them in separate files
under the directory 'test' than put them in the file 'test.py'. The
interface 'test.A' is used by end users. However, there will be a
problem if 'import test' is used for developers, because both A and B
are imported, which cause dependence between A and B. For example,
during the modification of B (not finished), 'import A' would not
work. This is means that modifications of A and B are not independent,
which cause a lot of problem when maintaining the package.

Naming the filename different from the class is a solution, but it is
a little bit annoying.

I'm wondering how people handle this situation when they have to
separate a module into multiple modules.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Robert Kern

On 2009-10-31 15:31 PM, Peng Yu wrote:


The original problem comes from the maintenance of the package. When A
and B are large classes, it is better to put them in separate files
under the directory 'test' than put them in the file 'test.py'. The
interface 'test.A' is used by end users. However, there will be a
problem if 'import test' is used for developers, because both A and B
are imported, which cause dependence between A and B. For example,
during the modification of B (not finished), 'import A' would not
work. This is means that modifications of A and B are not independent,
which cause a lot of problem when maintaining the package.


To be frank, that development process is going to cause you a lot of problems 
well beyond these import entanglements. Developers should have their own 
workspace! They shouldn't push things into production until the system is 
working. Checking something into source control shouldn't automatically deploy 
things into production.



Naming the filename different from the class is a solution, but it is
a little bit annoying.

I'm wondering how people handle this situation when they have to
separate a module into multiple modules.


Even if we organize things along the lines of one class per module, we use 
different capitalization conventions for modules and classes. In part, this 
helps solve your problem, but it mostly saves the developer thought-cycles from 
having to figure out which you are referring to when reading the code.


Personally, I like to keep my __init__.py files empty such that I can import 
exactly what I need from the package. This allows me to import exactly the 
module that I need. In large packages with extension modules that can be 
expensive to load, this is useful. We usually augment this with an api.py that 
exposes the convenient public API of the package, the A and B classes in your 
case.


--
Robert Kern

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

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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 4:14 PM, Robert Kern robert.k...@gmail.com wrote:
 On 2009-10-31 15:31 PM, Peng Yu wrote:

 The original problem comes from the maintenance of the package. When A
 and B are large classes, it is better to put them in separate files
 under the directory 'test' than put them in the file 'test.py'. The
 interface 'test.A' is used by end users. However, there will be a
 problem if 'import test' is used for developers, because both A and B
 are imported, which cause dependence between A and B. For example,
 during the modification of B (not finished), 'import A' would not
 work. This is means that modifications of A and B are not independent,
 which cause a lot of problem when maintaining the package.

 To be frank, that development process is going to cause you a lot of
 problems well beyond these import entanglements. Developers should have
 their own workspace! They shouldn't push things into production until the
 system is working. Checking something into source control shouldn't
 automatically deploy things into production.

I don't quite agree with your opinion. But please don't take it too personaly.

Even in the developer's work space, it is possible to change multiple
classes simultaneously. So the import entanglement problem still
exists.

 Naming the filename different from the class is a solution, but it is
 a little bit annoying.

 I'm wondering how people handle this situation when they have to
 separate a module into multiple modules.

 Even if we organize things along the lines of one class per module, we use
 different capitalization conventions for modules and classes. In part, this
 helps solve your problem, but it mostly saves the developer thought-cycles
 from having to figure out which you are referring to when reading the code.

I know that multiple classes or functions are typically defined in one
file (i.e. module in python). However, I feel this make the code not
easy to read. Therefore, I insist on one class or function per file
(i.e module in python).

When one class per module is strictly enforced, there will be no need
to have different capitalization conventions for modules and classes.
Developers should be able to tell whether it is a class or a module
from the context.

In my question, module A and B exist just for the sake of
implementation. Even if I have module A and B, I don't want the user
feel the existence of module A and B. I want them feel exact like
class A and B are defined in module 'test' instead of feeling two
modules A and B are in package 'test'. I know that module names should
be in lower cases, in general. However, it is OK to have the module
name capitalized in this case since the end users don't see them.

In C++, what I am asking can be easily implemented, because the
namespace and the directory hierachy is not bind to each other.
However, the binding between the namespace and the directory hierachy
make this difficult to implement. I don't know if it is not
impossible, but I'd hope there is a way to do so.

 Personally, I like to keep my __init__.py files empty such that I can import
 exactly what I need from the package. This allows me to import exactly the
 module that I need. In large packages with extension modules that can be
 expensive to load, this is useful. We usually augment this with an api.py
 that exposes the convenient public API of the package, the A and B classes
 in your case.

I looked at python library, there are quite a few __init__.py files
are not empty. In fact, they are quite long. I agree with you that
'__init__.py' should not be long. But I'm wondering why in python
library __init__.py are quite long.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: datetime question

2009-10-31 Thread Ben Finney
Albert Hopkins mar...@letterboxes.org writes:

 No, I meant cleaning up the standard library in spite of
 incompatibilities was one of the goals of Python3 (PEP 3108).

Ah, okay. That PEP is “Standard Library Reorganization”
URL:http://www.python.org/dev/peps/pep-3108/, and is specifically
about removing or renaming modules to “clean up” the library.

I don't think it applies to this conversation, which is not about
renaming nor removing modules.

 Personally I don't see anything wrong with the modules, but that was
 my question to the person who said they should all be integrated but
 wasn't because of incompatibilities.

The changes I'm suggesting would be necessary are to the behaviour of
the ‘time’, ‘datetime’, and ‘calendar’ modules: the types they consume
and produce would be unified. They're not now, which would represent an
incompatible API change, not covered by PEP 3108 nor any Python 2→3 PEP
to my knowledge.

-- 
 \ “Alternative explanations are always welcome in science, if |
  `\   they are better and explain more. Alternative explanations that |
_o__) explain nothing are not welcome.” —Victor J. Stenger, 2001-11-05 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do you use python?

2009-10-31 Thread TerryP
For me a language is a language for the most part, doesn't matter...

Python is a language I choose for any of several reasons:
0.) It is easy to setup dependent packages on both BSD, Linux, and
Windows
1.) Most important things already have a Python binding somewhere
2.) Working in Python is fun
3.) Python scripts are more portable between OSes then compiled
executables.

I find that Python makes me very productive, and lets me worry more
about the engineering side of the art. However, I'm just as happy
using C or a portable subset of sh. Unless it means writing Swing
layouts by hand.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get the realpath of a symbolic link?

2009-10-31 Thread Wolodja Wentland
On Sat, Oct 31, 2009 at 14:48 -0500, Peng Yu wrote:
 On Sat, Oct 31, 2009 at 1:46 PM, Terry Reedy tjre...@udel.edu wrote:
  Peng Yu wrote:
[ snip ]

 I find the following two files that define realpath. But I don't find
 'realpath' in os.py. I looked at 'os.py'. But I don't understand how
 the function realpath is introduced in the name space in os.path.
 Would you please let me know?

 gfind . ! -path '*backup*' -name *.py -type f -exec grep -n def
 realpath {} \; -printf %p\\n\\n
 193:def realpath(path):
 ./macpath.py
 
 345:def realpath(filename):
 ./posixpath.py

The os module needs to support different platforms. The os.path module
is actually one of the platform specific ones (ntpath, posixpath,
...) that are imported 'as path' depending on the platform the code is
executed.

Have a look at the source code of the os module:

--- os.py - Python 2.6.3 ---
...
f 'posix' in _names:
...
import posixpath as path

elif 'nt' in _names:
...
import ntpath as path

import nt
__all__.extend(_get_exports_list(nt))
del nt

...

else:
raise ImportError, 'no os specific module found'

sys.modules['os.path'] = path

--- snip ---

If you really want to understand how a module is working then have a
look at its source code. Python is open source -- Use that privilige!

kind regards

Wolodja Wentland


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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Wolodja Wentland
On Sat, Oct 31, 2009 at 16:53 -0500, Peng Yu wrote:
 On Sat, Oct 31, 2009 at 4:14 PM, Robert Kern robert.k...@gmail.com wrote:

[ snip ]

 I know that multiple classes or functions are typically defined in one
 file (i.e. module in python). However, I feel this make the code not
 easy to read. Therefore, I insist on one class or function per file
 (i.e module in python).

Are you serious? Do you *really* put each function in its own file? How
exactly does this enhance the readability of the source code? Especially
if you compare that to a (sic!) modularisation scheme that groups
classes and functions together by task or semantic relatedness.

regards

Wolodja


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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Robert Kern

On 2009-10-31 16:53 PM, Peng Yu wrote:

On Sat, Oct 31, 2009 at 4:14 PM, Robert Kernrobert.k...@gmail.com  wrote:

On 2009-10-31 15:31 PM, Peng Yu wrote:


The original problem comes from the maintenance of the package. When A
and B are large classes, it is better to put them in separate files
under the directory 'test' than put them in the file 'test.py'. The
interface 'test.A' is used by end users. However, there will be a
problem if 'import test' is used for developers, because both A and B
are imported, which cause dependence between A and B. For example,
during the modification of B (not finished), 'import A' would not
work. This is means that modifications of A and B are not independent,
which cause a lot of problem when maintaining the package.


To be frank, that development process is going to cause you a lot of
problems well beyond these import entanglements. Developers should have
their own workspace! They shouldn't push things into production until the
system is working. Checking something into source control shouldn't
automatically deploy things into production.


I don't quite agree with your opinion. But please don't take it too personaly.

Even in the developer's work space, it is possible to change multiple
classes simultaneously. So the import entanglement problem still
exists.


But it's a problem that should have different consequences than you are 
claiming. Having users prevented from using A because developers are modifying 
their copy of B in production is a problem that needs to be solved by changing 
your development process. If you don't change your development process, you will 
run into the same problems without import entanglements.


Now as to import entanglements in the developer's workspace, it is true that 
they can cause issues from time to time, but they are much, much smaller in 
practice. I can just go in and comment out the offending import temporarily 
while I finish working on the other part until I'm ready to address both of them 
together. Then when I'm finished and things are working again, I can check my 
code into source control. It's just not a big deal.



Naming the filename different from the class is a solution, but it is
a little bit annoying.

I'm wondering how people handle this situation when they have to
separate a module into multiple modules.


Even if we organize things along the lines of one class per module, we use
different capitalization conventions for modules and classes. In part, this
helps solve your problem, but it mostly saves the developer thought-cycles
from having to figure out which you are referring to when reading the code.


I know that multiple classes or functions are typically defined in one
file (i.e. module in python). However, I feel this make the code not
easy to read. Therefore, I insist on one class or function per file
(i.e module in python).


One function per file is a little extreme. I am sympathetic to one class per 
module, but functions *should* be too short too warrant a module to themselves.



When one class per module is strictly enforced, there will be no need
to have different capitalization conventions for modules and classes.
Developers should be able to tell whether it is a class or a module
from the context.


Given enough brain-time, but you can make your code easier to read by using 
different conventions for different things. Developer brain-time is expensive! 
As much as possible, it should be spent on solving problems, not comprehension.



In my question, module A and B exist just for the sake of
implementation. Even if I have module A and B, I don't want the user
feel the existence of module A and B. I want them feel exact like
class A and B are defined in module 'test' instead of feeling two
modules A and B are in package 'test'. I know that module names should
be in lower cases, in general. However, it is OK to have the module
name capitalized in this case since the end users don't see them.

In C++, what I am asking can be easily implemented, because the
namespace and the directory hierachy is not bind to each other.
However, the binding between the namespace and the directory hierachy
make this difficult to implement. I don't know if it is not
impossible, but I'd hope there is a way to do so.


I'm not sure that C++ is a lot better. I still have to know the file hierarchy 
in order to #include the right files. Yes, the namespaces get merged when you go 
to reference things in the code, but those #includes are intimately tied to the 
file hierarchy.


In C++, you can often #include one file that #includes everything else because 
linking won't bring in the symbols you don't actually use. Oddly enough, we 
don't have that luxury because we are in a dynamic language. Python imports have 
runtime consequences because there is no compile or link step. You can't think 
of import statements as #include statements and need to use different patterns.


Of course, to really take advantage of that 

Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 5:45 PM, Wolodja Wentland
wentl...@cl.uni-heidelberg.de wrote:
 On Sat, Oct 31, 2009 at 16:53 -0500, Peng Yu wrote:
 On Sat, Oct 31, 2009 at 4:14 PM, Robert Kern robert.k...@gmail.com wrote:

 [ snip ]

 I know that multiple classes or functions are typically defined in one
 file (i.e. module in python). However, I feel this make the code not
 easy to read. Therefore, I insist on one class or function per file
 (i.e module in python).

 Are you serious? Do you *really* put each function in its own file? How
 exactly does this enhance the readability of the source code? Especially
 if you compare that to a (sic!) modularisation scheme that groups
 classes and functions together by task or semantic relatedness.

If two functions are too long to put in file, I generally put them in
two different files. And I always put a single class in a file.
Suppose that I have many functions in one file, it is not clear to see
how many functions are in the file at first glance. If I put each
function in its own file,  just by looking at the directory structure,
I can easily see how many functions there are.

One advantage is on refactoring. When each function has its own file,
I can change variable names, etc., for a give function without
worrying accidentally change variable names in other functions. When I
find a function is more appropriate to put in another namespace, I can
just move the file around.

Another advantage is on testing. I can have associated test dir for
each class or function. By this way I can easily locate the definition
and the test to track any potential problems.

You can use package rather than module to group semantic related
classes and functions.


 -BEGIN PGP SIGNATURE-
 Version: GnuPG v1.4.10 (GNU/Linux)

 iQIcBAEBCAAGBQJK7L38AAoJEIt/fTDK8U78Pv4P/icEwsmSLcuINWZHj4Shs0A/
 1/TZcuP5VwuR6gZ0WsywKzbKJqL0WDahUi4o3VkwFuM2mKskXzch3buZ5NvlwOp6
 I+NqBn9jCTb3nXiVb5wHdF6uYf84BPZe1WccRIDJLIoGRWX/V6tmMH1LtLGnBeVi
 RGOd6Mz2KGr1cgisyYJ2h4Qm5tzKNuZ1KDtzXoOG4DYzwEEZBITFOwDNXy5tihJz
 v/NcAZOa4aBfJZtKxA7Ikl+30nDV8ZZhEU7Br/rIus2JrSqMp6gAh4f+zTz9jQzL
 Sp7O3bTQiHoghej+G4YW+/eMDTiNDSKm1u8++V5svwednp/mmYBnzA8aIPKFSoN6
 vn4D0Q2XVGQAoWyY7pT9zyRKBJnn63xXD5h9T6JimEz7uMWGzTebIuxFRHsd1vkt
 TYTW1kJDH8aIsy51egBezZx8o6sntBFu3D+D3itqDW2D2sZ75sPiblgkLCWHvZMR
 RaBjCkvhVjOaiJOZ64mRmkW3RUJzY6lGvEqfQqW1bRpHxLEUKaWLy6rWa0sQTTut
 rIZ/5TdnjPec1Dx+9v6V7sW8bZtCttpb7j4k+DBAMjRpW7mocnGfuxGN/57Y/uD0
 gFOURpMz1rjEdPCiYZuUQX+joS3tl9IxnBZL7gTRl3slSWoVlGuhcqsew3nAkrGB
 Fx8iwKUAwwRULxzigHHB
 =n3s3
 -END PGP SIGNATURE-

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


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


Why 'import module' will not import module.py but the directory module?

2009-10-31 Thread Peng Yu
If I have both the directory 'module' and the file 'module.py' in a
directory in $PYTHONPATH, python will import 'module' rather than
'module.py'. I'm wondering what is the design rationale of setting
higher priorities to directories. Is there a way to reverse the
priority?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sqlite3. Substitution of names in query.

2009-10-31 Thread Lawrence D'Oliveiro
In message mailman.2376.1257005738.2807.python-l...@python.org, Carsten 
Haese wrote:

 Lawrence D'Oliveiro wrote:

 In message mailman.2357.1256964121.2807.python-l...@python.org, Dennis
 Lee Bieber wrote:
 
 This way regular string interpolation operations (or whatever Python
 3.x has replaced it with) are safe to construct the SQL, leaving only
 user supplied (or program generated) data values to be passed via the
 DB-API parameter system -- so that they are properly escaped and
 rendered safe.
 
 Mixing the two is another recipe for confusion and mistakes.
 
 Mixing the two is necessary.
 ...
 As long as you understand what you're doing, there should be no confusion.
 (And if you don't understand what you're doing, you shouldn't be doing
 it!)

But if you understand what you're doing, you don't need to mix the two.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why 'import module' will not import module.py but the directory module?

2009-10-31 Thread Robert Kern

On 2009-10-31 18:51 PM, Peng Yu wrote:

If I have both the directory 'module' and the file 'module.py' in a
directory in $PYTHONPATH, python will import 'module' rather than
'module.py'. I'm wondering what is the design rationale of setting
higher priorities to directories. Is there a way to reverse the
priority?


You mean that you have a package module/? With an __init__.py? Plain 
directories that aren't packages shouldn't be imported by Python.


No, you can't reverse the priority between packages and modules. I'm not sure 
why that would help you. The package would then be inaccessible if you did. If 
it's inaccessible, then why have it at all?


--
Robert Kern

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

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


Re: Sqlite3. Substitution of names in query.

2009-10-31 Thread Carsten Haese
Lawrence D'Oliveiro wrote:
 In message mailman.2376.1257005738.2807.python-l...@python.org, Carsten 
 Haese wrote:
 
 Lawrence D'Oliveiro wrote:

 In message mailman.2357.1256964121.2807.python-l...@python.org, Dennis
 Lee Bieber wrote:

 This way regular string interpolation operations (or whatever Python
 3.x has replaced it with) are safe to construct the SQL, leaving only
 user supplied (or program generated) data values to be passed via the
 DB-API parameter system -- so that they are properly escaped and
 rendered safe.
 Mixing the two is another recipe for confusion and mistakes.
 Mixing the two is necessary.
 ...
 As long as you understand what you're doing, there should be no confusion.
 (And if you don't understand what you're doing, you shouldn't be doing
 it!)
 
 But if you understand what you're doing, you don't need to mix the two.

Are we talking about the same thing here? I thought we're talking about
string interpolation and parameter binding, and I explained that mixing
those two is necessary if you have a query in which the movable bits
are identifiers or other syntax elements.

On what grounds are you asserting that it's not necessary to mix the
two? Please elaborate your point.

--
Carsten Haese
http://informixdb.sourceforge.net

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


Re: Why 'import module' will not import module.py but the directory module?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 7:02 PM, Robert Kern robert.k...@gmail.com wrote:
 On 2009-10-31 18:51 PM, Peng Yu wrote:

 If I have both the directory 'module' and the file 'module.py' in a
 directory in $PYTHONPATH, python will import 'module' rather than
 'module.py'. I'm wondering what is the design rationale of setting
 higher priorities to directories. Is there a way to reverse the
 priority?

 You mean that you have a package module/? With an __init__.py? Plain
 directories that aren't packages shouldn't be imported by Python.

Yes. I mean a pakcage 'module/' with an __init__.py.

 No, you can't reverse the priority between packages and modules. I'm not
 sure why that would help you. The package would then be inaccessible if you
 did. If it's inaccessible, then why have it at all?

Why the package 'module' has to be inaccessible? I can 'import
module.part1' to access the component of the package.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Wolodja Wentland
On Sat, Oct 31, 2009 at 18:29 -0500, Peng Yu wrote:
 On Sat, Oct 31, 2009 at 5:45 PM, Wolodja Wentland
 wentl...@cl.uni-heidelberg.de wrote:
  On Sat, Oct 31, 2009 at 16:53 -0500, Peng Yu wrote:

  Are you serious? Do you *really* put each function in its own file? How
  exactly does this enhance the readability of the source code? Especially
  if you compare that to a (sic!) modularisation scheme that groups
  classes and functions together by task or semantic relatedness.

 If two functions are too long to put in file, I generally put them in
 two different files. 

If it should ever happen that two functions are too long to put in a
single file you should refactor your code. It is usually a good idea of
breaking problems down into single steps (ie functions) so you never end
up with a 5000 SLOC *function*.

How do functions of this length enhance the readability of your source
code?

 And I always put a single class in a file.

Why? What do you gain by that?

 Suppose that I have many functions in one file, it is not clear to see
 how many functions are in the file at first glance.

Use a better editor/IDE for that.

[snip]

I thought about answering your post in greater detail, but i would like
to evaluate your style of work first. Is there any place where I can
have a look at some of your source code? It would be perfect if it is a
medium sized project with said unit tests, packages and
function-modules and the rest you described.

Why does not a single module in in the stdlib follow your code layout
scheme?

regards

Wolodja


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


Re: Why 'import module' will not import module.py but the directory module?

2009-10-31 Thread Robert Kern

On 2009-10-31 19:16 PM, Peng Yu wrote:

On Sat, Oct 31, 2009 at 7:02 PM, Robert Kernrobert.k...@gmail.com  wrote:

On 2009-10-31 18:51 PM, Peng Yu wrote:


If I have both the directory 'module' and the file 'module.py' in a
directory in $PYTHONPATH, python will import 'module' rather than
'module.py'. I'm wondering what is the design rationale of setting
higher priorities to directories. Is there a way to reverse the
priority?


You mean that you have a package module/? With an __init__.py? Plain
directories that aren't packages shouldn't be imported by Python.


Yes. I mean a pakcage 'module/' with an __init__.py.


No, you can't reverse the priority between packages and modules. I'm not
sure why that would help you. The package would then be inaccessible if you
did. If it's inaccessible, then why have it at all?


Why the package 'module' has to be inaccessible? I can 'import
module.part1' to access the component of the package.


No, it wouldn't. It's a moot point because Python picks the package first, but 
if it did pick modules before packages, then sys.modules['module'] would point 
to the module from module.py and not module/__init__.py . import module.part1 
first executes import module, then looks in there to determine who to resolve 
module.part1. Since sys.modules['module'] is a regular module and not the 
package, it wouldn't find module/part1.py .


--
Robert Kern

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

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


Problems in merge.py when building the Python MSI installer

2009-10-31 Thread Gabriel Genellina
I'm trying to build a Python MSI installer for Windows using the scripts  
provided at Tools\msi\


msi.py runs fine and generates a python-xxx.msi file.
Then, when I run the merge.py script, I get these error messages:

e:\prog\python\py3k\Tools\msi\msilib.py:8: DeprecationWarning: the sets  
module i

s deprecated
  import re, string, os, sets, glob, subprocess, sys, _winreg, struct
Opened Log
Opened DB
C:\Archivos de programa\Archivos comunes\Merge  
Modules\Microsoft_VC90_CRT_x86.ms

m
Opened Module C:\Archivos de programa\Archivos comunes\Merge  
Modules\Microsoft_V

C90_CRT_x86.msm
Errors:
4 _Validation _Validation
   Modkeys: _Validation Nullable
   DBKeys: _Validation Nullable
4 _Validation _Validation
   Modkeys: CustomAction Type
   DBKeys: CustomAction Type
4 _Validation _Validation
   Modkeys: File Sequence
   DBKeys: File Sequence
C:\Archivos de programa\Archivos comunes\Merge  
Modules\policy_9_0_Microsoft_VC90

_CRT_x86.msm
Opened Module C:\Archivos de programa\Archivos comunes\Merge  
Modules\policy_9_0_

Microsoft_VC90_CRT_x86.msm
Errors:
5 ModuleInstallExecuteSequence InstallExecuteSequence
   Modkeys: SxsInstallCA
   DBKeys: SxsInstallCA
5 ModuleInstallExecuteSequence InstallExecuteSequence
   Modkeys: SxsUninstallCA
   DBKeys: SxsUninstallCA
4 _Validation _Validation
   Modkeys: _Validation Nullable
   DBKeys: _Validation Nullable
4 _Validation _Validation
   Modkeys: CustomAction Type
   DBKeys: CustomAction Type
4 _Validation _Validation
   Modkeys: File Sequence
   DBKeys: File Sequence
Start of Media 2644

The final .msi file appears to work fine as long as I can tell.
Error type 4 is msmErrorTableMerge and type 5 is  
msmErrorResequenceMerge, but I don't know enough of the msi technology  
to tell if those errors are important or not.


--
Gabriel Genellina

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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Robert Kern

On 2009-10-31 19:21 PM, Wolodja Wentland wrote:

On Sat, Oct 31, 2009 at 18:29 -0500, Peng Yu wrote:



And I always put a single class in a file.


Why? What do you gain by that?


While it's never a good idea to follow the rule slavishly, it's often a good 
idea. Many classes are themselves a semantic unit of functionality enough to 
justify their own module. I've certainly seen the rule overapplied, though.


--
Robert Kern

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

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


Re: Are *.pyd's universal?

2009-10-31 Thread Steven D'Aprano
On Sat, 31 Oct 2009 23:58:33 +1300, Lawrence D'Oliveiro wrote:

 In message mailman.2365.1256979069.2807.python-l...@python.org, Albert
 Hopkins wrote:
 
 On Sat, 2009-10-31 at 21:32 +1300, Lawrence D'Oliveiro wrote:

In message
6e603d9c-2be0-449c-9c3c-bab49e09e...@13g2000prl.googlegroups.com, Carl
Banks wrote:

 Modules will sometimes find themselves on the path in Windows, so the
 fact that Windows performs a library search on the path is quite
 significant.
 
 Why is it only Windows is prone to this problem?

I question your assumption. DLL hell is merely a platform-specific name 
for a particular variety of a broader class of problems, namely 
dependency conflicts and the bootstrapping problem. It's not even 
specific to software -- bootstrapping problems are well known in many 
fields of endeavour: before you can make the tools to make things, you 
need to make the tools to make the tools...

It is far from true to say that Windows is the only system subject to 
this problem.

http://en.wikipedia.org/wiki/Dependency_hell

Python, like most (all?) programming languages, has it's own version of 
dependency hell, namely circular imports: module A depends on module B, 
which depends on module C, which depends on module A... 

Python also has a second DLL Hell, as many newbies discover: shadowing 
standard library modules. Create a module (say) random.py in the 
current directory, and then try to import the standard library random. 
This is a form of DLL Hell, because Python uses standard library modules 
dynamically, importing them at runtime.

One solution to dependency conflicts and circular dependencies is to 
avoid dependencies altogether. At the cost of disk space and memory, use 
static linking and give up the benefits of dynamic libraries. Platforms 
that encourage static linking avoid the DLL conflicts, but at the cost of 
increased memory and storage usage, and static applications need to be 
upgraded individually instead of merely upgrading the shared library.


 I think as someone pointed out earlier, in Unix-like operating systems,
 a regular library's file name starts with lib, e.g. libcrypt.so. 
 So this would not conflict with Python's crypt.so.

But it would conflict with a Python module libcrypt.so if the PYTHONPATH 
and the OS's shared library path coincided.


 I just checked my Debian installation:
 
 l...@theon:~ find /lib /usr/lib -name \*.so -a -not -name lib\*
 -print | wc -l 2950
 l...@theon:~ find /lib /usr/lib -name \*.so -print | wc -l 4708
 
 So 63% of the shareable libraries on my system have names NOT beginning
 with lib.


53% on my system. This just goes to show that even Linux developers often 
don't bother with the conventions for their platforms.



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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 7:21 PM, Wolodja Wentland
wentl...@cl.uni-heidelberg.de wrote:
 On Sat, Oct 31, 2009 at 18:29 -0500, Peng Yu wrote:
 On Sat, Oct 31, 2009 at 5:45 PM, Wolodja Wentland
 wentl...@cl.uni-heidelberg.de wrote:
  On Sat, Oct 31, 2009 at 16:53 -0500, Peng Yu wrote:

  Are you serious? Do you *really* put each function in its own file? How
  exactly does this enhance the readability of the source code? Especially
  if you compare that to a (sic!) modularisation scheme that groups
  classes and functions together by task or semantic relatedness.

 If two functions are too long to put in file, I generally put them in
 two different files.

 If it should ever happen that two functions are too long to put in a
 single file you should refactor your code. It is usually a good idea of
 breaking problems down into single steps (ie functions) so you never end
 up with a 5000 SLOC *function*.

My definition of long is more than one screen.

 How do functions of this length enhance the readability of your source
 code?

If the code is of one screen, you can easily see what it does without
having to scroll back and forth.

 And I always put a single class in a file.

 Why? What do you gain by that?

It makes me easier to look for an class. I can just search the
filename to get a class. By looking at the packages imported in the
file, I can easily see what packages a class depends. If there are
multiple classes in a file, I will not know which packages a class
actually depends on.

 Suppose that I have many functions in one file, it is not clear to see
 how many functions are in the file at first glance.

 Use a better editor/IDE for that.

What editor you use? I use vim. I'm sure there is a way to configure
vim to fold the definition of a class. But I don't use it because I
feel folding and unfolding code is not convenient.

 [snip]

 I thought about answering your post in greater detail, but i would like
 to evaluate your style of work first. Is there any place where I can
 have a look at some of your source code? It would be perfect if it is a
 medium sized project with said unit tests, packages and
 function-modules and the rest you described.

I just started writing python code. So I don't have much code to show
you now. But I have written a project of 25,000 lines of code in C++.
In this project, I always have a file for a class. My directory
structure looks like the following (toward the end). But having this
structure it is every easy to navigate to any class or function.
Please let me know if you need any more information.

src/ has all the source code and testing code. Because I usually need
to change source code and the corresponding test code at the same
time, I put them close to each other.

include/ has links to all the header files and maintains the directory
structure (include/ is generated automatically based on the content of
src/). All .cpp files are linked to lib/, where the library file is
made.

The unit test for divide is in main.cpp.
|   |-- divide.cpp
|   |-- divide.hpp
|   `-- main.cpp

When test cases become big, I separate them into multiple main.cpp
files like below.
|   |-- half
|   |   |-- Makefile
|   |   |-- cap
|   |   |   |-- Makefile
|   |   |   `-- main.cpp
|   |   |-- half.hpp
|   |   |-- is_interleaved
|   |   |   |-- Makefile
|   |   |   `-- main.cpp
|   |   |-- less_than
|   |   |   |-- Makefile
|   |   |   `-- main.cpp
|   |   `-- main
|   |   |-- Makefile
|   |   `-- main.cpp

The namespace is defined accordingly. For example, class divide.hpp is
in numerical/divide.hpp, therefore, I use numerical::divide to refer
it.

### example directory structure
.
|-- include
|   `-- numerical
|   |-- divide.hpp - ../../src/divide/divide.hpp
|   |-- interval
|   |   |-- combine_half.hpp -
../../../src/interval/combine_half/combine_half.hpp
|   |   |-- half.hpp - ../../../src/interval/half/half.hpp
|   |   |-- linear.hpp - ../../../src/interval/linear/linear.hpp
|   |   `-- rotated.hpp - ../../../src/interval/rotated/rotated.hpp
|   `-- smart_increment.hpp - ../../src/smart_increment/smart_increment.hpp
|-- lib
|   |-- Makefile
|   |-- libnumerical.so - libnumerical.so.1
|   |-- libnumerical.so.1 - libnumerical.so.1.0.0
|   `-- numerical
|   `-- divide.cpp - ../../src/divide/divide.cpp
`-- src
|-- Makefile
|-- divide
|   |-- Makefile
|   |-- divide.cpp
|   |-- divide.hpp
|   `-- main.cpp
|-- interval
|   |-- Makefile
|   |-- combine_half
|   |   |-- Makefile
|   |   |-- combine_half.hpp
|   |   `-- main.cpp
|   |-- half
|   |   |-- Makefile
|   |   |-- cap
|   |   |   |-- Makefile
|   |   |   `-- main.cpp
|   |   |-- half.hpp
|   |   |-- is_interleaved
|   |   |   |-- Makefile
|   |   |   `-- main.cpp
|   |   |-- less_than
|   |   |   |-- Makefile
|   |   |   `-- 

Re: import bug

2009-10-31 Thread Steven D'Aprano
On Sat, 31 Oct 2009 16:27:20 +, kj wrote:

1) it's a bad idea to name your own modules after modules in the stdlib
 
 Obviously, since it leads to the headaches this thread illustrates. But
 there is nothing intrisically wrong with it.  The fact that it is
 problematic in Python is a design bug, plain and simple.  There's no
 rational basis for it, 

Incorrect. Simplicity of implementation and API is a virtue, in and of 
itself. The existing module machinery is quite simple to understand, use 
and maintain. Dealing with name clashes doesn't come for free. If you 
think it does, I encourage you to write a patch implementing the 
behaviour you would prefer.

In addition, there are use-cases where the current behaviour is the 
correct behaviour. Here's one way to backport (say) functools to older 
versions of Python (untested):


# === functools.py ===

import sys

if sys.version = '2.5':
# Use the standard library version if it is available.
old_path = sys.path[:]
del sys.path[0]  # Delete the current directory.
from functools import *
sys.path[:] = old_path  # Restore the path.
else:
# Backport code you want.
pass




 and represents an unreasonable demand on module
 writers, since contrary to the tight control on reserved Python
 keywords, there does not seem to be a similar control on the names of
 stdlib modules.  What if, for example, in the future it was decided that
 my_favorite_module name would become part of the standard library?  This
 alone would cause code to break.

Not necessarily. Obviously your module my_favorite_module.py isn't 
calling the standard library version, because it didn't exist when you 
wrote it. Nor are any of your callers. Mere name clashes alone aren't 
necessarily an issue. One problem comes about when some module you import 
is modified to start using the standard library module, which conflicts 
with yours. Example:

You have a collections module, which imports the standard library stat 
module. The Python standard library can safely grow a collections module, 
but what it can't do is grow a collections module *and* modify stat to 
use that.

But in general, yes, you are correct -- there is a risk that future 
modules added to the standard library can clash with existing third party 
modules. This is one of the reasons why Python is conservative about 
adding to the std lib.

In other words, yes, module naming conflicts is the Python version of DLL 
Hell. Python doesn't distinguish between my modules and standard 
modules and third party modules -- they're all just modules, there 
aren't three different implementations for importing a module and you 
don't have to learn three different commands to import them. 

But there is a downside too: if you write import os Python has no 
possible way of knowing whether you mean the standard os.py module or 
your own os.py module.

Of course, Python does expose the import machinary to you. If avoiding 
standard library names is too much a trial for you, or if you are 
paranoid and want to future-proof your module against changes to the 
standard library (a waste of time in my opinion), you can use Python's 
import machinery to build your own system.




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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Dave Angel

Peng Yu wrote:

On Sat, Oct 31, 2009 at 5:45 PM, Wolodja Wentland
wentl...@cl.uni-heidelberg.de wrote:
  

On Sat, Oct 31, 2009 at 16:53 -0500, Peng Yu wrote:


On Sat, Oct 31, 2009 at 4:14 PM, Robert Kern robert.k...@gmail.com wrote:
  

[ snip ]



I know that multiple classes or functions are typically defined in one
file (i.e. module in python). However, I feel this make the code not
easy to read. Therefore, I insist on one class or function per file
(i.e module in python).
  

Are you serious? Do you *really* put each function in its own file? How
exactly does this enhance the readability of the source code? Especially
if you compare that to a (sic!) modularisation scheme that groups
classes and functions together by task or semantic relatedness.



snip
One advantage is on refactoring. When each function has its own file,
I can change variable names, etc., for a give function without
worrying accidentally change variable names in other functions. When I
find a function is more appropriate to put in another namespace, I can
just move the file around.

  
Variables in a function are already private.  How can the names in one 
function be affected by other functions in the same module?


DaveA

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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Steven D'Aprano
On Sat, 31 Oct 2009 18:29:35 -0500, Peng Yu wrote:

 If two functions are too long to put in file, I generally put them in
 two different files.

If two functions are too long for a single file, the functions are too 
big and need to be broken up into ten or thirty sub-functions each!

Ideally, no function should be more than ten or twenty lines (plus 
docstring), certainly no more than a page. In a language like Python that 
is so compact, monster functions that are hundreds of lines long is 
almost certainly inexcusable.


 And I always put a single class in a file.

An unnecessary and silly restriction. It is reasonable for large 
complicated classes, but not a rule for all classes, some of which might 
be as small as two lines.


 Suppose that I have many functions in one file, it is not clear to see
 how many functions are in the file at first glance.

So what? Why is it import to see how many functions are in the file?



 If I put each function in its
 own file,  just by looking at the directory structure, I can easily see
 how many functions there are.

Why do you care how many functions there are?


 One advantage is on refactoring. When each function has its own file, I
 can change variable names, etc., for a give function without worrying
 accidentally change variable names in other functions. 

Do you understand that each function has it's own local namespace? Unless 
you are foolish enough to use global variables for everything, you can 
refactor any given function without effecting other functions in the same 
file. 


 When I find a
 function is more appropriate to put in another namespace, I can just
 move the file around.

A module is a namespace.


 Another advantage is on testing. I can have associated test dir for each
 class or function. By this way I can easily locate the definition and
 the test to track any potential problems.

You can do this when functions share the same file.


 You can use package rather than module to group semantic related classes
 and functions.

Python makes available many namespaces:

Function
Module
Package

You are artificially restricting yourself to use:

Function = Module
Package

and that's supposed to be an advantage???


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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Steven D'Aprano
On Sat, 31 Oct 2009 20:03:29 -0500, Peng Yu wrote:

 If it should ever happen that two functions are too long to put in a
 single file you should refactor your code. It is usually a good idea of
 breaking problems down into single steps (ie functions) so you never
 end up with a 5000 SLOC *function*.
 
 My definition of long is more than one screen.
 
 How do functions of this length enhance the readability of your source
 code?
 
 If the code is of one screen, you can easily see what it does without
 having to scroll back and forth.

Far enough, but you exchange scrolling back and forth from function to 
function with tabbing through editor tabs from file to file. I don't see 
that this is an advantage.

You also complicate calling functions. Instead of:


def parrot():
spam()
ham()
cheeseshop()


you need:


from spam import spam
from ham import ham
from import cheeseshop

def parrot():
spam()
ham()
cheeseshop()


This is not an advantage!




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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Steven D'Aprano
On Sat, 31 Oct 2009 16:53:50 -0500, Peng Yu wrote:

 I know that multiple classes or functions are typically defined in one
 file (i.e. module in python). However, I feel this make the code not
 easy to read. Therefore, I insist on one class or function per file (i.e
 module in python).
 
 When one class per module is strictly enforced, there will be no need to
 have different capitalization conventions for modules and classes.
 Developers should be able to tell whether it is a class or a module from
 the context.

Classes and modules are first-class objects in Python, so you can't 
necessarily tell what they are from context. I frequently have code like 
this:


def verify(test_func, objects_to_process, expected_results):
for obj, result in zip(objects_to_process, expected_results):
print Testing %r... % obj, 
r = test_func(obj)
if r == result:
print verified
else:
print expected %r but got %s % (result, r)


objects_to_process could be a list of floats, strings, functions, 
modules, or anything else. Having a naming convention is still useful for 
distinguishing (say) factory functions from classes, or modules from 
classes.


 In my question, module A and B exist just for the sake of
 implementation. Even if I have module A and B, I don't want the user
 feel the existence of module A and B. I want them feel exact like class
 A and B are defined in module 'test' instead of feeling two modules A
 and B are in package 'test'. 


Inside test/__init__.py:

from A import A  # class A from file A.py
from B import B  # class B from file B.py


or better still:

from a import A  # class A from file a.py
from b import B  # class B from file b.py



 I know that module names should be in lower
 cases, in general. However, it is OK to have the module name capitalized
 in this case since the end users don't see them.

Of course they do.



 I looked at python library, there are quite a few __init__.py files are
 not empty. In fact, they are quite long. I agree with you that
 '__init__.py' should not be long. But I'm wondering why in python
 library __init__.py are quite long.

Define quite long.

In Python 2.6, I have:

[st...@sylar python2.6]$ for file in */__init__.py; do echo $file `cat 
$file | wc -l` ; done
bsddb/__init__.py 450
compiler/__init__.py 29
ctypes/__init__.py 546
curses/__init__.py 59
distutils/__init__.py 26
email/__init__.py 123
encodings/__init__.py 157
hotshot/__init__.py 78
idlelib/__init__.py 1
json/__init__.py 318
lib2to3/__init__.py 1
logging/__init__.py 1490
multiprocessing/__init__.py 271
sqlite3/__init__.py 24
test/__init__.py 1
wsgiref/__init__.py 23
xml/__init__.py 47



With the exception of logging, I don't see any of those as quite long.



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


Looking for help getting tkinter to work.

2009-10-31 Thread Shue Boks
I tried to compile Python and Tcl/Tk on Linux using the following
files:

Python-3.1.1.tar.gz
tcl8.5.7-src.tar.gz

Cannot get tkinter to work after compiling  installing Tcl/Tk.  I get
the following error after compiling Python:

Python build finished, but the necessary bits to build these modules
were not found:
_tkinter
To find the necessary bits, look in setup.py in detect_modules() for
the module's name.

Are the above files the correct versions to get tkinter to work?

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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 10:03 PM, Steven D'Aprano
st...@remove-this-cybersource.com.au wrote:
 On Sat, 31 Oct 2009 16:53:50 -0500, Peng Yu wrote:

 I know that multiple classes or functions are typically defined in one
 file (i.e. module in python). However, I feel this make the code not
 easy to read. Therefore, I insist on one class or function per file (i.e
 module in python).

 When one class per module is strictly enforced, there will be no need to
 have different capitalization conventions for modules and classes.
 Developers should be able to tell whether it is a class or a module from
 the context.

 Classes and modules are first-class objects in Python, so you can't
 necessarily tell what they are from context. I frequently have code like
 this:


 def verify(test_func, objects_to_process, expected_results):
    for obj, result in zip(objects_to_process, expected_results):
        print Testing %r... % obj,
        r = test_func(obj)
        if r == result:
            print verified
        else:
            print expected %r but got %s % (result, r)

This example doesn't invalidate my statement. When I say Developers
should be able to tell whether it is a class or a module from the
context, I mean the case of defining them or creating objects. In the
above case, you still can enforce one class/function per module.

 objects_to_process could be a list of floats, strings, functions,
 modules, or anything else. Having a naming convention is still useful for
 distinguishing (say) factory functions from classes, or modules from
 classes.


 In my question, module A and B exist just for the sake of
 implementation. Even if I have module A and B, I don't want the user
 feel the existence of module A and B. I want them feel exact like class
 A and B are defined in module 'test' instead of feeling two modules A
 and B are in package 'test'.


 Inside test/__init__.py:

 from A import A  # class A from file A.py
 from B import B  # class B from file B.py

I can not use the above approach as I mentioned its problem in my
previous message.

 or better still:

 from a import A  # class A from file a.py
 from b import B  # class B from file b.py

This artificially introduces the constraint that the file name can not
be the same as the class/function name. There is no constraint like
this if I can C++.

 I know that module names should be in lower
 cases, in general. However, it is OK to have the module name capitalized
 in this case since the end users don't see them.

 Of course they do.

This sentence is confusing. Can you spell out what you mean?

 I looked at python library, there are quite a few __init__.py files are
 not empty. In fact, they are quite long. I agree with you that
 '__init__.py' should not be long. But I'm wondering why in python
 library __init__.py are quite long.

 Define quite long.

 In Python 2.6, I have:

 [st...@sylar python2.6]$ for file in */__init__.py; do echo $file `cat
 $file | wc -l` ; done
 bsddb/__init__.py 450
 compiler/__init__.py 29
 ctypes/__init__.py 546
 curses/__init__.py 59
 distutils/__init__.py 26
 email/__init__.py 123
 encodings/__init__.py 157
 hotshot/__init__.py 78
 idlelib/__init__.py 1
 json/__init__.py 318
 lib2to3/__init__.py 1
 logging/__init__.py 1490
 multiprocessing/__init__.py 271
 sqlite3/__init__.py 24
 test/__init__.py 1
 wsgiref/__init__.py 23
 xml/__init__.py 47



 With the exception of logging, I don't see any of those as quite long.

I have defined 'long' in one of my previous message. I consider a file
long, when it does not fit in one or two screen. Basically, I want to
get a whole picture of the file after glancing of the file.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 9:42 PM, Steven D'Aprano
st...@remove-this-cybersource.com.au wrote:
 On Sat, 31 Oct 2009 20:03:29 -0500, Peng Yu wrote:

 If it should ever happen that two functions are too long to put in a
 single file you should refactor your code. It is usually a good idea of
 breaking problems down into single steps (ie functions) so you never
 end up with a 5000 SLOC *function*.

 My definition of long is more than one screen.

 How do functions of this length enhance the readability of your source
 code?

 If the code is of one screen, you can easily see what it does without
 having to scroll back and forth.

 Far enough, but you exchange scrolling back and forth from function to
 function with tabbing through editor tabs from file to file. I don't see
 that this is an advantage.

 You also complicate calling functions. Instead of:


 def parrot():
    spam()
    ham()
    cheeseshop()


 you need:


 from spam import spam
 from ham import ham
 from import cheeseshop

 def parrot():
    spam()
    ham()
    cheeseshop()


 This is not an advantage!

I would say this is the disadvantage of python, if there is no
walkaround to avoid using 'from spam import spam'. As this can be
handled in C++ with walkaround.

So far the best solution is:

Inside test/__init__.py:

from a import A  # class A from file a.py
from b import B  # class B from file b.py

However, this is not satisfactory enough because this artificially
introduces the constraint that the file name can not be the same as
the class/function name.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Robert Kern

Peng Yu wrote:


I have defined 'long' in one of my previous message. I consider a file
long, when it does not fit in one or two screen. Basically, I want to
get a whole picture of the file after glancing of the file.


I think you are going to have to get used to the fact that you have very strange 
personal preferences and that Python is designed for the vast majority of 
programmers who do not share them.


--
Robert Kern

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

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


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 9:34 PM, Steven D'Aprano
st...@remove-this-cybersource.com.au wrote:
 On Sat, 31 Oct 2009 18:29:35 -0500, Peng Yu wrote:

 If two functions are too long to put in file, I generally put them in
 two different files.

 If two functions are too long for a single file, the functions are too
 big and need to be broken up into ten or thirty sub-functions each!

 Ideally, no function should be more than ten or twenty lines (plus
 docstring), certainly no more than a page. In a language like Python that
 is so compact, monster functions that are hundreds of lines long is
 almost certainly inexcusable.


 And I always put a single class in a file.

 An unnecessary and silly restriction. It is reasonable for large
 complicated classes, but not a rule for all classes, some of which might
 be as small as two lines.


 Suppose that I have many functions in one file, it is not clear to see
 how many functions are in the file at first glance.

 So what? Why is it import to see how many functions are in the file?



 If I put each function in its
 own file,  just by looking at the directory structure, I can easily see
 how many functions there are.

 Why do you care how many functions there are?

Because I want to easily see what functions are there.

 One advantage is on refactoring. When each function has its own file, I
 can change variable names, etc., for a give function without worrying
 accidentally change variable names in other functions.

 Do you understand that each function has it's own local namespace? Unless
 you are foolish enough to use global variables for everything, you can
 refactor any given function without effecting other functions in the same
 file.


 When I find a
 function is more appropriate to put in another namespace, I can just
 move the file around.

 A module is a namespace.

This is a constrain imposed by python. C++ does not have this
constraint, so I can have each class in a file without any
complication in calling classes.

Because of this constraint in python. I have do something

from spam import spam

or

call spam.spam.

 Another advantage is on testing. I can have associated test dir for each
 class or function. By this way I can easily locate the definition and
 the test to track any potential problems.

 You can do this when functions share the same file.


 You can use package rather than module to group semantic related classes
 and functions.

 Python makes available many namespaces:

 Function
 Module
 Package

 You are artificially restricting yourself to use:

 Function = Module
 Package

 and that's supposed to be an advantage???

Since Module = file, by making Function = Module and Class = Module,
it simplifies the search of a function and a class. I also put the
testing code along with the module, which make it easier to move
module across packages (when I do refactoring, sometime I found some
module is not appropriate to put in a package anymore, so I need to
move it to a better package).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 9:18 PM, Dave Angel da...@ieee.org wrote:
 Peng Yu wrote:

 On Sat, Oct 31, 2009 at 5:45 PM, Wolodja Wentland
 wentl...@cl.uni-heidelberg.de wrote:


 On Sat, Oct 31, 2009 at 16:53 -0500, Peng Yu wrote:


 On Sat, Oct 31, 2009 at 4:14 PM, Robert Kern robert.k...@gmail.com
 wrote:


 [ snip ]



 I know that multiple classes or functions are typically defined in one
 file (i.e. module in python). However, I feel this make the code not
 easy to read. Therefore, I insist on one class or function per file
 (i.e module in python).


 Are you serious? Do you *really* put each function in its own file? How
 exactly does this enhance the readability of the source code? Especially
 if you compare that to a (sic!) modularisation scheme that groups
 classes and functions together by task or semantic relatedness.


 snip
 One advantage is on refactoring. When each function has its own file,
 I can change variable names, etc., for a give function without
 worrying accidentally change variable names in other functions. When I
 find a function is more appropriate to put in another namespace, I can
 just move the file around.



 Variables in a function are already private.  How can the names in one
 function be affected by other functions in the same module?

You misunderstood me.

If there are multiple functions or classes in a file, when I change
variables in a function/class, I have to make sure that they are not
in other functions or classes. This makes the refactoring process
tedious. If I have a single class/function in a file, I can safely
change variable names without worrying broken other classes or
functions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Peng Yu
On Sat, Oct 31, 2009 at 10:35 PM, Robert Kern robert.k...@gmail.com wrote:
 Peng Yu wrote:

 I have defined 'long' in one of my previous message. I consider a file
 long, when it does not fit in one or two screen. Basically, I want to
 get a whole picture of the file after glancing of the file.

 I think you are going to have to get used to the fact that you have very
 strange personal preferences and that Python is designed for the vast
 majority of programmers who do not share them.

So python would not be able to accommodate my preference one
class/function per file? I.e., I have to use something like 'from spam
import spam' or 'spam.spam()', or accept that the filename is not the
same as the class/function name.

Inside test/__init__.py:
from a import A  # class A from file a.py
from b import B  # class B from file b.py
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to import only one module in a package when the package __init__.py has already imports the modules?

2009-10-31 Thread Robert Kern

Peng Yu wrote:

On Sat, Oct 31, 2009 at 10:35 PM, Robert Kern robert.k...@gmail.com wrote:

Peng Yu wrote:


I have defined 'long' in one of my previous message. I consider a file
long, when it does not fit in one or two screen. Basically, I want to
get a whole picture of the file after glancing of the file.

I think you are going to have to get used to the fact that you have very
strange personal preferences and that Python is designed for the vast
majority of programmers who do not share them.


So python would not be able to accommodate my preference one
class/function per file? I.e., I have to use something like 'from spam
import spam' or 'spam.spam()', or accept that the filename is not the
same as the class/function name.

Inside test/__init__.py:
from a import A  # class A from file a.py
from b import B  # class B from file b.py


If you are going to expose symbols in your __init__.py, they should not have the 
same name as any of the modules in the package.


--
Robert Kern

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

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


  1   2   >