Skip to content

Commit

Permalink
Merge pull request #6 from mgedmin/py3
Browse files Browse the repository at this point in the history
Port pyspacewar to Python 3
  • Loading branch information
mgedmin committed Sep 21, 2016
2 parents 40c4fa3 + 2ee2170 commit c8babd4
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 269 deletions.
11 changes: 4 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ language: python
sudo: false
python:
- 2.7
addons:
apt:
packages:
- python-pygame
virtualenv:
system_site_packages: true
- 3.4
- 3.5
install:
- pip install -U pip
- pip install coverage coveralls -e .
script:
- coverage run --source=src/pyspacewar test.py
- xvfb-run -a coverage run --source=src/pyspacewar test.py
after_success:
- coveralls
notifications:
Expand Down
2 changes: 1 addition & 1 deletion NEWS.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Unreleased:

- No changes yet.
- Support Python 3.4 and 3.5 in addition to 2.7.

September 17, 2016: Released version 1.0.0:

Expand Down
42 changes: 1 addition & 41 deletions RELEASE.rst
Original file line number Diff line number Diff line change
@@ -1,44 +1,4 @@
How to make a PySpaceWar release
================================

(Because I forget)


1. Change the version number in src/pyspacewar/version.py
2. Update NEWS.txt
3. Commit.
4. Tag the release

bzr tag $version

5. Build the tarball and zip file

python setup.py sdist --formats=gztar,zip

6. Test the archives: go to a temporary directory, untar the tarball, run the
game (./pyspacewar), run the unit tests (./test.py), make a sdist again
and check that it is the same as the first one.

7. Upload the tarballs and update the pyspacewar website

