def exposed_create_table(self): self.db = DbManager('localhost', 'root', '', 'testowa') self.db.executesql( "CREATE TABLE IF NOT EXISTS programs (id integer PRIMARY KEY AUTO_INCREMENT, program_name VARCHAR(128) NOT NULL, code_text VARCHAR(4096) NOT NULL, date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP)" ) self.db.executesql( "CREATE TABLE IF NOT EXISTS programs_diffs (id integer PRIMARY KEY AUTO_INCREMENT, program1_id integer NOT NULL, program2_id integer NOT NULL, similarity float NOT NULL DEFAULT 0, diff VARCHAR(4096) NULL)" )
def update_players_table(): db_manager = DbManager(NBA_DB) existing_player_ids = set() if not players_table_exists(db_manager): create_players_table(db_manager) else: existing_player_ids.update(get_player_ids(db_manager)) if not game_logs_table_exists(db_manager): create_game_logs_table(db_manager) active_players = get_active_players() for player in active_players: time.sleep(1) player_id = player[0] print(player_id) if not player_id in existing_player_ids: insert_player(db_manager, player) stats = get_player_stats(player_id, 2020) for game_log in stats: insert_game_log(db_manager, game_log)
_PROFIT_ = Decimal(0.001) decimal_precision = 1000000 # 6 openpos = False pos = Position('null', -99, -99) net = Decimal(0) capital = Decimal(1000) count = 0 lastdate = None lastbuy = None lastsell = None manager = DbManager() manager.connect('kraken.db') pd.options.display.width = 0 def data_writer(data): t = time.localtime() current_time = time.strftime("%H:%M:%S", t) f = open("tetherdata.txt", "a") f.write(data + "\n") def margin_check(): bid_ask = c.getBidAsk('USDTZUSD') bidmargin = bid_ask[0] - 1
def __init__(self): self.db = DbManager()
class Module4Server(Service): filename = 'test.py' db = DbManager('localhost', 'root', '', 'testowa') message_for_client = '' client_program_id = 0 db_thread = threading.Thread() def exposed_create_table(self): self.db = DbManager('localhost', 'root', '', 'testowa') self.db.executesql( "CREATE TABLE IF NOT EXISTS programs (id integer PRIMARY KEY AUTO_INCREMENT, program_name VARCHAR(128) NOT NULL, code_text VARCHAR(4096) NOT NULL, date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP)" ) self.db.executesql( "CREATE TABLE IF NOT EXISTS programs_diffs (id integer PRIMARY KEY AUTO_INCREMENT, program1_id integer NOT NULL, program2_id integer NOT NULL, similarity float NOT NULL DEFAULT 0, diff VARCHAR(4096) NULL)" ) def exposed_save_code(self, code: list): file = open(self.filename, 'w') file.truncate() code_text = '' for line in code: file.write(line) code_text += line self.db_thread = threading.Thread(target=self.save_into_db, args=(code_text, )) self.db_thread.start() file.close() return def save_into_db(self, code: str): sql = "INSERT INTO programs (program_name, code_text) VALUES (\'" + self.filename + "\',\'" + code + "\')" self.db.executesql(sql) self.db.commit_changes() self.client_program_id = self.db.cursor.lastrowid return self.client_program_id def exposed_execute_code(self, val: str): try: if val.isdigit() == False: raise ValueError() fib = subprocess.call("python " + self.filename + ' ' + val) return str(fib) except ValueError: er_comment = "Niepoprawna wartosc argumentu" return er_comment except: er_comment = "Blad w programie obliczajacym ciag Fib" return er_comment def exposed_compare_code(self, n: int): sql = "SELECT * FROM programs" self.db.executesql(sql) rows = self.db.get_result() row = next((x for x in rows if x['id'] == n), None) if row == None: return code = str(row['code_text']) message = '' new_id_text = '=========\nID = ' sim = 'Similarity = ' for r in rows: r_id = int(r['id']) if r_id == n: continue code2 = str(r['code_text']) c1 = code.splitlines(False) c2 = code2.splitlines(False) diff = difflib.unified_diff(c1, c2) match = difflib.SequenceMatcher(None, code, code2) m = match.ratio() str_diff = '' sql = '' temp_text = new_id_text + str(r['id']) + '\n' message += temp_text temp_text = sim + str(m) + '\n' message += temp_text for line in diff: str_diff += line message += line + '\n' sql = "INSERT INTO programs_diffs (program1_id, program2_id, similarity, diff) VALUES (" + str( n) + "," + str(r_id) + "," + str(m) + ",\'" + str_diff + "\')" self.db.executesql(sql) self.db.commit_changes() self.message_for_client = message def exposed_get_message(self): return self.message_for_client def exposed_get_program_id(self): if self.db_thread.is_alive: self.db_thread.join() return self.client_program_id
import unittest from module_4_server import Module4Server from dbmanager import DbManager server = Module4Server() db = DbManager('localhost', 'root', '', 'testowa') class TestServer(unittest.TestCase): def test_create_table(self): server.exposed_create_table() def test_save_code(self): query = "SELECT COUNT(*) FROM programs" db.executesql(query) rows = db.get_result() db.commit_changes() table_size_before = rows[0]['COUNT(*)'] content = '' with open('code.txt', 'r') as file: content = file.readlines() server.exposed_save_code(content) db.executesql(query) rows = db.get_result() db.commit_changes() table_size_after = rows[0]['COUNT(*)'] self.assertEqual(table_size_before + 1, table_size_after) def test_execute_code(self):
def __init__(self): self.db = DbManager( ) # () koymazsak self parametresi eksik hatası veriyor ve çalışmıyor UNUTMA!!!!!! ()
def main(gtfs_zip_or_dir, feed_url, db_file, interval): loader = transitfeed.Loader(feed_path=gtfs_zip_or_dir, memory_db=False) schedule = loader.Load() agency = schedule.GetAgencyList()[0] global time_zone time_zone = pytz.timezone(agency.agency_timezone) db_manager = DbManager(db_file) if not schedule.GetShapeList(): logging.error("This feed doesn't contain shape.txt file. Exit...") return active_trips = ActiveTrips() logging.info("Start at local time {}".format(datetime.now())) while True: cnt, all = 0, 0 before = time.time() feed = read_feed(feed_url) for entity in feed.entity: if entity.HasField('vehicle'): trip_id = entity.vehicle.trip.trip_id try: trip = schedule.GetTrip(trip_id) except KeyError as e: logging.warning( "Faulty trip_id for entity: {}".format(entity)) continue all += 1 vehiclePoint = Point.FromLatLng( entity.vehicle.position.latitude, entity.vehicle.position.longitude) try: trip_state = TripState(trip, vehiclePoint, entity.vehicle.stop_id) except VehicleOutOfPolylineException as e: logging.warning( "Vehicle {1} is out of shape for trip_id {0}".format( trip_id, (entity.vehicle.position.latitude, entity.vehicle.position.longitude))) continue except StopFarFromPolylineException as e: logging.warning( "Couldn't reach all stops for trip_id {}".format( trip_id)) continue cur_trip_progress = active_trips.get_trip_progress(trip_id) new_progress = trip_state.get_trip_progress() if trip_state.get_distance_to_end_stop( ) < 100 and cur_trip_progress == new_progress: continue if cur_trip_progress is not None and new_progress < cur_trip_progress: logging.warning( "The trip_id {} seems to go backwards. Timestamp {}". format(trip_id, entity.vehicle.timestamp)) continue if not active_trips.is_trip_active( trip_id) and trip_state.get_prev_stop_seq() > 2: continue prev_timestamp = active_trips.get_timestamp_for_trip(trip_id) if active_trips.is_trip_active(trip_id): speed = trip_state.get_avrg_speed( entity.vehicle.timestamp - prev_timestamp, new_progress - cur_trip_progress) if speed > 120: #sanity check logging.warning( "Trip {} is trying to advance too quick -> {}km/h, timestamp {}" .format(trip_id, speed, entity.vehicle.timestamp)) continue if entity.vehicle.timestamp != prev_timestamp: cnt += 1 estimated_time = trip_state.get_estimated_scheduled_time() stop_progress = trip_state.get_stop_progress() delay = calculate_delay( _normalize_time(entity.vehicle.timestamp), estimated_time) active_trips.add_update_trip(trip_id, entity.vehicle.timestamp, new_progress) start_day = active_trips.get_day_for_trip(trip_id) db_manager.insert_log(entity.vehicle.trip.route_id, trip_id, trip_state.get_prev_stop_seq(), entity.vehicle.timestamp, start_day, delay, new_progress, stop_progress) try: db_manager.commit() except OperationalError as e: logging.warning("Hard drive overload") continue active_trips.clean_inactive_trips(feed.header.timestamp) proc_time = time.time() - before logging.info("Procesing time {}. Saved {} out of {} records".format( proc_time, cnt, all)) if interval - proc_time > 0: time.sleep(interval - proc_time) else: logging.warning("Processing is taking too long")
#coding=utf-8 import sys import json import os sys.path.append("..") from dbmanager import DbManager from common import Common os.chdir("../") cfg = Common.loadConfig() #print cfg['db'] db = DbManager() db.initConn(cfg['db']) sql = "show databases like 'cms_site_%'" n,data = db.executeQuery(0,sql) for row in data: pid = row[0].replace('cms_site_','') #创建statistics表 sql = "CREATE TABLE IF NOT EXISTS `cms_template_statistics` (`template_id` int(11) NOT NULL COMMENT '模板id',`document_count` int(11) NOT NULL COMMENT '文档数',UNIQUE KEY `template_id` (`template_id`),KEY `document_count` (`document_count`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;" n,createdata = db.execute(pid,sql) print sql print '@@@@@@@@' sql = "show tables like 'cms_tbl_%'" n ,tbldata = db.executeQuery(pid,sql) for tbl in tbldata: tid = tbl[0].replace('cms_tbl_','') sql = 'select count(document_id) as doc_count from '+tbl[0] n,statdata = db.executeQuery(pid,sql) for stat in statdata: print 'TID:',tid,' Count:',stat[0]
import os from distutils.util import strtobool from logging import getLogger import configparser from halo import Halo from ssm import session from ssm.modules.create_logger import EasyLogger from dbmanager import DbManager config_ini = configparser.ConfigParser(os.environ) config_ini.read('./config.ini', encoding='utf-8') logger_level = config_ini['DEFAULT']['log'] use_language = config_ini['DEFAULT']['lang'] show_commit_log = config_ini['OPTIONS']['show_commit_log'] force_show_commit_log = config_ini['OPTIONS']['force_show_commit_log'] # -------------------------------- # 1.loggerの設定 # -------------------------------- # loggerオブジェクトの宣言 logger = getLogger('main') logger = EasyLogger(logger, logger_level=f'{logger_level}').create() spinner = Halo(text='初期化に成功しました', spinner='dots') spinner.start() db_manager = DbManager(session=session, logger=logger, logger_level=f'{logger_level}', show_commit_log=bool(strtobool(show_commit_log)), force_show_commit_log=bool(strtobool(force_show_commit_log))) spinner.succeed()
class BotManager: """ Define all commands used in the bot """ keyboard_manager = KeyboardManager() db_manager = DbManager() def call_back(self, bot, update): """ Represents an incoming callback query from a callback button in an inline keyboard. """ query = update.callback_query method, option = query.data.split(" ") if (option == "all"): reply_markup = self.keyboard_manager.confirm(method) bot.edit_message_text(text="Are you sure?", chat_id=query.message.chat.id, message_id=query.message.message_id, reply_markup=reply_markup) elif (option.isnumeric() == False): if (tobool(option) and method == "rm"): self.db_manager.remove_products(query.message.chat.id) bot.edit_message_text( text="All products successfully removed.", chat_id=query.message.chat.id, message_id=query.message.message_id) elif (tobool(option) and method == "rm_debt"): self.db_manager.pay_debt(query.message.chat.id) bot.edit_message_text(text="Debt successfully paid.", chat_id=query.message.chat.id, message_id=query.message.message_id) else: bot.edit_message_text(text="Operation cancelled", chat_id=query.message.chat.id, message_id=query.message.message_id) elif (method == "add"): """ Increases the quantity of an specific product in database """ query_db = db.SESSION.query(Product).filter_by(id=int(option)) product = query_db.one() product.quantity = product.quantity + 1 db.SESSION.commit() bot.edit_message_text( text="{} successfully added. You owe {}".format( product.name, product.quantity), chat_id=query.message.chat.id, message_id=query.message.message_id) elif (method == "rm"): """ Removes a product from database """ query_db = db.SESSION.query(Product).filter_by(id=int(option)) product = query_db.one() db.SESSION.delete(product) db.SESSION.commit() bot.edit_message_text(text="{} successfully removed.".format( product.name), chat_id=query.message.chat.id, message_id=query.message.message_id) elif (method == "rm_debt"): """ Decreases the quantity of an specific product in database """ query_db = db.SESSION.query(Product).filter_by(id=int(option)) product = query_db.one() product.quantity = product.quantity - 1 db.SESSION.commit() bot.edit_message_text(text="A paid {}. You owe {}".format( product.name, product.quantity), chat_id=query.message.chat.id, message_id=query.message.message_id) def list_product(self, bot, update): """ Lists all products linked to chat """ query = db.SESSION.query(Product).filter_by( chat=update.message.chat.id) product = query.all() text = "*Products*\n" for i in product: text += i.name + " - " + i.price + "\n" update.message.reply_markdown(text) def new_product(self, bot, update): """ Inserts a product into the database linked to chat """ text = update.message.text commands = text.split(" ") commands[-1] = commands[-1].replace(",", ".") if (isinteger(commands[-1]) and commands[-1].find(".") == -1): commands[-1] += ".00" if (len(commands) >= 3 and isfloat(commands[-1])): name = " ".join(commands[1:-1]) price = commands[-1] product = Product(chat=update.message.chat.id, name=name, price=price, quantity=0) db.SESSION.add(product) db.SESSION.commit() update.message.reply_text("Procuct succesfully added") else: update.message.reply_markdown( "To add a new product, type:\n*/newProd* <name of product> <price>" ) def remove_product(self, bot, update): """ Removes a product into the database linked to chat """ keyboard = self.keyboard_manager.create_rm_buttons( update.message.chat.id) reply_markup = InlineKeyboardMarkup(keyboard) update.message.reply_text("Please choose a product:", reply_markup=reply_markup) def fab_config(self, bot, update): """ Adds all products used in FSW """ product = Product(chat=update.message.chat.id, name="Guaraná", price="1.50", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Coca-Cola", price="1.50", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Fanta", price="2.50", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Schweppes", price="2.50", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Sprite", price="2.50", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Suco Maçã", price="2.00", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Suco Goiaba", price="2.00", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Suco Caju", price="2.00", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Suco Uva", price="2.00", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Paçoquinha", price="0.50", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Pé de Moça", price="1.00", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Oreo", price="2.50", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Teens Chocolate", price="1.50", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Bolinho Cenoura", price="1.00", quantity=0) db.SESSION.add(product) product = Product(chat=update.message.chat.id, name="Bolinho Brigadeiro", price="1.00", quantity=0) db.SESSION.add(product) db.SESSION.commit() update.message.reply_text("Products successfully added") def add_to_product(self, bot, update): """ Adds a product to the account """ keyboard = self.keyboard_manager.create_add_buttons( update.message.chat.id) reply_markup = InlineKeyboardMarkup(keyboard) update.message.reply_text("Please choose a product:", reply_markup=reply_markup) def remove_from_product(self, bot, update): """ Removes a product from the account """ keyboard = self.keyboard_manager.create_rm_debt_buttons( update.message.chat.id) reply_markup = InlineKeyboardMarkup(keyboard) update.message.reply_text("Please choose a product:", reply_markup=reply_markup) def show_debt(self, bot, update): """ Shows the total debt in product """ query = db.SESSION.query(Product).filter_by( chat=update.message.chat.id) products = query.all() response = "*Product Price Quantity Value*\n" response += "----------------------------------------------------------\n" debt = 0 for i in products: value = float(i.price) * float(i.quantity) debt += value formatted_name = self.format_names(i.name) formatted_price = self.format_price(i.price) formatted_quantity = self.format_quantity(i.quantity) if (value > 0): formatted_value = self.format_float(value) formatted_value = self.format_price(formatted_value) response += "{} R${} {} R${}\n".format( formatted_name[0], formatted_price, formatted_quantity, formatted_value) for i in range(1, len(formatted_name)): response += formatted_name[i] + "\n" response += "----------------------------------------------------------\n" response += '\nTotal debt = {}'.format(debt) update.message.reply_markdown(response) def format_price(self, price): """ Formats spaces for better apresentation """ price = self.format_float(price) if (float(price) < 10): price = " {}".format(price) return price def format_quantity(self, quantity): """ Formats spaces for better apresentation """ if (int(quantity) < 10): quantity = "{} ".format(quantity) return quantity def format_float(self, old_value): """ Formats the 0s in the number's decimal part """ value = str(old_value).split(".") while (len(value[1]) < 2): value[1] += "0" new_value = value[0] + "." + value[1] return new_value def format_names(self, name): names_list = [] while (len(name) > 8): names_list.append(name[0:8]) name = name[8:] for i in range(len(name), 8): name += " " names_list.append(name) return names_list def help(self, bot, update): """ Replies to user all commands used in the bot """ update.message.reply_text(constants.HELP) def error(self, bot, update, error): """ Log Errors caused by Updates. """ logger.warning('Update "%s" caused error "%s"', update, error)
from config_validator import validate_spider_configuration from configurationspider import ConfigurationSpider from dbmanager import DbManager if __name__ == '__main__': configure_logging() # src/settings.py project_settings = get_project_settings() # all config files must be in the 'spiders' directory spider_files = os.listdir('spiders') # create db manager db = DbManager(elastic_url=project_settings.get('ELASTICSEARCH_URL')) # collect spiders here runner = CrawlerProcess(settings=project_settings) # get all configs from the 'piders' folder for spider_file in spider_files: with open(os.path.join('spiders', spider_file)) as f: spider_config = json.load(f) spider_config['db'] = db # terminate if the validation fails success = validate_spider_configuration(spider_config) if not success: continue
def __init__(self): self.version = "1.8.0" self.guid = Common.md5( str(int(round(time.time() * 1000))) + str(os.getpid())) self.cfg = Common.loadConfig() self.core_pid = self.cfg['system']['pid'] self.core = Core() self.core.set_env('root', os.path.split(os.path.realpath(__file__))[0]) self.thirdparty = ThirdParty() self.thirdparty.load_all() self.curent_user = {} handlers = [(r"/", IndexHandler), (r"/admin/(\w+)", AdminHandler), (r"/editor/(\w+)", EditorHandler), (r"/demo/(\w*)", DemoHandler), (r"/system/(\w+)", SystemHandler), (r"/api/(.*)", ApiHandler)] settings = dict( app_title=u"CMS", app_logo= "ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKIF8gX18gIF8gICBfICBfX18gXyBfXyBfX18gIF9fXyAKfCAnXyBcfCB8IHwgfC8gX198ICdfIGAgXyBcLyBfX3wKfCB8XykgfCB8X3wgfCAoX198IHwgfCB8IHwgXF9fIFwKfCAuX18vIFxfXywgfFxfX198X3wgfF98IHxffF9fXy8KfF98ICAgIHxfX18vICAgICAgICAgICAgICAgICAgICAK", template_path=os.path.join(sys.path[0], "templates"), static_path=os.path.join(sys.path[0], "static"), upload_path=os.path.join(sys.path[0], "upload"), cookie_secret="11oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", xsrf_cookies=False, autoescape=None, pycket={ 'engine': 'redis', 'storage': { 'host': self.cfg['redis']['host'], 'port': self.cfg['redis']['port'], 'db_sessions': 10, 'db_notifications': 11, 'max_connections': 2**31, }, }, cookies={ 'expires_days': 2, }, ui_modules={'Menu': MenuModule}) tornado.web.Application.__init__(self, handlers, **settings) print(str(datetime.now()), 'init db') # Have one global connection to the blog DB across all handlers self.db = DbManager() self.db.initConn(self.cfg['db']) self.cache = redis.Redis(connection_pool=redis.ConnectionPool( host=self.cfg['redis']['host'], port=self.cfg['redis']['port'], db=1)) self.cacheExpired = 3600 print(str(datetime.now()), 'build cache') #build system cache project_cfg = [] for item in self.cfg['db']: if item['pid'] != 0: project_cfg.append({ 'pid': item['pid'], 'project': item['project'], 'domain': item['domain'] }) self.cache.set('project_config', json.dumps(project_cfg, ensure_ascii=False)) self.cache.set('lang', json.dumps(self.core.language_config)) self.cache.set( 'system_fields', json.dumps(self.cfg['default_template_field'], ensure_ascii=False)) #用于内部API调用的token self.cache.set('guid', self.guid) self.schema = Schema(self) self.schema.load_schema()
from logging import getLogger from halo import Halo from sqlalchemy import create_engine from .module.create_logger import EasyLogger from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base from dbmanager import DbManager # スピナー周り spinner = Halo(text='Loading', spinner='dots') spinner.start("プログラムの初期化を開始します") # ログ周り logger_level = 'DEBUG' logger = getLogger(__name__) logger = EasyLogger(logger, logger_level=f'{logger_level}').create() spinner.succeed("ログの初期化に成功") # データベース周り Engine = create_engine('sqlite:///app/db/intsl_py.db', encoding="utf-8") # DEBUG時のみecho=True Session = sessionmaker(bind=Engine) session = Session() Base = declarative_base() db_manager = DbManager(session=session, logger=logger, logger_level=f'info') spinner.succeed("データベースの初期化に成功")