Skip to content

reprexlite.formatting

Attributes

venues_dispatcher: Dict[str, Type[reprexlite.formatting.Reprex]]

Mapping from venue keywords to their Reprex implementation.

Classes

Advertisement

Class for generating the advertisement note for reprexlite.

Attributes:

Name Type Description
timestamp str

Timestamp of instance instantiation

version str

Version of reprexlite

Source code in reprexlite/formatting.py
class Advertisement:
    """Class for generating the advertisement note for reprexlite.

    Attributes:
        timestamp (str): Timestamp of instance instantiation
        version (str): Version of reprexlite
    """

    pkg = "reprexlite"
    url = "https://github.com/jayqi/reprexlite"

    def __init__(self):
        self.timestamp = datetime.now().astimezone().strftime("%Y-%m-%d %H:%M:%S %Z")
        self.version = f"v{__version__}"

    def markdown(self) -> str:
        """Render reprexlite advertisement in GitHub Flavored Markdown."""
        return f"<sup>Created at {self.timestamp} by [{self.pkg}]({self.url}) {self.version}</sup>"

    def html(self) -> str:
        """Render reprexlite advertisement in HTML."""
        return (
            f"<p><sup>Created at {self.timestamp} by "
            f'<a href="{self.url}">{self.pkg}</a> {self.version}</sup></p>'
        )

    def code_comment(self) -> str:
        """Render reprexlite advertisement as a comment in Python code."""
        return f"# {self.text()}"

    def text(self) -> str:
        """Render reprexlite advertisement in plain text."""
        return f"Created at {self.timestamp} by {self.pkg} {self.version} <{self.url}>"
pkg
url

Methods

__init__(self) special
Source code in reprexlite/formatting.py
def __init__(self):
    self.timestamp = datetime.now().astimezone().strftime("%Y-%m-%d %H:%M:%S %Z")
    self.version = f"v{__version__}"
code_comment(self)

Render reprexlite advertisement as a comment in Python code.

Source code in reprexlite/formatting.py
def code_comment(self) -> str:
    """Render reprexlite advertisement as a comment in Python code."""
    return f"# {self.text()}"
html(self)

Render reprexlite advertisement in HTML.

Source code in reprexlite/formatting.py
def html(self) -> str:
    """Render reprexlite advertisement in HTML."""
    return (
        f"<p><sup>Created at {self.timestamp} by "
        f'<a href="{self.url}">{self.pkg}</a> {self.version}</sup></p>'
    )
markdown(self)

Render reprexlite advertisement in GitHub Flavored Markdown.

Source code in reprexlite/formatting.py
def markdown(self) -> str:
    """Render reprexlite advertisement in GitHub Flavored Markdown."""
    return f"<sup>Created at {self.timestamp} by [{self.pkg}]({self.url}) {self.version}</sup>"
text(self)

Render reprexlite advertisement in plain text.

Source code in reprexlite/formatting.py
def text(self) -> str:
    """Render reprexlite advertisement in plain text."""
    return f"Created at {self.timestamp} by {self.pkg} {self.version} <{self.url}>"

GitHubReprex (Reprex)

Concrete implementation for rendering reprexes in GitHub Flavored Markdown.

Source code in reprexlite/formatting.py
class GitHubReprex(Reprex):
    """Concrete implementation for rendering reprexes in GitHub Flavored Markdown."""

    default_advertise: bool = True

    def __str__(self) -> str:
        out = []
        out.append("```python")
        out.append(str(self.code_block))
        out.append("```")
        if self.advertise:
            out.append("\n" + Advertisement().markdown())
        if self.session_info:
            out.append("\n<details><summary>Session Info</summary>")
            out.append("```text")
            out.append(str(SessionInfo()))
            out.append("```")
            out.append("</details>")
        return "\n".join(out)

Attributes

default_advertise: bool

Default for whether to include reprexlite advertisement for this venue format.

HtmlReprex (Reprex)

Concrete implementation for rendering reprexes in HTML. If optional dependency Pygments is available, the rendered HTML will have syntax highlighting for the Python code.

