此代码记录6
6次:
(function timer() {
for (var i=0; i<=5; i++) {
setTimeout(function clog() {console.log(i)}, i*1000);
}
})();
Run Code Online (Sandbox Code Playgroud)
但是这段代码......
(function timer() {
for (let i=0; i<=5; i++) {
setTimeout(function clog() {console.log(i)}, i*1000);
}
})();
Run Code Online (Sandbox Code Playgroud)
...记录以下结果:
0
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
为什么?
是因为不同地let
绑定到内部范围每个项目并var
保持最新值i
?
这是我写的一个小函数来理解send方法:
>>> def test():
... for x in xrange(10):
... res = yield
... yield res
>>> a = test()
>>> next(a)
>>> next(a)
>>> next(a)
>>> next(a)
>>> a.send(0)
Traceback (most recent call last):
<ipython-input-220-4abef3782000> in <module>()
StopIteration
>>> a = test()
>>> a.send(0)
Traceback (most recent call last):
<ipython-input-222-4abef3782000> in <module>()
TypeError: can't send non-None value to a just-started generator
>>> a.send(None)
>>> a.send(0)
0
>>> a.send(0)
>>> a.send(0)
0
>>> a.send(0)
>>> a.send(0)
0
>>> a.send(0)
Run Code Online (Sandbox Code Playgroud)
为什么第一次出现错误? …
假设我已经定义了一个Graph类.
graph iowa = {.....nodes:{edges.......}}
Run Code Online (Sandbox Code Playgroud)
和类似的图簇.
一旦脚本在对象中运行其全部.
但是如何存储图形信息(包括其属性的定义)?你把它保存在db表中,csv?
可能不满意的查询: 在Neo4J中存储多个图形 (它表示newo4j一个实例一次支持一个图形,如果要运行多个图形,则支持一个图形下的子图形)
*****更新*****:Alexanders在下面回答可用的Graph DBs,我想知道是否有人知道VertexDB可能的分支,他们已经将这个高性能的lib导入任何其他语言,最好是NodeJS还是Python?
http://www.dekorte.com/projects/opensource/vertexdb/docs/manual.html
有没有人有使用AllegroGraph vs Neo4j的经验?
这是python中的合并排序逻辑:(这是第一部分,忽略函数merge())问题点是将递归逻辑转换为while循环.代码礼貌:Rosettacode Merge Sort
def merge_sort(m):
if len(m) <= 1:
return m
middle = len(m) / 2
left = m[:middle]
right = m[middle:]
left = merge_sort(left)
right = merge_sort(right)
return list(merge(left, right))
Run Code Online (Sandbox Code Playgroud)
是否有可能在while循环中使它成为一种动态,而每个左右数组都分成两个,一种指针根据左右数组的数量不断增加并打破它们直到只剩下单个长度的列表?因为每当下一次分割进入左右两侧时,阵列就会不断分解,直到只剩下单个长度列表,因此左侧(左 - 左,右 - 右)和右侧(右 - 左,右 - 右)中断将增加,直到达到所有大小为1的列表.
参考这篇文章 多线程NLP与Spacy管道谈论,
from spacy.attrs import *
# All strings mapped to integers, for easy export to numpy
np_array = doc.to_array([LOWER, POS, ENT_TYPE, IS_ALPHA])
from reddit_corpus import RedditComments
reddit = RedditComments('/path/to/reddit/corpus')
# Parse a stream of documents, with multi-threading (no GIL!)
# Processes over 100,000 tokens per second.
for doc in nlp.pipe(reddit.texts, batch_size=10000, n_threads=4):
# Multi-word expressions, such as names, dates etc
# can be merged into single tokens
for ent in doc.ents:
ent.merge(ent.root.tag_, ent.text, ent.ent_type_)
# …
Run Code Online (Sandbox Code Playgroud) 如何建立协会有使用外生的新一_many_to_many_关系的两个表之间使用many_to_many
功能?
从组织到用户,我有一对多的关系:
organization = Organization.shortcode_changeset(%Organization{}, org_field)
organization = organization |> Repo.insert!
organization |> build_assoc(:users)
Run Code Online (Sandbox Code Playgroud)
这使
%User{__meta__: #ecto.Schema.Metadata<:built, "users">,......}
user = Repo.preload(user, [:organization, :usergroup])
Run Code Online (Sandbox Code Playgroud)
如何在用户和组之间使用many_to_many来完成此操作?
我尝试使用argparse来学习如何解析给定列表:
parser = argparse.ArgumentParser()
parser.add_argument('--ls', nargs='*', type=str, default = [])
Out[92]: _StoreAction(option_strings=['--ls'], dest='ls', nargs='*', const=None, default=[], type=<type 'str'>, choices=None, help=None, metavar=None)
args = parser.parse_args("--ls 'tomato' 'jug' 'andes'".split())
args
Out[94]: Namespace(ls=["'tomato'", "'jug'", "'andes'"])
args.ls
Out[96]: ["'tomato'", "'jug'", "'ande'"]
args.ls[0]
Out[97]: "'tomato'"
eval(args.ls[0])
Out[98]: 'tomato'
Run Code Online (Sandbox Code Playgroud)
Q1:上面的作品,但是否有更好的方法来访问列表中的值?
然后我尝试用Dictionary解析给定的字典:
dict_parser = argparse.ArgumentParser()
dict_parser.add_argument('--dict', nargs='*',type=dict,default={})
Out[104]: _StoreAction(option_strings=['--dict'], dest='dict', nargs='*', const=None, default={}, type=<type 'dict'>, choices=None, help=None, metavar=None)
arg2 = dict_parser.parse_args("--dict {'name':'man', 'address': 'kac', 'tags':'don'}")
usage: -c [-h] [--dict [DICT [DICT ...]]]
-c: error: unrecognized arguments: - …
Run Code Online (Sandbox Code Playgroud) 我希望创建一个查询,扫描表并根据一些 calc_dist(attribute) 函数计算表中给定位置和存储的地理位置属性之间的差异,该函数可以动态过滤它并返回从最近到的切片结果最远
假设表类是这样的:
Hoo(Base):
attribute_names = Column(......)
@hybrid_property
def calc_dist(self):
#do some foo(self.attr_)
Run Code Online (Sandbox Code Playgroud)
如何做类似的事情:
session.query(Hoo).filter(Hoo.attr_ and given_geo_loc USING this location value- Is there a way to pass this val into calc_dist here?)
Run Code Online (Sandbox Code Playgroud)
每次传入具有新给定地理位置的新请求时,我们如何使用此给定位置将其传递到过滤器查询中,以便 calc_dist 计算它?
对于每个给定的位置参数,都会有一个切片查询结果,该结果根据从最近到最远的限制生成值。
更新
这真的很有帮助
我尝试像这样使用它来解决我的问题:
class Hoo(Base):
#Table info
__tablename__ = 'hoo'
#mappers
lat = Column(String(50))
lng = Column(String(50))
@hybrid_method
def rankByloc(self, lat, lng):
return haversine((float(self.lat), float(self.lng)), (float(lat), float(lng)))
@rankByloc.expression
def rankByloc(cls, lat, lng):
return haversine((float(self.lat), float(self.lng)), (float(lat), float(lng)))
Run Code Online (Sandbox Code Playgroud)
当我做
session.query(Hoo).order_by(Hoo.rankByloc(lat, lng))
Run Code Online (Sandbox Code Playgroud)
给出错误:
NameError: …
Run Code Online (Sandbox Code Playgroud) 我做了以下事情
class dumb(object):
def __init__(self):
self.g = {}
def __getitem__(self,key): return self.g[key] if key in self.g else None
def __setitem__(self,key,val):
self.g[key] = val
te = dumb()
te[1]=2
te[1]
Out[4]: 2
1 in te
Run Code Online (Sandbox Code Playgroud)
它挂起..
所以如果我想搜索这样的东西,我该怎么做?请不要告诉我子类class dictionary.
提前致谢!
这里可能有相关问题: __contains__做了什么,可以调用__contains__函数
如何运行一个给定的模块给定我想同时运行一些不一定使用路由的功能(可能是守护进程服务),同时运行应用服务器?
例如:
#some other route functions app.post(...)
#some other concurrent functions
def alarm():
'''
Run this service every X duration
'''
ALARM = 21
try:
while 1:
#checking time and doing something. Then finding INTERVAL
gevent.sleep(INTERVAL)
except KeyboardInterrupt,e:
print 'exiting'
Run Code Online (Sandbox Code Playgroud)
主要之后我必须使用上面这样的吗?
gevent.joinall(gevent.spawn(alarm))
app.run(....)
Run Code Online (Sandbox Code Playgroud)
要么
gevent.joinall((gevent.spawn(alarm),gevent.spawn(app.run)))
Run Code Online (Sandbox Code Playgroud)
目标是像守护进程服务一样运行这些警报,执行其工作并暂停,而其他服务操作照常工作.服务器也应该同时启动.如果我不在正确的轨道上,请纠正我.