Home / Expert Answers / Computer Science / python-please-write-a-class-called-nbsp-user-nbsp-which-will-be-used-to-represent-a-user-of-our-mus-pa627

(Solved): python, please Write a class called User which will be used to represent a user of our Mus ...



python, please

Write a class called User which will be used to represent a user of our Music Library web application. The constructor of this class requires user_id, a non-negative integer and two string arguments, username, and password. Internally, the user class stores a list of tracks a user liked (objects of class Track) as well as a list of comments the user has written (objects of class Review). In summary, these are the read only attributes (implement access via Python property mechanism) that are required:

  • user_id (int): If the user id is a negative integer or not an integer, a ValueError is raised. 
  • user_name (str): the user_name attribute should not contain trailing or leading whitespace and is always converted to lowercase! if the argument passed to the constructor is not a valid string, this attribute is set to None
  • password (str): password is stored without any modification, but it has to be at least 7 characters long, set None otherwise.
  • liked_tracks (list of Track objects)
  • reviews (list of Review objects)

 

 

The following methods have to be correctly implemented for the User class:

  • __repr__: defines the unique string representation of the object, see example
  • __eq__: check for equality of two User object instances by comparing the attribute that makes a user unique
  • __lt__: implement a sorting order defined by comparing the attribute that makes a user unique
  • __hash__: defines which attribute is used for computing a hash value as used in set or dictionary keys
  • add_liked_track (track): this method adds the liked track to the list of already liked tracks by this user.
  • remove_liked_track (track): this method removes a track from the list of all liked tracks of this user.
  • add_review (review): this method adds a comment that this user has written to the list of all comments written by this user
  • remove_review (review): this method removes a comment from the list of all comments written by this user

We will test this class with a number of mostly hidden unit tests.

Note: you have to copy four of the classes we have already developed (Artist, Album, Track, Review) into the answer window as well, since the User class depends on them.

 

Some example test cases (may not cover all hidden test cases) -

# Invalid ID type raises error
# Test ID less than 0 raises error
# Test username is all lowercase
# Invalid username type sets username to None
# Invalid password type set password to None
# Password length < 7 sets password to None
# Password of length 7 sets the password correctly
# Check equality with different types
# Test users are successfully added and removed from the set.
# Test it does not add review of invalid types
# Test it does not add the same review twice
# Test removing non-existing review does nothing

 

 

 

For example:

Test Result
user1 = User(123, 'Shyamli', 'pw12345')
user2 = User(345, 'asma', 'pw67890')
user3 = User(567, 'Daniel', 'pw87465')
print(user1)
print(user2)
print(user3)
<User shyamli, user id = 123>
<User asma, user id = 345>
<User daniel, user id = 567>


We have an Answer from Expert

View Expert Answer

Expert Answer


# Source code for User.py: # Class User which will be used to represent a user of our CS235Flix web application class User: def _init_(self, username = str(), pw = str()): self.user_name = username.strip().lower() self.password =
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe