Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/08_directory_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ def main():
# Create directory
fs.mkdir("/tmp/my_project")

# Create nested directories
fs.mkdir("/tmp/my_project/src/utils", recursive=True)
# Create nested directories (API creates parents automatically)
fs.mkdir("/tmp/my_project/src/utils")

# List directory
contents = fs.list_dir("/tmp/my_project")
print(f"Contents: {contents}")

# Create project structure
fs.mkdir("/tmp/my_project/src", recursive=True)
fs.mkdir("/tmp/my_project/tests", recursive=True)
fs.mkdir("/tmp/my_project/src")
fs.mkdir("/tmp/my_project/tests")
fs.write_file("/tmp/my_project/src/main.py", "print('Hello')")
fs.write_file("/tmp/my_project/README.md", "# My Project")

Expand Down
8 changes: 4 additions & 4 deletions examples/08_directory_operations_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ async def main():
# Create directory
await fs.mkdir("/tmp/my_project")

# Create nested directories
await fs.mkdir("/tmp/my_project/src/utils", recursive=True)
# Create nested directories (API creates parents automatically)
await fs.mkdir("/tmp/my_project/src/utils")

# List directory
contents = await fs.list_dir("/tmp/my_project")
print(f"Contents: {contents}")

# Create project structure
await fs.mkdir("/tmp/my_project/src", recursive=True)
await fs.mkdir("/tmp/my_project/tests", recursive=True)
await fs.mkdir("/tmp/my_project/src")
await fs.mkdir("/tmp/my_project/tests")
await fs.write_file("/tmp/my_project/src/main.py", "print('Hello')")
await fs.write_file("/tmp/my_project/README.md", "# My Project")

Expand Down
13 changes: 5 additions & 8 deletions examples/09_binary_files.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3
"""Binary file operations"""

import base64
import os

from koyeb import Sandbox
Expand All @@ -24,17 +23,15 @@ def main():

fs = sandbox.filesystem

# Write binary data
# Write binary data (encoding="base64" handles the encoding automatically)
binary_data = b"Binary data: \x00\x01\x02\x03\xff\xfe\xfd"
base64_data = base64.b64encode(binary_data).decode("utf-8")
fs.write_file("/tmp/binary.bin", base64_data, encoding="base64")
fs.write_file("/tmp/binary.bin", binary_data, encoding="base64")

# Read binary data
# Read binary data (encoding="base64" decodes and returns bytes)
file_info = fs.read_file("/tmp/binary.bin", encoding="base64")
decoded = base64.b64decode(file_info.content)
print(f"Original: {binary_data}")
print(f"Decoded: {decoded}")
assert binary_data == decoded
print(f"Read back: {file_info.content}")
assert binary_data == file_info.content

except Exception as e:
print(f"Error: {e}")
Expand Down
13 changes: 5 additions & 8 deletions examples/09_binary_files_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""Binary file operations (async variant)"""

import asyncio
import base64
import os

from koyeb import AsyncSandbox
Expand All @@ -25,17 +24,15 @@ async def main():

fs = sandbox.filesystem

# Write binary data
# Write binary data (encoding="base64" handles the encoding automatically)
binary_data = b"Binary data: \x00\x01\x02\x03\xff\xfe\xfd"
base64_data = base64.b64encode(binary_data).decode("utf-8")
await fs.write_file("/tmp/binary.bin", base64_data, encoding="base64")
await fs.write_file("/tmp/binary.bin", binary_data, encoding="base64")

# Read binary data
# Read binary data (encoding="base64" decodes and returns bytes)
file_info = await fs.read_file("/tmp/binary.bin", encoding="base64")
decoded = base64.b64decode(file_info.content)
print(f"Original: {binary_data}")
print(f"Decoded: {decoded}")
assert binary_data == decoded
print(f"Read back: {file_info.content}")
assert binary_data == file_info.content

except Exception as e:
print(f"Error: {e}")
Expand Down
2 changes: 1 addition & 1 deletion examples/10_batch_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def main():
{"path": "/tmp/project/README.md", "content": "# My Project"},
]

fs.mkdir("/tmp/project", recursive=True)
fs.mkdir("/tmp/project")
fs.write_files(project_files)
print("Created project structure")

Expand Down
2 changes: 1 addition & 1 deletion examples/10_batch_operations_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def main():
{"path": "/tmp/project/README.md", "content": "# My Project"},
]

await fs.mkdir("/tmp/project", recursive=True)
await fs.mkdir("/tmp/project")
await fs.write_files(project_files)
print("Created project structure")

Expand Down
2 changes: 1 addition & 1 deletion examples/12_file_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def main():
# Setup
fs.write_file("/tmp/file1.txt", "Content of file 1")
fs.write_file("/tmp/file2.txt", "Content of file 2")
fs.mkdir("/tmp/test_dir", recursive=True)
fs.mkdir("/tmp/test_dir")

# Rename file
fs.rename_file("/tmp/file1.txt", "/tmp/renamed_file.txt")
Expand Down
2 changes: 1 addition & 1 deletion examples/12_file_manipulation_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def main():
# Setup
await fs.write_file("/tmp/file1.txt", "Content of file 1")
await fs.write_file("/tmp/file2.txt", "Content of file 2")
await fs.mkdir("/tmp/test_dir", recursive=True)
await fs.mkdir("/tmp/test_dir")

# Rename file
await fs.rename_file("/tmp/file1.txt", "/tmp/renamed_file.txt")
Expand Down
Loading