Skip to content

Commit 638ee7e

Browse files
authored
refactor: modularize file_append example into setup, create, and appe… (#557)
Signed-off-by: tobi <onatadetobi212@gmail.com> Signed-off-by: Tobi Onatade <onatadetobi212@gmail.com>
1 parent 48b4f3d commit 638ee7e

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
3434
- Added "One Issue Per Pull Request" section to `examples/sdk_developers/common_issues.md`.
3535
- docs: Improved the contributing section in the README.md file
3636
- Refactored `examples/transfer_nft.py` to be more modular by isolating transfer logic.
37+
- Refactored `examples/file_append.py` into modular functions for better readability, reuse, and consistency across examples.
38+
- Ensured identical runtime behavior and output to the previous version to maintain backward compatibility.
3739
- Renamed `examples/hbar_allowance.py` to `examples/account_allowance_hbar.py` for naming consistency
3840
- Converted monolithic function in `token_create_nft_infinite.py` to multiple modular functions for better structure and ease.
3941
- docs: Use relative paths for internal GitHub links (#560).

examples/file_append.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,8 @@ def setup_client():
3636

3737
return client
3838

39-
def file_append():
40-
"""
41-
Demonstrates appending content to a file on the network by:
42-
1. Setting up client with operator account
43-
2. Creating a file with initial content
44-
3. Appending additional content to the file
45-
"""
46-
client = setup_client()
47-
48-
file_private_key = PrivateKey.generate_ed25519()
49-
50-
# Step 1: Create a file with initial content
39+
def create_file(client, file_private_key):
40+
"""Create a file with initial content"""
5141
print("Creating file with initial content...")
5242
create_receipt = (
5343
FileCreateTransaction()
@@ -66,7 +56,10 @@ def file_append():
6656
file_id = create_receipt.file_id
6757
print(f"File created successfully with ID: {file_id}")
6858

69-
# Step 2: Append content to the file (single chunk)
59+
return file_id
60+
61+
def append_file_single(client, file_id, file_private_key):
62+
"""Append content to the file (single chunk)"""
7063
print("\nAppending content to file (single chunk)...")
7164
append_receipt = (
7265
FileAppendTransaction()
@@ -82,8 +75,9 @@ def file_append():
8275
sys.exit(1)
8376

8477
print("Content appended successfully!")
85-
86-
# Step 3: Append large content (multi-chunk)
78+
79+
def append_file_large(client, file_id, file_private_key):
80+
"""Append large content to the file (multi-chunk)"""
8781
print("\nAppending large content (multi-chunk)...")
8882
large_content = b"Large content that will be split into multiple chunks. " * 100
8983

@@ -105,5 +99,25 @@ def file_append():
10599
print("Large content appended successfully!")
106100
print(f"Total chunks used: {FileAppendTransaction().set_contents(large_content).get_required_chunks()}")
107101

102+
def main():
103+
"""
104+
Demonstrates appending content to a file on the network by:
105+
1. Setting up client with operator account
106+
2. Creating a file with initial content
107+
3. Appending additional content to the file
108+
"""
109+
client = setup_client()
110+
111+
file_private_key = PrivateKey.generate_ed25519()
112+
113+
# Step 1: Create a file with initial content
114+
file_id = create_file(client, file_private_key)
115+
116+
# Step 2: Append content to the file (single chunk)
117+
append_file_single(client, file_id, file_private_key)
118+
119+
# Step 3: Append large content (multi-chunk)
120+
append_file_large(client, file_id, file_private_key)
121+
108122
if __name__ == "__main__":
109-
file_append()
123+
main()

0 commit comments

Comments
 (0)