-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.py
More file actions
194 lines (160 loc) · 5.09 KB
/
hello.py
File metadata and controls
194 lines (160 loc) · 5.09 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import json
class Node:
def __init__(self, data, text, link):
self.item = data
self.nref = None
self.pref = None
self.text = text
self.link = link
class DoublyLinkedList:
def __init__(self):
self.start_node = None
def print_LL(self):
if self.start_node is None:
print("List has no element")
return
else:
n = self.start_node
while n is not None:
print(n.item, " ")
n = n.nref
def __iter__(self):
return _IteradorListaEnlazada(self.start_node)
def __str__(self):
# defining a blank res variable
res = ""
# initializing ptr to head
ptr = self.start_node
# traversing and adding it to res
while ptr:
res += str(ptr.item) + ", "
ptr = ptr.nref
# removing trailing commas
res = res.strip(", ")
# chen checking if
# anything is present in res or not
if len(res):
return "[" + res + "]"
else:
return "[]"
def insert_at_end(self, data, text, link):
if self.start_node is None:
new_node = Node(data, text, link)
self.start_node = new_node
return
n = self.start_node
while n.nref is not None:
n = n.nref
new_node = Node(data, text, link)
n.nref = new_node
new_node.pref = n
def insert_after_item(self, x, data, text, link):
if self.start_node is None:
print("List is empty")
return
else:
n = self.start_node
while n is not None:
if n.item == x:
break
n = n.nref
if n is None:
print("item not in the list")
else:
new_node = Node(data, text, link)
new_node.pref = n
new_node.nref = n.nref
if n.nref is not None:
n.nref.prev = new_node
n.nref = new_node
class _IteradorListaEnlazada(object):
" Iterador para la clase ListaEnlazada "
def __init__(self, prim):
""" Constructor del iterador.
prim es el primer elemento de la lista. """
self.actual = prim
def next(self):
""" Devuelve uno a uno los elementos de la lista. """
if self.actual == None:
raise StopIteration("No hay más elementos en la lista")
# Guarda el dato
dato = self.actual.dato
# Avanza en la lista
self.actual = self.actual.prox
# Devuelve el dato
return dato
def crearLista():
linkedList = DoublyLinkedList()
"Leer Archivo Json"
with open('datos.json', encoding='utf-8') as file:
data = json.load(file)
"Crear Lista Enlazada"
for i in data:
linkedList.insert_at_end(i, i, data[i])
linkedList.print_LL()
return linkedList
lista_enlazada = crearLista()
"Insertar despues de un item"
antes = input("Insertar después de: ->")
despues = input("Insertar item: ->")
print("https://ready2cut.blogspot.com/search/label/")
link = input("Insertar link: ->")
lista_enlazada.insert_after_item(antes, despues, despues, link)
nuevos_datos = {}
def guardarDatos(linked_list):
if linked_list.start_node is None:
print("List has no element")
return
else:
n = linked_list.start_node
while n is not None:
nuevos_datos[n.item] = n.link
n = n.nref
guardarDatos(lista_enlazada)
"Guardar Archivo Json"
with open('datos.json', 'w', encoding="utf-8") as file:
json.dump(nuevos_datos, file, ensure_ascii=False, indent=4)
lista_enlazada_nueva = crearLista()
def textoHTML(linked_list):
listaHTML = []
contador = 0
a = "<b:widget-setting name='text-"
b = "'>"
c = "</b:widget-setting>"
if linked_list.start_node is None:
print("List has no element")
return
else:
n = linked_list.start_node
while n is not None:
listaHTML.append(a + str(contador) + b + n.text + c)
n = n.nref
contador += 1
return listaHTML
def linkHTML(linked_list):
listaHTML = []
contador = 0
a = "<b:widget-setting name='link-"
b = "'>"
c = "</b:widget-setting>"
if linked_list.start_node is None:
print("List has no element")
return
else:
n = linked_list.start_node
while n is not None:
listaHTML.append(a + str(contador) + b + n.link + c)
n = n.nref
contador += 1
return listaHTML
textos = textoHTML(lista_enlazada_nueva)
links = linkHTML(lista_enlazada_nueva)
f = open ('widgets.txt','w')
f.write("<b:widget-settings>"+'\n')
f.write("<b:widget-setting name='sorting'>NONE</b:widget-setting>"+'\n')
for i in textos:
f.write(i+'\n')
for i in links:
f.write(i+'\n')
f.write("</b:widget-settings>"+'\n')
f.close()