POV-Ray : Newsgroups : povray.off-topic : Programming style question - specifically Python Server Time
29 Jul 2024 16:29:10 EDT (-0400)
  Programming style question - specifically Python (Message 1 to 10 of 16)  
Goto Latest 10 Messages Next 6 Messages >>>
From: Shay
Subject: Programming style question - specifically Python
Date: 17 Aug 2011 11:46:06
Message: <4e4be23e@news.povray.org>
How would I best type the following:

if long_condition1 or (long_condition2 and long_condition3):

Without making a complete, unreadable mess.

some_name = long_conditionn # bool

will not work, because I can't evaluate long_condition2 if 1 is true.

 -Shay


Post a reply to this message

From: nemesis
Subject: Re: Programming style question - specifically Python
Date: 17 Aug 2011 13:12:24
Message: <4e4bf678$1@news.povray.org>
Shay escreveu:
> How would I best type the following:
> 
> if long_condition1 or (long_condition2 and long_condition3):
> 
> Without making a complete, unreadable mess.
> 
> some_name = long_conditionn # bool
> 
> will not work, because I can't evaluate long_condition2 if 1 is true.

what's the problem people have with parentheses?

-- 
a game sig: http://tinyurl.com/d3rxz9


Post a reply to this message

From: Shay
Subject: Re: Programming style question - specifically Python
Date: 17 Aug 2011 21:00:08
Message: <4e4c6418$1@news.povray.org>
On Wed, 17 Aug 2011 14:12:02 -0300, nemesis wrote:

> Shay escreveu:
>> How would I best type the following:
>> 
>> if long_condition1 or (long_condition2 and long_condition3):
>> 
>> Without making a complete, unreadable mess.
>> 
>> some_name = long_conditionn # bool
>> 
>> will not work, because I can't evaluate long_condition2 if 1 is true.
> 
> what's the problem people have with parentheses?


No problem with parenthesis. But Python is a little funny about spacing.

A = [0, 1]
B = [0, 1]
C = [0, 1]
D = [0, 1]
List = [A, B, C, D]

if (
	some_funk(x for x in List) or
	(
		some_other_func(x for x in List) and
		a_third_func(x for x in List))):


That's legal(ish), but now I'm screwed with my indentation.


if (
	some_funk(x for x in List) or
	(some_other_func(x for x in List) and
	a_third_func(x for x in List))):
	print("execute code here")

How I ended up going, but it's still a little hairy to read.

Then I though about it a bit more and went with:

my_bool = (
	some_funk(x for x in List) or
	(
		some_other_func(x for x in List) and
		a_third_func(x for x in List)))
if my_bool:
	print("execute code here")


 -Shay


Post a reply to this message

From: Darren New
Subject: Re: Programming style question - specifically Python
Date: 17 Aug 2011 22:45:44
Message: <4e4c7cd8@news.povray.org>
Hmm.

     y = (x for x in List)
     if some_funk(y) or some_other_func(y) and a_third_func(y):

Why would you even want to calculate (x for x in List) three times?

Or am I misunderstanding "x for x in List"? Isn't that a list comprehension 
in Python?

-- 
Darren New, San Diego CA, USA (PST)
   How come I never get only one kudo?


Post a reply to this message

From: Kevin Wampler
Subject: Re: Programming style question - specifically Python
Date: 18 Aug 2011 12:02:50
Message: <4e4d37aa$1@news.povray.org>
On 8/17/2011 7:45 PM, Darren New wrote:
> Hmm.
>
> y = (x for x in List)
> if some_funk(y) or some_other_func(y) and a_third_func(y):
>
> Why would you even want to calculate (x for x in List) three times?
>
> Or am I misunderstanding "x for x in List"? Isn't that a list
> comprehension in Python?
>

It's not a list comprehension, but rather creates a generator object 
iterating over the items in List.  The list comprehension would use 
square brackets [x for x in List].  I still don't understand why it's 
being used here though, since it seems like List could just be passed in 
directly since it's obviously iterable too (unless the called functions 
are doing something odd, which maybe is the case here).


Post a reply to this message

From: Shay
Subject: Re: Programming style question - specifically Python
Date: 19 Aug 2011 16:04:33
Message: <4e4ec1d1$1@news.povray.org>
On Wed, 17 Aug 2011 19:45:42 -0700, Darren New wrote:

> Hmm.
> 
>      y = (x for x in List)
>      if some_funk(y) or some_other_func(y) and a_third_func(y):
> 
> Why would you even want to calculate (x for x in List) three times?
> 
> Or am I misunderstanding "x for x in List"? Isn't that a list
> comprehension in Python?


That's just a BS example. Just a style question. Here's a real example of 
what I'm trying to do:

