add-function

द्वारा microsoft

लाइब्रेरी में नए फंक्शन जोड़ने के लिए मार्गदर्शिका। नए API रैपर या उपयोगिता फंक्शन लागू करते समय इसका उपयोग करें।

npx skills add https://github.com/microsoft/semantic-link-labs --skill add-function

Adding New Functions

This skill covers the workflow for adding new functions to the Semantic Link Labs library.

When to Use This Skill

Use this skill when you need to:

  • Add a new API wrapper function
  • Create a new utility function
  • Extend existing functionality with new features
  • Add functions to submodules (admin, report, lakehouse, etc.)

Function Categories

CategoryLocationPurpose
Top-level functionssrc/sempy_labs/_*.pyMain library exports
Admin functionssrc/sempy_labs/admin/Admin API operations
Report functionssrc/sempy_labs/report/Report operations
Lakehouse functionssrc/sempy_labs/lakehouse/Lakehouse operations
Direct Lake functionssrc/sempy_labs/directlake/Direct Lake model operations
TOM methodssrc/sempy_labs/tom/_model.pyTOMWrapper class methods

Step 0: Find the API Documentation

Before implementing an API wrapper, find the relevant API documentation:

# Use the API search tool
cd .claude/skills/rest-api-patterns/scripts
python search_public_api_doc.py "your search query"

# Examples:
python search_public_api_doc.py "workspace users" --source fabric
python search_public_api_doc.py "dataset refresh" --source powerbi

See the REST API Patterns skill for more details.


Step 1: Choose the Right Location

Top-Level Function

For general-purpose functions exported from sempy_labs:

# src/sempy_labs/_my_feature.py

Submodule Function

For functions belonging to a specific domain:

# src/sempy_labs/admin/_my_admin_function.py
# src/sempy_labs/lakehouse/_my_lakehouse_function.py
# src/sempy_labs/report/_my_report_function.py

Step 2: Create the Function

Required Imports

import pandas as pd
from typing import Optional, List
from uuid import UUID

# Logging decorator from sempy
from sempy._utils._log import log

# Helper functions
from sempy_labs._helper_functions import (
    resolve_workspace_name_and_id,
    resolve_workspace_id,
    _base_api,
    _create_dataframe,
)

# Icons for user messages
import sempy_labs._icons as icons

Function Template

@log
def my_new_function(
    item: str | UUID,
    workspace: Optional[str | UUID] = None,
    option: str = "default",
) -> pd.DataFrame:
    """
    Short description of what the function does.

    Extended description with more details about the function's behavior,
    use cases, and any important notes.

    This is a wrapper function for the following API: `API Name <https://learn.microsoft.com/rest/api/...>`_.

    Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).

    Parameters
    ----------
    item : str | uuid.UUID
        The name or ID of the item.
    workspace : str | uuid.UUID, default=None
        The Fabric workspace name or ID.
        Defaults to None which resolves to the workspace of the attached lakehouse
        or if no lakehouse attached, resolves to the workspace of the notebook.
    option : str, default="default"
        An option that controls function behavior.

    Returns
    -------
    pandas.DataFrame
        A pandas dataframe showing the results.
        Columns include: 'Column1', 'Column2', 'Column3'.

    Raises
    ------
    ValueError
        If the item does not exist.
    FabricHTTPException
        If the API request fails.
    """

    # Resolve workspace
    (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)

    # Define result DataFrame structure
    columns = {
        "Column1": "string",
        "Column2": "string",
        "Column3": "int",
    }
    df = _create_dataframe(columns=columns)

    # Make API call
    responses = _base_api(
        request=f"/v1/workspaces/{workspace_id}/items",
        uses_pagination=True,
        client="fabric_sp",
    )

    # Process responses
    rows = []
    for r in responses:
        for item in r.get("value", []):
            rows.append({
                "Column1": item.get("id"),
                "Column2": item.get("name"),
                "Column3": item.get("count", 0),
            })

    if rows:
        df = pd.DataFrame(rows)

    return df

Step 3: Export the Function

From Module File

Add to the module's __init__.py:

# src/sempy_labs/admin/__init__.py (example for admin submodule)

from ._my_admin_function import my_new_function

__all__ = [
    ...,
    "my_new_function",
]

From Main Package

For top-level functions, add to src/sempy_labs/__init__.py:

from ._my_feature import my_new_function

__all__ = [
    ...,
    "my_new_function",
]

Common Patterns

Functions That Modify Resources

