Skip to content

Commit f6c1640

Browse files
committed
Catching up
1 parent 6b72e54 commit f6c1640

File tree

11 files changed

+1125
-12
lines changed

11 files changed

+1125
-12
lines changed

days/19-21-itertools/code/bite_65.ipynb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"import os\n",
1111
"import urllib.request\n",
1212
"import string\n",
13-
"\n",
13+
" \n",
1414
"# PREWORK\n",
1515
"DICTIONARY = os.path.join('/tmp', 'dictionary.txt')\n",
1616
"urllib.request.urlretrieve('http://bit.ly/2iQ3dlZ', DICTIONARY)\n",
@@ -38,6 +38,47 @@
3838
" \n",
3939
" return [list(itertools.permutations(draw,i)) for i in range(2,len(draw)+1)]"
4040
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 14,
45+
"metadata": {},
46+
"outputs": [],
47+
"source": [
48+
"import itertools\n",
49+
"import os\n",
50+
"import urllib.request\n",
51+
"\n",
52+
"# PREWORK\n",
53+
"DICTIONARY = os.path.join('/tmp', 'dictionary.txt')\n",
54+
"urllib.request.urlretrieve('http://bit.ly/2iQ3dlZ', DICTIONARY)\n",
55+
"\n",
56+
"with open(DICTIONARY) as f:\n",
57+
" dictionary = set([word.strip().lower() for word in f.read().split()])\n",
58+
"\n",
59+
"\n",
60+
"def get_possible_dict_words(draw):\n",
61+
" \"\"\"Get all possible words from a draw (list of letters) which are\n",
62+
" valid dictionary words. Use _get_permutations_draw and provided\n",
63+
" dictionary\"\"\"\n",
64+
" permutations = [''.join(word).lower()\n",
65+
" for word in _get_permutations_draw(draw)]\n",
66+
" return set(permutations) & set(dictionary)\n",
67+
"\n",
68+
"\n",
69+
"def _get_permutations_draw(draw):\n",
70+
" \"\"\"Helper to get all permutations of a draw (list of letters), hint:\n",
71+
" use itertools.permutations (order of letters matters)\"\"\"\n",
72+
" for i in range(1, 8):\n",
73+
" yield from list(itertools.permutations(draw, i))"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"metadata": {},
80+
"outputs": [],
81+
"source": []
4182
}
4283
],
4384
"metadata": {

days/22-24-decorators/bite_22.ipynb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 20,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from functools import wraps"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 41,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"def make_html(element):\n",
19+
" begin = f'<{element}>'\n",
20+
" end = f'</{element}>'\n",
21+
" def decorator(func):\n",
22+
" @wraps(func)\n",
23+
" def wrapper(*args, **kwargs):\n",
24+
" value = begin + func(*args, **kwargs) + end\n",
25+
" return value\n",
26+
" return wrapper\n",
27+
" return decorator"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 42,
33+
"metadata": {},
34+
"outputs": [
35+
{
36+
"data": {
37+
"text/plain": [
38+
"'<p><strong>I code with PyBites</strong></p>'"
39+
]
40+
},
41+
"execution_count": 42,
42+
"metadata": {},
43+
"output_type": "execute_result"
44+
}
45+
],
46+
"source": [
47+
"@make_html('p')\n",
48+
"@make_html('strong')\n",
49+
"def get_text(text='I code with PyBites'):\n",
50+
" return text\n",
51+
"\n",
52+
"get_text()"
53+
]
54+
}
55+
],
56+
"metadata": {
57+
"kernelspec": {
58+
"display_name": "Python 3",
59+
"language": "python",
60+
"name": "python3"
61+
},
62+
"language_info": {
63+
"codemirror_mode": {
64+
"name": "ipython",
65+
"version": 3
66+
},
67+
"file_extension": ".py",
68+
"mimetype": "text/x-python",
69+
"name": "python",
70+
"nbconvert_exporter": "python",
71+
"pygments_lexer": "ipython3",
72+
"version": "3.6.7"
73+
}
74+
},
75+
"nbformat": 4,
76+
"nbformat_minor": 2
77+
}

