def rename_key(self, key: str, remote_path: Union(str, PathLike), new_remote_path: Union(str, PathLike)): remote_path = PurePath(remote_path) new_remote_path = PurePath(new_remote_path) remote_file = self.get_key(key, remote_path).file new_parent = self.folder.create_path(str(new_remote_path.parent)) remote_file.move(new_parent, new_name=new_remote_path.name)
def get_key(self, key: str, remote_path: Union(str, PathLike)) -> ExportKey: remote_path = PurePath(remote_path) remote_file = self.folder.child_from_path(str(remote_path)) if remote_file.isfolder(): raise NotAFileError(str(remote_path)) return ExportKey(self, key, remote_path, remote_file)
def apply_transit_feature_change( self, trip_ids: pd.Series, properties: list, in_place: bool = True) -> Union(None, TransitNetwork): """ Changes the transit attributes for the selected features based on the project card information passed Args: trip_ids : pd.Series all trip_ids to apply change to properties : list of dictionaries transit properties to change in_place : bool whether to apply changes in place or return a new network Returns: None """ for i in properties: if i["property"] in ["headway_secs"]: self._apply_transit_feature_change_frequencies( trip_ids, i, in_place) elif i["property"] in ["routing"]: self._apply_transit_feature_change_routing( trip_ids, i, in_place)
def _apply_transit_feature_change_frequencies( self, trip_ids: pd.Series, properties: dict, in_place: bool = True) -> Union(None, TransitNetwork): freq = self.feed.frequencies.copy() # Grab only those records matching trip_ids (aka selection) freq = freq[freq.trip_id.isin(trip_ids)] # Check all `existing` properties if given if properties.get("existing") is not None: if not all(freq.headway_secs == properties["existing"]): WranglerLogger.error("Existing does not match for at least " "1 trip in:\n {}".format( trip_ids.to_string())) raise ValueError # Calculate build value if properties.get("set") is not None: build_value = properties["set"] else: build_value = [i + properties["change"] for i in freq.headway_secs] # Update self or return a new object q = self.feed.frequencies.trip_id.isin(freq["trip_id"]) if in_place: self.feed.frequencies.loc[q, properties["property"]] = build_value else: updated_network = copy.deepcopy(self) updated_network.loc[q, properties["property"]] = build_value return updated_network
def setX(self, x: Union(List[[float]], np.ndarray)) -> Data: if type(x).__name__ == "ndarray": self.x = x if type(x).__name__ == "list": arr = np.array(x, dtype=float) if len(arr.shape) > 1: self.x = np.array(x, dtype=float) else: raise Exception("Two dimentional list expected.") try: del self.xTrain except AttributeError: pass try: del self.xTest except AttributeError: pass try: del self.yTest except AttributeError: pass try: del self.yTrain except AttributeError: pass return self
def resolve(self) -> bool: """ Resolves conflicts on the blockchain """ winner_chain = self.chain chain_replaced = False for node in self.peer_nodes: try: response: Union(List[Block.__dict__], None) = requests.get( self.get_url(node)).json() if response: chain: List[Block] = [ block_from_deserialized_block(block) for block in response ] if self.replace_chain(chain): winner_chain = chain chain_replaced = True except requests.exceptions.ConnectionError: continue self.update_blockchain(winner_chain) if chain_replaced: self.reset_open_tx() return chain_replaced
def get_session( self, block_hash: Union(str, bytes)) -> session.UploadSession: block_hash = reconstructhash.deconstruct_hash( bytesconverter.bytes_to_str(block_hash)) for session in self.sessions: if session.block_hash == block_hash: return session raise KeyError
def __init__( self, data: Optional[Union(Series, DataFrame, dict[str, list])] = None, index: Optional[PandasIndex] = None, ) -> None: if data is None: self._data = DataFrame(dtype="object") elif type(data) is Series: self._data = cast(Series, data).to_frame().transpose() elif type(data) is DataFrame: self._data = DataFrame(data) elif type(data) is dict: sample_value = next(iter(data.values())) if not isinstance(sample_value, Iterable) or isinstance( sample_value, str): self._data = Series(data).to_frame().transpose() else: self._data = DataFrame(data) else: raise ValueError( f"Received unexpected value type {type(data)}: {data}") if index is None: self._data.index.name = "index" self._index = PandasIndex(self._data.index, []) else: if not isinstance(index, PandasIndex): index = PandasIndex(index) self._data.index = index._data self._index = index self._loc = _LocIndexer(self) self._iloc = _ILocIndexer(self)
def is_accending(nums: list) -> Union(None, int): i = None for j in range(len(nums) - 1): if nums[j] > nums[j + 1]: i = j break return (i)
def get_groupby( self, code_list: Union(str, List[str]), start_date: str = None, end_date: str = None, method: str = "rough", industry: str = "sw_l1", ): """ 获取股票列表对应的行业列表,默认为粗糙获取模式,即用当前股票所属行业作为其历史行业 :param code_list: 股票列表 :param start_date: 起始时间 :param end_date: 结束时间 """ if isinstance(code_list, str): code_list = [code_list] if not start_date: if not end_date: end_date = str(datetime.date.today()) return QA.QA_fetch_stock_industry(code=code_list, start=end_date, end=end_date, industry=industry) if not end_date: end_date = str(datetime.date.today()) return QA.QA_fetch_stock_industry(code=code_list, start=start_date, end=end_date, industry=industry)
def from_xgboost( model: Union.__getitem__(tuple(xgb_operator_list)), # noqa inputs: Sequence[IOShape], device: str = 'cpu', extra_config: Optional[dict] = None, ) -> torch.nn.Module: """Convert PyTorch module from XGBoost""" # inputs for XGBoost should contains only 1 argument with 2 dim if not (len(inputs) == 1 and len(inputs[0].shape) == 2): raise RuntimeError( 'XGboost does not support such input data for inference. The input data should contains only 1\n' 'argument with exactly 2 dimensions.' ) if extra_config is None: extra_config = dict() # assert batch size batch_size = inputs[0].shape[0] if batch_size == -1: batch_size = 1 test_input = np.random.rand(batch_size, inputs[0].shape[1]) extra_config_ = PyTorchConverter.hb_common_extra_config.copy() extra_config_.update(extra_config) return _convert_xgboost( model, 'torch', test_input=test_input, device=device, extra_config=extra_config_ )
def new_zone(self, zone_name: str) -> (bool,Union(str,None)): req: list = self.db.search(self.zone_query.name == zone_name) if len(req) > 0: return (False, "This Zone already exists") else: self.db.insert({'name':zone_name, 'user':{'$ALL':{}}}) return True
def union(self, other, all=True): """ Returns a Query representing a the union of the two queries. This is simply the two tables concatenated. By passing the argument all as true the duplicates are also removed. Parameters ---------- other : Query An instance of a query object. all : Bool If true returns sql UNION ALL else returns UNION Returns ------- Union Query representing the concatenation of the two queries Examples -------- >>> dl1 = daily_location('2016-01-01', spatial_unit=CellSpatialUnit()) >>> dl2 = daily_location('2016-01-02', spatial_unit=CellSpatialUnit()) >>> dl1.union(dl2).get_query() 'cell_msisdn_20160101 UNION ALL cell_msisdn_20160102' >>> dl1.union(dl2,all=False).get_query() 'cell_msisdn_20160101 UNION cell_msisdn_20160102' """ from .union import Union return Union(self, other, all)
def _get_recovered_source_columns(self, source: Union(list, dict)) -> list: """Recovered hexed columns and modifiers from parsed script.""" recovered_source = [] if isinstance(source, dict): source = [source] for term in source: recovered = None if isinstance(term, str) and term in self.modifier_names: recovered = self.source_modifiers[term] elif isinstance(term, str): recovered = self.parser.get_field_from_script( term, self.source_columns, self.schema) elif isinstance(term, list): recovered = self._get_recovered_source_columns(term) elif isinstance(term, dict): # Check if 'destination' is a column or schema field destination = term.get("destination") if destination: destination = self.parser.get_field_from_script( destination, self.source_columns, self.schema) recovered = { "action": term.get("action"), "destination": destination, "source": self._get_recovered_source_columns(term.get("source")), } if not recovered: raise ValueError(f"Term ({term}) cannot be parsed.") recovered_source.append(recovered) return recovered_source
def delete_dir(self, dir_path: Union(str, PathLike)): try: remote_folder = self.folder.child_from_path(str(dir_path)) except FileNotFoundError: return if not remote_folder.isfolder(): raise NotADirectoryError remote_folder.remove()
def test_cannot_instantiate(self): with self.assertRaises(TypeError): Union() u = Union[int, float] with self.assertRaises(TypeError): u() with self.assertRaises(TypeError): type(u)()
def lookup_power_for_brightness(self, lut_value: Union(dict, int), light_setting: LightSetting): if light_setting.color_mode == COLOR_MODE_BRIGHTNESS: return lut_value if light_setting.color_mode == COLOR_MODE_COLOR_TEMP: return self.get_nearest(lut_value, light_setting.color_temp) else: sat_values = self.get_nearest(lut_value, light_setting.hue) return self.get_nearest(sat_values, light_setting.saturation)
def __init__(self, rootfolder: DriveFolder, annex: Annex, uuid: str = None, local_appdir: Union(str, PathLike) = None): super().__init__(rootfolder, annex, uuid=uuid, local_appdir=local_appdir)
def __init__(self) -> None: self.states: Dict[str, State] = {} self.current_state: Optional[State] = None self.transitions: Dict[str, Transition] = {} self.add_overloads: Dict[str, Callable[[Union(State, Transition)], None]] = { 'State': self._add_state, 'Transition': self._add_transition }
def new_key(self, key: str, remote_path: Union(str, PathLike)) -> ExportKey: remote_path = PurePath(remote_path) try: self.get_key(key, str(remote_path)) except FileNotFoundError: parent = self.folder.create_path(str(remote_path.parent)) remote_file = parent.new_file(remote_path.name) return ExportKey(self, key, remote_path, remote_file) else: raise FileExistsError(remote_path)
def __init__(self, rootfolder: DriveFolder, annex: Annex, uuid: str = None, local_appdir: Union(str, PathLike) = None): self.creator = None self.annex = annex self.folder = rootfolder self.uuid = uuid if local_appdir is not None: self.local_appdir = Path(local_appdir)
def chunk_lengths(s: str) -> Union(int): # Get length of left and right chunks left = count_from_left(s) + 1 right = len(s) - count_from_right(s) # String with left and right chunks removed neighbor_s = s[left:-right] # Get length of left and right chunks' neighbors left_neighbor = count_from_left(neighbor_s) + 1, right_neighbor = len(neighbor_s) - count_from_right(neighbor_s) # Return lengths return left + left_neighbor, right + right_neighbor
def addThing(self, thing: Thing) -> Union(list, None): """Gets a Thing to add; looks for it in existing list of things; if the thing is already present return None; otherwise add the Thing and return the updated list of things """ if thing in self.things: return None self.things.append(thing) return self.things
def __add__(self, rhs: Union(str, StrArray)) -> StrArray: result: list[str] = [] if isinstance(rhs, str): for item in self.values: result.append(item + rhs) else: assert len(self.values) == len(rhs.values) for i in range(len(self.values)): result.append(self.values[i] + rhs.values[i]) return StrArray(result)
def __transform__(self, params): params = Union._flatten_params(params) param_len = len(params) if param_len == 0: raise TypeError('Cannot instantiate typing.Union') elif param_len == 1: return params[0] self.__args__ = params return self
def __init__(self, rootfolder: DriveFolder, annex: Annex, uuid: str = None, local_appdir: Union(str, PathLike) = None): super().__init__(rootfolder, annex, uuid=uuid, local_appdir=local_appdir) self.nested_prefix = "NESTED-" self.reserved_name = self.nested_prefix + "00000000-0000-0000-0000-000000000000" self.full_suffix = "-FULL"
def broadcast_transactions(): transaction_data: Union(Dict, None) = request.get_json() ensure_required_fields(transaction_data, RequiredFields.get_required_fields(BlockchainEndpoints.TRANSACTION), action_subject='broadcast transaction') ensure_tx_appended(blockchain.add_transaction( **transaction_data, is_broadcast_tx=True)) message = get_message(HttpMethods.POST, True, 'broadcast transaction') return Response({'transaction': get_serializable_transaction(blockchain.latest_transaction), 'missing_fields': None, "transactions_synced": True}, message, 200).get()
def _parse_instruction(self, instruction: str) -> List[Union(List[int], int)]: if instruction[0] == 'U': direction = [1, 0] elif instruction[0] == 'D': direction = [-1, 0] elif instruction[0] == 'L': direction = [0, -1] elif instruction[0] == 'R': direction = [0, 1] return [direction, int(instruction[1:])]
def removeThing(self, thing: Thing) -> Union(list, None): """Gets a Thing to remove; looks for it in existing list of things; if the thing is not already present return None; otherwise remove the Thing and return the updated list of things """ if not thing in self.things: return None self.things.remove(thing) return self.things
def __call__(self, data: ndarray, quantity: Union(float, int)) -> ndarray: """The damager callable function Parameters: - data (ndarray): Data to damage. - amount (float): Proportion of the damaged to undamaged elements.""" numberOfNaN = int(data.shape[0] * data.shape[1] * quantity) data.ravel()[np.random.choice(data.size, numberOfNaN, replace=False)] = np.nan self.logger.info("damaged {0} elements of the {1}".format( numberOfNaN, data.__class__.__name__)) return data