Esempio n. 1
0
def lose_it_upload(request):
    session = Session()
    
    # TODO: duplicate detection
    input_file = request.POST['file'].file
    reader = LoseItDataReader(input_file)
    existing_for_date = {}
    ignored = []
    for entry in reader:
        existing_food = session.query(Food).filter_by(name=entry.name).all()
        food = None
        if len(existing_food) == 0:
            food = Food(entry.name)
            session.add(food)
        else:
            food = existing_food[0]
        if not entry.date in existing_for_date:
            existing_entries = session.query(FoodEntry).filter_by(date=entry.date).all()
            existing_for_date[entry.date] = existing_entries
        if existing_for_date[entry.date]:
            ignored.append(entry)   
        else:
            new_entry = FoodEntry(food, entry.calories, entry.date)
            session.add(new_entry)
    
    session.commit()
    if ignored:
        return {'title': 'Existing Data Not Imported', 'ignored': ignored}
    else:
        return HTTPFound('/food_entry/lose_it_upload_form')
Esempio n. 2
0
def food_edit_food_tags(request):
    food_id = request.matchdict['id']
    tag_ids = map(int, request.params.getall('tag_id'))
    session = Session()
    food = session.query(Food).filter_by(id=food_id).first()
    already_tagged = []
    to_remove = []
    for food_tag in food.food_tags:
        if not food_tag.id in tag_ids:
            to_remove.append(food_tag)
        else:
            already_tagged.append(food_tag.id)
    for tag in to_remove:
        food.food_tags.remove(tag)
    to_add = session.query(FoodTag).filter(FoodTag.id.in_(set(tag_ids)-set(already_tagged))).all()
    food.food_tags.extend(to_add)
    session.commit()
    session = Session()
    food = session.query(Food).filter_by(id=food_id).first()
    session.close()
    
    if request.is_xhr:
        return {'request': request, 'food': food}
    else:
        return HTTPFound('/food/list')
Esempio n. 3
0
 def add_job(self, valid_datetime, job_type, details):
     session = Session()
     new_job = Job(timestamp_when_valid=valid_datetime,
                   job_type=job_type,
                   details=details)
     session.add(new_job)
     session.commit()
     session.close()
Esempio n. 4
0
def food_tag_add(request):
    name = request.params['name']
    
    session = Session()
    new_tag = FoodTag(name)
    session.add(new_tag)
    session.commit()
    
    return HTTPFound('/food_tag/list')
Esempio n. 5
0
def init_result():
    session = Session()
    entry = Session(Result).filter(Result.id == 1).one_or_none()
    # 如数据不存在,就新增一条为1的空数据
    if not entry:
        session.add(Result(modules="", total_time="", data="", dev=""))
        session.commit()
    else:
        pass
    session.close()
Esempio n. 6
0
 def commit_entry(self, staged_entry):
     session = Session()
     indicator_values = {}
     for indicator in self.indicators_metadata:
         value_location = self.location_engine.convert_postcode(
             staged_entry.postcode,
             self.indicators_metadata[indicator]['resolution'])
         previous_value_entry = session.query(IndicatorValue)\
             .filter(IndicatorValue.indicator == indicator,
                     IndicatorValue.location == value_location,
                     IndicatorValue.date < staged_entry.date)\
             .order_by(IndicatorValue.date.desc())\
             .first()
         following_value_entry = session.query(IndicatorValue)\
             .filter(IndicatorValue.indicator == indicator,
                     IndicatorValue.location == value_location,
                     IndicatorValue.date > staged_entry.date)\
             .order_by(IndicatorValue.date)\
             .first()
         if previous_value_entry is None and following_value_entry is None:
             value = None
             indicator_date = None
         elif following_value_entry is None \
                 or abs((previous_value_entry.date - staged_entry.date).days) \
                 < abs((following_value_entry.date - staged_entry.date).days):
             value = previous_value_entry.value
             indicator_date = previous_value_entry.date
         else:
             value = following_value_entry.value
             indicator_date = following_value_entry.date
         # Check if value falls within frequency range
         if indicator_date is not None and (abs(staged_entry.date - indicator_date)).days\
                 > (FREQUENCY_DAY_COUNTS[self.indicators_metadata[indicator]['frequency']] / 2):
             value = None
         indicator_values[indicator] = value
     new_entry = TargetEntry(
         entry_id=staged_entry.entry_id,
         sale_id=staged_entry.sale_id,
         date=staged_entry.date,
         value=staged_entry.value,
         PDD_type=staged_entry.PDD_type,
         postcode=staged_entry.postcode,
         town_or_city=staged_entry.town_or_city,
         district=staged_entry.district,
         county=staged_entry.county,
         new_property_flag=staged_entry.new_property_flag,
         property_type=staged_entry.property_type,
         tenure_type=staged_entry.tenure_type)
     for indicator in indicator_values:
         setattr(new_entry, indicator, indicator_values[indicator])
     session.add(new_entry)
     session.commit()
     session.close()
