Created
April 28, 2024 08:34
-
-
Save 2tony2/4c359bd5e034f58dfb72ebaa710c6cf6 to your computer and use it in GitHub Desktop.
This file contains 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
from abc import ABC, abstractmethod | |
class DatabaseConfig(ABC): | |
@property | |
@abstractmethod | |
def connection_string(self): | |
"""Define a standardized property to get a database connection string.""" | |
pass | |
class ProductionDatabaseConfig(DatabaseConfig): | |
def __init__(self, host, database, user, password): | |
self.host = host | |
self.database = database | |
self.user = user | |
self.password = password | |
@property | |
def connection_string(self): | |
"""Generate a connection string using production credentials.""" | |
return f"Server={self.host};Database={self.database};User Id={self.user};Password={self.password};" | |
class DevelopmentDatabaseConfig(DatabaseConfig): | |
def __init__(self, host, database): | |
self.host = host | |
self.database = database | |
@property | |
def connection_string(self): | |
"""Generate a connection string using development credentials with default user.""" | |
return f"Server={self.host};Database={self.database};User Id=dev;Password=dev;" | |
# Usage example | |
if __name__ == "__main__": | |
# Initialize configurations | |
prod_config = ProductionDatabaseConfig("prod-server", "prod-db", "admin", "securepassword") | |
dev_config = DevelopmentDatabaseConfig("dev-server", "dev-db") | |
# Print connection strings | |
print("Production Connection String:") | |
print(prod_config.connection_string) | |
print("Development Connection String:") | |
print(dev_config.connection_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment