Difference between revisions of "User:Orso.b.schmid"

From Vectorworks Developer
Jump to: navigation, search
m (encode in py wiki code wrapper)
(the problem of incrementing variables)
Line 1: Line 1:
 
Ciao,
 
Ciao,
  
I am Orso, an Italian Vectorscripter since many years. I feel very comfortable with VS but will now switch over to Python for even more power.
+
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. --[[User:Orso.b.schmid|Orso.b.schmid]] ([[User talk:Orso.b.schmid|talk]]) 08:13, 17 May 2015 (EDT)
 
I will try to share here comments, problems -and solutions- from the point of view of a non-programmer. --[[User:Orso.b.schmid|Orso.b.schmid]] ([[User talk:Orso.b.schmid|talk]]) 08:13, 17 May 2015 (EDT)
  
Line 24: Line 24:
 
months = {1: 'Jan', 2: 'Feb', 3: 'Mar'} # --> {1: 'Jan', 2: 'Feb', 3: 'Mar'}
 
months = {1: 'Jan', 2: 'Feb', 3: 'Mar'} # --> {1: 'Jan', 2: 'Feb', 3: 'Mar'}
 
</code>
 
</code>
 +
 +
== 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:
 +
 +
<code lang="pas">cnt := cnt +1; { variable cnt is incremented }</code>
 +
 +
For example:
 +
<code lang="pas">
 +
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);
 +
</code>
 +
 +
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:
 +
<code lang="py">cnt += 1</code>
 +
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. 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 [[VS:ForEachObject| ForEachObject]]

Revision as of 11:07, 17 May 2015

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

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. 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

Personal tools
NamespacesFFFF

Variants
Actions
Advanced Search
See Also
Navigation
Toolbox