0

I have two classes and one is using selenium to scrape like 25 different pages and it takes a really long time. I have another class that would take 1 second to run but its calling a variable from the other class. I want self.numbers to equal data.scores and somehow keep the value set so that testing will only take a second. The class that takes forever is AllData() and I want self.numbers to persist without having to copy and paste the printed value.

from collections import defaultdict
from data import *

class Test:
    def __init__(self,):
        data = AllData()
        self.numbers = data.scores
dmo2412
  • 51
  • 5
  • What do you mean by 'keep the value' and 'persist'? – mapf Dec 18 '20 at 22:28
  • As far as persisting data: You can write it to a text file, write it to a database, pickle it, hard-code it into the module you're importing in script, etc. There's not really enough detail in this question about what you've tried for us to know how best to help. Try expanding your question to include a [mcve] – G. Anderson Dec 18 '20 at 22:32
  • What is the type of `data.scores`? – 0x5453 Dec 18 '20 at 22:32
  • data.scores is a dictionary. I want it to set data = data.scores and then have that value persist as in after running it once I want to be able to remove the call to the AllData class. ie: data = AllData.scores = {'num': [1,2,3,4]} and not have to run the file again – dmo2412 Dec 18 '20 at 22:38
  • Are you asking how to save data in a file? – mapf Dec 18 '20 at 22:44
  • ```class Numbers: def __init__(self): self.number = 4 class Other: def __init__(self): num = Numbers() self.persist = num.number # I want persist to equal 4 and then when I call the Other class again, # persist still equals 4 but its as if i set persist to 4 instead # of calling Numbers() in order to get persist to equal 4 – dmo2412 Dec 18 '20 at 22:50
  • Yes mapf, I am trying to save the value – dmo2412 Dec 18 '20 at 22:51
  • 1
    [Well then](https://stackoverflow.com/questions/1389738/how-to-save-data-with-python) – mapf Dec 18 '20 at 23:17
  • Thanks, I'll check this out. From what I've briefly read it seems like it's exactly what I was asking. – dmo2412 Dec 18 '20 at 23:23
  • Does this answer your question? [How to save data with Python?](https://stackoverflow.com/questions/1389738/how-to-save-data-with-python) – mapf Dec 18 '20 at 23:33
  • @dmo2412 Please [edit] your question to include any more details, especially code since you can format it in a comment. – Code-Apprentice Dec 18 '20 at 23:39
  • ``` class AllData: def __init__(self, path='/Users/dannymorgan/Desktop/ESPN/Excel'): if not os.path.exists(path): os.mkdir(path) self.scores = defaultdict(list) class Test: def __init__(self,): data = AllData() self.numbers = data.scores I want to call Test once, which calls AllData(). Now the next time I run Test, I want self.numbers to be equal to the result from when I called AllData() inside Test the first time – dmo2412 Dec 19 '20 at 00:12

1 Answers1

0

I'm not sure I've fully understood. Would using a class variable solve your problem? This means that you can call the current value

class a:
    var = {'init':'has', 'not':'run'}
    
    def __new__(cls):
        cls.var = {'new has':'run'}
        return super(a, cls).__new__(cls) 
    
    def __init__(self):
        self.var['so has'] = 'init'

class b:
    def __init__(self, obj):
        self.sav = obj.var

Example use:

>>> b(a).var
{'init':'has', 'not':'run'}

>>> b(a()).var
{'new has': 'run', 'so has': 'init'}
J.Warren
  • 728
  • 1
  • 4
  • 14
  • I don't think this would work for what I am trying. Every time your run class B it will run class A as well. I am trying to store the value after it has run. I want to rerun class B and not have it call class A again, but store the value from the previous time I ran it. – dmo2412 Dec 19 '20 at 00:08
  • The problem is I’m never directly setting the variable in the AllData class. It takes about 5 minutes to calculate the value every time. – dmo2412 Dec 19 '20 at 00:48
  • It’s not something I will include in production, but it will significantly speed up the debugging process. I could copy and paste it but I just assumed there was a way to actually do it with code. – dmo2412 Dec 19 '20 at 00:53