discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Re: The nature of Python in OpenSCAD and future plans/features

NB
Nick Bailey
Mon, Sep 11, 2023 1:22 PM

That function seems unnecessarily complicated. I'd have written

def writeln(*args) :

...     print(''.join(map(str, args)))
...

writeln('The answer is ', 42)

The answer is 42

and you can even leave out the map() if you are prepared to demand all
the args are strings. In which case, maybe you don't need a function at
all? you can add file=f to the print command and end up with something like

with open('path_to_file', 'w') as f :
    print(stuff, file=f)

which seems a bit small for a whole python module.

I'm thinking python module, but maybe I'm missing the boat. Is that an
OpenSCAD module?

On 11/09/2023 12:09, William F. Adams via Discuss wrote:

def writeln(*arguments):
    line_to_write = ""
    for number in arguments:
        line_to_write += number
    f.write(line_to_write)

save that to a file: gcodewrite.py

and use it with commands such as:

writeln("G0 X",str(w),"Y",str(d),"Z",str(h))

Which just leaves importing that, and opening/naming the file:

use < gcodewrite.py>

fn = "gcode.nc"
f = open(fn, "w")

which seems simple enough, but I'd like to work up a way to wrap that
in an if/then in an OpenSCAD file --- the obvious:

if (usepython == true) {use <gcodewrite.py>};

triggers the error:

ERROR: Parser error: syntax error in file gcodepreviewing.scad, line 3

Execution aborted

So, is there a way to conditionally execute such a command in an
elegant fashion? I'm about to stuff it into a module, but I don't like
that conceptually.

William

--
Sphinx of black quartz, judge my vow.
https://designinto3d.com/

That function seems unnecessarily complicated. I'd have written >>> def writeln(*args) : ...     print(''.join(map(str, args))) ... >>> writeln('The answer is ', 42) The answer is 42 and you can even leave out the map() if you are prepared to demand all the args are strings. In which case, maybe you don't need a function at all? you can add file=f to the print command and end up with something like with open('path_to_file', 'w') as f :     print(stuff, file=f) which seems a bit small for a whole python module. I'm thinking python module, but maybe I'm missing the boat. Is that an OpenSCAD module? On 11/09/2023 12:09, William F. Adams via Discuss wrote: > def writeln(*arguments): >     line_to_write = "" >     for number in arguments: >         line_to_write += number >     f.write(line_to_write) > > > save that to a file: gcodewrite.py > > and use it with commands such as: > > writeln("G0 X",str(w),"Y",str(d),"Z",str(h)) > > > Which just leaves importing that, and opening/naming the file: > > use < gcodewrite.py> > > fn = "gcode.nc" > f = open(fn, "w") > > which seems simple enough, but I'd like to work up a way to wrap that > in an if/then in an OpenSCAD file --- the obvious: > > if (usepython == true) {use <gcodewrite.py>}; > > triggers the error: > > ERROR: Parser error: syntax error in file gcodepreviewing.scad, line 3 > > Execution aborted > > So, is there a way to conditionally execute such a command in an > elegant fashion? I'm about to stuff it into a module, but I don't like > that conceptually. > > William > > -- > Sphinx of black quartz, judge my vow. > https://designinto3d.com/ >
WF
William F. Adams
Mon, Sep 11, 2023 2:30 PM

On Monday, September 11, 2023 at 09:22:31 AM EDT, Nick Bailey nicholas.bailey@glasgow.ac.uk wrote:

That function seems unnecessarily complicated. I'd have written

def writeln(*args) :               

...     print(''.join(map(str, args)))
...  

writeln('The answer is ', 42)      

The answer is 42
and you can even leave out the map() if you are prepared to demand all the args are strings. >In which case, maybe you don't need a function at all? you can add file=f to the print command > and end up with something like
with open('path_to_file', 'w') as f :
    print(stuff, file=f)

which seems a bit small for a whole python module. >  >I'm thinking python module, but maybe I'm missing the boat. Is that an OpenSCAD module?

it's a Python module which gets imported into OpenSCAD, since I'm far more familiar with that.
Trying to feel out the best way to use Python w/in OpenSCAD w/ the nifty new version --- what are the best practices?
Bonus points if they allow me to continue to use:
https://github.com/derkork/openscad-graph-editor

William

On Monday, September 11, 2023 at 09:22:31 AM EDT, Nick Bailey <nicholas.bailey@glasgow.ac.uk> wrote: >That function seems unnecessarily complicated. I'd have written >>>> def writeln(*args) :                >...     print(''.join(map(str, args))) >...   >>>> writeln('The answer is ', 42)       >The answer is 42 >and you can even leave out the map() if you are prepared to demand all the args are strings. >In which case, maybe you don't need a function at all? you can add file=f to the print command > and end up with something like >with open('path_to_file', 'w') as f : >    print(stuff, file=f) >which seems a bit small for a whole python module. > >I'm thinking python module, but maybe I'm missing the boat. Is that an OpenSCAD module? it's a Python module which gets imported into OpenSCAD, since I'm far more familiar with that. Trying to feel out the best way to use Python w/in OpenSCAD w/ the nifty new version --- what are the best practices? Bonus points if they allow me to continue to use: https://github.com/derkork/openscad-graph-editor William