Skip to content

Commit

Permalink
Merge pull request #108 from bartvm/metaclass
Browse files Browse the repository at this point in the history
Replaced the Python 2 __metaclass__ = ABCMeta with six's version
  • Loading branch information
bartvm committed Jan 17, 2015
2 parents 827ea96 + 3a23e67 commit 32e9554
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 33 deletions.
7 changes: 3 additions & 4 deletions blocks/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from collections import OrderedDict

import theano
from six import add_metaclass
from theano import tensor

from blocks.graph import ComputationGraph


@add_metaclass(ABCMeta)
class TrainingAlgorithm(object):
"""Base class for training algorithms.
Expand All @@ -18,8 +20,6 @@ class TrainingAlgorithm(object):
called with a batch of training data as a parameter.
"""
__metaclass__ = ABCMeta

@abstractmethod
def initialize(self):
"""Initialize the training algorithm."""
Expand All @@ -38,6 +38,7 @@ def process_batch(self, batch):
pass


@add_metaclass(ABCMeta)
class DifferentiableCostMinimizer(TrainingAlgorithm):
"""Minimizes a differentiable cost given as a Theano expression.
Expand Down Expand Up @@ -82,8 +83,6 @@ class the parameter used only inside scans are not fetched
currently.
"""
__metaclass__ = ABCMeta

