我已经阅读了Python中的什么类方法?但那篇文章中的例子很复杂.我正在寻找Python中类方法的特定用例的清晰,简单,简单的例子.
您能说出一个小的,具体的示例用例,其中Python类方法将是该工作的正确工具吗?
我试图调用DynamoDB客户端方法并从DynamoDB表中获取一个项目.我正在使用AWS Lambda.但是,我一直收到消息:
"在完成请求之前退出流程."
我已经增加了超时以确保,但处理时间小于超时.有什么建议?
console.log('Loading event');
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = function(event, context) {
dynamodb.listTables(function(err, data) {
});
var params = {
"TableName": "User",
"Key":
{"User Id" : {"S":event.objectId}
},
"AttributesToGet" : ["First Name","Last Name", "Latitude", "Longitude"],
"ConsistentRead" : true
}
dynamodb.getItem(params, function(response,result) {
response.on('data', function(chunk){
console.log(""+chunk);
console.log("test1")
context.done(result);
});
result.on('ready', function(data){
console.log("test2")
console.log("Error:" + data.error);
console.log("ConsumedCapacityUnits:" + data.ConsumedCapacityUnits);
context.done('Error',data);
// ...
});
});
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个带有固定标题和可滚动内容区域的网页.当标题具有已知高度时,这是微不足道的,但是当标题流畅时,我正在努力寻找解决方案.
我想要的布局是:
--------------
head
--------------
content
--------------
Run Code Online (Sandbox Code Playgroud)
其中"head"是其内容需要的高度,"content"没有最小高度,但在变为可滚动之前将达到视口底部的最大高度.
这些天在纯CSS中这可能吗?我的目标是IE8 +.
为了澄清我想要的东西,如果我知道标题的高度,我会怎么做:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
margin: 0;
}
#head {
background: yellow;
height: 20px; /* I can't rely on knowing this. */
}
#content {
background: red;
position: absolute;
top: 20px; /* here also */
bottom: 0;
width: 100%;
overflow: auto;
}
</style>
</head>
<body>
<div id="head">some variable height content</div>
<div id="content">
scrollable content<br/>
scrollable content<br/>
scrollable content<br/>
scrollable content<br/>
scrollable content<br/>
scrollable content<br/> …Run Code Online (Sandbox Code Playgroud) 我有一个自定义经理.我想将它用于相关对象.我在docs中找到了 use_for_related_fields.但它不像我使用它的方式工作:
class RandomQueryset(models.query.QuerySet):
def randomize(self):
count = self.count()
random_index = random.randint(0, count - 1)
return self.all()[random_index]
class RandomManager(models.Manager):
use_for_related_fields = True
def get_query_set(self):
return RandomQueryset(self.model, using=self._db)
def randomize(self):
return self.get_query_set().randomize()
Run Code Online (Sandbox Code Playgroud)
我用它作为一个型号:
>>> post = PostPages.default_manager.filter(image_gallery__isnull=False).distinct().randomize()
Run Code Online (Sandbox Code Playgroud)
并试图对m2m相关对象做同样的事情:
>>> post.image_gallery.randomize()
Run Code Online (Sandbox Code Playgroud)
出了错误:
AttributeError: 'ManyRelatedManager' object has no attribute 'randomize'
Run Code Online (Sandbox Code Playgroud)
是否可以像我一样使用自定义管理器?如果是这样,你如何使它工作?
我的模特:
class ShivaImage(models.Model, ImageResizing):
image = models.ImageField(upload_to='img')
slide_show = models.BooleanField()
title = models.CharField(max_length=100)
text = models.TextField(max_length=400)
ordering = models.IntegerField(blank=True, null=True)
objects = RandomManager()
class PostPages(models.Model):
image_gallery = models.ManyToManyField(ShivaImage, blank=True,
related_name='gallery',) …Run Code Online (Sandbox Code Playgroud) 我正在使用这些参数在DynamoDB中创建一个表和GSI,根据文档:
configId是表的主键,我使用它publisherId作为GSI的主键.(为简洁起见,我删除了一些不必要的配置参数)
var params = {
TableName: 'Configs',
KeySchema: [
{
AttributeName: 'configId',
KeyType: 'HASH',
}
],
AttributeDefinitions: [
{
AttributeName: 'configId',
AttributeType: 'S',
},
{
AttributeName: 'publisherId',
AttributeType: 'S',
}
],
GlobalSecondaryIndexes: [
{
IndexName: 'publisher_index',
KeySchema: [
{
AttributeName: 'publisherId',
KeyType: 'HASH',
}
]
}
]
};
Run Code Online (Sandbox Code Playgroud)
我用这个查询这个表:
{ TableName: 'Configs',
IndexName: 'publisher_index',
KeyConditionExpression: 'publisherId = :pub_id',
ExpressionAttributeValues: { ':pub_id': { S: '700' } } }
Run Code Online (Sandbox Code Playgroud)
但我一直收到错误:
"ValidationException:一个或多个参数值无效:条件参数类型与模式类型不匹配"
在文档中,它指定主要KeyType可以是HASH或RANGE,并且您 …
即你能做这样的事情:
.example {
display: block !important; // sass-lint: ignore
}
Run Code Online (Sandbox Code Playgroud) 我想在/etc/nginx/conf.d创建或修改目录中的任何文件时重新启动nginx服务.
该目录中有许多文件,而不是指定特定文件,我想观察所有更改.
我试过这个:
nginx:
pkg.installed:
- name: nginx
service:
- running
- enable: True
- restart: True
- watch:
- file: /etc/nginx/nginx.conf
- file: /etc/nginx/conf.d
- pkg: nginx
Run Code Online (Sandbox Code Playgroud)
但这条线- file: /etc/nginx/conf.d并没有做我想要的.
这是错误:
ID: nginx
Function: service.running
Result: False
Comment: The following requisites were not found:
watch:
file: /etc/nginx/conf.d
Changes:
Run Code Online (Sandbox Code Playgroud)
我也尝试了许多变化,包括尾部斜线,但它们都不起作用.
应该- file: /etc/nginx/conf.d/改变什么?
我创建了一个带有ImageField的简单模型,我想用django-rest-framework + django-rest-swagger创建一个api视图,该文档已记录并能够上传文件.
这是我得到的:
models.py
from django.utils import timezone
from django.db import models
class MyModel(models.Model):
source = models.ImageField(upload_to=u'/photos')
is_active = models.BooleanField(default=False)
created_at = models.DateTimeField(default=timezone.now)
def __unicode__(self):
return u"photo {0}".format(self.source.url)
Run Code Online (Sandbox Code Playgroud)
serializer.py
from .models import MyModel
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = [
'id',
'source',
'created_at',
]
Run Code Online (Sandbox Code Playgroud)
views.py
from rest_framework import generics
from .serializer import MyModelSerializer
class MyModelView(generics.CreateAPIView):
serializer_class = MyModelSerializer
parser_classes = (FileUploadParser, )
def post(self, *args, **kwargs):
"""
Create a MyModel
---
parameters:
- name: source
description: …Run Code Online (Sandbox Code Playgroud) 对于字典,我可以iter()用来迭代字典的键.
y = {"x":10, "y":20}
for val in iter(y):
print val
Run Code Online (Sandbox Code Playgroud)
当我有迭代器如下,
class Counter:
def __init__(self, low, high):
self.current = low
self.high = high
def __iter__(self):
return self
def next(self):
if self.current > self.high:
raise StopIteration
else:
self.current += 1
return self.current - 1
Run Code Online (Sandbox Code Playgroud)
为什么我不能这样使用它
x = Counter(3,8)
for i in x:
print x
Run Code Online (Sandbox Code Playgroud)
也不
x = Counter(3,8)
for i in iter(x):
print x
Run Code Online (Sandbox Code Playgroud)
但这样呢?
for c in Counter(3, 8):
print c
Run Code Online (Sandbox Code Playgroud)
功能的用途是iter()什么?
我想这可能是如何iter() …
python ×4
django ×3
aws-lambda ×1
aws-sdk ×1
class ×1
class-method ×1
css ×1
django-orm ×1
iterator ×1
lint ×1
nginx ×1
node.js ×1
orm ×1
salt-stack ×1
sass ×1
service ×1
swagger ×1