-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_notes.py
More file actions
728 lines (506 loc) · 19.4 KB
/
python_notes.py
File metadata and controls
728 lines (506 loc) · 19.4 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
from sys import argv
from functools import partial
print '-------------------- Strings --------------------'
print 'This is a \n test' # Test will appear on a new line
print r'This is a \n test' # Raw string, backslash doesn't act as an escape character
print 'It was the best of times. \
It was the worst of times.' # Use backslash to continue string on next line 'It was the best of times. It was the worst of times.'
print """
Use triple quotes to
do multi-line strings
"""
hello_world = 'Hello' ', ' 'world'
print hello_world # 'Hello, world'
# String interpolation
print 'String interpolation {0} {1}'.format('Hello', 'world') # Hello world'
print 'String interpolation {foo} {bar}'.format(foo = 'Hello', bar = 'world') # Hello world!
print 'String interpolation %s %s' % ('Hello', 'world') # Hello world! <-- old style (deprecated)
words = ['Now', 'is', 'the', 'time']
print ' '.join(words) # 'Now is the time'
print 'foo'.capitalize() # 'Foo'
print 'foo'.upper() # 'FOO'
print 'FOO'.lower() # 'foo'
print 'kevin william pang'.title() # 'Kevin William Pang'
print 'A b C'.swapcase() # 'a B c'
print '-------------------- Functions --------------------'
def print_two(*args):
'A string placed at the beginning of a function is used as documentation'
arg1, arg2 = args
print 'arg1: %r, arg2: %r'.format(arg1, arg2)
def print_two_again(arg1, arg2):
print 'arg1: %r, arg2: %r'.format(arg1, arg2)
print_two('Kevin', 'Pang')
print_two_again('Kevin', 'Pang')
print print_two.__doc__ # 'A string placed at the beginning of a function is used as documentation'
# Functions are objects too, so you can assign them to variables
test = print_two
test('Kevin', 'Pang')
# Simulating Ruby's "each" method. Not as elegant without blocks since you have to define the function elsewhere (or use a lambda),
# but at least it's doable. It would probably be more Pythonic to simply do something like:
#
# for item in my_list:
# add_s(item)
class MyList(list):
def each(self, func):
for item in self:
func(item)
def add_s(item):
print item + 's'
my_list = MyList()
my_list.append('banana')
my_list.append('grape')
my_list.each(add_s)
def empty_method(self):
pass
def one_line_method(self): return 'foo'
def pass_in_list_and_dictionary(*arr, **dict):
for arr_item in arr:
print arr_item
for key in dict.keys():
print key, ':', dict[key]
pass_in_list_and_dictionary(1, 2, 3, a=4, b=5, c=6) # 1, 2, 3, a:4, b:5, c:6
print '-------------------- If / else --------------------'
var_1 = 1
var_2 = 2
if var_1 > var_2:
print '1 > 2'
elif var_1 == var_2:
print '1 = 2'
else:
print '1 < 2'
print '-------------------- Lists --------------------'
colors = ['red', 'blue']
colors.append('green')
colors.insert(1, 'white')
colors.append('gold')
colors.pop()
print colors # ['red', 'white', 'blue', 'green']
print range(0, 5) # [0, 1, 2, 3, 4]
print range(0, 5, 2) # [0, 2, 4]
print colors[0] # red
print colors[-1] # green
# Slice notation
print colors[2:4] # ['blue', 'green']
print colors[:2] # From beginning to index 2 ['red', 'white']
print colors[2:] # From index 2 to end ['blue', 'green']
empty_list = list()
print len(empty_list) # 0
another_empty_list = []
print len(another_empty_list) # 0
empty_list[0:] = [1, 2, 3]
print empty_list # [1, 2, 3]
print sorted([5, 4, 3, 2, 1]) # [1, 2, 3, 4, 5]
another_list = [5, 4, 3, 2, 1]
another_list.sort()
print another_list # [1, 2, 3, 4, 5]
# Filter selects certain items from a list
def is_even(item):
return item % 2 == 0
seq = [1, 2, 3, 4, 5, 6]
filtered_seq = filter(is_even, seq)
print seq # [1, 2, 3, 4, 5, 6]
print filtered_seq # [2, 4, 6]
print '-------------------- Sets --------------------'
highlanders = ['MacLeod', 'Ramirez', 'MacLeod', 'Matunas', 'MacLeod', 'Malcolm', 'MacLeod']
there_can_be_only_one = set(highlanders)
print len(highlanders) # 7
print len(there_can_be_only_one) # 4
print set('12345') # set(['1', '2', '3', '4', '5'])
print '-------------------- Tuples --------------------'
a_tuple = (1, 2, 3) # Immutable, cannot assign new values or append
print a_tuple[1] # 2
a_list = list(a_tuple) # Convert tuple to list
a_list.append(4)
print a_list # [1, 2, 3, 4]
a_tuple = tuple(a_list)
print a_tuple # (1, 2, 3, 4)
empty_tuple = ()
another_empty_tuple = tuple()
print '-------------------- For loops --------------------'
for color in colors:
print color
for i in range(0, len(colors)):
print colors[i]
print '-------------------- While loops --------------------'
i = 0
while i < len(colors):
print colors[i]
i += 1
print '-------------------- Dictionaries / Hashes --------------------'
stuff = {'name' : 'Kevin', 'age' : 29}
print stuff['name']
print stuff['age']
print stuff # {'age':29, 'name':'Kevin'}
print stuff.keys() # ['age', 'name']
print stuff.values() # [29, 'Kevin']
del stuff['age'] # Note that del can also remove attributes from objects / classes
print stuff # {'name':'Kevin'}
print 'age' in stuff # False
print 'name' in stuff # True
empty_dictionary = dict()
print len(empty_dictionary) # 0
another_empty_dictionary = {}
print len(another_empty_dictionary) # 0
print '-------------------- Try / Catch --------------------'
try:
None.some_method_none_does_not_know_about()
except Exception as ex:
print ex.__class__.__name__ # AttributeError
print ex.args[0] # 'NoneType' object has no attribute 'some_method_none_does_not_know_about'
finally:
print "Finally!"
try:
raise Exception("1", "2")
except Exception as ex:
print ex.args[0] # 1
print ex.args[1] # 2
var_1, var_2 = ex
print var_1 # 1
print var_2 # 2
try:
pass
except Exception as ex:
print 'This shouldn\'t be hit!'
else:
print "Yay!" # Yay!
print '-------------------- Classes --------------------'
class Car(object):
total_cars = 0 # Class variable (any variables defined outside of a function are class variables)
def __init__(self):
self.miles = 0
self.make = ''
Car.total_cars += 1
def drive(self, miles):
self.miles += miles
def print_mileage(self):
print self.miles
def _pseudo_private_method(self):
# Prefixing a method with an underscore implies private scope (but not enforced, there are no private methods / variables
# in Python)
print 'pseudo private method'
def __more_private_method(self):
# Prefixing a method with double underscore makes it harder to use directly due to name mangling, but you can still access it
print 'more private method'
# Creating properties via decorators
@property
def make(self):
return self._make
@make.setter
def make(self, a_make):
self._make = a_make
def __str__(self):
return self._make
def __repr__(self):
return self._make + ' ' + str(self.miles)
# Note that staticmethods and classmethods overshadow instance methods, so if we had instance methods rev_engine and
# rev_engine2, they wouldn't be hit (instead the staticmethod and classmethods defined below would be hit).
#
# Staticmethod and classmethod are similar, the only difference is classmethods get passed the class it was called on,
# or the class of the instance it was called on, as the first argument.
@staticmethod
def rev_engine():
print 'VROOM!'
@classmethod
def rev_engine2(cls):
print 'VROOM2!'
car = Car()
car.drive(50)
car.print_mileage()
car._pseudo_private_method() # This works!
car._Car__more_private_method() # This works too! Nothing is private!
car.make = 'Honda'
print car.make # Honda
print str(car) # Honea
print repr(car) # Honda 50
Car.rev_engine() # VROOM!
Car.rev_engine2() # VROOM2!
# Inheritance
class Hummer(Car):
def drive(self, miles):
self.miles += 2 * miles
def print_mileage(self):
super(Hummer, self).print_mileage() # This is how you invoke the parent class.
hummer = Hummer()
hummer.drive(50)
hummer.print_mileage()
print Car.total_cars # 2
print '-------------------- Monkey patching --------------------'
class Dog(object):
def bark(self):
return 'WOOF'
def wag(self):
return 'HAPPY'
Dog.wag = wag
a_dog = Dog()
print a_dog.bark() # WOOF
print a_dog.wag() # HAPPY
print '-------------------- Lambdas --------------------'
def make_incrementor(n):
return lambda x: x + n # Creates an anonymous function, restricted to a single expression
def make_incrementor_without_lambdas(n):
# This is equivalent to using a lambda, except you can define multi-line functions
def foo(x):
return x + n
return foo
f = make_incrementor(42)
print f(0) # 42
print f(1) # 43
f = make_incrementor_without_lambdas(42)
print f(0) # 42
print f(1) # 43
print '-------------------- Map --------------------'
# Map transforms elements of a list
def add_ten(item):
return item + 10
seq = [1, 2, 3]
mapped_seq = map(add_ten, seq)
print seq # [1, 2, 3]
print mapped_seq # [11, 12, 13]
print '-------------------- Reduce --------------------'
# Reduce applies a function to each item of the list and accumulates the values to reduce the list to a single value
def add(accumulated_value, item):
return accumulated_value + item
def multiply(accumulated_value, item):
return accumulated_value * item
print reduce(add, [1, 2, 3]) # 1 + 2 + 3 = 6
print reduce(multiply, [1, 2, 3], 1) # 1 * 1 * 2 * 3 = 6
print '-------------------- List comprehensions --------------------'
meats = ['ham', 'turkey', 'steak']
print [meat.upper() for meat in meats] # ['HAM', 'TURKEY', 'STEAK']
print map(lambda meat: meat.upper(), meats) # This is equivalent to the above
def upper_meat(meat):
return meat.upper()
print map(upper_meat, meats) # This is also equivalent
print '-------------------- Generators --------------------'
# Generators areslightly different than list comprehensions b/c generators must be iterated through, the values are
# generated on the fly rather than stored. Generators are memory friendly, but less versatile.
result = []
bacon_generator = (n + ' bacon' for n in ['crunchy', 'veggie', 'danish'])
for bacon in bacon_generator:
result.append(bacon)
print result # ['crunchy bacon', 'veggie bacon', 'danish bacon']
dynamite = ('Boom!' for n in range(3))
attempt_1 = list(dynamite) # ['Boom!', 'Boom!', 'Boom!]
attempt_2 = list(dynamite) # [] <-- This is because generators are a one shot deal
print list(attempt_1)
print list(attempt_2)
# The presence of the yield keyword turns abc into a generator factory. Execution starts when the next() routine is invoked, then
# stops when the first yield keyword is hit. Then resumes when next() is invoked again, then stops wen the next yield keyword is hit.
def abc():
yield "a"
yield "b"
yield "c"
generator_factory = abc()
print generator_factory.next() # a
print generator_factory.next() # b
print generator_factory.next() # c
def simple_generator_method():
yield 'peanut'
yield 'butter'
yield 'and'
yield 'jelly'
result = []
for item in simple_generator_method(): # The for loop will invoke the next() routine on simple_generator_method
result.append(item)
print result # ['peanut', 'butter', 'and', 'jelly']
def square_me(seq):
for x in seq:
yield x * x
square_me_generator = square_me(range(5))
for item in square_me_generator:
print item # 0, 1, 4, 9, 16
# This will also work, since converting to a list will iterate via the next() routine
# print list(square_me_generator) # [0, 1, 4, 9 ,16]
def fibon(n):
a = b = 1
for i in xrange(n):
yield a
a, b = b, a + b
for x in fibon(5):
print x # 1 1 2 3 5
print '-------------------- Iterators --------------------'
it = iter(range(0, 6))
for num in it:
print num # 0 1 2 3 4 5
it = iter(range(0, 6))
print next(it) # 0
print next(it) # 1
print '-------------------- Enums --------------------'
# Python doesn't have support for Enums, but you can make your own via classes
class Colors:
RED = 1
BLUE = 2
GREEN = 3
print Colors.RED # 1
# print Colors.ORANGE # This will throw an AttributeError exception since Colors doesn't contain an attribute called ORANGE
print '-------------------- IO --------------------'
if False:
# Get input from command line
age = raw_input('How old are you? ')
print 'You are %s years old!' % age
# Parses command line arguments
script, user_name = argv
print 'Hello %s, I am the %s script' % (user_name, script)
# Read files (r for reading, w for writing)
txt = open('python_notes.py', 'r')
print txt.read()
txt.close()
# Reading files using the with statement
with open('python_notes.py', 'r') as f:
print f.read()
print '-------------------- Partials --------------------'
def max(a, b):
if a > b:
return a
else:
return b
max100 = partial(max, 100)
print max100(50) # 100
print max100(150) # 150
print '-------------------- Decorating with functions --------------------'
def addCowbell(fn):
fn.foo = 'foo'
return fn
@addCowbell
def bar():
return 'bar'
print bar.foo # foo
print '-------------------- Decorators --------------------'
# Decorators are wrappers that let you execute code before and after the function they decorate
def makebold(fn):
def wrapper():
return "<b>" + fn() + "</b>"
return wrapper
def makeitalic(fn):
def wrapper():
return "<i>" + fn() + "</i>"
return wrapper
@makebold
@makeitalic
def say():
return "hello"
# say = makebold(makeitalic(say)) # This is equivalent to using the decorators above
print say() # <b><i>hello</i></b>
print '-------------------- Metaclasses --------------------'
def some_method(self):
print 'some_method'
# You can dynamically create classes by using the "type" function. The 2nd argument is a tuple of the parent class(es).
# The 3rd argument is a dictionary containing attribute names and values
MyOtherShinyClass = type('MyOtherShinyClass', (), {'some_attribute' : True, 'some_method' : some_method})
print MyOtherShinyClass # <class '__main__.MyOtherShinyClass'>
print MyOtherShinyClass.some_attribute # True
an_instance = MyOtherShinyClass()
an_instance.some_method() # some_method
# Metaclasses are classes that create classes (i.e. a class factory). When Python sees a class definition, it checks if
# the class definition contains a __metaclass__ attribute. If so, it will use it to create the class object. If not, it
# will go to the parent class and try to do the same thing. If it can't find any __metaclass__ attributes, it falls back to
# using the "type" function to create the class object.
#
# The main purpose of using metaclasses is to change the class automatically, when it's created.
# Using a function as a metaclasses
def upper_attr(future_class_name, future_class_parents, future_class_attr):
"""
Return a class object, with the list of its attribute turned
into uppercase.
"""
# pick up any attribute that doesn't start with '__'
attrs = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__'))
# turn them into uppercase
uppercase_attr = dict((name.upper(), value) for name, value in attrs)
# let `type` do the class creation
return type(future_class_name, future_class_parents, uppercase_attr)
class Foo(object):
__metaclass__ = upper_attr
# we can define __metaclass__ here instead to affect only this class
bar = 'bip'
f = Foo()
print hasattr(Foo, 'bar') # False
print hasattr(Foo, 'BAR') # True
print f.BAR # bip
# Using a class as a metaclass
class UpperAttrMetaclass(type):
# __new__ is the method called before __init__
# it's the method that creates the object and returns it
# while __init__ just initializes the object passed as parameter
# you rarely use __new__, except when you want to control how the object
# is created.
# here the created object is the class, and we want to customize it
# so we override __new__
# you can do some stuff in __init__ too if you wish
# some advanced use involves overriding __call__ as well, but we won't
# see this
def __new__(self, future_class_name, future_class_parents, future_class_attr):
attrs = ((name, value) for name, value in future_class_attr.items() if not name.startswith('__'))
uppercase_attr = dict((name.upper(), value) for name, value in attrs)
# return type(future_class_name, future_class_parents, uppercase_attr) # Same as below, but less OOP
return type.__new__(self, future_class_name, future_class_parents, uppercase_attr)
# return super(UpperAttrMetaclass, cls).__new__(cls, name, bases, uppercase_attr) # Yet another way, in case this metaclass inherits from another metaclass
class Foo2(object):
__metaclass__ = UpperAttrMetaclass
bar = 'bip'
f2 = Foo2()
print hasattr(Foo2, 'bar') # False
print hasattr(Foo2, 'BAR') # True
print f2.BAR # bip
print '-------------------- Imports --------------------'
# Alternative way of writing:
# from sys import argv
#
# would be:
# import sys.argv
#
# but any calls to argv would need to be fully qualified (i.e. sys.argv)
print '-------------------- Packages --------------------'
# Putting an __init__.py file into a directory makes Python treat the directory as a containing packages. It can
# be empty or you can put initialization code for the package in there. Some package authors will define the
# __all__ list inside __init__.py so that anyone importing * from the package will get all the names listed
# within the package's __all__ list (this is by convention).
#
# So for example, the following declares what gets exposed when someone types 'from python_note.py import *'
# __all__ = (
# 'Goat',
# '_Velociraptor'
# )
#
# The following will import the Duck class from a_package_folder/a_module.py
# from a_package_folder.a_module import Duck
print '-------------------- Misc --------------------'
import sys
print sys.path # List of directories Python goes through to search for modules and files (note that you can append to this)
# Logic operators: and, or, not, !=, ==, >=, <=
# Use all caps to indicate a constant. This is purely by convention. There are no such things as constants in Python.
# It's up to developers to not modify them
PI = 3.1416
print isinstance(None, object) # True (None is Python's version of null)
print issubclass(str, object) # True
print None is None # True (There is only one None)
print type(1) # <type 'int'>
print None.__class__ # <type 'NoneType'>
# Use dir() to print out all the object's methods
# print dir(sys) # Displays names defined in module 'sys'
# print dir() # Displays named defined currently
# print dir(__builtin__) # Displays names of built-in functions and variables
class Parent(object): pass
class Child(Parent): pass
# __mro__ (can also use the mro() function) is the Method Resolution Order. It returns a tuple listing the types
# the class is derived from, in the order they are searched for methods.
print Child.__mro__ # (<class '__main__.Child'>, <class '__main__.Parent'>, <type 'object'>)
print Child.__mro__.__class__ # <type 'tuple'>
print Child.__mro__.__class__.__name__ # tuple
# This is useful when writing Python scripts that need to be usable as both scripts run from the command line
# as well as modules imported from other Python modules.
if __name__ == "__main__":
print 'This script was run from the command line'
class FooCatcher(object):
# Intercepts all requests for attributes, regardless of whether they're defined or not. Note that you can overwrite
# __getattr__ instead if you only want to intercept unknown attributes. Similarly, you can overwrite __setattr__ to
# intercept attribute assignments and __delattr__ to intercept attribute deletions (as in removing an attribute altogether
# via the "del" statement)
def __getattribute__(self, attr_name):
if attr_name[:3] == 'foo':
return 'Foo to you too'
else:
return super(FooCatcher, self).__getattribute__(attr_name)
catcher = FooCatcher()
print catcher.foo # ' Foo to you too
print catcher.foobar # ' Foo to you too