The pythonistas or language lawyers would probably consider this "more Pythonic". Why? Well it's more obfuscated, therefore it must be better.heda wrote: ↑Tue Oct 26, 2021 3:36 pm ...
can be done with the one-liner...Code: Select all
pts = [makePt(x, y) for x, y in zip(xpts, ypts)]
help about drawing
Forum rules
and Helpful information
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help
Also, be nice to others! Read the FreeCAD code of conduct!
Also, be nice to others! Read the FreeCAD code of conduct!
Re: help about drawing
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Re: help about drawing
had it coming (of course)
-
- Veteran
- Posts: 5505
- Joined: Thu Apr 05, 2018 1:53 am
Re: help about drawing
Cool. Thanks for not resisting. You have taught me something new -- a way to use zip() in list comprehension. We can, of course, refine this even further by eliminating the function and the pts list:heda wrote: ↑Tue Oct 26, 2021 3:36 pm off-topic.
any code that works is good code in my book, however cannot resist from putting a slight variation of marksters code out there,
does the same - is just a bit more compact...
the passagecan be done with the one-linerCode: Select all
pts = [] ii=0 for x in xpts: y = ypts[ii] pts.append(makePt(x,y)) ii += 1
or if one prefers loopsCode: Select all
pts = [makePt(x, y) for x, y in zip(xpts, ypts)]
my personal preference is the one-liner, easy to both read and write once one is used to that type of construct, and who knows, might be the fastest one (on the margin) as wellCode: Select all
pts = list() for x, y in zip(xpts, ypts): pts.append(makePt(x, y))
Code: Select all
import Draft
xpts = [0,0,0,0.01,0.02,.05,0.11,0.26,0.55,1.06,1.81,2.78,3.95,5.28,6.72,8.22,9.74,11.27,12.80,14.33]
ypts = [18.98,17.45,15.91,14.38,12.85,11.32,9.80,8.27,6.77,5.33,4.00,2.82,1.84,1.08,0.57,0.26,0.11,0.04,0.01,0.0]
doc = FreeCAD.newDocument() if not FreeCAD.ActiveDocument else FreeCAD.ActiveDocument
Draft.makeBSpline([FreeCAD.Vector(x,y,0) for x,y in zip(xpts,ypts)])
doc.recompute()