Ejemplo n.º 1
0
 def test_no_args(self):
     try:
         flip()
     except TypeError:
         pass
     else:
         self.fail("Failed to raise TypeError")
Ejemplo n.º 2
0
 def test_no_args(self):
     try:
         flip()
     except TypeError:
         pass
     else:
         self.fail("Failed to raise TypeError")
Ejemplo n.º 3
0
 def test_bad_first_arg(self):
     try:
         flip("foo")
     except TypeError:
         pass
     else:
         self.fail("Failed to raise TypeError")
Ejemplo n.º 4
0
 def test_bad_first_arg(self):
     try:
         flip("foo")
     except TypeError:
         pass
     else:
         self.fail("Failed to raise TypeError")
Ejemplo n.º 5
0
class Signature(object):
    '''A Signature of types for a function. Can be ordered, though it's only a
    partial ordering.
    '''

    def __init__(self, tsig):
        #Explicitly convert to tuple to prevent Bad Things from happening with
        #generator expressions.
        self.tsig = tuple(tsig)
    
    def supertypes(self, other):
        '''Returns True if our types supertype or are equal to the operand's
        types.
        '''
        if self.tsig == other.tsig:
            return True
        return self.strict_supertypes(other)

    def strict_supertypes(self, other):
        '''Returns True if all of our types supertype the operand's types.
        
        This also returns True if only some do, and the rest are equal.
        '''
        if len(self.tsig) != len(other.tsig):
            return False

        if self.tsig == other.tsig:
            return False
        
        zipped_types = zip(self.tsig, other.tsig)
        #Is it just me, or is the argument order for issubclass completely
        #arbitrary? I've tripped up on that so many times - my preferred 
        #method of turning it into a Haskell style infix thingy and then 
        #reading it aloud ("b `issubclass` a") doesn't help. I think it ought 
        #to be renamed 'issubclassof' and the argument order reversed.
        if all(_cooler_issubclass(theirs, ours) 
               for ours, theirs in zipped_types):
            return True

        return False

    __ge__ = supertypes
    __le__ = flip(supertypes)
    __gt__ = strict_supertypes
    __lt__ = flip(strict_supertypes)

    def __repr__(self):
        return "Signature%s" % repr(self.tsig)

    def __hash__(self):
        return hash(self.tsig)

    def __eq__(self, other):
        return self.tsig == other.tsig
Ejemplo n.º 6
0
 def test_weakrefs(self):
     import weakref
     
     f = flip(flip)
     w = weakref.ref(f)
     
     assert w() is f
Ejemplo n.º 7
0
    def test_weakrefs(self):
        import weakref

        f = flip(flip)
        w = weakref.ref(f)

        assert w() is f
Ejemplo n.º 8
0
    def test_bad_call(self):
        flipped_sub = flip(sub)

        try:
            flipped_sub(4)
        except TypeError:
            pass
        else:
            self.fail("Failed to raise TypeError")
Ejemplo n.º 9
0
    def test_all_kw(self):
        flipped_sub = flip(sub)

        try:
            flipped_sub(f=4, g=3)
        except TypeError:
            pass
        else:
            self.fail("Failed to raise TypeError")
Ejemplo n.º 10
0
 def test_all_kw(self):
     flipped_sub = flip(sub)
     
     try:
         flipped_sub(f=4, g=3)
     except TypeError:
         pass
     else:
         self.fail("Failed to raise TypeError")
Ejemplo n.º 11
0
 def test_bad_call(self):
     flipped_sub = flip(sub)
     
     try:
         flipped_sub(4)
     except TypeError:
         pass
     else:
         self.fail("Failed to raise TypeError")
Ejemplo n.º 12
0
class OrderedSet(list):
    '''A collection of unique elements, kept in order. Mutable.'''

    #Possibly todo: write an immutable ordered set? Inheriting from tuple?

    def __init__(self, seq=()):
        list.__init__(self)
        self.extend(seq)

    _overwrite = list.__init__

    def __contains__(self, item):
        hash(item)
        return list.__contains__(self, item)

    def __repr__(self):
        return '%s(%s)' % (type(self).__name__, list.__repr__(self))

    def __setslice__(self, i, j, sequence):
        self.__setitem__(slice(i, j), sequence)

    def __setitem__(self, key, value):
        if isinstance(key, slice):
            res = self.intersection(value)
        else:
            res = value in self
        if res:
            raise ValueError('Item is already a member.')
        list.__setitem__(self, key, value)

    def __getitem__(self, key):
        res = list.__getitem__(self, key)
        if isinstance(key, slice):
            return OrderedSet(res)
        return res

    def __getslice__(self, i, j):
        return self.__getitem__(slice(i, j))

    #List methods, suitably wrapped and checked.

    def append(self, item):
        '''Append an element to the end of the ordered set.

        Quietly returns if it is already a member.
        '''
        hash(item)
        if item in self:
            #raise ValueError("Item is already a member.")
            return  #silently ignore.
            #XXX: strict option?
        list.append(self, item)

    def count(self, value):
        '''Return the number of times an element occurs.

        Due to the very nature of sets, this will either be 0 or 1.
        '''
        return int(value in self)

    def extend(self, values):
        '''Extend the ordered set with a sequence of elements.

        This preserves the order of the elements. Duplicates are silently
        ignored.
        '''
        appending = []
        for value in values:
            hash(value)
            appending.append(value)

        for value in appending:
            self.add(value)

    def insert(self, key, value):
        '''Set a given index to a value.

        Raises a ValueError if the element is already a member.
        '''
        #why not just self[key] = value?
        hash(value)
        self[key:key] = value

    #methods for frozenset compatability.

    #These two could be done using itertools.imap and the boolean operations.
    def issubset(self, other):
        '''Returns True if a given set is a subset of the ordered set.'''
        #don't blow up with generators
        other = list(other)
        if len(self) > len(other):
            return False
        for elem in self:
            if elem not in other:
                return False
        return True

    def issuperset(self, other):
        '''Returns True if a given set is a superset of the ordered set.'''
        #don't blow up with generators
        other = list(other)
        if len(self) < len(other):
            return False
        for elem in other:
            if elem not in self:
                return False
        return True

    def union(self, other):
        '''Return an OrderedSet of elements in both operands.'''
        res = OrderedSet(self)
        res.extend(other)
        return res

    def intersection(self, other):
        '''Return an OrderedSet of elements common to both operands.'''
        res = OrderedSet()
        other = list(other)
        if len(self) > len(other):
            #speed: this check is not needed other than to minimise the number
            #of iterations.
            other, self = self, other
        for elem in self:
            if elem in other:
                res.add(elem)
        return res

    def difference(self, other):
        '''Return the elements that do not appear in the other operand which 
        do appear in this one.
        '''
        res = OrderedSet()
        other = list(other)
        for elem in self:
            if elem not in other:
                res.add(elem)
        return res

    def symmetric_difference(self, other):
        '''Return the elements that appear in one operand but not the other.'''
        res = self.difference(other)
        for elem in other:
            if elem not in self:
                res.add(elem)
        return res

    def copy(self):
        '''Return a new OrderedSet with the same elements.

        The elements are not copied.
        '''
        return OrderedSet(self)

    __copy__ = copy

    #Set methods.
    update = extend

    def intersection_update(self, other):
        '''Overwrite this OrderedSet with the operands' intersection.'''
        self._overwrite(self.intersection(other))

    def difference_update(self, other):
        '''Overwrite this OrderedSet instance with the operands' assymetric
        difference.
        '''
        self._overwrite(self.difference(other))

    def symmetric_difference_update(self, other):
        '''Overwrite this OrderedSet instance with the operands' symmetric
        difference.
        '''
        self._overwrite(self.symmetric_difference(other))

    def add(self, elem):
        '''Add an element to the OrderedSet, at the end.'''
        hash(elem)
        if elem not in self:
            list.append(self, elem)

    #remove is already implemented in the list inheritance: it'll raise the
    #wrong error, though (ValueError instead of KeyError). So we must wrap it,
    #making sure the error raised can be caught by ValueError and KeyError.
    def remove(self, elem):
        '''Remove an element from the OrderedSet, raising an error if the
        element is not present.
        '''
        hash(elem)
        try:
            list.remove(self, elem)
        except ValueError:
            raise DualValueError()

    def discard(self, elem):
        '''Remove an element from the OrderedSet quietly.'''
        if elem in self:
            self.remove(elem)

    #ditto as for remove, but we can do some dispatching. Thanks to Python's
    #amazing consistency, we have to raise a different error here: lists
    #throw a ValueError in remove but an IndexError in pop.
    def pop(self, index=None):
        '''Remove an object at a given index from the OrderedSet, or at the end
        if not specified.
        '''
        if index is None:
            #if index is None, it might be a set method, so the error raised
            #must be caught by "except IndexError" and "except KeyError".
            try:
                return list.pop(self, -1)
            except IndexError:
                raise DualError()
        else:
            #if not, it's a list thing: IndexError is expected, and list.pop
            #will raise that anyway.
            return list.pop(self, index)

    __or__ = _operand_set_checking(union)
    __ror__ = _operand_set_checking(flip(union))
    __and__ = _operand_set_checking(intersection)
    __rand__ = _operand_set_checking(flip(intersection))
    __sub__ = _operand_set_checking(difference)
    __rsub__ = _operand_set_checking(flip(difference))
    __xor__ = _operand_set_checking(symmetric_difference)
    __rxor__ = _operand_set_checking(flip(symmetric_difference))
    __ior__ = _operand_set_checking(update)
    __iand__ = _operand_set_checking(intersection_update)
    __isub__ = _operand_set_checking(difference_update)
    __ixor__ = _operand_set_checking(symmetric_difference_update)

    def clear(self):
        '''Remove all elements from the instance.'''
        self._overwrite([])

    #Comparisons are difficult, as sets and lists do wildly different things.
    #Since the list comparisons are undefined in the library reference, AFAIK,
    #and the set ones are, those are what OrderedSets will support.

    #Should equality just test for membership (ie, set-style), or should
    #position count as well (list-style)? Of course, I could always do
    #dispatching to sniff out sets and compare them according to set rules,
    #and then use the list style on everything else.
    #The problem here is that __eq__ is overloaded, with two fundamentally
    #different operations. What would be more handy is two wholly separate
    #comparison functions. Then, code writers could pick and choose.

    def __lt__(self, other):
        if len(self) >= len(other):
            return False
        return self.issubset(other)

    __ge__ = issuperset

    def __gt__(self, other):
        if len(self) <= len(other):
            return False
        return self.issuperset(other)

    __le__ = issubset

    def __eq__(self, other):
        if isinstance(other, _set_types):
            return self.setcompare(other)
        else:
            return self.listcompare(other)

    listcompare = list.__eq__

    #we can't convert to set because we might have unhashable elements.
    #This assumes that neither operand will have non-unique elements.
    #obviously, we can't have non-unique elements, but they might.
    def setcompare(self, other):
        '''Compare the OrderedSet to another set type as a set.'''
        return not self.symmetric_difference(other)

    def __ne__(self, other):
        return not (self == other)

    #my convenience methods.
    def rmapp(self, elem):
        '''Remove an element from the OrderedSet if it is present, then place
        it at the end.
        '''
        try:
            self.remove(elem)
        except IndexError:
            pass
        self.append(elem)
