Detailed Example Walkthrough
This guide walks through a complete example of using the sorterpy SDK to create an alphabet sorting system. We’ll explore how the SDK interfaces with the sorter API and examine important implementation details.
Setup and Initialization
The example begins with setting up the Sorter client:
from sorterpy.sorterpy import Sorter
import os
from pathlib import Path
from dotenv import load_dotenv
dotenv_path = Path(__file__).parent.parent / '.env'
load_dotenv(dotenv_path)
sorter = Sorter(
api_key=os.getenv('SORT_API_KEY', 'your-api-key'),
base_url=os.getenv('SORT_BASE_URL', 'https://sorter.social'),
options={
"vote_magnitude": "positive", # Note: This option is deprecated
"verbose": True,
"quiet": False
}
)
API Connection Details
The initialization process establishes a connection to the sorter API:
The
api_keyauthenticates your requestsbase_urldetermines the API endpoint (production or staging)Options configure the client behavior
Voting Implementation
The example implements a custom voting system based on letter distance:
def letter_distance(a, b):
raw_score = (ord(a.name) - ord(b.name)) * (50 / 25)
return int(raw_score + 50) # Convert to 0,100 range
Voting Process
When a vote is made:
The SDK gets a pair of items using
tag.rankings().pair()Calculates the vote magnitude using
letter_distance()Submits the vote through the API
left, right = tag.rankings().pair()
score = letter_distance(left, right)
sorter.vote(left, right, int(score))
Note: The multiple parameter orderings for the vote method (e.g., vote(left, score, right) vs vote(left, right, score)) are deprecated and will be removed in a future version.
Working with Rankings
The example shows how to retrieve and work with rankings:
# Get current sorted order
sorted_items = tag.sorted()
current_order = "".join(item.name for item in sorted_items)
# Get unsorted items
unsorted_items = tag.unsorted()
These operations map to:
GET
/api/v2/tags/{tag_id}/rankingsfor overall rankingsGET
/api/v2/tags/{tag_id}/items/sortedfor sorted itemsGET
/api/v2/tags/{tag_id}/items/unsortedfor unsorted items
Attribute Voting
The example demonstrates attribute-based voting:
quality_attr = sorter.attribute("quality", "How good is this letter")
tag.vote(left, right, 25, attribute=quality_attr)
This creates a new dimension for voting:
Creates or retrieves the attribute via
/api/v2/attributesAssociates votes with this attribute
Allows for multiple ranking dimensions on the same items
Runtime Configuration
The example shows how to modify client behavior at runtime:
sorter.options(vote_magnitude="positive", verbose=False)
tag.vote(left, right, 75)
Note: The vote_magnitude option is deprecated and will be removed in a future version. All votes will use a standardized scale.
URL Generation
The example demonstrates generating shareable URLs:
tag_link = tag.link()
item_link = item_a.link()
These methods construct URLs that map to the sorter web interface, allowing for easy sharing and viewing of tags and items.