Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python

2009-01-08 Thread Kay Schluehr
O.K. Mark. Since you seem to accept the basic requirement to build an
*external* DSL I can provide some help. I'm the author of EasyExtend
( EE ) which is a system to build external DSLs for Python.

http://www.fiber-space.de/EasyExtend/doc/EE.html

EE is very much work in progress and in the last year I was more
engaged with increasing power than enhance accessibility for
beginners. So be warned.

A DSL in EE is called a *langlet*. Download the EE package and import
it in a Python shell. A langlet can then be built this way:

>>> import EasyExtend
>>> EasyExtend.new_langlet("my_langlet", prompt = "myl> ", source_ext = ".dsl")

This creates a bunch of files in a directory

/EasyExtend/langlets/my_langlet

Among them is run_my_langet.py and langlet.py. You can cd to the
directory and apply

   $python run_my_langlet.py

which opens a console with prompt 'myl>'. Each langlet is immediatly
interactive. A user can also run a langlet specific module like

   $python run_my_langlet.py mod.dsl

with the suffix .dsl defined in the langlet builder function. Each
module xxx.dsl can be imported from other modules of the my_langlet
langlet. EE provides a generic import hook for user defined suffixes.

In order to do anything meaningful one has to implement langlet
transformations in the langlet.py module. The main transformations are
defined in a class called LangletTransformer. It defines a set of
visitor methods that are marked by a decorator called @trans. Each
@trans method is named like a terminal/non-terminal in a grammar file
and responds to a terminal or non-terminal node of the parse tree
which is traversed. The structure of the parse tree is the same as
those you'd get from Pythons builtin parser. It is entirely determined
by 4 files:

- Grammar which is precisely the Python grammar found in the Python
source distribution.
- Grammar.ext which defines new non-terminals and overwrites old
ones.
- Token which defines Pythons token.
- Token.ext which is the analog of Grammar.ext for token definitions.

The Grammar.ext file is in the directory my_langlet/parsedef. There is
also an analog lexdef directory for Token.ext.

A possible Grammar.ext extension of the Python grammar that overwrites
two non-terminals of looks like this:

Grammar.ext
---

trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME | NAME |
NUMBER | STRING

atom: ('(' [yield_expr|testlist_gexp] ')' |
   '[' [listmaker] ']' |
   '{' [dictmaker] '}' |
   '`' testlist1 '`' |
   NAME | NUMBER | STRING)

---

Once this has been defined you can start a new my_langlet session and
type

myl> navigate_to 'www.openstreetmap.org' website
Traceback (most recent call last):
  File "C:\lang\Python25\lib\site-packages\EasyExtend\eeconsole.py",
line 270, in compile_cst
_code = compile(src,"","single", COMPILER_FLAGS)
  File "", line 1
navigate_to 'www.openstreetmap.org' website
  ^
SyntaxError: invalid syntax
myl>

It will raise a SyntaxError but notice that this error stems from the
*compiler*, not the parser. The parser perfectly accepts the modified
non-terminals and produces a parse tree. This parse tree has to be
transformed into a valid Python parse tree that can be accepted by
Pythons bytecode compiler.

I'm not going into detail here but recommend to read the tutorial

http://www.fiber-space.de/EasyExtend/doc/tutorial/EETutorial.html

that walks through a complete example that defines a few terminals,
non-terminals and the transformations accordingly. It also shows how
to use command line options to display parse tree properties, unparse
parse trees back into source code ( you can eliminate DSL code from
the code base entirely and replace it by equivalent Python code ), do
some validation on transformed parse trees etc.


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


Python Imaging Library and textmate

2009-01-08 Thread bilgin arslan
Hello,
I am a beginner in python and I am trying to create image files that contain
lines from a text file.
I am trying to do this with PIL, which seems like a suitable tool. I have a
copy of TextMate(1.5.8) and I run Macosx 10.5.6

The problem is, even though I installed PIL correctly (I get no errors from
"import Image" in IDLE or in terminal), I can't get TextMate to import the
module.
I keep getting "ImportError: No module named Image" error.
I tried reloading TextMate's bundles but that did not work either.

I used this set of instructions to install PIL
http://www.p16blog.com/p16/2008/05/appengine-installing-pil-on-os-x-1053.html

I think there is something wrong with symbolic links, although I am also a
novice to UNIX based systems so I do not know for sure what to do about it.
There was a similar thread in this list with textmate but I can't understand
how that issue was solved in the end.
Thanks
Ali
--
http://mail.python.org/mailman/listinfo/python-list


Re: threading in PyQt vs threading in standard library

2009-01-08 Thread Steven Woody
On Fri, Jan 9, 2009 at 3:32 PM, Phil Thompson
 wrote:
> On Fri, 9 Jan 2009 15:15:28 +0800, "Steven Woody" 
> wrote:
>> Hi,
>>
>> I am considering using PyQt for GUI programs, and I notices that both
>> of them include threading supports, so which one should I pick up?
>> Similar also applies 'socket'.
>
> I'd recommend using the PyQt versions of both.
>
> A significant advantage of PyQt's sockets is that they are integrated with
> the event loop. This means you don't need threads to handle networking.
>

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


threading in PyQt vs threading in standard library

2009-01-08 Thread Steven Woody
Hi,

I am considering using PyQt for GUI programs, and I notices that both
of them include threading supports, so which one should I pick up?
Similar also applies 'socket'.

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


Re: Best practice in organize classes into modules

2009-01-08 Thread Steven Woody
On Fri, Jan 9, 2009 at 2:52 PM, Steve Holden  wrote:
> Chris Rebert wrote:
>> On Thu, Jan 8, 2009 at 9:09 PM, Steven Woody  wrote:
>>> On Fri, Jan 9, 2009 at 1:02 PM, James Mills
>>>  wrote:
 On Fri, Jan 9, 2009 at 2:57 PM, Steven Woody  wrote:
> In C++/Java, people usually put one class into one file.  What's the
> suggestion on this topic in Python?  I so much interesting this
> especially when exception classes also involved.
 Normally i group related functionality into the one module.
>>> Will that lead to too large source file size?  Is there a
>>> recommendation on max lines of a python source?  Thanks.
>>
>> I don't think there's really a hard-and-fast rule (just like in Java &
>> C++!). When the program starts to feel unwieldly, then start splitting
>> it into multiple modules. Python files can generally contain several
>> classes and functions and still be quite manageable.
>>
> The OP can take a look at the standard library to get some impression of
> what's been considered acceptable over the years. Just remember that
> some of the code is a little antiquated, as working code is not
> rewritten just for the fun of fixing the bugs this would inject.
>
> regards
>  Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Thanks for all your inputs!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best practice in organize classes into modules

2009-01-08 Thread Steve Holden
Chris Rebert wrote:
> On Thu, Jan 8, 2009 at 9:09 PM, Steven Woody  wrote:
>> On Fri, Jan 9, 2009 at 1:02 PM, James Mills
>>  wrote:
>>> On Fri, Jan 9, 2009 at 2:57 PM, Steven Woody  wrote:
 In C++/Java, people usually put one class into one file.  What's the
 suggestion on this topic in Python?  I so much interesting this
 especially when exception classes also involved.
>>> Normally i group related functionality into the one module.
>> Will that lead to too large source file size?  Is there a
>> recommendation on max lines of a python source?  Thanks.
> 
> I don't think there's really a hard-and-fast rule (just like in Java &
> C++!). When the program starts to feel unwieldly, then start splitting
> it into multiple modules. Python files can generally contain several
> classes and functions and still be quite manageable.
> 
The OP can take a look at the standard library to get some impression of
what's been considered acceptable over the years. Just remember that
some of the code is a little antiquated, as working code is not
rewritten just for the fun of fixing the bugs this would inject.

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

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


Re: Implementing file reading in C/Python

2009-01-08 Thread Steve Holden
MRAB wrote:
> Johannes Bauer wrote:
>> Hello group,
[and about 200 other lines there was no need to quote]
[...]
> Have a look at psyco: http://psyco.sourceforge.net/

Have a little consideration for others when making a short reply to a
long post, please. Trim what isn't necessary. Thanks.

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

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


Re: Implementing file reading in C/Python

2009-01-08 Thread James Mills
On Fri, Jan 9, 2009 at 2:29 PM, James Mills
 wrote:
> I shall attempt to optimize this :)
> I have  a funny feeling you might be caught up with
> some features of Python - one notable one being that
> some things in Python are immutable.
>
> psyco might help here though ...

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

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

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


Re: why cannot assign to function call

