Skip to content

Commit

Permalink
Upgrade pydantic to 2.0
Browse files Browse the repository at this point in the history
Signed-off-by: Toomore Chiang <[email protected]>
  • Loading branch information
toomore committed Jun 29, 2024
1 parent fe153d6 commit 7a7dbd3
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 114 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
os: ["ubuntu-20.04", "ubuntu-22.04"]
python-version: ["3.10", "3.11"]
python-version: ["3.10", "3.11", "3.12"]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
Expand All @@ -27,7 +27,7 @@ jobs:
- name: Install and configure Poetry
uses: snok/install-poetry@v1
with:
version: 1.8.2
version: 1.8.3
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
Expand All @@ -40,7 +40,7 @@ jobs:
strategy:
matrix:
os: ["ubuntu-20.04", "ubuntu-22.04"]
python-version: ["3.10", "3.11"]
python-version: ["3.10", "3.11", "3.12"]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
Expand All @@ -54,7 +54,7 @@ jobs:
- name: Install and configure Poetry
uses: snok/install-poetry@v1
with:
version: 1.8.2
version: 1.8.3
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
Expand All @@ -68,7 +68,7 @@ jobs:
strategy:
matrix:
os: ["ubuntu-20.04", "ubuntu-22.04"]
python-version: ["3.10", "3.11"]
python-version: ["3.10", "3.11", "3.12"]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
Expand All @@ -82,7 +82,7 @@ jobs:
- name: Install and configure Poetry
uses: snok/install-poetry@v1
with:
version: 1.8.2
version: 1.8.3
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
Expand Down Expand Up @@ -111,7 +111,7 @@ jobs:
- name: Install and configure Poetry
uses: snok/install-poetry@v1
with:
version: 1.8.2
version: 1.8.3
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
Expand Down
302 changes: 222 additions & 80 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ classifiers = [
"Natural Language :: English",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python",
"Topic :: Communications :: Chat",
"Topic :: Internet :: WWW/HTTP",
Expand All @@ -27,14 +28,15 @@ classifiers = [
python = "^3.10"
requests = "^2.28.2"
certifi = "*"
pydantic = "^1.10.6"
pydantic = "^2"
arrow = "^1.2.3"

[tool.poetry.group.dev.dependencies]
mypy = "^1.1.1"
pylint = "^2.17.0"
autopep8 = "^2.0.2"
types-requests = "^2.28.11.15"
pytest = "^8.2.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
log_cli = true
64 changes: 64 additions & 0 deletions tests/test_pretalx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
''' Test Pretalx '''
import logging

import pytest

from toldwords.pretalx import Pretalx

logging.basicConfig(
filename='test.log',
filemode='w',
level=logging.ERROR,
format='%(asctime)s - %(levelname)s: %(message)s',
)


@pytest.fixture
def auth_info():
''' Get login info '''
return Pretalx(domain='pretalx.coscup.org', event='coscup-2024',
token='')


class TestPretalx:
''' Test Pretalx '''

@staticmethod
def test_fetch_all(auth_info):
''' Test fetch all '''
pretalx = auth_info

datas = list(pretalx.submissions())

logging.info(datas)
logging.info(len(datas))

@staticmethod
def test_rooms(auth_info):
''' Test rooms '''
pretalx = auth_info

datas = list(pretalx.rooms())

logging.info(datas)
logging.info(len(datas))

@staticmethod
def test_speakers(auth_info):
''' Test speakers '''
pretalx = auth_info

datas = list(pretalx.speakers())

logging.info(datas)
logging.info(len(datas))

@staticmethod
def test_talks(auth_info):
''' Test talks'''
pretalx = auth_info

datas = list(pretalx.talks())

logging.info(datas)
logging.info(len(datas))
11 changes: 4 additions & 7 deletions toldwords/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum
from typing import List

from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict
from requests import Session


Expand All @@ -15,13 +15,10 @@ class Role(str, Enum):

class Message(BaseModel):
''' Message in chat format '''
model_config = ConfigDict(use_enum_values=True)
role: Role
content: str

class Config: # pylint: disable=too-few-public-methods
''' Config '''
use_enum_values = True


class TokenUsage(BaseModel):
''' Token usage '''
Expand Down Expand Up @@ -67,11 +64,11 @@ def chat_completions(self, # pylint: disable=too-many-arguments
''' chat completions '''
data = {
'model': model,
'messages': [msg.dict() for msg in messages],
'messages': [msg.model_dump() for msg in messages],
'temperature': temperature,
'n': n,
'user': user,
}
return RespCompletions.parse_obj(
return RespCompletions.model_validate(
self.post(self.url + '/chat/completions', json=data).json()
)
42 changes: 23 additions & 19 deletions toldwords/pretalx.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
''' Fetch data from pretalx '''
import logging
from datetime import datetime
from typing import Any, Generator, List

import arrow
from pydantic import BaseModel, Field, parse_obj_as, validator
from pydantic import BaseModel, Field, TypeAdapter, field_validator
from requests import Session


Expand Down Expand Up @@ -31,9 +32,9 @@ class Slot(BaseModel):
room: dict[str, str] = Field(default_factory=dict, description='Room name')
room_id: int = Field(default=0, description='Room ID')

_validate_convert_datetime = validator(
_validate_convert_datetime = field_validator(
'start', 'end',
pre=True, allow_reuse=True, always=True)(convert_datetime)
mode="before", check_fields=True)(convert_datetime)


class Talk(BaseModel):
Expand Down Expand Up @@ -62,7 +63,7 @@ class Talk(BaseModel):
speakers: list[Speaker] = Field(
default_factory=list, description='A list of speaker objects')

@validator('track', pre=True)
@field_validator('track', mode="before")
def verify_track(cls, value: Any) -> dict[str, str]:
''' verify track '''
if value is None:
Expand All @@ -82,8 +83,8 @@ class Submission(BaseModel):
description='The submission’s title')
track: dict[str, str] = Field(
description='The track this talk belongs to')
track_id: int | None = Field(
description='ID of the track this talk belongs to')
track_id: str | None = Field(default='',
description='ID of the track this talk belongs to')
submission_type: dict[str, str] | None = Field(
description='The submission type')
state: str = Field(
Expand All @@ -95,12 +96,12 @@ class Submission(BaseModel):
default_factory=int, description='The talk’s duration in minutes, or null')
content_locale: str = Field(
default_factory=str, description='The language the submission is in, e.g. “en” or “de”')
notes: str = Field(default_factory=str, description='note')
notes: str | None = Field(default='', description='note')
internal_notes: str | None = Field(
description='Notes the organisers left on the submission.'
'Available if the requesting user has organiser permissions.')

@validator('track', pre=True)
@field_validator('track', mode="before")
def verify_track(cls, value: Any) -> dict[str, str]:
''' verify track '''
if value is None:
Expand All @@ -113,16 +114,17 @@ class Room(BaseModel):
''' Room '''
id: int = Field(default_factory=int,
description="The unique ID of the room object")
name: str = Field(default_factory=str, description='The name of the room')
description: str = Field(
default_factory=str, description='The description of the room')
name: dict[str, str] = Field(
default_factory=dict, description='The name of the room')
description: dict[str, str] = Field(
description='The description of the room')
capacity: int = Field(default_factory=int,
description='How many people fit in the room')
position: int = Field(
default_factory=int, description='A number indicating the ordering of '
'the room relative to other rooms, '
'e.g. in schedule visualisations')
speaker_info: str = Field(default_factory=str)
speaker_info: dict[str, str] = Field(default_factory=dict)
availabilities: list[dict[str, Any]] = Field(default_factory=list)


Expand All @@ -141,32 +143,34 @@ def __init__(self, domain: str, event: str, token: str) -> None:
super().__init__()
self.url = f'https://{domain}/api/events/{event}'
self.headers['Authorization'] = f'Token {token}'
self.headers['User-Agent'] = 'pipy/toldwords'

def fetch_all(self, path: str) -> Generator[PretalxResponse, None, None]:
''' Fetch all '''
result = self.get(url=self.url+path, params={'limit': 50}).json()
yield PretalxResponse.parse_obj(result)
print(result)
yield TypeAdapter(PretalxResponse).validate_python(result)

while 'next' in result and result['next']:
result = self.get(result['next']).json()
yield PretalxResponse.parse_obj(result)
yield TypeAdapter(PretalxResponse).validate_python(result)

def talks(self) -> Generator[list[Talk], None, None]:
''' Fetch talks '''
for resp in self.fetch_all(path='/talks'):
yield parse_obj_as(list[Talk], resp.results)
yield TypeAdapter(list[Talk]).validate_python(resp.results)

def submissions(self) -> Generator[list[Submission], None, None]:
''' Fetch submissions '''
for resp in self.fetch_all(path='/submissions'):
yield parse_obj_as(list[Submission], resp.results)
for resp in self.fetch_all(path='/submissions/'):
yield TypeAdapter(list[Submission]).validate_python(resp.results)

def speakers(self) -> Generator[list[Speaker], None, None]:
''' Fetch speakers '''
for resp in self.fetch_all(path='/speakers'):
yield parse_obj_as(list[Speaker], resp.results)
yield TypeAdapter(list[Speaker]).validate_python(resp.results)

def rooms(self) -> Generator[list[Room], None, None]:
''' Fetch rooms '''
for resp in self.fetch_all(path='/rooms'):
yield parse_obj_as(list[Room], resp.results)
yield TypeAdapter(list[Room]).validate_python(resp.results)
5 changes: 5 additions & 0 deletions toldwords/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@
'domain': 'pretalx.coscup.org',
'event': 'coscup-2023',
}

DATA2024 = {
'domain': 'pretalx.coscup.org',
'event': 'coscup-2024',
}

0 comments on commit 7a7dbd3

Please sign in to comment.