我按键对字典进行排序,但我想颠倒顺序.但是,我在网上看到的一些例子并没有让我感到高兴.
这是排序
tempdict = collections.OrderedDict(sorted(tempdict.items()))
Run Code Online (Sandbox Code Playgroud)
现在我正在努力:
reverse = collections.OrderedDict(tempdict.items()[::-1])
reverse = collections.OrderedDict(map(reversed, tempdict.items()))
Run Code Online (Sandbox Code Playgroud)
但这些都行不通.什么是最智能和最优雅的字典排序方式.是的我知道,字典并不是真的用于排序,但这对我们很有用.谢谢.
我想总体上说明我们当前的谈话.我正在耕种工作,想知道目前的进展情况.因此,如果我将100作业发送到10处理器,我如何显示返回的当前作业数量.我可以获取id但是如何从我的map函数中计算已完成的返回作业的数量.
我正在调用我的函数如下:
op_list = pool.map(PPMDR_star, list(varg))
Run Code Online (Sandbox Code Playgroud)
在我的功能中,我可以打印当前名称
current = multiprocessing.current_process()
print 'Running: ', current.name, current._identity
Run Code Online (Sandbox Code Playgroud) 在我的蓝图中拾取我的mongo对象的正确方法是什么?
这是我如何拥有我的父母login.py:
app.config.from_object('config')
from flask.ext.pymongo import PyMongo
from child import child
from child2 import child2
app = Flask(__name__)
app.register_blueprint(child2.child2)
app.register_blueprint(child.child)
Run Code Online (Sandbox Code Playgroud)
在我的 child.py
from app import app
from flask.ext.pymongo import PyMongo
mongo = PyMongo(app)
child = Blueprint('child', __name__)
Run Code Online (Sandbox Code Playgroud)
child2.py 与孩子的结构相同:
from app import app
from flask.ext.pymongo import PyMongo
mongo = PyMongo(app)
child2 = Blueprint('child2', __name__)
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
raise Exception('duplicate config_prefix "%s"' % config_prefix)
Exception: duplicate config_prefix "MONGO"
Run Code Online (Sandbox Code Playgroud)
我在蓝图中尝试了以下内容
mongo = app.data.driver
Run Code Online (Sandbox Code Playgroud)
但这给了:
AttributeError: 'Flask' object has no attribute 'data'
Run Code Online (Sandbox Code Playgroud)
一旦我的应用创建了连接,我应该如何在我的蓝图中提取它? …
我遇到的问题是从字符串转换为整数以创建平均值.我知道在以后的版本中使用$ convert,但我找不到使用$ toInt术语的正确位置.我知道使用此关键字的转换在单个示例的命令行上有效,但我应该将它放在聚合框架中的哪个位置.
db.my_batch.aggregate([{"$unwind": "$current.Data.x"}, {"$match": {"current.Data.x.Typ": "01", "current.Data.x.Value": {"$lt": "TTTT"}}}, {"$project": {"current.Data.x.Value": 1, "uId":1}}, {"$group": {"_id": null, "ad": {"$avg": {"$toInt": "$current.Data.x.Value"}}}} ])
Run Code Online (Sandbox Code Playgroud)
我收到以下回复:
2018-07-20T17:19:42.707+0200 E QUERY [thread1] Error: command failed: {
"ok" : 0,
"errmsg" : "Unrecognized expression '$toInt'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"
} : aggregate failed :
Run Code Online (Sandbox Code Playgroud) 在jinja中有urlenclode过滤器,可以使用urlencode,但我正在寻找我使用的加号版本{{ url | urlencode }}.是否有现成的或者是定制过滤器的时间?
我正在建立一个网站,我没有index.py当我输入我的http:// localhost:8080 /时,如何确保index.html出现?
我有yaml
- url: /
static_files: static/HTML
upload: static/HTML/index.html
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.
我正在使用LinkedIn身份验证:http://developer.linkedin.com/documents/authentication我需要发帖.我熟悉GET请求,例如:
client = oauth.Client(consumer, access_token)
resp,content = client.request("http://api.linkedin.com/v1/people/~?format=json", "GET", "")
Run Code Online (Sandbox Code Playgroud)
哪个使用了Oauth身份验证.但是对于Post部分的身份验证,我必须POST一个示例字符串,例如:
https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code
&code=AUTHORIZATION_CODE
&redirect_uri=YOUR_REDIRECT_URI
&client_id=YOUR_API_KEY
&client_secret=YOUR_SECRET_KEY
Run Code Online (Sandbox Code Playgroud)
而我的简单问题涉及理解最好的方法是什么 - 不使用Oauth(假设这是正确的方法),因为我已经返回了AUTHORIZATION_CODE.如果这是一个简单的问题,请道歉.
换句话说,我有一个上述形式的字符串,现在,我如何执行帖子?
我尝试过使用cURL.但我发现代码在邮政行中失败了:c.perform().这是我正在使用的代码:
accesscode = request.GET.get('code')
redirect_uri = 'http://www.example.com'
url_post = 'https://www.linkedin.com/uas/oauth2/accessToken'
postdata = urllib2.quote('grant_type=authorization_code&code='+accesscode+'&redirect_uri='+redirect_uri+'&client_id='+consumer_key+'&client_secret='+consumer_secret)
#return HttpResponse(postdata)
c = pycurl.Curl()
c.setopt(c.URL, url_post)
c.setopt(c.POSTFIELDS, postdata)
c.setopt(c.VERBOSE, True)
c.setopt(c.FAILONERROR, True)
c.perform()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error at /loginsuccess/
(22, 'The requested URL returned error: 400 Bad Request')
Run Code Online (Sandbox Code Playgroud)
以下是我如何获取身份验证的第一阶段的代码(如LI文档所示):
def login(request):
redirect_uri = urllib2.quote('http://127.0.0.1:8080/loginsuccess')
codeURL = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=c3skrqz5wqmm&scope=r_fullprofile&state=DCEEFWF45453sdffef425&redirect_uri=" + redirect_uri
return HttpResponseRedirect(codeURL)
Run Code Online (Sandbox Code Playgroud)
这是我使用下面建议的代码发布数据后得到的最新回溯.在跟踪之后,我将提出一些问题. …
我正在尝试在OpenShift上安装python包,但我看到关于最佳方法的页面缺乏.有人可以建议最好的方式来说oauth2和simplejson.我已经尝试将这些包含在内setup.py,但我不知道这些是否实际可用,或者我必须上传并指向路径.我怎样才能做到这一点?
我有一系列按顺序调用的正则表达式。我需要检查第一个,然后是第二个,然后是第三个等等,直到最后。我需要对匹配的字符串进行一些处理,所以我试图避免太多的逻辑,但在 python 中,与 perl 不同,我不认为我可以在 ..blocks 中执行赋值,if-elif-elif所以我最终会做一个赋值,然后检查匹配项并获取该匹配项的结果。例如:
m = re.search(patternA, string)
if m:
stripped = m.group(0)
xyz = stripped[45:67]
elif:
m = re.search(patternB, string)
if m:
stripped = m.group(0)
abc = stripped[5:7]
elif:
m = re.search(patternB, string)
if m:
stripped = m.group(0)
txt = stripped[4:5]
elif:
......
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望找到一个更好的结构,确保保留测试的正则表达式的顺序,并且可以将赋值合并到 if-then 语句中。例如:
if (m = re.search(patternA, string)):
stripped = m.group(0)
xyz = stripped[45:67]
elif (m = re.search(patternB, string)):
stripped = m.group(0)
abc = stripped[5:7]
...
Run Code Online (Sandbox Code Playgroud)
处理这个问题最Pythonic的方法是什么?谢谢。
用例是读取旧数据——非常旧的数据。但是,每个字符串可能包含有关特定值的信息,并且仅当正则表达式与特定模式匹配时,这些信息才会出现。因此提取的变量高度依赖于匹配的内容。
我正在使用quote_plusfromurllib与它的表兄弟一起工作得很好unquote_plus,以便在空间之间产生优势,这样:The Cat Sat变得The+Cat+Sat很棒。然而现在的问题是,使用这个会影响路径,当我变成/description/id/34/the cat sat on这样的时候
%2Fdescription%2F274%2Fthe+cat+sat+on
Run Code Online (Sandbox Code Playgroud)
我应该怎么办。我真的想要空格的加号,但我在只替换加号的空格之间左右为难。在建立道路时是否有适当的治疗方法?我真的很想保留斜杠和加号。
烧瓶模板视图:
<a href="{{animal.url|quote_plus}}">{{animal.title}}</a>
Run Code Online (Sandbox Code Playgroud)
在代码.py中:
animal["url"] = "/description/"+str(animal["id"]) + "/" + animal["title"]
Run Code Online (Sandbox Code Playgroud)
还:
app.jinja_env.filters['quote_plus'] = lambda u: quote_plus(u)
Run Code Online (Sandbox Code Playgroud)