def processRequest(querry): print(querry) dataDict = dict() # Save the original querry dataDict['rNumber'] = number # Verifies if the number's information already exists in the db # # Is the querry not a number? try: number = int(number) cnx = dbcModule.connect() if cnx: cursor = cnx.cursor() cursor.execute( "SELECT is_prime, is_pali, factorization FROM math_is_fun WHERE id = %s", (number, )) row = cursor.fetchone() cursor.close() if row: (dataDict['isPrime'], dataDict['isPalin'], dataDict['fResult']) = row cnx.close() dataDict['fResult'] = json.loads(dataDict['fResult']) return json.dumps(dataDict) cnx.close() # is the number feasibly factorizable? if len(str(number)) <= 9: dataDict['fResult'] = m4f.prime_fact(number) # does the query has only one factor and its factor's exponent is 1? # Yes! Then It is prime! :D if len(dataDict['fResult']) == 1 and list( dataDict['fResult'].values())[0] == 1: dataDict['isPrime'] = True else: dataDict['isPrime'] = False # check if the number is palindromic dataDict['isPalin'] = m4f.is_palindromic(number) # Record the number information into the database # if not dataDict['isPrime']: cnx = dbcModule.connect() if cnx: cursor = cnx.cursor() cursor.execute( "INSERT INTO math_is_fun (id, is_prime, is_pali, factorization) VALUES (%s, %s, %s, %s)", (dataDict['rNumber'], dataDict['isPrime'], dataDict['isPalin'], json.dumps(dataDict['fResult']))) cursor.close() cnx.commit() cnx.close() else: dataDict['fResult'] = 'Too large' except ValueError: dataDict['fResult'] = 'NaN' return json.dumps(dataDict)
def __next__(self): # If the prime list isn't exausted then # send the next prime number. if self.counter < len(self.primes_list): self.counter += 1 return self.primes_list[self.counter - 1] # Ok, It is exausted, try now to retrive more prime numbers from # the database elif self.availableRecords > len(self.primes_list): cnx = dbcModule.connect() if cnx: cursor = cnx.cursor() l = len(self.primes_list) + 1 cursor.execute( "SELECT id FROM math_is_fun WHERE is_prime LIMIT %s, %s", (l, l + 1000)) for record in cursor: self.primes_list.append(record[0]) cursor.close() cnx.close() return self.primes_list[self.counter] # If the connection with the database was not successful # # Then set the flag to not retrive more records # else: self.availableRecords = 0 # It seems that we have exhausted the number primes # # From both the loaded list and the database # # Then Let's start to generate prime number on the fly # while True: for p in self.primes_list[1:]: # Is it divisible by p? if self.prime_candidate % p == 0: break # Have we exhausted the primes list? and haven't we found \\ # any number which # Set the first prime candidate # Therefore we've found a new prime number if self.prime_candidate % p != 0: self.primes_list.append(self.prime_candidate) # Insert the discovered prime number into the DB # cnx = dbcModule.connect() if cnx: cursor = cnx.cursor() cursor.execute( "INSERT INTO math_is_fun (id, is_prime, is_pali, factorization) VALUES (%s, %s, %s, %s)", (self.primes_list[-1], True, is_palindromic(self.primes_list), json.dumps({self.primes_list[-1]: 1}))) cursor.close() cnx.commit() cnx.close() return self.prime_candidate # Generate a new prime candidate self.prime_candidate += 2
def __init__(self): # Set the fundamental global variables self.primes_list = [] self.availableRecords = 0 self.counter = 0 # Access the db in order to get the first 1000 primes cnx = dbcModule.connect() if cnx: cursor = cnx.cursor() # How many prime records are therein the database? cursor.execute("SELECT COUNT(id) FROM math_is_fun WHERE is_prime") self.availableRecords = cursor.fetchone()[0] # Retrive the records if the quantity is more than 2 if self.availableRecords >= 2: cursor.execute( "SELECT id FROM math_is_fun WHERE is_prime LIMIT 1000") for id in cursor: self.primes_list.append(id[0]) else: # if there are less than the two prime numbers in the # # Add them into the DB # q = "INSERT INTO math_is_fun (id, is_prime, is_pali, factorization) VALUES (%s, %s, %s, %s)" cursor.execute(q, (2, True, True, json.dumps({'2': 1}))) cursor.execute(q, (3, True, True, json.dumps({'3': 3}))) cnx.commit() self.primes_list = [2, 3] cursor.close() cnx.close() # If the connection with the database fails, # # Set a list with the two first prime numbers # else: self.primes_list = [2, 3]
def img_url(column_name,table_name,id_article): img_url_list=[] post_id_list=[] posts_list=database_connection.connect(column_name,table_name) for i in range(len(posts_list)): user_post=posts_list[i] soup = bs4.BeautifulSoup(user_post, "html.parser") images = soup.findAll('img') for image in images: img_url_list.append(str(image['src'])) post_id_list.append(id_article[i]) return img_url_list,post_id_list
import dash_core_components as dcc import dash_html_components as html import dash_table import pandas as pd import dash import plotly.graph_objects as go import numpy as np from database_connection import connect import dash_bootstrap_components as dbc # connect to database conn = connect() sql = "select * from vehicle_data;" df_vehicle_data = pd.read_sql_query(sql, conn) sql = "select * from vehicle_cost_data" df_cost_data = pd.read_sql_query(sql, conn) df_cost_data = df_cost_data.round(decimals=2) sql = "select * from driving_data" df_driving_data = pd.read_sql_query(sql, conn) df_driving_data = df_driving_data.round(decimals=2) conn = None # get data from csv files # df_vehicle_data = pd.read_csv('vehicle_data.csv') # df_driving_data = pd.read_csv('driving_data.csv') # df_driving_data = df_driving_data.round(decimals=2) # df_cost_data = pd.read_csv('vehicle_cost_data.csv') # df_cost_data = df_cost_data.round(decimals=2) # colors colors_1 = [
try: img = Image.open(BytesIO(response.content)) text=pytesseract.image_to_string(img,lang='eng') plt.imshow(img) plt.show(img) print(text) print('-'*50) except Exception as e: print('Skipping ', i) print('-'*50) i+=1 continue id_article=database_connection.connect('id,profile_image','p1036_mst_article') #Requesting database for Description column and extracting images url articles_images_url_list,post_id_list=img_url('description','p1036_mst_article',id_article) #Extracting text from images present inside the article text_from_article_images(articles_images_url_list,post_id_list) #Requesting database for profile images of article profile_img_url_list=database_connection.connect('profile_image','p1036_mst_article') profile_img_list,profile_id_list=preprocess_profile_images(profile_img_url_list,id_article) #Extracting text from profile images text_from_profile_images(profile_img_list,profile_id_list)
def is_exact_power(number): ''' Returns True if arg is an exact power of a certain number. Otherwise returns False. ''' if not type(number) is int: return None # Gets its absolute number number = abs(number) # Let's check if we arelady have the prime factorization in the database cnx = dbcModule.connect() if cnx: cursor = cnx.cursor() cursor.execute( "SELECT is_prime, factorization FROM math_is_fun WHERE id = %s", (number, )) # Does the record exists? nData = cursor.fetchone() if nData: # Is it a prime number ? if nData[0]: return False # Does the number has only one prime factor? if len(json.loads(nData[1])) == 1: return True else: return False cursor.close() cnx.close() pIterator = PrimeIterator() pFact = [{}, number] # Let's ascertain if it's an exact power for p in pIterator: # Okay, is the number disivible by p? if number % p == 0: # Sets the key as the divisor, and the power counter of our number pFact[0][p] = 0 # Let's divide the number and check if it has another prime factor while number != 1: # Okay, the number is not 1 # Does the next division by p has a remainder? if number % p: return False number /= p pFact[0][p] += 1 # Okay we have divided the number for only one prime number # And the final result is 1 # As it has only one prime number divisor # And it IS NOT a prime number # Therefore it is an exact power break # Okay, the number is an exact power, so let's record it in the database cnx = dbcModule.connect() if cnx: cursor = cnx.cursor() cursor.execute( "INSERT INTO math_is_fun (id, is_prime, is_pali, factorization) VALUES (%s, False, %s, %s)", (pFact[1], is_palindromic(pFact[1]), json.dumps(pFact[0]))) cnx.commit() cursor.close() cnx.close() return True
parameter: html content of page return: String of all visible text ''' #function to extract text from html page soup = BeautifulSoup(body, 'html.parser') texts = soup.findAll(text=True) visible_texts = filter(tag_visible, texts) return u" ".join(t.strip() for t in visible_texts) stop = stopwords.words('english') articles = database_connection.connect_2_col('id,description', 'p1036_mst_article') predefined_groups = database_connection.connect('group_name', 'p1036_mst_group') for i in articles: word_tokens_post = word_tokenize(text_from_html(i[1])) for j in range(len(predefined_groups)): w_token = word_tokenize(str(predefined_groups[j])) filter_grp = [a for a in w_token if a not in stop] predefined_groups[j] = ' '.join(filter_grp) #lmtzr = WordNetLemmatizer() #group_list=str() filtered_sentence = [a for a in word_tokens_post if a not in stop] filtered_sentence = [x.lower() for x in filtered_sentence] for j in range(len(predefined_groups)): if predefined_groups[j].lower() in filtered_sentence: print(str(predefined_groups[j]) + '-->' + str(i[0])) #filtered_groups=[a for a in word_tokens if a not in stop]