-
Notifications
You must be signed in to change notification settings - Fork 1
/
base_model.py
executable file
·60 lines (52 loc) · 2.03 KB
/
base_model.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
#!/usr/bin/python3
"""Module that defines all common attributes/methods for other classes """
from uuid import uuid4
from datetime import datetime
import models
class BaseModel:
"""Defines all common attributes/methods for other classes"""
def __init__(self, *args, **kwargs):
"""Initializes the BaseModel instance attributes
Args:
*args: tuple of positional arguments
**kwargs: dict of keyword arguments
"""
if kwargs:
for key, value in kwargs.items():
if key == "created_at" or key == "updated_at":
setattr(self, key, datetime.strptime(value,
"%Y-%m-%dT%H:%M:%S.%f"))
elif key == "__class__":
continue
else:
setattr(self, key, value)
else:
self.id = str(uuid4())
self.created_at = datetime.now()
self.updated_at = datetime.now()
models.storage.new(self)
def __str__(self):
"""returns [<class name>] (<self.id>) <self.__dict__>
"""
return "[{:s}] ({:s}) {}".format(self.__class__.__name__, self.id,
self.__dict__)
def __repr__(self):
"""returns [<class name>] (<self.id>) <self.__dict__>
"""
return "[{:s}] ({:s}) {}".format(self.__class__.__name__, self.id,
self.__dict__)
def save(self):
"""updates the public instance attribute
updated_at with the current datetime
"""
self.updated_at = datetime.now()
models.storage.save()
def to_dict(self):
"""returns a dictionary containing
all keys/values of __dict__ of the instance
"""
new_dict = self.__dict__.copy()
new_dict["__class__"] = type(self).__name__
new_dict["created_at"] = self.created_at.isoformat()
new_dict["updated_at"] = self.updated_at.isoformat()
return new_dict