pythonutil

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChildNodes

func ChildNodes(n *Node) iter.Seq[*Node]

Direct child nodes in field order, contexts and operators included

func Compile

func Compile(src []byte, filename string) error

Error Python's compiler would raise for the bytes of a file, nil when it compiles: parse errors and the later ones a parser alone misses, such as return outside a function or a nonlocal with no binding; a *SyntaxError naming filename, or a *LimitError

func LauncherInterpreter

func LauncherInterpreter(data []byte) (string, bool)

Interpreter path a Windows console script launcher executable embeds: the loader stub, a "#!<python> -I" line, then a zip archive; false for any other layout

func Literal

func Literal(v any) string

Python source text of a literal value: a Constant value of a tree or a value LiteralEval gives

func LiteralEval

func LiteralEval(src string) (any, error)

Value of a Python literal expression: nil for None, bool, int64, *big.Int past int64, float64, complex128, string, []byte, Ellipsis, []any for a list, Tuple, Set or *Dict

func LiteralStrings

func LiteralStrings(src string) ([]string, bool)

Items of a Python list literal as text, a string item as it is, None items left out and any other item in its literal form, false when src is not a list literal

func Quote

func Quote(s string) string

s as a Python string literal, single quoted unless only double quotes spare an escape, escaped as Python's repr writes it; a byte that is not UTF-8 becomes the \udcXX escape Python's surrogateescape gives it, so the literal encodes back to the same bytes as a file name or argument, as the embedded interpreter receives such a string

func Unparse

func Unparse(n *Node) (s string, err error)

Python source text of a tree or of any node in it, written with the minimal parentheses and the quotes that need fewest escapes

func Walk

func Walk(n *Node) iter.Seq[*Node]

Every node of the tree breadth first, the root first

Types

type Dict

type Dict struct{ Keys, Values []any }

Value of a dict literal, keys in source order, a repeated key keeping its first place and its last value

type Ellipsis

type Ellipsis struct{}

Value of the ... constant

type ErrorKind

type ErrorKind uint8

Category of a syntax error, told apart the way Python reports it

const (
	// Source that breaks the grammar or a compile time rule
	KindSyntax ErrorKind = iota
	// An unexpected, missing or unmatched indent
	KindIndentation
	// Tabs and spaces mixed so the indentation is ambiguous
	KindTab
)

func (ErrorKind) String

func (k ErrorKind) String() string

Words naming the kind: syntax error, indentation error or tab error

type LimitError

type LimitError struct {
	Line, Limit int
}

Source nested deeper than the parser accepts, the guard that keeps hostile input from exhausting the stack

func (*LimitError) Error

func (e *LimitError) Error() string

Limit crossed and the line where it happened

type LiteralError

type LiteralError struct {
	Line int
	Msg  string
}

Expression that parses but is not a literal, or a literal whose set member or dict key cannot be one

func (*LiteralError) Error

func (e *LiteralError) Error() string

Message prefixed with the line of the offending expression when it is known

type Node

type Node struct {
	Type                       string
	Line, Col, EndLine, EndCol int
	// contains filtered or unexported fields
}

One syntax tree node: Type is the class name the Python ast module uses, lines count from 1 and columns are 0-based UTF-8 byte offsets, all zero for a node without a position

func Parse

func Parse(src string) (*Node, error)

Module tree of Python source text, already decoded so a coding declaration is not applied; the error is a *SyntaxError or a *LimitError

func ParseBytes

func ParseBytes(src []byte) (*Node, error)

Module tree of the bytes of a Python file, decoded as its BOM or coding declaration says and as UTF-8 otherwise; the error is a *SyntaxError or a *LimitError

func (*Node) Child

func (n *Node) Child(name string) *Node

Node a field holds, nil when the field is unset or holds no node

func (*Node) Children

func (n *Node) Children(name string) []*Node

Nodes of a list field, nil when the field holds no node list

func (*Node) Get

func (n *Node) Get(name string) any

Value of a field, nil when absent or None; a Constant value is a bool, int64, *big.Int past int64, float64, complex128, string, []byte or Ellipsis

func (*Node) Int

func (n *Node) Int(name string) (int, bool)

Integer of a field such as an import level and whether it is set

func (*Node) Is

func (n *Node) Is(types ...string) bool

Whether the node exists and is of one of the classes

func (*Node) Names

func (n *Node) Names(name string) []string

Identifiers of a list field such as the names of a global statement

func (*Node) Str

func (n *Node) Str(name string) (string, bool)

Identifier or string of a field and whether it is set

type Set

type Set []any

Value of a set literal, its distinct members in source order

type SyntaxError

type SyntaxError struct {
	Kind                             ErrorKind
	Msg                              string
	Line, Column, EndLine, EndColumn int
	Text                             string
	Filename                         string
}

Python source the parser or the compiler rejects, with Python's own message, 1-based character columns and the source line, Line zero when the error has no position

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

Location and message as file:line:column: message, the parts that are not known left out

type Tuple

type Tuple []any

Value of a tuple literal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL