Skip to content

Commit 0d29abb

Browse files
committed
class contente added
1 parent 245e233 commit 0d29abb

File tree

5 files changed

+837
-54
lines changed

5 files changed

+837
-54
lines changed

.ipynb_checkpoints/13. Generators and Decorators-checkpoint.ipynb

Lines changed: 203 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,126 @@
192192
"```"
193193
]
194194
},
195+
{
196+
"cell_type": "markdown",
197+
"metadata": {},
198+
"source": [
199+
"### 2. First Class Functions\n",
200+
"\n",
201+
" - treating functions like other normal entities\n",
202+
" - passing function as an argument, assigning to a variable, returning from a function"
203+
]
204+
},
205+
{
206+
"cell_type": "code",
207+
"execution_count": 22,
208+
"metadata": {},
209+
"outputs": [
210+
{
211+
"name": "stdout",
212+
"output_type": "stream",
213+
"text": [
214+
"['Mr Kumar', 'Mrs Sabi']\n"
215+
]
216+
}
217+
],
218+
"source": [
219+
"# EXAMPLE\n",
220+
"\n",
221+
"def append_prefix(data):\n",
222+
" if data[0] == 'male':\n",
223+
" return 'Mr ' + data[1]\n",
224+
" if data[0] == 'female':\n",
225+
" return 'Mrs ' + data[1]\n",
226+
"\n",
227+
" \n",
228+
"def map_function(func, my_list):\n",
229+
" final_output = []\n",
230+
" for each in my_list:\n",
231+
" final_output.append(func(each))\n",
232+
" return final_output\n",
233+
"\n",
234+
"output = map_function(append_prefix, [('male', 'Kumar'), ('female', 'Sabi')])\n",
235+
"print(output)\n",
236+
" "
237+
]
238+
},
239+
{
240+
"cell_type": "markdown",
241+
"metadata": {},
242+
"source": [
243+
"### 3. Closures\n"
244+
]
245+
},
246+
{
247+
"cell_type": "code",
248+
"execution_count": 32,
249+
"metadata": {
250+
"collapsed": true
251+
},
252+
"outputs": [],
253+
"source": [
254+
"def greeting(prefix):\n",
255+
" \n",
256+
" def append_msg(msg):\n",
257+
" print ('{0}{1}{2}'.format(prefix,' ', msg))\n",
258+
" return append_msg ## returning without execution"
259+
]
260+
},
261+
{
262+
"cell_type": "code",
263+
"execution_count": 35,
264+
"metadata": {},
265+
"outputs": [
266+
{
267+
"name": "stdout",
268+
"output_type": "stream",
269+
"text": [
270+
"<function greeting.<locals>.append_msg at 0x7f0c003cd488>\n",
271+
"append_msg\n",
272+
"Mr. Kumar\n",
273+
"Mr. Kabi\n"
274+
]
275+
}
276+
],
277+
"source": [
278+
"first = greeting('Mr.')\n",
279+
"\n",
280+
"print(first)\n",
281+
"print(first.__name__)\n",
282+
"\n",
283+
"# REMEMBER THE PARAMETER 'Mr.' even after outer function is executed already\n",
284+
"first('Kumar')\n",
285+
"first('Kabi')"
286+
]
287+
},
288+
{
289+
"cell_type": "code",
290+
"execution_count": 30,
291+
"metadata": {},
292+
"outputs": [
293+
{
294+
"name": "stdout",
295+
"output_type": "stream",
296+
"text": [
297+
"Mrs. Kumari\n",
298+
"Mrs. Kabita\n"
299+
]
300+
}
301+
],
302+
"source": [
303+
"second = greeting('Mrs.')\n",
304+
"second('Kumari')\n",
305+
"second('Kabita')"
306+
]
307+
},
195308
{
196309
"cell_type": "markdown",
197310
"metadata": {
198311
"collapsed": true
199312
},
200313
"source": [
201-
"### 2. DECORATORS\n",
314+
"### 4. DECORATORS\n",
202315
"\n",
203316
"There may be a scenario to perform any tasks before / after executing a function. This can be achieved through Decorators.\n",
204317
"\n",
@@ -213,7 +326,80 @@
213326
"cell_type": "markdown",
214327
"metadata": {},
215328
"source": [
216-
"#### 2.1 EXAMPLE - 1"
329+
"### Illustration"
330+
]
331+
},
332+
{
333+
"cell_type": "code",
334+
"execution_count": 12,
335+
"metadata": {},
336+
"outputs": [
337+
{
338+
"name": "stdout",
339+
"output_type": "stream",
340+
"text": [
341+
"I am executed before original function\n",
342+
"hey I am wrapped by decorator\n"
343+
]
344+
}
345+
],
346+
"source": [
347+
"# define a decorator \n",
348+
"def decorator_func(original_func):\n",
349+
" def wrapper_func():\n",
350+
" # without modifying original func, I can add more functionality\n",
351+
" print (\"I am executed before original function\")\n",
352+
" return original_func()\n",
353+
" return wrapper_func\n",
354+
"\n",
355+
"# define your working function\n",
356+
"\n",
357+
"@decorator_func\n",
358+
"def my_func():\n",
359+
" print('hey I am wrapped by decorator')\n",
360+
"\n",
361+
" \n",
362+
"my_func()"
363+
]
364+
},
365+
{
366+
"cell_type": "code",
367+
"execution_count": 17,
368+
"metadata": {},
369+
"outputs": [
370+
{
371+
"name": "stdout",
372+
"output_type": "stream",
373+
"text": [
374+
"I am executed before original function\n",
375+
"hey I am wrapped by decorator\n"
376+
]
377+
}
378+
],
379+
"source": [
380+
"# define a decorator \n",
381+
"def decorator_func(original_func):\n",
382+
" def wrapper_func(*args, **kwargs):\n",
383+
" # without modifying original func, I can add more functionality\n",
384+
" print (\"I am executed before original function\")\n",
385+
" return original_func(*args, **kwargs)\n",
386+
" return wrapper_func\n",
387+
"\n",
388+
"# define your working function\n",
389+
"\n",
390+
"@decorator_func\n",
391+
"def my_func_para(param1, param2):\n",
392+
" print('hey I am wrapped by decorator')\n",
393+
"\n",
394+
" \n",
395+
"my_func_para('hey', 'buddy')"
396+
]
397+
},
398+
{
399+
"cell_type": "markdown",
400+
"metadata": {},
401+
"source": [
402+
"#### EXAMPLE - 1"
217403
]
218404
},
219405
{
@@ -374,7 +560,7 @@
374560
"collapsed": true
375561
},
376562
"source": [
377-
"### EXAMPLE - 1"
563+
"### EXAMPLE - 2"
378564
]
379565
},
380566
{
@@ -438,25 +624,32 @@
438624
},
439625
{
440626
"cell_type": "code",
441-
"execution_count": 4,
627+
"execution_count": 18,
442628
"metadata": {},
443629
"outputs": [
444630
{
445631
"name": "stdout",
446632
"output_type": "stream",
447633
"text": [
634+
"inside italic wrapped\n",
635+
"inside bold hello\n",
448636
"<i><b>Hello World</b></i>\n"
449637
]
450638
}
451639
],
452640
"source": [
453641
"def makebold(fn):\n",
642+
" print('makebold called')\n",
454643
" def wrapped():\n",
644+
" print('inside bold', fn.__name__)\n",
645+
" \n",
455646
" return \"<b>%s</b>\" % fn()\n",
456647
" return wrapped\n",
457648
"\n",
458649
"def makeitalic(fn):\n",
459650
" def cover():\n",
651+
" print('inside italic', fn.__name__)\n",
652+
" \n",
460653
" return \"<i>%s</i>\" %fn()\n",
461654
" return cover\n",
462655
"\n",
@@ -499,46 +692,13 @@
499692
},
500693
{
501694
"cell_type": "code",
502-
"execution_count": 4,
503-
"metadata": {},
504-
"outputs": [
505-
{
506-
"name": "stdout",
507-
"output_type": "stream",
508-
"text": [
509-
"third one\n",
510-
"second one\n",
511-
"first one\n",
512-
"Hello, world!\n",
513-
"Hello, solar system!\n",
514-
"Hello, galaxy!\n"
515-
]
516-
}
517-
],
695+
"execution_count": null,
696+
"metadata": {
697+
"collapsed": true
698+
},
699+
"outputs": [],
518700
"source": [
519-
"def testtime(original_function):\n",
520-
" def new_function():\n",
521-
" print('second one')\n",
522-
" #original_function() # the () after \"original_function\" causes original_function to be called\n",
523-
" print(\"Hello, solar system!\")\n",
524-
" return new_function\n",
525-
" \n",
526-
"def testlog(original_function):\n",
527-
" def new_function():\n",
528-
" print('third one')\n",
529-
" #original_function() # the parentheses after \"original_function\" cause original_function to be called\n",
530-
" print(\"Hello, galaxy!\")\n",
531-
" return new_function\n",
532-
" \n",
533-
"\n",
534-
"@testlog\n",
535-
"@testtime\n",
536-
"def call_me():\n",
537-
" print('first one')\n",
538-
" print (\"Hello, world!\")\n",
539-
" \n",
540-
"# Here is where we actually *do* something!\n",
541-
"call_me()"
701+
"def counter()"
542702
]
543703
},
544704
{

0 commit comments

Comments
 (0)