Anthony Truchet commited on
Commit
2695dfd
·
1 Parent(s): c93b918

refactor: extract cached_download()

Browse files
Files changed (2) hide show
  1. src/app.py +5 -25
  2. src/athai/data_utils.py +30 -0
src/app.py CHANGED
@@ -1,9 +1,10 @@
1
  import numpy as np
2
  import pandas as pd
3
  import streamlit as st
4
- import shelve
5
  import os
6
- import pathlib
 
 
7
 
8
  st.title("Uber pickups in NYC")
9
 
@@ -13,32 +14,11 @@ DATA_URL = (
13
  "streamlit-demo-data/uber-raw-data-sep14.csv.gz"
14
  )
15
 
16
- DATA_PATH = pathlib.Path(os.environ.get("APP_DATA"))
17
-
18
- def cached_download_csv(url : str, **kwargs) -> pd.DataFrame:
19
- shelf_filename = DATA_PATH.joinpath("shelf.dat").as_posix()
20
- try:
21
- s = shelve.open(shelf_filename, writeback=True, flag='c')
22
- except Exception as exn:
23
- st.warning("Unexpected exception, by-passing persistent cache :\n" + exn.with_traceback())
24
- return pd.read_csv(url, **kwargs)
25
- with s as shelf:
26
- st.info(f"Opened or created the shelf file '{shelf_filename}'")
27
- maybe_cached = shelf.get(url)
28
- if maybe_cached is None:
29
- st.info(f"Downloawding URL '{url}'")
30
- df = pd.read_csv(url, **kwargs)
31
- shelf[url] = df
32
- return df
33
- else :
34
- st.info(f"Re-using cached URL '{url}'")
35
- assert isinstance(maybe_cached, pd.DataFrame)
36
- return maybe_cached
37
- # The context manager 'shelf' ensure set value as written back or return
38
 
39
  @st.cache_resource
40
  def load_data(nrows):
41
- data = cached_download_csv(DATA_URL, nrows=nrows)
42
  def lowercase(x):
43
  return str(x).lower()
44
 
 
1
  import numpy as np
2
  import pandas as pd
3
  import streamlit as st
 
4
  import os
5
+ from pathlib import Path
6
+
7
+ from athai.data_utils import cached_download_csv
8
 
9
  st.title("Uber pickups in NYC")
10
 
 
14
  "streamlit-demo-data/uber-raw-data-sep14.csv.gz"
15
  )
16
 
17
+ DATA_PATH = Path(os.environ.get("APP_DATA"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  @st.cache_resource
20
  def load_data(nrows):
21
+ data = cached_download_csv(DATA_PATH, DATA_URL,nrows=nrows)
22
  def lowercase(x):
23
  return str(x).lower()
24
 
src/athai/data_utils.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import pandas as pd
3
+ import logging
4
+
5
+ import shelve
6
+
7
+ logging.basicConfig(level=logging.INFO)
8
+
9
+ def cached_download_csv(data_path : Path, url : str, **kwargs) -> pd.DataFrame:
10
+ shelf_filename = data_path.joinpath("shelf.dat").as_posix()
11
+ try:
12
+ s = shelve.open(shelf_filename, writeback=True, flag='c')
13
+ except Exception as exn:
14
+ logging.warning("Unexpected exception, by-passing persistent cache :\n" + str())
15
+ logging.exception("Unexpected exception, while trying to access the shelf")
16
+ return pd.read_csv(url, **kwargs)
17
+ with s as shelf:
18
+ logging.info(f"Opened or created the shelf file '{shelf_filename}'")
19
+ maybe_cached = shelf.get(url)
20
+ if maybe_cached is None:
21
+ logging.info(f"Downloawding URL '{url}'")
22
+ df = pd.read_csv(url, **kwargs)
23
+ shelf[url] = df
24
+ return df
25
+ else :
26
+ logging.info(f"Re-using cached URL '{url}'")
27
+ assert isinstance(maybe_cached, pd.DataFrame)
28
+ return maybe_cached
29
+ # The context manager 'shelf' ensure set value as written back or return
30
+