-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_one-liners.py
More file actions
50 lines (37 loc) · 1 KB
/
20_one-liners.py
File metadata and controls
50 lines (37 loc) · 1 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
# one-liner Python commands
# 1. simple web server
'''
# Python 2
python -m SimpleHTTPServer
# Python 3
python -m http.server
'''
# 2.pretty printing
import itertools
from pprint import pprint
my_dict = {'name': 'Yasoob', 'age': 'undefined', 'personality': 'awesome'}
print(dir(my_dict))
pprint(dir(my_dict))
# cat file.json | python -m json.tool
# 3. profiling a script
# python -m cProfile my_script.py
# 4. CSV to json
# python -c "import csv,json;print json.dumps(list(csv.reader(open('csv_file.csv'))))"
# 5. list flattening
a_list = [[1, 2], [3, 4], [5, 6]]
print(list(itertools.chain.from_iterable(a_list)))
# Output: [1, 2, 3, 4, 5, 6]
# or
print(list(itertools.chain(*a_list)))
# Output: [1, 2, 3, 4, 5, 6]
# 6. One-Line Constructors
'''
avoid boilerplate assignments
'''
class A(object):
def __init__(self, a, b, c, d, e, f):
self.__dict__.update(
{k: v for k, v in locals().items() if k != 'self'})
'''
레퍼런스: https://wiki.python.org/moin/Powerful%20Python%20One-Liners
'''