0

I created a class in my Django project and I call it from views. I need a result of a function in this class but I cannot return the array. I tried to equalize a array from outside but it returns:

<module 're' from 'C:\\Users\\edeni\\AppData\\Local\\Programs\\Python\\Python39\\lib\\re.py'>

How can I use this array in my views?

views.py

    def setup_wizard(request):
      ...
      functions.myClass(setup.n_username, setup.n_password,
                                              setup.n_url, setup.n_port, setup.db_password,
                                              username=request.user.username)  

functions.py

class myClass():

    def __init__(self, n_user, n_password, n_url, n_port, db_password, username):
        ...
        self.elastice_yolla(username)
        ...

        def elastice_yolla(self, username):
            self.report_final = []
            array_length = len(glob.glob(self.location + "\*\*.nessus"))
            self.static_fields = dict()
            for in_file in glob.glob(self.location + "\*\*.nessus"):
                try:
                    i = 0   
                    with open(in_file) as f:
                        if os.path.getsize(in_file) > 0:
                            np = NES2(in_file, self.index_name, self.static_fields, username)
                            report_final = np.toES()
                            time.sleep(0.02)

                    i = i + 1
                except:
                    pass

        print("report")
        print(report_final)

class NES2:
  def __init__(self, input_file, index_name, static_fields, username):
  ....

  def toES(self):
    ...
    for ...
        for ...
                            try:
                                if bas['cve']:
                                self.es.index(index=self.index_name, doc_type="_doc", body=json.dumps(bas))
                                rep_result.append(json.dumps(bas))
                            except KeyError:
                                pass
  return rep_result
12
  • I think the code is right. But you should not mention the module name as file name. Also, are you using anaconda? Commented Nov 14, 2021 at 20:22
  • @SivaSankar I am using pycharm Commented Nov 14, 2021 at 20:32
  • No, anaconda is a distribution of python. If it is installed, we don't have to install python separately. Would you post the full stack trace of the error. Commented Nov 14, 2021 at 20:36
  • there is not enough here to reproduce your issue... so there is also unlikely to be enough here to answer your question Commented Nov 14, 2021 at 20:36
  • @SivaSankar there is no error trace. Just I am printing and this returns. Commented Nov 14, 2021 at 20:38

1 Answer 1

1

I don't know what is your real problem but I think you should organize code in different way.

elastice_yolla() shouldn't be defined inside __init__ and it shouldn't be executed in __init_, and it should use return report_final

And then in setup_wizard you can creat instance and run elastice_yolla()

my_object = functions.myClass(...)
report = my_object.elastice_yolla()

def setup_wizard(request):
      ...
      my_object = functions.myClass(setup.n_username, setup.n_password,
                                              setup.n_url, setup.n_port, setup.db_password,
                                              username=request.user.username)  

      report = my_object.elastice_yolla()
class myClass():

    def __init__(self, n_user, n_password, n_url, n_port, db_password, username):
        ...
        # self.elastice_yolla(username)
        ...

    # outside `__init__`
    def elastice_yolla(self, username)
        report_final = []
        
        array_length = len(glob.glob(self.location + "\*\*.nessus"))
        self.static_fields = dict()
        for in_file in glob.glob(self.location + "\*\*.nessus"):
            try:
                i = 0   
                with open(in_file) as f:
                    if os.path.getsize(in_file) > 0:
                        np = NES2(in_file, self.index_name, self.static_fields, username)
                        report_final = np.toES()
                        time.sleep(0.02)

                i = i + 1
            except Exception as ex:
                print('Exception:', ex)

        return report_final

Eventually you could run elastice_yolla inside __init__ and assing result to class variable with self. and then other function can get this value

class myClass():

    def __init__(self, n_user, n_password, n_url, n_port, db_password, username):
        ...
        self.report_final = self.elastice_yolla(username)
        ...

    # outside `__init__`
    def elastice_yolla(self):  # use self.username
        report_final = []

        array_length = len(glob.glob(self.location + "\*\*.nessus"))
        self.static_fields = dict()
        for in_file in glob.glob(self.location + "\*\*.nessus"):
            try:
                i = 0   
                with open(in_file) as f:
                    if os.path.getsize(in_file) > 0:
                        np = NES2(in_file, self.index_name, self.static_fields, self.username)
                        report_final = np.toES()
                        time.sleep(0.02)

                i = i + 1
            except Exception as ex:
                print('Exception:', ex)

        return report_final
def setup_wizard(request):
      ...
      my_object = functions.myClass(setup.n_username, setup.n_password,
                                              setup.n_url, setup.n_port, setup.db_password,
                                              username=request.user.username)  

      report = my_object.report_final   # get from variable, not from function.

Or you should use self.report_final in elastice_yolla

class myClass():

    def __init__(self, n_user, n_password, n_url, n_port, db_password, username):
        ...
        self.elastice_yolla(username)
        ...

    # outside `__init__`
    def elastice_yolla(self):  # use self.username
        self.report_final = []  # <--- self.

        array_length = len(glob.glob(self.location + "\*\*.nessus"))
        self.static_fields = dict()
        for in_file in glob.glob(self.location + "\*\*.nessus"):
            try:
                i = 0   
                with open(in_file) as f:
                    if os.path.getsize(in_file) > 0:
                        np = NES2(in_file, self.index_name, self.static_fields, self.username)
                        self.report_final = np.toES()  # <--- self.
                        time.sleep(0.02)

                i = i + 1
            except Exception as ex:
                print('Exception:', ex)
def setup_wizard(request):
      ...
      my_object = functions.myClass(setup.n_username, setup.n_password,
                                              setup.n_url, setup.n_port, setup.db_password,
                                              username=request.user.username)  

      report = my_object.report_final   # get from variable, not from function.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.