SOLID principle implementation in Python

Here I will share about my learning on SOLID principle and implement those learning in Python.

I use here Python version 3 and Object oriented approach for doing that

Requirement: Need to build a user registration module where the user data should be stored with in SQLite DB and if there is an Error we need to log on to that error and also when registration success user will be notified by and email.

Class UserRegistration():
    def RegisterUser(uname,pwd,email)
    def LogError(error)
    def sendEmail(email,content)

import sqlite3
import json
import smtplib,ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import logging

class User:
    def RegisterUser(self, uname, pwd, email):
        con=sqlite3.connect('sqlite.db')
        sql="insert into Users values ('{0}','{1}','{2}')".format(uname, pwd, email)
        con.execute(sql)
        con.commit()
        print("User registered successfully with {uname} and {pwd}")


class Logger:
    def WriteLogToSystem(self, message):
        logging.Logger(logging.ERROR, message)
            #syslog(syslog.LOG_ERR, message)



class Email:
    def SendEmail(self, to_email, message_content, subject="User Registered"):
        with open('credentials.json') as f:
            data=json.load(f)

        smtp_server="smtp.gmail.com"
        port=465
        sender_email=data["fromuser"]
        password=data["password"]
        context=ssl.create_default_context()
        message=MIMEMultipart("alternative")
        message["From"]=sender_email
        message["To"]=to_email
        message["subject"]=subject
        message_content=f'Hello {message_content} All the best, </br> Best wishes, Zaki Live'
        part=MIMEText(message_content,"html")
        message.attach(part)

        with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
            server.login(sender_email, password)
            server.sendmail(sender_email, to_email, message.as_string())

        print(f"Mail sent to {to_email}")

class Registrations:
    def RegisterUser(self,uname,pwd,email):
        try:
            User().RegisterUser(uname, pwd, email)
            Email().SendEmail(email, 'You have registered!')
        except Exception:
            Logger().WriteLogToSystem('Error while Registering User')

r=Registrations()
r.RegisterUser('zakilive','12345','[email protected]')
{ "fromuser": "[email protected]", "password": }

SQL Query:

CREATE TABLE "Users" (
	"Username"	TEXT,
	"Password"	TEXT,
	"EmaildID"	TEXT,
	PRIMARY KEY("Username")
);

Write a program that take user data and give output in console about a area

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *