class AsyncResourceLock(BaseModel): _lock: Lock = PrivateAttr() _is_locked = PrivateAttr() def __init__(self, **data: Any) -> None: super().__init__(**data) self._lock = Lock() @classmethod def from_is_locked(cls, is_locked: bool) -> "AsyncResourceLock": instance = cls() instance._is_locked = is_locked return instance async def mark_as_locked_if_unlocked(self) -> bool: """ If the resource is currently not in used it will mark it as in use. returns: True if it succeeds otherwise False """ async with self._lock: if not self._is_locked: self._is_locked = True return True return False async def unlock_resource(self) -> None: """Marks the resource as unlocked""" async with self._lock: self._is_locked = False
class ReferenceDataParams(CompareMixin): """Parameters for the "reference" data series""" data_available: ReferenceDataEnum performance_granularity: Optional[PerformanceGranularityEnum] # type: ignore _weather_types = PrivateAttr((JobDataTypeEnum.reference_weather,)) _model_chain_method: str = PrivateAttr() def __init__(self, **data): super().__init__(**data) if self.data_available == ReferenceDataEnum.weather_and_ac: self._performance_types = (JobDataTypeEnum.reference_performance,) elif self.data_available == ReferenceDataEnum.weather_and_ac_and_dc: self._performance_types = ( JobDataTypeEnum.reference_performance, JobDataTypeEnum.reference_performance_dc, ) else: self._performance_types = () self._model_chain_method = MODEL_CHAIN_METHOD_MAP[self.irradiance_type] @root_validator def check_performance_granularity(cls, values): da = values.get("data_available") pg = values.get("performance_granularity") if da == ReferenceDataEnum.weather_only and pg is not None: raise ValueError( "Performance granularity is invalid when not providing reference " "performance" ) return values
class ServerNode(BaseModel): server_uid: int = 0 server_name: str = "" host: str = "" port: str = "" desc: str = "" service_type: Dict[str, str] = {} # 下面两个成员不是元数据 # _session是一个弱引用, 减少一次查询 _session: Optional[weakref.ReferenceType[SocketSession]] = PrivateAttr(default=None) _session_id: int = PrivateAttr(default=0) @property def session_id(self): return self._session_id @property def session(self) -> Optional[SocketSession]: if self._session is not None: return self._session() return None def set_session(self, session: SocketSession): self._session_id = session.session_id self._session = weakref.ref(session)
class BaseModel(PydanticBaseModel): _client = PrivateAttr(Optional['B2C2APIClient']) # noqa _widgets_cache = PrivateAttr(None) def bind_to_client(self, client: 'B2C2APIClient') -> None: # noqa self._client = client def copy(self, *args, **kwargs): new = super().copy(*args, **kwargs) new._widgets_cache = None return new @property def _widgets(self): # Defered property: there's no point # creating widgets until they're accessed. # They will not be accessed until someone # deliberately uses the GUI. if not self._widgets_cache: self._widgets_cache = dict(self._get_data_widgets()) return self._widgets_cache @property def is_bound(self) -> bool: return bool(getattr(self, '_client', None)) def __repr__(self): return pformat(self) def _as_csv_row(self): return ','.join( str(v) for v in self.dict().values() ) def _repr_pretty_(self, printer, cycle) -> None: """ Display hook for the IPython repl. """ printer.text(pformat(self)) def _get_data_widgets(self): for field, field_value in self.dict().items(): field_name_human_readable = field.replace('_', ' ').title() label = ipywidgets.Label( '{}:'.format(field_name_human_readable), layout={'width': '150px'} ) data = ipywidgets.Label(str(field_value)) yield (field, ipywidgets.HBox([label, data])) def _ipython_display_(self): """ Display hook for Jupyter/QT notebooks """ display( ipywidgets.VBox(list(self._widgets.values())) )
class PVWattsModuleParameters(PVLibBase): """Parameters for the modules that make up an array in a PVWatts-like model""" pdc0: float = Field( ..., description="Power of the modules at 1000 W/m^2 and cell reference temperature", ) gamma_pdc: float = Field( ..., description=( "Temperature coefficient of power in units of %/C. " "Typically -0.2 to -0.5 % per degree C" ), ) _modelchain_dc_model: str = PrivateAttr("pvwatts") _gamma: float = PrivateAttr() def __init__(self, **data): super().__init__(**data) self._gamma = self.gamma_pdc / 100 def pvlib_dict(self): """Convert to a dict pvlib understands for `module_parameters` i.e. scale gamma_pdc to 1/C""" return {k: v / 100 if k == "gamma_pdc" else v for k, v in self.dict().items()}
class TranslationAnalyzer(BaseAnalyzer): _pipeline: Pipeline = PrivateAttr() _max_length: int = PrivateAttr() TYPE: str = "Translation" model_name_or_path: str def __init__(self, **data: Any): super().__init__(**data) tokenizer = AutoTokenizer.from_pretrained(self.model_name_or_path) model = AutoModelForSeq2SeqLM.from_pretrained(self.model_name_or_path) self._pipeline = pipeline("translation", model=model, tokenizer=tokenizer, device=self._device_id) if hasattr(self._pipeline.model.config, "max_position_embeddings"): self._max_length = self._pipeline.model.config.max_position_embeddings else: self._max_length = MAX_LENGTH def analyze_input( self, source_response_list: List[TextPayload], analyzer_config: Optional[BaseAnalyzerConfig] = None, **kwargs, ) -> List[TextPayload]: analyzer_output = [] for batch_responses in self.batchify(source_response_list, self.batch_size): texts = [ source_response.processed_text[:self._max_length] for source_response in batch_responses ] batch_predictions = self._pipeline(texts) for prediction, source_response in zip(batch_predictions, batch_responses): segmented_data = { "translation_data": { "original_text": source_response.processed_text } } if source_response.segmented_data: segmented_data = { **segmented_data, **source_response.segmented_data, } analyzer_output.append( TextPayload( processed_text=prediction["translation_text"], meta=source_response.meta, segmented_data=segmented_data, source_name=source_response.source_name, )) return analyzer_output
class CompareExpectedActualJobParameters(CompareMixin, JobParametersBase): """Calculate and compare expected to actual performance""" compare: ExpectedActualEnum _weather_types: Tuple[JobDataTypeEnum, ...] = PrivateAttr( (JobDataTypeEnum.actual_weather, )) _performance_types: Tuple[JobDataTypeEnum, ...] = PrivateAttr( (JobDataTypeEnum.actual_performance, ))
class TimeAwareModel(BaseModel): _processed_at: datetime = PrivateAttr(default_factory=datetime.now) _secret_value: str = PrivateAttr() def __init__(self, **data): super().__init__(**data) # this could also be done with default_factory self._secret_value = randint(1, 5)
class Relationship(UniqueGraphObject, metaclass=RelationshipMetaclass): _end_node_id: int = PrivateAttr() _start_node_id: int = PrivateAttr() _type: str = PrivateAttr() def __init__(self, **data): super().__init__(**data) self._start_node_id = data.get("_start_node_id") self._end_node_id = data.get("_end_node_id") self._type = data.get("_type", getattr(type(self), "type", "Relationship")) @property def _nodes(self) -> Tuple[int, int]: return (self._start_node_id, self._end_node_id) def __str__(self) -> str: return "".join(( f"<{type(self).__name__}", f" id={self._id}", f" start_node_id={self._start_node_id}", f" end_node_id={self._end_node_id}", f" nodes={self._nodes}", f" type={self._type}", f" properties={self._properties}", ">", )) def save(self, db: "Memgraph") -> "Relationship": # noqa F821 """Saves a relationship to Memgraph. If relationship._id is not None it finds the relationship in Memgraph and updates it's properties with the values in `relationship`. If relationship._id is None, it creates a new relationship. If you want to set a relationship._id instead of creating a new relationship, use `load_relationship` first. """ relationship = db.save_relationship(self) for field in self.__fields__: setattr(self, field, getattr(relationship, field)) self._id = relationship._id return self def load(self, db: "Memgraph") -> "Relationship": # noqa F821 """Returns a relationship loaded from Memgraph. If the relationship._id is not None it fetches the relationship from Memgraph that has the same internal id. Otherwise it returns the relationship whose relationship._start_node_id and relationship._end_node_id and all relationship properties that are not None match the relationship in Memgraph. If there is no relationship like that in Memgraph, or if there are multiple relationships like that in Memgraph, throws GQLAlchemyError. """ relationship = db.load_relationship(self) for field in self.__fields__: setattr(self, field, getattr(relationship, field)) self._id = relationship._id return self
class ModeledDataParams(CalculateMixin): """Parameters for the "modeled" data series""" _weather_types = PrivateAttr((JobDataTypeEnum.actual_weather,)) _model_chain_method: str = PrivateAttr() def __init__(self, **data): super().__init__(**data) self._model_chain_method = MODEL_CHAIN_METHOD_MAP[self.irradiance_type]
def test_private_attribute_multiple_inheritance(): # We need to test this since PrivateAttr uses __slots__ and that has some restrictions with regards to # multiple inheritance default = {'a': {}} class GrandParentModel(BaseModel): __foo__ = PrivateAttr(default) class ParentAModel(GrandParentModel): pass class ParentBModel(GrandParentModel): __bar__ = PrivateAttr(default) class Model(ParentAModel, ParentBModel): __baz__ = PrivateAttr(default) assert GrandParentModel.__slots__ == {'__foo__'} assert ParentBModel.__slots__ == {'__bar__'} assert Model.__slots__ == {'__baz__'} assert repr( Model.__foo__) == "<member '__foo__' of 'GrandParentModel' objects>" assert repr( Model.__bar__) == "<member '__bar__' of 'ParentBModel' objects>" assert repr(Model.__baz__) == "<member '__baz__' of 'Model' objects>" assert Model.__private_attributes__ == { '__foo__': PrivateAttr(default), '__bar__': PrivateAttr(default), '__baz__': PrivateAttr(default), } m = Model() assert m.__foo__ == default assert m.__foo__ is not default assert m.__foo__['a'] is not default['a'] assert m.__bar__ == default assert m.__bar__ is not default assert m.__bar__['a'] is not default['a'] assert m.__baz__ == default assert m.__baz__ is not default assert m.__baz__['a'] is not default['a'] m.__foo__ = None assert m.__foo__ is None m.__bar__ = None assert m.__bar__ is None m.__baz__ = None assert m.__baz__ is None assert m.dict() == {} assert m.__dict__ == {}
class Message(BaseModel): _id: Optional[int] = PrivateAttr() counter: ClassVar[int] = 1 content: str = Field(min_length=1, max_length=160) _view_counter: Optional[int] = PrivateAttr(0) def add_view(self): self._view_counter += 1 def show_view_counter(self): return self._view_counter
class Port(BaseServiceIOModel): key: str = Field(..., regex=PROPERTY_KEY_RE) widget: Optional[Dict[str, Any]] = None default_value: Optional[DataItemValue] = Field(None, alias="defaultValue") value: Optional[DataItemValue] = None _py_value_type: Tuple[Type[ItemConcreteValue], ...] = PrivateAttr() _py_value_converter: Callable[[Any], ItemConcreteValue] = PrivateAttr() _node_ports = PrivateAttr() _used_default_value: bool = PrivateAttr(False) @validator("content_schema", always=True) @classmethod def valid_content_jsonschema(cls, v): if v is not None: try: jsonschema_validate_schema(schema=v) except jsonschema.SchemaError as err: raise ValueError( f"Invalid json-schema in 'content_schema': {err}") from err return v @validator("value", always=True) @classmethod def ensure_value(cls, v: DataItemValue, values: Dict[str, Any]) -> DataItemValue: if v is not None and (property_type := values.get("property_type")): if port_utils.is_file_type(property_type): if not isinstance(v, (FileLink, DownloadLink, PortLink)): raise ValueError( f"[{values['property_type']}] must follow " f"{FileLink.schema()}, {DownloadLink.schema()} or {PortLink.schema()}" ) elif property_type == "ref_contentSchema": try: content_schema = values["content_schema"] v = jsonschema_validate_data(instance=v, schema=content_schema, return_with_default=True) except jsonschema.ValidationError as err: raise ValueError( f"{v} invalid against content_schema: {err.message}" ) from err else: if isinstance(v, (list, dict)): # TODO: SEE https://github.com/ITISFoundation/osparc-simcore/issues/2849 raise ValueError( f"Containers as {v} currently only supported within content_schema" ) return v
class SleepTask(BaseModel): sleep: int _generator: Optional[Iterator] = PrivateAttr() _time_begin: dt.datetime = PrivateAttr(default_factory=dt.datetime.utcnow) _this_is_sleeptask = PrivateAttr(default=True) def allow(self) -> bool: return dt.datetime.utcnow() > self._time_begin + dt.timedelta( seconds=self.sleep) def get_task(self): return self._generator
class Job(SPIBase): # duplicated here to track without worrying about changes system_definition: PVSystem parameters: JobParametersType _data_items: Dict[Tuple[str, JobDataTypeEnum], JobDataItem] = PrivateAttr() _model_chain_method: Optional[str] = PrivateAttr() def __init__(self, **data): super().__init__(**data) self._data_items = self.parameters._construct_data_items(self.system_definition) # determine what columns to expect in uploads irradiance_type = getattr(self.parameters, "irradiance_type", None) self._model_chain_method = MODEL_CHAIN_METHOD_MAP.get(irradiance_type, None)
class StreamBase(BaseModel): key_id: str secret_key: str endpoint: str _ws: typing.Optional[WebSocketClientProtocol] = PrivateAttr(default=None) _handler: typing.Optional[_HandlerType] = PrivateAttr(default=None) def set_handler(self, handler: _HandlerType): self._handler = handler async def __connect(self): if self._ws is None: self._ws = await websockets.connect(self.endpoint) await self.auth() async def send(self, d: dict) -> typing.Union[dict, typing.List[dict]]: raise NotImplementedError async def auth(self): raise NotImplementedError async def __close(self): if self._ws is not None: await self._ws.close() self._ws = None async def pre_action(self): raise NotImplementedError async def consume(self): raise NotImplementedError async def run_forever(self): retries = 0 assert self._handler is not None while True: try: if self._ws is None: await self.__connect() await self.pre_action() await self.consume() except websockets.WebSocketException: await self.__close() retries += 1 if retries > 3: raise if retries > 1: await asyncio.sleep(3) finally: await asyncio.sleep(0.01)
class Outputer(BaseModel): path: str _output: TextIO = PrivateAttr() _comment_mark: str = PrivateAttr() _comment_indentation: int = PrivateAttr( ) # doesn't apply to the comment in output_header() def __init__(self, *args, comment_mark="#", comment_indentation=0, **kwargs): super().__init__(*args, **kwargs) self._output = open(self.path, "w") self._comment_mark = comment_mark self._comment_indentation = comment_indentation def __del__(self): self._output.close() def output_enum(self, enum: Enum, prefix="", assignment="=", suffix=""): for (i, value) in enumerate(enum.values): self._output.write(f"{prefix}{value} {assignment} {i}{suffix}\n") def output_comment(self, comment): indent = '\t' * self._comment_indentation self._output.write(f"\n{indent}{self._comment_mark} {comment}\n") def output_constant(self, constant: Constant, prefix="", assignment="=", suffix=""): if type(constant.value) == int: value = constant.value elif type(constant.value) == str: value = f'"{constant.value}"' else: raise Exception("Internal error - illegal constant type. %s", type(constant.value)) self._output.write( f"{prefix}{constant.name} {assignment} {value}{suffix}\n") def output_header(self): self._output.write( f"{self._comment_mark} autogenerated by reconstant - do not edit!\n" ) def output_footer(self): pass
class Tenant(TenantInCreate): _parent = PrivateAttr(f"projects/{settings.project_id}") name: Optional[ str] = None # project/project_id/tenant/tenant_id, You will receive it when tenant has been created. def create(self): tenant = talent.Tenant(**self.dict()) response = client_tenant.create_tenant(parent=self._parent, tenant=tenant) self.name = response.name @classmethod def get(cls, name): response = client_tenant.get_tenant(name=name) return cls.from_orm(response) def update(self): tenant = talent.Tenant(**self.dict()) _ = client_tenant.update_tenant(tenant=tenant) def delete(self): client_tenant.delete_tenant(name=self.name) self.name = None @classmethod def list(cls): return [ cls.from_orm(response) for response in client_tenant.list_tenants(parent=cls._parent) ]
def test_private_attribute_annotation(): class Model(BaseModel): """The best model""" __foo__: str class Config: underscore_attrs_are_private = True assert Model.__slots__ == {'__foo__'} assert repr(Model.__foo__) == "<member '__foo__' of 'Model' objects>" assert Model.__private_attributes__ == {'__foo__': PrivateAttr(Undefined)} assert repr(Model.__doc__) == "'The best model'" m = Model() with pytest.raises(AttributeError): m.__foo__ m.__foo__ = '123' assert m.__foo__ == '123' m.__foo__ = None assert m.__foo__ is None del m.__foo__ with pytest.raises(AttributeError): m.__foo__ m.__foo__ = '123' assert m.__foo__ == '123' assert m.dict() == {} assert m.__dict__ == {}
class EventDebugSettings(BaseSettings): """Parameters controlling how event debugging logs appear. To enable Event debugging: 1. pip install rich pydantic[dotenv] 2. export NAPARI_DEBUG_EVENTS=1 # or modify the .env_sample file 3. see .env_sample file for ways to set these fields here. """ # event emitters (e.g. 'Shapes') and event names (e.g. 'set_data') # to include/exclude when printing events. include_emitters: Set[str] = Field(default_factory=set) include_events: Set[str] = Field(default_factory=set) exclude_emitters: Set[str] = {'TransformChain', 'Context'} exclude_events: Set[str] = {'status', 'position'} # stack depth to show stack_depth: int = 20 # how many sub-emit nesting levels to show # (i.e. events that get triggered by other events) nesting_allowance: int = 0 _cur_depth: ClassVar[int] = PrivateAttr(0) class Config: env_prefix = 'event_debug_' env_file = '.env' if dotenv is not None else ''
class MessageBase(BaseModel): _callback: Optional[Callable] = PrivateAttr() def to_dict(self) -> dict: return json.loads(self.json()) async def send(self, sio, to: Optional[SioSID] = None, callback: Optional[Callable] = None): message_name = self.__class__.__name__[:-7].lower() self._callback = callback if to is None: logger.debug(f"Sending message {message_name}:\n{self!r}") await sio.emit(message_name, self.to_dict(), callback=self.callback) else: logger.debug( f"Sending message {message_name} to {to!r}:\n{self!r}") await sio.emit(message_name, self.to_dict(), room=to, callback=self.callback) async def callback(self, msgtype=None, *args): if msgtype == "fail": error = args[0] raise all_exceptions[error["error"]]( f"{error['details']!r},\nReceived in response to: {self!r}") elif self._callback: logger.debug(f"Recv Callback: msgtype={msgtype!r}, args={args!r}") await self._callback(*args)
class PredictedDataParams(CompareMixin): """Parameters for the "reference" data series""" data_available: PredictedDataEnum performance_granularity: Optional[ PerformanceGranularityEnum] # type: ignore _weather_types = PrivateAttr((JobDataTypeEnum.original_weather, )) def __init__(self, **data): super().__init__(**data) if self.data_available == PredictedDataEnum.weather_and_ac: self._performance_types = (JobDataTypeEnum.predicted_performance, ) elif self.data_available == PredictedDataEnum.weather_and_ac_and_dc: self._performance_types = ( JobDataTypeEnum.predicted_performance, JobDataTypeEnum.predicted_performance_dc, ) else: self._performance_types = () @root_validator def check_performance_granularity(cls, values): da = values.get("data_available") pg = values.get("performance_granularity") if da == PredictedDataEnum.weather_only and pg is not None: raise ValueError( "Performance granularity is invalid when not providing predicted " "performance") return values
class CalculateMixin(SPIBase): # in principle, these both could be on a per model chain/inverter basis, # but easier to just keep everything the same for the system irradiance_type: IrradianceTypeEnum temperature_type: TemperatureTypeEnum weather_granularity: WeatherGranularityEnum _weather_types: Tuple[JobDataTypeEnum, ...] = PrivateAttr() def _construct_data_items( self, system: PVSystem ) -> Dict[Tuple[str, JobDataTypeEnum], JobDataItem]: if self.weather_granularity == WeatherGranularityEnum.system: weather_paths = ["/"] elif self.weather_granularity == WeatherGranularityEnum.inverter: weather_paths = [ f"/inverters/{i}" for i in range(len(system.inverters)) ] elif self.weather_granularity == WeatherGranularityEnum.array: weather_paths = [ f"/inverters/{i}/arrays/{j}" for i, inv in enumerate(system.inverters) for j in range(len(inv.arrays)) ] out = {(wp, jt): JobDataItem.from_types( schema_path=wp, type_=jt, irradiance_type=getattr(self, "irradiance_type", None), temperature_type=getattr(self, "temperature_type", None), ) for jt in self._weather_types for wp in weather_paths} return out
class JobDataItem(SPIBase): schema_path: str = Field( ..., description= "Relative to PV system definition, i.e. /inverters/0/arrays/0") type: JobDataTypeEnum _data_cols: List[str] = PrivateAttr() @classmethod def from_types( cls, schema_path: str, type_: JobDataTypeEnum, irradiance_type: Optional[IrradianceTypeEnum] = None, temperature_type: Optional[TemperatureTypeEnum] = None, **kwargs, ): """Intialization that also sets _data_cols for ease of use later when adding data_columns to StoredJobDataMetadata""" cols = [ "time", ] if type_ in (JobDataTypeEnum.original_weather, JobDataTypeEnum.actual_weather): if irradiance_type == IrradianceTypeEnum.effective: cols += ["effective_irradiance"] elif irradiance_type == IrradianceTypeEnum.poa: cols += ["poa_global", "poa_direct", "poa_diffuse"] else: cols += ["ghi", "dni", "dhi"] if temperature_type == TemperatureTypeEnum.cell: cols += ["cell_temperature"] elif temperature_type == TemperatureTypeEnum.module: cols += ["module_temperature"] else: cols += ["temp_air", "wind_speed"] elif type_ in ( JobDataTypeEnum.predicted_performance, JobDataTypeEnum.actual_performance, JobDataTypeEnum.expected_performance, JobDataTypeEnum.predicted_performance_dc, ): cols += ["performance"] elif type_ in ( JobDataTypeEnum.monthly_actual_weather, JobDataTypeEnum.monthly_original_weather, ): cols = [ "month", "total_poa_insolation", "average_daytime_cell_temperature", ] elif type_ in ( JobDataTypeEnum.monthly_actual_performance, JobDataTypeEnum.monthly_original_performance, ): cols = ["month", "total_energy"] out = cls(schema_path=schema_path, type=type_, **kwargs) out._data_cols = cols return out
class Post(BaseModel): # pylint: disable=too-few-public-methods """ Model representing a post. """ # path to the post path: Path # the title of the post, stored in the "subject" header title: str # post creation timestamp, stored in the "date" header timestamp: datetime # all the headers from the post metadata: Dict[str, Any] # special metadata tags: Set[str] categories: Set[str] announcers: Set[str] enclosures: List[Enclosure] # a custom type will use a custom template type: str # relative URL to the post, without any extensions, eg ``first/index`` url: str # the Markdown contents of the file content: str # add a lock to manipulate the post file _lock: asyncio.Lock = PrivateAttr(default_factory=asyncio.Lock)
class SpacyLemmatization(TextCleaningFunction): _nlp: Language = PrivateAttr() model_name_or_path: Optional[str] = Field("en_core_web_sm") batch_size: int = 4 n_process: int = 1 def __init__(self, **data: Any): super().__init__(**data) try: self._nlp = spacy.load( self.model_name_or_path, disable=["parser", "ner"], ) except: download(self.model_name_or_path) self._nlp = spacy.load( self.model_name_or_path, disable=["parser", "ner"], ) def execute(self, tokens: List[str], **kwargs) -> List[str]: processed_tokens: List[str] = [] for doc in self._nlp.pipe(texts=tokens, batch_size=self.batch_size, n_process=self.n_process): processed_tokens.append(" ".join([token.lemma_ for token in doc])) return processed_tokens
class LimitedKmeansClustering(AbstractClusteringConfig): """ The kmeans implementation is backed by sklearn and for a more detailed description we refer the reader to the documentation of sklearn.cluster.KMeans (https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html). """ type: Literal["limited_kmeans"] = "limited_kmeans" init: Literal["k-means++", "random"] = Field( "k-means++", description=("The criterion to use in forming flat clusters. "), ) n_init: conint(gt=0, strict=True) = Field( 10, description=( "Number of time the k-means algorithm will be run with " "different centroid seeds. The final results will be the " "best output of n_init consecutive runs in terms of inertia." ), ) max_iter: conint(gt=0, strict=True) = Field( 300, description=( "Maximum number of iterations of the k-means algorithm for a single run." ), ) random_state: conint(gt=0, strict=True) = Field( None, description=( "Determines random number generation for centroid initialization. " "Use an int to make the randomness deterministic" ), ) _cluster_function: PyObject = PrivateAttr(kmeans_analysis)
class ModelTwo(BaseModel): __foo__ = PrivateAttr({'private'}) a: float b: int = 10 c: str = 'foobar' d: Model
class TranslationAnalyzer(BaseAnalyzer): _pipeline: Pipeline = PrivateAttr() TYPE: str = "Translation" model_name_or_path: str def __init__(self, **data: Any): super().__init__(**data) tokenizer = AutoTokenizer.from_pretrained(self.model_name_or_path) model = AutoModelForSeq2SeqLM.from_pretrained(self.model_name_or_path) self._pipeline = pipeline("translation", model=model, tokenizer=tokenizer, device=self._device_id) def analyze_input( self, source_response_list: List[AnalyzerRequest], **kwargs ) -> List[AnalyzerResponse]: responses = [] for source_response in source_response_list: translated_text = self._pipeline(source_response.processed_text) responses.append( AnalyzerResponse( processed_text=source_response.processed_text, meta=source_response.meta, source_name=source_response.source_name, segmented_data={"translated_text": translated_text[0]['translation_text']} ) ) return responses
class Item(Base): class Config: extra = Extra.forbid __inventory__: Optional["MutableInventory"] = PrivateAttr( default=None) # Link to the inventory id: ItemId = Field(alias="_id", default_factory=generate_item_id) tpl: TemplateId = Field(alias="_tpl") slot_id: Optional[str] = Field(alias="slotId") parent_id: Optional[ItemId] = Field(alias="parentId", default=None) location: Optional[AnyItemLocation] = None upd: ItemUpd = Field(default_factory=ItemUpd) def get_inventory(self) -> "MutableInventory": if self.__inventory__ is None: raise ValueError("Item does not have inventory") return self.__inventory__ # @root_validator(pre=False, skip_on_failure=True) # def validate_medkit_hp(cls, values: dict) -> dict: # pylint: disable=no-self-argument,no-self-use # if "id" not in values: # return values # # item_tpl_id: TemplateId = cast(TemplateId, values.get("tpl")) # item_template = tarkov.inventory.item_templates_repository.get_template(item_tpl_id) # if item_template.parent == "5448f39d4bdc2d0a728b4568": # Medkit Id # assert isinstance(item_template.props, MedsProps) # upd: ItemUpd = cast(ItemUpd, values.get("upd")) # if not isinstance(item_template.props.MaxHpResource, int): # raise ResourceWarning( # f"""Item template that inherits directly form MedKit does not have MaxHpResource property # template id: {item_template.id} # """ # ) # upd.MedKit = ( # upd.MedKit if upd.MedKit else ItemUpdMedKit(HpResource=item_template.props.MaxHpResource) # ) # # return values # # @root_validator(pre=False, skip_on_failure=True) # def validate_upd_none(cls, values: dict) -> dict: # pylint: disable=no-self-argument,no-self-use # if "upd" in values and values["upd"] is None: # values["upd"] = ItemUpd() # # return values def copy(self: Item, **kwargs: Any) -> Item: item_inventory = self.__inventory__ # Avoid copying inventory self.__inventory__ = None item_copy: Item = super().copy(**kwargs) self.__inventory__ = item_inventory return item_copy def __hash__(self) -> int: return hash(self.id)