def calculate(self, logid, work): print("calculate({}, {})".format(logid, work)) if work.op == Operation.ADD: val = work.num1 + work.num2 elif work.op == Operation.SUBTRACT: val = work.num1 - work.num2 elif work.op == Operation.MULTIPLY: val = work.num1 * work.num2 elif work.op == Operation.DIVIDE: if work.num2 == 0: x = InvalidOperation() x.whatOp = work.op x.why = "Cannot divide by 0" raise x val = work.num1 / work.num2 else: x = InvalidOperation() x.whatOp = work.op x.why = "Invalid operation" raise x log = SharedStruct() log.key = logid log.value = '%d' % (val) self.log[logid] = log return val
def calculate(self, logid, work): logging.info('calculate({0}, {1})'.format(logid, work)) if work.op == Operation.ADD: val = work.num1 + work.num2 elif work.op == Operation.SUBTRACT: val = work.num1 - work.num2 elif work.op == Operation.MULTIPLY: val = work.num1 * work.num2 elif work.op == Operation.DIVIDE: if work.num2 == 0: x = InvalidOperation() x.whatOp = work.op x.why = 'Cannot divide by 0' raise x val = work.num1 / work.num2 else: x = InvalidOperation() x.whatOp = work.op x.why = 'Invalid operation' raise x log = SharedStruct(key=int(logid), value=str(val)) self.log[logid] = log return val
def resolve_ib_contract(self, ibcontract, reqId=DEFAULT_GET_CONTRACT_ID): print("Getting full contract details from the server... ") contract_details_queue = FinishableQueue(self.init_contract_details(reqId)) self.reqContractDetails(reqId, ibcontract) # Run until we get a valid contract(s) or get bored waiting new_contract_details = contract_details_queue.get(timeout=self.MAX_WAIT_SECONDS) while self.wrapper.is_error(): print("resolve_ib_contract: " + self.get_error()) if contract_details_queue.timed_out(): print("resolve_ib_contract: Exceeded maximum wait for wrapper to confirm finished - seems to be normal behaviour") if len(new_contract_details) == 0: print("resolve_ib_contract: No new contract details found: returning unresolved contract") return ibcontract if len(new_contract_details) > 1: print("resolve_ib_contract: got multiple contracts using first one") new_contract_details = new_contract_details[0] if hasattr(new_contract_details, "summary"): resolved_ibcontract = new_contract_details.summary elif hasattr(new_contract_details, "contract"): resolved_ibcontract = new_contract_details.contract else: raise InvalidOperation(why="resolve_ib_contract unable to resolve response properly") print("resolve_ib_contract: returning resolved contract") return resolved_ibcontract
def calculate(self, logid, work): print('calculate(%d, %r)' % (logid, work)) if work.op == Operation.ADD: val = work.num1 + work.num2 elif work.op == Operation.SUBTRACT: val = work.num1 - work.num2 elif work.op == Operation.MULTIPLY: val = work.num1 * work.num2 elif work.op == Operation.DIVIDE: if work.num2 == 0: raise InvalidOperation(work.op, 'Cannot divide by 0') val = work.num1 / work.num2 else: raise InvalidOperation(work.op, 'Invalid operation') log = SharedStruct() log.key = logid log.value = '%d' % (val) self.log[logid] = log return val
def __init__(self, ip_address, port_id, client_id): IBJTSWrapper.__init__(self) IBJTSClient.__init__(self, wrapper=self) print("Attempting Connection to IB Gateway..." + str(ip_address) + ":" + str(port_id) + "#" + str(client_id)) self.connect(ip_address, port_id, client_id) thread = Thread(target=self.run) thread.start() setattr(self, "_thread", thread) while self.wrapper.is_error(): (error_id, error_code, error_message) = self.get_error(as_string=False) raise InvalidOperation(whatOp=error_code, why=error_message)
def calculate(self, logid, work): """By default, this method runs in the server's executor threads.""" print('calculate(%d, %r)' % (logid, work)) # Calculate the 10000000th prime number first, it takes 1 second. end = time.time() + 1 while True: now = time.time() if now >= end: break if work.op == Operation.ADD: val = work.num1 + work.num2 elif work.op == Operation.SUBTRACT: val = work.num1 - work.num2 elif work.op == Operation.MULTIPLY: val = work.num1 * work.num2 elif work.op == Operation.DIVIDE: if work.num2 == 0: x = InvalidOperation() x.what = work.op x.why = 'Cannot divide by 0' raise x val = work.num1 // work.num2 else: x = InvalidOperation() x.what = work.op x.why = 'Invalid operation' raise x log = SharedStruct() log.key = logid log.value = '%d' % (val) self.log[logid] = log return val
def request_matching_symbols(self, query_symbol): if self.ibgateway_app_client is None: self.connect() try: result = self.ibgateway_app_client.get_IB_matching_symbols( query_symbol) except Exception as err: raise InvalidOperation(why=str(err)) final_result = [] for queue_results in result: for original_result in queue_results: print( str(original_result.contract.symbol + "," + original_result.contract.primaryExchange)) final_result.append( str(original_result.contract.symbol + "," + original_result.contract.primaryExchange)) return final_result