Skip to content

Instantly share code, notes, and snippets.

@mikeholler
Created August 10, 2020 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeholler/f9fc17f45fd848a01a9de91950c533f8 to your computer and use it in GitHub Desktop.
Save mikeholler/f9fc17f45fd848a01a9de91950c533f8 to your computer and use it in GitHub Desktop.
Pyracing chat
import asyncio
import datetime as dt
import logging
import os
import pyracing
import sys
from pyracing.client import Client
from pyracing.response_objects.chart_data import IRating
from pyracing.constants import ChartType
from pyracing.constants import Category
from dataclasses import dataclass
from typing import List, TypeVar, Generic, cast, Optional
T = TypeVar("T")
Success = TypeVar("Success")
Failure = TypeVar("Failure")
@dataclass(frozen=True)
class Either(Generic[Success, Failure]):
def __post_init__(self):
if self.success is None and self.failure is None:
raise ValueError("Must set success or failure.")
elif self.success is not None and self.failure is not None:
raise ValueError("Cannot set both success AND failure.")
success: Optional[Success] = None
failure: Optional[Failure] = None
@property
def is_success(self) -> bool:
return self.success is not None
@property
def is_failure(self) -> bool:
return self.failure is not None
@dataclass
class ChartData(Generic[T]):
category: str # oval, road, dirt_road, dirt_oval
type: str # irating, ttrating, license_class
content: List[T]
# The last thing in the list, which is chronologically, the most recent
def current(self):
return self.content[-1]
def type_string(self):
return ChartType(self.type).name
def category_string(self):
return Category(self.category).name
def irating() -> ChartData[IRating]:
irs = [
IRating(tuple=(dt.datetime.utcnow(), 1234))
]
return ChartData(
category=Category.road.value,
type="asdf",
content=irs
)
def test() -> Either[str, str]:
import time
if int(time.time()) % 2 == 0:
return Either(success="yay")
else:
return Either(failure="boo")
# t = test()
# try:
# t = test()
# print(t)
# except:
# print("failure!")
#
# print(test())
#
# def main():
# data = irating()
# username = os.getenv("IRACING_USERNAME")
# password = os.getenv("IRACING_PASSWORD")
# logger = logging.getLogger(__name__)
# pr_logger = logging.getLogger(pyracing.__name__)
#
#
# my_customer_id = 404787
#
#
# async def main():
# logging.basicConfig(
# stream=sys.stdout,
# level=logging.INFO,
# datefmt="%Y-%m-%d %H:%M:%S",
# format="%(name)s;%(asctime)s;%(levelname)s;%(message)s"
# )
#
# client = Client(username, password)
#
# pr_logger.level = logging.CRITICAL
# my_ir = await client.irating(
# cust_id=my_customer_id,
# category=Category.road.value,
# )
#
# logger.debug(f"My (DEBUG) IR is: {my_ir.current().value}")
# logger.info(f"My IR is: {my_ir.current().value}")
# logging.getLogger().info("hello I am root")
# logging.getLogger().level = logging.INFO
#
#
#
# if __name__ == "__main__":
# loop = asyncio.new_event_loop()
# asyncio.set_event_loop(loop)
# future = loop.create_task(main())
# loop.run_until_complete(future)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment