Working with Michelson code

Expanding macros

class pytezos.michelson.macros.PxrNode(depth, annots, args, is_root)
annots

Alias for field number 1

args

Alias for field number 2

depth

Alias for field number 0

is_root

Alias for field number 3

pytezos.michelson.macros.build_pxr_tree(pxr_macro, pxr_annots) PxrNode[source]
pytezos.michelson.macros.dip_n(instr, depth=1)[source]
pytezos.michelson.macros.expand_assert(prim, annots, args) dict[source]
pytezos.michelson.macros.expand_assert_cmpx(prim, annots, args) list[source]
pytezos.michelson.macros.expand_assert_left(prim, annots, args) dict[source]
pytezos.michelson.macros.expand_assert_none(prim, annots, args) dict[source]
pytezos.michelson.macros.expand_assert_right(prim, annots, args) dict[source]
pytezos.michelson.macros.expand_assert_some(prim, annots, args) dict[source]
pytezos.michelson.macros.expand_assert_x(prim, annots, args) list[source]
pytezos.michelson.macros.expand_caxr(prim, annots, args) list[source]
pytezos.michelson.macros.expand_cdxr(prim, annots, args) list[source]
pytezos.michelson.macros.expand_cmpx(prim, annots, args) list[source]
pytezos.michelson.macros.expand_cxr(prim, annots) list[source]
pytezos.michelson.macros.expand_dixp(prim, annots, args) dict[source]
pytezos.michelson.macros.expand_duxp(prim, annots, args) dict[source]
pytezos.michelson.macros.expand_fail(prim, annots, args) list[source]
pytezos.michelson.macros.expand_if_right(prim, annots, args) dict[source]
pytezos.michelson.macros.expand_if_some(prim, annots, args) dict[source]
pytezos.michelson.macros.expand_ifcmpx(prim, annots, args) list[source]
pytezos.michelson.macros.expand_ifx(prim, annots, args) list[source]
pytezos.michelson.macros.expand_macro(prim, annots, args, internal=False)[source]

Expands Michelson macro.

Parameters:
  • prim – macro name

  • annots – annotations (optional)

  • args – arguments (optional)

  • internal – this function is called during another macro expansion

Returns:

Code sequence (Micheline expression)

pytezos.michelson.macros.expand_map_car(prim, annots, args) list[source]
pytezos.michelson.macros.expand_map_caxr(prim, annots, args) list[source]
pytezos.michelson.macros.expand_map_cdr(prim, annots, args) list[source]
pytezos.michelson.macros.expand_map_cdxr(prim, annots, args) list[source]
pytezos.michelson.macros.expand_map_cxr(prim, annots, args)[source]
pytezos.michelson.macros.expand_pxr(prim, annots, args) list[source]
pytezos.michelson.macros.expand_set_car(prim, annots, args) list[source]
pytezos.michelson.macros.expand_set_caxr(prim, annots, args) list[source]
pytezos.michelson.macros.expand_set_cdr(prim, annots, args) list[source]
pytezos.michelson.macros.expand_set_cdxr(prim, annots, args) list[source]
pytezos.michelson.macros.expand_set_cxr(prim, annots)[source]
pytezos.michelson.macros.expand_unpxr(prim, annots, args) list[source]
pytezos.michelson.macros.expr(**kwargs) dict[source]
pytezos.michelson.macros.get_field_annots(annots)[source]
pytezos.michelson.macros.get_map_cxr_annots(annots) Tuple[str, list][source]
pytezos.michelson.macros.get_var_annots(annots)[source]
pytezos.michelson.macros.macro(regexp)[source]
pytezos.michelson.macros.seq(instr=None) list[source]
pytezos.michelson.macros.skip_nones(array)[source]
pytezos.michelson.macros.traverse_pxr_tree(prim, annots, produce)[source]

Parsing AST

class pytezos.michelson.parse.MichelsonParser(debug=False, write_tables=False, extra_primitives: List[str] | None = None)[source]

Customizable Michelson parser

p_annot(p)[source]

annot : ANNOT

p_annots(p)[source]

annots : annot | empty

p_annots_list(p)[source]

annots : annots annot

p_arg_byte(p)[source]

arg : BYTE

p_arg_group(p)[source]

arg : LEFT_PAREN expr RIGHT_PAREN

p_arg_int(p)[source]

arg : INT

p_arg_prim(p)[source]

arg : PRIM

p_arg_str(p)[source]

arg : STR

p_arg_subseq(p)[source]

arg : LEFT_CURLY instr RIGHT_CURLY

p_args(p)[source]

args : arg | empty

p_args_list(p)[source]

args : args arg

p_empty(p)[source]

empty :

p_error(p)[source]
p_expr(p)[source]

expr : PRIM annots args

p_instr(p)[source]

instr : expr | empty

p_instr_byte(p)[source]

instr : BYTE

p_instr_int(p)[source]

instr : INT

p_instr_list(p)[source]

instr : instr SEMI instr

p_instr_str(p)[source]

instr : STR

p_instr_subseq(p)[source]

instr : LEFT_CURLY instr RIGHT_CURLY

parse(code)[source]

Parse Michelson source.

Parameters:

code – Michelson source

Returns:

Micheline expression

tokens = ('INT', 'BYTE', 'STR', 'ANNOT', 'PRIM', 'LEFT_CURLY', 'RIGHT_CURLY', 'LEFT_PAREN', 'RIGHT_PAREN', 'SEMI')
exception pytezos.michelson.parse.MichelsonParserError(token: LexToken, message=None)[source]
format_stdout() str[source]
class pytezos.michelson.parse.Sequence(iterable=(), /)[source]
class pytezos.michelson.parse.SimpleMichelsonLexer[source]
t_ANNOT = '[:@%]+([_0-9a-zA-Z\\.]*)?'
t_BYTE = '0x[A-Fa-f0-9]*'
t_INT = '-?[0-9]+'
t_LEFT_CURLY = '\\{'
t_LEFT_PAREN = '\\('
t_PRIM = '[A-Za-z][A-Za-z0-9_]+'
t_RIGHT_CURLY = '\\}'
t_RIGHT_PAREN = '\\)'
t_SEMI = ';'
t_STR = '\\"(\\\\.|[^\\"])*\\"'
t_error(t)[source]
t_ignore = ' \t\r\n\x0c'
t_ignore_COMMENT = '#[^\\n]*'
t_ignore_MULTI_COMMENT = '/\\*[^*]*\\*/'
tokens = ('INT', 'BYTE', 'STR', 'ANNOT', 'PRIM', 'LEFT_CURLY', 'RIGHT_CURLY', 'LEFT_PAREN', 'RIGHT_PAREN', 'SEMI')
pytezos.michelson.parse.doc(docstring)[source]
pytezos.michelson.parse.michelson_to_micheline(data, parser=None)[source]

Converts Michelson source text into a Micheline expression.

Parameters:
  • data – Michelson string

  • parser – custom Michelson parser (optional)

Returns:

Micheline expression

Code formatter

exception pytezos.michelson.format.MichelsonFormatterError[source]
pytezos.michelson.format.format_timestamp(timestamp: int) str[source]

Format unix timestamp.

Parameters:

timestamp – Unix timestamp (seconds)

pytezos.michelson.format.micheline_to_michelson(data, inline=False, wrap=False) str[source]

Converts micheline expression into formatted Michelson source.

Parameters:
  • data – Micheline expression

  • inline – produce single line, used for tezos-client arguments (False by default)

  • wrap – ensure expression is wrapped in brackets

Parameter section

class pytezos.michelson.sections.parameter.ParameterSection(item: MichelsonType)[source]
aggregate_lazy_diff(mode='readable') List[dict][source]
attach_context(context: AbstractContext)[source]
classmethod create_type(args: List[Type[Micheline] | Any], annots: list | None = None, **kwargs) Type[ParameterSection][source]
classmethod execute(stack, stdout: List[str], context: AbstractContext)[source]
classmethod from_micheline_value(val_expr) ParameterSection[source]
classmethod from_parameters(parameters: Dict[str, Any]) ParameterSection[source]
classmethod from_python_object(py_obj) ParameterSection[source]
classmethod generate_pydoc() str[source]
classmethod list_entrypoints() Dict[str, Type[MichelsonType]][source]
static match(type_expr) Type[ParameterSection][source]
merge_lazy_diff(lazy_diff: List[dict]) ParameterSection[source]
prim: str | None = 'parameter'
root_name: str
to_micheline_value(mode='readable', lazy_diff=None)[source]
to_parameters(mode='readable') Dict[str, Any][source]
to_python_object(try_unpack=False, lazy_diff=None) dict[source]

