简体   繁体   English

需要设置一个等于函数一部分结果的变量

[英]Need to set a variable equal to the result of a part of a function

I am attempting to create a script that uses a value in a remote database to create a wol packet. 我正在尝试创建一个脚本,该脚本使用远程数据库中的值来创建wol数据包。

I need to pass the value of a (something that I honestly don't know what to call) to a variable. 我需要将a的值(老实说我不知道​​要调用什么)传递给变量。 I am unable to just set it to the variable, and I can't figure out how to import it from somewhere else. 我无法仅将其设置为变量,也无法弄清楚如何从其他位置导入它。 I need "payload" which is printed and returned under "def message" to be saved to the variable "data" 我需要打印并在“ def message”下返回的“ payload”以保存到变量“ data”

Below is my code, and I will link the MQTT code that this relies on. 下面是我的代码,我将链接它所依赖的MQTT代码。

# Import standard python modules.
import sys

# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient
from Adafruit_IO import *


# Set to your Adafruit IO key & username below.
ADAFRUIT_IO_KEY      = 'where the api key goes'
ADAFRUIT_IO_USERNAME = 'my username'  # See https://accounts.adafruit.com
                                                    # to find your username.

# Set to the ID of the feed to subscribe to for updates.
FEED_ID = 'test1'


# Define callback functions which will be called when certain events happen.
def connected(client):
    # Connected function will be called when the client is connected to Adafruit IO.
    # This is a good place to subscribe to feed changes.  The client parameter
    # passed to this function is the Adafruit IO MQTT client so you can make
    # calls against it easily.
    print ('Connected to Adafruit IO!  Listening for {0} changes...').format(FEED_ID)
    # Subscribe to changes on a feed named DemoFeed.
    client.subscribe(FEED_ID)

def disconnected(client):
    # Disconnected function will be called when the client disconnects.
    print ('Disconnected from Adafruit IO!')
    sys.exit(1)

def message(client, feed_id, payload):
    # Message function will be called when a subscribed feed has a new value.
    # The feed_id parameter identifies the feed, and the payload parameter has
    print ('Feed {0} received new value: {1}').format(FEED_ID, payload)
    return (payload == payload)

data = message(x, x, x) 

# Create an MQTT client instance.
client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

# Setup the callback functions defined above.
client.on_connect    = connected
client.on_disconnect = disconnected
client.on_message    = message

# Connect to the Adafruit IO server.
client.connect()

# Start a message loop that blocks forever waiting for MQTT messages to be
# received.  Note there are other options for running the event loop like doing
# so in a background thread--see the mqtt_client.py example to learn more.

if data == '1':
    print('Latest value from Test: {0}'.format(data.value))
    wol.send_magic_packet('my mac addy')
    time.sleep(3)
    # Send a value to the feed 'Test'. 
    aio.send('test1', 0)
    print ("worked this time")
client.loop_blocking()

Here is the link to the other code it relies on https://github.com/adafruit/io-client-python/blob/master/Adafruit_IO/mqtt_client.py 这是它依赖于https://github.com/adafruit/io-client-python/blob/master/Adafruit_IO/mqtt_client.py的其他代码的链接

First of all, you don't need to import the same module twice 首先,您不需要两次导入相同的模块

from Adafruit_IO import MQTTClient # this imports part MQTTClient
from Adafruit_IO import *          # this imports everything, including MQTTClient again

As for your problem, you've returned (payload == payload) which will always be True, i'm not sure what you're trying to do here but it should look something like this: 至于您的问题,您已经返回了(payload == payload) ,该值始终为True,我不确定您要在此处执行的操作,但它看起来应该像这样:

def message(client, feed_id, payload):
    ...
    print ('Feed {0} received new value: {1}').format(FEED_ID, payload)
    return payload == payload_from_database # what you return will be saved as the variable data

data = message("Steve the happy client", 85, "£100") # in this case, data will be payload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果结果存在,则将变量设置为等于结果 - Set a variable equal to result if result exists 如何设置一个变量等于输入的一部分? - How to set a variable equal to part of an input? 使用 python function 的一部分的结果并存储在 function 外部的变量中 - Using the result from part of a python function and store in variable outside function 如何设置一个变量等于python中函数返回的值 - How do you set a variable equal to the value returned by a function in python 将Python类变量设置为等于类函数的输出 - Set Python class variable equal to the output of class function python:我可以将变量设置为等于自身的函数吗? - python: can i set a variable to equal a function of itself? 如何通过将外部参数传递给函数来设置与函数内部另一个局部变量相等的变量? - How to set a variable equal to another local variable inside a function by passing an external argument to the function? 如何为tkinter Button的命令功能结果设置变量? - How to set a variable to the result of command function of a tkinter Button? 设置新的列表变量,该变量等于在传递了列表框值的函数中生成的列表 - set new list variable equal to list generated in a function that has listbox values passed to it 将函数结果存储到变量中 - Store function result into variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM