Skip to content

Commit e4968fd

Browse files
committed
moved around utils and exceptions
1 parent 5ec7e91 commit e4968fd

File tree

5 files changed

+289
-2
lines changed

5 files changed

+289
-2
lines changed
File renamed without changes.

nbs/model/pydantic_mode.ipynb

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Ragas `BaseModel`\n",
8+
"\n",
9+
"> An Extended version of Pydantics `BaseModel` for some ragas specific stuff"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 1,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"#| default_exp model.pydantic_model"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 1,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"#| export\n",
28+
"import typing as t\n",
29+
"\n",
30+
"from pydantic import BaseModel, PrivateAttr\n",
31+
"\n",
32+
"from ragas_annotator.typing import FieldMeta as RagasFieldMeta"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 2,
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"#| export\n",
42+
"class ExtendedPydanticBaseModel(BaseModel):\n",
43+
" \"\"\"Extended Pydantic BaseModel with database integration capabilities\"\"\"\n",
44+
" \n",
45+
" # Private attribute for storing the database row_id\n",
46+
" _row_id: t.Optional[int] = PrivateAttr(default=None)\n",
47+
" \n",
48+
" # Class variable for storing column mapping overrides\n",
49+
" __column_mapping__: t.ClassVar[t.Dict[str, str]] = {}\n",
50+
" \n",
51+
" def __init__(self, **data):\n",
52+
" super().__init__(**data)\n",
53+
" # Initialize column mapping if not already defined\n",
54+
" if not self.__class__.__column_mapping__:\n",
55+
" self._initialize_column_mapping()\n",
56+
" \n",
57+
" @classmethod\n",
58+
" def _initialize_column_mapping(cls):\n",
59+
" \"\"\"Initialize mapping from field names to column IDs.\"\"\"\n",
60+
" for field_name, field_info in cls.model_fields.items():\n",
61+
" # Check if field has Column metadata (for Pydantic v2)\n",
62+
" column_id = None\n",
63+
" for extra in field_info.metadata or []:\n",
64+
" if isinstance(extra, RagasFieldMeta) and extra.id:\n",
65+
" column_id = extra.id\n",
66+
" break\n",
67+
" \n",
68+
" # If no Column metadata found, use field name as column ID\n",
69+
" if not column_id:\n",
70+
" column_id = field_name\n",
71+
" \n",
72+
" cls.__column_mapping__[field_name] = column_id\n",
73+
" \n",
74+
" @classmethod\n",
75+
" def get_column_id(cls, field_name: str) -> str:\n",
76+
" \"\"\"Get the column ID for a given field name.\"\"\"\n",
77+
" if field_name not in cls.__column_mapping__:\n",
78+
" raise ValueError(f\"No column mapping found for field {field_name}\")\n",
79+
" return cls.__column_mapping__[field_name]\n",
80+
" \n",
81+
" @classmethod\n",
82+
" def set_column_id(cls, field_name: str, column_id: str):\n",
83+
" \"\"\"Set the column ID for a given field name.\"\"\"\n",
84+
" if field_name not in cls.model_fields:\n",
85+
" raise ValueError(f\"Field {field_name} not found in model\")\n",
86+
" cls.__column_mapping__[field_name] = column_id\n",
87+
" \n",
88+
" def get_db_field_mapping(self) -> t.Dict[str, str]:\n",
89+
" \"\"\"Get a mapping from field names to column IDs for this model.\"\"\"\n",
90+
" return self.__class__.__column_mapping__\n",
91+
"\n"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": 12,
97+
"metadata": {},
98+
"outputs": [],
99+
"source": [
100+
"import ragas_annotator.typing as rt"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 13,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"# Example usage\n",
110+
"class TestDataRow(ExtendedPydanticBaseModel):\n",
111+
" id: t.Optional[int] = None\n",
112+
" query: t.Annotated[str, rt.Text(id=\"search_query\")]\n",
113+
" persona: t.List[t.Literal[\"opt1\", \"opt2\", \"opt3\"]]"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 14,
119+
"metadata": {},
120+
"outputs": [
121+
{
122+
"data": {
123+
"text/plain": [
124+
"{}"
125+
]
126+
},
127+
"execution_count": 14,
128+
"metadata": {},
129+
"output_type": "execute_result"
130+
}
131+
],
132+
"source": [
133+
"TestDataRow.__column_mapping__"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 15,
139+
"metadata": {},
140+
"outputs": [],
141+
"source": [
142+
"t = TestDataRow(id=1, query=\"this is a test\", persona=[\"opt1\"])"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 16,
148+
"metadata": {},
149+
"outputs": [
150+
{
151+
"data": {
152+
"text/plain": [
153+
"{'id': 'id', 'query': 'search_query', 'persona': 'persona'}"
154+
]
155+
},
156+
"execution_count": 16,
157+
"metadata": {},
158+
"output_type": "execute_result"
159+
}
160+
],
161+
"source": [
162+
"t.__column_mapping__"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": null,
168+
"metadata": {},
169+
"outputs": [],
170+
"source": []
171+
}
172+
],
173+
"metadata": {
174+
"kernelspec": {
175+
"display_name": ".venv",
176+
"language": "python",
177+
"name": "python3"
178+
},
179+
"language_info": {
180+
"codemirror_mode": {
181+
"name": "ipython",
182+
"version": 3
183+
},
184+
"file_extension": ".py",
185+
"mimetype": "text/x-python",
186+
"name": "python",
187+
"nbconvert_exporter": "python",
188+
"pygments_lexer": "ipython3",
189+
"version": "3.12.8"
190+
}
191+
},
192+
"nbformat": 4,
193+
"nbformat_minor": 2
194+
}

nbs/utils.ipynb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#| default_exp utils"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"#| export\n",
19+
"import string\n",
20+
"import uuid"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"#| export\n",
30+
"def create_nano_id(size=12):\n",
31+
" # Define characters to use (alphanumeric)\n",
32+
" alphabet = string.ascii_letters + string.digits\n",
33+
" \n",
34+
" # Generate UUID and convert to int\n",
35+
" uuid_int = uuid.uuid4().int\n",
36+
" \n",
37+
" # Convert to base62\n",
38+
" result = \"\"\n",
39+
" while uuid_int:\n",
40+
" uuid_int, remainder = divmod(uuid_int, len(alphabet))\n",
41+
" result = alphabet[remainder] + result\n",
42+
" \n",
43+
" # Pad if necessary and return desired length\n",
44+
" return result[:size]"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"metadata": {},
51+
"outputs": [],
52+
"source": []
53+
}
54+
],
55+
"metadata": {
56+
"kernelspec": {
57+
"display_name": ".venv",
58+
"language": "python",
59+
"name": "python3"
60+
},
61+
"language_info": {
62+
"name": "python",
63+
"version": "3.12.8"
64+
}
65+
},
66+
"nbformat": 4,
67+
"nbformat_minor": 2
68+
}

ragas_annotator/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""All the exceptions specific to the `notion_annotator` project."""
22

3-
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/utils/exceptions.ipynb.
3+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/exceptions.ipynb.
44

55
# %% auto 0
66
__all__ = ['ValidationError', 'DuplicateError', 'NotFoundError']
77

8-
# %% ../nbs/utils/exceptions.ipynb 2
8+
# %% ../nbs/exceptions.ipynb 2
99
class ValidationError(Exception):
1010
"""Raised when field validation fails."""
1111

ragas_annotator/utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/utils.ipynb.
2+
3+
# %% auto 0
4+
__all__ = ['create_nano_id']
5+
6+
# %% ../nbs/utils.ipynb 1
7+
import string
8+
import uuid
9+
10+
# %% ../nbs/utils.ipynb 2
11+
def create_nano_id(size=12):
12+
# Define characters to use (alphanumeric)
13+
alphabet = string.ascii_letters + string.digits
14+
15+
# Generate UUID and convert to int
16+
uuid_int = uuid.uuid4().int
17+
18+
# Convert to base62
19+
result = ""
20+
while uuid_int:
21+
uuid_int, remainder = divmod(uuid_int, len(alphabet))
22+
result = alphabet[remainder] + result
23+
24+
# Pad if necessary and return desired length
25+
return result[:size]

0 commit comments

Comments
 (0)