forked from jemn21819/AirBnB_clone
/
base_model.py
executable file
·50 lines (44 loc) · 1.68 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
#!/usr/bin/python3
""""
Module that contain BaseModel class
"""
import models
import uuid
from datetime import datetime
class BaseModel:
"""
BaseModel class for all other classes
"""
def __init__(self, *args, **kwargs):
""" initialization the constructor"""
if kwargs:
for key, value in kwargs.items():
if key != "__class__":
setattr(self, key, value)
if hasattr(self, "created_at") and type(self.created_at) is str:
self.created_at = datetime.strptime(
kwargs["created_at"], "%Y-%m-%dT%H:%M:%S.%f")
if hasattr(self, "updated_at") and type(self.updated_at) is str:
self.updated_at = datetime.strptime(
kwargs["updated_at"], "%Y-%m-%dT%H:%M:%S.%f")
else:
self.id = str(uuid.uuid4())
self.created_at = datetime.now()
self.updated_at = self.created_at
models.storage.new(self)
models.storage.save()
def __str__(self):
"""string represent BaseModel class"""
return "[{}] ({}) {}".format(self.__class__.__name__, self.id,
self.__dict__)
def save(self):
"""update public instance att updated_at to current date"""
self.updated_at = datetime.now()
models.storage.save()
def to_dict(self):
"""return a dictionary with all key/value"""
new_dict = self.__dict__.copy()
new_dict["__class__"] = self.__class__.__name__
new_dict["created_at"] = self.created_at.isoformat()
new_dict["updated_at"] = self.updated_at.isoformat()
return new_dict