This repository has been archived by the owner on Aug 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurtleDB_system.py
126 lines (117 loc) · 4.57 KB
/
TurtleDB_system.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import contextlib
def find_between(filepath,first_word, second_word, occurrence):
with open(filepath, 'r') as file:
lines = file.readlines()
first_line_number = None
second_line_number = None
count = 0
for i, line in enumerate(lines):
if first_word in line:
count += 1
if count == occurrence:
first_line_number = i
if second_word in line and count == occurrence:
second_line_number = i
break
if first_line_number is not None and second_line_number is not None:
words_between = []
for i in range(first_line_number + 1, second_line_number):
line = lines[i].strip()
words_between.append(lines[i])
return words_between
else:
return [] # If either first_word or second_word is not found in the file or occurrence is not found
def find_between2(code:str,first_word, second_word, occurrence):
lines = code.strip().split("\n")
first_line_number = None
second_line_number = None
count = 0
for i, line in enumerate(lines):
if first_word in line:
count += 1
if count == occurrence:
first_line_number = i
if second_word in line and count == occurrence:
second_line_number = i
break
if first_line_number is not None and second_line_number is not None:
words_between = []
for i in range(first_line_number + 1, second_line_number):
line = lines[i].strip()
words_between.append(lines[i])
return words_between
else:
return [] # If either first_word or second_word is not found in the file or occurrence is not found
def find_between2_2(code:str,first_word, second_word, occurrence):
lines = code.split("\n")
first_line_number = None
second_line_number = None
count = 0
for i, line in enumerate(lines):
if first_word in line:
count += 1
if count == occurrence:
first_line_number = i
if second_word in line and count == occurrence:
second_line_number = i
break
if first_line_number is not None and second_line_number is not None:
words_between = []
for i in range(first_line_number + 1, second_line_number):
line = lines[i].strip()
words_between.append(lines[i])
return words_between
else:
return [] # If either first_word or second_word is not found in the file or occurrence is not found
def find_between3(text, firstword, secondword):
start_index = text.find(firstword) + len(firstword)
end_index = text.find(secondword)
if start_index == -1 or end_index == -1:
return None
return text[start_index:end_index]
def list_to_string(lst):
string = ''.join(lst)
return string.replace('\\n', '\n')
def list_to_string2(lst):
string = '\n'.join(lst)
return string.replace('\\n', '\n')
class system():
def __init__(self):
pass
def createDB(self,filename:str):
with contextlib.suppress(FileExistsError):
open(f'{filename}.TDB', 'x').close()
def opencontainer(self,filename:str,container:str,containernumber:int):
try:
self.read = open(filename,'r')
self.x = self.read.read()
self.datalist = self.x.strip().split("\n")
self.read.close()
return find_between2(self.x,f'<{container} s data>{containernumber}',f'<{container} e data>{containernumber}',1)
except FileNotFoundError:
print('file not found error')
def amount(self, sub, word):
return sum(word[i:i+len(sub)] == sub for i in range(len(word)))
def openall(self,filename:str,printfile:bool):
try:
self.read = open(filename,'r')
self.x = self.read.read()
if printfile == True:
print(self.x)
if printfile == False:
pass
if printfile != True and printfile != False:
print('printfile parameter has an invalid value')
exit()
self.datalist = self.x.strip().split("\n")
self.read.close()
self.amountvar = self.amount('data>',self.x)
self.realamount = self.amountvar / 2
self.finlist = []
for i in range(int(self.realamount)):
i += 1
self.finlist.append(find_between2(self.x,f's data>{i}',f'e data>{i}',1))
return self.finlist
except FileNotFoundError:
print('file not found error')
TurtleDB = system()