Skip to content

Commit aef61f3

Browse files
authored
refactor: Make examples/topic_create.py modular and add docstrings (#513)
Signed-off-by: Muhammad Saad Sabir <[email protected]> Signed-off-by: Muhammad Saad Sabir <[email protected]>
1 parent 2983319 commit aef61f3

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
1010
- Unified balance and transfer logging format — both now consistently display values in hbars for clarity.
1111

1212
### Added
13+
- Refactored `examples/topic_create.py` into modular functions for better readability and reuse.
1314
- Add `examples/account_id.py` demonstrating AccountId class usage including creating standard AccountIds, parsing from strings, comparing instances, and creating AccountIds with public key aliases
1415

1516
- Added Google-style docstrings to `CustomFractionalFee` class and its methods in `custom_fractional_fee.py`.

examples/topic_create.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
import os
66
import sys
7+
from typing import Tuple
78
from dotenv import load_dotenv
89

910
from hiero_sdk_python import (
@@ -14,21 +15,44 @@
1415
Network,
1516
)
1617

18+
# Load environment variables from .env file
1719
load_dotenv()
1820

19-
# set up the Hedera client with operator credentials
20-
def setup_client():
21+
22+
def setup_client() -> Tuple[Client, PrivateKey]:
23+
"""
24+
Sets up and configures the Hiero client for the testnet.
25+
Reads OPERATOR_ID and OPERATOR_KEY from environment variables.
26+
"""
2127
network = Network(network='testnet')
2228
client = Client(network)
2329

24-
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
25-
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
30+
operator_id_str = os.getenv('OPERATOR_ID')
31+
operator_key_str = os.getenv('OPERATOR_KEY')
32+
33+
# Check if the environment variables are loaded correctly
34+
if not operator_id_str or not operator_key_str:
35+
print("Error: OPERATOR_ID or OPERATOR_KEY not found in environment.")
36+
print("Please create a .env file in the project's root directory with:")
37+
print("\nOPERATOR_ID=your_id_here")
38+
print("OPERATOR_KEY=your_key_here\n")
39+
sys.exit(1)
40+
41+
try:
42+
operator_id = AccountId.from_string(operator_id_str)
43+
operator_key = PrivateKey.from_string(operator_key_str)
44+
except (TypeError, ValueError) as e:
45+
print(f"Error: Invalid OPERATOR_ID or OPERATOR_KEY format: {e}")
46+
sys.exit(1)
2647

2748
client.set_operator(operator_id, operator_key)
2849
return client, operator_key
2950

30-
# create a new topic on Hedera network
31-
def create_topic(client, operator_key):
51+
52+
def create_topic(client: Client, operator_key: PrivateKey):
53+
"""
54+
Builds, signs, and executes a new topic creation transaction.
55+
"""
3256
transaction = (
3357
TopicCreateTransaction(
3458
memo="Python SDK created topic",
@@ -41,7 +65,7 @@ def create_topic(client, operator_key):
4165
try:
4266
receipt = transaction.execute(client)
4367
if receipt and receipt.topic_id:
44-
print(f"Topic created with ID: {receipt.topic_id}")
68+
print(f"Success! Topic created with ID: {receipt.topic_id}")
4569
else:
4670
print("Topic creation failed: Topic ID not returned in receipt.")
4771
sys.exit(1)
@@ -51,6 +75,9 @@ def create_topic(client, operator_key):
5175

5276

5377
def main():
78+
"""
79+
Main workflow to set up the client and create a new topic.
80+
"""
5481
client, operator_key = setup_client()
5582
create_topic(client, operator_key)
5683

0 commit comments

Comments
 (0)