def __init__(self, cost, params=None):
self.cost = cost
self.params = (params if params
Expand Down
3 changes: 2 additions & 1 deletion blocks/bricks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from itertools import chain

import numpy
from six import add_metaclass
from theano import tensor

from blocks.utils import (pack, repr_attrs, reraise_as, shared_floatx_zeros,
Expand All @@ -17,6 +18,7 @@
logger = logging.getLogger(__name__)


@add_metaclass(ABCMeta)
class Brick(object):
"""A brick encapsulates Theano operations with parameters.
Expand Down Expand Up @@ -164,7 +166,6 @@ class Brick(object):
linear_apply_output
"""
__metaclass__ = ABCMeta
#: See :attr:`Brick.lazy`
lazy = True
#: See :attr:`Brick.print_shapes`
Expand Down
13 changes: 5 additions & 8 deletions blocks/bricks/sequence_generators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sequence generation framework."""
from abc import ABCMeta, abstractmethod

from six import add_metaclass
from theano import tensor

from blocks.bricks import (application, Brick, Initializable, Identity, lazy,
Expand All @@ -12,6 +13,7 @@
from blocks.utils import dict_subset, dict_union, update_instance


@add_metaclass(ABCMeta)
class BaseSequenceGenerator(Initializable):
"""A generic sequence generator.
Expand Down Expand Up @@ -243,8 +245,6 @@ def initial_state(self, name, batch_size, *args, **kwargs):

class AbstractEmitter(Brick):
"""The interface for the emitter component of a readout."""
__metaclass__ = ABCMeta

@abstractmethod
def emit(self, readouts):
pass
Expand All @@ -258,15 +258,15 @@ def initial_outputs(self, batch_size, *args, **kwargs):
pass


@add_metaclass(ABCMeta)
class AbstractFeedback(Brick):
"""The interface for the feedback component of a readout."""
__metaclass__ = ABCMeta

@abstractmethod
def feedback(self, outputs):
pass


@add_metaclass(ABCMeta)
class AbstractReadout(AbstractEmitter, AbstractFeedback):
"""The interface for the readout component of a sequence generator.
Expand All @@ -275,21 +275,18 @@ class AbstractReadout(AbstractEmitter, AbstractFeedback):
Explain what the methods should do.
"""
__metaclass__ = ABCMeta

@abstractmethod
def readout(self, **kwargs):
pass


@add_metaclass(ABCMeta)
class AbstractAttentionTransition(BaseRecurrent):
"""A base class for a transition component of a sequence generator.
A recurrent transition combined with an attention mechanism.
"""
__metaclass__ = ABCMeta

@abstractmethod
def apply(self, **kwargs):
pass
Expand Down
7 changes: 3 additions & 4 deletions blocks/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import numpy
import six
from six import add_metaclass

from blocks.utils import update_instance


@add_metaclass(ABCMeta)
class Dataset(object):
"""A dataset.
Expand Down Expand Up @@ -36,8 +38,6 @@ class Dataset(object):
simultaneously.
"""
__metaclass__ = ABCMeta

def __init__(self, sources=None):
if sources is not None:
if not all(source in self.sources for source in sources):
Expand Down Expand Up @@ -130,6 +130,7 @@ def get_default_stream(self):
return DataStream(self, iteration_scheme=self.default_scheme)


@add_metaclass(ABCMeta)
class ContainerDataset(Dataset):
"""Equips a Python container with the dataset interface.
Expand Down Expand Up @@ -200,8 +201,6 @@ class AbstractDataStream(object):
given by the dataset.
"""
__metaclass__ = ABCMeta

def __init__(self, iteration_scheme=None):
self.iteration_scheme = iteration_scheme

Expand Down
5 changes: 3 additions & 2 deletions blocks/datasets/schemes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import itertools
from abc import ABCMeta, abstractmethod

from six import add_metaclass


@add_metaclass(ABCMeta)
class IterationScheme(object):
"""An iteration scheme.
Expand All @@ -23,8 +26,6 @@ class IterationScheme(object):
https://docs.python.org/3.3/library/stdtypes.html#iterator-types
"""
__metaclass__ = ABCMeta

@abstractmethod
def get_request_iterator(self):
raise NotImplementedError
Expand Down
5 changes: 3 additions & 2 deletions blocks/extensions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import print_function
from abc import ABCMeta, abstractmethod

from six import add_metaclass


@add_metaclass(ABCMeta)
class TrainingExtension(object):
"""The base class for training extensions.
Expand All @@ -16,8 +19,6 @@ class TrainingExtension(object):
The main loop to which the extension belongs.
"""
__metaclass__ = ABCMeta

def dispatch(self, callback_name, *args):
"""Runs callback with the given name.
Expand Down
4 changes: 2 additions & 2 deletions blocks/groundhog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from abc import ABCMeta, abstractmethod

import theano
from six import add_metaclass
from theano import tensor
from theano import Variable

Expand All @@ -13,14 +14,13 @@
logger = logging.getLogger(__name__)


@add_metaclass(ABCMeta)
class GroundhogIterator(object):
"""A base class for Groundhog compatible iterator.
Has mock implementations of all required methods except `next`.
"""
__metaclass__ = ABCMeta

def start(self, offset):
pass

Expand Down
4 changes: 2 additions & 2 deletions blocks/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import numpy
import six
import theano
from six import add_metaclass

from blocks.utils import update_instance


@add_metaclass(ABCMeta)
class NdarrayInitialization(object):
"""Base class specifying the interface for ndarray initialization."""
__metaclass__ = ABCMeta

@abstractmethod
def generate(self, rng, shape):
"""Generate an initial set of parameters from a given distribution.
Expand Down
8 changes: 4 additions & 4 deletions blocks/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from abc import ABCMeta, abstractmethod
from collections import defaultdict

from six import add_metaclass


@add_metaclass(ABCMeta)
class AbstractTrainingStatus(object):
"""The base class for objects that carry the training status.
Expand All @@ -27,8 +30,6 @@ class AbstractTrainingStatus(object):
printed or not.
"""
__metaclass__ = ABCMeta

def __init__(self):
self.iterations_done = 0
self.epochs_done = 0
Expand All @@ -44,6 +45,7 @@ def __iter__(self):
pass


@add_metaclass(ABCMeta)
class TrainingLogRow(object):
"""A convenience interface for a row of the training log.
Expand Down Expand Up @@ -88,8 +90,6 @@ class AbstractTrainingLog(object):
to allow convenient access to the rows.
"""
__metaclass__ = ABCMeta

@abstractmethod
def get_status(self):
"""Returns the training status.
Expand Down
5 changes: 3 additions & 2 deletions blocks/main_loop.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""The event-based main loop of Blocks."""
from abc import ABCMeta

from six import add_metaclass

from blocks.log import TrainingLog
from blocks.utils import update_instance


@add_metaclass(ABCMeta)
class MainLoop(object):
"""The standard main loop of Blocks.
Expand Down Expand Up @@ -39,8 +42,6 @@ class MainLoop(object):
here.
"""
__metaclass__ = ABCMeta

def __init__(self, model, data_stream, algorithm,
log=None, extensions=None):
if not log:
Expand Down
5 changes: 3 additions & 2 deletions blocks/monitoring/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
import logging
from abc import ABCMeta, abstractmethod

from six import add_metaclass

from blocks.utils import shared_like, update_instance

logger = logging.getLogger(__name__)


@add_metaclass(ABCMeta)
class AggregationScheme(object):
"""Specify how incrementally evaluate a Theano variable on a dataset.
Expand All @@ -25,8 +28,6 @@ class AggregationScheme(object):
expression that computes the desired value on a single batch.
"""
__metaclass__ = ABCMeta

@abstractmethod
def get_aggregator(self):
"""Return a new Aggregator for this variable."""
Expand Down

0 comments on commit 32e9554

Please sign in to comment.