def targets(numbers): """Returns all possible positive integer targets of the given numbers. >>> targets([1, 2, 3, 4]) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 32, 36} >>> len(targets([1, 2, 3, 4])) 31 """ result = set() for digits in permutations(numbers): for operations in OPERATIONS: target = calculate(digits, operations) logger.debug(f"{target} ({operations})") if target >= 1 and float(target).is_integer(): result.add(int(target)) target = calculate_parenthesis(digits, operations) if target >= 1 and float(target).is_integer(): result.add(int(target)) return result
def _save_chunk_data(self, uploaded_file_name: str, chunk_num: int, chunk_data: InMemoryUploadedFile, chunk_dir: Path) -> None: """ Save this request chunk data in a temporary file with a lock so other resumable requests can't edit/delete it until finishes uploading Args: uploaded_file_name: File name uploaded chunk_num: Resumable Chunk Number chunk_data: Request chunk file data chunk_dir: Dir to store chunk files """ # Make the dirs if not created yet if not chunk_dir.is_dir(): chunk_dir.mkdir(parents=True) # Save chunk file with lock file until finished saving chunk_name = self._chunk_file_name(uploaded_file_name, chunk_num) lock_file_path = self._chunk_lock_file_path(chunk_dir, chunk_num) with open(lock_file_path, 'a'): os.utime(lock_file_path, None) fs = FileSystemStorage(location=chunk_dir) fs.save(chunk_name, chunk_data) os.unlink(lock_file_path) logger.debug( f'[{self.LOG_PREFIX}] Created chunk file {chunk_dir / chunk_name!r}' )
def _input_process(self): while self._running: for key, event in self._selector.select(timeout=BLOCKING_TIME): if self.Operation.ACCEPT == key.data: connection, (ip, port) = key.fileobj.accept() connection.setblocking(False) logger.debug("New connection to {}:{}".format(ip, port)) self._selector.register(connection, selectors.EVENT_READ, self.Operation.READ) elif self.Operation.READ == key.data: connection = key.fileobj try: data = connection.recv(MAX_BUFFER_SIZE) except: self._close_connection(connection) else: if data: ip, port = connection.getpeername() for input_pack in self._package_factory.create_input_packages( data, connection): logger.debug_message( "Message - {} - from {}:{}".format( input_pack.message.__class__.__name__, ip, port)) self._package_queue.enqueue_input(input_pack) else: self._close_connection(connection)
def _register_player(self, character, endpoint): if 1 > len(character) or -1 == string.ascii_uppercase.find(character): logger.warning( "Login attempt with invalid character: {}".format(character)) return Message.LoginStatus.INVALID_CHARACTER status = self._room.add_player(character, endpoint) if Room.ADDITION_SUCCESSFUL == status: logger.info( "Player '{}' registered successfully".format(character)) return Message.LoginStatus.LOGGED elif Room.ADDITION_REUSE == status: logger.info("Player '{}' reconnected".format(character)) return Message.LoginStatus.RECONNECTION elif Room.ADDITION_ERR_COMPLETE == status: logger.debug("Player '{}' tried to register: room complete".format( character)) return Message.LoginStatus.ROOM_COMPLETED elif Room.ADDITION_ERR_ALREADY_EXISTS == status: logger.debug( "Player '{}' tried to register: already exists".format( character)) return Message.LoginStatus.ALREADY_EXISTS
def _close_connection(self, connection): self._package_queue.enqueue_input(InputPack(None, connection)) self._package_factory.untrack_endpoint(connection) self._selector.unregister(connection) try: ip, port = connection.getpeername() connection.close() logger.debug("Connection closed with {}:{}".format(ip, port)) except: pass
def solve(limit=13): odd_periods = 0 for n in range(2, limit + 1): a0, *period = period_of_root(n) if len(period) % 2 != 0: odd_periods += 1 if period: logger.info( f"√{n} = [{a0}; {tuple(period)}], period={len(period)}") else: logger.debug(f"√{n} = {sqrt(n)}") return odd_periods
def on_update(self, state): self._entity.enable_movement(False) last_movement = self._entity.get_position() - self._last_step_position if last_movement != Vec2.zero(): self._last_step_position = self._entity.get_position().copy() logger.debug("Player '{}' at step {} moves {}".format( self._entity.get_character(), state.get_step(), last_movement)) if self._last_cast_skill != None: spell = super().register_cast(state, self._last_cast_skill) if spell: state.add_spell(spell) logger.debug("Player '{}' at step {} casts {}".format( self._entity.get_character(), state.get_step(), self._last_cast_skill)) self._last_cast_skill = None
def _info_server_request(self, version_message, endpoint): validation = Version.check(version_message.value) checked_version_message = Message.CheckedVersion( Version.CURRENT, validation) self._output_queue.put(OutputPack(checked_version_message, endpoint)) compatibility = "compatible" if validation else "incompatible" logger.debug("Client with version {} - {}".format( version_message.value, compatibility)) character_list = self._room.get_character_list() players = self._room.get_size() points = self._room.get_points_to_win() game_info_message = Message.GameInfo(character_list, players, points, self._arena_size, self._seed) self._output_queue.put(OutputPack(game_info_message, endpoint))
def _create_full_file_from_chunks(self, target_file_path: Path, all_chunk_paths: List[Path], chunk_dir: Path): """ Once all the chunk data has been written create the full file with the aggregated chunks """ # Make sure some other chunk didn't trigger file reconstruction if target_file_path.exists(): logger.debug( f'[{self.LOG_PREFIX}] File {target_file_path!r} exists already. Overwriting..' ) target_file_path.unlink() # Save file from all uploaded chunk data with open(target_file_path, "ab") as fp: for p in all_chunk_paths: with open(p, 'rb') as stored_chunk_file: fp.write(stored_chunk_file.read()) logger.info( f'[{self.LOG_PREFIX}] File saved to {target_file_path!r}') # Remove the chunk dir all all files in it shutil.rmtree(chunk_dir)