如何构建模拟评论部分。 [英] How do I build a mock comments section.
本文介绍了如何构建模拟评论部分。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import datetime
class user:
def __init__(self, name):
self.name = name
def name(self):
return self.name
def name(self, value):
self.name = value
def is_logged_in(self):
def last_logged_in_at(self):
def log_in(self):
def log_out(self):
def can_edit(self, comment):
def can_delete(self, comment):
def to_string(self):
class moderator():
def __init__(self, name):
class admin():
def __init__(self, name):
class comment:
def __init__(self, author, message, replied_to):
def author(self):
def author(self, value):
def message(self):
def message(self, value):
def created_at(self):
def replied_to(self):
def replied_to(self, value):
def to_string(self):
Object Oriented Tests
For this challenge, you are going to build a mock comments section.
Design
We're going to focus on two aspects:
Users
Users come in 3 flavors, normal users, moderators, and admins. Normal users can only create new comments, and edit the their own comments. Moderators have the added ability to delete comments (to remove trolls), while admins have the ability to edit or delete any comment.
Users can log in and out, and we track when they last logged in
Comments
Comments are simply a message, a timestamp, and the author.
Comments can also be a reply, so we'll store what the parent comment was.
Your Challenge
This is challenge is not about building a fully functional API, but more about focusing on the design from an object-oriented point-of-view.
We've provided the basic API (which is incomplete), which we would like you to complete being aware of the following Object-Oriented Programming concepts:
Encapsulation of Properties
All classes should have no publicly accessible fields
You should make sure you at least "hide" the required fields, for example, using _name instead of _name. Alternatively, feel free to use a better solution as extra credit.
The method-based API is provided. These must be completed as-is.
Additional methods are allowed, though remember to keep read-only properties read-only.
Instantiation
Classes should be instantiated with properties (as provided), to create instances with values already assigned.
User/Moderator/Admin defaults:
Should be marked as not logged in
Should return None for the last logged in at property
Comment defaults:
Should set the current timestamp for the created at property upon instantiation
Replied To is optional, and should be None if not provided.
Inheritance & Access Control
Note: for the sake of simplicity, you can simply treat object equality as "equal", though more complete solutions will also pass.
User
Users can be logged in and out.
When logging in, set the last_logged_in_at timestamp. Do not modify this timestamp when logging out
Users can only edit their own comments
Users cannot delete any comments
Moderator is a User
Moderators can only edit their own comments
Moderators can delete any comments
Admin is both a User and a Moderator
Admins can edit any comments
Admins can delete any comments
Composition
Comments contain a reference to the User who created it (author)
Comments optionally contain a reference to another comment (replied_to)
When converting to a string (to_string), the following format is used:
No replied to:
message + " by " + author.name
With replied to:
message + " by " + author.name + " (replied to " + repliedTo.author.name + ")"
Beyond these basics, you are free to add to the API, but only these concepts will be scored automatically.
What I have tried:
reading python documentations but i still cant understand what the challenge requires. maybe because i am a beginner
推荐答案
我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。
所以,只需复制'问题'并说我仍然无法理解挑战需要。也许是因为我是一个初学者没有给你一个解决方案。仔细阅读,仔细阅读上一课的笔记,如果你仍然不理解与老师交谈。
自己动手,你可能会发现它并不像你认为!
如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
So just copy'n'paste the question and saying "i still cant understand what the challenge requires. maybe because i am a beginner" doesn;t get you a solution. Read it carefully, go through your notes on your last lecture(s), and if you still don't understand talk to your teacher.
Try it yourself, you may find it is not as difficult as you think!
If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
导入日期时间
类用户:
def __init __(self,name,lastloggedIn = None):
self.name = name
self.loggedIn = False
self.lastloggedIn = lastloggedIn
def name(self):
返回self.name
def名称(自我,价值):
self.name = value
def is_logged_in(个体经营):
返回self.loggedIn
def last_logged_in_at(个体经营):
返回自我。 lastloggedIn
def log_in(self):
self.loggedIn = True
self.lastloggedIn = datetime。 datetime.utcnow()。strftime(%Y-%m-%d%H:%M:%S)
def log_out(self):
self.loggedIn = False
def can_edit(self,comment):
如果comment.author .name == self.name:
返回True
否则:
返回False
def can_delete(self,comment):
返回False
#def to_string(self):
#pass
班主持人(用户):
def __init __(自我,姓名):
user .__ init __(self,name)
def can_delete(self,comment):
返回True
班级管理员(主持人):
def __init __( self,name):
版主.__ init __(自我,姓名)
def can_edit(自我,评论):
返回True
类评论:
def __init __(self,author,message,replied_to =无,createdAt =无):
self.createdAt = datetime.datetime.now()。strftime(%Y-%m-%d%H:%M:%S)
self.author = author
self.mes sage =消息
self.replied_to = replied_to
def author(self):
返回self._author
def author(self,value):
self.author = value
def message(self):
return self.message
def message(self,value):
self.message = value
def created_at(个体经营):
返回self.createdAt
def replied_to(self):
返回self.replied_to
def replied_to(self,value):
self.replied_to = value
def to_string(self):
if self.replied_to == None :
返回self.replied_to +by+ self.author.name
import unittest
user1 = user('User 1')
mod =主持人('主持人')
class Test(unittest.TestCase):
def test_instantiation(self):
self.assertEqual(user1.name,'User 1' ,'用户名设置正确')
user1.name ='用户1已更新'
self.assertEqual(user1.name,'用户1已更新','用户名称可以更新')
self.assertIsInstance(mod,用户,'主持人是用户')
import datetime
class user:
def __init__(self, name, lastloggedIn = None):
self.name = name
self.loggedIn = False
self.lastloggedIn = lastloggedIn
def name(self):
return self.name
def name(self, value):
self.name = value
def is_logged_in(self):
return self.loggedIn
def last_logged_in_at(self):
return self.lastloggedIn
def log_in(self):
self.loggedIn = True
self.lastloggedIn = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
def log_out(self):
self.loggedIn = False
def can_edit(self, comment):
if comment.author.name == self.name:
return True
else:
return False
def can_delete(self, comment):
return False
# def to_string(self):
# pass
class moderator(user):
def __init__(self, name):
user.__init__(self, name)
def can_delete(self, comment):
return True
class admin(moderator):
def __init__(self, name):
moderator.__init__(self, name)
def can_edit(self, comment):
return True
class comment:
def __init__(self, author, message, replied_to = None, createdAt = None):
self.createdAt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.author = author
self.message = message
self.replied_to = replied_to
def author(self):
return self._author
def author(self, value):
self.author = value
def message(self):
return self.message
def message(self, value):
self.message = value
def created_at(self):
return self.createdAt
def replied_to(self):
return self.replied_to
def replied_to(self, value):
self.replied_to = value
def to_string(self):
if self.replied_to == None:
return self.replied_to + " by " + self.author.name
import unittest
user1 = user('User 1')
mod = moderator('Moderator')
class Test(unittest.TestCase):
def test_instantiation(self):
self.assertEqual(user1.name,'User 1', 'User name is set correctly')
user1.name = 'User 1 Updated'
self.assertEqual(user1.name,'User 1 Updated', 'User name can be updated')
self.assertIsInstance(mod, user, 'Moderator is a user')
这篇关于如何构建模拟评论部分。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文