小编Kri*_*ian的帖子

如何只在Heroku https上制作python?

我在Heroku(Cedar堆栈)上有python/django应用程序,并希望只通过https访问它.我启用了"ssl piggyback"-option,并可以通过https连接到它.

但是,禁用http访问或重定向到https的最佳方法是什么?

python django ssl https heroku

55
推荐指数
5
解决办法
2万
查看次数

如何在Django中创建一个非空的CharField?

我有一个简单的模型,看起来像这样:

class Group(models.Model):
    name = models.CharField(max_length = 100, blank=False)
Run Code Online (Sandbox Code Playgroud)

我希望这会抛出完整性错误,但它不会:

group = Group() # name is an empty string here
group.save()
Run Code Online (Sandbox Code Playgroud)

如何确保name变量设置为非空?即使数据库拒绝任何保存空字符串的尝试?

django django-models

23
推荐指数
4
解决办法
9641
查看次数

是否有更多的pythonic方法可以在列表中找到最接近另一个点的点?

我有一个2d点的列表,并希望找到最接近给定点的那个.下面的代码(get_closest_point())做我想要的.但是在python中有更好的方法吗?

class Circle(object):
    def __init__(self, pos):
        self.position = pos


class Point(object):
    ..
    def compute_distance_to(self, p)
        ..

class SomeClient(object):
    ..

    def get_closest_point(self, points, p1):
        closest = (None, float(sys.maxint))
        for p2 in points:
            distance = p2.compute_distance_to(p1) 
            if distance < closest[1]:
                closest = (p2, distance)

        return closest[0]

    def get_closest_circle(self, circles, p1):
        closest = (None, float(sys.maxint))
        for c in circles:
            distance = c.position.compute_distance_to(p1) 
            if distance < closest[1]:
                closest = (c, distance)

        return closest[0]
Run Code Online (Sandbox Code Playgroud)

python

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

QWidget*QApplication :: mainWidget()在Qt4中变成了什么?

我正在将应用程序从Qt3移植到Qt4,需要Qt4替换QApplication :: mainWidget(),它用于返回Qt3中的顶级窗口小部件.有没有人知道如何在Qt4中这样做?

c++ qt4

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

MapReduce,Python和NetworkX

我为使用NetworkX在Python中构建的图实现了非加权随机游动函数。下面是我的程序中处理随机游走的片段。在程序的其他地方,我有一个创建图的方法,并且有一个方法模拟我编写的各种自定义图测试方法。这些图测试方法之一是从图中随机选择两个节点,然后在两个节点之间进行随机游走。根据此随机游走计算出的两件事是:命中时间(从起点到终点经过的链接数)和通勤时间(从起点到终点再回到起点的经过的链接数) )。

def unweighted_random_walk(starting_point,ending_point, graph):
    '''
    starting_point: String that represents the starting point in the graph
    ending_point: String that represents the ending point in the graph
    graph: A NetworkX Graph object
    '''
    ##Begin the random walk
    current_point=starting_point
    #current_node=graph[current_point]
    current_point_neighors=graph.neighbors(current_point)
    hitting_time=0

    #Determine the hitting time to get to an arbitrary neighbor of the
    #starting point
    while current_point!=ending_point:
        #pick one of the edges out of the starting_node with equal probs
        possible_destination=current_point_neighbors[random.randint(0,current_point_neighors)]
        current_point=possible_destination
        current_point_neighbors=graph.neighbors(current_point)
        hitting_time+=1
    return hitting_time
Run Code Online (Sandbox Code Playgroud)

我的随机游走代码非常简单明了,因为我只是选择随机节点直到到达终点。但是,当我尝试运行几次随机游走时,此当前实现非常慢(我认为我有时需要跑一百万步)。

我的问题是:有什么方法可以使用Hadoop MapReduce并行化此随机游走在此处进行的某些操作?我有更好的方式随机行走吗?

python hadoop mapreduce networkx

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

你如何断言所有std :: vector <std :: string>都是一行中的给定大小?

我有一个方法接受sha1哈希的std :: vector作为字符串,其长度必须为20个字符.在单行中断言这个前提条件得到尊重将是很好的.

void MyClass::setSha1Sums(const std::vector<std::string>& sha1Sums)
{
  assert(magic_oneliner_which_verifies_that_all_strings_are_20_chars_long);
  sha1Sums_ = sha1Sums;
}
Run Code Online (Sandbox Code Playgroud)

c++ assert stl

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

AVX优化的代码没有在linux redhat 5.6上运行

我有一些简单的测试代码,我试图生成AVX优化代码,用于在Linux Redhat 5.6上使用icc v12.1.代码如下所示:

int main() {
    double sum = 0.0;
    for (unsigned int i = 0; i < 1024; i++) {
        sum += static_cast<double>(i);
    }

    std::cout << "Sum: "<< sum << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我用它编译它(并且矢量报告说循环被矢量化):

icc -xavx -vec-report1  main.cpp
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,我收到以下错误:

Fatal Error: This program was not built to run in your system.
Please verify that both the operating system and the processor support Intel(R) AVX.
Run Code Online (Sandbox Code Playgroud)

我确信该处理器具有AVX功能,但是其他人是否在Redhat 5.6上遇到AVX问题?

linux redhat icc avx

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

标签 统计

python ×3

c++ ×2

django ×2

assert ×1

avx ×1

django-models ×1

hadoop ×1

heroku ×1

https ×1

icc ×1

linux ×1

mapreduce ×1

networkx ×1

qt4 ×1

redhat ×1

ssl ×1

stl ×1