python

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0

README

Python code builder

The go.osspkg.com/gogen/python package builds Python 3.10+ source from composable tokens. Package-level constructors create expressions and statement headers; fluent *Tokens methods append more tokens. Tokens.Render and package-level python.Render write readable source directly without formatting or interpreter validation.

go get go.osspkg.com/gogen/python

Build a module

package main

import (
    "bytes"
    "fmt"

    py "go.osspkg.com/gogen/python"
)

func main() {
    file := py.FromImport("dataclasses", py.ID("dataclass")).Line().Join(
        py.Decorator(py.ID("dataclass")),
        py.Class("User").Block(
            py.ID("name").Colon().String(),
            py.ID("active").Colon().Bool().Op("=").True(),
        ),
    ).Line().Join(
        py.AsyncDef("load_user").Bracket(py.ID("user_id").Colon().Int()).Arrow().TypeUnion(py.ID("User"), py.NoneValue()).Block(
            py.If(py.ID("user_id").Op("<").Raw("0")).Block(py.Return().NoneValue()),
            py.Return().Await().ID("repository").Selector("load").Call(py.ID("user_id")),
        ),
    )

    var source bytes.Buffer
    if err := py.Render(&source, file); err != nil {
        panic(err)
    }
    fmt.Print(source.String())
}

The rendered source uses four spaces for each suite level:

from dataclasses import dataclass
@dataclass
class User:
    name: str
    active: bool = True

async def load_user(user_id: int) -> User | None:
    if user_id < 0:
        return None
    return await repository.load(user_id)

Line() explicitly separates top-level statements. Nested suite builders add their own line breaks and indentation. Empty suites render as pass.

Declarations and control flow

Use Import, ImportAs, and FromImport for imports. Def, AsyncDef, and Class return headers that can be extended with Bracket, Colon, Arrow, and Block. Decorator emits a decorator on its own line. Control headers include If, Elif, Else, ForEach, AsyncForEach, While, With, AsyncWith, Try, Except, Finally, Match, and Case. Call Block on a header to add its suite. Clause builders stay aligned after the preceding suite:

py.If(py.ID("ready")).Block(py.Return().True()).Else().Block(py.Return().False())

Expressions and values

Builder Use
ID, Pkg, Selector Validate identifiers or compose dotted access.
Raw, Text, FString, FStringText, FStringExpr Add caller-provided source, escaped strings, and interpolated strings.
Op, Call, Bracket, Index, List, KeyValue Compose operators, calls, grouped arguments, indexing, and key/value entries.
ListLiteral, TupleLiteral, SetLiteral, DictLiteral Build common collection literals.
ListComp, SetComp, DictComp, GeneratorExpr Build comprehensions with ForClause, AsyncForClause, and IfClause.
TypeUnion Compose Python 3.10 union annotations with `
Int, Float, Complex, Bool, String, Str, Bytes, ByteArray, MemoryView, Range, ObjectType, TypeType, Any Emit built-in types; Any emits the name from typing.
ListType, DictType, SetType, FrozenSetType, TupleType Emit PEP 585 generic container types.
NoneValue, True, False, Ellipsis Emit Python's built-in literal values.

Text and f-string text parts escape their respective string contexts. F-string expressions accept tokens; advanced conversion flags and format specifications can be composed with Raw.

Built-in types

Use the type builders in annotations and generic expressions. They emit Python's built-in type names and PEP 585 forms such as list[str], dict[str, int], and tuple[str, ...]:

py.Def("load_names").Bracket().Arrow().ListType(py.String()).Block(
    py.Return().ListLiteral(py.Text("Ada")),
)

Int, Float, Complex, Bool, String/Str, Bytes, ByteArray, MemoryView, Range, ObjectType, and TypeType emit built-in type names. Use ListType, DictType, SetType, FrozenSetType, and TupleType for generic containers. TupleType() without arguments emits bare tuple; TupleType(py.Int(), py.Ellipsis()) emits tuple[int, ...]. Any() emits Any and expects the generated module to import it from typing. For None, use NoneValue(); unions can be composed with TypeUnion.

Builder reference

Package-level constructors and chainable *Tokens methods are available unless the table identifies a component token. Component constructors return types.Token; use them as arguments to the parent builder.

Area Builders Notes
Imports and declarations Import, ImportAs, FromImport, Decorator, Def, AsyncDef, Class Build imports, decorators, function headers, and class headers.
Control flow If, Elif, Else, ForEach, AsyncForEach, While, With, AsyncWith, Try, Except, Finally, Match, Case Append Block to a header to emit its suite.
Statements and syntax Return, Yield, Raise, Pass, Break, Continue, Assert, Del, Global, Nonlocal, Await, As, Arrow, Colon, Comma Compose statements, async expressions, annotations, aliases, and punctuation.
Identifiers and expressions ID, Pkg, Selector, Raw, Text, Op, Call, Bracket, Index, List, KeyValue, TypeUnion Raw writes source verbatim; Text escapes a Python string literal. List is a comma-separated sequence without delimiters.
Built-in types Any, Int, Float, Complex, Bool, String, Str, Bytes, ByteArray, MemoryView, Range, ObjectType, TypeType Any emits typing.Any; generated modules must import it. String and Str both emit str.
Generic types ListType, DictType, SetType, FrozenSetType, TupleType Emit Python 3.10-compatible PEP 585 annotations.
Collection values ListLiteral, TupleLiteral, SetLiteral, DictLiteral Build list, tuple, set, and dictionary expressions.
Comprehensions ListComp, SetComp, DictComp, GeneratorExpr Combine with the component constructors ForClause, AsyncForClause, and IfClause.
F-strings and values FString, NoneValue, True, False, Ellipsis FStringText and FStringExpr create f-string parts; they return types.Token.
Layout and output Block, Comment, Line, Join, Render, Unwrap Block emits a colon and an indented suite; Line adds an explicit top-level line break.

Errors and custom tokens

ID validates Unicode identifier syntax and rejects Python keywords. Op rejects operators outside the adapter's supported set. Rendering propagates token and writer errors, but does not check Python grammar, names, types, or runtime behavior. Raw is written verbatim; use it for syntax controlled by the caller. To provide a custom builder, implement types.Token and write its source in Render(io.Writer) error.

For other language adapters and repository information, see the root README, Go guide, and TypeScript/TSX guide. The package reference is on pkg.go.dev.

Documentation

Overview

Package python builds Python 3.10+ source from composable tokens. It includes builders for declarations, control flow, expressions, values, and built-in or generic type annotations. Suites use four-space indentation; callers insert explicit Line tokens between top-level statements. Rendering writes readable source directly and does not format, parse, or type-check the generated code.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsyncForClause

func AsyncForClause(target, iterable types.Token) types.Token

AsyncForClause creates an asynchronous for-in clause used by comprehensions.

func FStringExpr

func FStringExpr(expression types.Token) types.Token

FStringExpr creates an expression part for FString.

func FStringText

func FStringText(value string) types.Token

FStringText creates an escaped text part for FString.

func ForClause

func ForClause(target, iterable types.Token) types.Token

ForClause creates the for-in clause used by comprehensions.

func IfClause

func IfClause(condition types.Token) types.Token

IfClause creates a filter clause used by comprehensions.

func Render

func Render(w io.Writer, arg types.Token) error

Render writes arg without formatting or validating the complete Python program.

Example
package main

import (
	"bytes"
	"fmt"

	py "go.osspkg.com/gogen/python"
)

func main() {
	file := py.FromImport("dataclasses", py.ID("dataclass")).Line().Join(
		py.Decorator(py.ID("dataclass")),
		py.Class("User").Block(
			py.ID("name").Colon().ID("str"),
			py.ID("active").Colon().ID("bool").Op("=").True(),
		),
		py.Def("greet").Bracket(py.ID("user").Colon().ID("User")).Arrow().ID("str").Block(
			py.Return().FString(py.FStringText("Hello, "), py.FStringExpr(py.ID("user").Selector("name"))),
		),
	)

	var source bytes.Buffer
	if err := py.Render(&source, file); err != nil {
		panic(err)
	}
	fmt.Print(source.String())
}
Output:
from dataclasses import dataclass
@dataclass
class User:
    name: str
    active: bool = True
def greet(user: User) -> str:
    return f"Hello, {user.name}"

Types

type Tokens

type Tokens []types.Token

Tokens is a chainable sequence of Python source tokens.

func Any

func Any() *Tokens

Any creates an Any type annotation. Import Any from typing in generated code.

func Arrow

func Arrow() *Tokens

Arrow creates the function return annotation arrow.

func As

func As() *Tokens

As creates an as-clause token sequence.

func Assert

func Assert() *Tokens

Assert creates an assert statement token sequence.

func AsyncDef

func AsyncDef(name string) *Tokens

AsyncDef creates an asynchronous function declaration header.

func AsyncForEach

func AsyncForEach(target, iterable types.Token) *Tokens

AsyncForEach creates an async for-in header.

func AsyncWith

func AsyncWith(contexts ...types.Token) *Tokens

AsyncWith creates an async with header.

func Await

func Await() *Tokens

Await creates an await expression token sequence.

func Block

func Block(body ...types.Token) *Tokens

Block creates a Python suite that appends a colon and indents its body.

func Bool

func Bool() *Tokens

Bool creates Python's bool type.

func Bracket

func Bracket(args ...types.Token) *Tokens

Bracket creates a parenthesized token list.

func Break

func Break() *Tokens

Break creates a break statement token sequence.

func ByteArray

func ByteArray() *Tokens

ByteArray creates Python's bytearray type.

func Bytes

func Bytes() *Tokens

Bytes creates Python's bytes type.

func Call

func Call(args ...types.Token) *Tokens

Call creates a parenthesized call argument list.

func Case

func Case(pattern types.Token) *Tokens

Case creates a case header.

func Class

func Class(name string, bases ...types.Token) *Tokens

Class creates a class declaration header with optional base classes.

func Colon

func Colon() *Tokens

Colon creates a colon punctuation token.

func Comma

func Comma() *Tokens

Comma creates a comma punctuation token.

func Comment

func Comment(text string) *Tokens

Comment creates a Python comment, prefixing every line with #.

func Complex

func Complex() *Tokens

Complex creates Python's complex type.

func Continue

func Continue() *Tokens

Continue creates a continue statement token sequence.

func Decorator

func Decorator(expression types.Token) *Tokens

Decorator creates a decorator expression followed by a line break.

func Def

func Def(name string) *Tokens

Def creates a function declaration header.

func Del

func Del() *Tokens

Del creates a del statement token sequence.

func DictComp

func DictComp(key, value types.Token, clauses ...types.Token) *Tokens

DictComp creates a dictionary comprehension from key/value expressions and clauses.

func DictLiteral

func DictLiteral(entries ...types.Token) *Tokens

DictLiteral creates a Python dictionary literal.

func DictType

func DictType(key, value types.Token) *Tokens

DictType creates a parameterized dict type such as dict[str, int].

func Elif

func Elif(condition types.Token) *Tokens

Elif creates an elif header.

func Ellipsis

func Ellipsis() *Tokens

Ellipsis creates Python's ellipsis literal.

func Else

func Else() *Tokens

Else creates an else header.

func Except

func Except(exception ...types.Token) *Tokens

Except creates an except header, optionally with an exception type.

func FString

func FString(parts ...types.Token) *Tokens

FString creates a Python f-string from text and expression parts.

func False

func False() *Tokens

False creates Python's False literal.

func Finally

func Finally() *Tokens

Finally creates a finally header.

func Float

func Float() *Tokens

Float creates Python's float type.

func ForEach

func ForEach(target, iterable types.Token) *Tokens

ForEach creates a for-in header.

func FromImport

func FromImport(module string, names ...types.Token) *Tokens

FromImport creates a from-import statement with optional names.

func FrozenSetType

func FrozenSetType(element types.Token) *Tokens

FrozenSetType creates a parameterized frozenset type.

func GeneratorExpr

func GeneratorExpr(expression types.Token, clauses ...types.Token) *Tokens

GeneratorExpr creates a generator expression from an expression and clauses.

func Global

func Global() *Tokens

Global creates a global declaration token sequence.

func ID

func ID(name string) *Tokens

ID creates a token sequence containing a validated Python identifier.

func If

func If(condition types.Token) *Tokens

If creates an if header.

func Import

func Import(module string) *Tokens

Import creates an import statement for a dotted module path.

func ImportAs

func ImportAs(module, alias string) *Tokens

ImportAs creates an import statement with an alias.

func Index

func Index(index types.Token) *Tokens

Index creates a square-bracketed index or type argument.

func Int

func Int() *Tokens

Int creates Python's int type.

func KeyValue

func KeyValue(key, value types.Token) *Tokens

KeyValue creates a dictionary or keyword-argument entry.

func Line

func Line() *Tokens

Line creates a token sequence containing an explicit line break.

func List

func List(args ...types.Token) *Tokens

List creates a comma-separated sequence without delimiters.

func ListComp

func ListComp(expression types.Token, clauses ...types.Token) *Tokens

ListComp creates a list comprehension from an expression and clauses.

func ListLiteral

func ListLiteral(values ...types.Token) *Tokens

ListLiteral creates a Python list literal.

func ListType

func ListType(element types.Token) *Tokens

ListType creates a parameterized list type such as list[str].

func Match

func Match(subject types.Token) *Tokens

Match creates a match header.

func MemoryView

func MemoryView() *Tokens

MemoryView creates Python's memoryview type.

func NoneValue

func NoneValue() *Tokens

NoneValue creates Python's None literal.

func Nonlocal

func Nonlocal() *Tokens

Nonlocal creates a nonlocal declaration token sequence.

func ObjectType

func ObjectType() *Tokens

ObjectType creates Python's object type.

func Op

func Op(op string) *Tokens

Op creates a token sequence containing a supported Python operator.

func Pass

func Pass() *Tokens

Pass creates a pass statement token sequence.

func Pkg

func Pkg(module string) *Tokens

Pkg creates a dotted module or package name.

func Raise

func Raise() *Tokens

Raise creates a raise statement token sequence.

func Range

func Range() *Tokens

Range creates Python's range type.

func Raw

func Raw(source string) *Tokens

Raw creates a token sequence containing source text verbatim.

func Return

func Return() *Tokens

Return creates a return statement token sequence.

func Selector

func Selector(name string) *Tokens

Selector creates an attribute selector.

func SetComp

func SetComp(expression types.Token, clauses ...types.Token) *Tokens

SetComp creates a set comprehension from an expression and clauses.

func SetLiteral

func SetLiteral(values ...types.Token) *Tokens

SetLiteral creates a Python set literal. An empty set is emitted as set().

func SetType

func SetType(element types.Token) *Tokens

SetType creates a parameterized set type such as set[int].

func Str

func Str() *Tokens

Str creates Python's str type.

func String

func String() *Tokens

String creates Python's str type as a readable alias for Str.

func Text

func Text(value string) *Tokens

Text creates a token sequence containing an escaped Python string literal.

func True

func True() *Tokens

True creates Python's True literal.

func Try

func Try() *Tokens

Try creates a try header.

func TupleLiteral

func TupleLiteral(values ...types.Token) *Tokens

TupleLiteral creates a Python tuple literal.

func TupleType

func TupleType(elements ...types.Token) *Tokens

TupleType creates a tuple type. Pass element types for fixed tuples or Ellipsis() as the second argument for a variable-length tuple.

func TypeType

func TypeType() *Tokens

TypeType creates Python's type type.

func TypeUnion

func TypeUnion(values ...types.Token) *Tokens

TypeUnion creates a Python 3.10 union annotation.

func While

func While(condition types.Token) *Tokens

While creates a while header.

func With

func With(contexts ...types.Token) *Tokens

With creates a with header for one or more context expressions.

func Yield

func Yield() *Tokens

Yield creates a yield expression token sequence.

func (*Tokens) Any

func (v *Tokens) Any() *Tokens

Any appends Any, the typing module's unconstrained type annotation.

func (*Tokens) Arrow

func (v *Tokens) Arrow() *Tokens

Arrow appends the function return annotation arrow.

func (*Tokens) As

func (v *Tokens) As() *Tokens

As appends the as keyword.

func (*Tokens) Assert

func (v *Tokens) Assert() *Tokens

Assert appends the assert keyword.

func (*Tokens) AsyncDef

func (v *Tokens) AsyncDef(name string) *Tokens

AsyncDef appends an asynchronous function declaration header.

func (*Tokens) AsyncForEach

func (v *Tokens) AsyncForEach(target, iterable types.Token) *Tokens

AsyncForEach appends an async for-in header.

func (*Tokens) AsyncWith

func (v *Tokens) AsyncWith(contexts ...types.Token) *Tokens

AsyncWith appends an async with header.

func (*Tokens) Await

func (v *Tokens) Await() *Tokens

Await appends the await keyword.

func (*Tokens) Block

func (v *Tokens) Block(body ...types.Token) *Tokens

Block creates a Python suite that appends a colon and indents its body.

func (*Tokens) Bool

func (v *Tokens) Bool() *Tokens

Bool appends Python's bool type.

func (*Tokens) Bracket

func (v *Tokens) Bracket(args ...types.Token) *Tokens

Bracket appends a parenthesized token list.

func (*Tokens) Break

func (v *Tokens) Break() *Tokens

Break appends the break keyword.

func (*Tokens) ByteArray

func (v *Tokens) ByteArray() *Tokens

ByteArray appends Python's bytearray type.

func (*Tokens) Bytes

func (v *Tokens) Bytes() *Tokens

Bytes appends Python's bytes type.

func (*Tokens) Call

func (v *Tokens) Call(args ...types.Token) *Tokens

Call appends a parenthesized call argument list.

func (*Tokens) Case

func (v *Tokens) Case(pattern types.Token) *Tokens

Case appends a case header.

func (*Tokens) Class

func (v *Tokens) Class(name string, bases ...types.Token) *Tokens

Class appends a class declaration header with optional base classes.

func (*Tokens) Colon

func (v *Tokens) Colon() *Tokens

Colon appends a colon punctuation token.

func (*Tokens) Comma

func (v *Tokens) Comma() *Tokens

Comma appends a comma punctuation token.

func (*Tokens) Comment

func (v *Tokens) Comment(text string) *Tokens

Comment creates a Python comment, prefixing every line with #.

func (*Tokens) Complex

func (v *Tokens) Complex() *Tokens

Complex appends Python's complex type.

func (*Tokens) Continue

func (v *Tokens) Continue() *Tokens

Continue appends the continue keyword.

func (*Tokens) Decorator

func (v *Tokens) Decorator(expression types.Token) *Tokens

Decorator appends a decorator expression followed by a line break.

func (*Tokens) Def

func (v *Tokens) Def(name string) *Tokens

Def appends a function declaration header; call Block to add its suite.

func (*Tokens) Del

func (v *Tokens) Del() *Tokens

Del appends the del keyword.

func (*Tokens) DictComp

func (v *Tokens) DictComp(key, value types.Token, clauses ...types.Token) *Tokens

DictComp creates a dictionary comprehension from key/value expressions and clauses.

func (*Tokens) DictLiteral

func (v *Tokens) DictLiteral(entries ...types.Token) *Tokens

DictLiteral creates a Python dictionary literal.

func (*Tokens) DictType

func (v *Tokens) DictType(key, value types.Token) *Tokens

DictType appends a parameterized dict type such as dict[str, int].

func (*Tokens) Elif

func (v *Tokens) Elif(condition types.Token) *Tokens

Elif appends an elif header.

func (*Tokens) Ellipsis

func (v *Tokens) Ellipsis() *Tokens

Ellipsis appends Python's ellipsis literal.

func (*Tokens) Else

func (v *Tokens) Else() *Tokens

Else appends an else header.

func (*Tokens) Except

func (v *Tokens) Except(exception ...types.Token) *Tokens

Except appends an except header, optionally with an exception type.

func (*Tokens) FString

func (v *Tokens) FString(parts ...types.Token) *Tokens

FString creates a Python f-string from text and expression parts.

func (*Tokens) False

func (v *Tokens) False() *Tokens

False appends Python's False literal.

func (*Tokens) Finally

func (v *Tokens) Finally() *Tokens

Finally appends a finally header.

func (*Tokens) Float

func (v *Tokens) Float() *Tokens

Float appends Python's float type.

func (*Tokens) ForEach

func (v *Tokens) ForEach(target, iterable types.Token) *Tokens

ForEach appends a for-in header.

func (*Tokens) FromImport

func (v *Tokens) FromImport(module string, names ...types.Token) *Tokens

FromImport appends a from-import statement with optional names.

func (*Tokens) FrozenSetType

func (v *Tokens) FrozenSetType(element types.Token) *Tokens

FrozenSetType appends a parameterized frozenset type.

func (*Tokens) GeneratorExpr

func (v *Tokens) GeneratorExpr(expression types.Token, clauses ...types.Token) *Tokens

GeneratorExpr creates a generator expression from an expression and clauses.

func (*Tokens) Global

func (v *Tokens) Global() *Tokens

Global appends the global keyword.

func (*Tokens) ID

func (v *Tokens) ID(name string) *Tokens

ID appends a validated Python identifier.

func (*Tokens) If

func (v *Tokens) If(condition types.Token) *Tokens

If appends an if header; call Block to add its suite.

func (*Tokens) Import

func (v *Tokens) Import(module string) *Tokens

Import appends an import statement for a dotted module path.

func (*Tokens) ImportAs

func (v *Tokens) ImportAs(module, alias string) *Tokens

ImportAs appends an import statement with an alias.

func (*Tokens) Index

func (v *Tokens) Index(index types.Token) *Tokens

Index appends a square-bracketed index or type argument.

func (*Tokens) Int

func (v *Tokens) Int() *Tokens

Int appends Python's int type.

func (*Tokens) Join

func (v *Tokens) Join(args ...types.Token) *Tokens

Join appends tokens to the sequence and returns it.

func (*Tokens) Line

func (v *Tokens) Line() *Tokens

Line appends an explicit line break.

func (*Tokens) List

func (v *Tokens) List(args ...types.Token) *Tokens

List appends a comma-separated sequence without delimiters.

func (*Tokens) ListComp

func (v *Tokens) ListComp(expression types.Token, clauses ...types.Token) *Tokens

ListComp creates a list comprehension from an expression and clauses.

func (*Tokens) ListLiteral

func (v *Tokens) ListLiteral(values ...types.Token) *Tokens

ListLiteral creates a Python list literal.

func (*Tokens) ListType

func (v *Tokens) ListType(element types.Token) *Tokens

ListType appends a parameterized list type such as list[str].

func (*Tokens) Match

func (v *Tokens) Match(subject types.Token) *Tokens

Match appends a match header.

func (*Tokens) MemoryView

func (v *Tokens) MemoryView() *Tokens

MemoryView appends Python's memoryview type.

func (*Tokens) NoneValue

func (v *Tokens) NoneValue() *Tokens

NoneValue appends Python's None literal.

func (*Tokens) Nonlocal

func (v *Tokens) Nonlocal() *Tokens

Nonlocal appends the nonlocal keyword.

func (*Tokens) ObjectType

func (v *Tokens) ObjectType() *Tokens

ObjectType appends Python's object type.

func (*Tokens) Op

func (v *Tokens) Op(op string) *Tokens

Op appends a supported Python operator.

func (*Tokens) Pass

func (v *Tokens) Pass() *Tokens

Pass appends the pass keyword.

func (*Tokens) Pkg

func (v *Tokens) Pkg(module string) *Tokens

Pkg appends a dotted module or package name.

func (*Tokens) Raise

func (v *Tokens) Raise() *Tokens

Raise appends the raise keyword.

func (*Tokens) Range

func (v *Tokens) Range() *Tokens

Range appends Python's range type.

func (*Tokens) Raw

func (v *Tokens) Raw(source string) *Tokens

Raw appends source text verbatim without validating it.

func (*Tokens) Render

func (v *Tokens) Render(w io.Writer) error

Render writes the token sequence to w without formatting it.

func (*Tokens) Return

func (v *Tokens) Return() *Tokens

Return appends the return keyword.

func (*Tokens) Selector

func (v *Tokens) Selector(name string) *Tokens

Selector appends an attribute selector.

func (*Tokens) SetComp

func (v *Tokens) SetComp(expression types.Token, clauses ...types.Token) *Tokens

SetComp creates a set comprehension from an expression and clauses.

func (*Tokens) SetLiteral

func (v *Tokens) SetLiteral(values ...types.Token) *Tokens

SetLiteral creates a Python set literal. An empty set is emitted as set().

func (*Tokens) SetType

func (v *Tokens) SetType(element types.Token) *Tokens

SetType appends a parameterized set type such as set[int].

func (*Tokens) Str

func (v *Tokens) Str() *Tokens

Str appends Python's str type.

func (*Tokens) String

func (v *Tokens) String() *Tokens

String appends Python's str type as a readable alias for Str.

func (*Tokens) Text

func (v *Tokens) Text(value string) *Tokens

Text appends an escaped Python string literal.

func (*Tokens) True

func (v *Tokens) True() *Tokens

True appends Python's True literal.

func (*Tokens) Try

func (v *Tokens) Try() *Tokens

Try appends a try header.

func (*Tokens) TupleLiteral

func (v *Tokens) TupleLiteral(values ...types.Token) *Tokens

TupleLiteral creates a Python tuple literal.

func (*Tokens) TupleType

func (v *Tokens) TupleType(elements ...types.Token) *Tokens

TupleType appends a tuple type. Pass element types for fixed tuples or Ellipsis() as the second argument for a variable-length tuple.

func (*Tokens) TypeType

func (v *Tokens) TypeType() *Tokens

TypeType appends Python's type type.

func (*Tokens) TypeUnion

func (v *Tokens) TypeUnion(values ...types.Token) *Tokens

TypeUnion creates a Python 3.10 union annotation.

func (*Tokens) Unwrap

func (v *Tokens) Unwrap() []types.Token

Unwrap returns the underlying token slice without copying it.

func (*Tokens) While

func (v *Tokens) While(condition types.Token) *Tokens

While appends a while header.

func (*Tokens) With

func (v *Tokens) With(contexts ...types.Token) *Tokens

With appends a with header for one or more context expressions.

func (*Tokens) Yield

func (v *Tokens) Yield() *Tokens

Yield appends the yield keyword.

Jump to

Keyboard shortcuts

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