-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmooc-python.py
More file actions
1059 lines (807 loc) · 24.8 KB
/
mooc-python.py
File metadata and controls
1059 lines (807 loc) · 24.8 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# These are my notes taken during the Python MOOC (completed Jan. 2018).
# https://www.fun-mooc.fr/courses/course-v1:UCA+107001+session01/about
# https://github.com/parmentelat/moocpython/blob/master/pdf_du_cours/from_latex/Python.pdf
# https://github.com/parmentelat/moocpython/files/1639944/Python3.-.1.a.9.avec.index.pdf
## ----------------------------------------------------------
## ----------------------------------------------------------
## Introduction
## Guido Van Rossum, creator, BDFL until 2018
## Highly readable language for an easy access
## Pragmatic, focus on ease of use
## Uniform syntax on the different types of objects
## Allows a rapid development
## A lot of libraries, especially for data science, ...
## v1 in 1994, v2 in 2000, v3 in 2008 (breaking changes)
## https://docs.python.org/3/faq/programming.html
print("Hello, World!")
## The zen of python
import this
# several flavours python, IPython
# idle, notebooks
## modules
import math # import module
from numpy import cos # import a function
import numpy as np # import with alias
print(math)
dir(math) # list included functions
help(math.pi) # math.pi? in jupyter notebooks
## standard library (modules included by default)
## a variable is a name that references an object
'spam'.upper() # upper is a method
n = 3
type(n)
n = "spam" # dynamic (and strongly typed) language
type(n)
isinstance(n, str)
del n # garbage collector will free the memory
## keywords that can't be used as variable name
import keyword
keyword.kwlist
## ----------------------------------------------------------
## ----------------------------------------------------------
## Numeric types
# int, float, complex
i = 1 # integer
i += 1 # a = a + 1
f = 0.1 # float
c = 1 + 1j # complex
1j ** 2 # square
7 // 3 # integer division
7.//3 # // with float returns float
7 % 3 # modulo
abs(-1) # absolute value
## floating point precision
z = 1.1 - 1
z == 0.1
print(z)
print("%0.1f" % z)
format(0.1, '.17f')
## check decimal or fractions modules
from decimal import Decimal
Decimal('1.1') - Decimal('1') == Decimal('0.1')
True + False
int("1") # int, float, complex, str
## bitwise operators
bin(3)
bin(6)
bin(12)
3 << 1
3 << 2
i >>= 2
0xf0
# import sys; print(sys.float_info)
## ----------------------------------------------------------
## ----------------------------------------------------------
## Basic notions
## strings
# reminder: bits (01) are decoded to an integer value
# for a given encoding system (e.g. ASCII), a value corresponds to a character
# problem: limited number of possible characters
# 7 bits => 128 characters
# 8 bits => 256 characters extended
# several systems exist but incompatible
# UNICODE project: UTF-8/16/32, UTF-8 compatible with ascii
# unicode > 120k coded characters
# USE UTF-8
"\u529b" ## python supports unicode
s = "力"
en = s.encode("utf-8")
en.decode("utf-8")
dir(str) # str? IPython only
help(str)
c = "spam" ## strings are immutable (see pythontutor)
c.title()
c.replace("spam", "ham")
# f-strings
a = 1
b = 2
f"{a} and {b}"
"{} and {}".format(a, b)
## some examples of string methods
'abc:def:ghi:jkl'.split(':')
":".join(['abc', 'def', 'ghi', 'jkl'])
"abcdeabcdeabcde".replace("abc", "zoo")
"abcdeabcdeabcde".replace("abc", "zoo", 2)
"[x] versus [y]".replace("[x]", "spam").replace("[y]", "ham")
" abc:de f:g hi ".replace(" ", "")
" \trm_special\n".strip()
'abc;def;ghi;jk;'.strip(';').split(';')
"abcdefcdefghefg".find("def")
"abcdefcdefghefg".find("zoo")
"abcdefcdefghefg".rfind("fgh")
"abcdefcdefghefg".index("zef") # like find but throws exception
"cde" in "abcdek"
"abcdefcdefghefg".count("ef")
"abcdefcdefghefg".startswith("abcd")
"abcdefcdefghefg".endswith("hefg")
"abcdefcdefghefg".upper()
"abcdefcdefghefg".swapcase()
"abcdefcdefghefg".capitalize()
## https://docs.python.org/3/library/stdtypes.html#string-methods
## string formatting
print(1, 'a', 12 + 4j)
first, last, age = 'John', 'Doe', 35
f"{first} {last} is {age}"
f"In 5 years {first} will be {age + 5}"
"%s %s is %s" % (first, last, age) # old version
## format numbers
from math import pi
f"pi rounded: {pi:.2f}"
x = 1
f"{x:03d}"
## fixed length
x = [(1, 1, 1), (111, 111, 111), (11111, 11111, 11111)]
for col1, col2, col3 in x:
print(f"{col1:<7} -- {col2:^7} -- {col3:>7}")
# number = input(Pick a number: ")
# print(f"number={number}")
## https://docs.python.org/3/library/string.html#formatstrings
## Regular expressions
import re
x = "Lorem ipsum dolor sit amet"
print(re.findall(r"[a-m]", x))
x = "dsf-6 ,333 1._nb9;"
re.findall("[-\d]+", x)
# ...
import string
char = string.ascii_lowercase
print(char)
###########################
## Sequences
## sequences = list/tuple/str/bytes/...
## elements with finite and ordered
s = 'spam'
len(s)
s[0]
"sp" not in s
s = s + " and ham"
s.index('a')
s.count('m')
max(s)
"x" * 30
## slicing
s[0:3]
s[2:]
s[0:10:2]
s[::2]
s[-5:-1]
s[::-1]
s[:] # shallow copy
###########################
## Lists
## Lists are sequences of heterogeneous objects
## List don't store the obj, store the reference (so memory size independant)
## Lists are mutable (doesn't need a copy => memory efficient)
## Sequences operations can be applied on lists
## Lists are highly flexible
a = []
type(a)
a = [1,2,3,4]
a[1:3] = [1,2,3] ## ! delete, then add
a[1:3] = [] ## ! delete
del a[1]
dir(list)
help(list.append) # list.append? IPython
a.append(9)
a.extend(a)
a.sort(reverse = True) # modified by reference
b = sorted(a) # sort and copy
s = "spam egg beans"
s = s.split()
s[0] = s[0].upper()
" ".join(s)
a = list(range(10))
a + a
a.insert(0, "a")
a.remove("a") # first match only
b = a.pop(2) # extract and remove (last one by default)
a.reverse()
a * 3 # duplicates n times
###########################
## If
## instruction block : + spaces
## <79 characters on one line better
if 'g' in 'egg':
print('yes')
else:
print('no')
## nested if: indent again
char = 'spam'
if 'a' in char:
if 'b' in char:
cas11 = True
print('a and b')
else:
cas12 = True
print('a but not b')
else:
if 'b' in char:
cas21 = True
print('b but not a')
else:
cas22 = True
print('neither a nor b')
# if elif elif ... else
## More about if
# test can be any expression => bool evaluated (or len, 0 => false)
# built-in: False 0 [] {} () ''
# comparaison / membership ==, !=, is, is not in <=, <, >, >=
# operators: and or not
###########################
## For loops and functions
for i in range(10):
print(i**2)
for i in [1, 2, True]:
print(i)
a = []
for n in [1, 2, '3', 4, 'END']:
a.append(str(n))
print(",".join(a))
for i, e in enumerate([1, 3, 5]):
print(i, e)
def square(x): # note: x passed by reference
L = []
for i in x:
L.append(i**2)
return L
def foo():
return
type(foo()) # None
def foo2(): ## function that does nothing
pass
for integer in range(1000):
# ignore numbers non multiple of 10
if integer % 10 != 0:
continue
print(f"processing {integer}")
# stop at 50
if integer >= 50:
break
x = True
y = 1 if x else 0
a = list(range(1,10))
while a:
a.pop()
if len(a) == 5:
continue # break # continue goes to the top of the while
print(a)
while True:
s = input("what is your question?\n")
if 'none' in s:
break
# while can take an else
###########################
## Lists (and list comprehension)
a = [1,4,18,29,13]
import math
b = [math.log(i) for i in a]
a = [-1,4,18,29,13]
b = [math.log(i) for i in a if i > 0]
a = ["Eve","bob","TOM"]
a = [p.lower() for p in a]
entry = [1, 2, 3, 4, 5, 6, 7, 8, 9]
squares = [x**2 for x in entry]
[(num1, num2) for num1 in range(1, 3) for num2 in range(3, 5)]
['positive' if x > 0 else 'negative' for x in [-2,2,3]]
## ----------------------------------------------------------
## ----------------------------------------------------------
## More about basic notions, shared references
###########################
## Files
f = open(r"c:\spam.txt", "w", encoding = "utf8")
# r = rawstring
# r = read, w = write, a = append
for i in range(100):
f.write(f"line {i + 1}\n")
f.close()
# !type or !cat c:\spam.txt # ipython only
f = open(r"c:\spam.txt", "r", encoding = "utf8")
f2 = open(r"c:\spam2.txt", "w", encoding = "utf8")
for line in f:
line = line.split()
line[0] = line[0].upper()
f2.write(",".join(line) + "\n")
f.close()
f2.close()
## context manager: modern way to manipulate files
## context manager protocol, avoids to use .close()
with open(r"c:\spam.txt", "r", encoding = "utf8") as f:
for line in f:
print(line) # file close automatically
## binary file (b before w indicates binary, e.g. pickled file)
with open(r"c:\spam.bin", "bw") as f:
for i in range(100):
f.write(b'\x3d') # bytes
import pickle
# Open pickle file and load data: d
with open('data.pkl', 'rb') as file:
d = pickle.load(file)
# other:
# https://docs.python.org/3/library/pathlib.html
# https://docs.python.org/3/library/json.html
###########################
## Tuples (immutable lists)
## a tuple is a sequence
## tuples are immutable lists (useful for dictionnaries)
t = ()
t = (4,) # (4) is simply an integer, use the comma
t = (True, 1, "a")
t = True, 1, "a" # parentheses are not mandatory
1 in t
t[0]
a = list(t) # convert tuple to list
t = tuple(a) # convert list to tuple
(a, b) = [3, 4] # tuple unpacking
a, b = [3, 4] # tuple unpacking
a = list(range(10)) # extended tuple unpacking
x, *y = a
*x, y = a
1,3 + 3, 4, + 2,1 + 1,2 # weird...
up = ["A", "B", "C"]
low = ["a", "b", "c"]
list(zip(up, low)) # zip function, more than two possible
###########################
## Dictionnaries
# hash tables: set and dictionnaries
# time of operations is independant of the nb of elements
# e.g. access, insert, test in, delete, ...
# all immutable objects are 'hashable'
# no order in dictionnaries
age = {}
age = {'ana': 35, 'eve': 30, 'bob': 38}
age['ana']
age = dict(ana = 35, eve = 35, bob = 35)
a = [('ana', 35), ('eve', 35), ('bob', 35)]
age = dict(a)
age['bob']
age['bob'] = 45 # see also update
del age['bob']
len(age)
'ana' in age
age.get("bob", 0)
k = age.keys() # k will be modified if age modified
age.values()
age.items() # returns a vue (a vue is an iterable object)
for k, v in age.items():
print(f"{k} {v}")
# see also collections module
###########################
## Sets
## related to dictionaries, but stores only keys, no values
## useful for unique values, or membership test
## better to convert a sequence to a set for in test
s = set()
type(s)
s = {1,2,3, 'a', True}
s = set([1,1,2,3,20,18,4]) # s is ordered, keys are unique
len(s)
1 in s
s.add("bob")
s.update([1,2,3,4,5,6,7])
s
s1 = {1,2,3}
s2 = {2,3,4}
s1 - s2 # difference
s1 | s2 # union
s1 & s2 # intersection
s1 ^ s2 # symetrical difference
a = [0]
#s{0}
# frozenset: set that can not be modified
fs = frozenset(s) # fs.add(2) will fail
###########################
## Exceptions
## exceptions can be captured and bring info about the error
## frequently used mechanism in python
# 1 / 0 => ZeroDivisionError
def div (a,b):
try:
print(a / b)
except ZeroDivisionError:
print("Can not divide by zero")
except TypeError:
print("Numbers required")
print("next...")
# try: captures exception, except: properly handles the error
# div(3, 1); div(3, 0); div(3,"a")
## bubbling => exceptions go up
def function_with_finally(number):
try:
return 1 / number
except ZeroDivisionError as e:
print(f"D'oh! {type(e)}, {e}")
return("zero-divide")
finally:
print("Final action even if return called above")
function_with_finally(0)
def function_with_else(number):
try:
1 / number
except ZeroDivisionError as e:
print(f"D'oh! {type(e)}, {e}")
else:
print("Do this only if non-zero number")
return 'Something else'
function_with_else(0)
###########################
## Shared references
## side effects
a = [1,2]
b = a
b[0] = 0
a
b = a[:] # use a shallow copy to avoid side effects
# but if the list references a mutable object, shallow is not enough
# for example:
a = [1, [2]]
b = a[:]
a[1][0] = 9
b
import copy
b = copy.deepcopy(a) # deep copy
a is b # check shared references
id(a) # 'memory adress'
# https://docs.python.org/3/reference/datamodel.html#objects-values-and-types
# circular references...
el = [0]
myList = [el, el, el]
myList[0][0] = 1
myList = 3 * [ [0] ]
myList
myList[0][0] = 1
myList
###########################
## Classes
class C:
pass
c1 = C()
# self corresponds to the class instance
class Phrase:
def __init__(self, phrase):
self.words = phrase.split()
def upper(self):
self.words = [m.upper() for m in self.words]
def __str__(self): # str allows to print the content
return "\n".join(self.words)
p = Phrase("hello world")
p.words # words = attribute
p.upper() # upper = method
print(p)
## ----------------------------------------------------------
## ----------------------------------------------------------
## Functions (scope), if, while, ...
###########################
## Functions
# functions are object, below ff is the variable that references the function
def ff(a,b,c):
print(a,b,c)
g = ff
g(1,2,3)
# in Python, arguments are passed by reference (vs by value)
# side effect *IN PLACE*
# "MUST BE USED CAREFULLY", good for performance
# need to document properly using docstring (help(list.sort))
def add1(a):
"""
Help of add1 function (docstring).
"""
a.append(1)
L = []
add1(L)
# but using shallow copy may be more safe
def add2(a):
a = a[:]
a.append(1)
return a
L = add2(L) # is more explicit
# polymorphism => execute on all the types compatible with the function
# for example
def my_add(a, b):
print(f"{a} and {b}")
return a + b
# my_add works on integers/floats/characters
# my_add(1, 2); my_add(1.0, 2.0); my_add("a", "b");
# type hints inform about expected types
# just to document, no check during execution
nb_items : int = 0
def fact(n : int) -> int:
return 1 if n <= 1 else n * fact(n-1)
from typing import List
def fun_with_hints(x: List[int]) -> List[str]:
pass
# https://docs.python.org/3/library/typing.html#user-defined-generic-types
def fi(x):
# use isinstance to check type of arguments
if isinstance(x, int):
return x + 1
else:
raise TypeError(x) # raise ValueError('Error message')
fi("a")
###########################
## Parameters and arguments
## parameters: in the definition of the function
## arguments: variables passed for execution
def fff(a, b = 3): # default values can be used
print(a, b)
fff(b = 3, a = 6) # named
fff(5) # ordered
def hh(*t): # * arguments put in a tupple, number arbitraty
print(t)
hh(); hh(1,2,3) # tupple
def jj(**d):
print(d)
jj(a = 1, b = 2) # dictionary
# * and ** can also be used arguments even if not in parameters
L = [1,2,3]
hh(*L) # tupple unpacking
d = {"sep" : " and ", "end" :"\n\n"}
print(1,2, **d) # ** can be used to pass arguments as a dict
# https://docs.python.org/3/reference/expressions.html#calls
# be careful with mutable arguments!
# https://docs.python.org/3/faq/programming.html#why-are-default-values-shared-between-objects
###########################
## Scoping rules
## to access variables, Python follows the LEGB rule
## LEGB: Local, Enclosing, Global, Built-in
## Local (in the block of a function)
## Global variables (defined outside functions)
a, b, c = 1, 1, 1
def gg():
b = 2
b = b + 10
# print(locals()) # b
def h():
c = 5
print(a, b, c)
h()
gg()
# a global, b enclosing, c local
print(a, b, c) # all global (print is built-in)
locals(); globals()
import builtins
dir(builtins) # accessible built-in functions
builtins.print
# UnboundLocalError
# when trying to modify locally a referenced (used) global variable
# need to use the global instruction for that
# but not recommended: use parameters and assignment
## similarly, nonlocal instruction used to modify enclosing variables
# Namespaces
# global variables for each module
# how to communicate across namespaces?
## ----------------------------------------------------------
## ----------------------------------------------------------
## Iteration, import, and namespaces
###########################
## Iterators
s = {1,2,3,'a'}
[x for x in s if type(x) is int] # list comphrehension
it = iter(s) # creates an iterator on s
it
next(it)
next(it)
next(it)
next(it)
next(it) # StopIteration error
## iterable object vs iterators
# __iter__() # iterable objects have this method ## ''contains the data'
# iter(s) ~ s.__iter__()
# this method returns an iterator
## iterators have two methods: .__iter__() and .__next__() (goes through the data)
## iterator can be run over only once
## simple, compact, very low cost to create
enum = enumerate([1, 3, 5], start = 1) # returns index and value
next(enum); next(enum); next(enum)
a = [1,2]
b = [3,4]
z = zip(a, b) # [tuple(a0,b0), tuple(a1,b1)]
z is iter(z)
[i for i in z]
[i for i in z] # used once, now empty
import itertools ## offers more functionalities for iterators (combinations, ...)
# https://docs.python.org/3/library/itertools.html
###########################
## Functional programming
## functional programing: use functions as objects and use them as argument of other functions
# lambda functions are anonymous, they are an expression
# sometimes, easier to use (avoids to declare a function)
# map: applies a function to each element
m = map(lambda x: x**2 - 1, range(10))
m
list(m)
## filter function
## filter an iterable object using a test
n = filter(lambda x: x%2 == 0, range(10))
n
list(n)
# map and filter produce iterables (usable once)
# map and filter are like list comphehension
# list comprehension is more pythonic
###########################
## Comprehension: lists, sets, and dictionnary (all iterable objects)
names = ["ana","EVE","Alice", "bob"]
[n.lower() for n in names if n.lower().startswith('a')]
names.extend(names) # duplicates
[n.lower() for n in names if n.lower().startswith('a')]
{n.lower() for n in names if n.lower().startswith('a')}
# curly brackets => unique values directly!
# list comprehension can also be used on dictionary
ages = [('Ana',20), ('Eve', 30), ('Bob', 40)]
ages = dict(ages)
{p.lower():a for p, a in ages.items()}
{p.lower():a for p, a in ages.items() if a < 40}
##
[n + p for n in [2, 4] for p in [10, 20, 30]]
[n + p for n in [2, 4] for p in [10, 20, 30] if n*p >= 40]
###########################
## Generator expressions
## Avoid to generate temporary objects
## Very memory-efficient
## List comprehension returns a list
## Generators returns an iterator
sqr = [x**2 for x in range(1000)]
len(sqr)
sum(sqr)
sqr = (x**2 for x in range(1000)) # parentheses instead of square brackets
sqr
sum(sqr)
sum(sqr) ## iterator is over
# can be chained!
gen_sqr = (x**2 for x in range(1000))
palindrome = (x for x in gen_sqr if str(x) == str(x)[::-1])
list(palindrome)
# http://python-history.blogspot.com/2010/06/from-list-comprehensions-to-generator.html
## generative function
def gen():
yield 10
gen()
###########################
## Modules and namespaces
# object.attribut
# access to an attribute in the object's namespace
# namespace: group of variables belonging to an object
import os
print(os) ## os is a variable that references the module object
os.environ['PYTHONPATH']
import sys
sys.path # paths to search for modules
sys.modules
sys.builtin_module_names
## when a module is imported
## precompilation: byte code in __pycache__
## then byte code read to create the object module
## to force the reloading of a module
import importlib; importlib.reload(mod)
# %load_ext autoreload # for notebooks
# to build a custom module, use setuptools
# modules are mutable
# modules have their namespace
# see also packages (collection of modules)
###########################
## OOP
# a class allows to define a custom type
# calling a class creates an instance
# namespace instance first, then namespace class: inheritance tree
# method = function defined in a class
class Phrase0:
my_phrase = "Python is fun."
Phrase0
p = Phrase0()
vars(Phrase0) # Phrase.__dict__
p.my_phrase # not defined in instance, found in the class
Phrase0.words = Phrase0.my_phrase.split()
p.words
class Phrase1:
"Docstring for the Phrase class"
def __init__(self, my_phrase):
## __init_ method is the constructor
self.my_Phrase = my_phrase
# self = reference of the instance
p=Phrase1("Python is fun.")
vars(p)
help(Phrase1)
## special methods __something__
## allows to define methods that behave like builtin types
## len, print, in, +, ...
## a lot of special methods available (around eighty?)
## https://docs.python.org/3/reference/datamodel.html#specialnames
class Phrase2:
def __init__(self, my_phrase):
self.my_phrase = my_phrase
self.words = my_phrase.split()
def nb_letters(self):
return len(self.my_phrase)
def __len__(self):
return len(self.words)
def __contains__(self, word):
return word in self.words
def __str__(self):
return " ".join(self.words)
p = Phrase2("Python is fun.")
p.words
p.nb_letters()
len(p)
"is" in p
print(p)
## inheritance
## an instance inherits from the class
## a class can inherit from other classes
s = "Python is fun."
class PhraseNoCase(Phrase2):
def __init__(self, my_phrase):
Phrase2.__init__(self, my_phrase)
self.words_lower = {m.lower() for m in self.words}
pnoc = PhraseNoCase(s)
isinstance(pnoc, Phrase2)
isinstance(pnoc, PhraseNoCase)
pnoc.words_lower
## multiple inheritance
## MRO: method resolution order
## attribute resolution
## object = super class of all the classes
class CC:
pass
CC.__bases__
CC.mro() ## path followed in attribute resolution
## mro depends on the order the super classes are defined
# see also class property
# https://docs.python.org/3.6/library/functions.html#property
## define a class that is an iterator
class Phrase3:
def __init__(self, my_phrase):
self.my_phrase = my_phrase
self.words = my_phrase.split()
def __iter__(self):
return self
def __next__(self):
if not self.words:
raise StopIteration
return self.words.pop(0)
pp = Phrase3("Python is fun") ## Phrase3 defined as an iterator
[m for m in pp]
# next(pp) => StopIteration
iter(pp)
## define a class that is iterable
class Phrase4:
def __init__(self, my_phrase):
self.my_phrase = my_phrase
self.words = my_phrase.split()