|
| 1 | +"#[número] - [lenguaje_utilizado]" |
| 2 | +# |
| 3 | +# IMPORTANTE: Sólo debes subir el fichero de código como parte del ejercicio. |
| 4 | +# |
| 5 | +# EJERCICIO: |
| 6 | +# Desarrolla un programa capaz de crear un archivo XML y JSON que guarde los |
| 7 | +# siguientes datos (haciendo uso de la sintaxis correcta en cada caso): |
| 8 | +# - Nombre |
| 9 | +# - Edad |
| 10 | +# - Fecha de nacimiento |
| 11 | +# - Listado de lenguajes de programación |
| 12 | +# Muestra el contenido de los archivos. |
| 13 | +# Borra los archivos. |
| 14 | +# |
| 15 | +# DIFICULTAD EXTRA (opcional): |
| 16 | +# Utilizando la lógica de creación de los archivos anteriores, crea un |
| 17 | +# programa capaz de leer y transformar en una misma clase custom de tu |
| 18 | +# lenguaje los datos almacenados en el XML y el JSON. |
| 19 | +# Borra los archivos. |
| 20 | +# |
| 21 | + |
| 22 | +import os |
| 23 | +from random import randint |
| 24 | +from random import choice |
| 25 | +from datetime import date |
| 26 | +from datetime import datetime |
| 27 | + |
| 28 | +def serparacion(cadena): |
| 29 | + print('{}'.format(cadena * 20)) |
| 30 | + |
| 31 | +import json as jsonlib |
| 32 | +import xml.etree.ElementTree as xmllib |
| 33 | + |
| 34 | +lenguajes_programacion = [ |
| 35 | + "Python", |
| 36 | + "JavaScript", |
| 37 | + "Java", |
| 38 | + "C#", |
| 39 | + "C++", |
| 40 | + "C", |
| 41 | + "TypeScript", |
| 42 | + "Ruby", |
| 43 | + "PHP", |
| 44 | + "Swift", |
| 45 | + "Kotlin", |
| 46 | + "Go", |
| 47 | + "Rust", |
| 48 | + "SQL", |
| 49 | + "Perl", |
| 50 | + "Scala", |
| 51 | + "Lua", |
| 52 | + "R", |
| 53 | + "Dart", |
| 54 | + "Haskell" |
| 55 | +] |
| 56 | + |
| 57 | +personajes_clasicos = [ |
| 58 | + # Looney Tunes / Merrie Melodies |
| 59 | + "Bugs Bunny", |
| 60 | + "Pato Lucas", |
| 61 | + "Porky", |
| 62 | + "Silvestre", |
| 63 | + "Piolín", |
| 64 | + "Correcaminos", |
| 65 | + "Wile E. Coyote", |
| 66 | + "Sam Bigotes", |
| 67 | + "Elmer Gruñón", |
| 68 | + |
| 69 | + # Hanna-Barbera |
| 70 | + "Pedro Picapiedra", |
| 71 | + "Pablo Mármol", |
| 72 | + "Scooby-Doo", |
| 73 | + "Shaggy Rogers", |
| 74 | + "Oso Yogui", |
| 75 | + "Bubu", |
| 76 | + "Don Gato", |
| 77 | + "Benito Bodoque", |
| 78 | + "Tiro Loco McGraw", |
| 79 | + "Supersónico", # (George Jetson) |
| 80 | + |
| 81 | + # MGM / Fleischer / Varios |
| 82 | + "Tom", |
| 83 | + "Jerry", |
| 84 | + "Popeye", |
| 85 | + "Olivia Olivo", |
| 86 | + "Brutus", |
| 87 | + "La Pantera Rosa", |
| 88 | + "Pájaro Loco", # (Woody Woodpecker) |
| 89 | + "Félix el Gato", |
| 90 | + |
| 91 | + # Disney (Clásicos) |
| 92 | + "Mickey Mouse", |
| 93 | + "Pato Donald", |
| 94 | + "Goofy" |
| 95 | +] |
| 96 | + |
| 97 | + |
| 98 | +class Persona: |
| 99 | + |
| 100 | + def __init__(self, name: str, program_lenguages: list, birth_date=None, age=None): |
| 101 | + self.name = name |
| 102 | + self.program_lenguages = program_lenguages |
| 103 | + self.birth_date = birth_date |
| 104 | + self.age = age |
| 105 | + if self.birth_date == None or self.age == None: self.create_date() |
| 106 | + |
| 107 | + def __str__(self) -> str: |
| 108 | + return f'Nombre: {self.name}, Birth: {self.date_spain()}, Age: {self.age}, Program lenguages: {self.program_lenguages}' |
| 109 | + |
| 110 | + def date_spain(self) -> str: |
| 111 | + return f'{self.birth_date.day:02}/{self.birth_date.month:02}/{self.birth_date.year:04}' |
| 112 | + |
| 113 | + def create_date(self) -> tuple[date, int]: |
| 114 | + self.syear = randint(1900, 2025) |
| 115 | + self.smonth = randint(1, 12) |
| 116 | + if self.smonth == 2 and self.syear % 4 == 0: |
| 117 | + self.sday = randint(1, 29) |
| 118 | + elif self.smonth == 2: |
| 119 | + self.sday = randint(1, 28) |
| 120 | + elif self.smonth in [1, 3, 5, 7, 8, 10, 12]: |
| 121 | + self.sday = randint(1, 31) |
| 122 | + elif self.smonth in [4, 6, 9, 11]: |
| 123 | + self.sday = randint(1, 30) |
| 124 | + self.birth_date = date(self.syear, self.smonth, self.sday) |
| 125 | + self.age = (2025 - self.syear) |
| 126 | + |
| 127 | +def create_json_xml(): |
| 128 | + students = [] |
| 129 | + for indxstu in range(randint(7,20)): |
| 130 | + lenguage = [] |
| 131 | + for indxlg in range(randint(1,5)): |
| 132 | + while True: |
| 133 | + lg = choice(lenguajes_programacion) |
| 134 | + if lg not in lenguage: |
| 135 | + lenguage.append(lg) |
| 136 | + break |
| 137 | + namecho = choice(personajes_clasicos) |
| 138 | + personajes_clasicos.remove(namecho) |
| 139 | + students.append(Persona(namecho, lenguage)) |
| 140 | + |
| 141 | + with open("barrancus.json", mode="w", encoding="utf-8") as write_file: |
| 142 | + write_file.write('[\n') |
| 143 | + counter = 0 |
| 144 | + for student in students: |
| 145 | + counter += 1 |
| 146 | + x = { |
| 147 | + "Nombre": student.name, |
| 148 | + "Edad": student.age, |
| 149 | + "Nacimiento": student.date_spain(), |
| 150 | + "Lenguajes": student.program_lenguages |
| 151 | + } |
| 152 | + jsonlib.dump(x, write_file, indent=4) |
| 153 | + if counter != len(students): write_file.write(',\n') |
| 154 | + write_file.write('\n]') |
| 155 | + |
| 156 | + with open("barrancus.json", "r") as json_file: |
| 157 | + # loading json file data to variable data |
| 158 | + data = jsonlib.load(json_file) |
| 159 | + |
| 160 | + # Building the root element of the xml file |
| 161 | + root = xmllib.Element("Estudiantes") |
| 162 | + root.tag = "Estudiantes" |
| 163 | + root.text = '\n\t' |
| 164 | + countline = 0 |
| 165 | + for line in data: |
| 166 | + countline += 1 |
| 167 | + stud = xmllib.SubElement(root, "Estudiante") |
| 168 | + stud.text = '\n\t\t' |
| 169 | + if len(students) == countline: |
| 170 | + stud.tail = '\n' |
| 171 | + else: |
| 172 | + stud.tail = '\n\t' |
| 173 | + name = xmllib.SubElement(stud, "Nombre") |
| 174 | + name.text = f'{line["Nombre"]}' |
| 175 | + name.tail = '\n\t\t' |
| 176 | + age = xmllib.SubElement(stud, "Edad") |
| 177 | + age.text = str(line["Edad"]) |
| 178 | + age.tail = '\n\t\t' |
| 179 | + birthday = xmllib.SubElement(stud, "Nacimiento") |
| 180 | + birthday.text = str(line["Nacimiento"]) |
| 181 | + birthday.tail = '\n\t\t' |
| 182 | + lenguages = xmllib.SubElement(stud, "Lenguajes") |
| 183 | + lenguages.text = str(line["Lenguajes"]) |
| 184 | + lenguages.tail = '\n\t' |
| 185 | + |
| 186 | + tree = xmllib.ElementTree(root) |
| 187 | + tree.write("barrancus.xml", encoding= "UTF-8", method= 'xml') |
| 188 | + |
| 189 | +def obtaintopersona(): |
| 190 | + personas_json = [] |
| 191 | + with open("barrancus.json", "r") as json_file: |
| 192 | + # loading json file data to variable data |
| 193 | + data_json = jsonlib.load(json_file) |
| 194 | + |
| 195 | + for line in data_json: |
| 196 | + birth = datetime.strptime(line["Nacimiento"], "%d/%m/%Y") |
| 197 | + personas_json.append(Persona(str(line["Nombre"]), line["Lenguajes"], birth, line["Edad"])) |
| 198 | + |
| 199 | + for persona in personas_json: |
| 200 | + print(persona) |
| 201 | + |
| 202 | + serparacion('-----') |
| 203 | + personas_xml = [] |
| 204 | + data_xml = xmllib.parse("barrancus.xml") |
| 205 | + root = data_xml.getroot() |
| 206 | + for child in root: |
| 207 | + estudent_xml=[subchild.text for subchild in child] |
| 208 | + personas_xml.append(Persona( |
| 209 | + estudent_xml[0], |
| 210 | + estudent_xml[3], |
| 211 | + datetime.strptime(estudent_xml[2], "%d/%m/%Y"), |
| 212 | + int(estudent_xml[1]) |
| 213 | + ) |
| 214 | + ) |
| 215 | + for personita in personas_xml: |
| 216 | + print(personita) |
| 217 | + |
| 218 | +def main(): |
| 219 | + create_json_xml() |
| 220 | + obtaintopersona() |
| 221 | + os.remove("barrancus.json") |
| 222 | + os.remove("barrancus.xml") |
| 223 | + |
| 224 | +main() |
0 commit comments