import subprocess
def my_function(x):
return x + 100
output = subprocess.Popen(my_function, 1) #I would like to pass the function object and its arguments
print output
#desired output: 101
Run Code Online (Sandbox Code Playgroud)
我只找到了使用单独脚本打开子进程的文档.有谁知道如何传递函数对象甚至是一种简单的方法来传递函数代码?
我正在尝试将分类变量的字符串数组转换为分类变量的整数数组.
防爆.
import numpy as np
a = np.array( ['a', 'b', 'c', 'a', 'b', 'c'])
print a.dtype
>>> |S1
b = np.unique(a)
print b
>>> ['a' 'b' 'c']
c = a.desired_function(b)
print c, c.dtype
>>> [1,2,3,1,2,3] int32
Run Code Online (Sandbox Code Playgroud)
我意识到这可以通过循环完成,但我想有一种更简单的方法.谢谢.
如何为通过安全cookie验证用户的龙卷风处理程序编写单元测试?这是我想通过的虚拟测试的代码(和sudo代码).我正在使用Tornado 3.1.
from tornado.web import Application, RequestHandler
from tornado.escape import to_unicode, json_decode, json_encode
from tornado.testing import AsyncHTTPTestCase
class MainHandler(RequestHandler):
"""
Base handler to authenticate user via a secure cookie.
This is used for an API.
"""
def get(self):
user = self.get_secure_cookie('user')
if user == 'user_email':
self.write('sucess')
else:
self.write('fail')
class UserAPITest(AsyncHTTPTestCase):
def get_app(self):
self.app = Application([('/', MainHandler)],
cookie_secret='asdfasdf')
return self.app
def test_user_profile_annoymous(self):
#SUDO CODE (what should go here?)
#cookie = make_secure_cookie('user', 'user_email', cookie_secret)
#headers = {'Cookie':cookie}
response = self.fetch('/', method='GET', headers=headers) …Run Code Online (Sandbox Code Playgroud) 为什么我不能使用下划线(_)扩展来更新未在模式定义中定义属性的mongoose模型.有办法解决这个问题吗?
节点模型:
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var NodeSchema = new Schema({
label: {type : String, default : 'none'}
}, { strict: false })
mongoose.model('Node', NodeSchema)
Run Code Online (Sandbox Code Playgroud)
节点控制器:
var node = new Node();
node = _.extend(node, {"EXTENDNOTinSchema":"TRUE"});
console.log("extend: " + node);
node.set("SETNOTinSchema","TRUE");
console.log("set: " + node);
Run Code Online (Sandbox Code Playgroud)
控制台输出:
extend: { __v: 0,
_id: 50bb05656880a68976000001,
label: 'none' }
set: { __v: 0,
_id: 50bb05656880a68976000001,
label: 'none'
SETNOTinSchema: TRUE}
Run Code Online (Sandbox Code Playgroud) python ×3
cookies ×1
function ×1
mongoose ×1
node.js ×1
numpy ×1
popen ×1
statistics ×1
subprocess ×1
tornado ×1
unit-testing ×1