小编Kir*_*hou的帖子

ReactJS:如何在Jest中使用动作触发reducer

我正在为减速机测试工作.但是具有动作功能的减速器的返回状态是异常的.

reducer.react-test.js

import reducer from '../../test_module/reducer';

describe('Test Reducer', () => {

  const initStatus = { id: -1, list: [] };

  it('1. has default state', () => {    
    expect(reducer(initStatus, { type: 'unexpected' })).toEqual({
      ...initStatus
    });    
  });

  it('2. has added once', () => {    
    expect(reducer(initStatus, { type: "ADD" })).toEqual({
      ...initStatus,
      id: 0,
      list: [0],
    });   
  });

  it('3. has added twice', () => {    
    const afterAddOnce = reducer(initStatus, { type: "ADD" });
    expect(reducer(afterAddOnce, { type: "ADD" })).toEqual({
      ...initStatus,
      id: 1,
      list: [0,1],
    }); …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs jestjs redux

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

用于选择下拉列表的 Laravel 模型绑定

我希望在编辑表单中选择选择下拉值。

在我的控制器中

public function edit($id)
{
    $vedit = DB::table('vehicles')->where('id', $id)->first();
    $cartype= DB::table('car_category')->pluck('cartype'); 
    return view('vehicles.edit', compact('vedit','cartype'));
}
Run Code Online (Sandbox Code Playgroud)

视野中

{{ Form::label('Vehicle Type', 'Vehicle Type') }}
<select name="vehicle_type" class="form-control">
  @foreach($cartype as $cartypes)   
  <option value="{{ $cartypes}}">{{ $cartypes}}</option>
  @endforeach
</select>
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现这个目标?

php laravel-5

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

使用 Python 提取 BZ2 压缩文件夹

我正在尝试bz2在特定位置提取压缩文件夹。我可以通过以下方式看到里面的数据:

handler = bz2.BZ2File(path, 'r')
print handler.read()
Run Code Online (Sandbox Code Playgroud)

但我希望将此压缩文件夹中的所有文件解压缩到一个位置(由用户指定),以维护文件夹的内部目录结构。

我对这种语言还很陌生.. 请帮忙...

python compression

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

Option 类型的类型转换

我是 Python 的 Rust 新手。我相信这是一个基本问题,但我太新了,无法通过像Type Casting Option这样的关键字找到答案。

在Python中,为了让类型检查器知道返回类型不是Optional[int] + int,我们可以解决assert逻辑来强制类型检查器知道x永远不会在行None之后assert

from typing import Optional


def add_one(x: Optional[int] = None) -> int:
    if x is None:
        x = 0
    assert x is not None
    return x + 1


if __name__ == '__main__':
    add_one(0)    # 1
    add_one()     # 1
    add_one(999)  # 1000
Run Code Online (Sandbox Code Playgroud)

在 Rust 中,假设接口相同,如何实现相同的事情?x即,如何让编译器知道不再是类型Option

fn add_one(mut x: Option<i32>) -> i32 {
    if x == None …
Run Code Online (Sandbox Code Playgroud)

casting type-conversion rust

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

Flask,获取当地时间

我正在用 Python 构建一个 Flask 应用程序,我想保存用户本地时间。

我想用这个

time.strftime('%A %B, %d %Y %H:%M:%S')
Run Code Online (Sandbox Code Playgroud)

但这给了我服务器本地时间。

python flask

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

TypeError:在Python 2中,必须以实例作为第一个参数(而不是int实例)调用未绑定方法

在python 3中,以下代码集有效,我想知道为什么在python 2.7中它给我TypeError:必须以calc实例作为第一个参数(而不是int实例)调用未绑定方法add()?如何在Python 2.7中解决此问题?

class calc:
    def add(x,y):
        answer = x + y
        print(answer)

    def sub(x,y):
        answer = x - y
        print(answer)

    def mult(x,y):
        answer = x * y
        print(answer)

    def div(x,y):
        answer = x / y
        print(answer)

calc.add(5, 7)
Run Code Online (Sandbox Code Playgroud)

python python-2.7

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

Rust 溢出时带边界的整数运算

我最近遇到的问题需要根据整数类型的位进行带边界的整数运算。

例如,使用i32整数进行add运算,这里有一段伪代码来表达这个想法:

sum = a + b
max(min(sum, 2147483647), -2147483648)

// if the sum is larger than 2147483647, then return 2147483647.
// if the sum is smaller than -2147483648, then return -2147483648.
Run Code Online (Sandbox Code Playgroud)

为了实现这一点,我天真地写了以下丑陋的代码:

sum = a + b
max(min(sum, 2147483647), -2147483648)

// if the sum is larger than 2147483647, then return 2147483647.
// if the sum is smaller than -2147483648, then return -2147483648.
Run Code Online (Sandbox Code Playgroud)

代码运行良好;但是我的六感告诉我使用类型转换是有问题的。因此,我尝试使用传统的恐慌(异常)处理来解决这个问题……但我坚持使用下面的代码(恐慌结果无法检测下溢或溢出):

fn i32_add_handling_by_casting(a: i32, b: i32) -> i32 {
    let sum: …
Run Code Online (Sandbox Code Playgroud)

integer-overflow panic rust

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

Linux的Max Pipe命令

我最近写了一个简单的shell.我实现管道时出现了这个问题.

我知道在Ubuntu命令行参数的最大长度为2097152.(通过)

#define MAX_CMD_LEN 2097152
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有最大数量的管道命令?例如:( n的数量)

ps -aux |?grep "a.out" | awk '{print $5}' | top | ... | cat
1       |  2            | 3                | 4   | ... | n
Run Code Online (Sandbox Code Playgroud)

linux shell pipe

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

strncat上的分段错误()

首先,我写了一个简单的程序.

  1 #include <string.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4
  5 int main()
  6 {
  7     char *buf = (char*)malloc(sizeof(char)*2000);
  8     char tmp[256];
  9     strcpy (tmp, "test");
 10     printf ("tmp: %s\n", tmp);
 11     strncat (buf, tmp, strlen(tmp));
 12     printf ("buf: %s\n", buf);
 13 }
Run Code Online (Sandbox Code Playgroud)

预期的结果是:

tmp: test
buf: test
Run Code Online (Sandbox Code Playgroud)

但是在我的大项目中合并代码之后.(使用大量堆段)

153     char *inbuf = (char*)malloc(sizeof(char)*2000);
154     char *outbuf = (char*)malloc(sizeof(char)*2000);
155     char *oldinbuf = (char*)malloc(sizeof(char)*2000);
156     char *errbuf = (char*)malloc(sizeof(char)*2000);
157     memset (inbuf, '\0', strlen(inbuf));
158     memset (oldinbuf, '\0', …
Run Code Online (Sandbox Code Playgroud)

c string heap-memory segmentation-fault

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

强制为函数调用赋值

我知道可以通过这种方式替换 buildin 函数。

>>> import os
>>> print(os.system)
<built-in function system>

>>> os.system = "a"
>>> print(os.system)
a
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我不能os.system直接用源代码编写。然后我尝试了getattr。但它不起作用。

>>> import os
>>> print(getattr(os, "system"))
<built-in function system>

>>> getattr(os, "system") = "a"
  File "<stdin>", line 1
SyntaxError: can't assign to function call
Run Code Online (Sandbox Code Playgroud)

这有可能实现吗?

python python-3.x

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

精确地图数据数组

我正在构建一个组件,该组件将动态列出接下来的三个会议日期。在组件内部,当我console.log时,props变量将其映射到数组中的每个字符。控制台日志中的第一行是getMeetingDates函数返回的内容

在此处输入图片说明

这是渲染

render() {
    return (
        <div class={style.sidebar}>
            <div className="sidebar-sections">
                <h3>Next Meeetings</h3>
                {this.getMeetingDates().map(date => <MeetingDates {...date} />)}
            </div>
        </div>
    );
}
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?

reactjs preact

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

HTML图像和文本对齐顶部和底部

我使用下面的代码来显示图像和一些1 2 3 4正确的文本.我是html和css的新手,虽然图像部分工作正常,但我希望文本对齐以填充图像的顶部和底部,如下所示:

在此输入图像描述

同样的方法,如果我添加更多文本然后自动对齐...我应该在哪里寻找这个?

<!DOCTYPE html>
<html>
  <head>
    <style></style>
  </head>
  <body>
    <img src = "http://icons.iconarchive.com/icons/uiconstock/christmas-clipart/128/flower-icon.png" />1 2 3 4   
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

html css

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