提交 81ea8258 authored 作者: Brandon T. Willard's avatar Brandon T. Willard 提交者: Brandon T. Willard

Simplify PPrinter.assign and add type hints to PPrinter methods

上级 debdd547
...@@ -12,7 +12,7 @@ from abc import ABC, abstractmethod ...@@ -12,7 +12,7 @@ from abc import ABC, abstractmethod
from copy import copy from copy import copy
from functools import reduce from functools import reduce
from io import IOBase, StringIO from io import IOBase, StringIO
from typing import Dict, Iterable, List, Optional, Union from typing import Callable, Dict, Iterable, List, Optional, Union
import numpy as np import numpy as np
...@@ -889,14 +889,13 @@ class PPrinter(Printer): ...@@ -889,14 +889,13 @@ class PPrinter(Printer):
self.printers = [] self.printers = []
self.printers_dict = {} self.printers_dict = {}
def assign(self, condition, printer): def assign(self, condition: Union[Op, type, Callable], printer: Printer):
# condition can be a class or an instance of an Op.
if isinstance(condition, (Op, type)): if isinstance(condition, (Op, type)):
self.printers_dict[condition] = printer self.printers_dict[condition] = printer
return else:
self.printers.insert(0, (condition, printer)) self.printers.insert(0, (condition, printer))
def process(self, r, pstate=None): def process(self, r: Variable, pstate: Optional[PrinterState] = None) -> str:
if pstate is None: if pstate is None:
pstate = PrinterState(pprinter=self) pstate = PrinterState(pprinter=self)
elif isinstance(pstate, dict): elif isinstance(pstate, dict):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论