def fold_values(values, factor): group = OrderedDict() by_factor = lambda i: i[0] & factor for ns, items in itertools.groupby(values, key=by_factor): namespace = group.setdefault(ns, []) namespace.extend([[i[0] - ns, i[1] - ns] for i in items]) return group
def fold_values(values, factor): group = OrderedDict() by_factor = lambda i: i[0] & factor for ns, items in itertools.groupby(values, key=by_factor): namespace = group.setdefault(ns, []) namespace.extend([[i[0] - ns, i[1] - ns] for i in items]) return group
def new_tree(): tree = OrderedDict() for codepoint in hrange(0, sys.maxunicode + 1): cat = unicodedata.category(hunichr(codepoint)) target = tree.setdefault(cat, []) if target and codepoint == target[-1][-1] + 1: target[-1][-1] += 1 else: target.append([codepoint, codepoint]) return tree
def new_tree(): tree = OrderedDict() for codepoint in hrange(0, sys.maxunicode + 1): cat = unicodedata.category(hunichr(codepoint)) target = tree.setdefault(cat, []) if target and codepoint == target[-1][-1] + 1: target[-1][-1] += 1 else: target.append([codepoint, codepoint]) return tree
def test_ordered_dictionaries_preserve_keys(): r = Random() keys = list(range(100)) r.shuffle(keys) x = fixed_dictionaries(OrderedDict([(k, booleans()) for k in keys])).example() assert list(x.keys()) == keys
def do_filter_tree_by_codepoints(tree, min_codepoint, max_codepoint, acc, base): for key, value in tree.items(): this_base = base + key if this_base > max_codepoint: continue if isinstance(value, dict): subtree = do_filter_tree_by_codepoints(value, min_codepoint, max_codepoint, OrderedDict(), this_base) if subtree: acc[key] = subtree else: filtered_items = [] for item in value: if item[0] + this_base > max_codepoint: continue if item[1] + this_base < min_codepoint: continue if item[0] + this_base < min_codepoint: item = (min_codepoint - this_base, item[1]) if item[1] + this_base > max_codepoint: item = (item[0], max_codepoint - this_base) filtered_items.append(item) if filtered_items: acc[key] = tuple(filtered_items) return acc
def just_draw_columns(draw): index = draw(index_strategy) local_index_strategy = st.just(index) data = OrderedDict((c.name, None) for c in rewritten_columns) # Depending on how the columns are going to be generated we group # them differently to get better shrinking. For columns with fill # enabled, the elements can be shrunk independently of the size, # so we can just shrink by shrinking the index then shrinking the # length and are generally much more free to move data around. # For columns with no filling the problem is harder, and drawing # them like that would result in rows being very far apart from # each other in the underlying data stream, which gets in the way # of shrinking. So what we do is reorder and draw those columns # row wise, so that the values of each row are next to each other. # This makes life easier for the shrinker when deleting blocks of # data. columns_without_fill = [ c for c in rewritten_columns if c.fill.is_empty ] if columns_without_fill: for c in columns_without_fill: data[c.name] = pandas.Series(np.zeros(shape=len(index), dtype=c.dtype), index=index) seen = { c.name: set() for c in columns_without_fill if c.unique } for i in hrange(len(index)): for c in columns_without_fill: if c.unique: for _ in range(5): value = draw(c.elements) if value not in seen[c.name]: seen[c.name].add(value) break else: reject() else: value = draw(c.elements) data[c.name][i] = value for c in rewritten_columns: if not c.fill.is_empty: data[c.name] = draw( series( index=local_index_strategy, dtype=c.dtype, elements=c.elements, fill=c.fill, unique=c.unique, )) return pandas.DataFrame(data, index=index)
def test_collections_ordereddict(): # Create OrderedDict with cycle a = OrderedDict() a['key'] = a cases = [ (OrderedDict(), 'OrderedDict()'), (OrderedDict( (i, i) for i in range(1000, 1010)), 'OrderedDict([(1000, 1000),\n' ' (1001, 1001),\n' ' (1002, 1002),\n' ' (1003, 1003),\n' ' (1004, 1004),\n' ' (1005, 1005),\n' ' (1006, 1006),\n' ' (1007, 1007),\n' ' (1008, 1008),\n' ' (1009, 1009)])'), (a, "OrderedDict([('key', OrderedDict(...))])"), ] for obj, expected in cases: assert_equal(pretty.pretty(obj), expected)
def do_filter_tree(tree, categories, blacklist_characters, min_codepoint, max_codepoint): new_tree = OrderedDict() for key, subtree in tree.items(): if key not in categories: continue subtree = do_filter_tree_by_codepoints(subtree, min_codepoint, max_codepoint, OrderedDict(), 0) if not subtree: continue subtree = do_filter_tree_by_characters(subtree, sorted(blacklist_characters), OrderedDict(), 0) if not subtree: continue new_tree[key] = subtree return new_tree
def do_filter_tree_by_characters(tree, blacklist_characters, acc, base): if not blacklist_characters: return tree for key, value in tree.items(): this_base = base + key index = bisect.bisect(blacklist_characters, hunichr(this_base)) characters = blacklist_characters[index - 1 if index else 0:] if isinstance(value, dict): subtree = do_filter_tree_by_characters(value, characters, OrderedDict(), this_base) if subtree: acc[key] = subtree else: filtered_value = value for character in characters: codepoint = ord(character) value_acc = [] for item in filtered_value: locp, hicp = item[0] + this_base, item[1] + this_base if locp == codepoint == hicp: continue elif not (locp <= codepoint <= hicp): value_acc.append(item) elif locp == codepoint: item = (codepoint + 1 - this_base, item[1]) value_acc.append(item) elif hicp == codepoint: item = (item[0], codepoint - 1 - this_base) value_acc.append(item) else: value_acc.append((item[0], codepoint - 1 - this_base)) value_acc.append((codepoint + 1 - this_base, item[1])) filtered_value = value_acc if filtered_value: acc[key] = tuple(filtered_value) return acc
profile = config.getoption(LOAD_PROFILE_OPTION) if profile: settings.load_profile(profile) seed = config.getoption(SEED_OPTION) if seed is not None: try: seed = int(seed) except ValueError: pass core.global_force_seed = seed config.addinivalue_line( 'markers', 'hypothesis: Tests which use hypothesis.') gathered_statistics = OrderedDict() @pytest.mark.hookwrapper def pytest_runtest_call(item): if not (hasattr(item, 'obj') and is_hypothesis_test(item.obj)): yield else: store = StoringReporter(item.config) def note_statistics(stats): gathered_statistics[item.nodeid] = stats with collector.with_value(note_statistics): with with_reporter(store): yield
# register_profile creates a new profile, exactly like the current one, # with the extra values given (in this case 'verbosity') settings.register_profile(profile_name, verbosity=verbosity_value) settings.load_profile(profile_name) seed = config.getoption(SEED_OPTION) if seed is not None: try: seed = int(seed) except ValueError: pass core.global_force_seed = seed config.addinivalue_line("markers", "hypothesis: Tests which use hypothesis.") gathered_statistics = OrderedDict() # type: dict @pytest.hookimpl(hookwrapper=True) def pytest_runtest_call(item): if not (hasattr(item, "obj") and is_hypothesis_test(item.obj)): yield else: store = StoringReporter(item.config) def note_statistics(stats): lines = [item.nodeid + ":", ""] + stats.get_description() + [""] gathered_statistics[item.nodeid] = lines item.hypothesis_statistics = lines with collector.with_value(note_statistics):
def assign_rows(draw): index = draw(index_strategy) result = pandas.DataFrame( OrderedDict(( c.name, pandas.Series(np.zeros(dtype=c.dtype, shape=len(index)), dtype=c.dtype), ) for c in rewritten_columns), index=index, ) fills = {} any_unique = any(c.unique for c in rewritten_columns) if any_unique: all_seen = [ set() if c.unique else None for c in rewritten_columns ] while all_seen[-1] is None: all_seen.pop() for row_index in hrange(len(index)): for _ in hrange(5): original_row = draw(rows) row = original_row if isinstance(row, dict): as_list = [None] * len(rewritten_columns) for i, c in enumerate(rewritten_columns): try: as_list[i] = row[c.name] except KeyError: try: as_list[i] = fills[i] except KeyError: fills[i] = draw(c.fill) as_list[i] = fills[i] for k in row: if k not in column_names: raise InvalidArgument( "Row %r contains column %r not in columns %r)" % (row, k, [c.name for c in rewritten_columns])) row = as_list if any_unique: has_duplicate = False for seen, value in zip(all_seen, row): if seen is None: continue if value in seen: has_duplicate = True break seen.add(value) if has_duplicate: continue row = list(try_convert(tuple, row, "draw(rows)")) if len(row) > len(rewritten_columns): raise InvalidArgument( ("Row %r contains too many entries. Has %d but " "expected at most %d") % (original_row, len(row), len(rewritten_columns))) while len(row) < len(rewritten_columns): row.append(draw(rewritten_columns[len(row)].fill)) result.iloc[row_index] = row break else: reject() return result
from hypothesis.errors import InvalidArgument from hypothesis._settings import hypothesis_home_dir from hypothesis.internal.compat import hrange, hunichr, OrderedDict __all__ = ( 'ascii_tree', 'unicode_tree', 'categories', 'category_by_codepoint', 'codepoints', 'codepoints_for_category', 'filter_tree', 'random_codepoint', ) ASCII_TREE = OrderedDict() UNICODE_TREE = OrderedDict() def ascii_tree(): """Returns tree for ASCII characters.""" global ASCII_TREE if not ASCII_TREE: ASCII_TREE = filter_tree(unicode_tree(), max_codepoint=127) return ASCII_TREE def unicode_tree(): """Returns tree of Unicode characters.""" global UNICODE_TREE if not UNICODE_TREE: