小编pyt*_*der的帖子

TemplateDoesNotExist - Django错误

我正在使用Django Rest Framework.而且我一直收到错误

Exception Type: TemplateDoesNotExist
Exception Value: rest_framework/api.html
Run Code Online (Sandbox Code Playgroud)

我不知道我怎么会出错.这是我第一次尝试使用REST Framework.这是代码.

views.py

import socket, json
from modules.data.models import *
from modules.utils import *
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from modules.actions.serializers import ActionSerializer


@api_view(['POST'])
@check_field_exists_wrapper("installation")
def api_actions(request, format = None):

    action_type = request.POST['action_type']
    if action_type == "Shutdown" : 
        send_message = '1'
        print "Shutting Down the system..."
    elif action_type == "Enable" : 
        send_message = '1'
        print "Enabling the system..."
    elif action_type == "Disable" : 
        send_message …
Run Code Online (Sandbox Code Playgroud)

python django django-rest-framework

112
推荐指数
3
解决办法
3万
查看次数

Python - 装饰器

我正在努力学习装饰器.我理解它的概念,现在试图实现它.

这是我写 的代码代码是不言自明的.它只是检查参数是否传入int.

def wrapper(func):
    def inner():
        if issubclass(x,int): pass
        else: return 'invalid values'

    return inner()

@wrapper
def add(x,y):
    return x+y

print add('a',2)
Run Code Online (Sandbox Code Playgroud)

这是错误的说法global name 'x' is not defined.我明白它没有定义inner,但不知道如何纠正这个代码?我哪里错了?

python python-decorators

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

使用Nutch进行爬网...显示IOException

我开始使用Nutch,一切都很好,直到遇到IOException异常,

$ ./nutch crawl urls -dir myCrawl -depth 2 -topN 4
cygpath: can't convert empty path
solrUrl is not set, indexing will be skipped...
crawl started in: myCrawl
rootUrlDir = urls
threads = 10
depth = 2
solrUrl=null
topN = 4
Injector: starting at 2012-06-23 03:37:51
Injector: crawlDb: myCrawl/crawldb
Injector: urlDir: urls
Injector: Converting injected urls to crawl db entries.
Exception in thread "main" java.io.IOException: Failed to set permissions of path: \tmp\hadoop-Rahul\mapred\staging\Rahul255889423\.staging to 0700
    at org.apache.hadoop.fs.FileUtil.checkReturnValue(FileUtil.java:682)
    at org.apache.hadoop.fs.FileUtil.setPermission(FileUtil.java:655) …
Run Code Online (Sandbox Code Playgroud)

java open-source web-crawler nutch ioexception

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

如何将datetime字符串转换为UTC以在Highcharts上绘制点

我正在研究HighCharts样条不规则数据.这里数据传递为

data: [
      [Date.UTC(1970,  9, 27), 0   ],
      [Date.UTC(1970, 10, 10), 0.6 ],
      [Date.UTC(1970, 10, 18), 0.7 ],
      [Date.UTC(1970, 11,  2), 0.8 ],
      [Date.UTC(1970, 11,  9), 0.6 ],
      [Date.UTC(1970, 11, 16), 0.6 ],
      ]
Run Code Online (Sandbox Code Playgroud)

但是,我是通过AJAX调用从python后端发送数据的

data: [
      "2013-11-07 00:10:27", 0   ],
      "2013-11-07 00:10:27", 1   ],
      "2013-11-07 00:10:27", 2   ],
      "2013-11-07 00:10:27", 3   ],
      "2013-11-07 00:10:27", 4   ],
      "2013-11-07 00:10:27", 5   ],
      ]
Run Code Online (Sandbox Code Playgroud)

因此,数据点不会被绘制.如何将日期时间转换为UTC或后端(python)的任何其他日期时间格式,这将使Highcharts能够绘制正确的X和Y轴点.

这是我尝试过的.由于UTC时间Date.UTC(1970, 9, 27)返回秒数,因此我将时间转换为自幕后1970-1-1以来经过的总秒数.

time = "2013-11-06 18:00:00"
time = ((datetime.datetime.strptime(time, '%Y-%m-%d %H:%M:%S'))-datetime.datetime(1970,1,1)).total_seconds()            
Run Code Online (Sandbox Code Playgroud)

然后将其传递给UI.因此我的数据发送为

[[1383760800.0, 1870.2000000000007], …

python highcharts

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

从 Javascript 中的 if 块退出

我想从下面的 Javascript 块中退出。如果我返回,那么它不会检查下一个 if 条件。我怎么做?

if ($('#id1').length > 0) {

    if(yester_energy == "NaN" || yester_energy == 0){
      //break from #id1
    }

    else{
      //something
    }
    $("#abc").html(somthing)
}

if ($('#id2').length > 0) {

        if(yester_energy == "NaN" || yester_energy == 0){
          //break from #id2
        }

        else{
          //something
        }
}
Run Code Online (Sandbox Code Playgroud)

javascript if-statement

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

如何在大型 JSON 文件中查找唯一值?

我有大小2个JSON文件data_large(150.1mb)data_small(7.5kb)。每个文件中的内容都是[{"score": 68},{"score": 78}]. 我需要从每个文件中找到唯一分数的列表。

在处理data_small 时,我执行了以下操作,并且能够使用0.1 secs.

with open('data_small') as f:
    content = json.load(f)

print content # I'll be applying the logic to find the unique values later.
Run Code Online (Sandbox Code Playgroud)

但是在处理data_large 时,我做了以下事情,我的系统被挂起,速度很慢,不得不强制关闭它以使其恢复正常速度。2 mins 打印其内容需要花费一些时间。

with open('data_large') as f:
    content = json.load(f)

print content # I'll be applying the logic to find the unique values later.
Run Code Online (Sandbox Code Playgroud)

如何在处理大型数据集时提高程序的效率?

python json

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

获取具有类活动的元素?

我有一份清单.我想获得具有类活动的id的元素.

<ul class="nav nav-tabs" id="nav">
   <li class="active" id="day" onclick="changeDur('day')"><a data-toggle="tab">Day</a></li>
   <li id="week" onclick="changeDur('week')"><a data-toggle="tab">Week</a></li>
   <li id="month" onclick="changeDur('month')"><a data-toggle="tab">Month</a></li>
   <li id="year" onclick="changeDur('year')"><a data-toggle="tab">Year</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试的?

var period_val = $("#nav.active").attr('id');
alert(period_val)
Run Code Online (Sandbox Code Playgroud)

它给了我undefined.

我哪里错了?

javascript jquery

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

如何计算python中BST的高度

我创建了一个BST.现在我想找到BST开发的高度.

这是我构建BST的代码

class Node:
    '''represents a new node in the BST'''
    def __init__(self,key):
        self.key=key
        self.disconnect()
    def disconnect(self):
        self.left=None;
        self.right=None;
        self.parent=None;
    def __str__(self):
        return 'node with kay %s'%self.key

class BST:
    def __init__(self):
        self.root=None

    def insert(self,t):
        '''inserts a new element into the tree'''
        if self.root is None:
            self.root = Node(t)
        else:
            self._do_insert(self.root,t)

    def _do_insert(self,parent,t):
        if t > parent.key:
            if parent.left is None:
                parent.left = Node(t)
            else:
                self._do_insert(parent.left,t)
        elif t < parent.key:
            if parent.right is None:
                parent.right = Node(t)
            else:
                self._do_insert(parent.right,t) …
Run Code Online (Sandbox Code Playgroud)

python binary-search-tree

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