Skip to content

Commit

Permalink
Use six for metaclass and iters in Python 2/3.
Browse files Browse the repository at this point in the history
  • Loading branch information
seanfisk committed Jan 12, 2014
1 parent 295af2b commit 699249a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
21 changes: 11 additions & 10 deletions ecs/managers.py
@@ -1,5 +1,7 @@
"""Entity and System Managers."""

import six

from ecs.exceptions import (
NonexistentComponentTypeForEntity, DuplicateSystemTypeError)
from ecs.models import Entity
Expand Down Expand Up @@ -68,11 +70,11 @@ def remove_component(self, entity, component_type):
pass

def pairs_for_type(self, component_type):
"""Return a list of ``(entity, component_instance)`` tuples for all
entities in the database possessing a component of ``component_type``.
Return an empty list if there are no components of this type in the
database. Can use in a loop like this, where ``Renderable`` is a
component type:
"""Return an iterator over ``(entity, component_instance)`` tuples for
all entities in the database possessing a component of
``component_type``. Return an empty iterator if there are no components
of this type in the database. It should be used in a loop like this,
where ``Renderable`` is a component type:
.. code-block:: python
Expand All @@ -83,15 +85,14 @@ def pairs_for_type(self, component_type):
:param component_type: a type of created component
:type component_type: :class:`type` which is :class:`Component`
subclass
:return: list of ``(entity, component_instance)`` tuples
:rtype: :class:`tuple` of
:return: iterator on ``(entity, component_instance)`` tuples
:rtype: :class:`iter` on
(:class:`ecs.models.Entity`, :class:`ecs.models.Component`)
"""
try:
# Return a copy of the items for Python 3.
return list(self._database[component_type].items())
return six.iteritems(self._database[component_type])
except KeyError:
return []
return six.iteritems({})

def component_for_entity(self, entity, component_type):
"""Return the instance of ``component_type`` for the entity from the
Expand Down
9 changes: 5 additions & 4 deletions ecs/models.py
Expand Up @@ -3,6 +3,8 @@
from __future__ import print_function
from abc import ABCMeta, abstractmethod

import six


class Entity(object):
"""Encapsulation of a GUID to use in the entity database."""
Expand All @@ -27,12 +29,11 @@ class Component(object):
pass


@six.add_metaclass(ABCMeta)
class System(object):
"""An object that represents an operation on a set of objects from the game
database. The :meth:`update` method must be implemented.
"""
__metaclass__ = ABCMeta

@abstractmethod
def update(self, entity_manager, dt):
"""Run the system for this frame. This method is called by the system
Expand All @@ -44,5 +45,5 @@ def update(self, entity_manager, dt):
:param dt: delta time, or elapsed time for this frame
:type dt: :class:`float`
"""
print("System's update() method was called: "
"entity_manager={0}, dt={1}".format(entity_manager, dt))
six.print_("System's update() method was called: "
'entity_manager={0}, dt={1}'.format(entity_manager, dt))
2 changes: 2 additions & 0 deletions requirements-dev.txt
@@ -1,3 +1,5 @@
--requirement requirements.txt

# Testing
pytest==2.5.1
py==1.4.19
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
@@ -0,0 +1,2 @@
# For Python 2/3 compatibility
six==1.5.2
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -221,7 +221,7 @@ def run_tests(self):
'Topic :: Software Development :: Libraries :: Application Frameworks',
],
packages=find_packages(),
install_requires=[],
install_requires=['six==1.5.2'],
# Allow tests to be run with `python setup.py test'.
tests_require=[
'pytest==2.5.1',
Expand Down
4 changes: 2 additions & 2 deletions tests/test_managers.py
Expand Up @@ -48,14 +48,14 @@ def test_create_new_entities(self, manager):
class TestPairsForType(object):
def test_existing_component_type(
self, manager, entities, components, component_types):
assert manager.pairs_for_type(component_types[0]) == [
assert list(manager.pairs_for_type(component_types[0])) == [
(entities[0], components[0]),
(entities[1], components[5]),
(entities[3], components[0])]

def test_nonexistent_component_type(
self, manager, entities, component_types):
assert manager.pairs_for_type(component_types[2]) == []
assert list(manager.pairs_for_type(component_types[2])) == []

class TestRemoveComponent(object):
def test_remove_some_of_a_component(
Expand Down

0 comments on commit 699249a

Please sign in to comment.