小编Bel*_*dar的帖子

Django - Slug字段对于用户必须是唯一的,但对所有用户都不是

我知道我们可以要求SlugField在unique = True选项中是唯一的,但可以要求它仅对特定用户是唯一的,因此两个不同的用户可以拥有相同的SlugField,但用户不能拥有两个相同的slugField吗?

楷模.潘岳:

from django.db import models
from django.contrib.auth.models import User

class ezApp(models.Model):
    name = models.SlugField(max_length=50, unique=True )
    date_created = models.DateTimeField('date created')
    date_updated = models.DateTimeField('date updated')
    created_by = models.ForeignKey(User)
    in_use = models.BooleanField()
Run Code Online (Sandbox Code Playgroud)

django django-models

1
推荐指数
1
解决办法
346
查看次数

测试一个点是否大约在由另外两个点形成的线段上

我想确定是否在点之间进行了点击,并且大致是在连接这两点的线段上进行了点击.

我的问题类似于如何确定线段上两个点之间的点?但它推迟了两点:

  1. 我在javascript工作,坐标是整数(像素)
  2. 该点不必完全在线段上.需要宽容.

以下代码改编自这个答案,但我不知道如何在线段周围插入公差

a并且b是线段的终点,c是点击的点.我想检查是否c在连接和之间的段之间a和之间bab

PencilTool.prototype._isBetween = function(a,b,c){
    //test if a, b and c are aligned
    var crossproduct = (c.y - a.y) * (b.x - a.x) - (c.x - a.x) * (b.y - a.y);
    if(Math.abs(crossproduct) !== 0){ return false; }

    var dotproduct = (c.x - a.x) * (b.x - a.x) + (c.y - a.y)*(b.y - a.y)
    if(dotproduct < 0){ return false }

    var …
Run Code Online (Sandbox Code Playgroud)

javascript algebra linear-algebra

1
推荐指数
1
解决办法
1950
查看次数

Promise.all() - 如何在不返回 undefined 或 value 的情况下解决 ()

我想用来Promise.all()检查一个值是否在数组中。我的问题是当在数组中找不到值时,promise 返回undefined,但我只想拥有在我的数组中找到的值。

var array = [1,5,10];
var values = [1,2,3,4,5,6,7,8,9,10];
var foundValues = [];

values.forEach(function(value) {
    foundValues.push(isInArray(array, value));
});

Promise.all(foundValues).then(function(values) {
    console.log(values) // [1, undefined, undefined, undefined, 5, undefined, undefined, undefined, undefined, 10 ]
});

function isInArray(array, value) {
    return new Promise(function(resolve, reject) {
        if (array.indexOf(value) > -1) {
            resolve(value); //here the value is returned
        } else {
            resolve(); //here undefined is returned
        }
    });
};
Run Code Online (Sandbox Code Playgroud)

编辑:这个问题并不是关于在数组中找到一个值,我只是选择这个简单的例子来说明我的问题。

javascript promise

1
推荐指数
1
解决办法
2911
查看次数

PostgreSQL - 对 hstore 的查询 - 列不存在

我想知道是否有人知道hstorePostgreSQL 9.2 中的列上的这个简单查询出了什么问题

\n\n

查询在 pgAdmin 中运行

\n\n
select attributeValue->"CODE_MUN" from shapefile_feature\n
Run Code Online (Sandbox Code Playgroud)\n\n

返回:\xc2\xab 属性值\xc2\xbb 列不存在

\n\n

当做:

\n\n
select * from shapefile_feature;\n
Run Code Online (Sandbox Code Playgroud)\n\n

返回所有列,包括 attributeValue、hstore 列

\n\n

问题是什么?

\n

postgresql hstore

0
推荐指数
1
解决办法
1408
查看次数

Javascript - 字符串连接

我有这个功能:

JavaScript的:

function popup(mylink, windowname, w, h)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, "width=w,height=h,scrollbars=yes,toolbar=no" );
return false;
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<a href="test.html"  onClick="return popup(this, 'Test', '400', '600')">test</a>
Run Code Online (Sandbox Code Playgroud)

我试着去插入变量w,并h在绳子,但没有成功.在javascript中执行此操作的正确方法是什么?

javascript

0
推荐指数
2
解决办法
66
查看次数

检查元组列表中元组的第二个元素是否完全相同

我想检查元组列表中的元组的第二个元素是否完全相同

features = [(a,b), (c,b), (a,d)]
Run Code Online (Sandbox Code Playgroud)

元组的第一个元素可以是不同的.

x = []
for feature, geom_type in features:
    x.append(geom_type)
y = collections.Counter(x)
print len([i for i in y if y[i]>1])
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
1643
查看次数

对矩阵中的列进行排序

我有一个非唯一值的矩阵(或多维数组),如下所示:

var matrix = [
                [1, 3, 2, 4, 1],
                [2, 4, 1, 3, 2],
                [4, 3, 2, 1, 4]
             ]
Run Code Online (Sandbox Code Playgroud)

我想对这个矩阵的一行进行排序,但是其他行应该重新排序,以保持列像组织一样.

//matrix sorted by the row 0
var sorted_matrix = [
                      [1, 1, 2, 3, 4],
                      [2, 2, 1, 4, 3],
                      [4, 4, 2, 3, 1]
                    ]
Run Code Online (Sandbox Code Playgroud)

如果可能的话,我更喜欢lodash解决方案.

javascript sorting matrix lodash

-1
推荐指数
1
解决办法
107
查看次数

使用迭代器解压缩可变长度列表

Python 2.7中,我有一个生成器,它接收x,y坐标列表并解压缩它们.

但是,有时列表不仅包含x,y.有时它是x,y,z和其他时间x,y,z,m

xy_coords = [[0,0], [1,1], [2,2]]
xyz_coords = [[0,0,0], [1,1,1], [2,2,2]]
xyzm_coords = [[0,0,0,0], [1,1,1,1], [2,2,2,2]]

def unpack_coords(coords):
    iterator = iter(coords)
    x, y = iterator.next()
    yield x
    yield y
Run Code Online (Sandbox Code Playgroud)

当coords包含超过2个值时,我有ValueError too many values to unpack.

有没有办法处理所有可能的情况,以便只产生xy.该zm值可以忽略不计.

python python-2.7

-1
推荐指数
1
解决办法
60
查看次数

在Python中使模块方法变量成为全局良好实践吗?

我想将脚本的代码功能分成新模块.我想定义一些配置作为该模块中的参数传递.然后,在模块中,我将此配置定义为全局.这是一个好习惯吗?有更好的解决方案吗?

主要脚本:

import myModule

config = {
    "foo1" : "bar1",
    "foo2" : "bar2",
    "foo3" : "bar3"
}

myModule.execute(config)
Run Code Online (Sandbox Code Playgroud)

模块:

def execute(config):
   global CONFIG
   CONFIG = config
   value1, value2 = handleRequest()
   print(value1)
   print(value2)

def handleRequest()
   value1 = doSomething(CONFIG["foo1"])
   value2 = doSomethingElse(CONFIG["foo2"])
   return value1, value2
Run Code Online (Sandbox Code Playgroud)

python python-2.7

-3
推荐指数
1
解决办法
116
查看次数