strela.templates
Templates that turn alerts into strings.
- Currently, alerts are already barebone strings, and these templates rather "embellish" them.
- There are no specific tests for this module bc other tests also implicitly test the templates.
1"""Templates that turn alerts into strings. 2 3- Currently, alerts are already barebone strings, and these templates rather "embellish" 4 them. 5- There are no specific tests for this module bc other tests also implicitly test the 6 templates. 7""" 8 9from typing import Optional 10from strela.symboltype import SymbolType 11from strela.alertstates import AlertState 12 13 14class AlertToTextTemplate: 15 """Template to turn alert into text string. Also serves as the base class for other 16 templates. (*Note: Should add a proper ABC for templates rather than using 17 AlertToTextTemplate (FIXME).*) 18 """ 19 20 def __init__( 21 self, 22 category_name: str, 23 alert_name: str, 24 metric_name: str, 25 link_pattern: str = "", 26 ): 27 """`AlertToTextTemplate` initializer. 28 29 - `category_name`: Alert category, e.g. "Crypto". 30 - `alert_name`: Alert name, e.g. "Fluctulert". 31 - `metric_name`: Metric name, e.g. "Price". 32 - `link_pattern`: Link pattern, will be extrapolated in `apply`, e.g. 33 "https://www.google.com/search?q={symbol.name}+stock" 34 35 All the names are informational only and have no functional purpose. 36 """ 37 self.category_name = category_name 38 self.alert_name = alert_name 39 self.metric_name = metric_name 40 self.link_pattern = link_pattern 41 42 def get_title(self) -> str: 43 """Return the title/subject of the alert.""" 44 return f"📈🚨📉 {self.category_name} {self.metric_name} {self.alert_name}" 45 46 def apply( 47 self, 48 symbol: SymbolType, 49 alert_state: AlertState, 50 old_state: Optional[AlertState], 51 latest_value: float, 52 ) -> str: 53 """Extrapolate the template into a string and return it. 54 55 - `symbol`: Symbol for which the alert is being generated. 56 - `alert_state`: Current state of the alert. 57 - `old_state`: Previous state of the alert. 58 - `latest_value`: Latest value of the metric. 59 """ 60 return f"""\ 61{symbol.name} ⚠lert 62{alert_state.textify(old_state).rstrip()} 63Latest {self.metric_name}: {latest_value} 64{self.link_pattern.format(symbol=symbol)} 65""" 66 67 68class AlertToHtmlTemplate(AlertToTextTemplate): 69 """Template subclass that generates HTML.""" 70 71 def apply( 72 self, 73 symbol: SymbolType, 74 alert_state: AlertState, 75 old_state: AlertState, 76 latest_value: float, 77 ) -> str: 78 return f"""\ 79<a href="{self.link_pattern.format(symbol=symbol)}">{symbol.name} ⚠lert</a> 80{alert_state.htmlify(old_state).rstrip()} 81Latest {self.metric_name}: {latest_value} 82""" 83 84 def wrap_body(self, body: str) -> str: 85 """Wrap the alert in HTML wrapper tags.""" 86 return f"""\ 87<pre><font face="Consolas, Lucida Console, Fira Code, Courier New, Courier, monospace"> 88{body} 89</font></pre> 90"""
class
AlertToTextTemplate:
15class AlertToTextTemplate: 16 """Template to turn alert into text string. Also serves as the base class for other 17 templates. (*Note: Should add a proper ABC for templates rather than using 18 AlertToTextTemplate (FIXME).*) 19 """ 20 21 def __init__( 22 self, 23 category_name: str, 24 alert_name: str, 25 metric_name: str, 26 link_pattern: str = "", 27 ): 28 """`AlertToTextTemplate` initializer. 29 30 - `category_name`: Alert category, e.g. "Crypto". 31 - `alert_name`: Alert name, e.g. "Fluctulert". 32 - `metric_name`: Metric name, e.g. "Price". 33 - `link_pattern`: Link pattern, will be extrapolated in `apply`, e.g. 34 "https://www.google.com/search?q={symbol.name}+stock" 35 36 All the names are informational only and have no functional purpose. 37 """ 38 self.category_name = category_name 39 self.alert_name = alert_name 40 self.metric_name = metric_name 41 self.link_pattern = link_pattern 42 43 def get_title(self) -> str: 44 """Return the title/subject of the alert.""" 45 return f"📈🚨📉 {self.category_name} {self.metric_name} {self.alert_name}" 46 47 def apply( 48 self, 49 symbol: SymbolType, 50 alert_state: AlertState, 51 old_state: Optional[AlertState], 52 latest_value: float, 53 ) -> str: 54 """Extrapolate the template into a string and return it. 55 56 - `symbol`: Symbol for which the alert is being generated. 57 - `alert_state`: Current state of the alert. 58 - `old_state`: Previous state of the alert. 59 - `latest_value`: Latest value of the metric. 60 """ 61 return f"""\ 62{symbol.name} ⚠lert 63{alert_state.textify(old_state).rstrip()} 64Latest {self.metric_name}: {latest_value} 65{self.link_pattern.format(symbol=symbol)} 66"""
Template to turn alert into text string. Also serves as the base class for other templates. (Note: Should add a proper ABC for templates rather than using AlertToTextTemplate (FIXME).)
AlertToTextTemplate( category_name: str, alert_name: str, metric_name: str, link_pattern: str = '')
21 def __init__( 22 self, 23 category_name: str, 24 alert_name: str, 25 metric_name: str, 26 link_pattern: str = "", 27 ): 28 """`AlertToTextTemplate` initializer. 29 30 - `category_name`: Alert category, e.g. "Crypto". 31 - `alert_name`: Alert name, e.g. "Fluctulert". 32 - `metric_name`: Metric name, e.g. "Price". 33 - `link_pattern`: Link pattern, will be extrapolated in `apply`, e.g. 34 "https://www.google.com/search?q={symbol.name}+stock" 35 36 All the names are informational only and have no functional purpose. 37 """ 38 self.category_name = category_name 39 self.alert_name = alert_name 40 self.metric_name = metric_name 41 self.link_pattern = link_pattern
AlertToTextTemplate
initializer.
category_name
: Alert category, e.g. "Crypto".alert_name
: Alert name, e.g. "Fluctulert".metric_name
: Metric name, e.g. "Price".link_pattern
: Link pattern, will be extrapolated inapply
, e.g. "https://www.google.com/search?q={symbol.name}+stock"
All the names are informational only and have no functional purpose.
def
get_title(self) -> str:
43 def get_title(self) -> str: 44 """Return the title/subject of the alert.""" 45 return f"📈🚨📉 {self.category_name} {self.metric_name} {self.alert_name}"
Return the title/subject of the alert.
def
apply( self, symbol: strela.symboltype.SymbolType, alert_state: strela.alertstates.alertstate.AlertState, old_state: Optional[strela.alertstates.alertstate.AlertState], latest_value: float) -> str:
47 def apply( 48 self, 49 symbol: SymbolType, 50 alert_state: AlertState, 51 old_state: Optional[AlertState], 52 latest_value: float, 53 ) -> str: 54 """Extrapolate the template into a string and return it. 55 56 - `symbol`: Symbol for which the alert is being generated. 57 - `alert_state`: Current state of the alert. 58 - `old_state`: Previous state of the alert. 59 - `latest_value`: Latest value of the metric. 60 """ 61 return f"""\ 62{symbol.name} âš lert 63{alert_state.textify(old_state).rstrip()} 64Latest {self.metric_name}: {latest_value} 65{self.link_pattern.format(symbol=symbol)} 66"""
Extrapolate the template into a string and return it.
symbol
: Symbol for which the alert is being generated.alert_state
: Current state of the alert.old_state
: Previous state of the alert.latest_value
: Latest value of the metric.
69class AlertToHtmlTemplate(AlertToTextTemplate): 70 """Template subclass that generates HTML.""" 71 72 def apply( 73 self, 74 symbol: SymbolType, 75 alert_state: AlertState, 76 old_state: AlertState, 77 latest_value: float, 78 ) -> str: 79 return f"""\ 80<a href="{self.link_pattern.format(symbol=symbol)}">{symbol.name} âš lert</a> 81{alert_state.htmlify(old_state).rstrip()} 82Latest {self.metric_name}: {latest_value} 83""" 84 85 def wrap_body(self, body: str) -> str: 86 """Wrap the alert in HTML wrapper tags.""" 87 return f"""\ 88<pre><font face="Consolas, Lucida Console, Fira Code, Courier New, Courier, monospace"> 89{body} 90</font></pre> 91"""
Template subclass that generates HTML.
def
apply( self, symbol: strela.symboltype.SymbolType, alert_state: strela.alertstates.alertstate.AlertState, old_state: strela.alertstates.alertstate.AlertState, latest_value: float) -> str:
72 def apply( 73 self, 74 symbol: SymbolType, 75 alert_state: AlertState, 76 old_state: AlertState, 77 latest_value: float, 78 ) -> str: 79 return f"""\ 80<a href="{self.link_pattern.format(symbol=symbol)}">{symbol.name} âš lert</a> 81{alert_state.htmlify(old_state).rstrip()} 82Latest {self.metric_name}: {latest_value} 83"""
Extrapolate the template into a string and return it.
symbol
: Symbol for which the alert is being generated.alert_state
: Current state of the alert.old_state
: Previous state of the alert.latest_value
: Latest value of the metric.
def
wrap_body(self, body: str) -> str:
85 def wrap_body(self, body: str) -> str: 86 """Wrap the alert in HTML wrapper tags.""" 87 return f"""\ 88<pre><font face="Consolas, Lucida Console, Fira Code, Courier New, Courier, monospace"> 89{body} 90</font></pre> 91"""
Wrap the alert in HTML wrapper tags.