Storage section

class pytezos.michelson.sections.storage.StorageSection(item: MichelsonType)[source]
aggregate_lazy_diff(mode='readable') List[dict][source]
attach_context(context: AbstractContext)[source]
classmethod dummy(context: AbstractContext)[source]
classmethod execute(stack, stdout: List[str], context: AbstractContext)[source]
classmethod from_micheline_value(val_expr) StorageSection[source]
classmethod from_python_object(py_obj) StorageSection[source]
classmethod generate_pydoc() str[source]
static match(type_expr) Type[StorageSection][source]
merge_lazy_diff(lazy_diff: List[dict]) StorageSection[source]
prim: str | None = 'storage'
to_micheline_value(mode='readable', lazy_diff=False)[source]
to_python_object(try_unpack=False, lazy_diff=False)[source]

Code section

class pytezos.michelson.sections.code.CodeSection[source]
classmethod execute(stack, stdout: List[str], context: AbstractContext)[source]
static match(code_expr) Type[CodeSection][source]
prim: str | None = 'code'

Script wrapper

class pytezos.michelson.program.MichelsonProgram(name: str, parameter: ParameterSection, storage: StorageSection)[source]

Michelson .tz contract interpreter interface

classmethod as_micheline_expr() List[Dict[str, Any]][source]
begin(stack: MichelsonStack, stdout: List[str], context: ExecutionContext) None[source]

Prepare stack for contract execution

code: Type[CodeSection]
static create(sequence: Type[MichelineSequence]) Type[MichelsonProgram][source]

Create MichelsonProgram type from micheline

end(stack: MichelsonStack, stdout: List[str], output_mode='readable') Tuple[List[dict], Any, List[dict], PairType][source]

Finish contract execution

execute(stack: MichelsonStack, stdout: List[str], context: ExecutionContext) MichelsonInstruction[source]

Execute contract in interpreter

execute_view(stack: MichelsonStack, stdout: List[str], context: ExecutionContext)[source]

Execute view in interpreter

classmethod get_view(name: str) Type[ViewSection][source]
classmethod instantiate(entrypoint: str, parameter, storage) MichelsonProgram[source]
classmethod instantiate_view(name: str, parameter, storage) MichelsonProgram[source]
static load(context: ExecutionContext, with_code=False) Type[MichelsonProgram][source]

Create MichelsonProgram type from filled context

static match(expr) Type[MichelsonProgram][source]
parameter: Type[ParameterSection]
ret(stack: MichelsonStack, stdout: List[str], output_mode='readable') MichelsonType[source]
storage: Type[StorageSection]
views: List[Type[ViewSection]]
class pytezos.michelson.program.TztMichelsonProgram[source]

Michelson .tzt contract interpreter interface

classmethod as_micheline_expr() List[Dict[str, Any]][source]
begin(stack: MichelsonStack, stdout: List[str], context: ExecutionContext) None[source]

Prepare stack for contract execution

big_maps: Type[BigMapsSection] | None
code: Type[CodeSection]
static create(sequence: Type[MichelineSequence]) Type[TztMichelsonProgram][source]

Create TztMichelsonProgram type from micheline

end(stack: MichelsonStack, stdout: List[str], context: ExecutionContext) None[source]

Finish contract execution

execute(stack: MichelsonStack, stdout: List[str], context: ExecutionContext) MichelsonInstruction[source]

Execute contract in interpreter

fill_context(script, context: ExecutionContext) None[source]
input: Type[InputSection]
classmethod instantiate() TztMichelsonProgram[source]
static load(context: ExecutionContext, with_code=False)[source]

Create TztMichelsonProgram type from filled context

static match(expr) Type[TztMichelsonProgram][source]
output: Type[OutputSection]
register_bigmaps(stack: MichelsonStack, stdout: List[str], context: ExecutionContext) None[source]