2009-01-08 Thread rurpy
Joe Strout wrote:
> ru...@yahoo.com wrote:
>
>> "the same as anyone else's" only if [Python's] "idea
>> of assignment" does not include producing the same
>> results.
>>
>>   a = array (1,2,3)
>>   b = a
>>   a[1] = 4
>>   print b
>>
>> C, C++, VBA, Fortran, Perl:  1, 2, 3
>> Python:  1, 4, 3
>
> You are mistaken

I don't think so.
See http://groups.google.com/group/comp.lang.python/msg/f99d5a0d8f869b96
The code I quoted there was tested.
In the C/C++ case, array-to-pointer coercion confuses
the issue so I embedded the array in a struct which
is more like an "object".  The assignment semantics are
copy-like producing the results I quoted.  (Keep in mind
my point was not to show the behavior of arrays, but to
show that several common languages *do not* use Python's
"*all* names are references" model -- though of course
this does not preclude their having some Python-like
assignments since they all have some way of doing
references.)  It seems plausible to me that experience
with copy-like assignments semantics in other languages
accounts for the frequent misunderstanding of Python
assignments that is seen on this list.

> (except perhaps in the Fortran case, which is an
> oddball by modern standards, and I don't know Perl well enough to judge).

Whether or not Perl is oddball or modern is irrelevant;
it is still widely used and it is reasonable to assume
that there are a significant number of people coming
to Python with a lot of previous experience with Perl.
Even Fortran is still used in scientific computing circles
(or so I'm told) although I can't say I have seen any
c.l.p. postings from people claiming Python doesn't work
like fortran. :-)

> C/C++ code:
>
>   int* a = malloc(3);
>   a[0] = 1;
>   a[1] = 2;
>   a[2] = 3;
>   int* b = a;
>   a[1] = 4;
>   print_array(b)
>   ---> Result: 1, 4, 3
>
> REALbasic code:
>
>   Dim a() As Integer = Array(1,2,3)
>   Dim b() As Integer = a
>   a(1) = 4
>   PrintArray b
>   --> Result: 1, 4, 3
>
> VB.NET code would be very similar in syntax, and identical in behavior,
> to the REALbasic code.

Don't know about RealBasic or VB.Net, my experience
and quoted results were from MS Visual Basic for Apps
which is (I think) based on VB6.

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


Re: drive a desktop app from python?

2009-01-08 Thread Srinivasa NL
You can use subprocess module to start an application and also read it's
output. You can kill it yourself (when you know when to do it)
Srinivas.


On Fri, Jan 9, 2009 at 2:36 AM, Tim Arnold  wrote:

> Hi, I don't even know what to google for on this one. I need to drive a
> commercial desktop app (on windows xp) since the app doesn't have a batch
> interface.  It's intended to analyze one file at a time and display a
> report.
>
> I can get the thing to write out the report an html browser, but I have
> thousands of files I need it to analyze every night.
>
> Is there any lib or recipe(s) for doing something like this via python?
>
> thanks,
> --Tim Arnold
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: "python -3" not working as expected

2009-01-08 Thread John Machin
On Jan 9, 1:56 pm, Benjamin  wrote:
> On Jan 8, 4:21 pm, Thorsten Kampe  wrote:
>
> > * Terry Reedy (Thu, 08 Jan 2009 17:04:04 -0500)
> > > Since you are, I believe, at least the second person to report being bit
> > > by this confusion, please open an issue at bugs.python.org and suggest a
> > > couple of revised sentences that you think are more informative.
>
> > Will do tomorrow. The revised sentence could be in the line of "warn
> > about Python 3.x incompatibilities that cannot trivially be fixed by
> > 2to3.py".
>
> Actually, don't bother now; I've fixed it up in the trunk.

Would you mind giving a pointer to where or what your fix is? The
reason for asking is that Thorsten's suggestion is ambiguous: warn
about some? all? 3.x problems that can't be trivially fixed by 2to3?
Can't be "all"; there are in fact a number of problems that can't be
trivially fixed by 2to3 and can't be detected by running 2.6 with the
-3 option.

These include
(a) problems that cause a reasonably informative exception in 3.x
right at the point where the problem exists
(b) problems where the behaviour has changed but no exception is
raised, and your code lurches off down the wrong path, and you need to
work backwards from subsequent exception(s) and/or failing test case
(s) to pin-point the problem.

I'll use string constants to provide an example of each type. When
faced with "abcd", 2to3 has no way of telling whether that should be
str ("abcd") or bytes (b"abcd"). In the vast majority of cases, to
guess it should be str is correct, so there is no change to the source
file, and a warning would  almostly always be noise.

Example of problem (a): chunks is a list of slices of bytes read from
a binary file.
In 2.x you write
glued = ''.join(chunks)
In 3.0 you get this:
>>> chunks = [b'x', b'y']
>>> ''.join(chunks)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: sequence item 0: expected str instance, bytes found

Example of problem (b): some_bytes has been read from a file that was
opened in 'rb' mode and contains the 4 ASCII bytes 'abcd'
# 2.x simulation
>> some_bytes == "abcd"
True
# 3.0 simulation
>>> type(some_bytes)

>>> type("abcd")

>>> some_bytes == "abcd"
False # because the types are not comparable for equality.

Another type (b) example is the (majority-guessed) 2to3 change from [c]
StringIO.StringIO to io.StringIO ... if you really should feed some
library an io.BytesIO instance instead, it can travel quite a distance
before blowing up.

Perhaps some of this info could be put into
http://docs.python.org/dev/py3k/whatsnew/3.0.html#porting-to-python-3-0
... or maybe a separate HOWTO or wiki chapter could be set up for
porting to 3.x, including topics like:
(1) maintaining one set of source files (when you are maintaining a
package that must run on e.g. 2.1 through 3.x)
(2) the possibility of a 3to2 kit for those who have the 2.1 to 3.x
support-range issue but would like to have the one set of source
looking like 3.x code instead of the ugliness of version-conditional
stuff like
BYTES_NULL = bytes(0) # 3.x
or
BYTES_NULL = '' # 2.x
and (3) [getting in early] what about a %to{} kit (or a {}to% kit!)?

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


Re: Best practice in organize classes into modules

2009-01-08 Thread Chris Rebert
On Thu, Jan 8, 2009 at 9:09 PM, Steven Woody  wrote:
> On Fri, Jan 9, 2009 at 1:02 PM, James Mills
>  wrote:
>> On Fri, Jan 9, 2009 at 2:57 PM, Steven Woody  wrote:
>>> In C++/Java, people usually put one class into one file.  What's the
>>> suggestion on this topic in Python?  I so much interesting this
>>> especially when exception classes also involved.
>>
>> Normally i group related functionality into the one module.
>
> Will that lead to too large source file size?  Is there a
> recommendation on max lines of a python source?  Thanks.

I don't think there's really a hard-and-fast rule (just like in Java &
C++!). When the program starts to feel unwieldly, then start splitting
it into multiple modules. Python files can generally contain several
classes and functions and still be quite manageable.

Cheers,
Chris

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


drive a desktop app from python?

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

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

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

thanks,
--Tim Arnold


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


Re: Printed Documentation

2009-01-08 Thread Tim Arnold
"floob"  wrote in message 
news:0af87074-6d9c-41a8-98ec-501f6f37b...@s1g2000prg.googlegroups.com...
>I have been searching for a way to print the official Python
> documentation into some kind of book (for my own uses).  I don't
> really care if it's printed on newspaper and bound with elmer's
> glue ... any way I can get relatively recent _official documentation_
> in print form will do.
>
> I'm on the go a lot, and can't read for long periods of time on LCD
> screens anyhow (so having a laptop is not my solution).  Until eBook
> readers grow up a bit, I'm stuck trying to print the documentation
> that I REALLY need to read and absorb.
>
> Lulu.com is an option, but it would cost something around $100 US
> before shipping to get everything printed.  Also, I would have to
> split up some larger documents into Volumes, which I'd rather not have
> to do.
>
> Has anyone tried this before?  Is the documentation already available
> in print?
>
> Thanks,
>
> drfloob

just a datapoint, but I used lulu.com to print the latex sources (525 pages) 
hardbound for a cost of $25 US.
--Tim Arnold


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


Re: Best practice in organize classes into modules

2009-01-08 Thread Steven Woody
On Fri, Jan 9, 2009 at 1:02 PM, James Mills
 wrote:
> On Fri, Jan 9, 2009 at 2:57 PM, Steven Woody  wrote:
>> In C++/Java, people usually put one class into one file.  What's the
>> suggestion on this topic in Python?  I so much interesting this
>> especially when exception classes also involved.
>
> Normally i group related functionality into the one module.

Will that lead to too large source file size?  Is there a
recommendation on max lines of a python source?  Thanks.

> I do try to keep my std dev. as low as I can though
> if that makes any sense.
>
> cheers
> James
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing and SIGINT

2009-01-08 Thread akineko
It is a bit awkward to respond to my posting but I have some updates.

I created two simple test programs, one uses threading and another
uses multiprocessing.

What I found was:
(1) test program with threading
Only main thread receives SIGINT (generated thread won't receive
SIGINT)
(2) test program with multiprocessing
Both processes receives SIGINT.
OS apparently distributes the SIGINT event to processes associated
with the terminal.
(3) signal handler
I realized that I could assign a signal handler specific to a process
by placing it to a worker method.

def worker():
   # this process ignores SIGINT
   signal.signal(signal.SIGINT, signal.SIG_IGN)
   ... the rest ...

def main():
   process = multiprocessing.Process(target=worker)
   process.start()

(4) terminating the spawned process
I needed to send a shutdown message to the process via a communication
between two processes.
You can use Process.terminate() to brutally kill the process but that
is a last resort.

Now my program can shutdown elegantly when I type Ctrl-C.
I see a big potential in multiprocessing as more PCs now have multi-
Core CPU.

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


Re: Best practice in organize classes into modules

2009-01-08 Thread James Mills
On Fri, Jan 9, 2009 at 2:57 PM, Steven Woody  wrote:
> In C++/Java, people usually put one class into one file.  What's the
> suggestion on this topic in Python?  I so much interesting this
> especially when exception classes also involved.

Normally i group related functionality into the one module.
I do try to keep my std dev. as low as I can though
if that makes any sense.

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


Best practice in organize classes into modules

2009-01-08 Thread Steven Woody
Hi,

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

Thanks.

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


Re: Making a decorator a staticmethod

2009-01-08 Thread Carl Banks
On Jan 8, 1:57 pm, Jonathan Gardner 
wrote:
> (Aside: I really can't think of any reason to use staticmethods in
> Python other than to organize functions into namespaces, and even
> then, that's what modules are for, right?)

In a certain widget toolkit (which I won't mention by name but it
begins with G, ends with K, and the middle rhymes with "be") sometimes
when subclassing a widget and setting up methods of the class to be
event handlers, you want to use staticmethods, because the toolkit
passes the widget as the first argument.  Since this is a subclass of
the widget, the first argument ends up being the object itself, so we
end up with odd-looking definitions like this:

@staticmethod
def on_mouse_button_down(self,event):

If it wasn't for staticmethods, you'd have to define it like this:

def on_mouse_button_down(self,widget,event):

widget and self would always be the same object, and that's just a
waste.



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


Re: figuring week of the day....

2009-01-08 Thread Tim Chase

tekion wrote:

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


sounds like you want the standard library's "calendar" module, 
particularly the monthcalendar() which gets you pretty close.


For a lazy version just using the stdlib with minimal fuss:

  >>> import calendar as c
  >>> week = lambda y,m,d: [w for w in c.monthcalendar(y, m) if 
d in w][0]

  >>> week(2009, 1, 8)
  [5, 6, 7, 8, 9, 10, 11]


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


-tkc




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


Re: Implementing file reading in C/Python

2009-01-08 Thread James Mills
On Fri, Jan 9, 2009 at 3:13 PM, Johannes Bauer  wrote:
> Uhh, yes, you're right there... I must admit that I was too lazy to
> include all the stat headers and to a proper st_size check in the C
> version (just a quick hack), so it's practically hardcoded.
>
> With files of exactly 2GB in size the results should be the same (more
> or less, +- 1 line doesn't matter really), because 2 GB / 2048 (the
> buffer) = 1 Million.
>
> Sorry I didn't mention that, it was really kind of sloppy,
> quick-and-dirty C writing on my part. But you're right, the Python
> implementation does what is actually supposed to happen.

I shall attempt to optimize this :)
I have  a funny feeling you might be caught up with
some features of Python - one notable one being that
some things in Python are immutable.

psyco might help here though ...

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


Re: why cannot assign to function call

2009-01-08 Thread Joe Strout

ru...@yahoo.com wrote:


"the same as anyone else's" only if [Python's] "idea
of assignment" does not include producing the same
results.

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

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


You are mistaken (except perhaps in the Fortran case, which is an 
oddball by modern standards, and I don't know Perl well enough to judge).


C/C++ code:

 int* a = malloc(3);
 a[0] = 1;
 a[1] = 2;
 a[2] = 3;
 int* b = a;
 a[1] = 4;
 print_array(b)
 ---> Result: 1, 4, 3

REALbasic code:

 Dim a() As Integer = Array(1,2,3)
 Dim b() As Integer = a
 a(1) = 4
 PrintArray b
 --> Result: 1, 4, 3

VB.NET code would be very similar in syntax, and identical in behavior, 
to the REALbasic code.  Java would also have the same semantics, though 
my Java is too rusty to get the syntax right without actually trying it.


If you can find a language where an array variable is NOT a reference 
(and thus does not behave exactly as in Python, Java, REALbasic, C++, 
etc.), then that language is either a dinosaur or some weird academic 
oddity.


Best,
- Joe


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


Re: Implementing file reading in C/Python

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

> I have tested this against a randomly generated
> file from /dev/urandom (10M). Yes the Python
> one is much slower, but I believe it's bebcause
> the Python implementation is _correct_ where
> teh C one is _wrong_ :)
> 
> The resulting test.bin.pgm from python is exactly
> 3.5M (from 10M). The resulting test.bin.pgm from
> the C version is 16K.
> 
> Something is not quite right here :)

Uhh, yes, you're right there... I must admit that I was too lazy to
include all the stat headers and to a proper st_size check in the C
version (just a quick hack), so it's practically hardcoded.

With files of exactly 2GB in size the results should be the same (more
or less, +- 1 line doesn't matter really), because 2 GB / 2048 (the
buffer) = 1 Million.

Sorry I didn't mention that, it was really kind of sloppy,
quick-and-dirty C writing on my part. But you're right, the Python
implementation does what is actually supposed to happen.

Kind regards,
Johannes

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


Re: Implementing file reading in C/Python

2009-01-08 Thread James Mills
On Fri, Jan 9, 2009 at 1:04 PM, Johannes Bauer  wrote:
> Hello group,

Hello.

(...)

> Which takes about 40 seconds. I want the niceness of Python but a little
> more speed than I'm getting (I'd settle for factor 2 or 3 slower, but
> factor 30 is just too much).
>
> Can anyone point out how to solve this efficiently in Python?

Johannes, your 2 programs, 1 in Python and the
other in C do _not_ produce the same result.

I have tested this against a randomly generated
file from /dev/urandom (10M). Yes the Python
one is much slower, but I believe it's bebcause
the Python implementation is _correct_ where
teh C one is _wrong_ :)

The resulting test.bin.pgm from python is exactly
3.5M (from 10M). The resulting test.bin.pgm from
the C version is 16K.

Something is not quite right here :)

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


Re: Implementing file reading in C/Python

2009-01-08 Thread MRAB

Johannes Bauer wrote:

Hello group,

