Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions rolando solution/assignment2CST438.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# all users and posts
users = []
posts = []

# class hloding user info
class User:
def __init__(self, username, displayName, state):
self.username = username
self.displayName = displayName
self.state = state
self.friends = []

def addFriends(self, friends):
self.friends.extend(friends)

def __str__(self):
return f'username: {self.username}\ndisplayName: {self.displayName}\nfriends: {self.friends}'

#class holding post info
class Post:
def __init__(self, postId, username, visibility):
self.postId = postId
self.username = username
self.visibility = visibility

def __str__(self):
return f'post id: {self.postId}\nusername: {self.username}\nvisibility: {self.visibility}'

def processUsers(file):
usersInfo = file.readlines()
for userInfo in usersInfo:
# strip file line and split user info by ;
userInfo.strip()
userSplit = userInfo.split(';')

# add user basic info
user = User(userSplit[0], userSplit[1], userSplit[2])
users.append(user)

# adds friends to user
friendSection = userSplit[3].strip('[]\n')
friends = friendSection.split(',')
user.addFriends(friends)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not do this at line 35 so you can pass it into the constructor?


return users

def processPosts(file):
postsInfo = file.readlines()
for postInfo in postsInfo:
# strip line and add to post object
postInfo.strip()
postSplit = postInfo.split(';')
post = Post(postSplit[0], postSplit[1], postSplit[2].strip('\n'))
posts.append(post)

return posts

# loading data from path
def loadInputData():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separating and using recursion in your functions makes your code very readable and efficient.

try:
userPath = input('Enter user information path: ')
postPath = input('Enter post information path: ')

userFile = open(userPath, 'r')
postFile = open(postPath, 'r')
users = processUsers(userFile)
posts = processPosts(postFile)
except:
print('could not find file')
loadInputData()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice combination of try/catch and recursion


# get user by username
def getUser(username):
for user in users:
if user.username == username:
return user

return None

def canViewPost(post, username):
if post.visibility == 'friend':

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your use of Python as it allows for much simpler code than C++ when working with methods for classes. Will remember this next time for similar projects

user = getUser(post.username)
if post.username == username:
return True

if username in user.friends:
return True

# not in friends list
return False
else:
return True

# get visibility of post with post id and username
def getVisibility():
id = input('Enter post id: ')
username = input('Enter username: ')

for post in posts:
if post.postId == id:
if canViewPost(post, username):
print('Access Permitted\n')
break
else:
print('Access Denied\n')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could rewrite as

print('Access Permitted\n' if canViewPost(post, username) else 'Access Denied\n')

break

def retrievePosts():
username = input('Enter username: ')
viewablePosts = []

# getting all post user can see
for post in posts:
if canViewPost(post, username):
viewablePosts.append(post.postId)

print(f'Viewable posts: {viewablePosts}\n')

# returns list of users with same state as parameter
def getUsersByState(state):
hits = []
for user in users:
if user.state == state:
hits.append(user.displayName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use list comprehension here - off the top of my head I think it would look something like

hits = [user.displayName for user in users if user.state == state]


return hits

def searchByLocation():
state = input('Enter State: ')
usersInState = getUsersByState(state)
print(f'Users in {state}: {usersInState}\n')

# main program loop
def runProgram():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good separating into functions, your main loop is way cleaner than mine

while True:
print('Assignment 2')
print('1.\tLoad Input Data.')
print('2.\tCheck Visibility.')
print('3.\tRetrieve Posts.')
print('4.\tSearch Users By Location.')
print('5.\tExit.')
try:
selection = int(input("Enter option number(1-5): "))
except:
print('Not a number!\n')
continue

match selection:
case 1:
loadInputData()
print('Data Loaded!\n')
case 2:
getVisibility()
case 3:
retrievePosts()
case 4:
searchByLocation()
case 5:
print('Goodbye!\n')
break
case _:
print('Invalid input, try again.\n')


def main():
runProgram()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly redundant? Could rename runProgram->main


main()

3 changes: 3 additions & 0 deletions rolando solution/post-info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
post1112;goldenlover1;friend
post2123;whiskerwatcher;friend
post3298;petpal4ever;public
3 changes: 3 additions & 0 deletions rolando solution/user-info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
goldenlover1;Jane Doe;CA;[petpal4ever,whiskerwatcher]
whiskerwatcher;John Doe;NY;[goldenlover1]
petpal4ever;Great Name;WV;[goldenlover1]