Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
174 lines (141 loc) · 6.36 KB
/
Copy pathutils.py
File metadata and controls
174 lines (141 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import re
from typing import List, Dict, Optional, Tuple
class DataProcessor:
"""Utility class for processing stock data and news content"""
@staticmethod
def extract_price_from_text(text: str, currency_symbols: List[str]) -> Optional[float]:
"""Extract price value from text content with improved accuracy"""
text = text.lower()
# Look for common price patterns supporting both USD and INR
price_patterns = [
r'(?:price|stock|trading|closed?)\s*(?:at|for|:)?\s*[\$₹]?\s*(\d{1,6}\.?\d{0,2})',
r'[\$₹]\s*(\d{1,6}\.?\d{0,2})',
r'(\d{1,6}\.?\d{0,2})\s*[\$₹]',
r'(?:priced?|valued?|worth)\s*[\$₹]?\s*(\d{1,6}\.?\d{0,2})',
r'rs\.?\s*(\d{1,6}\.?\d{0,2})', # Indian rupees format
r'inr\s*(\d{1,6}\.?\d{0,2})', # INR format
]
potential_prices = []
for pattern in price_patterns:
matches = re.findall(pattern, text, re.IGNORECASE)
for match in matches:
try:
price = float(match)
# Adjust price range based on currency (Indian stocks can be higher in INR)
if any(symbol in text for symbol in ['₹', 'rs.', 'inr']):
# Indian stocks: ₹10 to ₹100,000 range
if 10.0 <= price <= 100000:
potential_prices.append(price)
else:
# US stocks: $1 to $10,000 range
if 1.0 <= price <= 10000:
potential_prices.append(price)
except ValueError:
continue
# Return the most reasonable price (typically the highest credible price)
if potential_prices:
# For stocks, usually the actual price is higher than small decimals
potential_prices = sorted(set(potential_prices), reverse=True)
return potential_prices[0]
return None
@staticmethod
def extract_percentage_from_text(text: str) -> Optional[float]:
"""Extract percentage change from text content"""
# Look for patterns like "+5.2%", "-3.1%", "5.2%"
percentage_pattern = r'[+-]?\d+\.?\d*%'
matches = re.findall(percentage_pattern, text)
for match in matches:
try:
# Remove % and convert to float
value = float(match.replace('%', ''))
if -100 <= value <= 1000: # Reasonable range for stock changes
return value
except ValueError:
continue
return None
@staticmethod
def extract_pe_ratio_from_text(text: str) -> Optional[float]:
"""Extract PE ratio from text content"""
text = text.lower()
pe_patterns = [
r'p/e\s*ratio?\s*:?\s*(\d+\.?\d*)',
r'pe\s*ratio?\s*:?\s*(\d+\.?\d*)',
r'price\s*to\s*earnings?\s*:?\s*(\d+\.?\d*)'
]
for pattern in pe_patterns:
matches = re.findall(pattern, text)
for match in matches:
try:
pe_ratio = float(match)
if 0 < pe_ratio < 1000: # Reasonable PE ratio range
return pe_ratio
except ValueError:
continue
return None
@staticmethod
def clean_and_truncate_text(text: str, max_length: int = 500) -> str:
"""Clean and truncate text content"""
if not text:
return ""
# Remove extra whitespace and newlines
cleaned = re.sub(r'\s+', ' ', text.strip())
# Truncate if too long
if len(cleaned) > max_length:
cleaned = cleaned[:max_length] + "..."
return cleaned
@staticmethod
def validate_ticker(ticker: str) -> Tuple[bool, str]:
"""Validate stock ticker format"""
if not ticker:
return False, "Ticker cannot be empty"
# Remove whitespace and convert to uppercase
ticker = ticker.strip().upper()
# Basic validation - alphanumeric with optional dots and hyphens
if not re.match(r'^[A-Z0-9.-]+$', ticker):
return False, "Ticker contains invalid characters"
if len(ticker) < 1 or len(ticker) > 10:
return False, "Ticker length should be between 1-10 characters"
return True, ticker
@staticmethod
def format_currency(amount: float, currency: str = "$") -> str:
"""Format currency amount showing exact prices with comma separation"""
if amount == 0:
return "N/A"
# Show exact amount with comma thousand separators for both currencies
return f"{currency}{amount:,.2f}"
@staticmethod
def create_news_summary(news_items: List[Dict], max_items: int = 5) -> Tuple[str, List[Dict]]:
"""Create formatted news summary with references"""
if not news_items:
return "No recent news found.", []
news_summary = ""
references = []
for idx, item in enumerate(news_items[:max_items], 1):
title = DataProcessor.clean_and_truncate_text(item.get('title', 'No title'), 100)
content = DataProcessor.clean_and_truncate_text(item.get('content', 'No content'), 300)
url = item.get('url', '#')
news_summary += f"{idx}. {title}: {content} [Ref: {url}]\n"
references.append({
'index': idx,
'url': url,
'title': title
})
return news_summary, references
class CacheManager:
"""Simple cache manager for API responses"""
def __init__(self, max_size: int = 100):
self.cache = {}
self.max_size = max_size
def get(self, key: str) -> Optional[any]:
"""Get cached value"""
return self.cache.get(key)
def set(self, key: str, value: any) -> None:
"""Set cached value"""
if len(self.cache) >= self.max_size:
# Remove oldest entry
oldest_key = next(iter(self.cache))
del self.cache[oldest_key]
self.cache[key] = value
def clear(self) -> None:
"""Clear all cached values"""
self.cache.clear()