μ½˜ν…μΈ λ‘œ κ±΄λ„ˆλ›°κΈ°

μ°Έμ‘° hub_sdk/modules/users.py

μ°Έκ³ 

이 νŒŒμΌμ€ https://github.com/ultralytics/ hub-sdk/blob/main/ hub_sdk/modules/users .pyμ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. 문제λ₯Ό λ°œκ²¬ν•˜λ©΄ ν’€ λ¦¬ν€˜μŠ€νŠΈ (πŸ› οΈ)λ₯Ό μ œμΆœν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν•˜λŠ” 데 도움을 μ£Όμ„Έμš”. κ°μ‚¬ν•©λ‹ˆλ‹€ πŸ™!



hub_sdk.modules.users.Users

기지: CRUDClient

CRUD 연산을 톡해 μ‚¬μš©μžμ™€ μƒν˜Έ μž‘μš©ν•˜κΈ° μœ„ν•œ ν΄λΌμ΄μ–ΈνŠΈλ₯Ό λ‚˜νƒ€λ‚΄λŠ” ν΄λž˜μŠ€μž…λ‹ˆλ‹€. 이 ν΄λž˜μŠ€λŠ” CRUDClient 클래슀λ₯Ό ν™•μž₯ν•˜κ³  μ‚¬μš©μžμ™€ μž‘μ—…ν•˜κΈ° μœ„ν•œ νŠΉμ • λ©”μ„œλ“œλ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€.

속성:

이름 μœ ν˜• μ„€λͺ…
id (str, None)

μ‚¬μš©μžμ˜ 고유 μ‹λ³„μž(μ‚¬μš© κ°€λŠ₯ν•œ 경우)μž…λ‹ˆλ‹€.

data dict

μ‚¬μš©μž 데이터λ₯Ό μ €μž₯ν•˜λŠ” μ‚¬μ „μž…λ‹ˆλ‹€.

μ°Έκ³ 

'id' 속성은 μ΄ˆκΈ°ν™” 쀑에 μ„€μ •λ˜λ©° μ‚¬μš©μžλ₯Ό κ³ μœ ν•˜κ²Œ μ‹λ³„ν•˜λŠ” 데 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. '데이터' 속성은 APIμ—μ„œ κ°€μ Έμ˜¨ μ‚¬μš©μž 데이터λ₯Ό μ €μž₯ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ hub_sdk/modules/users.py
class Users(CRUDClient):
    """
    A class representing a client for interacting with Users through CRUD operations. This class extends the CRUDClient
    class and provides specific methods for working with Users.

    Attributes:
        id (str, None): The unique identifier of the user, if available.
        data (dict): A dictionary to store user data.

    Note:
        The 'id' attribute is set during initialization and can be used to uniquely identify a user.
        The 'data' attribute is used to store user data fetched from the API.
    """

    def __init__(self, user_id: Optional[str] = None, headers: Optional[Dict[str, Any]] = None) -> None:
        """
        Initialize a Users object for interacting with user data via CRUD operations.

        Args:
            user_id (str, optional): The unique identifier of the user.
            headers (dict, optional): A dictionary of HTTP headers to be included in API requests.
        """
        super().__init__("users", "user", headers)
        self.id = user_id
        self.data = {}
        if user_id:
            self.get_data()

    def get_data(self) -> None:
        """
        Retrieves data for the current user instance.

        If a valid user ID has been set, it sends a request to fetch the user data and stores it in the instance.
        If no user ID has been set, it logs an error message.

        Returns:
            (None): The method does not return a value.
        """
        if self.id:
            resp = super().read(self.id).json()
            self.data = resp.get("data", {})
            self.logger.debug("user id is %s", self.id)
        else:
            self.logger.error("No user id has been set. Update the user id or create a user.")

    def create_user(self, user_data: dict) -> None:
        """
        Creates a new user with the provided data and sets the user ID for the current instance.

        Args:
            user_data (dict): A dictionary containing the data for creating the user.

        Returns:
            (None): The method does not return a value.
        """
        resp = super().create(user_data).json()
        self.id = resp.get("data", {}).get("id")
        self.get_data()

    def delete(self, hard: bool = False) -> Optional[Response]:
        """
        Delete the user resource represented by this instance.

        Args:
            hard (bool, optional): If True, perform a hard delete.

        Note:
            The 'hard' parameter determines whether to perform a soft delete (default) or a hard delete.
            In a soft delete, the model might be marked as deleted but retained in the system.
            In a hard delete, the model is permanently removed from the system.

        Returns:
            (Optional[Response]): Response object from the delete request, or None if delete fails
        """
        return super().delete(self.id, hard)

    def update(self, data: dict) -> Optional[Response]:
        """
        Update the user resource represented by this instance.

        Args:
            data (dict): The updated data for the user resource.

        Returns:
            (Optional[Response]): Response object from the update request, or None if update fails
        """
        return super().update(self.id, data)

