Skip to content

Commit

Permalink
feat: introduce 2 new ways to define the DEFAULT_PROJECT (#85)
Browse files Browse the repository at this point in the history
* feat: introduce 2 new ways to define the DEFAULT_PROJECT

* fix(linting): code formatting

---------

Co-authored-by: Azory YData Bot <[email protected]>
  • Loading branch information
portellaa and azory-ydata authored Jan 18, 2024
1 parent 7354ce2 commit acfcddd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/ydata/sdk/common/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Client(metaclass=SingletonClient):

codes = codes

DEFAULT_PROJECT: Optional[Project] = environ.get("DEFAULT_PROJECT", None)

def __init__(self, credentials: Optional[Union[str, Dict]] = None, project: Optional[Project] = None, set_as_global: bool = False):
self._base_url = environ.get("YDATA_BASE_URL", DEFAULT_URL)
self._scheme = 'https'
Expand All @@ -56,10 +58,19 @@ def __init__(self, credentials: Optional[Union[str, Dict]] = None, project: Opti

self._handshake()

self._default_project = project or self._get_default_project(credentials)
self._default_project = project or Client.DEFAULT_PROJECT or self._get_default_project(
credentials)
if set_as_global:
self.__set_global()

@property
def project(self) -> Project:
return Client.DEFAULT_PROJECT or self._default_project

@project.setter
def project(self, value: Project):
self._default_project = value

def post(
self, endpoint: str, data: Optional[Dict] = None, json: Optional[Dict] = None,
project: Optional[Project] = None, files: Optional[Dict] = None, raise_for_status: bool = True
Expand Down
4 changes: 4 additions & 0 deletions src/ydata/sdk/connectors/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def uid(self) -> UID:
def type(self) -> ConnectorType:
return self._model.type

@property
def project(self) -> Project:
return self._project or self._client.project

@staticmethod
@init_client
def get(uid: UID, project: Optional[Project] = None, client: Optional[Client] = None) -> "Connector":
Expand Down
4 changes: 4 additions & 0 deletions src/ydata/sdk/datasources/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def uid(self) -> UID:
def datatype(self) -> DataSourceType:
return self._model.datatype

@property
def project(self) -> Project:
return self._project or self._client.project

@property
def status(self) -> Status:
try:
Expand Down
3 changes: 2 additions & 1 deletion src/ydata/sdk/synthesizers/multitable.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class MultiTableSynthesizer(BaseSynthesizer):
The synthesizer instance is created in the backend only when the `fit` method is called.
Arguments:
write_connector (UID): Connector of type RDBMS to be used to write the samples
write_connector (UID | Connector): Connector of type RDBMS to be used to write the samples
uid (UID): (optional) UID to identify this synthesizer
name (str): (optional) Name to be used when creating the synthesizer. Calculated internally if not provided
client (Client): (optional) Client to connect to the backend
"""
Expand Down
4 changes: 4 additions & 0 deletions src/ydata/sdk/synthesizers/synthesizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def _init_common(self, client: Optional[Client] = None):
self._client = client
self._logger = create_logger(__name__, level=LOG_LEVEL)

@property
def project(self) -> Project:
return self._project or self._client.project

def fit(self, X: Union[DataSource, pdDataFrame],
privacy_level: PrivacyLevel = PrivacyLevel.HIGH_FIDELITY,
datatype: Optional[Union[DataSourceType, str]] = None,
Expand Down

0 comments on commit acfcddd

Please sign in to comment.