提交 b4181fcb authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

Remove more support for python < 2.6

上级 7540e33a
...@@ -4,15 +4,15 @@ from theano.compat import cmp, defaultdict ...@@ -4,15 +4,15 @@ from theano.compat import cmp, defaultdict
# {{{ http://code.activestate.com/recipes/578231/ (r1) # {{{ http://code.activestate.com/recipes/578231/ (r1)
# Copyright (c) Oren Tirosh 2012 # Copyright (c) Oren Tirosh 2012
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy of # Permission is hereby granted, free of charge, to any person obtaining a copy
# this software and associated documentation files (the "Software"), to deal in # of this software and associated documentation files (the "Software"), to deal
# the Software without restriction, including without limitation the rights to # in the Software without restriction, including without limitation the rights
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# of the Software, and to permit persons to whom the Software is furnished to do # copies of the Software, and to permit persons to whom the Software is
# so, subject to the following conditions: # furnished to do so, subject to the following conditions:
# #
# The above copyright notice and this permission notice shall be included in all # The above copyright notice and this permission notice shall be included in
# copies or substantial portions of the Software. # all copies or substantial portions of the Software.
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
...@@ -30,6 +30,7 @@ def memodict(f): ...@@ -30,6 +30,7 @@ def memodict(f):
ret = self[key] = f(key) ret = self[key] = f(key)
return ret return ret
return memodict().__getitem__ return memodict().__getitem__
# end of http://code.activestate.com/recipes/578231/ }}} # end of http://code.activestate.com/recipes/578231/ }}}
......
...@@ -14,31 +14,29 @@ def check_deterministic(iterable): ...@@ -14,31 +14,29 @@ def check_deterministic(iterable):
assert isinstance(iterable, ( assert isinstance(iterable, (
list, tuple, OrderedSet, types.GeneratorType, basestring)) list, tuple, OrderedSet, types.GeneratorType, basestring))
if MutableSet is not None: # Copyright (C) 2009 Raymond Hettinger
# Copyright (C) 2009 Raymond Hettinger # Permission is hereby granted, free of charge, to any person obtaining a
# Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the
# copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including
# "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish,
# without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to permit
# distribute, sublicense, and/or sell copies of the Software, and to permit # persons to whom the Software is furnished to do so, subject to the
# persons to whom the Software is furnished to do so, subject to the # following conditions:
# following conditions:
# The above copyright notice and this permission notice shall be included
# The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software.
# in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # {{{ http://code.activestate.com/recipes/576696/ (r5)
# {{{ http://code.activestate.com/recipes/576696/ (r5) import weakref
import collections
import weakref class Link(object):
class Link(object):
# This make that we need to use a different pickle protocol # This make that we need to use a different pickle protocol
# then the default. Othewise, there is pickling errors # then the default. Othewise, there is pickling errors
__slots__ = 'prev', 'next', 'key', '__weakref__' __slots__ = 'prev', 'next', 'key', '__weakref__'
...@@ -60,7 +58,7 @@ if MutableSet is not None: ...@@ -60,7 +58,7 @@ if MutableSet is not None:
if len(state) == 3: if len(state) == 3:
self.key = state[2] self.key = state[2]
class OrderedSet(collections.MutableSet): class OrderedSet(MutableSet):
'Set the remembers the order elements were added' 'Set the remembers the order elements were added'
# Big-O running times for all methods are the same as for regular sets. # Big-O running times for all methods are the same as for regular sets.
# The internal self.__map dictionary maps keys to links in a doubly linked list. # The internal self.__map dictionary maps keys to links in a doubly linked list.
...@@ -185,77 +183,7 @@ if MutableSet is not None: ...@@ -185,77 +183,7 @@ if MutableSet is not None:
else: else:
return NotImplemented return NotImplemented
# end of http://code.activestate.com/recipes/576696/ }}} # end of http://code.activestate.com/recipes/576696/ }}}
else:
# Python 2.4
class OrderedSet(object):
"""
An implementation of OrderedSet based on the keys of
an OrderedDict.
"""
def __init__(self, iterable=None):
self.data = OrderedDict()
if iterable is not None:
self.update(iterable)
def update(self, container):
check_deterministic(container)
for elem in container:
self.add(elem)
def add(self, key):
self.data[key] = None
def __len__(self):
return len(self.data)
def __contains__(self, key):
return key in self.data
def discard(self, key):
if key in self.data:
del self.data[key]
def remove(self, key):
if key in self.data:
del self.data[key]
else:
raise KeyError(key)
def __iter__(self):
return self.data.__iter__()
def __reversed__(self):
return self.data.__reversed__()
def pop(self, last=True):
raise NotImplementedError()
def __eq__(self, other):
# Note that we implement only the comparison to another
# `OrderedSet`, and not to a regular `set`, because otherwise we
# could have a non-symmetric equality relation like:
# my_ordered_set == my_set and my_set != my_ordered_set
if isinstance(other, OrderedSet):
return len(self) == len(other) and list(self) == list(other)
elif isinstance(other, set):
# Raise exception to avoid confusion.
raise TypeError(
'Cannot compare an `OrderedSet` to a `set` because '
'this comparison cannot be made symmetric: please '
'manually cast your `OrderedSet` into `set` before '
'performing this comparison.')
else:
return NotImplemented
# NB: Contrary to the other implementation above, we do not override
# the `__del__` method. On one hand, this is not needed since this
# implementation does not add circular references. Moreover, one should
# not clear the underlying dictionary holding the data as soon as the
# ordered set is cleared from memory, because there may still be
# pointers to this dictionary.
if __name__ == '__main__': if __name__ == '__main__':
print list(OrderedSet('abracadaba')) print list(OrderedSet('abracadaba'))
......
""" """
This file implement specialization optimization that break the canonization form of the graph. This file implement specialization optimization that break the
canonization form of the graph.
Currently there is problem with the order of optimization and the definition of definition of
canonized graph. Currently there is problem with the order of optimization and the
definition of definition of canonized graph.
Right now there is a canonization optimization phase that try to make all equivalent graph
identical. This is not always the case, but it do many of the basic stuff canonical. We Right now there is a canonization optimization phase that try to make
need to extend the definition of canonization to make this true more often. all equivalent graph identical. This is not always the case, but it do
many of the basic stuff canonical. We need to extend the definition of
The problem this file indent to fix in the future is that in the "Equilibrium" specialization canonization to make this true more often.
optimization phase, there is optimization that request that the graph is canonical, some other
request that this is not true, and some other that break the canonicalization for The problem this file indent to fix in the future is that in the
some optimization. As we can't control the order of those optimization, there is case that some "Equilibrium" specialization optimization phase, there is optimization
optimization requesting a canonical graph won't be applied as optimization that break the that request that the graph is canonical, some other request that this
canonicalization form of the graph executed before. is not true, and some other that break the canonicalization for some
optimization. As we can't control the order of those optimization, there
To fix this, we need to split the specialization phase into a phase where optimization can't break the canonicalization form and one where this is allowed. This is also needed for the stabilized optimization phase, but as it happen before the specialization phase, this cause less problem. is case that some optimization requesting a canonical graph won't be
applied as optimization that break the canonicalization form of the
Also, we should make the fgraph refuse optimization that break the canonization of the graph in the optimizations phases where the graph is supposed to be canonical. graph executed before.
To fix this, we need to split the specialization phase into a phase
where optimization can't break the canonicalization form and one where
this is allowed. This is also needed for the stabilized optimization
phase, but as it happen before the specialization phase, this cause less
problem.
Also, we should make the fgraph refuse optimization that break the
canonization of the graph in the optimizations phases where the graph is
supposed to be canonical.
""" """
# TODO: intelligent merge for mul/add # TODO: intelligent merge for mul/add
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论