def test_watch_query_full(): """Test that `watch_query`: * Returns the expected results. * Executes the defined times inside the given interval, in this case with a 0.3 seconds wait, it should execute 4 times inside a 1 seconds interval. * Stops at Ctrl-C """ watch_seconds = 0.3 wait_interval = 1 expected_value = "1" query = "SELECT {0!s}".format(expected_value) expected_title = '> {0!s}'.format(query) expected_results = 4 ctrl_c_process = send_ctrl_c(wait_interval) with db_connection().cursor() as cur: results = list( result for result in mycli.packages.special.iocommands.watch_query( arg='{0!s} {1!s}'.format(watch_seconds, query), cur=cur)) ctrl_c_process.join(1) assert len(results) == expected_results for result in results: assert result[0] == expected_title assert result[2][0] == expected_value
def test_watch_query_full(): """Test that `watch_query`: * Returns the expected results. * Executes the defined times inside the given interval, in this case with a 0.3 seconds wait, it should execute 4 times inside a 1 seconds interval. * Stops at Ctrl-C """ watch_seconds = 0.3 wait_interval = 1 expected_value = "1" query = "SELECT {0!s}".format(expected_value) expected_title = '> {0!s}'.format(query) expected_results = 4 ctrl_c_process = send_ctrl_c(wait_interval) with db_connection().cursor() as cur: results = list( result for result in mycli.packages.special.iocommands.watch_query( arg='{0!s} {1!s}'.format(watch_seconds, query), cur=cur ) ) ctrl_c_process.join(1) assert len(results) == expected_results for result in results: assert result[0] == expected_title assert result[2][0] == expected_value
def choose_content(self): """ This can be either - gif from pankreator.org site or - djvu image from the PAN library. """ with db_connection(self.db) as cursor: cursor.execute( 'select * from pankreator_gifs order by id desc limit 1;') last_record = cursor.fetchone() # If gif wasn't added yesterday, add one. yesterday = date.today() - relativedelta(days=+1) if (not last_record) or (last_record[4] < yesterday): media_file_path, result = self.get_gif() if media_file_path and result: query = 'insert into pankreator_gifs (title, url, gif_url, date_added)'\ 'values (?, ?, ?, ?)' cursor.execute(query, (result['title'], result['url'], result['gif_url'], date.today())) return media_file_path, '%s %s' % (result['title'], result['url']) media_file_path, title = self.get_djvu() return media_file_path, title
def connection(): create_db('_test_db') connection = db_connection('_test_db') yield connection drop_tables(connection) connection.close()
def connection(): utils.create_schema() connection = utils.db_connection() yield connection utils.drop_schema(connection) connection.close()
def connection(): create_db("_test_db") connection = db_connection("_test_db") yield connection drop_tables(connection) connection.close() os.remove("_test_db")
def test_watch_query_iteration(): """Test that a single iteration of the result of `watch_query` executes the desired query and returns the given results.""" expected_value = "1" query = "SELECT {0!s}".format(expected_value) expected_title = '> {0!s}'.format(query) with db_connection().cursor() as cur: result = next( mycli.packages.special.iocommands.watch_query(arg=query, cur=cur)) assert result[0] == expected_title assert result[2][0] == expected_value
def test_watch_query_iteration(): """Test that a single iteration of the result of `watch_query` executes the desired query and returns the given results.""" expected_value = "1" query = "SELECT {0!s}".format(expected_value) expected_title = '> {0!s}'.format(query) with db_connection().cursor() as cur: result = next(mycli.packages.special.iocommands.watch_query( arg=query, cur=cur )) assert result[0] == expected_title assert result[2][0] == expected_value
def test_watch_query_bad_arguments(): """Test different incorrect combinations of arguments for `watch` command.""" watch_query = mycli.packages.special.iocommands.watch_query with db_connection().cursor() as cur: with pytest.raises(ProgrammingError): next(watch_query('a select 1;', cur=cur)) with pytest.raises(ProgrammingError): next(watch_query('-a select 1;', cur=cur)) with pytest.raises(ProgrammingError): next(watch_query('1 -a select 1;', cur=cur)) with pytest.raises(ProgrammingError): next(watch_query('-c -a select 1;', cur=cur))
def test_watch_query_clear(clear_mock): """Test that the screen is cleared with the -c flag of `watch` command before execute the query.""" with db_connection().cursor() as cur: watch_gen = mycli.packages.special.iocommands.watch_query( arg='0.1 -c select 1;', cur=cur) assert not clear_mock.called next(watch_gen) assert clear_mock.called clear_mock.reset_mock() next(watch_gen) assert clear_mock.called clear_mock.reset_mock()
def test_watch_query_clear(clear_mock): """Test that the screen is cleared with the -c flag of `watch` command before execute the query.""" with db_connection().cursor() as cur: watch_gen = mycli.packages.special.iocommands.watch_query( arg='0.1 -c select 1;', cur=cur ) assert not clear_mock.called next(watch_gen) assert clear_mock.called clear_mock.reset_mock() next(watch_gen) assert clear_mock.called clear_mock.reset_mock()
def test_watch_query_interval_clear(clear_mock): """Test `watch` command with interval and clear flag.""" def test_asserts(gen): clear_mock.reset_mock() start = time() next(gen) assert clear_mock.called next(gen) exec_time = time() - start assert exec_time > seconds and exec_time < (seconds + seconds) seconds = 1.0 watch_query = mycli.packages.special.iocommands.watch_query with db_connection().cursor() as cur: test_asserts(watch_query('{0!s} -c select 1;'.format(seconds), cur=cur)) test_asserts(watch_query('-c {0!s} select 1;'.format(seconds), cur=cur))
def check_new_posts(self): """ Something that was found on the site, but wasn't added to the db yet. """ results = self.extract_data_from_page() if not results: return None, None with db_connection(self.db) as cursor: cursor.execute('select * from pankreator_gifs order by id asc') differences = self.compare_results(cursor.fetchall(), results) if differences: for item in results: if item['gif_url'] in differences: new_item = item logger.info('Something new! %s' % new_item['title']) return self.download_image( new_item['gif_url']), new_item return None, None
return botSentence, weight, trainMe, checkStore if __name__ == "__main__": conf = utils.get_config() regexpYes = re.compile(r'yes') DBHOST = conf["MySQL"]["server"] DBUSER = conf["MySQL"]["dbuser"] DBNAME = conf["MySQL"]["dbname"] print("Starting Bot...") # initialize the connection to the database print("Connecting to database...") connection = utils.db_connection(DBHOST, DBUSER, DBNAME) cursor = connection.cursor() connectionID = utils.db_connectionID(cursor) print("...connected") trainMe = False checkStore = False botSentence = 'Hello!' while True: # Output bot's message if DEBUG_WEIGHT: print('Bot> ' + botSentence + ' DEBUG_WEIGHT:' + str(round(weight, 5))) else:
def connection(): utils.create_schema() with utils.db_connection() as connection: yield connection utils.drop_schema(connection)
from flask import Flask, request, jsonify from utils import db_connection app = Flask(__name__) conn, cursor = db_connection() dic = {} # @app.route("/hello") # def hello_world(): # return "hello world!!" # @app.route("/") # def home(): # conn, cursor = db_connection() # if conn: # return "success" # return "not success" @app.route("/login", methods=['POST']) def login(): if request.method == 'POST': data = request.get_json() cursor.execute("SELECT email from details") user_email = cursor.fetchall() for email in user_email: if data["email"] in email: return "email already exists" cursor.execute( "INSERT INTO Details (first_name,last_name,email,password)VALUES (%s, %s,%s,%s)", (data['first_name'], data['last_name'], data['email'],
def connection(): create_db('_test_db') connection = db_connection('_test_db') yield connection connection.close()
print( "Configuring Tables for database configuration: \n \tServer: {0} \n \tDB-User: {1} \n \tDB-Name: {2}" .format(DBHOST, DBUSER, DBNAME)) print("\n** ALL EXISTING TABLES AND DATA WILL BE LOST **\n") response = utils.query_yes_no("Continue?") if response: charTypeShort = "VARCHAR(16) COLLATE utf8_general_ci" charTypeMedium = "VARCHAR(64) COLLATE utf8_general_ci" charTypeLong = "VARCHAR(768) COLLATE utf8_general_ci" print("Connecting to database...", end=" ") connection = utils.db_connection(DBHOST, DBUSER, DBNAME, DBCHARSET) cursor = connection.cursor() print("connected.") print("\nCreating sentences table:") try: try_drop(cursor, "sentences") SQL = 'CREATE TABLE sentences (hashid ' + charTypeShort + ' , sentence ' + charTypeLong + ' , result ' + charTypeLong + ' ) ' print(SQL) cursor.execute(SQL) except Exception as e: print("\n** ERROR **", e) print("\nDone.") else:
def test_favorite_query(): with db_connection().cursor() as cur: query = u'select "✔"' mycli.packages.special.execute(cur, u'\\fs check {0}'.format(query)) assert next(mycli.packages.special.execute( cur, u'\\f check'))[0] == "> " + query
DBNAME = sys.argv[2] loadfile = sys.argv[3] ERRORFILE = "./dump/load.err" # MAIN # print("\nLoading table [" + tablename + "] in database [" + DBNAME + "] from " + loadfile + "\n\n") i=0 failed=0 print("Connecting to database...") connection = utils.db_connection() cursor = connection.cursor() print("connected.") DATE = datetime.now().strftime("%Y-%m-%d %H:%M:%S") MSG = "## Starting " + tablename + " Data-Load:" + DATE + " ##" with open(ERRORFILE, "a") as efile: efile.write(MSG + "\n") with open(loadfile,'rt', encoding = 'utf8') as f: reader = csv.reader(f, skipinitialspace=True) for row in reader: #first line = col names i = i + 1
def connection(): connection = db_connection() yield connection connection.close()
raise RuntimeError('unhandled sentence classification') from error return botSentence, weight, trainMe, checkStore if __name__ == "__main__": conf = utils.get_config() regexpYes = re.compile(r'yes') DBFILE = conf["Sqlite"]["filepath"] print("Starting Bot...") # initialize the connection to the database print("Connecting to database...") connection = utils.db_connection(DBFILE) cursor = connection.cursor() #connectionID = utils.db_connectionID(cursor) print("...connected") trainMe = False checkStore = False botSentence = 'Hello!' while True: # Output bot's message if DEBUG_WEIGHT: #print('Bot> ' + botSentence + ' DEBUG_WEIGHT:' + str(round(weight,5)) ) #2018-02-19 print('Bot> ' + botSentence + ' DEBUG_WEIGHT:' + str(round(weight, 5)))
def connection(): create_db("_test_db") connection = db_connection("_test_db") yield connection connection.close()
import json from utils import db_connection, get_current_turn, change_turns conn = db_connection() def lambda_handler(event, context): id_game = None pathParameters = event.get('pathParameters') if pathParameters: id_game = pathParameters.get('idGame') team = get_current_turn(conn, id_game) if not team: return { 'statusCode': 404, 'headers': { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" }, 'body': json.dumps({'message': "Game not found"}), } try: change_turns(conn, id_game, team) conn.commit() return { 'statusCode': 200, 'headers': {
def session(connection): i = 0 # counter for how many times we have been round the loop # startMessage = "Starting Bot...\n" # Get Config conf = utils.get_config() DBHOST = conf["MySQL"]["server"] DBUSER = conf["MySQL"]["dbuser"] DBNAME = conf["MySQL"]["dbname"] logging.info("Starting Bot session-thread...") # initialize the connection to the database logging.info(" session-thread connecting to database...") DBconnection = utils.db_connection(DBHOST, DBUSER, DBNAME) DBcursor = DBconnection.cursor() DBconnectionID = utils.db_connectionID(DBcursor) logging.info(" ...connected") botSentence = 'Hello!' weight = 0 trainMe = False checkStore = False # startMessage = startMessage + ("...started\n") def receive(connection): logging.debug(" receive(connection): PID {}, thread {} \n".format( pid, thread)) received = connection.recv(1024) if not received: #logging.info("Closing connection {}".format(thread)) return False else: #logging.debug(" Received {}, echoing".format(received)) return received while True: pid = os.getpid() thread = threading.current_thread() # pass received message to chatbot received = receive(connection) humanSentence = received.decode().strip() if humanSentence == '' or humanSentence.strip(punctuation).lower( ) == 'quit' or humanSentence.strip(punctuation).lower() == 'exit': break # Chatbot processing botSentence, weight, trainMe, checkStore = chatbot.chat_flow( DBcursor, humanSentence, weight) logging.debug( " Received botSentence {} from chatbot.chat_flow".format( botSentence)) if trainMe: logging.debug(" trainMe is True") send = "Please train me - enter a response for me to learn (or \"skip\" to skip)' ".encode( ) connection.send(send) previousSentence = humanSentence received = receive(connection) humanSentence = received.decode().strip() logging.debug(" trainMe received {}".format(humanSentence)) if humanSentence != "skip": chatbot.train_me(previousSentence, humanSentence, DBcursor) botSentence = "Thanks I've noted that" #connection.send(send) else: botSentence = "OK, moving on..." #connection.send(send) trainMe = False if checkStore: logging.debug("CheckStore is True") send = 'Shall I store that as a fact for future reference? ("yes" to store)'.encode( ) connection.send(send) previousSentence = humanSentence received = receive(connection) humanSentence = received.decode().strip() logging.debug(" checkStore received {}".format(humanSentence)) if regexpYes.search(humanSentence.lower()): #Store previous Sentence logging.debug(" Storing...") chatbot.store_statement(previousSentence, DBcursor) logging.debug(" Statement Stored.") botSentence = random.choice(chatbot.STATEMENT_STORED) else: botSentence = "OK, moving on..." checkStore = False DBconnection.commit() logging.debug(" sending botSentence back: {}".format(botSentence)) send = botSentence.encode() # if i == 0: # send = startMessage.encode() + send connection.send(send) # i = i + 1 logging.info(" Closing Session")
def session(connection): i = 0 # counter for how many times we have been round the loop startMessage = "Starting Bot...\n" # initialize the connection to the database conf = utils.get_config() DBHOST = conf["MySQL"]["server"] DBUSER = conf["MySQL"]["dbuser"] DBNAME = conf["MySQL"]["dbname"] print("Starting Bot...") # initialize the connection to the database print("Connecting to database...") DBconnection = utils.db_connection(DBHOST, DBUSER, DBNAME) DBcursor = DBconnection.cursor() DBconnectionID = utils.db_connectionID(DBcursor) print("...connected") botSentence = 'Hello!' weight = 0 trainMe = False botSentence = 'Hello!' startMessage = startMessage + ("...started\n") def receive(connection): if DEBUG_SERVER: print("PID {}, thread {} \n".format(pid, thread)) received = connection.recv(1024) if not received: print("Closing connection {}".format(thread)) return False else: if DEBUG_SERVER: print("Received {}, echoing".format(received)) return received while True: pid = os.getpid() thread = threading.current_thread() # pass received message to chatbot received = receive(connection) humanSentence = received.decode().strip() if humanSentence == '' or humanSentence.strip(punctuation).lower( ) == 'quit' or humanSentence.strip(punctuation).lower() == 'exit': break # Chatbot processing botSentence, weight, trainMe = chatbot.chat_flow( DBcursor, humanSentence, weight) if trainMe: send = "Bot> Please train me - enter a response for me to learn (or \"skip\" to skip)' ".encode( ) connection.send(send) previousSentence = humanSentence received = receive(connection) humanSentence = received.decode().strip() if humanSentence != "skip": chatbot.train_me(previousSentence, humanSentence, DBcursor) botSentence = "Bot> Thanks I've noted that" #connection.send(send) else: botSentence = "Bot> OK, moving on..." #connection.send(send) trainMe = False DBconnection.commit() send = botSentence.encode() if i == 0: send = startMessage.encode() + send connection.send(send) i = i + 1