High-level API
The top-level PyReprism package exposes a convenience API. Import it as
pr and pass lang as a language name ("python"), a file extension
(".py"), or a language class:
import PyReprism as pr
pr.remove_comments(source, lang="python")
pr.extract_comments(source, lang="python")
pr.count_comments(source, lang="python")
pr.tokenize(source, lang="python")
pr.normalize(source, lang="python")
pr.stats(source, lang="python")
pr.preprocess(source, lang="java", steps=["comments", "strings", "whitespace"])
Every operation accepts engine="regex" (default), engine="pygments"
(requires pip install pyreprism[accurate]), or engine="auto".
The re-exported Token, TokenType, CodeStats and Normalizer are
documented on their own pages (Tokens, Metrics, Normalizer).
PyReprism: a framework for source-code preprocessing.
High-level convenience API:
import PyReprism as pr
pr.remove_comments(src, lang="python")
pr.extract_comments(src, lang="python")
pr.count_comments(src, lang="python")
pr.tokenize(src, lang="python")
pr.preprocess(src, lang="java", steps=["comments", "strings", "whitespace"])
lang may be a language name ("python"), a file extension (".py"), or a
language class. Use detect_language() to infer it from a filename.
- PyReprism.blank_comments(source: str, lang: str | type, replacement: str = ' ', engine: str = 'regex') str
Remove comment content while preserving line numbers.
- PyReprism.detect_language(filename: str | None = None, source: str | None = None) Type | None
Infer a language class from a
filenameand/or asourcestring.Resolution order: filename extension, then (if
sourceis given) a#!shebang line, then a best-effort Pygments content guess when Pygments is installed. ReturnsNonewhen nothing matches.
- PyReprism.get_language(lang: str | type) Type
Resolve
langto a registered language class.Accepts a language class, a registry name (
"Python"/"python"), or a file extension (".py"). RaisesValueErrorif it cannot be resolved.
- PyReprism.normalize(source: str, lang: str | type, engine: str = 'regex', **options) str
Return a canonicalized form of
sourcefor ML / clone detection.See
PyReprism.languages.base.BaseLanguage.normalize()for options.
- PyReprism.preprocess(source: str, lang: str | type, steps: Sequence[str] = ('comments',), engine: str = 'regex') str
Apply a sequence of removal
stepsin order and return the result.Valid steps:
comments,strings,numbers,operators,keywords,whitespace.
- PyReprism.remove_comments(source: str, lang: str | type, engine: str = 'regex', isList: bool = False)
Remove comments from
sourcefor the given language.
- PyReprism.remove_whitespaces(source: str) str
Collapse insignificant whitespace (language-independent).