我正在学习Python我没有得到一件事.考虑以下代码:
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def __getitem__(self,index):
print "index",index
return self.items[index]
def __len__(self):
return len(self.items)
stack = Stack()
stack.push(2)
stack.push(1)
stack.push(0)
for item in stack:
print item
Run Code Online (Sandbox Code Playgroud)
和输出
index 0
2
index 1
1
index 2
0
index 3
Run Code Online (Sandbox Code Playgroud)
为什么getitem被召唤四次?
这是我的Python代码
DosCmd = 'matlab -wait -automation -nosplash -r "run \'' + to_run + "'\""
os.system(DosCmd)
curve_file = open('curve/'+str(index)+'.curve','r')
Run Code Online (Sandbox Code Playgroud)
我在 python 脚本中运行 .m 文件,它工作正常,但执行 .m 文件后,它卡在os.system(DosCmd). 为了让 python 运行以下代码,我必须关闭这个窗口:

由于这部分代码处于循环中,这确实让我感到不安。我在网上发现有人说matlab在执行.m文件后可以自动退出,但我的却没有。有人能告诉我我做错了什么或者我应该做什么吗?谢谢!
假设我有一个具有以下上下文的 Django 模板:
data1 = "this is data1"
data2 = "this is data2"
data_name = "data2"
Run Code Online (Sandbox Code Playgroud)
现在我知道data_name(假设它是"data2")的值,是否可以使用它来访问变量data2?
为了让我的意图更清楚,这就是你可以用 Python 做到的方式
>>> a = 1
>>> b = 2
>>> name = 'a'
>>> locals()[name]
1
>>> name = 'b'
>>> locals()[name]
2
Run Code Online (Sandbox Code Playgroud) 我在一张SNS相册中看到了这两个陈述,标题是"我见过的最优雅的输出方式"等等.
以下是两个陈述:
printf("%d%c", a, " \n"[i==n]);
puts("YES\0No"+condition * 4);
Run Code Online (Sandbox Code Playgroud)
我不知道他们在做什么以及他们是如何工作的.有人会向我解释一下吗?谢谢.
我需要在自己的项目中完成一个登录任务.幸运的是我发现有人已经完成了.
这是相关的代码.
import re,urllib,urllib2,cookielib
class Login():
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
def __init__(self,name='',password='',domain=''):
self.name=name
self.password=password
self.domain=domain
urllib2.install_opener(self.opener)
def login(self):
params = {'domain':self.domain,'email':self.name,'password':self.password}
req = urllib2.Request(
website_url,
urllib.urlencode(params)
)
self.openrate = self.opener.open(req)
print self.openrate.geturl()
info = self.openrate.read()
Run Code Online (Sandbox Code Playgroud)
我已经测试了代码,它运行得很好(根据info).
现在我想将它移植到Python 3以及使用requestslib而不是urllib2.
我的想法:
opener,虽然不确定,我认为它的等价物requests是requests.Session jar = cookiejar.CookieJar()在提出要求时通过?不确定.我尝试过类似的东西
import requests
from http import cookiejar
from urllib.parse import urlencode
jar = cookiejar.CookieJar()
s = requests.Session()
s.post(
website_url,
data = urlencode(params),
allow_redirects = …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以使用concurrent.futuresFlask.这是一个例子.
import requests
from flask import Flask
from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor(max_workers=10)
app = Flask(__name__)
@app.route("/path/<xxx>")
def hello(xxx):
f = executor.submit(task, xxx)
return "OK"
def task():
resp = requests.get("some_url")
# save to mongodb
app.run()
Run Code Online (Sandbox Code Playgroud)
任务是IO绑定的,不需要返回值.请求不会经常发生,我估计最多10/s.
我测试了它,它工作.我想知道的是我是否可以通过这种方式使用多线程来提升性能.Flask会以某种方式阻止任务吗?
我想将一些图像附加到立方体的几个侧面,然后将其他侧面留空。我很难让它发挥作用。如果我将所有边设置为一种材料,我可以加载文件并正常显示它,但是如果我尝试只设置一侧来显示图像,它只是空白。
var textureLoader = new THREE.TextureLoader();
var texture0 = textureLoader.load( 'images/0.png' );
var texture1 = textureLoader.load( 'images/1.png' );
var texture2 = textureLoader.load( 'images/2.png' );
var texture3 = textureLoader.load( 'images/3.png' );
var texture4 = textureLoader.load( 'images/4.png' );
var texture5 = textureLoader.load( 'images/5.png' );
var cubeMaterials = [
new THREE.MeshBasicMaterial( { map: texture0 } ),
new THREE.MeshBasicMaterial( { map: texture1 } ),
new THREE.MeshBasicMaterial( { map: texture2 } ),
new THREE.MeshBasicMaterial( { map: texture3 } ),
new THREE.MeshBasicMaterial( { map: texture4 } …Run Code Online (Sandbox Code Playgroud) 所以我想了解dir()功能的细节。首先我查看了它的实现:
https://github.com/python/cpython/blob/e76daebc0c8afa3981a4c5a8b54537f756e805de/Objects/object.c#L1450-L1477
/* Helper for PyObject_Dir: object introspection. */
static PyObject *
_dir_object(PyObject *obj)
{
PyObject *result, *sorted;
PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
assert(obj);
if (dirfunc == NULL) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
return NULL;
}
/* use __dir__ */
result = _PyObject_CallNoArg(dirfunc);
Py_DECREF(dirfunc);
if (result == NULL)
return NULL;
/* return sorted(result) */
sorted = PySequence_List(result);
Py_DECREF(result);
if (sorted == NULL)
return NULL;
if (PyList_Sort(sorted)) {
Py_DECREF(sorted);
return NULL;
}
return sorted;
} …Run Code Online (Sandbox Code Playgroud) 我使用 Context 类:
\n\nclass Context(object):\n ...\nRun Code Online (Sandbox Code Playgroud)\n\n所以我可以使用这个类来定义一个支持 getattr 和 setattr 的对象。我想使用该对象在一些线程之间进行通信。
\n\n但是我认为让用户定义这样的类是愚蠢的。所以我想找出标准库中是否有支持 getattr 和 setattr 的原始类型或类。
\n\n我已经尝试过object,但它的对象无法设置属性:
a = object()\na.b = 1\n\nclass C(object):\n ...\n\nc = C()\nc.d = 1\nRun Code Online (Sandbox Code Playgroud)\n\n我可以设置c.d = 1,但a.b抱怨'object' object has no attribute 'b'\xe3\x80\x82