dir()
e inspectapi
)
EcoStruxure Machine Expert fornisce una API Python utilizzabile per script EcoStruxure Machine Expert. Quando si scrivono script Python, spesso sono necessari solo nomi di funzioni o l'elenco dei parametri richiesti e i relativi nomi. Spesso sono inoltre disponibili le funzioni disponibili di un oggetto restituito da una funzione API chiamata. Le seguenti funzioni consentono di esplorare la API EcoStruxure Machine Expert Python per assistenza durante lo sviluppo negli script Python.
dir(…)
integrata di Python
Senza argomenti, questa funzione restituisce l'elenco dei nomi nell'ambito corrente locale. Con un argomento, questa funzione tenta di restituire un elenco di attributi validi per tale oggetto.
Il meccanismo predefinito dir()
di Python si comporta in modo diverso con differenti tipi di oggetti, in quanto tenta di produrre quello più rilevante, invece di completare le informazioni:
Se l'oggetto è di tipo modulo, l'elenco contiene i nomi degli attributi del modulo.
Se l'oggetto è un oggetto classe o tipo, l'elenco contiene i nomi degli attributi e, ricorrentemente degli attributi delle relative basi.
In alternativa, l'elenco contiene i nomi degli attributi dell'oggetto, i nomi degli attributi della propria classe e ricorrentemente degli attributi delle classi di base.
Esempio (in LogicBuilderShell.exe):
>>> import struct
>>> dir() # show the names in the module namespace
['__builtins__', '__doc__', '__name__', 'struct']
>>> dir(struct) # show the names in the struct module
['Struct', '__builtins__', '__doc__', '__file__', '__name__',
'__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
'unpack', 'unpack_from']
>>> class Shape(object):
def __dir__(self):
return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'perimeter', 'location']
inspectapi
Poiché la funzione dir()
non elenca tutti gli elementi dell'API EcoStruxure Machine Expert Python, sono fornite altre funzioni per visualizzarli per gli sviluppatori di script Python.
inspectapi
è un modulo Python caricato automaticamente durante l'avvio di LogicBuilderShell.exe o della vista . Di conseguenza, una variabile/un modulo inspectapi
è disponibile nell'ambito corrente. Tale variabile è inoltre elencata eseguendo la funzione dir()
.
La variabile/il modulo inspectapi
fornisce le funzioni seguenti:
Nome della funzione |
Parametri |
Descrizione |
---|---|---|
|
(facoltativo) |
Questa funzione fornisce un elenco dei membri disponibili dell'API EcoStruxure Machine Expert Python che è possibile chiamare. Sono elencati campi, proprietà, eventi e metodi. |
|
Questa funzione fornisce un elenco di eventi disponibili dell'API EcoStruxure Machine Expert Python che è possibile chiamare. |
|
|
||
|
||
|
# To see the API of the inspect api itself
inspectapi.dir(inspectapi)
Members of type 'InspectAPI'
Methods:
dir (obj, verbose = False)
dir_events (obj, verbose = False)
dir_fields (obj, verbose = False)
dir_methods (obj, verbose = False)
dir_properties (obj, verbose = False)
# To see the API of projects variable
inspectapi.dir(projects)
Members of type 'ScriptProjects'
Properties:
[get] all
[get] primary
Methods:
convert (stPath, stOutputPath, converter, bPrimary)
convert (stPath, stOutputPath, converterGuid, bPrimary)
create (stPath, bPrimary)
get_all ()
get_by_path (stPath)
get_primary ()
open (stPath, stPassword, bPrimary)
open (stPath, encryption_password, session_user, session_password, bPrimary)
open_archive (stArchiveFile, stProjectPath, bOverwrite, stPassword)
open_archive (stArchiveFile, stProjectPath, bOverwrite, encryption_password, session_user, session_password)
# open a project
proj = projects.open("c:\\temp\\MyScript.py ")
# To see API of primary project
inspectapi.dir(projects.primary)
Members of type 'ScriptProject'
Properties:
[set] active_application
[get] dirty
[get] handle
[get] has_library_manager
[get] has_project_info
[get] is_root
[get] library
[get] path
[get] primary
[get] project
[get] svn
[get] user_management
Methods:
add (stName, id, stModuleId)
add (stName, iType, stId, stVersion, stModuleId)
check_all_pool_objects ()
clean_all ()
close ()
compare_to (projectFile)
compare_to (projectFile, ignoreWhiteSpace, ignoreComments, ignoreProperties)
...
logout ()
save ()
save_archive (stArchiveFile)
save_archive (stPath, additional_categories)
save_archive (stPath, comment, additional_categories)
save_archive (stPath, additional_files, additional_categories)
....
save_as (stPath, stPassword)
save_as_compiled_library (destination_name)
set_active_application (value)
update ()
# search objects by name (e.g. a device with name SERCOSIII)
objs = projects.primary.find("SERCOSIII", True)
# extract object at index 0 from returned list (if found).
theObj = obj[0]
# see the available API of the found object SERCOSIII
inspectapi.dir(theObj)
Members of type 'ScriptObject'
Properties:
[get] connectors
[get] device_parameters
[get] embedded_object_types
[get] guid
[get] handle
[get] has_textual_declaration
[get] has_textual_implementation
[get] index
[get] is_application
[get] is_device
[get] is_explicit_connector
[get] is_folder
[get] is_libman
[get] is_project_info
[get] is_root
[get] is_task
[get] parent
[get] project
[get] svn
[get] type
Methods:
add (stName, id, stModuleId)
add (stName, iType, stId, stVersion, stModuleId)
allowed_interfaces_at (index)
can_convert (targetDeviceId, targetModuleId = null)
can_convert (targetType, targetId, targetVersion, targetModuleId = null)
convert (targetDeviceId, targetModuleId = null)
convert (targetType, targetId, targetVersion, targetModuleId = null)
create_folder (stFolderName)
disable ()
enable ()
export_native (destination, includeChildren, profileName, reporter)
export_xml (reporter, stPath, bRecursive)
export_xml (reporter, stPath, bRecursive, bExportFolderStructure)
export_xml (reporter, stPath, bRecursive, bExportFolderStructure, bPlainText)
export_xml (stPath, bRecursive, bExportFolderStructure, bPlainText)
find (namePath)
find (stName, recursive)
get_address ()
get_all_parameters ()
...
move (newParent, nNewIndex)
plug (stName, id, stModuleId)
plug (stName, iType, stId, stVersion, stModuleId)
reboot_plc ()
remove ()
rename (stNewName)
reset_diagnosis_messages ()
set_communication_address (address)
set_gateway_and_address (stGateway, stAddress)
set_gateway_and_address (gateway, stAddress)
set_parameter (parameter, value)
set_parameter_iec_address (identifier, connectorId, iecAddress)
set_parameter_io_variable_mapping (identifier, connectorId, variable, createVariable = False)
set_parameter_io_variable_mapping (identifier, connectorId, subElementIndex, variable, createVariable = False)
set_simulation_mode (bSimulation)
unplug ()
update (id, stModuleId)
update (iType, stId, stVersion, stModuleId)