User:Orso.b.schmid: Difference between revisions
(expand: colors) |
(variable scope) |
||
Line 11: | Line 11: | ||
! style="width:40%"| Vectorscript | ! style="width:40%"| Vectorscript | ||
! style="width:40%"| Python | ! style="width:40%"| Python | ||
|- | |||
| Assignment operator: | |- style="vertical-align: top;" | ||
| | |||
; Assignment operator: | |||
| <code>:=</code> | | <code>:=</code> | ||
| <code>=</code> | | <code>=</code> | ||
|- | |||
| Empty handle: | |- style="vertical-align: top;" | ||
| | |||
; Empty brakes for functions: don't forget in python the empty brakets for routines without parameters, this rises errors that are so tricky to find. | |||
| If a routine has no parameters, it can be expressed without brakets: | |||
* <code>FSActLayer;</code> | |||
* <code>MySubroutine;</code> | |||
| If a routine has no parameters, it must be nevertheless expressed with brakets: | |||
* <code>vs.FSActLayer()</code> | |||
* <code>MySubroutine()</code> | |||
|- style="vertical-align: top;" | |||
| | |||
; Empty handle: do pay attention also to the variable scope (see below). | |||
| | | | ||
* <code>h <> NIL</code> | * <code>h <> NIL</code> | ||
Line 22: | Line 36: | ||
* <code>h != None</code> | * <code>h != None</code> | ||
* <code>h != vs.Handle() # not inited instance of a handle, how cryptic</code> | * <code>h != vs.Handle() # not inited instance of a handle, how cryptic</code> | ||
|- | |||
| Colors: | |- style="vertical-align: top;" | ||
| | |||
; Colors: | |||
| | | | ||
* Color Index: | * Color Index: | ||
Line 41: | Line 57: | ||
* <code>vs.PenFore((65535, 0, 0)) </code> correct | * <code>vs.PenFore((65535, 0, 0)) </code> correct | ||
* <code>vs.PenFore(65535, 0, 0) </code> fails | * <code>vs.PenFore(65535, 0, 0) </code> fails | ||
|- | |||
| Concatenate text: | |- style="vertical-align: top;" | ||
| | |||
; Variable scope: perhaps the largest source of error for the vectorscripter transitioning to python | |||
| | |||
Global wins over local: | |||
* Variables must be declared | |||
* Subroutines "see" their own variables and those of any parent function/procedure where they are contained. | |||
<code lang="pas"> | |||
{ GLOBAL ACCESS } | |||
{ parent of subroutine "Increment" } | |||
PROCEDURE Main; | |||
VAR | |||
{ good praxis: label globals with "g" } | |||
gIndex, gNum : INTEGER; | |||
{ subroutine } | |||
PROCEDURE Increment; | |||
BEGIN | |||
{ gNum is not defined in this subroutine | |||
the parser goes up to the parent container | |||
until it finds a declaration for the var gNum. | |||
In this case in Main } | |||
gNum := gNum +1; | |||
SysBeep; | |||
END; | |||
BEGIN | |||
gNum := 10; { init } | |||
FOR gIndex := 1 TO 10 DO | |||
Increment; { increments the variable gNum } | |||
AlrtDialog(Concat(gNum)); | |||
{ returns 20 } | |||
END; | |||
Run(Main); | |||
</code> | |||
| | |||
Local wins over global: | |||
* Variables must NOT be declared | |||
* Subroutines create automatically a local instance of any used variable. | |||
<code lang="py"> | |||
# LOCAL ACCESS | |||
# subroutine | |||
def Increment(): | |||
# gNum is not defined in this subroutine | |||
# the parser creates a local instance of the var gNum! | |||
gNum +=1 | |||
vs.SysBeep | |||
gNum = 10 # init | |||
for gIndex in range(1, 10): | |||
Increment | |||
# increments the variable gNum | |||
# but only inside Increment! | |||
vs.AlrtDialog(str(gNum)) | |||
# returns 10! The global var didn't set | |||
</code> | |||
<code lang="py"> | |||
# GLOBAL ACCESS | |||
# subroutine | |||
def Increment(): | |||
# gNum is not defined in this subroutine | |||
# tell the parser that you want to edit gNum global! | |||
global gNum | |||
gNum +=1 | |||
vs.SysBeep() | |||
gNum = 10 # init | |||
# please observe that the range is NOT 1, 10! | |||
for gIndex in range(0, 10): | |||
Increment() | |||
# increments the variable gNum | |||
# but only inside Increment! | |||
vs.AlrtDialog(str(gNum)) | |||
# returns 20 | |||
</code> | |||
|- style="vertical-align: top;" | |||
| | |||
; Concatenate text: | |||
| | | | ||
* <code>Concat(text1, ' ', text2)</code> | * <code>Concat(text1, ' ', text2)</code> | ||
| | | | ||
* <code> text1 + ' ' + text2</code> | * <code> text1 + ' ' + text2</code> | ||
|- | |||
| | |- style="vertical-align: top;" | ||
| | |||
; Python version: | |||
| | | | ||
| <code>import sys | | <code>import sys | ||
Line 54: | Line 155: | ||
vs.Message(repr(ver))</br> | vs.Message(repr(ver))</br> | ||
</code> | </code> | ||
|- | |||
| "import vs" yes or no? | |- style="vertical-align: top;" | ||
| | |||
; "import vs" yes or no? | |||
| | | | ||
| <code>import vs</code> # do I need this? | | <code>import vs</code> # do I need this? | ||
|- | |||
| | |- style="vertical-align: top;" | ||
| | |||
; Caching: some caching prevents your script to reflect changes: | |||
| | | | ||
| varPersistentPythonEngine = 412 { Boolean } | | varPersistentPythonEngine = 412 { Boolean } | ||
In the SDK starting from VW 2014 we can read: | In the SDK starting from VW 2014 we can read: | ||
''When True the Python engine is the same for the execution of all scripts, this solves some issues with Py_Initialize and Py_Finalize. For example, when debugging externally python leaves threas that cause crash if Py_Initialize and Py_Finalize is used for each script call. So, this allows the engine to be preserved between calls, however Vectorworks will delete all custom modules and objects defined in the engine prior each execution.'' | ''When True the Python engine is the same for the execution of all scripts, this solves some issues with Py_Initialize and Py_Finalize. For example, when debugging externally python leaves threas that cause crash if Py_Initialize and Py_Finalize is used for each script call. So, this allows the engine to be preserved between calls, however Vectorworks will delete all custom modules and objects defined in the engine prior each execution.'' | ||
|- | |||
| Encryption: | |- style="vertical-align: top;" | ||
| | |||
; Encryption: | |||
| Whatever .vs or .px file is linked through your includes, will be encrypted upon running the encrypt command. More infos [[VS:Include_Files_and_Encryption| here]]. | | Whatever .vs or .px file is linked through your includes, will be encrypted upon running the encrypt command. More infos [[VS:Include_Files_and_Encryption| here]]. | ||
| Create list of your included files in an xml file. Please read [https://techboard.vectorworks.net/ubbthreads.php?ubb=showflat&Number=197639&Searchpage=1&Main=39809&Words=python&Search=true#Post197639 Vlado on Techboard] | | Create list of your included files in an xml file. Please read [https://techboard.vectorworks.net/ubbthreads.php?ubb=showflat&Number=197639&Searchpage=1&Main=39809&Words=python&Search=true#Post197639 Vlado on Techboard] | ||
Line 85: | Line 192: | ||
del months[2] # --> ['Jan', 'Feb', 'Apr', 'May', 'Jun', 'Jul'] | del months[2] # --> ['Jan', 'Feb', 'Apr', 'May', 'Jun', 'Jul'] | ||
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> |
Revision as of 06:45, 19 May 2015
Ciao,
I am Orso, an Italian Vectorscripter since many years. Some of you might know me from Vectorlab or the comments on the present Developer wiki. 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)
If you add comments, please use the full wiki formatting, easily available clicking on Advanced while on edit mode and don't forget to sign up your comment using --~~~~!
VS <> Py FAQ
Description | Vectorscript | Python |
---|---|---|
|
:=
|
=
|
|
If a routine has no parameters, it can be expressed without brakets:
|
If a routine has no parameters, it must be nevertheless expressed with brakets:
|
|
|
|
|
|
Warning: don't forget the brakets:
|
|
Global wins over local:
{ GLOBAL ACCESS } { parent of subroutine "Increment" } PROCEDURE Main; VAR { good praxis: label globals with "g" } gIndex, gNum : INTEGER; { subroutine } PROCEDURE Increment; BEGIN { gNum is not defined in this subroutine the parser goes up to the parent container until it finds a declaration for the var gNum. In this case in Main } gNum := gNum +1; SysBeep; END; BEGIN gNum := 10; { init } FOR gIndex := 1 TO 10 DO Increment; { increments the variable gNum } AlrtDialog(Concat(gNum)); { returns 20 } END; Run(Main); |
Local wins over global:
# LOCAL ACCESS # subroutine def Increment(): # gNum is not defined in this subroutine # the parser creates a local instance of the var gNum! gNum +=1 vs.SysBeep gNum = 10 # init for gIndex in range(1, 10): Increment # increments the variable gNum # but only inside Increment! vs.AlrtDialog(str(gNum)) # returns 10! The global var didn't set # GLOBAL ACCESS # subroutine def Increment(): # gNum is not defined in this subroutine # tell the parser that you want to edit gNum global! global gNum gNum +=1 vs.SysBeep() gNum = 10 # init # please observe that the range is NOT 1, 10! for gIndex in range(0, 10): Increment() # increments the variable gNum # but only inside Increment! vs.AlrtDialog(str(gNum)) # returns 20 |
|
|
|
|
import sys
| |
|
import vs # do I need this?
| |
|
varPersistentPythonEngine = 412 { Boolean }
In the SDK starting from VW 2014 we can read: When True the Python engine is the same for the execution of all scripts, this solves some issues with Py_Initialize and Py_Finalize. For example, when debugging externally python leaves threas that cause crash if Py_Initialize and Py_Finalize is used for each script call. So, this allows the engine to be preserved between calls, however Vectorworks will delete all custom modules and objects defined in the engine prior each execution. | |
|
Whatever .vs or .px file is linked through your includes, will be encrypted upon running the encrypt command. More infos here. | Create list of your included files in an xml file. Please read Vlado on Techboard |
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] # --> 'Mar' note that the index is 0-based months2 = "Jan, Feb, Mar, Apr, May, Jun, Jul" months2.split(', ') # --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'] use comma and empty space as splitter months.append('Jul') # --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'] append adds an item to a list months.pop() #- -> 'Jul' pop fetches the last item of a list ', sunny '.join(months) # --> ', sunny Jan, sunny Feb, sunny Mar, sunny Apr, sunny May, sunny Jun, sunny 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'}