Skip to content

Commit 3f9db2a

Browse files
authored
Merge pull request #16 from gjbex/development
Development
2 parents 9ed2ab7 + 6b81ad8 commit 3f9db2a

10 files changed

Lines changed: 110 additions & 21 deletions

File tree

hands-on/julia.ipynb

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,7 @@
1111
"cell_type": "markdown",
1212
"metadata": {},
1313
"source": [
14-
"Install the `sh` module since this is a bit off the beaten track."
15-
]
16-
},
17-
{
18-
"cell_type": "code",
19-
"execution_count": null,
20-
"metadata": {},
21-
"outputs": [],
22-
"source": [
23-
"!pip install sh"
14+
"Install the `sh` module if it is not installed since this is a bit off the beaten track."
2415
]
2516
},
2617
{
@@ -35,19 +26,23 @@
3526
"outputs": [],
3627
"source": [
3728
"import matplotlib.pyplot as plt\n",
38-
"%matplotlib inline\n",
3929
"import numpy as np\n",
4030
"from pathlib import Path\n",
4131
"import psutil\n",
42-
"import sh\n",
32+
"try:\n",
33+
" import sh\n",
34+
"except ModuleNotFoundError:\n",
35+
" print('installing sh using pip')\n",
36+
" !pip install sh\n",
37+
" import sh\n",
4338
"import time"
4439
]
4540
},
4641
{
4742
"cell_type": "markdown",
4843
"metadata": {},
4944
"source": [
50-
"Download the source code from GitHub."
45+
"Download the source code from GitHub if it is not in the current working directory."
5146
]
5247
},
5348
{
@@ -56,7 +51,9 @@
5651
"metadata": {},
5752
"outputs": [],
5853
"source": [
59-
"!wget https://raw.githubusercontent.com/gjbex/PythonSysProg/master/julia_omp.f90"
54+
"julia_src = Path.cwd() / 'julia_omp.f90'\n",
55+
"if not julia_src.exists():\n",
56+
" sh.wget('https://raw.githubusercontent.com/gjbex/Python-for-systems-programming/refs/heads/master/hands-on/julia_omp.f90')"
6057
]
6158
},
6259
{
@@ -229,7 +226,7 @@
229226
],
230227
"metadata": {
231228
"kernelspec": {
232-
"display_name": "Python 3",
229+
"display_name": "Python 3 (ipykernel)",
233230
"language": "python",
234231
"name": "python3"
235232
},
@@ -243,9 +240,9 @@
243240
"name": "python",
244241
"nbconvert_exporter": "python",
245242
"pygments_lexer": "ipython3",
246-
"version": "3.7.5"
243+
"version": "3.11.6"
247244
}
248245
},
249246
"nbformat": 4,
250-
"nbformat_minor": 2
247+
"nbformat_minor": 4
251248
}

hands-on/shell_interaction.ipynb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@
5151
"sh.ls('-l')"
5252
]
5353
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"!ls -l"
61+
]
62+
},
5463
{
5564
"cell_type": "code",
5665
"execution_count": null,
@@ -611,6 +620,15 @@
611620
"If you need to add or modify environment variables, it is good practice to do that on a copy of `os.environ`."
612621
]
613622
},
623+
{
624+
"cell_type": "code",
625+
"execution_count": null,
626+
"metadata": {},
627+
"outputs": [],
628+
"source": [
629+
"import os"
630+
]
631+
},
614632
{
615633
"cell_type": "code",
616634
"execution_count": null,

source-code/enviroment-variables/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# Environment variables
22

3-
This directory shows some examples of how to use environment variables in a Python
4-
script.
3+
This directory shows some examples of how to use environment variables in a
4+
Python script.
5+
56

67
## What is it?
78

8-
1. `accumulate_data.py`: script that will read all `.txt` files in a directory defined
9-
in an environment variable `DATA_DIR`.
9+
1. `accumulate_data.py`: script that will read all `.txt` files in a directory
10+
defined in an environment variable `DATA_DIR`.
1011
1. `data`: directory that contains two text files with data.
12+
1. `dotenv`: the `dotenv` package is used to load environment variables from a
13+
`.env` file.
14+
1115

1216
## How to use it?
1317

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# include .env file to prevent sensitive data from being committed
2+
.env
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# dotenv
2+
3+
The `dotenv` package is a simple and efficient way to load environment
4+
variables from a `.env` file into your application's environment. It is
5+
commonly used in development and testing environments to manage configuration
6+
settings without hardcoding them into the source code. Another important use
7+
case is to keep sensitive information, such as API keys and database
8+
credentials, out of version control systems.
9+
10+
11+
## What is it?
12+
13+
1. `dot_env`: file to be renamed to `.env` in the directory where the script is
14+
run.
15+
1. `dotenv_demo.py`: demo script that uses `dotenv` to load environment
16+
variables from the `.env` file.
17+
1. `.gitignore`: to ignore the `.env` file in version control.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MY_SECRET="if you see this, expect black helicopters"
2+
MY_PATH="somewhere/to/go"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
3+
from dotenv import load_dotenv
4+
import os
5+
6+
7+
def main():
8+
# Check whether the environment variable is already set
9+
if 'MY_SECRET' in os.environ:
10+
print('MY_SECRET is already set in the environment.')
11+
else:
12+
# Load environment variables from .env file
13+
print('Loading environment variables from .env file...')
14+
load_dotenv()
15+
16+
# Access an environment variable
17+
secret_key = os.getenv('MY_SECRET')
18+
19+
if secret_key:
20+
print(f'MY_SECRET: {secret_key}')
21+
else:
22+
print('MY_SECRET not found in environment variables.')
23+
24+
25+
if __name__ == '__main__':
26+
main()

source-code/hydra/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ or
5151
$ ./gen_rand.py --config-name=file_config.yaml
5252
```
5353

54+
Application logging is configured through Hydra in
55+
`conf/hydra/job_logging/custom.yaml`. The script still uses Python's
56+
`logging.getLogger(__name__)`, but formatting and log level are controlled
57+
from configuration. For example, to change the root log level:
58+
```bash
59+
$ ./gen_rand.py hydra.job_logging.root.level=DEBUG
60+
```
61+
5462
To perform multiple runs with different parameter values:
5563
```bash
5664
$ ./gen_rand.py -m distr=uniform,gauss

source-code/hydra/conf/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ n: 1
22
file: false
33
defaults:
44
- distr: gauss
5+
- override hydra/job_logging: custom
56
- _self_
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: 1
2+
formatters:
3+
simple:
4+
format: '[%(asctime)s][%(name)s][%(levelname)s] %(message)s'
5+
handlers:
6+
console:
7+
class: logging.StreamHandler
8+
formatter: simple
9+
stream: ext://sys.stdout
10+
root:
11+
level: INFO
12+
handlers:
13+
- console
14+
disable_existing_loggers: false

0 commit comments

Comments
 (0)