小编Pla*_*cid的帖子

将列名添加到从没有列名的csv文件中读取的数据

我正在使用Apache Spark和Scala.

我有一个csv文件,第一行没有列名.就像这样:

28,Martok,49,476
29,Nog,48,364
30,Keiko,50,175
31,Miles,39,161
Run Code Online (Sandbox Code Playgroud)

列表示ID,名称,年龄,numOfFriends.

在我的Scala对象中,我使用来自csv文件的SparkSession创建数据集,如下所示:

val spark = SparkSession.builder.master("local[*]").getOrCreate()
val df = spark.read.option("inferSchema","true").csv("../myfile.csv")
df.printSchema()
Run Code Online (Sandbox Code Playgroud)

当我运行程序时,结果是:

|-- _c0: integer (nullable = true)
|-- _c1: string (nullable = true)
|-- _c2: integer (nullable = true)
|-- _c3: integer (nullable = true)
Run Code Online (Sandbox Code Playgroud)

如何在数据集中的列中添加名称?

csv scala apache-spark apache-spark-sql

5
推荐指数
1
解决办法
4913
查看次数

Jest快照测试具有随机生成的ID(UUID)的React组件

我正在尝试为使用uuid生成唯一ID的React组件创建开玩笑的快照测试。我正在尝试模拟uuid功能。但是嘲笑似乎没有用。

零件:

import React from 'react';
import v1 from 'uuid/v1';

class MyComponent extends React.Component {
  // Other codes ...

  render() {
    const id = v1();
    return (
      <div>
        <label htmlFor={id}>
          <input type="checkbox"/>  
        </label>
      </div>
    )
  }
}

export default MyComponent;
Run Code Online (Sandbox Code Playgroud)

测试:

import React from 'react';
import renderer from 'react-test-renderer';
import MyComponent from './MyComponent';

describe('<MyComponent/>', () => {
  it('renders correctly', () => {
    jest.mock('uuid/v1', () => jest.fn(() => 1));
    const tree = renderer.create(<Checkbox />).toJSON();
    expect(tree).toMatchSnapshot();
  });
});
Run Code Online (Sandbox Code Playgroud)

uuid reactjs jestjs

4
推荐指数
1
解决办法
1498
查看次数

在Laravel PHPUnit中测试未经授权的用户限制

Laravel版本5.2

在我的项目中,role_id = 4的用户具有admin角色并可以管理用户.

我在AuthServiceProvider中定义了以下功能:

public function boot(GateContract $gate)
{
    $this->registerPolicies($gate);

    $gate->define('can-manage-users', function ($user)
    {
        return $user->role_id == 4;
    });
}
Run Code Online (Sandbox Code Playgroud)

我在UserController __construct方法中使用了这个功能,如下所示:

public function __construct()
{
    $this->authorize('can-manage-users');
}
Run Code Online (Sandbox Code Playgroud)

在ExampleTest中,我创建了两个测试来检查定义的授权是否有效.

对于具有role_id = 4的管理员用户的第一个测试.此测试通过.

public function testAdminCanManageUsers()
{
    $user = Auth::loginUsingId(1);
    $this->actingAs($user)
        ->visit('users')
        ->assertResponseOk();
}
Run Code Online (Sandbox Code Playgroud)

第二个测试是针对没有role_id = 4的另一个用户.我已经尝试了响应状态401和403.但是测试失败了:

public function testNonAdminCannotManageUsers()
{
    $user = Auth::loginUsingId(4);
    $this->actingAs($user)
        ->visit('users')
        ->assertResponseStatus(403);
}
Run Code Online (Sandbox Code Playgroud)

下面给出了失败消息的前几行:

对[ http:// localhost/users]的请求失败.收到状态代码[403].

C:\ wamp\www\laravel\blog\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:196 C:\ wamp\www\laravel\blog\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:80 C:\ wamp\www\laravel\blog\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:61 C:\ wamp\WWW\laravel \博客\测试\ ExampleTest.php:33

由异常'Illuminate\Auth\Access\AuthorizationException'引起,并显示消息'此操作未经授权.' 在C:\ wamp\www\laravel\blog\vendor\laravel\framework\src\Illuminate\Auth\Access\HandlesAuthorization.php:28

我也尝试使用'see'方法如下:

public function testNonAdminCannotManageUsers() …
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing laravel laravel-5.2

3
推荐指数
1
解决办法
3857
查看次数

jQuery选择具有name属性的多个元素的属性值

我有一个具有多个input,select,textarea元素的表单.使用jQuery,我如何获取每个元素的name属性值?我尝试了以下但它不起作用:

var names = $('[name]');
names.each(function(){
    console.log(names.attr('name'));
})
Run Code Online (Sandbox Code Playgroud)

javascript jquery

2
推荐指数
1
解决办法
550
查看次数

在 VSCode 主题中自定义块引用颜色

我在 VScode 中使用Nord 主题。Markdown 文件中的块引号看起来像这样,非常难以辨认。如何更改文本背景?

在此处输入图片说明

我在用户设置中尝试了以下操作,但它不起作用:

"workbench.colorCustomizations": {
    "textBlockQuote.background": "#ff0000", // changes the markdown preview
    "editor.textBlockQuote.background": "#0000ff", // Property not allowed
    "[Nord]": {
        "textBlockQuote.background": "#ff0000", // changes the markdown preview
    },
},
"editor.tokenColorCustomizations": {
    "textBlockQuote.background": "#ff0000", // Property not allowed
    "editor.textBlockQuote.background": "#ff0000", // Property not allowed
    "[Nord]": {
        "textBlockQuote.background": "#ff0000", // Property not allowed 
        "editor.textBlockQuote.background": "#ff0000", // Property not allowed
    }
},
Run Code Online (Sandbox Code Playgroud)

visual-studio-code

2
推荐指数
1
解决办法
1730
查看次数

为什么打字稿给出“重复标识符”错误

我越来越多Duplicate identifierSubsequent property declarations must have the same type.错误,当我跑yarn check-types在我的项目。

包.json:

{
  "scripts": {
    "start": "webpack-dev-server --mode development --hot",
    "build": "webpack --mode production",
    "check-types": "tsc"
  },
  "dependencies": {
    "react": "^16.12.0",
    "react-dom": "^16.12.0"
  },
  "devDependencies": {
    "@babel/core": "^7.8.3",
    "@babel/plugin-proposal-class-properties": "^7.8.3",
    "@babel/plugin-proposal-object-rest-spread": "^7.8.3",
    "@babel/preset-env": "^7.8.3",
    "@babel/preset-react": "^7.8.3",
    "@babel/preset-typescript": "^7.8.3",
    "@hot-loader/react-dom": "^16.11.0",
    "@types/react": "^16.9.17",
    "@types/react-dom": "^16.9.4",
    "babel-loader": "^8.0.6",
    "html-webpack-plugin": "^3.2.0",
    "react-hot-loader": "^4.12.18",
    "typescript": "^3.7.5",
    "webpack": "^4.41.5",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.10.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

配置文件

{
    "compilerOptions": {
        "target": …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

0
推荐指数
1
解决办法
1705
查看次数