Sample Projects
This section provides links and descriptions of complete sample projects that demonstrate CSnakes in action.
Available Sample Projects
1. Basic Console Application
Location: samples/simple/QuickConsoleTest
Description: The simplest possible CSnakes implementation showing basic Python function calls from C#.
Features:
- Basic string manipulation functions
- Mathematical operations
- Type conversion examples
- Error handling demonstration
Key Files:
hello_world.py
- Simple greeting function with optional parametersProgram.cs
- Basic C# console application setup
What You'll Learn:
- Basic CSnakes project setup
- Simple function calls
- Parameter passing
- Return value handling
2. AOT Console Application
Location: samples/simple/AOTConsoleApp
Description: Demonstrates CSnakes compatibility with Native AOT compilation for improved performance.
Features:
- Native AOT compilation support
- Minimal memory footprint
- Fast startup times
- Source generator usage (required for AOT)
Key Files:
aot_demo.py
- Simple Python functions for AOT demoProgram.cs
- AOT-compatible C# codeAOTConsoleApp.csproj
- Project configuration with<PublishAot>true</PublishAot>
What You'll Learn:
- AOT compatibility requirements
- Performance benefits of AOT
- Source generator usage
- Deployment considerations
3. Web Application
Location: samples/simple/WebApp
Description: ASP.NET Core web application integrating Python data processing capabilities.
Features:
- REST API endpoints calling Python functions
- Dependency injection integration
- Machine learning model integration
- Performance monitoring
- Load testing configuration
Key Files:
kmeans_example.py
- K-means clustering implementationphi3_demo.py
- Small language model integrationControllers/
- Web API controllersloadtest.jmx
- JMeter load test configuration
What You'll Learn:
- Web application integration
- Dependency injection setup
- API design patterns
- Performance considerations
- Load testing strategies
4. F# Sample
Location: samples/simple/FSharpSample
Description: Demonstrates CSnakes usage in F# functional programming context.
Features:
- F# functional programming patterns
- Immutable data structures
- Pipeline operations
- Type-safe Python integration
Key Files:
math_functions.py
- Mathematical operationsProgram.fs
- F# application using CSnakesFSharpSample.fsproj
- F# project configuration
What You'll Learn:
- F# and CSnakes integration
- Functional programming patterns
- Type safety in F#
- Cross-language interoperability
5. Aspire Distributed Application
Location: samples/Aspire
Description: .NET Aspire orchestrated application with multiple services using CSnakes.
Features:
- Service orchestration with .NET Aspire
- Multiple microservices
- Shared Python modules
- Service discovery
- Monitoring and observability
Sample Project Details
Machine Learning Examples
K-means Clustering
File: kmeans_example.py
from sklearn.cluster import k_means
import numpy as np
def calculate_kmeans_inertia(data: list[tuple[int, int]], n_clusters: int) -> tuple[list[list[float]], float]:
X = np.array(data)
centroid, label, inertia = k_means(
X, n_clusters=n_clusters, n_init="auto", random_state=0
)
return centroid.tolist(), inertia
C# Usage:
var kmeans = env.KmeansExample();
var data = new[] { (1, 2), (1, 4), (1, 0), (10, 2), (10, 4), (10, 0) };
var (centroids, inertia) = kmeans.CalculateKmeansInertia(data, 2);
Getting Started with Samples
1. Clone the Repository
2. Choose a Sample
Navigate to the sample you want to explore:
3. Install Dependencies
For samples requiring Python packages:
# Create virtual environment
python -m venv .venv
.venv\Scripts\activate # Windows
source .venv/bin/activate # Linux/macOS
# Install requirements
pip install -r requirements.txt
4. Run the Sample
Sample Project Structure
Most samples follow this structure:
SampleProject/
├── SampleProject.csproj # Project configuration
├── Program.cs # Main application entry point
├── python_modules/ # Python code directory
│ ├── __init__.py # Makes it a Python package
│ ├── module1.py # Python functions
│ └── module2.py # More Python functions
├── requirements.txt # Python dependencies
├── README.md # Sample-specific documentation
└── appsettings.json # Configuration (for web apps)
Performance Benchmarks
Web Application Performance
The WebApp sample includes JMeter load test configurations showing:
- Throughput: 1000+ requests/second for simple operations
- Latency: Sub-millisecond Python function calls
- Memory: Stable memory usage under load
- Scalability: Linear scaling with CPU cores
Troubleshooting Samples
Common Issues
-
Python Not Found
-
Solution: Use
FromRedistributable()
locator -
Alternative: Set
PYTHONHOME
environment variable -
Package Import Errors
-
Solution: Check
requirements.txt
and virtual environment setup -
Alternative: Use
WithPipInstaller()
method -
Build Errors
-
Solution: Ensure Python files are marked as
AdditionalFiles
-
Alternative: Check .csproj configuration
-
Runtime Exceptions
-
Solution: Check Python syntax and type annotations
- Alternative: Use try-catch blocks around Python calls
Getting Help
- Check the FAQ for common questions
- Review troubleshooting guide for detailed solutions
- Examine the sample's README.md for specific instructions