Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- import re
- insert = """
- INSERT INTO tools (pocket, tool, description, X, Y, Z, A, B, C, U, V, W, D, orientation, frontangle, backangle)
- VALUES (:Pocket, :Tool, ":Comment", :X, :Y, :Z, :A, :B, :C, :U, :V, :W, :Diameter, :Orientation, :Frontangle, :Backangle);
- """
- schema = """
- BEGIN TRANSACTION;
- DROP TABLE IF EXISTS tools;
- CREATE TABLE tools (
- tool INTEGER PRIMARY KEY UNIQUE NOT NULL,
- pocket INTEGER NOT NULL,
- description TEXT DEFAULT "",
- X REAL DEFAULT 0.0,
- Y REAL DEFAULT 0.0,
- Z REAL DEFAULT 0.0,
- A REAL DEFAULT 0.0,
- B REAL DEFAULT 0.0,
- C REAL DEFAULT 0.0,
- U REAL DEFAULT 0.0,
- V REAL DEFAULT 0.0,
- W REAL DEFAULT 0.0,
- D REAL DEFAULT 0.0,
- orientation INTEGER DEFAULT NULL,
- frontangle REAL DEFAULT NULL,
- backangle REAL DEFAULT NULL
- );
- COMMIT;
- """
- def dict_into_query(q, d):
- "replaces each occurence of :key in the input file with the textual representation of the value of d[key]"
- f = re.search(':([\w]+)', q)
- while f:
- if f.group(1) in d:
- if d[f.group(1)]:
- q = q[:f.start()] + str(d[f.group(1)]) + q[f.end():]
- else:
- q = q[:f.start()] + "NULL" + q[f.end():]
- else: #Tags that are in the query, but not the dict
- q = q[:f.start()] + "NULL" + q[f.end():]
- f = re.search(':([\w]+)', q)
- return q
- db = sqlite3.connect('tools.db')
- db.executescript(schema)
- tbl = open("sim.tbl")
- for t in tbl.readlines():
- tool = {'Tool':re.search('[Tt]([0-9+-.]+) ', t),
- 'Pocket':re.search('[Pp]([0-9+-.]+)', t),
- 'X':re.search('[Xx]([0-9+-.]+)', t),
- 'Y':re.search('[Yy]([0-9+-.]+)', t),
- 'Z':re.search('[Zz]([0-9+-.]+)', t),
- 'A':re.search('[Aa]([0-9+-.]+)', t),
- 'B':re.search('[Bb]([0-9+-.]+)', t),
- 'C':re.search('[Cc]([0-9+-.]+)', t),
- 'U':re.search('[Uu]([0-9+-.]+)', t),
- 'V':re.search('[Vv]([0-9+-.]+)', t),
- 'W':re.search('[Ww]([0-9+-.]+)', t),
- 'Diameter':re.search('[Dd]([0-9+-.]+)', t),
- 'Frontangle':re.search('[Ii]([0-9+-.]+)', t),
- 'Backangle':re.search('[Jj]([0-9+-.]+)', t),
- 'Orientation':re.search('[Qq]([0-9+-.]+)', t),
- 'Comment':re.search(';(.*)', t)}
- for m in tool:
- if tool[m]:
- tool[m] = tool[m].group(1)
- db.executescript(dict_into_query(insert, tool))
Advertisement
Add Comment
Please, Sign In to add comment