<jupyter_start><jupyter_text>[![img/pythonista.png](img/pythonista.png)](https://www.pythonista.io)  *Marsmallow*. https://marshmallow.readthedocs.io  El proyecto *Marshmallow*.<jupyter_code>!pip install marshmallow<jupyter_output><empty_output><jupyter_text>La clase ```marshmallow.Schema```.  Campos.https://marshmallow.readthedocs.io/en/stable/marshmallow.fields.html  Validadores.https://marshmallow.readthedocs.io/en/stable/marshmallow.validate.html **Ejemplo:**<jupyter_code>from marshmallow import Schema
from marshmallow.fields import String, Int, Float, Bool
from marshmallow.validate import Length, OneOf, Range
from json import dumps
from data import carreras
class AlumnoSchema(Schema):
    cuenta = Int(required=True, validate=Range(min=1000000, max=9999999))
    nombre = String(required=True, validate=Length(min=2, max=50))
    primer_apellido = String(required=True, validate=Length(min=2, max=50))
    segundo_apellido = String(required=False, validate=Length(min=2, max=50))
    carrera = String(required=True, validate=OneOf(carreras))
    semestre = Int(required=True, validate=Range(min=1, max=50))
    promedio = Float(required=True, validate=Range(min=1, max=50))
    al_corriente = Bool(required=True)
AlumnoSchema().load({
    'cuenta': 1231221, 
    'al_corriente': False, 
    'carrera': 'Arquitectura', 
    'nombre': 'Pedro', 
    'primer_apellido': 'Solis', 
    'promedio': 7.8, 
    'semestre': 3, 
    'segundo_apellido': 'Cabañas'})
AlumnoSchema().load({
    'cuenta': 1232210, 
    'al_corriente': False, 
    'carrera': 'Arquitectura', 
    'nombre': 'Pedro', 
    'primer_apellido': 'Solis', 
    'promedio': 7.8, 
    'semestre': 3,})
AlumnoSchema().load({
    'cuenta': 1232210, 
    'al_corriente': False, 
    'carrera': 'Veterinaria', 
    'nombre': 'Pedro', 
    'primer_apellido': 'Solis', 
    'promedio': 7.8, 
    'semestre': 3,})
AlumnoSchema().load({
    'cuenta': 1232210, 
    'al_corriente': False, 
    'carrera': 'Derecho', 
    'nombre': 'Pedro', 
    'primer_apellido': 'Solis', 
    'promedio': 7.8, 
    'semestre': 3,
    'Género': 'M'})
from dataclasses import dataclass
@dataclass
class Alumno:
    cuenta: int
    nombre: str
    primer_apellido: str
    segundo_apellido: str
    carrera: str
    semestre: int
    promedio: float
    al_corriente: bool
alumno = Alumno(cuenta=1231221, 
    al_corriente=False, 
    carrera='Arquitectura', 
    nombre='Pedro', 
    primer_apellido='Solis', 
    promedio=7.8, 
    semestre=3, 
    segundo_apellido='Cabañas')
AlumnoSchema().dump(alumno)<jupyter_output><empty_output><jupyter_text>La clase ```marshmallow.Schema.Meta```.https://marshmallow.readthedocs.io/en/stable/extending.htmlcustom-class-meta-options  *Marshmallow-SQLAlchemy*.https://marshmallow-sqlalchemy.readthedocs.io/en/latest/  La clase ```marshmallow_sqlalchemy.SQLAlchemySchema```.<jupyter_code>import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, relationship, backref
engine = sa.create_engine("sqlite:///data/alumnosql.sqlite")
session = scoped_session(sessionmaker(bind=engine))
Base = declarative_base()
class AlumnosSQL(Base):
    __tablename__ = 'alumno'
    
    cuenta = sa.Column(sa.Integer, primary_key=True)
    nombre = sa.Column(sa.String(50))
    primer_apellido = sa.Column(sa.String(50))
    segundo_apellido = sa.Column(sa.String(50))
    carrera = sa.Column(sa.String(50))
    semestre = sa.Column(sa.Integer)
    promedio = sa.Column(sa.Float)
    al_corriente = sa.Column(sa.Boolean)
Base.metadata.create_all(engine)
from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field
class AlumnoSchema(SQLAlchemySchema):
    class Meta:
        model = AlumnosSQL
        load_instance = True
        
    cuenta = auto_field()
    nombre = auto_field()
    primer_apellido = auto_field()
    segundo_apellido = auto_field()
    carrera = auto_field()
    semestre = auto_field()
    promedio = auto_field()
    al_corriente = auto_field()
alumno = AlumnosSQL(cuenta=12347)
session.add(alumno)
session.commit()
alumno_schema = AlumnoSchema()
alumno_schema.dump(alumno)
nuevo_alumno = alumno_schema.load({
    'cuenta': 12345,
    'al_corriente': False, 
    'carrera': 'Derecho', 
    'nombre': 'Pedro', 
    'primer_apellido': 'Solis', 
    'promedio': 7.8, 
    'semestre': 3,},
    session=session)
nuevo_alumno
session.add(nuevo_alumno)
session.commit()
%load_ext sql
%sql sqlite:///data/alumnosql.sqlite
%%sql 
select * from alumno<jupyter_output><empty_output><jupyter_text>La clase ```marshmallow_sqlalchemy.SQLAlchemyAutoSchema```.<jupyter_code>from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
class AlumnoSchema2(SQLAlchemyAutoSchema):
    class Meta:
        model = AlumnosSQL
        include_relationships = True
        load_instance = True
alumno_schema2 = AlumnoSchema2()
alumno_schema2.dump(nuevo_alumno)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1344946323678347, 'fraction_numerical': 0.021065424346769293, 'mean_word_length': 4.688940092165899, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 6, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '12788', 'license': 'MIT', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1855, 'n_words': 295, 'path': '25_el_paquete_marsmallow.ipynb', 'repo_name': 'davidrodord56/py221', 'size': '4937'}