Exemple #1
0
def main():
    thread_count = 0
    dm = '0.0.0.0:5000'
    db_manager = DatabaseManager()
    host_relation = db_manager.get_relation('host')

    j = 0
    watchdogs = []
    hosts = []
    for i in range(0, len(host_relation)):
        if j < number_of_comps:
            hosts.append(host_relation[i]['host'])
            j += 1
            if j >= number_of_comps:
                thread_count += 1
                wd = WatchDog(thread_count, dm, hosts)
                watchdogs.append(wd)
                hosts = []
                j = 0

    if number_of_comps > len(hosts) > 0:
        thread_count += 1
        wd = WatchDog(thread_count, dm, hosts)
        watchdogs.append(wd)

    for wd in watchdogs:
        wd.start()
class ScenarioLoader(QDialog):
    def __init__(self):
        super(ScenarioLoader, self).__init__()
        self.setWindowTitle("Load Scenario")

        main_layout = QVBoxLayout()
        self.database = DatabaseManager()

        self.scenario_select = QComboBox()
        scenarios = self.database.get_names_in_table("Scenario")
        self.scenario_select.addItems(scenarios)
        main_layout.addWidget(self.scenario_select)

        self.done_button = QPushButton("Done")
        main_layout.addWidget(self.done_button)
        self.done_button.clicked.connect(self.close)
        self.done_button.setDefault(True)

        self.setLayout(main_layout)

    def close(self):
        scenario_name = self.scenario_select.currentText()
        self.database.update_date_used(("Scenario", ), (scenario_name, ))
        self.database.connection.close()
        super(ScenarioLoader, self).close()
Exemple #3
0
    def search(self, text, isSvd = False):

        if isSvd:
            print "Search with SVD"
        else:
            print "Search with LDA"

        start = time.time()
        vector = text.split()
        cleanedVector = cleanVector(vector)
        #cleanedVector = self.wordsToAsk

        print cleanedVector

        if isSvd:
            bagOfWords, indices = createBagOfWordsFromVector(cleanedVector, self.amountOfWords, self.dictOfWords, self.idfs)
            b = fasterCorrelations(self.matrix, indices, bagOfWords, self.amountOfFiles)

        else:
            bagOfWords = createBagOfWordsForLDA(cleanedVector, self.amountOfWords, self.dictOfWords)

            #print 'bow shape:'
            #print bagOfWords.shape

            #print 'topic word shape:'
            #print np.transpose(self.model.topic_word_).shape
            res = np.dot(bagOfWords, np.transpose(self.model.topic_word_))

            #print 'first mul shape:'
            #print res.shape
            #print res
            #print 'doc topic shape:'
            #print np.transpose(self.model.doc_topic_).shape

            res2 = np.dot(res, np.transpose(self.model.doc_topic_))

            #print 'final shape:'
            #print res2.shape

            bestValues = sorted(list(res2[0]), reverse=True)[:5]

            b = []

            for bestVal in bestValues:
                for ind, val in enumerate(list(res2[0])):
                    if val == bestVal:
                        b.append((ind, val))


        results = []
        dbMan = DatabaseManager()
        for x in b:
            results.append(dbMan.get_link(self.listOfArticles[x[0]]))

        for res in results:
            print res.url
            print res.title

        stop = time.time()
        return results, stop - start
Exemple #4
0
def load_X_Y_nn(table_name,
                top100_labels=False,
                validation_set=False,
                test_set=False):
    db = DatabaseManager()
    cur = db.get_bag_of_words_vectors(table_name)
    n_patients = cur.rowcount

    Y = np.zeros((n_patients, 100 if top100_labels else 10))

    icd9_codes_map = get_icd9_codes_map(top100_labels=top100_labels)

    data = []
    row_ind = []
    col_ind = []
    for cnt, (subject_id, _, bag_of_words_binary_vector_col_ind,
              bag_of_words_binary_vector_data) in enumerate(cur):
        bag_of_words_vector_col_ind = pickle.loads(
            bag_of_words_binary_vector_col_ind)
        bag_of_words_vector_data = pickle.loads(
            bag_of_words_binary_vector_data)

        data += bag_of_words_vector_data
        row_ind += [cnt] * len(bag_of_words_vector_col_ind)
        col_ind += bag_of_words_vector_col_ind

        # Get the icd9 codes of the diseases this subject_id has.
        diagnoses = db.get_icd9_codes(subject_id=subject_id,
                                      validation_set=validation_set,
                                      test_set=test_set)
        for icd9_code in diagnoses:
            idx = icd9_codes_map[icd9_code]
            Y[cnt][idx] = 1

    return data, row_ind, col_ind, n_patients, 40000, Y  # TODO
Exemple #5
0
def categories_to_database():
    """ Categories are sent to the databse, to do ONCE, DONE """
    categories_extraction = DatasManager.categories_extract(categories_list)
    categories_tuples_in_list = DatasManager.convert_list_tuples(
        categories_list, categories_extraction)
    DatabaseManager.categories_to_database(category_table,
                                           categories_tuples_in_list)
def set_background(request,channel_id):
    clock = Clock(logger=logger)
    clock.start()
    dbm = DatabaseManager(channel = channel_id)
    new_background = request.GET['new_background']
    dbm.set_background(background=new_background)
    logger.info("set_background returned in %f seconds" % clock.stop())
    return HttpResponse(1)  
 def __init__(self):
     config_path = os.path.join(os.path.abspath(""), "config",
                                "database.config")
     db_storage = os.path.join(os.path.abspath(""), "db_storage")
     with open(config_path) as config:
         user = config.readline().strip()
         pwd = config.readline().strip()
     self.database = DatabaseManager(user, pwd, "nonogramDB", db_storage)
Exemple #8
0
def products_to_database():
    """ Prodcuts are sent to the databse, to do ONCE, DONE """
    category_name = DatabaseManager.category_from_database(category_table)
    category_url_list = DatasManager.category_to_url(categories_list,
                                                     category_name)
    products_datas = DatasManager.products_extract(products_lists,
                                                   category_url_list)
    DatabaseManager.products_to_database(products_table, products_datas)
def insert_db_tables():
    dm = DatabaseManager()
    cm = ConfigManager()
    tables = dm.list_tables()
    cm.save_tables(tables)
    for table in tables:
        model_id = cm.find_table(table)
        fields = dm.list_columns(table)
        cm.save_columns(model_id, fields)
    cm.get_models_config()
Exemple #10
0
def word_cloud():
    query = request.args.get("query")
    if query is None:
        return jsonify({"error": "missing query"}), 400

    db = DatabaseManager()
    comments = db.comments_containing(query)
    words = [word.lower() for c in comments for word in c.content.split()]
    counts = collections.Counter(words).most_common(25)
    return jsonify(dict(counts))
Exemple #11
0
    def __init__(self, width, height, path):
        self.width, self.height = width, height

        os_user = windll.user32
        self.x_off, self.y_off = os_user.GetSystemMetrics(0), os_user.GetSystemMetrics(1)

        self.root = tkinter.Tk()
        self.root.withdraw()
        self.root.resizable(False, False)
        self.window = FloatingWindow(self.root)
        self.window.geometry("{}x{}+{}+{}".format(self.width, self.height, int((self.x_off - self.width) / 2), int((self.y_off - self.height) / 2)))
        self.window.overrideredirect(True)

        self.exit_button_size = (30, 30)
        self.sign_in_button_size = (10, 1)
        self.create_account_button_size = (15, 1)
        self.exit_button_padding = 10
        self.exit_button_pressed_padding = 0
        self.entry_size = 25
        self.entry_label_padding = 30

        self.header_label_x, self.header_label_y = self.width / 2, 115
        self.entry_label_x, self.entry_label_y = self.width / 2 - 175, self.height / 2
        self.entry_x, self.entry_y = self.width / 2 - 60, self.height / 2 + 5
        self.login_header_x, self.login_header_y = self.width / 2, 125
        self.sign_in_button_x, self.sign_in_button_y = self.width / 2 + 169, self.height / 2 + 65
        self.error_label_x, self.error_label_y = self.width / 2, self.height / 2 + 100
        self.signup_label_x, self.signup_label_y = self.width - 150, self.height - 8
        self.create_account_button_x, self.create_account_button_y = self.width - 5, self.height - 5

        self.bg_colour = '#171717'
        self.highlight_bg_colour = '#2b2b2b'
        self.highlight_fg_colour = '#7612db'
        self.hover_bg_colour = '#3b3b3b'
        self.text_colour = '#ffffff'
        self.error_colour = '#ffffff'

        self.header_font = ('Montserrat ExtraLight', '35')
        self.entry_label_font = ('Montserrat Light', '16')
        self.entry_font = ('Montserrat Light', '10')
        self.sign_in_button_font = ('Montserrat Medium', '10')
        self.error_label_font = ('Montserrat Medium', '10')
        self.signup_label_font = ('Montserrat Light', '12')

        self.parent_dir = os.path.abspath(os.path.join(path, os.pardir))
        self.db_manager = DatabaseManager(r'{}/data/EngineData.db'.format(self.parent_dir))
        self._launcher_closed = False
        self.error_text = None
        self.account_created_text = None
        self.new_sign_in_button_canvas = None
        self.path = path

        self.construct_launcher()
        self.root.mainloop()
Exemple #12
0
def most_upvoted():
    query = request.args.get("query", )
    n = request.args.get("n", type=int)
    if query is None:
        return jsonify({"error": "missing query"}), 400
    if n is None:
        return jsonify({"error": "missing n"}), 400

    db = DatabaseManager()
    comments = db.most_upvoted_comments_containing(query, n)
    comments.sort(key=lambda x: x.votes, reverse=True)
    return jsonify([c.to_dict() for c in comments])
Exemple #13
0
    def __init__(self, previous_start_pos, previous_square_size):
        super(ConfigDialog, self).__init__()
        self.setWindowTitle("Configure Settings")

        self.database = DatabaseManager()
        self.main_layout = QVBoxLayout()

        # Robot select
        self.robot_layout = RobotSelect(self.database)
        self.main_layout.addLayout(self.robot_layout)

        # Room select
        self.room_layout = RoomSelect(self.database, previous_start_pos,
                                      self.validate_input)
        self.main_layout.addLayout(self.room_layout)

        # Square size
        self.square_size_layout = QHBoxLayout()
        self.square_size_label = QLabel("Square Size: ")
        self.square_size_text_edit = QLineEdit()
        if previous_square_size is None:
            previous_square_size = DEFAULT_SQUARE_SIZE
        self.square_size_text_edit.setText(str(previous_square_size))

        self.square_size_layout.addWidget(self.square_size_label)
        self.square_size_layout.addWidget(self.square_size_text_edit)
        self.main_layout.addLayout(self.square_size_layout)

        # Done button
        self.done_button = QPushButton("Done")
        self.main_layout.addWidget(self.done_button)
        self.done_button.clicked.connect(self.close)
        self.done_button.setDefault(True)
        self.robot_layout.set_main_button_default.connect(
            self.done_button.setDefault)
        self.robot_layout.set_main_button_default.connect(
            self.done_button.repaint)

        # Get all info together for input validation
        self.start_pos_line_edits = self.room_layout.get_start_pos_line_edits()
        self.room_select_dropdown = self.room_layout.room_select_dropdown
        self.robot_select_dropdown = self.robot_layout.robot_select_dropdown

        self.start_pos_line_edits[0].textChanged.connect(self.validate_input)
        self.start_pos_line_edits[1].textChanged.connect(self.validate_input)
        self.room_select_dropdown.currentIndexChanged.connect(
            self.validate_input)
        self.robot_select_dropdown.currentIndexChanged.connect(
            self.validate_input)
        self.square_size_text_edit.textChanged.connect(self.validate_input)
        self.validate_input()

        self.setLayout(self.main_layout)
def index(request,channel_id):
    clock = Clock(logger=logger)
    clock.start()
    backgrounds,backgrounds_directory = __get_backgrounds()
    dbm = DatabaseManager(channel = channel_id)
    context = { "title":dbm.get_title(),
                "backgrounds":backgrounds,
                "current_background":'',
                "channel_id":channel_id
                }
    logger.info("Index returned in %f seconds" % clock.stop())
    return render(request,'channels/index.html',context)
class DataHandler:

    # Creates new data handler, connects to database using username and password saved in config file.
    def __init__(self):
        config_path = os.path.join(os.path.abspath(""), "config",
                                   "database.config")
        db_storage = os.path.join(os.path.abspath(""), "db_storage")
        with open(config_path) as config:
            user = config.readline().strip()
            pwd = config.readline().strip()
        self.database = DatabaseManager(user, pwd, "nonogramDB", db_storage)

    # Handles queries according to their type. Returns response to each query.
    def process(self, data):
        try:
            query = loads(data, encoding="UTF-8")
            Logger.writeInfo("Query type: {}".format(query["type"]))
            if query["type"] == "saveNonogram":
                return self.createResponse(
                    self.database.saveNonogram(query["data"]))
            if query["type"] == "getNonogramPreviewInfo":
                return self.createResponse(
                    self.database.getNonogramPreviewInfo(query["data"]))
            if query["type"] == "getNonogramById":
                return self.createResponse(
                    self.database.getNonogramById(query["data"]))
            if query["type"] == "solveNonogram":
                return self.createResponse(NonogramLogic.solve(query["data"]))
            if query["type"] == "createNonogram":
                return self.createResponse(NonogramLogic.create(query["data"]))
            Logger.writeError("Method " + query["type"] + " does not exist")
            return self.createResponse({
                "response":
                "fail",
                "desc":
                "Method '{}' not implemented yet".format(query["type"])
            })

        except (JSONDecodeError, KeyError) as e:
            Logger.writeError("DataHandler.process: " + str(e))
            return self.createResponse({"response": "fail", "desc": str(e)})

    # Packs size of response object and inserts it at the beginning of response string, creating the response.
    def createResponse(self, obj):
        msg = dumps(obj, separators=(',', ':')).encode()
        length = pack("<i", len(msg))
        Logger.writeInfo("Created response")
        return length + msg

    def close(self):
        self.database.close()
