我正在迁移tornado到asyncio,而且我找不到asyncio相同tornado的东西PeriodicCallback.(A PeriodicCallback有两个参数:运行的函数和调用之间的毫秒数.)
asyncio?RecursionError一会儿的风险?我想通过asyncio和连接到websocket websockets,格式如下所示.我怎么能做到这一点?
from websockets import connect
class EchoWebsocket:
def __init__(self):
self.websocket = self._connect()
def _connect(self):
return connect("wss://echo.websocket.org")
def send(self, message):
self.websocket.send(message)
def receive(self):
return self.websocket.recv()
echo = EchoWebsocket()
echo.send("Hello!")
print(echo.receive()) # "Hello!"
Run Code Online (Sandbox Code Playgroud) 在Python中,可以使用单行来以简单,直观的方式设置具有特殊条件(例如默认值或条件)的值.
result = 0 or "Does not exist." # "Does not exist."
result = "Found user!" if user in user_list else "User not found."
Run Code Online (Sandbox Code Playgroud)
是否有可能编写一个捕获异常的类似语句?
from json import loads
result = loads('{"value": true}') or "Oh no, explosions occurred!"
# {'value': True}
result = loads(None) or "Oh no, explosions occurred!"
# "Oh no, explosions occurred!" is desired, but a TypeError is raised.
Run Code Online (Sandbox Code Playgroud) Julia有内置常量pi,类型Irrational.
julia> pi
? = 3.1415926535897...
julia> ?
? = 3.1415926535897...
julia> typeof(pi)
Irrational{:?}
Run Code Online (Sandbox Code Playgroud)
从SymPy,它具有编辑N()功能,我想评价pi(或其它IrrationalS,例如e,golden等),以n个数字.
In [5]: N(pi, n=50)
Out[5]: 3.1415926535897932384626433832795028841971693993751
Run Code Online (Sandbox Code Playgroud)
这可能吗?我假设这pi是基于它的数学定义,而不是仅仅十三个小数位.
使用NumPy,我想生成一个长度为k的n维数组的所有行和对角线的列表.
以长度为3的以下三维数组为例.
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
Run Code Online (Sandbox Code Playgroud)
对于这种情况,我想获得以下所有类型的序列.对于任何给定的情况,我想获得每种类型的所有可能序列.对于每种情况,在下面的括号中给出了所需序列的实例.
0, 1, 2)0, 3, 6)0, 9, 18)0, 4, 8,2, 4, 6)0, 10, 20,2, 10, 18)0, 12, 24,6, 12, 18)在Python中,可以选择为是否在理解中包含特定项目提供条件.
[x**2 for x in range(10) if x > 5]
# [36, 49, 64, 81]
Run Code Online (Sandbox Code Playgroud)
有条件地使用函数是可能的,但我还没有找到一种完全排除值的方法,除了filter!它们在理解之外.
l = collect(0:9)
filter!(x -> x > 5, l)
l = [x^2 for x in l] # alternatively, map!(x -> x^2, l)
# [36, 49, 64, 81]
Run Code Online (Sandbox Code Playgroud)
朱莉娅有可能吗?
我正在尝试编写一个机器学习算法,scikit-learn用于解析文本并根据训练数据对其进行分类。
直接取自scikit-learn文档的使用文本数据的示例使用 aCountVectorizer生成每个单词出现次数的稀疏数组。
>>> from sklearn.feature_extraction.text import CountVectorizer
>>> count_vect = CountVectorizer()
>>> X_train_counts = count_vect.fit_transform(twenty_train.data)
Run Code Online (Sandbox Code Playgroud)
不幸的是,这没有考虑短语的任何排序。可以使用更大的ngrams( CountVectorizer(ngram_range=(min, max))) 来查看特定的短语,但这会迅速增加特征的数量,甚至不是那么好。
有没有一种以另一种方式处理有序文本的好方法?我肯定愿意将使用自然语言解析器(nltk,textblob沿等)scikit-learn。
我有一个接受的函数*args,但我想设置一个默认元组,以防没有提供.(这是不可能的def f(*args=(1, 3, 5)),这会引发一个SyntaxError.)最好的方法是什么?预期的功能如下所示.
f()
# I received 1, 2, 3!
f(1)
# I received 1!
f(9, 3, 72)
# I received 9, 3, 72!
Run Code Online (Sandbox Code Playgroud)
以下功能g将提供正确的功能,但我更喜欢*args.
def g(args=(1, 2, 3)):
return "I received {}!".format(', '.join(str(arg) for arg in args))
g()
# I received 1, 2, 3!
g((1,))
# I received 1!
g((9, 3, 72))
# I received 9, 3, 72!
Run Code Online (Sandbox Code Playgroud) 在Julia中,;可用于创建二维数组.
julia> [1 2; 3 4]
2x2 Array{Int64,2}:
1 2
3 4
Run Code Online (Sandbox Code Playgroud)
是否可以使用类似的语法来创建三维(或更高维)的数组?以下是有效的,但我不确定是否有更清洁,更好的方法.
julia> reshape(collect(1:8), 2, 2, 2)
2x2x2 Array{Int64,3}:
[:, :, 1] =
1 3
2 4
[:, :, 2] =
5 7
6 8
Run Code Online (Sandbox Code Playgroud) 是否有可能在Julia中键入函数kwargs?
以下适用于标准Varargs.
function int_args(args::Integer...)
args
end
int_args(1, 2, 3)
# (1, 2, 3)
int_args(1, 2, 3.0)
# ERROR: MethodError: `int_args` has no method matching int_args(::Int64, ::Int64, ::Float64)
Run Code Online (Sandbox Code Playgroud)
但是,当将相同的语法应用于kwargs时,所有函数调用似乎都是错误的.
function int_kwargs(; kwargs::Integer...)
kwargs
end
int_kwargs(x=1, y=2)
# ERROR: MethodError: `__int_kwargs#0__` has no method matching __int_kwargs#0__(::Array{Any,1})
Run Code Online (Sandbox Code Playgroud) 在Python中,if可能会在以下情况下使用可选的字符串格式.
bar = 3
"{n} bar{s}".format(n=bar, s='s' if bar != 1 else '')
# "3 bars"
bar = 1
"{n} bar{s}".format(n=bar, s='s' if bar != 1 else '')
# "1 bar"
Run Code Online (Sandbox Code Playgroud)
Julia使用美元符号进行字符串格式化.
foo = 3
"foo $foo" # "foo 3"
Run Code Online (Sandbox Code Playgroud)
是否可以使用Julia简单地镜像Python代码的功能?
在课堂上Wizard,我想将属性设置wand为coroutine返回的值magic.
class Wizard:
async def acquire_wand(self):
self.wand = await magic()
Run Code Online (Sandbox Code Playgroud)
但是,这段代码被认为是"糟糕的Python",因为wand它没有被定义__init__.但是我无法定义它__init__,因为它await可能只用于async繁琐的函数.
class Wizard:
def __init__(self):
self.wand = None
async def acquire_wand(self):
self.wand = await magic()
async def perform_spell(self):
if self.wand is None:
await self.acquire_wand()
self.wand.wave()
Run Code Online (Sandbox Code Playgroud)
我可以设置wand要None在__init__和使用if self.wand is None:的地方访问它,但这似乎混乱和不便.
我怎样才能确保wand在整个班级中定义?
我有一个项目列表,我希望定期“循环”通过,访问第一个(第 0 个)项目,然后将其移动到列表的后面。实现这一目标的最佳方法是什么?我想要的语法如下所示。
items = ["Alex", "Bob", "Charlie", "Doug", "Eddie"]
display_next_item(items)
# Alex
display_next_item(items)
# Bob
# ...
display_next_item(items)
# Eddie
display_next_item(items)
# Alex
Run Code Online (Sandbox Code Playgroud) python ×10
python-3.x ×7
julia ×5
python-3.5 ×3
arrays ×2
if-statement ×2
constants ×1
diagonal ×1
evaluation ×1
exception ×1
filter ×1
format ×1
kwargs ×1
list ×1
nltk ×1
numpy ×1
optimization ×1
scikit-learn ×1
string ×1
sympy ×1
tornado ×1
types ×1
websocket ×1