如果我if __name__ == '__main__'在脚本中使用argparse和测试我也想用作模块,我应该在该测试下导入argparse然后初始化它吗?我没有发现在脚本中使用argparse提到的样式指南,并且许多argparse脚本示例不使用'if name'测试或以不同方式使用它.这是我到目前为止所做的事情:
#! /usr/bin/env python
def main(name):
print('Hello, %s!' % name)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description = 'Say hello')
parser.add_argument('name', help='your name, enter it')
args = parser.parse_args()
main(args.name)
Run Code Online (Sandbox Code Playgroud)
我应该使用顶部的其他模块导入argparse并在脚本体中进行配置吗?
学习iPhone编程时,我见过的每个Xcode模板都包含一个AppName-Prefix.pch文件,其中包含以下内容:
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
Run Code Online (Sandbox Code Playgroud)
我的理解是在编译之前,这个文件的内容以每个源代码文件为前缀.然而,每个其他文件也导入UIKit,这似乎是多余的.例如,main.m开始......
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
...
Run Code Online (Sandbox Code Playgroud)
Mac OS X中的Cocoa应用程序执行相同的操作,在前缀文件和头文件中导入Cocoa.h.
为什么两者都有?我#import从除前缀文件之外的所有源文件中删除了指令,并且编译并正确运行.
我第一次使用Python创建了一个库,我正试图利用这个项目中的机会来学习单元测试.我写了第一个方法,我想为它编写一些单元测试.(是的,我知道TDD要求我先写测试,我会到那里,真的.)
该方法相当简单,但它期望该类具有file属性集,该属性指向现有文件,并且该文件是某种类型的存档(目前仅使用zip文件,tar,rar等. ,稍后补充).该方法应该返回存档中的文件数.
我在我的项目中创建了一个文件夹,files其中包含一些示例文件,我手动测试了该方法,并且它到目前为止仍然有效.手动测试如下所示,位于archive_file.py文件中:
if __name__ == '__main__':
archive = ArchiveFile()
script_path = path.realpath(__file__)
parent_dir = path.abspath(path.join(script_path, os.pardir))
targ_dir = path.join(parent_dir, 'files')
targ_file = path.join(targ_dir, 'test.zip' )
archive.file = targ_file
print(archive.file_count())
Run Code Online (Sandbox Code Playgroud)
我所做的就是确保所印刷的内容是我所期望的内容test.zip.
这是file_count看起来像:
def file_count(self):
"""Return the number of files in the archive."""
if self.file == None:
return -1
with ZipFile(self.file) as zip:
members = zip.namelist()
# Remove folder members if there are any.
pruned = [item for item in …Run Code Online (Sandbox Code Playgroud) 我一直在这里阅读一些关于使用单元测试来测试私有方法和属性的问题.我是单元测试的新手,想要输入我正在尝试的方法,以便我的测试可以访问私有/受保护的属性和方法.
在我正在进行的测试中,我想确认将特定参数传递给对象会导致设置属性.我正在使用SimpleTest进行单元测试教育,我的测试方法如下:
function test__Construction_Should_Properly_Set_Tables() {
$cv = new CVObject( array( 'tables' => $this->standardTableDef ) );
$tables = $cv->tables;
$this->assertEqual( $tables, $this->standardTableDef );
}
Run Code Online (Sandbox Code Playgroud)
然后我在CVObject中写了一个__get方法,如下所示:
function __get( $name ) {
$trace = debug_backtrace();
$caller = $trace[1];
$inTesting = preg_match( '/simpletest/', $caller['file'] );
if ( $inTesting ) {
return $this->$name;
} else {
trigger_error( 'Cannot access protected property CVObject::$' .
$name . ' in ' . $trace[0]['file'] . ' on line ' .
$trace[0]['line'],
E_USER_NOTICE );
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是,如果调用文件来自SimpleTest,请继续使该属性可用于测试目的,但如果没有,则触发错误.这允许我保持属性私有但能够在测试中使用它,这对我来说更重要的是我将要开始编写的特定私有方法.
所以,我的问题是,我错过了一些非常糟糕的东西,应该避免这种技术吗?
我正在处理一个正在进行的项目,并且在文件中做笔记以解释现有代码的用途。我注意到的一件事是 VS Code 允许(甚至附带)对.vscode文件夹内的 JSON 文件进行注释。这里是.vscode/extensions.json.
但是,如果我尝试在其他地方的 JSON 文件中添加注释,我会被告知不允许添加注释。
我知道如何使用自定义设置解决此问题,但这基本上是 Microsoft 违反规则并允许在 JSON 中读取注释,即使 JSON 不允许注释,但在不允许的文件中强制执行注释禁令要去读什么?
而且,虽然微软可能允许他们正在阅读的 JSON 中的注释,但任何阅读其他 JSON 配置文件的人可能不会,我想我应该远离它们之外的.vscode?
我用我的MacVim设置安装了Janus.为了了解vim脚本是如何工作的,我一直在阅读Janus使用的vimrc文件,我不明白这个作者是如何使用函数的.例如,这是vimrc中的一个函数:
function s:setupWrapping()
set wrap
set wrapmargin=2
set textwidth=72
endfunction
Run Code Online (Sandbox Code Playgroud)
现在,根据vim手册的" 定义函数"部分,"函数名称必须以大写字母开头".根据手册的Local mappings and functions部分,'在脚本中定义一个函数时,"s:"可以添加到名称前面,使其成为脚本的本地.但是,在将其范围指定为脚本的本地范围时,没有提及能够以小写字母开头的函数名称.
那么,函数是否在语法上是不正确的,但无论如何都是有效的,或者它在语法上是否正确但是我找不到那样说的文档?
我暂时以UITableViewCell编程方式添加了一些值.但我需要为每个单元格添加图像.我怎样才能做到这一点?
这是我的代码..h文件
@interface BidalertsViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {
NSArray *listData;
}
@property(nonatomic, retain) NSArray *listData;
@end
Run Code Online (Sandbox Code Playgroud)
.m文件
@synthesize listData;
- (void)viewDidLoad {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(5,0,310,28)];
newView.backgroundColor=[UIColor whiteColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 25.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = @"Asset Name";
[mytext release];
[newView addSubview:mytext];
[self.view addSubview:newView];
[newView release];
NSArray *array = [[NSArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil];
self.listData = array;
[array …Run Code Online (Sandbox Code Playgroud) 从 C++ 访问 OS X 上的标准目录的现代、标准、规范的方法是什么,例如~/Library/Application Support或~/Library/Preferences?
我已经看到提到使用CoreServices,但也提到它已被弃用,我无法找到可以让我做的不仅仅是粘贴代码的文档。
我发现提到使用 Objective C++,但关于该选项的大部分信息都围绕着从 Objective C 调用 C++ 代码,而且 Apple 的文档似乎相当稀少,或者至少我没有成功找到它。
我正在尝试将CocoaLumberjack集成到现有项目中(这样我可以更好地登录报告了我无法复制的错误的客户计算机)。我已经成功构建了一个示例项目,并且可以在其中运行,但是在我自己的应用程序中似乎什么也没做。
我已将Lumberjack目录复制到我的项目目录中,并将其添加到项目中。我已将以下内容添加到“ Prefix.pch”文件中:
#import "DDLog.h"
#import "DDASLLogger.h"
#import "DDTTYLogger.h"
#import "DDFileLogger.h"
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
Run Code Online (Sandbox Code Playgroud)
我将以下内容放入 -applicationDidFinishLaunching:
[DDLog addLogger:[DDASLLogger sharedInstance]];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
Run Code Online (Sandbox Code Playgroud)
最后我有以下内容 -awakeFromNib
NSLog(@"%@", @(ddLogLevel));
NSLog(@"%@", @(LOG_LEVEL_VERBOSE));
NSLog(@"%@", @"About to DDLog");
DDLogError(@"This is an error.");
DDLogWarn(@"This is a warning.");
DDLogInfo(@"This is just a message.");
DDLogVerbose(@"This is a verbose message.");
NSLog(@"%@", @"Done with DDLog");
Run Code Online (Sandbox Code Playgroud)
控制台显示:
2014-05-26 14:57:13.530 [21943:303] 31
2014-05-26 14:57:13.530 [21943:303] 31
2014-05-26 14:57:13.530 [21943:303] About to DDLog
2014-05-26 14:57:13.531 [21943:303] Done with DDLog …Run Code Online (Sandbox Code Playgroud) 是否有内置的Pythonic方法来确定一个列表是否完全包含另一个列表的内容,包括重复的条目但忽略了项目的顺序?
>>> l1 = [2, 2, 3]
>>> l2 = [2, 2]
>>> l3 = [3, 2]
>>> l4 = [2, 2, 2]
>>> l5 = [2, 5, 2]
>>> is_superset(l1, l2)
True
>>> is_superset(l1, l3)
True
>>> is_superset(l1, l4)
False
>>> is_superset(l1, l5)
False
Run Code Online (Sandbox Code Playgroud) python ×3
cocoa ×2
unit-testing ×2
c++ ×1
cocoa-touch ×1
comments ×1
ios ×1
json ×1
list ×1
lumberjack ×1
macos ×1
macvim ×1
objective-c ×1
php ×1
scripting ×1
simpletest ×1
uitableview ×1
vim ×1