Exemple #16
0
def load_X_Y_rnn(table_name,
                 chunk_idx,
                 total_chunks,
                 top100_labels=False,
                 validation_set=False,
                 test_set=False):
    db = DatabaseManager()

    subject_ids = db.unique_subject_ids(table_name)
    chunked = np.array_split(np.array(subject_ids), total_chunks)
    subject_id_chunk = chunked[chunk_idx]
    m_subject_id_to_idx = dict()
    for i, subject_id in enumerate(subject_id_chunk):
        m_subject_id_to_idx[subject_id] = i

    n_patients = subject_id_chunk.shape[0]

    Y = np.zeros((n_patients, 100 if top100_labels else 10))

    icd9_codes_map = get_icd9_codes_map(top100_labels=top100_labels)

    first_patient, last_patient = subject_id_chunk[0].item(
    ), subject_id_chunk[-1].item()

    seq_length = 20  # TODO
    n_features = 40000  # TODO
    ret = np.zeros((n_patients, seq_length, n_features))

    cur = db.get_bag_of_words_vectors_rnn(table_name, first_patient,
                                          last_patient)

    for note_in_seq, row_id, subject_id, chart_date, bag_of_words_binary_vector_col_ind, bag_of_words_binary_vector_data in cur:
        bag_of_words_vector_col_ind = pickle.loads(
            bag_of_words_binary_vector_col_ind)
        bag_of_words_vector_data = pickle.loads(
            bag_of_words_binary_vector_data)

        for col_ind, data in zip(bag_of_words_vector_col_ind,
                                 bag_of_words_vector_data):
            ret[m_subject_id_to_idx[subject_id]][note_in_seq -
                                                 1][col_ind] = data

        # Get the icd9 codes of the diseases this subject_id has.
        diagnoses = db.get_icd9_codes(subject_id=subject_id,
                                      validation_set=validation_set,
                                      test_set=test_set)
        for icd9_code in diagnoses:
            idx = icd9_codes_map[icd9_code]
            Y[m_subject_id_to_idx[subject_id]][idx] = 1

    return ret, n_patients, n_features, Y
    def __init__(self):
        self.manager = DatabaseManager()
        self.manager.create_databases()

        self.cinema_hall = [['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.']]
Exemple #18
0
 def aggregate_counts(self, seconds: int, top: int):
     database_manager = DatabaseManager(self.db_file)
     while True:
         cur_counter = self.counter.copy()
         self.counter = collections.Counter()
         now = time.time()
         for symbol in cur_counter:
             self.velocity[symbol] = cur_counter[symbol] - self.prev_counter[symbol]
             self.acceleration[symbol] = self.velocity[symbol] - self.prev_velocity[symbol]
             row = (now, symbol, cur_counter[symbol])
             database_manager.insert_row_data(row)
         self.prev_counter = cur_counter.copy()
         self.prev_velocity = self.velocity.copy()
         self.generate_output(top)
         time.sleep(seconds)
Exemple #19
0
def parse_semantic_scholar_corpus_file(path, database_path="aip.db"):
        database = DatabaseManager(location=database_path)

        hash, parsed = database.did_parse_file(path)
        if parsed:
            return True

        file_iterator_func = iterload_file_lines_gzip if path.endswith("gz") else iterload_file_lines
        # print(corpus_file)
        # The json files contain stacked json objects, which is bad practice. It should be wrapped in a JSON array.
        # Libraries will throw errors if you attempt to load the file, so now we lazy load each object line by line.
        publication_iterator = file_iterator_func(path)
        for publication in publication_iterator:
            if publication is None:  # Corrupt JSON line possibly. Skip it.
                continue

            if "venue" not in publication:  # While parsing we sometimes get KeyError: 'venue'...
                continue

            # Try to match the publication to a venue we are interested in.
            # Wrap in str() as it sometimes is an int (???)
            venue_string = str(publication['venue'])
            if len(venue_string) == 0:
                continue

            # Check if any of the venue strings are a substring of the mentioned value, add it to that set.
            publication_title = publication['title']
            publication_abstract = publication['paperAbstract']
            publication_year = publication['year'] if 'year' in publication else -1
            publication_journal_volume = publication['journalVolume'].replace(" ",
                                                                              "_")  # Empty for conferences.
            # publication_keywords = publication['entities']
            publication_id = publication['id']

            num_citations = 0
            if "inCitations" in publication:
                num_citations = len(publication["inCitations"])

            publication_doi = publication['doi']
            if publication_doi is None or len(publication_doi) == 0:
                publication_doi_url = publication['doiUrl']
                if "doi.org/" in publication_doi_url:
                    publication_doi = publication['doiUrl'][
                                      publication['doiUrl'].index("doi.org/") + len("doi.org/"):]

            database.update_or_insert_paper(id=publication_id, doi=publication_doi, title=publication_title,
                                            abstract=publication_abstract, raw_venue_string=venue_string,
                                            year=publication_year, volume=publication_journal_volume,
                                            num_citations=num_citations)
        # database.flush_missing_venues()
        database.add_parsed_file(hash)
        database.close()
        return True
    def delete_data():
        # get connection to database
        db_conn = DatabaseManager.get_connection()
        cursor = db_conn.cursor()

        # delete median listing price records across all tables
        cursor.execute(
            DELETE_MEDIAN_LISTING_PRICE_TEMPLATE.format(
                MEDIAN_LISTING_PRICE_PER_SQFT_ALL_HOMES_TABLE))
        cursor.execute(
            DELETE_MEDIAN_LISTING_PRICE_TEMPLATE.format(
                MEDIAN_LISTING_PRICE_PER_SQFT_CONDO_TABLE))
        cursor.execute(
            DELETE_MEDIAN_LISTING_PRICE_TEMPLATE.format(
                MEDIAN_LISTING_PRICE_PER_SQFT_DUPLEX_TRIPLEX_TABLE))
        cursor.execute(
            DELETE_MEDIAN_LISTING_PRICE_TEMPLATE.format(
                MEDIAN_LISTING_PRICE_PER_SQFT_SFR_TABLE))

        # commit transaction
        db_conn.commit()

        # close cursor and connection
        cursor.close()
        db_conn.close()
    def __init__(self, iface, parent=None):
        """Constructor.

        :param iface:
        :param parent:
        """
        super(DownloadDialog, self).__init__(parent)
        # Set up the user interface from Designer.
        # After setupUI you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        QtGui.QDialog.__init__(self, parent)
        self.setupUi(self)
        self.message_bar = None
        self.iface = iface
        self.populate_combo_box()

        self.site_layer = iface.activeLayer()
        self.parcel_layer = None
        self.sg_code_field = None
        self.output_directory = None
        self.all_features = None
        self.log_file = None

        self.database_manager = DatabaseManager(sg_diagrams_database)

        self.restore_state()
