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
| # coding: utf-8
import random
from dataclasses import dataclass
from bs4 import BeautifulSoup
from datetime import datetime, timedelta ,date
import time
#from lxml import *
from lxml import etree
@dataclass
class Ametropie:
oeil :str = None
sphere : float = 0.0
cylindre :float = 0.0
axeDuCylindre :int = 0
addition : float = 0.0
photochromie :int =0
def xml(self):
resultat = f"<ametropie>\r"
resultat += f"<oeil>{self.oeil}</oeil>\r"
resultat += f"<sphere>{self.sphere}</sphere>\r"
resultat += f"<cylindre>{self.cylindre}</cylindre>\r"
resultat += f"<axeDuCylindre>{self.axeDuCylindre}</axeDuCylindre>\r"
resultat += f"<addition>{self.addition}</addition>\r"
resultat += f"<photochromie>{self.photochromie}</photochromie>\r"
resultat += f"</ametropie>"
print(resultat)
print("-----------------------------------")
soup = BeautifulSoup(resultat ,'xml') # Mise en forme
resultatsoup= soup.prettify()
return resultatsoup
def xml_2(self):
ametropie = etree.Element("ametropie")
oeil = etree.SubElement(ametropie, "oeil")
oeil.text = f'{self.oeil}'
sphere = etree.SubElement(ametropie, "sphere")
sphere.text =f'{self.sphere}'
cylindre = etree.SubElement(ametropie, "cylindre")
cylindre.text = f'{self.cylindre}'
axeDuCylindre = etree.SubElement(ametropie, "axeDuCylindre")
axeDuCylindre.text = f'{self.axeDuCylindre}'
addition = etree.SubElement(ametropie, "addition")
addition.text = f'{self.addition}'
photochromie = etree.SubElement(ametropie, "photochromie")
photochromie.text = f'{self.photochromie}'
return etree.tostring(ametropie, pretty_print=True)
def aleaoire(self):
self.axeDuCylindre = random.randint(0, 150)
self.sphere = random.randrange(-100,100,4)/16
self.cylindre = random.randrange(-100,100,4)/16
self.addition = random.randrange(0,16,4)/16
self.photochromie = random.randint(0, 1)
if __name__ == '__main__':
ametropie_OD =Ametropie()
ametropie_OD.oeil ="3D"
ametropie_OD.aleaoire()
xml_am = ametropie_OD.xml()
print(xml_am)
print("------------------------")
xml_am = ametropie_OD.xml_2()
print(xml_am) |
Partager