Improved python syntax

This commit is contained in:
Syrus Akbary 2015-11-02 19:28:06 -08:00
parent d46e957863
commit 0a395fec58
2 changed files with 21 additions and 21 deletions

View File

@ -1,10 +1,10 @@
humanData = {}
droidData = {}
human_data = {}
droid_data = {}
def setup():
from .schema import Human, Droid
global humanData, droidData
global human_data, droid_data
luke = Human(
id='1000',
name='Luke Skywalker',
@ -45,7 +45,7 @@ def setup():
home_planet=None,
)
humanData = {
human_data = {
'1000': luke,
'1001': vader,
'1002': han,
@ -69,29 +69,29 @@ def setup():
primary_function='Astromech',
)
droidData = {
droid_data = {
'2000': threepio,
'2001': artoo,
}
def getCharacter(id):
return humanData.get(id) or droidData.get(id)
def get_character(id):
return human_data.get(id) or droid_data.get(id)
def getFriends(character):
return map(getCharacter, character.friends)
def get_friends(character):
return map(get_character, character.friends)
def getHero(episode):
def get_hero(episode):
if episode == 5:
return humanData['1000']
return droidData['2001']
return human_data['1000']
return droid_data['2001']
def getHuman(id):
return humanData.get(id)
def get_human(id):
return human_data.get(id)
def getDroid(id):
return droidData.get(id)
def get_droid(id):
return droid_data.get(id)

View File

@ -2,7 +2,7 @@ import graphene
from graphene import resolve_only_args
from graphql.core.type import GraphQLEnumValue
from .data import getCharacter, getDroid, getHero, getHuman
from .data import get_character, get_droid, get_hero, get_human
Episode = graphene.Enum('Episode', dict(
NEWHOPE=GraphQLEnumValue(4),
@ -19,7 +19,7 @@ class Character(graphene.Interface):
def resolve_friends(self, args, *_):
# The character friends is a list of strings
return [getCharacter(f) for f in self.friends]
return [get_character(f) for f in self.friends]
class Human(Character):
@ -43,15 +43,15 @@ class Query(graphene.ObjectType):
@resolve_only_args
def resolve_hero(self, episode=None):
return getHero(episode)
return get_hero(episode)
@resolve_only_args
def resolve_human(self, id):
return getHuman(id)
return get_human(id)
@resolve_only_args
def resolve_droid(self, id):
return getDroid(id)
return get_droid(id)
Schema = graphene.Schema(query=Query)