def modified_k_means(drivers, athletes): """ :param drivers: :param athletes: :return: """ data = init_distance_matrix(drivers, athletes) # assign athletes to cars assign_to_cars(drivers, athletes, data) # update centroids based on new grouping is_complete = update_drivers(drivers, athletes) # move centroids until old and new match while not is_complete: # get distance matrix distances = init_distance_matrix(drivers, athletes) # assign athletes to cars assign_to_cars(drivers, athletes, distances) # update centroids based on new grouping is_complete = update_drivers(drivers, athletes) log.info('Successfully assigned athletes to cars as follows: {}'.format( drivers)) return drivers
def init_coordinates(self): geolocator = Nominatim(scheme='http') address = ' '.join( [self.address, self.city, self.state, str(self.zip)]) log.info(quote(address)) query = { 'street': self.address, 'city': self.city, 'state': self.state, 'postalcode': str(self.zip) } try: location = geolocator.geocode(query) except GeocoderServiceError as e: log.error(e) log.error( 'Address {},{},{},{} could not be found using geopy.'.format( self.address, self.city, self.state, self.zip)) location = None if location: self.x = location.latitude self.y = location.longitude db.update('users', update_cols=['x', 'y'], update_params=[self.x, self.y], where_cols=['user_id'], where_params=[self.user_id]) else: log.error("Address {} {}, {} {} could not be located".format( self.address, self.city, self.state, self.zip))
def save_erg_image(): img = request.form.get('img') if img: log.info("Have new erg image") #picture_location = util_basic.upload_erg_image(img, current_user.user_id) img = img.split(',')[1] print(len(img)) #nparr = np.fromstring(img, np.uint8) #img_np = cv2.imdecode(data, cv2.IMREAD_ANYDEPTH) #thread = threading.Thread(target=save_contour, args=(self.contours[cnt_idx], self.width, self.height, im_save_path)) #thread.start() # update current user #current_user.picture = pic_location # update the database #db.update('profile', ['picture'], [pic_location], ['user_id'], [current_user.user_id]) # sign certificate #signed_url = sign_certificate(pic_location) return Response(json.dumps({'data': 'hello_world'}), status=201, mimetype='application/json') return Response(json.dumps({}), status=400, mimetype='application/json')
def create_app(): app = Flask('blog') app.config.from_object(CONFIG) log.info("注册各接口蓝本...") app.register_blueprint(article) app.register_blueprint(USER) app.register_blueprint(sneaker) app.register_blueprint(movie) app.register_blueprint(tools) log.info("注册成功...") return app
def spider_job(): """ 爬虫任务 """ tags = CONFIG.TAG the_recommend_movie = {} movie_spider = MovieSpider() all_movie = movie_spider.get_movies() log.info("——————————————根据tag获取movie数据并且筛选出一部推荐电影————————————") for tag in tags: data = MovieInfoDao().select_movie_id_by_tag(tag) db_movie_id = [] if len(data) > 0: for item in data: db_movie_id.append(item.movie_id) for movie in all_movie[tag]: if int(movie['id']) not in db_movie_id: the_recommend_movie[tag] = movie log.info(f"本次推荐电影的index为{all_movie[tag].index(movie)}") break else: the_recommend_movie[tag] = all_movie[tag][0] log.info("——————————————根据推荐电影的id去获取电影的详细信息——————————————————") for index in the_recommend_movie.keys(): movie_dict = {} movie_detail = movie_spider.get_movie_detail( the_recommend_movie[index]['id'])['subject'] log.info(f"{index}: 本次推荐 {json.dumps(movie_dict, ensure_ascii=False)}") if movie_detail != "null": cover = movie_spider.get_cover(the_recommend_movie[index]['cover'], the_recommend_movie[index]['id']) movie_dict['id'] = the_recommend_movie[index]['id'] movie_dict['tag'] = index movie_dict['name'] = movie_detail['title'] movie_dict['url'] = movie_detail['url'] movie_dict['cover'] = cover movie_dict['rate'] = movie_detail['rate'] movie_dict['playable'] = movie_detail['playable'] movie_dict['new'] = the_recommend_movie[index]['is_new'] movie_dict['source'] = None movie_dict['actors'] = movie_detail['actors'] movie_dict['directors'] = movie_detail['directors'] movie_dict['region'] = movie_detail['region'] movie_dict['types'] = movie_detail['types'] movie_dict['release_year'] = movie_detail['release_year'] movie_dict['duration'] = movie_detail['duration'] MovieInfoDao().insert_movie(movie_dict) log.info("——————————————————把推荐电影插入数据库————————————————")
def get_article_id(): kind = request.args.get('kind') data = db.select_by_kind(kind, 10) if data is not False: res_list = [] if len(data) > 0: for item in data: article_dict = { 'name': item.title, 'id': item.id, 'time': item.create_time.strftime("%Y-%m-%d"), 'tags': json.loads(item.tag) } res_list.append(article_dict) log.info("getNameByKind返回") return json.dumps(res_list) else: return 'error'
def validate_address(address_str): parsed_address = usaddress.tag(address_str) address_dict = None tagged_address = dict(parsed_address[0]) log.debug('tagged_address: {}'.format(tagged_address)) if not parsed_address[1] == 'Street Address': log.warn('{} is not a valid address'.format(address_str)) else: address_number_seq = (tagged_address.get('AddressNumberPrefix', ''), tagged_address.get('AddressNumber', ''), tagged_address.get('AddressNumberSuffix', '')) address_number = ' '.join(address_number_seq) # Remove extra internal whitespace address_number = ' '.join(address_number.strip().split()) street_name_seq = (tagged_address.get('StreetNamePreDirectional', ''), tagged_address.get('StreetNamePreModifier', ''), tagged_address.get('StreetNamePreType', ''), tagged_address.get('StreetName', ''), tagged_address.get('StreetNamePostDirectional', ''), tagged_address.get('StreetNamePostModifier', ''), tagged_address.get('StreetNamePostType', '')) street_name = ' '.join(street_name_seq) street_name = ' '.join(street_name.strip().split()) street_address = ' '.join((address_number, street_name)) address_dict = { 'address': street_address, 'city': tagged_address.get('PlaceName', ''), 'state': tagged_address.get('StateName', ''), 'zip': int(tagged_address.get('ZipCode', '')) } log.info('Successfully parsed {} into {}'.format( address_str, address_dict)) return address_dict
def user_from_csv_row(cls, csv_data, active=True): csv_data = {k: v for k, v in csv_data.items() if v is not None} col_names = list(csv_data.keys()) col_values = list(csv_data.values()) username_select = db.select(table_name='users', select_cols=['user_id'], where_cols=['username'], where_params=[csv_data['username']]) if username_select is not None: user_id = username_select['user_id'] db.update('users', col_names, col_values, where_cols=['user_id'], where_params=[user_id], operators=['=']) log.info('Updated {}'.format(user_id)) else: user_id = db.insert('users', col_names, col_values, 'user_id') bio_string = 'Hi, my name is %s!' % csv_data['first'] db.insert('profile', ['bio', 'user_id'], [bio_string, user_id], 'user_id') log.info('Inserted new user {}'.format(user_id))
def job_start(self): self.add_movie_job() log.info("movie 爬虫启动") self.schedule.start()
import os import sys from Utils.db import Database from Utils.log import log TESTING = bool(os.environ.get('TESTING')) try: DB_INIT = os.environ['REQ_DB_INIT'] except KeyError: DB_INIT = False log.info('DB_INIT: {}\nTESTING: {}\n'.format(DB_INIT, TESTING)) db = Database(TESTING) environ_twilio = True try: twilio_sid = os.environ['TWILIO_SID'] twilio_auth_token = os.environ['TWILIO_AUTH_TOKEN'] twilio_number = os.environ['TWILIO_NUMBER'] except KeyError: environ_twilio = False try: from Utils.secret_config import twilio_sid, twilio_auth_token, twilio_number except ModuleNotFoundError: log.error('Could Not Find Twilio Credentials') sys.exit(1)
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import threading try: from Utils.log import log as logging logging.info("from Utils.log import log ok") except ImportError: try: from lib.Log.log import log as logging logging.info("from lib.Log.log import log ok") except ImportError: import logging # logging.basicConfig(level='INFO') log = logging.Logger('root') # getLogger will get the default root logger as its parent, which is different from logging.Logger # log = logging.getLogger("test") # log.setLevel(logging.DEBUG) # log = logging.Logger('test') handler = logging.StreamHandler(sys.stdout) handler.setLevel(logging.INFO) handler.setFormatter( logging.Formatter( "[%(levelname)s] %(asctime)s,%(lineno)4d, %(funcName)s : %(message)s", "%d/%b/%Y:%H:%M:%S %z")) log.addHandler(handler) log.debug("OK")
def new_signup(): # forms to handle sign up and sign in signup_form = web_forms.SignUpForm() signin_form = web_forms.SignInForm() login = True if request.method == 'POST': if signin_form.data['submit_bttn']: if signin_form.validate_on_submit(): username = signin_form.data['username_field'] password = signin_form.data['password_field'] result = db.select('users', ['password', 'user_id'], ['username'], [username]) log.info('here is result: {}'.format(result)) if result: hash = result['password'] password_match = pbkdf2_sha256.verify(password, hash) if password_match: curr_user = User(result['user_id']) login_user(curr_user) next_url = request.args.get('next') if not is_safe_url(next_url): return abort(400) if not current_user.is_profile_complete(): flash( 'Please complete your profile before continuing!' ) return redirect(next_url or url_for('profile')) return redirect(next_url or url_for('team')) signin_form.username_field.errors.append( "Invalid Username or Password.") elif signup_form.data['submit']: if signup_form.validate(): # create token token = ts.dumps(signup_form.data['email'], salt='email-confirm-key') # build url confirm_url = url_for('confirm_email', token=token, _external=True) # set up html that makes up email html = render_template('signup_email.html', validate_url=confirm_url, user={ 'first': signup_form.data['first'], 'last': signup_form.data['last'] }) # create thread to speed up process subject = "Confirm Your Email" t1 = threading.Thread(target=util_basic.send_email, args=(signup_form.data['email'], html, subject)) t1.start() # create user curr_user = User.user_from_form(signup_form.data) # log user in login_user(curr_user) # wait for thread t1.join() # flash message and redirect user to their new profile page flash( 'Please check your email and follow the instructions to confirm your email address.', 'alert-success') return redirect(url_for('profile')) login = False return render_template( 'signup.html', sign_up=signup_form, sign_in=signin_form, login=login, _url= "https://s3-us-west-2.amazonaws.com/athlessary-images/defaults/login_photo.jpg" )
def logout(): log.info('Logging out user id:%s' % current_user.user_id) logout_user() return redirect(url_for('new_signup'))
fetchone=True) athletes.append(result) car_string += ('{} {}: {}, {} - {}\n'.format( result['first'], result['last'], result['address'], result['city'], result['phone'])) car_info['athletes'] = athletes twilio_client.messages.create( to="+1{}".format(driver['phone']), from_=twilio_number, body="Hi {}, your car tomorrow is: {}".format( driver['first'], car_string)) full_car_info.append(car_info) return render_template('cars.html', car_info=full_car_info) def validate_filename(filename): return '.' in filename and filename.rsplit( '.', 1)[1].lower() in ALLOWED_EXTENSIONS @application.route('/no_login') def no_login(): return 'no login required' if __name__ == '__main__': log.info('Begin Main') application.run()
from email.mime.text import MIMEText import boto3 import xml.etree.ElementTree as ET import numpy as np import requests from Forms import web_forms from Utils.log import log from Utils.config import db from Utils.config import password_recovery_email, password_recovery_email_creds # get s3 bucket name try: bucket_name = os.environ['S3_BUCKET'] log.info('bucket {} found from environment'.format(bucket_name)) except KeyError: try: from Utils.secret_config import bucket_name log.info('bucket {} found from config file'.format(bucket_name)) except ModuleNotFoundError: sys.stderr.write('Could Not Establish Bucket Connection') sys.exit(1) def create_workout(user_id, db, meters, minutes, seconds, by_distance): # TODO should this be a part of user?? # get time stamp without seconds date_stamp = datetime.datetime.utcnow() stamp = "{}-{}-{} {}:{}:{}".format(date_stamp.year, date_stamp.month,