I've come from C/C++ and am now trying to code some Python because I
absolutely love the language. However I still have trouble getting
Python code to run efficiently. Right now I have a easy task: Get a
file, split it up into a million chunks, count the most prominent
character in each chunk and output that value into a file - in other
words: Say we have a 2 GB file, we evaluate what character is most
prominent in filepos [0, 2048[ - say it's a "A", then put a 65 in there
(ord("A")).

I've first tried Python. Please don't beat me, it's slow as hell and
probably a horrible solution:

#!/usr/bin/python
import sys
import os

f = open(sys.argv[1], "r")
filesize = os.stat(sys.argv[1])[6]

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

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

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

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

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

most = maxchr

posx = havepixels % width
posy = havepixels / width

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

picture[(posx, posy)] = most

pic = open(sys.argv[1] + ".pgm", "w")
pic.write("P2\n")
pic.write("# CREATOR: Crappyass Python Script\n")
pic.write("%d %d\n" % (width, height))
pic.write("255\n")
for y in range(height):
for x in range(width):
pos = (x, y)
most = picture.get(pos, -1)
pic.write("%d\n" % (most))

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

#include 
#include 
#include 
#include 

#define BLOCKSIZE 2048

int main(int argc, char **argv) {
unsigned int count[256];
int width, height;
FILE *f;
FILE *in;
width = 1024;
height = 1024;
char temp[2048];

if (argc != 2) { fprintf(stderr, "Argument?\n"); exit(2); }

in = fopen(argv[1], "r");
if (!in) { perror("fopen"); exit(1); }

snprintf(temp, 255, "%s.pgm", argv[1]);
f = fopen(temp, "w");
if (!f) { perror("fopen"); exit(1); }

fprintf(f, "P2\n");
fprintf(f, "# CREATOR: C\n");
fprintf(f, "%d %d\n", width, height);
fprintf(f, "255\n");

width = 1024;
height = 1024;
while (fread(temp, 1, sizeof(temp), in) == sizeof(temp)) {
int i;
memset(count, 0, sizeof(count));
for (i = 0; i < sizeof(temp); i++) {
count[(int)temp[i]]++;
}

int greatest;
int maxcount;

greatest = 0;
maxcount = count[0];
for (i = 1; i < 256; i++) {
if (count[i] > maxcount) {
maxcount = count[i];
greatest = i;
}
}

fprintf(f, "%d\n", greatest);
}

fclose(f);
fclose(in);
return 0;
}

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

Can anyone point out how to solve this efficiently in Python?


Have a look at psyco: http://psyco.sourceforge.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: "python -3" not working as expected

2009-01-08 Thread Benjamin
On Jan 8, 4:21 pm, Thorsten Kampe  wrote:
> * Terry Reedy (Thu, 08 Jan 2009 17:04:04 -0500)
> > Since you are, I believe, at least the second person to report being bit
> > by this confusion, please open an issue at bugs.python.org and suggest a
> > couple of revised sentences that you think are more informative.
>
> Will do tomorrow. The revised sentence could be in the line of "warn
> about Python 3.x incompatibilities that cannot trivially be fixed by
> 2to3.py".

Actually, don't bother now; I've fixed it up in the trunk.

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


Re: BadZipfile "file is not a zip file"

2009-01-08 Thread webcomm
On Jan 8, 8:54 pm, MRAB  wrote:
> Have you tried gzip instead?

There's no option to download the data in a gzipped format.  The files
are .zip archives.

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


Re: Tree views - Best design practices

2009-01-08 Thread Diez B. Roggisch
Filip Gruszczyński wrote:

> class Element(object):
>>operations = "Element operations"
>>
>>
> class Group(object):
>>operations = "Group operations"
>>
>>
> e = Element()
> g = Group()
>
> e.operations
>> 'Element operations'
> g.operations
>> 'Group operations'
> 
> But this is the same as asking for a class, except for having to write
> a method giving some form of a class name and then basing on this
> classname display operations. I know this solution, but this is what I
> would like to evade.
> 

It is *not* the same - the code that knows about the operations is not in
the treeview-logic anymore, but local to the class that can actually deal
with the operations.

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


Re: How to Delete a Cookie?

2009-01-08 Thread Jose C
> To kill the cookie, simply set a cookie with the same name (and path)

Actually you may not even need to specify the path.  Just the name and
expires param should do the trick.  Haven't played with cookies in a
while so try with and without the path.
--
http://mail.python.org/mailman/listinfo/python-list


Implementing file reading in C/Python

2009-01-08 Thread Johannes Bauer
Hello group,

I've come from C/C++ and am now trying to code some Python because I
absolutely love the language. However I still have trouble getting
Python code to run efficiently. Right now I have a easy task: Get a
file, split it up into a million chunks, count the most prominent
character in each chunk and output that value into a file - in other
words: Say we have a 2 GB file, we evaluate what character is most
prominent in filepos [0, 2048[ - say it's a "A", then put a 65 in there
(ord("A")).

I've first tried Python. Please don't beat me, it's slow as hell and
probably a horrible solution:

#!/usr/bin/python
import sys
import os

f = open(sys.argv[1], "r")
filesize = os.stat(sys.argv[1])[6]

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

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

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

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

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

most = maxchr

posx = havepixels % width
posy = havepixels / width

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

picture[(posx, posy)] = most

pic = open(sys.argv[1] + ".pgm", "w")
pic.write("P2\n")
pic.write("# CREATOR: Crappyass Python Script\n")
pic.write("%d %d\n" % (width, height))
pic.write("255\n")
for y in range(height):
for x in range(width):
pos = (x, y)
most = picture.get(pos, -1)
pic.write("%d\n" % (most))

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

#include 
#include 
#include 
#include 

#define BLOCKSIZE 2048

int main(int argc, char **argv) {
unsigned int count[256];
int width, height;
FILE *f;
FILE *in;
width = 1024;
height = 1024;
char temp[2048];

if (argc != 2) { fprintf(stderr, "Argument?\n"); exit(2); }

in = fopen(argv[1], "r");
if (!in) { perror("fopen"); exit(1); }

snprintf(temp, 255, "%s.pgm", argv[1]);
f = fopen(temp, "w");
if (!f) { perror("fopen"); exit(1); }

fprintf(f, "P2\n");
fprintf(f, "# CREATOR: C\n");
fprintf(f, "%d %d\n", width, height);
fprintf(f, "255\n");

width = 1024;
height = 1024;
while (fread(temp, 1, sizeof(temp), in) == sizeof(temp)) {
int i;
memset(count, 0, sizeof(count));
for (i = 0; i < sizeof(temp); i++) {
count[(int)temp[i]]++;
}

int greatest;
int maxcount;

greatest = 0;
maxcount = count[0];
for (i = 1; i < 256; i++) {
if (count[i] > maxcount) {
maxcount = count[i];
greatest = i;
}
}

fprintf(f, "%d\n", greatest);
}

fclose(f);
fclose(in);
return 0;
}

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

Can anyone point out how to solve this efficiently in Python?

Kind regards,
Johannes

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


Re: BadZipfile "file is not a zip file"

2009-01-08 Thread MRAB

webcomm wrote:

On Jan 8, 8:02 pm, MRAB  wrote:

You're just creating a file called "data.zip". That doesn't make it
a zip file. A zip file has a specific format. If the file doesn't
have that format then the zipfile module will complain.


Hmm.  When I open it in Windows or with 7-Zip, it contains a text
file that has the data I would expect it to have.  I guess that alone
doesn't necessarily prove it's a zip file?

datum is something I'm downloading via a web service.  The providers 
of the service say it's a zip file, and have provided a code sample

in C# (which I know nothing about) that shows how to deal with it.
In the code sample, the file is base64 decoded and then unzipped.
I'm trying to write something in Python to decode and unzip the file.

I checked the file for comments and it has none.  At least, when I 
view the properties in Windows, there are no comments.



Ah, OK. You didn't explicitly say in your original posting that the
decoded data was definitely zipfile data. There was a thread a month ago
about gzip Unix commands which could also handle non-gzipped files and I
was wondering whether this problem was something like that. Have you
tried gzip instead?
--
http://mail.python.org/mailman/listinfo/python-list


Re: BadZipfile "file is not a zip file"

2009-01-08 Thread webcomm
On Jan 8, 8:39 pm, "James Mills"  wrote:
> Send us a sample of this file in question...

It contains data that I can't share publicly.  I could ask the
providers of the service if they have a dummy file I could use that
doesn't contain any real data, but I don't know how responsive they'll
be.  It's an event registration service called RegOnline.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6 fails on compiling > Bug report

2009-01-08 Thread googler . 1 . webmaster
Hi searched for "Library/Frameworks/" in the config file and edited it
to "@executable_path".

Well my configure and make command worked very fine. Just 'make
install' aborted after this error message.


test -d "/Applications/Python 2.6" || mkdir -p "/Applications/Python
2.6"
test -d "/Applications/Python 2.6/IDLE.app" && rm -r "/Applications/
Python 2.6/IDLE.app"
cp -PR IDLE.app "/Applications/Python 2.6"
touch "/Applications/Python 2.6/IDLE.app"
cp ./config-main.def "@executable_path/Python.framework/Versions/2.6/
lib/python2.6/idlelib/config-main.def"
cp: @executable_path/Python.framework/Versions/2.6/lib/python2.6/
idlelib/config-main.def: No such file or directory
make[2]: *** [install] Error 1
make[1]: *** [install_IDLE] Error 2
make: *** [frameworkinstallapps4way] Error 2


That is not cool.. really.. thats not cool. Thanks for your tipps.
--
http://mail.python.org/mailman/listinfo/python-list


Re: BadZipfile "file is not a zip file"

2009-01-08 Thread James Mills
On Fri, Jan 9, 2009 at 11:28 AM, webcomm  wrote:
> Hmm.  When I open it in Windows or with 7-Zip, it contains a text file
> that has the data I would expect it to have.  I guess that alone
> doesn't necessarily prove it's a zip file?
>
> datum is something I'm downloading via a web service.  The providers
> of the service say it's a zip file, and have provided a code sample in
> C# (which I know nothing about) that shows how to deal with it.  In
> the code sample, the file is base64 decoded and then unzipped.  I'm
> trying to write something in Python to decode and unzip the file.

Send us a sample of this file in question...

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


Python 2.6 fails on compiling > Bug report

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

I compiled Python 2.6.1 on a Mac OSX 10.5.5 Intel machine with this
configure command and got this message


FAILS: ./configure --with-framework-name=Python --with-
universal-archs=all --enable-framework --enable-
universals...@executable_path/my/path/to/app


WORKS: ./configure --with-framework-name=Python --with-
universal-archs=all --enable-framework --enable-universalsdk=/


[...]
configure: WARNING: dlfcn.h: present but cannot be compiled
configure: WARNING: dlfcn.h: check for missing prerequisite
headers?
configure: WARNING: dlfcn.h: see the Autoconf documentation
configure: WARNING: dlfcn.h: section "Present But Cannot Be
Compiled"
configure: WARNING: dlfcn.h: proceeding with the preprocessor's result
configure: WARNING: dlfcn.h: in the future, the compiler will take
precedence
configure: WARNING: ##
 ##
configure: WARNING: ## Report this to http://www.python.org/python-bugs
##
configure: WARNING: ##
 ##
checking for dlfcn.h... yes
[...] many of this error warnings occure.


/* its already noted in the bug reporter but there is no solution.
Does anyone know how to use my custom path?


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


Re: BadZipfile "file is not a zip file"

2009-01-08 Thread webcomm
On Jan 8, 8:02 pm, MRAB  wrote:
> You're just creating a file called "data.zip". That doesn't make it a
> zip file. A zip file has a specific format. If the file doesn't have
> that format then the zipfile module will complain.

Hmm.  When I open it in Windows or with 7-Zip, it contains a text file
that has the data I would expect it to have.  I guess that alone
doesn't necessarily prove it's a zip file?

datum is something I'm downloading via a web service.  The providers
of the service say it's a zip file, and have provided a code sample in
C# (which I know nothing about) that shows how to deal with it.  In
the code sample, the file is base64 decoded and then unzipped.  I'm
trying to write something in Python to decode and unzip the file.

I checked the file for comments and it has none.  At least, when I
view the properties in Windows, there are no comments.
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-08 Thread rurpy
Mark Wooding wrote:
...
> I agree with the comment about Pascal, but C is actually pretty similar
> to Python here.  C only does pass-by-value.

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


OSCON 2009: Call For Participation

2009-01-08 Thread Aahz
The O'Reilly Open Source Convention has opened up the Call For
Participation -- deadline for proposals is Tuesday Feb 3.

OSCON will be held July 20-24 in San Jose, California.

For more information, see
http://conferences.oreilly.com/oscon
http://en.oreilly.com/oscon2009/public/cfp/57
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it."  --Brian W. Kernighan
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-08 Thread rurpy
Mark Wooding wrote:
...
> Python doesn't need another name for assignment,

OK.

> because actually its
> idea of assignment is the same as anyone else's.  The difference is in
> the way it deals with objects.  See argument elsewhere.

"the same as anyone else's" only if [Python's] "idea
of assignment" does not include producing the same
results.

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

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

Telling someone coming to Python from one of those
languages that Python's assignment works the same way
as those languages is confusing at best.  "dealing
objects" is part of assignment semantics ISTM.


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


Re: BadZipfile "file is not a zip file"

2009-01-08 Thread MRAB

webcomm wrote:

The error...


file = zipfile.ZipFile('data.zip', "r")

Traceback (most recent call last):
  File "", line 1, in 
file = zipfile.ZipFile('data.zip', "r")
  File "C:\Python25\lib\zipfile.py", line 346, in __init__
self._GetContents()
  File "C:\Python25\lib\zipfile.py", line 366, in _GetContents
self._RealGetContents()
  File "C:\Python25\lib\zipfile.py", line 378, in _RealGetContents
raise BadZipfile, "File is not a zip file"
BadZipfile: File is not a zip file

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

data.zip is created in this script...

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

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

What would cause a zip file to not have a table of contents?  Is there
some way I can add a table of contents to a zip file using python?
Maybe there is some more fundamental problem with the data that is
making it seem like there is no table of contents?

You're just creating a file called "data.zip". That doesn't make it a 
zip file. A zip file has a specific format. If the file doesn't have 
that format then the zipfile module will complain.

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


BadZipfile "file is not a zip file"

2009-01-08 Thread webcomm
The error...

>>> file = zipfile.ZipFile('data.zip', "r")
Traceback (most recent call last):
  File "", line 1, in 
file = zipfile.ZipFile('data.zip', "r")
  File "C:\Python25\lib\zipfile.py", line 346, in __init__
self._GetContents()
  File "C:\Python25\lib\zipfile.py", line 366, in _GetContents
self._RealGetContents()
  File "C:\Python25\lib\zipfile.py", line 378, in _RealGetContents
raise BadZipfile, "File is not a zip file"
BadZipfile: File is not a zip file

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

data.zip is created in this script...

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

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

What would cause a zip file to not have a table of contents?  Is there
some way I can add a table of contents to a zip file using python?
Maybe there is some more fundamental problem with the data that is
making it seem like there is no table of contents?

Thanks in advance for your help.
Ryan
--
http://mail.python.org/mailman/listinfo/python-list


Re: ulimit stack size and python threads

2009-01-08 Thread Greg Lindahl
I see. I should be blaming the default behavior of pthreads. I did
work on a OpenMP library once, and we worked around this problem, plus
we gave good error messages. Given the number of HPC sites which use
Python, I'd think that Python would have grown similar features. (HPC
sites are more likely to have intermediate-sized stack limits due to
use of Fortran.)

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


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

2009-01-08 Thread Sergey Kishchenko
On 8 янв, 22:03, "Chris Rebert"  wrote:
> On Thu, Jan 8, 2009 at 5:53 AM, Sergey Kishchenko  wrote:
> > In Python empty container equals False in 'if' statements:
>
> > # prints "It's ok"
> > if not []:
> >    print "It's ok"
>
> > Let's create a simple Foo class:
>
> > class Foo:
> >    pass
>
> > Now I can use Foo objects in 'if' statements:
>
> > #prints "Ouch!"
> > f=Foo()
> > if f:
> >    print "Ouch!"
>
> > So, default __nonzero__ impl is to return True. I think, this
> > behaviour conflicts with 'Explicit is better than implicit' and
> > 'Practicality beats purity' statements. I think, throwing a TypeError
> > exception would be better.  It will result in more explicit code with
> > fewer errors.
>
> Python has a rich notion of boolean truth compared to other languages.
> In this case, by default, non-None objects are considered True. It's a
> reasonable default behavior since wanting to differentiate between
> None and non-None objects is such a common task.
> Also, I've been programming in Python for a long while and have yet to
> encounter any bug due to this behavior.
> Regarding the Zen, on the contrary, this is a perfect example of
> "Practicality beats purity" in action.
>
> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...http://rebertia.com

I agree with you. I completely forget about differentiating between
None and non-None objects. I think, thread can be closed.
--
http://mail.python.org/mailman/listinfo/python-list


Re: ulimit stack size and python threads

2009-01-08 Thread Andrew MacIntyre

Greg Lindahl wrote:

I figure this is a FAQ, but I can't find it in any FAQs.

I want to limit the stacksize on my server.

If I set it to 8 megs, or unlimited, python is happy.

If I set it to 4 gigabytes, things like yum (which is a python
program) crash creating a thread. This is on an x86_64 linux kernel,
RHEL5, etc etc.

Why is Python overloading the meaning of the ulimit -s like this?
There are plenty of real non-python programs with huge stack usage,
and I'd like my system default stack limit to be less than unlimited
but much larger than Python will allow.


The Python interpreter itself (absent a call to resource.setrlimit())
does nothing with resource limits - it just uses the environment it is
loaded into.

In the absence of effective alternative solutions, it may be possible to
achieve the effect you desire by calling resource.setrlimit() in your
Python installation's site.py, effectively over-riding the system
default.

--
-
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: andy...@bullseye.apana.org.au  (pref) | Snail: PO Box 370
   andy...@pcug.org.au (alt) |Belconnen ACT 2616
Web:http://www.andymac.org/   |Australia
--
http://mail.python.org/mailman/listinfo/python-list


Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python

2009-01-08 Thread J Kenneth King
Kay Schluehr  writes:

> On 8 Jan., 16:25, J Kenneth King  wrote:
>
>> As another poster mentioned, eventually PyPy will be done and then
>> you'll get more of an "in-Python" DSL.
>
> May I ask why you consider it as important that the interpreter is
> written in Python?

I don't think it's important for Python to have a meta-circular
interpreter (though it can't hurt).

> I see no connection between PyPy and syntactical
> Python extensions and the latter isn't an objective of PyPy. You can
> write Python extensions with virtually any Python aware parser.
> M.A.Lemburg already mentioned PLY and PLY is used for Cython. Then
> there is ANTLR which provides a Python grammar. I also know about two
> other Python aware parsers. One of them was written by myself.

Because... there is no connection to see? I never mentioned any such
relation.

DSL's tend to be a natural side-effect of languages which can manipulate
their own expressions without extensive parsing.

Creating a new parser that can generate Python AST's is certainly a
valid approach (and probably the easiest one). It's not the only one.

It depends on your definition of a DSL.

My definition isn't satisfied with creating a parser, and so my answers
reflect that.
--
http://mail.python.org/mailman/listinfo/python-list


Re: ulimit stack size and python threads

2009-01-08 Thread Greg Lindahl
> Why do you think Python is overloading the meaning of that? I ensure
> you it isn't - it doesn't actively care what the limits are.

Always crashing because I asked the OS to please not allow a process
to grow too big is what I call overloading the meaning of ulimit -s.
It's quite surprising. Not to mention the poor error message.

-- greg

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


Re: Making a decorator a staticmethod

2009-01-08 Thread Bruno Desthuilliers

Zac Burns a écrit :

To Bruno's first e-mail: Everything you said was correct but largely
off topic.


for a definition of "off topic" equals to "didn't fit your expectations".


I did already understand these things as well.


Sorry - but it was not necessarily obvious from your post, and it's near 
to impossible to guess someone's background on a single post basis. So 
please don't hold it against me. Anyway, you're not the only people 
reading this group, and some readers might not have the same knowledge.



To Bruno's second email
quote:
"""
Nope. He's relying on (part of) the interface(s) implemented by the
first argument. The class object itself has nothing to do with is
(well... it does, but only as far as it contribute to the
implementation of the interface expected by the decorator).
"""

Yes - this is exactly what I was trying to communicate. In addition -
perhaps what I should have made clear all along is that the interface
I'm relying on isn't as much of an interface as it is an
implementation. The attributes that I will be accessing are 'local'
(starting with underscores, let's not get into a discussion about this
- I know they aren't really local) and are subject to change as the
implementation of the class changes.


Ok.


The class provides a 'service' for the inherited classes with
differing behaviors for different method via the decorators it
provides. 


Not sure it's quite clear to me, but I think I get the whole picture.


The details of which I can't get into because it's
proprietary.


No problem - the above precisions are enough IMHO.


If this use case is objectionable to you then fine,  I've implemented
my own staticdecorator. 


What I still find questionable is the "necessity" to make your 
decorators part of the base class. OO is about objects - not classes - 
and Python's functions _are_ objects (just like modules, classes and 
everything else FWIW), so even if your decorators are bound to a 
specific implementation, well, what's the problem ? (BTW, please read 
until the end of the post before replying). Just document the fact that 
they only accept instances of your base class, and that's enough - well, 
IMHO at least !-).


Anyway, given Python's highly dynamic nature, being an instance of a 
given class _at a given moment in time_ doesn't garantee you have this 
or this _implementation attribute - at best, it makes it highly 
plausible, but that's all you can expect, really.



(which simply inherits from staticmethod and
implements call).


If it makes you feel better - no judgement call on this, I personnaly 
care a lot about feeling good with my own code, and perfectly understand 
we don't all have the same feeling about what looks right or wrong -, 
then go for it. FWIW, I might even agree with your choice if I saw the 
real code and knew the whole context. My point was just that it was not 
*technically* necessary to make the decorators part of the base class !-)



But to those who the use case makes sense for it may
be worth thinking about implementing the call method in the main
distribution.


Python is a free, opensource software, so feel free to send the patch - 
who knows, it might even be accepted if don't break anything. But 
prepare to make your point, the BDFL tends to be very conservative 
(which is a very GoodThing(tm) IMHO).


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


Re: ulimit stack size and python threads

2009-01-08 Thread Martin v. Löwis
> But even if that worked, I'd be worried that python is doing something
> bad with the ulimit -s value under the covers.

Again: it definitely isn't.

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


Re: Tree views - Best design practices

2009-01-08 Thread Jonathan Gardner
On Jan 8, 1:50 pm, "Filip Gruszczyński"  wrote:
>
> But I am looking for a different type of flexibility. I would like to
> be able to add more classes to my hierarchy and not have to change my
> code in many places when I add new class to the hierarchy. If I have
> to change every class in the hierarchy because I add new class, then
> it's not something I would like to do. And idea how I can avoid this?
>

I don't understand why you have to change other classes when you add a
new class under duck typing. The whole point of duck typing is you
don't look at the class or the interface of an object so you don't
have to keep track of it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: ulimit stack size and python threads

2009-01-08 Thread Greg Lindahl
> How much higher? You could try just under 4GB (unsigned 32-bit) and just
> under 2GB (signed 32-bit).

I'd like to set it to be about 1/2 the memory size of my server, which
happens to end up being 4 gbytes. And no, slightly less than 4 gb
doesn't work.

But even if that worked, I'd be worried that python is doing something
bad with the ulimit -s value under the covers.

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


Re: "python -3" not working as expected

2009-01-08 Thread Thorsten Kampe
* Terry Reedy (Thu, 08 Jan 2009 17:04:04 -0500)
> Thorsten Kampe wrote:
> > * Marc 'BlackJack' Rintsch (8 Jan 2009 16:26:55 GMT)
> >> On Thu, 08 Jan 2009 16:38:53 +0100, Thorsten Kampe wrote:
> >>> [Python 2.6.1]
> >>>
> >>> to test existing Python code, I ran "python -3" ("warn about Python 3.x
> >>> incompatibilities") against a test file that only contains "print
> >>> 'test'".
> >>>
> >>> Unfortunately I saw no warnings about print becoming a function in
> >>> Python 3 ("print()"). Where is the problem?
> >> There is no problem.  ``print``\s are handled fine by the 2to3.py 
> >> script.  The option warns about stuff that is not easily automatically 
> >> converted.
> > 
> > There /is/ obviously a problem: the Python command line help[1] and the 
> > "Porting To Python 3.0" section of "What’s New In Python 3.0" from Guido 
> > van Rossum are misleading (if not to say wrong):
> > 
> > """
> > For porting existing [...] code to Python 3.0, the best strategy is the 
> > following:
> > [...]
> > 2. [...] Turn on the -3 command line switch. This enables warnings about 
> > features that will be removed (or change) in 3.0.[...]
> > 3. Run the 2to3 source-to-source translator [...]
> > """
> > 
> > Thorsten
> > [1] "-3 : warn about Python 3.x incompatibilities"
> > --
> 
> Since you are, I believe, at least the second person to report being bit 
> by this confusion, please open an issue at bugs.python.org and suggest a 
> couple of revised sentences that you think are more informative.

Will do tomorrow. The revised sentence could be in the line of "warn 
about Python 3.x incompatibilities that cannot trivially be fixed by 
2to3.py".

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


Re: linked list with cycle structure

2009-01-08 Thread David Hláčik
Hi,

well i am able to find a loop in a list using two iterators.One
iterator runs "two times faster than the other", and if he encounters
the first, it means that there is a loop.

Example :
1,2,3,4,5,6,7,8,9,5
the algorithm would generate:

start - 1,2
iteration 1- 2, 4
iteration 2- 3, 6
iteration 3- 4, 8
iteration 4- 5, 5 ( match)

But how can this help me with counting list elements? :(

Thanks,

D.

On Thu, Jan 8, 2009 at 5:48 PM, Diez B. Roggisch  wrote:
>
> David Hláčik wrote:
>
> > Hi,
> >
> > so okay, i will create a helping set, where i will be adding elements
> > ID, when element ID will be allready in my helping set i will stop and
> > count number of elements in helping set. This is how long my cycled
> > linked list is.
>
> > But what if i have another condition , and that is *i can use only
> > helping memory with constant size* ? This means i am not able to
> > create any set and adding elements there. I need to have a constant
> > size variables . This is complication a complication for me.
>
> This isn't to hard - think about what you are really interested in - knowing
> if *all* other elements are already counted, or a specific one? You can get
> away with only one, to detect the cycle and abort.
>
> Diez
> --
> http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list


Re: eval('07') works, eval('08') fails, why?

2009-01-08 Thread Grant Edwards
On 2009-01-08, Bruno Desthuilliers  
wrote:
> Grant Edwards a ?crit :
>> On 2009-01-08, Alex van der Spek  wrote:
>> 
>>> Thanks much, that makes sense!
>> 
>> Well, that's the correct explanation.
>> 
>> Whether that feature makes sense or not is debatable.  Even I'm
>> not old-school enough that I ever use octal literals -- and I
>> used Unix on a PDP-11 for years (actually had my own PDP-11 for
>> while, but it never worked).  Now that I think of it, my
>> Heathkit Z80 stuff used octal notation too.
>
> What about your DeathStation 9000 ?-)

The closest thing I've seen to a DS9K would probably be a CDC
Cyber 6600 mainframe.  I guess for somebody with a mainframe
background it might not have been too weird, but for somebody
who came from a PDP-11, Z80, 65XX background, it seemed awfully
obtuse.

-- 
Grant Edwards   grante Yow! I'm having a RELIGIOUS
  at   EXPERIENCE ... and I don't
   visi.comtake any DRUGS
--
http://mail.python.org/mailman/listinfo/python-list


Re: ulimit stack size and python threads

2009-01-08 Thread Martin v. Löwis
> Why is Python overloading the meaning of the ulimit -s like this?

Why do you think Python is overloading the meaning of that? I ensure
you it isn't - it doesn't actively care what the limits are.

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


Re: Making a decorator a staticmethod

2009-01-08 Thread Zac Burns
To Bruno's first e-mail: Everything you said was correct but largely
off topic. I did already understand these things as well.
To Bruno's second email
quote:
"""
Nope. He's relying on (part of) the interface(s) implemented by the
first argument. The class object itself has nothing to do with is
(well... it does, but only as far as it contribute to the
implementation of the interface expected by the decorator).
"""

Yes - this is exactly what I was trying to communicate. In addition -
perhaps what I should have made clear all along is that the interface
I'm relying on isn't as much of an interface as it is an
implementation. The attributes that I will be accessing are 'local'
(starting with underscores, let's not get into a discussion about this
- I know they aren't really local) and are subject to change as the
implementation of the class changes.

The class provides a 'service' for the inherited classes with
differing behaviors for different method via the decorators it
provides. The details of which I can't get into because it's
proprietary.

If this use case is objectionable to you then fine, I've implemented
my own staticdecorator. (which simply inherits from staticmethod and
implements call). But to those who the use case makes sense for it may
be worth thinking about implementing the call method in the main
distribution.

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games



On Thu, Jan 8, 2009 at 12:21 PM, Bruno Desthuilliers
 wrote:
> Jonathan Gardner a écrit :
>>
>> On Jan 8, 11:18 am, "Zac Burns"  wrote:
>>>
>>> In my use case (not the example below) the decorator returns a
>>> function of the form def f(self, *args, **kwargs) which makes use of
>>> attributes on the instance self. So, it only makes sense to use the
>>> staticmethod in the class and in the baseclass. Making this decorator
>>> a module level function doesn't make sense here.
>>>
>>
>> I don't think you should be using staticmethod in this case since you
>> are relying on information in the class itself.
>
> Nope. He's relying on (part of) the interface(s) implemented by the first
> argument. The class object itself has nothing to do with is (well... it
> does, but only as far as it contribute to the implementation of the
> interface expected by the decorator).
>
>
>> (Aside: I really can't think of any reason to use staticmethods in
>> Python other than to organize functions into namespaces, and even
>> then, that's what modules are for, right?)
>
> I sometimes found staticmethods to be useful, in that they provided
> polymorphic dispatch without having to care about the containing module (or
> whether the object thru which the staticmethod_or_function is accessed is a
> module, class or instance).
>
>
>> I think you need to show a better example of what it is you are trying
>> to do.
>
> +1 on this. So far, the only concrete use case for staticmethod as a
> decorator I can think of is the one exposed above for staticmethods in
> general.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: "python -3" not working as expected

2009-01-08 Thread Terry Reedy

Thorsten Kampe wrote:

* Marc 'BlackJack' Rintsch (8 Jan 2009 16:26:55 GMT)

On Thu, 08 Jan 2009 16:38:53 +0100, Thorsten Kampe wrote:

[Python 2.6.1]

to test existing Python code, I ran "python -3" ("warn about Python 3.x
incompatibilities") against a test file that only contains "print
'test'".

Unfortunately I saw no warnings about print becoming a function in
Python 3 ("print()"). Where is the problem?
There is no problem.  ``print``\s are handled fine by the 2to3.py 
script.  The option warns about stuff that is not easily automatically 
converted.


There /is/ obviously a problem: the Python command line help[1] and the 
"Porting To Python 3.0" section of "What’s New In Python 3.0" from Guido 
van Rossum are misleading (if not to say wrong):


"""
For porting existing [...] code to Python 3.0, the best strategy is the 
following:

[...]
2. [...] Turn on the -3 command line switch. This enables warnings about 
features that will be removed (or change) in 3.0.[...]

3. Run the 2to3 source-to-source translator [...]
"""

Thorsten
[1] "-3 : warn about Python 3.x incompatibilities"
--


Since you are, I believe, at least the second person to report being bit 
by this confusion, please open an issue at bugs.python.org and suggest a 
couple of revised sentences that you think are more informative.


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


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

2009-01-08 Thread Bruno Desthuilliers

Sergey Kishchenko a écrit :

In Python empty container equals False in 'if' statements:


Yes.


# prints "It's ok"
if not []:
print "It's ok"

Let's create a simple Foo class:

class Foo:
pass

Now I can use Foo objects in 'if' statements:


Yes.


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

So, default __nonzero__ impl is to return True.


Yes. It's clearly documented FWIW.


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


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



and
'Practicality beats purity'


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



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


I can understand that you've been bitten by the rules regarding truth 
values of Python objects. But if so, please remember that it's only 
because *you* assumed something different from what's documented.

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


Re: ulimit stack size and python threads

2009-01-08 Thread MRAB

Greg Lindahl wrote:

I'm only guessing, but could it be a 32-bit limit somewhere? Have you
tried, say, 1GB, which would be within a 32-bit limit?


Indeed, ulimit -s 100 (a bit smaller than 1 GB) does work, but it
doesn't solve my problem, since I want to set the limit higher than 1
GB.

How much higher? You could try just under 4GB (unsigned 32-bit) and just 
under 2GB (signed 32-bit).

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


Re: Work with Open Office

2009-01-08 Thread Michael Torrie
Dan Esch wrote:
> Have been browsing through this list and reading documentation and tutorials
> for python self-study.  I have, apparently, teh stupid.  Google is my
> friend.  Off I go.  Thanks.

Let us know how it goes.  Last time I tried to script OO, I found it to
be much more difficult than VBA.  OO's scripting is a mess, honestly.

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


Re: Tree views - Best design practices

2009-01-08 Thread Filip Gruszczyński
> Yes. There is a difference between the interface of an object (namely,
> what methods and attributes it has and what their semantic meaning is)
> and the class of an object (what methods and attributes it has and how
> they are implemented.)
>
> In general, you shouldn't be asking about an object's class. Down the
> road, you may want to use an object that isn't of the same class but
> does support the interface.
>
> Consider how the file object is used in Python. Pretty much every
> place you can use a file object you can use a StringIO, right? That's
> because StringIO supports the file interface while it isn't a file.
>
> You may want to read up on 'duck typing' to get a better sense of why
> this is important.

I have read also your next message and these are good arguments for
dynamically typed languages and really convinces me - I mean that they
provide some flexibility at the cost of writing a little more. They
provide flexibility though in a different field. Duck typing is great,
because it allows completely use of a completely different hierarchy,
which only has to have the same interface.

But I am looking for a different type of flexibility. I would like to
be able to add more classes to my hierarchy and not have to change my
code in many places when I add new class to the hierarchy. If I have
to change every class in the hierarchy because I add new class, then
it's not something I would like to do. And idea how I can avoid this?

-- 
Filip Gruszczyński
--
http://mail.python.org/mailman/listinfo/python-list


Re: "python -3" not working as expected

2009-01-08 Thread Thorsten Kampe
* Marc 'BlackJack' Rintsch (8 Jan 2009 16:26:55 GMT)
> On Thu, 08 Jan 2009 16:38:53 +0100, Thorsten Kampe wrote:
> > [Python 2.6.1]
> > 
> > to test existing Python code, I ran "python -3" ("warn about Python 3.x
> > incompatibilities") against a test file that only contains "print
> > 'test'".
> > 
> > Unfortunately I saw no warnings about print becoming a function in
> > Python 3 ("print()"). Where is the problem?
> 
> There is no problem.  ``print``\s are handled fine by the 2to3.py 
> script.  The option warns about stuff that is not easily automatically 
> converted.

There /is/ obviously a problem: the Python command line help[1] and the 
"Porting To Python 3.0" section of "What’s New In Python 3.0" from Guido 
van Rossum are misleading (if not to say wrong):

"""
For porting existing [...] code to Python 3.0, the best strategy is the 
following:
[...]
2. [...] Turn on the -3 command line switch. This enables warnings about 
features that will be removed (or change) in 3.0.[...]
3. Run the 2to3 source-to-source translator [...]
"""

Thorsten
[1] "-3 : warn about Python 3.x incompatibilities"
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tree views - Best design practices

2009-01-08 Thread Jonathan Gardner
On Jan 8, 1:00 pm, "Filip Gruszczyński"  wrote:
>
> Is it really any better than asking for class? I mean, if I need to
> add another class to a hierarchy which behaves differently, will it be
> more flexible (actually you have to add another method to every class
> and check for in the gui). I believe it's just the same as asking for
> the class, but we hide it under static methods. It's no different
> though.
>

One additional note:

Given that the interface and class of an object are two, orthogonal
and independent things, how do you tell what interfaces an object
supports?

There are a variety of methods. I can break them down into 3.

(1) The user of the object keeps track of which classes support which
interfaces. This is bad because you can't anticipate new classes
properly. Sometimes it is necessary when the other two options aren't
feasible.

(2) The implementor of the object provides information on what
interfaces it supports through a method or attribute of some sort.
This is bad because there may be new interfaces that come into
existence that the object does support but the implementor doesn't
know about it and so the object says it doesn't support the interface.

(3) Some 3rd Party registration of interfaces and classes keeps track
of which classes support which interfaces, and vice-versa. When you
add a new interface, you have to list all the existing classes that
also support that interface. When you add a new class, you list all
the existing interfaces that it supports. This is just plain hard to
do, of course.

None of these solutions are perfect, of course.

Duck typing tries to solve this problem with option (4): Nobody really
keeps track of interfaces at all, and you just kind of wing it hoping
for the best. This solution is also far from perfect, but it suggests
that you never look at the class of an object, or really, even its
interface. You just start using it.

So my solution is the "just use it" bit. All the user really needs to
know is "Are you a leaf or a branch?" And the objects simply have to
answer that question.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tree views - Best design practices

2009-01-08 Thread Jonathan Gardner
On Jan 8, 1:00 pm, "Filip Gruszczyński"  wrote:
>
> Is it really any better than asking for class? I mean, if I need to
> add another class to a hierarchy which behaves differently, will it be
> more flexible (actually you have to add another method to every class
> and check for in the gui). I believe it's just the same as asking for
> the class, but we hide it under static methods. It's no different
> though.
>

Yes. There is a difference between the interface of an object (namely,
what methods and attributes it has and what their semantic meaning is)
and the class of an object (what methods and attributes it has and how
they are implemented.)

In general, you shouldn't be asking about an object's class. Down the
road, you may want to use an object that isn't of the same class but
does support the interface.

Consider how the file object is used in Python. Pretty much every
place you can use a file object you can use a StringIO, right? That's
because StringIO supports the file interface while it isn't a file.

You may want to read up on 'duck typing' to get a better sense of why
this is important.
--
http://mail.python.org/mailman/listinfo/python-list


Re: eval('07') works, eval('08') fails, why?

2009-01-08 Thread Bruno Desthuilliers

Grant Edwards a écrit :

On 2009-01-08, Alex van der Spek  wrote:


Thanks much, that makes sense!


Well, that's the correct explanation.

Whether that feature makes sense or not is debatable.  Even I'm
not old-school enough that I ever use octal literals -- and I
used Unix on a PDP-11 for years (actually had my own PDP-11 for
while, but it never worked).  Now that I think of it, my
Heathkit Z80 stuff used octal notation too.


What about your DeathStation 9000 ?-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Making a decorator a staticmethod

2009-01-08 Thread Bruno Desthuilliers

Jonathan Gardner a écrit :

On Jan 8, 11:18 am, "Zac Burns"  wrote:

In my use case (not the example below) the decorator returns a
function of the form def f(self, *args, **kwargs) which makes use of
attributes on the instance self. So, it only makes sense to use the
staticmethod in the class and in the baseclass. Making this decorator
a module level function doesn't make sense here.



I don't think you should be using staticmethod in this case since you
are relying on information in the class itself. 


Nope. He's relying on (part of) the interface(s) implemented by the 
first argument. The class object itself has nothing to do with is 
(well... it does, but only as far as it contribute to the implementation 
of the interface expected by the decorator).




(Aside: I really can't think of any reason to use staticmethods in
Python other than to organize functions into namespaces, and even
then, that's what modules are for, right?)


I sometimes found staticmethods to be useful, in that they provided 
polymorphic dispatch without having to care about the containing module 
(or whether the object thru which the staticmethod_or_function is 
accessed is a module, class or instance).




I think you need to show a better example of what it is you are trying
to do.


+1 on this. So far, the only concrete use case for staticmethod as a 
decorator I can think of is the one exposed above for staticmethods in 
general.



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


Re: ulimit stack size and python threads

2009-01-08 Thread Greg Lindahl
> I'm only guessing, but could it be a 32-bit limit somewhere? Have you
> tried, say, 1GB, which would be within a 32-bit limit?

Indeed, ulimit -s 100 (a bit smaller than 1 GB) does work, but it
doesn't solve my problem, since I want to set the limit higher than 1
GB.

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


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

2009-01-08 Thread Martin v. Löwis
>  i'd just ... much rather be completely independent of proprietary
> software when it comes to building free software.

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

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


Re: Making a decorator a staticmethod

2009-01-08 Thread Bruno Desthuilliers

Zac Burns a écrit :

I've read the "Making staticmethod objects callable?" thread now, and
would have to disagree that all the use cases are strange as stated at
http://www.python.org/dev/summary/2006-03-01_2006-03-15/#making-staticmethod-objects-callable

In my use case (not the example below)


Then please provide the concrete use case (or at least anything close 
enough)



the decorator returns a
function of the form def f(self, *args, **kwargs)


There's nothing magic wrt/ 'self'. Heck, it's not even a keyword.


which makes use of
attributes on the instance self.


s/instance self/object passed as first argument/


So, it only makes sense to use the
staticmethod in the class and in the baseclass.


Nope. It makes senses for any object implementing the (implied) 
interface, whatever the class or base class.



Making this decorator
a module level function doesn't make sense here.


It does, as long as you clearly document the expected interface.

Please understand that what you decorate are plain functions - not 
"methods". Whether the function is being used as the implementation for 
a method or not is totally irrelevant - what's relevant is what the 
decorated function expects as arguments.


wrt/ functions vs methods, technically, there's *no* difference between:

def func(some_interface):
   do_something_depending_on(some_interface)

and

class SomeClassImplementingSomeInterface(object):
def method(self):
   do_something_depending_on(some_interface)


As a matter of fact, the second snippet is strictly equivalent to:

def func(some_interface):
   do_something_depending_on(some_interface)

class SomeClassImplementingSomeInterface(object):
pass

SomeClassImplementingSomeInterface.method = func

IOW, a method is implemented by a function (that takes the object as 
first argument). To be true, a Python "method" object is only a thin 
wrapper around a function and an object, that is instanciated (by the 
function object itself) each time an attribute lookup resolves to a 
function object accessed thru the descriptor protocol.


wrt/ decorators: well, a "decorator" is just any callable(that is, any 
object implementing the __call__ method) taking a callable as first 
argument and returning a callable[1].


Now some decorator are generic - they just don't care about the 
decorated function's signature -, and some are not - they expect a (more 
or less) defined argument list. For example, the Django framework 
provides a set of decorators that expects to be applied to callables 
taking an HttpRequest object as first argument. That doesn't mean these 
decorators have to be methods of the HttpRequest class - they are just 
callable that takes as argument a callable which itself takes an 
HttpRequest object as first argument.



[1] and even this definition, while technically mostly correct, doesn't 
cover all acceptations of the term "decorator" - some so-called 
decorators are in fact callables taking some specific argument list and 
returning a proper decorator. That's what you get each time you see 
something like:


@decorate(some_arg)
def some_func(other_arg):
   code_here



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


Re: ulimit stack size and python threads

2009-01-08 Thread MRAB

Greg Lindahl wrote:

I figure this is a FAQ, but I can't find it in any FAQs.

I want to limit the stacksize on my server.

If I set it to 8 megs, or unlimited, python is happy.

If I set it to 4 gigabytes, things like yum (which is a python
program) crash creating a thread. This is on an x86_64 linux kernel,
RHEL5, etc etc.

Why is Python overloading the meaning of the ulimit -s like this?
There are plenty of real non-python programs with huge stack usage,
and I'd like my system default stack limit to be less than unlimited
but much larger than Python will allow.

I'm only guessing, but could it be a 32-bit limit somewhere? Have you 
tried, say, 1GB, which would be within a 32-bit limit?

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


Re: Tree views - Best design practices

2009-01-08 Thread Filip Gruszczyński
>class Element(object):
>@staticmethod
>def is_leaf(): return True
>@staticmethod
>def is_branch(): return False
>
>class Group(object):
>@staticmethod
>def is_leaf(): return False
>@staticmethod
>def is_branch(): return True
>
> Of course, you have to give priority to one or the other, in case an
> object thinks it is both.
>
>if thing.is_branch():
># Treat it like a branch
>elif thing.is_leaf():
># Treat it like a leaf
>
> I believe this is a simpler method than checking a single attribute
> for a name.

Is it really any better than asking for class? I mean, if I need to
add another class to a hierarchy which behaves differently, will it be
more flexible (actually you have to add another method to every class
and check for in the gui). I believe it's just the same as asking for
the class, but we hide it under static methods. It's no different
though.

-- 
Filip Gruszczyński
--
http://mail.python.org/mailman/listinfo/python-list


ulimit stack size and python threads

2009-01-08 Thread Greg Lindahl
I figure this is a FAQ, but I can't find it in any FAQs.

I want to limit the stacksize on my server.

If I set it to 8 megs, or unlimited, python is happy.

If I set it to 4 gigabytes, things like yum (which is a python
program) crash creating a thread. This is on an x86_64 linux kernel,
RHEL5, etc etc.

Why is Python overloading the meaning of the ulimit -s like this?
There are plenty of real non-python programs with huge stack usage,
and I'd like my system default stack limit to be less than unlimited
but much larger than Python will allow.

-- greg

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


Re: mmap only supports string

2009-01-08 Thread Robert Kern

Neal Becker wrote:

Problem is, AFAIK a string can only be created as a copy of some other data.  
Say I'd like to take some large object and read/write to/from mmap object.  A 
good way to do this would be the buffer protocol.  Unfortunately, mmap only 
supports string.  A string could only be created after copying the original 
object AFAIK.

I think mmap should work directly with buffer protocol, so it could directly 
read/write with objects supporting buffer protocol.  Specifically, mmap slice 
should support buffer protocol.


You could use numpy as an intermediate. Slices will be numpy arrays which are 
views onto the mmap (no memory copying), and you can get a buffer from the numpy 
arrays.


--
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: Replying to list messages

2009-01-08 Thread Terry Reedy

Ben Finney wrote:


For Thunderbird (which I see you're using, Paul), the open bug report
is https://bugzilla.mozilla.org/show_bug.cgi?id=45715>.
Meanwhile, you can install an add-on to provide the function
http://www.juergen-ernst.de/addons/replytolist.html>.


When I read the list as a newsgroup (gmane.comp.python.general), the 
Reply button of Thunderbird replies to the newsgroup.  There is also a 
Replay All button and Message/Reply to Sender only menu selection.


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


Re: Tree views - Best design practices

2009-01-08 Thread Jonathan Gardner
On Jan 8, 8:16 am, MRAB  wrote:
> Filip Gruszczyński wrote:
> > Hi!
>
> > I have certain design problem, which I cannot solve elegantly. Maybe
> > you know some good design patterns for this kind of tasks.
>
> > Task:
>
> > We have a model which has two kinds of objects: groups and elements.
> > Groups can hold other groups (subgroups) and elements. It's a simple
> > directory tree, for example. We would like to display it in a tree
> > view (which sound good for this kind of model). What is more required,
> > for groups and elements there are different sets of operations, which
> > should be available under right click. For example for group, there
> > should be operations: 'add element' and 'add group', and for element
> > there should be 'change properties'.
>
> > Do you know any smart way to achieve this? The simplest way is to ask
> > for the class and display operations accordingly. But from the first
> > day with OO programming I have heard, that asking for class is wrong.
> > But I can hardly see any easy and more maintainable solution for this
> > problem. Could you help me with this?
>
> You could ask the object what the operations are. Here's an example
> using strings:
>
>  >>> class Element(object):
>         operations = "Element operations"
>
>  >>> class Group(object):
>         operations = "Group operations"
>
>  >>> e = Element()
>  >>> g = Group()
>  >>>
>  >>> e.operations
> 'Element operations'
>  >>> g.operations
> 'Group operations'

When faced with this kind of scenario, I usually write boolean methods
like "is_leaf" or "is_branch".

class Element(object):
@staticmethod
def is_leaf(): return True
@staticmethod
def is_branch(): return False

class Group(object):
@staticmethod
def is_leaf(): return False
@staticmethod
def is_branch(): return True

Of course, you have to give priority to one or the other, in case an
object thinks it is both.

if thing.is_branch():
# Treat it like a branch
elif thing.is_leaf():
# Treat it like a leaf

I believe this is a simpler method than checking a single attribute
for a name.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to deepcopy a list of user defined lists?

2009-01-08 Thread Terry Reedy

srinivasan srinivas wrote:

Hi,
I have a class which is a subclass of builtin-type list.

#--
class clist(list):
def __new__(cls, values, ctor):
val = []
for item in values:
item = ctor(item)
val.append(item)

self = list.__new__(cls, val)

self.__values = val
self.__ctor = ctor
return self


A subclass of list should populate the list in __init__, not __new__, 
usually by calling list.__init__, as lists are mutable and subclasses 
thereof should be too.


class clist(list):
 def __init__(self, values, ctor):
 list.__init__(self, map(ctor, values))
 self.__ctor = ctor

clist1 = clist((1,2,3),str)
clist2 = clist((1,2,3), float)
alist1 = [clist1,clist2]
print(alist1)
#[['1', '2', '3'], [1.0, 2.0, 3.0]]

from copy import deepcopy
alist2 = deepcopy(alist1)
print(alist2)
#[['1', '2', '3'], [1.0, 2.0, 3.0]]

print(alist1[0] is alist2[0])
#False - ie, inner clist was copied

I omitted your __values attribute as redundant with the value of the 
clist itself.  Since clist is not a mixin class, double underscores for 
name-mangling are not needed.


Unless you make use of _ctor in other methods, initializing regular 
lists with 'list(map(ctor,values)) would work as well.


Terry Jan Reedy

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


Re: Tree views - Best design practices

2009-01-08 Thread Filip Gruszczyński
I'd love to have operations methods, which would return names of
operations and references to proper methods. The problem is that
certain operations require more, than just running some operation on
the Group or Element. Let's say, you want to add new group to an
existing one - you can't do that to an element. But adding a group
cannot be just done by calling group.addGroup. You must first
construct the group or at least get information require to construct a
group, so you must for example call a dialog which will ask for
information about the group. And that's in conflict with model/view
pattern.

And that's not all. I am using qt and when I want to add this group to
another one, I have to first call beginInsertRows on the model used by
the tree view (it's a layer between the gui and the real model).

I tried building a wrapper around my model which would provide
additional info, but then I had to keep relations between objects in
two places (relations between real groups and elements and wrapper
groups and wrapper elements), which wasn't the right way to do. I just
can figure a simple, elegant way to do this.

-- 
Filip Gruszczyński
--
http://mail.python.org/mailman/listinfo/python-list


Re: figuring week of the day....

2009-01-08 Thread Chris Rebert
On Tue, Jan 6, 2009 at 7:56 AM, tekion  wrote:
> Is there a module where you could figure week of the day, like where
> it starts and end. I need to do this for a whole year. Thanks.

The "%U" time format specifier (Week number of the year) to strftime()
[http://docs.python.org/library/datetime.html#strftime-behavior] might
be helpful.

Cheers,
Chris

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


Re: eval('07') works, eval('08') fails, why?

2009-01-08 Thread Terry Reedy

Alex van der Spek wrote:

I can't think of anything that could cause this. Similarly, eval('09') 
fails, but for string 0x with x<8 it works. I am teaching myself Python 
in order to climb the ladder from Algol(1980s)-->Pascal(1990s)--
VisualBasic(2000)-->Python. I am a physicist, have programmed computers 
all my life but I won't understand the real tech jargon of present day 
computer science. Please keep it simple


I taught myself Python as a statistician with a Fortran/C backgound by 
interleaving interactive experiments (such as you did) with reading of 
the manuals.  The Language Manual chapter on Lexical Analysis has an 
Integer Literals subsection that answers this question.  I strongly 
recommend you peruse the Language Manual and the initial Library Manual 
chapters on built-ins.


tjr

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


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

2009-01-08 Thread Chris Rebert
On Thu, Jan 8, 2009 at 5:53 AM, Sergey Kishchenko  wrote:
> In Python empty container equals False in 'if' statements:
>
> # prints "It's ok"
> if not []:
>print "It's ok"
>
> Let's create a simple Foo class:
>
> class Foo:
>pass
>
> Now I can use Foo objects in 'if' statements:
>
> #prints "Ouch!"
> f=Foo()
> if f:
>print "Ouch!"
>
> So, default __nonzero__ impl is to return True. I think, this
> behaviour conflicts with 'Explicit is better than implicit' and
> 'Practicality beats purity' statements. I think, throwing a TypeError
> exception would be better.  It will result in more explicit code with
> fewer errors.

Python has a rich notion of boolean truth compared to other languages.
In this case, by default, non-None objects are considered True. It's a
reasonable default behavior since wanting to differentiate between
None and non-None objects is such a common task.
Also, I've been programming in Python for a long while and have yet to
encounter any bug due to this behavior.
Regarding the Zen, on the contrary, this is a perfect example of
"Practicality beats purity" in action.

Cheers,
Chris

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


Re: why cannot assign to function call

2009-01-08 Thread Joe Strout

Mark Wooding wrote:


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

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


I addressed this elsewhere.  Summary: `pass-by-reference' is a different
thing to `all you manipulate are references': Python does pass-by-value,
but the things it passes -- by value -- are references.


Quite right.  It's easy to see this in languages where some types are 
references and others are simple values; and even easier in such a 
language that supports both pass-by-value and pass-by-reference (such as 
REALbasic or VB.NET).  Then you can easily see, in one language, all 
combinations of [value type, reference type] * [pass by ref, pass by val].


In Python, we only have reference types, and we only have pass by value, 
so out of the four combinations above, there is only one: references 
passed by value.  You'd think this would make it easier, but from the 
raging debates and repeated obfuscation on this point, it apparently 
makes Python MORE difficult to understand and explain (at least for some).



I agree with the comment about Pascal, but C is actually pretty similar
to Python here.  C only does pass-by-value.  If you want a function to
modify your variable, you have to pass a pointer value which points to
it.


Right -- a C/C++ pointer is (or at least, can be in normal usage) pretty 
similar to a reference (though of course you can do more low-level and 
hackish things with them too).  In C, such a reference is always passed 
by value, as in Python or Java, and just like the default mode in RB or 
.NET.  In C++, you can also choose to pass such a parameter by 
reference, like the ByRef mode in RB and .NET.  This parameter passing 
mode is unavailable in Python or Java.



Okay, the abstraction has leaked again... are the paperweights references
to the objects, or the names we've bound objects to? I'm confused...


They're the references to the objects.  You don't bind names to
objects.  You bind names slots in which you store references.


Well put (again).  This is technically the correct description, though 
in casual usage, I think it's fine to occasionally gloss over some of 
these layers as long as everyone involved understands what is meant. 
(This is especially true when the references are to immutable objects, 
which are functionally very similar to simple values.)



How do we decide what is best for newcomers to Python, depending on
background?


That I really don't know.  I'm not good at teaching total beginners
(does it show?) because I'm too enmired in the theory. ...


FWIW, I've spent a fair amount of time teaching beginners, though not so 
much in Python yet.  But plenty of time in other languages where the 
same questions come up.  In my experience, pointing out that a variable 
of any object type contains a *reference* to that object, rather than 
the object data itself, and then illustrating with a couple of examples, 
quickly clears up any confusion.  I've never had a newbie require more 
than a couple of exchanges on this topic before they get it.  (And 
before I joined the Python community, I never even felt the need to 
actually draw a picture [1] to make it clearer.)



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


I agree completely.  I can barely understand the other models myself.

Best,
- Joe

[1] http://www.strout.net/info/coding/valref/


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


Re: Making a decorator a staticmethod

2009-01-08 Thread Jonathan Gardner
On Jan 8, 11:18 am, "Zac Burns"  wrote:
>
> In my use case (not the example below) the decorator returns a
> function of the form def f(self, *args, **kwargs) which makes use of
> attributes on the instance self. So, it only makes sense to use the
> staticmethod in the class and in the baseclass. Making this decorator
> a module level function doesn't make sense here.
>

I don't think you should be using staticmethod in this case since you
are relying on information in the class itself. This really looks like
it should be a classmethod. Granted, the example you gave can be a
staticmethod, but it sounds like you want to incorporate some of the
information in the class.

(Aside: I really can't think of any reason to use staticmethods in
Python other than to organize functions into namespaces, and even
then, that's what modules are for, right?)

I think you need to show a better example of what it is you are trying
to do.
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-08 Thread Terry Reedy

Sergey Kishchenko wrote:

In Python empty container equals False in 'if' statements:

# prints "It's ok"
if not []:
print "It's ok"

Let's create a simple Foo class:

class Foo:
pass

Now I can use Foo objects in 'if' statements:

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

So, default __nonzero__ impl is to return True. I think, this
behaviour conflicts with 'Explicit is better than implicit' and
'Practicality beats purity' statements. I think, throwing a TypeError
exception would be better.  It will result in more explicit code with
fewer errors.


Sensible (and documented) defaults pervade Python.

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


Re: cPickle vs pickle discrepancy

2009-01-08 Thread Zac Burns
Correct, 2.6.1 does not complain.

If only I could upgrade! The workaround is obvious and I'll do that.
Thanks for your help.

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games



On Thu, Jan 8, 2009 at 11:34 AM, Carl Banks  wrote:
> On Jan 8, 12:27 pm, "Zac Burns"  wrote:
>> Thanks for your patience waiting for me to isolate the problem.
>>
>> | Package
>> --__init__.py ->empty
>> --Package.py ->empty
>> --Module.py
>>  import cPickle
>>  class C(object):
>> pass
>>  def fail():
>> return cPickle.dumps(C(), -1)
>>
>> import Package.Module
>> Package.Module.fail()
>>
>> The failure seems to happen because pickle correctly does an absolute
>> import and cPickle incorrectly relatively imports Package.py and fails
>> to find Module underneath.
>>
>> The package and the module named package was because in there was a
>> main class defined with the same name as the package in that file and
>> Module.py contained support code for the package.
>
> I'd call it a bug.  Nice detecitve work pinpointing it.
>
> I suspect the issue disappears (accidentally) in 2.6, though.  From
> the What's New in Python 2.6 doc:
>
> C API: the PyImport_Import and PyImport_ImportModule functions now
> default to absolute imports, not relative imports. This will affect C
> extensions that import other modules.
>
> Presumably __import__ also defaults to absolute (since PyImport_Import
> calls it) so the two modules should both use absolute imports in 2.6.
>
>
> Carl Banks
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python

2009-01-08 Thread mark
> So you can't make an internal DSL like this that uses Python's built-
> in grammar.  You'd have to hack the parser or settle for an external
> preprocessor.

This time it is really hard for me but I begin accepting the fact that
I will have to build an external DSL. I experimented some weeks ago
with ANTLR and the tools work fine but I do not like the extra effort
to learn and maintain the extra tooling. I think that in the beginning
the DSL language will have to change a very often as new features are
added. To implement a standardized rock solid language like SQL ANTLR
might be the perfect tool but to develop something from scratch that
will be expanded interactively a internal DSL has huge benefits.

Please not that I really like ANTLR. It is just the first tool I used
for this task and I want to double check if other tools fit better to
my needs.

I will look into Ply and Pyparsing over the next weeks unless someone
points out that there is some special tool that makes growing a new
"fast evolving" language as easy as building an internal DSL. Maybe
this is all overkill and there is a hacking ruby-style DSLs with
regular expressions recipe out there? So far I could not find one.

> However, the gist of it seems to be that you want to be able to write
> files in your DSL that can be imported just like a regular Python
> module.  Yes, that can be done.
>
> See PEP 302, Import Hooks:
>
> http://www.python.org/dev/peps/pep-0302/
>
> Python's standard importer looks for files with *.py, *.pyc, *.pyd, or
> *.so extensions.  You could write an importer that looks for *.dsl
> files, and, instead of loading it as a Python file, invokes your DSL
> parser.

This is really helpful. Thanks for giving me directions.

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


Re: Tree views - Best design practices

2009-01-08 Thread Terry Reedy

Filip Gruszczyński wrote:

class Element(object):

   operations = "Element operations"



class Group(object):

   operations = "Group operations"



e = Element()
g = Group()

e.operations

'Element operations'

g.operations

'Group operations'


But this is the same as asking for a class,


The point is to put (and use) the information about what operations 
apply to instances in each class rather than in an external function.


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


Re: cPickle vs pickle discrepancy

2009-01-08 Thread Carl Banks
On Jan 8, 12:27 pm, "Zac Burns"  wrote:
> Thanks for your patience waiting for me to isolate the problem.
>
> | Package
> --__init__.py ->empty
> --Package.py ->empty
> --Module.py
>      import cPickle
>      class C(object):
>         pass
>      def fail():
>         return cPickle.dumps(C(), -1)
>
> import Package.Module
> Package.Module.fail()
>
> The failure seems to happen because pickle correctly does an absolute
> import and cPickle incorrectly relatively imports Package.py and fails
> to find Module underneath.
>
> The package and the module named package was because in there was a
> main class defined with the same name as the package in that file and
> Module.py contained support code for the package.

I'd call it a bug.  Nice detecitve work pinpointing it.

I suspect the issue disappears (accidentally) in 2.6, though.  From
the What's New in Python 2.6 doc:

C API: the PyImport_Import and PyImport_ImportModule functions now
default to absolute imports, not relative imports. This will affect C
extensions that import other modules.

Presumably __import__ also defaults to absolute (since PyImport_Import
calls it) so the two modules should both use absolute imports in 2.6.


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


Re: Work with Open Office

2009-01-08 Thread Terry Reedy

Benjamin Kaplan wrote:



On Thu, Jan 8, 2009 at 10:07 AM, Dan Esch > wrote:


Okay, I'm currently stuck with VBA / Excel in work and the following
paradigm:
 
VB (6? .NET? not sure) ==> VBA ==> Excel 2003 and Access
 
Where I'd like to be is this
 
Python ==>  X  ==> Open Office / (MySQL or other) for some

sufficiently useful value of X.
 
Does it exist?  Is it just a set of modules I need to be looking

for? or something else?
 



Did you google search first? This is the second result for "Python 
OpenOffice".


http://wiki.services.openoffice.org/wiki/Python


PyUNO is basically for working 'live' with OOo.  There is also odfpy at
http://opendocumentfellowship.com/development/projects/odfpy
for working with open doc format documents as produced by OOo and other 
programs.


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


Re: why cannot assign to function call

2009-01-08 Thread Mark Wooding
Erik Max Francis  wrote:
> Terry Reedy wrote:
>
> >  >>> a='par'+'rot'
> >  >>> b='parrot'
> >  >>> a is b
> > True
> 
> One exactly doesn't really say much.  It's implementation dependent, and 
> depends on the length of the string:
> 
>  >>> a = 'this is a much longer ' + 'parrot'
>  >>> b = 'this is a much longer parrot'
>  >>> a is b
> False

That Terry's example works is due to constant folding in the bytecode
compiler.  Consider:

In [1]: a = 'parrot'

In [2]: a is 'par' + 'rot'
Out[2]: True

Fair enough.  But build the string in a more complicated way:

In [3]: b = 'par'

In [4]: a is b + 'rot'
Out[4]: False

What's going on?  In the first case, the compiler notices that both
operands to `+' are constants, and evaluates the concatenation at
compile-time.  The resulting constant string is then interned if it's
short enough.

Putting part of the string in a variable is enough to stymie this
optimization -- the same compiler gets used in functions which can't
assume that the variable will still have the same value as it does now.
The concatenation method on strings doesn't try to intern the result, as
that might be a runtime performance loss.

> In practice, tests like these are pretty much never useful.  It's 
> completely implementation dependent when and under what circumstances 
> fundamental immutable objects are reused, and it's not useful anyway; 
> what you care about is whether two objects are equal or not, not whether 
> they're the same object through some optimization behind the scenes.

Absolutely.  The examples above provide insight into how the specific
implementation actually behaves; but that's of strictly academic
interest.  (Well, I find that sort of thing interesting, anyway.)

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


Re: Making a decorator a staticmethod

2009-01-08 Thread Zac Burns
I've read the "Making staticmethod objects callable?" thread now, and
would have to disagree that all the use cases are strange as stated at
http://www.python.org/dev/summary/2006-03-01_2006-03-15/#making-staticmethod-objects-callable

In my use case (not the example below) the decorator returns a
function of the form def f(self, *args, **kwargs) which makes use of
attributes on the instance self. So, it only makes sense to use the
staticmethod in the class and in the baseclass. Making this decorator
a module level function doesn't make sense here.

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games



On Thu, Jan 8, 2009 at 10:54 AM, Zac Burns  wrote:
> I have a decorator in a class to be used by that class and by inheriting 
> classes
>
> ##
> class C(object):
>@staticmethod # With  this line enabled or disabled usage in either C
> or D will be broken. To see that D works remember to remove usage in C
>def decorateTest(func):
>def newFunc(*args, **kwargs):
>print args, kwargs
>return func(*args, **kwargs)
>return newFunc
>
>@decorateTest
>def testDecorated(self):
>return None
> class D(C):
>@C.decorateTest
>def test2(self):
>return None
> ##
>
> The exception that I get when using it as a staticmethod and try to
> use it in the baseclass is "TypeError: 'staticmethod' object is not
> callable".
> When it is not staticmethod the exception I get in the extension class
> is is "TypeError: unbound method decorateTest() must be called with C
> instance as first argument (got function instance instead)"
>
> Python version is 2.5.1
>
> --
> Zachary Burns
> (407)590-4814
> Aim - Zac256FL
> Production Engineer (Digital Overlord)
> Zindagi Games
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do you write to the printer ?

2009-01-08 Thread Albert Hopkins
On Wed, 2009-01-07 at 16:46 -0600, da...@bag.python.org wrote:
> Can find nothing in the on-line docs or a book.
> Groping in the dark I attempted :
> 
> script24
> import io
> io.open('stdprn','w')  # accepted
> stdprn.write('hello printer')   # fails  < stdprn is not defined >

You didn't specify what platform you are running on, but the first
problem I see with the above is... well stdprn is not defined.  Did you
perhaps mean:

sdtprn = io.open('stdprn', 'w')
stdprn.write(...)

However on a Linux system that will simply open and write to a file
called 'stdprn' in your current directory.

-a


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


Re: Making a decorator a staticmethod

2009-01-08 Thread Bruno Desthuilliers

Zac Burns a écrit :

I have a decorator in a class


Why ?

(snip)

The exception that I get when using it as a staticmethod and try to
use it in the baseclass is "TypeError: 'staticmethod' object is not
callable".
When it is not staticmethod the exception I get in the extension class
is is "TypeError: unbound method decorateTest() must be called with C
instance as first argument (got function instance instead)"


Just make it a plain function and you'll be fine.

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


Re: How to Delete a Cookie?

2009-01-08 Thread Jose C
On Jan 8, 10:33 am, tryg.ol...@gmail.com wrote:
> On Jan 8, 1:16 pm, Jose C  wrote:
>
>
>
> > > c["mycook"]["expires"] = 0
>
> > Set ["expires"] using the following format to any time less than
> > current (which causes the browser to delete the cookie).
> > Here's a function I use to return a cookie expiry timestamp, negative
> > values passed in result in cookie being deleted.
>
> > def cookie_expiry_date(numdays):
> >     """ Returns a cookie expiry date in the required format.  -ve
> > value in = kill cookie.
> >     `expires` should be a string in the format "Wdy, DD-Mon-YY
> > HH:MM:SS GMT"
> >     NOTE!  Must use [expires] because earlier IE versions don't
> > support [max-age].
> >     """
> >     from datetime import date, timedelta
> >     new = date.today() + timedelta(days = numdays)
> >     return new.strftime("%a, %d-%b-%Y 23:59:59 GMT")
>
> > Usage:
> > c["mycook"]["expires"] = cookie_expiry_date(-10)  # any negative value
> > will remove cookie
>
> > HTH,
> > JC
>
> Jose C's piece of code works to delete the cookie as does setting
> ["expires"]=0 but ONLY as long as I also set the path.  Why is this?

The path specifies which directory the cookie is active. Usually the
path is set to /, which means the cookie is valid throughout the
entire domain, but you could set it to /mydir meaning it would only be
active for pages within /mydir.

> So what would be the best way to do this.  I tried reading in the
> existing cookie (b), creating a new cookie (c) with all the same
> values except for the "expires" but this did not get my cookie
> deleted.

To kill the cookie, simply set a cookie with the same name (and path)
and a past date (or 0, although IIRC there was some issue with 0 being
used on a particular browser some time ago, can't remember which on at
the moment) as an 'expires' parameter, is enough to tell the browser
to kill an existing cookie with that same name, regardles of it's
value or previous expiry, etc.

Basically, when you set a cookie, the browser overwrites the previous
one of the same name if it exists.  If not, it creates a new cookie
with your specified parameters.

So in your case, when you want to kill the cookie you set previously,
you should be able to just set a cookie of the exact same name, path
and 'expire' it appropriately, and the browser takes care of the
rest.  Don't worry about assigning it's previous value, the browser is
just going to delete it anyway.

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


Re: compiling python2.5 on linux under wine

2009-01-08 Thread lkcl
>  ... nd, that means disabling setup.py or hacking it significantly
> to support a win32 build, e.g. to build pyexpat, detect which modules
> are left, etc. by examining the remaining vcproj files in PCbuild.

ok - i started the hacking.

the first bit of hacking is this, in distutils/sysconfig.py,
added to _init_nt()

try:
filename = get_makefile_filename()
parse_makefile(filename, g)
except IOError, msg:
my_msg = "invalid Python installation: unable to open %s" %
filename
if hasattr(msg, "strerror"):
my_msg = my_msg + " (%s)" % msg.strerror

raise DistutilsPlatformError(my_msg)

# load the installed pyconfig.h:
try:
prefix = EXEC_PREFIX
prefix = os.path.join(prefix, "PC")
filename = os.path.join(prefix, "pyconfig.h")
parse_config_h(file(filename), g)
except IOError, msg:
my_msg = "invalid Python installation: unable to open %s" %
filename
if hasattr(msg, "strerror"):
my_msg = my_msg + " (%s)" % msg.strerror

raise DistutilsPlatformError(my_msg)

global _config_vars
_config_vars = g


that gets me part-way - at least i get... oh dear :

self.build_extensions()
  File "../setup.py", line 183, in build_extensions
self.compiler.set_executables(**args)
  File "Z:\mnt\src\python2.5-2.5.2\lib\distutils\ccompiler.py", line
165, in set_executables
(key, self.__class__.__name__)
ValueError: unknown executable 'compiler_so' for class MSVCCompiler

whoops :)

so, next, we hack in a compiler, in to ... ooo, let's saaay...
distutils/cygwinccompiler.py, just for fun.

now we get this!

winegcc -fPIC -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -
Wstrict-prototypes -I. -IZ:\mnt\src\python2.5-2.5.2\./Include -I. -
IInclude -I../Include -I/usr/local/include -IZ:\mnt\src
\python2.5-2.5.2\include -IZ:\mnt\src\python2.5-2.5.2\PC -c Z:\mnt\src
\python2.5-2.5.2\Modules\_ctypes/_ctypes_test.c -o z:\mnt\src
\python2.5-2.5.2\modules\_ctypes\_ctypes_test.o

wha-hey!

but... oh dear.

oh dear number 1)

firstly, err this is cross-compiling - those path names are
bullshit because actually we're compiling on LINUX damnit, not
windows.  hmm there's something to work around that one, perhaps,
by installing the mingw32 compiler under wine (o god i've done that
before, it's dreadfully slow)

oh dear number 2)

  File "Z:\mnt\src\python2.5-2.5.2\lib\os.py", line 562, in spawnv
return _spawnvef(mode, file, args, None, execv)
  File "Z:\mnt\src\python2.5-2.5.2\lib\os.py", line 545, in _spawnvef
wpid, sts = waitpid(pid, 0)
NameError: global name 'waitpid' is not defined

 err oh - ok, found another missing function: spawnv.  so, added
 that, in PC/pcbuild.h:

#ifdef __WINE__
#define HAVE_SPAWNV
#endif

 and after some futzing around with yet more #ifdefs in posixmodule.c
 we have another build - this time using wine's spawnv so it doesn't
 try to find a non-existent waitpid aaannnd SPLAT yesss, we get the
 crash-output from winegcc:

Failed to configure _ctypes module
building '_ctypes_test' extension
winegcc -fPIC -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -
Wstrict-prototypes -I. -IZ:\mnt\src\python2.5-2.5.2\./Include -I. -
IInclude -I../Include -I/usr/local/include -IZ:\mnt\src
\python2.5-2.5.2\include -IZ:\mnt\src\python2.5-2.5.2\PC -c Z:\mnt\src
\python2.5-2.5.2\Modules\_ctypes/_ctypes_test.c -o z:\mnt\src
\python2.5-2.5.2\modules\_ctypes\_ctypes_test.o
wine: Unhandled page fault on read access to 0x7265704f at address
0x601ec25b (thread 001c), starting debugger...
Unhandled exception: page fault on read access to 0x7265704f in 32-bit
code (0x601ec25b).
Register dump:
 CS:0023 SS:002b DS:002b ES:002b FS:0063 GS:006b
 EIP:601ec25b ESP:0032c70c EBP:0032c718 EFLAGS:00010206(   - 00  -
RIP1)
 EAX:7265704f EBX:7bc8a7a4 ECX:0003 EDX:604ab3d7
 ESI:0032c848 EDI:7265704f
Stack dump:
0x0032c70c:  7bc6859d 7265704f 6056a0b8 0032c788
0x0032c71c:  603fd0eb 7265704f 006e9544 001bc84c
0x0032c72c:  006c574c 6056b82c 605721a0 0002
0x0032c73c:  0032c7d8 718e21fe 0016329c 7265704f
0x0032c74c:  00730065 002e0074 006f 00159320
0x0032c75c:  603aa590 001b3f0c 005086e0 0004
Backtrace:
=>1 0x601ec25b strlen+0xb() in libc.so.6 (0x0032c718)
fixme:dbghelp_dwarf:dwarf2_parse_variable Unsupported form for const
value degToRad (a)
  2 0x603fd0eb do_mkvalue+0x3db(p_format=, p_va=, flags=0x0) [/mnt/src/python2.5-2.5.2/
build/../Python/modsupport.c:419] in python (0x0032c788)
  3 0x603fcc6d do_mktuple+0x7d(p_format=0x32c848, p_va=0x32c844,
endchar=0x29, n=0x2, flags=0x0) [/mnt/src/python2.5-2.5.2/build/../
Python/modsupport.c:268] in python (0x0032c7b8)
 .
 .


 hey, this is fun!  let's try a crazed compile of python and see what
falls over, whe :)


 ... much as this seems to be consuming much of my time, for some
bizarre reason i just can't seem to stop.


 anyway - yes, this is effectively cross-compiling, and so the
python25.exe and en

Making a decorator a staticmethod

2009-01-08 Thread Zac Burns
I have a decorator in a class to be used by that class and by inheriting classes

##
class C(object):
@staticmethod # With  this line enabled or disabled usage in either C
or D will be broken. To see that D works remember to remove usage in C
def decorateTest(func):
def newFunc(*args, **kwargs):
print args, kwargs
return func(*args, **kwargs)
return newFunc

@decorateTest
def testDecorated(self):
return None
class D(C):
@C.decorateTest
def test2(self):
return None
##

The exception that I get when using it as a staticmethod and try to
use it in the baseclass is "TypeError: 'staticmethod' object is not
callable".
When it is not staticmethod the exception I get in the extension class
is is "TypeError: unbound method decorateTest() must be called with C
instance as first argument (got function instance instead)"

Python version is 2.5.1

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games
--
http://mail.python.org/mailman/listinfo/python-list


Re: sftp with no password from python

2009-01-08 Thread Mike Hjorleifsson
On Jan 8, 10:39 am, loial  wrote:
> Is it possible to use sftp without a password from python?

Yes you can use keys you preestablish between the server and client so
you dont need passwords, i do this on all my servers then lock off the
ability to accept passwords at all, this way no one can dictionary
attack my ssh servers.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is it ok to type check a boolean argument?

2009-01-08 Thread Carl Banks
On Jan 7, 6:21 pm, Scott David Daniels  wrote:
> Adal Chiriliuc wrote:
> > On Jan 7, 10:15 pm, Bruno Desthuilliers
> >  wrote:
> >> ... I'd either keep the argument as a boolean but rename it "ascending" ...
>
> > Well, I lied a bit :-p  
> > But what if we can't solve it as elegantly, and we need to ...
>
> > Should we typecheck in this case to ensure that if we pass a string
> > for "fast_mode" we will raise an exception?
>
> Why are you concerned only with type errors on inputs?
> Even if you could do exhaustive checking of input parameters to
> make sure they are the only acceptable values, what prevents your
> user from providing the wrong valid value?  What made you pick on
> type errors in the first place?  If it turns out that an argument
> of 422 is a typo for 42, why is that less of a problem?  Just because
> you are used to systems where one kind of error is always caught does
> not really make you invulnerable.  You'll have no problem telling
> your users that the 422 is their fault.  Why do you have such
> certainty that passing in "nonsense" as a boolean is a failure
> you need to deal with?


I'm going to play Devil's Advocate here.

The motivation here is not "we want type safety" but "our unit tests
can't register this deliberate error because it passes silently".
Presumably if they had a function that accepted integers, and 442 was
an invalid value, and the function failed silently in a unit test,
they would also consider whether it should instead fail loudly.

It's always a judgment call how much to screen for bad input, but type
errors aren't different from any other error in this regard.
Sometimes it's appropriate (note: not, IMHO, in this case), just like
it's sometimes appropriate to check for 442.


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


Re: why cannot assign to function call

2009-01-08 Thread Mark Wooding
[Steven's message hasn't reached my server, so I'll reply to it here.
Sorry if this is confusing.]

Aaron Brady  wrote:
> On Jan 8, 1:45 am, Steven D'Aprano
>  wrote:
> > On Wed, 07 Jan 2009 10:17:55 +, Mark Wooding wrote:
> >
> > > The `they're just objects' model is very simple, but gets tied up in
> > > knots explaining things.  The `it's all references' model is only a
> > > little more complicated, but explains everything.
> >
> > But it *over* explains, because it implies things that "everybody knows"
> > about references in other languages that aren't true for Python.

I addressed this elsewhere.  Summary: `pass-by-reference' is a different
thing to `all you manipulate are references': Python does pass-by-value,
but the things it passes -- by value -- are references.

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

> > Of course it's not literally true that "everybody knows" that you
> > can use references to implement a swap(x, y) procedure. But people
> > coming from a C or Pascal background tend to assume that everything
> > is like C/Pascal, and there are a lot of them. If C was a rare,
> > unfamiliar language, my opposition to using the term "reference"
> > would be a lot milder. Maybe in another five years?

I agree with the comment about Pascal, but C is actually pretty similar
to Python here.  C only does pass-by-value.  If you want a function to
modify your variable, you have to pass a pointer value which points to
it.  Python has no pointer values, so you need a different hack.  The
hack usually involves lists.  (Though it's easier in the main to return
compound data objects like tuples.  I don't suppose that a proposal for
true multiple return values would go down well here.  No, didn't think
so...)

> > Okay, the abstraction has leaked again... are the paperweights references
> > to the objects, or the names we've bound objects to? I'm confused...

They're the references to the objects.  You don't bind names to
objects.  You bind names slots in which you store references.

This discussion -- I'd call it an argument, but that might give the
wrong impression, because I think we're being remarkably civil and
constructive by the standards of Usenet arguments! -- hasn't started on
the topic of variable bindings or environments yet.

> > How do we deal with anonymous objects in your model?
> >
> > --
> > Steven
> 
> Mark, hi, Steven, pleasure as always.

Hello. ;-)

> Neither side is perfect or wild; (Do admit it); 

It's true.

> How do we decide what is best for newcomers to Python, depending on
> background?

That I really don't know.  I'm not good at teaching total beginners
(does it show?) because I'm too enmired in the theory.  (It doesn't help
that I go off on tangents about how language X does something similar
but subtly different all the time, though my rich background comes in
very useful all over the place and that's something I think is worth
sharing.)

It probably doesn't help that I came to Python with a thorough
understanding of Scheme (among many others) under my belt, because many
Scheme concepts carry over directly, including the data model (it's all
references) and the variable model (nested, mutable, lexical
environments with closures).

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

Oh, while I remember: the `distributed Python' model, with auto-updating
copies, only works for sharing.  Circular structures still require
actual references or a Tardis.

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


Re: "python -3" not working as expected

2009-01-08 Thread Benjamin Peterson
Steve Holden  holdenweb.com> writes:
> Thorsten Kampe wrote:
> > Unfortunately I saw no warnings about print becoming a function in 
> > Python 3 ("print()"). Where is the problem?
> > 
> I *believe* that's not flagged because 2to3 will fix it automatically.

This is correct; there's not much point to adding py3k warning for things that
2to3 can fix easily.



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


Re: Replying to list messages

2009-01-08 Thread Paul McNett

Ben Finney wrote:

Paul McNett  writes:

But arguing about this here isn't going to change anything: opinions
differ just like tabs/spaces and bottom-post/top-post.


In cases like this, one side can simply be wrong :-)

Best of luck getting your programs behaving as you want them to!


BTW, I agree with you that in an ideal, pure world mailing lists wouldn't munge 
the
reply-to field, but when 80% of the people use email clients that don't support
reply-list, the practical thing to do as a list admin that wants to avoid 
having to
explain over and over again that "your client software is broken" is to simply
swallow some pride and munge the reply-to. Now, 99% of the users are happy, and 
the
remaining 1% are elite enough to understand how to get around any problems this
caused. Happy is good.

Paul

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


Re: How to Delete a Cookie?

2009-01-08 Thread tryg . olson
On Jan 8, 1:16 pm, Jose C  wrote:
> > c["mycook"]["expires"] = 0
>
> Set ["expires"] using the following format to any time less than
> current (which causes the browser to delete the cookie).
> Here's a function I use to return a cookie expiry timestamp, negative
> values passed in result in cookie being deleted.
>
> def cookie_expiry_date(numdays):
> """ Returns a cookie expiry date in the required format.  -ve
> value in = kill cookie.
> `expires` should be a string in the format "Wdy, DD-Mon-YY
> HH:MM:SS GMT"
> NOTE!  Must use [expires] because earlier IE versions don't
> support [max-age].
> """
> from datetime import date, timedelta
> new = date.today() + timedelta(days = numdays)
> return new.strftime("%a, %d-%b-%Y 23:59:59 GMT")
>
> Usage:
> c["mycook"]["expires"] = cookie_expiry_date(-10)  # any negative value
> will remove cookie
>
> HTH,
> JC


Jose C's piece of code works to delete the cookie as does setting
["expires"]=0 but ONLY as long as I also set the path.  Why is this?
So what would be the best way to do this.  I tried reading in the
existing cookie (b), creating a new cookie (c) with all the same
values except for the "expires" but this did not get my cookie
deleted.

b = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
c = Cookie.SimpleCookie()
c[cookieName] = b[cookieName].value
c[cookieName]["expires"] = 0
c[cookieName]["path"]= b[cookieName]["path"]
print c
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >