User:CBM-c-/VS-Py Rosetta Stone: Difference between revisions
(Create a new page for the VS-Py Rosetta stone) |
m (add link, do they work today?) |
||
Line 7: | Line 7: | ||
Transitioning from Vectorscript Pascal to Vectorscript Python has its challenges, here a Rosetta Stone to help you out with the task. | Transitioning from Vectorscript Pascal to Vectorscript Python has its challenges, here a Rosetta Stone to help you out with the task. | ||
; Note: as of today the wiki doesn't accept external links, which are thus not clickable. | |||
== Set up Python == | |||
== Set up Python (Mac) == | |||
As of this writing, Vectorworks requires Python 3.x. MacOS X before 10.15 ships with Python 2.x, so you might need to install the version 3. | As of this writing, Vectorworks requires Python 3.x. MacOS X before 10.15 ships with Python 2.x, so you might need to install the version 3. | ||
Line 23: | Line 25: | ||
== Set up an editor == | == Set up an editor == | ||
I tried using Aptana but couldn't configure it. Then I found at Computerworks excellent instructions for Visual Studio Code: (German) www.vectorworksforum.eu/topic/14087-entwicklungsumgebung-für-vectorworks-python-plug-ins/ | I tried using Aptana but couldn't configure it. Then I found at Computerworks excellent instructions by Oliver Daus for Visual Studio Code: (German) www.vectorworksforum.eu/topic/14087-entwicklungsumgebung-für-vectorworks-python-plug-ins/ | ||
Below a rough translation until Computerworks writes a proper one in English. | Below a rough translation until Computerworks writes a proper one in English. | ||
* Download '''Visual Studio Code''' | * Download '''Visual Studio Code''' from code.visualstudio.com and install it | ||
* Install the '''Python extension''': launch Visual Studio Code, click on Extensions (shift +cmd + X), click on the search field, enter ms-python, click on '''install Python'''. | * Install the '''Python extension''': launch Visual Studio Code, click on Extensions (shift +cmd + X), click on the search field, enter ms-python, click on '''install Python'''. | ||
Revision as of 06:05, 31 December 2020
VS-Python Rosetta Stone
Transitioning from Vectorscript Pascal to Vectorscript Python has its challenges, here a Rosetta Stone to help you out with the task.
- Note
- as of today the wiki doesn't accept external links, which are thus not clickable.
Set up Python (Mac)
As of this writing, Vectorworks requires Python 3.x. MacOS X before 10.15 ships with Python 2.x, so you might need to install the version 3.
- launch the Terminal
- type: python3 --version; if there is an error, you should install Python 3 and configure it.
- follow the instructions on installpython3.com/mac/.
This document is very complete and leads you step by step through the rather cryptical configurations. During this process you will install XCode and Homebrew through the terminal.
- Warning
- For some reason the XCode wouldn't install on my MacOS X.14. After various hours of fumbling, I gave up and went straight to the official Python 3 installer, which is available at www.python.org/downloads/release/python-391/
After that I proceeded with the configurations as described in installpython3.com/mac/ and all seems well enough.
Set up an editor
I tried using Aptana but couldn't configure it. Then I found at Computerworks excellent instructions by Oliver Daus for Visual Studio Code: (German) www.vectorworksforum.eu/topic/14087-entwicklungsumgebung-für-vectorworks-python-plug-ins/
Below a rough translation until Computerworks writes a proper one in English.
- Download Visual Studio Code from code.visualstudio.com and install it
- Install the Python extension: launch Visual Studio Code, click on Extensions (shift +cmd + X), click on the search field, enter ms-python, click on install Python.
VS - Py
Description | Vectorscript | Python |
---|---|---|
Statement ending: | Semicolon always needed
|
Semicolon needed only for multiple statements on one line:
Error Message: SyntaxError: invalid syntax |
Case sensitivity | Not case sensitive:
|
Case sensitive:
Error Message: AttributeError: 'module' object has no attribute 'vs.ALRTDIALOG' |
Empty brakes for functions
|
Can be without brakets
|
Can't be without brakets
Error Message: - none! be careful! - |
Empty handles
|
|
|
Variable scope
|
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 climbs up parent containers until it finds a declaration for the var gNum. In this case it can be found 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: CORRECT # 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 # GLOBAL ACCESS: WRONG # 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() # no init! for gIndex in range(0, 10): Increment() # increments the variable gNum # but only inside Increment! vs.AlrtDialog(str(gNum)) # rises error Error Message: NameError: global name 'gNum' is not defined |
FOR statements | Runs including last value:
{ runs 3 times! 1, 2 and 3 } FOR i := 1 TO 3 DO AlrtDialog(Concat(i)); |
Runs excluding last value:
# runs 2 times! 1 and 2 for i in range(1, 3): vs.AlrtDialog(str(i)) |
Colors |
|
Warning: don't forget the brakets:
Error Message: - none! be careful! - |
Concatenate text | Supports implicit conversion:
|
Doesn't support implicit conversion:
Error Message: TypeError: unsupported operand type(s) for +: 'int' and 'str' |
Encryption | Whatever .vs or .px file is linked through your includes, will be encrypted upon running the encrypt command. More infos about standard Vectorscript (Pascal) encryptions are here. | Create list of your included files in an xml file.
For encryption in Python there are difficulties. Instructions from Vlado on the Techboard, search for "problems-encrypting-a-python-script" (at the moment we cannot add external links to the present wiki). |
Python version | import sys
| |
"import vs" | import vs # do I need this?
| |
Caching
|
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. |
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'}
Errors
Python Error Messages:
BaseExceptions:
+-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration +-- ArithmeticError | +-- FloatingPointError | +-- OverflowError | +-- ZeroDivisionError +-- AssertionError +-- AttributeError +-- BufferError +-- EOFError +-- ImportError +-- LookupError | +-- IndexError | +-- KeyError +-- MemoryError +-- NameError | +-- UnboundLocalError +-- OSError | +-- BlockingIOError | +-- ChildProcessError | +-- ConnectionError | | +-- BrokenPipeError | | +-- ConnectionAbortedError | | +-- ConnectionRefusedError | | +-- ConnectionResetError | +-- FileExistsError | +-- FileNotFoundError | +-- InterruptedError | +-- IsADirectoryError | +-- NotADirectoryError | +-- PermissionError | +-- ProcessLookupError | +-- TimeoutError +-- ReferenceError +-- RuntimeError | +-- NotImplementedError +-- SyntaxError | +-- IndentationError | +-- TabError +-- SystemError +-- TypeError +-- ValueError | +-- UnicodeError | +-- UnicodeDecodeError | +-- UnicodeEncodeError | +-- UnicodeTranslateError +-- Warning +-- DeprecationWarning +-- PendingDeprecationWarning +-- RuntimeWarning +-- SyntaxWarning +-- UserWarning +-- FutureWarning +-- ImportWarning +-- UnicodeWarning +-- BytesWarning +-- ResourceWarning