u/KNA_Lennox

PyJwt doubt | Why "algorithm" argument during encode() but "algorithms" argument during decode()?

So I was using PyJwt and was trying to create and consume JWTs

I went through the source code for the design of .encode() and .decode() methods.

The method signature for encode is
`jwt.encode(payload, key, algorithm)`
- I can specify a hashing algorithm like SHA or RSA for signing the JWT as a String.

The method signature for decode is
`jwt.decode(token, key, algorithms)`
- I can whitelist algorithms as a List of Strings to retrieve the Payload from the JWT.

What is the object of deliberately having this design like why the difference in algorithm/algorithms during creation v/s consumption?

reddit.com
u/KNA_Lennox — 3 days ago
▲ 0 r/flask

Why do they deliberately access form object properties using the subscript [] over the dot notation in flask-wtf source code implementation?

Okay so the dot notation . is encouraged to be used when accessing the properties/attributes of a field right?

They often don't do that in the source code why?

I was creating a form uisng flask-wtf and am inheriting from the FlaskForm class, my form is an object here then why do they access it like a dictionary in the source code.

I am specifically talking about the implementation of the EqualTo() validator, here is the segment from the source code:

class EqualTo:
    """
    Compares the values of two fields.


    :param fieldname:
        The name of the other field to compare to.
    :param message:
        Error message to raise in case of a validation error. Can be
        interpolated with `%(other_label)s` and `%(other_name)s` to provide a
        more helpful error.
    """


    def __init__(
self
, 
fieldname
, 
message
=None):
        
self
.fieldname = 
fieldname
        
self
.message = 
message


    def __call__(
self
, 
form
, 
field
):
        
try
:
            other = 
form
[
self
.fieldname]
        
except
 KeyError 
as
 exc:
            
raise
 ValidationError(
                
field
.gettext("Invalid field name '%s'.") % 
self
.fieldname
            ) 
from
 exc
        
if
 
field
.data == other.data:
            
return


        d = {
            "other_label": hasattr(other, "label")
            and other.label.text
            or 
self
.fieldname,
            "other_name": 
self
.fieldname,
        }
        message = 
self
.message
        
if
 message is None:
            message = 
field
.gettext("Field must be equal to %(other_name)s.")


        
raise
 ValidationError(message % d) lass EqualTo:
    """
    Compares the values of two fields.


    :param fieldname:
        The name of the other field to compare to.
    :param message:
        Error message to raise in case of a validation error. Can be
        interpolated with `%(other_label)s` and `%(other_name)s` to provide a
        more helpful error.
    """


    def __init__(self, fieldname, message=None):
        self.fieldname = fieldname
        self.message = message


    def __call__(self, form, field):
        try:
            other = form[self.fieldname]
        except KeyError as exc:
            raise ValidationError(
                field.gettext("Invalid field name '%s'.") % self.fieldname
            ) from exc
        if field.data == other.data:
            return


        d = {
            "other_label": hasattr(other, "label")
            and other.label.text
            or self.fieldname,
            "other_name": self.fieldname,
        }
        message = self.message
        if message is None:
            message = field.gettext("Field must be equal to %(other_name)s.")


        raise ValidationError(message % d)

Why do they do,
other = form[self.fieldname]
and not something like,
fieldname = self.fieldname
other = form.fieldname

Why is the design choice here for the form to behave like a dictionary of fields and not like any simple object? Did try to get the answer from AI, confused me instead of clarifying.

reddit.com
u/KNA_Lennox — 5 days ago