summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2017-01-24 12:02:00 +0100
committerschneefux <schneefux+commit@schneefux.xyz>2017-01-24 12:02:00 +0100
commit3ccef39f3fe7c995abd3a7b429bf8e134661097a (patch)
treebdf801bfe14b111ef888e610c7dc28d8970177b7
parent8b0b01aa4cbb645bfcecc5b6579b34a213389db1 (diff)
downloadpython-gamelocker-3ccef39f3fe7c995abd3a7b429bf8e134661097a.tar.gz
python-gamelocker-3ccef39f3fe7c995abd3a7b429bf8e134661097a.zip
remove string hacks
-rw-r--r--README.md4
-rw-r--r--examples/dashboard/app.py28
-rw-r--r--gamelocker/api.py3
-rw-r--r--gamelocker/datatypes.py11
-rw-r--r--gamelocker/strings.py74
-rw-r--r--tests/gamelocker_test.py28
6 files changed, 43 insertions, 105 deletions
diff --git a/README.md b/README.md
index 6f16b34..c9f9960 100644
--- a/README.md
+++ b/README.md
@@ -17,8 +17,8 @@ Example usage:
[<gamelocker.datatypes.Match object at 0x7f2682314ac8>, <gamelocker.datatypes.Match object at 0x7f26823d3c50>]
>>> m.rosters[0].participants[0].player.name
"iiDruid"
->>> m.rosters[0].participants[0].stats.kills
+>>> m.rosters[0].participants[0].stats["kills"]
10
->>> m.rosters[0].stats.acesEarned
+>>> m.rosters[0].stats["acesEarned"]
2
```
diff --git a/examples/dashboard/app.py b/examples/dashboard/app.py
index 35f6332..f256f10 100644
--- a/examples/dashboard/app.py
+++ b/examples/dashboard/app.py
@@ -59,28 +59,28 @@ def data():
players[participant.player.name] = 0
players[participant.player.name] += 1
- if participant.actor.pretty() not in heroes:
- heroes[participant.actor.pretty()] = 0
- heroes[participant.actor.pretty()] += 1
+ if gamelocker.pretty(participant.actor) not in heroes:
+ heroes[gamelocker.pretty(participant.actor)] = 0
+ heroes[gamelocker.pretty(participant.actor)] += 1
# TODO use id instead of name?
- playersactors[participant.player.name] = participant.actor.pretty()
- cs[participant.player.name] = participant.stats.minionKills / match.duration * 60
- minions += participant.stats.minionKills
+ playersactors[participant.player.name] = gamelocker.pretty(participant.actor)
+ cs[participant.player.name] = participant.stats["minionKills"] / match.duration * 60
+ minions += participant.stats["minionKills"]
- for item in participant.stats.items:
- if item.pretty() in ["Sprint Boots", "Travel Boots", "Journey Boots", "War Treads", "Halcyon Chargers"]:
+ for item in participant.stats["items"]:
+ if gamelocker.pretty(item) in ["Sprint Boots", "Travel Boots", "Journey Boots", "War Treads", "Halcyon Chargers"]:
boots += 1
- for item in participant.stats.itemUses:
- if gamelocker.strings.LazyObject(item).pretty() == "Halcyon Potion":
- potions += participant.stats.itemUses[item]
+ for item in participant.stats["itemUses"]:
+ if gamelocker.pretty(item) == "Halcyon Potion":
+ potions += participant.stats["itemUses"][item]
- for sold in participant.stats.itemSells:
- item = gamelocker.strings.LazyObject(sold).pretty()
+ for sold in participant.stats["itemSells"]:
+ item = gamelocker.pretty(sold)
if not item in sells:
sells[item] = 0
- sells[item] += participant.stats.itemSells[sold]
+ sells[item] += participant.stats["itemSells"][sold]
sells = sorted(sells.items(), key=lambda x: x[1], reverse=True)
data["topsold"] = ", ".join([s[0] for s in sells[0:3]])
diff --git a/gamelocker/api.py b/gamelocker/api.py
index 3f0388f..1220336 100644
--- a/gamelocker/api.py
+++ b/gamelocker/api.py
@@ -197,3 +197,6 @@ class Gamelocker(object):
limit -= max_limit
return matches
+
+
+pretty = gamelocker.strings.pretty
diff --git a/gamelocker/datatypes.py b/gamelocker/datatypes.py
index 74ce965..a5ecadc 100644
--- a/gamelocker/datatypes.py
+++ b/gamelocker/datatypes.py
@@ -9,7 +9,6 @@ Classes and utility functions to map API responses to objects.
import inspect
import sys
-import gamelocker.strings
from gamelocker.janus import DataMessage, Attribute
@@ -44,15 +43,15 @@ class Player(DataMessage):
key_id = attr(str, "id")
name = attr(str, "name")
- stats = attr(gamelocker.strings.Stats, "stats")
+ stats = attr(dict, "stats")
class Participant(DataMessage):
type_name = "participant"
key_id = attr(str, "id")
- actor = attr(gamelocker.strings.Hero, "actor")
- stats = attr(gamelocker.strings.Stats, "stats")
+ actor = attr(str, "actor")
+ stats = attr(dict, "stats")
player = rel(Player, "player")
@@ -68,7 +67,7 @@ class Roster(DataMessage):
type_name = "roster"
key_id = attr(str, "id")
- stats = attr(gamelocker.strings.Stats, "stats")
+ stats = attr(dict, "stats")
participants = rel(Participant, "participants")
team = rel(Team, "team")
@@ -83,7 +82,7 @@ class Match(DataMessage):
gameMode = attr(str, "gameMode")
patchVersion = attr(str, "patchVersion")
region = attr(str, "region")
- stats = attr(gamelocker.strings.Stats, "stats")
+ stats = attr(dict, "stats")
rosters = rel(Roster, "rosters")
diff --git a/gamelocker/strings.py b/gamelocker/strings.py
index 4036c6f..72f1654 100644
--- a/gamelocker/strings.py
+++ b/gamelocker/strings.py
@@ -10,56 +10,7 @@ Currently Vainglory-specific only.
"""
-# this is quite of a hack.
-class Stats(dict):
- """A hybrid between `dict` and `object`
- that types :class:`LazyObject`'s to strings."""
- def __typeit(self, obj):
- if isinstance(obj, (tuple, list)):
- l = []
- for o in obj:
- l.append(self.__typeit(o))
- return l
- if isinstance(obj, dict):
- return Stats(obj)
- if isinstance(obj, str):
- return LazyObject(obj)
- return obj
-
- def __get(self, name):
- # name = LazyObject(name).pretty() # TODO prettify itemUses
- if name in self:
- return self.__typeit(self[name])
- else:
- raise AttributeError("No such attribute: " + name)
-
- def __getattr__(self, name):
- return self.__get(name)
-
- def __setattr__(self, name, value):
- self[name] = value
-
- def __delattr__(self, name):
- if name in self:
- del self[name]
- else:
- raise AttributeError("No such attribute: " + name)
-
- @property
- def items(self):
- return self.__get("items")
-
-class LazyObject(str):
- """Can be both :class:`Hero` or :class:`Item`."""
- def pretty(self):
- for cls in (Hero, Item):
- prettystr = cls(self).pretty()
- if prettystr != self:
- return prettystr
- return self
-
-
-class Hero(str):
+class VaingloryStrings(object):
heroes = {
"*Adagio*": "Adagio",
"*Alpha*": "Alpha",
@@ -93,13 +44,6 @@ class Hero(str):
"*Vox*": "Vox"
}
- def pretty(self):
- if self in self.heroes:
- return self.heroes[self]
- return self
-
-
-class Item(str):
items = {
"Aftershock": "Aftershock",
"Armor2": "Coat of Plates",
@@ -236,9 +180,13 @@ class Item(str):
"*1105_Item_SlumberingHusk*": "Slumbering Husk"
}
- def pretty(self):
- if self in self.items:
- return self.items[self]
- if self in self.item_ids:
- return self.item_ids[self]
- return self
+
+def pretty(string):
+ """Returns prettified hero or item or original string."""
+ if string in VaingloryStrings.heroes:
+ return VaingloryStrings.heroes[string]
+ if string in VaingloryStrings.items:
+ return VaingloryStrings.items[string]
+ if string in VaingloryStrings.item_ids:
+ return VaingloryStrings.item_ids[string]
+ return string
diff --git a/tests/gamelocker_test.py b/tests/gamelocker_test.py
index 36d4ef0..50c1cb7 100644
--- a/tests/gamelocker_test.py
+++ b/tests/gamelocker_test.py
@@ -23,28 +23,16 @@ class TestGamelocker:
assert gamelocker.datatypes.modulemap()["match"] is gamelocker.datatypes.Match
def test_strings(self, api):
- stats = gamelocker.strings.Stats({"foo": 1, "bar": 2, "baz": {"deep": True}})
- assert stats.foo == 1
- assert stats.baz.deep == True
-
- taka = gamelocker.strings.Hero("*Sayoc*")
- assert taka == "*Sayoc*"
- assert taka.pretty() == "Taka"
- assert gamelocker.strings.Hero("notexisting") == "notexisting"
-
- assert gamelocker.strings.Item("Boots2").pretty() == "Travel Boots"
- assert gamelocker.strings.Item("*1032_Item_TravelBoots*").pretty() == "Travel Boots"
- assert gamelocker.strings.Item("unknowntestitem").pretty() == "unknowntestitem"
-
- assert gamelocker.strings.LazyObject("Boots2").pretty() == "Travel Boots"
- assert gamelocker.strings.LazyObject("*Sayoc*").pretty() == "Taka"
+ assert gamelocker.strings.pretty("notexisting") == "notexisting"
+ assert gamelocker.strings.pretty("Boots2") == "Travel Boots"
+ assert gamelocker.strings.pretty("*1032_Item_TravelBoots*") == "Travel Boots"
+ assert gamelocker.strings.pretty("unknowntestitem") == "unknowntestitem"
match = api.match("91cf2ee4-d7d0-11e6-ad79-062445d3d668")
- assert isinstance(match.rosters[0].participants[0].actor, gamelocker.strings.Hero)
+ assert isinstance(match.rosters[0].participants[0].actor, str)
- assert isinstance(match.rosters[0].stats.acesEarned, int)
- assert isinstance(match.rosters[0].participants[0].stats.items[0], gamelocker.strings.LazyObject)
- assert isinstance(match.rosters[0].participants[0].stats.items[0].pretty(), str)
+ assert isinstance(match.rosters[0].stats["acesEarned"], int)
+ assert isinstance(match.rosters[0].participants[0].stats["items"][0], str)
def test_match(self, api):
match = api.match("91cf2ee4-d7d0-11e6-ad79-062445d3d668")
@@ -72,7 +60,7 @@ class TestGamelocker:
commons += 1
assert commons == 2
- assert len(api.matches(limit=52)) == 52
+ assert len(api.matches(limit=42)) == 42
# TODO uncomment as soon as the API is up
# matches = api.matches(limit=10, sort="duration")