mirror of
https://github.com/quantum5/optimize-later.git
synced 2025-04-24 04:21:58 -04:00
Add Python 3 support.
This commit is contained in:
parent
d02c8f6e6b
commit
62d312338d
|
@ -1,7 +1,7 @@
|
|||
import logging
|
||||
from functools import wraps
|
||||
|
||||
from optimize_later.utils import NoArgDecoratorMeta
|
||||
from optimize_later.utils import NoArgDecoratorMeta, with_metaclass
|
||||
|
||||
try:
|
||||
import threading
|
||||
|
@ -38,9 +38,7 @@ def global_callback(report):
|
|||
log.exception('Failed to invoke global callback: %r', callback)
|
||||
|
||||
|
||||
class optimize_context(object):
|
||||
__metaclass__ = NoArgDecoratorMeta
|
||||
|
||||
class optimize_context(with_metaclass(NoArgDecoratorMeta)):
|
||||
def __init__(self, callbacks=None):
|
||||
self.callbacks = callbacks
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ from functools import wraps
|
|||
from numbers import Number
|
||||
|
||||
from optimize_later.config import global_callback
|
||||
from optimize_later.utils import NoArgDecoratorMeta
|
||||
from optimize_later.utils import NoArgDecoratorMeta, with_metaclass
|
||||
|
||||
log = logging.getLogger(__name__.rpartition('.')[0] or __name__)
|
||||
timer = [time.time, time.clock][os.name == 'nt']
|
||||
|
@ -89,9 +89,7 @@ class OptimizeReport(object):
|
|||
return self.short()
|
||||
|
||||
|
||||
class optimize_later(object):
|
||||
__metaclass__ = NoArgDecoratorMeta
|
||||
|
||||
class optimize_later(with_metaclass(NoArgDecoratorMeta)):
|
||||
def __init__(self, name=None, limit=None, callback=None):
|
||||
if limit is None and isinstance(name, Number):
|
||||
name, limit = None, name
|
||||
|
|
|
@ -162,7 +162,7 @@ class OptimizeLaterTest(TestCase):
|
|||
self.assertEqual(len(reports), 1)
|
||||
self.assertReport(reports[0], blocks=2)
|
||||
report = reports[0].long()
|
||||
print report
|
||||
print(report)
|
||||
self.assertIn(' - Block ', report)
|
||||
self.assertIn(' - Block', report)
|
||||
self.assertEqual(report.count(', children:'), 2)
|
||||
|
@ -177,7 +177,7 @@ class OptimizeLaterTest(TestCase):
|
|||
self.assertEqual(function.__name__, 'function')
|
||||
self.assertEqual(function.__module__, __name__)
|
||||
|
||||
for i in xrange(10):
|
||||
for i in range(10):
|
||||
function()
|
||||
self.assertEqual(len(reports), 10)
|
||||
for report in reports:
|
||||
|
|
|
@ -6,3 +6,17 @@ class NoArgDecoratorMeta(type):
|
|||
if len(args) == 1 and isinstance(args[0], FunctionType):
|
||||
return cls()(args[0])
|
||||
return super(NoArgDecoratorMeta, cls).__call__(*args, **kwargs)
|
||||
|
||||
|
||||
# Borrowed from the six library.
|
||||
def with_metaclass(meta, *bases):
|
||||
"""Create a base class with a metaclass."""
|
||||
|
||||
# This requires a bit of explanation: the basic idea is to make a dummy
|
||||
# metaclass for one level of class instantiation that replaces itself with
|
||||
# the actual metaclass.
|
||||
class metaclass(meta):
|
||||
def __new__(cls, name, this_bases, d):
|
||||
return meta(name, bases, d)
|
||||
|
||||
return type.__new__(metaclass, 'temporary_class', (), {})
|
||||
|
|
Loading…
Reference in a new issue