Code Monkey home page Code Monkey logo

social-network-inheritance's Introduction

Rmotrgram

Today we're going to build a clone of a social network (really similar to the blue bird one, but keep it a secret).

As any other social network we're going to have different types of Posts. All these different types of Posts should inherit from the parent Post class:

  • TextPost: Just a simple text post. Should be constructed as TextPost(text="Post Text").
  • PicturePost: A post containing text and the URL of a picture: Should be constructed as PicturePost(text="Post Text", image_url="imgur.com/OAWTSJu").
  • CheckInPost: A post containing text and coordinates of the user's position. Should be constructed as CheckInPost(text="Post Text", latitude="40.741895", longitude="-73.989308").

Rmotrgram also has users. A user is a simple class that can be created as: User(first_name="John", last_name="Smith", email="[email protected]").

Creating posts

Posts are going to be created and then assigned a user. Once we have our user and post created, we're going to use the add_post method from the user class. add_post should add the post to the user's list of posts, and assign that user to the post. Example, to create a text post for our user John, we'll do something like:

john = User("John", "Lennon", "[email protected]")
text_post = TextPost("All you need is love!")
text_post.user == None  # Important! Since the post has no user yet, the user attribute should be None.

john.add_post(text_post)

text_post.user == john  # Important!
len(john.posts)
>>> 1

As you can see from our previous example, a post is created without a user. It's an "orphan" we might say. By default when a post is created it's user attribute is None. But once we add that post to a user using add_post(), the post's user attribute should be updated, and the post should be added to the user's list of posts.

Following users

Users will be able to follow other users. The follow method is super simple:

john = User("John", "Lennon", "[email protected]")
paul = User("Paul", "McCartney", "[email protected]")

john.follow(paul)
print(john.following)
>>> [<User: "Paul McCartney">]

A user's timeline

This should be almost exactly like twitter. A user will have a timeline, that's just a list of posts created by other users that we're following, sorted by datetime (most recent first).

john = User("John", "Lennon", "[email protected]")
paul = User("Paul", "McCartney", "[email protected]")
george = User("George", "Harrison", "[email protected]")

john.follow(paul)
john.follow(george)

paul.add_post(TextPost("Post 1"))
george.add_post(TextPost("Post 2"))
paul.add_post(TextPost("Post 3"))

print(john.following)
>>>[[<User: "Paul McCartney">], [<User: "George Harrison">]] # John is following Paul and George

print(john.get_timeline())
>>> [<TextPost: Post 3>, <TextPost: Post 2>, <TextPost: Post 1> 

Important: A user's timeline should only include posts from the users they are following.

Reading Posts

Finally, one of the most interesting use cases of this project is going to be realted to the "visual representation" of the posts. It's a great example of Polymorphism. The concept is simple. If I try to print different types of posts, I'm going to get different representations. Example:

john = User("John", "Lennon", "[email protected]")
text_post_1 = TextPost("All you need is love!")
picture_post_2 = PicturePost("Check my new submarine.", image_url='imgur.com/submarine.jpg')
checkin_post_3 = CheckInPost("At Abbey Road Studios", latitude="19.111", longitude="-9.2222")
john.add_post(text_post_1)
john.add_post(picture_post_2)
john.add_post(checkin_post_3)

print(text_post_1)
"""
John Lennon: "All you need is love!"
  Friday, Feb 03, 2017
"""

print(picture_post_2)
"""
John Lennon: "Check my new guitar"
  Pic URL: imgur.com/guitar.png
  Friday, Feb 03, 2017
"""

print(checkin_post_3)
"""
John Checked In: "At Abbey Road Studios"
  19.111, -9.2222
  Friday, Feb 03, 2017
"""

(check tests to see more examples)

social-network-inheritance's People

Contributors

jaybk avatar santiagobasulto avatar martinzugnoni avatar ivanzugnoni avatar jasonmeeks avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.