days/22-24-decorators/decorators.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,9 @@
632632
],
633633
"metadata": {
634634
"kernelspec": {
635-
"display_name": "venv",
635+
"display_name": "Python 3",
636636
"language": "python",
637-
"name": "venv"
637+
"name": "python3"
638638
},
639639
"language_info": {
640640
"codemirror_mode": {
@@ -646,7 +646,7 @@
646646
"name": "python",
647647
"nbconvert_exporter": "python",
648648
"pygments_lexer": "ipython3",
649-
"version": "3.6.1"
649+
"version": "3.6.7"
650650
}
651651
},
652652
"nbformat": 4,

days/22-24-decorators/scratch.ipynb

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 2,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from functools import wraps"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 4,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"def my_decorator(func):\n",
19+
" @wraps(func)\n",
20+
" def wrapper(*args,**kwargs):\n",
21+
" print('before function')\n",
22+
" result = func(*args,**kwargs)\n",
23+
" print('after function')\n",
24+
" return result # just return the result don't execute it \n",
25+
" return wrapper "
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 6,
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"@my_decorator\n",
35+
"def my_func():\n",
36+
" print(\"this is the simple function!\")"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 7,
42+
"metadata": {},
43+
"outputs": [
44+
{
45+
"name": "stdout",
46+
"output_type": "stream",
47+
"text": [
48+
"before function\n",
49+
"this is the simple function!\n",
50+
"after function\n"
51+
]
52+
}
53+
],
54+
"source": [
55+
"my_func()"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"metadata": {},
62+
"outputs": [],
63+
"source": []
64+
}
65+
],
66+
"metadata": {
67+
"kernelspec": {
68+
"display_name": "Python 3",
69+
"language": "python",
70+
"name": "python3"
71+
},
72+
"language_info": {
73+
"codemirror_mode": {
74+
"name": "ipython",
75+
"version": 3
76+
},
77+
"file_extension": ".py",
78+
"mimetype": "text/x-python",
79+
"name": "python",
80+
"nbconvert_exporter": "python",
81+
"pygments_lexer": "ipython3",
82+
"version": "3.6.7"
83+
}
84+
},
85+
"nbformat": 4,
86+
"nbformat_minor": 2
87+
}

days/22-24-decorators/scratch_3.ipynb

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"Create a logging decorator"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 14,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"from functools import wraps\n",
17+
"import logging\n",
18+
"from datetime import datetime\n",
19+
"import os\n",
20+
"import time"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 15,
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"# Get CWD\n",
30+
"log_dir = os.getcwd() + '/logs/'\n",
31+
"\n",
32+
"# Create logs folder if it doesn't exist\n",
33+
"if not os.path.exists(log_dir):\n",
34+
" os.makedirs(log_dir)\n",
35+
"\n",
36+
"# Set logging\n",
37+
"logging.basicConfig(\n",
38+
" format='%(asctime)s | %(levelname)s | %(message)s',\n",
39+
" datefmt='%m/%d/%Y %I:%M:%S %p',\n",
40+
" filename=log_dir+datetime.today().strftime('%Y%m%d')+'.log',\n",
41+
" level=logging.INFO)"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 13,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"def timeit(func):\n",
51+
" '''Decorator to time a function'''\n",
52+
" @wraps(func)\n",
53+
" def wrapper(*args, **kwargs):\n",
54+
" \n",
55+
" # before calling the decorated function\n",
56+
" print('== starting timer')\n",
57+
" start = time.time()\n",
58+
" \n",
59+
" # call the decorated function\n",
60+
" func(*args, **kwargs)\n",
61+
" \n",
62+
" # after calling the decorated function\n",
63+
" end = time.time()\n",
64+
" print(f'== {func.__name__} took {int(end-start)} seconds to complete')\n",
65+
" \n",
66+
" return wrapper"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 22,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"def logger(func):\n",
76+
" '''Decorator to log a function'''\n",
77+
" @wraps(func)\n",
78+
" def wrapper(*args, **kwargs):\n",
79+
" \n",
80+
" # before calling the decorated function start the logger\n",
81+
" logging.info(f'Starting {func.__name__} with {args} or {kwargs}')\n",
82+
" start = time.time()\n",
83+
" \n",
84+
" # call the function\n",
85+
" func(*args, **kwargs)\n",
86+
" \n",
87+
" # before calling the decorated function start the logger\n",
88+
" end = time.time()\n",
89+
" logging.info(f'Completed {func.__name__}. Elapsed time: {int(end-start)} seconds.')\n",
90+
" \n",
91+
" return wrapper"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": 23,
97+
"metadata": {},
98+
"outputs": [
99+
{
100+
"name": "stdout",
101+
"output_type": "stream",
102+
"text": [
103+
"(actual function) Done, report links ...\n"
104+
]
105+
}
106+
],
107+
"source": [
108+
"@logger\n",
109+
"def generate_report(seconds):\n",
110+
" '''Function to generate revenue report'''\n",
111+
" time.sleep(seconds)\n",
112+
" print('(actual function) Done, report links ...')\n",
113+
"\n",
114+
"generate_report(10)"
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"execution_count": null,
120+
"metadata": {},
121+
"outputs": [],
122+
"source": []
123+
}
124+
],
125+
"metadata": {
126+
"kernelspec": {
127+
"display_name": "Python 3",
128+
"language": "python",
129+
"name": "python3"
130+
},
131+
"language_info": {
132+
"codemirror_mode": {
133+
"name": "ipython",
134+
"version": 3
135+
},
136+
"file_extension": ".py",
137+
"mimetype": "text/x-python",
138+
"name": "python",
139+
"nbconvert_exporter": "python",
140+
"pygments_lexer": "ipython3",
141+
"version": "3.6.7"
142+
}
143+
},
144+
"nbformat": 4,
145+
"nbformat_minor": 2
146+
}

0 commit comments

Comments
 (0)