def main():
    """
    The threading in this file works like this

    We have a bottle server listening for submissions. When it gets a submission
    it gives it to the put_submission_on_lb. This makes sure that the user is on the
    leaderboard/ the leaderboard reflects their most up to date submission.

    That method then enqueues the submission for concordance check.
    """
    np.random.seed(1337)

    create_logger()
    db_manager = DatabaseManager()
    fm = FileManager('/tmp/', logging)
    logging.getLogger().info("Creating servers")

    threading.Thread(target=run, kwargs=dict(host='0.0.0.0',
                                             port=int(PORT))).start()
    logging.getLogger().info("Spawning new threads to score concordance")

    threading.Thread(target=put_submission_on_lb,
                     kwargs=dict(db_manager=db_manager,
                                 filemanager=fm)).start()
    threading.Thread(target=score_concordance,
                     kwargs=dict(db_manager=db_manager,
                                 filemanager=fm)).start()

    # clean up the /tmp folder so we don't run out of disk space
    threading.Thread(target=schedule_cleanup,
                     kwargs=dict(filemanager=fm)).start()
Exemple #23
0
    def insert_data(self):
        # open file
        data_file = open("data/demographic/sex_race_age.csv", "r")

        # get connection to database
        db_conn = DatabaseManager.get_connection()
        cursor = db_conn.cursor()

        # parse data lines
        data_lines = data_file.readlines()

        for data_line in data_lines[2:]:
            # split data line
            data_line = data_line.replace('\"', '').split(',')

            # get zip field from data line
            zip_data = data_line[ZIP_INDEX]
            zip_data = zip_data.split(' ')[1]

            # filter based on Los Angeles zips
            if int(zip_data) not in zip_codes:
                continue

            # get age/race/sex data if zip is in LA list
            men_data = data_line[MEN_INDEX]
            women_data = data_line[WOMEN_INDEX]
            percent_white_data = data_line[PERCENT_WHITE]
            percent_black_data = data_line[PERCENT_BLACK]
            percent_asian_data = data_line[PERCENT_ASIAN]

            if (men_data == '' or women_data == '' or percent_white_data == ''
                    or percent_black_data == '' or percent_asian_data == ''):
                continue

            # get parent record objects
            state = self.state_dao.insert_data("CA")
            county = self.county_dao.insert_data(state.state_id, "Los Angeles")
            metro = self.metro_dao.insert_data(
                state.state_id, "Los Angeles-Long Beach-Anaheim")
            city = self.city_dao.insert_data(county.county_id, metro.metro_id,
                                             state.state_id, "Los Angeles")
            zip_code = self.zip_dao.insert_data(city.city_id, county.county_id,
                                                metro.metro_id, state.state_id,
                                                zip_data)

            # insert age_race_sex records
            cursor.execute(
                INSERT_AGE_RACE_SEX_TEMPLATE.format(zip_code.zip_id,
                                                    int(men_data),
                                                    int(women_data),
                                                    float(percent_white_data),
                                                    float(percent_black_data),
                                                    float(percent_asian_data)))

        # commit transaction
        db_conn.commit()

        # close cursor and connection
        cursor.close()
        db_conn.close()
Exemple #24
0
    def insert_data(self, state_id, metro_name):
        # check to see if metro already exists
        metro = self.get_metro(state_id, metro_name)
        if metro is not None:
            return metro

        # insert new metro data
        db_conn = DatabaseManager.get_connection()
        cursor = db_conn.cursor()
        cursor.execute(INSERT_METRO_TEMPLATE.format(state_id, metro_name))
        metro_id = cursor.lastrowid

        # create Metro object
        metro = Metro(metro_id, state_id, metro_name)

        # update metro_data dictionary; there should only be one record
        # created per metro
        self.metro_data[metro_id] = metro

        # commit changes
        db_conn.commit()

        # close cursor and connection
        cursor.close()
        db_conn.close()

        # return new metro
        return metro
Exemple #25
0
    def insert_data(self, state_name):
        # check to see if state already exists
        state = self.get_state_by_name(state_name)
        if state is not None:
            return state

        # insert new state data
        db_conn = DatabaseManager.get_connection()
        cursor = db_conn.cursor()
        cursor.execute(INSERT_STATE_TEMPLATE.format(state_name))
        state_id = cursor.lastrowid

        # create State object
        state = State(state_id, state_name)

        # update state_data dictionary; there should only be one record
        # created per state
        self.state_data[state_id] = state

        # commit changes
        db_conn.commit()

        # close cursor and connection
        cursor.close()
        db_conn.close()

        # return new state
        return state
Exemple #26
0
    def insert_data(self, city_id, county_id, metro_id, state_id, zip_code):
        # check to see if zip already exists
        zip = self.get_zip(city_id, county_id, metro_id, state_id, zip_code)
        if zip is not None:
            return zip

        # insert new zip data
        db_conn = DatabaseManager.get_connection()
        cursor = db_conn.cursor()
        cursor.execute(
            INSERT_ZIP_TEMPLATE.format(city_id, county_id, metro_id, state_id,
                                       zip_code))
        zip_id = cursor.lastrowid

        # create Zip object
        zip = Zip(zip_id, city_id, county_id, metro_id, state_id, zip_code)

        # update zip_data dictionary; there should only be one record
        # created per zip
        self.zip_data[zip_id] = zip

        # commit changes
        db_conn.commit()

        # close cursor and connection
        cursor.close()
        db_conn.close()

        # return new zip
        return zip
