小编Roe*_*ant的帖子

如何在python36中获取当前正在运行的EventLoop?

我知道在python37中我们有一个新的api asyncio.get_running_loop(),它很容易使用,让我们在调用协程时不需要显式地传递eventloop。

我想知道我们是否可以使用任何方法在 python36 中获得相同的效果?

# which allows us coding conveniently with this api:
import asyncio

async def test():
    print("hello world !")

async def main():
    loop = asyncio.get_running_loop()
    loop.create_task(test())

asyncio.run(main())
Run Code Online (Sandbox Code Playgroud)

python-asyncio python-3.6

5
推荐指数
1
解决办法
4007
查看次数

Jupyter 在 docker 容器中启动内核?

我想在不同内核之间轻松切换我的笔记本。一个用例是在tensorflow 2、2.2、2.3中快速测试一段代码,类似的用例还有很多。但是,我现在更喜欢将我的环境定义为 docker,而不是不同的(conda)环境。

现在我知道您可以在容器中启动 jupyter,但这不是我想要的。我只想单击Kernel > use kernel > TF 2.2 (docker),让 jupyter 连接到在此容器中运行的内核。

周围有这样的东西吗?我曾经livy通过 ssh 连接到远程 Spark 内核,所以感觉这应该是可能的。

python docker jupyter-notebook livy

5
推荐指数
1
解决办法
2247
查看次数

诗歌更新访问被拒绝

我在 Windows 上的 conda 环境中使用诗歌。尽管我在提升的 shell 中工作(管理员权限),但我在涉及 的包(.dll例如 tensorflow 或 scipy.

conda create --name test python=3.7
conda activate test
pip install poetry
poetry add "tensorflow=1.5.1" scipy pandas
poetry install
poetry add "tensorflow=1.4.1"
poetry install 
Run Code Online (Sandbox Code Playgroud)

会归还我

ERROR: Could not install packages due to an EnvironmentError: [WinError 5] Access is denied:
Run Code Online (Sandbox Code Playgroud)

python python-poetry

5
推荐指数
1
解决办法
3041
查看次数

是否可以在 IPython/Jupyter 中结合魔法?

有时您想同时使用多种魔法。现在我知道你可以使用

%%time
%%bash
ls 
Run Code Online (Sandbox Code Playgroud)

但是当我制定自己的命令时,这种链接不起作用......

from IPython.core.magic import register_cell_magic

@register_cell_magic
def accio(line, cell):
    print('accio')
    exec(cell)
Run Code Online (Sandbox Code Playgroud)

导致使用时报错

%%accio
%%bash
ls
Run Code Online (Sandbox Code Playgroud)

我应该使用什么而不是exec

python ipython jupyter-notebook

4
推荐指数
1
解决办法
704
查看次数

AWS athena (presto SQL):如何在 group by 语句中获取数组的(类似集合的)并集

我正在尝试将唯一用户 ID 的数组合并为一个唯一用户 ID 的数组。AWS athena没有该set_union功能,所以我无法使用

, set_union(userids)
Run Code Online (Sandbox Code Playgroud)

而且reduce_agg似乎不允许数组

, reduce_agg(userids, ARRAY[], (a, b) -> array_union(a, b), (a, b) -> array_union(a, b))
Run Code Online (Sandbox Code Playgroud)

是否有任何其他技巧可以用来将数组组合成一个数组(不同的项目)

sql amazon-web-services presto amazon-athena

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

在变量中的python字符串上触发f字符串解析

这个问题来自处理jupyter magics,但可以用更简单的方式表达。给定一个字符串s = "the key is {d['key']}"和一个字典d = {'key': 'val'},我们想解析这个字符串。

旧方法是.format(),这会引发错误 - 它不处理字典键。

"the key is {d['key']}".format(d=d)  # ERROR
Run Code Online (Sandbox Code Playgroud)

我认为唯一的方法是将字典转换为对象(在此处或此处解释)。

"the key is {d.key}".format(obj(d))
Run Code Online (Sandbox Code Playgroud)

但是Martijn很好地解释了,您可以简单地省略引号以使其正常工作:

"the key is {d[key]}".format(d=d)
Run Code Online (Sandbox Code Playgroud)

新方法仍然f'string'以直观的 Python 方式处理字典键:

f"the key is {d['key']}"
Run Code Online (Sandbox Code Playgroud)

它还处理功能——某些东西.format也无法处理。

f"this means {d['key'].lower()}"
Run Code Online (Sandbox Code Playgroud)

虽然我们现在知道你可以用来做.format,但我仍然想知道最初的问题:给定sd,你如何强制f'string'解析s?我在大括号内添加了另一个带有函数的示例,该示例.format也无法处理并且f'string'能够解决。

是否有一些功能.fstring()或方法可用?Python 内部使用什么?

python string string-formatting python-3.x f-string

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

如何从网站中提取冠状病毒病例?

我正在尝试从网站 ( https://www.trackcorona.live ) 中提取冠状病毒,但出现错误。

\n\n

这是我的代码:

\n\n
response = requests.get(\'https://www.trackcorona.live\')\ndata = BeautifulSoup(response.text,\'html.parser\')\nli = data.find_all(class_=\'numbers\')\nconfirmed = int(li[0].get_text())\nprint(\'Confirmed Cases:\', confirmed)\n
Run Code Online (Sandbox Code Playgroud)\n\n

它给出了以下错误(尽管它在几天前工作),因为它返回一个空列表(li)

\n\n
 IndexError                               \n Traceback (most recent call last)\n<ipython-input-15-7a09f39edc9d> in <module>\n      2 data=BeautifulSoup(response.text,\'html.parser\')\n      3 li=data.find_all(class_=\'numbers\')\n----> 4 confirmed = int(li[0].get_text())\n      5 countries = li[1].get_text()\n      6 dead = int(li[3].get_text())\n\nIndexError: list index out of range\n
Run Code Online (Sandbox Code Playgroud)\n\n

\xe2\x80\x8b

\n

python api beautifulsoup web-scraping

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