小编Meh*_*bla的帖子

Python yield 语句每次都返回相同的值

编辑:我想查看使用该next语句的解决方案。我正在访问一个返回json对象的天气应用程序API,该对象的一部分信息是每天的日出和日落时间,这是它的内容(三天):

my_dict = {
"daily": [
    {
      "dt": "2020-06-10 12:00:00+01:00",
      "sunrise": "2020-06-10 05:09:15+01:00",
      "sunset": "2020-06-10 19:47:50+01:00"
    },
    {
        "dt": "2020-06-11 12:00:00+01:00",
        "sunrise": "2020-06-11 05:09:11+01:00",
        "sunset": "2020-06-11 19:48:17+01:00"
    },
    {
      "dt": "2020-06-12 12:00:00+01:00",
      "sunrise": "2020-06-12 05:09:08+01:00",
      "sunset": "2020-06-12 19:48:43+01:00"
    }
]
}
Run Code Online (Sandbox Code Playgroud)

这是应该每天返回一个元组的函数,但它没有。它不断返回同一天(第一天)的一组数据。

daily_return = my_dict['daily']


def forecast(daily_return):
    # daily_return is a list
    for day in daily_return:
        # day becomes a dict
        sunrise = day['sunrise']
        sunset = day['sunset']
        yield sunrise, sunset

for i in range(3):
    print(next(forecast(daily_return)))
Run Code Online (Sandbox Code Playgroud)

这是输出:

('2020-06-10 05:09:15+01:00', …
Run Code Online (Sandbox Code Playgroud)

python yield next

3
推荐指数
1
解决办法
294
查看次数

Dart Flutter,帮助我了解 future

看这段代码:

class SomeClass{
  String someVariable;
  SomeClass();
  
  Future<String> getData ()  async {
    Response response = await get('http://somewebsite.com/api/content');
    
    Map map = jsonDecode(response.body); // do not worry about statuscode, trying to keep it minimal
    someVariable = map['firstName'];
    return 'This is the first name : $someVariable';
  }
}
Run Code Online (Sandbox Code Playgroud)

现在看主要:

void main(){
  String someFunction() async {
    SomeClass instance = SomeClass(); // creating object
    String firstNameDeclaration = await instance.getData().then((value) => value); 
    return firstNameDeclaration;
  }
}
Run Code Online (Sandbox Code Playgroud)

firstNameDeclaration 当使用 Future 时,就像为什么我必须使用方法来访问字符串对象的情况一样.then(),因为我正在等待函数完成?在网上搜索时,有些人使用.then()其他人不使用,我很困惑。

请帮助我更清楚地了解 Future 和异步函数的整体工作原理。

asynchronous future dart flutter

2
推荐指数
1
解决办法
1839
查看次数

WAMP 服务器建立数据库连接时出错

我在我的 localhost wamp 服务器上运行了 wordpress,然后我决定重新安装它,因为它变慢了,现在重新安装后,我不断收到这个错误:建立数据库连接时出错。

wordpress wamp localhost wampserver server

0
推荐指数
1
解决办法
2万
查看次数

*后的Python线程模块错误参数必须是可迭代的,而不是int

所以我在玩线程模块以了解它的基础知识,当我运行我的代码时,我收到一个错误,没有明确说明我做错了什么。这是代码,在下面,您可以找到错误日志:

import threading
import time

start = time.perf_counter()


def square(y):
    print('Starting processing')
    for x in range(y):
        i = x * x
    print(f'Done processing {i}')

threads = []

# creating thread
for _ in range(10):
    thread = threading.Thread(target=square, args=1000000)
    # starting thread
    thread.start()
    threads.append(thread)

# makes sure that the threads complete before moving to the rest of the code
for i in threads:
    i.join()




finish = time.perf_counter()

print(f'Done processing in {round(finish - start, 4)} second(s).')
Run Code Online (Sandbox Code Playgroud)

我得到这个错误:

Exception in thread Thread-1:
Traceback …
Run Code Online (Sandbox Code Playgroud)

python multithreading module

0
推荐指数
1
解决办法
1282
查看次数

Python 为什么 .lower() 不能在我的其余代码中永久应用?

我正在学习python,在课程中,我不得不制作一个将元音转换为字母“g”的翻译器。在程序中,我必须检查要翻译的短语是否有任何大写元音,以便用大写字母“G”替换它们。我不明白为什么.lower()不适用于其余的代码?在我看来,如果我letter.lower()在下一行应用,变量的值letter仍应为小写。这是我的代码:

def translate(phrase):
translated = ""
for letter in phrase:
    if letter.lower() in "aeouiy":
        if letter.isupper():
            translated = translated + "G"
        else:
            translated = translated + "g"
    else:
        translated = translated + letter
return translated
print(translate(input("enter phrase to translate into giraffe elegant language: ")))
Run Code Online (Sandbox Code Playgroud)

python string transform lowercase

-2
推荐指数
1
解决办法
131
查看次数