我来自Java世界并阅读Bruce Eckels的Python 3模式,食谱和成语.
在阅读类时,接着说在Python中没有必要声明实例变量.你只需在构造函数中使用它们,然后繁荣,它们就在那里.
例如:
class Simple:
def __init__(self, s):
print("inside the simple constructor")
self.s = s
def show(self):
print(self.s)
def showMsg(self, msg):
print(msg + ':', self.show())
Run Code Online (Sandbox Code Playgroud)
如果这是真的,那么类的任何对象都Simple可以只改变s类外的变量值.
例如:
if __name__ == "__main__":
x = Simple("constructor argument")
x.s = "test15" # this changes the value
x.show()
x.showMsg("A message")
Run Code Online (Sandbox Code Playgroud)
在Java中,我们学习了有关公共/私有/受保护变量的知识.这些关键字是有意义的,因为有时你想要类中的变量,类外没有人可以访问.
为什么Python中不需要这样做?
如何将django Model对象转换为包含其所有字段的dict ?理想情况下,所有内容都包含外键和带有editable = False的字段.
让我详细说明一下.假设我有一个类似以下的django模型:
from django.db import models
class OtherModel(models.Model): pass
class SomeModel(models.Model):
normal_value = models.IntegerField()
readonly_value = models.IntegerField(editable=False)
auto_now_add = models.DateTimeField(auto_now_add=True)
foreign_key = models.ForeignKey(OtherModel, related_name="ref1")
many_to_many = models.ManyToManyField(OtherModel, related_name="ref2")
Run Code Online (Sandbox Code Playgroud)
在终端中,我做了以下事情:
other_model = OtherModel()
other_model.save()
instance = SomeModel()
instance.normal_value = 1
instance.readonly_value = 2
instance.foreign_key = other_model
instance.save()
instance.many_to_many.add(other_model)
instance.save()
Run Code Online (Sandbox Code Playgroud)
我想将其转换为以下字典:
{'auto_now_add': datetime.datetime(2015, 3, 16, 21, 34, 14, 926738, tzinfo=<UTC>),
'foreign_key': 1,
'id': 1,
'many_to_many': [1],
'normal_value': 1,
'readonly_value': 2}
Run Code Online (Sandbox Code Playgroud)
回答不满意的问题:
是否有可能在IPython笔记本中以某种不同的颜色显示某些输出?例如,有些东西:
print("Hello Red World", color='red')
Run Code Online (Sandbox Code Playgroud) 跑步时
pip install pylibmc
Run Code Online (Sandbox Code Playgroud)
在Ubuntu上,我收到以下错误:
_pylibmcmodule.h:42:36: fatal error: libmemcached/memcached.h: No such file or directory
Run Code Online (Sandbox Code Playgroud) 在python中使用multiprocessing.Pool时,如下代码,有一些奇怪的行为.
from multiprocessing import Pool
p = Pool(3)
def f(x): return x
threads = [p.apply_async(f, [i]) for i in range(20)]
for t in threads:
try: print(t.get(timeout=1))
except Exception: pass
Run Code Online (Sandbox Code Playgroud)
我得到以下错误三次(池中的每个线程一个),并打印"3"到"19":
AttributeError: 'module' object has no attribute 'f'
Run Code Online (Sandbox Code Playgroud)
前三个apply_async调用永远不会返回.
同时,如果我尝试:
from multiprocessing import Pool
p = Pool(3)
def f(x): print(x)
p.map(f, range(20))
Run Code Online (Sandbox Code Playgroud)
我得到了AttributeError 3次,shell打印"6"到"19",然后挂起并且不能被[Ctrl] + [C]杀死
多处理文档有以下说法:
此程序包中的功能要求主模块可由子项导入.
这是什么意思?
为了澄清,我在终端中运行代码来测试功能,但最终我希望能够将其放入Web服务器的模块中.你如何在python终端和代码模块中正确使用multiprocessing.Pool?
我有一个AWS_ACCESS_KEY_ID和一个AWS_SECRET_KEY.这些是活动凭据,因此它们属于属于AWS账户的活动用户.如何使用Boto3找到此AWS账户的ID?
我已经配置了我的Elastic Beanstalk环境以将所有页面重定向到https,重定向工作,但是,实例未通过运行状况检查并终止,任何想法如何配置重写规则?
我的配置:
NameVirtualHost *:80
<VirtualHost *:80>
.
.
.
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule !/_hostmanager/healthcheck https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</VirtualHost>
Run Code Online (Sandbox Code Playgroud) apache mod-rewrite https amazon-web-services amazon-elastic-beanstalk
在python中,在Ubuntu服务器上,我试图让requests库发出https请求,如下所示:
import requests
requests.post("https://example.com")
Run Code Online (Sandbox Code Playgroud)
起初,我得到以下内容:
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90:InsecurePlatformWarning:真正的SSLContext对象不可用.这可以防止urllib3正确配置SSL,并可能导致某些SSL连接失败.有关更多信息,请参阅 https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
遵循此问题中的建议:InsecurePlatformWarning:真正的SSLContext对象不可用.这可以防止urllib3正确配置SSL,我现在已将警告升级为以下错误:
AttributeError:'_socketobject'对象没有属性'set_tlsext_host_name'
我该如何解决?
django中有没有办法不需要{% load static %}每个模板的顶部?
此问题表明您可以将常用加载标记分解为设置,但在这种情况下不提供您需要的详细信息.
我有一个代码如下:
from django.utils.timezone import get_current_timezone, make_aware
make_aware(some_datetime, get_current_timezone())
Run Code Online (Sandbox Code Playgroud)
该make_aware呼叫偶尔引发
AmbiguousTimeError: 2013-11-03 01:23:17
Run Code Online (Sandbox Code Playgroud)
我从Django文档中了解到这是一个夏令时问题,而且这个时间戳实际上是模棱两可的.现在我该如何解决它(比如这可能是两个可能的第一个)?
python ×9
django ×4
https ×2
ubuntu ×2
apache ×1
boto3 ×1
class ×1
dictionary ×1
dst ×1
flask ×1
libmemcached ×1
memcached ×1
mod-rewrite ×1
pip ×1
pool ×1
private ×1
ssl ×1
static-files ×1
timezone ×1