From: James Mills on
On Tue, May 4, 2010 at 9:56 PM, superpollo <utente(a)esempio.net> wrote:
> of course! *but* if i must generate on-the-fly python code that defines a
> function i am back again to the problem:

One-liner:

$ python
Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49)
[GCC 4.4.1 (CRUX)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a, b = 2, 3
>>> print a + b if a > b else a**b - b**2
-1
>>> a, b = 3, 2
>>> print a + b if a > b else a**b - b**2
5
>>>

--James
From: Benjamin Kaplan on
On Tue, May 4, 2010 at 7:23 AM, superpollo <utente(a)esempio.net> wrote:
> Stefan Behnel ha scritto:
>>
>> superpollo, 04.05.2010 12:28:
>>>
>>> i think there is an issue if you -- say -- produce python code, from
>>> within another programming environment, to be executed on the fly, at
>>> least in some instances. there might be problems if for example you
>>> generate code from a one-line template.
>>
>> There are a couple of code generation tools available that you can find on
>> PyPI.
>>
>> However, the main reason why this problem doesn't hurt much in Python is
>> that Python is a dynamic language that can get you extremely far without
>> generating code. It's simply not necessary in most cases, so people don't
>> run into problems with it.
>>
>> Stefan
>>
>
> Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> A,B=2,3
>>>> if A>B:
> ...     print A+B
> ... else:
> ...     print A**B-B**2
> ...
> -1
>>>> A,B=3,2
>>>> if A>B:
> ...     print A+B
> ... else:
> ...     print A**B-B**2
> ...
> 5
>>>>
>
> tell me please: how can generate the same output (depending on A and B)
> without control structure? i mean in a natural "pythonic" way...
>
> bye

Well, it requires 2.6 or 3

Python 2.6.5 (r265:79063, Mar 21 2010, 22:38:52)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> A,B=2,3;print(A+B if A > B else A**B - B**2)
-1
>>> A,B=3,2;print(A+B if A > B else A**B - B**2)
5

>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
From: superpollo on
James Mills ha scritto:
> On Tue, May 4, 2010 at 9:56 PM, superpollo <utente(a)esempio.net> wrote:
>> of course! *but* if i must generate on-the-fly python code that defines a
>> function i am back again to the problem:
>
> One-liner:
>
> $ python
> Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49)
> [GCC 4.4.1 (CRUX)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> a, b = 2, 3
>>>> print a + b if a > b else a**b - b**2
> -1
>>>> a, b = 3, 2
>>>> print a + b if a > b else a**b - b**2
> 5
>
> --James

much obliged.

From: Stefan Behnel on
superpollo, 04.05.2010 13:56:
> Stefan Behnel ha scritto:
>> The question is: why do you have to generate the above code in the
>> first place? Isn't a function enough that does the above?
>
> of course! *but* if i must generate on-the-fly python code that defines
> a function [...]

Well, could you provide a use case where you have to generate Python code,
and where normally written code with some kind of parametrisation won't work?

The only thing that currently comes to my mind is a template compiler that
translates a user provided template into an executable Python program. But
that should be pretty trivial to do as well. I just remembered this little
thing on Fredrik's effbot site, which should help here:

http://effbot.org/zone/python-code-generator.htm

Stefan

From: Stefan Behnel on
Ed Keith, 04.05.2010 14:15:
> I wrote AsciiLitProg (http://asciilitprog.berlios.de/) in Python. It is
> a literate programming tool. It generates code from a document. It can
> generate code in any language the author wants. It would have been a LOT
> easier to write if it did not generate Python code.
>
> Python is a great language to write in (although I do wish it did a
> better job with closures). But it is a PITA to generate code for!

Interesting. Could you elaborate a bit? Could you give a short example of
what kind of document text you translate into what kind of Python code, and
what the problems were that you faced in doing so?

Stefan