patternpythonMinor
'Mini Twitter' in Python 3
Viewed 0 times
twitterpythonmini
Problem
I've written a 'Mini Twitter' (that's what I've called it) in Python. It includes a text-based login screen and user panel where you can create Tweets, and change your username and password.
Users' names, passwords and whether or not it is an administrator is logged in a text file. It also logs Tweets Users have made in a separate .txt file.
There are 4 different .py files and 2 .txt files named: 'Administrator.py', 'main.py', 'Twitter_functions.py' and 'User.py'
Please assess it and let me know how I can improve it.
User.py:
```
#User.py
"""
Zak
July-August 2015
User.py
In here is a class named User that has methods such as makeTweet and setPassword.
There are also other functions that the class User uses.
"""
#strftime makes us able to make a note of the current date.
from time import strftime
#Create the class named User
class User:
'''
Class that acts as an online user and has methods such as 'make_tweet'.
It takes 2 arguments, username and password.
'''
#Init constructor. This is called as soon as an instance is created.
def __init__(self, username, password):
self.__password = password
self.__username = username
self.__creationDate = strftime("%d/%m/%Y") #This is the current date.
#Boolean to show if this class is an Administrator.
self.__isAdmin = False
def __repr__(self):
return "User({}, {})".format(self.__username,self.__creationDate)
#Getters
def get_password(self):
return self.__password
def get_username(self):
return self.__username
def get_creation_date(self):
return self.__creationDate
#Setters
def set_password(self, password):
self.__password = password
def set_username(self, username):
self.__username = username
def make_tweet(self, text):
'''Method To make a tweet. It writes text to a file in a specific format.'''
textLen = len(text)
if textLen > 140: #140 is
Users' names, passwords and whether or not it is an administrator is logged in a text file. It also logs Tweets Users have made in a separate .txt file.
There are 4 different .py files and 2 .txt files named: 'Administrator.py', 'main.py', 'Twitter_functions.py' and 'User.py'
Please assess it and let me know how I can improve it.
User.py:
```
#User.py
"""
Zak
July-August 2015
User.py
In here is a class named User that has methods such as makeTweet and setPassword.
There are also other functions that the class User uses.
"""
#strftime makes us able to make a note of the current date.
from time import strftime
#Create the class named User
class User:
'''
Class that acts as an online user and has methods such as 'make_tweet'.
It takes 2 arguments, username and password.
'''
#Init constructor. This is called as soon as an instance is created.
def __init__(self, username, password):
self.__password = password
self.__username = username
self.__creationDate = strftime("%d/%m/%Y") #This is the current date.
#Boolean to show if this class is an Administrator.
self.__isAdmin = False
def __repr__(self):
return "User({}, {})".format(self.__username,self.__creationDate)
#Getters
def get_password(self):
return self.__password
def get_username(self):
return self.__username
def get_creation_date(self):
return self.__creationDate
#Setters
def set_password(self, password):
self.__password = password
def set_username(self, username):
self.__username = username
def make_tweet(self, text):
'''Method To make a tweet. It writes text to a file in a specific format.'''
textLen = len(text)
if textLen > 140: #140 is
Solution
datastructures
One thing I would do is change the datastructure that stores the users. You want to be able to look up each user for the purposes of authentication/logins/administration/etc quickly. If you are storing the users as a list that is accessed by
Instead you want to choose a more suitable datastructure. Personally I would store the users information in a dictionary, with the login name as the keys and the values would be the tuple containing the other information associated with the user. This will make all operations involving user data manipulation much much faster.
refactoring
In
In the following code:
You have a bare except, this means that if anything at all gets thrown then
Place your user input functionality into it's own function and deal with the vaildation there, if anything fails then report the failure and a different level of the code can try to recover if possible.
Private variables and Python
I can see that you are from a Java background so this one might be a bit of a paradigm shift, but I really think you should avoid using the
One thing I would do is change the datastructure that stores the users. You want to be able to look up each user for the purposes of authentication/logins/administration/etc quickly. If you are storing the users as a list that is accessed by
yielding tuples you will have O(n) lookup time for each user. This will not scale well if you have a lot of users. You don't want to have to check every existing user every time that you need to do something with a user.Instead you want to choose a more suitable datastructure. Personally I would store the users information in a dictionary, with the login name as the keys and the values would be the tuple containing the other information associated with the user. This will make all operations involving user data manipulation much much faster.
refactoring
In
def loggedin(account): I would refactor this so that each option was in it's own function.In the following code:
try:
username = input("Username: ")
except KeyboardInterrupt:
exit()
except:
main()You have a bare except, this means that if anything at all gets thrown then
main is called. Say an OSError is raised, do you really just want to go back to main again? This is why bare except clauses are usually a code smell, they hide bugs and create problems in the future.Place your user input functionality into it's own function and deal with the vaildation there, if anything fails then report the failure and a different level of the code can try to recover if possible.
Private variables and Python
I can see that you are from a Java background so this one might be a bit of a paradigm shift, but I really think you should avoid using the
__variable+getter/setter approach. Python is not like Java, there is no compiler to enforce private variable scope so don't concern yourself too much with it. If you want to signal to people that they should treat things as implementation details the convention is to just use a single underscore to state your intention. The philosophy with Python is that if people misuse your classes that is their responsibility, not yours. Overall I think this is good Python code coming from a Java background. I recently had to go from a very Python heavy base of code to a Java code base at work back again and it's a very different paradigm.Code Snippets
try:
username = input("Username: ")
except KeyboardInterrupt:
exit()
except:
main()Context
StackExchange Code Review Q#101828, answer score: 4
Revisions (0)
No revisions yet.