def load_values(db_session: session, module): def add_units(data, unit_type: IngredientUnit.UnitType, is_cldr: bool): for entry in data: unit = IngredientUnit(type_=unit_type, name=entry[0], factor=entry[1], description=entry[2], cldr=is_cldr) db_session.add(unit) def add_custom_units(data, unit_type: IngredientUnit.UnitType): for entry in data: unit = IngredientUnit(type_=unit_type, name=entry[0], factor=None, description=entry[1], cldr=False) db_session.add(unit) add_units(cldr.DATA_MASS, IngredientUnit.UnitType.MASS, is_cldr=True) add_units(cldr.DATA_VOLUME, IngredientUnit.UnitType.VOLUME, is_cldr=True) data_quantity = getattr(module, "DATA_QUANTITY") add_units(data_quantity, IngredientUnit.UnitType.QUANTITY, is_cldr=False) data_unspecific = getattr(module, "DATA_UNSPECIFIC") add_custom_units(data_unspecific, IngredientUnit.UnitType.UNSPECIFIC) db_session.commit()
def _initProcessingStateTable(self, dbsession: session, submitInteral: int = 100): if dbsession.query(ProcessingState).first(): logger.warning("error") return for root, dirs, files in os.walk(self.allow_paths): interval: int = 0 templist = [] for file in files: if file.endswith("txt") or file.endswith("sol"): sol_version_set: set = self._getSolidityVersions(file) templist.append( ProcessingState( contractAddr=os.path.splitext(file)[0], fullyextracted=0 if len(sol_version_set) else -1, solc_versions=";".join(sol_version_set))) interval = interval + 1 if interval % submitInteral == 0: # dbsession.add_all(templist) templist = [] # dbsession.flush() dbsession.commit() # print("sumbit: %d times" % ((interval - 1) // submitInteral + 1)) if templist: dbsession.add_all(templist) dbsession.flush() dbsession.commit() print("sumbit: %d times" % ((interval - 1) // submitInteral + 1))
def create(request: schemas.User, db: session): new_user = models.User(name=request.name, email=request.email, password=Hash.bcrypt(request.password)) db.add(new_user) db.commit() db.refresh(new_user) return new_user
def update(id: int, request: schemas.Blog, db: session): blog = db.query(models.Blog).filter(models.Blog.id == id) if not blog.first(): raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Blog with id {id} not found") blog.update(request) db.commit() return "done"
def destroy(id: int, db: session): blog = db.query(models.Blog).filter(models.Blog.id == id) if not blog.first(): raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Blog with id {id} not found") blog.delete(synchronize_session=False) db.commit() return 'done'
def doctors_delete_by_id(id: int, db: session): db_doctor = db.query(models.Doctor).filter(models.Doctor.id == id).first() if not db_doctor: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"doctor with the id '{id}' not found") db.delete(db_doctor) db.commit()
def create_task(db: session, task: schemas.TaskCreate): db_task = models.Task(**task.dict(), added=datetime.now()) db.add(db_task) db.commit() db.refresh(db_task) db_task.notes_path = f"notes/{db_task.id}_{db_task.name.replace(' ', '_')}.md" db.add(db_task) db.commit() db.refresh(db_task) return db_task
def pillboxes_create(how_many: int, db: session) -> list[dict]: for _ in range(how_many): pillbox_model = models.Pillbox() db.add(pillbox_model) db.commit() db.refresh(pillbox_model) yield pillbox_model
def pillboxes_delete_by_id(id: int, db: session) -> None: db_pillbox = db.query( models.Pillbox).filter(models.Pillbox.id == id).first() if not db_pillbox: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"pillbox with the id '{id}' not found") db.delete(db_pillbox) db.commit()
def patients_delete_by_id(id: int, db: session) -> None: db_patient = db.query( models.Patient).filter(models.Patient.id == id).first() if not db_patient: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"patient with the id '{id}' not found") db.delete(db_patient) db.commit()
async def create_stock(stock_request: StockRequest, background_tasks: BackgroundTasks , db: session = Depends(get_db)): stock = Stock() stock.symbol = stock_request.symbol db.add(stock) db.commit() background_tasks.add_task(fetch_stock_data, stock.id) return { "code": "success", "message": "stock was added to the database" }
def doctors_updated_by_id(id: int, doctor: schemas.DoctorBase, db: session) -> dict: db_doctor = db.query(models.Doctor).filter(models.Doctor.id == id).first() if not db_doctor: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"doctor with the id '{id}' not found") db_doctor.first_name = doctor.first_name db_doctor.last_name = doctor.last_name db_doctor.email = doctor.email db_doctor.phone_number = doctor.phone_number db.commit() db.refresh(db_doctor) return db_doctor
def doctors_create(doctor: schemas.DoctorCreateIn, db: session) -> dict: password = pg.generate() doctor_model = models.Doctor( first_name=doctor.first_name, last_name=doctor.last_name, email=doctor.email, phone_number=doctor.phone_number, hashed_password=hashing.get_password_hash(password), ) db.add(doctor_model) db.commit() db.refresh(doctor_model) doctor_model.__dict__['password'] = password return doctor_model
def patients_create(patient: schemas.PatientCreateIn, db: session) -> dict: password = pg.generate() patient_model = models.Patient( first_name=patient.first_name, last_name=patient.last_name, email=patient.email, phone_number=patient.phone_number, hashed_password=hashing.get_password_hash(password), doctor_id=patient.doctor_id) db.add(patient_model) db.commit() db.refresh(patient_model) patient_model.__dict__['password'] = password return patient_model
def patients_updated_by_id(id: int, patient: schemas.PatientBase, db: session) -> dict: db_patient = db.query( models.Patient).filter(models.Patient.id == id).first() if not db_patient: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"patient with the id '{id}' not found") db_patient.first_name = patient.first_name db_patient.last_name = patient.last_name db_patient.email = patient.email db_patient.phone_number = patient.phone_number db_patient.doctor_id = patient.doctor_id db.commit() db.refresh(db_patient) return db_patient
def initialize_db(my_session: session, load_data: bool = True): """ Args: load_data (): Load the default date (false for tests) Returns: """ db.Base.metadata.drop_all(db.engine, checkfirst=True) db.Base.metadata.create_all(db.engine, checkfirst=True) if load_data: load_all(my_session) # Setup the default ingredient_unit unit_group = data.IngredientUnit(name="Internal group unit", cldr=False, factor=None, type_=data.IngredientUnit.UnitType.GROUP) my_session.add(unit_group) my_session.commit() data.IngredientUnit.update_unit_dict(my_session)
def pillboxes_updated_by_id(id: int, pillbox: schemas.PillboxBase, db: session) -> dict: db_pillbox = db.query( models.Pillbox).filter(models.Pillbox.id == id).first() if not db_pillbox: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"pillbox with the id '{id}' not found") db_owner = db.query( models.Patient).filter(models.Patient.id == pillbox.owner_id).first() if not db_owner: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"patient with the id '{id}' not found") db_pillbox.owner_id = pillbox.owner_id db.commit() db.refresh(db_pillbox) return db_pillbox
def _extractCountMetric(self, dbsession: session, contractAddress: str, rootNode: solcast.nodes.NodeBase) -> bool: try: cmResult: CountMetric = CountMetricCalculator( contract_addr=contractAddress, contract_path=os.path.join( self.contract_root_path, contractAddress + "." + self.contract_src_path_skeleton.split(".")[1]), rootNode=rootNode).getCountMetrics() except Exception as e: logger.error( "|_____Extracting CountMetric for Contract:{contract} ERROR: {emsg}" .format(contract=contractAddress, emsg=e)) return False else: # dbsession.add(cmResult) dbsession.commit() dbsession.flush() return True
def update_task(db: session, task_id: int, task: schemas.TaskCreate): updated = db.query(models.Task).filter(models.Task.id == task_id).update( task.dict()) if (updated == 0): db_task = models.Task(**task.dict(), added=datetime.now()) db.add(db_task) db.commit() db.refresh(db_task) db_task.notes_path = f"notes/{db_task.id}_{db_task.name.replace(' ', '_')}.md" db.add(db_task) db.commit() return db_task db.commit() return get_task(db, task_id)
def create(request: schemas.Blog, db: session): new_blog = models.Blog(title=request.title, body=request.body, user_id=1) db.add(new_blog) db.commit() db.refresh(new_blog) return new_blog
def extractBatchContractFeature(self, dbsession: session, min_id: int = 0, max_id: int = 60000, batchSize: int = 500): processinglist:List[ProcessingState] = dbsession.query(ProcessingState)\ .filter(ProcessingState.id.between(min_id,max_id))\ .filter(ProcessingState.fullyextracted == 0)\ .limit(batchSize) if not processinglist: logger.warning( "All Smart Contracts Have Been Successfully Feature-Extracted") # SCid: int = 1 for processing in processinglist: # time.sleep(0.2) print("当前抽取特征的批次完成进度: %d / %d" % (SCid, batchSize)) contractAddr = processing.contractAddr logger.info("Extracting Features for Contract:{contract}".format( contract=contractAddr)) # cpmState = processing.complexityMetricExtracted ctmState = processing.countMetricExtracted oomState = processing.objectOrientedMetricExtracted lrmState = processing.languageRelatedMetricExtracted allState = all([cpmState, ctmState, oomState, lrmState]) rootNode: solcast.nodes.NodeBase = self._getRootNode( contractAddr=contractAddr, versionString=processing.solc_versions) print(processing) try: if not processing.complexityMetricExtracted: cpmState: bool = self._extractComplexityMetric( dbsession, contractAddr, rootNode=rootNode) if cpmState: logger.info( "|___Extracting ComplexityMetric for Contract:{contract} Successfully." .format(contract=contractAddr)) else: logger.error( "|___Extracting ComplexityMetric for Contract:{contract} Failed!." .format(contract=contractAddr)) assert cpmState == True if not processing.countMetricExtracted: ctmState: bool = self._extractCountMetric( dbsession, contractAddress=contractAddr, rootNode=rootNode) if ctmState: logger.info( "|___Extracting CountMetric Features for Contract:{contract} Successfully." .format(contract=contractAddr)) else: logger.error( "|___Extracting CountMetric Features for Contract:{contract} Failed!." .format(contract=contractAddr)) assert ctmState == True if not processing.objectOrientedMetricExtracted: oomState: bool = self._extractObjectOrientedMetric( dbsession, contractAddress=contractAddr, rootNode=rootNode) if oomState: logger.info( "|___Extracting ObjectOrientedMetric Features for Contract:{contract} Successfully." .format(contract=contractAddr)) else: logger.error( "|___Extracting ObjectOrientedMetric Features for Contract:{contract} Failed!." .format(contract=contractAddr)) assert oomState == True if not processing.languageRelatedMetricExtracted: lrmState: bool = self._extractLanguageRelatedMetric( dbsession, contractAddress=contractAddr, rootNode=rootNode) if lrmState: logger.info( "|___Extracting LanguageRelatedMetric Features for Contract:{contract} Successfully." .format(contract=contractAddr)) else: logger.error( "|___Extracting LanguageRelatedMetric Features for Contract:{contract} Failed!." .format(contract=contractAddr)) assert lrmState == True except Exception as e: logger.error( "|___Extracting Features for Contract:{contract} ERROR: {emsg}" .format(contract=contractAddr, emsg=e)) break else: allState = all([cpmState, ctmState, oomState, lrmState]) if allState: logger.success( "Extracting Features for Contract:{contract} Full SUCCESSFULLY!" .format(contract=contractAddr)) else: logger.warning( "Extracting Features for Contract:{contract} PARTIAL SUCCESSFULLY!" .format(contract=contractAddr)) finally: processing.fullyextracted = allState processing.complexityMetricExtracted = cpmState processing.countMetricExtracted = ctmState processing.objectOrientedMetricExtracted = oomState processing.languageRelatedMetricExtracted = lrmState dbsession.commit() dbsession.flush() logger.success( "Update Information on ProcessingState Table for Contract:{contract} successfully!" .format(contract=contractAddr)) SCid = SCid + 1
def delete_task(db: session, task_id: int): deleted = db.query(models.Task).filter(models.Task.id == task_id).delete() db.commit() return deleted
def load_values(db_session: session, module=None): """ Plain and boring. Just a version number """ meta = Meta(1) db_session.add(meta) db_session.commit()