# A special case for 2-dimensional vectors
# with acute ccw angles
A = [[1, 2, 0], [2, 3, 4], [4, 8, 6]]

bool_a = len(A[0]) == 2
bool_b = min([A[2] == x[2] for x in A[1:])
bool_c = ccw_angle(*A) < pi

ccw_angle is my own function
bool_b will not work if bool_a is true

if bool_a or bool_b:
	if bool_c:

would mean code repetition where:
a or b is true and c is false
neither a nor b is true

So, maybe I do have the best answer with:
bool_ = long conditions
if bool_:


Post a reply to this message

From: Darren New
Subject: Re: Programming style question - specifically Python
Date: 19 Aug 2011 16:49:02
Message: <4e4ecc3e$1@news.povray.org>
On 8/19/2011 13:04, Shay wrote:
> bool_a = len(A[0]) == 2
> bool_b = min([A[2] == x[2] for x in A[1:])
> bool_c = ccw_angle(*A)<  pi
>
> bool_b will not work if bool_a is true

bool_b = bool_a ? min(...) : false

-- 
Darren New, San Diego CA, USA (PST)
   How come I never get only one kudo?


Post a reply to this message

From: Shay
Subject: Re: Programming style question - specifically Python
Date: 21 Aug 2011 01:52:04
Message: <4e509d04@news.povray.org>
On Fri, 19 Aug 2011 13:49:01 -0700, Darren New wrote:

> On 8/19/2011 13:04, Shay wrote:
>> bool_a = len(A[0]) == 2
>> bool_b = min([A[2] == x[2] for x in A[1:]) bool_c = ccw_angle(*A)<  pi
>>
>> bool_b will not work if bool_a is true
> 
> bool_b = bool_a ? min(...) : false

Beautiful, but that doesn't seem to be valid Python. However, a bit of 
guess-work reveals that:

bool_b = False if bool_a else min(...)

does work. So, I learned something there.


Thanks.
 -Shay


Post a reply to this message

From: Darren New
Subject: Re: Programming style question - specifically Python
Date: 21 Aug 2011 11:00:33
Message: <4e511d91$1@news.povray.org>
On 8/20/2011 22:52, Shay wrote:
> On Fri, 19 Aug 2011 13:49:01 -0700, Darren New wrote:
>
>> On 8/19/2011 13:04, Shay wrote:
>>> bool_a = len(A[0]) == 2
>>> bool_b = min([A[2] == x[2] for x in A[1:]) bool_c = ccw_angle(*A)<   pi
>>>
>>> bool_b will not work if bool_a is true
>>
>> bool_b = bool_a ? min(...) : false
>
> Beautiful, but that doesn't seem to be valid Python.

OK. I had assumed python had that operator. It's the same as

if bool_a then bool_b = min(...) else bool_b = false;

> bool_b = False if bool_a else min(...)
> does work. So, I learned something there.

I think you *might* have the condition reversed on that. That looks like 
it's saying you don't evaluate bool_b if bool_a is true.

-- 
Darren New, San Diego CA, USA (PST)
   How come I never get only one kudo?


Post a reply to this message

From: Shay
Subject: Re: Programming style question - specifically Python
Date: 21 Aug 2011 22:14:30
Message: <4e51bb86$1@news.povray.org>
On Sun, 21 Aug 2011 08:00:28 -0700, Darren New wrote:

> On 8/20/2011 22:52, Shay wrote:
>> On Fri, 19 Aug 2011 13:49:01 -0700, Darren New wrote:
>>
>>> On 8/19/2011 13:04, Shay wrote:
>>>> bool_a = len(A[0]) == 2
>>>> bool_b = min([A[2] == x[2] for x in A[1:]) bool_c = ccw_angle(*A)<  
>>>> pi
>>>>
>>>> bool_b will not work if bool_a is true
>>>
>>> bool_b = bool_a ? min(...) : false
>>
>> Beautiful, but that doesn't seem to be valid Python.
> 
> OK. I had assumed python had that operator. It's the same as
> 
> if bool_a then bool_b = min(...) else bool_b = false;
> 
>> bool_b = False if bool_a else min(...) does work. So, I learned
>> something there.
> 
> I think you *might* have the condition reversed on that. That looks like
> it's saying you don't evaluate bool_b if bool_a is true.

That's what I want. I mistyped a portion of my example

bool_b = min([A[2] == x[2] for x in A[1:])

should have been

bool_b = min([A[0][2] == x[2] for x in A[1:])

bool_b tests if all of the [2]s are equal. If bool_a is true, there are 
no [2]s.

Thanks for the help.

 -Shay


Post a reply to this message

Goto Latest 10 Messages Next 6 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.