Skip to content

Commit 74c1905

Browse files
Explicitly precompile the ANSI-colors regular expression
This may not be terribly important, as the re module caches patterns, but it doesn’t hurt. Corresponds to jonathaneunice/colors#2.
1 parent 6ad99e4 commit 74c1905

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

papermill/exceptions.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,24 @@ class PapermillParameterOverwriteWarning(PapermillWarning):
5959
"""Callee overwrites caller argument to pass down the stream."""
6060

6161

62+
# The regular expression is copied from:
63+
# https://github.com/jonathaneunice/colors/blob/
64+
# c965f5b9103c5bd32a1572adb8024ebe83278fb0/colors/colors.py#L122-L131
65+
#
66+
# The original docstring notes that this does not strip all possible ANSI
67+
# escape sequences related to color and style, but it attempts to cover the
68+
# most common ones and a few known oddities produced by actual colorization
69+
# libraries, including \x1b[K (EL, erase to end of line) and \x1b[m (more
70+
# commonly expressed as \x1b[0m).
71+
_COLORS = re.compile("\x1b\\[(K|.*?m)")
72+
73+
6274
def strip_color(text):
6375
"""Remove most ANSI color and style sequences from a string
6476
6577
Based on https://pypi.org/project/ansicolors/."""
6678

67-
# The regular expression is copied from:
68-
# https://github.com/jonathaneunice/colors/blob/
69-
# c965f5b9103c5bd32a1572adb8024ebe83278fb0/colors/colors.py#L122-L131
70-
#
71-
# The original docstring notes that this does not strip all possible ANSI
72-
# escape sequences related to color and style, but it attempts to cover the
73-
# most common ones and a few known oddities produced by actual
74-
# colorization libraries, including \x1b[K (EL, erase to end of line) and
75-
# \x1b[m (more commonly expressed as \x1b[0m).
76-
return re.sub("\x1b\\[(K|.*?m)", "", text)
79+
return _COLORS.sub("", text)
7780

7881

7982
def missing_dependency_generator(package, dep):

0 commit comments

Comments
 (0)