financegy 0.1.0__py3-none-any.whl → 1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
financegy/__init__.py CHANGED
@@ -14,6 +14,8 @@ from financegy.modules.securities import(
14
14
  get_historical_trades
15
15
  )
16
16
 
17
+ from financegy.cache.cache_manager import clear_cache
18
+
17
19
  __all__ = [
18
20
  "get_securities",
19
21
  "get_security_by_symbol",
@@ -23,5 +25,6 @@ __all__ = [
23
25
  "get_security_session_trade",
24
26
  "search_securities",
25
27
  "get_trades_for_year",
26
- "get_historical_trades"
28
+ "get_historical_trades",
29
+ "clear_cache"
27
30
  ]
@@ -0,0 +1,62 @@
1
+ import os, json, hashlib
2
+ from datetime import datetime, timedelta
3
+ import shutil
4
+
5
+ SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
6
+ CACHE_DIR = os.path.join(SCRIPT_DIR, "cache")
7
+
8
+ def make_cache_key(func_name, *args, **kwargs):
9
+ """Create a unique hash for the given function call."""
10
+
11
+ key_data = {
12
+ "func": func_name,
13
+ "args": args,
14
+ "kwargs": kwargs
15
+ }
16
+
17
+ key_string = json.dumps(key_data, sort_keys=True, default=str)
18
+ hashed = hashlib.md5(key_string.encode()).hexdigest()
19
+
20
+ return f"{func_name}_{hashed}.json"
21
+
22
+ def load_cache(func_name, *args, max_age_days=7, **kwargs):
23
+ os.makedirs(CACHE_DIR, exist_ok=True)
24
+ cache_file = make_cache_key(func_name, *args, **kwargs)
25
+ cache_path = os.path.join(CACHE_DIR, cache_file)
26
+
27
+ if not os.path.exists(cache_path):
28
+ return None
29
+
30
+ with open(cache_path, "r") as f:
31
+ data = json.load(f)
32
+
33
+ timestamp = datetime.fromisoformat(data["timestamp"])
34
+ if datetime.now() - timestamp > timedelta(days=max_age_days):
35
+ return None
36
+
37
+ return data["value"]
38
+
39
+ def save_cache(func_name, value, *args, **kwargs):
40
+ os.makedirs(CACHE_DIR, exist_ok=True)
41
+ cache_file = make_cache_key(func_name, *args, **kwargs)
42
+ cache_path = os.path.join(CACHE_DIR, cache_file)
43
+
44
+ with open(cache_path, "w") as f:
45
+ json.dump({
46
+ "timestamp": datetime.now().isoformat(),
47
+ "value": value
48
+ }, f)
49
+
50
+ def clear_cache():
51
+ """Completely clears the FinanceGY cache directory."""
52
+ if not os.path.exists(CACHE_DIR):
53
+ print("No cache directory found.")
54
+ return False
55
+
56
+ try:
57
+ shutil.rmtree(CACHE_DIR)
58
+ print("Cache cleared successfully.")
59
+ return True
60
+
61
+ except Exception as e:
62
+ print(f"Failed to clear cache: {e}")
@@ -1,13 +1,26 @@
1
1
  from financegy.core import request_handler, parser
2
+ from financegy.cache import cache_manager
2
3
 
3
- def get_securities():
4
+ def get_securities(use_cache=True):
4
5
  """Get names of all currently traded securities"""
6
+
7
+ func_name = "get_securities"
8
+
9
+ if use_cache:
10
+ cached = cache_manager.load_cache(func_name)
11
+ if cached:
12
+ return parser.parse_get_securities(cached)
13
+
5
14
  path = "/securities/"
6
15
  html = request_handler.fetch_page(path)
16
+
17
+ cache_manager.save_cache(func_name, html)
18
+
7
19
  return parser.parse_get_securities(html);
8
20
 
9
- def get_security_by_symbol(symbol: str):
21
+ def get_security_by_symbol(symbol: str, use_cache=True):
10
22
  """Get the security details by its ticker symbol"""
23
+
11
24
  securities = get_securities()
12
25
 
13
26
  symbol = symbol.strip().upper()
@@ -17,70 +30,127 @@ def get_security_by_symbol(symbol: str):
17
30
  None,
18
31
  )
19
32
 
20
- def get_security_recent_year(symbol:str):
33
+ def get_security_recent_year(symbol:str, use_cache=True):
21
34
  """Get the most recent year's trade data for any of the traded securities"""
22
35
 
36
+ func_name = "get_security_recent_year"
37
+
23
38
  security_name = get_security_by_symbol(symbol)
24
39
  security_name = security_name.lower().replace(" ", "-")
25
40
 
41
+ if use_cache:
42
+ cached = cache_manager.load_cache(func_name, symbol)
43
+ if cached:
44
+ return parser.parse_get_security_recent_year(cached)
45
+
26
46
  path = "/security/" + security_name
27
47
  html = request_handler.fetch_page(path)
48
+
49
+ cache_manager.save_cache(func_name, html, symbol)
50
+
28
51
  return parser.parse_get_security_recent_year(html)
29
52
 
30
- def get_recent_trade(symbol: str):
53
+ def get_recent_trade(symbol: str, use_cache=True):
31
54
  """Get the most recent trade data for any of the traded securities"""
55
+
56
+ func_name = "get_recent_trade"
32
57
 
33
58
  security_name = get_security_by_symbol(symbol)
34
59
  security_name = security_name.lower().replace(" ", "-")
35
60
 
61
+ if use_cache:
62
+ cached = cache_manager.load_cache(func_name, symbol)
63
+ if cached:
64
+ return parser.parse_get_recent_trade(cached)
65
+
36
66
  path = "/security/" + security_name
37
67
  html = request_handler.fetch_page(path)
68
+
69
+ cache_manager.save_cache(func_name, html, symbol)
70
+
38
71
  return parser.parse_get_recent_trade(html)
39
72
 
40
- def get_session_trades(session: str):
73
+ def get_session_trades(session: str, use_cache=True):
41
74
  """Get the session trade data for all the available securities"""
42
75
 
76
+ func_name = "get_session_trades"
77
+
78
+ if use_cache:
79
+ cached = cache_manager.load_cache(func_name, session)
80
+ if cached:
81
+ return parser.parse_get_session_trades(cached)
82
+
43
83
  path = f"/financial_session/{session}/"
44
84
  html = request_handler.fetch_page(path)
85
+
86
+ cache_manager.save_cache(func_name, html, session)
87
+
45
88
  return parser.parse_get_session_trades(html)
46
89
 
47
- def get_security_session_trade(symbol: str, session: str):
90
+ def get_security_session_trade(symbol: str, session: str, use_cache=True):
48
91
  """Get the session trade data for a given security"""
49
92
 
93
+ func_name = "get_security_session_trade"
94
+
50
95
  symbol = symbol.strip().upper()
51
96
 
97
+ if use_cache:
98
+ cached = cache_manager.load_cache(func_name, symbol, session)
99
+ if cached:
100
+ return parser.parse_get_security_session_trade(symbol, cached)
101
+
52
102
  path = f"/financial_session/{session}/"
53
103
  html = request_handler.fetch_page(path)
54
- return parser.parse_get_security_session_trade(symbol, html)
55
104
 
105
+ cache_manager.save_cache(func_name, html, symbol, session)
106
+
107
+ return parser.parse_get_security_session_trade(symbol, html)
56
108
 
57
- def get_trades_for_year(symbol: str, year: str):
109
+ def get_trades_for_year(symbol: str, year: str, use_cache=True):
58
110
  """Get security trade information from a specific year"""
59
111
 
112
+ func_name = "get_trades_for_year"
113
+
60
114
  symbol = symbol.strip().upper()
61
115
 
116
+ if use_cache:
117
+ cached = cache_manager.load_cache(func_name, symbol, year)
118
+ if cached:
119
+ return parser.parse_get_trades_for_year(year, cached)
120
+
62
121
  security_name = get_security_by_symbol(symbol)
63
122
  security_name = security_name.lower().replace(" ", "-")
64
123
 
65
124
  path = f"/security/{security_name}/"
66
125
  html = request_handler.fetch_page(path)
67
126
 
127
+ cache_manager.save_cache(func_name, html, symbol, year)
128
+
68
129
  return parser.parse_get_trades_for_year(year, html)
69
130
 
70
- def get_historical_trades(symbol: str, start_date: str, end_date: str):
131
+ def get_historical_trades(symbol: str, start_date: str, end_date: str, use_cache=True):
71
132
  """Get historical trade data for a date range"""
72
133
 
134
+ func_name = "get_historical_trades"
135
+
73
136
  symbol = symbol.strip().upper()
74
137
 
138
+ if use_cache:
139
+ cached = cache_manager.load_cache(func_name, symbol, start_date, end_date)
140
+ if cached:
141
+ return parser.parse_get_historical_trades(start_date, end_date, cached)
142
+
75
143
  security_name = get_security_by_symbol(symbol)
76
144
  security_name = security_name.lower().replace(" ", "-")
77
145
 
78
146
  path = f"/security/{security_name}/"
79
147
  html = request_handler.fetch_page(path)
80
148
 
149
+ cache_manager.save_cache(func_name, html, symbol, start_date, end_date)
150
+
81
151
  return parser.parse_get_historical_trades(start_date, end_date, html)
82
152
 
83
- def search_securities(query: str):
153
+ def search_securities(query: str, use_cache=True):
84
154
  """Search securities by symbol or name (partial match)"""
85
155
 
86
156
  query = query.lower().strip()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: financegy
3
- Version: 0.1.0
3
+ Version: 1.3
4
4
  Summary: Unofficial Python library for accessing GSE (Guyana Stock Exchange) financial data
5
5
  Author-email: Ezra Minty <ezranminty@gmail.com>
6
6
  License: MIT
@@ -105,6 +105,38 @@ Fetches historical trade data for a security within the specified date range (`d
105
105
 
106
106
  ---
107
107
 
108
+ ## Caching System
109
+
110
+ FinanceGY includes a lightweight local caching system designed to speed up repeated requests and reduce unnecessary calls.
111
+
112
+ Whenever you call a data retrieval function (such as `get_securities()` or `get_recent_trade()`), FinanceGY automatically checks whether a cached response already exists for that specific query:
113
+
114
+ - If a valid cache file (less than 7 days old since sessions are help once per week) is found, the result is returned instantly from the cache.
115
+ - If the cache is missing, disabled, or older than one week, FinanceGY fetches fresh data from the GSE and updates the cache automatically.
116
+
117
+ All cache files are stored in a local `cache/` directory as small JSON files containing the retrieved data and a timestamp.
118
+
119
+ You can manually clear all cached data at any time:
120
+
121
+ ```python
122
+ import financegy
123
+
124
+ financegy.clear_cache()
125
+ ```
126
+
127
+ This will delete all cached files and force the next data request to fetch fresh data directly from the source.
128
+
129
+ If you prefer to bypass the cache for a specific call, simply pass `use_cache=False` to any function. For example:
130
+
131
+ ```python
132
+ # Force a fresh fetch from the GSE, ignoring cached data
133
+ recent_trade = financegy.get_recent_trade("DDL", use_cache=False)
134
+ ```
135
+
136
+ By default, caching is enabled for all supported functions unless explicitly turned off.
137
+
138
+ ---
139
+
108
140
  ## License
109
141
 
110
142
  This project is licensed under the **MIT License**
@@ -0,0 +1,11 @@
1
+ financegy/__init__.py,sha256=c0b1JX1ZkQVxF984dIW-C6E24blvb1cSmmjY4Msh9kY,790
2
+ financegy/config.py,sha256=SD5LpUVZvthUJEN2KxInRzoOLZCgSMXHV-tc1kKSPt8,193
3
+ financegy/cache/cache_manager.py,sha256=l2J7Fj3g0EnkJLGeIMuaslwFI9-6Ipmf4PHJLUMphAU,1859
4
+ financegy/core/parser.py,sha256=w1JBLphaKSbKimQsGkfDMyj69zmIGtGClnqHxWHx-Lc,10259
5
+ financegy/core/request_handler.py,sha256=g0C0R-nvIvicAhxnCEAtTU8buVtzUn8725EERvBA6ZU,276
6
+ financegy/modules/securities.py,sha256=PNEqfot_cqneO0j4muRfYC5sbCUaZaJoKiq1exBXSz4,5233
7
+ financegy-1.3.dist-info/licenses/LICENSE,sha256=HGLhx0fI215whUzIvTFdFivB447d_IdIIIRncUCQaEs,1088
8
+ financegy-1.3.dist-info/METADATA,sha256=1wrYM-4anai8UX3HVbT5wXWb1gLlKvRQ9y2qUFZKYyE,4722
9
+ financegy-1.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
10
+ financegy-1.3.dist-info/top_level.txt,sha256=TpdYDxtK61m5xnvvzbqnDVJ82gphEqxnXN_Ur8rjvxQ,10
11
+ financegy-1.3.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- financegy/__init__.py,sha256=tXwqh9F9KYRVJ8Sv1kTag29GnGNv-NsOYR_P59V6n5w,713
2
- financegy/config.py,sha256=SD5LpUVZvthUJEN2KxInRzoOLZCgSMXHV-tc1kKSPt8,193
3
- financegy/core/parser.py,sha256=w1JBLphaKSbKimQsGkfDMyj69zmIGtGClnqHxWHx-Lc,10259
4
- financegy/core/request_handler.py,sha256=g0C0R-nvIvicAhxnCEAtTU8buVtzUn8725EERvBA6ZU,276
5
- financegy/modules/securities.py,sha256=yAEX6MchhML9-hwPpEb71j0OS8FP9P2D94xaUHkKTys,3096
6
- financegy-0.1.0.dist-info/licenses/LICENSE,sha256=HGLhx0fI215whUzIvTFdFivB447d_IdIIIRncUCQaEs,1088
7
- financegy-0.1.0.dist-info/METADATA,sha256=vDqzfmxMhZLJ9JJcNy2Eoqzt8XH5fDgkNrXc6bI6dcU,3367
8
- financegy-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
- financegy-0.1.0.dist-info/top_level.txt,sha256=TpdYDxtK61m5xnvvzbqnDVJ82gphEqxnXN_Ur8rjvxQ,10
10
- financegy-0.1.0.dist-info/RECORD,,