def newSupplier(cnx, user): clear() print("Please enter the following details for the new Supplier") sname = input("Supplier name: ") phone = input("Phone number: ") postalCode = input("Postal code: ") street = input("Street: ") city = input("City: ") province = input("Province: ") country = input("Country: ") supplier = Supplier(0, sname, phone, street, city, province, country, postalCode) try: clear() DatabaseHelper.addSupplier(cnx, supplier, user) print("Supplier successfully created") input("Please press enter to continue.") except: print("Could not create supplier") input("Please press enter to continue.") return
def get_survey(survey_id): """Return all of the data for a given survey :param survey_id: The id of the desired survey :return: A JSON object with all survey info, as well as all associated questions and responses """ survey_data = db.get_survey(survey_id) if not survey_data: return json.dumps({}) question_list = db.get_all_questions(survey_data[0]) data = { 'surveyId': survey_data[0], 'surveyName': survey_data[1], 'questions': [] } for question in question_list: question_data = { 'questionId': question[0], 'questionIndex': question[2], 'questionText': question[3], 'answers': [] } response_list = db.get_responses_by_id(question[0]) for response in response_list: response_data = { 'answerId': response[0], 'phoneNumber': response[2], 'answerText': response[3], 'timestamp': response[4] } question_data['answers'].append(response_data) data['questions'].append(question_data) json_data = json.dumps(data) return json_data
def newExpense(cnx, user): clear() print("Please enter the following details for the Expense") edate = date.today() cost = -1 while cost < 0: try: cost = int(input("Cost of expense: ")) except: print("Invalid cost") cost = -1 details = input("Enter the details of the expense: \n") e = Expense(0, edate, cost, details) try: clear() DatabaseHelper.addExpense(cnx, e, user) print("Expense successfully added.") input("Please press enter to continue.") except: print("Could not add expense.") input("Please press enter to continue.") return
def add_blacklist(self, tolist): myhelper = DatabaseHelper() valid = myhelper.add_blacklist(self, tolist) if valid: self.blacklist.append(tolist) return True else: return False
def parse(file_url): DatabaseHelper.initiateDB() with open(file_url, 'r') as f: for line in f: prevWord = "" for word in line.split(): DatabaseHelper.addToDatabase(prevWord, word) prevWord = word
def parse(file_url): DatabaseHelper.initiateDB() with open(file_url, 'r') as f: for line in f: prevWord = "" for word in line.split(): DatabaseHelper.addToDatabase(prevWord, word) prevWord = word
def camera_scaner(path_to_db=None, cascade=args.home + '/' + 'haarcascade_frontalface_default.xml'): font = cv2.FONT_HERSHEY_SIMPLEX recognizer = cv2.face.LBPHFaceRecognizer_create() home = os.getcwd() ymls = dbh.sort_files(path_to_db, ['.yml']) if len(ymls) == 0: print('Нет тренировочного файла') return -1 print('Ymls: ', ymls) if not os.path.isfile(cascade): print('Ошибка.', cascade, 'не найден.') return -1 face_cascade = cv2.CascadeClassifier(cascade) #os.chdir(path_to_db) recognizer.read(ymls[0]) label = '' names = dbh.get_list_names(path_to_db) num = 0 cap = cv2.VideoCapture(0) #cap.set(cv2.CAP_PROP_FPS, 10) cv2.namedWindow('video') while True: ret, frame = cap.read() frame = cv2.flip(frame, 1) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.35, minNeighbors=4, minSize=(30, 30)) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 100, 25), 3) num, conf = recognizer.predict(gray[y:y + h, x:x + w]) if conf < 100: label = names[num] info = " {0}%".format(round(100 - conf)) #print('Num:', num, ' Conf:', conf) else: label = "unknown" info = " {0}%".format(round(100 - conf)) cv2.putText(frame, str(label), (x + 5, y - 5), font, 1, (25, 55, 255), 3) cv2.putText(frame, str(info), (x + 5, y + h + 30), font, 1, (5, 5, 255), 3) cv2.imshow('video', frame) key = cv2.waitKey(1) if key == 27 or key == 13: cap.release() cv2.destroyAllWindows() break cap.release() cv2.destroyAllWindows()
def add_whitelist(self, tolist): myhelper = DatabaseHelper() valid = myhelper.add_whitelist(self, tolist) if valid: self.whitelist.append(tolist) return True else: print("nope") return False
def start_survey(user_number, survey_number): """Get the first question for a given survey and begin the survey. :param user_number: The number that will be receiving the survey :param survey_number: The number associated with the survey """ survey_id = db.get_survey_id_by_number(survey_number) first_question = db.get_question_by_index(survey_id, 0) send_next_question(user_number, survey_number, first_question)
def updateExpense(cnx, user): clear() print("Please enter the id of the expense to update") while True: try: print("All Expenses: ") expenses = DatabaseHelper.searchExpenses(cnx, "") if len(expenses) != 0: for i in range(len(expenses)): print("Result {}".format(i + 1)) print(expenses[i]) index = -1 while index < 0: try: index = int( input("Select expense by result number: ")) - 1 if index > len(expenses) - 1 or index < 0: index = -1 print("Index out of bounds. Try again.") except: print("Incorrect input. Try again.") expense = expenses[0] break else: print("Could not find expense with id, try again.") except: print("Invalid expense id, try again.") print("Please enter the following details for the updated Expense") cost = -1 while cost < 0: try: cost = int(input("Cost of expense: ")) except: print("Invalid cost") cost = -1 details = input("Enter the details of the expense: \n") e = Expense(expense.getId(), expense.getDate(), cost, details) try: clear() DatabaseHelper.updateExpense(cnx, e) print("Updated expense successfully.") input("Please press enter to continue.") except: print("Could not update expense.") input("Please press enter to continue.") return return
def send_survey_to_numbers(survey_id, numbers): """Send a survey to a list of numbers :param survey_id: The survey to send :param numbers: The list of numbers that will receive the survey """ first_question = db.get_question_by_index(survey_id, 0) if not first_question: return send_number = db.get_survey_phone_number(survey_id) for num in numbers: send_next_question(num, send_number, first_question)
def send_next_question(rec_phone, send_phone, question): """Send the next survey question. :param rec_phone: The phone number that will be receiving the question :param send_phone: The phone number associated with the survey :param question: The question text to be sent """ messaging_api = init_bandwidth_messaging_client() messaging_api.send_message(from_=send_phone, to=rec_phone, text=question[3]) db.save_status(phone=rec_phone, question_id=question[0], send_phone=send_phone)
def renew_robot_status(rid, db_info): sql = '''update t_robot set ipv4='%s', hnm='%s', prid=%d where id=%d ''' % (socket.gethostbyname(socket.gethostname()), socket.gethostname(), os.getpid(), rid) conn = None try : conn, cur = DB.connect(db_info) cur.execute(sql) conn.commit() except : log.error('Fail to renew robot status!!', exc_info=1) finally: DB.close(conn)
def create_survey(json): """Create a survey from a JSON object. Save questions if any are given. :param json: The JSON object containing survey info :return: The id of the new survey """ survey_name = json['surveyName'] if 'surveyNumber' in json and json['surveyNumber']: survey_number = json['surveyNumber'] else: survey_number = create_bandwidth_app() survey_id = db.save_survey(survey_name, survey_number) if 'questions' in json: db.save_questions(survey_id=survey_id, questions=json['questions']) return str(survey_id)
def start_task(record, robot_conf, db_info): start_time = time.time() try : err = 0 dura = '' step = 0 msg = None task_d = json.loads(record['detail']) h = BaseHandler(task_d, robot_conf) from vtest.client.handlers import SUCCESS, FAIL try : result = h.run() if result == SUCCESS : err = 0 elif result == FAIL : err = 1 else : err = 2 dura = ','.join([str(t) for t in h.nodes_report['times']]) if err : step = h.nodes_report['step'] msg = h.last_msg except : log.error('Fail to start new task', exc_info=1) time_used = (time.time() - start_time) * 1000 log.info('Time use = %dms' % time_used) conn = None try : conn, cur = DB.connect(db_info) cur.execute('''insert into t_task_re(rid,tid,lm,err,step,msg,total,dura) values(%d,%d,now(),%d,%d,'%s',%d,'%s')''' % (robot_conf['rid'], record['id'], err, step, msg, time_used, dura)) if err == 0 : cur.execute('''update t_task set done=done+1 , nok=nok+1 where id=%d''' % record['id']) else : cur.execute('''update t_task set done=done+1 , nfail=nfail+1 where id=%d''' % record['id']) cur.execute('''update t_task set stat=2 where done >= fnn''') conn.commit() log.info('Task done and update t_task_re success') except : log.error('Fail to update task status?!!', exc_info=1) finally: DB.close(conn) except : raise log.error('Fail to execute task?!!', exc_info=1)
def load_info(self): myhelper = DatabaseHelper() wl = myhelper.get_whitelist(self) for tup in wl: for ip in tup: self.whitelist.append(ip) bl = myhelper.get_blacklist(self) for tup in bl: for ip in tup: self.blacklist.append(ip) fw = myhelper.get_firewall(self) for tup in fw: try: self.firewall = Firewall(tup[0], tup[1]) except: print("default firewall")
def newEmployee(cnx, user): clear() print("Please enter the following details for the Employee") name = input("Full Name: ") salary = 0 while salary < 1: try: salary = int(input("Salary: ")) except: print("Invalid salary") dateEmployed = date.today() while True: isManager = input("Is employee a manager?(Y/N): ").upper() if isManager == "Y": isManager = True break elif isManager == "N": isManager = False break mid = 0 while mid < 1: try: mid = int(input("Manager id: ")) except: print("Invalid manager id") mid = 0 username = input("Username: "******"Password: "******"none", isManager, mid) user = User(username, password, e) try: clear() DatabaseHelper.addEmployee(cnx, user) print("Employee successfully added.") input("Please press enter to continue.") except: print("Could not add employee.") input("Please press enter to continue.") return
def result(): ''' Handles what happens when submit is clicked ''' service = dh.getSheetsService() if request.method == 'POST': result = request.form logEntry = [] for key, value in result.items(): logEntry.append(value) dh.addLogEntry(service, logEntry[0]) recentActs = dh.getRecentActivities(service) return render_template("main.html", result = recentActs)
def newCustomer(cnx, user): clear() print("Please enter the following details for the new Customer") cname = input("Customer name: ") join_date = date.today() phone = input("Phone number: ") customer = Customer(0, cname, join_date, phone) try: clear() DatabaseHelper.addCustomer(cnx, customer, user) print("Customer successfully created") input("Please press enter to continue.") return customer except: print("Could not create customer") input("Please press enter to continue.") return None
def removeEmployee(cnx, user): clear() print("Please enter the id of the employee to remove") eid = 0 while eid < 1: try: eid = int(input("Employee id: ")) except: print("Invalid employee id") eid = 0 try: clear() DatabaseHelper.removeEmployee(cnx, eid) print("Employee successfully removed") input("Please press enter to continue.") except: print("Could not remove employee.") input("Please press enter to continue.") return
def searchSuppliers(cnx, user): cont = True while cont: clear() s = input("Enter a search query:") r = DatabaseHelper.searchSuppliers(cnx, s) for i in range(len(r)): print(r[i]) if input( "Enter 'Y' to search again. Any other key to return to previous: " ).upper() != 'Y': cont = False return
def main(termsList, xliffFile): # Loads terms List into Term Loader termLoader = PhraseLoader.TermLoader(termsList) termLoader.loadTerms() # Grabs Terms to be saved and terms not being saved in database specTermDictForDatabase, specTermDictNoSave = termLoader.getTermDicts() database = db.DatabaseHelper() # Parses xliff file into a list of trees parser = XLIFFParser2.XMLParser(xliffFile) parsedTree = parser.run() # Grabs content pairs extractor = ExtractContent.TagBinder(parsedTree) IPCCODEGENERIC, IPCODESPECIFIC = extractor.findIPCCode() # Fetches relevant IPC Code terms from database genTermDict = database.getTerms(IPCCODEGENERIC) # creates output file in same directory as the xliff file output = open(getFileDir(xliffFile) + "output_check.txt", "w+") # For SDLXLIFF Files matchList = extractor.findSourceTargetMatch("seg-source", "target") # For XLIFF Files # TODO: add XLIFF functionality # matchList += extractor.findSourceTargetMatch("source", "target") # Runs check k = Validator( {**genTermDict, **specTermDictForDatabase, **specTermDictNoSave}, output, matchList, ) phraseResults = k.run() # Runs reference tag check refChecker = ReferenceNumberCounter.CompareReferenceElements(matchList, output) refResults, numResults = refChecker.compareTexts() # Write output file k.writeResults(phraseResults, refResults, numResults) # add relevant terms to dictionary and rewrite to database database.setTerms(specTermDictForDatabase, IPCCODEGENERIC) database.updateDatabase() output.close()
def searchCars(cnx, user): cont = True while cont: clear() s = input("Enter a search query:") r = DatabaseHelper.searchCars(cnx, s) print("\nYour Search returned {} result(s).".format(len(r))) for i in range(len(r)): print(r[i]) if input( "Enter 'Y' to search again. Any other key to return to previous: " ).upper() != 'Y': cont = False return
def profitSummary(cnx, user): clear() totalProfit = 0.0 try: sales = DatabaseHelper.searchSales(cnx, "") except: print("Could not retrieve sale information.") for i in range(len(sales)): totalProfit = totalProfit + float(sales[i].getCar().getPrice()) print("Your total profit is: ${0:.2f}".format(totalProfit)) input("Press any button to continue.") return
def login(cnx): user = None u = "" p = "" go = True while user is None and go: clear() u = input("Username: "******"Password: "******"Invalid username/password combination. Retry?(Y/N) " ).upper() == 'Y' return user
def get_all_surveys(): """Get all surveys and convert them to a JSON object. :return: A JSON object containing a list of all surveys """ surveys = db.get_all_surveys() data = {'surveys': []} for s in surveys: survey_data = { 'surveyId': s[0], 'surveyName': s[1], 'surveyNumber': s[2] } data['surveys'].append(survey_data) json_data = json.dumps(data) return json_data
def send_nearest_atm(message): print(message) bot.send_chat_action(message.chat.id, "typing").wait() db = DatabaseHelper.DatabaseHelper(settings.DATABASE_NAME, settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_HOST, settings.DATABASE_PORT) result = db.select_atms(message.location.longitude, message.location.latitude) if len(result) == 0: result = db.select_atms(message.location.longitude, message.location.latitude, 6000) db.close() if len(result) == 0: bot.send_message(message.chat.id, "К сожалению, поблизости нет банкоматов.").wait() else: bot.send_location(message.chat.id, result[0][3], result[0][2]).wait() descr_text = '''Ближайший банкомат : Адрес:{} {} Расстояние: {} м.'''.format(result[0][0], result[0][1], result[0][4]) bot.send_message(message.chat.id, descr_text).wait()
def one_day_shuffles_per_hour(bird, day): listx = 24 * [0] for hour in range(0, 24): tuple_list = dh.get_one_hour(bird, day, hour) total_events = len(tuple_list) if total_events == 0: print "No events at ", (day, hour) continue # classe voor shuffling per bird anders if bird == "b73": shuffle_class = 0 elif bird == "DB4": shuffle_class = 1 else: shuffle_class = 3 amount_shuffles = extract_shuffles(tuple_list, shuffle_class) normalized_shuffles = (float(amount_shuffles) / float(total_events)) * 3600 listx[hour] = (normalized_shuffles) return listx
def loadMoviesOnUser(): db = dh.DataBaseWorker() value = int(request.json['value']) selected_cluster = int(request.json['selected_cluster']) defaultUserMovies, recommendedMovies = sf.getsimilarMovies(value, selected_cluster) movies_list = [] for users_movie in recommendedMovies.keys(): [movies_list.append(movie) for movie in recommendedMovies.get(users_movie)] unique_movies = sorted(set(movies_list)) user_key = list(defaultUserMovies.keys())[0] ggg = defaultUserMovies.get(user_key); def_movies = [] ratings = [] for g in ggg: def_movies.append(g['movieID']) ratings.append(g); default_user_movies = sorted(set(def_movies)) recommendedMovies = db.loadMovies(isInitial=False, keys=unique_movies) defaultUserMovies = db.loadMovies(isInitial=False, keys=default_user_movies, ratingData=ratings) return flask.jsonify(defaultMovies=defaultUserMovies, recommended=recommendedMovies)
def read_response(response): """If the user has not been given a survey, send the first question. Otherwise, save the response and send the next survey question. If there are no additional questions, remove the user's status. :param response: The JSON response received. """ text_response = response["text"] phone_number = response["from"] send_phone = response["to"] status = db.get_status(phone_number, send_phone) if not status: start_survey(phone_number, send_phone) return db.save_response(response=text_response, phone_number=phone_number, question_id=status[0]) question = db.get_question_by_id(status[0]) next_question = db.get_question_by_index(question[1], question[2] + 1) if not next_question: db.delete_status(phone_number) return send_next_question(rec_phone=phone_number, send_phone=response["to"], question=next_question)
def main(): r'''加载配置文件,如果没有,就选默认的robot.cnf''' conf_path = os.getenv('vtest.robot.cnf', 'robot.cnf') robot_conf = {} with open(conf_path) as f : for line in f : if line.startswith('#') : continue if line.find('=') : line = line[0:-1] key = line[0:line.find('=')] value = line[(line.find('=') + 1):] robot_conf[key] = value log.info('Robot conf :\n',json.dumps(robot_conf, indent=2)) if not robot_conf.get('tmp') : robot_conf['tmp'] = '/tmp/' db_info = { 'host' : robot_conf.get('db_host','127.0.0.1'), 'port' : int(robot_conf.get('db_port','3306')), 'user' : robot_conf.get('db_username','root'), 'password' : robot_conf.get('db_password','123456'), 'database' : robot_conf.get('db_name','vtest'), 'charset' : 'utf8', 'use_unicode' : True, 'get_warnings' : False } from vtest.client.helper import init_log init_log(None, '%s/vtest_main.log' % robot_conf['tmp']) if not robot_conf.get('rid') : log.info('No robot id found, create it!!') conn, cur = DB.connect(db_info) sql = '''insert into t_robot(ipv4, hnm, prid, lm) values('%s', '%s', %d, now())''' % (socket.gethostbyname(socket.gethostname()), socket.gethostname(), os.getpid()) print sql cur.execute(sql) conn.commit() robot_conf['rid'] = cur.getlastrowid() conn.close() else : renew_robot_status(robot_conf['rid'], db_info) log.info('Robot id = %d' % robot_conf['rid']) while 1 : log.info('Scan new task ...') conn = None try : conn, cur = DB.connect(db_info) cur.execute('select id,detail from t_task where stat < 2 order by rand() limit 1') res = cur.fetchall() if res : record = {'id' : res[0][0], 'detail' : res[0][1]} log.info('Found a task , id=%d', record['id']) log.info("========================================================================") cur.execute('update t_task set lm=now() where id=%d' % record['id']) conn.commit() conn.close() start_task(record, robot_conf, db_info) log.info('Task is Done, great!!') log.info("========================================================================") else : log.info('No task found ....') except : log.error('Fail to execute task?!!', exc_info=1) DB.rollback(conn) finally: DB.close(conn) renew_robot_status(robot_conf['rid'], db_info) log.info('Sleep 1s ... ... ...') time.sleep(1)
def remove_server(self, toremove): myhelper = DatabaseHelper() myhelper.remove_server(toremove) del self.serverdict[toremove.alias] self.servercount -= 1
def add_server(self, new_server): myhelper = DatabaseHelper() myhelper.add_server(self.username, new_server) self.serverdict[new_server.alias] = new_server self.serverlist.append(new_server) self.servercount += 1
def __init__(self): self.db = helper.DatabaseHelper() pass
def create_db(): dh.drop_db() dh.create_db()
from DatabaseHelper import * SAVELOCATION = os.getcwd() + "\\webcomics\\" # Load each web series from the json into a list of webcomic objects webseries = [] openJson = open('webcomiclist.json') data = json.load(openJson) for webcomic in data["webcomics"]: webseries.append( Webcomic(webcomic["name"], webcomic["baseURL"], webcomic["startURL"], webcomic["nextURLXPath"], webcomic["comicXPath"], webcomic["genres"])) # Connect to the database. If the database does not exist it will be created. databaseHelper = DatabaseHelper("WebcomicDatabase.db") # Iterate through every webseries for comic in webseries: # Is this the first comic to be downloaded (false), or are we picking up where we left off (true) print(comic.name) comicsToDownload = True saveTo = SAVELOCATION + comic.name + "\\" # If the directory for this webseries does not exist, create it if not os.path.exists(saveTo): os.makedirs(saveTo) # Get the webseries data from the database. If it does not exist in the database this will return None. webseriesData = databaseHelper.getWebseriesData(comic.name)
def writeDatabase(puzzname, passedoptions,isunreverse,solverclass): if puzzname.find('/') != -1 or puzzname.find('\\') != -1: raise ArgumentException, "invalid puzzle" module = __import__(puzzname) puzzleclass = getattr(module, puzzname) options = {} options.update(puzzleclass.default_options) options.update(passedoptions) print "Solving "+puzzleclass.__name__+" with options "+repr(options)[1:-1]+"..." puzzle = puzzleclass.unserialize(options) #.generate_start(**options) # instantiates a new puzzle object solver = solverclass() solveriter = solver.solve(puzzle,verbose=True) # Generator function maxlevel = 255 # WARNING: BIG ASSUMPTION, NEED A 2-PASS SOLVER! options = {} options.update(puzzleclass.default_options) options.update(passedoptions) print options fields =[('remoteness',numBits(maxlevel))] if isunreverse: fields.append(('score', numBits(maxlevel*2))); print fields bitClass = DatabaseHelper.makeBitClass(fields) CHUNKBITS=8 CHUNK = 1<<CHUNKBITS filename = DatabaseHelper.getFileName(puzzname, options) default = str(bitClass(remoteness=0)) # remoteness==0 but not solution means not seen. print "writing to file %s"%filename maxchunkwritten = -1 countitem = 0 pct = 0.0 data = {} # indexed by chunk # Not as innocent as it looks: solveriter.next() may take a long time. for levelnum, curlevel in solveriter: try: x = solver.maxHash except: solver.maxHash = 2**32 len_curlevel = len(curlevel) countitem += len_curlevel if solver.maxHash: pct = float(100*countitem)/solver.maxHash print "[% 2.4f%%] Writing level %d, numitems=%d, curitem=%d, maxhash=%d."% \ (pct,levelnum,len(curlevel),countitem,solver.maxHash) maxchunks = 1 + solver.maxHash/CHUNK #print "Maximum hash is %d, number of %d-byte chunks to write is %d"%(solver.maxHash, CHUNK, maxchunks) mykeys = curlevel.keys() mykeys.sort() mykeys.append(None) # write the last chunk to disk lastchunknum = -1 arr = None for position in mykeys: if position is None: chunknum = -1 else: chunknum = position>>CHUNKBITS chunkoff = position & (CHUNK-1) if chunknum != lastchunknum: if arr: maxchunkwritten = max(maxchunkwritten, lastchunknum) data[lastchunknum] = (''.join(arr)) arr = None lastchunknum = chunknum if position is not None: #position = (chunknum<<CHUNKBITS)+chunkoff #if position not in curlevel: # continue myval = levelnum if arr == None: if chunknum not in data: arr = [] else: arr = [c for c in data[chunknum]] if isunreverse: #print dict(remoteness=(maxlevel - myval[0]), score=myval[1]) val = bitClass(remoteness=(maxlevel - myval[0])) #, score=myval[1]) #print repr(str(val)) else: val = bitClass(remoteness=myval) setList(arr, chunkoff, str(val), default) #print "Done with level %d!"%levelnum print "Solved! Thank you, writing to disk." f = open(filename, "wb") pickle.dump({'puzzle': puzzname, 'options': options, 'fields': fields, 'version': 1, 'maxlevel': solver.get_max_level(), 'chunkbits': CHUNKBITS}, f) chunkbase = (f.tell() + (CHUNK-1)) >> CHUNKBITS f.close() f = open(filename, "r+b") # read/write, no truncate. for chunknum in xrange(1 + (solver.maxHash >> CHUNKBITS)): if chunknum in data: f.seek((chunkbase + (chunknum*bitClass.bytesize))<<CHUNKBITS) f.write(data[chunknum]) print "DONE." f.close() solveriter.close() return solver
def add_to_db(csvfile, birdname): dh.csv_to_db(csvfile, birdname)
def writeDatabase(puzzname, passedoptions, solver, isunreverse): if solver is None: return module = __import__(puzzname) puzzleclass = getattr(module, puzzname) maxlevel = solver.get_max_level() options = {} options.update(puzzleclass.default_options) options.update(passedoptions) print options fields = [("remoteness", numBits(maxlevel))] if isunreverse: fields.append(("score", numBits(maxlevel * 2))) print fields bitClass = DatabaseHelper.makeBitClass(fields) CHUNKBITS = 8 CHUNK = 1 << CHUNKBITS filename = DatabaseHelper.getFileName(puzzname, options) print "writing to file %s" % filename f = open(filename, "wb") pickle.dump( { "puzzle": puzzname, "options": options, "fields": fields, "version": 1, "maxlevel": maxlevel, "chunkbits": CHUNKBITS, }, f, ) chunkbase = (f.tell() + (CHUNK - 1)) >> CHUNKBITS default = str(bitClass(remoteness=0)) # remoteness==0 but not solution means not seen. mystr = None try: mystr = solver.getBytes() except: pass # Not a bytearray solve. Use the normal writing functions. if mystr: f.seek(chunkbase << CHUNKBITS) print "Writing %d-length buffer to disk..." % len(mystr) f.write(mystr) f.close() return try: print solver.maxHash except: solver.maxHash = 2 ** 32 maxchunks = 1 + solver.maxHash / CHUNK print "Maximum hash is %d, number of %d-byte chunks to write is %d" % (solver.maxHash, CHUNK, maxchunks) lasttime = starttime = time.time() for chunknum in xrange(0, maxchunks): arr = [] for chunkoff in xrange(CHUNK): position = (chunknum << CHUNKBITS) + chunkoff myval = solver.seen.get(position, None) if not myval: continue if isunreverse: print dict(remoteness=(maxlevel - myval[0]), score=myval[1]) val = bitClass(remoteness=(maxlevel - myval[0])) # , score=myval[1]) # print repr(str(val)) else: val = bitClass(remoteness=myval) setList(arr, chunkoff, str(val), default) nexttime = time.time() pct = 100.0 * float(1 + chunknum) / maxchunks if chunknum == 0: print "Writing first chunk out of %d to file %s" % (maxchunks, filename) elif nexttime - lasttime > 0.5 or chunknum == 1: timeleft = (maxchunks - chunknum) * ((nexttime - starttime) / chunknum) minleft = int(timeleft / 60) secleft = int(timeleft) % 60 print "[%3.2f%%] Writing chunk %d/%d to file %s, %d:%d minutes left" % ( pct, chunknum, maxchunks, filename, minleft, secleft, ) lasttime = nexttime if arr: f.seek((chunkbase + (chunknum * bitClass.bytesize)) << CHUNKBITS) f.write("".join(arr)) f.close() print "Done!"