Skip to content

Commit e5014b7

Browse files
committed
Genericize utility examples
1 parent d541c67 commit e5014b7

6 files changed

Lines changed: 15 additions & 14 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ A collection of practical, production-ready Python utilities built while working
1919
| `utils/logger.py` | Structured logging with colour + file rotation |
2020
| `utils/env.py` | Typed `.env` / environment variable loader |
2121
| `utils/string_utils.py` | slugify, truncate, camel_to_snake |
22-
| `utils/datetime_utils.py` | UTC helpers, humanize_delta, market hours |
22+
| `utils/datetime_utils.py` | UTC helpers, humanize_delta, business-hour helpers |
2323
| `utils/number_utils.py` | Currency format, tick rounding, pct_change |
2424
| `utils/list_utils.py` | chunk, flatten, deduplicate, batch_by |
2525
| `utils/dict_utils.py` | deep_merge, safe_get, flatten_dict |
2626
| `utils/cache_utils.py` | TTLCache + @memoize for API caching |
2727
| `utils/async_utils.py` | async retry, timeout, gather_safe |
2828
| `utils/crypto_utils.py` | HMAC-SHA256 signing, nonce generation |
29-
| `utils/validation_utils.py` | Guard clauses, symbol/email validation |
29+
| `utils/validation_utils.py` | Guard clauses, identifier/email validation |
3030

3131
## Install
3232

tests/test_crypto_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55

66
def test_hmac_sha256_known_value():
7-
# Known reference: echo -n "symbol=BTCUSDT" | openssl dgst -sha256 -hmac "secret"
8-
result = hmac_sha256("secret", "symbol=BTCUSDT")
7+
# Known reference: echo -n "client=ACME42" | openssl dgst -sha256 -hmac "secret"
8+
result = hmac_sha256("secret", "client=ACME42")
99
assert isinstance(result, str)
1010
assert len(result) == 64 # hex-encoded SHA-256 is always 64 chars
11+
assert result == "0000b930a1653351394e4a4edea00aeb530e4198fe634ebdd557b7d7d3dccb28"
1112

1213

1314
def test_hmac_sha256_deterministic():

tests/test_validation_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ def test_string_excluded(self):
2828

2929
class TestIsValidSymbol:
3030
def test_valid_no_slash(self):
31-
assert is_valid_symbol("BTCUSDT") is True
31+
assert is_valid_symbol("CLIENT42") is True
3232

3333
def test_valid_with_slash(self):
34-
assert is_valid_symbol("BTC/USDT") is True
34+
assert is_valid_symbol("TEAM/API") is True
3535

3636
def test_lowercase_normalised(self):
37-
assert is_valid_symbol("btcusdt") is True
37+
assert is_valid_symbol("client42") is True
3838

3939
def test_empty_string(self):
4040
assert is_valid_symbol("") is False

utils/crypto_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Hashing and HMAC utilities — useful for exchange API request signing."""
1+
"""Hashing and HMAC utilities for signed API requests."""
22

33
from __future__ import annotations
44
import hashlib
@@ -8,7 +8,7 @@
88

99

1010
def hmac_sha256(secret: str, message: str) -> str:
11-
"""Generate HMAC-SHA256 signature (used by Binance, Bybit, etc.).
11+
"""Generate an HMAC-SHA256 signature.
1212
1313
Args:
1414
secret: Your API secret key.
@@ -18,7 +18,7 @@ def hmac_sha256(secret: str, message: str) -> str:
1818
Hex-encoded HMAC-SHA256 signature.
1919
2020
Example:
21-
>>> sig = hmac_sha256("my_secret", "symbol=BTCUSDT&side=BUY")
21+
>>> sig = hmac_sha256("my_secret", "client=ACME42&action=export")
2222
"""
2323
return hmac.new(
2424
secret.encode("utf-8"),

utils/datetime_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Date and time utilities for trading and automation.
1+
"""Date and time utilities for operations and automation.
22
33
Usage
44
-----

utils/validation_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ def is_positive_number(value: Any) -> bool:
1111

1212

1313
def is_valid_symbol(symbol: str) -> bool:
14-
"""Check if string looks like a valid trading symbol (e.g. BTCUSDT, BTC/USDT).
14+
"""Check if a compact uppercase identifier uses a safe symbol format.
1515
16-
>>> is_valid_symbol("BTCUSDT")
16+
>>> is_valid_symbol("CLIENT42")
1717
True
18-
>>> is_valid_symbol("BTC/USDT")
18+
>>> is_valid_symbol("TEAM/API")
1919
True
2020
>>> is_valid_symbol("")
2121
False

0 commit comments

Comments
 (0)