Esempio n. 7
0
def set_result(data):
    session = Session()
    entry = Session(Result).filter(Result.id == data["id"]).one_or_none()
    if entry:
        entry.modules = data["modules"]
        entry.total_time = data["total_time"]
        entry.data = data["data"]
        entry.dev = data["dev"]
        session.commit()
    else:
        print("实体类不存在")
    session.close()
Esempio n. 8
0
 def add_new_model(self, settings):
     new_model_name = settings['name']
     new_model = Model(settings)
     self.models[new_model_name] = new_model
     self.save_model(new_model_name)
     new_model_record = ModelEntry(name=settings['name'],
                                   type=settings['type'],
                                   dataset=settings['dataset'],
                                   state='untrained')
     session = Session()
     session.add(new_model_record)
     session.commit()
     session.close()
def main():
    engine = create_engine('postgresql://[email protected]/my_metrics')
    Base.metadata.create_all(engine)
    Session.configure(bind=engine)
    
    session = Session()
    food_entries = session.query(FoodEntry).order_by(FoodEntry.date)
    for entry in food_entries:
        food_name = entry.name
        existing_food = session.query(Food).filter_by(name=food_name).all()
        if len(existing_food) == 0:
            new_food = Food(food_name)
            session.add(new_food)
    session.commit()
Esempio n. 10
0
 def pull_land_registry(self):
     session = Session()
     data = request.urlopen(LAND_REGISTRY_URL).read()
     data = str(data, 'utf-8')
     data_frame = pandas.read_csv(StringIO(data),
                                  header=None,
                                  names=LAND_REGISTRY_DATA_HEADERS)
     # Only include entries with PPD type A
     # data_frame = [data_frame.PDD_type == 'A']
     # Convert date strings to date objects
     data_frame['date'] = pandas.to_datetime(
         data_frame['date'], format=LAND_REGISTRY_TIMESTAMP_FORMAT)
     # Reorder columns
     data_frame = data_frame[STAGED_ENTRY_HEADERS]
     # Convert old or new character value to boolean
     data_frame['new_property_flag'] = data_frame['new_property_flag'].map(
         dict(Y=True, N=False))
     current_highest_id = -1
     latest_entry = session.query(StagedEntry).order_by(
         desc(StagedEntry.entry_id)).first()
     if latest_entry is not None:
         current_highest_id = latest_entry.id
     batch_start_id = current_highest_id + 1
     batch_end_id = batch_start_id + len(data_frame)
     # Add id column
     data_frame.insert(0, 'entry_id', range(batch_start_id, batch_end_id))
     data_frame.to_sql('staged_entries',
                       con=self.database_engine,
                       if_exists='append',
                       index=False)
     # Process deletion entries
     deletion_entries = session.query(StagedEntry).filter(
         StagedEntry.record_type == 'D')
     for deletion_entry in deletion_entries:
         session.query(TargetEntry).filter(
             TargetEntry.sale_id == deletion_entry.sale_id).delete()
         deletion_entry.delete()
     # Get update entries
     update_entries = session.query(StagedEntry).filter(
         StagedEntry.record_type == 'C')
     for update_entry in update_entries:
         existing_entry = session.query(TargetEntry).filter(
             TargetEntry.sale_id == update_entry.sale_id).one()
         if existing_entry is not None:
             update_entry_from_land_registry(update_entry, existing_entry)
         update_entry.delete()
         session.commit()
     session.close()
     return str(batch_end_id)
Esempio n. 11
0
 def delete_model(self, model_name):
     session = Session()
     model_record = session.query(ModelEntry).filter(
         ModelEntry.name == model_name)
     model_record.delete()
     session.commit()
     del self.models[model_name]
     os.remove(model_name)
     # Update max inputs value
     self.max_inputs = 0
     for model_name in self.get_trained_model_names():
         model_input_count = len(self.get_model_inputs(model_name))
         if model_input_count > self.max_inputs:
             self.max_inputs = model_input_count
     session.close()
def main():
    engine = create_engine('postgresql://[email protected]/my_metrics')
    Base.metadata.create_all(engine)
    Session.configure(bind=engine)
    
    session = Session()
    foods = session.query(Food)
    foods_by_name = {}
    for food in foods:
        foods_by_name[food.name] = food
    
    food_entries = session.query(FoodEntry).order_by(FoodEntry.date)
    for entry in food_entries:
        food = foods_by_name[entry.name]
        entry.food = food
    session.commit()
Esempio n. 13
0
def food_entry_add(request):
    name = request.params['name']
    calories = int(request.params['calories'])
    day = date.today() - timedelta(int(request.params['days_ago']))
    
    session = Session()
    existing_food = session.query(Food).filter_by(name=food_name).all()
    food = None
    if len(existing_food) == 0:
        food = Food(food_name)
        session.add(food)
    else:
        food = existing_food[0]
    new_entry = FoodEntry(food, calories, day)
    session.add(new_entry)
    session.commit()
    
    return HTTPFound('/food_entry/add_form')
