forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python-cheatsheet.py
executable file
·1430 lines (1153 loc) · 44 KB
/
python-cheatsheet.py
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
#!/usr/bin/env python
# This is a file of reminders of various neat Python features
# that I always forget how to use.
# On #python as of early 2017, recommended beginning Python books include
# http://greenteapress.com/wp/think-python-2e/
# https://automatetheboringstuff.com/
# (the oft-recommended "Learn Python the Hard Way" is not well favored).
# I can't vouch for these books myself.
########################################################
# Interactive Python interpreter
########################################################
# Reload a module you already imported: modulename isn't quoted.
import modulename
reload(modulename)
# If you've imported it under some other name, use the name you used
import modulename as mn
reload(mn)
#############################################################
# What's available in objects and modules? and type-checking.
#############################################################
# Show methods in an object
dir(obj)
# Does a function exist in an object?
if hasattr(obj, 'attr_name'):
if 'attr_name' in dir(obj):
# Does a function exist in a module?
hasattr(os, 'get_terminal_size'):
# You can also get with a default:
getattr(os, 'get_terminal_size', "Doesn't exist")
# Is a variable defined? You have to check locals and globals separately.
if 'myVar' in locals():
if 'myVar' in globals():
# Or, more generally,
try:
myVar
except NameError:
myVar = None
# Is something a particular type? (But of course duck-typing is better.)
if type(s) is str:
print("It's a string")
if isinstance(s, str):
print("It's a string")
# More deprecated:
if type(s) == types.StringType:
print "It's a string"
# Is something list-like?
if hasattr(l, "__getitem__"):
print "It's list-like"
else:
print "It's more scalar-like"
# Be cautious with this, though: strings are list-like.
# Is something a dict? Use isinstance rather than type()
# because isinstance will work for derived classes.
if isinstance(x, dict)
# Difference between is and ==:
# is checked whether two things are the same object (reference equality),
# == only tests for value equality.
#
# The place where this is most important is nan: (nan == nan) -> False
# but (nan is nan) -> True.
# However, in lists or tuples this breaks, [nan] == [nan] -> True
# You can also use math.isnan(x)
#
# Testing thing is None rather than thing == None is idiomatic;
# testing thing == None supposedly allows a few non-None objects
# to masquerade as None, but I haven't been able to find an example.
# Of course, don't use "is" for lists, strings or other complex types,
# or even for integers; it may work for small integers because python
# caches them, but not for larger ones.
# Update an object from a dictionary, obj.something = d['something']
for k in d:
setattr(obj, k, d[k])
# Initialize a list to multiple copies of something.
# DON'T USE l = [ [] ] * 10 -- you'll get ten copies of the same list!
# Instead,
l = [ [] for i in range(10) ]
########################################################
# Shell One-liners
########################################################
# Python gives unclear errors if you try to make a one-liner that
# includes multiple lines plus a loop or conditional.
# Because the loop is a compound statement which can't be included
# on the same line with a "small statement" according to Python's grammar:
# https://docs.python.org/reference/grammar.html
# Ways of getting around this:
# Pass the program as stdin:
echo 'import time\nl = range(10)\nfor i in l: print(i)' | python
# Use ANSI quoting n bash, zsh or ksh along with \n:
python -c $'import time\nl = range(10)\nfor i in l: print(i)\n'
# More POSIX compliant: use command substitution:
python -c "$(printf %b 'import sys\nfor r in range(10): print("%d:" % r)')"
# You can also sometimes rewrite loops with list comprehensions or map()
# to turn them into small statements.
########################################################
# Debugging and stack traces
########################################################
# Dump into the debugger:
breakpoint()
# Print a stack trace -- how did we get here?
traceback.print_stack()
# Print a traceback after an exception:
print(traceback.format_exc())
# Equivalent of verbose mode in a shell: print each line before executing.
python -m trace --trace /tmp/foo.py
# Debugging in python2:
print >>sys.stderr, "foo"
# and in python3:
print('foo', file=sys.stderr)
# Suppress stdout and stderr chatter:
sys.stdout = open(os.devnull, "w")
sys.stderr = open(os.devnull, "w")
# This only works for chatter that comes from Python libraries, though.
# For an example that can suppress underlying C libraries,
# see run_main_quietly() at the end of quickbrowse.py.
# Prettyprinting
from pprint import pprint
pprint(obj)
# Binary bit fields to string: all the native ways of printing binary
# in Python insist on signed integers.
def tobin(data, width=8):
data_str = bin(data & (2**width-1))[2:].zfill(width)
return data_str
# And, speaking of bit fields, Python's ~ operator is fairly useless
# because it always turns on a sign bit, no matter how large the operand,
# and there's apparently no way to mask it off.
# So instead, use ^0xff (or appropriate length) if you want a bitwise NOT:
>>> ~0xff
-256
>>> 0xff ^ 0xff
0
# Print a listing of variables along with a traceback: see
# http://code.activestate.com/recipes/52215-get-more-information-from-tracebacks/
########################################################
# Stringy stuff
########################################################
# Decode vs. Encode:
# "string of bytes".decode('utf-8') --> unicode
# u"unicode string".encode('utf-8') --> bytes
# Either of these can take
# errors='replace', 'ignore', 'backslashreplace', 'xmlcharrefreplace'
>>> u = u'piñon'
>>> u
u'pi\xf1on'
# For Python3 skip to the end of this file.
# Fix "UnicodeEncodeError: 'ascii' codec can't encode character":
.encode('utf-8', "xmlcharrefreplace")
# Split a long string over multiple lines in the source file
url1 = ( "http://www.crummy.com/software/BeautifulSoup/"
"bs3/documentation.html" )
# Note no commas in the parenthesized one:
# parentheses without a comma inside are just grouping, not a tuple.
(42) # is type int
(42,) # is a tuple with len 1
# You can also use a backslash and no parentheses:
url2 = "http://www.crummy.com/software/BeautifulSoup/" \
"bs3/documentation.html"
#
# Fuzzy string match.
# SequenceMatcher's first argument is a function that returns true for
# characters considered to be "junk". For instance, if blanks are junk,
# lambda x: x == " "
# To consider nothing as junk, pass None.
#
from difflib import SequenceMatcher
best_ratio = -1
best_match = None
for b in string_list:
r = SequenceMatcher(None, matchname, b).ratio()
if r > best_ratio:
best_match = b
best_ratio = r
# raw string literals: r'' avoids any backslash escapes.
# printf-style %x still works, e.g. r'abc %d' % 42
r = r'abc\def'
c = 'abc\\def'
r == c # True
# Replace non-breaking spaces in unicode (python3):
s = s.replace("\u00A0"," ")
# Split with a regexp:
sep = re.compile('[,\s]+')
sep.split('HB42,SJR1, HR67 SB3')
#############################
# All the ways of formatting numbers.
# https://docs.python.org/3/tutorial/inputoutput.html
# For pre-2.6, see https://stackoverflow.com/a/2962966
# String concatenation:
filename = 'file' + str(num) + '.txt'
# Conversion Specifier:
filename = 'file%s.txt' % num
#############################
# Formatted string literals, Python 3.6+
print(f'Fly to {name}: {lat}N {lon}E')
print(f'The value of pi is approximately {math.pi:.3f}.')
# str and repr in formatted string literals
>>> animals = 'eels'
>>> print(f'My hovercraft is full of {animals!s}.') # applies str() (default)
My hovercraft is full of eels.
>>> print(f'My hovercraft is full of {animals!r}.') # applies repr()
My hovercraft is full of 'eels'.
# More formatted string literals tricks:
# Escaping braces
>>> f"{{74}}"
'{74}'
# decimals and field widths
>>> f'{math.pi:.2f}'
'3.14'
>>> f'{5:>3d}'
' 5'
>>> f'{5:0>3d}' # you can fill with any character, not just 0
'005'
# Right, left and center alignment
>>> f'{123:10d}'
' 123'
>>> f'{123:<10d}'
'123 '
>>> f'{123:^10d}'
' 123 '
# Other bases
>>> f'{255:x}'
'ff'
>>> f'{255:o}'
'377'
>>> f'{255:b}'
'11111111'
# Adding commas
>>> f'{1234567:,}'
'1,234,567'
# Python 3.8+:
# https://stribny.name/blog/2019/06/debugging-python-programs
# https://realpython.com/python38-new-features/
>>> print(f"{i=}, {word=}")
i=42, word=everything
# Justification:
print(repr(x).rjust(4))
# Older, pre-3.6 Pythons can use format():
filename = 'file{0}.txt'.format(num)
# Using variable names with format()
filename = 'file%(num)s.txt' % locals() # Neat trick
# Using string.Template:
filename = string.Template('file${num}.txt').substitute(locals()))
############################
# Frustrations at exceptions when printing,
# when Python (even Python 3) wants to convert to ascii rather than
# the system encoding:
# I need a test case for this, so save one next time it happens!
# Otherwise, ignore this section, they're just notes to help debug
# next time I hit this problem.
# New in Python 3.7:
sys.stdout.reconfigure(errors='surrogateescape')
# In 3.6, you can get a similar effect with an env variable:
PYTHONIOENCODING=utf-8:surrogateescape
# In Python2, this might also help:
sys.setdefaultencoding('utf8')
# though it's frowned upon:
# https://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script/34378962#34378962
# You'd think it would use the system locale by default for printing,
# and quite a few pages claim UTF-8 is the default, but that doesn't
# seem to be true: even in Python 3 I frequently see exceptions
# indicating that for some bizarre reason python3 is trying to
# convert to ascii for printing. And in python3, sys.getdefaultencoding()
# is already utf-8 so that doesn't explain the ascii codec exceptions.
#############################
# Speaking of exception frustrations: here's how to print a traceback
# from the current exception. Curiously, you don't need to pass in the
# actual exception.
traceback.format_exc()
########################################################
# Byte strings and byte arrays
########################################################
buf = bytearray(b'\x51\x02\x00\x00\x00')
buf.append(0xa2)
buf.insert(2, 0xf7)
# struct: https://docs.python.org/2/library/struct.html
# is perhaps a better way to handle byte strings like this.
########################################################
# iterator, list and dictionary helpers
########################################################
# Remove items from a list: I always forget how to do this.
mylist.remove("item") # Removes the first instance of "item"
mylist.pop(i) # Removes and returns list[i]
# There's no single call to remove ALL instances of an item,
# so you have to use a list comprehension to do that.
# () turns a list comprehension into a generator:
>>> ( i*2 for i in range(5) )
<generator object <genexpr> at 0x7f8fc17db050>
# Delete an item from a dictionary:
del thedic[key]
# Insert an item into a list BEFORE the given index:
l = ['a', 'b', 'c', 'd', 'e']
l.insert(3, 'xxx')
# --> ['a', 'b', 'c', 'xxx', 'd', 'e']
# List comprehensions can be multiple:
[ a*b+c for a in A for b in B for c in C ]
# though itertools.product is arguably cleaner for math problems like that.
# Comprehensions can also have 'if' expressions, but the syntax
# is a little tricky and inconsistent. A conditional with no "else"
# must come at the end, after the list, but a conditional with an else
# may come before the list.
nums = [1, 2, 3, 4, 5]
odds = [n for n in nums if n%2]
isodd = ['t' if n%2 else 'f' for n in nums]
# There are dict comprehensions too though the syntax is more fiddly:
>>> { key: value for key, value in [ (1, 11), (2, 22), (3, 33) ] }
{1: 11, 2: 22, 3: 33}
>>> { key: value for key, value in zip( [1, 2, 3], [11, 22, 33] ) }
{1: 11, 2: 22, 3: 33}
# dictionary default values, several ways:
# With regular dicts
total['newvalue']] = total.get(key, 0) + 42
# With collections:
from collections import defaultdict
total = defaultdict(int)
total['newvalue'] += 42
# Pairwise loops with zip():
names = ["Eiffel Tower", "Empire State", "Sears Tower"]
heights = [324, 381, 442]
for name, height in zip(names, heights):
print "%s: %s meters" % (name, height)
# Or make a dictionary from a zip():
tall_buildings = dict(zip(names, heights))
print max(tall_buildings.values())
#
# Read a file of name=value pairs and return a dictionary.
#
# https://mail.python.org/pipermail/baypiggies/2015-October/009556.html
def file2dict(filename):
with open(filename) as af:
return dict(line.strip().split('=',1) for line in af)
#
# Walk a directory tree
#
def walkfiles(rootdir):
for root, dirs, files in os.walk(rootdir):
for f in files:
print os.path.join(root, f)
# os.walk is handy, but it doesn't allow any type of sorting.
# So here's a rewritten os.walk that sorts alphabetically.
def pathwalk(top, topdown=True, onerror=None, followlinks=False, sortfn=None):
# We may not have read permission for top, in which case we can't
# get a list of the files the directory contains. os.path.walk
# always suppressed the exception then, rather than blow up for a
# minor reason when (say) a thousand readable directories are still
# left to visit. That logic is copied here.
try:
names = os.listdir(top)
if sortfn:
names.sort(sortfn)
else:
names.sort()
except os.error, err:
if onerror is not None:
onerror(err)
return
dirs, nondirs = [], []
for name in names:
if os.path.isdir(os.path.join(top, name)):
dirs.append(name)
else:
nondirs.append(name)
if topdown:
yield top, dirs, nondirs
for name in dirs:
path = os.path.join(top, name)
if followlinks or not os.path.islink(path):
for x in pathwalk(path, topdown, onerror, followlinks):
yield x
if not topdown:
yield top, dirs, nondirs
########################################################
# Useful regular expressions
########################################################
# Difference between match and search:
# match matches only from the beginning of the string,
# search will look anywhere in the string.
# Find MAC address:
match = re.search(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', instr, re.I)
if match: return match.group()
# Find IP address:
match = re.search(r'([0-9]{1,3}[\.]){3}([0-9]{1,3})', instr)
if match: return match.group()
########################################################
# Command-line Argument parsing
########################################################
# #python recommends click first (not installed by default),
# then argparse, over optparse.
import argparse
def parse_args():
"""Parse commandline arguments."""
parser = argparse.ArgumentParser(description="Do some stuff")
# Boolean flag
parser.add_argument('-c', "--check", dest="check", default=False,
action="store_true", help="Help string")
# int or string flag.
# Without type=, will store a string.
# For a mandatory argument, add required=True.
parser.add_argument('-b', action="store", default=2, dest="beta", type=int,
help='Beta parameter (default: 2)')
# Date
parser.add_argument('-s', "--start", dest="start_date", default=None,
help="Start date, YYYY-MM-DD, "
"default beginning of this year",
type=lambda s: datetime.datetime.strptime(s,
'%Y-%m-%d'))
# Flag that takes multiple arguments, with different names for each arg.
# Note that this is also a way around the problem of passing
# an argument that starts with a dash: argparse won't allow strings
# that start with a dash, thinking they're flags, but if they
# look like numbers with no extra characters embedded, it will
# allow them.
parser.add_argument('-a', '--area', action='store',
type=int, nargs=4,
dest='area', default=[-180, 180, -70, 74],
metavar=("west", "east", "south", "north"),
help="Area to plot")
# single positional argument
parser.add_argument('url', help='The URL to open')
parser.add_argument('urls', nargs='?', default='http://localhost/',
help="URLs to open")
# or, multiple arguments requiring at least one.
parser.add_argument('urls', nargs='+', default='http://localhost/',
help="URLs to open")
# nargs can also be an integer, but usage statements will repeat
# the argument name N times, e.g. this will say "coords coords"
# so be sure to have a good help string as well.
# If using numeric types, argparse is smart enough to allow negative vals.
parser.add_argument('coords', nargs=2, type=float,
help="longitude latitude in decimal degrees")
args = parser.parse_args(sys.argv[1:])
# Now we have args.check, args.beta, args.url or urls.
# parse_known_args() is like parse_args() except that it doesn't
# give an error if you pass extra arguments; instead, it returns
# a 2-item tuple, consisting of the arg namespace and a list of
# the remaining args:
args, rest = parser.parse_known_args(sys.argv)
# Optional argument example:
parser.add_argument('-n', "--dryrun", dest="dryrun", default=False,
action="store_true")
parser.add_argument('-s', "--sync", dest="sync", default=False,
action="store_true")
parser.add_argument("src")
parser.add_argument("dst", nargs='?')
args = parser.parse_args(sys.argv)
# For something more complicated, like different numbers of arguments
# depending on a flag, it's not clear argparse can handle that.
# See androidfiles.py for a sample workaround.
# To allow for newlines in the help strings:
parser = argparse.ArgumentParser(description="Long string\nwith newlines",
formatter_class=argparse.RawTextHelpFormatter)
# Print usage:
parser.print_help()
########################################################
# Dates and times
########################################################
# Printing formats:
>>> dt = datetime.datetime.now()
>>> dt.strftime("%Y-%m-%d %H:%M")
'2019-07-22 19:46'
>>> dt.isoformat()
'2019-07-22T19:46:20.164427'
>>> f"{dt:%Y-%m-%d %H:%M}" # Only in Python >= 3.6
'2019-07-22 19:46'
#
# Add N months to a date: same day of month but next month.
#
import datetime
from dateutil.relativedelta import relativedelta
today = datetime.date.today()
three_months_from_now = today + relativedelta(months=3)
# Note that relativedelta can also take a month= as well as a months=.
# month gives you the current dayofmonth in a specific month number;
# months gives you how many months relative to the current one.
# For differences of just days or weeks, datetime.timedelta works.
def nextmonth(d):
'''Beginning of the following month.
'''
month = d.month + 1
year = d.year
if month > 12:
month = 1
year += 1
return d.replace(day=1, month=month, year=year)
# Beginning of today:
datetime.datetime.now().replace(hour=0, minute=0,
second=0, microsecond=0)
# Earliest and latest dates:
datetime.datetime.min, datetime.datetime.max
# Subtracting datetimes gives a datetime.timedelta, and that's also
# a good way to add or subtract time from a datetime.
now = datetime.datetime.now()
if (now - time_end).seconds < 7200:
time_end = now - datetime.timedelta(seconds=7200)
#
# Parse a date in RFC 2822 format.
#
datetime.datetime.strptime('2016-01-01', '%Y-%m-%d')
# Same thing with decimal seconds:
datetime.datetime.strptime('2016-01-01.234', '%Y-%m-%d.%f')
# datetime to Unix timestamp:
time.mktime(d.timetuple())
# Unix timestamp to datetime:
datetime.datetime.fromtimestamp(1553010256)
datetime.datetime.utcfromtimestamp(1553010256)
# but unfortunately there's no way, with just the Python core,
# to parse a date that might or might not have decimal seconds.
# You have to strip it off in this way, which seems horrifying
# but you'll see it recommended all over, and you'd better hope
# the decimal point is where you expect it and not some other place
# inside the string:
timestr = '2016-01-01.234'
if '.' in timestr:
d = datetime.datetime.strptime(timestr, '%Y-%m-%d.%f')
else:
d = datetime.datetime.strptime(timestr, '%Y-%m-%d')
# email.utils.parsedate returns a tuple.
# But be warned it can only parse a full date/time, not just a date,
# even though the time part of the tuple generally isn't correct.
t = time.mktime(email.utils.parsedate("Thu, 11 Aug 2016 14:46:50 GMT")))
(y, m, d, h, m, s, weekday, yearday, isdst) = t
# the last three items of the tuple aren't very useful: typically 0, 1, -1.
# -1 means "unknown" for the dst flag.
secs_since_epoch = time.mktime(t)
t2 = time.mktime_tz(email.utils.parsedate("Thu, 11 Aug 2016 14:46:50 GMT")))
(y, m, d, h, m, s, weekday, yearday, isdst, offset_from_utc) = t2
secs_since_epoch = email.utils.mktime_tz(t2)
#
# Parse a date in unknown format into a datetime.datetime object
# Unfortunately dateutil isn't part of the python core,
# it's a separate package so it adds a dependency.
# But it's more reliable than email.utils.parsedate.
#
import dateutil.parser
d = dateutil.parser.parse("2012-08-16 14:25:05.265739")
d = dateutil.parser.parse("10/31/2016 14:25")
d = dateutil.parser.parse("6/15/2016 14:25 MDT")
# Also see the Arrow library, a Datetime replacement
# that offers super-general date parsing like "an hour ago".
#
# Calendar.timedelta
#
import datetime
import calendar
today = datetime.date.today()
days_this_month = calendar.monthrange(today.year, today.month)[1]
one_month_from_now = today + datetime.timedelta(days=days_this_month)
# There's also isodate.parse_datetime which I haven't looked into yet.
# Number of days in a month:
calendar.monthrange(year, month)[1]
# monthrange returns weekday of the first day and number of days in the month.
# Or do it using only datatime:
(datetime.datetime(year, month % 12 + 1, 1) - datetime.timedelta(days=1)).day
########################################################
# Threading and multiprocessing
########################################################
# Easy way to schedule something:
# In a single-threaded environment:
import sched, time
def print_time():
print "From print_time", time.time()
if __name__ == '__main__':
s = sched.scheduler(time.time, time.sleep)
s.enter(5, 1, print_time, ())
s.enter(10, 1, print_time, ())
s.run()
# In multi-threaded environments:
from threading import Timer
import time
def run_later(a, b):
print("Hello, it's later now, and time is %f" % time.time())
print(a, b)
if __name__ == '__main__':
Timer(5, run_later, (1, 2)).start()
Timer(11, run_later, (4, 5)).start()
for i in range(10):
print(i*2)
time.sleep(2)
########################################################
# CSV
########################################################
with open(filename) as csvfp:
reader = csv.DictReader(csvfp)
for row in reader:
# Each row is an OrderedDict
########################################################
# BeautifulSoup
########################################################
Difference between .string and .text:
.string returns a NavigableString object, which offers a lot of
the same methods tags do.
.text returns a unicode object that concatenates all the child strings.
Useful recent additions: tag.replace_with_children()
# Find tags with inline style attribute:
for t in soup.findAll(style=True)
# Harder way, using lambda:
soup.findAll(lambda tag: 'style' in tag.attrs)
########################################################
# Handling cookies with Requests
########################################################
# To handle cookies with requests, ignore the Requests documentation
# that says to use a RequestCookieJar: that's apparently only for
# setting cookies, not fetching them. Instead, use a Session:
session = requests.Session()
r = session.get(url)
########################################################
# Some handy utility classes
########################################################
# Copying and moving files: shutil.copy and shutil.move
# Handle quoting for something that might need to be passed to a shell:
# in Python 3, shlex.quote() does it, but if it needs to be compatible
# with both 2 and 3, use pipes.quote().
########################################################
# subprocess
########################################################
# Read lines from a subprocess as they appear:
outstring = subprocess.check_output(['identify', filename))
# To suppress stderr, add stderr=subprocess.DEVNULL
# More complicated way:
import subprocess
proc = subprocess.Popen(["procname"], stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
print("line: %s" % line)
# Chain a multi-command pipeline:
p1 = subprocess.Popen([args1],
shell=False, stdout=subprocess.PIPE)
p2 = subprocess.Popen([args2],
shell=False, stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
p3 = subprocess.Popen([args3],
shell=False, stdin=p2.stdout, stdout=subprocess.PIPE)
p2.stdout.close()
p4 = subprocess.Popen([args4].
shell=False, stdin=p3.stdout, stdout=subprocess.PIPE)
p3.stdout.close()
output = p4.communicate()[0]
########################################################
# CGI: how to tell if something is run as a CGI or locally
########################################################
if 'REQUEST_METHOD' in os.environ:
print("Run as CGI", file=sys.stderr)
form = cgi.FieldStorage()
else:
print("Run locally")
########################################################
# Conditional import and testing imported libraries
########################################################
try:
import foo
except:
pass
import sys
if 'foo' in sys.modules:
myfoo = foo.Foo()
else:
myfoo = None
########################################################
# Import of runtime-specified modules and functions
########################################################
modulename = 'MyModule'
functionname = 'TheFunction'
themodule = __import__(modulename)
val = getattr(themodule, functionname)()
########################################################
# OS-specific stuff
########################################################
# Read keys in cbreak mode.
# Some info at:
# http://docs.python.org/2/faq/library.html#how-do-i-get-a-single-keypress-at-a-time
# but it's incomplete, so see
# https://github.com/akkana/scripts/blob/master/keyreader.py
# for a better solution.
#
# Of course, you can also do this with curses.
########################################################
# Lambda foo
########################################################
#
# map + lambda example
#
def helloall(names):
return '\n'.join(map(lambda name: "Hello, " + name, names))
# but in practice, you generally shouldn't need map+lambda, because
# map(lambda x: <expr>, xs) can be rewritten as (<expr> for x in xs)
# and the latter is much more readable.
#
# filter + lambda example
#
def only_even(numbers):
return filter(lambda x: x%2 == 0, numbers)
#
# Simple map used for rounding.
# int() truncates, round() rounds but doesn't return an int.
#
def roundall(numbers):
return map(int, map(round, numbers))
#
# sorting + lambda examples
#
# The cmp function is obsolete in py3. Instead, use a key function,
# which is called on each element of the list prior to sorting,
# and for each item, returns something that can be simply sorted,
# like a string or int.
# https://wiki.python.org/moin/HowTo/Sorting
# The advantage of a key function is that a comparison function needs
# to be called on every comparison, a key function only once.
#
def sort_by_last_letter(words):
# sorted() returns a new sorted list.
print sorted(words, key = lambda a: a[-1])
# list.sort() modifies the list in place
words.sort(key = lambda a: a[-1])
print words
#
# Reduce example
#
# https://mail.python.org/pipermail/baypiggies/2015-September/009548.html
# Note: There's also collections.Counter.
def letter_frequency(data):
def count(total, item):
total[item] = total.get(item, 0) + 1
return total
return reduce(count, data, {})
if __name__ == "__main__":
import os
print "This is file: ", __file__
print "which is really:", os.path.realpath(__file__)
#
# Enum values in PyGTK. I'm forever running up against dialogs that
# return some random undocumented negative number from run(),
# and there's no way to find out which gtk.RESPONSE_FOO
# the negative number corresponds to.
#
def enumval(e):
for i in range(-1000, 1000):
if e == i:
return i
return None
# This is also a useful hint for how to look up an arbitrary string
# in the environment.
# list from http://www.pygtk.org/pygtk2reference/gtk-constants.html#gtk-response-type-constants
for s in ("NONE", "REJECT", "ACCEPT", "DELETE_EVENT", "OK", "CANCEL", "CLOSE", "YES", "NO", "APPLY", "HELP"):
print s, eval("enumval(gtk.RESPONSE_" + s + ")")
# As of Dec 2016, this gives:
# NONE -1
# REJECT -2
# ACCEPT -3
# DELETE_EVENT -4
# OK -5
# CANCEL -6
# CLOSE -7
# YES -8
# NO -9
# APPLY -10
# HELP -11
################################################################
# Pathlib, cross-platform replacement for os.path and much more.
################################################################
>>> from pathlib import Path, PosixPath
# For paths in Python3=only projects, consider using pathlib rather than os.path
# https://docs.python.org/3/library/pathlib.html#pathlib.Path
Path.home() # the user's homedir
>>> rock = Path('~', 'Music', 'Rock').expanduser()
>>> rock
PosixPath('/home/akkana/Music/Rock')
>>> g = rock.rglob('*.mp3')
>>> g
<generator object Path.rglob at 0x7f630d9e48d0>
>>> list(g)
# ... list of PosixPath objects
# rglob is recursive, glob is nonrecursive unless the pattern starts with **/
# which means “this directory and all subdirectories, recursively”.
>>> p = Path()
>>> p
PosixPath('.')
>>> p.resolve()
PosixPath('/home/username/pathlib')
# Other methods include .mkdir(), .rmdir(), .unlink().rename(), .exists(),
# .is_dir(), .is_file(), .stat(),
# .open(), .read_bytes(), .read_text(), .write_bytes(), .write_text()
# reads and writes don't require open first
################################################################
# Nonlocal variables, class statics, and closures
################################################################
# Class-static variables, more or less:
def f(a):
try:
f.numruns += 1
except AttributeError:
# First time through
f.numruns = 1
# To avoid try/except you can also test if 'numruns' in f.__dict__
# or hasattr(f, 'numruns')
print(a, "f has been run %d times" % f.numruns)
f(1)
f(12)
f(42)
# Variables inside an inner function can see outer function variables
# (including parameters) as read-only. Use "nonlocal" to modify them.
# Example:
def outer_fcn():
thestr = "Set by outer"
def inner_fcn1():
print("Inside fcn1:", thestr)
def inner_fcn2():
thestr = "Inner function overrode the variable"
print("Inside fcn2:", thestr)
def inner_fcn3():
nonlocal thestr
thestr = "Changed by inner function"
print("Inside fcn3:", thestr)
print("Initially:", thestr)
inner_fcn1()
print("After fcn1:", thestr)
inner_fcn2()
print("After fcn2:", thestr)
inner_fcn3()