Guest User

Untitled

a guest
Dec 1st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. import pickle
  2. class PostClass:
  3.     def __init__(self,userID,text,Privacy=None,link=None): #we say there MUST be a user, as well as content.
  4.         #if the user is "none", then it's a post from the user to nobody, btw.
  5.         if userID:
  6.             self.Poster = userID #Users use an ID lookup.
  7.         else:
  8.             self.Poster = None
  9.         self.Text    = text    #simple enough, I'd think.
  10.         self.Link    = link    #an optional link to use.
  11.         self.Privacy = Privacy  #1 is default.
  12.     def show(self):
  13.         print(self.Text)
  14.         if self.Link:
  15.             print("External Link: {}".format(self.Link))
  16. class User:
  17.     def __init__(self,Name,passWord,Privacy=1): #an ID of 0 = new user.
  18.             self.Name = Name
  19.             self.Password = passWord
  20.             #the below ( Friends, and Blocked ) are a list of id's.
  21.             self.Friends = [self] #<- Having Friends[0] equal to self, provides some nice functionality. o:
  22.             self.Blocked = []
  23.             self.Requests = []
  24.             self.Posts = []
  25.             self.ID = User.ID #static variable
  26.             User.ID = User.ID + 1
  27.  
  28.     #the naming was difficult, because 'post' can be either a noun, or a verb... :\
  29.     def post(self,text,userID=0,Privacy=None,link=None):
  30.         self.Friends[userID].Posts.insert(0,PostClass(self,text,link))
  31.  
  32.     def showPost(self,postID,viewer,isFeed=True):
  33.         if not isFeed:
  34.             if self.Posts[postID].Privacy:
  35.                 if viewer in Post.Privacy:
  36.                     print("{} posted:".format(self.Name))
  37.                     self.Posts[postID].show()
  38.                     return True
  39.                 else: return False
  40.             else:
  41.                 print("{} posted:".format(self.Name))
  42.                 self.Posts[postID].show()
  43.                 return True
  44.         else:
  45.             for Post in self.Posts:
  46.                 #if Post.Poster:
  47.                 #   continue
  48.                 if Post.Privacy:
  49.                     if viewer in Post.Privacy:
  50.                         print("{} posted:".format(self.Name))
  51.                         Post.show()
  52.                     else: return False
  53.                 else:
  54.                     print("{} posted:".format(self.Name))
  55.                     Post.show()
  56.                     return True
  57.         return false
  58.  
  59.     def showFeed(self,userID=0):
  60.         print("I have {} friends.".format(len(self.Friends)))
  61.         for i in range(1,10):
  62.             if( i >= len(self.Friends) ): break
  63.             print("Friend number {}'s name is {}".format(i,self.Friends[i].Name))
  64.             print("They have {} posts.".format(len(self.Friends[i].Posts)))
  65.         if userID == 0: #home. grab one post from 20 friends.
  66.             for i in range(1,20):
  67.                 if i >= len(self.Friends): break
  68.                 #try until we show one
  69.                 x = 0
  70.                 while not self.Friends[i].showPost(x,self.ID):
  71.                     print("{} is showing feed {}::{}.".format(self.Name, self.Friends[i].Name, x))
  72.                     if len(self.Friends[i].Posts)<=x: break
  73.                     else: x = x + 1
  74.         else:
  75.             for i in range(1,20):
  76.                 if i >= len(self.Friends): break
  77.                 x = 0
  78.                 while not self.Friends[userID].showPost(x,self.ID):
  79.                     if len(self.Friends[userID].Posts)<=x: break
  80.                     else: x = x + 1
  81.     def friend(self,targeted): targeted.Requests.append(self)
  82.     def denyRequest(self,indice): del self.Requests[indice]
  83.     def acceptRequest(self,indice): self.Friends.append(self.Requests.pop(indice).Friends.append(self))
  84. def saveUsers(dat,fname):
  85.     try:
  86.         f = open(fname,'wb')
  87.         pickle.dump(dat,f)
  88.     except IOError as e:
  89.         print("Couldn't open {} for saving the users. :l".format(fname))
  90. def loadUsers(fname):
  91.     try:
  92.         f = open(fname,'rb')
  93.         ret = pickle.load(f)
  94.     except IOError as e:
  95.         ret = []
  96.     return ret
  97. Users = loadUsers('fbdata.dat')
  98. User.ID = 0
  99. import atexit
  100. atexit.register(saveUsers,dat=Users,fname='fbdata.dat')
Add Comment
Please, Sign In to add comment