Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would dearly love some feedback on the model below. It's a basic commenting structure where users can comment on a variety of objects in the model. I have denormalised it to make querying simpler, but I'm not sure if I've over/under done it. The model tests fine, but it all feels a little clunky.

#####################################################################
# Base ##############################################################
class Base():
    def __init__(self, **kwargs):
        self._ClassName = self.__class__.__name__
        self._Created = datetime.now()
        self.__dict__.update(kwargs)

    def __repr__(self):
        return '<%s>' % self.__class__.__name__

#####################################################################
# Comment ###########################################################
class Comment(Base):
    def __init__(self, username, person, comment, parent, **kwargs):
        self.Username = username
        self.Person = person
        self.Comment = comment
        self.Parent = parent
        self.__dict__.update(kwargs)

    def _save(self):
        #save to Comments
        db.Comments.save(self.__dict__)
        #save to parent collection
        parent_obj = db.dereference(self.Parent)
        query = {'_id':parent_obj['_id']}
        update = {'$addToSet':{'Comments':self.__dict__}}
        db[parent_obj['_Collection']].find_and_modify(query, update, safe=True)
        #save to people collection
        query = {'_id':self.Person.id}
        db.People.find_and_modify(query, update, safe=True)


    def _remove(self):
        #remove from Comments
        query = {'_id':self._id}
        db.Comments.remove(query, safe=True)
        #remove from parent collection
        query = {'Comments._id':self._id}
        update = {'$pull':{'Comments':self.__dict__}}
        db[self.Parent.collection].find_and_modify(query, update, safe=True)
        #remove from people collection
        db.People.find_and_modify(query, update, safe=True)

    def __str__(self):
        return '%s - %s...' % (self.Username, self.Comment[:10])

person = {'Username':'foobarman'}
db.People.save(person, safe=True)

program = {'Title':'the foobar', '_Collection', 'Programs'}
db.Programs.save(program, safe=True)

comment = Comment(
                person['Username'],
                DBRef('People', person['_id']),
                'this is a comment about the foobar program',
                DBRef(program['_Collection'], program['_id']))

comment._save()

# find latest 10 comments
list(db.Comments.find().sort('_Created', -1).limit(10))

# find all comments for username
db.People.find_one({'Username':'foobarman'})['Comments']

# find all comments for program
db.Programs.find({'Title':'the foobar'})['Comments']

# find all comments for program by user
list(db.Comments.find({
                'Parent':DBRef(program['_Collection'], program['_id']),
                'Username':'foobarman'}))
share|improve this question

closed as unclear what you're asking by holroy, mdfst13, Mast, Marc-Andre, Ethan Bierlein Feb 29 at 16:29

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
I cannot run this code as it is written, as I don't know what db.* is. So my question is whether this question is still relevant, and if so what is the db.* stuff? Or if it should be deleted due to no relevancy any more, or closed as broken code? – holroy Nov 27 '15 at 0:08
1  
Please edti your question and add your import lines (as mentioned by @holroy, we don't know what db is) – oliverpool Feb 29 at 9:44
    
Delete the question. – MFB Feb 29 at 23:56
#####################################################################
# Comment ###########################################################

This sort of stuff is useless noise that adds a lot of maintenance.

If your classes are getting hard to find, move them over to separate files. If you don't want to move them to separate files, consider making use of your IDE to find the class you're looking for, rather than relying on optical recognition via big blocks of screenfilling characters.

The reasons for this are twofold:

  • If you refactor the name of the class, the comment doesn't change and you spend time formatting a comment like this, when you could have been doing normal programming
  • If you ever had a name that is longer or wanted to describe a class with a docstring you might break convention and might have to restyle all your classes or risk inconsistencies
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.