__init__(user_id=None, headers=None)

CRUD μž‘μ—…μ„ 톡해 μ‚¬μš©μž 데이터와 μƒν˜Έ μž‘μš©ν•  수 μžˆλ„λ‘ μ‚¬μš©μž 개체λ₯Ό μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€.

λ§€κ°œλ³€μˆ˜:

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
user_id str

μ‚¬μš©μžμ˜ 고유 μ‹λ³„μžμž…λ‹ˆλ‹€.

None
headers dict

API μš”μ²­μ— 포함될 HTTP ν—€λ”μ˜ μ‚¬μ „μž…λ‹ˆλ‹€.

None
의 μ†ŒμŠ€ μ½”λ“œ hub_sdk/modules/users.py
def __init__(self, user_id: Optional[str] = None, headers: Optional[Dict[str, Any]] = None) -> None:
    """
    Initialize a Users object for interacting with user data via CRUD operations.

    Args:
        user_id (str, optional): The unique identifier of the user.
        headers (dict, optional): A dictionary of HTTP headers to be included in API requests.
    """
    super().__init__("users", "user", headers)
    self.id = user_id
    self.data = {}
    if user_id:
        self.get_data()

create_user(user_data)

제곡된 λ°μ΄ν„°λ‘œ μƒˆ μ‚¬μš©μžλ₯Ό μƒμ„±ν•˜κ³  ν˜„μž¬ μΈμŠ€ν„΄μŠ€μ— λŒ€ν•œ μ‚¬μš©μž IDλ₯Ό μ„€μ •ν•©λ‹ˆλ‹€.

λ§€κ°œλ³€μˆ˜:

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
user_data dict

μ‚¬μš©μž 생성을 μœ„ν•œ 데이터가 ν¬ν•¨λœ μ‚¬μ „μž…λ‹ˆλ‹€.

ν•„μˆ˜

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
None

이 λ©”μ„œλ“œλŠ” 값을 λ°˜ν™˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ hub_sdk/modules/users.py
def create_user(self, user_data: dict) -> None:
    """
    Creates a new user with the provided data and sets the user ID for the current instance.

    Args:
        user_data (dict): A dictionary containing the data for creating the user.

    Returns:
        (None): The method does not return a value.
    """
    resp = super().create(user_data).json()
    self.id = resp.get("data", {}).get("id")
    self.get_data()

delete(hard=False)

이 μΈμŠ€ν„΄μŠ€κ°€ λ‚˜νƒ€λ‚΄λŠ” μ‚¬μš©μž λ¦¬μ†ŒμŠ€λ₯Ό μ‚­μ œν•©λ‹ˆλ‹€.

λ§€κ°œλ³€μˆ˜:

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
hard bool

True이면 ν•˜λ“œ μ‚­μ œλ₯Ό μˆ˜ν–‰ν•©λ‹ˆλ‹€.

False
μ°Έκ³ 

