From: Martin P. Hellwig on
On 05/04/10 11:28, superpollo wrote:
> Samuel Williams ha scritto:
>> I personally like indentation.
>>
>> I just wonder whether it is an issue that some people will dislike.
<cut>
> there might be problems if for example you
> generate code from a one-line template.
<cut>
Well a one-line template code generator are great and such, but if the
output should be human readable it is necessary to have at least some
form of mark-up control on it. Like newlines and indentations.

On the other hand if it is only meant to generate executable code,
generating an interpreted code might not be the best solution for the
actual problem.

For the corner cases (I can think of a couple) it is good to know you
can use ';' most of the time.

--
mph
From: Stefan Behnel on
superpollo, 04.05.2010 13:23:
> Stefan Behnel ha scritto:
>> 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.
>
> 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...

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?

Stefan

From: superpollo on
Stefan Behnel ha scritto:
> superpollo, 04.05.2010 13:23:
>> Stefan Behnel ha scritto:
>>> 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.
>>
>> 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...
>
> 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 i am back again to the problem:

def fun():
....

ecc...

how can i put *that* on a oneliner?
From: superpollo on
Martin P. Hellwig ha scritto:
> On 05/04/10 11:28, superpollo wrote:
>> Samuel Williams ha scritto:
>>> I personally like indentation.
>>>
>>> I just wonder whether it is an issue that some people will dislike.
> <cut>
>> there might be problems if for example you
>> generate code from a one-line template.
> <cut>
> Well a one-line template code generator are great and such, but if the
> output should be human readable it is necessary to have at least some
> form of mark-up control on it. Like newlines and indentations.

or latex markup, in my case...

> On the other hand if it is only meant to generate executable code,
> generating an interpreted code might not be the best solution for the
> actual problem.

yes, maybe generating code for a *compiler* might be faster but you must
consider overhead due to compilation time, especially if code snippets
are sprinkled by the dozen all over the template file.

> For the corner cases (I can think of a couple) it is good to know you
> can use ';' most of the time.
>

most but not always as i noted (think about loops or function definition)

bye

From: James Mills on
On Tue, May 4, 2010 at 9:43 PM, Stefan Behnel <stefan_ml(a)behnel.de> wrote:
>> 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...

>>> def quadratic(a, b):
.... return a + b if a > b else a**b - b**2
....
>>> a, b = 2, 3
>>> print quadratic(a, b)
-1
>>> a, b = 3, 2
>>> print quadratic(a, b)
5
>>>

--james