'Using the methods in 1 python file in another pyspark file

Im trying to inherit the method defined in the below python code file to another pyspark file. Running my code in Python3 environment.

File >> email_notifications.py

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
    
class notification(object):
    def __init__(self, to, subject, email_body):
        self.to         = to
        self.from_addr  = to
        self.subject    = subject
        self.email_body = email_body    

    def send_email(self):
        recipient = self.to
        sender = self.from_addr
        subject = subj
        msg = MIMEMultipart()
        msg['Subject'] = subject
        msg['From'] = sender
        msg['To'] = recipient
        msg.attach(MIMEText(self.email_body))
        session = smtplib.SMTP('localhost')
        session.sendmail(sender, recipient, msg.as_string())
        session.quit()

File >> data_check.py

from pyspark import SparkContext, SparkConf
import email_notifications

class data_notify(object):
    def __init__(self):
        
        self.spark = SparkSession \
                            .builder \
                            .appName ( "POC" ) \
                            .config ( "spark.serializer", "org.apache.spark.serializer.KryoSerializer" ) \
                            .config ( "spark.shuffle.compress", "true" ) \
                            .enableHiveSupport ().getOrCreate ()
                            
                            
    def main(self):
        get_object = email_notifications.notification()
        send_email = ge_object.send_email()
        email_to = '[email protected]'
        spark = self.spark
        df = spark.sql("select * from db.spark_table")
        val = df.count()
        
        if val = 0:
            print("load is not complete")
            sub = 'Check data load'
            body = "the count of xxx table is : " + str(val) + "\nPlease validate "
            send_email(email_to,sub,body)
        else:
            print("Completed successfully")
            
if __name__ == "__main__":
    try:
        print("~~ Running validator app")
        data_notify().main()
    except Exception as e:
        print("##### Error running data_notify")
        raise Exception(e)

But when I do this, I'm getting errors like

__init__() takes exactly 4 arguments (1 given)

or

__init__() missing 3 required positional arguments: 'to' , 'subject' and 'email_body'

Can someone please help me with this.

Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source