我有一些python模块,我导入为:
from mygraph.draw import pixel
Run Code Online (Sandbox Code Playgroud)
文件结构如下所示:
mygraph/
__init__.py
draw.py
Run Code Online (Sandbox Code Playgroud)
并draw.py包含def pixel()
现在,我想添加另一个函数,line()我想将其导入为
from mygraph.draw import line
Run Code Online (Sandbox Code Playgroud)
我可以简单地添加line到draw.py.但是,我想line()在一个单独的文件中,line.py而不是与draw.py混乱.但是,如果我将它放在一个单独的文件中,它将被导入为
from mygraph.line import line
Run Code Online (Sandbox Code Playgroud)
那不是我想要的......
是否有可能以某种方式"别名",line.py因此它在draw模块中可见,但是在separete文件中?我想过要添加一个虚拟函数draw
def line():
return real_line.line()
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我将不会有原始的"docstring" line,并且我将在调用实线函数时失去一些性能.
我想得到 .text()一个iframe,但它不起作用.iframe位于同一个域中.一次点击我将页面加载到框架,第二次我想在警报中显示内容,但我得到空警报.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready( function() {
// load simple paage to the frame
$("#load").click( function(){
$("<iframe id='theframe' width='400' // single line
height='400' src='http://localhost/dummy.html'/>").appendTo('body');
} );
// display the content - but I get empty alert
$('#content').click( function() {
var content;
content = $("#theframe").text();
alert(content);
});
});
</script>
</head>
<body>
<a id='load' href="#">load</a>
<a id='content' href="#">check content</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我一直在看这个简单的Levenshtein编辑距离的 python实现.
def lev(a, b):
"""Recursively calculate the Levenshtein edit distance between two strings, a and b.
Returns the edit distance.
"""
if("" == a):
return len(b) # returns if a is an empty string
if("" == b):
return len(a) # returns if b is an empty string
return min(lev(a[:-1], b[:-1])+(a[-1] != b[-1]), lev(a[:-1], b)+1, lev(a, b[:-1])+1)
Run Code Online (Sandbox Code Playgroud)
来自:http://www.clear.rice.edu/comp130/12spring/editdist/
我知道它具有指数复杂性,但我将如何从头开始计算这种复杂性?
我一直在互联网上搜索,但没有找到任何解释,只有它是指数的声明.
谢谢.
python algorithm complexity-theory dynamic-programming levenshtein-distance
我有相当多的代码,在开发版本中运行良好,assert()代码中有许多代码.我-DNDEBUG通过传递给g ++的指令禁用了断言,现在我的代码打破了seg.故障.有关assert()的东西我不知道吗?
我有一个Perl脚本,在bash下输出一些彩色文本.例如,这将输出一个红色字符串:
perl -e 'print "\e[1;31m RED \e[m"
Run Code Online (Sandbox Code Playgroud)
当我把它管道到另一个程序,例如vim时... | vim -,我可以看到颜色形成杂乱的字符:
^[[1;31m RED ^[[m
Run Code Online (Sandbox Code Playgroud)
我希望他们被跳过.例如grep,您在bash中看到颜色,但在重定向输出时会跳过它们.
怎么做?
我有一个函数接受坐标(元组)作为其参数之一:
func({X, Y}, Something) when is_integer(X), is_integer(Y) -> ...
Run Code Online (Sandbox Code Playgroud)
我想确保坐标:
我可以使用上面的防护,它可以正常工作.但是,我有许多使用坐标的函数,我想知道我是否可以以某种方式清理这个构造(一些宏?)所以会有类似的东西:
func(XY, Something) when ?is_coord(XY) -> ... % how to define ?is_coord
Run Code Online (Sandbox Code Playgroud)
有没有一种干净,惯用的方式呢?是erlang-ish吗?
编辑
Erlang文档明确阻止防御性编程:
3.13不要"防守"编程
防御性程序是程序员不"信任"输入数据到他们正在编程的系统部分的程序.通常,不应该将输入数据测试到函数的正确性.系统中的大多数代码都应该在假设所讨论的函数的输入数据是正确的情况下编写.只有一小部分代码应该实际执行任何数据检查.这通常是在数据第一次"进入"系统时完成的,一旦数据被检查,因为它进入系统后应该被认为是正确的.
我使用 SWIG 和 Numpy。我定义了一个 C 函数inplace()来快速处理数据数组,我想进行一些错误检查(如果两个数组具有相同的维度)。
我在文件中使用%rename和。据我了解,重命名应该映射函数名称,所以每次有人使用时,都会运行并检查错误。%inline.iinplacesafe_inplace
但它不起作用:(。据我所知,safe_inplace 没有被执行,python 直接运行inplace而不触及函数的安全版本。
# .i
%include "inplace.h"
%rename (inplace) safe_inplace;
%inline %{
void safe_inplace(int* datain, int in_dx, int in_dy, int in_dz,
int* dataout, int out_dx, int out_dy)
{
if ((in_dx != out_dx) || (in_dy != out_dy)) {
PyErr_Format(PyExc_ValueError, /*... messgage*/)
return;
}
inplace( /* .. pass the arguments to original function*/ );
}
Run Code Online (Sandbox Code Playgroud)
头文件:
# .h
void inplace(int* datain, int in_dx, int in_dy, …Run Code Online (Sandbox Code Playgroud) python ×3
c ×2
c++ ×2
erlang ×2
algorithm ×1
architecture ×1
assert ×1
bash ×1
iframe ×1
inheritance ×1
jquery ×1
memoization ×1
perl ×1
swig ×1