User:Orso.b.schmid

From Vectorworks Developer
Revision as of 11:05, 17 May 2015 by CBM-c- (Talk | contribs)

Jump to: navigation, search

Ciao,

I am Orso, an Italian Vectorscripter since many years. I feel very comfortable with Vectorscript (from now on: VS) but will now switch over to Python for even more power. I will try to share here comments, problems -and solutions- from the point of view of a non-programmer. --Orso.b.schmid (talk) 08:13, 17 May 2015 (EDT)

Open the terminal on your Mac and write

python

try snippets there or in an editor of your choice

Lists

Lists are powerful in Python, below some fascinating lists manipulations. They remind me of Applescript:

months = "Jan Feb Mar Apr May Jun Jul"
months = months.split() # no splitter defined and it will use the empty space --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
months[2] # note that the index is 0-based --> 'Mar'
months2 = "Jan, Feb, Mar, Apr, May, Jun, Jul" # overwrite a copy
months2.split(', ') # use comma and empty space as splitter --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
months.append('Jul') # append adds an item to a list --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
months.pop() # pop fetches the last item of a list --> 'Jul'
', delirious '.join(months) # --> 'Jan, delirious Feb, delirious Mar, delirious Apr, delirious May, delirious Jun, delirious Sep'
'-'.join(months[1:3]) # --> 'Feb-Mar'
del months[2] # --> ['Jan', 'Feb', 'Apr', 'May', 'Jun', 'Jul']
months = {1: 'Jan', 2: 'Feb', 3: 'Mar'} # --> {1: 'Jan', 2: 'Feb', 3: 'Mar'}

Increment a var

So far I have spent a really inordinate amount of time trying to increment a counter. In VS one does it like this:

cnt := cnt +1; { variable cnt is incremented }

For example:

PROCEDURE Test;
VAR
    cnt : INTEGER; { variable scope here is global }

     { subroutine fitting ForEachObject }
    PROCEDURE DoSomething(h: HANDLE);
        BEGIN
            { ... do something }
            cnt := cnt +1; { variable is incremented }
        END;
BEGIN
    cnt := 0; { explicit is better than implicit :) }
    ForEachObject(DoSomething, (ALL)); { pick objects by criteria, there the variable cnt will increment }
    AlrtDialog(Concat('Did something ', cnt, ' times.'));
END;
RUN(Test);

How do I get this done in Python? It's not as easy as it looks like. I thought that one did it like this:

cnt += 1

For example:

DoSomething(h):
   # ... do something
   cnt += 1 # variable is incremented

cnt = 0 # explicit is better than implicit :)
vs.ForEachObject(DoSomething, ("ALL"))  # pick objects by criteria, there the variable cnt will increment
vs.AlrtDialog(vs.Concat('Did something ', cnt, ' times.'))

but this doesn't work when the variable is not local where it runs. In my particular case it doesn't work if I try to increment it in DoSomething. It rises some error (UnboundLocalError: local variable can't be referenced before assignment). Obviously the variable can't be modified from a subroutine, but I can't turn DoSomething into a function outputting an integer, otherwise it won't fit the required callback syntax expected by ForEachObject

Searching the web I found this. LOL. This comment wins, on my opinion: "We can make Python ask Perl to ask C."

Personal tools
NamespacesFFFF

Variants
Actions
Advanced Search
See Also
Navigation
Toolbox