-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_en.html
More file actions
484 lines (419 loc) · 20.8 KB
/
python_en.html
File metadata and controls
484 lines (419 loc) · 20.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>Python at CASD</title>
<link rel="stylesheet" href="dist/reset.css"/>
<link rel="stylesheet" href="dist/reveal.css"/>
<link rel="stylesheet" href="dist/theme/dracula.css"/>
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/monokai.css"/>
</head>
<body>
<div class="reveal">
<div class="slides">
<!--Slide 1: Overview-->
<section>
<section>
<img src="assets/CASD.png" alt="casd logo" style="
height: 250px;
margin: 0 auto 4rem auto;
background: transparent;
-webkit-filter: invert(1);
filter: invert(1);
" class="demo-logo"/>
<h3>Python at CASD</h3>
<p>
<small>Datascience team</small>
</p>
</section>
<section data-transition="fade-in slide-out">
<h3>Goals</h3>
<ul>
<li>Why use python?</li>
<li>Key concepts in python</li>
<li>How to use Python at CASD?</li>
<li>Basic python syntax</li>
<li>Python for data science</li>
</ul>
</section>
</section>
<!--Slide 2: Why use python?-->
<section data-transition="fade-in slide-out">
<section data-align="left">
<h3>Why use Python?</h3>
<p>
<em>Python</em> is a <span style="font-weight: bold;">General Purpose Language(GPL)</span> born
February 1991. In the 2010s Python became popular, driven by the growing popularity of <em>data
science</em>.
</p>
<p class="fragment">
<em>R</em> is a <span style="font-weight: bold;">Domain-Specific language (DSL)</span> for data
analysis and visualization.
</p>
</section>
<section>
<h4>Python Key Advantages:</h4>
<ul>
<li class="fragment">Simple and readable syntax</li>
<li class="fragment">Cross-Platform and Versatile</li>
<li class="fragment">Interoperability with other languages</li>
<li class="fragment">Vast and mature ecosystem</li>
<li class="fragment">Supports Multiple Paradigms(e.g. procedural, object-oriented, functional)</li>
</ul>
<aside class="notes">
Python code is close to natural English
Runs on Linux, Windows, macOS without code changes.
Easily integrates with:
- C/C++ via ctypes, cython, pybind11
- Java via Jython
- .NET via pythonnet
500k of libraries via PyPI and Conda:
- Data science: pandas, numpy, matplotlib, scikit-learn
- Web dev: Django, Flask, FastAPI
- Automation: requests, selenium, pyautogui
- AI/ML: tensorflow, torch, transformers
</aside>
</section>
<section id="disadvantages">
<h3>Some disadvantages</h3>
<ul>
<li class="fragment"><em>Interpreted Language</em>: slower than complied languages(e.g. C, Go)</li>
<li class="fragment"><em>Dynamically Typed</em>: Python interpreter infers variable types
automatically <a href="#/var-type" class="button">⏩ Code example</a></li>
<li class="fragment"><em>Not strongly Typed</em>: unlike Java <a href="#/weak-type" class="button">⏩
Code example</a></li>
<li class="fragment"><em>Global Interpreter Lock (GIL)</em>: Threads cannot run in parallel on
multiple cores
</li>
</ul>
</section>
<!-- var type code example-->
<section id="var-type">
<h3>Variables are dynamically typed in python</h3>
<pre><code class="language-python" data-trim data-line-numbers="|1|4|">
x = 5
print(f"type of x is: {type(x)}")
# type of x is int
x = "hello"
print(f"type of x is: {type(x)}")
# type of x is str
</code></pre>
<a href="#/disadvantages" class="button">⏩ Go back</a>
</section>
<!-- weak type code example-->
<section id="weak-type">
<h3>Python is not strongly typed</h3>
<pre><code class="language-python" data-trim data-line-numbers="|2|4|5|">
# python function checks operator types
1 + "2" # ❌ TypeError: unsupported operand types (int + str)
# but some type is allowed
1 + True # ✅ 2 (True → 1)
3 * "a" # ✅ "aaa"
</code></pre>
<a href="#/disadvantages" class="button">⏩ Go back</a>
</section>
</section>
<!--Slide 3: Key concepts in python-->
<section data-transition="fade-in slide-out">
<!--Slide 3-1: Key concepts summery-->
<section>
<h3>Key Components in a Python Project</h3>
<ul>
<small>
<li class="fragment"><em>Python Interpreter</em>: The core runtime that executes Python code
</li>
</small>
<small>
<li class="fragment"><em>Virtual Environment</em>: An isolated environment for avoiding version
conflicts between packages.
</li>
</small>
<small>
<li class="fragment"><em>Package Manager</em>: Tools handle installation, upgrades, and removal
of third-party libraries
</li>
</small>
<small>
<li class="fragment"><em>Integrated Development Environment (IDE)</em>: Environments provide
code editing, linting, debugging.
</li>
</small>
<small>
<li class="fragment"><em>Project Structure & Configuration Files</em>: Files like
`pyproject.toml` list project author, dependencies, Licence, etc.
</li>
</small>
</ul>
</section>
<!--Slide 3-2: python interpreter-->
<section>
<p>A Python Interpreter is :</p>
<ul>
<small>
<li class="fragment"><em>Bytecode Compiler</em>: Translates .py source files into .pyc bytecode
</li>
</small>
<small>
<li class="fragment"><em>Stack-based Virtual Machine</em>: Executes .pyc bytecode instructions
to allow python code to have the same output across OS
</li>
</small>
<small>
<li class="fragment"><em>Built-in Types and Functions</em>: Implementations of Primitive
types(e.g. int, str, etc.) and functions(e.g. print, len, etc.)
</li>
</small>
<small>
<li class="fragment"><em>Runtime</em>: Interfaces with the host OS (memory, file, socket, etc.)
</li>
</small>
<small>
<li class="fragment"><em>Garbage Collector</em>: Cyclic garbage cleaning to free memory</li>
</small>
<small>
<li class="fragment"><em>Standard Library</em>: Module like `os`, `math`, `statistics`</li>
</small>
</ul>
</section>
<!--Slide 3-3: Virtual Environment-->
<section>
<p><em>Python Virtual Environment</em> is an isolated workspace that contains its <em>own Python
interpreter and dependencies</em>.
</p>
<ul>
<small>
<li class="fragment"><em>Isolation</em>: Keep dependencies for each project separate to avoid
conflicts.
</li>
</small>
<small>
<li class="fragment"><em>Reproducibility</em>: Freeze (pip freeze) exact versions for repeatable
builds.
</li>
</small>
<small>
<li class="fragment"><em>Clean Environment</em>: Avoid polluting global/system Python with
project-specific packages.
</li>
</small>
<small>
<li class="fragment"><em>Multiple Python Versions</em>: Different projects can use different
Python versions (e.g., 3.9 vs 3.12).
</li>
</small>
</ul>
</section>
<section>
<img src="assets/python-virtual-envs.webp" alt="illustration venv"/>
</section>
<!--Slide 3-4: python package manager-->
<section>
<p><em>Package management</em> in Python refers to the system that handles:</p>
<ul>
<li class="fragment"> Installing, upgrading, and removing <em>third-party libraries (a.k.a.
packages)</em></li>
<li class="fragment"> Managing dependencies between packages</li>
<li class="fragment"> Ensuring version compatibility</li>
<li class="fragment"> Popular tools: <em>pip, conda, poetry </em></li>
</ul>
</section>
<!--Slide 3-5: IDE for python package manager-->
<section>
<p>IDE for python:</p>
<p>An <em>IDE</em> is a software application that provides a complete set of tools for <em>writing,
debugging, testing, and deploying
code faster</em> in a single interface.</p>
<ul>
<small>
<li class="fragment"><em>PyCharm</em>: Excellent code analysis, debugger, etc. Heavy on memory.
</li>
</small>
<small>
<li class="fragment"><em>Visual Studio Code (VS Code)</em>: Lightweight, needs to install
plugins for python development
</li>
</small>
<small>
<li class="fragment"><em>JupyterLab</em>: Notebook-Centric, best for data analysis, machine
learning.
</li>
</small>
<small>
<li class="fragment"><em>Spyder</em>: MATLAB-like interface, best for scientific computing.</li>
</small>
</ul>
</section>
<!-- Slide 3-6: Python project structure and configuration files-->
<section>
<p>Python project structure and configuration files</p>
<pre data-id="code-animation"><code class="language-Bash" data-trim
data-line-numbers="|3-10|12-15|24|25|26|27|28|"><script
type="text/template">
my_project/
│
├── src/ # Source code (actual application)
│ └── my_project/ # Main package (same name as project)
│ ├── __init__.py
│ ├── core.py
│ ├── utils.py
│ └── submodule/
│ ├── __init__.py
│ └── logic.py
│
├── tests/ # Unit and integration tests
│ ├── __init__.py
│ ├── test_core.py
│ └── test_logic.py
│
├── scripts/ # One-off scripts, CLI tools, migrations, etc.
│ └── generate_report.py
│
├── docs/ # Documentation (Sphinx, Markdown, etc.)
│ └── index.md
│
├── .gitignore
├── MANIFEST.in # To include extra resource into the packages
├── requirements.txt # Frozen dependencies for production
├── requirements-dev.txt # Dev/test dependencies (e.g., pytest, flake8)
├── README.md
├── pyproject.toml # Modern build/configuration file
</script></code></pre>
</section>
</section>
<!--Slide 4: python in CASD-->
<section data-transition="fade-in slide-out">
<!-- Slide 4-1: overview -->
<section>
<p>Python in CASD</p>
<ul>
<li class="fragment"><em>Python Interpreter</em>: conda</li>
<li class="fragment"><em>Virtual Environment</em>: conda</li>
<li class="fragment"><em>Package Manager</em>: pip</li>
<li class="fragment"><em>Integrated Development Environment (IDE)</em>: vs-code</li>
<li class="fragment"><em>Project Structure & Configuration Files</em>: CASD best practices</li>
</ul>
</section>
<section>
<h3>CASD VS Outside world:</h3>
<img src="assets/serveurs.png" height="400">
</section>
<section>
<p>SOP for creating python project in CASD</p>
<pre data-id="code-animation"><code class="language-powershell" data-trim
data-line-numbers="|1-4|6-11|13-14|16-22|24-26|28-29|31-32|">
<script type="text/template">
# 1. check if conda exists in the current shell
conda --version
# enable conda cmd in powershell (optional)
conda init powershell
# 2. create a virtual environment
conda create --name project-name python=python-version --offline
# view existing virtual environment list
conda env list
# check status of a virtual environment
conda info --envs
# 3. activate a virtual environment
conda activate project-name
# 4. manage packages of your project
# check installed package list
pip list
# install a new package, make sure the casd pypi server is on
pip install pandas
# install package via requirements.txt
pip install -r requirements.txt
# 5. Run your code
cd /path/to/project-name
python main.py
# 6. Close a virtual environment
conda deactivate
# 7. Remove a virtual environment
conda remove -n env_name --all
</script>
</code></pre>
</section>
<section>
<h3>Python packages :</h3>
<p>Standard packages are native in python Interpreter!</p>
<pre><code> pip install sys</code></pre>
<p class="fragment">This command will <em>fail</em>, because <em>sys</em> is a standard package,it's
already in the python environment. We can use the standard package directly.</p>
<pre><code> import sys</code></pre>
</section>
<section>
<h3>3rd party packages :</h3>
<small><p>Users need to activate the <em>CASD PyPi server</em>, before running the below command </p></small>
<pre><code>pip install pandas</code></pre>
<small><p><em>CASD PyPi server</em> does not have all the packages in the official PyPi repo. You can check
package availabilities
with the below commands :</p></small>
<pre><code class="language-Bash">pip install package-name --dry-run</code></pre>
or
<pre><code class="language-Bash">pip index versions package-name</code></pre>
<p>Contact <em>service@casd.eu</em>, if the required package is not available</p>
</section>
</section>
<!--Slide 5: vscode in CASD-->
<section>
<h2>VScode in CASD</h2>
<p>By default, <em>vscode</em> does not have the required plugins for python development.</p>
<p>CASD provides a list of <em>vscode</em> plugins for python development:</p>
<ul>
<li class="fragment"><em>Plugin location</em>: S:\VsCode\</li>
<li class="fragment"><em>Plugin for python</em>: S:\VsCode\Python\*</li>
<li class="fragment"><em>Plugin for markdown</em>: S:\VsCode\Markdown\*</li>
<li class="fragment"><em>Plugin for Git</em>: S:\VsCode\Git\*</li>
</ul>
</section>
<!--Slide 6: Basic python syntax -->
<section>
<section>
<h3>Basic python syntax</h3>
<ul>
<li class="fragment">Indentation (Important!) </li>
<li class="fragment">Variables and Data Types </li>
<li class="fragment">Conditions </li>
<li class="fragment">Loops </li>
<li class="fragment">Functions </li>
<li class="fragment">Common data structure </li>
<li class="fragment">Exception handling </li>
<li class="fragment">Imports </li>
</ul>
<p class="fragment">Go to the TP2: <a href="https://github.com/CASD-EU/Seminar_Python_Git/blob/main/notebooks/02.basic_python_syntax.ipynb">basic_python_syntax</a></p>
</section>
</section>
<!--Slide 7: Python for data science -->
<section>
<section>
<h4>Python for data science</h4>
<p>Python provides thousands of packages for data science. Below are some most popular packages</p>
<ul>
<small> <li class="fragment"><em>Data Wrangling</em>: <span style="color:#b2dba1;">pyspark, pandas</span>, pyarrow, dask, polars </li></small>
<small> <li class="fragment"><em>Geospatial Data Science </em>: <span style="color:#b2dba1;">sedona, geopandas, shapely, pyproj, rasterio</span>, kepler.gl</li></small>
<small> <li class="fragment"><em>Statistics and Math</em>: <span style="color:#b2dba1;">ydata-profiling, statsmodels, sympy, numpy</span>, scikit-misc</li></small>
<small> <li class="fragment"><em>Machine Learning</em>: <span style="color:#b2dba1;">scikit-learn, xgboost</span>, lightgbm, catboost </li></small>
<small> <li class="fragment"><em>Deep Learning</em>: <span style="color:#b2dba1;">tensorflow, torch, keras</span>, fastai</li></small>
<small> <li class="fragment"><em>Time Series & Forecasting</em>: <span style="color:#b2dba1;">prophet, tsfresh, darts</span>, sktime </li></small>
<small> <li class="fragment"><em>Natural Language Processing (NLP) </em>: <span style="color:#b2dba1;">nltk, spacy, transformers</span>, gensim, textblob, </li></small>
</ul>
</section>
</section>
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script>
// More info about initialization & config:
// - https://revealjs.com/initialization/
// - https://revealjs.com/config/
Reveal.initialize({
hash: true,
// Learn about plugins: https://revealjs.com/plugins/
plugins: [RevealMarkdown, RevealHighlight, RevealNotes],
});
</script>
</body>
</html>