-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework_7_28092020.py
88 lines (65 loc) · 2.3 KB
/
homework_7_28092020.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from cryptography.fernet import Fernet
def encryptor(func):
def newmessage(message):
message = func(message)
key = Fernet.generate_key()
f = Fernet(key)
encrypted = f.encrypt(message)
return encrypted
return newmessage
def boldfunc(func):
def newbold(message):
print(f"\033[1m{func(message)}\033[0m")
return newbold
def italicfunc(func):
def newitalic(message):
print(f"\x1B[3m{func(message)}\x1B[23m")
return newitalic
def underlinefunc(func):
def newunderline(message):
print(f"\033[4m{func(message)}\033[0m")
return newunderline
@encryptor
def thismessage(message):
return message
def read_namelist():
nameslist = []
with open("names_list.txt", "r", encoding="utf-8") as name:
nameslist += (i.strip("\n") for i in name.readlines())
return nameslist
@boldfunc
def lowercase(nameslist):
"""
1. Given a list of names. Find the total number of names starting with lowercase. Use lambda function.
"""
lowercasenames = list(filter(lambda x: x == x.lower(), nameslist))
#print(f"No. of lowercase names: {len(lowercasenames)}")
return lowercasenames
@underlinefunc
def sortednameslist(nameslist):
"""
2. Sort the words in the list based on their second letter from a to z. Use sorted() and lambda function.
"""
sortednames = sorted(nameslist, key=lambda x: x[1])
return sortednames
@italicfunc
def sortlastcharacter(nameslist):
"""
3. Sort the tuples in the list based on the last character of the second items. Use sorted() and lambda function.
"""
sortedlastcharacters = sorted(nameslist, key=lambda x: x[-1])
return sortedlastcharacters
if __name__ == "__main__":
"""
1. Create three decorator functions namely ⟶ (bold, italic and underline) to decorate a text. Use decorator syntax to use the decorator functions. [HINT: You can use multiple decorators on a function]
"""
"""
2. Write a decorator function that encrypts a given string message. Use following code to encrypt a string message.
"""
encrypted = thismessage("Life is beautiful".encode())
print(encrypted)
nameslist = read_namelist()
print(nameslist)
lowercase(nameslist)
sortednameslist(nameslist)
sortlastcharacter(nameslist)