-
Notifications
You must be signed in to change notification settings - Fork 4
/
note_python_at_dhsc.Rmd
92 lines (67 loc) · 2.89 KB
/
note_python_at_dhsc.Rmd
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
# Python at DHSC {#py_at_dhsc}
The following are the DHSC [sensible defaults](#sensible_defaults) for Python:
## Version & IDE
Use Python 3 via [Jupyter Notebooks](https://jupyter.org/) or [VSCode](https://code.visualstudio.com/)
## Default Packages and Add Ins
## General
* Use pandas for data analysis and reshaping
* Use `loc` and `iloc` to index into data frames
* Use Altair for basic data visualisation
* Use Scikit Learn for machine learning
* Use SQLAlchemy and pandas for database interactions, rather than writing your own SQL
## Packages {#py_default_packages}
Recommended Packages:
_Note: This list is under development - If you have a package you would like to suggest / remove please submit an issue._
* Data Analysis
* [pandas](https://pandas.pydata.org/)
* Database Interface
* [SQLAlchemy](https://www.sqlalchemy.org/)
* Plotting
* [matplotlib](https://matplotlib.org/)
* Machine Learning
* [sklearn](https://scikit-learn.org)
## Project Workflow {#py_projects}
Python has many different options, all supported by different IDEs and tools:
* Anaconda & Conda Projects - [Getting started with Conda](https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html)
* Pycharm Projects - [Pycharm Projects](https://www.jetbrains.com/help/pycharm/project.html)
## Packaging Your Code {#py_packages}
Python - [Packaging Projects](https://packaging.python.org/tutorials/packaging-projects/)
## Managing Dependencies {#py_dependencies}
Virtual Environments [venv](https://docs.python.org/3/library/venv.html)
## Error Handling {#py_errorhandling}
In Python, an error can be a syntax error or an exception. Exceptions will crash your program as they are encountered. Fortunately, Python has a method for dealing with exception errors: the try-except block.
The try-except block is used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program.
The code that follows the except statement is the program’s response to any exceptions in the preceding try clause. For example,
```{python eval=FALSE, python.reticulate = FALSE}
try:
print(0/0)
except:
print ("Cannot divide by O!" )
```
```
Cannot divide by O!
```
It is also possible to write programs that handle selected exceptions (and this is generally considered good practice):
```{python eval=FALSE, python.reticulate = FALSE}
try:
print(0/0)
except ZeroDivisionError:
print ("Cannot divide by O!" )
```
```
Cannot divide by O!
```
And to handle different types of errors:
```{python eval=FALSE, python.reticulate = FALSE}
try:
print(0/j)
except ZeroDivisionError:
print("Cannot divide by O!")
except NameError:
print("Something else went wrong")
```
```
Something else went wrong
```
For more information see python documentation on [error handling](https://docs.python.org/3/tutorial/errors.html).
## Unit Testing {#py_tests}