-2

Sorry this is the second post in two days.. I am pulling my hair out with this. I am attempting to take data from reddit and put it into an array in a way I can pull the data out later for tensorflow to parse it. Now the issue is my second object inside of the other object is not giving me whats inside it... "<main.Submission" why am I getting this back?

Goals of this post:

1: Why am I getting <main.Submission> and how should I be doing this.

  File "C:/automation/git/tensorflow/untitled0.py", line 35, in <module>
    submissions[sm.id].addSubSubmission(Submission.addComment(cmt.id, cmt.author.name, cmt.body))

TypeError: addComment() missing 1 required positional argument: 'body'

Sorry for the long winded and most likely basic questions. Going from powershell to python was not as straight forward as I thought..

Thanks Cody

error and data back

import praw

# sets log in data for session
reddit = praw.Reddit(client_id='bY',
                     client_secret='v9',
                     user_agent='android:com.example.myredditapp:'
                     'v1.2.3 (by /u/r)')

class Submission(object):
    def __init__(self, id, title, author):
        self.id = id
        self.title = title
        self.subSubmission = {}
        self.author = author

    def addComment(self, id, author, body):
        self.id = id
        self.author = author
        self.body = body

    def addSubSubmission(self,submission):
        self.subSubmission[submission,id] = submission

    def getSubSubmission(self,id):
        return self.subSubmission[id]

submissions = {}
for sm in reddit.subreddit('redditdev').hot(limit=2):
    # pulls the ID and makes that the head of each
    submissions[sm.id] = Submission(sm.id, sm.title, sm.author.name)
    mySubmission = reddit.submission(id=sm.id)
    mySubmission.comments.replace_more(limit=0)
    # Get all the comments and first post and list their id author and body(comment)
    for cmt in mySubmission.comments.list():
        submissions[sm.id].addSubSubmission(Submission.addComment(cmt.id, cmt.author.name, cmt.body))

# My trying to read what all there??!? ##

for key in submissions.keys():
    value = submissions[key]
    print(key, "=", value)

for key, value in submissions.items():
    print(key, "=", value)

expecting to see:

{Title = test {comment.id = 1111 {Comment = 'blah', Author = 'Bob'}}
                   {comment.id = 1112 {Comment = 'blah2', Author = 'Bob2'}}
} 
Mal229
  • 129
  • 1
  • 3
  • 10
  • 1
    `submissions` is a `dict`. You populate that dict with values that are `Submission` objects: `submissions[sm.id] = Submission(sm.id, sm.title, sm.author.name)` ... what *else* were you expecting to see when you accessed one of these objects? – juanpa.arrivillaga Sep 06 '17 at 23:17
  • Note, you've hopefully created some sort of tree-structure (no cycles) where each node, a `Submission` has three values: `id, author, body`. There are many ways of searching through your tree. But you'll have to implement that yourself. – juanpa.arrivillaga Sep 06 '17 at 23:22
  • Thanks for getting back. Yeah I was expecting to see "Added under #" this sounds like I might be going about this all wrong. thanks for the info – Mal229 Sep 06 '17 at 23:29
  • 1
    Python doesn't implement how to represent your object as a string for you. All objects inherit the basic implementation from `object`. It is up to you to implement that. Or, you *might* just want to use a `dict` of `dict`s to get a feel for things. – juanpa.arrivillaga Sep 06 '17 at 23:36
  • 1
    Also, you really shouldn't ask multiple questions, but as to why `Submission.addComment` is failing, it's because you are using an *instance method* directly from the *class*. You need to use *an instance*. I don't think you are necessarily going about this all wrong, just that you don't seem to have a grasp of OOP in Python. maybe stick to built-in containers like `dict` and `list` if you are trying to just hack something together. – juanpa.arrivillaga Sep 06 '17 at 23:45
  • Ahh sorry about that. Perfect thanks for that! :) – Mal229 Sep 06 '17 at 23:47

1 Answers1

2

It is giving you back the entire Submission object - but then you're printing it. How should a submission object look on screen when printed? This is something you can define in the Submission class - check out the first answer in this post: Difference between __str__ and __repr__ in Python

To explain this further: python doesn't know how to represent a class on screen. Sure, the class has attributes that are strings, lists, dicts etc, but python knows how to print those. Your class you just created? What's important? What should be printed? python doesn't know this, and is smart enough not to make any assumptions.

If you add a __repr__ function to your class, python will call it and print whatever that function returns.

Danielle M.
  • 3,607
  • 1
  • 14
  • 31