DUBSlib 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dubslib/__init__.py +2 -0
- dubslib/constants/__init__.py +13 -0
- dubslib/constants/ordinals/__init__.py +13 -0
- dubslib/constants/ordinals/classification.py +125 -0
- dubslib/io/__init__.py +8 -0
- dubslib/io/data/__init__.py +5 -0
- dubslib/io/data/io.py +75 -0
- dubslib/io/exceptions/__init__.py +3 -0
- dubslib/io/exceptions/serializationerror.py +57 -0
- dubslib/io/raw/__init__.py +7 -0
- dubslib/io/raw/i.py +90 -0
- dubslib/io/raw/o.py +97 -0
- dubslib/mockclasses/__init__.py +4 -0
- dubslib/mockclasses/mock_custom_classes.py +99 -0
- dubslib/mockclasses/mock_tkinter.py +172 -0
- dubslib/path/__init__.py +15 -0
- dubslib/path/file_paths.py +157 -0
- dubslib-0.1.0.dist-info/METADATA +67 -0
- dubslib-0.1.0.dist-info/RECORD +22 -0
- dubslib-0.1.0.dist-info/WHEEL +5 -0
- dubslib-0.1.0.dist-info/licenses/LICENSE +674 -0
- dubslib-0.1.0.dist-info/top_level.txt +1 -0
dubslib/__init__.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from .ordinals import (
|
|
2
|
+
LETTER_CHARS, LETTER_DICT, LETTER_ORDS,
|
|
3
|
+
NUMBER_CHARS, NUMBER_DICT, NUMBER_ORDS,
|
|
4
|
+
WHITE_SPACE_CHARS, WHITE_SPACE_DICT, WHITE_SPACE_ORDS,
|
|
5
|
+
OPERATOR_CHARS, OPERATOR_ORDS, OPERATORS_DICT
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"LETTER_CHARS", "LETTER_DICT", "LETTER_ORDS",
|
|
10
|
+
"NUMBER_CHARS", 'NUMBER_DICT', 'NUMBER_ORDS',
|
|
11
|
+
"WHITE_SPACE_CHARS", "WHITE_SPACE_DICT", "WHITE_SPACE_ORDS",
|
|
12
|
+
"OPERATOR_CHARS", "OPERATOR_ORDS", "OPERATORS_DICT"
|
|
13
|
+
]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from .classification import (
|
|
2
|
+
LETTER_CHARS, LETTER_DICT, LETTER_ORDS,
|
|
3
|
+
NUMBER_CHARS, NUMBER_DICT, NUMBER_ORDS,
|
|
4
|
+
WHITE_SPACE_CHARS, WHITE_SPACE_DICT, WHITE_SPACE_ORDS,
|
|
5
|
+
OPERATOR_CHARS, OPERATOR_ORDS, OPERATORS_DICT
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"LETTER_CHARS", "LETTER_DICT", "LETTER_ORDS",
|
|
10
|
+
"NUMBER_CHARS", 'NUMBER_DICT', 'NUMBER_ORDS',
|
|
11
|
+
"WHITE_SPACE_CHARS", "WHITE_SPACE_DICT", "WHITE_SPACE_ORDS",
|
|
12
|
+
"OPERATOR_CHARS", "OPERATOR_ORDS", "OPERATORS_DICT"
|
|
13
|
+
]
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
|
|
2
|
+
WHITE_SPACE_CHARS = [
|
|
3
|
+
"\n",
|
|
4
|
+
"\t",
|
|
5
|
+
" "
|
|
6
|
+
]
|
|
7
|
+
|
|
8
|
+
OPERATOR_CHARS = [
|
|
9
|
+
"!",
|
|
10
|
+
"\"",
|
|
11
|
+
"#",
|
|
12
|
+
"$",
|
|
13
|
+
"%",
|
|
14
|
+
"&",
|
|
15
|
+
"'",
|
|
16
|
+
"(",
|
|
17
|
+
")",
|
|
18
|
+
"*",
|
|
19
|
+
"+",
|
|
20
|
+
",",
|
|
21
|
+
"-",
|
|
22
|
+
".",
|
|
23
|
+
"/",
|
|
24
|
+
":",
|
|
25
|
+
";",
|
|
26
|
+
"<",
|
|
27
|
+
"=",
|
|
28
|
+
">",
|
|
29
|
+
"?",
|
|
30
|
+
"@",
|
|
31
|
+
"[",
|
|
32
|
+
"\\",
|
|
33
|
+
"]",
|
|
34
|
+
"^",
|
|
35
|
+
"_",
|
|
36
|
+
"`",
|
|
37
|
+
"{",
|
|
38
|
+
"|",
|
|
39
|
+
"}",
|
|
40
|
+
"~"
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
NUMBER_CHARS = [
|
|
44
|
+
"0",
|
|
45
|
+
"1",
|
|
46
|
+
"2",
|
|
47
|
+
"3",
|
|
48
|
+
"4",
|
|
49
|
+
"5",
|
|
50
|
+
"6",
|
|
51
|
+
"7",
|
|
52
|
+
"8",
|
|
53
|
+
"9"
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
LETTER_CHARS = [
|
|
57
|
+
"A",
|
|
58
|
+
"B",
|
|
59
|
+
"C",
|
|
60
|
+
"D",
|
|
61
|
+
"E",
|
|
62
|
+
"F",
|
|
63
|
+
"G",
|
|
64
|
+
"H",
|
|
65
|
+
"I",
|
|
66
|
+
"J",
|
|
67
|
+
"K",
|
|
68
|
+
"L",
|
|
69
|
+
"M",
|
|
70
|
+
"N",
|
|
71
|
+
"O",
|
|
72
|
+
"P",
|
|
73
|
+
"Q",
|
|
74
|
+
"R",
|
|
75
|
+
"S",
|
|
76
|
+
"T",
|
|
77
|
+
"U",
|
|
78
|
+
"V",
|
|
79
|
+
"W",
|
|
80
|
+
"X",
|
|
81
|
+
"Y",
|
|
82
|
+
"Z",
|
|
83
|
+
"a",
|
|
84
|
+
"b",
|
|
85
|
+
"c",
|
|
86
|
+
"d",
|
|
87
|
+
"e",
|
|
88
|
+
"f",
|
|
89
|
+
"g",
|
|
90
|
+
"h",
|
|
91
|
+
"i",
|
|
92
|
+
"j",
|
|
93
|
+
"k",
|
|
94
|
+
"l",
|
|
95
|
+
"m",
|
|
96
|
+
"n",
|
|
97
|
+
"o",
|
|
98
|
+
"p",
|
|
99
|
+
"q",
|
|
100
|
+
"r",
|
|
101
|
+
"s",
|
|
102
|
+
"t",
|
|
103
|
+
"u",
|
|
104
|
+
"v",
|
|
105
|
+
"w",
|
|
106
|
+
"x",
|
|
107
|
+
"y",
|
|
108
|
+
"z"
|
|
109
|
+
]
|
|
110
|
+
|
|
111
|
+
WHITE_SPACE_ORDS = [ord(i) for i in WHITE_SPACE_CHARS]
|
|
112
|
+
|
|
113
|
+
OPERATOR_ORDS = [ord(i) for i in OPERATOR_CHARS]
|
|
114
|
+
|
|
115
|
+
NUMBER_ORDS = [ord(i) for i in NUMBER_CHARS]
|
|
116
|
+
|
|
117
|
+
LETTER_ORDS = [ord(i) for i in LETTER_CHARS]
|
|
118
|
+
|
|
119
|
+
WHITE_SPACE_DICT = dict(zip(WHITE_SPACE_CHARS, WHITE_SPACE_ORDS))
|
|
120
|
+
|
|
121
|
+
OPERATORS_DICT = dict(zip(OPERATOR_CHARS, OPERATOR_ORDS))
|
|
122
|
+
|
|
123
|
+
NUMBER_DICT = dict(zip(NUMBER_CHARS, NUMBER_ORDS))
|
|
124
|
+
|
|
125
|
+
LETTER_DICT = dict(zip(LETTER_CHARS, LETTER_ORDS))
|
dubslib/io/__init__.py
ADDED
dubslib/io/data/io.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# This file is part of dubslib
|
|
2
|
+
# Copyright (C) 2026 Donald Raymond Reilly Jr.
|
|
3
|
+
#
|
|
4
|
+
# This program is free software: you can redistribute it and/or modify
|
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
|
6
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
# (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
# GNU General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU General Public License
|
|
15
|
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
|
|
17
|
+
import json
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from dubslib.io.exceptions import UnsupportedFormatError
|
|
20
|
+
|
|
21
|
+
# WORK [ ]: Module doc string.
|
|
22
|
+
class Persistence:
|
|
23
|
+
"""
|
|
24
|
+
Provides a unified entry point for data structure Input and output.
|
|
25
|
+
|
|
26
|
+
Persistence provides an ergonomic API file input and output.
|
|
27
|
+
"""
|
|
28
|
+
def __init__(self):
|
|
29
|
+
|
|
30
|
+
self._loaders = {
|
|
31
|
+
"json":(json, json.load)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
self._dumpers = {
|
|
35
|
+
"json": (json.dump, {"indent": 4, "default": str})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
def to_file(self, pydict: dict, file_path: (Path | str), format: str):
|
|
39
|
+
"""
|
|
40
|
+
Write the provided dictionary to file, using the chosen format.
|
|
41
|
+
|
|
42
|
+
Params:
|
|
43
|
+
pydict(dict): The python dictionary to be saved to file.
|
|
44
|
+
file_path(str | Path): Path to file.
|
|
45
|
+
format(str): Format for saved dictionary if None default is used.
|
|
46
|
+
|
|
47
|
+
Raises:
|
|
48
|
+
UnsupportedFormatError(format; AcceptedFormats)
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
if format not in self._dumpers:
|
|
52
|
+
raise UnsupportedFormatError(format, self._loaders.keys())
|
|
53
|
+
else:
|
|
54
|
+
|
|
55
|
+
with open(file_path, 'w', encoding="utf-8") as dict_file:
|
|
56
|
+
self._dumpers[format][0](pydict, dict_file, **self._dumpers[format][1])
|
|
57
|
+
|
|
58
|
+
def from_file(self, file_path, format):
|
|
59
|
+
"""
|
|
60
|
+
Reads a data set from file and returns a dictionary.
|
|
61
|
+
|
|
62
|
+
Params:
|
|
63
|
+
file_path(str | path): Path to a file.
|
|
64
|
+
format: The format of the saved file.
|
|
65
|
+
|
|
66
|
+
Raises:
|
|
67
|
+
UnsupportedFormatError: If format not Supported
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
if format not in self._loaders:
|
|
71
|
+
raise UnsupportedFormatError(format, self._loaders.keys())
|
|
72
|
+
else:
|
|
73
|
+
with open(file_path, 'r', encoding="utf-8") as config_file:
|
|
74
|
+
return self._loaders[format](config_file)
|
|
75
|
+
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# This file is part of dubslib
|
|
2
|
+
# Copyright (C) 2026 Donald Raymond Reilly Jr.
|
|
3
|
+
#
|
|
4
|
+
# This program is free software: you can redistribute it and/or modify
|
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
|
6
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
# (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
# GNU General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU General Public License
|
|
15
|
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
|
|
17
|
+
class SerializationError(Exception):
|
|
18
|
+
"""
|
|
19
|
+
Base exception for all serialization errors.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
class UnsupportedFormatError(SerializationError):
|
|
25
|
+
|
|
26
|
+
def __init__(self, format, supported_formats):
|
|
27
|
+
"""
|
|
28
|
+
Unsupported serialization format.
|
|
29
|
+
|
|
30
|
+
Params:
|
|
31
|
+
format(module): The intended format of the serialized data.
|
|
32
|
+
supported_formats(list[module]): A list of the supported formats.
|
|
33
|
+
"""
|
|
34
|
+
self.format = format
|
|
35
|
+
|
|
36
|
+
self.supported_formats = tuple(supported_formats)
|
|
37
|
+
|
|
38
|
+
error_message = (f"Unexpected format: {format!r} ."
|
|
39
|
+
f"Expected one of; {', '.join(supported_formats)}")
|
|
40
|
+
super().__init__(error_message)
|
|
41
|
+
|
|
42
|
+
class UnsupportedReadModeError(SerializationError):
|
|
43
|
+
|
|
44
|
+
def __init__(self, read_mode, supported_modes):
|
|
45
|
+
"""
|
|
46
|
+
Unsupported serialization format.
|
|
47
|
+
|
|
48
|
+
Params:
|
|
49
|
+
read_mode(str): The read mode requested.
|
|
50
|
+
supported_modes(list[strings]): A list of the supported modes.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
self.supported_modes = tuple(supported_modes)
|
|
54
|
+
|
|
55
|
+
error_message = (f"Unexpected Read Mode: {read_mode!r} ."
|
|
56
|
+
f"Expected one of; {', '.join(supported_modes)}")
|
|
57
|
+
super().__init__(error_message)
|
dubslib/io/raw/i.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# This file is part of dubslib
|
|
2
|
+
# Copyright (C) 2026 Donald Raymond Reilly Jr.
|
|
3
|
+
#
|
|
4
|
+
# This program is free software: you can redistribute it and/or modify
|
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
|
6
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
# (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
# GNU General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU General Public License
|
|
15
|
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from dubslib.io.exceptions import UnsupportedReadModeError
|
|
19
|
+
|
|
20
|
+
def _yield_from_file(file_path: str):
|
|
21
|
+
"""
|
|
22
|
+
Yeild lines from the provided file. Defaults to utf-8 encoding.
|
|
23
|
+
|
|
24
|
+
Params:
|
|
25
|
+
file_path(str): Path to the file.
|
|
26
|
+
|
|
27
|
+
Yields:
|
|
28
|
+
str: Each line in the file (including newline characters).
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
with open(file_path, mode="r", encoding="utf-8") as rawtext:
|
|
32
|
+
yield from rawtext
|
|
33
|
+
|
|
34
|
+
def _list_from_file(file_path: str)-> list:
|
|
35
|
+
"""
|
|
36
|
+
Read all lines from a file into a list.
|
|
37
|
+
|
|
38
|
+
Params:
|
|
39
|
+
file_path(str): Path to the file.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
list[str]: List of lines (including newline characters).
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
with open(file_path, mode="r", encoding="utf-8") as rawtext:
|
|
46
|
+
return list(rawtext)
|
|
47
|
+
|
|
48
|
+
def _string_from_file(file_path: str | Path)-> str:
|
|
49
|
+
"""
|
|
50
|
+
Read entire file contents as a single string.
|
|
51
|
+
|
|
52
|
+
Params:
|
|
53
|
+
file_path(str): Path to the file.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
str: Full file contents.
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
with open(file_path, mode="r", encoding="utf-8") as rawtext:
|
|
60
|
+
return rawtext.read()
|
|
61
|
+
|
|
62
|
+
def read_from_file(file_path: str | Path, read_mode: str):
|
|
63
|
+
"""
|
|
64
|
+
Reads a file, read_from_file either yields a list, reads the entire contents to a single string, or reads the contents to a list of lines.
|
|
65
|
+
|
|
66
|
+
Params:
|
|
67
|
+
file_path(str): Path to the file to read from.
|
|
68
|
+
read_mode(str): How the file is to be read.
|
|
69
|
+
Accepted: "String", "List", "Yield", "string", "list", "yield, 'S', 'L', 'Y', 's', 'l', 'y'
|
|
70
|
+
|
|
71
|
+
Raises:
|
|
72
|
+
UnsupportedFormatError(format; AcceptedFormats)
|
|
73
|
+
"""
|
|
74
|
+
supported_modes = (
|
|
75
|
+
"String", "List", "Yield",
|
|
76
|
+
"string", "list", "yield",
|
|
77
|
+
'S', 'L', 'Y',
|
|
78
|
+
's', 'l', 'y'
|
|
79
|
+
)
|
|
80
|
+
from_file = {
|
|
81
|
+
"s": _string_from_file,
|
|
82
|
+
"l": _list_from_file,
|
|
83
|
+
"y": _yield_from_file
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if read_mode in supported_modes:
|
|
87
|
+
rm = read_mode[0].lower()
|
|
88
|
+
return from_file[rm](file_path)
|
|
89
|
+
else:
|
|
90
|
+
raise UnsupportedReadModeError(read_mode, supported_modes)
|
dubslib/io/raw/o.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# This file is part of dubslib
|
|
2
|
+
# Copyright (C) 2026 Donald Raymond Reilly Jr.
|
|
3
|
+
#
|
|
4
|
+
# This program is free software: you can redistribute it and/or modify
|
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
|
6
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
7
|
+
# (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
# GNU General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU General Public License
|
|
15
|
+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
|
|
17
|
+
from dubslib.io.exceptions import UnsupportedFormatError
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
def _write_string_to_file(file_path: str, content: str):
|
|
21
|
+
"""
|
|
22
|
+
Overwrite a file with a string.
|
|
23
|
+
|
|
24
|
+
Params:
|
|
25
|
+
file_path(str): Path to the file.
|
|
26
|
+
content(str): Text to write.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
with open(file_path, "w", encoding="utf-8") as f:
|
|
30
|
+
f.write(content)
|
|
31
|
+
|
|
32
|
+
def _write_lines_to_file(file_path: str, lines: list[str]):
|
|
33
|
+
"""
|
|
34
|
+
Overwrite a file with a sequence of lines.
|
|
35
|
+
|
|
36
|
+
Params:
|
|
37
|
+
file_path(str): Path to the file.
|
|
38
|
+
lines (Iterable[str]): Lines to write (must include newline characters if desired).
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
with open(file_path, "w", encoding="utf-8") as f:
|
|
42
|
+
f.writelines(lines)
|
|
43
|
+
|
|
44
|
+
def _append_string_to_file(file_path: str, content: str):
|
|
45
|
+
"""
|
|
46
|
+
Append a string to the end of a file.
|
|
47
|
+
|
|
48
|
+
Params:
|
|
49
|
+
file_path(str): Path to the file.
|
|
50
|
+
content(str): Text to append.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
with open(file_path, "a", encoding="utf-8") as f:
|
|
54
|
+
f.write(content)
|
|
55
|
+
|
|
56
|
+
def _append_lines_to_file(file_path: str, lines: list[str]):
|
|
57
|
+
"""
|
|
58
|
+
Append multiple lines to the end of a file.
|
|
59
|
+
|
|
60
|
+
Params:
|
|
61
|
+
file_path(str): Path to the file.
|
|
62
|
+
lines(Iterable[str]): Lines to append (must include newline characters if desired).
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
with open(file_path, "a", encoding="utf-8") as f:
|
|
66
|
+
f.writelines(lines)
|
|
67
|
+
|
|
68
|
+
def write_to_file(file_path: str| Path,
|
|
69
|
+
content: str | list[str],
|
|
70
|
+
write_mode: str
|
|
71
|
+
):
|
|
72
|
+
"""
|
|
73
|
+
Writes/appends contents of a list or string to the specified file. The
|
|
74
|
+
write type is not assumed and must be provided.
|
|
75
|
+
|
|
76
|
+
Params:
|
|
77
|
+
file_path(str): Path to the file to overwrite, create or append to.
|
|
78
|
+
content(str): String or list of strings to write to file.
|
|
79
|
+
write_mode(str): How to write the file.
|
|
80
|
+
Accepted: "Write", "Appened", "write", "appened", 'w', 'a'
|
|
81
|
+
|
|
82
|
+
Raises:
|
|
83
|
+
UnsupportedFormatError(format; AcceptedFormats)
|
|
84
|
+
"""
|
|
85
|
+
to_file = {
|
|
86
|
+
"w": {
|
|
87
|
+
str: _write_string_to_file,
|
|
88
|
+
list: _write_lines_to_file
|
|
89
|
+
},
|
|
90
|
+
"a": {
|
|
91
|
+
str: _append_string_to_file,
|
|
92
|
+
list: _append_lines_to_file
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
mode = write_mode[0].lower()
|
|
96
|
+
if mode in to_file:
|
|
97
|
+
to_file[mode][type(content)](file_path, content)
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
|
|
2
|
+
class DescriptorExample:
|
|
3
|
+
"""Simple descriptor to test descriptor behavior."""
|
|
4
|
+
def __init__(self, name):
|
|
5
|
+
self.name = name
|
|
6
|
+
|
|
7
|
+
def __get__(self, instance, owner):
|
|
8
|
+
return f"Descriptor __get__ called on {instance} for {self.name}"
|
|
9
|
+
|
|
10
|
+
def __set__(self, instance, value):
|
|
11
|
+
instance.__dict__[self.name] = value
|
|
12
|
+
|
|
13
|
+
def __delete__(self, instance):
|
|
14
|
+
del instance.__dict__[self.name]
|
|
15
|
+
|
|
16
|
+
class GoldenBase:
|
|
17
|
+
"""Base class to test inheritance."""
|
|
18
|
+
base_class_variable = 42
|
|
19
|
+
|
|
20
|
+
def base_method(self):
|
|
21
|
+
return "Base method called"
|
|
22
|
+
|
|
23
|
+
class GoldenClass(GoldenBase):
|
|
24
|
+
"""A fully loaded class for introspection testing."""
|
|
25
|
+
|
|
26
|
+
# Class variables
|
|
27
|
+
class_variable = "I am a class variable"
|
|
28
|
+
another_class_variable: int = 123
|
|
29
|
+
|
|
30
|
+
# Descriptor
|
|
31
|
+
descriptor_field = DescriptorExample("descriptor_field")
|
|
32
|
+
|
|
33
|
+
# Nested class
|
|
34
|
+
class Nested:
|
|
35
|
+
"""Nested class inside GoldenClass."""
|
|
36
|
+
nested_var = "I am nested"
|
|
37
|
+
|
|
38
|
+
def nested_method(self):
|
|
39
|
+
return "Nested method called"
|
|
40
|
+
|
|
41
|
+
def __init__(self, x: int = 5, y: str = "hello", *args, **kwargs):
|
|
42
|
+
self.instance_var = "I am an instance variable"
|
|
43
|
+
self._private_var = "I am private"
|
|
44
|
+
self.x = x
|
|
45
|
+
self.y = y
|
|
46
|
+
self.args = args
|
|
47
|
+
self.kwargs = kwargs
|
|
48
|
+
|
|
49
|
+
# Regular method
|
|
50
|
+
def method_one(self, param1, param2: int = 10) -> str:
|
|
51
|
+
"""This is method one"""
|
|
52
|
+
local_variable = "I am a local variable in method one"
|
|
53
|
+
return f"Method one called with {param1} and {param2}"
|
|
54
|
+
|
|
55
|
+
# Method with *args, **kwargs, keyword-only args
|
|
56
|
+
def method_two(self, a, b=3, *args, c, d=4, **kwargs):
|
|
57
|
+
"""Method with complex signature"""
|
|
58
|
+
local_variable = "Local var in method two"
|
|
59
|
+
return a, b, c, d, args, kwargs
|
|
60
|
+
|
|
61
|
+
# Static method
|
|
62
|
+
@staticmethod
|
|
63
|
+
def static_method(alpha: float = 1.5):
|
|
64
|
+
"""Static method example"""
|
|
65
|
+
return alpha * 2
|
|
66
|
+
|
|
67
|
+
# Class method
|
|
68
|
+
@classmethod
|
|
69
|
+
def class_method(cls, name: str):
|
|
70
|
+
"""Class method example"""
|
|
71
|
+
return f"Called on {cls.__name__} with {name}"
|
|
72
|
+
|
|
73
|
+
# Property with getter, setter, deleter
|
|
74
|
+
@property
|
|
75
|
+
def my_property(self) -> str:
|
|
76
|
+
"""This is a property"""
|
|
77
|
+
some_local_variable = "Local var in getter"
|
|
78
|
+
return self._private_var
|
|
79
|
+
|
|
80
|
+
@my_property.setter
|
|
81
|
+
def my_property(self, value: str = "that", other_value: str = "this", *, x, y) -> None:
|
|
82
|
+
"""Setter for my_property"""
|
|
83
|
+
some_local_variable = "Local var in setter"
|
|
84
|
+
self._private_var = value
|
|
85
|
+
|
|
86
|
+
@my_property.deleter
|
|
87
|
+
def my_property(self) -> None:
|
|
88
|
+
"""Deleter for my_property"""
|
|
89
|
+
del self._private_var
|
|
90
|
+
|
|
91
|
+
# Method referencing nested class
|
|
92
|
+
def create_nested(self):
|
|
93
|
+
"""Returns an instance of the nested class."""
|
|
94
|
+
return self.Nested()
|
|
95
|
+
|
|
96
|
+
# Method returning a lambda (tests function parsing)
|
|
97
|
+
def lambda_factory(self):
|
|
98
|
+
"""Returns a lambda function."""
|
|
99
|
+
return lambda z: z * 2
|