def __init__(self, status_or_exception: T.Union[int, HTTPException], description: T.Optional[str] = None, headers: T.Optional[T.Dict[str, str]] = None) -> None: # If an exception is passed, unpack it and rerun if isinstance(status_or_exception, HTTPException): e = status_or_exception self.__init__(e.status_code, description or e.text, headers) return assert description is not None self._description = description self.status_code = status_or_exception try: reason = _status_map[self.status_code] except KeyError: log(f"HTTP {self.status_code} status is undefined", Level.Debug) reason = "Undefined Error" headers_with_content_type = { "Content-Type": f"{MIMEType.JSON.value}; charset={ENCODING}", **(headers or {}) } body = json.encode({ "status": self.status_code, "reason": reason, "description": self.description }) super().__init__(headers=headers_with_content_type, reason=reason, body=body)
def _JSONResponse(body: T.Any, *, serialise: bool = True, status: int = 200) -> Response: """ Standardised Response factory for JSON payloads """ return Response(status=status, content_type=MIMEType.JSON.value, charset=ENCODING, body=json.encode(body) if serialise else body)
async def json(self) -> bytes: """ Return the JSON serialisation of the object's serialisable form """ serialisable = await self.__serialisable__() return json.encode(serialisable)
parser.add_option('--time-limit-gwd',dest='time_limit_gwd', type='int', default='20', help='the time limit for the initial winner determination problem') parser.add_option('--time-limit',dest='time_limit', type='int', default='20', help='the time limit for all problems but the initial winner determination problem') parser.add_option('--epgap',dest='epgap', type='float', default=None, help='the epgap used for all problems') parser.add_option('--offset',dest='offset', type='int', default=0, help='the scenario offset used') for option in parser.option_list: if option.default != ("NO", "DEFAULT"): option.help += (" " if option.help else "") + "[default: %default]" if sys.stdin.isatty(): print parser.format_help() sys.exit() # parse scenario and options scenarios = json.decode(sys.stdin.read()) options = parser.parse_args()[0] scenario = scenarios[options.offset] convertToNamedTuples(scenario) slots, bidder_infos = scenario # create processor object and set values proc = TvAuctionProcessor() if options.price_vector=='vcg': proc.vcgClass = Vcg elif options.price_vector=='zero': proc.vcgClass = Zero if options.core_algorithm=='trim': proc.core_algorithm = CorePricing.TRIM_VALUES elif options.core_algorithm=='switch': proc.core_algorithm = CorePricing.SWITCH_COALITIONS elif options.core_algorithm=='reuse': proc.core_algorithm = CorePricing.REUSE_COALITIONS # solve and print res = proc.solve(slots, bidder_infos, options.time_limit or None, options.time_limit_gwd or None, options.epgap or None) print json.encode(res)
def serialize(data): return json.encode(data)