Skip to content

Commit 830d728

Browse files
committed
update Iterator and Common Patterns
1 parent fef3e8b commit 830d728

File tree

2 files changed

+190
-42
lines changed

2 files changed

+190
-42
lines changed

notebooks/CommonDesignPatterns.ipynb

Lines changed: 123 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
},
249249
{
250250
"cell_type": "code",
251-
"execution_count": 12,
251+
"execution_count": 26,
252252
"id": "52b6f924",
253253
"metadata": {},
254254
"outputs": [],
@@ -263,7 +263,7 @@
263263
},
264264
{
265265
"cell_type": "code",
266-
"execution_count": 13,
266+
"execution_count": 27,
267267
"id": "a2055780",
268268
"metadata": {},
269269
"outputs": [
@@ -273,7 +273,7 @@
273273
"'6-card deals: 20,358,520'"
274274
]
275275
},
276-
"execution_count": 13,
276+
"execution_count": 27,
277277
"metadata": {},
278278
"output_type": "execute_result"
279279
}
@@ -284,7 +284,7 @@
284284
},
285285
{
286286
"cell_type": "code",
287-
"execution_count": 14,
287+
"execution_count": 28,
288288
"id": "0915bccf",
289289
"metadata": {},
290290
"outputs": [],
@@ -299,7 +299,7 @@
299299
},
300300
{
301301
"cell_type": "code",
302-
"execution_count": 15,
302+
"execution_count": 29,
303303
"id": "7a113558",
304304
"metadata": {},
305305
"outputs": [
@@ -309,7 +309,7 @@
309309
"'6-card deals: 20,358,520'"
310310
]
311311
},
312-
"execution_count": 15,
312+
"execution_count": 29,
313313
"metadata": {},
314314
"output_type": "execute_result"
315315
}
@@ -320,14 +320,95 @@
320320
},
321321
{
322322
"cell_type": "code",
323-
"execution_count": 16,
323+
"execution_count": 31,
324+
"id": "0f452305",
325+
"metadata": {},
326+
"outputs": [
327+
{
328+
"data": {
329+
"text/plain": [
330+
"CacheInfo(hits=0, misses=1, maxsize=64, currsize=1)"
331+
]
332+
},
333+
"execution_count": 31,
334+
"metadata": {},
335+
"output_type": "execute_result"
336+
}
337+
],
338+
"source": [
339+
"binom.cache_info()"
340+
]
341+
},
342+
{
343+
"cell_type": "code",
344+
"execution_count": 32,
345+
"id": "c6c71dd3",
346+
"metadata": {},
347+
"outputs": [],
348+
"source": [
349+
"# let's look into memoized fib function\n",
350+
"@lru_cache(maxsize=None)\n",
351+
"def fib(n):\n",
352+
" if n < 2:\n",
353+
" return n\n",
354+
" return fib(n-1) + fib(n-2)"
355+
]
356+
},
357+
{
358+
"cell_type": "code",
359+
"execution_count": 33,
360+
"id": "a700c86a",
361+
"metadata": {},
362+
"outputs": [
363+
{
364+
"data": {
365+
"text/plain": [
366+
"[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]"
367+
]
368+
},
369+
"execution_count": 33,
370+
"metadata": {},
371+
"output_type": "execute_result"
372+
}
373+
],
374+
"source": [
375+
"[fib(n) for n in range(16)]"
376+
]
377+
},
378+
{
379+
"cell_type": "code",
380+
"execution_count": 34,
381+
"id": "7d5897e4",
382+
"metadata": {},
383+
"outputs": [
384+
{
385+
"data": {
386+
"text/plain": [
387+
"CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)"
388+
]
389+
},
390+
"execution_count": 34,
391+
"metadata": {},
392+
"output_type": "execute_result"
393+
}
394+
],
395+
"source": [
396+
"fib.cache_info()"
397+
]
398+
},
399+
{
400+
"cell_type": "code",
401+
"execution_count": 35,
324402
"id": "5d5badb2",
325403
"metadata": {},
326404
"outputs": [],
327405
"source": [
328406
"# class decorators\n",
329-
"# run the named_logger.py file to see the log being generated\n",
330-
"# demo.log is not generated from the Jupyter Notebook\n",
407+
"# Generates demo.log when applied to a function\n",
408+
"# __call__ overrides when the class is called like a function\n",
409+
"\n",
410+
"from functools import wraps\n",
411+
"from typing import Callable, Any\n",
331412
"import logging\n",
332413
"import time\n",
333414
"\n",
@@ -364,7 +445,7 @@
364445
},
365446
{
366447
"cell_type": "code",
367-
"execution_count": 17,
448+
"execution_count": 36,
368449
"id": "1a4223d8",
369450
"metadata": {},
370451
"outputs": [],
@@ -376,7 +457,7 @@
376457
},
377458
{
378459
"cell_type": "code",
379-
"execution_count": 18,
460+
"execution_count": 37,
380461
"id": "2c7eb728",
381462
"metadata": {},
382463
"outputs": [
@@ -386,7 +467,7 @@
386467
"6"
387468
]
388469
},
389-
"execution_count": 18,
470+
"execution_count": 37,
390471
"metadata": {},
391472
"output_type": "execute_result"
392473
}
@@ -397,15 +478,16 @@
397478
},
398479
{
399480
"cell_type": "code",
400-
"execution_count": 19,
481+
"execution_count": 38,
401482
"id": "5757b477",
402483
"metadata": {},
403484
"outputs": [
404485
{
405486
"name": "stdout",
406487
"output_type": "stream",
407488
"text": [
408-
"2024-04-09 16:28:18,423 test4, 2.0μs\n"
489+
"2024-11-14 09:10:02,286 test4, 0.9μs\r\n",
490+
"2024-11-14 10:36:01,177 test4, 0.9μs\r\n"
409491
]
410492
}
411493
],
@@ -458,7 +540,7 @@
458540
},
459541
{
460542
"cell_type": "code",
461-
"execution_count": 20,
543+
"execution_count": 13,
462544
"id": "b0845c87",
463545
"metadata": {},
464546
"outputs": [],
@@ -504,7 +586,7 @@
504586
},
505587
{
506588
"cell_type": "code",
507-
"execution_count": 21,
589+
"execution_count": 15,
508590
"id": "60c907d5",
509591
"metadata": {},
510592
"outputs": [],
@@ -515,7 +597,7 @@
515597
" \"\"\" Updates the Hexviewer. \"\"\"\n",
516598
" \n",
517599
" def update(self, subject):\n",
518-
" print('HexViewer: Subject {} has data 0x{:x}'.format(subject.name, subject.data))\n",
600+
" print(f'HexViewer: Subject {subject.name} has data 0x{subject.data:x}')\n",
519601
" \n",
520602
"\n",
521603
"class OctalViewer:\n",
@@ -534,7 +616,7 @@
534616
},
535617
{
536618
"cell_type": "code",
537-
"execution_count": 22,
619+
"execution_count": 16,
538620
"id": "970c60ec",
539621
"metadata": {},
540622
"outputs": [],
@@ -544,7 +626,7 @@
544626
},
545627
{
546628
"cell_type": "code",
547-
"execution_count": 23,
629+
"execution_count": 17,
548630
"id": "cf9b10df",
549631
"metadata": {},
550632
"outputs": [],
@@ -556,7 +638,7 @@
556638
},
557639
{
558640
"cell_type": "code",
559-
"execution_count": 24,
641+
"execution_count": 18,
560642
"id": "600cabe8",
561643
"metadata": {},
562644
"outputs": [],
@@ -568,7 +650,7 @@
568650
},
569651
{
570652
"cell_type": "code",
571-
"execution_count": 25,
653+
"execution_count": 19,
572654
"id": "9dac70cf",
573655
"metadata": {},
574656
"outputs": [
@@ -747,7 +829,7 @@
747829
},
748830
{
749831
"cell_type": "code",
750-
"execution_count": null,
832+
"execution_count": 20,
751833
"id": "e7dccd47",
752834
"metadata": {},
753835
"outputs": [],
@@ -860,7 +942,7 @@
860942
},
861943
{
862944
"cell_type": "code",
863-
"execution_count": null,
945+
"execution_count": 21,
864946
"id": "eb74083e",
865947
"metadata": {},
866948
"outputs": [],
@@ -875,7 +957,7 @@
875957
},
876958
{
877959
"cell_type": "code",
878-
"execution_count": null,
960+
"execution_count": 22,
879961
"id": "84f930e9",
880962
"metadata": {},
881963
"outputs": [],
@@ -887,10 +969,24 @@
887969
},
888970
{
889971
"cell_type": "code",
890-
"execution_count": null,
972+
"execution_count": 23,
891973
"id": "e07cf19c",
892974
"metadata": {},
893-
"outputs": [],
975+
"outputs": [
976+
{
977+
"name": "stdout",
978+
"output_type": "stream",
979+
"text": [
980+
"Invoker: Does anybody want something done before I begin?\n",
981+
"SimpleCommand: See, I can do simple things like printing(Say Hi!)\n",
982+
"Invoker: ...doing something really important...\n",
983+
"Invoker: Does anybody want something done after I finish?\n",
984+
"ComplexCommand: Complex stuff should be done by a receiver object\n",
985+
"Receiver: Working on (Send email.)\n",
986+
"Receiver: Also working on (Save report.)"
987+
]
988+
}
989+
],
894990
"source": [
895991
"invoker.do_something_important()"
896992
]
@@ -1538,7 +1634,7 @@
15381634
"name": "python",
15391635
"nbconvert_exporter": "python",
15401636
"pygments_lexer": "ipython3",
1541-
"version": "3.12.2"
1637+
"version": "3.10.8"
15421638
}
15431639
},
15441640
"nbformat": 4,

0 commit comments

Comments
 (0)