Exemple #27
0
    def insert_data(self, state_id, county_name):
        # check to see if county already exists
        county = self.get_county(state_id, county_name)
        if county is not None:
            return county

        # insert new county data
        db_conn = DatabaseManager.get_connection()
        cursor = db_conn.cursor()
        cursor.execute(INSERT_COUNTY_TEMPLATE.format(state_id, county_name))
        county_id = cursor.lastrowid

        # create County object
        county = County(county_id, state_id, county_name)

        # update county_data dictionary; there should only be one record
        # created per county
        self.county_data[county_id] = county

        # commit changes
        db_conn.commit()

        # close cursor and connection
        cursor.close()
        db_conn.close()

        # return new county
        return county
    def delete_data():
        # get connection to database
        db_conn = DatabaseManager.get_connection()
        cursor = db_conn.cursor()

        # delete median listing price records across all tables
        cursor.execute(
            DELETE_MEDIAN_LISTING_PRICE_TEMPLATE.format(
                MEDIAN_LISTING_PRICE_PER_SQFT_ONE_ROOM_TABLE))
        cursor.execute(
            DELETE_MEDIAN_LISTING_PRICE_TEMPLATE.format(
                MEDIAN_LISTING_PRICE_PER_SQFT_TWO_ROOM_TABLE))
        cursor.execute(
            DELETE_MEDIAN_LISTING_PRICE_TEMPLATE.format(
                MEDIAN_LISTING_PRICE_PER_SQFT_THREE_ROOM_TABLE))
        cursor.execute(
            DELETE_MEDIAN_LISTING_PRICE_TEMPLATE.format(
                MEDIAN_LISTING_PRICE_PER_SQFT_FOUR_ROOM_TABLE))
        cursor.execute(
            DELETE_MEDIAN_LISTING_PRICE_TEMPLATE.format(
                MEDIAN_LISTING_PRICE_PER_SQFT_FIVE_PLUS_ROOM_TABLE))

        # commit transaction
        db_conn.commit()

        # close cursor and connection
        cursor.close()
        db_conn.close()
Exemple #29
0
    def __init__(self, width, height, login_sys, path):

        self.line_colour = (255, 255, 255)
        self.point_colour = (255, 255, 255)
        self.bg_colour = (35, 35, 35)
        self.fps_colour = (255, 255, 255)
        self.relative_line_colour = (118, 18, 219)

        self.displacement_arrows = 0
        
        self.fps_array, self.time_array = [], []
        self.fps_array_max_length = 500
        self.fps_graph_interval = 500
        self.start_time = time.time()

        # Initialising all variables used with chosen point and chosen rotation anchor to None
        self.chosen_point, self.chosen_rotation_anchor, self.input_boxes, self.responsive_text = None, None, None, None
        self.clickable_radius = 5 # The radius at which a point can be clicked beyond its shown radius
        self.translating, self.translating_x, self.translating_y = False, False, False
        self.use_custom_rotation_anchor, self.running = False, True

        self.login_sys = login_sys        
        path = os.getcwd()
        self.parent_dir = os.path.join(path, os.pardir)
        Path(r'{}/data'.format(self.parent_dir)).mkdir(parents=True, exist_ok=True)
        Path(r'{}/data/EngineData.db'.format(self.parent_dir)).touch(exist_ok=True)
        self.db_manager = DatabaseManager(r'{}/data/EngineData.db'.format(self.parent_dir))


        # lighting_factor: Controls the contrast of colour in objects, higher means more contrast
        self.display_surfaces, self.display_lines, self.display_points, \
        self.debug_mode, self.display_hud, self.display_logo, \
        self.rotation_factor, self.scaling_factor, self.translation_factor, \
        self.movement_factor, self.max_frame_rate, self.max_render_distance, \
        self.min_render_distance, self.lighting_factor , self.point_radius = self.db_manager.load_user_settings(self)

        self.camera = Camera(self)
        self.gui = GUI(self, self.db_manager, width, height, path)
        self.engine = Engine3D('orthographic', self.gui.viewer_centre)

        pygame.init()
        self.viewer = pygame.display.set_mode((self.gui.viewer_width, self.gui.viewer_height))
        self.clock = pygame.time.Clock()
        self.font = pygame.font.Font(r'{}/fonts/Montserrat-SemiBold.ttf'.format(self.parent_dir), 16)
        pygame.key.set_repeat(1, self.movement_factor)
        self.logo = pygame.image.load(r'{}/images/FL3D_small.png'.format(self.parent_dir))
        self.logo_size = (197, 70)
    def insert_median_listing_price_per_sqft_data(self, table, data_file):
        # get connection to database
        db_conn = DatabaseManager.get_connection()
        cursor = db_conn.cursor()

        # parse data lines
        data_lines = data_file.readlines()

        date_fields = data_lines[0].replace(
            '\"', '').split(',')[MEDIAN_LISTING_PRICE_DATA_MIN_INDEX:]

        for data_line in data_lines[1:]:
            # split data line
            data_line = data_line.replace('\"', '').split(',')

            # get record fields from data line
            zip_data = data_line[ZIP_INDEX]
            state_data = data_line[STATE_INDEX]
            county_data = data_line[COUNTY_NAME_INDEX]
            metro_data = data_line[METRO_INDEX]
            city_data = data_line[CITY_INDEX]
            median_listing_price_data = \
                data_line[MEDIAN_LISTING_PRICE_DATA_MIN_INDEX:]

            # get parent record objects
            state = self.state_dao.insert_data(state_data)
            county = self.county_dao.insert_data(state.state_id, county_data)
            metro = self.metro_dao.insert_data(state.state_id, metro_data)
            city = self.city_dao.insert_data(county.county_id, metro.metro_id,
                                             state.state_id, city_data)
            zip_code = self.zip_dao.insert_data(city.city_id, county.county_id,
                                                metro.metro_id, state.state_id,
                                                zip_data)

            # insert median listing price records
            for date_time, median_listing_price in zip(
                    date_fields, median_listing_price_data):
                # format date
                date_time = date_time.split('-')
                date_time = date(int(date_time[0]), int(date_time[1]),
                                 1)  # MM/1/YYYY

                # format median listing price
                if median_listing_price == '':
                    median_listing_price = "NULL"
                else:
                    median_listing_price = float(median_listing_price)

                cursor.execute(
                    INSERT_MEDIAN_LISTING_PRICE_TEMPLATE.format(
                        table, zip_code.zip_id, date_time.isoformat(),
                        median_listing_price))

        # commit transaction
        db_conn.commit()

        # close cursor and connection
        cursor.close()
        db_conn.close()
    def insert_data(self):
        # open file
        data_file = open("data/demographic/transportation.csv", "r")

        # get connection to database
        db_conn = DatabaseManager.get_connection()
        cursor = db_conn.cursor()

        # parse data lines
        data_lines = data_file.readlines()

        for data_line in data_lines[2:]:
            # split data line
            data_line = data_line.replace('\"', '').split(',')

            # get zip field from data line
            zip_data = data_line[ZIP_INDEX]
            zip_data = zip_data.split(' ')[1]

            # filter based on Los Angeles zips
            if int(zip_data) not in zip_codes:
                continue

            # get transportation data if zip is in LA list
            personal_vehicle_data = data_line[PERSONAL_VEHICLE_INDEX]
            carpool_data = data_line[CARPOOL_INDEX]
            public_transit_data = data_line[PERSONAL_VEHICLE_INDEX]
            taxi_data = data_line[TAXI_INDEX]
            motorcycle_data = data_line[MOTORCYCLE_INDEX]
            bicycle_data = data_line[BICYCLE_INDEX]
            walking_data = data_line[WALKING_INDEX]

            # get parent record objects
            state = self.state_dao.insert_data("CA")
            county = self.county_dao.insert_data(state.state_id, "Los Angeles")
            metro = self.metro_dao.insert_data(state.state_id, "Los Angeles-Long Beach-Anaheim")
            city = self.city_dao.insert_data(county.county_id, metro.metro_id, state.state_id, "Los Angeles")
            zip_code = self.zip_dao.insert_data(city.city_id,
                                                county.county_id,
                                                metro.metro_id,
                                                state.state_id,
                                                zip_data)

            # insert transportation method records
            cursor.execute(INSERT_TRANSPORTATION_METHOD_TEMPLATE.format(zip_code.zip_id,
                                                                        int(personal_vehicle_data),
                                                                        int(carpool_data),
                                                                        int(public_transit_data),
                                                                        int(taxi_data),
                                                                        int(motorcycle_data),
                                                                        int(bicycle_data),
                                                                        int(walking_data)))

        # commit transaction
        db_conn.commit()

        # close cursor and connection
        cursor.close()
        db_conn.close()
def run_ui_test():
    """
    This runs a UI test if we run it
    as the main Python program
    """
    db_manager = DatabaseManager('data.json')
    ui = UserInterface(db_manager)
    ui.mainloop()
Exemple #33
0
    def getArticles(self, drillDownPath):

        fileName = ''
        formerFileName = ''
        for ind in drillDownPath:
            formerFileName = fileName
            fileName+='_' + str(ind)

        with open(CLUST_DIR + 'b' + formerFileName + '.pickle',  'rb') as handle:
            formerClust = pickle.load(handle)

        artNumbers = formerClust[drillDownPath[-1]]

        results = []
        dbMan = DatabaseManager()
        for x in artNumbers:
            results.append(dbMan.get_link(self.listOfArticles[x]))

        return results
Exemple #34
0
def cleanAllWordsFromArticles(listOfArticles, pathToArticles, lock):
    dbManager = DatabaseManager()

    for currentFileName in listOfArticles:

        # Start with structure cleaning
        args = structureCleaning(currentFileName)
        if not args==None:
            lock.acquire()
            passed =  dbManager.put_article_with_args(args)
            lock.release()

            if passed:

                # Save file to new file
                write_to_file(args[3], pathToArticles + currentFileName)

                # Open it again
                # TODO it shouldn't be opened again
                newFileContent = ""
                currentFile = open(pathToArticles + currentFileName, 'r+')

                # Use nltk
                for line in currentFile:
                    for word in line.split():
                        cleanedWord = cleaningOfWord(word)
                        if cleanedWord is not None:
                            newFileContent+=cleanedWord +" "

                # Write new content of file
                currentFile.seek(0)
                currentFile.write(newFileContent)
                currentFile.truncate()
                currentFile.close()
            else:
                print "Duplicate file: " + currentFileName
        else:
            print "Wrong structure, denied file: " + currentFileName
            os.remove(DIR_DUMP + currentFileName)

    return len(listOfArticles)
    def test_fetchone(self):
        """Test for fetching one record."""
        spatialite_path = sg_diagrams_database
        db_manager = DatabaseManager(spatialite_path)

        query = "SELECT province FROM provinces WHERE "
        query += "Within(GeomFromText('POINT(25 -30)'), Geometry)"

        result = db_manager.fetch_one(query)
        expected_result = ('Free State',)
        message = 'Expected %s, got %s' % (expected_result, result)
        self.assertEqual(result, expected_result, message)

        query = "SELECT province FROM provinces WHERE "
        query += "Within(GeomFromText('POINT(100 100)'), Geometry)"

        result = db_manager.fetch_one(query)
        expected_result = None
        message = 'Expected %s, got %s' % (expected_result, result)
        self.assertEqual(result, expected_result, message)
        db_manager.close()
    def __init__(self):
        self.manager = DatabaseManager()
        self.manager.create_databases()

        self.cinema_hall = [['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.']]
 def __init__(self):
     super(ServerService, self).__init__()
     self.database = DatabaseManager()
     self._USERNAME = "******"
     self._DOMAIN = "YLGW036484"
     self._SECRET = "1234"
     self._attack_dict = {}
     self._aggression_matrix = None
     self._cumulative_matrix = None
     self._transition_matrix = None
     self._eigen_vector = None
     self.server_version = 0.5
     self.experimental_round = 1
     self.round_number = 1
    def test_connection(self):
        spatialite_path = sg_diagrams_database
        db_manager = DatabaseManager(spatialite_path)

        query = 'SELECT count(*) FROM provinces'
        result = db_manager.fetch_one(query)

        expected_result = (7,)
        message = 'Expected %s, got %s' % (expected_result, result)
        self.assertEqual(result, expected_result, message)

        db_manager.close()

        spatialite_path = sg_diagrams_database + 'zero'
        db_manager = DatabaseManager(spatialite_path)

        query = 'SELECT count(*) FROM provinces'

        self.assertRaises(DatabaseException, db_manager.fetch_one, query)

        db_manager.close()

        if os.path.exists(spatialite_path):
            os.remove(spatialite_path)
class ReservationSystem:
    MAX_SEATS = 100
    LENGTH = 10
    WIDTH = 10
    __TOP_CINEMA_SEATS = '   1 2 3 4 5 6 7 8 9 10'

    def __init__(self):
        self.manager = DatabaseManager()
        self.manager.create_databases()

        self.cinema_hall = [['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
                            ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.']]

    def show_movies(self):
        movies = self.manager.cursor.execute('SELECT * FROM Movies ORDER BY rating DESC')

        for movie in movies:
            print('[{}] - {} ({})'.format(movie[0], movie[1], movie[2]))

    def make_reservation(self):
        name = input('Input name > ')
        tickets = int(input('Number of tickets > '))

        print('Current Movies: ')
        self.show_movies()

        choice = input('Choose a movie > ')
        movie_name = self.manager.cursor.execute('''SELECT name
                                                    FROM Movies
                                                    WHERE ? = id''', (choice,)).fetchone()[0]
        self.list_projections(movie_name, choice)

        projection_choice = input('Choose projection > ')
        hall_map = self.list_available_seat_map(projection_choice)

        seats = self.choose_seats(hall_map, tickets)

        date = self.manager.cursor.execute('''SELECT date, time
                                      FROM Projections
                                      WHERE id = ?''', (projection_choice, )).fetchone()

        seats_string = ''
        for seat in seats:
            seats_string += '{} '.format(str(seat))

        self.summit_reservation(name, movie_name, '{} {}'.format(date[0], date[1]), seats_string)

        finalizer = input('Write "finalize" to confirm > ')
        if finalizer == 'finalize':
            for seat in seats:
                self.add_reservation(name, projection_choice, seat[0], seat[1])

    def summit_reservation(self, name, movie_name, date, seats):
        print('This is your reservation:\nMovie: {}\nDate: {}\nSeats: {}\n\n'
              .format(movie_name, date, seats))

    def choose_seats(self, hall_map, tickets_count):
        result = []

        while tickets_count > 0:
            seat_choice_string = tuple(input("Choose a seat (e.g '(1, 2)') > "))
            seat_choice = Seat.string_to_seat_tuple(seat_choice_string)

            if Seat.isOutOfRange(seat_choice[0], seat_choice[1], self.LENGTH, self.WIDTH) is True:
                print("Seat is out of boundries")

            elif hall_map[seat_choice[0] - 1][seat_choice[1] - 1] == Seat.SEAT_TAKEN:
                print("Seat is already taken")
            else:
                result.append(seat_choice)
                tickets_count -= 1

        return result

    def list_projections(self, movie_name, choice):
        print('Projections for {}: '.format(movie_name))
        projections = self.manager.cursor.execute('''SELECT id, date, time, type
                                                     FROM Projections
                                                     WHERE movie_id = ?''', (choice, )).fetchall()

        for proj in projections:
            slots = self.manager.cursor.execute('''SELECT Projections.id
                                           FROM Projections
                                           INNER JOIN Reservations
                                           ON Projections.id = Reservations.projection_id
                                           WHERE Projections.id = ?''', (proj[0], ))

            print('[{}] - {} {} ({}) - {}  seats available'.format
                 (proj[0], proj[1], proj[2], proj[3], self.MAX_SEATS - len(list(slots))))

        return projections

    def list_available_seat_map(self, projection_choice):
        print('Available seats (marked with a dot):')

        matrix = deepcopy(self.cinema_hall)
        occupied = self.manager.cursor.execute('''SELECT Reservations.row, Reservations.col
                                          FROM Reservations
                                          INNER JOIN Projections
                                          ON Reservations.projection_id = Projections.id
                                          WHERE Projections.id = ?''', (projection_choice, ))

        for seat in occupied:
            matrix[seat[0] - 1][seat[1] - 1] = Seat.SEAT_TAKEN

        row = ''

        print(self.__TOP_CINEMA_SEATS)

        for i in range(10):
            row += '{:2} '.format(i + 1)

            for j in range(10):
                row += '{} '.format(matrix[i][j])

            print(row)
            row = ''

        return matrix
class ServerService(ClientService):
    def __init__(self):
        super(ServerService, self).__init__()
        self.database = DatabaseManager()
        self._USERNAME = "******"
        self._DOMAIN = "YLGW036484"
        self._SECRET = "1234"
        self._attack_dict = {}
        self._aggression_matrix = None
        self._cumulative_matrix = None
        self._transition_matrix = None
        self._eigen_vector = None
        self.server_version = 0.5
        self.experimental_round = 1
        self.round_number = 1

    def start_experimental_session(self, template, location, experimenter_name):
        self.database.start_experimental_session(template, location, experimenter_name)

    def start_game_phase(self):
        self.database.start_new_game_phase()

    def end_game_phase(self):
        self.database.end_game_phase()

    def save_attacks(self, round_number):
        jid_attack_dict = {}
        for key in self._attack_dict:
            attacker = key.name
            defender = self._attack_dict[key].name
            jid_attack_dict[attacker] = defender
        self.database.insert_attacks(jid_attack_dict, round_number)

    def insert_session(self, experimental_round, game_phase=None, information=None, questionnaire=None):
        self.database.insert_session_order(experimental_round, game_phase, information, questionnaire)

    def register_jids(self):
        self.database.register_jabber_ids(self.player_list)

    def add_attack(self, player0, player1):
        # Only add the attack if that player doesn't have a registered attack yet
        if player0 not in self._attack_dict:
            self._attack_dict[player0] = player1

    def flush_attacks(self):
        self._aggression_matrix = self.generate_aggression_matrix()
        self.add_to_cumulative_matrix(self._aggression_matrix)
        self._transition_matrix = self.calculate_transition_matrix(self._cumulative_matrix)
        self._eigen_vector = self.get_eigen_vector_from_largest_eigen_value(self._transition_matrix)
        # TODO remove test print
        print self._eigen_vector
        self.save_attacks(self.round_number)
        self._attack_dict = {}
        self.round_number = self.round_number + 1

    def player_name_list(self):
        name_list = []
        for player in self.player_list:
            name_list.append(player.name)
        return name_list

    def generate_attack_string(self):
        msg = ""
        for key in self._attack_dict:
            msg = msg + "::" + key.name
            msg = msg + "::" + self._attack_dict[key].name
        return msg

    def elapsed_time(self):
        # Start the timer
        if not self.started:
            self.timestamp = None
            return 0
        # sets the first timer
        if self.timestamp is None:
            self.timestamp = time()
            return 0
        # if there is a previous timer check elapsed time
        else:
            elapsed_time = time() - self.timestamp
            # if elapsed_time is larger than the maximum time, reset timer
            if elapsed_time >= self.max_time:
                self.timestamp = time()
            return elapsed_time

    def generate_aggression_matrix(self):
        # TODO poor performance for now, look into this
        # makes an array the size of self.player_list fills it with 0's
        temp_array = [0] * len(self.player_list)
        # then every entry in the array will be changed to a new array filled with zeroes
        # this array has the size of self.player_list, this makes a n x n array with n = len(self.player_list)
        for entry in range(0, len(temp_array)):
            temp_array[entry] = [0] * len(self.player_list)
        for attack in self._attack_dict:
            # start by finding the indexes of the players
            attacker = None
            defender = None
            found = False
            while not found:
                for index in range(0, len(self.player_list)):
                    if self.player_list[index].name == attack.name:
                        attacker = index
                    elif self.player_list[index].name == self._attack_dict[attack].name:
                        defender = index
                    if attacker is not None and defender is not None:
                        found = True
                # attacker goes to the x and defender to the y
                temp_array[attacker][defender] = 1
                # clears attacker and defender for next iteration
                attacker = None
                defender = None
        return temp_array

    def add_to_cumulative_matrix(self, aggression_matrix):
        if aggression_matrix is not None and self._cumulative_matrix is not None:
            self._cumulative_matrix = numpy.add(aggression_matrix, self._cumulative_matrix)
        else:
            self._cumulative_matrix = aggression_matrix

    def calculate_transition_matrix(self, cumulative_matrix):
        # makes a self.player_list x self.player_list sized matrix
        temp_array = [0] * len(self.player_list)
        for entry in range(0, len(temp_array)):
            temp_array[entry] = [0] * len(self.player_list)

        # getting as small as possible epsilon value given pythons default float as type
        epsilon = numpy.finfo(1.0).eps

        # The mathematical formula in latex form
        # T_{ij} = \dfrac{a_{ij} + \epsilon}{\sum\limits_{n=1}^n=(a_{ik} + \epsilon}
        for i in range(0, len(temp_array)):
            for j in range(0, len(temp_array[i])):
                numerator = cumulative_matrix[i][j] + epsilon
                divisor = 0
                # for every spot calculate the sum of each element in the column i + epsilon for each element
                for k in cumulative_matrix[i]:
                    divisor = divisor + k + epsilon
                temp_array[i][j] = numerator / divisor
        return temp_array

    def get_eigen_vector_from_largest_eigen_value(self, transition_matrix):
        eigen_results = eig(transition_matrix)
        eigen_values = eigen_results[0]
        eigen_vectors = eigen_results[1]

        largest_eigen_index = None
        distance_from_0 = 0
        # finds the index of the eigen value with the largest absolute distance from 0
        for index in range(0, len(eigen_values)):
            if abs(eigen_values[index]) > distance_from_0:
                largest_eigen_index = index
                distance_from_0 = abs(eigen_values[index])

        return eigen_vectors[largest_eigen_index]
Exemple #41
0
class Game:
    DEFAULT_QUESTION_SESSION = 10
    __HELP_MESSAGE = '''
    start - start the game
    exit - quit the game
    highscores - display highscores
    help - display this message'''

    def __init__(self):
        self.manager = DatabaseManager()
        self.user = None

        self.current_question = 1
        self.question_count = self.manager.get_question_count()

    def show_highscores(self):
        users = self.manager.get_highscores()
        for user in users:
            return '{} - {}'.format(user.name, user.score)

    def add_more_questions(self):
        for i in range(self.DEFAULT_QUESTION_SESSION):
            self.manager.add_random_question()

        self.question_count = self.manager.get_question_count()

    def prompt_user(self):
        self.user = UI.get_input('Enter Username: '******'Loading ..')
        self.add_more_questions()

        ques = None
        score = 0

        while self.question_count > 0:
            ques = self.manager.get_question(self.current_question)
            self.current_question += 1

            UI.display(ques.question)
            answer = UI.get_input('Answer: ')

            try:
                if ques.answer == int(answer):
                    UI.display('Correct!\n')
                    score += 2
                else:
                    UI.display('Wrong!\n')
                    break

            except ValueError:
                UI.display('Please enter a valid answer.')
                self.current_question -= 1
                continue

            if self.question_count == 1:
                self.add_more_questions()

            self.question_count -= 1

        self.manager.set_score(self.user, score)

    def start(self):
        while True:
            command = UI.get_input("> ")
            if command == 'start':
                self.ask()
            elif command == 'highscores':
                UI.display(self.show_highscores())
            elif command == 'help':
                UI.display(self.__HELP_MESSAGE)
            elif command == 'exit':
                return
Exemple #42
0
    def __init__(self):
        self.manager = DatabaseManager()
        self.user = None

        self.current_question = 1
        self.question_count = self.manager.get_question_count()
    def canvasReleaseEvent(self, event):
        """Slot called when the mouse button is released on the canvas.

        :param event: Canvas event containing position of click, which button
            was clicked etc.
        """
        if not event.button() == Qt.LeftButton:
            return

        def progress_callback(current, maximum, message=None):
            """GUI based callback implementation for showing progress.

            :param current: Current progress.
            :type current: int

            :param maximum: Maximum range (point at which task is complete.
            :type maximum: int

            :param message: Optional message to display in the progress bar
            :type message: str, QString
            """
            if message is not None:
                self.message_bar.setText(message)
            if self.progress_bar is not None:
                self.progress_bar.setMaximum(maximum)
                self.progress_bar.setValue(current)

        self.iface.messageBar().pushMessage(
            self.tr('SG Downloader.'),
            self.tr('Preparing for download'),
            level=QgsMessageBar.INFO,
            duration=10)

        # No need to check that it is a valid, polygon layer
        # as the QAction for this map tool already does that
        layer = self.canvas.currentLayer()

        place = self.toMapCoordinates(event.pos())
        rectangle = point_to_rectangle(place)

        request = QgsFeatureRequest(QgsFeatureRequest.FilterRect)
        # Ensure only those features really intersecting the rect are returned
        request.setFlags(QgsFeatureRequest.ExactIntersect)
        request.setFilterRect(rectangle)
        polygons = layer.getFeatures(request)
        feature = QgsFeature()
        fetch_list = []
        all_fields = layer.pendingFields()
        text_fields = []
        # Ignore any columns that don't contain text data
        for field in all_fields:
            if field.typeName() == 'String' or field.typeName() == 'Text':
                text_fields.append(field)

        self.setup_message_bar()
        sg_field = None
        while polygons.nextFeature(feature):
            # geom = feature.geometry()
            # attributes = feature.attributes()
            # matched = False
            # sg_code = None
            if sg_field is None:
                for field in text_fields:
                    value = str(feature[field.name()])
                    if not is_valid_sg_code(value):
                        continue
                    sg_field = field.name()
                    fetch_list.append(value)
            else:
                # We already know which column has SG codes
                value = str(feature[sg_field])
                fetch_list.append(value)
        if len(fetch_list) == 0:
            self.iface.messageBar().pushMessage(
                self.tr('SG Downloader.'),
                self.tr('No parcels found with a valid 21 Digit code'),
                level=QgsMessageBar.WARNING,
                duration=10)
            return

        province = province_for_point(self.db_manager, place)

        report = ''
        sg_diagrams_database = os.path.join(DATA_DIR, 'sg_diagrams.sqlite')
        data_manager = DatabaseManager(sg_diagrams_database)

        i = 0
        for sg_code in fetch_list:
            i += 1
            message = 'Downloading SG Code %s from %s' % (sg_code, province)
            progress_callback(i, len(fetch_list), message)
            report += download_sg_diagram(
                data_manager,
                sg_code,
                province,
                self.output_directory,
                callback=progress_callback)
        data_manager.close()

        try:
            write_log(report, self.log_file)
        except IOError as e:
            print e

        self.show_log(report, self.log_file)