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
furas
  • 134,197
  • 12
  • 106
  • 148
edche
  • 636
  • 6
  • 33
  • I think the code is right. But you should not mention the module name as file name. Also, are you using anaconda? – Siva Sankar Nov 14 '21 at 20:22
  • @SivaSankar I am using pycharm – edche Nov 14 '21 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. – Siva Sankar Nov 14 '21 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 – Joran Beasley Nov 14 '21 at 20:36
  • @SivaSankar there is no error trace. Just I am printing and this returns. – edche Nov 14 '21 at 20:38
  • @JoranBeasley What should I add it? I am sorry I cannot understand – edche Nov 14 '21 at 20:39
  • Don't add it. I asked if in case it is installed. https://stackoverflow.com/a/57595192/11282077 , https://stackoverflow.com/a/63008197/11282077 try these links. Issue is related to pycharm. – Siva Sankar Nov 14 '21 at 20:40
  • @SivaSankar I check it and my setting is the same – edche Nov 14 '21 at 20:46
  • @edche did you check the other answers in that url. Issue is not related to python or django. It is related to the Pc (windows, Mac, vista), python path and python. – Siva Sankar Nov 14 '21 at 20:52
  • @SivaSankar Actually everything just works fine. For example, when I print rep_result inside of the function it is true. I should move the result outside of the scope. Maybe if is there an alternative solution it can work too. – edche Nov 14 '21 at 20:55
  • if you need result from function then you should use `return` – furas Nov 14 '21 at 22:12
  • did you put `elastice_yolla` inside `__init__` ? It is wrong idea. It should be in class but outside `__init__` and then you should first create instance `item = functions.myClass(...)` and later run `result = item.elastice_yolla()` in `setup_wizard` – furas Nov 14 '21 at 22:15

1 Answers1

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.
furas
  • 134,197
  • 12
  • 106
  • 148