Total Complexity | 2 |
Total Lines | 50 |
Duplicated Lines | 92 % |
Coverage | 66.67% |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | 1 | import re |
|
2 | |||
3 | |||
4 | 1 | View Code Duplication | class SDoc: |
|
|||
5 | """ |
||
6 | Utility class with functions for generating SDoc code. |
||
7 | """ |
||
8 | |||
9 | # ------------------------------------------------------------------------------------------------------------------ |
||
10 | 1 | @staticmethod |
|
11 | 1 | def escape(text: str) -> str: |
|
12 | """ |
||
13 | Returns an escaped string that is safe to use in SDoc. |
||
14 | |||
15 | :param str text: The escaped string. |
||
16 | """ |
||
17 | |||
18 | 1 | def replace(match_obj): |
|
19 | """ |
||
20 | Returns the match text prefixed with backslash |
||
21 | |||
22 | :param re.match match_obj: The match. |
||
23 | |||
24 | :rtype: str |
||
25 | """ |
||
26 | return '\\' + match_obj.group(0) |
||
27 | |||
28 | 1 | return re.sub(r'[\\{}]', replace, text) |
|
29 | |||
30 | # ------------------------------------------------------------------------------------------------------------------ |
||
31 | 1 | @staticmethod |
|
32 | 1 | def unescape(text: str) -> str: |
|
33 | """ |
||
34 | Returns an unescaped SDoc escaped string. I.e. removes back slashes. |
||
35 | |||
36 | :param str text: The SDoc escaped string. |
||
37 | """ |
||
38 | |||
39 | def replace(match_obj): |
||
40 | """ |
||
41 | Returns the match text without prefixed backslash. |
||
42 | |||
43 | :param re.match match_obj: The match. |
||
44 | |||
45 | :rtype: str |
||
46 | """ |
||
47 | return match_obj.group(0)[1:] |
||
48 | |||
49 | return re.sub(r'\\.', replace, text) |
||
50 | |||
52 |