Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1f967d7

Browse files
authored
Add YAML, XML, INI file format and RabbitMQ & ARQ queue documentation examples (#11838)
1 parent0a21ab1 commit1f967d7

File tree

2 files changed

+271
-6
lines changed

2 files changed

+271
-6
lines changed

‎docs/examples/files.md

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class Person(BaseModel):
3535

3636
json_string= pathlib.Path('person.json').read_text()
3737
person= Person.model_validate_json(json_string)
38-
print(repr(person))
39-
#>Person(name='John Doe', age=30, email='john@example.com')
38+
print(person)
39+
#> name='John Doe' age=30 email='john@example.com'
4040
```
4141

4242
If the data in the file is not valid,`pydantic` will raise a[`ValidationError`][pydantic_core.ValidationError].
@@ -229,8 +229,110 @@ with open('person.toml', 'rb') as f:
229229
data= tomllib.load(f)
230230

231231
person= Person.model_validate(data)
232-
print(repr(person))
233-
#>Person(name='John Doe', age=30, email='john@example.com')
232+
print(person)
233+
#> name='John Doe' age=30 email='john@example.com'
234234
```
235235

236-
<!-- TODO: YAML and other file formats (great for new contributors!)-->
236+
##YAML files
237+
238+
YAML (YAML Ain't Markup Language) is a human-readable data serialization format that is often used for configuration files.
239+
240+
Consider the following YAML file:
241+
242+
```yaml
243+
name:John Doe
244+
age:30
245+
email:john@example.com
246+
```
247+
248+
Here's how we validate that data:
249+
250+
```python {test="skip"}
251+
import yaml
252+
253+
from pydantic import BaseModel, EmailStr, PositiveInt
254+
255+
256+
class Person(BaseModel):
257+
name:str
258+
age:PositiveInt
259+
email:EmailStr
260+
261+
262+
with open('person.yaml') as f:
263+
data = yaml.safe_load(f)
264+
265+
person = Person.model_validate(data)
266+
print(person)
267+
#> name='John Doe' age=30 email='john@example.com'
268+
```
269+
270+
##XML files
271+
272+
XML (eXtensible Markup Language) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.
273+
274+
Consider the following XML file:
275+
276+
```xml
277+
<?xml version="1.0"?>
278+
<person>
279+
<name>John Doe</name>
280+
<age>30</age>
281+
<email>john@example.com</email>
282+
</person>
283+
```
284+
285+
Here's how we validate that data:
286+
287+
```python {test="skip"}
288+
import xml.etree.ElementTreeasET
289+
290+
from pydanticimport BaseModel, EmailStr, PositiveInt
291+
292+
293+
classPerson(BaseModel):
294+
name:str
295+
age: PositiveInt
296+
email: EmailStr
297+
298+
299+
tree=ET.parse('person.xml').getroot()
300+
data= {child.tag: child.textfor childin tree}
301+
person= Person.model_validate(data)
302+
print(person)
303+
#> name='John Doe' age=30 email='john@example.com'
304+
```
305+
306+
##INI files
307+
308+
INI files are a simple configuration file format that uses sections and key-value pairs. They are commonly used in Windows applications and older software.
309+
310+
Consider the following INI file:
311+
312+
```ini
313+
[PERSON]
314+
name = John Doe
315+
age = 30
316+
email = john@example.com
317+
```
318+
319+
Here's how we validate that data:
320+
321+
```python {test="skip"}
322+
import configparser
323+
324+
from pydanticimport BaseModel, EmailStr, PositiveInt
325+
326+
327+
classPerson(BaseModel):
328+
name:str
329+
age: PositiveInt
330+
email: EmailStr
331+
332+
333+
config= configparser.ConfigParser()
334+
config.read('person.ini')
335+
person= Person.model_validate(config['PERSON'])
336+
print(person)
337+
#> name='John Doe' age=30 email='john@example.com'
338+
```

‎docs/examples/queues.md

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,167 @@ pop_from_queue()
6565
#> Queue is empty
6666
```
6767

68-
<!-- TODO: kafka, rabbitMQ, celery, arq, etc - better for SEO, great for new contributors!-->
68+
##RabbitMQ
69+
70+
RabbitMQ is a popular message broker that implements the AMQP protocol.
71+
72+
In order to run this example locally, you'll first need to[install RabbitMQ](https://www.rabbitmq.com/download.html) and start your server.
73+
74+
Here's a simple example of how you can use Pydantic to:
75+
76+
1. Serialize data to push to the queue
77+
2. Deserialize and validate data when it's popped from the queue
78+
79+
First, let's create a sender script.
80+
81+
```python {test="skip"}
82+
import pika
83+
84+
from pydanticimport BaseModel, EmailStr
85+
86+
87+
classUser(BaseModel):
88+
id:int
89+
name:str
90+
email: EmailStr
91+
92+
93+
connection= pika.BlockingConnection(pika.ConnectionParameters('localhost'))
94+
channel= connection.channel()
95+
QUEUE_NAME='user_queue'
96+
channel.queue_declare(queue=QUEUE_NAME)
97+
98+
99+
defpush_to_queue(user_data: User) ->None:
100+
serialized_data= user_data.model_dump_json()
101+
channel.basic_publish(
102+
exchange='',
103+
routing_key=QUEUE_NAME,
104+
body=serialized_data,
105+
)
106+
print(f'Added to queue:{serialized_data}')
107+
108+
109+
user1= User(id=1,name='John Doe',email='john@example.com')
110+
user2= User(id=2,name='Jane Doe',email='jane@example.com')
111+
112+
push_to_queue(user1)
113+
#> Added to queue: {"id":1,"name":"John Doe","email":"john@example.com"}
114+
115+
push_to_queue(user2)
116+
#> Added to queue: {"id":2,"name":"Jane Doe","email":"jane@example.com"}
117+
118+
connection.close()
119+
```
120+
121+
And here's the receiver script.
122+
123+
```python {test="skip"}
124+
import pika
125+
126+
from pydanticimport BaseModel, EmailStr
127+
128+
129+
classUser(BaseModel):
130+
id:int
131+
name:str
132+
email: EmailStr
133+
134+
135+
defmain():
136+
connection= pika.BlockingConnection(pika.ConnectionParameters('localhost'))
137+
channel= connection.channel()
138+
QUEUE_NAME='user_queue'
139+
channel.queue_declare(queue=QUEUE_NAME)
140+
141+
defprocess_message(
142+
ch: pika.channel.Channel,
143+
method: pika.spec.Basic.Deliver,
144+
properties: pika.spec.BasicProperties,
145+
body:bytes,
146+
):
147+
user= User.model_validate_json(body)
148+
print(f'Validated user:{repr(user)}')
149+
ch.basic_ack(delivery_tag=method.delivery_tag)
150+
151+
channel.basic_consume(queue=QUEUE_NAME,on_message_callback=process_message)
152+
channel.start_consuming()
153+
154+
155+
if__name__=='__main__':
156+
try:
157+
main()
158+
exceptKeyboardInterrupt:
159+
pass
160+
```
161+
162+
To test this example:
163+
164+
1. Run the receiver script in one terminal to start the consumer.
165+
2. Run the sender script in another terminal to send messages.
166+
167+
##ARQ
168+
169+
ARQ is a fast Redis-based job queue for Python.
170+
It's built on top of Redis and provides a simple way to handle background tasks.
171+
172+
In order to run this example locally, you’ll need to[Install Redis](https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/) and start your server.
173+
174+
Here's a simple example of how you can use Pydantic with ARQ to:
175+
176+
1. Define a model for your job data
177+
2. Serialize data when enqueueing jobs
178+
3. Validate and deserialize data when processing jobs
179+
180+
```python {test="skip"}
181+
import asyncio
182+
from typingimport Any
183+
184+
from arqimport create_pool
185+
from arq.connectionsimport RedisSettings
186+
187+
from pydanticimport BaseModel, EmailStr
188+
189+
190+
classUser(BaseModel):
191+
id:int
192+
name:str
193+
email: EmailStr
194+
195+
196+
REDIS_SETTINGS= RedisSettings()
197+
198+
199+
asyncdefprocess_user(ctx: dict[str, Any],user_data: dict[str, Any]) ->None:
200+
user= User.model_validate(user_data)
201+
print(f'Processing user:{repr(user)}')
202+
203+
204+
asyncdefenqueue_jobs(redis):
205+
user1= User(id=1,name='John Doe',email='john@example.com')
206+
user2= User(id=2,name='Jane Doe',email='jane@example.com')
207+
208+
await redis.enqueue_job('process_user', user1.model_dump())
209+
print(f'Enqueued user:{repr(user1)}')
210+
211+
await redis.enqueue_job('process_user', user2.model_dump())
212+
print(f'Enqueued user:{repr(user2)}')
213+
214+
215+
classWorkerSettings:
216+
functions= [process_user]
217+
redis_settings=REDIS_SETTINGS
218+
219+
220+
asyncdefmain():
221+
redis=await create_pool(REDIS_SETTINGS)
222+
await enqueue_jobs(redis)
223+
224+
225+
if__name__=='__main__':
226+
asyncio.run(main())
227+
```
228+
229+
This script is complete.
230+
It should run "as is" both to enqueue jobs and to process them.
231+
<!-- TODO: kafka, celery, etc - better for SEO, great for new contributors!-->

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp