小编son*_*ium的帖子

Python 3.9:在 __anext__ 中使用 Yield 时,“async_generator 不能在 'await' 表达式中使用”

我正在尝试以下操作:

class Payload_Session_Generator:
    def __init__(self):
        pass

    async def __anext__(self):
        async for payload in generate_fb_payload():
            if type(payload) != str:
                yield payload
            else:
                StopAsyncIteration

    def __aiter__(self):
        return self
Run Code Online (Sandbox Code Playgroud)

然后将其作为实例传递给不同的函数,并__aiter__显式调用该方法,并且该方法_iter是上述类的对象:

chunk = await self._iter.__anext__()
Run Code Online (Sandbox Code Playgroud)

这会产生以下错误:

类型错误:对象 async_generator 不能在“await”表达式中使用

python generator python-3.x python-asyncio

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

Anaconda:IPython/Jupyter Notebook中的Python 3和2

我有一个Anaconda的Python 3安装,并希望能够在python2和3内核之间快速切换.这是在OSX上.

我到目前为止的步骤包括:

conda create -p ~/anaconda/envs/python2 python=2.7
source activate python2
conda install ipython
ipython kernelspec install-self
source deactivate
Run Code Online (Sandbox Code Playgroud)

在此之后,我在python3 IPython笔记本中有一个python2内核可供选择,但是无法启动.

所以我继续修改/usr/local/share/jupyter/kernels/python2/kernel.json

{
 "display_name": "Python 2",
 "language": "python",
 "argv": [
  "/Users/sonium/anaconda/envs/python2/bin/python",
  "-m",
  "IPython.kernel",
  "-f",
  "{connection_file}"
 ],
 "env":{"PYTHONHOME":"~/anaconda/envs/python2/:~/anaconda/envs/python2/lib/"}
}
Run Code Online (Sandbox Code Playgroud)

现在,当我启动python2内核时,它失败了:

ImportError: No module named site
Run Code Online (Sandbox Code Playgroud)

python ipython jupyter

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

求解微分方程时的高频噪声

我试图根据菲克第二定律模拟一个简单的扩散.

from pylab import *
import numpy as np
gridpoints = 128

def profile(x):
    range = 2.
    straggle = .1576
    dose = 1 
    return dose/(sqrt(2*pi)*straggle)*exp(-(x-range)**2/2/straggle**2)

x = linspace(0,4,gridpoints)
nx = profile(x)
dx = x[1] - x[0] # use np.diff(x) if x is not uniform
dxdx = dx**2

figure(figsize=(12,8))

plot(x,nx)
timestep = 0.5
steps = 21
diffusion_coefficient = 0.002
for i in range(steps):
    coefficients = [-1.785714e-3, 2.539683e-2, -0.2e0, 1.6e0,
                    -2.847222e0,
                    1.6e0, -0.2e0, 2.539683e-2, -1.785714e-3]
    ccf = (np.convolve(nx, coefficients) / …
Run Code Online (Sandbox Code Playgroud)

python physics numpy scientific-computing differential-equations

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

在theading.Thread中使用sqlalchemy scoped_session

我在使用sqlalchemy和线程时遇到了问题.

import queue
import threading

import sqlalchemy
from sqlalchemy import create_engine, Column, Integer, String, Sequence
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.scoping import scoped_session

engine = create_engine('sqlite:///:memory:', echo=False)
Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
    name = Column(String)
    fullname = Column(String)
    password = Column(String)

    def __repr__(self):
        return "<User(name='%s', fullname='%s', password='%s')>" % (
        self.name, self.fullname, self.password)
Base.metadata.create_all(engine)

sessionfactory = sessionmaker(bind=engine)

# called by each thread
def write_name(q, name, sessionfactory):
    session = scoped_session(sessionfactory) …
Run Code Online (Sandbox Code Playgroud)

python multithreading sqlalchemy python-3.4

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

Matplotlib:更改指数的字体大小

我想更改图片上标记的指数的字体大小 图片 我不能使用 matplotlib.rc('font', **font) 方法,因为我有不同的图需要不同的字体大小,所以我单独更改每个元素。但是,我找不到指数的字体属性。

matplotlib

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

为pypy建造熊猫

我无法在osx和raspbian上为pypy 4.0.0编译pandas(0.17.0).

但在两种情况下都失败了,但原因不同:

raspbian:

pandas/lib.c:84937:76: error: ‘PyDateTime_Date’ undeclared (first use in this function)
    pandas/lib.c:84938:84: error: ‘PyDateTime_DateTime’ undeclared (first use in this function)
    error: command 'cc' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)

OSX:

In file included from pandas/src/datetime_helper.h:3:
    /Users/sonium/Raspbrewry/venv-pypy/site-packages/numpy/core/include/numpy/arrayscalars.h:8:3: error: typedef redefinition with different types ('struct PyBoolScalarObject' vs 'struct PyBoolScalarObject')
    } PyBoolScalarObject;
Run Code Online (Sandbox Code Playgroud)

我知道大熊猫没有正式支持pypy,但是pypy不应该成为替代品吗?

python pypy pandas

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