Source code in reprexlite/formatting.py
class HtmlReprex(Reprex):
    """Concrete implementation for rendering reprexes in HTML. If optional dependency Pygments is
    available, the rendered HTML will have syntax highlighting for the Python code."""

    default_advertise: bool = True

    def __str__(self) -> str:
        out = []
        try:
            from pygments import highlight
            from pygments.formatters import HtmlFormatter
            from pygments.lexers import PythonLexer

            formatter = HtmlFormatter(
                style="friendly", lineanchors=True, linenos=True, wrapcode=True
            )
            out.append(f"<style>{formatter.get_style_defs('.highlight')}</style>")
            out.append(highlight(str(self.code_block), PythonLexer(), formatter))
        except ImportError:
            out.append(f"<pre><code>{self.code_block}</code></pre>")

        if self.advertise:
            out.append(Advertisement().html())
        if self.session_info:
            out.append("<details><summary>Session Info</summary>")
            out.append(f"<pre><code>{SessionInfo()}</code></pre>")
            out.append("</details>")
        return "\n".join(out)

Attributes

default_advertise: bool

Default for whether to include reprexlite advertisement for this venue format.

PyScriptReprex (Reprex)

Concrete implementation for rendering reprexes as a Python script.

Source code in reprexlite/formatting.py
class PyScriptReprex(Reprex):
    """Concrete implementation for rendering reprexes as a Python script."""

    default_advertise: bool = False

    def __str__(self) -> str:
        out = [str(self.code_block)]
        if self.advertise:
            out.append("\n" + Advertisement().code_comment())
        if self.session_info:
            out.append("")
            sess_lines = str(SessionInfo()).split("\n")
            out.extend("# " + line for line in sess_lines)
        return "\n".join(out)

Attributes

default_advertise: bool

Default for whether to include reprexlite advertisement for this venue format.

Reprex (ABC)

Abstract base class for a reprex instance. Concrete subclasses should implement the formatting logic appropriate to a specific venue for sharing. Call str(...) on an instance to return the formatted reprex.

Attributes:

Name Type Description
code_block CodeBlock

instance of CodeBlock

advertise bool

whether to render reprexlite advertisement

session_info bool

whether to render session info

Source code in reprexlite/formatting.py
class Reprex(ABC):
    """Abstract base class for a reprex instance. Concrete subclasses should implement the
    formatting logic appropriate to a specific venue for sharing. Call `str(...)` on an instance
    to return the formatted reprex.

    Attributes:
        code_block (CodeBlock): instance of [`CodeBlock`][reprexlite.code.CodeBlock]
        advertise (bool): whether to render reprexlite advertisement
        session_info (bool): whether to render session info
    """

    default_advertise: bool
    """Default for whether to include reprexlite advertisement for this venue format."""

    def __init__(
        self, code_block: CodeBlock, advertise: Optional[bool] = None, session_info: bool = False
    ):
        self.code_block: CodeBlock = code_block
        self.advertise: bool = self.default_advertise if advertise is None else advertise
        self.session_info: bool = session_info

    @abstractmethod
    def __str__(self) -> str:  # pragma: no cover
        pass
__init__(self, code_block, advertise=None, session_info=False) special
Source code in reprexlite/formatting.py
def __init__(
    self, code_block: CodeBlock, advertise: Optional[bool] = None, session_info: bool = False
):
    self.code_block: CodeBlock = code_block
    self.advertise: bool = self.default_advertise if advertise is None else advertise
    self.session_info: bool = session_info

RtfReprex (Reprex)

Concrete implementation for rendering reprexes in Rich Text Format.

Source code in reprexlite/formatting.py
class RtfReprex(Reprex):
    """Concrete implementation for rendering reprexes in Rich Text Format."""

    default_advertise: bool = False

    def __str__(self) -> str:
        try:
            from pygments import highlight
            from pygments.formatters import RtfFormatter
            from pygments.lexers import PythonLexer
        except ImportError:
            raise ImportError("Pygments is required for RTF output.")

        out = str(self.code_block)
        if self.advertise:
            out += "\n\n" + Advertisement().text()
        if self.session_info:
            out += "\n\n" + str(SessionInfo())
        return highlight(out, PythonLexer(), RtfFormatter())

Attributes

default_advertise: bool

Default for whether to include reprexlite advertisement for this venue format.

SlackReprex (Reprex)

Concrete implementation for rendering reprexes as Slack markup.

Source code in reprexlite/formatting.py
class SlackReprex(Reprex):
    """Concrete implementation for rendering reprexes as Slack markup."""

    default_advertise: bool = False

    def __str__(self):
        out = []
        out.append("```")
        out.append(str(self.code_block))
        out.append("```")
        if self.advertise:
            out.append("\n" + Advertisement().text())
        if self.session_info:
            out.append("\n```")
            out.append(str(SessionInfo()))
            out.append("```")
        return "\n".join(out)

Attributes

default_advertise: bool

Default for whether to include reprexlite advertisement for this venue format.

Venue (str, Enum)

Enum for valid venue options.