Last active
June 28, 2019 15:06
-
-
Save crodriguez1a/8ebe3e9a64f7591e721fde9f0c62ffd1 to your computer and use it in GitHub Desktop.
Lesson - Classes (encapsulation, static methods/pure functions, inheritance)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
STATIC_MAKE_URL = 'https://foo.com/make/' | |
STATIC_CANCEL_URL = 'https://foo.com/cancel/' | |
class AppointmentMaker: | |
__slots__ = ['_status', '_response', '_url'] | |
def make_request(self, url:str, http_method:str='POST') -> dict: # -> instance method | |
# TODO: make a request | |
return {} | |
@staticmethod # -> pure function | |
def format_response(formatting:dict, response:dict) -> str: # -> static method | |
# TODO: apply formatting | |
return formatting # Temp -> str | |
class Appointment(AppointmentMaker): # -> inheritance | |
def __init__(self): | |
super().__init__() | |
self._url = STATIC_MAKE_URL # -> encapsulation | |
def perform(self): | |
self._response = self.make_request(self._url, 'POST') # -> encapsulation | |
@property # -> property decorator | |
def status(self) -> dict: | |
# TODO: create formatting | |
make_format = 'appointment made' | |
return self.format_response(make_format, self._response) | |
class Cancellation(AppointmentMaker): | |
def __init__(self): | |
super().__init__() | |
self._url = STATIC_CANCEL_URL | |
def perform(self) -> dict: | |
self._response = self.make_request(self._url, 'DELETE') | |
@property | |
def status(self) -> dict: | |
# TODO: create formatting | |
cancel_format = 'appointment cancelled' | |
return self.format_response(cancel_format, self._response) | |
cancellation:Cancellation = Cancellation() | |
cancellation_message:dict = cancellation.perform() | |
appointment:Appointment = Appointment() | |
appointment_message:dict = appointment.perform() | |
print(cancellation.status, '||', appointment.status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment