From f7530963bc4ac0620eee1ff1efe692e8c27e1fe6 Mon Sep 17 00:00:00 2001 From: justinpolygon <123573436+justinpolygon@users.noreply.github.com> Date: Sun, 14 Apr 2024 22:27:38 -0700 Subject: [PATCH 1/2] Fix poetry black lint check --- examples/rest/demo_correlation_matrix.py | 1 + polygon/rest/models/conditions.py | 32 +++--- polygon/rest/models/contracts.py | 8 +- polygon/rest/models/financials.py | 126 ++++++++++++++--------- polygon/rest/models/markets.py | 24 +++-- polygon/rest/models/snapshot.py | 112 ++++++++++++-------- polygon/rest/models/tickers.py | 12 +-- polygon/websocket/__init__.py | 6 +- 8 files changed, 195 insertions(+), 126 deletions(-) diff --git a/examples/rest/demo_correlation_matrix.py b/examples/rest/demo_correlation_matrix.py index f056ab6d..df939590 100644 --- a/examples/rest/demo_correlation_matrix.py +++ b/examples/rest/demo_correlation_matrix.py @@ -40,6 +40,7 @@ essential to do your own research or consult a financial advisor for personalized advice when investing. """ + import pandas as pd # type: ignore import numpy as np # type: ignore import seaborn as sns # type: ignore diff --git a/polygon/rest/models/conditions.py b/polygon/rest/models/conditions.py index 3fc0d776..98baa261 100644 --- a/polygon/rest/models/conditions.py +++ b/polygon/rest/models/conditions.py @@ -47,12 +47,16 @@ class UpdateRules: @staticmethod def from_dict(d): return UpdateRules( - consolidated=None - if "consolidated" not in d - else Consolidated.from_dict(d["consolidated"]), - market_center=None - if "market_center" not in d - else MarketCenter.from_dict(d["market_center"]), + consolidated=( + None + if "consolidated" not in d + else Consolidated.from_dict(d["consolidated"]) + ), + market_center=( + None + if "market_center" not in d + else MarketCenter.from_dict(d["market_center"]) + ), ) @@ -82,11 +86,15 @@ def from_dict(d): id=d.get("id", None), legacy=d.get("legacy", None), name=d.get("name", None), - sip_mapping=None - if "sip_mapping" not in d - else SipMapping.from_dict(d["sip_mapping"]), + sip_mapping=( + None + if "sip_mapping" not in d + else SipMapping.from_dict(d["sip_mapping"]) + ), type=d.get("type", None), - update_rules=None - if "update_rules" not in d - else UpdateRules.from_dict(d["update_rules"]), + update_rules=( + None + if "update_rules" not in d + else UpdateRules.from_dict(d["update_rules"]) + ), ) diff --git a/polygon/rest/models/contracts.py b/polygon/rest/models/contracts.py index dc69f614..469779b6 100644 --- a/polygon/rest/models/contracts.py +++ b/polygon/rest/models/contracts.py @@ -32,9 +32,11 @@ class OptionsContract: @staticmethod def from_dict(d): return OptionsContract( - additional_underlyings=None - if "additional_underlyings" not in d - else [Underlying.from_dict(u) for u in d["additional_underlyings"]], + additional_underlyings=( + None + if "additional_underlyings" not in d + else [Underlying.from_dict(u) for u in d["additional_underlyings"]] + ), cfi=d.get("cfi", None), contract_type=d.get("contract_type", None), correction=d.get("correction", None), diff --git a/polygon/rest/models/financials.py b/polygon/rest/models/financials.py index 85a63e37..1a480c48 100644 --- a/polygon/rest/models/financials.py +++ b/polygon/rest/models/financials.py @@ -74,16 +74,22 @@ class CashFlowStatement: @staticmethod def from_dict(d): return CashFlowStatement( - exchange_gains_losses=None - if "exchange_gains_losses" not in d - else ExchangeGainsLosses.from_dict(d["exchange_gains_losses"]), - net_cash_flow=None - if "net_cash_flow" not in d - else NetCashFlow.from_dict(d["net_cash_flow"]), - net_cash_flow_from_financing_activities=None - if "net_cash_flow_from_financing_activities" not in d - else NetCashFlowFromFinancingActivities.from_dict( - d["net_cash_flow_from_financing_activities"] + exchange_gains_losses=( + None + if "exchange_gains_losses" not in d + else ExchangeGainsLosses.from_dict(d["exchange_gains_losses"]) + ), + net_cash_flow=( + None + if "net_cash_flow" not in d + else NetCashFlow.from_dict(d["net_cash_flow"]) + ), + net_cash_flow_from_financing_activities=( + None + if "net_cash_flow_from_financing_activities" not in d + else NetCashFlowFromFinancingActivities.from_dict( + d["net_cash_flow_from_financing_activities"] + ) ), ) @@ -145,18 +151,24 @@ class ComprehensiveIncome: @staticmethod def from_dict(d): return ComprehensiveIncome( - comprehensive_income_loss=None - if "comprehensive_income_loss" not in d - else ComprehensiveIncomeLoss.from_dict(d["comprehensive_income_loss"]), - comprehensive_income_loss_attributable_to_parent=None - if "comprehensive_income_loss_attributable_to_parent" not in d - else ComprehensiveIncomeLossAttributableToParent.from_dict( - d["comprehensive_income_loss_attributable_to_parent"] + comprehensive_income_loss=( + None + if "comprehensive_income_loss" not in d + else ComprehensiveIncomeLoss.from_dict(d["comprehensive_income_loss"]) + ), + comprehensive_income_loss_attributable_to_parent=( + None + if "comprehensive_income_loss_attributable_to_parent" not in d + else ComprehensiveIncomeLossAttributableToParent.from_dict( + d["comprehensive_income_loss_attributable_to_parent"] + ) ), - other_comprehensive_income_loss=None - if "other_comprehensive_income_loss" not in d - else OtherComprehensiveIncomeLoss.from_dict( - d["other_comprehensive_income_loss"] + other_comprehensive_income_loss=( + None + if "other_comprehensive_income_loss" not in d + else OtherComprehensiveIncomeLoss.from_dict( + d["other_comprehensive_income_loss"] + ) ), ) @@ -248,18 +260,26 @@ class IncomeStatement: @staticmethod def from_dict(d): return IncomeStatement( - basic_earnings_per_share=None - if "basic_earnings_per_share" not in d - else BasicEarningsPerShare.from_dict(d["basic_earnings_per_share"]), - cost_of_revenue=None - if "cost_of_revenue" not in d - else CostOfRevenue.from_dict(d["cost_of_revenue"]), - gross_profit=None - if "gross_profit" not in d - else GrossProfit.from_dict(d["gross_profit"]), - operating_expenses=None - if "operating_expenses" not in d - else OperatingExpenses.from_dict(d["operating_expenses"]), + basic_earnings_per_share=( + None + if "basic_earnings_per_share" not in d + else BasicEarningsPerShare.from_dict(d["basic_earnings_per_share"]) + ), + cost_of_revenue=( + None + if "cost_of_revenue" not in d + else CostOfRevenue.from_dict(d["cost_of_revenue"]) + ), + gross_profit=( + None + if "gross_profit" not in d + else GrossProfit.from_dict(d["gross_profit"]) + ), + operating_expenses=( + None + if "operating_expenses" not in d + else OperatingExpenses.from_dict(d["operating_expenses"]) + ), revenues=None if "revenues" not in d else Revenues.from_dict(d["revenues"]), ) @@ -275,18 +295,28 @@ class Financials: @staticmethod def from_dict(d): return Financials( - balance_sheet=None - if "balance_sheet" not in d - else {k: DataPoint.from_dict(v) for (k, v) in d["balance_sheet"].items()}, - cash_flow_statement=None - if "cash_flow_statement" not in d - else CashFlowStatement.from_dict(d["cash_flow_statement"]), - comprehensive_income=None - if "comprehensive_income" not in d - else ComprehensiveIncome.from_dict(d["comprehensive_income"]), - income_statement=None - if "income_statement" not in d - else IncomeStatement.from_dict(d["income_statement"]), + balance_sheet=( + None + if "balance_sheet" not in d + else { + k: DataPoint.from_dict(v) for (k, v) in d["balance_sheet"].items() + } + ), + cash_flow_statement=( + None + if "cash_flow_statement" not in d + else CashFlowStatement.from_dict(d["cash_flow_statement"]) + ), + comprehensive_income=( + None + if "comprehensive_income" not in d + else ComprehensiveIncome.from_dict(d["comprehensive_income"]) + ), + income_statement=( + None + if "income_statement" not in d + else IncomeStatement.from_dict(d["income_statement"]) + ), ) @@ -311,9 +341,9 @@ def from_dict(d): company_name=d.get("company_name", None), end_date=d.get("end_date", None), filing_date=d.get("filing_date", None), - financials=None - if "financials" not in d - else Financials.from_dict(d["financials"]), + financials=( + None if "financials" not in d else Financials.from_dict(d["financials"]) + ), fiscal_period=d.get("fiscal_period", None), fiscal_year=d.get("fiscal_year", None), source_filing_file_url=d.get("source_filing_file_url", None), diff --git a/polygon/rest/models/markets.py b/polygon/rest/models/markets.py index 509caa86..321d23ab 100644 --- a/polygon/rest/models/markets.py +++ b/polygon/rest/models/markets.py @@ -73,16 +73,22 @@ class MarketStatus: def from_dict(d): return MarketStatus( after_hours=d.get("afterHours", None), - currencies=None - if "currencies" not in d - else MarketCurrencies.from_dict(d["currencies"]), + currencies=( + None + if "currencies" not in d + else MarketCurrencies.from_dict(d["currencies"]) + ), early_hours=d.get("earlyHours", None), - exchanges=None - if "exchanges" not in d - else MarketExchanges.from_dict(d["exchanges"]), - indicesGroups=None - if "indicesGroups" not in d - else MarketIndices.from_dict(d["indicesGroups"]), + exchanges=( + None + if "exchanges" not in d + else MarketExchanges.from_dict(d["exchanges"]) + ), + indicesGroups=( + None + if "indicesGroups" not in d + else MarketIndices.from_dict(d["indicesGroups"]) + ), market=d.get("market", None), server_time=d.get("serverTime", None), ) diff --git a/polygon/rest/models/snapshot.py b/polygon/rest/models/snapshot.py index d97f17c3..ceb5f7f8 100644 --- a/polygon/rest/models/snapshot.py +++ b/polygon/rest/models/snapshot.py @@ -70,9 +70,9 @@ def from_dict(d): type=d.get("type", None), ticker=d.get("ticker", None), market_status=d.get("market_status", None), - session=None - if "session" not in d - else IndicesSession.from_dict(d["session"]), + session=( + None if "session" not in d else IndicesSession.from_dict(d["session"]) + ), error=d.get("error", None), message=d.get("message", None), ) @@ -96,12 +96,12 @@ class TickerSnapshot: def from_dict(d): return TickerSnapshot( day=None if "day" not in d else Agg.from_dict(d["day"]), - last_quote=None - if "lastQuote" not in d - else LastQuote.from_dict(d["lastQuote"]), - last_trade=None - if "lastTrade" not in d - else LastTrade.from_dict(d["lastTrade"]), + last_quote=( + None if "lastQuote" not in d else LastQuote.from_dict(d["lastQuote"]) + ), + last_trade=( + None if "lastTrade" not in d else LastTrade.from_dict(d["lastTrade"]) + ), min=None if "min" not in d else MinuteSnapshot.from_dict(d["min"]), prev_day=None if "prevDay" not in d else Agg.from_dict(d["prevDay"]), ticker=d.get("ticker", None), @@ -223,24 +223,32 @@ class OptionContractSnapshot: def from_dict(d): return OptionContractSnapshot( break_even_price=d.get("break_even_price", None), - day=None - if "day" not in d - else DayOptionContractSnapshot.from_dict(d["day"]), - details=None - if "details" not in d - else OptionDetails.from_dict(d["details"]), + day=( + None + if "day" not in d + else DayOptionContractSnapshot.from_dict(d["day"]) + ), + details=( + None if "details" not in d else OptionDetails.from_dict(d["details"]) + ), greeks=None if "greeks" not in d else Greeks.from_dict(d["greeks"]), implied_volatility=d.get("implied_volatility", None), - last_quote=None - if "last_quote" not in d - else LastQuoteOptionContractSnapshot.from_dict(d["last_quote"]), - last_trade=None - if "last_trade" not in d - else LastTradeOptionContractSnapshot.from_dict(d["last_trade"]), + last_quote=( + None + if "last_quote" not in d + else LastQuoteOptionContractSnapshot.from_dict(d["last_quote"]) + ), + last_trade=( + None + if "last_trade" not in d + else LastTradeOptionContractSnapshot.from_dict(d["last_trade"]) + ), open_interest=d.get("open_interest", None), - underlying_asset=None - if "underlying_asset" not in d - else UnderlyingAsset.from_dict(d["underlying_asset"]), + underlying_asset=( + None + if "underlying_asset" not in d + else UnderlyingAsset.from_dict(d["underlying_asset"]) + ), fair_market_value=d.get("fmv", None), ) @@ -274,12 +282,16 @@ class SnapshotTickerFullBook: def from_dict(d): return SnapshotTickerFullBook( ticker=d.get("ticker", None), - bids=None - if "bids" not in d - else [OrderBookQuote.from_dict(o) for o in d["bids"]], - asks=None - if "asks" not in d - else [OrderBookQuote.from_dict(o) for o in d["asks"]], + bids=( + None + if "bids" not in d + else [OrderBookQuote.from_dict(o) for o in d["bids"]] + ), + asks=( + None + if "asks" not in d + else [OrderBookQuote.from_dict(o) for o in d["asks"]] + ), bid_count=d.get("bidCount", None), ask_count=d.get("askCount", None), spread=d.get("spread", None), @@ -404,22 +416,32 @@ def from_dict(d): return UniversalSnapshot( ticker=d.get("ticker", None), type=d.get("type", None), - session=None - if "session" not in d - else UniversalSnapshotSession.from_dict(d["session"]), - last_quote=None - if "last_quote" not in d - else UniversalSnapshotLastQuote.from_dict(d["last_quote"]), - last_trade=None - if "last_trade" not in d - else UniversalSnapshotLastTrade.from_dict(d["last_trade"]), + session=( + None + if "session" not in d + else UniversalSnapshotSession.from_dict(d["session"]) + ), + last_quote=( + None + if "last_quote" not in d + else UniversalSnapshotLastQuote.from_dict(d["last_quote"]) + ), + last_trade=( + None + if "last_trade" not in d + else UniversalSnapshotLastTrade.from_dict(d["last_trade"]) + ), greeks=None if "greeks" not in d else Greeks.from_dict(d["greeks"]), - underlying_asset=None - if "underlying_asset" not in d - else UniversalSnapshotUnderlyingAsset.from_dict(d["underlying_asset"]), - details=None - if "details" not in d - else UniversalSnapshotDetails.from_dict(d["details"]), + underlying_asset=( + None + if "underlying_asset" not in d + else UniversalSnapshotUnderlyingAsset.from_dict(d["underlying_asset"]) + ), + details=( + None + if "details" not in d + else UniversalSnapshotDetails.from_dict(d["details"]) + ), break_even_price=d.get("break_even_price", None), implied_volatility=d.get("implied_volatility", None), open_interest=d.get("open_interest", None), diff --git a/polygon/rest/models/tickers.py b/polygon/rest/models/tickers.py index 1c2ea947..f7ff2bed 100644 --- a/polygon/rest/models/tickers.py +++ b/polygon/rest/models/tickers.py @@ -108,9 +108,9 @@ class TickerDetails: def from_dict(d): return TickerDetails( active=d.get("active", None), - address=None - if "address" not in d - else CompanyAddress.from_dict(d["address"]), + address=( + None if "address" not in d else CompanyAddress.from_dict(d["address"]) + ), branding=None if "branding" not in d else Branding.from_dict(d["branding"]), cik=d.get("cik", None), composite_figi=d.get("composite_figi", None), @@ -169,9 +169,9 @@ def from_dict(d): image_url=d.get("image_url", None), keywords=d.get("keywords", None), published_utc=d.get("published_utc", None), - publisher=None - if "publisher" not in d - else Publisher.from_dict(d["publisher"]), + publisher=( + None if "publisher" not in d else Publisher.from_dict(d["publisher"]) + ), tickers=d.get("tickers", None), title=d.get("title", None), ) diff --git a/polygon/websocket/__init__.py b/polygon/websocket/__init__.py index b9f45a2e..77865d3f 100644 --- a/polygon/websocket/__init__.py +++ b/polygon/websocket/__init__.py @@ -127,9 +127,9 @@ async def connect( self.schedule_resub = False try: - cmsg: Union[ - List[WebSocketMessage], Union[str, bytes] - ] = await asyncio.wait_for(s.recv(), timeout=1) + cmsg: Union[List[WebSocketMessage], Union[str, bytes]] = ( + await asyncio.wait_for(s.recv(), timeout=1) + ) except asyncio.TimeoutError: continue From ca0c6bb447662972c5727b24bd3117eea2cc5850 Mon Sep 17 00:00:00 2001 From: justinpolygon <123573436+justinpolygon@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:48:22 -0700 Subject: [PATCH 2/2] Revert formatting changes to ws init --- polygon/websocket/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/polygon/websocket/__init__.py b/polygon/websocket/__init__.py index 77865d3f..b9f45a2e 100644 --- a/polygon/websocket/__init__.py +++ b/polygon/websocket/__init__.py @@ -127,9 +127,9 @@ async def connect( self.schedule_resub = False try: - cmsg: Union[List[WebSocketMessage], Union[str, bytes]] = ( - await asyncio.wait_for(s.recv(), timeout=1) - ) + cmsg: Union[ + List[WebSocketMessage], Union[str, bytes] + ] = await asyncio.wait_for(s.recv(), timeout=1) except asyncio.TimeoutError: continue