'hard' λ§€κ°œλ³€μˆ˜λŠ” μ†Œν”„νŠΈ μ‚­μ œ(κΈ°λ³Έκ°’)λ₯Ό μˆ˜ν–‰ν• μ§€, μ•„λ‹ˆλ©΄ ν•˜λ“œ μ‚­μ œλ₯Ό μˆ˜ν–‰ν• μ§€λ₯Ό κ²°μ •ν•©λ‹ˆλ‹€. μ†Œν”„νŠΈ μ‚­μ œμ˜ 경우 λͺ¨λΈμ€ μ‚­μ œλœ κ²ƒμœΌλ‘œ ν‘œμ‹œλ˜μ§€λ§Œ μ‹œμŠ€ν…œμ—λŠ” μœ μ§€λ  수 μžˆμŠ΅λ‹ˆλ‹€. ν•˜λ“œ μ‚­μ œμ—μ„œλŠ” λͺ¨λΈμ΄ μ‹œμŠ€ν…œμ—μ„œ 영ꡬ적으둜 μ œκ±°λ©λ‹ˆλ‹€.

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
Optional[Response]

μ‚­μ œ μš”μ²­μ˜ 응닡 객체, μ‚­μ œμ— μ‹€νŒ¨ν•œ 경우 μ—†μŒ

의 μ†ŒμŠ€ μ½”λ“œ hub_sdk/modules/users.py
def delete(self, hard: bool = False) -> Optional[Response]:
    """
    Delete the user resource represented by this instance.

    Args:
        hard (bool, optional): If True, perform a hard delete.

    Note:
        The 'hard' parameter determines whether to perform a soft delete (default) or a hard delete.
        In a soft delete, the model might be marked as deleted but retained in the system.
        In a hard delete, the model is permanently removed from the system.

    Returns:
        (Optional[Response]): Response object from the delete request, or None if delete fails
    """
    return super().delete(self.id, hard)

get_data()

ν˜„μž¬ μ‚¬μš©μž μΈμŠ€ν„΄μŠ€μ— λŒ€ν•œ 데이터λ₯Ό κ²€μƒ‰ν•©λ‹ˆλ‹€.

μœ νš¨ν•œ μ‚¬μš©μž IDκ°€ μ„€μ •λ˜μ–΄ 있으면 μ‚¬μš©μž 데이터 κ°€μ Έμ˜€κΈ° μš”μ²­μ„ μ „μ†‘ν•˜μ—¬ μΈμŠ€ν„΄μŠ€μ— μ €μž₯ν•©λ‹ˆλ‹€. μ‚¬μš©μž IDκ°€ μ„€μ •λ˜μ§€ μ•Šμ€ 경우 였λ₯˜ λ©”μ‹œμ§€λ₯Ό κΈ°λ‘ν•©λ‹ˆλ‹€.

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
None

이 λ©”μ„œλ“œλŠ” 값을 λ°˜ν™˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ hub_sdk/modules/users.py
def get_data(self) -> None:
    """
    Retrieves data for the current user instance.

    If a valid user ID has been set, it sends a request to fetch the user data and stores it in the instance.
    If no user ID has been set, it logs an error message.

    Returns:
        (None): The method does not return a value.
    """
    if self.id:
        resp = super().read(self.id).json()
        self.data = resp.get("data", {})
        self.logger.debug("user id is %s", self.id)
    else:
        self.logger.error("No user id has been set. Update the user id or create a user.")

update(data)

이 μΈμŠ€ν„΄μŠ€κ°€ λ‚˜νƒ€λ‚΄λŠ” μ‚¬μš©μž λ¦¬μ†ŒμŠ€λ₯Ό μ—…λ°μ΄νŠΈν•©λ‹ˆλ‹€.

λ§€κ°œλ³€μˆ˜:

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
data dict

μ‚¬μš©μž λ¦¬μ†ŒμŠ€μ— λŒ€ν•œ μ—…λ°μ΄νŠΈλœ λ°μ΄ν„°μž…λ‹ˆλ‹€.

ν•„μˆ˜

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
Optional[Response]

μ—…λ°μ΄νŠΈ μš”μ²­μ˜ 응닡 객체, λ˜λŠ” μ—…λ°μ΄νŠΈμ— μ‹€νŒ¨ν•œ 경우 None을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ hub_sdk/modules/users.py
def update(self, data: dict) -> Optional[Response]:
    """
    Update the user resource represented by this instance.

    Args:
        data (dict): The updated data for the user resource.

    Returns:
        (Optional[Response]): Response object from the update request, or None if update fails
    """
    return super().update(self.id, data)