-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
700 lines (646 loc) · 24.8 KB
/
index.html
File metadata and controls
700 lines (646 loc) · 24.8 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
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Modern Programming for Data Analytics - Intro to Python</title>
<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="dist/theme/solarized.css">
<link rel="stylesheet" href="css/custom.css">
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/monokai.css">
<link rel="stylesheet" href="plugin/title-footer/title-footer.css">
<link rel="stylesheet" href="plugin/asciinema/asciinema-player.css">
<body>
<div class="reveal">
<div class="slides">
<section id="titleSlide">
<h2> Modern Programming for Data Analytics - Very Brief Intro to Python </h2>
<h4> Instructor: Shawn T. Brown </h4>
</section>
<section>
<section>
<h3> Python: </h3>
<ul class="r-fit-text">
<li> is an interpreted high-level general-purpose programming language.
<li> is an indented language that promotes highly readable programming.
<li> supports both object-oriented programming and procedural progamming models.
<li> has a massive community level of support with a plethora of modules to add a wide-array of functionality
<li> the most widely used programming language (31.47% according to PYPL)
</ul>
</section>
<section>
<h3>Python in scientific programming</h3>
<ul class="r-fit-text">
<li> Python was widely thought to not be high-performance
<ul>
<li> modules such as Numpy, SciPy, and Pandas have ensured that Python is a high-performance language
</ul>
<li> Python is by far the language of choice for Artificial Intelligence applications
<ul>
<li> Frameworks such as TensorFlow, PyTorch, and Caffe are all Python frameworks
</ul>
<li> Most scientific applications and data frameworks, while maybe not written in Python provide robust Python interfaces for data and procedural workflows
</ul>
</section>
<section>
<h3> Brief Python Language Basics</h3>
<p> For those of you not familiar with Python, there are some excellent online tutorials
that you can use to catchup</p>
<ul>
<li> <a href="https://docs.python.org/3/tutorial/" target="_blank">
The Official "Python Tutorial
</a>
<li> <a href="https://www.learnpython.org/" target="_blank">
Interactive LearnPython.org tutorial
</a>
<li> <a href="https://python.readthedocs.io/en/latest/" target="_blank">
Python Language - Read the Docs </a>
</ul>
</section>
<section>
<h3> A simple python program </h3>
<pre class="python"><code data-line-numbers="2|15-16|20-24|28-31|35-39|4-8|43-46|48-49">
import os
def print_message_from_file(filename):
with open(filename, "r") as f:
message = f.read()
print("This is the message")
print("{}".format(message))
def print_separator(n=45):
print("{}".format("".join(["-" for x in range(0,n)])))
def main():
for i in range(0, 10):
print("Hello World!#{}".format(i))
print_separator()
sample_list = [0, 1, 2, 3, 4,
5, 6, 7, 8, 9]
for i in sample_list:
print("Hello List!#{}".format(i))
print_separator()
sample_list_comp = [x for x in range(0,1)]
for i in sample_list:
print("Hello Comprehension!#{}".format(i))
print_separator()
sample_dictionary = {"english": "hat",
"french": "chapeau"}
for k,v in sample_dictionary.items():
print("The word in {} is {}".format(k,v))
print_separator()
filename = os.path.join(os.getcwd(), "message.txt")
print("The File is at {}".format(filename))
print_message_from_file(filename)
if __name__ == "__main__":
main()
</code></pre>
<p>
<small>
Available on GitHub:
<a href="https://github.com/CMU-MS-DAS-Modern-Programming-Mini/simple-python-example" target="_blank">
https://github.com/CMU-MS-DAS-Modern-Programming-Mini/simple-python-example
</a>
</small>
</p>
</section>
<section>
<h3>Classes and Object Oriented Python</h3>
<pre class="python"><code data-line-numbers="2|3-4|6-7|9-10|12-20">
class Message:
def __init__(self, message_string):
self.message_string = message_string
def print_message(self):
print("This is the class message {}".format(self.message_string))
def __str__(self):
return "This is overriding the str class of the fuction: {}".format(self.message_string)
def __add__(self, msg):
if isinstance(msg, str):
r_msg = Message(self.message_string + msg)
elif isinstance(msg, Message):
r_msg = Message(self.message_string + msg.message_string)
else:
raise TypeError("Class Message does not know how to add" +
"a variable of type {}".format(type(msg)))
return r_msg
</code></pre></small>
</section>
<section>
<h3>Classes and Object Oriented Python</h3>
<pre class="python"><code data-line-numbers="3|50-51|55|59-60|64-66|69-73">
import os
from message import Message
def print_message_from_file(filename):
with open(filename, "r") as f_to_read:
message = f_to_read.read()
print("This is the message")
print("{}".format(message))
def print_separator(num_chars=45):
print("{}".format("".join(["-" for x in range(0, num_chars)])))
def main():
for i in range(0, 10):
print("Hello World!#{}".format(i))
sample_list = [0, 1, 2, 3, 4,
5, 6, 7, 8, 9]
print_separator()
for i in sample_list:
print("Hello List!#{}".format(i))
print_separator()
sample_list_comp = [x for x in range(0, 1)]
for i in sample_list_comp:
print("Hello Comprehension!#{}".format(i))
print_separator()
sample_dictionary = {"english": "hat",
"french": "chapeau"}
for k, val in sample_dictionary.items():
print("The word in {} is {}".format(k, val))
print_separator()
filename = os.path.join(os.getcwd(), "message.txt")
print("The File is at {}".format(filename))
print_message_from_file(filename)
print_separator()
msg = Message("... this is in class.")
msg.print_message()
print_separator()
print("{}".format(msg))
print_separator()
msg_add = msg + " | adding a string"
msg_add.print_message()
print_separator()
msg2 = Message("... adding a message class")
msg_add = msg + msg2
msg_add.print_message()
print_separator()
try:
num = 6.003432
msg_add = msg + num
except Exception as e_msg:
print("There was an exception {}".format(str(e_msg)))
if __name__ == "__main__":
main()
</code></pre>
</section>
</section>
<section>
<section>
<h3> Installing and Using Python </h3>
<p class="r-fit-text">
In this section we will go through some of the Python packages available, environment and package control, and interactive Python frameworks.
</p>
</section>
<section>
<h3> A word about Python versions.... </h3>
<div style="float:left; width: 40%">
<ul class="r-fit-text">
<li>There are two major overarching versions of Python in circulation, Version 2.x and 3.x.
<li>These two versions are generally not compatible, and have multiple syntax and functional differences.
</ul>
</div>
<div style="float:right; width:40%">
<img src="static/images/python-2-vs-3-2018.png" height=100%>
</div>
</section>
<section>
<h3> Common Python Istallations </h3>
<ul class="r-fit-text">
<li>All Linux distributions come with Python initially installed as part of the operating system
<ul>
<li>Generally, this is now Python version 3.x
</ul>
<div class="r-stack">
<div class="fragment fade-out" data-fragment-index="0">
<p class="r-fit-text">
If you would like to install Python3 on Ubuntu
</p>
<pre>
<code class="language-plaintext">
apt-get install python3.7
</code>
</pre>
</div>
<div class="fragment current-visible" data-fragment-index="0">
<p class="r-fit-text">
If you would like to install Python3 on Centos
</p>
<pre>
<code class="language-plaintext">
yum install python3.7
</code>
</pre>
</div>
<div class="fragment">
<p class="r-fit-text">
To find out if and what version of Python is installed:
</p>
<pre>
<code class="language-plaintext">
> which python
/usr/bin/python
> python --version
Python 3.7.6
</code>
</pre>
</div>
</div>
</section>
<section>
<h3>Anaconda Python - A Complete, Comprehensive Python Solution</h3>
<ul class="r-fit-text">
<li> Available on Linux, Windows, and OSX
<li> Easy installable package for download
<ul>
<li><a href="https://www.anaconda.com/products/individual" target="_blank">
https://www.anaconda.com/products/individual
</a>
</ul>
<li> Includes full package management and commonly used, optimized node_modules
</ul>
</section>
</section>
<section>
<section>
<h3>
Running Python
</h3>
<p class="r-fit-text">
There are several ways that you can execute Python programs
</p>
</section>
<section>
<h3>
Running Python Interactively
</h3>
<p class="r-fit-text">
You can just open a terminal and start running Python
</p>
<asciinema-player src="https://cmu-ms-das-modern-programming-mini.github.io/IntroToPython/static/scripts/python-interactive-ascii.json"
preload theme="solarized-dark" fonts-size="small" rows="30"></asciinema-player>
</section>
<section>
<h3>
Running Python Programs
</h3>
<p class="r-fit-text">
You can run a Python program by invoking Python on the script
</p>
<asciinema-player src="https://cmu-ms-das-modern-programming-mini.github.io/IntroToPython/static/scripts/python-command-2.json"
preload theme="solarized-dark" fonts-size="small" rows="30"></asciinema-player>
</section>
<section>
<h3>
Running Jupyter Notebooks
</h3>
<p class="r-fit-text">
Jupyter Notebooks are in credibly powerful tool for running interactive Python in a web browser
</p>
<p class="r-fit-text">
To startup a Jupyter Notebook (assuming Jupyter is installed with your Python distro):
</p>
<ul class="r-fit-text">
<li> Open a terminal
<li> Invoke Jupyter
<pre>
<code>
> jupyter notebook
</code>
</pre>
<li> Your default web browser should automatically open to Jupyter
<ul>
<li> If it does not, open a browser and enter the URL:
http://localhost:8888/tree
</ul>
</ul>
</section>
</section>
<section>
<section>
<h3>Python Modules</h3>
<p class="r-fit-text">
Python comes with a large community of modules that are enhance the functionality
</p>
<p class="r-fit-text">
Here we will introduce a few popular modules and how to use them.
</p>
</section>
<section>
<h3> Including a Module </h3>
<p> To include a module in your code: </p>
<div class="r-stack">
<div class="fragment fade-out" data-fragment-index="0">
<p>
Add the entire module ot your software
</p>
<pre>
<code class="language-plaintext">
import numpy
x = numpy.zeros(100)
</code>
</pre>
</div>
<div class="fragment current-visible" data-fragment-index="0">
<p>
To add the module and give it an alias
</p>
<pre>
<code class="language-plaintext">
import numpy as np
x = np.zeros(100)
</code>
</pre>
</div>
<div class="fragment">
<p>
To only include a function or class from a module
</p>
<pre>
<code class="language-plaintext">
from numpy import zeros
x = zeros(100)
</code>
</pre>
</div>
</div>
</section>
<section>
<h3>
Basic helpful Python Modules
</h3>
<ul class="r-fit-text">
<li>
<a href="https://docs.python.org/3/library/os.html" target="_blank">
os
</a> - Miscellaneous operating system interfaces
</li>
<li>
<a href="https://docs.python.org/3/library/os.path.html" target="_blank">
os.path
</a> - Common pathname manipulations
</li>
<li>
<a href="https://docs.python.org/3/library/sys.html" target="_blank">
sys
</a> - System-specific parameters and functions
</li>
<li>
<a href="https://docs.python.org/3/library/time.html" target="_blank">
time
</a> - Time access and conversions
</li>
<li>
<a href="https://docs.python.org/3/library/datetime.html" target="_blank">
datetime
</a> - Basic date and time types
</li>
<li>
<a href="https://docs.python.org/3/library/pickle.html" target="_blank">
pickle
</a> - Python object serialization
</li>
<li>
<a href="https://docs.python.org/3/library/multiprocessing.html" target="_blank">
multiprocessing
</a> - Process-based parallelism
</li>
</ul>
</section>
<section>
<h3> Numerical Processing Modules</h3>
<p>
(we will go into more detail later in the course)
</p>
<ul class="r-fit-text">
<li>
<a href="https://numpy.org/" target="_blank">
NumPy
</a> - A fundemintal package for scientific computing with Python
</li>
<li>
<a href="https://www.scipy.org/" target="_blank">
SciPy
</a> - An collection of open source software for scientific computing in Python
</li>
<li>
<a href="https://pandas.pydata.org/" target="_blank">
Pandas
</a> - a fast, powerful, flexible and easy to use open source data analysis and manipulation tool.
</li>
</ul>
</section>
<section>
<h3> Visualization Modules </h3>
<p>
Here are some modules that will be useful for visualizing data
</p>
<ul class="r-fit-text">
<li>
<a href="https://matplotlib.org/" target="_blank">
matplotlib
</a> - A data tool for creating static, animated, and interactive visualizations
</li>
<li>
<a href="https://plotly.com/python/" target="_blank">
plotly (Open Source Version)
</a> - A graphing library for making publication-quality graphics
</li>
<li>
<a href="http://https://seaborn.pydata.org/" target="_blank">
seaborn
</a> - Statistical Data visualization
</li>
<li>
<a href="https://bokeh.org/" target="_blank">
bokeh
</a> - A viusaliztion library for bliding powerful interactive data applications
</li>
</ul>
</section>
<section>
<h3> Other Powerful Modules </h3>
<p> Here are a few more modules that are fairly useful for you to know about </p>
<ul class="r-fit-text">
<li>
<a href="https://docs.python-requests.org/en/master/" target="_blank">
Requests
</a> - HTTP requests for Humans
</li>
<li>
<a href="https://docs.python.org/3/library/zipfile.html" target="_blank">
zipfile
</a> - module for working directly with Zip files
</li>
<li>
<a href="https://flask.palletsprojects.com/en/2.0.x/" target="_blank">
Flask
</a> - A web development platform for Python
</li>
<li>
<a href="https://www.sqlalchemy.org/" target="_blank">
SQLAlchemy
</a> - A Python SQL Toolkit and Object Relational Mapper
</li>
</ul>
</section>
</section>
<section>
<section>
<h3> Installing and Managing Modules </h3>
<p> Here we will discuss installing modules and managing python environmnets </p>
</section>
<section>
<h3> <a href="https://pypi.org/project/pip/" target="_blank">Pip</a> - the official tool for installing Python Packages</h3>
<p class="r-fit-text"> Pip provides a command line tool that allows you to automatically install python modules from PyPi
and other custom sources.
<!--<h3><a href="https://pypi.org/">PyPi</a> - The Python Package Index</h3>
<p> The definitive source for canonical Python Modules.</p>-->
<asciinema-player src="https://cmu-ms-das-modern-programming-mini.github.io/IntroToPython/static/scripts/pip-install-3.json"
preload theme="solarized-dark" fonts-size="small" rows="30"></asciinema-player>
</section>
<section>
<h3> <a href="https://virtualenv.pypa.io/en/latest/" target="_blank">virtualenv</a>
- managing environments in Python </h3>
<ul class="r-fit-text">
<li>Every python program comes with a set of modules that it may need to run
<li>One can define various environments with various python versions and modules version
<li>This tool can create a completely local, user-defined python environment
</ul>
</section>
<section>
<asciinema-player src="https://cmu-ms-das-modern-programming-mini.github.io/IntroToPython/static/scripts/virtualenv.json"
preload theme="solarized-dark" fonts-size="small" rows="40"></asciinema-player>
</section>
<section>
<h3> Exporting your environment with Pip </h3>
<p class="r-fit-text"> You can actually export your environment and module requirements to a file
so that when someone else downloads the package, they can immediately install necessary modules </p>
<asciinema-player src="https://cmu-ms-das-modern-programming-mini.github.io/IntroToPython/static/scripts/freeze.json"
preload theme="solarized-dark" fonts-size="small" rows="25"></asciinema-player>
</section>
<section>
<h3>Anaconda Python</h3>
<p class="r-fit-text">
<a href="https://docs.conda.io/projects/conda/en/latest/index.html" target="_blank">conda</a> - an open source environment
management system designed to work with Anaconda.
</p>
<p> <a href="https://docs.conda.io/projects/conda/en/latest/_downloads/843d9e0198f2a193a3484886fa28163c/conda-cheatsheet.pdf"
target="_blank">
The Conda Cheat Sheet </a>
- is a really easy resource to follow to find all of the useful commands in using conda.
</p>
<p> Anaconda Python also comes with a GUI package manager if you would like to use that on your local desktop</p>
</section>
<section>
<asciinema-player src="https://cmu-ms-das-modern-programming-mini.github.io/IntroToPython/static/scripts/conda-create.json"
preload theme="solarized-dark" fonts-size="small" rows="40"></asciinema-player>
</section>
</section>
<section>
<section>
<h3>Documenting your Python Code </h3>
<p>Documenting your code is crucial to others understanding it (and frankly you remembering what you did)</p>
</section>
<section>
<h3> Some useful documentation systems with Python </h3>
<ul class="r-fit-text">
<li><a href="https://www.sphinx-doc.org/en/master/" target="_blank">Sphinx</a> - automatically generates documentation for Python projects
<li><a href="https://docs.readthedocs.io/en/stable/intro/getting-started-with-sphinx.html" target="_blank"> Getting Started with Shinx </a>
A great guide Read The Docs which allows you to host your Sphinx documentation.
<li><a href="https://www.doxygen.nl/index.html" target="_blank">Doxygen</a> - Generates class documentaiton from Python (also C,C++,Java, etc.)
<a href="https://www.doxygen.nl/manual/docblocks.html#pythonblocks" target="_blank">Example Comments</a>
/ <a href="https://freecad.github.io/SourceDoc/index.html" target="_blank">FreeCAD Example</a>
<li><a href="https://www.mkdocs.org/" target="_blank">MkDocs</a> - Markdown based project documentation
</ul>
</section>
<section>
<h3>DocStrings</h3>
<ul class="r-fit-text">
<li>A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition.
<li>Such a docstring becomes the __doc__ special attribute of that object.
<li>DocStrings are technically free format, but many packages can automatically generate documentation from them
</ul>
</section>
<section>
<p>In our previous example:</p>
<pre class="python"><code>
import os
def print_message_from_file(filename):
"""
Prints a message from contents for a
Arguments:
filename - A string giving the name of the files
that is in the current directory
Returns:
Returns no value, prints the message to stdout
"""
with open(filename, "r") as f:
message = f.read()
print("This is the message")
print("{}".format(message))
...
</code></pre>
<small><p>Let's look at the rest of the code
<a href="https://github.com/CMU-MS-DAS-Modern-Programming-Mini/simple-python-example/blob/step-3/message.py"
target="_blank">
here</a></p></small>
</section>
</section>
<section>
<section>
<h3> Python Coding Standards </h3>
<ul class="r-fit-text">
<li><a href="https://www.python.org/dev/peps/pep-0008/#introduction" target="_blank">
The PEP 8 -- Style Guide for Python Code
</a>
<li> Adopted by Guido and the Python core team to provide consistency in programming styles
<li> The style guide provides a number of rules and best practices that have been developed
over time to make readable
<li> Very important in a team development setting to provide consistency
<li> Augment with team agreed upon guidelines
</ul>
</section>
<section>
<h3> <a href="https://pylint.org/" target="_blank">
PyLint</a> - A tool for ensuring code adheres to code standards</h3>
<ul class="r-fit-text">
<li> Command line to provides a report on compiance with Pep8
<li> Can be added to continuous inegration to automatically test all code
<li> Sometimes gives suggestions that can be ignored
</ul>
</section>
<section>
<h3> <a href="https://pylint.org/" target="_blank">
PyLint</a> - A tool for ensuring code adheres to code standards</h3>
<asciinema-player src="https://cmu-ms-das-modern-programming-mini.github.io/IntroToPython/static/scripts/pylint.json"
preload theme="solarized-dark" fonts-size="small" rows="30"></asciinema-player>
</section>
</section>
<section>
<h3>Developing in Python</h3>
<ul>
<li><a href="https://www.eclipse.org/" target="_blank">Eclipse IDE</a>
with <a href="https://www.pydev.org/download.html" target="_blank">PyDev</a>
<li><a href="https://www.jetbrains.com/pycharm/" target="_blank">PyCharm</a>
</ul>
</section>
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="static/scripts/head.min.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script src="plugin/asciinema/asciinema-player.js"></script>
<script>
// More info about initialization & config:
// - https://revealjs.com/initialization/
// - https://revealjs.com/config/
Reveal.initialize({
hash: true,
dependencies: [
{src: 'plugin/title-footer/title-footer.js', async: true, callback: function() { title_footer.initialize(); }},
//{src: 'plugin/reveal-asciinema/asciinema-player.js'},
],
// Learn about plugins: https://revealjs.com/plugins/
plugins: [ RevealMarkdown, RevealHighlight, RevealNotes ]
});
</script>
</body>
</html>