In [20]: print None or False
-------> print(None or False)
False
In [21]: print False or None
-------> print(False or None)
None
Run Code Online (Sandbox Code Playgroud)
这种行为让我很困惑.有人可以向我解释为什么会发生这种情况?我希望他们两个都表现得一样.
是否可以使用对象的slug(或任何其他字段)来访问项目的详细信息,而不是使用ID?
例如,如果我有一个带有slug"lorem"和ID 1的项目.默认情况下,URL是http://localhost:9999/items/1/.我希望通过它来访问它http://localhost:9999/items/lorem/.
添加lookup_field序列化程序的Meta类没有改变自动生成的URL,也没有允许我通过手动编写slug而不是URL中的ID来访问该项目.
models.py
class Item(models.Model):
slug = models.CharField(max_length=100, unique=True)
title = models.CharField(max_length=100, blank=True, default='')
# An arbitrary, user provided, URL
item_url = models.URLField(unique=True)
Run Code Online (Sandbox Code Playgroud)
serializers.py
class ClassItemSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Item
fields = ('url', 'slug', 'title', 'item_url')
Run Code Online (Sandbox Code Playgroud)
views.py
class ItemViewSet(viewsets.ModelViewSet):
queryset = Item.objects.all()
serializer_class = ItemSerializer
Run Code Online (Sandbox Code Playgroud)
urls.py
router = DefaultRouter()
router.register(r'items', views.ItemViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
]
Run Code Online (Sandbox Code Playgroud)
生成的JSON:
[
{
"url": "http://localhost:9999/items/1/",
"slug": "lorem",
"title": "Lorem",
"item_url": "http://example.com"
}
]
Run Code Online (Sandbox Code Playgroud) 我试图找到一个矩阵的特征值乘以它的转置,但我不能用numpy做.
testmatrix = numpy.array([[1,2],[3,4],[5,6],[7,8]])
prod = testmatrix * testmatrix.T
print eig(prod)
Run Code Online (Sandbox Code Playgroud)
我希望得到以下产品结果:
5 11 17 23
11 25 39 53
17 39 61 83
23 53 83 113
Run Code Online (Sandbox Code Playgroud)
和特征值:
0.0000
0.0000
0.3929
203.6071
Run Code Online (Sandbox Code Playgroud)
相反,我ValueError: shape mismatch: objects cannot be broadcast to a single shape在乘以testmatrix它的转置时得到了.
这在MatLab中工作(乘法,而不是代码),但我需要在python应用程序中使用它.
有人能告诉我我做错了什么吗?
假设我有以下代码:
a_list = [[0]*10]*10
Run Code Online (Sandbox Code Playgroud)
这会生成以下列表:
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, …Run Code Online (Sandbox Code Playgroud) 我有一个模型,订单,在管理面板中有一个动作,让管理员将订单信息发送给该订单列出的某些人.每个人都有语言集,这是消息应该发送的语言.
我正在使用的简短版本:
from django.utils.translation import ugettext as _
from django.core.mail import EmailMessage
lang = method_that_gets_customer_language()
body = _("Dear mister X, here is the information you requested\n")
body += some_order_information
subject = _("Order information")
email = EmailMessage(subject, body, 'customer@example.org', ['admin@example.org'])
email.send()
Run Code Online (Sandbox Code Playgroud)
有关他使用的语言的客户信息,请参阅lang.默认语言为en-us,翻译为法语(fr)和德语(de).
有没有办法使用翻译为指定的语言的方式lang进行body,并subject再切换回EN-US?例如:lang是'de'.主题和正文应该获得'de'翻译文件中指定的字符串.
编辑:
找到了解决方案.
from django.utils import translation
from django.utils.translation import ugettext as _
body = "Some text in English"
translation.activate('de')
print "%s" % _(body)
translation.activate('en')
Run Code Online (Sandbox Code Playgroud)
这需要body变量,将其翻译为德语,打印它然后将语言返回到英语.
就像是 …
我声明性地定义了以下表(非常简化的版本):
class Profile(Base):
__tablename__ = 'profile'
id = Column(Integer, primary_key = True)
name = Column(String(65), nullable = False)
def __init__(self, name):
self.name = name
class Question(Base):
__tablename__ = 'question'
id = Column(Integer, primary_key = True)
description = Column(String(255), nullable = False)
number = Column(Integer, nullable = False, unique = True)
def __init__(self, description, number):
self.description = description
self.number = number
class Answer(Base):
__tablename__ = 'answer'
profile_id = Column(Integer, ForeignKey('profile.id'), primary_key = True)
question_id = Column(Integer, ForeignKey('question.id'), primary_key = True)
value …Run Code Online (Sandbox Code Playgroud) 我有以下lambda函数:
f = lambda x: x == None and '' or x
Run Code Online (Sandbox Code Playgroud)
如果它接收None作为参数,它应该返回一个空字符串,如果它不是None,则返回参数.
例如:
>>> f(4)
4
>>> f(None)
>>>
Run Code Online (Sandbox Code Playgroud)
如果我调用f(None)而不是获得一个空字符串,我会得到None.我打印了函数返回的类型,我得到了NoneType.我在期待弦乐.
type('')返回字符串,所以我想知道为什么当我将None作为参数传递时,lambda不会返回空字符串.
我对lambdas很新,所以我可能误解了一些关于它们如何工作的事情.
我有一个应用程序,打开多个子窗口小部件作为单独的窗口,如下所示:window1打开窗口2,打开窗口3(简化形式).
在主窗口中,我将CTRL + Q设置为退出快捷方式.下面是主类的精简示例.
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.actionExit = QtGui.QAction(_('E&xit'),self)
self.actionExit.setShortcut('Ctrl+Q')
self.actionExit.setStatusTip(_('Close application'))
self.connect(self.actionExit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
Run Code Online (Sandbox Code Playgroud)
现在,如果我打开第三个孩子并推动CTRL + Q没有任何反应.是否有一种方法可以让孩子继承退出的快捷键或使快捷方式成为全局,或者我必须在每个中声明它?
In [1]: class T(object):
...: pass
...:
In [2]: x = T()
In [3]: print(x)
<__main__.T object at 0x03328E10>
In [4]: x = T()
In [5]: print(x)
<__main__.T object at 0x03345090>
Run Code Online (Sandbox Code Playgroud)
何时T()释放分配给第一个对象(0x03328E10)的内存位置?是在x覆盖变量还是运行垃圾收集器或脚本结束时?
我假设它是垃圾收集器运行的时候,但我不知道如何测试这个假设.
我正在使用/tracksAPI端点来检索播放计数和收藏计数等歌曲统计数据.我注意到API中的播放计数与网站上的播放计数不同.
我在这里找到了一个解决方法/sf/answers/2588994061/,但似乎使用它可能是针对Soundcloud ToS,虽然我没有在ToS中找到具体的文章.最重要的是,我宁愿不使用未记录的API,因为它可能会更改或删除,恕不另行通知.
检索轨道的实际播放计数的正确方法是什么?
python ×7
django ×2
eigenvalue ×1
lambda ×1
list ×1
numpy ×1
pyqt ×1
pyqt4 ×1
python-3.x ×1
scipy ×1
soundcloud ×1
sqlalchemy ×1
translation ×1