def load_departments(app: Flask): with app.app_context(): from src.database import get_db from src.models.departments_model import DepartmentModel static_folder = cast(str, app.static_folder) eng_data_path = os.path.join(static_folder, "data", "departments_en.json") fr_data_path = os.path.join(static_folder, "data", "departments_fr.json") with open(eng_data_path, encoding="utf-8") as f: eng_data = json.loads(f.read(), encoding="utf-8") with open(fr_data_path, encoding="utf-8") as f: fr_data = json.loads(f.read(), encoding="utf-8") session = get_db() for dept in eng_data: deptobj = DepartmentModel(department=dept, language="en") if eng_data[dept]['abbreviation'] != "null": deptobj.abbreviation = eng_data[dept]['abbreviation'] session.add(deptobj) for dept in fr_data: deptobj = DepartmentModel(department=dept, language="fr") if fr_data[dept]['abbreviation'] != "null": deptobj.abbreviation = fr_data[dept]['abbreviation'] session.add(deptobj) session.commit()
def load_conductedSurveyStatuses(): session = get_db() active = ConductedSurveyStatusModel( status="active", description="The conducted survey is currently being conducted") closed = ConductedSurveyStatusModel( status="closed", description= "The conducted survey is currently closed for answering or modification" ) processing = ConductedSurveyStatusModel( status="processing", description="The survey data is being processed by the server") released = ConductedSurveyStatusModel( status="released", description= "The survey data has been processed and is now available through the api " ) session.add(active) session.add(closed) session.add(processing) session.add(released) session.commit()
def update_prices(self, app): price_timer = Timer( 60.0, self.update_prices, kwargs={'app': app} ) price_timer.name = 'price_timer' price_timer.daemon = True price_timer.start() conn = database.get_db(app) db = conn.cursor() for unit in app.config['units']: self.log.info('fetching price for {}'.format(unit)) streamer_price = self.streamer.get_price(unit) if streamer_price is None: price = self.standard.get_price(unit) self.log.warn('price streamer offline!') else: price = streamer_price db.execute( "INSERT INTO prices (unit, price) VALUES (%s,%s)", ( unit, price ) ) self.log.info('{} price set to {}'.format(unit, price)) conn.commit() conn.close()
def load_object(schema: Schema, json: dict): if json is None or bool(json) is False: return { "error": "must provide JSON for this request" }, 400 result, errors = schema.load(json) if errors is not None and bool(errors) is True: return { "error":{ "message": "The JSON schema you have sent is invalid", "details": errors } }, 400 try: session = get_db() session.add(result) session.commit() except SQLAlchemyError as e: capture_message(e) raise e return { "error": "An Internal Service Error has occured" }, 500 return {},200
def main(fps): db = database.get_db() cur = db.cursor() themes = functions.load_jsondict(os.path.join(dirname, 'db/themes.json')) dossiers = functions.load_jsondict( os.path.join(dirname, 'db/dossiers.json')) pol_parties = database.dbitems_from_table(cur, 'pol_parties') pol_persons = database.dbitems_from_table(cur, 'pol_persons') for fp in fps: for line in functions.jsonl_iterator(fp): if not line['source'] in FLEMISH: continue doc = database.DBItem( 'doc_all', { 'date': functions.timestamp_to_datetime(float(line['published'])), 'theme_name': nlp.detect_theme(themes, line['text']), 'dossier_name': nlp.detect_dossier(dossiers, line['text']), 'url': line['url'], }) news = database.DBItem('doc_news', { 'news_id': line['identifier'], 'source': line['source'], }) for sentence in sent_tokenize(line['text'], language='dutch'): fragment = database.DBItem( 'fragments', { 'content': sentence, 'sentiment': nlp.detect_polarity(sentence, sentiment) }) for pol_party in pol_parties: # temporary fix. todo: Better fix if pol_party.data['short_name'] == 'CD&V': pol_party.data['short_name'] = 'CD' if pol_party.data['short_name'] == 'Vuye&Wouters': pol_party.data['short_name'] = 'Vuye & Wouters' if nlp.detect_pol_party_in_news(pol_party, sentence): mention = database.DBItem( 'mentions', {'pol_id': pol_party.data['pol_id']}) insert = (doc, news, fragment, mention) database.commit_mention(cur, db, insert) for pol_person in pol_persons: if nlp.detect_pol_person_in_news(pol_person, sentence): mention = database.DBItem( 'mentions', {'pol_id': pol_person.data['pol_id']}) insert = (doc, news, fragment, mention) database.commit_mention(cur, db, insert) cur.close() db.close()
def main(fps): db = database.get_db() cur = db.cursor() themes = functions.load_jsondict(os.path.join(dirname, 'db/themes.json')) dossiers = functions.load_jsondict( os.path.join(dirname, 'db/dossiers.json')) pol_parties = database.dbitems_from_table(cur, 'pol_parties') pol_persons = database.dbitems_from_table(cur, 'pol_persons') for fp in fps: for line in functions.jsonl_iterator(fp): if not 'text' in line or not 'id_str' in line or 'retweeted_status' in line: continue text = line['extended_tweet'][ 'full_text'] if 'extended_tweet' in line else line['text'] doc = database.DBItem( 'doc_all', { 'date': functions.timestamp_to_datetime( float(line['timestamp_ms']) / 1000), 'theme_name': nlp.detect_theme(themes, text), 'dossier_name': nlp.detect_dossier(dossiers, text), 'url': 'https://twitter.com/statuses/{}'.format(line["id_str"]), }) tweet = database.DBItem( 'doc_tweets', { 'tweet_id': line['id_str'], 'username': line['user']['screen_name'], }) fragment = database.DBItem( 'fragments', { 'content': text, 'sentiment': nlp.detect_polarity(text, sentiment) }) for pol_party in pol_parties: if nlp.detect_pol_party_in_tweet(pol_party, text): mention = database.DBItem( 'mentions', {'pol_id': pol_party.data['pol_id']}) insert = (doc, tweet, fragment, mention) database.commit_mention(cur, db, insert) for pol_person in pol_persons: if nlp.detect_pol_person_in_tweet(pol_person, text): mention = database.DBItem( 'mentions', {'pol_id': pol_person.data['pol_id']}) insert = (doc, tweet, fragment, mention) database.commit_mention(cur, db, insert) cur.close() db.close()
def get(self, id=None, slug=None): session = get_db() if id is not None: dump, status = get_one_row_by_id(session, SurveyModel, id, SurveySchema) elif slug is not None: dump, status = get_one_row_by_slug(session, SurveyModel, slug, SurveySchema) else: dump, status = get_all_rows(session, SurveyModel, SurveySchema) return dump, status
def load_section_types(app: Flask): with app.app_context(): from src.models.sectionType_model import SectionTypeModel from src.database import get_db session = get_db() greeting = SectionTypeModel(type="introduction") ending = SectionTypeModel(type="conclusion") group = SectionTypeModel(type="group") session.add(greeting) session.add(ending) session.add(group) session.commit()
def get(self): def cal_rho(lon_a,lat_a,lon_b,lat_b): ra=6378.140 # equatorial radius (km) rb=6356.755 # polar radius (km) F=(ra-rb)/ra # flattening of the earth rad_lat_a=np.radians(lat_a) rad_lon_a=np.radians(lon_a) rad_lat_b=np.radians(lat_b) rad_lon_b=np.radians(lon_b) pa=np.arctan(rb/ra*np.tan(rad_lat_a)) pb=np.arctan(rb/ra*np.tan(rad_lat_b)) xx=np.arccos(np.sin(pa)*np.sin(pb)+np.cos(pa)*np.cos(pb)*np.cos(rad_lon_a-rad_lon_b)) c1=(np.sin(xx)-xx)*(np.sin(pa)+np.sin(pb))**2/np.cos(xx/2)**2 c2=(np.sin(xx)+xx)*(np.sin(pa)-np.sin(pb))**2/np.sin(xx/2)**2 dr=F/8*(c1-c2) rho=ra*(xx+dr) return rho args = self.reqparse.parse_args() lat_a = args.lat #データ受け取り lon_a = args.lon #データ受け取り # print(args.lat, args.lon) url = 'https://www.google.com/maps/search/?api=1&query=' db = get_db('osm') min_distance=9999999 # coll ~= db.samples #テストとして京都府庁の緯度経度を使用(緯度: 35.020956 経度: 135.75556) # lon_a =35.043399 # lat_a =135.733959 coll = db['sample'] data = coll.find({'tags.shop':'convenience'}) for one in data: lon_b = one['lon'] lat_b = one['lat'] rho = cal_rho(float(lon_a), float(lat_a), float(lon_b), float(lat_b)) distance = rho if(distance < min_distance): min_distance = distance min_lon = lon_b min_lat = lat_b return jsonify({ "lon":str(min_lon),"lat":(min_lat),"url":url + str(min_lat) + ',' + str(min_lon) })
def load_surveyStatuses(): session = get_db() draft = SurveyStatusModel( status="draft", description="The survey is in draft mode and not released") editing = SurveyStatusModel( status="editing", description="The survey is currently being edited") released = SurveyStatusModel( status="released", description="The survey has finished being edited and can be released") session.add(draft) session.add(editing) session.add(released) session.commit()
def test_calculate_order_reward(self): """ Test the reward calculation for each order :return: """ conn = database.get_db(self.app) c = conn.cursor() c.execute("SELECT * FROM orders") all_orders = c.fetchall() # we know there are no duplicate orders in the test data # each order is 100 NBT # total for each side/rank is 500 # therefore each order is 0.2 * reward for order in all_orders: order_reward = credit.calculate_order_reward(order, self.total_liquidity, self.rewards) calc_reward = (float(self.rewards[order[8]][order[9]][order[5]][order[2]]) * 0.2) self.assertEqual(order_reward[0], calc_reward)
def main(): db = database.get_db() cur = db.cursor() cur.execute("DELETE FROM hashtags") pol_parties = database.dbitems_from_table(cur, 'pol_parties') pol_persons = database.dbitems_from_table(cur, 'pol_persons') for pol_party in pol_parties: fragments = get_party_fragments(pol_party, cur) hashtag_counter = count_hashtags(fragments) top_hashtags = hashtag_counter.most_common(10) for rank, hashtag_tup in enumerate(top_hashtags): hashtag, count = hashtag_tup dbitem = database.DBItem( 'hashtags', { 'pol_id': pol_party.data['pol_id'], 'rank': rank, 'hashtag': hashtag, 'count': count }) dbitem.commit(cur, db) for pol_person in pol_persons: fragments = get_person_fragments(pol_person, cur) hashtag_counter = count_hashtags(fragments) top_hashtags = hashtag_counter.most_common(10) for rank, hashtag_tup in enumerate(top_hashtags): hashtag, count = hashtag_tup dbitem = database.DBItem( 'hashtags', { 'pol_id': pol_person.data['pol_id'], 'rank': rank, 'hashtag': hashtag, 'count': count }) dbitem.commit(cur, db) cur.close() db.close()
def load_classifications(app: Flask): with app.app_context(): from src.database import get_db from src.models.classification_model import ClassificationModel session = get_db() for cl in CLASSIFICATIONS: class_array = cl.split("-") if len(class_array) == 1: base_class = class_array[0].strip() classobj = session.query(ClassificationModel).filter_by( classification=base_class).first() if classobj is None: classobj = ClassificationModel(classification=base_class) session.add(classobj) elif len(class_array) == 2: base_class = class_array[0].strip() try: level = int(class_array[1]) except ValueError: level = None # type: ignore if level is None: classobj = session.query(ClassificationModel).filter_by( classification=base_class).first() if classobj is None: classobj = ClassificationModel( classification=base_class) else: classobj = session.query(ClassificationModel).filter_by( classification=base_class, level=level).one_or_none() if classobj is None: classobj = ClassificationModel( classification=base_class, level=level) session.add(classobj) else: print(class_array) session.commit()
def setUp(self): """ Load the config :return: """ self.log = logging.Logger('ALP_Test') stream = logging.StreamHandler() formatter = logging.Formatter(fmt='%(message)s') stream.setFormatter(formatter) self.log.addHandler(stream) os.environ['CONFIG_DIR'] = join('tests', 'config') import pool_server app = pool_server.app self.app = TestApp(app) self.headers = {'Content-Type': 'application/json'} # clear TEST_USER_1 conn = database.get_db(app) c = conn.cursor() c.execute("DELETE FROM users WHERE key='TEST_USER_1'") conn.commit() conn.close()
def load_question_types(app: Flask): with app.app_context(): from src.models.questionTypes_model import QuestionTypeModel from src.database import get_db session = get_db() matrix = QuestionTypeModel(type="matrix") ranking = QuestionTypeModel(type="ranking") matrix_row = QuestionTypeModel(type="matrix_row") rank_row = QuestionTypeModel(type="rank_row") multiple_choice = QuestionTypeModel(type="mcq") dropdown = QuestionTypeModel(type="dropdown") radio = QuestionTypeModel(type="radio") scale = QuestionTypeModel(type="scale") text = QuestionTypeModel(type="text") session.add(matrix_row) session.add(rank_row) session.add(multiple_choice) session.add(dropdown) session.add(radio) session.add(text) session.add(scale) session.commit()
def menu_route(): """ Handler for menu page :return: rendered template for url """ # try to get current session user username = session.get('username') # get user details user_details = query_db(get_user_details_query(username)) db = get_db() # ensure there are user details details = {} if len(user_details) > 0: user_details = user_details[0] # remove None values from the query for a, b in zip(user_details, db.cursor.description): if a is not None: details[b[0]] = a return render_template("menu.html", details=details)
def load_regions(app: Flask): with app.app_context(): from src.database import get_db from src.models.regions_model import RegionModel session = get_db() west = RegionModel(language="en", region="west") east = RegionModel(language="en", region="east") central = RegionModel(language="en", region="central") north = RegionModel(language="en", region="north") ncr = RegionModel(language="en", region="ncr") rcn = RegionModel(language="fr", region="rcn") nord = RegionModel(language="fr", region="nord") central = RegionModel(language="fr", region="central") est = RegionModel(language="fr", region="est") ouest = RegionModel(language="fr", region="ouest") session.add(west) session.add(east) session.add(north) session.add(central) session.add(ncr) session.add(rcn) session.add(nord) session.add(est) session.add(ouest) session.add(central) session.commit()
def test_crediting(self): """ Test the credit function :return: """ # for crediting we expect the credit output to look like check_time = int(time.time()) credit.credit(self.app, self.log) # get the credit details from the database conn = database.get_db(self.app) c = conn.cursor() c.execute("SELECT * FROM credits") credit_data = c.fetchall() # self.log.debug(credit_data) # check the credits are correct for cred in credit_data: # check the time is about right self.assertAlmostEqual(cred[1], check_time) # check the user isn't something random self.assertIn(cred[2], ['TEST_USER_1', 'TEST_USER_2', 'TEST_USER_3', 'TEST_USER_4', 'TEST_USER_5']) # check the exchange is good self.assertEqual(cred[3], 'test_exchange') # check unit, side and rank self.assertIn(cred[4], ['btc', 'ppc']) self.assertIn(cred[5], ['rank_1', 'rank_2']) self.assertIn(cred[6], ['ask', 'bid']) # check the amount provided self.assertEqual(cred[8], 100) # check the percentage self.assertEqual(cred[9], 20) # check the reward self.assertEqual(cred[10], float(self.rewards[cred[3]][cred[4]][cred[6]][cred[5]]) * 0.2) self.assertEqual(cred[11], 0)
def load_question(self, data): session = get_db() if data.get("id") is not None: question = session.query(QuestionModel).filter_by( id=data["id"]).one_or_none() if question is None: raise ValidationError( f"The id {data[id]} for the question you have provided does not exist", "id") else: question = QuestionModel() if data.get("question") is not None: question.question = data["question"] if data.get("type") is not None: type = session.query(QuestionTypeModel).filter_by( type=data['type']).one_or_none() if type is None: raise ValidationError("The type " + data.get("type") + "is not supported") else: question.type = type if data.get("options") is not None: question.options = data["options"] if data.get("subQuestions") is not None: if question.type.type != "matrix" and question.type.type != "ranking": raise ValidationError( "Only matrix and ranking type questions may have sub questions" ) for sub in question.subQuestions: if sub not in data["subQuestions"]: sub.status = "deactive" for sub in data['subQuestions']: if sub['object'] not in question.subQuestions: question.subQuestions.append(sub['object']) elif sub.status == "deactive": sub.status = "active" for sub in data['subQuestions']: if sub.get('order') is not None: question.set_item_order(sub['order'], sub['object']) elif (question.type.type == "matrix" or question.type.type == "ranking"): for sub in question.subQuestions: if sub.status == "active": sub.status == "deactive" question.status = "deactive" # handle the case of matrix and ranking questions # the sub questions of these types need the parent questionKey as a prefix if (question.questionKey is None and question.type.type == "matrix" and question.type.type == "ranking"): for sub in question.subQuestions: if sub.questionKey is None: sub.set_prefix(question.questionKey) deserialized_return = {"object": question} if data.get("order") is not None: deserialized_return['order'] = data['order'] return deserialized_return
def make_survey(self, data): session = get_db() if data.get("id") is not None: # eager loading of active sections and questions. # note the contains_eager in the options chain # this is to prevent additional expensive lazy loading queries survey = session.query(SurveyModel).filter( SurveyModel.id == data["id"]).one_or_none() if survey is None: raise ValidationError( f"Could not find survey with the following id: " + str(data["id"])) elif data.get("slug") is not None: survey = session.query(SurveyModel).filter( SurveyModel.slug == data["slug"]).one_or_none() if survey is None: raise ValidationError( f"Could not find survey with the following slug: " + str(data["slug"])) else: survey = SurveyModel(session=session) if data.get("title") is not None: survey.title = data['title'] if data.get("description") is not None: survey.description = data['description'] if data.get("language") is not None: survey.language = data['language'] if data.get("questions") is not None: # get all the question objects from the deserialized question collection questions = [question["object"] for question in data["questions"]] # loop over the questions and if they are not the deserialized payload then set them to deactivate for survey_question in survey.questions: if survey_question not in questions and survey_question.status == "active": survey_question.status = "deactive" if data.get("sections") is not None: sections = [section["object"] for section in data["sections"]] for survey_section in survey.sections: if survey_section not in sections and survey_section.status == "active": survey_section.status = "deactive" for section in data["sections"]: if not section["object"] in survey.sections: survey.sections.append(section["object"]) else: section["object"].status = "active" if section.get("introduction") is not None: survey.set_introduction_section(section) elif section.get("order") is not None: survey.set_item_order(section["order"], section["object"]) for question in data["questions"]: if not question["object"] in survey.questions: survey.questions.append(question["object"]) else: question["object"].status = "active" if question.get("order") is not None: survey.set_item_order(question["order"], question["object"]) elif data.get("sections") is not None: sections = [section["object"] for section in data["sections"]] for survey_section in survey.sections: if survey_section not in sections and survey_section.status == "active": survey_section.status = "deactive" for section in data["sections"]: if not section["object"] in survey.sections: survey.sections.append(section["object"]) else: section["object"].status = "active" if section.get("introduction") is not None: survey.set_introduction_section(section) elif section.get("order") is not None: survey.set_item_order(section["order"], section["object"]) return survey
app.config['start_time'] = time.time() # set up a price fetcher object pf = PriceFetcher(app, log) # Set the timer for credits log.info('running credit timer') credit_timer = Timer(60.0, credit.credit, kwargs={'app': app, 'log': log}) credit_timer.name = 'credit_timer' credit_timer.daemon = True credit_timer.start() # Set the timer for payouts log.info('running payout timer') conn = database.get_db(app) db = conn.cursor() db.execute("SELECT value FROM info WHERE key = %s", ('next_payout_time',)) next_payout_time = int(db.fetchone()[0]) if next_payout_time == 0: payout_time = 86400 db.execute('UPDATE info SET value=%s WHERE key=%s', (int(time.time() + payout_time), 'next_payout_time')) else: payout_time = int(next_payout_time - int(time.time())) conn.commit() conn.close() payout_timer = Timer(payout_time, payout.pay, kwargs={'app': app, 'log': log}) payout_timer.name = 'payout_timer' payout_timer.daemon = True
def app(): app = create_app('test') with app.app_context(): db = get_db() db.run_sql(_data_sql) yield app
from src.database import get_sqlalchemy, get_db from sqlalchemy.orm import validates from passlib.context import CryptContext import datetime import random, string import re from src.i18n import list_locales from flask_babel import _ db = get_db() sqla = get_sqlalchemy() pwd_context = CryptContext(schemes=["sha256_crypt", "md5_crypt"]) class User(sqla.Model): __name_len_max = 50 __name_len_min = 1 __email_len_max = 120 __email_len_min = 5 __password_len_max = 120 __password_len_min = 6 __lang_len_max = 3 __lang_len_min = 1 __vericode_len = 50 id = sqla.Column(sqla.Integer, primary_key=True) name = sqla.Column(sqla.String(__name_len_max), unique=True) email = sqla.Column(sqla.String(__email_len_max), unique=True) password = sqla.Column(sqla.String(__password_len_max)) lang = sqla.Column(sqla.String(__lang_len_max))
def pay(app, log): """ Pay all users who have a balance greater than the minimum payout :param log: :param rpc: :param app: :return: """ log.info('payout started') # get the credit details from the database conn = database.get_db(app) db = conn.cursor() db.execute("SELECT c.id,c.key,c.reward,u.address FROM credits AS c INNER JOIN " "users AS u on u.key=c.key WHERE c.paid=0") rewards = db.fetchall() # Calculate the total credit for each unique address user_rewards = {} for reward in rewards: if reward[3] not in user_rewards: user_rewards[reward[3]] = 0.00 user_rewards[reward[3]] += float(reward[2]) # remove those which don't meet the minimum payout threshold # and round to 6dp user_payouts = user_rewards.copy() for address in user_rewards: if user_rewards[address] < float(app.config['pool.minimum_payout']): del(user_payouts[address]) continue user_payouts[address] = round(float(user_payouts[address]), 6) if not user_payouts: log.info('no-one to payout to: %s', user_rewards) timer_time = 86400.0 else: # SendMany from nud. Report any error to log output try: # get an rpc connection rpc = get_rpc(app, log) rpc.sendmany("", user_payouts) log.info('payout successful: \'%s\'', json.dumps(user_payouts)) # mark credits to paid addresses as paid for reward in rewards: if reward[3] in user_payouts: db.execute('UPDATE credits SET paid=1 WHERE id=%s', (reward[0],)) # set the timer for the next payout timer_time = 86400.0 except JSONRPCException as e: log.error('Payout failed - %s: \'%s\'', e.message, json.dumps(user_payouts)) timer_time = 120.0 except (socket.error, CannotSendRequest, ValueError): log.error('Payout failed - no connection with nud: \'%s\'', json.dumps( user_payouts)) timer_time = 120.0 # reset timer payout_timer = Timer(timer_time, pay, kwargs={'app': app, 'log': log}) payout_timer.name = 'payout_timer' payout_timer.daemon = True payout_timer.start() # update the next payout time db.execute('UPDATE info SET value=%s WHERE key=%s', (int(time.time() + timer_time), 'next_payout_time')) conn.commit() conn.close()
def setUp(self): """ Set up the database with some orders ready for a credit :return: """ # Build the tests Logger self.log = logging.Logger('Tests') stream = logging.StreamHandler() formatter = logging.Formatter(fmt='%(message)s') stream.setFormatter(formatter) self.log.addHandler(stream) # set us up a bottle application with correct config self.app = bottle.Bottle() config.load(self.app, self.log, join('tests', 'config'), log_output=False) # build the database if it doesn't exist database.build(self.app, self.log, log_output=False) # clear any existing orders in the database conn = database.get_db(self.app) c = conn.cursor() c.execute("DELETE FROM orders") c.execute("DELETE FROM credits") conn.commit() # create test data # 5 test users each with 100 NBT on each exchange/pair/side/rank self.test_data = {} test_order_number = 1 for i in xrange(0, 5): for unit in ['btc', 'ppc']: for side in ['ask', 'bid']: for rank in ['rank_1', 'rank_2']: c.execute("INSERT INTO orders (key,rank,order_id,order_amount," "side,exchange,unit,credited) VALUES " "(%s,%s,%s,%s,%s,%s,%s,%s)", ('TEST_USER_{}'.format(i + 1), rank, test_order_number, 100, side, 'test_exchange', unit, 0)) test_order_number += 1 conn.commit() conn.close() # setup test data for test_get_total_liquidity] # get the orders from the database conn = database.get_db(self.app) c = conn.cursor() c.execute("SELECT * FROM orders") orders = c.fetchall() # get the liquidity as calculated by the main function self.total_liquidity = credit.get_total_liquidity(self.app, orders) # setup data for test_calculate_rewards # target for btc is 2500. total for btc is 2000.0 which is 0.8 of target # so already reward for btc is 0.02 instead of 0.025 # ask and bid are 50:50 so each gets 0.01. rank_1 ratio is 1.0 and rank_2 is 0 for # both. # # target for ppc is 1500. total for ppc is 2000.0 so full reward of 0.0250 # ask is 0.6 * 0.025 = 0.015 # bid is 0.4 * 0.025 = 0.010 # ask rank_1 is 1 # bid rank_1 is 0.8 * 0.010 = 0.008 # bid rank_2 is 0.2 * 0.010 = 0.002 self.rewards = {'test_exchange': {'btc': {'ask': {'rank_1': 0.01, 'rank_2': 0.0}, 'bid': {'rank_1': 0.01, 'rank_2': 0.0} }, 'ppc': {'ask': {'rank_1': 0.015, 'rank_2': 0.0}, 'bid': {'rank_1': 0.008, 'rank_2': 0.002}}}}
def load_section(self, data): session = get_db() if data.get("id") is not None: section = session.query(SectionModel).filter_by( id=data['id']).one_or_none() if section is None: raise ValidationError( f"The id {data[id]} for the section you have provided does not exist", "id") else: section = SectionModel() if data.get("title") is not None: section.title = data["title"] if data.get("body") is not None: section.body = data["body"] if data.get("type") is not None: sectionType = session.query(SectionTypeModel).filter_by( type=data["type"]).one_or_none if sectionType is None: raise ValidationError("The type " + data["type"] + "is not a valid section type") else: section.type = sectionType if data.get("questions") is not None: questions = [question['object'] for question in data['questions']] for question in section.questions and question.status == "active": if question not in questions: question.status = "deactive" if data.get("subSections") is not None: subSections = [ subSection["object"] for subSection in data['subSections'] ] for subSection in section.subSections: if not subSection in subSections and subSection.status == "active": subSection.status = "deactive" for subSection in data['subSections']: subSectionObject = subSection[ "object"] # type: SectionModel if subSectionObject.type.type == "introduction" or subSectionObject.type.type == "conclusion": raise ValidationError( "Cannot have sub sections of type introduction or conclusion", "subSections") elif subSectionObject not in section.subSections: section.subSections.append(subSectionObject) else: subSectionObject.status = "active" if subSection.get("order") is not None: section.set_item_order(subSectionObject, subSection["object"]) for question in data["questions"]: if question["object"] not in section.questions: section.questions.append(question["object"]) else: question["object"].status = "active" if question.get("order") is not None: section.set_item_order(question["order"], question["object"]) elif data.get("subSections") is not None: subSections = [ subSection["object"] for subSection in data['subSections'] ] for subSection in section.subSections: if not subSection in subSections and subSection.status == "active": subSection.status = "deactive" for subSection in data['subSections']: subSectionObject = subSection["object"] # type: SectionModel if subSectionObject.type.type == "introduction" or subSectionObject.type.type == "conclusion": raise ValidationError( "Cannot have sub sections of type introduction or conclusion", "subSections") elif subSectionObject not in section.subSections: section.subSections.append(subSectionObject) else: subSectionObject.status = "active" if subSection.get("order") is not None: section.set_item_order(subSectionObject, subSection["object"]) deserialized_return = {"object": section} if data.get("order") is not None: deserialized_return["order"] = data["order"] if section.type.type == "introduction": deserialized_return["introduction"] = True return deserialized_return