Skip to content

Class PythonCodeAttribute

Namespace: Belay.Attributes
Assembly: Belay.Attributes.dll

Specifies Python code to be executed on the MicroPython device when a method is called. This attribute allows embedding Python code directly in the method declaration without requiring the method to have an implementation body.

csharp
[AttributeUsage(AttributeTargets.Method)]
public sealed class PythonCodeAttribute : Attribute

Inheritance

objectAttributePythonCodeAttribute

Inherited Members

Attribute.Equals(object?), Attribute.GetCustomAttribute(Assembly, Type), Attribute.GetCustomAttribute(Assembly, Type, bool), Attribute.GetCustomAttribute(MemberInfo, Type), Attribute.GetCustomAttribute(MemberInfo, Type, bool), Attribute.GetCustomAttribute(Module, Type), Attribute.GetCustomAttribute(Module, Type, bool), Attribute.GetCustomAttribute(ParameterInfo, Type), Attribute.GetCustomAttribute(ParameterInfo, Type, bool), Attribute.GetCustomAttributes(Assembly), Attribute.GetCustomAttributes(Assembly, bool), Attribute.GetCustomAttributes(Assembly, Type), Attribute.GetCustomAttributes(Assembly, Type, bool), Attribute.GetCustomAttributes(MemberInfo), Attribute.GetCustomAttributes(MemberInfo, bool), Attribute.GetCustomAttributes(MemberInfo, Type), Attribute.GetCustomAttributes(MemberInfo, Type, bool), Attribute.GetCustomAttributes(Module), Attribute.GetCustomAttributes(Module, bool), Attribute.GetCustomAttributes(Module, Type), Attribute.GetCustomAttributes(Module, Type, bool), Attribute.GetCustomAttributes(ParameterInfo), Attribute.GetCustomAttributes(ParameterInfo, bool), Attribute.GetCustomAttributes(ParameterInfo, Type), Attribute.GetCustomAttributes(ParameterInfo, Type, bool), Attribute.GetHashCode(), Attribute.IsDefaultAttribute(), Attribute.IsDefined(Assembly, Type), Attribute.IsDefined(Assembly, Type, bool), Attribute.IsDefined(MemberInfo, Type), Attribute.IsDefined(MemberInfo, Type, bool), Attribute.IsDefined(Module, Type), Attribute.IsDefined(Module, Type, bool), Attribute.IsDefined(ParameterInfo, Type), Attribute.IsDefined(ParameterInfo, Type, bool), Attribute.Match(object?), Attribute.TypeId, object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Examples

Simple Python Code Execution

public interface ILEDController
{
    [Task]
    [PythonCode(@"
        import machine
        led = machine.Pin(2, machine.Pin.OUT)
        led.value(1)
    ")]
    Task TurnOnLEDAsync();

    [Task]
    [PythonCode(@"
        import machine
        led = machine.Pin({pin}, machine.Pin.OUT)
        led.value({state})
    ")]
    Task SetLEDAsync(int pin, int state);
}

Python Code with Return Value

public interface ISensorReader
{
    [Task]
    [PythonCode(@"
        import machine
        import time
        adc = machine.ADC(machine.Pin(26))
        reading = adc.read_u16()
        voltage = reading * 3.3 / 65535
        temperature = 27 - (voltage - 0.706) / 0.001721
        temperature
    ")]
    Task<float> ReadTemperatureAsync();
}

Remarks

The enables direct embedding of Python code that will be executed on the connected MicroPython device. This is useful for simple operations that don't require complex C# logic.

The Python code can include parameter placeholders using C# string interpolation syntax (e.g., {parameterName}) that will be replaced with actual parameter values at runtime.

Constructors

PythonCodeAttribute(string)

Initializes a new instance of the class.

Properties

Code

Gets the Python code to be executed on the device.

EnableParameterSubstitution

Gets or sets a value indicating whether parameter values should be automatically substituted into the Python code using string interpolation.

Methods

ToString()

Returns a string that represents the current .

Released under the Apache License 2.0.