sasctl.utils.pymas package#
Submodules#
sasctl.utils.pymas.core module#
Contains utilities for wrapping Python models in DS2 for publishing.
- class sasctl.utils.pymas.core.PyMAS(target_function, variables, python_source, return_code=None, return_msg=None, func_prefix=None, **kwargs)[source]#
Bases:
object
- Parameters:
- target_functionstr
The Python function to be executed.
- variableslist of DS2Variable
The input/ouput variables be declared in the module.
- python_sourcestr
Additional Python code to be executed during setup.
- return_codebool
Deprecated. Whether the DS2-generated return code should be included.
- return_msgbool
Deprecated. Whether the DS2-generated return message should be included.
- kwargsany
Passed to
build_wrapper_function()
.
Methods
score_code
([input_table, output_table, ...])Generate DS2 score code
- score_code(input_table=None, output_table=None, columns=None, dest='MAS')[source]#
Generate DS2 score code
- Parameters:
- input_tablestr
The name of the table to execute the function against
- output_tablestr
The name of the table where execution results will be written
- columnslist of str
Names of the columns from table that will be passed to func
- deststr {‘MAS’, ‘EP’, ‘CAS’, ‘Python’}
Specifies the publishing destination for the score code to ensure that compatible code is generated.
- Returns:
- str
Score code
Changed in version 1.4: Added dest=’Python’ option
- sasctl.utils.pymas.core.build_wrapper_function(func, variables, array_input, name='wrapper', setup=None, return_msg=None)[source]#
Wraps a function to ensure compatibility when called by PyMAS.
PyMAS has strict expectations regarding the format of any function called directly by PyMAS. Isolating the desired function inside a wrapping function provides a simple way to ensure that functions called by PyMAS are compliant.
- Parameters:
- funcfunction or str
Function name or an instance of Function which will be wrapped
- variableslist of DS2Variable
- array_inputbool
Whether variables should be combined into a single array before passing to func
- namestr, optional
Name of the generated wrapper function. Defaults to ‘wrapper’.
- setupiterable
Python source code lines to be executed during package setup
- return_msgbool, optional
Deprecated. Whether error messages will be captured and returned as an additional output variable. Defaults to True.
- Returns:
- str
the Python definition for the wrapper function.
Notes
The format for the `# Output: ` is very strict. It must be exactly “# Output: <var>, <var>”. Any changes to spelling, capitalization, punctuation, or spacing will result in an error when the DS2 code is executed.
Changed in version 1.5: Added name parameter.
- sasctl.utils.pymas.core.from_inline(func, input_types=None, array_input=False, return_code=None, return_message=None)[source]#
Creates a PyMAS wrapper to execute the inline python function.
- Parameters:
- funcfunction
A Python function object to be used
- input_typeslist of type, optional
The expected type for each input value of func. Can be ommitted if func includes type hints.
- array_inputbool
Whether the function inputs should be treated as an array instead of individual parameters
- return_codebool
Deprecated. Whether the DS2-generated return code should be included
- return_messagebool
Deprecated. Whether the DS2-generated return message should be included
- Returns:
- PyMAS
Generated DS2 code which can be executed in a SAS scoring environment
Changed in version 1.5: Return code and message are disabled by default.
- sasctl.utils.pymas.core.from_pickle(file, func_name=None, input_types=None, array_input=False, return_code=None, return_message=None)[source]#
Create a deployable DS2 package from a Python pickle file.
- Parameters:
- filestr or bytes or file_like
Pickled object to use. String is assumed to be a path to a picked file, file_like is assumed to be an open file handle to a pickle object, and bytes is assumed to be the raw pickled bytes.
- func_namestr
Name of the target function to call
- input_typesDataFrame, type, list of type, or dict of str: type, optional
The expected type for each input value of the target function. Can be omitted if target function includes type hints. If a DataFrame is provided, the columns will be inspected to determine type information. If a single type is provided, all columns will be assumed to be that type, otherwise a list of column types or a dictionary of column_name: type may be provided.
- array_inputbool
Whether the function inputs should be treated as an array instead of individual parameters
- return_codebool
Deprecated. Whether the DS2-generated return code should be included
- return_messagebool
Deprecated. Whether the DS2-generated return message should be included
- Returns:
- PyMAS
Generated DS2 code which can be executed in a SAS scoring environment
Changed in version 1.5: Return code and message are disabled by default.
- sasctl.utils.pymas.core.from_python_file(file, func_name=None, input_types=None, array_input=False, return_code=None, return_message=None)[source]#
Creates a PyMAS wrapper to execute a function defined in an external .py file.
- Parameters:
- filestr
The path to a python source code file
- func_namestr
Name of the target function to call
- input_typeslist of type, optional
The expected type for each input value of the target function. Can be ommitted if target function includes type hints.
- array_inputbool
Whether the function inputs should be treated as an array instead of individual parameters
- return_codebool
Deprecated Whether the DS2-generated return code should be included
- return_messagebool
Deprecated Whether the DS2-generated return message should be included
- Returns:
- PyMAS
Generated DS2 code which can be executed in a SAS scoring environment
Changed in version 1.5: Return code and message are disabled by default.
sasctl.utils.pymas.ds2 module#
The ds2 module contains classes representing DS2 code constructs.
- class sasctl.utils.pymas.ds2.DS2BaseMethod(name, variables, body=None)[source]#
Bases:
object
- Attributes:
- name
Methods
code
- property name#
- class sasctl.utils.pymas.ds2.DS2BasePackage(code=None)[source]#
Bases:
object
Defines a DS2 package.
- Parameters:
- codestr
Any code to include in the body of the package.
- Attributes:
- methodslist
A collection of
DS2BaseMethod
instances that will be included in the package definition.- .. versionadded:: 1.5
Methods
code
()Get the DS2 code for the package.
- property id#
Unique identifier generated for the package.
- property name#
Unique name generated for the package.
- sasctl.utils.pymas.ds2.DS2Method(variables, code, target='wrapper')[source]#
Deprecated since version 1.5.
- sasctl.utils.pymas.ds2.DS2Package(variables, code=None, return_code=True, return_message=True, target=None)[source]#
Deprecated since version 1.5: Use DS2PyMASPackage instead.
- class sasctl.utils.pymas.ds2.DS2PyMASMethod(name, variables, python_code, return_code=None, return_message=None, target='wrapper', method_name='score')[source]#
Bases:
DS2BaseMethod
A DS2 method that builds a PyMAS object.
- Parameters:
- namestr
Name of Python model to define
- variables
- python_code
- return_code
- return_message
- target
- method_name
- Attributes:
- name
Methods
code
- class sasctl.utils.pymas.ds2.DS2PyMASPackage(code=None)[source]#
Bases:
DS2BasePackage
A DS2 package that uses PyMAS to invoke Python code.
- Parameters:
- code
- .. versionadded:: 1.5
- Attributes:
id
Unique identifier generated for the package.
name
Unique name generated for the package.
Methods
add_method
(name, target, variables)Add a DS2 method that calls a Python function defined by the package.
code
()Get the DS2 code for the package.
- add_method(name, target, variables)[source]#
Add a DS2 method that calls a Python function defined by the package.
- Parameters:
- namestr
Name of the DS2 method to create.
- targetstr
Name of the Python method to call
- variableslist of
DS2Variable
List of input and output variables for the method.
- Returns:
- None
- sasctl.utils.pymas.ds2.DS2ScoreMethod(variables, return_code=True, return_message=True, target='wrapper')[source]#
Deprecated since version 1.5.
- class sasctl.utils.pymas.ds2.DS2Thread(variables, table, column_names=None, return_code=True, return_message=True, package=None, method=None)[source]#
Bases:
object
- Attributes:
- id
- name
- property id#
- property name#
- class sasctl.utils.pymas.ds2.DS2Variable(*args, **kwargs)[source]#
Bases:
Ds2Variable
- Attributes:
- is_array
- size
Methods
DS2 variable declaration statement.
DS2 parameter syntax for method signatures.
count
(value, /)Return number of occurrences of value.
index
(value[, start, stop])Return first index of value.
pymas_statement
([python_var_name])Returns appropriate PyMAS get/set statements to move values between a Python variable and the DS2 variable.
as_model_metadata
- DS2_TYPE_TO_VIYA = {'char': 'string', 'double': 'decimal', 'varchar': 'string'}#
- PY_TYPE_TO_DS2 = {'double': 'double', 'double32': 'double', 'double64': 'double', 'float': 'double', 'float32': 'double', 'float64': 'double', 'int': 'integer', 'int32': 'integer', 'int64': 'integer', 'integer': 'integer', 'integer32': 'integer', 'integer64': 'integer', 'str': 'char', 'string': 'char', 'uint16': 'integer', 'uint32': 'integer', 'uint64': 'integer', 'uint8': 'integer', 'varchar': 'char'}#
- property is_array#
- pymas_statement(python_var_name=None)[source]#
Returns appropriate PyMAS get/set statements to move values between a Python variable and the DS2 variable.
- Parameters:
- python_var_namestr
Python variable name.
- Returns:
- property size#
sasctl.utils.pymas.python module#
- sasctl.utils.pymas.python.ds2_variables(input, output_vars=False, names=None)[source]#
Generate a collection of DS2Variable instances corresponding to the input
- Parameters:
- inputfunction or OrderedDict<string, tuple> or Pandas DataFrame or Numpy or OrderedDict<string, type>
a function or mapping parameter names to (type, is_output)
- output_varsbool
Whether or not to treat all variables from input as output variables
- nameslist of str
List of variable names to used. If a single string is specified it will be used as a prefix and variable names in the format “prefixNN” will be generated.
- Returns:
- list<DS2Variable>
Examples
>>> ds2_variables(OrderedDict(a=int, c=float)) [DS2Variable(name='a', type='integer', out=False), DS2Variable(name='c', type='double', out=False)]
>>> ds2_variables({'x': (float, True)}) [DS2Variable(name='x', type='double', out=True)]
Changed in version 1.5: names parameter added.
- sasctl.utils.pymas.python.parse_type_hints(func, skip_var='self')[source]#
Attempt to discern types for the input and output variable(s).
DS2 is a strongly-typed language but Python is not. Need to determine the types for input/output variables so they can be correctly mapped to DS2 types.
- Parameters:
- funcfunction
the object to inspect for parameters
- skip_varstr
name of the variable assumed to be the instance reference, if any. Will be ignored during parameter parsing.
- Returns:
- paramsOrderedDict
dictionary of str : (str, bool) mapping param_name : (param_type, is_return_val)
- Raises:
- ValueError
If the type of one or more variables could not be determined.