-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_dogs.py
79 lines (54 loc) · 2.27 KB
/
read_dogs.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
""" Import JSON dogs data file to python """
# Import needed packages
import json
# Define Dog class to be used with the imported JSON file
class Dog:
""" Dog class """
def __init__(self, name, breed, color, age):
self.name = name
self.breed = breed
self.color = color
self.age = age
# WHEN IMPORTING, WHAT ABOUT ERRORS FROM INCOMPLETE OBJECTS (ENTRIES)?
# def dog_unserialize(q):
# # strip off name with pop
# # name = q.pop('Name') # no default given, will throw KeyError if no name is provided in the object definition
# cls = dog
# obj = cls.__new__(cls) # new instance
# for key, value in q.items():
# setattr(obj, key, value)
# return obj
# Define exceptions for Dog objects
class DogError(Exception): pass
class NotStringError(DogError): pass
class InvalidAgeError(DogError): pass
class NotNumericError(DogError): pass
class InvalidColorError(DogError): pass
# Define Dog object validation functions
def check_dog_name(dog):
""" Check that an acceptable name is given to a dog """
if not isinstance(dog.name, str):
raise NotStringError("Dog name entered is not a string")
def check_dog_breed(dog):
""" Check that an acceptable breed is given for a dog """
if not isinstance(dog.breed, str):
raise NotStringError("Dog breed entered is not a string")
def check_dog_color(dog):
""" Check that an acceptable color is provided for a dog """
colors = ["White", "Black", "Brown", "Sable", "Gray", "Fawn", "Cream"]
if isinstance(dog.color, str):
if dog.color not in colors:
raise InvalidColorError("Dog color is not in the accepted list of colors")
else:
raise NotStringError("Dog color entered is not a string")
def check_dog_age(dog):
pass
# jsfl = open('dogs.json') # open database of measures
# data = json.load(jsfl, object_hook=dog_unserialize) # object_hook returns dict, object_pairs_hook returns list of tuples
# jsfl.close() # close JSON file
# Validate imported dogs
if __name__ == '__main__':
pass
#####################
#x = json.loads('[{"__type__": "User", "name": "Dave Johnson", "username": "djohnson"},{"__type__": "User", "name": "John Smith", "username": "jsmith"}]', object_hook=object_decoder)