小编cam*_*enl的帖子

如何在JavaScript中计算与Python timeit类似的函数

测量JavaScript中函数执行时间的最佳方法是什么?最好类似于Python timeit.

javascript performance function

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

Flask:如何向客户端发送动态生成的zipfile

我正在寻找一种方法将zipfile发送到客户端,该方法是从请求响应生成的.在此示例中,我将一个JSON字符串发送到url,该url返回已转换的JSON字符串的zip文件.

@app.route('/sendZip', methods=['POST'])
def sendZip():
    content = '{"type": "Point", "coordinates": [-105.01621, 39.57422]}'
    data = {'json' : content}
    r = requests.post('http://ogre.adc4gis.com/convertJson', data = data)
    if r.status_code == 200:
        zipDoc = zipfile.ZipFile(io.BytesIO(r.content))
        return Response(zipDoc,
                mimetype='application/zip',
                headers={'Content-Disposition':'attachment;filename=zones.zip'})
Run Code Online (Sandbox Code Playgroud)

但是我的zip文件是空的,而烧瓶返回的错误是

Debugging middleware caught exception in streamed response at a point where response 
headers were already sent
Run Code Online (Sandbox Code Playgroud)

python json zipfile flask

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

熊猫:如何将int64年的索引转换为datetime

我有一个int64索引,表示我想要作为时间戳索引处理的年份值:

df.index
Int64Index([2001,2002,2003], dtype='int64')
Run Code Online (Sandbox Code Playgroud)

如何将索引转换为pandas中的日期时间戳?

pandas

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

numpy中的多维布尔数组索引

我有两个二维数组,一个数字和一个布尔值:

x = 
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.,  2.],
       [ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.],
       [ 4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.],
       [ 5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.,  5.],
       [ 6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.,  6.], …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy

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

如何从 ajax POST 表单提交中使用 Flask 的 render_template

我希望得到这个类似问题的答案:

我有一个 jQuery 表单,它将 JSON 数据发送到由 Flask 提供的某个 URL:

    <div id="form">      
      <form id="send">        
        <input type="submit" value="Continue" >
      </form>
    </div>



jQuery(document).on('ready', function() {
        jQuery('form#send').bind('submit', function(event){
        event.preventDefault();

        var form = this;
        json = geojson.write(vectors.features);

        $.ajax({
            type: "POST",
            contentType: 'application/json',
            url: $SCRIPT_ROOT + "/create",
            dataType: "json",
            success: function(){},
            data: json

        });
    });
});
Run Code Online (Sandbox Code Playgroud)

在 Flask 方面,我有一个简单的函数来呈现一个显示 JSON 对象的 HTML 页面:

@app.route('/create', methods=['POST'])
def create():
    content = request.json
    print content
    return render_template('string.html', string = content)
Run Code Online (Sandbox Code Playgroud)

我知道 JSON 正在传递,因为我可以看到它打印在运行 Flask 服务器的控制台中:

控制台打印

问题是模板作为 Ajax …

python ajax jquery flask

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

将containsLocation与google.maps.Data.polygon一起使用

我有一个带有面要素的google.maps.Data图层:

state = new google.maps.Data();
state.loadGeoJson('static/geojson/ga_state.geojson', {
    idPropertyName: 'name'
});
state.setStyle({
    clickable: false,
    visible: false,
});
state.setMap(map);
Run Code Online (Sandbox Code Playgroud)

在此要素集内是一个表示格鲁吉亚州的多边形:

ga = state.getFeatureById('Georgia')
Run Code Online (Sandbox Code Playgroud)

我可以得到这个功能的几何:

gaGeom = ga.getGeometry()
Run Code Online (Sandbox Code Playgroud)

但是当我将这些对象中的任何一个以及原始数组传递给google.maps.geometry.polygon.containsFeature()时,我得到一个错误,该对象不包含get()函数:

google.maps.geometry.poly.containsLocation(p.getPosition(), ga)
Uncaught TypeError: b.get is not a function(…)

google.maps.geometry.poly.containsLocation(p.getPosition(), gaGeom)
Uncaught TypeError: b.get is not a function(…)

google.maps.geometry.poly.containsLocation(p.getPosition(), gaGeom.getArray())
Uncaught TypeError: b.get is not a function(…)
Run Code Online (Sandbox Code Playgroud)

如何将google.maps.Data.Polygon转换为google.maps.Polygon或使用此功能?

编辑:

我找到了一种从google.maps.Data.Polygon构建google.maps.Polygon的方法:

//gaGeom is the feature.geometry from the data layer
poly = new google.maps.Polygon({paths:gaGeom.getAt(0).getArray()})
Run Code Online (Sandbox Code Playgroud)

但肯定有一种更清洁的方式来构建google.maps.Polygon?

google-maps google-maps-api-3

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

大熊猫:使用ix超出范围,但我可以看到该列

我不知道为什么熊猫在此数据帧上超出范围:

SC7a        2009    2010    2011    2012    2013    2014
Region 10   10.1    10.6    11.1    11.6    9.7     10.8
Georgia     7.5     7.4     7.8     7.6     7.2     7.1
Run Code Online (Sandbox Code Playgroud)

我正在做的是打电话给:

df.ix[:, 2014]
Run Code Online (Sandbox Code Playgroud)

我得到这个错误:

IndexError: index 2014 is out of bounds for axis 0 with size 6 
Run Code Online (Sandbox Code Playgroud)

请注意,调用loc可以正常工作:

df.loc[:, 2014]
SC7a
Region 10    10.8
Georgia       7.1
Name: 2014, dtype: float64
Run Code Online (Sandbox Code Playgroud)

这是错误吗?df.loc和df.ix不能与此数据互换吗?

python indexing dataframe pandas

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

如何使用 Notepad++ 中的 Replace RegEx 小写所选文本

我正在寻找一种使用 Notepad++ 中的正则表达式来小写选定的 BACKTICKED 文本的方法

这是一个示例字符串:

Select * FROM `Weird_TEXT` WHERE
Run Code Online (Sandbox Code Playgroud)

以及所需的输出:

Select * FROM `weird_text` WHERE
Run Code Online (Sandbox Code Playgroud)

我可以使用正则表达式找到这些字符串(包括反引号):

(?<=FROM )(.*)(?= WHERE)
`Weird_TEXT`
Run Code Online (Sandbox Code Playgroud)

但我不知道如何将文本替换为小写,我尝试使用以下方法:

\1\L\2\L
\1\L\2\L\3
Run Code Online (Sandbox Code Playgroud)

编辑:

当我在 Notepad++ 中单击Find Next时,单词周围的反引号Weird_TEXT包含在选择中。这可能是 RegEx 不起作用的原因吗?

regex notepad++

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