我想在生产代码中模拟某些类的任何实例上的方法,以便于测试.Python中有没有可以促进这个的库?
基本上,我想要执行以下操作,但在Python中(以下代码是Ruby,使用Mocha库):
def test_stubbing_an_instance_method_on_all_instances_of_a_class
Product.any_instance.stubs(:name).returns('stubbed_name')
assert_equal 'stubbed_name', SomeClassThatUsesProduct.get_new_product_name
end
Run Code Online (Sandbox Code Playgroud)
从上面要注意的重要一点是我需要在类级别上模拟它,因为我实际上需要在我正在测试的事物创建的实例上模拟方法.
使用案例:
我有一个类QueryMaker在一个实例上调用一个方法RemoteAPI.我想模拟出RemoteAPI.get_data_from_remote_server返回一些常量的方法.如何在测试中执行此操作,而无需在RemoteAPI代码中放置特殊情况以检查其运行的环境.
我想要的实例:
# a.py
class A(object):
def foo(self):
return "A's foo"
# b.py
from a import A
class B(object):
def bar(self):
x = A()
return x.foo()
# test.py
from a import A
from b import B
def new_foo(self):
return "New foo"
A.foo = new_foo
y = B()
if y.bar() == "New foo":
print "Success!"
Run Code Online (Sandbox Code Playgroud) 我可以使用apt-get源获取源包,但有一种方法可以使用aptitude吗?我还使用aptitude下载了python2.6的.deb包,然后使用ar解压缩它.
aptitude download python2.6
ar xv python2.6_2.6.5-1ubuntu6_i386.deb
Run Code Online (Sandbox Code Playgroud)
我还使用apt-get下载了python源代码
sudo apt-get source python2.6
Run Code Online (Sandbox Code Playgroud)
这两个下载的内容都不同.为什么?
创建依赖于javascript的网站仍然是不好的做法吗?
我知道它曾经是,但现在大多数浏览器都支持它们......为什么或为什么不应该担心这个?
在我的应用程序中,我使用a AsyncTask将一些数据写入事务中的数据库.也可以从UI线程访问此数据库.在查看可用的数据库方法时,我遇到了yieldIfContendedSafely().看起来这个方法应该用于从单独的线程进行事务的任何情况.但除了以下内容之外,几乎没有关于此方法的任何文档:
暂时结束事务以允许其他线程运行.到目前为止,该交易被认为是成功的.在打电话
setTransactionSuccessful之前不要打电话.返回时,将创建一个新事务但未标记为成功.这假定没有嵌套事务(beginTransaction仅被调用一次)并且如果不是这种情况将抛出异常.
以下是我将如何假设您将从线程中使用此方法:
try {
db.beginTransaction();
//insert some stuff into the database here
...
// is this how you use this method?
boolean yielded = db.yieldIfContendedSafely();
if (yielded) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
db.setTransactionSuccessful();
} catch (SQLException e) {
return false;
} finally {
db.endTransaction();
db.close();
}
Run Code Online (Sandbox Code Playgroud)
这是使用此方法的正确方法吗?是否可以db.yieldIfContendedSafely()在同一个事务中多次使用,在多次写入数据库中的不同表之间?有什么建议?
我想以编程方式在用户的iphone上找出货币区域设置.这意味着,如果用户在美国商店,货币区域应该是美元,对于澳大利亚,它应该是澳元.我的目的是尝试将我们应用程序中列出的商品价格转换为几乎与AppStore要求的价格相匹配.
例如,如果我们销售视频3美元,而澳大利亚人想购买它,那么我应该在我的应用程序屏幕上显示2.8澳元.它将减少用户对其国家实际价格的计算.有谁知道怎么做?
有人可以帮我把这个C#函数转换成java函数吗?
我可以做一些简单的for循环而不是所有这些foreach吗?
static Queue RadixSort(Queue Items, int Digits)
{
int Digit = Digits - 1;
while (Digit >= 0)
{
Queue Zero = new Queue();
Queue One = new Queue();
Queue Two = new Queue();
Queue Three = new Queue();
Queue Four = new Queue();
Queue Five = new Queue();
Queue Six = new Queue();
Queue Seven = new Queue();
Queue Eight = new Queue();
Queue Nine = new Queue();
int UpperLimit = Items.Count;
int counter = 1;
while (counter …Run Code Online (Sandbox Code Playgroud) 我正在尝试将电子邮件地址输入到撰写邮件窗口的" 收件人"字段中.
我尝试获取收件人的地址属性,根据VS,应该给我电子邮件.
我收到一个看起来像这样的字符串:
"/c=US/a=att/p=Microsoft/o=Finance/ou=Purchasing/s=Furthur/g=Joe"
Run Code Online (Sandbox Code Playgroud)
如何在收件人字段中获取电子邮件地址?
我的代码到目前为止:
List <string> emails = new List<string>();
if (thisMailItem.Recipients.Count > 0)
{
foreach (Recipient rec in thisMailItem.Recipients)
{
emails.Add(rec.Address);
}
}
return emails;
Run Code Online (Sandbox Code Playgroud) 我需要php explode()的功能,但没有分隔符.
例如,将变量"12345"转换为数组,单独保存每个数字.
这可能吗?我已经谷歌搜索但只发现爆炸(),似乎没有用.
谢谢!
我猜对了,但仍然惊讶地看到,这两个程序,写在C和C++的输出,在编译时有很大不同.这让我觉得对象的概念仍然存在于最低层.这会增加开销吗?如果是这样,目前将面向对象的代码转换为程序样式或者很难做到这一点是不可能的优化?
#include <stdio.h>
int main(void) {
printf("Hello World!\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译如下:
gcc helloworld.cpp -o hwcpp.S -S -O2
gcc helloworld.c -o hwc.S -S -O2
Run Code Online (Sandbox Code Playgroud)
制作了这段代码:
.file "helloworld.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Hello World!\n"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl $.LC0, 4(%esp)
movl $1, (%esp)
call __printf_chk
xorl %eax, …Run Code Online (Sandbox Code Playgroud) 我的问题是如何在C#中使用*和'space'制作金字塔?输出将是这样的.
*
* *
* * *
* * * *
* * * * *
Run Code Online (Sandbox Code Playgroud)
我们只需要为此程序使用"for loop".我只知道如何制作这个.
*
**
***
****
*****
Run Code Online (Sandbox Code Playgroud)
我做了一个这样的程序:
static void Main(string[]args)
{
int i=o;
int j=o;
for(i=5;1>=1;i--)
for(j=1;j<=5;j++)
{
Console.Write("*");
}
Console.WriteLine(" ");
}
Run Code Online (Sandbox Code Playgroud)
当谈到金字塔时我很困惑,因为它包含空格.谢谢你的帮助!