def update_index(self, rebuild=False): """Update index. Arguments: rebuild -- Rebuild the index from scratch Otherwise, it only updates count and last_prime """ if rebuild: os.remove(self.index) self.connect() files = os.listdir(self.dir_) for f in files: if f == "index.db": continue print "Adding", f self.add(polynomial.name_to_polynomial(f)) self.update_index() else: for f in self.list(): count = self.count(f, True) last_prime = self.last_prime(f, True) if count != self.count(f) or \ last_prime != self.last_prime(f): print "Updating", f self.conn.execute("UPDATE polynomials \ SET roots=?, last_prime=? \ WHERE coefficients=?", (count, last_prime, polynomial.name_polynomial(f))) self.conn.commit() return None
def show(self, degree=None, group=None): """Show database contents.""" data = self.list(degree, group, info=True) current_degree = str(0) current_group = str(0) for row in data: id = "(" + str(row[0]) + ")" id = id.ljust(6) f = polynomial.name_to_polynomial(row[1]) degree = str(row[2]) group = str(row[3]) count = str(row[4]).center(10) last_prime = str(row[5]).center(10) if current_degree != degree: current_degree = degree print print "Degree", degree if current_group != group: current_group = group print print "Galois Group", group print id, count, last_prime, f return None
def list(self, degree=None, group=None, info=False): """List polynomials in database.""" if degree is not None and group is not None: degree = int(degree) group = int(group) c = self.conn.execute("SELECT * FROM polynomials \ WHERE degree=? AND galois_group=? \ ORDER BY coefficients", (degree, group)) elif degree is not None and group is None: degree = int(degree) c = self.conn.execute("SELECT * FROM polynomials \ WHERE degree=? \ ORDER BY galois_group, coefficients", (degree, )) else: c = self.conn.execute("SELECT * FROM polynomials \ ORDER BY degree, galois_group, \ coefficients") fs = list() if info: for row in c: fs.append(row) else: for row in c: fs.append(polynomial.name_to_polynomial(row[1])) return fs
def load(self, id_): """Return a Sage polynomial object. Arguments: id -- number assigned by the database, shows up when DB.show() is called """ c = self.conn.execute("SELECT coefficients FROM polynomials \ WHERE id=?", (int(id_), )) return polynomial.name_to_polynomial(c.fetchone()[0])