def get_ships(self, num=10): query = f"SELECT {FIELDS} FROM ships " \ f"INNER JOIN countries ON countries.id = ships.country_id " \ f"ORDER BY ships.id ASC " \ f"LIMIT {num}" self.cur.execute(query) for params in self.cur: yield Ship(*params)
def get_ship_by_name(self, ship_name): query = f"SELECT {FIELDS} FROM ships " \ f"INNER JOIN countries ON countries.id = ships.country_id " \ f"WHERE ships.name = %s" self.cur.execute(query, (ship_name,)) params = self.cur.fetchone() self.log.info(f'get_ship_by_name [{ship_name}]: {params}') return Ship(*params)
def add_ship(): if request.method == 'GET': return render_template('add_ship.html') else: temp_ship=Ship(name=request.form['name'],country_name=request.form['country'],description=request.form['description'], length=request.form['length'],width = request.form['width'],built_year=request.form['year'] , sid =None) get_db().insert_ship(temp_ship) return render_template('ships_table_form.html',ship_list=get_db().get_ships())
def edit(): if request.method == 'POST': if (not request.form['name'] or not request.form['country'] or not request.form['description']or not request.form['year'] or not request.form['length']or not request.form['width']): print("Ошибка") # flash('Введены не все поля!', 'error') else: temp_ship = Ship(name=request.form['name'], built_year=request.form['year'],country_name=request.form['country'], length=request.form['length'], description=request.form['description'], width=request.form['width'], sid=request.form['id']) print(temp_ship.make_dict()) get_db().update_ship(temp_ship) return redirect(url_for("result")) else: ship_name = request.args.get('name') temp_ship = get_db().get_ship_by_name(ship_name) countries = get_db().get_countries() return render_template('edit.html', ship=temp_ship, countries=countries)
def run_game(): """游戏主体函数""" # 初始化游戏并创建一个屏幕对象 pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # 创建Play按钮 play_button = Button(ai_settings, screen, "Play") # 创建一个用于存储游戏统计信息的实例 stats = GameStats(ai_settings) # 创建一艘飞船 ship = Ship(ai_settings, screen) # 创建一个用于存储子弹的编组 bullets = Group() # 创建一个外星人群组 aliens = Group() # 创建外星人群 gf.create_fleet(ai_settings, screen, ship, aliens) # 开始游戏主循环 while True: # 监视键盘和鼠标事件 gf.check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets) # 调整飞船的位置 if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) # 绘制屏幕 gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets, play_button)
def new(): app.logger.info(request) if request.method == 'POST': if (not request.form['name'] or not request.form['country'] or not request.form['ship_description']): flash('Please enter all the fields', 'error') else: ship = Ship(None, request.form['name'], request.form['country'], request.form['ship_description'], request.form['built_year']) message = get_db().insert_ship(ship) flash(message) return redirect(url_for('show_all')) countries = get_db().get_countries() return render_template('new.html', countries=countries)
def edit(): ship = None if request.method == 'POST': if (not request.form['name'] or not request.form['country'] or not request.form['ship_description']): flash('Please enter all the fields', 'error') else: ship = Ship(request.form['ship_id'], request.form['name'], request.form['country'], request.form['ship_description'], request.form['built_year']) message = get_db().update_ship(ship) flash(message) return redirect(url_for('show_all')) else: ship_name = request.args.get('name') if ship_name: ship = get_db().get_ship_by_name(ship_name) countries = get_db().get_countries() return render_template('edit.html', ship=ship, countries=countries)
def work(self, wnum): self.log.debug(f'{wnum} worker started') rab_connection = RabbitQueue(CRAWLER_EXCHANGE_NAME, CRAWLER_QUEUE_NAME) db_connection = DbPg(self.log) driver, prox = self.init_browser() for raw_msg in rab_connection.get_generator(self.exit_event): if not raw_msg: if self.exit_event.wait(2): break continue msg = raw_msg.json() if 'url' not in msg: self.log.warning(f'{wnum}: bad task: {msg}') raw_msg.ack() continue if msg['num'] == 0: msg['url'] = msg['url'].split('?')[0] try: driver.get(msg['url']) self.log.debug(driver.current_url) time.sleep(3) # parse with selenium rows = driver.find_elements_by_css_selector("tr") if not rows: self.log.debug(f'{wnum}: not rows in table') raw_msg.nack(requeue=True) break for row in rows: cells = row.find_elements_by_css_selector("td") if not cells: continue data = { 'img_url': cells[0].find_element_by_css_selector( 'img').get_attribute('src'), 'country': cells[1].find_element_by_css_selector( 'span').get_attribute('title'), 'vessel_name': cells[1].text.split('\n')[0], 'vessel_type': cells[1].text.split('\n')[1], 'year': cells[2].text, 'gt': cells[3].text, 'dwt': cells[4].text, 'sz': cells[5].text } vlength, vwidth = [ int(v.strip()) for v in data['sz'].split('/') ] self.log.debug(data) ship = Ship( sid=None, name=data['vessel_name'], country_name=data['country'], description=f'{data["vessel_type"]}, {data["img_url"]}', built_year=data['year'], length=vlength, width=vwidth, gt=data['gt'], dwt=data['dwt']) db_connection.insert_ship(ship) db_connection.exec_query(f''' INSERT INTO pages (page_num) VALUES({msg['num']}) ''') raw_msg.ack() except Exception as e0: self.log.error(f'{wnum}: get page error: {e0}') raw_msg.nack(requeue=True) if USE_PROXY: self.proxy_gen.back_proxy(prox, str(e0)) driver.close() driver, prox = self.init_browser() time.sleep(random.randrange(1, 5)) rab_connection.close() db_connection.close() self.log.info(f'{wnum}: worker exit')
def work(self, wnum): rab_connection = RabbitQueue(CRAWLER_EXCHANGE_NAME, CRAWLER_QUEUE_NAME) db_connection = DbPg(logger=None) driver, prox = self.init_browser() for raw_msg in rab_connection.get_generator(self.exit_event): if not raw_msg: if self.exit_event.wait(2): break continue msg = raw_msg.json() if 'url' not in msg: raw_msg.ack() continue if msg['num'] == 0: msg['url'] = msg['url'].split('?')[0] try: driver.get(msg['url']) time.sleep(3) html = driver.page_source dom = lxml_html.fromstring(html) # parse with selenium rows = dom.cssselect("tr") if not rows: raw_msg.nack(requeue=True) break for row in rows: cells = row.cssselect("td") if not cells: continue data = { 'img_url': cells[0].cssselect('img')[0].get('src'), 'country': cells[1].cssselect('span')[0].get('title'), 'vessel_name': cells[1].cssselect('a')[0].text_content().strip(), 'vessel_type': cells[1].cssselect('small')[0].text_content().strip(), 'year': cells[2].text_content(), 'gt': cells[3].text_content(), 'dwt': cells[4].text_content(), 'sz': cells[5].text_content() } vlength, vwidth = [int(v.strip()) for v in data['sz'].split('/')] ship = Ship( sid=None, name=data['vessel_name'], country_name=data['country'], description=f'{data["vessel_type"]}, {data["img_url"]}', built_year=data['year'], length=vlength, width=vwidth, gt=data['gt'], dwt=data['dwt'] ) db_connection.insert_ship(ship) print(12121) db_connection.exec_query(f''' INSERT INTO pages (page_num) VALUES({msg['num']}) ''') raw_msg.ack() except Exception as e0: raw_msg.nack(requeue=True) if USE_PROXY: self.proxy_gen.back_proxy(prox, str(e0)) try: driver.close() except: pass if not self.exit_event.is_set(): driver, prox = self.init_browser() time.sleep(random.randrange(1, 5)) try: rab_connection.close() db_connection.close() driver.close() except: pass