/python/space-age/space_age.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import functools


class SpaceAge(object):
    # One Earth year, in seconds
    EARTH_YEAR = 31557600

    # Orbital periods of planets, in Earth years
    ORBITS = {
        'mercury': 0.2408467,
        'venus': 0.61519726,
        'earth': 1.0,
        'mars': 1.8808158,
        'jupiter': 11.862615,
        'saturn': 29.447498,
        'uranus': 84.016846,
        'neptune': 164.79132,
    }

    def __init__(self, seconds):
        self.seconds = seconds

    def _get_years(self, orbital_period):
        """
        Get the number of years in the given orbital period
        """
        return round(self.seconds / (self.EARTH_YEAR * orbital_period), 2)

    def __getattr__(self, name):
        # Supply an implementation for on_<planet> based on _get_years
        # for each planet in ORBITS
        if name.startswith('on_'):
            planet = name[3:]
            if planet in self.ORBITS:
                return functools.partial(self._get_years, self.ORBITS[planet])

        raise AttributeError

Comments

I like the use of __getattr__ here.

yjabri commented 7 April 2015 at 05:48 UTC

You're not logged in right now. Please login via GitHub to comment