Ejemplo n.º 13
0
    def test_flips_all(self):
        def sub_m(*args, **kwargs):
            return args, kwargs

        assert flip(sub_m)(4, 5, 6, d=5, e=7) == ((6, 5, 4), {'d': 5, 'e': 7})
Ejemplo n.º 14
0
    def test(self):
        flipped_sub = flip(sub)

        self.assertEqual(sub(4, 5), -1)
        self.assertEqual(flipped_sub(4, 5), 1)
Ejemplo n.º 15
0
        self.url = url
        self.count = count

    def __repr__(self):
        return "<Link %s (%d)>" % (self.url, self.count)

# create the table if it doesn't exist
engine = sql.create_engine('sqlite:///tweets.db')
Tweet.metadata.create_all(engine)
Link.metadata.create_all(engine)

# get the timeline and links in it
timeline = urllib.urlopen('http://www.twitter.com/statuses/public_timeline.json').read()
timeline = json.loads(timeline)

encode = partial(flip(encode), 'ascii')
find_urls = re.compile(r'http://[^\s]+[^\s\.]')

links = []
tweets  = [Tweet(t['id']) for t in timeline]
for matches in [map(encode, find_urls.findall(t['text'])) for t in timeline]:
    if len(matches) > 0:
        tweet_links = []
        for url in matches:
            # find the real URL (after redirects)
            url = urllib.urlopen(url)
            url = url.geturl()
            tweet_links.append(Link(url, 1))
        links.append(tweet_links)
    else:
        links.append(None)
Ejemplo n.º 16
0
        self.url = url
        self.count = count

    def __repr__(self):
        return "<Link %s (%d)>" % (self.url, self.count)

# create the table if it doesn't exist
engine = sql.create_engine('sqlite:///tweets.db')
Tweet.metadata.create_all(engine)
Link.metadata.create_all(engine)

# get the timeline and links in it
timeline = urllib.urlopen('http://www.twitter.com/statuses/public_timeline.json').read()
timeline = json.loads(timeline)

encode = partial(flip(encode), 'ascii')
find_urls = re.compile(r'http://[^\s]+[^\s\.]')

links = []
tweets  = [Tweet(t['id']) for t in timeline]
for matches in [map(encode, find_urls.findall(t['text'])) for t in timeline]:
    if len(matches) > 0:
        tweet_links = []
        for url in matches:
            # find the real URL (after redirects)
            url = urllib.urlopen(url)
            url = url.geturl()
            tweet_links.append(Link(url, 1))
        links.append(tweet_links)
    else:
        links.append(None)
Ejemplo n.º 17
0
 def __call__(self, img):
     return F.flip(img, self.flip_mode)
Ejemplo n.º 18
0
 def __call__(self, img):
     flip_mode = random.randint(-1, 2)
     if flip_mode == 2:
         return img
     else:
         return F.flip(img, flip_mode)
Ejemplo n.º 19
0
 def __call__(self, img):
     if random.random() > 0.5:
         return F.flip(img, 0)
     else:
         return img
Ejemplo n.º 20
0
def test_flip():
    def subtract(x, y):
        return x - y

    assert subtract(4, 2) == 2
    assert functional.flip(subtract)(4, 2) == -2
Ejemplo n.º 21
0
 def test_flips_all(self):
     def sub_m(*args, **kwargs):
         return args, kwargs
         
     assert flip(sub_m)(4, 5, 6, d=5, e=7) == ((6, 5, 4), {'d': 5, 'e': 7})
Ejemplo n.º 22
0
 def test(self):
     flipped_sub = flip(sub)
     
     self.assertEqual(sub(4, 5), -1)
     self.assertEqual(flipped_sub(4, 5), 1)