Advertisement
gregwa

FCM #85 Programming in Python # 54

May 7th, 2014
718
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # makedb.py
  2. # DMC.xml to SQLite database
  3. # For Full Circle Magazine #85
  4.  
  5. import apsw
  6. from xml.etree import ElementTree as ET
  7.  
  8. tablename = "DMC"
  9.  
  10. def ReadXML():
  11.     global connection
  12.     global cursor
  13.     fn = 'dmc.xml'
  14.     tree = ET.parse(fn)
  15.     root = tree.getroot()
  16.     cntr = 0
  17.     for floss in root.findall('floss'):
  18.         name = floss.find('name').text
  19.         desc = floss.find('description').text
  20.         for colour in floss.findall('color'):
  21.             red = colour.find('red').text
  22.             green = colour.find('green').text
  23.             blue = colour.find('blue').text
  24.         SQL = "INSERT INTO DMC (DMC,Description,Red,Green,Blue) VALUES \
  25.               ('%s','%s',%s,%s,%s)" % (name,desc,red,green,blue)
  26.         cursor.execute(SQL)
  27.         print "Working record {0}".format(cntr)
  28.         cntr += 1
  29.  
  30. def OpenDB():
  31.     global connection
  32.     global cursor
  33.     global ucursor
  34.     global dbname
  35.     connection = apsw.Connection("floss.db3")
  36.     cursor = connection.cursor()
  37.     ucursor = connection.cursor()
  38.    
  39. def MakeTables():
  40.     sql = '''CREATE TABLE IF NOT EXISTS DMC
  41.              (pkID INTEGER PRIMARY KEY, DMC INTEGER,
  42.              Description TEXT, Red INTEGER, Green INTEGER, Blue INTEGER,
  43.              HEX TEXT,H INTEGER,S INTEGER,V INTEGER)'''
  44.     cursor.execute(sql)
  45.    
  46. def EmptyTables():
  47.     sql="DELETE FROM %s" % tablename
  48.     cursor.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 UpdateDB():  
  74.     global ucursor
  75.     global cursor  
  76.     sql = "SELECT * FROM DMC"
  77.     c = cursor.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 ("Updating record {0}".format(pkid))
  89.         ucursor.execute(sql2)          
  90.    
  91. OpenDB()   
  92. MakeTables()
  93. EmptyTables() # Just to be safe
  94. ReadXML()
  95. UpdateDB()     
  96. print "Finished"
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement