Skip to content

Commit

Permalink
BaseModel class set-up
Browse files Browse the repository at this point in the history
  • Loading branch information
alexaorrico committed Sep 28, 2017
1 parent 80a5abf commit 3512319
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
Empty file added models/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions models/base_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/python3
"""
Contains class BaseModel
"""

from datetime import datetime
import uuid


class BaseModel:
"""The BaseModel class from which future classes will be derived"""
def __init__(self):
"""Initialization of the base model"""
self.id = str(uuid.uuid4())
self.created_at = datetime.now()
self.updated_at = self.created_at

def __str__(self):
"""String representation of the BaseModel class"""
return "[{:s}] ({:s}) {}".format(self.__class__.__name__, self.id,
self.__dict__)

def save(self):
"""updates the attribute 'updated_at' with the current datetime"""
self.updated_at = datetime.now()

def to_dict(self):
"""returns a dictionary containing all keys/values of the instance"""
time = "%Y-%m-%dT%H:%M:%S.%f"
new_dict = self.__dict__.copy()
new_dict["created_at"] = new_dict["created_at"].strftime(time)
new_dict["updated_at"] = new_dict["updated_at"].strftime(time)
new_dict["__class__"] = self.__class__.__name__
return new_dict
Empty file added tests/__init__.py
Empty file.
Empty file added tests/test_models/__init__.py
Empty file.
Empty file.

0 comments on commit 3512319

Please sign in to comment.