基于字符串数组制作 TypeScript 类型的最佳方法是什么?我使用的是 2.6.2 版。该数组很长,我不想通过复制 Enum 声明中的字符串值来重复自己。
我想做的是这样的:
const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
export type Color = convertStringArrayToType(colors);
Run Code Online (Sandbox Code Playgroud)
以下解决方案(源代码)工作正常,但感觉很糟糕:
/** Utility function to create a K:V from a list of strings */
function strEnum<T extends string>(o: Array<T>): {[K in T]: K} {
return o.reduce((res, key) => {
res[key] = key;
return res;
}, Object.create(null));
}
/**
* Sample create a string enum
*/
/** Create a K:V */
const Direction = strEnum([
'North', …Run Code Online (Sandbox Code Playgroud) 我有一个包含如下模板的组件:
// Template
<form #f="ngForm" (ngSubmit)="onFormSubmit(f)">
<!-- A bunch of form fields -->
</form>
Run Code Online (Sandbox Code Playgroud)
我的组件有一个方法,如:
onFormSubmit(form: NgForm) {
this.save();
}
Run Code Online (Sandbox Code Playgroud)
我想编写一个基本上看起来像这样的测试,测试在提交表单时调用save函数:
it('saves the widget when the form is submitted', () => {
let testForm = new NgForm([], []);
testForm.setValue({
name: "Hello",
category: "World"
});
component.onFormSubmit(testForm);
// Run tests to check that the component data was updated
expect(component.data.name).toEqual('Hello');
expect(component.data.category).toEqual('World');
});
Run Code Online (Sandbox Code Playgroud)
如何创建表单的模拟版本以传递给onFormSubmit()函数?我已经尝试过上面的操作,但是我得到了错误:"There are no form controls registered with this group yet. If you're using ngModel, you may want to …
我安装了最新的PyDev(2.8.2)和pylint(1.0.0).我试图在PyDev编辑器中显示pylint错误和警告.当我启用它时似乎什么都不做.当我将其设置为将输出重定向到控制台时,它似乎正常工作(请参见屏幕截图).
我怎样才能让它运转起来?

我正在尝试在Angular 4中的模板驱动器表单中实现自定义表单控件组件.因为我希望它与父表单很好地集成,我试图将其实现为ControlValueAccessor.我遵循了我能找到的指南,但Angular没有合作.根据文档,它应该调用我的实现registerOnChange(),但它没有.这是为什么?
/* editor-clause.component.ts */
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { QueryClause } from '../../../../models/query-clause';
import { Input, Component, EventEmitter, Output } from '@angular/core';
@Component({
moduleId: module.id,
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: EditorClauseComponent,
multi: true,
}],
selector: 'editor-clause',
templateUrl: './editor-clause.component.html',
styleUrls: ['./editor-clause.component.css']
})
export class EditorClauseComponent implements ControlValueAccessor {
@Input('clause') clause: QueryClause;
parentOnChange: any;
writeValue(_obj: any): void {
return;
}
registerOnChange(fn: any): void {
// This never gets printed and I don't know why
console.log('Registering …Run Code Online (Sandbox Code Playgroud) 我在单元测试中嘲笑导入的模块时遇到了麻烦.我正在尝试tracker.models使用mock模块在我的模块中模拟PIL Image类.我知道你应该模仿使用它们的东西,所以我写作@mock.patch('tracker.models.Image')我的装置进行单元测试.我正在尝试检查下载的图像是否作为PIL图像打开.模拟补丁似乎覆盖了整个Image模块.这是我运行测试时遇到的错误:
File "/home/ubuntu/workspace/tracker/models.py", line 40, in set_photo
width, height = image.size
ValueError: need more than 0 values to unpack
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试:
test_models.py
@responses.activate
@mock.patch('tracker.models.Image')
def test_set_photo(self, mock_pil_image):
# Initialize data
hammer = Product.objects.get(name="Hammer")
fake_url = 'http://www.example.com/prod.jpeg'
fake_destination = 'Hammer.jpeg'
# Mock successful image download using sample image. (This works fine)
with open('tracker/tests/test_data/small_pic.jpeg', 'r') as pic:
sample_pic_content = pic.read()
responses.add(responses.GET, fake_url, body=sample_pic_content, status=200, content_type='image/jpeg')
# Run the actual method
hammer.set_photo(fake_url, fake_destination)
# Check that it was opened …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 ngx-datatable 包在我的 Angular 4 应用程序中显示一些数据,并且我对用于设置列宽度的“强制”模式特别感兴趣。据我了解,该模式应该根据单元格内容智能地确定列宽,以便所有内容都可见。然而,它对我不起作用。列的宽度设置为 150px,而不是它们各自的自定义宽度。
这是我的表格 HTML:
<ngx-datatable
[rows]="rows"
[columns]="columns"
[columnMode]="'force'"
[scrollbarV]="true"
[scrollbarH]="true"
[rowHeight]="50"
>
</ngx-datatable>
Run Code Online (Sandbox Code Playgroud)
这是我的配置:
rows = [
{ displayName: 'Austin', emailAddress: 'a@a.aa', role: 'Swimlane',
a: 'This should all be visible and none of this should be cut off because the table is in force mode and it should keep it all visible' },
{ displayName: 'Dany', emailAddress: 'b@b.bb', role: 'KFC' , a:'b',
b:'This text should also be completely visible due to the selected display mode …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一系列哈希.这是我的代码.$ 1,$ 2等与正则表达式匹配,我检查过它们是否存在.
更新:修复了我的初始问题,但现在我遇到的问题是,当我将项目推到它上时,我的数组不会超过1的大小...
更新2:这是一个范围问题,因为需要在循环外声明@ACL.感谢大家!
while (<>) {
chomp;
my @ACLs = ();
#Accept ACLs
if($_ =~ /access-list\s+\d+\s+(deny|permit)\s+(ip|udp|tcp|icmp)\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\s+eq (\d+))?/i){
my %rule = (
action => $1,
protocol => $2,
srcip => $3,
srcmask => $4,
destip => $5,
destmask => $6,
);
if($8){
$rule{"port"} = $8;
}
push @ACLs, \%rule;
print "Got an ACL rule. Current number of rules:" . @ACLs . "\n";
Run Code Online (Sandbox Code Playgroud)
哈希数组似乎没有变得更大.
背景:我正在研究一个网络抓取工具,以跟踪在线商店的价格。它使用Django。我为每个商店都有一个模块,每个模块都具有get_price()和get_product_name()编写的功能,因此主刮板模块可以互换使用这些模块。我有store_a.py,store_b.py,store_c.py等,每个都定义了这些功能。
为了防止重复代码,我制作了StoreTestCase,它继承自TestCase。对于每个商店,我都有一个StoreTestCase的子类,例如StoreATestCase和StoreBTestCase。
当我手动测试StoreATestCase 类时,测试运行器执行我想要的操作。它使用子类中的数据self.data进行测试,而不尝试自行建立和测试父类:
python manage.py test myproject.tests.test_store_a.StoreATest
Run Code Online (Sandbox Code Playgroud)
但是,当我手动测试模块时,例如:
python manage.py test myproject.tests.test_store_a
Run Code Online (Sandbox Code Playgroud)
它首先为子类运行测试并成功,但是随后为父类运行测试并返回以下错误:
for page in self.data:
TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)
store_test.py(父类)
from django.test import TestCase
class StoreTestCase(TestCase):
def setUp(self):
'''This should never execute but it does when I test test_store_a'''
self.data = None
def test_get_price(self):
for page in self.data:
self.assertEqual(store_a.get_price(page['url']), page['expected_price'])
Run Code Online (Sandbox Code Playgroud)
test_store_a.py(子类)
import store_a
from store_test import StoreTestCase
class StoreATestCase(StoreTestCase):
def setUp(self):
self.data = …Run Code Online (Sandbox Code Playgroud) 为什么在Python下面的工作,没有宣布一个在全球范围内第一个?
def setA():
global a
a = 5
def printA():
print a
setA()
printA()
Run Code Online (Sandbox Code Playgroud)
在我看来,这是正确的方法:
a = None
def setA():
global a
a = 5
def printA():
print a
setA()
printA()
Run Code Online (Sandbox Code Playgroud) 我想ReplaySubject在我的Angular应用中制作一个,并对其进行设置,以使观察者订阅它和收到更新之间总是存在延迟。
let delayedReplay = new ReplaySubject(1);
delayedReplay.subscribe((data) => {
console.log('Got:', data);
});
delayedReplay.next('Test');
Run Code Online (Sandbox Code Playgroud)
我想要的是对ReplaySubject本身施加延迟,以便上面的代码将'Got: Test'在1秒后记录。