Skip to content

V1

Compare
Choose a tag to compare
@dhvcc dhvcc released this 31 May 23:20
· 49 commits to master since this release
5c20e0e

V1

Complete rewrite of the library to use xmltodict and pydantic

Notable changes:

  • Ditched bs4
  • Now using xmltodict and pydantic
  • Removed limit option
  • Parser now uses classmethods

I suggest reading new docs in Readme, but here's the key point apart from using pydantic


Tag field

This is a generic field that handles tags as raw data or a dictonary returned with attributes

Although this is a complex class, it forwards most of the methods to it's content attribute, so you don't notice a difference if you're only after the .content value

Example

from rss_parser.models import XMLBaseModel
class Model(XMLBaseModel):
     number: Tag[int]
     string: Tag[str]

m = Model(
    number=1,
    string={'@attr': '1', '#text': 'content'},
)

m.number.content == 1  # Content value is an integer, as per the generic type

m.number.content + 10 == m.number + 10  # But you're still able to use the Tag itself in common operators

m.number.bit_length() == 1  # As it's the case for methods/attributes not found in the Tag itself

type(m.number), type(m.number.content) == (<class 'rss_parser.models.image.Tag[int]'>, <class 'int'>)  # types are NOT the same, however, the interfaces are very similar most of the time

m.number.attributes == {}  # The attributes are empty by default

m.string.attributes == {'attr': '1'}  # But are populated when provided. Note that the @ symbol is trimmed from the beggining, however, camelCase is not converted

# Generic argument types are handled by pydantic - let's try to provide a string for a Tag[int] number

m = Model(number='not_a_number', string={'@customAttr': 'v', '#text': 'str tag value'})  # This will lead in the following traceback

# Traceback (most recent call last):
#     ...
# pydantic.error_wrappers.ValidationError: 1 validation error for Model
# number -> content
#     value is not a valid integer (type=type_error.integer)