forked from kitelife/mpdp-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1f0a4fa
Showing
32 changed files
with
1,991 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
class Frog: | ||
|
||
def __init__(self, name): | ||
self.name = name | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
def interact_with(self, obstacle): | ||
print('{} the Frog encounters {} and {}!'.format(self, | ||
obstacle, obstacle.action())) | ||
|
||
|
||
class Bug: | ||
|
||
def __str__(self): | ||
return 'a bug' | ||
|
||
def action(self): | ||
return 'eats it' | ||
|
||
|
||
class FrogWorld: | ||
|
||
def __init__(self, name): | ||
print(self) | ||
self.player_name = name | ||
|
||
def __str__(self): | ||
return '\n\n\t------ Frog World ———' | ||
|
||
def make_character(self): | ||
return Frog(self.player_name) | ||
|
||
def make_obstacle(self): | ||
return Bug() | ||
|
||
|
||
class Wizard: | ||
|
||
def __init__(self, name): | ||
self.name = name | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
def interact_with(self, obstacle): | ||
print('{} the Wizard battles against {} and {}!'.format(self, obstacle, obstacle.action())) | ||
|
||
|
||
class Ork: | ||
|
||
def __str__(self): | ||
return 'an evil ork' | ||
|
||
def action(self): | ||
return 'kills it' | ||
|
||
|
||
class WizardWorld: | ||
|
||
def __init__(self, name): | ||
print(self) | ||
self.player_name = name | ||
|
||
def __str__(self): | ||
return '\n\n\t------ Wizard World ———' | ||
|
||
def make_character(self): | ||
return Wizard(self.player_name) | ||
|
||
def make_obstacle(self): | ||
return Ork() | ||
|
||
|
||
class GameEnvironment: | ||
|
||
def __init__(self, factory): | ||
self.hero = factory.make_character() | ||
self.obstacle = factory.make_obstacle() | ||
|
||
def play(self): | ||
self.hero.interact_with(self.obstacle) | ||
|
||
|
||
def validate_age(name): | ||
try: | ||
age = input('Welcome {}. How old are you? '.format(name)) | ||
age = int(age) | ||
except ValueError as err: | ||
print("Age {} is invalid, please try \ | ||
again…".format(age)) | ||
return (False, age) | ||
return (True, age) | ||
|
||
|
||
def main(): | ||
name = input("Hello. What's your name? ") | ||
valid_input = False | ||
while not valid_input: | ||
valid_input, age = validate_age(name) | ||
game = FrogWorld if age < 18 else WizardWorld | ||
environment = GameEnvironment(game(name)) | ||
environment.play() | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
[ | ||
{ | ||
"id": "0001", | ||
"type": "donut", | ||
"name": "Cake", | ||
"ppu": 0.55, | ||
"batters": { | ||
"batter": [ | ||
{ | ||
"id": "1001", | ||
"type": "Regular" | ||
}, | ||
{ | ||
"id": "1002", | ||
"type": "Chocolate" | ||
}, | ||
{ | ||
"id": "1003", | ||
"type": "Blueberry" | ||
}, | ||
{ | ||
"id": "1004", | ||
"type": "Devil's Food" | ||
} | ||
] | ||
}, | ||
"topping": [ | ||
{ | ||
"id": "5001", | ||
"type": "None" | ||
}, | ||
{ | ||
"id": "5002", | ||
"type": "Glazed" | ||
}, | ||
{ | ||
"id": "5005", | ||
"type": "Sugar" | ||
}, | ||
{ | ||
"id": "5007", | ||
"type": "Powdered Sugar" | ||
}, | ||
{ | ||
"id": "5006", | ||
"type": "Chocolate with Sprinkles" | ||
}, | ||
{ | ||
"id": "5003", | ||
"type": "Chocolate" | ||
}, | ||
{ | ||
"id": "5004", | ||
"type": "Maple" | ||
} | ||
] | ||
}, | ||
{ | ||
"id": "0002", | ||
"type": "donut", | ||
"name": "Raised", | ||
"ppu": 0.55, | ||
"batters": { | ||
"batter": [ | ||
{ | ||
"id": "1001", | ||
"type": "Regular" | ||
} | ||
] | ||
}, | ||
"topping": [ | ||
{ | ||
"id": "5001", | ||
"type": "None" | ||
}, | ||
{ | ||
"id": "5002", | ||
"type": "Glazed" | ||
}, | ||
{ | ||
"id": "5005", | ||
"type": "Sugar" | ||
}, | ||
{ | ||
"id": "5003", | ||
"type": "Chocolate" | ||
}, | ||
{ | ||
"id": "5004", | ||
"type": "Maple" | ||
} | ||
] | ||
}, | ||
{ | ||
"id": "0003", | ||
"type": "donut", | ||
"name": "Old Fashioned", | ||
"ppu": 0.55, | ||
"batters": { | ||
"batter": [ | ||
{ | ||
"id": "1001", | ||
"type": "Regular" | ||
}, | ||
{ | ||
"id": "1002", | ||
"type": "Chocolate" | ||
} | ||
] | ||
}, | ||
"topping": [ | ||
{ | ||
"id": "5001", | ||
"type": "None" | ||
}, | ||
{ | ||
"id": "5002", | ||
"type": "Glazed" | ||
}, | ||
{ | ||
"id": "5003", | ||
"type": "Chocolate" | ||
}, | ||
{ | ||
"id": "5004", | ||
"type": "Maple" | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<persons> | ||
<person> | ||
<firstName>John</firstName> | ||
<lastName>Smith</lastName> | ||
<age>25</age> | ||
<address> | ||
<streetAddress>21 2nd Street</streetAddress> | ||
<city>New York</city> | ||
<state>NY</state> | ||
<postalCode>10021</postalCode> | ||
</address> | ||
<phoneNumbers> | ||
<phoneNumber type="home">212 555-1234</phoneNumber> | ||
<phoneNumber type="fax">646 555-4567</phoneNumber> | ||
</phoneNumbers> | ||
<gender> | ||
<type>male</type> | ||
</gender> | ||
</person> | ||
<person> | ||
<firstName>Jimy</firstName> | ||
<lastName>Liar</lastName> | ||
<age>19</age> | ||
<address> | ||
<streetAddress>18 2nd Street</streetAddress> | ||
<city>New York</city> | ||
<state>NY</state> | ||
<postalCode>10021</postalCode> | ||
</address> | ||
<phoneNumbers> | ||
<phoneNumber type="home">212 555-1234</phoneNumber> | ||
</phoneNumbers> | ||
<gender> | ||
<type>male</type> | ||
</gender> | ||
</person> | ||
<person> | ||
<firstName>Patty</firstName> | ||
<lastName>Liar</lastName> | ||
<age>20</age> | ||
<address> | ||
<streetAddress>18 2nd Street</streetAddress> | ||
<city>New York</city> | ||
<state>NY</state> | ||
<postalCode>10021</postalCode> | ||
</address> | ||
<phoneNumbers> | ||
<phoneNumber type="home">212 555-1234</phoneNumber> | ||
<phoneNumber type="mobile">001 452-8819</phoneNumber> | ||
</phoneNumbers> | ||
<gender> | ||
<type>female</type> | ||
</gender> | ||
</person> | ||
</persons> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import xml.etree.ElementTree as etree | ||
import json | ||
|
||
|
||
class JSONConnector: | ||
|
||
def __init__(self, filepath): | ||
self.data = dict() | ||
with open(filepath, mode='r', encoding='utf-8') as f: | ||
self.data = json.load(f) | ||
|
||
@property | ||
def parsed_data(self): | ||
return self.data | ||
|
||
|
||
class XMLConnector: | ||
|
||
def __init__(self, filepath): | ||
self.tree = etree.parse(filepath) | ||
|
||
@property | ||
def parsed_data(self): | ||
return self.tree | ||
|
||
|
||
def connection_factory(filepath): | ||
if filepath.endswith('json'): | ||
connector = JSONConnector | ||
elif filepath.endswith('xml'): | ||
connector = XMLConnector | ||
else: | ||
raise ValueError('Cannot connect to {}'.format(filepath)) | ||
return connector(filepath) | ||
|
||
|
||
def connect_to(filepath): | ||
factory = None | ||
try: | ||
factory = connection_factory(filepath) | ||
except ValueError as ve: | ||
print(ve) | ||
return factory | ||
|
||
|
||
def main(): | ||
sqlite_factory = connect_to('data/person.sq3') | ||
print() | ||
|
||
xml_factory = connect_to('data/person.xml') | ||
xml_data = xml_factory.parsed_data | ||
liars = xml_data.findall(".//{}[{}='{}']".format('person', | ||
'lastName', 'Liar')) | ||
print('found: {} persons'.format(len(liars))) | ||
for liar in liars: | ||
print('first name: {}'.format(liar.find('firstName').text)) | ||
print('last name: {}'.format(liar.find('lastName').text)) | ||
[print('phone number ({})'.format(p.attrib['type']), | ||
p.text) for p in liar.find('phoneNumbers')] | ||
|
||
print() | ||
|
||
json_factory = connect_to('data/donut.json') | ||
json_data = json_factory.parsed_data | ||
print('found: {} donuts'.format(len(json_data))) | ||
for donut in json_data: | ||
print('name: {}'.format(donut['name'])) | ||
print('price: ${}'.format(donut['ppu'])) | ||
[print('topping: {} {}'.format(t['id'], t['type'])) for t in donut['topping']] | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class A(object): | ||
pass | ||
|
||
if __name__ == '__main__': | ||
a = A() | ||
b = A() | ||
|
||
print(id(a) == id(b)) | ||
print(a, b) |
Oops, something went wrong.