Skip to content

Commit 0bd5648

Browse files
bite 167
1 parent 5a4bbfc commit 0bd5648

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
/19/README.md
2020
/161/README.md
2121
/165/README.md
22+
/167/README.md

165/passwd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ def get_users_for_shell(passwd_output: str = PASSWD_OUTPUT,
6767
return sorted(users)
6868

6969

70-
# print(get_users_for_shell(passwd_output=OTHER_PASSWD_OUTPUT, grep_shell='ksh'))
70+
# print(get_users_for_shell(passwd_output=OTHER_PASWD_OUTPUT, grep_shell='ksh'))

167/test_user.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from user import User
2+
3+
4+
def test_bob_lowercase():
5+
bob = User('bob', 'belderbos')
6+
assert bob.get_full_name == 'Bob Belderbos'
7+
assert bob.username == 'bbelderb' # lowercase!
8+
assert str(bob) == 'Bob Belderbos (bbelderb)'
9+
assert repr(bob) == 'User("bob", "belderbos")'
10+
11+
12+
def test_julian_mixed_case():
13+
bob = User('julian', 'Sequeira')
14+
assert bob.get_full_name == 'Julian Sequeira'
15+
assert bob.username == 'jsequeir' # lowercase!
16+
assert str(bob) == 'Julian Sequeira (jsequeir)'
17+
assert repr(bob) == 'User("julian", "Sequeira")'
18+
19+
20+
def test_tania_title_name():
21+
bob = User('Tania', 'Courageous')
22+
assert bob.get_full_name == 'Tania Courageous' # aka PyBites Ninja
23+
assert bob.username == 'tcourage' # lowercase!
24+
assert str(bob) == 'Tania Courageous (tcourage)'
25+
assert repr(bob) == 'User("Tania", "Courageous")'
26+
27+
28+
def test_noah_use_dunder_in_repr():
29+
"""Make sure repr does not have the class
30+
name hardcoded.
31+
Also tests for a shorter surname.
32+
"""
33+
class SpecialUser(User):
34+
pass
35+
36+
noah = SpecialUser('Noah', 'Kagan')
37+
assert noah.get_full_name == 'Noah Kagan'
38+
assert noah.username == 'nkagan' # lowercase!
39+
assert str(noah) == 'Noah Kagan (nkagan)'
40+
41+
# it should show the subclass!
42+
assert repr(noah) == 'SpecialUser("Noah", "Kagan")'

167/user.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class User:
2+
"""A User class
3+
(Django's User model inspired us)
4+
"""
5+
6+
def __init__(self, first_name, last_name):
7+
"""Constructor, base values"""
8+
self.first_name = first_name
9+
self.last_name = last_name
10+
11+
@property
12+
def get_full_name(self):
13+
"""Return first separated by a whitespace
14+
and using title case for both.
15+
"""
16+
# TODO 1: you code
17+
return self.first_name.title()+" "+self.last_name.title()
18+
19+
@property
20+
def username(self):
21+
"""A username consists of the first char of
22+
the user's first_name and the first 7 chars
23+
of the user's last_name, both lowercased.
24+
25+
If this is your first property, check out:
26+
https://pybit.es/property-decorator.html
27+
"""
28+
# TODO 2: you code
29+
return self.first_name[0].lower()+self.last_name[0:7].lower()
30+
31+
# TODO 3: you code
32+
#
33+
# add a __str__ and a __repr__
34+
# see: https://stackoverflow.com/a/1438297
35+
# "__repr__ is for devs, __str__ is for customers"
36+
#
37+
# see also TESTS for required output
38+
39+
def __str__(self):
40+
return self.get_full_name+" "+"("+self.username+")"
41+
42+
def __repr__(self):
43+
"""Don't hardcode the class name, hint: use a
44+
special attribute of self.__class__ ...
45+
"""
46+
return self.__class__.__name__+'("'+self.first_name+'", "'+self.last_name+'")'
47+
48+
# bob = User('bob', 'belderbos')
49+
# print(bob.__repr__())

0 commit comments

Comments
 (0)