-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.py
72 lines (60 loc) · 1.69 KB
/
list.py
1
2
3
4
5
6
7
8
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!usr/bin/python3
"""
Module Name:
list
Module Description:
This module contains one class which is a subclass of the built-in `list`
Module Classes:
- List(list)
Module Attributes:
- None
"""
class List(list):
"""
The `List` class is a subclass of the built-in `list` class in Python.
It adds several methods to the `list` class, including `print`, `pint`,
`add`, `swap`, `to_dict`, `save`, and `reload`.
"""
def print(self) -> None:
"""
Prints the elements in the list separated by a newline character.
"""
if self:
print(*self, sep="\n")
def pint(self) -> None:
"""
Prints the first element in the list.
"""
print(self[0])
def add(self, value) -> None:
"""
Adds a value to the second position in the list.
"""
self.insert(1, value)
def swap(self) -> None:
"""
Swaps the first two elements in the list.
"""
self[0], self[1] = self[1], self[0]
def to_dict(self) -> dict:
"""
Converts the list to a dictionary with a single key "data".
"""
return {"data": self}
def save(self) -> None:
"""
Saves the list depending on the `storage` module.
"""
from .__init__ import storage
dict_list = self.to_dict()
storage.save(dict_list)
def reload(self):
"""
Loads the list depending on the `storage`
module and appends the elements to the list.
"""
from .__init__ import storage
data = storage.reload()
if data is not None:
for value in list(data.values())[0]:
self.append(value)