Python at DHSC {#py_at_dhsc}

The following are the DHSC sensible defaults for Python:

Version & IDE

Use Python 3 via Jupyter Notebooks or VSCode

Default Packages and Add Ins

General

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.

Project Workflow {#py_projects}

Python has many different options, all supported by different IDEs and tools:

Packaging Your Code {#py_packages}

Python - Packaging Projects

Managing Dependencies {#py_dependencies}

Virtual Environments venv

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.

Unit Testing {#py_tests}



DataS-DHSC/coding_principles_book documentation built on March 11, 2020, 4:13 a.m.