added from_choice classmethod
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from .classes import ExperientialModelOutput
|
from .classes import VisualSyntaxModelOutput
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, ValidationError
|
||||||
from typing import List
|
from typing import List
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
class OptionNotSetException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
class ModelOutput(BaseModel):
|
class ModelOutput(BaseModel):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -20,6 +23,18 @@ class ModelOutput(BaseModel):
|
|||||||
"""Instantiate with random numbers."""
|
"""Instantiate with random numbers."""
|
||||||
kwargs = {field: random.random() for field in cls.list_fields()}
|
kwargs = {field: random.random() for field in cls.list_fields()}
|
||||||
return cls(**kwargs)
|
return cls(**kwargs)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_choice(cls, option: str):
|
||||||
|
"""Instantiate from choice."""
|
||||||
|
if option is None:
|
||||||
|
raise ValidationError()
|
||||||
|
assert isinstance(option, str), "option is not a string"
|
||||||
|
allowed_options_list = cls.list_fields()
|
||||||
|
assert option in allowed_options_list, f"{option} is not among allowed fields {allowed_options_list}"
|
||||||
|
kwargs = {field: 0 for field in cls.list_fields()}
|
||||||
|
kwargs[option] = 1
|
||||||
|
return cls(**kwargs)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
model_dict = self.model_dump()
|
model_dict = self.model_dump()
|
||||||
@@ -39,7 +54,7 @@ class ModelOutput(BaseModel):
|
|||||||
return max(model_dict.values())
|
return max(model_dict.values())
|
||||||
|
|
||||||
|
|
||||||
class ExperientialModelOutput(ModelOutput):
|
class VisualSyntaxModelOutput(ModelOutput):
|
||||||
non_transactional_action: float
|
non_transactional_action: float
|
||||||
non_transactional_reaction: float
|
non_transactional_reaction: float
|
||||||
unidirectional_transactional_action: float
|
unidirectional_transactional_action: float
|
||||||
@@ -61,7 +76,7 @@ class ExperientialModelOutput(ModelOutput):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
m = ExperientialModelOutput.from_random()
|
m = VisualSyntaxModelOutput.from_random()
|
||||||
print(m)
|
print(m)
|
||||||
print(repr(m))
|
print(repr(m))
|
||||||
print(m.highest_score_field())
|
print(m.highest_score_field())
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, ValidationError
|
||||||
from typing import List
|
from typing import List
|
||||||
import random
|
import random
|
||||||
|
|
||||||
@@ -20,6 +20,18 @@ class ModelOutput(BaseModel):
|
|||||||
"""Instantiate with random numbers."""
|
"""Instantiate with random numbers."""
|
||||||
kwargs = {field: random.random() for field in cls.list_fields()}
|
kwargs = {field: random.random() for field in cls.list_fields()}
|
||||||
return cls(**kwargs)
|
return cls(**kwargs)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_choice(cls, option: str):
|
||||||
|
"""Instantiate from choice."""
|
||||||
|
if option is None:
|
||||||
|
raise ValidationError()
|
||||||
|
assert isinstance(option, str)
|
||||||
|
allowed_options_list = cls.list_fields()
|
||||||
|
assert option in allowed_options_list, f"{option} is not among allowed fields {allowed_options_list}"
|
||||||
|
kwargs = {field: 0 for field in cls.list_fields()}
|
||||||
|
kwargs[option] = 1
|
||||||
|
return cls(**kwargs)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
model_dict = self.model_dump()
|
model_dict = self.model_dump()
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, ValidationError
|
||||||
from typing import List
|
from typing import List
|
||||||
import random
|
import random
|
||||||
|
|
||||||
@@ -20,6 +20,18 @@ class ModelOutput(BaseModel):
|
|||||||
"""Instantiate with random numbers."""
|
"""Instantiate with random numbers."""
|
||||||
kwargs = {field: random.random() for field in cls.list_fields()}
|
kwargs = {field: random.random() for field in cls.list_fields()}
|
||||||
return cls(**kwargs)
|
return cls(**kwargs)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_choice(cls, option: str):
|
||||||
|
"""Instantiate from choice."""
|
||||||
|
if option is None:
|
||||||
|
raise ValidationError()
|
||||||
|
assert isinstance(option, str)
|
||||||
|
allowed_options_list = cls.list_fields()
|
||||||
|
assert option in allowed_options_list, f"{option} is not among allowed fields {allowed_options_list}"
|
||||||
|
kwargs = {field: 0 for field in cls.list_fields()}
|
||||||
|
kwargs[option] = 1
|
||||||
|
return cls(**kwargs)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
model_dict = self.model_dump()
|
model_dict = self.model_dump()
|
||||||
|
|||||||
Reference in New Issue
Block a user