def testAlgorithm(algorithm: Algorithm, test_set_generator: TestSetGenerator, test_set_result_handler_class: Type( TestSetResultHandler) = BasicTestResultHandler, output_handlers: List[OutputHandler] = [LogHandler()]): """ Uses the framework to test the given algorithm against the test set generated by the provided generator params: algorithm: An Algorithm that takes an input and responds with an output test_set_generator: Instance of TestSetGenerator that creates the test cases test_set_result_handler_class: subclass TestSetResultHandler, specifies how the results of the test runs will be compiled output_handlers: List of Instances of Subclasses of OutputHandler, each one will be passed the full results for storage / output """ test_set = test_set_generator.generate() test_set.algorithm = algorithm test_set_result_handler = test_set_result_handler_class(test_set) for test in test_set.tests: test_output = algorithm.run(test) test_set_result_handler.addOutput(test_output) test_set_result_handler.calculateResults() for output_handler in output_handlers: output_handler.handle(test_set_result_handler.test_set_result)
def delete(self, object_id: str, dependent_id: Optional[str] = None) -> ResponseType: obj = getattr(request, Type().type) obj.deleted_at = datetime.datetime.utcnow() request.session.commit() return {"message": "success"}, 200
def patch(self, object_id: str, dependent_id: Optional[str] = None) -> ResponseType: form = Form(request.get_json() or {}, is_update=True) if not form.validate(): return {"message": "invalid data", "errors": form.errors}, 400 obj = getattr(request, Type().type) for name, value in form.valid_data.items(): setattr(obj, name, value) request.session.commit() return obj.export(), 200
def filter(self, get_type: Type(Base), expr: str) -> List[Base]: """Retrieves the elements of the given type from the database with the given filter. :param get_type: The type of the elements to be found (subclass of Base) :type get_type: Type(Base) :param expr: The SQL expression to filter the results :type expr: str :return: The list of the elements that match the given expression :rtype: List[Base] """ return self.session.query(get_type).filter(text(expr)).all()
def get(self, object_id: Optional[str] = None) -> ResponseType: """ Return all objects that match the given criteria and that the user is allowed to see. """ with settings.session() as session: if DependentTypes: dependent_type = DependentTypes[0]().type dependent_obj = getattr(request, dependent_type) filters = [Type.deleted_at == None] joins = [] if JoinBy: joins.append(JoinBy) filters.extend( [getattr(JoinBy, dependent_type) == dependent_obj]) else: filters.append( getattr(Type, dependent_type) == dependent_obj) query = session.query(Type).filter(*filters).join(*joins) else: visible_objs = ObjectRole.select_for( session, request.user, Type().type) # we add objects visible via the users organizations org_ids = admin_orgs_id_query(session, request.user) query = session.query(Type).filter( or_(Type.id.in_(visible_objs), Type.organization_id.in_(org_ids)), Type.deleted_at == None, ) # If requested, we join dependent objects for faster response times... if Joins: for j in Joins: joinedloads = None for Join in j: if joinedloads is None: joinedloads = joinedload(Join, innerjoin=True) else: joinedloads = joinedloads.joinedload( Join, innerjoin=True) query = query.options(joinedloads) objs = query.all() return {"data": [obj.export() for obj in objs]}, 200
def get(self, get_type: Type(Base), get_id: int = None) -> List[Base]: """Retrieves the elements of the given type from the database. :param get_type: The type of the elements to be found (subclass of Base) :type get_type: Type(Base) :param get_id: The ID of the element to retrieve, defaults to None :param get_id: int, optional :raises DatabaseError: If the requested element can not be found :return: The entire collection if get_id is not given, or a list composed of the element with the given ID :rtype: List[Base] """ if get_id is None: ret_obj = self.session.query(get_type).all() else: ret_obj = [self.session.query(get_type).get(get_id)] if ret_obj[0] is None: raise DatabaseError("The requested object could not be found.") return ret_obj
def get(self, object_id: str, dependent_id: Optional[str] = None) -> ResponseType: return getattr(request, Type().type).export(), 200
def post(self, object_id: Optional[str] = None, organization_id: Optional[str] = None) -> ResponseType: form = Form(request.get_json() or {}) if not form.validate(): return {"message": "invalid data", "errors": form.errors}, 400 dependent_obj: Optional[Base] = None org: Optional[Organization] = None join_by: Optional[Base] = None if DependentTypes: dependent_type = DependentTypes[0]().type dependent_obj = getattr(request, dependent_type) with settings.session() as session: obj = Type(**form.valid_data) if organization_id is not None: org = Organization.get_or_create(session, request.organization) obj.organization = org if dependent_obj: if JoinBy: # if this object has a M2M table, we create a row in # the table for the newly created object join_by = JoinBy() setattr(join_by, dependent_type, dependent_obj) setattr(join_by, obj.type, obj) else: setattr(obj, dependent_type, dependent_obj) # we expunge the object from the session, as it might have been added # when we associated the dependent properties with it... session.expunge(obj) existing_obj = obj.get_existing(session) if existing_obj: # we update the existing object instead update_form = Form(request.get_json() or {}, is_update=True) if not update_form.validate(): return { "message": "invalid data", "errors": form.errors }, 400 for k, v in update_form.valid_data.items(): setattr(existing_obj, k, v) session.commit() return existing_obj.export(), 201 if join_by: session.add(join_by) session.add(obj) if not DependentTypes: # we create an object role for the newly created object # only if it does not depends on another object assert isinstance(org, Organization) for org_role in ["admin", "superuser"]: ObjectRole.get_or_create(session, obj, org, "admin", org_role) session.commit() return obj.export(), 201
def get_export_headers(self) -> Type(List): return self.headers
def construct_query_set(self) -> Type(db.Model): return
def has_read_permission(self, qs) -> Type(db.Model): return qs
def FindWithComponent(componentType: Type( 'Component')) -> Set['Component']: return {o.gameObject for o in Object.FindObjectsOfType(componentType)}
def cost(self, node: Type('Node')) -> int: if node not in self.edges: return None
self.save() if platform == "darwin": DATA_BASE = Path(r"/Users/rob/Desktop/starsystem") else: DATA_BASE = Path(r"S:\Steam\Steamapps\common\BATTLETECH\BattleTech_Data") def expandall(p: str, Path): return expandvars(expanduser(str(p))) # Typing S = TypeVar("S", list, StarSystem) StarSystemName = Type("UserRequest", str) def parse_systems_request(request: str): request = StarSystemName(request.strip().casefold()) if request == "*": return get_all_systems() else: return [get_system(request)] PlanetName = Type("PlanetName", str) StarSystemResult = Tuple[S, Callable[[S], List[StarSystem]]] DATA_DIR = Path(DATA_BASE) / "StreamingAssets/data/starsystem" ORIG_DATA_PKL = DATA_DIR / "orig.pkl"