def test_excepts(): # These are descriptors, make sure this works correctly. assert excepts.__name__ == 'excepts' assert ( 'A wrapper around a function to catch exceptions and\n' ' dispatch to a handler.\n' ) in excepts.__doc__ def idx(a): """idx docstring """ return [1, 2].index(a) def handler(e): """handler docstring """ assert isinstance(e, ValueError) return -1 excepting = excepts(ValueError, idx, handler) assert excepting(1) == 0 assert excepting(2) == 1 assert excepting(3) == -1 assert excepting.__name__ == 'idx_excepting_ValueError' assert 'idx docstring' in excepting.__doc__ assert 'ValueError' in excepting.__doc__ assert 'handler docstring' in excepting.__doc__ def getzero(a): """getzero docstring """ return a[0] excepting = excepts((IndexError, KeyError), getzero) assert excepting([]) is None assert excepting([1]) == 1 assert excepting({}) is None assert excepting({0: 1}) == 1 assert excepting.__name__ == 'getzero_excepting_IndexError_or_KeyError' assert 'getzero docstring' in excepting.__doc__ assert 'return_none' in excepting.__doc__ assert 'Returns None' in excepting.__doc__ def raise_(a): """A function that raises an instance of the exception type given. """ raise a() excepting = excepts((ValueError, KeyError), raise_) assert excepting(ValueError) is None assert excepting(KeyError) is None assert raises(TypeError, lambda: excepting(TypeError)) assert raises(NotImplementedError, lambda: excepting(NotImplementedError)) excepting = excepts(object(), object(), object()) assert excepting.__name__ == 'excepting' assert excepting.__doc__ == excepts.__doc__
def _get_yaml_list_comments(value): items = value.ca.items raw_comment_lines = tuple(excepts((AttributeError, KeyError, TypeError), lambda q: items.get(q)[0].value.strip() or None, lambda _: None # default value on exception )(q) for q in range(len(value))) return raw_comment_lines
def _get_yaml_map_comments(rawvalue): return dict(( key, excepts( KeyError, lambda k: rawvalue.ca.items[k][2].value.strip() or None, lambda _: None # default value on exception )(key)) for key in rawvalue)
def base_url(self): _path = excepts(AttributeError, lambda: self._path.lstrip('/'))() if self._token: _path = self._token + '/' + _path if self._netloc in Channel._old_channel_alias_netloc: ca = Channel(context.channel_alias) return urlunparse((ca._scheme, ca._netloc, _path, None, None, None)) else: return urlunparse((self._scheme, self._netloc, _path, None, None, None))
def base_url(self): _path = excepts(AttributeError, lambda: self._path.lstrip("/"))() if self._token: _path = self._token + "/" + _path if self._netloc in Channel._old_channel_alias_netloc: ca = Channel(context.channel_alias) return urlunparse((ca._scheme, ca._netloc, _path, None, None, None)) else: return urlunparse((self._scheme, self._netloc, _path, None, None, None))
def middleware(method, params): if method in method_handlers: exc_type, handler = method_handlers[method] return excepts( exc_type, make_request, handler, )(method, params) else: return make_request(method, params)
def _revert_pending_transaction_filter(self, filter): is_valid_transaction_hash = excepts( (TransactionNotFound,), compose( bool, self.get_transaction_by_hash, self.normalizer.normalize_outbound_transaction_hash, ), lambda v: False, ) values_to_remove = remove(is_valid_transaction_hash, filter.get_all()) filter.remove(*values_to_remove)
def _revert_block_filter(self, filter): is_valid_block_hash = excepts( (BlockNotFound,), compose( bool, self.get_block_by_hash, self.normalizer.normalize_outbound_block_hash, ), lambda v: False, ) values_to_remove = tuple(remove(is_valid_block_hash, filter.get_all())) filter.remove(*values_to_remove)
def _get_yaml_map_comments(rawvalue): return dict((key, excepts(KeyError, lambda k: rawvalue.ca.items[k][2].value.strip() or None, lambda _: None # default value on exception )(key)) for key in rawvalue)
def null_if_excepts(exc_type, fn): return excepts( exc_type, fn, static_return(None), )
not_implemented, 'compileSolidity': not_implemented, 'compileSerpent': not_implemented, 'newFilter': create_log_filter, 'newBlockFilter': call_eth_tester('create_block_filter'), 'newPendingTransactionFilter': call_eth_tester('create_pending_transaction_filter'), 'uninstallFilter': excepts( FilterNotFound, compose( is_null, call_eth_tester('delete_filter'), ), static_return(False), ), 'getFilterChanges': null_if_filter_not_found(call_eth_tester('get_only_filter_changes')), 'getFilterLogs': null_if_filter_not_found(call_eth_tester('get_all_filter_logs')), 'getLogs': not_implemented, 'getWork': not_implemented, 'submitWork': not_implemented, 'submitHashrate': not_implemented,
def exact_value(self): return excepts(ValueError, int(self.raw_value))
), call_eth_tester('get_transaction_receipt'), )), 'getUncleByBlockHashAndIndex': not_implemented, 'getUncleByBlockNumberAndIndex': not_implemented, 'getCompilers': not_implemented, 'compileLLL': not_implemented, 'compileSolidity': not_implemented, 'compileSerpent': not_implemented, 'newFilter': create_log_filter, 'newBlockFilter': call_eth_tester('create_block_filter'), 'newPendingTransactionFilter': call_eth_tester('create_pending_transaction_filter'), 'uninstallFilter': excepts( FilterNotFound, compose( is_null, call_eth_tester('delete_filter'), ), static_return(False), ), 'getFilterChanges': null_if_filter_not_found(call_eth_tester('get_only_filter_changes')), 'getFilterLogs': null_if_filter_not_found(call_eth_tester('get_all_filter_logs')), 'getLogs': get_logs, 'getWork': not_implemented, 'submitWork': not_implemented, 'submitHashrate': not_implemented, }, 'db': { 'putString': not_implemented, 'getString': not_implemented, 'putHex': not_implemented, 'getHex': not_implemented,
positive_scores_non_persona + negative_scores_non_persona, ) ) print("Constructing persona graph.") PG = persona_graph(G_original) print("Constructing lookups.") forward_persona, reverse_persona = lookup_tables(PG) forward, reverse = lookup_tables(G) groups = groupby(operator.attrgetter("node"), PG.nodes()) positive_scores_persona = [ excepts(ValueError, max, lambda _: 0.0)( iter_get_scores_networkx(groups, node1, node2, PG, jaccard_coefficient) ) for (node1, node2) in positive_samples ] negative_scores_persona = [ excepts(ValueError, max, lambda _: 0.0)( iter_get_scores_networkx(groups, node1, node2, PG, jaccard_coefficient) ) for (node1, node2) in negative_samples ] print(sum(positive_scores_persona)) print(sum(negative_scores_persona)) print( roc_auc_score( [1] * len(positive_samples) + [0] * len(negative_samples),