Skip to content

Commit c635469

Browse files
author
BASNETANAMIKA
committed
Listcomp & decorators
1 parent 6b7b0f1 commit c635469

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed

Self_Practice_Modules/2Lists.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Can you write a simple list comprehension to convert these names to title case (brad pitt -> Brad Pitt). Or reverse the first and last name?
2+
# Should print(values might change as random):
3+
#
4+
# Arnold teams up with Brad
5+
# Alec teams up with Julian
6+
7+
import random
8+
NAMES = ['arnold schwarzenegger', 'alec baldwin', 'bob belderbos',
9+
'julian sequeira', 'sandra bullock', 'keanu reeves',
10+
'julbob pybites', 'bob belderbos', 'julian sequeira',
11+
'al pacino', 'brad pitt', 'matt damon', 'brad pitt']
12+
13+
caps_name = [name.title() for name in NAMES]
14+
print(caps_name)
15+
16+
def gen_pairs(names=NAMES):
17+
for _ in range(len(names)):
18+
a = random.randint(0, len(names)-1)
19+
first_name = names[a].split()[0]
20+
b = random.randint(0, len(names)-1)
21+
second_name = names[b].split()[0]
22+
final_string = first_name + " is with " + second_name
23+
yield final_string
24+
25+
26+
pairs = gen_pairs()
27+
for _ in range(10):
28+
print(next(pairs))
29+

Self_Practice_Modules/Decorators.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from functools import wraps
2+
3+
4+
def decorator_without_args(function):
5+
# import pdb; pdb.set_trace()
6+
@wraps(function)
7+
def new_inner():
8+
print("entering", function.__name__)
9+
function()
10+
print("exiting", function.__name__)
11+
return new_inner
12+
13+
14+
def log_decorator(log_enabled):
15+
def actual_decorator(func):
16+
wraps(func)
17+
def wrapper(*args, **kwargs):
18+
if log_enabled:
19+
print("Calling Function: " + func.__name__)
20+
return func(*args, **kwargs)
21+
return wrapper
22+
return actual_decorator
23+
24+
25+
@log_decorator(True)
26+
def print_args(*args):
27+
for arg in args:
28+
print(arg)
29+
30+
31+
# print_args(1, 2, 3)
32+
33+
34+
def make_html(element):
35+
def actual_decorator(func):
36+
wraps(func)
37+
def wrapper(*args,**kwargs):
38+
print("starting func: " + func.__name__)
39+
start_tag = "<" + element +">"
40+
end_tag = "</"+ element + ">"
41+
# print(args)
42+
new_args = [start_tag + arg + end_tag for arg in args ]
43+
# print(new_args)
44+
for key in kwargs.keys():
45+
kwargs[key] = start_tag + kwargs[key] + end_tag
46+
# print(kwargs)
47+
return func(new_args, **kwargs)
48+
return wrapper
49+
return actual_decorator
50+
51+
52+
@make_html('p')
53+
def get_text(text):
54+
print(text)
55+
56+
57+
get_text("YAAAY")
58+
59+
60+
# # PythonDecorators/entry_exit_function.py
61+
# def entry_exit(f):
62+
# def new_f():
63+
# print("Entering", f.__name__)
64+
# f()
65+
# print("Exited", f.__name__)
66+
# return new_f
67+
68+
@decorator_without_args
69+
def func1():
70+
print("inside func1()")
71+
return 1
72+
73+
@decorator_without_args
74+
def func2():
75+
print("inside func2()")
76+
return 1
77+
78+
# func1()
79+
# func2()
80+
#
81+
# print(func1.__name__)

Self_Practice_Modules/RockPaperScissorsGame/CoreLogic.py

Whitespace-only changes.

Self_Practice_Modules/RockPaperScissorsGame/__init__.py

Whitespace-only changes.

Self_Practice_Modules/RockPaperScissorsGame/game.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import itertools
2+
from time import sleep
3+
import random
4+
5+
colour = "red green orange".split()
6+
7+
rotation = itertools.cycle(colour)
8+
9+
10+
def light_rotation(rotation):
11+
for colour in rotation:
12+
if colour == "red":
13+
print("stop - the color is red")
14+
sleep(random.randint(3,7))
15+
elif colour == "orange":
16+
print("caution!!")
17+
sleep(random.randint(3,7))
18+
else:
19+
print("GO")
20+
sleep(random.randint(3,7))
21+
22+
if __name__=='__main__':
23+
light_rotation(rotation)

0 commit comments

Comments
 (0)