Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
|
calculate_python_expression [2026/08/31 16:58] hermann old revision restored (2026/08/28 03:14) |
calculate_python_expression [2026/08/31 17:10] (current) hermann |
||
|---|---|---|---|
| Line 8: | Line 8: | ||
| ^ Name ^ Type ^ Description ^ | ^ Name ^ Type ^ Description ^ | ||
| - | | Expression | [[Code Type]] | The expression to run on Python. Written directly as a Code constant using its own raw string syntax — see [[#writing_the_expression_in_ego_script|Writing the expression in EGO Script]] below. | | + | | Expression | [[Code Type]] | The expression to run on Python. Written directly as a Code constant using its own raw string syntax -- see [[#writing_the_expression_in_ego_script|Writing the expression in EGO Script]] below. | |
| ===== Optional Inputs ===== | ===== Optional Inputs ===== | ||
| Line 28: | Line 28: | ||
| ==== Expression inputs ==== | ==== Expression inputs ==== | ||
| - | Data is passed into the expression through hook functors placed inside the container's block — the same verbose-form hook mechanism used by the calculator functors: | + | Data is passed into the expression through hook functors placed inside the container's block -- the same verbose-form hook mechanism used by the calculator functors: |
| * Tables and lookup tables → [[Number Table]] → available in the expression as dinamica.inputs["t1"], dinamica.inputs["t2"], …, dinamica.inputs["t100"] | * Tables and lookup tables → [[Number Table]] → available in the expression as dinamica.inputs["t1"], dinamica.inputs["t2"], …, dinamica.inputs["t100"] | ||
| Line 34: | Line 34: | ||
| * Strings → [[Number String]] → available as dinamica.inputs["s1"], dinamica.inputs["s2"], …, dinamica.inputs["s100"] | * Strings → [[Number String]] → available as dinamica.inputs["s1"], dinamica.inputs["s2"], …, dinamica.inputs["s100"] | ||
| - | Maps cannot be connected — this functor has no cell context. There is no shorthand notation. See [[Calculate Functors|Calculate Functors — Complete Operator Documentation]] for the general hook mechanism and syntax. | + | Maps cannot be connected -- this functor has no cell context. There is no shorthand notation. See [[Calculate Functors|Calculate Functors -- Complete Operator Documentation]] for the general hook mechanism and syntax. |
| - | Every table or lookup table arriving through dinamica.inputs is represented in Python as a list of lists: the first inner list contains the column names (the header row) and every subsequent inner list is a row of data. The header carries plain column names only — no asterisk marking key columns, no type annotation — so which columns were keys in the original table is information Python doesn't receive and can't recover from the input alone. Code that needs that distinction has to be told it separately, for instance by also connecting the key count as its own [[Number Value]] hook. | + | Every table or lookup table arriving through dinamica.inputs is represented in Python as a list of lists: the first inner list contains the column names (the header row) and every subsequent inner list is a row of data. The header carries plain column names only -- no asterisk marking key columns, no type annotation -- so which columns were keys in the original table is information Python doesn't receive and can't recover from the input alone. Code that needs that distinction has to be told it separately, for instance by also connecting the key count as its own [[Number Value]] hook. |
| ==== Expression outputs ==== | ==== Expression outputs ==== | ||
| Line 48: | Line 48: | ||
| </code> | </code> | ||
| - | Tables and lookup tables cannot be assigned directly — they must first be converted using the utilities described below. If a table arriving from an input already carries asterisk markers on its key column names, it can be assigned directly to an output without conversion. | + | Tables and lookup tables cannot be assigned directly -- they must first be converted using the utilities described below. If a table arriving from an input already carries asterisk markers on its key column names, it can be assigned directly to an output without conversion. |
| ==== Retrieving outputs ==== | ==== Retrieving outputs ==== | ||
| Line 84: | Line 84: | ||
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ | ||
| - | | packageName | str | — | Identifies the package. Used as the default for both installPath and loadPath when those are omitted. | | + | | packageName | str | -- | Identifies the package. Used as the default for both installPath and loadPath when those are omitted. | |
| | installPath | str | packageName | What is passed to pip install. Can be a plain name, a version-pinned requirement, a wheel filename or URL, a git+ URL, or a name followed by extra pip flags. | | | installPath | str | packageName | What is passed to pip install. Can be a plain name, a version-pinned requirement, a wheel filename or URL, a git+ URL, or a name followed by extra pip flags. | | ||
| | loadPath | str | packageName | The name used to import the module in Python. | | | loadPath | str | packageName | The name used to import the module in Python. | | ||
| Line 122: | Line 122: | ||
| **The Packages port** | **The Packages port** | ||
| - | The Packages input port takes one package identifier per line, in any form accepted by pip. Every listed package is installed before the expression runs. Unlike dinamica.package(), the port only installs — it does not also import the module, and it does not support specifying a separate install name and import name. The expression must still import each package itself, using its actual importable name (which may differ from the name given to pip). | + | The Packages input port takes one package identifier per line, in any form accepted by pip. Every listed package is installed before the expression runs. Unlike dinamica.package(), the port only installs -- it does not import anything. Importing is left entirely to the expression, using standard Python import syntax and each package's actual importable name, which may differ from the name given to pip. |
| The Packages port: | The Packages port: | ||
| Line 135: | Line 135: | ||
| <code python> | <code python> | ||
| - | import numpy | + | import numpy as np |
| - | import requests | + | from requests import Session |
| - | import torchvision | + | from torchvision import transforms |
| </code> | </code> | ||
| Line 145: | Line 145: | ||
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ | ||
| - | | inputTable | list of lists | — | The table data. First inner list is the header row; subsequent inner lists are data rows. | | + | | inputTable | list of lists | -- | The table data. First inner list is the header row; subsequent inner lists are data rows. | |
| - | | numKeys | int | — | Number of key columns. Key column names will have an asterisk appended in the output. | | + | | numKeys | int | -- | Number of key columns. Key column names will have an asterisk appended in the output. | |
| === dinamica.prepareLookupTable() === | === dinamica.prepareLookupTable() === | ||
| - | Converts a list of lists into a lookup table ready to be assigned to an output. The first inner list must be the header row. Lookup tables are always Real-typed on both key and value sides — there is no String option — so every value in lut must be numeric. | + | Converts a list of lists into a lookup table ready to be assigned to an output. The first inner list must be the header row. Lookup tables are always Real-typed on both key and value sides -- there is no String option -- so every value in lut must be numeric. |
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ | ||
| - | | lut | list of lists | — | The lookup table data. First inner list is the header row; subsequent inner lists are data rows. | | + | | lut | list of lists | -- | The lookup table data. First inner list is the header row; subsequent inner lists are data rows. | |
| === dinamica.toTable() === | === dinamica.toTable() === | ||
| - | Converts several Python data shapes into a valid Dinamica table for output. Column types are inferred the same way as dinamica.prepareTable() — except for a flat list, which always produces a Real-typed lookup table with sequential keys, matching the Real-only rule for lookup tables. | + | Converts several Python data shapes into a valid Dinamica table for output. Column types are inferred the same way as dinamica.prepareTable() -- except for a flat list, which always produces a Real-typed lookup table with sequential keys, matching the Real-only rule for lookup tables. |
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ | ||
| - | | inputTable | list of lists; dict of lists; list of tuples; flat list; pandas.DataFrame; numpy.array | — | The data to convert. A flat list produces a lookup table with sequential keys. A numpy.array must have its header as the first row. | | + | | inputTable | list of lists; dict of lists; list of tuples; flat list; pandas.DataFrame; numpy.array | -- | The data to convert. A flat list produces a lookup table with sequential keys. A numpy.array must have its header as the first row. | |
| - | | numKeys | int | — | Number of key columns. Key column names will have an asterisk appended in the output. Ignored for a flat list, which always produces a lookup table with sequential keys. | | + | | numKeys | int | -- | Number of key columns. Key column names will have an asterisk appended in the output. Ignored for a flat list, which always produces a lookup table with sequential keys. | |
| ==== Examples ==== | ==== Examples ==== | ||
| Line 174: | Line 174: | ||
| </code> | </code> | ||
| - | The print() call is visible in Dinamica EGO's Message Log, shown as a Result-level message. Log levels are ordered Unconditional, Error, Warning, Result, Info, Info2, Debug, Debug2 — messages printed from Python are only shown when the Message Log level is set to Result or a more verbose level; at Unconditional, Error, or Warning they are suppressed. | + | The print() call is visible in Dinamica EGO's Message Log, shown as a Result-level message. Log levels are ordered Unconditional, Error, Warning, Result, Info, Info2, Debug, Debug2 -- messages printed from Python are only shown when the Message Log level is set to Result or a more verbose level; at Unconditional, Error, or Warning they are suppressed. |
| Print the rows of both connected tables to verify their contents: | Print the rows of both connected tables to verify their contents: | ||
| Line 282: | Line 282: | ||
| CalculatePythonExpression | CalculatePythonExpression | ||
| - | |||