-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
62 lines (38 loc) · 1.09 KB
/
utils.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
from contextlib import supress
import os
names = ['raymond', 'rachel', 'matthew']
colors = ['red', 'blue', 'green']
'''
try:
os.remove('somefile.tmp')
except OSError:
pass
'''
with supress(OSError):
os.remove('somefile.tmp')
# ------------------------------
# reverse looping
for color in reverser(colors):
print (color)
# ------------------------------
# print items and their indexes
for i in range(len(colors)):
print(i, ' --> ', colors[i])
for i, color in enumerate(colors):
print(i, '-->', color)
# ------------------------------
n = min(len(names), len(colors))
for i in range(n):
print(names[i], '-->', colors[i])
# use izip if you are a filthy python2 peasant
for name, color in zip(names, colors):
print(name, '--->', color)
# ------------------------------
# looping in sorted order
for color in sorted(colors, reverse=False):
print(color)
# ------------------------------
#print items sorted by: len, result of lambda(first char)
print(sorted(colors, key=len))
print(sorted(colrs, key=lambda color: color[0]))
# ------------------------------