-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase_model.py
executable file
·39 lines (35 loc) · 1.1 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
#!/usr/bin/python3
import uuid
from datetime import datetime
import models
class BaseModel:
"""Public Instance here"""
def __init__(self, *args, **kwargs):
"""Initializing"""
if len(kwargs) != 0:
for key, value in kwargs.items():
if key == "__class__":
continue
elif key == "created_at" or key == "updated_at":
self.__dict__[key] = datetime.fromisoformat(value)
else:
self.__dict__[key] = value
else:
self.id = str(uuid.uuid4())
self.created_at = datetime.now()
self.updated_at = datetime.now()
models.storage.new(self)
def __str__(self):
"""To return a string value"""
return f"[{self.__class__.__name__}] ({self.id}) {self.__dict__}"
def save(self):
"""To update to current time"""
self.updated_at = datetime.now()
models.storage.save()
def to_dict(self):
"""returna a dictionary containing all keys and values of __dict__ of the instance"""
dict_copy = self.__dict__.copy()
dict_copy["__class__"] = self.__class__.__name__
dict_copy["created_at"] = self.created_at.isoformat()
dict_copy["updated_at"] = self.updated_at.isoformat()
return dict_copy