问题描述
在运行 python 代码的 raspBerry pi 4 上向我的服务器执行来自邮递员的“GET”请求时遇到问题...
我面临的错误是:
'int' object is not callable The view function did not return a valid response. The return type must be a string,tuple,Response instance,or Wsgi callable,but it was a int.
这个代码的工作是计算来自传感器的值流,然后返回它们的平均值,但是每当我尝试它时,它总是给我这个错误.. #注意,它在 Pycharm 上运行良好,但在请求中却没有。
python 脚本是:
def oxygin():
result = "Result is: "
print("\nSparkFun MAX3010x Photodetector - Example 5\n")
sensor = qwiic_max3010x.QwiicMax3010x()
if sensor.begin() == False:
print("The Qwiic MAX3010x device isn't connected to the system. Please check your connection",\
file=sys.stderr)
return
else:
print("The Qwiic MAX3010x is connected.")
print("Place your index finger on the sensor with steady pressure.")
if sensor.setup() == False:
print("Device setup failure. Please check your connection",\
file=sys.stderr)
return
else:
print("Setup complete.")
sensor.setpulseAmplitudeRed(0x0A) # Turn Red LED to low to indicate sensor is running
sensor.setpulseAmplitudeGreen(0) # Turn off Green LED
RATE_SIZE = 4 # Increase this for more averaging. 4 is good.
rates = list(range(RATE_SIZE)) # list of heart rates
rateSpot = 0
lastBeat = 0 # Time at which the last beat occurred
beatsPerMinute = 0.00
OxiAvg = 0
samplesTaken = 0 # Counter for calculating the Hz or read rate
startTime = millis() # Used to calculate measurement rate
while True:
irValue = sensor.getIR()
samplesTaken += 1
if sensor.checkForBeat(irValue) == True:
# We sensed a beat!
#print('')
delta = ( millis() - lastBeat )
lastBeat = millis()
beatsPerMinute = 60 / (delta / 1000.0)
beatsPerMinute = round(beatsPerMinute,1)
if beatsPerMinute < 255 and beatsPerMinute > 20:
rateSpot += 1
rateSpot %= RATE_SIZE # Wrap variable
rates[rateSpot] = beatsPerMinute # Store this reading in the array
# Take average of readings
OxiAvg = 0
for x in range(0,RATE_SIZE):
OxiAvg += rates[x]
OxiAvg /= RATE_SIZE
OxiAvg = round(OxiAvg)
Hz = round(float(samplesTaken) / ( ( millis() - startTime ) / 1000.0 ),2)
if (samplesTaken > 1000) == 1:
result = str(OxiAvg)
#return str(OxiAvg)
#print('SpO2=',(beatAvg+15),'%\t')
return result
解决方法
你应该返回一个字符串对象,而不是一个 int 对象
替换以下行:
enum AItem {
case string(String)
case data(Data)
case int(Int)
}
var anArray: [AItem] = [
.string("String"),.int(1234),.data("a path".data(using: .utf8)!)
]
for curItem in anArray {
let result = handleItem(item: curItem)
print("result = \(result)")
}
func handleItem(item: AItem) -> String {
// Note that no casting is required,and we don't need an unknown case
// because we know all types that might occur at compile time!
switch item {
case .string: return "String"
case .data: return "Data"
case .int: return "Int"
}
}
与:
return result