User:Orso.b.schmid: Difference between revisions

From Vectorworks Developer
Jump to navigation Jump to search
(the problem of incrementing variables)
(add link to reddit)
Line 27: Line 27:
== Increment a var ==
== 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:
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>
<code lang="pas">cnt := cnt +1; { variable cnt is incremented }</code>


For example:
For example:
<code lang="pas">
<code lang="pas">
PROCEDURE Test;
PROCEDURE Test;
Line 52: Line 52:
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:
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>
<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]]
 
For example:
 
<code lang="py">
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.'))
</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 (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 [[VS:ForEachObject| ForEachObject]]
 
Searching the web I found [http://www.reddit.com/r/Python/comments/wbs1o/best_way_to_increment_of_1_in_python this]. LOL. This comment wins, on my opinion: "We can make Python ask Perl to ask C."

Revision as of 16:05, 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

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