forked from yasoob/intermediatePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathone_liners.html
More file actions
101 lines (89 loc) · 5.29 KB
/
Copy pathone_liners.html
File metadata and controls
101 lines (89 loc) · 5.29 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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>One Liners</title>
<link rel="stylesheet" href="_static/epub.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="for_-_else.html" title="For - Else"
accesskey="N">next</a></li>
<li class="right" >
<a href="lambdas.html" title="Lambdas"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Python Tips 0.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="body" role="main">
<div class="section" id="one-liners">
<h1>One Liners</h1>
<p>In this chapter I will show you some one liner Python commands which can
be really helpful sometimes.</p>
<p><strong>Simple Webserver</strong></p>
<p>Ever wanted to quickly share a file over a network? Well you are in
luck. Python has a similar feature just for you. Go to the directory
which you want to serve over network and write the following code in
terminal:</p>
<div class="code python highlight-python"><div class="highlight"><pre># Python 2
python -m SimpleHTTPServer
# Python 3
python -m http.server
</pre></div>
</div>
<p><strong>Pretty printing</strong></p>
<p>You can print a list and dictionary in a beautiful format in Python
repl. Here is the relevant code:</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pprint</span> <span class="kn">import</span> <span class="n">pprint</span>
<span class="n">my_dict</span> <span class="o">=</span> <span class="p">{</span><span class="s">'name'</span><span class="p">:</span><span class="s">'Yasoob'</span><span class="p">,</span>
<span class="s">'age'</span><span class="p">:</span><span class="s">'undefined'</span><span class="p">,</span><span class="s">'personality'</span><span class="p">:</span><span class="s">'awesome'</span><span class="p">}</span>
<span class="n">pprint</span><span class="p">(</span><span class="n">my_dict</span><span class="p">)</span>
</pre></div>
</div>
<p>This is more effective on dicts. Moreover, if you want to pretty print
json quickly from a file then you can simply do:</p>
<div class="code python highlight-python"><div class="highlight"><pre>cat file.json | python -m json.tools
</pre></div>
</div>
<p><strong>Profiling a script</strong></p>
<p>This can be extremely helpful in pin pointing the bottlenecks in your
scripts.</p>
<div class="code python highlight-python"><div class="highlight"><pre>python -m cProfile my_script.py
</pre></div>
</div>
<p>Note: <code class="docutils literal"><span class="pre">cProfile</span></code> is a faster implementation of <code class="docutils literal"><span class="pre">profile</span></code> as it is
written in c</p>
<p><strong>CSV to json</strong></p>
<p>Run this in the terminal:</p>
<div class="code python highlight-python"><div class="highlight"><pre>python -c "import csv,json;print json.dumps(list(csv.reader(open('csv_file.csv'))))"
</pre></div>
</div>
<p>Make sure that you replace <code class="docutils literal"><span class="pre">csv_file.csv</span></code> to the relevant file name.</p>
<p><strong>List Flattening</strong></p>
<p>You can quickly and easily flatten a list using
<code class="docutils literal"><span class="pre">itertools.chain.from_iterable</span></code> from the <code class="docutils literal"><span class="pre">itertools</span></code> package. Here
is a simple example:</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">a_list</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]]</span>
<span class="k">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span><span class="n">a_list</span><span class="p">)))</span>
<span class="c"># Output: [1, 2, 3, 4, 5, 6]</span>
</pre></div>
</div>
<p>A couple of more one liners can be found on the <a class="reference external" href="https://wiki.python.org/moin/Powerful%20Python%20One-Liners">Python
website</a><span class="link-target"> [https://wiki.python.org/moin/Powerful%20Python%20One-Liners]</span></p>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer" role="contentinfo">
© Copyright 2015, Muhammad Yasoob Ullah Khalid.
</div>
</body>
</html>