Skip to content

Commit

Permalink
Refactor: Eliminate Global Variables for Improved Code Maintainabilit…
Browse files Browse the repository at this point in the history
…y_fix_release (#4208)

Refactor: Eliminate Global Variables for Improved Code Maintainability

- Replaced global variables with local variables or class attributes.
- Enhanced code readability and reduced potential side effects.
  • Loading branch information
edwinjosechittilappilly authored Oct 18, 2024
1 parent 592ad87 commit 68b7b24
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/backend/base/langflow/components/prototypes/CreateData.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
from langflow.schema import Data
from langflow.schema.dotdict import dotdict

MAX_NUMBER_OF_FIELDS = 15


class CreateDataComponent(Component):
display_name: str = "Create Data"
description: str = "Dynamically create a Data with a specified number of fields."
name: str = "CreateData"
MAX_FIELDS = 15 # Define a constant for maximum number of fields

inputs = [
IntInput(
Expand All @@ -22,7 +21,7 @@ class CreateDataComponent(Component):
info="Number of fields to be added to the record.",
real_time_refresh=True,
value=1,
range_spec=RangeSpec(min=1, max=15, step=1, step_type="int"),
range_spec=RangeSpec(min=1, max=MAX_FIELDS, step=1, step_type="int"),
),
MessageTextInput(
name="text_key",
Expand Down Expand Up @@ -50,10 +49,11 @@ def update_build_config(self, build_config: dotdict, field_value: Any, field_nam
except ValueError:
return build_config
existing_fields = {}
if field_value_int > MAX_NUMBER_OF_FIELDS:
build_config["number_of_fields"]["value"] = MAX_NUMBER_OF_FIELDS
if field_value_int > self.MAX_FIELDS:
build_config["number_of_fields"]["value"] = self.MAX_FIELDS
msg = (
f"Number of fields cannot exceed {MAX_NUMBER_OF_FIELDS}. Try using a Component to combine two Data."
f"Number of fields cannot exceed {self.MAX_FIELDS}. "
"Please adjust the number of fields to be within the allowed limit."
)
raise ValueError(msg)
if len(build_config) > len(default_keys):
Expand Down Expand Up @@ -88,7 +88,7 @@ async def build_data(self) -> Data:
return return_data

def get_data(self):
"""Function to get the Data from the attributes"""
"""Function to get the Data from the attributes."""
data = {}
for value_dict in self._attributes.values():
if isinstance(value_dict, dict):
Expand All @@ -100,7 +100,7 @@ def get_data(self):
return data

def validate_text_key(self):
"""This function validates that the Text Key is one of the keys in the Data"""
"""This function validates that the Text Key is one of the keys in the Data."""
data_keys = self.get_data().keys()
if self.text_key not in data_keys and self.text_key != "":
formatted_data_keys = ", ".join(data_keys)
Expand Down

0 comments on commit 68b7b24

Please sign in to comment.