Advertisement
Guest User

FCM n°85 Programmer en Python n°54

a guest
Nov 24th, 2014
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. # makedb.py
  2. # DMC.xml vers SQLite database
  3. # Pour Full Circle Magazine numero 85
  4.  
  5. import apsw
  6. from xml.etree import ElementTree as ET
  7.  
  8. nomtable = "DMC"
  9.  
  10. def LireXML():
  11.     global connexion
  12.     global curseur
  13.     nomfic = 'dmc.xml'
  14.     arbre = ET.parse(nomfic)
  15.     racine = arbre.getroot()
  16.     compteur = 0
  17.     for fil in racine.findall('floss'):
  18.         nom = fil.find('name').text
  19.         desc = fil.find('description').text
  20.         for couleur in fil.findall('color'):
  21.             rouge = couleur.find('red').text
  22.             vert = couleur.find('green').text
  23.             bleu = couleur.find('blue').text
  24.         SQL = "INSERT INTO DMC (DMC,Description,Rouge,Vert,Bleu) VALUES \
  25.               ('%s','%s',%s,%s,%s)" % (nom,desc,rouge,vert,bleu)
  26.         curseur.execute(SQL)
  27.         print "Enregistrement en cours : {0}".format(compteur)
  28.         compteur += 1
  29.  
  30. def OuvrirBase():
  31.     global connexion
  32.     global curseur
  33.     global ucurseur
  34.     global nombase
  35.     connexion = apsw.Connection("fils.db3")
  36.     curseur = connexion.cursor()
  37.     ucurseur = connexion.cursor()
  38.  
  39. def CreerTables():
  40.     sql = '''CREATE TABLE IF NOT EXISTS DMC
  41.              (pkID INTEGER PRIMARY KEY, DMC INTEGER,
  42.              Description TEXT, Rouge INTEGER, Vert INTEGER, Bleu INTEGER,
  43.              HEX TEXT,H INTEGER,S INTEGER,V INTEGER)'''
  44.     curseur.execute(sql)
  45.  
  46. def ViderTables():
  47.     sql="DELETE FROM %s" % nomtable
  48.     curseur.execute(sql)
  49.  
  50. def rgb2hex(rgb):
  51.     return '%02x%02x%02x' % rgb
  52.  
  53. def rgb2hsv(r, g, b):
  54.     r, g, b = r/255.0, g/255.0, b/255.0
  55.     mx = max(r, g, b)
  56.     mn = min(r, g, b)
  57.     df = mx-mn
  58.     if mx == mn:
  59.         h = 0
  60.     elif mx == r:
  61.         h = (60 * ((g-b)/df) + 360) % 360
  62.     elif mx == g:
  63.         h = (60 * ((b-r)/df) + 120) % 360
  64.     elif mx == b:
  65.         h = (60 * ((r-g)/df) + 240) % 360
  66.     if mx == 0:
  67.         s = 0
  68.     else:
  69.         s = df/mx
  70.     v = mx
  71.     return int(round(h,0)), int(round(s*100,0)), int(round(v*100,0))
  72.  
  73. def MAJBase():
  74.     global ucurseur
  75.     global curseur
  76.     sql = "SELECT * FROM DMC"
  77.     c = curseur.execute(sql)
  78.  
  79.     for i in c:
  80.         pkid = i[0]
  81.         r = i[3]
  82.         g = i[4]
  83.         b = i[5]
  84.         hex = rgb2hex((r,g,b))
  85.         h,s,v = rgb2hsv(r,g,b)
  86.         sql2 = "UPDATE DMC SET hex='{0}',h={1},s={2},v={3} WHERE \
  87.                 pkid = {4}".format(hex,h,s,v,pkid)
  88.         print ("Maj enregistrement {0}".format(pkid))
  89.         ucurseur.execute(sql2)
  90.  
  91. OuvrirBase()
  92. CreerTables()
  93. ViderTables() # Juste pour etre sur
  94. LireXML()
  95. MAJBase()
  96. print "Fin"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement