Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cleanup #116

Closed
wants to merge 10 commits into from
23 changes: 13 additions & 10 deletions integrationtests/ui/test_set_dynamic.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# Copyright (c) 2007, Enthought, Inc.
# License: BSD Style.

from traits.api import *
from traitsui.api import *
from traits.api import HasTraits, List, Str
from traitsui.api import Item, SetEditor, View

class Team ( HasTraits ):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tricky: there's an obviously deliberate style choice on Dave Morrill's part to include spaces around function arguments and base classes, and much of the traits code follows that style. I think we should aim for local consistency over PEP8 consistency.

There's always the option of just fixing the whole codebase, but see my other comment...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see traits move to PEP8 compliance (if just to shut my editor up...), but you make a good point. This branch should probably be based off the Python 3 effort as that is the most likely to have many conflicts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, the use of spaces around function arguments is not strictly consistent applied, or at least it wasn't in the tests I looked at. Use of spaces seems to have varied from time to time.
Obviously, I feel we should move toward PEP8 compliance everywhere (otherwise I wouldn't have spent the time to do it). I have PEP8 and PyFlakes hilighting in my editor by default, and it makes looking at code like Traits painful, and cleaning it up is relatively easy. I also find I navigate PEP8 code more easily.
That said, I'm willing to be overruled if there is strong opinion to favor local consistency over an Enthought standard.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the technical concern of merge conflict is dispositive here. The Python 3 branch is almost ready to go in. We can do PEP8 cleanup afterwards.

batting_order = List( Str )
roster = List( [ 'Tom', 'Dick', 'Harry', 'Sally' ], Str )
class Team (HasTraits):

view = View( Item( 'batting_order', editor = SetEditor( name = 'roster',
ordered = True ) ),
'_', 'roster@',
height=500,
resizable=True)
batting_order = List(Str)
roster = List(['Tom', 'Dick', 'Harry', 'Sally'], Str)

view = View(Item('batting_order',
editor=SetEditor(name='roster', ordered=True),),
'_',
'roster@',
height=500,
resizable=True,
)

if __name__ == '__main__':
Team().configure_traits()
175 changes: 68 additions & 107 deletions traits/tests/check_timing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
Expand All @@ -12,225 +11,188 @@
# Date: 03/03/2003
# Description: Perform timing tests on various trait styles to determine the
# amount of overhead that traits add.
#------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------

from __future__ import absolute_import

from time import time
from ..api import Any, DelegatesTo, HasTraits, Int, Range

#-------------------------------------------------------------------------------
# Constants:
#-------------------------------------------------------------------------------

# Number of iterations to perform:
n = 1000000

# Loop overhead time (actual value determined first time a measurement is made):
# Loop overhead time (actual value determined first time a measurement is made)
t0 = -1.0

#-------------------------------------------------------------------------------
# Measure how long it takes to execute a specified function:
#-------------------------------------------------------------------------------

def measure ( func ):
# Measure how long it takes to execute a specified function:
def measure(func):
now = time()
func()
return time() - now

#-------------------------------------------------------------------------------
# 'Old style' Python attribute get/set:
#-------------------------------------------------------------------------------

# 'Old style' Python attribute get/set:
class old_style_value:

def measure ( self, reference_get=1.0, reference_set=1.0 ):
def measure(self, reference_get=1.0, reference_set=1.0):
global t0
self.init()

if t0 < 0.0:
t0 = measure( self.null )
t1 = measure( self.do_get )
t2 = measure( self.do_set )
t0 = measure(self.null)
t1 = measure(self.do_get)
t2 = measure(self.do_set)

scale = 1.0e6 / n
get_time = max( t1 - t0, 0.0 ) * scale
set_time = max( t2 - t0, 0.0 ) * scale
get_time = max(t1 - t0, 0.0) * scale
set_time = max(t2 - t0, 0.0) * scale

return get_time, set_time

def null ( self ):
def null(self):
for i in range(n):
pass

def init ( self ):
def init(self):
self.value = -1

def do_set ( self ):
def do_set(self):
for i in range(n):
self.value = i

def do_get ( self ):
def do_get(self):
for i in range(n):
self.value

#-------------------------------------------------------------------------------
# 'New style' Python attribute get/set:
#-------------------------------------------------------------------------------

class new_style_value ( object ):
# 'New style' Python attribute get/set:
class new_style_value(object):

def measure ( self ):
def measure(self):
global t0
self.init()

if t0 < 0.0:
t0 = measure( self.null )
t1 = measure( self.do_get )
t2 = measure( self.do_set )
t0 = measure(self.null)
t1 = measure(self.do_get)
t2 = measure(self.do_set)

scale = 1.0e6 / n
get_time = max( t1 - t0, 0.0 ) * scale
set_time = max( t2 - t0, 0.0 ) * scale
get_time = max(t1 - t0, 0.0) * scale
set_time = max(t2 - t0, 0.0) * scale

return get_time, set_time

def null ( self ):
def null(self):
for i in range(n):
pass

def init ( self ):
def init(self):
self.value = -1

def do_set ( self ):
def do_set(self):
for i in range(n):
self.value = i

def do_get ( self ):
def do_get(self):
for i in range(n):
self.value

#-------------------------------------------------------------------------------
# Python 'property' get/set:
#-------------------------------------------------------------------------------

class property_value ( new_style_value ):
# Python 'property' get/set:
class property_value(new_style_value):

def get_value ( self ):
def get_value(self):
return self._value

def set_value ( self, value ):
def set_value(self, value):
self._value = value

value = property( get_value, set_value )
value = property(get_value, set_value)

#-------------------------------------------------------------------------------
# Python 'global' get/set:
#-------------------------------------------------------------------------------

class global_value ( new_style_value ):
# Python 'global' get/set:
class global_value(new_style_value):

def init ( self ):
def init(self):
global gvalue
gvalue = -1

def do_set ( self ):
def do_set(self):
global gvalue
for i in range(n):
gvalue = i

def do_get ( self ):
def do_get(self):
global gvalue
for i in range(n):
gvalue

#-------------------------------------------------------------------------------
# Trait that can have any value:
#-------------------------------------------------------------------------------

class any_value ( HasTraits, new_style_value ):
# Trait that can have any value:
class any_value(HasTraits, new_style_value):

value = Any

#-------------------------------------------------------------------------------
# Trait that can only have 'float' values:
#-------------------------------------------------------------------------------

class int_value ( any_value ):
# Trait that can only have 'float' values:
class int_value(any_value):

value = Int

#-------------------------------------------------------------------------------

# Trait that can only have 'range' values:
#-------------------------------------------------------------------------------
class range_value(any_value):

class range_value ( any_value ):
value = Range(-1, 2000000000)

value = Range( -1, 2000000000 )

#-------------------------------------------------------------------------------
# Executes method when float trait is changed:
#-------------------------------------------------------------------------------
class change_value(int_value):

class change_value ( int_value ):

def _value_changed ( self, old, new ):
def _value_changed(self, old, new):
pass

#-------------------------------------------------------------------------------
# Notifies handler when float trait is changed:
#-------------------------------------------------------------------------------

class monitor_value ( int_value ):
# Notifies handler when float trait is changed:
class monitor_value(int_value):

def init ( self ):
self.on_trait_change( self.on_value_change, 'value' )
def init(self):
self.on_trait_change(self.on_value_change, 'value')

def on_value_change ( self, object, trait_name, old, new ):
def on_value_change(self, object, trait_name, old, new):
pass

#-------------------------------------------------------------------------------
# Float trait is delegated to another object:
#-------------------------------------------------------------------------------

class delegate_value ( HasTraits, new_style_value ):
# Float trait is delegated to another object:
class delegate_value(HasTraits, new_style_value):

value = DelegatesTo( 'delegate' )
value = DelegatesTo('delegate')
delegate = Any

def init ( self ):
def init(self):
self.delegate = int_value()

#-------------------------------------------------------------------------------
# Float trait is delegated through one object to another object:
#-------------------------------------------------------------------------------

class delegate_2_value ( delegate_value ):
# Float trait is delegated through one object to another object:
class delegate_2_value(delegate_value):

def init ( self ):
def init(self):
delegate = delegate_value()
delegate.init()
self.delegate = delegate

#-------------------------------------------------------------------------------
# Float trait is delegated through two objects to another object:
#-------------------------------------------------------------------------------

class delegate_3_value ( delegate_value ):
# Float trait is delegated through two objects to another object:
class delegate_3_value(delegate_value):

def init ( self ):
def init(self):
delegate = delegate_2_value()
delegate.init()
self.delegate = delegate

#-------------------------------------------------------------------------------
# Run the timing measurements:
#-------------------------------------------------------------------------------

# Run the timing measurements:
def report(name, get_time, set_time, ref_get_time, ref_set_time):
""" Return string containing a benchmark report.

Expand All @@ -243,15 +205,15 @@ def report(name, get_time, set_time, ref_get_time, ref_set_time):
template = (
'{name:^30}: Get {get_time:02.3f} us (x {get_speed_up:02.3f}), '
'Set {set_time:02.3f} us (x {set_speed_up:02.3f})'
)
)

report = template.format(
name = name,
get_time = get_time,
get_speed_up = ref_get_time / get_time,
set_time = set_time,
set_speed_up = ref_set_time / set_time,
)
name=name,
get_time=get_time,
get_speed_up=ref_get_time / get_time,
set_time=set_time,
set_speed_up=ref_set_time / set_time,
)

return report

Expand All @@ -265,7 +227,6 @@ def run_benchmark(klass, ref_get_time, ref_set_time):


def main():

ref_get_time, ref_set_time = new_style_value().measure()

benchmarks = [
Expand Down
2 changes: 0 additions & 2 deletions traits/tests/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,13 @@ def bar(self):

AbstractFoo.register(FooLike)


class AbstractBar(object):
__metaclass__ = abc.ABCMeta

@abc.abstractmethod
def bar(self):
raise NotImplementedError()


class TestABC(unittest.TestCase):
def test_basic_abc(self):
self.assertRaises(TypeError, AbstractFoo)
Expand Down
Loading