[Arch] ArchWall dangling "else"

A forum dedicated to the Draft, Arch and BIM workbenches development.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
chennes
Veteran
Posts: 3910
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

[Arch] ArchWall dangling "else"

Post by chennes »

In ArchWall.py starting at line 864 we have a while loop:

Code: Select all

863                                    for edge in baseEdges:
864                                        while offset < (edge.Length-obj.Joint.Value):
865                                            #print i," Edge ",edge," : ",edge.Length," - ",offset
866                                            if offset:
867                                                t = edge.tangentAt(offset)
868                                                p = t.cross(n)
869                                                p.multiply(1.1*obj.Width.Value+obj.Offset.Value)
870                                                p1 = edge.valueAt(offset).add(p)
871                                                p2 = edge.valueAt(offset).add(p.negative())
872                                                sh = Part.LineSegment(p1,p2).toShape()
873                                                if obj.Joint.Value:
874                                                    sh = sh.extrude(-t.multiply(obj.Joint.Value))
875                                                sh = sh.extrude(n)
876                                                if i == 0:
877                                                    cuts1.append(sh)
878                                                else:
879                                                    cuts2.append(sh)
880                                            offset += (obj.BlockLength.Value + obj.Joint.Value)
881                                        else: # There is no break to cause this to be hit
882                                            offset -= (edge.Length - obj.Joint.Value)
The final statement there on line 882 can never be hit, because there's no break in the while loop. Is that else supposed to correspond to something else (so it's just mis-indented), or is it just unnecessary, or is there supposed to be a break in the loop someplace?
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: [Arch] ArchWall dangling "else"

Post by carlopav »

From stackoverflow:
"The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won't be executed. "

so the else will actually be executed when offset > (edge.Length-obj.Joint.Value) isn't it?
follow my experiments on BIM modelling for architecture design
User avatar
chennes
Veteran
Posts: 3910
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: [Arch] ArchWall dangling "else"

Post by chennes »

Yes, sorry, you're right, my statement was exactly backwards: the code in question will *always* be executed, because there is no break statement.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
paullee
Veteran
Posts: 5120
Joined: Wed May 04, 2016 3:58 pm

Re: [Arch] ArchWall dangling "else"

Post by paullee »

Good to read a series of posts to learn python indeed (still a python beginner) :)

It seems line 880 update the offset in every loop, so line 864 may not run another loop (condition False).

And " The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won’t be executed." - https://www.geeksforgeeks.org/python-while-loop/ ?

Code: Select all

864                                        while offset < (edge.Length-obj.Joint.Value):
...
880                                            offset += (obj.BlockLength.Value + obj.Joint.Value)
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: [Arch] ArchWall dangling "else"

Post by carlopav »

chennes wrote: Mon Sep 27, 2021 11:16 pm Yes, sorry, you're right, my statement was exactly backwards: the code in question will *always* be executed, because there is no break statement.
Oh, yes. You are right, so we could remove the "else" so the code Is easier to read?
Paullee, do you know what this piece of code Is for?
follow my experiments on BIM modelling for architecture design
paullee
Veteran
Posts: 5120
Joined: Wed May 04, 2016 3:58 pm

Re: [Arch] ArchWall dangling "else"

Post by paullee »

carlopav wrote: Tue Sep 28, 2021 6:08 am
Oh, yes. You are right, so we could remove the "else" so the code Is easier to read?
Paullee, do you know what this piece of code Is for?
See my finding in post above - seems 'while' does not need 'break' to run 'else'.

So based on the link explantion, the 'else' is probably required.


Can simply add a print ('run this else') to test....


This chunk of codes is for calculation of block number (there is a remark in earlier line :))
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: [Arch] ArchWall dangling "else"

Post by carlopav »

paullee wrote: Tue Sep 28, 2021 12:11 pm So based on the link explantion, the 'else' is probably required.
it's not :) . as @chennes correctly pointed out, the code inside the else: indent is alwais executed. So in my opinion it's clearer to de-indent it and to remove the else:, to make clear that the code is always supposed to be executed at the end of the while loop.
follow my experiments on BIM modelling for architecture design
paullee
Veteran
Posts: 5120
Joined: Wed May 04, 2016 3:58 pm

Re: [Arch] ArchWall dangling "else"

Post by paullee »

carlopav wrote: Tue Sep 28, 2021 12:39 pm it's not :) . as @chennes correctly pointed out, the code inside the else: indent is alwais executed. So in my opinion it's clearer to de-indent it and to remove the else:, to make clear that the code is always supposed to be executed at the end of the while loop.
Thanks pointing this out Again, as your both discussion above had already make this clear. It requires me reading several time to make sense why the 'else' is not required when there is no break :lol: Again, good exercise for python beginner like myself.

BTW, I always want to explore the algorithm as for a few cornercase it would fails.

And there is another chunk for 'counting blocks' in about line 956, wondering if 'counting' could be done together along with 'calculating'?


And always wonder if @realthunder's Link could be used to generate the Block Array rather than repeating all the Block Shape in the Wall Objects.
User avatar
chennes
Veteran
Posts: 3910
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: [Arch] ArchWall dangling "else"

Post by chennes »

Finally getting around to eliminating this:
git commit 17ebbd166

The algorithm still might be worth a revisit, but at least there's no confusion about whether that code executes or not.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
carlopav
Veteran
Posts: 2062
Joined: Mon Dec 31, 2018 1:49 pm
Location: Venice, Italy

Re: [Arch] ArchWall dangling "else"

Post by carlopav »

Thanks Chris!
follow my experiments on BIM modelling for architecture design
Post Reply