python setup.py sdist --formats=gztar,zip register upload
mv dist/* ~/www/pyspacewar/
cd ~/www/pyspacewar
svn add *$version*
vi index.html news.txt
update the News and Download sections at the very least
svn ci

8. Increase version number in src/pyspacewar/version.py, add 'dev' suffix,
commit.

9. Push changes to Launchpad

bzr push

10. Announce the new release on pygame.org, the pygame mailing list, my
weblog. Maybe.

PyGame: log in at http://pygame.org/
The list: pygame-users@seul.org

1. Run 'make release' and follow the instructions.
84 changes: 43 additions & 41 deletions benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
PySpaceWar benchmarks for optimisation work
"""
from __future__ import print_function

import os
import sys
Expand All @@ -16,7 +17,7 @@
def setup_path():
"""Set up python path if running from a source tree."""
pkgdir = os.path.join(os.path.dirname(__file__), 'src')
print pkgdir
print(pkgdir)
if os.path.isdir(pkgdir):
sys.path.insert(0, pkgdir)

Expand All @@ -31,8 +32,8 @@ def setup_path():

def get_cpu_speed():
try:
rows = [row for row in file('/proc/cpuinfo')
if row.startswith('cpu MHz')]
with open('/proc/cpuinfo') as f:
rows = [row for row in f if row.startswith('cpu MHz')]
except IOError:
return 0
try:
Expand All @@ -54,6 +55,7 @@ def control(self):
if len(self.ship.world.objects) < 90:
self.ship.launch()


class DummyTimeSource(object):

delta = 0
Expand All @@ -69,9 +71,9 @@ class Stats(object):
time = 0
ticks = 0
max_objects = 0
min_objects = sys.maxint
min_objects = 1e999
total_objects = 0
best_time = sys.maxint
best_time = 1e999
worst_time = 0

@property
Expand Down Expand Up @@ -214,64 +216,64 @@ def main():
help='use Psyco [default: %default]',
action='store_true', dest='psyco')
opts, args = parser.parse_args()
print "=== Parameters ==="
print
print("=== Parameters ===")
print()
if opts.psyco:
try:
import psyco
psyco.full()
except:
print 'psyco not available'
print('psyco not available')
else:
print 'using psyco'
print 'random seed: %r' % opts.seed
print 'warmup: %d' % opts.warmup
print 'ticks: %d' % opts.ticks
print 'ai: %s' % opts.ai_controller.__name__
print 'benchmark: %s' % opts.benchmark.__name__
print 'debug: %s' % opts.debug
print('using psyco')
print('random seed: %r' % opts.seed)
print('warmup: %d' % opts.warmup)
print('ticks: %d' % opts.ticks)
print('ai: %s' % opts.ai_controller.__name__)
print('benchmark: %s' % opts.benchmark.__name__)
print('debug: %s' % opts.debug)
benchmark = opts.benchmark(opts.seed, opts.ticks, opts.ai_controller,
opts.warmup, opts.profile, opts.debug)
start_time = time.time()
stats = benchmark.run()
total_time = time.time() - start_time
print
print "=== CPU ==="
print
print 'CPU speed before warmup: %.0f MHz' % stats.cpu_speed_before_warmup
print 'CPU speed after warmup: %.0f MHz' % stats.cpu_speed_after_warmup
print 'CPU speed after benchmark: %.0f MHz' % stats.cpu_speed_after_benchmark
print
print "=== Results ==="
print
print 'total time: %.3f seconds' % total_time
print 'ticks: %d' % stats.ticks
print 'objects: min=%d avg=%.1f max=%d' % (
print()
print("=== CPU ===")
print()
print('CPU speed before warmup: %.0f MHz' % stats.cpu_speed_before_warmup)
print('CPU speed after warmup: %.0f MHz' % stats.cpu_speed_after_warmup)
print('CPU speed after benchmark: %.0f MHz' % stats.cpu_speed_after_benchmark)
print()
print("=== Results ===")
print()
print('total time: %.3f seconds' % total_time)
print('ticks: %d' % stats.ticks)
print('objects: min=%d avg=%.1f max=%d' % (
stats.min_objects,
stats.avg_objects,
stats.max_objects)
print 'ticks per second: avg=%.3f' % stats.ticks_per_second
print 'ms per tick: min=%.3f avg=%.3f max=%.3f' % (
stats.max_objects))
print('ticks per second: avg=%.3f' % stats.ticks_per_second)
print('ms per tick: min=%.3f avg=%.3f max=%.3f' % (
stats.best_time * 1000.0,
stats.ms_per_tick,
stats.worst_time * 1000.0)
stats.worst_time * 1000.0))

if opts.profile:
stats = stats.profile_stats
stats.strip_dirs()
print
print "== Stats by internal time ==="
print
print()
print("== Stats by internal time ===")
print()
stats.sort_stats('time', 'calls')
stats.print_stats(40)
print
print "== Stats by number of calls, with callers ==="
print
stats.print(_stats(40))
print()
print("== Stats by number of calls, with callers ===")
print()
stats.sort_stats('calls', 'time')
stats.print_callers(20)
print
print "== Stats by cumulative time, with calees ==="
print
print()
print("== Stats by cumulative time, with calees ===")
print()
stats.sort_stats('cumulative', 'calls')
stats.print_callees(20)

Expand Down
25 changes: 19 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env python
import os
import sys
try:
from setuptools import setup
except ImportError:
from distutils.core import setup

from setuptools import setup


with open('NEWS.rst') as f:
news = f.read()

long_description = """\
Two ships duel in a gravity field. Gravity doesn't affect
Expand All @@ -19,7 +21,7 @@
Latest changes
--------------
""" + '\n\n'.join(file('NEWS.rst').read().split('\n\n')[:2])
""" + '\n\n'.join(news.split('\n\n')[:2])

pkgdir = os.path.join('src', 'pyspacewar')

Expand Down Expand Up @@ -51,10 +53,21 @@ def determine_version():
'Natural Language :: English',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Topic :: Games/Entertainment :: Arcade',
],
scripts=['pyspacewar'],
packages=['pyspacewar'],
package_dir={'': 'src'},
package_data={'pyspacewar': ['images/*', 'sounds/*', 'music/*']},
package_data={
'pyspacewar': [
'icons/*',
'images/*',
'music/*',
'sounds/*',
],
},
install_package_data=True,
install_requires=['pygame'],
)
2 changes: 1 addition & 1 deletion src/pyspacewar/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Written by Ignas Mikalajunas.
"""

from world import Ship
from .world import Ship


class AIController(object):
Expand Down
2 changes: 1 addition & 1 deletion src/pyspacewar/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
import random

from world import World, Vector, Ship, Planet
from .world import World, Vector, Ship, Planet


class PythonTimeSource(object):
Expand Down
27 changes: 14 additions & 13 deletions src/pyspacewar/tests/test_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def callback(*args):
def doctest_AIController_choose_enemy():
"""Tests for AIController.choose_enemy.
>>> from ai import AIController
>>> from world import Ship, World, Object, Vector
>>> from pyspacewar.ai import AIController
>>> from pyspacewar.world import Ship, World, Object, Vector
In a world with only two ships
Expand Down Expand Up @@ -68,8 +68,8 @@ def doctest_AIController_choose_enemy():
def doctest_AIController_control():
"""Tests for AIController.control.
>>> from ai import AIController
>>> from world import Ship, World, Object, Vector
>>> from pyspacewar.ai import AIController
>>> from pyspacewar.world import Ship, World, Object, Vector
>>> ship = Ship(velocity=Vector(30, 0))
>>> world = World()
>>> world.add(ship)
Expand Down Expand Up @@ -98,8 +98,8 @@ def doctest_AIController_control():
def doctest_AIController_target():
"""Tests for AIController.target.
>>> from ai import AIController
>>> from world import Ship, World, Vector
>>> from pyspacewar.ai import AIController
>>> from pyspacewar.world import Ship, World, Vector
>>> ship = Ship(position=Vector(0, 0))
>>> other = Ship(position=Vector(10, 20))
>>> world = World()
Expand Down Expand Up @@ -132,8 +132,8 @@ def doctest_AIController_target():
def doctest_AIController_evade():
"""Tests for AIController.evade.
>>> from ai import AIController
>>> from world import Ship, World, Missile, Planet, Vector
>>> from pyspacewar.ai import AIController
>>> from pyspacewar.world import Ship, World, Missile, Planet, Vector
>>> ship = Ship(position=Vector(0, 0))
>>> other = Ship(position=Vector(10, 20))
>>> sun = Planet(position=Vector(35, 20), radius=5)
Expand Down Expand Up @@ -189,8 +189,8 @@ def doctest_AIController_evade():
def doctest_AIController_get_closest_obstacle():
"""Tests for AIController.get_closest_obstacle.
>>> from ai import AIController
>>> from world import Ship, World, Missile, Planet, Vector
>>> from pyspacewar.ai import AIController
>>> from pyspacewar.world import Ship, World, Missile, Planet, Vector
>>> ship = Ship(position=Vector(30, 0))
>>> missile = Missile(position=Vector(31, 0))
>>> sun = Planet(position=Vector(35, 20), radius=5)
Expand All @@ -213,8 +213,8 @@ def doctest_AIController_get_closest_obstacle():
def doctest_AIController_maybe_fire():
"""Tests for AIController.maybe_fire.
>>> from ai import AIController
>>> from world import Ship, World, Object, Vector
>>> from pyspacewar.ai import AIController
>>> from pyspacewar.world import Ship, World, Object, Vector
>>> ship = Ship()
>>> ship.launch_effect = effect('Foom!')
>>> world = World()
Expand All @@ -236,7 +236,8 @@ def doctest_AIController_maybe_fire():


def test_suite():
path = os.path.join(os.path.dirname(__file__), os.path.pardir)
path = os.path.normpath(
os.path.join(os.path.dirname(__file__), '..', '..'))
if path not in sys.path:
sys.path.append(path)
return doctest.DocTestSuite()
Expand Down
Loading

0 comments on commit c8babd4

Please sign in to comment.