Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Gregson authored and Sam Gregson committed May 13, 2024
1 parent c5a3dcc commit 63fe206
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 20 deletions.
2 changes: 0 additions & 2 deletions data/components.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import json
import os

# Get the absolute path of the components.json file
file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'data', 'components.json'))

# Load the components.json file to a dictionary variable
def load_components():
Expand Down
92 changes: 77 additions & 15 deletions models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,98 @@

components_dict = load_components()


class Component(BaseModel):
Name: str = Field(..., description="The name of the component to be added. Only standard grasshopper components are allowed")
Id: int = Field(..., description="A unique identifier for the component, starting from 1 and counting upwards")
Value: Optional[str] = Field(None, alias='Value', description="The range of values for the component, if applicable. Only to be used for Panel, Number Slider, or Point components")
Name: str = Field(
...,
description="The name of the component to be added. Only standard"
"grasshopper components are allowed"
)
Id: int = Field(
...,
description="A unique identifier for the component, starting from 1"
"and counting upwards"
)
Value: Optional[str] = Field(
None,
alias='Value',
description="The range of values for the component, if applicable."
"Only to be used for Panel, Number Slider,"
"or Point components"
)

@field_validator("Name")
@classmethod
def validate_name_exists(cls, v):
name_iterator = (component["Name"] for component in components_dict)
if v not in name_iterator:
raise ValueError(f"The component {v} could not be found, either there is a typo or it does not exist. Please choose a valid component.")
raise ValueError(f"The component {v} could not be found, either"
"there is a typo or it does not exist. Please"
"choose a valid component.")
return v


class InputConnectionDetail(BaseModel):
Id: int = Field(..., description="The unique identifier of the component the connection is related to")
ParameterName: str = Field(..., description="The specific input parameter of the component that the connection affects")
Id: int = Field(
...,
description="The unique identifier of the component the connection is"
"related to"
)
ParameterName: str = Field(
...,
description="The specific input parameter of the component that the"
"connection affects"
)


class OutputConnectionDetail(BaseModel):
Id: int = Field(..., description="The unique identifier of the component the connection is related to")
ParameterName: str = Field(..., description="The specific output parameter of the component that the connection affects")
Id: int = Field(
...,
description="The unique identifier of the component the connection is"
"related to"
)
ParameterName: str = Field(
...,
description="The specific output parameter of the component that the"
"connection affects"
)


class Connection(BaseModel):
From: OutputConnectionDetail = Field(..., description="The source component and parameter from which the connection originates")
To: InputConnectionDetail = Field(..., description="The target component and parameter that the connection is directing to")
From: OutputConnectionDetail = Field(
...,
description="The source component and parameter from which the"
"connection originates"
)
To: InputConnectionDetail = Field(
...,
description="The target component and parameter that the connection is"
"directing to"
)


class GrasshopperScriptModel(BaseModel):
"""
A representation of a grasshopper script with all grasshopper components and the connections between them. Use Number Slider for variable inputs to the script
A representation of a grasshopper script with all grasshopper components
and the connections between them.
Use Number Slider for variable inputs to the script
"""
ChainOfThought: str = Field(..., description="step by step rational explaining how the script acheives the aim, including the main components used")
Advice: str = Field(..., description="A piece of advice or instruction related to using the grasshopper script")
Additions: List[Component] = Field(..., description="A list of components to be added to the configuration")
Connections: List[Connection] = Field(..., description="A list of connections defining relationships between components' parameters")
ChainOfThought: str = Field(
...,
description="step by step rational explaining how the script acheives "
"the aim, including the main components used"
)
Advice: str = Field(
...,
description="A piece of advice or instruction related to using the "
"grasshopper script"
)
Additions: List[Component] = Field(
...,
description="A list of components to be added to the configuration"
)
Connections: List[Connection] = Field(
...,
description="A list of connections defining relationships between "
"components' parameters"
)
14 changes: 11 additions & 3 deletions notebooks/test_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

# file path needed to work with pytest
dir_path = os.path.dirname(os.path.realpath(__file__))
notebooks = [os.path.join(dir_path, 'ghpt_baseline.ipynb'), os.path.join(dir_path, 'ghpt_instructor.ipynb')]
notebooks = [
os.path.join(dir_path, 'ghpt_baseline.ipynb'),
os.path.join(dir_path, 'ghpt_instructor.ipynb')
]


@pytest.mark.parametrize("notebook", notebooks)
def test_notebooks(notebook):
Expand All @@ -22,6 +26,10 @@ def test_notebooks(notebook):
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
try:
assert ep.preprocess(nb) is not None, f"Got empty notebook for {notebook}"
executed_nb, _ = ep.preprocess(
nb,
{'metadata': {'path': dir_path}}
)
assert executed_nb, f"Got empty notebook for {notebook}"
except Exception as e:
assert False, f"Failed executing {notebook}: {str(e)}"
assert False, f"Failed executing {notebook}: {str(e)}"

0 comments on commit 63fe206

Please sign in to comment.