From 77b7cfe3bccb0a6ef11702c6127199c5a7c91042 Mon Sep 17 00:00:00 2001 From: Shriniwas Kulkarni <153389794+Shriniwas18K@users.noreply.github.com> Date: Sat, 24 Aug 2024 01:00:45 +0530 Subject: [PATCH] Update value_class_encoder.py Refactored the multiple if statements into single for loop and provided proper documentation to the module --- appwrite/encoders/value_class_encoder.py | 107 +++++++++++------------ 1 file changed, 52 insertions(+), 55 deletions(-) diff --git a/appwrite/encoders/value_class_encoder.py b/appwrite/encoders/value_class_encoder.py index f343add..b897ef0 100644 --- a/appwrite/encoders/value_class_encoder.py +++ b/appwrite/encoders/value_class_encoder.py @@ -1,3 +1,19 @@ +""" +This module defines a custom JSON encoder that handles various enum classes. + +It provides a convenient way to serialize enum values to their corresponding string representations +when encoding objects to JSON. + +Usage: + import json + from your_module import ValueClassEncoder + + # Create an instance of the encoder + encoder = ValueClassEncoder() + + # Serialize an object containing enum values + json_data = json.dumps(your_object, cls=ValueClassEncoder) +""" import json from ..enums.authenticator_type import AuthenticatorType from ..enums.authentication_factor import AuthenticationFactor @@ -18,60 +34,41 @@ from ..enums.password_hash import PasswordHash from ..enums.messaging_provider_type import MessagingProviderType -class ValueClassEncoder(json.JSONEncoder): - def default(self, o): - if isinstance(o, AuthenticatorType): - return o.value - - if isinstance(o, AuthenticationFactor): - return o.value - - if isinstance(o, OAuthProvider): - return o.value - - if isinstance(o, Browser): - return o.value - - if isinstance(o, CreditCard): - return o.value - - if isinstance(o, Flag): - return o.value - - if isinstance(o, RelationshipType): - return o.value - if isinstance(o, RelationMutate): - return o.value - - if isinstance(o, IndexType): - return o.value - - if isinstance(o, Runtime): - return o.value - - if isinstance(o, ExecutionMethod): - return o.value - - if isinstance(o, Name): - return o.value - - if isinstance(o, SmtpEncryption): - return o.value - - if isinstance(o, Compression): - return o.value - - if isinstance(o, ImageGravity): - return o.value - - if isinstance(o, ImageFormat): - return o.value - - if isinstance(o, PasswordHash): - return o.value - - if isinstance(o, MessagingProviderType): - return o.value +class ValueClassEncoder(json.JSONEncoder): + """Custom JSON encoder for handling enum classes.""" + + VALUE_CLASSES = ( + AuthenticatorType, + AuthenticationFactor, + OAuthProvider, + Browser, + CreditCard, + Flag, + RelationshipType, + RelationMutate, + IndexType, + Runtime, + ExecutionMethod, + Name, + SmtpEncryption, + Compression, + ImageGravity, + ImageFormat, + PasswordHash, + MessagingProviderType, + ) - return super().default(o) \ No newline at end of file + def default(self, o): + """Encodes the given object to a JSON representation. + + Args: + o: The object to be encoded. + + Returns: + The JSON representation of the object. + """ + for enum_class in self.VALUE_CLASSES: + if isinstance(o, enum_class): + return o.value + return super().default(o)