从变量名列表构建函数签名

时间:2018-08-17 01:57:23

标签: python function signature

我有一个变量名列表:

function update(){
      context.drawImage(video,0,0,1580,700);   
      requestAnimationFrame(update); // wait for the browser to be ready to present another animation fram.       
    }

function readyToPlayVideo(event){ // this is a referance to the video
    // the video may not match the canvas size so find a scale to fit
    videoContainer.scale = Math.min(
                         canvas.width / this.videoWidth, 
                         canvas.height / this.videoHeight); 
    videoContainer.ready = true;
    // the video can be played so hand it off to the display function
    requestAnimationFrame(updateCanvas);
    // add instruction
    /*document.getElementById("playPause").textContent = "Click video to play/pause.";*/
    document.querySelector(".mute").textContent = "Mute";
}

function updateCanvas(){
    context.clearRect(0,0,canvas.width,canvas.height); 
    // only draw if loaded and ready
    if(videoContainer !== undefined && videoContainer.ready){ 
        // find the top left of the video on the canvas
        video.muted = muted;
        var scale = videoContainer.scale;
        var vidH = videoContainer.video.videoHeight;
        var vidW = videoContainer.video.videoWidth;
        var top = canvas.height / 2 - (vidH /2 ) * scale;
        var left = canvas.width / 2 - (vidW /2 ) * scale;
        // now just draw the video the correct size
        context.drawImage(videoContainer.video, left, top, vidW * scale, vidH * scale);
        /*if(videoContainer.video.paused){ // if not playing show the paused screen 
            drawPayIcon();
        }*/
    }
    // all done for display 
    // request the next frame in 1/60th of a second
    requestAnimationFrame(updateCanvas);
}

var canvasOffset = $("#canvas2").offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;

var isDrawing = false;
var startX;
var startY;

var mouseIsDown = true;
var mouseIsUp = true;
var startX;
var startY;

function handleMouseDown(e) {
    mouseX = parseInt(e.clientX - offsetX);
    mouseY = parseInt(e.clientY - offsetY);
    console.log(mouseX,mouseY);
    $("#downlog").html("Down: " + mouseX + " / " + mouseY);

    // Put your mousedown stuff here
    if (mouseIsDown) {
        console.log('1');
        canvas2.style.cursor="crosshair";
        mouseIsDown=false;
        mouseIsUp=false;
        console.log(mouseIsDown);
    } else {
        handleMouseUp();
    }

    mouseIsDown=false;
    mouseIsUp=true;

}

function handleMouseUp(e) {
    mouseIsDown=false;
    startX = parseInt(e.clientX - offsetX);
    startY = parseInt(e.clientY - offsetY);
    if (mouseIsUp) {
    console.log('2');

      context2.beginPath();
      context2.rect(startX,startY,mouseX-startX,mouseY-startY);
      context2.strokeStyle="limegreen";
      context2.lineWidth=2;
      context2.stroke();
      canvas2.style.cursor="default";
      mouseIsUp=true;
    }

}

$("#canvas2").mousedown(function (e) {
    handleMouseDown(e);
});

$("#canvas2").mouseup(function (e) {
    handleMouseUp(e);
});

function clearcanvas()
{
    var canvas2 = document.getElementById('canvas2'),
    context2 = canvas2.getContext("2d");
    context2.clearRect(0, 0, canvas2.width, canvas2.height);
} 

和接受矢量输入的函数,例如

var_names = ['x','y']

我想构建一个函数,该函数创建一个与f相同功能的多输入函数,例如

def f(vec):
   return vec[0]+vec[1]

有人知道如何以自动化方式创建像g这样的函数吗?我尝试过

def g(x,y):
   return f([x,y])

但是这给了我一些像这样的签名:

def _create_multiInput_fcn(vector_fcn,var_list):
    def g(*var_list):
        out = vector_fcn(var_list)
        return out
    return g

g = _create_multiInput_fcn(f,var_list)

当我真的想要这个时:

<function __main__._create_multiInput_fcn.<locals>.f(*var_list)>

我感谢有人可以给我的任何帮助/建议。谢谢。

1 个答案:

答案 0 :(得分:2)

您可以使用内置函数exec动态定义一个函数:

import nbformat
nb = nbformat.read('Untitled6.ipynb', as_version=4)
for cell in nb.cells.copy():
    if 'remove_cell' in cell.metadata.get('tags', []):
        nb.cells.remove(cell)
nbformat.write(nb, 'Untitled7.ipynb')