Python collections.abc.Iterable() Examples
The following are 30
code examples of collections.abc.Iterable().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
collections.abc
, or try the search function
.
Example #1
Source File: _torrent.py From torf with GNU General Public License v3.0 | 6 votes |
def files(self, files): if not isinstance(files, utils.Iterable): raise ValueError(f'Not an Iterable: {files}') for f in files: if not isinstance(f, utils.File): raise ValueError(f'Not a File object: {f}') elif f.is_absolute(): raise error.PathError(f, msg='Not a relative path') if not files: self._set_files(files=()) else: # os.path.commonpath() returns '' if there is no common path and # raises ValueError if there are absolute and relative paths. try: basepath = os.path.commonpath(files) except ValueError: basepath = '' if basepath == '': raise error.CommonPathError(files) self._set_files(files, pathlib.Path(basepath))
Example #2
Source File: causal_graphs.py From whynot with MIT License | 6 votes |
def check_and_cast_args(args): """Check and cast arguments for the dependency tracer. Since autograd is expecting to compute derivatives, ints are not allowable as arguments. However, for our purposes, casting ints to floats will not affect dependency tracing. """ # Currently this function only casts int into floats and raises # if the element is a string. This can be expanded to be more robust. def check_and_cast(arg): """Attempt to cast the arg to something traceable. Otherwise, error.""" if isinstance(arg, str): raise ValueError(f"Cannot build causal graph for arg {arg} of type str") if isinstance(arg, int): arg = float(arg) return arg if isinstance(args, Iterable): return [check_and_cast(arg) for arg in args] return check_and_cast(args)
Example #3
Source File: parser.py From GSEApy with BSD 3-Clause "New" or "Revised" License | 6 votes |
def gsea_cls_parser(cls): """Extract class(phenotype) name from .cls file. :param cls: the a class list instance or .cls file which is identical to GSEA input . :return: phenotype name and a list of class vector. """ if not isinstance(cls, str) and isinstance(cls, Iterable) : classes = list(cls) sample_name= unique(classes) elif isinstance(cls, str) : with open(cls) as c: file = c.readlines() classes = file[2].strip('\n').split(" ") sample_name = file[1].lstrip("# ").strip('\n').split(" ") else: raise Exception('Error parsing sample name!') if len(sample_name) != 2: raise Exception("Input groups have to be 2!") return sample_name[0], sample_name[1], classes
Example #4
Source File: _bashcomplete.py From recruit with Apache License 2.0 | 6 votes |
def is_incomplete_argument(current_params, cmd_param): """ :param current_params: the current params and values for this argument as already entered :param cmd_param: the current command parameter :return: whether or not the last argument is incomplete and corresponds to this cmd_param. In other words whether or not the this cmd_param argument can still accept values """ if not isinstance(cmd_param, Argument): return False current_param_values = current_params[cmd_param.name] if current_param_values is None: return True if cmd_param.nargs == -1: return True if isinstance(current_param_values, abc.Iterable) \ and cmd_param.nargs > 1 and len(current_param_values) < cmd_param.nargs: return True return False
Example #5
Source File: variables.py From gpkit with MIT License | 6 votes |
def __init__(self, *args, **descr): if len(args) == 1 and isinstance(args[0], VarKey): self.key, = args else: for arg in args: if isinstance(arg, Strings) and "name" not in descr: descr["name"] = arg elif (isinstance(arg, Numbers) or hasattr(arg, "__call__") and "value" not in descr): descr["value"] = arg elif (isinstance(arg, Iterable) and not isinstance(arg, Strings)): if is_sweepvar(arg): descr["value"] = arg else: descr["value"] = ("sweep", arg) elif isinstance(arg, Strings) and "units" not in descr: descr["units"] = arg elif isinstance(arg, Strings) and "label" not in descr: descr["label"] = arg addmodelstodescr(descr, addtonamedvars=self) self.key = VarKey(**descr) Monomial.__init__(self, self.key.hmap) # NOTE: needed because Signomial.__init__ will change the class self.__class__ = Variable
Example #6
Source File: tensor_utils.py From garage with MIT License | 6 votes |
def flatten_inputs(deep): """Flattens an Iterable recursively. Args: deep (Iterable): An Iterable to flatten. Returns: List: The flattened result. """ def flatten(deep): for d in deep: if isinstance(d, Iterable) and not isinstance( d, (str, bytes, tf.Tensor, np.ndarray)): yield from flatten(d) else: yield d return list(flatten(deep)) # pylint: enable=missing-yield-doc # pylint: enable=missing-yield-type-doc # yapf: enable
Example #7
Source File: squeeze.py From mars with Apache License 2.0 | 6 votes |
def _get_squeeze_shape(shape, axis): if axis is not None: if isinstance(axis, Iterable): axis = tuple(axis) else: axis = (axis,) for ax in axis: if shape[ax] != 1: raise ValueError('cannot select an axis to squeeze out ' 'which has size not equal to one') shape = tuple(s for i, s in enumerate(shape) if i not in axis) else: axis = tuple(i for i, s in enumerate(shape) if s == 1) shape = tuple(s for s in shape if s != 1) return shape, axis
Example #8
Source File: robot_emitter_receiver_csv.py From deepbots with GNU General Public License v3.0 | 6 votes |
def handle_emitter(self): """ This emitter uses the user-implemented create_message() method to get whatever data the robot gathered, convert it to a string if needed and then use the emitter to send the data in a string utf-8 encoding to the supervisor. """ data = self.create_message() assert isinstance(data, Iterable), "The action object should be Iterable" string_message = "" # message can either be a list that needs to be converted in a string # or a straight-up string if type(data) is list: string_message = ",".join(map(str, data)) elif type(data) is str: string_message = data else: raise TypeError( "message must be either a comma-separated string or a 1D list") string_message = string_message.encode("utf-8") self.emitter.send(string_message)
Example #9
Source File: aggregation.py From mars with Apache License 2.0 | 6 votes |
def _check_if_func_available(func): to_check = [] if isinstance(func, list): to_check.extend(func) elif isinstance(func, dict): for f in func.values(): if isinstance(f, Iterable) and not isinstance(f, str): to_check.extend(f) else: to_check.append(f) else: to_check.append(func) for f in to_check: if f not in _available_aggregation_functions: return False return True
Example #10
Source File: getitem.py From mars with Apache License 2.0 | 6 votes |
def __call__(self, groupby): indexed = groupby.op.build_mock_groupby()[self.selection] if isinstance(indexed, pd_groupby.SeriesGroupBy): self.output_types = [OutputType.series_groupby] params = dict(shape=(groupby.shape[0],), name=self.selection, dtype=groupby.dtypes[self.selection], index_value=groupby.index_value, key_dtypes=groupby.key_dtypes) else: self.output_types = [OutputType.dataframe_groupby] if isinstance(self.selection, Iterable) and not isinstance(self.selection, str): item_list = list(self.selection) else: item_list = [self.selection] params = groupby.params.copy() params['dtypes'] = new_dtypes = groupby.dtypes[item_list] params['selection'] = self.selection params['shape'] = (groupby.shape[0], len(item_list)) params['columns_value'] = parse_index(new_dtypes.index, store_data=True) return self.new_tileable([groupby], **params)
Example #11
Source File: getitem.py From mars with Apache License 2.0 | 6 votes |
def df_groupby_getitem(df_groupby, item): try: hash(item) hashable = True except TypeError: hashable = False if hashable and item in df_groupby.dtypes: output_types = [OutputType.series_groupby] elif isinstance(item, Iterable) and all(it in df_groupby.dtypes for it in item): output_types = [OutputType.dataframe_groupby] else: raise NameError('Cannot slice groupby with %r' % item) if df_groupby.selection: raise IndexError('Column(s) %r already selected' % df_groupby.selection) op = GroupByIndex(selection=item, output_types=output_types) return op(df_groupby)
Example #12
Source File: aggregation.py From mars with Apache License 2.0 | 6 votes |
def _is_funcs_agg(func): to_check = [] if isinstance(func, list): to_check.extend(func) elif isinstance(func, dict): for f in func.values(): if isinstance(f, Iterable) and not isinstance(f, str): to_check.extend(f) else: to_check.append(f) else: to_check.append(func) for f in to_check: if f not in _builtin_aggregation_functions: return False return True
Example #13
Source File: misc.py From vedaseg with Apache License 2.0 | 6 votes |
def iter_cast(inputs, dst_type, return_type=None): """Cast elements of an iterable object into some type. Args: inputs (Iterable): The input object. dst_type (type): Destination type. return_type (type, optional): If specified, the output object will be converted to this type, otherwise an iterator. Returns: iterator or specified type: The converted object. """ if not isinstance(inputs, collections_abc.Iterable): raise TypeError('inputs must be an iterable object') if not isinstance(dst_type, type): raise TypeError('"dst_type" must be a valid type') out_iterable = six.moves.map(dst_type, inputs) if return_type is None: return out_iterable else: return return_type(out_iterable)
Example #14
Source File: client.py From jira with BSD 2-Clause "Simplified" License | 6 votes |
def role(self): """Return Jira role information. :return: List of current user roles :rtype: Iterable """ # https://developer.atlassian.com/cloud/jira/platform/rest/v3/?utm_source=%2Fcloud%2Fjira%2Fplatform%2Frest%2F&utm_medium=302#api-rest-api-3-role-get url = self._options["server"] + "/rest/api/latest/role" r = self._session.get(url) return json_loads(r) # Experimental # Experimental support for iDalko Grid, expect API to change as it's using private APIs currently # https://support.idalko.com/browse/IGRID-1017
Example #15
Source File: core.py From mars with Apache License 2.0 | 6 votes |
def base_equal(self, ob1, ob2): if type(ob1) != type(ob2): return False def cmp(obj1, obj2): if isinstance(obj1, np.ndarray): return np.array_equal(obj1, obj2) elif isinstance(obj1, Iterable) and \ not isinstance(obj1, str) and \ isinstance(obj2, Iterable) and \ not isinstance(obj2, str): return all(cmp(it1, it2) for it1, it2 in itertools.zip_longest(obj1, obj2)) elif hasattr(obj1, 'key') and hasattr(obj2, 'key'): return obj1.key == obj2.key elif isinstance(obj1, ReferenceType) and isinstance(obj2, ReferenceType): return cmp(obj1(), obj2()) else: return obj1 == obj2 for slot in ob1.__slots__: if not cmp(getattr(ob1, slot, None), getattr(ob2, slot, None)): return False return True
Example #16
Source File: utils.py From DenseMatchingBenchmark with MIT License | 6 votes |
def cast_tensor_type(inputs, src_type, dst_type): if isinstance(inputs, torch.Tensor): return inputs.to(dst_type) elif isinstance(inputs, str): return inputs elif isinstance(inputs, np.ndarray): return inputs elif isinstance(inputs, abc.Mapping): return type(inputs)({ k: cast_tensor_type(v, src_type, dst_type) for k, v in inputs.items() }) elif isinstance(inputs, abc.Iterable): return type(inputs)( cast_tensor_type(item, src_type, dst_type) for item in inputs) else: return inputs
Example #17
Source File: utils.py From dgl with Apache License 2.0 | 6 votes |
def group_as_dict(pairs): """Combines a list of key-value pairs to a dictionary of keys and value lists. Does not require the pairs to be sorted by keys. Parameters ---------- pairs : iterable Iterable of key-value pairs Returns ------- dict The dictionary of keys and value lists. """ dic = defaultdict(list) for key, value in pairs: dic[key].append(value) return dic
Example #18
Source File: _torrent.py From torf with GNU General Public License v3.0 | 6 votes |
def filepaths(self, filepaths): if not isinstance(filepaths, utils.Iterable): raise ValueError(f'Not an Iterable: {filepaths}') filepaths = utils.Filepaths(filepaths) # Resolve directories if not filepaths: self._set_files(files=()) else: # Make all paths absolute so we can find the common path. Do not # resolve symlinks so the user isn't confronted with unexpected # paths in case of an error. cwd = pathlib.Path.cwd() filepaths_abs = tuple(fp if fp.is_absolute() else cwd / fp for fp in filepaths) try: basepath = pathlib.Path(os.path.commonpath(filepaths_abs)) except ValueError: raise error.CommonPathError(filepaths) filepaths = tuple(utils.File(fp, size=utils.real_size(fp)) for fp in filepaths) self._set_files(filepaths, basepath)
Example #19
Source File: _validate.py From UWGeodynamics with GNU General Public License v3.0 | 5 votes |
def _listify_validator(scalar_validator, allow_stringlist=False): def f(s): if isinstance(s, six.string_types): try: return [scalar_validator(v.strip()) for v in s.split(',') if v.strip()] except Exception: if allow_stringlist: # Sometimes, a list of colors might be a single string # of single-letter colornames. So give that a shot. return [scalar_validator(v.strip()) for v in s if v.strip()] else: raise # We should allow any generic sequence type, including generators, # Numpy ndarrays, and pandas data structures. However, unordered # sequences, such as sets, should be allowed but discouraged unless the # user desires pseudorandom behavior. elif isinstance(s, abc.Iterable) and not isinstance(s, abc.Mapping): # The condition on this list comprehension will preserve the # behavior of filtering out any empty strings (behavior was # from the original validate_stringlist()), while allowing # any non-string/text scalar values such as numbers and arrays. return [scalar_validator(v) for v in s if not isinstance(v, six.string_types) or v] else: msg = "{0!r} must be of type: string or non-dictionary iterable.".format(s) raise ValueError(msg) f.__doc__ = scalar_validator.__doc__ return f
Example #20
Source File: _torrent.py From torf with GNU General Public License v3.0 | 5 votes |
def trackers(self, value): if value is None: value = () if isinstance(value, abc.Iterable): self._trackers_changed(utils.Trackers(value)) else: raise ValueError(f'Must be Iterable, str or None, not {type(value).__name__}: {value}')
Example #21
Source File: geoip.py From msticpy with MIT License | 5 votes |
def lookup_ip( self, ip_address: str = None, ip_addr_list: Iterable = None, ip_entity: IpAddress = None, ) -> Tuple[List[Any], List[IpAddress]]: """ Lookup IP location abstract method. Parameters ---------- ip_address : str, optional a single address to look up (the default is None) ip_addr_list : Iterable, optional a collection of addresses to lookup (the default is None) ip_entity : IpAddress, optional an IpAddress entity (the default is None) - any existing data in the Location property will be overwritten Returns ------- Tuple[List[Any], List[IpAddress]]: raw geolocation results and same results as IpAddress entities with populated Location property. """
Example #22
Source File: glm_reporter.py From nistats with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _coerce_to_dict(input_arg): """ Constructs a dict from the provided arg. If input_arg is: dict then returns it unchanged. string or collection of Strings or Sequence[int], returns a dict {str(value): value, ...} Parameters ---------- input_arg: String or Collection[str or Int or Sequence[Int]] or Dict[str, str or np.array] Can be of the form: 'string' ['string_1', 'string_2', ...] list/array [list/array_1, list/array_2, ...] {'string_1': list/array1, ...} Returns ------- Dict[str, np.array or str] """ if not isinstance(input_arg, dict): if isinstance(input_arg, Iterable): if not isinstance(input_arg[0], Iterable): input_arg = [input_arg] input_arg = [input_arg] if isinstance(input_arg, str) else input_arg input_arg = {str(contrast_): contrast_ for contrast_ in input_arg} return input_arg
Example #23
Source File: _torrent.py From torf with GNU General Public License v3.0 | 5 votes |
def webseeds(self, value): if isinstance(value, str): urls = utils.URLs((value,)) elif isinstance(value, abc.Iterable): urls = utils.URLs(value) elif value is None: urls = utils.URLs(()) else: raise ValueError(f'Must be Iterable, str or None, not {type(value).__name__}: {value}') self._webseeds_changed(urls)
Example #24
Source File: utils.py From pybel with MIT License | 5 votes |
def subdict_matches(target: Mapping, query: Mapping, partial_match: bool = True) -> bool: """Check if all the keys in the query dict are in the target dict, and that their values match. 1. Checks that all keys in the query dict are in the target dict 2. Matches the values of the keys in the query dict a. If the value is a string, then must match exactly b. If the value is a set/list/tuple, then will match any of them c. If the value is a dict, then recursively check if that subdict matches :param target: The dictionary to search :param query: A query dict with keys to match :param partial_match: Should the query values be used as partial or exact matches? Defaults to :code:`True`. :return: if all keys in b are in target_dict and their values match """ for k, v in query.items(): if k not in target: return False elif not isinstance(v, (int, str, dict, Iterable)): raise ValueError('invalid value: {}'.format(v)) elif isinstance(v, (int, str)) and target[k] != v: return False elif isinstance(v, dict): if partial_match: if not isinstance(target[k], dict): return False elif not subdict_matches(target[k], v, partial_match): return False elif not partial_match and target[k] != v: return False elif isinstance(v, Iterable) and target[k] not in v: return False return True
Example #25
Source File: _torrent.py From torf with GNU General Public License v3.0 | 5 votes |
def exclude_globs(self, value): if not isinstance(value, utils.Iterable): raise ValueError(f'Must be Iterable, not {type(value).__name__}: {value}') self._exclude['globs'][:] = value
Example #26
Source File: _torrent.py From torf with GNU General Public License v3.0 | 5 votes |
def httpseeds(self, value): if isinstance(value, str): urls = utils.URLs((value,)) elif isinstance(value, abc.Iterable): urls = utils.URLs(value) elif value is None: urls = utils.URLs(()) else: raise ValueError(f'Must be Iterable, str or None, not {type(value).__name__}: {value}') self._httpseeds_changed(urls)
Example #27
Source File: flow.py From dataflows with MIT License | 5 votes |
def _chain(self, ds=None): from ..helpers import datapackage_processor, rows_processor, row_processor, iterable_loader for position, link in enumerate(self._preprocess_chain(), start=1): if isinstance(link, Flow): ds = link._chain(ds) elif isinstance(link, DataStreamProcessor): ds = link(ds, position=position) elif isfunction(link): sig = signature(link) params = list(sig.parameters) if len(params) == 1: if params[0] == 'row': ds = row_processor(link)(ds, position=position) elif params[0] == 'rows': ds = rows_processor(link)(ds, position=position) elif params[0] == 'package': ds = datapackage_processor(link)(ds, position=position) else: assert False, 'Failed to parse function signature %r' % params else: assert False, 'Failed to parse function signature %r' % params elif isinstance(link, Iterable): ds = iterable_loader(link)(ds, position=position) return ds
Example #28
Source File: vggtransformer.py From fairseq with MIT License | 5 votes |
def parse_transformer_context(self, transformer_context): """ transformer_context can be the following: - None; indicates no context is used, i.e., transformer can access full context - a tuple/list of two int; indicates left and right context, any number <0 indicates infinite context * e.g., (5, 6) indicates that for query at x_t, transformer can access [t-5, t+6] (inclusive) * e.g., (-1, 6) indicates that for query at x_t, transformer can access [0, t+6] (inclusive) """ if transformer_context is None: return None if not isinstance(transformer_context, Iterable): raise ValueError("transformer context must be Iterable if it is not None") if len(transformer_context) != 2: raise ValueError("transformer context must have length 2") left_context = transformer_context[0] if left_context < 0: left_context = None right_context = transformer_context[1] if right_context < 0: right_context = None if left_context is None and right_context is None: return None return (left_context, right_context)
Example #29
Source File: vggtransformer.py From fairseq with MIT License | 5 votes |
def parse_transformer_sampling(self, transformer_sampling, num_layers): """ parsing transformer sampling configuration Args: - transformer_sampling, accepted input: * None, indicating no sampling * an Iterable with int (>0) as element - num_layers, expected number of transformer layers, must match with the length of transformer_sampling if it is not None Returns: - A tuple with length num_layers """ if transformer_sampling is None: return (1,) * num_layers if not isinstance(transformer_sampling, Iterable): raise ValueError( "transformer_sampling must be an iterable if it is not None" ) if len(transformer_sampling) != num_layers: raise ValueError( "transformer_sampling {} does not match with the number " + "of layers {}".format(transformer_sampling, num_layers) ) for layer, value in enumerate(transformer_sampling): if not isinstance(value, int): raise ValueError("Invalid value in transformer_sampling: ") if value < 1: raise ValueError( "{} layer's subsampling is {}.".format(layer, value) + " This is not allowed! " ) return transformer_sampling
Example #30
Source File: tensor_utils.py From garage with MIT License | 5 votes |
def flatten_tensor_variables(ts): """Flattens a list of tensors into a single, 1-dimensional tensor. Args: ts (Iterable): Iterable containing either tf.Tensors or arrays. Returns: tf.Tensor: Flattened Tensor. """ return tf.concat(axis=0, values=[tf.reshape(x, [-1]) for x in ts], name='flatten_tensor_variables')