@log
def create_item(
    name: str,
    workspace: Optional[str | UUID] = None,
) -> None:
    """
    Creates a new item.
    ...
    """
    (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)

    payload = {
        "displayName": name,
    }

    _base_api(
        request=f"/v1/workspaces/{workspace_id}/items",
        method="post",
        payload=payload,
        status_codes=[201, 202],
        client="fabric_sp",
    )

    print(
        f"{icons.green_dot} The '{name}' item has been successfully created "
        f"in the '{workspace_name}' workspace."
    )

Functions That Delete Resources

@log
def delete_item(
    item: str | UUID,
    workspace: Optional[str | UUID] = None,
) -> None:
    """
    Deletes an item.
    ...
    """
    (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
    item_id = resolve_item_id(item=item, type="ItemType", workspace=workspace_id)

    _base_api(
        request=f"/v1/workspaces/{workspace_id}/items/{item_id}",
        method="delete",
        client="fabric_sp",
    )

    print(
        f"{icons.green_dot} The item has been successfully deleted "
        f"from the '{workspace_name}' workspace."
    )

Functions With Long-Running Operations

@log
def long_running_operation(
    item: str | UUID,
    workspace: Optional[str | UUID] = None,
) -> dict:
    """
    Performs a long-running operation.
    ...
    """
    workspace_id = resolve_workspace_id(workspace)
    item_id = resolve_item_id(item=item, type="ItemType", workspace=workspace_id)

    # lro_return_json handles polling for completion
    result = _base_api(
        request=f"/v1/workspaces/{workspace_id}/items/{item_id}/operation",
        method="post",
        lro_return_json=True,
        client="fabric_sp",
    )

    return result

Step 4: Add Tests

Create tests for the new function:

# tests/test_my_feature.py

import pytest
import pandas as pd


def test_my_new_function_returns_dataframe():
    """Test that my_new_function returns a DataFrame."""
    from sempy_labs import my_new_function

    # This might require mocking for unit tests
    result = my_new_function()

    assert isinstance(result, pd.DataFrame)


def test_my_new_function_with_workspace():
    """Test my_new_function with specific workspace."""
    from sempy_labs import my_new_function

    result = my_new_function(workspace="Test Workspace")

    assert isinstance(result, pd.DataFrame)

Step 5: Document the Function

Ensure the docstring follows numpydoc style:

  1. ✅ Short description (one line)
  2. ✅ Extended description (if needed)
  3. ✅ API reference link (for wrapper functions)
  4. ✅ Service Principal note (if supported)
  5. ✅ All parameters documented with types
  6. ✅ Return value documented
  7. ✅ Exceptions documented (if applicable)

Checklist Before Committing

  • Function follows naming conventions (list_, get_, create_, etc.)
  • @log decorator is applied
  • Complete docstring with numpydoc style
  • Type hints for all parameters and return value
  • Uses standard helper functions (_base_api, resolve_*, etc.)
  • Function exported in __init__.py
  • Tests written for the new function
  • Code formatted with black
  • No linting errors
  • Documentation builds without warnings

Example: Complete New Function

See _workspaces.py for well-implemented examples:

  • list_workspace_users — List function returning DataFrame
  • update_workspace_user — Update function with parameters
  • delete_user_from_workspace — Delete function with confirmation message

API Documentation Resources

When wrapping REST APIs, reference the official documentation:

APIDocumentation
Fabric Core APIhttps://learn.microsoft.com/rest/api/fabric/core/
Fabric Admin APIhttps://learn.microsoft.com/rest/api/fabric/admin/
Power BI REST APIhttps://learn.microsoft.com/rest/api/power-bi/
Azure Management APIhttps://learn.microsoft.com/rest/api/resources/

microsoft की और Skills

oss-growth
microsoft
OSS ग्रोथ हैकर व्यक्तित्व
official
microsoft-foundry
microsoft
Foundry एजेंटों को एंड-टू-एंड डिप्लॉय, मूल्यांकन और प्रबंधित करें: Docker बिल्ड, ACR पुश, होस्टेड/प्रॉम्प्ट एजेंट क्रिएट, कंटेनर स्टार्ट, बैच इवैल्यूएशन, कंटीन्यूअस इवैल्यूएशन, प्रॉम्प्ट ऑप्टिमाइज़र वर्कफ़्लो, agent.yaml, ट्रेस से डेटासेट क्यूरेशन। इसका उपयोग करें: Foundry पर एजेंट डिप्लॉय करना, होस्टेड एजेंट, एजेंट बनाना, एजेंट को इनवोक करना, एजेंट का मूल्यांकन
officialdevelopmentdevops
azure-ai
microsoft
Azure AI के लिए उपयोग करें: खोज, वाक्, OpenAI, दस्तावेज़ बुद्धिमत्ता। खोज, वेक्टर/हाइब्रिड खोज, वाक्-से-पाठ, पाठ-से-वाक्, प्रतिलेखन, OCR में सहायता करता है। कब उपयोग करें: AI खोज, क्वेरी खोज, वेक्टर खोज, हाइब्रिड खोज, सिमैंटिक खोज, वाक्-से-पाठ, पाठ-से-वाक्, प्रतिलेखन, OCR, पाठ को वाक् में बदलना।
officialdevelopmentapi
azure-deploy
microsoft
पहले से तैयार एप्लिकेशनों के लिए Azure डिप्लॉयमेंट निष्पादित करें जिनमें मौजूदा .azure/deployment-plan.md और इंफ्रास्ट्रक्चर फ़ाइलें हों। इस स्किल का उपयोग तब न करें जब उपयोगकर्ता कोई नया एप्लिकेशन बनाने के लिए कहे — इसके बजाय azure-prepare का उपयोग करें। यह स्किल azd up, azd deploy, terraform apply, और az deployment कमांड को बिल्ट-इन एरर रिकवरी के साथ चलाती है। इसके लिए azure-prepare से .azure/deployment-plan.md और azure-validate से सत्यापित स्थिति आवश्यक है। कब: "azd
officialdevopsaws
azure-storage
microsoft
Azure Storage सेवाएँ जिनमें Blob Storage, File Shares, Queue Storage, Table Storage और Data Lake शामिल हैं। स्टोरेज एक्सेस टियर (हॉट, कूल, कोल्ड, आर्काइव), प्रत्येक टियर का उपयोग कब करें और टियर तुलना के बारे में प्रश्नों के उत्तर देता है। ऑब्जेक्ट स्टोरेज, SMB फ़ाइल शेयर, एसिंक्रोनस मैसेजिंग, NoSQL की-वैल्यू और बिग डेटा एनालिटिक्स प्रदान करता है। लाइफसाइकिल प्रबंधन शामिल है। उपयोग करें: ब्लॉब स्टोरेज, फ़ाइल शेयर, क्य
officialdevelopmentdatabase
azure-diagnostics
microsoft
Azure पर AppLens, Azure Monitor, संसाधन स्वास्थ्य और सुरक्षित ट्राइएज का उपयोग करके Azure उत्पादन समस्याओं को डीबग करें। कब: उत्पादन समस्याओं को डीबग करना, ऐप सेवा समस्या निवारण, ऐप सेवा उच्च CPU, ऐप सेवा परिनियोजन विफलता, कंटेनर ऐप्स समस्या निवारण, फंक्शन्स समस्या निवारण, AKS समस्या निवारण, kubectl कनेक्ट नहीं हो सकता, kube-system/CoreDNS विफलताएँ, पॉड लंबित, क्रैशलूप, नोड तैयार नहीं, अपग्रेड विफ
officialdevopsdevelopment
azure-prepare
microsoft
Azure ऐप्स को तैनाती के लिए तैयार करें (infra Bicep/Terraform, azure.yaml, Dockerfiles)। निर्माण/आधुनिकीकरण या निर्माण+तैनाती के लिए उपयोग करें; क्रॉस-क्लाउड माइग्रेशन के लिए नहीं (azure-cloud-migrate का उपयोग करें)। इसका उपयोग न करें: copilot-sdk ऐप्स के लिए (azure-hosted-copilot-sdk का उपयोग करें)। कब: "create app", "build web app", "create API", "create serverless HTTP API", "create frontend", "create back end", "build a service", "modernize application", "update application", "add authentication", "add caching", "host on Azure", "create and...
officialdevelopmentdevops
azure-validate
microsoft
Azure तैनाती-पूर्व तत्परता के लिए सत्यापन। तैनाती से पहले कॉन्फ़िगरेशन, इंफ्रास्ट्रक्चर (Bicep या Terraform), RBAC भूमिका असाइनमेंट, प्रबंधित पहचान अनुमतियाँ और पूर्वापेक्षाओं की गहन जाँच करें। कब: मेरे ऐप को सत्यापित करें, तैनाती तत्परता की जाँच करें, प्रीफ्लाइट जाँच चलाएँ, कॉन्फ़िगरेशन सत्यापित करें, तैनाती के लिए तैयार है या नहीं जाँचें, azure.yaml सत्यापित करें, Bicep सत्यापित
officialdevopstesting