|
227 | 227 | <strong><span class="hljs-string"><span class="hljs-string">'5. Data'</span></span></strong>: [<a href="#json">JSON</a>, <a href="#pickle">Pickle</a>, <a href="#csv">CSV</a>, <a href="#sqlite">SQLite</a>, <a href="#bytes">Bytes</a>, <a href="#struct">Struct</a>, <a href="#array">Array</a>, <a href="#memoryview">Memory_View</a>, <a href="#deque">Deque</a>],
|
228 | 228 | <strong><span class="hljs-string"><span class="hljs-string">'6. Advanced'</span></span></strong>: [<a href="#threading">Threading</a>, <a href="#operator">Operator</a>, <a href="#introspection">Introspection</a>, <a href="#metaprograming">Metaprograming</a>, <a href="#eval">Eval</a>, <a href="#coroutines">Coroutine</a>],
|
229 | 229 | <strong><span class="hljs-string"><span class="hljs-string">'7. Libraries'</span></span></strong>: [<a href="#progressbar">Progress_Bar</a>, <a href="#plot">Plot</a>, <a href="#table">Table</a>, <a href="#curses">Curses</a>, <a href="#logging">Logging</a>, <a href="#scraping">Scraping</a>, <a href="#web">Web</a>, <a href="#profiling">Profile</a>,
|
230 |
| - <a href="#numpy">NumPy</a>, <a href="#image">Image</a>, <a href="#audio">Audio</a>, <a href="#pygame">Games</a>, <a href="#pandas">Data</a>, <a href="#cython">Cython</a>] |
| 230 | + <a href="#numpy">NumPy</a>, <a href="#image">Image</a>, <a href="#audio">Audio</a>, <a href="#pygame">Games</a>, <a href="#pandas">Data</a>, <a href="#gui">GUI</a>] |
231 | 231 | }
|
232 | 232 | </code></pre></div></div>
|
233 | 233 |
|
|
519 | 519 | {<el>:^<span class="hljs-number">10</span>} <span class="hljs-comment"># ' <el> '</span>
|
520 | 520 | {<el>:><span class="hljs-number">10</span>} <span class="hljs-comment"># ' <el>'</span>
|
521 | 521 | {<el>:.<<span class="hljs-number">10</span>} <span class="hljs-comment"># '<el>......'</span>
|
522 |
| -{<el>:<<span class="hljs-number">0</span>} <span class="hljs-comment"># '<el>'</span> |
| 522 | +{<el>:<span class="hljs-number">0</span>} <span class="hljs-comment"># '<el>'</span> |
523 | 523 | </code></pre></div>
|
524 | 524 |
|
525 | 525 | <div><h3 id="strings">Strings</h3><p><strong><code class="python hljs"><span class="hljs-string">'!r'</span></code> calls object's <a href="#class">repr()</a> method, instead of <a href="#class">str()</a>, to get a string.</strong></p><pre><code class="python language-python hljs">{<span class="hljs-string">'abcde'</span>!r:<span class="hljs-number">10</span>} <span class="hljs-comment"># "'abcde' "</span>
|
|
2175 | 2175 |
|
2176 | 2176 | <div><h4 id="test">Test:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install requests</span>
|
2177 | 2177 | <span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> requests
|
2178 |
| -<span class="hljs-meta">>>> </span>url = <span class="hljs-string">'http://localhost:8080/odds/football'</span> |
| 2178 | +<span class="hljs-meta">>>> </span>url = <span class="hljs-string">'http://localhost:8080/odds/football'</span> |
2179 | 2179 | <span class="hljs-meta">>>> </span>data = {<span class="hljs-string">'team'</span>: <span class="hljs-string">'arsenal f.c.'</span>}
|
2180 | 2180 | <span class="hljs-meta">>>> </span>response = requests.post(url, data=data)
|
2181 | 2181 | <span class="hljs-meta">>>> </span>response.json()
|
|
2841 | 2841 | </code></pre></div>
|
2842 | 2842 |
|
2843 | 2843 |
|
2844 |
| -<div><h2 id="plotly"><a href="#plotly" name="plotly">#</a>Plotly</h2><div><h3 id="coviddeathsbycontinent">Covid Deaths by Continent</h3><p></p><div id="2a950764-39fc-416d-97fe-0a6226a3095f" class="plotly-graph-div" style="height:400px; width:100%;"></div><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pandas plotly</span> |
2845 |
| -<span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd |
2846 |
| -<span class="hljs-keyword">import</span> plotly.express |
| 2844 | +<div><h2 id="plotly"><a href="#plotly" name="plotly">#</a>Plotly</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install plotly</span> |
| 2845 | +<span class="hljs-keyword">from</span> plotly.express <span class="hljs-keyword">import</span> line |
| 2846 | +<Figure> = line(<DF>, x=<col_name>, y=<col_name>) <span class="hljs-comment"># Or: line(x=<list>, y=<list>)</span> |
| 2847 | +<Figure>.write_html/json/image(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Also: <Figure>.show()</span> |
| 2848 | +</code></pre></div> |
2847 | 2849 |
|
2848 |
| -covid = pd.read_csv(<span class="hljs-string">'https://covid.ourworldindata.org/data/owid-covid-data.csv'</span>, |
| 2850 | +<div><h4 id="coviddeathsbycontinent">Covid deaths by continent:</h4><p></p><div id="2a950764-39fc-416d-97fe-0a6226a3095f" class="plotly-graph-div" style="height:360px; width:100%;"></div><pre><code class="python language-python hljs">covid = pd.read_csv(<span class="hljs-string">'https://covid.ourworldindata.org/data/owid-covid-data.csv'</span>, |
2849 | 2851 | usecols=[<span class="hljs-string">'iso_code'</span>, <span class="hljs-string">'date'</span>, <span class="hljs-string">'total_deaths'</span>, <span class="hljs-string">'population'</span>])
|
2850 | 2852 | continents = pd.read_csv(<span class="hljs-string">'https://datahub.io/JohnSnowLabs/country-and-continent-codes-'</span> + \
|
2851 | 2853 | <span class="hljs-string">'list/r/country-and-continent-codes-list-csv.csv'</span>,
|
|
2855 | 2857 | df[<span class="hljs-string">'Total Deaths per Million'</span>] = df.total_deaths * <span class="hljs-number">1e6</span> / df.population
|
2856 | 2858 | df = df[(<span class="hljs-string">'2020-03-14'</span> < df.date) & (df.date < <span class="hljs-string">'2020-06-25'</span>)]
|
2857 | 2859 | df = df.rename({<span class="hljs-string">'date'</span>: <span class="hljs-string">'Date'</span>, <span class="hljs-string">'Continent_Name'</span>: <span class="hljs-string">'Continent'</span>}, axis=<span class="hljs-string">'columns'</span>)
|
2858 |
| -plotly.express.line(df, x=<span class="hljs-string">'Date'</span>, y=<span class="hljs-string">'Total Deaths per Million'</span>, color=<span class="hljs-string">'Continent'</span>).show() |
2859 |
| -</code></pre></div></div> |
2860 |
| - |
| 2860 | +line(df, x=<span class="hljs-string">'Date'</span>, y=<span class="hljs-string">'Total Deaths per Million'</span>, color=<span class="hljs-string">'Continent'</span>).show() |
| 2861 | +</code></pre></div> |
2861 | 2862 |
|
2862 | 2863 |
|
2863 | 2864 |
|
2864 |
| -<div><h3 id="confirmedcovidcasesdowjonesgoldandbitcoinprice">Confirmed Covid Cases, Dow Jones, Gold, and Bitcoin Price</h3><p></p><div id="e23ccacc-a456-478b-b467-7282a2165921" class="plotly-graph-div" style="height:400px; width:100%;"></div><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pandas plotly</span> |
2865 |
| -<span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd |
2866 |
| -<span class="hljs-keyword">import</span> plotly.graph_objects <span class="hljs-keyword">as</span> go |
| 2865 | +<div><h4 id="confirmedcovidcasesdowjonesgoldandbitcoinprice">Confirmed covid cases, Dow Jones, gold, and Bitcoin price:</h4><p></p><div id="e23ccacc-a456-478b-b467-7282a2165921" class="plotly-graph-div" style="height:333px; width:100%;"></div><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> plotly.graph_objects <span class="hljs-keyword">as</span> go |
2867 | 2866 | <span class="hljs-keyword">import</span> datetime
|
2868 | 2867 |
|
2869 | 2868 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
|
2870 |
| - data = scrape_data() |
2871 |
| - df = wrangle_data(*data) |
2872 |
| - display_data(df) |
| 2869 | + display_data(wrangle_data(*scrape_data())) |
2873 | 2870 |
|
2874 | 2871 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">scrape_data</span><span class="hljs-params">()</span>:</span>
|
2875 | 2872 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">scrape_yahoo</span><span class="hljs-params">(id_)</span>:</span>
|
|
2911 | 2908 |
|
2912 | 2909 |
|
2913 | 2910 |
|
2914 |
| -<div><h2 id="cython"><a href="#cython" name="cython">#</a>Cython</h2><p><strong>Library that compiles Python code into C.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install cython</span> |
| 2911 | +<div><h2 id="gui"><a href="#gui" name="gui">#</a>GUI</h2><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install PySimpleGUI</span> |
| 2912 | +<span class="hljs-keyword">import</span> PySimpleGUI <span class="hljs-keyword">as</span> sg |
| 2913 | +layout = [[sg.Text(<span class="hljs-string">"What's your name?"</span>)], [sg.Input()], [sg.Button(<span class="hljs-string">'Ok'</span>)]] |
| 2914 | +window = sg.Window(<span class="hljs-string">'Window Title'</span>, layout) |
| 2915 | +event, values = window.read() |
| 2916 | +print(<span class="hljs-string">f'Hello <span class="hljs-subst">{values[<span class="hljs-number">0</span>]}</span>! Thanks for trying PySimpleGUI'</span>) |
| 2917 | +</code></pre></div> |
| 2918 | + |
| 2919 | +<div><h2 id="appendix"><a href="#appendix" name="appendix">#</a>Appendix</h2><div><h3 id="cython">Cython</h3><p><strong>Library that compiles Python code into C.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install cython</span> |
2915 | 2920 | <span class="hljs-keyword">import</span> pyximport; pyximport.install()
|
2916 | 2921 | <span class="hljs-keyword">import</span> <cython_script>
|
2917 | 2922 | <cython_script>.main()
|
2918 |
| -</code></pre></div> |
| 2923 | +</code></pre></div></div> |
| 2924 | + |
2919 | 2925 |
|
2920 | 2926 |
|
2921 |
| -<div><h3 id="definitions">Definitions</h3><ul> |
| 2927 | +<div><h4 id="definitions">Definitions:</h4><ul> |
2922 | 2928 | <li><strong>All <code class="python hljs"><span class="hljs-string">'cdef'</span></code> definitions are optional, but they contribute to the speed-up.</strong></li>
|
2923 | 2929 | <li><strong>Script needs to be saved with a <code class="python hljs"><span class="hljs-string">'pyx'</span></code> extension.</strong></li>
|
2924 | 2930 | </ul><pre><code class="python language-python hljs">cdef <type> <var_name> = <el>
|
|
2934 | 2940 | </code></pre>
|
2935 | 2941 | <pre><code class="python language-python hljs">cdef enum <enum_name>: <member_name_1>, <member_name_2>, ...
|
2936 | 2942 | </code></pre>
|
2937 |
| -<div><h2 id="appendix"><a href="#appendix" name="appendix">#</a>Appendix</h2><div><h3 id="pyinstaller">PyInstaller</h3><pre><code class="bash language-bash hljs"><code class="bash language-bash hljs">$ pip3 install pyinstaller |
| 2943 | +<div><h3 id="pyinstaller">PyInstaller</h3><pre><code class="bash language-bash hljs"><code class="bash language-bash hljs">$ pip3 install pyinstaller |
2938 | 2944 | $ pyinstaller script.py <span class="hljs-comment"># Compiles into './dist/script' directory.</span>
|
2939 | 2945 | $ pyinstaller script.py --onefile <span class="hljs-comment"># Compiles into './dist/script' console app.</span>
|
2940 | 2946 | $ pyinstaller script.py --windowed <span class="hljs-comment"># Compiles into './dist/script' windowed app.</span>
|
2941 | 2947 | $ pyinstaller script.py --add-data '<path>:.' <span class="hljs-comment"># Adds file to the root of the executable.</span>
|
2942 |
| -</code></code></pre></div></div> |
2943 |
| - |
| 2948 | +</code></code></pre></div> |
2944 | 2949 |
|
2945 | 2950 | <ul>
|
2946 | 2951 | <li><strong>File paths need to be updated to <code class="python hljs"><span class="hljs-string">'os.path.join(sys._MEIPASS, <path>)'</span></code>.</strong></li>
|
|
0 commit comments