Reputation: 1001
import praw
import time
class getPms():
r = praw.Reddit(user_agent="Test Bot By /u/TheC4T")
r.login(username='*************', password='***************')
cache = []
inboxMessage = []
file = 'cache.txt'
def __init__(self):
cache = self.cacheRead(self, self.file)
self.bot_run(self)
self.cacheSave(self, self.file)
time.sleep(5)
return self.inboxMessage
def getPms(self):
def bot_run():
inbox = self.r.get_inbox(limit=25)
print(self.cache)
# print(r.get_friends())#this works
for message in inbox:
if message.id not in self.cache:
# print(message.id)
print(message.body)
# print(message.subject)
self.cache.append(message.id)
self.inboxMessage.append(message.body)
# else:
# print("no messages")
def cacheSave(self, file):
with open(file, 'w') as f:
for s in self.cache:
f.write(s + '\n')
def cacheRead(self, file):
with open(file, 'r') as f:
cache1 = [line.rstrip('\n') for line in f]
return cache1
# while True: #threading is needed in order to run this as a loop. Probably gonna do this in the main method though
# def getInbox(self):
# return self.inboxMessage
The exception is:
cache = self.cacheRead(self, self.file)
AttributeError: 'getPms' object has no attribute 'cacheRead'
I am new to working with classes in python and need help with what I am doing wrong with this if you need any more information I can add some. It worked when it was all functions but now that I attempted to switch it to a class it has stopped working.
Upvotes: 0
Views: 157
Reputation: 380
Here are few points:
class A(object):
, class A():
, class A:
are equivalent.
Your class name and class method have the same name. I'm not sure does Python confuse about this or not, but you probably do. You can name your class PMS
and your method get
, for example, so you'll obtain PMS.get(...)
In the present version of indentation cacheRead
and cacheSave
functions are simply inaccessible from init; why not move them to generic class namespace?
When calling member functions, you don't need to specify self
as the first argument since you're already calling the function from this object. So instead of cache = self.cacheRead(self, self.file)
you have to do it like this: cache = self.cacheRead(self.file)
Upvotes: 0
Reputation: 184151
Your cacheRead
function (as well as bot_run
and cacheSave
) is indented too far, so it's defined in the body of your other function getPms
. Thus it is only accessible inside of getPms
. But you're trying to call it from __init__
.
I'm not sure what you're trying to achieve here because getPms
doesn't have anything else in it but three function definitions. As far as I can tell you should just take out the def getPms
line and unindent the three functions it contains so they line up with the __init__
method.
Upvotes: 3