Script Interface

Module: polygraphy.tools.script

safe(base_str, *args, **kwargs)[source]

Indicates a string is safe to use and will not compromise security.

NOTE: The caller is reponsible for checking that the string is actually safe. This function serves only to mark the string as such.

Can work with format strings as well. For example:

>>> safe("{} is my name", "polygraphy")
"'polygraphy' is my name"
inline(inp)[source]

Marks a safe string as being inline when used with other Script APIs. Non-inlined strings will remain strings when used with any of these APIs. For example:

>>> make_invocable("print", safe("example"))
"print('example')"

>>> make_invocable("print", inline(safe("example")))
"print(example)"
Parameters:

inp (Script.String) – The safe string to inline.

make_invocable(type_str, *args, **kwargs)[source]

Creates a string representation that will invoke the specified object, with the specified arguments.

Parameters:
  • type_str (str) – A string representing the object that should be invoked.

  • args – Arguments to pass along to the object. If a keyword argument is set to None, it will be omitted.

  • kwargs – Arguments to pass along to the object. If a keyword argument is set to None, it will be omitted.

Returns:

A string representation that invokes the object specified.

Return type:

str

For example:

>>> make_invocable("MyClass", 0, 1, last=3)
"MyClass(0, 1, last=3)"

>>> make_invocable("my_func", 0, 1, last=None)
"my_func(0, 1)"
make_invocable_if_nondefault(type_str, *args, **kwargs)[source]

Similar to make_invocable, but will return None if all arguments are None.

For example:

>>> make_invocable_if_nondefault("MyClass", 0, 1, last=3)
"MyClass(0, 1, last=3)"

>>> make_invocable_if_nondefault("my_func", None, None, last=None)
None
make_invocable_if_nondefault_kwargs(type_str, *args, **kwargs)[source]

Similar to make_invocable_if_nondefault, but will return None if all keyword arguments are None, even if positional arguments are not.

For example:

>>> make_invocable_if_nondefault("MyClass", 0, 1, last=3)
"MyClass(0, 1, last=3)"

>>> make_invocable_if_nondefault("my_func", 0, 1, last=None)
None
class Script(summary=None, always_create_runners=True)[source]

Bases: object

Represents a Python script that uses the Polygraphy API.

Parameters:
  • summary (str) – A summary of what the script does, which will be included in the script as a comment.

  • always_create_runners (bool) – Whether to create the list of runners even if it would be empty.

class String(s, safe=False, inline=False)[source]

Bases: object

Represents a string that has passed Polygraphy’s security checks.

unwrap()[source]

Returns the underlying string object.

Returns:

str

add_import(imports, frm=None, imp_as=None)[source]

Adds imports to this script.

For example:

script.add_import("numpy") # Equivalent to: import numpy
script.add_import("numpy", imp_as="np") # Equivalent to: import numpy as np
script.add_import("TrtRunner", frm="polygraphy.backend.trt") # Equivalent to: from polygraphy.backend.trt import TrtRunner
Parameters:
  • imports (Union[str, List[str]]) – Object/submodule or list of objects/submodules to import.

  • frm (str) – Module from which to import.

  • imp_as (str) – Name to import as. When this is specified, imports must be a string and not a list.

set_data_loader(data_loader_str)[source]

Adds a data loader to this script, overwriting any previous data loader.

Parameters:

data_loader_str (str) – A string constructing the data loader.

Returns:

The name of the data loader in the script, or None if the provided data loader is empty.

Return type:

str

add_loader(loader_str, loader_id)[source]

Adds a loader to the script. If the loader is a duplicate, returns the existing loader instead.

Parameters:
  • loader_str (str) – A string constructing the loader. For security reasons, this must be generated using make_invocable or make_invocable_if_nondefault.

  • loader_id (str) – A short human-readable identifier for the loader.

Returns:

The name of the loader added.

Return type:

str

add_runner(runner_str)[source]

Adds a runner to the script.

Parameters:

runner_str (str) – A string constructing the runner. For example, this may be generated by make_invocable.

append_preimport(line)[source]

Append a line to the pre-import prefix of the script.

Parameters:

line (str) – The line to append.

append_suffix(line)[source]

Append a line to the suffix of the script

Parameters:

line (str) – The line to append.

save(dest)[source]

Save this script to the specified destination.

Parameters:

dest (file-like) – A file-like object that defines write(), flush(), isatty, and has a name attribute.