Esempio n. 14
0
 def update_entries_from_source(self, source_entry_date, frequency,
                                indicators, area_resolution):
     session = Session()
     frequency_radius_delta = relativedelta(
         FREQUENCY_DAY_COUNTS[frequency] / 2)
     lower_threshold_date = source_entry_date - frequency_radius_delta
     upper_threshold_date = source_entry_date + frequency_radius_delta
     # noinspection PyComparisonWithNone
     entries_to_update = session.query(TargetEntry).filter(
         getattr(TargetEntry, indicators[0]) == None,
         TargetEntry.date >= lower_threshold_date,
         TargetEntry.date <= upper_threshold_date)
     for entry in entries_to_update:
         location = self.location_engine.convert_postcode(
             entry.postcode, area_resolution)
         for indicator in indicators:
             value = session.query(IndicatorValue).filter(
                 IndicatorValue == source_entry_date,
                 IndicatorValue.location == location)
             setattr(entry, indicator, value)
     session.commit()
     session.close()
Esempio n. 15
0
 def train_model(self, model_name):
     session = Session()
     model_record = session.query(ModelEntry).filter(ModelEntry.name == model_name).one()
     model_record.state = 'training'
     session.commit()
     parent_models = self.model_manager.models[model_name].input_models
     for parent_model in parent_models:
         # Train any untrained parent models
         if session.query(ModelEntry).filter(ModelEntry.name == parent_model, ModelEntry.state == 'untrained')\
                 is not None:
             self.train_model(parent_model)
         # Wait for any parent models that are still in training, in cases where another process started the training
         while session.query(ModelEntry).filter(ModelEntry.name == parent_model,
                                                     ModelEntry.state == 'training').one_or_none()\
                 is not None:
             time.sleep(30)
     model = self.model_manager.models[model_name]
     dataset_name = model.dataset
     entry_count = model.training_entry_count
     if dataset_name == 'core_dataset':
         dataframe = pandas.read_sql(session.query(Base.metadata.tables['core_dataset']).order_by(func.rand()).limit(entry_count).statement,
                                     self.database_engine)
     else:
         dataframe = pandas.read_csv(DEFAULT_DATA_PATH, sep='\s+', names=DEFAULT_DATA_HEADERS)
         if entry_count > len(dataframe):
             entry_count = len(dataframe)
         dataframe = dataframe.sample(entry_count)
     for parent_model in parent_models:
         self.recursive_process(parent_model, dataframe)
     model.train(dataframe)
     self.model_manager.save_model(model_name)
     model_record.state = 'trained'
     session.commit()
     # Update the model manager max input count - done here to happen at the end of training
     self.model_manager.update_max_inputs(len(self.model_manager.get_model_inputs(model_name)))
     session.close()
Esempio n. 16
0
qset291= session.query(func.count(Emp.ename)).filter(Emp.job=='董事长').filter(Emp.sal>15000).first()
qset292= session.query(func.count(Emp.ename)).filter(Emp.job=='经理').filter(Emp.sal>15000).first()
qset293= session.query(func.count(Emp.ename)).filter(Emp.job=='分析师').filter(Emp.sal>15000).first()
qset294= session.query(func.count(Emp.ename)).filter(Emp.job=='销售员').filter(Emp.sal>15000).first()
qset295= session.query(func.count(Emp.ename)).filter(Emp.job=='文员').filter(Emp.sal>15000).first()
print('\033[35;1m工资大于15000的董事长有%s位\033[0m' % qset291)
print('\033[35;1m工资大于15000的经理有%s位\033[0m' % qset292)
print('\033[35;1m工资大于15000的分析师有%s位\033[0m' % qset293)
print('\033[35;1m工资大于15000的销售员有%s位\033[0m' % qset294)
print('\033[35;1m工资大于15000的文员有%s位\033[0m' % qset295)
print(38*"*")

# #第30题
# print(38*"*")
# print('\033[31;1m第二十九题:统计工资大于 15000 的每个工作岗位人数,并且该工作岗位的人数大于等于 3\033[0m')
# qset301= session.query(Emp).filter(func.sum(Emp.sal,Emp.COMM)>15000)
# # qset302= session.query(func.count(Emp.ename)).filter(Emp.job=='经理').filter(Emp.sal>15000).first()
# # qset303= session.query(func.count(Emp.ename)).filter(Emp.job=='分析师').filter(Emp.sal>15000).first()
# # qset304= session.query(func.count(Emp.ename)).filter(Emp.job=='销售员').filter(Emp.sal>15000).first()
# # qset305= session.query(func.count(Emp.ename)).filter(Emp.job=='文员').filter(Emp.sal>15000).first()
# print(qset301)
# for data in qset301:
#     print('%s' % (data.ename))
# #     print(data)


# 确认
session.commit()

# 关闭
session.close()