def _assert_file(self, obj, fn_tested, fn_correct, compression=None, file_filter=lambda _: False): """Compare files.""" open_kwargs = {} if compression is None: open_fn = open # by default, open() will open files as text and return str # objects, but we need bytes objects open_kwargs['mode'] = 'rb' elif compression == 'gzip': open_fn = gzip.open elif compression == 'zip': open_fn = zipfile.ZipFile.open else: raise ValueError("Unsupported compression format.") output = os.path.join(settings.FLOW_EXECUTOR['DATA_DIR'], str(obj.pk), fn_tested) with open_fn(output, **open_kwargs) as output_file: output_contents = b"".join([line for line in filterfalse(file_filter, output_file)]) output_hash = hashlib.sha256(output_contents).hexdigest() correct_path = os.path.join(self.files_path, fn_correct) if not os.path.isfile(correct_path): shutil.copyfile(output, correct_path) self.fail(msg="Output file {} missing so it was created.".format(fn_correct)) with open_fn(correct_path, **open_kwargs) as correct_file: correct_contents = b"".join([line for line in filterfalse(file_filter, correct_file)]) correct_hash = hashlib.sha256(correct_contents).hexdigest() self.assertEqual(correct_hash, output_hash, msg="File contents hash mismatch: {} != {}".format( correct_hash, output_hash) + self._debug_info(obj))
def __iter__(self): if self.cache_location is not None: filenames = iglob(self.filename('*')) keys = map(lambda f: os.path.splitext(os.path.basename(f))[0], filenames) new_keys = filterfalse(lambda key: key in self._cache.keys(), keys) return chain(iterkeys(self._cache), new_keys) else: return iterkeys(self._cache)
def _process_pbs_nodefile(pbs_nodefile, hostname): with open(pbs_nodefile) as in_file: nodelist = in_file.read().splitlines() slave_compute_node_list = [ node for node, _ in groupby(filterfalse(lambda x: x == hostname, nodelist)) ] return slave_compute_node_list
def partition(pred, iterable): """ Partition the iterable into two disjoint entries based on the predicate. @return: Tuple (iterable1, iterable2) """ iter1, iter2 = itertools.tee(iterable) return filterfalse(pred, iter1), filter(pred, iter2)
def unique(iterable, filterfalse=filterfalse): """ Return only unique elements from the sequence. """ seen = set() add = seen.add for element in filterfalse(seen.__contains__, iterable): add(element) yield element
def load_rule_set(rules_file_name): with open(os.path.join(os.path.dirname(__file__), data_dir_name, "rules_{}.dat".format(rules_file_name))) as rules_file: reader = csv.reader(filterfalse(lambda x: re.match(r"^\s*(?:#|$)", x), rules_file), delimiter="\t") # SMARTS and name, tab-seperated rule_set = [{"n": n, "SMARTS": x[0], "rxn": AllChem.ReactionFromSmarts(x[0]), "name": x[1]} for n, x in enumerate(reader, 1)] return rule_set
def args_to_string(*args, **kwargs): invalid = tuple(filterfalse(repr_is_constructor, chain(args, kwargs.values()))) if invalid: raise ValueError return ', '.join(chain((repr(a) for a in args), ('{0}={1}'.format(str(k), repr(v)) for k, v in kwargs.items())))
def ls_moduledirs(path, private=True, full=True): """ lists all dirs which are python modules in path """ dir_list = ls_dirs(path) module_dir_iter = filter(is_module_dir, dir_list) if not private: module_dir_iter = filterfalse(is_private_module, module_dir_iter) if not full: module_dir_iter = map(basename, module_dir_iter) return list(module_dir_iter)
def to_datetime(content, dt_format=None, warn=False): """Parses and formats strings into datetimes. Args: content (str): The string to parse. dt_format (str): Date format passed to `strftime()` (default: None). warn (bool): raise error if content can't be safely converted (default: False) Returns: obj: The datetime object or formatted datetime string. See also: `tabutils.process.type_cast` Examples: >>> fmt = '%Y-%m-%d %H:%M:%S' >>> to_datetime('5/4/82 2:00 pm') datetime.datetime(1982, 5, 4, 14, 0) >>> to_datetime('5/4/82 10:00', fmt) == '1982-05-04 10:00:00' True >>> to_datetime('2/32/82 12:15', fmt) == '1982-02-28 12:15:00' True >>> to_datetime('spam') datetime.datetime(9999, 12, 31, 0, 0) >>> to_datetime('spam', warn=True) Traceback (most recent call last): ValueError: Invalid datetime value: `spam`. Returns: datetime """ bad_nums = map(str, range(29, 33)) good_nums = map(str, range(31, 27, -1)) try: bad_num = next(x for x in bad_nums if x in content) except StopIteration: options = [content] else: possibilities = (content.replace(bad_num, x) for x in good_nums) options = it.chain([content], possibilities) # Fix impossible dates, e.g., 2/31/15 results = filterfalse(lambda x: x[1], map(_to_datetime, options)) value = next(results)[0] if warn and value == DEFAULT_DATETIME: raise ValueError("Invalid datetime value: `%s`." % content) else: datetime = value.strftime(dt_format) if dt_format else value return datetime
def ls_modulefiles(path, private=True, full=True, noext=False): module_file_list = ls(path, '*.py') module_file_iter = iter(module_file_list) if not private: module_file_iter = filterfalse(is_private_module, module_file_iter) if not full: module_file_iter = map(basename, module_file_iter) if noext: module_file_iter = (splitext(path)[0] for path in module_file_iter) return list(module_file_iter)
def _candidate_dirs(base_path): """ Return all dirs in base_path that might be packages. """ has_dot = lambda name: '.' in name for root, dirs, files in os.walk(base_path, followlinks=True): # Exclude directories that contain a period, as they cannot be # packages. Mutate the list to avoid traversal. dirs[:] = filterfalse(has_dot, dirs) for dir in dirs: yield os.path.relpath(os.path.join(root, dir), base_path)
def equivalentMessage(self, msg1, msg2): msg1_count = count_messages(msg1) msg2_count = count_messages(msg2) if msg1_count != msg2_count: return False # Do some basic payload checking msg1_msgs = [m for m in msg1.walk()] msg1_msgs = [m for m in filterfalse(ud.is_skippable, msg1_msgs)] msg2_msgs = [m for m in msg2.walk()] msg2_msgs = [m for m in filterfalse(ud.is_skippable, msg2_msgs)] for i in range(0, len(msg2_msgs)): m1_msg = msg1_msgs[i] m2_msg = msg2_msgs[i] if m1_msg.get_charset() != m2_msg.get_charset(): return False if m1_msg.is_multipart() != m2_msg.is_multipart(): return False m1_py = m1_msg.get_payload(decode=True) m2_py = m2_msg.get_payload(decode=True) if m1_py != m2_py: return False return True
def unique_everseen(iterable, key=None): "List unique elements, preserving order. Remember all elements ever seen." # http://docs.python.org/2/library/itertools.html seen = set() seen_add = seen.add if key is None: for element in filterfalse(seen.__contains__, iterable): seen_add(element) yield element else: for element in iterable: k = key(element) if k not in seen: seen_add(k) yield element
def partition(pred, iterable): """ Returns a 2-tuple of iterables derived from the input iterable. The first yields the items that have ``pred(item) == False``. The second yields the items that have ``pred(item) == True``. >>> is_odd = lambda x: x % 2 != 0 >>> iterable = range(10) >>> even_items, odd_items = partition(is_odd, iterable) >>> list(even_items), list(odd_items) ([0, 2, 4, 6, 8], [1, 3, 5, 7, 9]) """ # partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9 t1, t2 = tee(iterable) return filterfalse(pred, t1), filter(pred, t2)
def wrapper(*args, **kwds): # cache key records both positional and keyword args key = args if kwds: key += (kwd_mark,) + tuple(sorted(kwds.items())) # record recent use of this key queue_append(key) refcount[key] += 1 # get cache entry or compute if not found try: result, expire_time = cache[key] if expire_time and time() > expire_time: raise KeyError('Expired') wrapper.hits += 1 except KeyError: result = user_function(*args, **kwds) if maxtime: expire_time = time() + maxtime else: expire_time = None cache[key] = result, expire_time wrapper.misses += 1 # purge least recently used cache entry if len(cache) > maxsize: key = queue_popleft() refcount[key] -= 1 while refcount[key]: key = queue_popleft() refcount[key] -= 1 del cache[key], refcount[key] # periodically compact the queue by eliminating duplicate keys # while preserving order of most recent access if len(queue) > maxqueue: refcount.clear() queue_appendleft(sentinel) for key in filterfalse(refcount.__contains__, iter(queue_pop, sentinel)): queue_appendleft(key) refcount[key] = 1 return result
def modify_interp_node(self, node): """Add extra fields which only exist on interp nodes""" # ['105', '22', 'Interp'] => section header node['section_header'] = len(node['label']) == 3 is_header = lambda child: child['label'][-1] == 'Interp' node['header_children'] = list(filter(is_header, node['children'])) node['par_children'] = list(filterfalse(is_header, node['children'])) if 'header' in node: node['header_markup'] = node['header'] citation = list(takewhile(lambda p: p != 'Interp', node['label'])) icl = self.inline_applier.layers.get( InternalCitationLayer.shorthand) if icl and len(citation) > 2: text = '%s(%s)' % (citation[1], ')('.join(citation[2:])) node['header_markup'] = node['header_markup'].replace( text, icl.render_url(citation, text))
def modify_interp_node(self, node): """Add extra fields which only exist on interp nodes""" # ['105', '22', 'Interp'] => section header node['section_header'] = len(node['label']) == 3 def is_header(child): return child['label'][-1] == 'Interp' node['header_children'] = list(filter(is_header, node['children'])) node['par_children'] = list(filterfalse(is_header, node['children'])) if 'header' in node: node['header_markup'] = node['header'] citation = list(takewhile(lambda p: p != 'Interp', node['label'])) for layer in self.layers: if (isinstance(layer, InternalCitationLayer) and len(citation) > 2): text = '%s(%s)' % (citation[1], ')('.join(citation[2:])) node['header_markup'] = node['header_markup'].replace( text, layer.render_url(citation, text))
def __init__(self, filenames): ''' Constructor, takes in the list of filenames to load, who's workspaces will be iterated over. ''' # Validate. if not isinstance(filenames, list): raise TypeError("Expected a list.") if not all([self._is_string(s) for s in filenames]): raise TypeError("Expected a list of strings.") if len(filenames) < 1: raise ValueError("Expected at least one filename.") # In the general case, we may or may not have checked for the existance # of the files previously, so before we even start iterating throw if # any are missing. missing_files = list(filterfalse(os.path.exists, filenames)) if len(missing_files) > 0: raise ValueError("One or more files are missing: " + str(missing_files)) self._filenames = filenames self._loaded_ws = None
def _unique_everseen(self, iterable, key=None): """ List unique elements, preserving order. Remember all elements ever seen. _unique_everseen('AAAABBBCCDAABBB') --> A B C D _unique_everseen('ABBCcAD', str.lower) --> A B C D """ seen = set() seen_add = seen.add if key is None: for element in filterfalse(seen.__contains__, iterable): seen_add(element) yield element else: for element in iterable: k = key(element) if k not in seen: seen_add(k) yield element
def clean_trxns(self, groups): for grp, trxns in groups: _args = [trxns, self.convert_amount] # if it's split, transactions skipping is all or none if self.is_split and self.skip_transaction(trxns[0]): continue elif self.is_split and not utils.verify_splits(*_args): raise Exception('Splits do not sum to zero.') elif not self.is_split: filtered_trxns = filterfalse(self.skip_transaction, trxns) else: filtered_trxns = trxns if self.is_split: main_pos = utils.get_max_split(*_args)[0] else: main_pos = 0 keyfunc = lambda enum: enum[0] != main_pos sorted_trxns = sorted(enumerate(filtered_trxns), key=keyfunc) yield (grp, main_pos, sorted_trxns)
def find(cls, where='.', exclude=(), include=('*',)): """Return a list all Python packages found within directory 'where' 'where' should be supplied as a "cross-platform" (i.e. URL-style) path; it will be converted to the appropriate local path syntax. 'exclude' is a sequence of package names to exclude; '*' can be used as a wildcard in the names, such that 'foo.*' will exclude all subpackages of 'foo' (but not 'foo' itself). 'include' is a sequence of package names to include. If it's specified, only the named packages will be included. If it's not specified, all found packages will be included. 'include' can contain shell style wildcard patterns just like 'exclude'. The list of included packages is built up first and then any explicitly excluded packages are removed from it. """ out = cls._find_packages_iter(convert_path(where)) out = cls.require_parents(out) includes = cls._build_filter(*include) excludes = cls._build_filter('ez_setup', '*__pycache__', *exclude) out = filter(includes, out) out = filterfalse(excludes, out) return list(out)
def raises(expected_exception, *args, **kwargs): r""" Assert that a code block/function call raises ``expected_exception`` and raise a failure exception otherwise. :arg message: if specified, provides a custom failure message if the exception is not raised :arg match: if specified, asserts that the exception matches a text or regex This helper produces a ``ExceptionInfo()`` object (see below). You may use this function as a context manager:: >>> with raises(ZeroDivisionError): ... 1/0 .. versionchanged:: 2.10 In the context manager form you may use the keyword argument ``message`` to specify a custom failure message:: >>> with raises(ZeroDivisionError, message="Expecting ZeroDivisionError"): ... pass Traceback (most recent call last): ... Failed: Expecting ZeroDivisionError .. note:: When using ``pytest.raises`` as a context manager, it's worthwhile to note that normal context manager rules apply and that the exception raised *must* be the final line in the scope of the context manager. Lines of code after that, within the scope of the context manager will not be executed. For example:: >>> value = 15 >>> with raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... assert exc_info.type == ValueError # this will not execute Instead, the following approach must be taken (note the difference in scope):: >>> with raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... >>> assert exc_info.type == ValueError Since version ``3.1`` you can use the keyword argument ``match`` to assert that the exception matches a text or regex:: >>> with raises(ValueError, match='must be 0 or None'): ... raise ValueError("value must be 0 or None") >>> with raises(ValueError, match=r'must be \d+$'): ... raise ValueError("value must be 42") **Legacy forms** The forms below are fully supported but are discouraged for new code because the context manager form is regarded as more readable and less error-prone. It is possible to specify a callable by passing a to-be-called lambda:: >>> raises(ZeroDivisionError, lambda: 1/0) <ExceptionInfo ...> or you can specify an arbitrary callable with arguments:: >>> def f(x): return 1/x ... >>> raises(ZeroDivisionError, f, 0) <ExceptionInfo ...> >>> raises(ZeroDivisionError, f, x=0) <ExceptionInfo ...> It is also possible to pass a string to be evaluated at runtime:: >>> raises(ZeroDivisionError, "f(0)") <ExceptionInfo ...> The string will be evaluated using the same ``locals()`` and ``globals()`` at the moment of the ``raises`` call. .. currentmodule:: _pytest._code Consult the API of ``excinfo`` objects: :class:`ExceptionInfo`. .. note:: Similar to caught exception objects in Python, explicitly clearing local references to returned ``ExceptionInfo`` objects can help the Python interpreter speed up its garbage collection. Clearing those references breaks a reference cycle (``ExceptionInfo`` --> caught exception --> frame stack raising the exception --> current frame stack --> local variables --> ``ExceptionInfo``) which makes Python keep all objects referenced from that cycle (including all local variables in the current frame) alive until the next cyclic garbage collection run. See the official Python ``try`` statement documentation for more detailed information. """ __tracebackhide__ = True for exc in filterfalse(isclass, always_iterable(expected_exception, BASE_TYPE)): msg = ( "exceptions must be old-style classes or" " derived from BaseException, not %s" ) raise TypeError(msg % type(exc)) message = "DID NOT RAISE {}".format(expected_exception) match_expr = None if not args: if "message" in kwargs: message = kwargs.pop("message") if "match" in kwargs: match_expr = kwargs.pop("match") if kwargs: msg = "Unexpected keyword arguments passed to pytest.raises: " msg += ", ".join(kwargs.keys()) raise TypeError(msg) return RaisesContext(expected_exception, message, match_expr) elif isinstance(args[0], str): code, = args assert isinstance(code, str) frame = sys._getframe(1) loc = frame.f_locals.copy() loc.update(kwargs) # print "raises frame scope: %r" % frame.f_locals try: code = _pytest._code.Source(code).compile() py.builtin.exec_(code, frame.f_globals, loc) # XXX didn'T mean f_globals == f_locals something special? # this is destroyed here ... except expected_exception: return _pytest._code.ExceptionInfo() else: func = args[0] try: func(*args[1:], **kwargs) except expected_exception: return _pytest._code.ExceptionInfo() fail(message)
def get_certs_by_domain(self, domain_name, project_id=None, flavor_id=None, cert_type=None): LOG.info("Check if cert on '{0}' exists".format(domain_name)) args = {'domain_name': domain_name.lower()} stmt = query.SimpleStatement( CQL_SEARCH_CERT_BY_DOMAIN, consistency_level=self._driver.consistency_level) resultset = self.session.execute(stmt, args) complete_results = list(resultset) certs = [] if complete_results: for r in complete_results: r_project_id = str(r.get('project_id')) r_flavor_id = str(r.get('flavor_id')) r_cert_type = str(r.get('cert_type')) r_cert_details = {} # in case cert_details is None cert_details = r.get('cert_details', {}) or {} # Need to convert cassandra dict into real dict # And the value of cert_details is a string dict for key in cert_details: r_cert_details[key] = json.loads(cert_details[key]) LOG.info("Certificate for domain: {0} with flavor_id: {1}, " "cert_details : {2} and cert_type: {3} present " "on project_id: {4}".format(domain_name, r_flavor_id, r_cert_details, r_cert_type, r_project_id)) ssl_cert = ssl_certificate.SSLCertificate( domain_name=domain_name, flavor_id=r_flavor_id, cert_details=r_cert_details, cert_type=r_cert_type, project_id=r_project_id) certs.append(ssl_cert) non_none_attrs_gen = filterfalse(lambda x: list(x.values())[0] is None, [{ 'project_id': project_id }, { 'flavor_id': flavor_id }, { 'cert_type': cert_type }]) non_none_attrs_list = list(non_none_attrs_gen) non_none_attrs_dict = {} if non_none_attrs_list: for attr in non_none_attrs_list: non_none_attrs_dict.update(attr) def argfilter(certificate): all_conditions = True if non_none_attrs_dict: for k, v in non_none_attrs_dict.items(): if getattr(certificate, k) != v: all_conditions = False return all_conditions total_certs = [cert for cert in certs if argfilter(cert)] if len(total_certs) == 1: return total_certs[0] else: return total_certs
def parse_input(self, X): """Parse and create features for the NSPD kernel. Parameters ---------- X : iterable For the input to pass the test, we must have: Each element must be an iterable with at most three features and at least one. The first that is obligatory is a valid graph structure (adjacency matrix or edge_dictionary) while the second is node_labels and the third edge_labels (that correspond to the given graph format). A valid input also consists of graph type objects. Returns ------- M : dict A dictionary with keys all the distances from 0 to self.d and values the the np.arrays with rows corresponding to the non-null input graphs and columns to the enumerations of tuples consisting of pairs of hash values and radius, from all the given graphs of the input (plus the fitted one's on transform). """ if not isinstance(X, collections.Iterable): raise TypeError('input must be an iterable\n') else: # Hold the number of graphs ng = 0 # Holds all the data for combinations of r, d data = collections.defaultdict(dict) # Index all keys for combinations of r, d all_keys = collections.defaultdict(dict) for (idx, x) in enumerate(iter(X)): is_iter = False if isinstance(x, collections.Iterable): is_iter, x = True, list(x) if is_iter and len(x) in [0, 3]: if len(x) == 0: warnings.warn('Ignoring empty element' + ' on index: ' + str(idx)) continue else: g = Graph(x[0], x[1], x[2]) g.change_format("adjacency") elif type(x) is Graph: g = Graph( x.get_adjacency_matrix(), x.get_labels(purpose="adjacency", label_type="vertex"), x.get_labels(purpose="adjacency", label_type="edge")) else: raise TypeError('each element of X must have either ' + 'a graph with labels for node and edge ' + 'or 3 elements consisting of a graph ' + 'type object, labels for vertices and ' + 'labels for edges.') # Bring to the desired format g.change_format(self._graph_format) # Take the vertices vertices = set(g.get_vertices(purpose=self._graph_format)) # Extract the dicitionary ed = g.get_edge_dictionary() # Convert edges to tuples edges = {(j, k) for j in ed.keys() for k in ed[j].keys()} # Extract labels for nodes Lv = g.get_labels(purpose=self._graph_format) # and for edges Le = g.get_labels(purpose=self._graph_format, label_type="edge") # Produce all the neighborhoods and the distance pairs # up to the desired radius and maximum distance N, D, D_pair = g.produce_neighborhoods(self.r, purpose="dictionary", with_distances=True, d=self.d) # Hash all the neighborhoods H = self._hash_neighborhoods(vertices, edges, Lv, Le, N, D_pair) if self._method_calling == 1: for d in filterfalse(lambda x: x not in D, range(self.d + 1)): for (A, B) in D[d]: for r in range(self.r + 1): key = (H[r, A], H[r, B]) keys = all_keys[r, d] idx = keys.get(key, None) if idx is None: idx = len(keys) keys[key] = idx data[r, d][ng, idx] = data[r, d].get( (ng, idx), 0) + 1 elif self._method_calling == 3: for d in filterfalse(lambda x: x not in D, range(self.d + 1)): for (A, B) in D[d]: # Based on the edges of the bidirected graph for r in range(self.r + 1): keys = all_keys[r, d] fit_keys = self._fit_keys[r, d] key = (H[r, A], H[r, B]) idx = fit_keys.get(key, None) if idx is None: idx = keys.get(key, None) if idx is None: idx = len(keys) + len(fit_keys) keys[key] = idx data[r, d][ng, idx] = data[r, d].get( (ng, idx), 0) + 1 ng += 1 if ng == 0: raise ValueError('parsed input is empty') if self._method_calling == 1: # A feature matrix for all levels M = dict() for (key, d) in filterfalse(lambda a: len(a[1]) == 0, iteritems(data)): indexes, data = zip(*iteritems(d)) rows, cols = zip(*indexes) M[key] = csr_matrix((data, (rows, cols)), shape=(ng, len(all_keys[key])), dtype=np.int64) self._fit_keys = all_keys self._ngx = ng elif self._method_calling == 3: # A feature matrix for all levels M = dict() for (key, d) in filterfalse(lambda a: len(a[1]) == 0, iteritems(data)): indexes, data = zip(*iteritems(d)) rows, cols = zip(*indexes) M[key] = csr_matrix( (data, (rows, cols)), shape=(ng, len(all_keys[key]) + len(self._fit_keys[key])), dtype=np.int64) self._ngy = ng return M
def raises(expected_exception, *args, **kwargs): r""" Assert that a code block/function call raises ``expected_exception`` or raise a failure exception otherwise. :kwparam match: if specified, asserts that the exception matches a text or regex :kwparam message: **(deprecated since 4.1)** if specified, provides a custom failure message if the exception is not raised .. currentmodule:: _pytest._code Use ``pytest.raises`` as a context manager, which will capture the exception of the given type:: >>> with raises(ZeroDivisionError): ... 1/0 If the code block does not raise the expected exception (``ZeroDivisionError`` in the example above), or no exception at all, the check will fail instead. You can also use the keyword argument ``match`` to assert that the exception matches a text or regex:: >>> with raises(ValueError, match='must be 0 or None'): ... raise ValueError("value must be 0 or None") >>> with raises(ValueError, match=r'must be \d+$'): ... raise ValueError("value must be 42") The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the details of the captured exception:: >>> with raises(ValueError) as exc_info: ... raise ValueError("value must be 42") >>> assert exc_info.type is ValueError >>> assert exc_info.value.args[0] == "value must be 42" .. deprecated:: 4.1 In the context manager form you may use the keyword argument ``message`` to specify a custom failure message that will be displayed in case the ``pytest.raises`` check fails. This has been deprecated as it is considered error prone as users often mean to use ``match`` instead. .. note:: When using ``pytest.raises`` as a context manager, it's worthwhile to note that normal context manager rules apply and that the exception raised *must* be the final line in the scope of the context manager. Lines of code after that, within the scope of the context manager will not be executed. For example:: >>> value = 15 >>> with raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... assert exc_info.type is ValueError # this will not execute Instead, the following approach must be taken (note the difference in scope):: >>> with raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... >>> assert exc_info.type is ValueError **Using with** ``pytest.mark.parametrize`` When using :ref:`pytest.mark.parametrize ref` it is possible to parametrize tests such that some runs raise an exception and others do not. See :ref:`parametrizing_conditional_raising` for an example. **Legacy form** It is possible to specify a callable by passing a to-be-called lambda:: >>> raises(ZeroDivisionError, lambda: 1/0) <ExceptionInfo ...> or you can specify an arbitrary callable with arguments:: >>> def f(x): return 1/x ... >>> raises(ZeroDivisionError, f, 0) <ExceptionInfo ...> >>> raises(ZeroDivisionError, f, x=0) <ExceptionInfo ...> The form above is fully supported but discouraged for new code because the context manager form is regarded as more readable and less error-prone. .. note:: Similar to caught exception objects in Python, explicitly clearing local references to returned ``ExceptionInfo`` objects can help the Python interpreter speed up its garbage collection. Clearing those references breaks a reference cycle (``ExceptionInfo`` --> caught exception --> frame stack raising the exception --> current frame stack --> local variables --> ``ExceptionInfo``) which makes Python keep all objects referenced from that cycle (including all local variables in the current frame) alive until the next cyclic garbage collection run. See the official Python ``try`` statement documentation for more detailed information. """ __tracebackhide__ = True for exc in filterfalse(isclass, always_iterable(expected_exception, BASE_TYPE)): msg = ( "exceptions must be old-style classes or" " derived from BaseException, not %s" ) raise TypeError(msg % type(exc)) message = "DID NOT RAISE {}".format(expected_exception) match_expr = None if not args: if "message" in kwargs: message = kwargs.pop("message") warnings.warn(deprecated.RAISES_MESSAGE_PARAMETER, stacklevel=2) if "match" in kwargs: match_expr = kwargs.pop("match") if kwargs: msg = "Unexpected keyword arguments passed to pytest.raises: " msg += ", ".join(kwargs.keys()) raise TypeError(msg) return RaisesContext(expected_exception, message, match_expr) elif isinstance(args[0], str): warnings.warn(deprecated.RAISES_EXEC, stacklevel=2) code, = args assert isinstance(code, str) frame = sys._getframe(1) loc = frame.f_locals.copy() loc.update(kwargs) # print "raises frame scope: %r" % frame.f_locals try: code = _pytest._code.Source(code).compile(_genframe=frame) six.exec_(code, frame.f_globals, loc) # XXX didn't mean f_globals == f_locals something special? # this is destroyed here ... except expected_exception: return _pytest._code.ExceptionInfo.from_current() else: func = args[0] try: func(*args[1:], **kwargs) except expected_exception: return _pytest._code.ExceptionInfo.from_current() fail(message)
def draw_plot(self, stats, title, points=None, show_labels=True, colormap='jet', contours=None, xyz=None, extent=None, y_marks=None, markerfacecolor="white", markeredgecolor="black", show_filling=True, markersize=5, point_style='bo'): # points = [(x,y,label, isoutlier(bool)), (), ...] import matplotlib.cm self._points = [] self._xyz = [] cm = getattr(matplotlib.cm, colormap) self.plot.clear() if (extent is None): extent = self.extent else: assert (len(extent) == 4) if show_filling: im = self.plot.imshow(stats, origin="lower", cmap=cm, extent=extent) # This code will draw bar with actual numbers that are represented by # color (sloppily over the axis values) # from mpl_toolkits.axes_grid1 import make_axes_locatable # divider = make_axes_locatable(self.plot) # cax = divider.append_axes('bottom', size='5%', pad=0.05) # self.figure.colorbar(im, cax=cax, orientation='horizontal') if (contours is not None): self.plot.contour(stats, contours, origin="lower", colors='k', extent=extent, zorder=9) if (y_marks is None): self.set_labels() else: self.set_labels(y_marks=y_marks) self.plot.set_title(title) if points is not None: if xyz is not None: assert (len(xyz) == len(points)) out = list(filter(lambda x: x[3], points)) out_columns = list(zip(*out)) # ^^^^ is doing e.g. this: # >>> l = [(1,2), (3,4), (8,9)] # >>> zip(*l) # [(1, 3, 8), (2, 4, 9)] non_out = list(filterfalse(lambda x: x[3], points)) non_out_columns = list(zip(*non_out)) if len(out) > 0: self.plot.plot(tuple(out_columns[0]), tuple(out_columns[1]), point_style, markerfacecolor='red', markersize=markersize, markeredgecolor=markeredgecolor) if show_labels: for x, y, label, _ in out: self.plot.text(x, y, label, color='black') if len(non_out) > 0: # make non-outliers smaller and translucent if there are many points if len(non_out) > 2500: markersize = 3 self.plot.plot(non_out_columns[0], non_out_columns[1], point_style, markerfacecolor=markerfacecolor, markersize=markersize, markeredgecolor=markeredgecolor) self.canvas.draw()
def test_filter_false(): from six.moves import filterfalse f = filterfalse(lambda x: x % 3, range(10)) assert six.advance_iterator(f) == 0 assert six.advance_iterator(f) == 3 assert six.advance_iterator(f) == 6
def partition(predicate, iterable): t1, t2 = tee(iterable) return list(filter(predicate, t2)), list(filterfalse(predicate, t1))
def raises(expected_exception, *args, **kwargs): r""" Assert that a code block/function call raises ``expected_exception`` or raise a failure exception otherwise. :kwparam match: if specified, asserts that the exception matches a text or regex :kwparam message: **(deprecated since 4.1)** if specified, provides a custom failure message if the exception is not raised .. currentmodule:: _pytest._code Use ``pytest.raises`` as a context manager, which will capture the exception of the given type:: >>> with raises(ZeroDivisionError): ... 1/0 If the code block does not raise the expected exception (``ZeroDivisionError`` in the example above), or no exception at all, the check will fail instead. You can also use the keyword argument ``match`` to assert that the exception matches a text or regex:: >>> with raises(ValueError, match='must be 0 or None'): ... raise ValueError("value must be 0 or None") >>> with raises(ValueError, match=r'must be \d+$'): ... raise ValueError("value must be 42") The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the details of the captured exception:: >>> with raises(ValueError) as exc_info: ... raise ValueError("value must be 42") >>> assert exc_info.type is ValueError >>> assert exc_info.value.args[0] == "value must be 42" .. deprecated:: 4.1 In the context manager form you may use the keyword argument ``message`` to specify a custom failure message that will be displayed in case the ``pytest.raises`` check fails. This has been deprecated as it is considered error prone as users often mean to use ``match`` instead. .. note:: When using ``pytest.raises`` as a context manager, it's worthwhile to note that normal context manager rules apply and that the exception raised *must* be the final line in the scope of the context manager. Lines of code after that, within the scope of the context manager will not be executed. For example:: >>> value = 15 >>> with raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... assert exc_info.type is ValueError # this will not execute Instead, the following approach must be taken (note the difference in scope):: >>> with raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... >>> assert exc_info.type is ValueError **Legacy form** It is possible to specify a callable by passing a to-be-called lambda:: >>> raises(ZeroDivisionError, lambda: 1/0) <ExceptionInfo ...> or you can specify an arbitrary callable with arguments:: >>> def f(x): return 1/x ... >>> raises(ZeroDivisionError, f, 0) <ExceptionInfo ...> >>> raises(ZeroDivisionError, f, x=0) <ExceptionInfo ...> The form above is fully supported but discouraged for new code because the context manager form is regarded as more readable and less error-prone. .. note:: Similar to caught exception objects in Python, explicitly clearing local references to returned ``ExceptionInfo`` objects can help the Python interpreter speed up its garbage collection. Clearing those references breaks a reference cycle (``ExceptionInfo`` --> caught exception --> frame stack raising the exception --> current frame stack --> local variables --> ``ExceptionInfo``) which makes Python keep all objects referenced from that cycle (including all local variables in the current frame) alive until the next cyclic garbage collection run. See the official Python ``try`` statement documentation for more detailed information. """ __tracebackhide__ = True for exc in filterfalse(isclass, always_iterable(expected_exception, BASE_TYPE)): msg = ("exceptions must be old-style classes or" " derived from BaseException, not %s") raise TypeError(msg % type(exc)) message = "DID NOT RAISE {}".format(expected_exception) match_expr = None if not args: if "message" in kwargs: message = kwargs.pop("message") warnings.warn(deprecated.RAISES_MESSAGE_PARAMETER, stacklevel=2) if "match" in kwargs: match_expr = kwargs.pop("match") if kwargs: msg = "Unexpected keyword arguments passed to pytest.raises: " msg += ", ".join(kwargs.keys()) raise TypeError(msg) return RaisesContext(expected_exception, message, match_expr) elif isinstance(args[0], str): warnings.warn(deprecated.RAISES_EXEC, stacklevel=2) code, = args assert isinstance(code, str) frame = sys._getframe(1) loc = frame.f_locals.copy() loc.update(kwargs) # print "raises frame scope: %r" % frame.f_locals try: code = _pytest._code.Source(code).compile(_genframe=frame) six.exec_(code, frame.f_globals, loc) # XXX didn't mean f_globals == f_locals something special? # this is destroyed here ... except expected_exception: return _pytest._code.ExceptionInfo.from_current() else: func = args[0] try: func(*args[1:], **kwargs) except expected_exception: return _pytest._code.ExceptionInfo.from_current() fail(message)
def get_certs_by_domain(self, domain_name, project_id=None, flavor_id=None, cert_type=None): LOG.info("Check if cert on '{0}' exists".format(domain_name)) args = { 'domain_name': domain_name.lower() } stmt = query.SimpleStatement( CQL_SEARCH_CERT_BY_DOMAIN, consistency_level=self._driver.consistency_level) resultset = self.session.execute(stmt, args) complete_results = list(resultset) certs = [] if complete_results: for r in complete_results: r_project_id = str(r.get('project_id')) r_flavor_id = str(r.get('flavor_id')) r_cert_type = str(r.get('cert_type')) r_cert_details = {} # in case cert_details is None cert_details = r.get('cert_details', {}) or {} # Need to convert cassandra dict into real dict # And the value of cert_details is a string dict for key in cert_details: r_cert_details[key] = json.loads(cert_details[key]) LOG.info( "Certificate for domain: {0} with flavor_id: {1}, " "cert_details : {2} and cert_type: {3} present " "on project_id: {4}".format( domain_name, r_flavor_id, r_cert_details, r_cert_type, r_project_id ) ) ssl_cert = ssl_certificate.SSLCertificate( domain_name=domain_name, flavor_id=r_flavor_id, cert_details=r_cert_details, cert_type=r_cert_type, project_id=r_project_id ) certs.append(ssl_cert) non_none_attrs_gen = filterfalse( lambda x: list(x.values())[0] is None, [{'project_id': project_id}, {'flavor_id': flavor_id}, {'cert_type': cert_type}]) non_none_attrs_list = list(non_none_attrs_gen) non_none_attrs_dict = {} if non_none_attrs_list: for attr in non_none_attrs_list: non_none_attrs_dict.update(attr) def argfilter(certificate): all_conditions = True if non_none_attrs_dict: for k, v in non_none_attrs_dict.items(): if getattr(certificate, k) != v: all_conditions = False return all_conditions total_certs = [cert for cert in certs if argfilter(cert)] if len(total_certs) == 1: return total_certs[0] else: return total_certs
def raises(expected_exception, *args, **kwargs): r""" Assert that a code block/function call raises ``expected_exception`` and raise a failure exception otherwise. :arg message: if specified, provides a custom failure message if the exception is not raised :arg match: if specified, asserts that the exception matches a text or regex This helper produces a ``ExceptionInfo()`` object (see below). You may use this function as a context manager:: >>> with raises(ZeroDivisionError): ... 1/0 .. versionchanged:: 2.10 In the context manager form you may use the keyword argument ``message`` to specify a custom failure message:: >>> with raises(ZeroDivisionError, message="Expecting ZeroDivisionError"): ... pass Traceback (most recent call last): ... Failed: Expecting ZeroDivisionError .. note:: When using ``pytest.raises`` as a context manager, it's worthwhile to note that normal context manager rules apply and that the exception raised *must* be the final line in the scope of the context manager. Lines of code after that, within the scope of the context manager will not be executed. For example:: >>> value = 15 >>> with raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... assert exc_info.type == ValueError # this will not execute Instead, the following approach must be taken (note the difference in scope):: >>> with raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... >>> assert exc_info.type == ValueError Since version ``3.1`` you can use the keyword argument ``match`` to assert that the exception matches a text or regex:: >>> with raises(ValueError, match='must be 0 or None'): ... raise ValueError("value must be 0 or None") >>> with raises(ValueError, match=r'must be \d+$'): ... raise ValueError("value must be 42") **Legacy forms** The forms below are fully supported but are discouraged for new code because the context manager form is regarded as more readable and less error-prone. It is possible to specify a callable by passing a to-be-called lambda:: >>> raises(ZeroDivisionError, lambda: 1/0) <ExceptionInfo ...> or you can specify an arbitrary callable with arguments:: >>> def f(x): return 1/x ... >>> raises(ZeroDivisionError, f, 0) <ExceptionInfo ...> >>> raises(ZeroDivisionError, f, x=0) <ExceptionInfo ...> It is also possible to pass a string to be evaluated at runtime:: >>> raises(ZeroDivisionError, "f(0)") <ExceptionInfo ...> The string will be evaluated using the same ``locals()`` and ``globals()`` at the moment of the ``raises`` call. .. currentmodule:: _pytest._code Consult the API of ``excinfo`` objects: :class:`ExceptionInfo`. .. note:: Similar to caught exception objects in Python, explicitly clearing local references to returned ``ExceptionInfo`` objects can help the Python interpreter speed up its garbage collection. Clearing those references breaks a reference cycle (``ExceptionInfo`` --> caught exception --> frame stack raising the exception --> current frame stack --> local variables --> ``ExceptionInfo``) which makes Python keep all objects referenced from that cycle (including all local variables in the current frame) alive until the next cyclic garbage collection run. See the official Python ``try`` statement documentation for more detailed information. """ __tracebackhide__ = True for exc in filterfalse(isclass, always_iterable(expected_exception, BASE_TYPE)): msg = ("exceptions must be old-style classes or" " derived from BaseException, not %s") raise TypeError(msg % type(exc)) message = "DID NOT RAISE {}".format(expected_exception) match_expr = None if not args: if "message" in kwargs: message = kwargs.pop("message") if "match" in kwargs: match_expr = kwargs.pop("match") if kwargs: msg = "Unexpected keyword arguments passed to pytest.raises: " msg += ", ".join(kwargs.keys()) raise TypeError(msg) return RaisesContext(expected_exception, message, match_expr) elif isinstance(args[0], str): code, = args assert isinstance(code, str) frame = sys._getframe(1) loc = frame.f_locals.copy() loc.update(kwargs) # print "raises frame scope: %r" % frame.f_locals try: code = _pytest._code.Source(code).compile() py.builtin.exec_(code, frame.f_globals, loc) # XXX didn'T mean f_globals == f_locals something special? # this is destroyed here ... except expected_exception: return _pytest._code.ExceptionInfo() else: func = args[0] try: func(*args[1:], **kwargs) except expected_exception: return _pytest._code.ExceptionInfo() fail(message)