Skip to content

Commit

Permalink
Merge pull request #828 from souravsingh/add-typehint
Browse files Browse the repository at this point in the history
Add Type Hints in Deterministic Cache
  • Loading branch information
meatballs authored Jan 30, 2017
2 parents b91c7cd + df6d5cc commit f4629b2
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions axelrod/deterministic_cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import UserDict
import pickle
from typing import Tuple

from axelrod import Player

Expand Down Expand Up @@ -29,7 +30,7 @@ class DeterministicCache(UserDict):
methods to save/load the cache to/from a file.
"""

def __init__(self, file_name=None):
def __init__(self, file_name: str=None):
"""
Parameters
----------
Expand All @@ -42,7 +43,7 @@ def __init__(self, file_name=None):
self.load(file_name)

@staticmethod
def _key_transform(key):
def _key_transform(key: Tuple[str, str, int]):
"""
Parameters
----------
Expand All @@ -51,16 +52,16 @@ def _key_transform(key):
"""
return key[0].name, key[1].name, key[2]

def __delitem__(self, key):
def __delitem__(self, key: Tuple[str, str, int]):
return super().__delitem__(self._key_transform(key))

def __getitem__(self, key):
def __getitem__(self, key: Tuple[str, str, int]):
return super().__getitem__(self._key_transform(key))

def __contains__(self, key):
def __contains__(self, key: Tuple[str, str, int]):
return super().__contains__(self._key_transform(key))

def __setitem__(self, key, value):
def __setitem__(self, key: Tuple[str, str, int], value):
"""Overrides the UserDict.__setitem__ method in order to validate
the key/value and also to set the turns attribute"""
if not self.mutable:
Expand All @@ -78,7 +79,7 @@ def __setitem__(self, key, value):
super().__setitem__(self._key_transform(key), value)

@staticmethod
def _is_valid_key(key):
def _is_valid_key(key: Tuple[str, str, int]) -> bool:
"""Validate a proposed dictionary key.
Parameters
Expand Down Expand Up @@ -116,7 +117,7 @@ def _is_valid_key(key):
return True

@staticmethod
def _is_valid_value(value):
def _is_valid_value(value) -> bool:
"""Validate a proposed dictionary value.
Parameters
Expand All @@ -133,7 +134,7 @@ def _is_valid_value(value):

return True

def save(self, file_name):
def save(self, file_name: str):
"""Serialise the cache dictionary to a file.
Parameters
Expand All @@ -145,7 +146,7 @@ def save(self, file_name):
pickle.dump(self.data, io)
return True

def load(self, file_name):
def load(self, file_name: str) -> bool:
"""Load a previously saved cache into the dictionary.
Parameters
Expand Down

0 comments on commit f4629b2

Please sign in to comment.