Skip to content

Commit

Permalink
Split up fixers into the two remaining bits
Browse files Browse the repository at this point in the history
  • Loading branch information
DRMacIver committed Mar 18, 2015
1 parent 5bb7667 commit 133443f
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 283 deletions.
4 changes: 2 additions & 2 deletions src/hypothesis/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# END HEADER

from hypothesis.internal.fixers import nice_string
from hypothesis.utils.show import show
from hypothesis.internal.tracker import Tracker
from hypothesis.searchstrategy.strategies import BadData, strategy
from hypothesis.database.formats import JSONFormat
Expand All @@ -33,7 +33,7 @@ def __init__(
self.descriptor = descriptor
self.format = format
self.strategy = strategy
self.key = nice_string(descriptor)
self.key = show(descriptor)

def save(self, value):
tracker = Tracker()
Expand Down
4 changes: 2 additions & 2 deletions src/hypothesis/descriptortests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from hypothesis import given
from hypothesis.database import ExampleDatabase
from hypothesis.settings import Settings
from hypothesis.utils.show import show
from hypothesis.internal.compat import text_type, integer_types
from hypothesis.internal.fixers import nice_string
from hypothesis.database.backend import SQLiteBackend
from hypothesis.internal.verifier import Verifier
from hypothesis.searchstrategy.strategies import BuildContext, \
Expand Down Expand Up @@ -88,7 +88,7 @@ class ValidationSuite(TestCase):

def __repr__(self):
return 'descriptor_test_suite(%s)' % (
nice_string(descriptor),
show(descriptor),
)

@given(descriptor, settings=settings)
Expand Down
205 changes: 0 additions & 205 deletions src/hypothesis/internal/fixers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,208 +19,3 @@

from __future__ import division, print_function, absolute_import, \
unicode_literals

import math
import unittest

import hypothesis.internal.reflection as reflection
from hypothesis.internal.compat import text_type, binary_type, \
integer_types
from hypothesis.utils.extmethod import ExtMethod


class IdKey(object):

def __init__(self, value):
self.value = value

def __eq__(self, other):
return (type(other) == IdKey) and (self.value is other.value)

def __ne__(self, other):
return not (self.__eq__(other))

def __hash__(self):
return hash(id(self.value))


class IdentitySet(object):

def __init__(self):
self.data = {}

def __contains__(self, value):
key = IdKey(value)
return self.data.get(key, 0) > 0

def add(self, value):
key = IdKey(value)
self.data[key] = self.data.get(key, 0) + 1

def remove(self, value):
key = IdKey(value)
self.data[key] = self.data.get(key, 0) - 1


class NiceString(ExtMethod):

def __call__(self, value, seen=None):
if seen is None:
seen = IdentitySet()
if value in seen:
return '(...)'
seen.add(value)
result = super(NiceString, self).__call__(value, seen)
seen.remove(value)
return result


nice_string = NiceString()


@nice_string.extend(bool)
def repr_string(value, seen):
return repr(value)


@nice_string.extend(object)
def generic_string(value, seen):
if hasattr(value, '__name__'):
return value.__name__
try:
d = value.__dict__
except AttributeError:
if type(value) == object:
return 'object()'
else:
return repr(value)
if (
reflection.unbind_method(type(value).__repr__) !=
reflection.unbind_method(object.__repr__)
):
return repr(value)
else:
return '%s(%s)' % (
value.__class__.__name__,
', '.join(
'%s=%s' % (
k2, nice_string(v2, seen)
) for k2, v2 in d.items()
)
)


@nice_string.extend(text_type)
def text_string(value, seen):
result = repr(value)
if result[0] == 'u': # pragma: no branch
return result[1:] # pragma: no cover
else:
return result # pragma: no cover


@nice_string.extend(binary_type)
def binary_string(value, seen):
result = repr(value)
if result[0] != 'b': # pragma: no branch
return 'b' + result # pragma: no cover
else:
return result # pragma: no cover


@nice_string.extend(type)
def type_string(value, seen):
return value.__name__


def is_nasty_float(x):
return math.isnan(x) or math.isinf(x)


@nice_string.extend(float)
def float_string(value, seen):
if is_nasty_float(value):
return 'float(%r)' % (str(value),)
else:
return repr(value)


@nice_string.extend(complex)
def complex_string(x, seen):
if is_nasty_float(x.real) or is_nasty_float(x.imag):
r = repr(x)
if r[0] == '(' and r[-1] == ')':
r = r[1:-1]
return 'complex(%r)' % (r,)
else:
return repr(x)


@nice_string.extend(list)
def list_string(value, seen):
return '[%s]' % (', '.join(
nice_string(c, seen) for c in value
))


@nice_string.extend(set)
def set_string(value, seen):
if value:
return '{%s}' % (', '.join(sorted(
nice_string(c, seen) for c in value
)))
else:
return repr(value)


@nice_string.extend(frozenset)
def frozenset_string(value, seen):
if value:
return 'frozenset({%s})' % (', '.join(sorted(
nice_string(c, seen) for c in value
)))
else:
return repr(value)


@nice_string.extend(tuple)
def tuple_string(value, seen):
if hasattr(value, '_fields'):
return '%s(%s)' % (
value.__class__.__name__,
', '.join(
'%s=%s' % (f, nice_string(getattr(value, f), seen))
for f in value._fields))
else:
core = ', '.join(
nice_string(c, seen) for c in value
)
if len(value) == 1:
core += ','
return '(%s)' % (core,)


@nice_string.extend(dict)
def dict_string(value, seen):
return '{' + ', '.join(sorted([
nice_string(k1, seen) + ': ' + nice_string(v1, seen)
for k1, v1 in value.items()
])) + '}'


@nice_string.extend(unittest.TestCase)
def test_string(value, seen):
return '%s(methodName=%r)' % (
type(value).__name__,
value._testMethodName,
)


def int_string(value, seen):
s = repr(value)
if s[-1] == 'L': # pragma: no branch
s = s[:-1] # pragma: no cover
return s

for t in integer_types:
nice_string.extend(t)(int_string)
8 changes: 4 additions & 4 deletions src/hypothesis/internal/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def get_pretty_function_description(f):


def arg_string(f, args, kwargs):
from hypothesis.internal.fixers import nice_string
from hypothesis.utils.show import show
args, kwargs = convert_positional_arguments(f, args, kwargs)

argspec = inspect.getargspec(f)
Expand All @@ -260,13 +260,13 @@ def arg_string(f, args, kwargs):

for a in argspec.args:
if a in kwargs:
bits.append('%s=%s' % (a, nice_string(kwargs.pop(a))))
bits.append('%s=%s' % (a, show(kwargs.pop(a))))
if kwargs:
for a in sorted(kwargs):
bits.append('%s=%s' % (a, nice_string(kwargs[a])))
bits.append('%s=%s' % (a, show(kwargs[a])))

return ', '.join(
[nice_string(x) for x in args] +
[show(x) for x in args] +
bits
)

Expand Down
6 changes: 3 additions & 3 deletions src/hypothesis/searchstrategy/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import hypothesis.internal.distributions as dist
from hypothesis.settings import Settings
from hypothesis.utils.show import show
from hypothesis.internal.compat import hrange
from hypothesis.internal.fixers import nice_string
from hypothesis.searchstrategy.strategies import SearchStrategy, \
MappedSearchStrategy, strategy, check_type, check_length, \
check_data_type, one_of_strategies
Expand Down Expand Up @@ -264,7 +264,7 @@ class SetStrategy(MappedSearchStrategy):

def __init__(self, strategies, average_length=50.0):
strategies = list(strategies)
strategies.sort(key=nice_string)
strategies.sort(key=show)

self.descriptor = {x.descriptor for x in strategies}
if self.descriptor:
Expand Down Expand Up @@ -367,7 +367,7 @@ class FixedKeysDictStrategy(MappedSearchStrategy):

def __init__(self, strategy_dict):
self.keys = tuple(sorted(
strategy_dict.keys(), key=nice_string
strategy_dict.keys(), key=show
))
super(FixedKeysDictStrategy, self).__init__(
descriptor={
Expand Down
4 changes: 2 additions & 2 deletions src/hypothesis/searchstrategy/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import hypothesis.descriptors as descriptors
import hypothesis.internal.distributions as dist
from hypothesis.types import RandomWithSeed
from hypothesis.utils.show import show
from hypothesis.internal.compat import integer_types
from hypothesis.internal.fixers import nice_string
from hypothesis.searchstrategy.strategies import BadData, SearchStrategy, \
strategy, check_type, check_data_type

Expand Down Expand Up @@ -77,7 +77,7 @@ def to_basic(self, template):

def from_basic(self, data):
if data is not None:
raise BadData('Expected None but got %s' % (nice_string(data,)))
raise BadData('Expected None but got %s' % (show(data,)))
return None


Expand Down
5 changes: 3 additions & 2 deletions src/hypothesis/searchstrategy/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
import hypothesis.internal.distributions as dist
from hypothesis.errors import BadData, WrongFormat
from hypothesis.settings import Settings
from hypothesis.utils.show import show
from hypothesis.descriptors import OneOf, one_of
from hypothesis.utils.idkey import IdKey
from hypothesis.internal.compat import hrange, integer_types
from hypothesis.internal.fixers import IdKey, nice_string
from hypothesis.utils.extmethod import ExtMethod
from hypothesis.internal.tracker import Tracker

Expand Down Expand Up @@ -122,7 +123,7 @@ class SearchStrategy(object):
def __repr__(self):
return '%s(%s)' % (
self.__class__.__name__,
nice_string(self.descriptor)
show(self.descriptor)
)

def __init__(self):
Expand Down
35 changes: 35 additions & 0 deletions src/hypothesis/utils/idkey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import division, print_function, absolute_import, \
unicode_literals


class IdKey(object):

def __init__(self, value):
self.value = value

def __eq__(self, other):
return (type(other) == IdKey) and (self.value is other.value)

def __ne__(self, other):
return not (self.__eq__(other))

def __hash__(self):
return hash(id(self.value))


class IdentitySet(object):

def __init__(self):
self.data = {}

def __contains__(self, value):
key = IdKey(value)
return self.data.get(key, 0) > 0

def add(self, value):
key = IdKey(value)
self.data[key] = self.data.get(key, 0) + 1

def remove(self, value):
key = IdKey(value)
self.data[key] = self.data.get(key, 0) - 1

0 comments on commit 133443f

Please sign in to comment.