我在模拟重载的__get($ index)方法时遇到了问题.要模拟的类的代码和使用它的被测系统如下:
<?php
class ToBeMocked
{
protected $vars = array();
public function __get($index)
{
if (isset($this->vars[$index])) {
return $this->vars[$index];
} else {
return NULL;
}
}
}
class SUTclass
{
protected $mocky;
public function __construct(ToBeMocked $mocky)
{
$this->mocky = $mocky;
}
public function getSnack()
{
return $this->mocky->snack;
}
}
Run Code Online (Sandbox Code Playgroud)
测试看起来如下:
<?php
class GetSnackTest extends PHPUnit_Framework_TestCase
{
protected $stub;
protected $sut;
public function setUp()
{
$mock = $this->getMockBuilder('ToBeMocked')
->setMethods(array('__get')
->getMock();
$sut = new SUTclass($mock);
}
/**
* @test
*/
public function …Run Code Online (Sandbox Code Playgroud) 我想覆盖一下mysql_num_rows,让我们说下面的话:
$dataset = array(array('id' => 1, 'name' => 'Zlatan', 'onSOF' => 1), array('id' => 1, 'name' => 'Guest', 'onSOF' => 0));
function mysql_num_rows($dataset) {
return sizeof($dataset);
}
Run Code Online (Sandbox Code Playgroud)
PHP是否支持内置函数覆盖?
扩展
我想创建一个OpenSource解决方案,它将覆盖所有现有的mysql_*函数,它的函数体我将使用PDO实例和方法以及属性.
这意味着已经使用mysql_*并且难以完全移动到PDO的用户应该只包含此函数覆盖,并且所有属性,函数调用,函数返回值,参数值等应该保持不变.
几个例子:
numpy.sum()
ndarray.sum()
numpy.amax()
ndarray.max()
numpy.dot()
ndarray.dot()
Run Code Online (Sandbox Code Playgroud)
......还有更多.它是支持一些遗留代码,还是有更好的理由?而且,我是根据我的代码"看起来"的方式选择的,还是两种方式中的一种比另一种更好?
我可以想象一个人可能想要numpy.dot()使用reduce(例如reduce(numpy.dot, A, B, C, D)),但我不认为这对于类似的东西会有用numpy.sum().
是否可以禁用甚至更好地替换自定义功能window.location?
这个问题是相关的:在javascript中禁用内置函数(alert)
虽然它工作得很好window.alert,但这不起作用window.location.
我们希望能够找到替换或禁用的方法(替换将是理想的,所以我们可以AJAX日志)window.location...肮脏的广告客户有时使用它来窃取人们远离我们的网络属性.
有任何想法吗?
即使只能在少数特定浏览器上运行的东西也可以正常运行(通过AJAX日志记录),我们可以相当快地采取行动.
gcc(最新版本:4.8,4.9)是否具有类似于__assume()icc支持的内置的"假设"子句?例如,__assume( n % 8 == 0 );
在Python 3.4中,我想创建一个子类float- 可以在数学和布尔操作中使用,例如a float,但具有其他自定义功能,并且可以在初始化时接收控制该功能的参数.(具体来说,我想要一个自定义__str__和该方法中使用的参数.)
但是,我似乎无法获得float具有功能性双参数构造函数的子类.为什么?这仅仅是扩展内置类型的限制吗?
例:
class Foo(float):
def __init__(self, value, extra):
super().__init__(value)
self.extra = extra
Run Code Online (Sandbox Code Playgroud)
现在如果我尝试Foo(1,2)我得到:
TypeError: float() takes at most 1 argument (2 given)
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,我的新__init__论点也被强制执行,所以如果我这样做,Foo(1)我会得到:
TypeError: __init__() missing 1 required positional argument: 'extra'
Run Code Online (Sandbox Code Playgroud)
这是什么交易?我已经用子类型完成了类似的事情,list并且对它没有用处感到惊讶float.
我正在使用多语言文本数据,其中包括使用西里尔字母和土耳其语的俄语.我基本上要的话在比较两个文件my_file和check_file,如果在的话my_file可以发现check_file,把它们写在输出文件中保留约从两个输入文件这些词的元信息.
有些单词是小写的,而其他单词是大写的,所以我必须小写所有单词来比较它们.当我使用Python 3.6.5并且Python 3使用unicode作为默认值时,它会处理小写,然后为Cyrillic正确地大写单词.但是对于土耳其语,有些字母处理不正确.大写'?'应对应小写'i',大写'I'应对应小写'?',小写'i'应对应大写'?',如果我在控制台中键入以下内容则不是这种情况:
>>> print('?'.lower())
i? # somewhat not rendered correctly, corresponds to unicode 'i\u0307'
>>> print('I'.lower())
i
>>> print('i'.upper())
I
Run Code Online (Sandbox Code Playgroud)
我正在做如下(简化的示例代码):
# python my_file check_file language
import sys
language = sys.argv[3]
# code to get the files as lists
my_file_list = [['?spanak', 'N'], ['?s?r', 'N'], ['ac?k', 'V']]
check_file_list = [['109', 'Ispanak', 'food_drink'], ['470', 'Is?r', 'action_words'], [409, 'Ac?k', 'action_words']] …Run Code Online (Sandbox Code Playgroud) 像str或等的类type
>>> type("pear")
<class 'str'>
>>> type(str)
<class 'type'>
Run Code Online (Sandbox Code Playgroud)
可以在Python中访问:
>>> str
<class 'str'>
>>> type
<class 'type'>
Run Code Online (Sandbox Code Playgroud)
然而,类function和builtin_function_or_method不.
>>> def foo(): pass
...
>>> type(foo)
<class 'function'>
>>> type(print)
<class 'builtin_function_or_method'>
Run Code Online (Sandbox Code Playgroud)
它们显示为内置类但尝试访问它们会引发名称错误:
>>> function
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>> builtin_function_or_method
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'builtin_function_or_method' is not defined
Run Code Online (Sandbox Code Playgroud)
有什么特别的function …
我有子类int添加一个额外的属性:
class Integer(int):
def __new__(cls, value, base=10, indirect=False):
try:
obj = int.__new__(cls, value, base)
except TypeError:
obj = int.__new__(cls, value)
return obj
def __init__(self, value, base=10, indirect=False):
self.indirect = indirect
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中使用此类,int(Integer(b'0')) 有时返回值48(= ord('0')!)或192,而不是正确的值0. str(Integer(b'0'))总是返回'0'.这似乎只发生在值0.首先解码b'0'为字符串,或传递int(b'0')给Integer没有区别.问题在于将a转换Integer(0)为intwith int().
此外,这是以随机方式发生的.后续运行将在应用程序(解析器)的不同点产生48或192.Python 3.2.2和3.2.3都表现相同(32位,Windows XP).
我似乎无法在一个简单的测试程序中重现这一点.以下产生无输出:
for i in range(100000):
integer = int(Integer(b'0'))
if integer > 0:
print(integer)
Run Code Online (Sandbox Code Playgroud)
检查int(Integer()) > 0我的应用程序中的条件(当我知道参数Integer是b'0')并有条件地打印int(Integer(b'0')) …
我正在将一些C#代码移植到VB6,因为遗留应用程序.我需要存储一对配对列表.我不需要进行关联查找,我只需要能够存储成对的项目.
我正在移植的片段如下所示:
List<KeyValuePair<string, string>> listOfPairs;
Run Code Online (Sandbox Code Playgroud)
如果我将它移植到C++,我会使用这样的东西:
std::list<std::pair<string, string> > someList;
Run Code Online (Sandbox Code Playgroud)
如果这是python,我只使用元组列表.
someList.append( ("herp", "derp") )
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种图书馆类型,但如果有必要,我会满足于其他的东西.我试图成为LAZY而不必编写cYetAnotherTinyUtilityClass.cls来获得此功能,或者回到经常被滥用的字符串操作.
我试过谷歌搜索,但VB6并没有真正在线记录,很多有什么,很好的挑战.如果你见过BigResource,你会明白我的意思.