Working with Michelson code

Expanding macros

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

Alias for field number 1

property args

Alias for field number 2

property depth

Alias for field number 0

property is_root

Alias for field number 3

pytezos.michelson.macros.build_pxr_tree(pxr_macro, pxr_annots)pytezos.michelson.macros.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: Optional[List[str]] = 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: ply.lex.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.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: pytezos.michelson.types.base.MichelsonType)[source]
aggregate_lazy_diff(mode='readable')List[dict][source]
attach_context(context: pytezos.context.abstract.AbstractContext)[source]
classmethod create_type(args: List[Union[Type[pytezos.michelson.micheline.Micheline], Any]], annots: Optional[list] = None, **kwargs)Type[pytezos.michelson.sections.parameter.ParameterSection][source]
classmethod execute(stack, stdout: List[str], context: pytezos.context.abstract.AbstractContext)[source]
classmethod from_micheline_value(val_expr)pytezos.michelson.sections.parameter.ParameterSection[source]
classmethod from_parameters(parameters: Dict[str, Any])pytezos.michelson.sections.parameter.ParameterSection[source]
classmethod from_python_object(py_obj)pytezos.michelson.sections.parameter.ParameterSection[source]
classmethod generate_pydoc()str[source]
classmethod list_entrypoints()Dict[str, Type[pytezos.michelson.types.base.MichelsonType]][source]
static match(type_expr)Type[pytezos.michelson.sections.parameter.ParameterSection][source]
merge_lazy_diff(lazy_diff: List[dict])pytezos.michelson.sections.parameter.ParameterSection[source]
prim: Optional[str] = 'parameter'
root_name: str
to_micheline_value(mode='readable', lazy_diff=False)[source]
to_parameters(mode='readable')Dict[str, Any][source]
to_python_object(try_unpack=False, lazy_diff=False)dict[source]

Storage section

class pytezos.michelson.sections.storage.StorageSection(item: pytezos.michelson.types.base.MichelsonType)[source]
aggregate_lazy_diff(mode='readable')List[dict][source]
attach_context(context: pytezos.context.abstract.AbstractContext)[source]
classmethod dummy(context: pytezos.context.abstract.AbstractContext)[source]
classmethod execute(stack, stdout: List[str], context: pytezos.context.abstract.AbstractContext)[source]
classmethod from_micheline_value(val_expr)pytezos.michelson.sections.storage.StorageSection[source]
classmethod from_python_object(py_obj)pytezos.michelson.sections.storage.StorageSection[source]
classmethod generate_pydoc()str[source]
static match(type_expr)Type[pytezos.michelson.sections.storage.StorageSection][source]
merge_lazy_diff(lazy_diff: List[dict])pytezos.michelson.sections.storage.StorageSection[source]
prim: Optional[str] = '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: pytezos.context.abstract.AbstractContext)[source]
static match(code_expr)Type[pytezos.michelson.sections.code.CodeSection][source]
prim: Optional[str] = 'code'

Script wrapper

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

Michelson .tz contract interpreter interface

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

Prepare stack for contract execution

code: Type[pytezos.michelson.sections.code.CodeSection]
static create(sequence: Type[pytezos.michelson.micheline.MichelineSequence])Type[pytezos.michelson.program.MichelsonProgram][source]

Create MichelsonProgram type from micheline

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

Finish contract execution

execute(stack: pytezos.michelson.stack.MichelsonStack, stdout: List[str], context: pytezos.context.impl.ExecutionContext)pytezos.michelson.instructions.base.MichelsonInstruction[source]

Execute contract in interpreter

classmethod instantiate(entrypoint: str, parameter, storage)pytezos.michelson.program.MichelsonProgram[source]
static load(context: pytezos.context.impl.ExecutionContext, with_code=False)Type[pytezos.michelson.program.MichelsonProgram][source]

Create MichelsonProgram type from filled context

static match(expr)Type[pytezos.michelson.program.MichelsonProgram][source]
parameter: Type[pytezos.michelson.sections.parameter.ParameterSection]
storage: Type[pytezos.michelson.sections.storage.StorageSection]
class pytezos.michelson.program.TztMichelsonProgram[source]

Michelson .tzt contract interpreter interface

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

Prepare stack for contract execution

big_maps: Optional[Type[pytezos.michelson.sections.tzt.BigMapsSection]]
code: Type[pytezos.michelson.sections.code.CodeSection]
static create(sequence: Type[pytezos.michelson.micheline.MichelineSequence])Type[pytezos.michelson.program.TztMichelsonProgram][source]

Create TztMichelsonProgram type from micheline

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

Finish contract execution

execute(stack: pytezos.michelson.stack.MichelsonStack, stdout: List[str], context: pytezos.context.impl.ExecutionContext)pytezos.michelson.instructions.base.MichelsonInstruction[source]

Execute contract in interpreter

fill_context(script, context: pytezos.context.impl.ExecutionContext)None[source]
input: Type[pytezos.michelson.sections.tzt.InputSection]
classmethod instantiate()pytezos.michelson.program.TztMichelsonProgram[source]
static load(context: pytezos.context.impl.ExecutionContext, with_code=False)[source]

Create TztMichelsonProgram type from filled context

static match(expr)Type[pytezos.michelson.program.TztMichelsonProgram][source]
output: Type[pytezos.michelson.sections.tzt.OutputSection]
register_bigmaps(stack: pytezos.michelson.stack.MichelsonStack, stdout: List[str], context: pytezos.context.impl.ExecutionContext)None[source]