我想为交互式CLI程序(Asterisk CLI)编写一个包装器.
基本上,我需要保持与CLI的交互(包括tab-completion),但我想过滤Asterisk的输出,以便只显示与给定模式匹配的行.
我尝试了一种基于select()的方法,使用popen.popen4并在read_fs中放入asterisk stdout_and_stderr和sys.stdin,但它有点不起作用.
谁能给我一些好的指示?
非常感谢,安德里亚
我正在与只具有静态功能的API进行交互,并且无法打开和更改.
public class WindowsNativeGraphAPI
{
public static IEnumerable<IGraphData> GetGraphData();
public static bool DeleteGraphData(IGraphData data);
}
Run Code Online (Sandbox Code Playgroud)
我希望能够将API传递给函数或构造函数并遵守依赖注入(以防我们以后更换API).
public void GatherGraphData(IGraphAPI api)
{...}
Run Code Online (Sandbox Code Playgroud)
为了允许这个API作为参数传入,我至少需要抽象来使用接口传递给函数.
public interface IGraphAPI
{
IEnumerable<IGraphData> GetGraphData();
bool DeleteGraphData(IGraphData data);
}
Run Code Online (Sandbox Code Playgroud)
但是,我需要在另一个类中实现该接口,因为我无法更改原始API.这个类是一个围绕API的轻量级包装器,它只调用API上的相应函数并返回相同的结果.
public class WindowsGraphAPI : IGraphAPI
{
public IEnumerable<IGraphData> GetGraphData()
{
return WindowsNativeGraphAPI.GetGraphData();
}
public bool DeleteGraphData(IGraphData data)
{
return WindowsNativeGraphAPI.DeleteGraphData(data)
}
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢创建另一个类来包装API的想法.我知道这个包装器非常轻量级,只返回API的结果,但是如何测试包装器?包装器可能还应该包含一些异常处理来处理API中的错误.如果我们要更改到另一个API,那就遇到了同样的问题,我们必须再次创建这些额外的类和接口.
理想情况下,最终结果将是一个可模拟的API,可以在为消耗它的新组件编写单元测试时使用.
这是正确的方法吗?可以用另一种方式吗?
谢谢
可能重复:
奇怪的Java拳击
最近,当我阅读有关包装类时,我遇到了这个奇怪的案例:
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1 == i2) System.out.println("same object");
Run Code Online (Sandbox Code Playgroud)
哪个印刷品:
different objects
Run Code Online (Sandbox Code Playgroud)
和
Integer i1 = 10;
Integer i2 = 10;
if(i1 != i2) System.out.println("different objects");
if(i1 == i2) System.out.println("same object");
Run Code Online (Sandbox Code Playgroud)
哪个印刷品:
same object
Run Code Online (Sandbox Code Playgroud)
这个案子有合理的解释吗?
谢谢
由于遗留函数调用,我有时会被迫编写像这样的丑陋包装器
function return = someWrapper(someField)
a = someField.a;
b = someField.b;
% and so on, realistically it's more like ten variables that
% could actually be grouped in a struct
save('params.mat', 'a', 'b'); %etc.
% then, on another machine, a function loads params.mat, does the calculations
% and saves the result in result.mat containing the variables c,d,...
load('result.mat', 'c', 'd');
return.c = c;
return.d = d;
% again, it's more than just two return values
Run Code Online (Sandbox Code Playgroud)
因此,基本思想是创建与someField字段名称相同的变量,运行函数并return使用 …
我正在使用子类HttpServletRequestWrapper对请求参数进行一些翻译,并在第一次请求时缓存翻译的值.例如,第一次getQueryString()被调用,我调用super.getQueryString()并计算我想要的结果并将其保存在字段中,然后返回它.下次,我只使用缓存的结果.
除非有一些"转发",否则这种方法就像魅力一样.转发请求时,Tomcat会替换原始请求,因此我的缓存查询字符串不会更改,转发的页面将获取原始查询字符串,而不是转发到的字符串.
覆盖setRequest()清除缓存的方法也无济于事,好像请求被包装两次一样,它调用setRequest内包装器(不是我的),我无法知道它何时发生.
我正在寻找一种在包装的请求层次结构发生变化时得到通知的方法,以便在有"转发"时我可以清除缓存.
我有一个简单的异常日志装饰器,当我的脚本抛出异常时,它可以方便地发送自己的电子邮件.
def logExceptions(func):
def wrapper():
try:
func()
except Exception, e:
logger.exception(e)
return wrapper
Run Code Online (Sandbox Code Playgroud)
但是,如果我想装饰一个类方法,我必须修改wrapper()以获取'self',否则我会收到以下错误:
TypeError: wrapper() takes no arguments (1 given)
Run Code Online (Sandbox Code Playgroud)
当然,在那一点上我不能用它来装饰任何非类方法,因为这样就会发生这样的错误:
TypeError: wrapper() takes exactly 1 argument (0 given)
Run Code Online (Sandbox Code Playgroud)
有没有一个干净的方法来解决这个问题?谢谢=)
考虑以下非常简单的代码:
class A(val a: String, val b: Int)
object Test {
implicit class wrap(obj: A) {
def fn = obj.a + obj.b
}
def main(args: Array[String]) =
println(new A("Hello", 1).fn)
}
Run Code Online (Sandbox Code Playgroud)
反汇编代码产生:
public void main(java.lang.String[]);
Code:
0: getstatic #29; //Field scala/Predef$.MODULE$:Lscala/Predef$;
3: aload_0
4: new #31; //class A
7: dup
8: ldc #33; //String Hello
10: iconst_1
11: invokespecial #36; //Method A."<init>":(Ljava/lang/String;I)V
14: invokevirtual #38; //Method wrap:(LA;)LTest$wrap;
17: invokevirtual #42; //Method Test$wrap.fn:()Ljava/lang/String;
20: invokevirtual #46; //Method scala/Predef$.println:(Ljava/lang/Object;)V
23: return
Run Code Online (Sandbox Code Playgroud)
当 …
有人可能解释为什么以下:
Integer[] arr1 = {1,2,3,4,5};
Collection<?> numbers = Arrays.asList(new Integer[]{1,2,3});
System.out.println(Arrays.asList(arr1).containsAll(numbers));
Run Code Online (Sandbox Code Playgroud)
打印"true",而如果我们像这样交换Integer:
int[] arr2 = {1,2,3,4,5};
Collection<?> numbers2 = Arrays.asList(new int[]{1,2,3});
System.out.println(Arrays.asList(arr2).containsAll(numbers2));
Run Code Online (Sandbox Code Playgroud)
打印"假"?
我有这个代码的问题,从现有的.lib(CryptoLib.lib)编写包装函数:
mycode.ccp
#include "stdafx.h"
#pragma managed(push, off)
#include "CryptoLib.h"
#pragma comment (lib, "CryptoLib.lib")
#pragma managed(pop)
using namespace System;//This is a C++-CLI project.
__declspec(dllexport) void Encrypt(unsigned char *Data, unsigned char *RandomNr)
{
CryptoLib_Encrypt(Data, RandomNr);
}
Run Code Online (Sandbox Code Playgroud)
cryptolib.h
#ifndef _CRYPTOLIB_H_
#define _CRYPTOLIB_H_
#define PUBLIC
//This procedure is written in c++ code
extern void CryptoLib_Encrypt(unsigned char *Data, unsigned char *RandomNr);
#endif /* _CRYPTOLIB_H_ */
Run Code Online (Sandbox Code Playgroud)
我已经连接了cryptolib.h和cryptolib,但我仍然得到"未解析的令牌Cryptolib_Encrypt"和"未解析的外部符号Cryptolib_Encrypt"错误.
谁能告诉我为什么?
谢谢你的帮助
确切的错误消息:
error LNK2028: unresolved token (0A000006) "void __cdecl CryptoLib_Encrypt(unsigned char *,unsigned char *)" (?CryptoLib_Encrypt@@$$FYAXPAE0@Z) referenced in function …Run Code Online (Sandbox Code Playgroud) 我有一个实现给定功能的python类,例如文件Test_script.py是:
class Test(object):
def __init__(self,name):
self.name=name
def f1(self,n):
return n**2
def f2(self,n,m):
return n+m
def f3(self,word):
print word
Run Code Online (Sandbox Code Playgroud)
我如何构建一个仅包装此类和所有函数的C ++类,以便此类反过来可以被其他C ++类/代码使用?boost :: python是否有助于提供一些快捷方式?
为什么这是相关的? 因为在某些特定的用例中,由于可以重用许多经过良好测试和记录的模块(例如,解析,numpy函数...),而不是从头开始,因此在python中实现给定功能会更快,更轻松。在C ++中。上面的类是一个玩具问题,一旦很清楚如何在C ++中包装/嵌入它,将很容易处理更复杂的功能。
谢谢