File tree 5 files changed +54
-0
lines changed
5 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,8 @@ to create it. There is some material not covered in the presentation as well.
28
28
configuration files.
29
29
1 . ` data-formats ` : illustrates how to deal with data formats such as CSV
30
30
files, binary data and XML.
31
+ 1 . ` enviroment-variables ` : illustrates how to use environment variables to set paths
32
+ in a script.
31
33
1 . ` hydra ` : Facebook Hydra application framework illustration.
32
34
1 . ` jinja ` : illustration of how to use the jinja2 template library.
33
35
1 . ` logging ` : illustration of Python's logging facilities.
Original file line number Diff line number Diff line change
1
+ # Environment variables
2
+
3
+ This directory shows some examples of how to use environment variables in a Python
4
+ script.
5
+
6
+ ## What is it?
7
+
8
+ 1 . ` accumulate_data.py ` : script that will read all ` .txt ` files in a directory defined
9
+ in an environment variable ` DATA_DIR ` .
10
+ 1 . ` data ` : directory that contains two text files with data.
11
+
12
+ ## How to use it?
13
+
14
+ ``` bash
15
+ $ export DATA_DIR=" $( pwd) /data"
16
+ $ ./accumulate_data.py
17
+ ```
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+
3
+ from glob import glob
4
+ import os
5
+ from pathlib import Path
6
+ import sys
7
+
8
+
9
+ if __name__ == '__main__' :
10
+ if 'DATA_DIR' in os .environ :
11
+ data_dir = Path (os .environ ['DATA_DIR' ])
12
+ print (f"### info: looking for data in '{ data_dir } '" , file = sys .stderr )
13
+ total = 0.0
14
+ for data_file in data_dir .glob ('*.txt' ):
15
+ with open (data_file , 'r' ) as file :
16
+ for line in file :
17
+ line = line .strip ()
18
+ if not line :
19
+ continue
20
+ try :
21
+ total += float (line )
22
+ except ValueError as error :
23
+ print (f'### warning: { error } ' , file = sys .stderr )
24
+ print (f'total = { total } ' )
25
+ else :
26
+ print ('### error: DATA_DIR not set' , file = sys .stderr )
Original file line number Diff line number Diff line change
1
+ 1.2
2
+ 2.3
3
+ 3.4
4
+
Original file line number Diff line number Diff line change
1
+ 4.5
2
+ 5.6
3
+ # dubious values
4
+ 6.7
5
+ 7.8
You can’t perform that action at this time.
0 commit comments