小编peo*_*les的帖子

为什么在C中添加时会提升整数类型?

所以我们遇到了一个字段问题,经过几天的调试后,将问题缩小到这个特定的代码位,其中没有发生while循环中的处理:

// heavily redacted code
// numberA and numberB are both of uint16_t
// Important stuff happens in that while loop

while ( numberA + 1 == numberB )
{
    // some processing
}
Run Code Online (Sandbox Code Playgroud)

这个运行得很好,直到我们达到了65535的uint16限制.另一堆打印语句后来,我们发现它numberA + 1有一个值65536,同时被numberB包裹回来0.检查失败,没有进行任何处理.

这让我很好奇,所以我整理了一个快速的C程序(用GCC 4.9.2编译)来检查:

#include <stdio.h>
#include <stdint.h>

int main()
{

    uint16_t numberA, numberB;
    numberA = 65535;
    numberB = numberA + 1;

    uint32_t numberC, numberD;
    numberC = 4294967295;
    numberD = numberC + 1;

    printf("numberA = %d\n", numberA …
Run Code Online (Sandbox Code Playgroud)

c integer-promotion

26
推荐指数
2
解决办法
2338
查看次数

javascript removeEventListener不在类中工作

我一直在玩es6类,并尝试设置一个简单的鼠标类.

addEventListener工作,但由于某种原因,我无法删除它们removeEventListener.我猜这与上下文绑定有关,但我无法弄清楚如何解决这个问题.

'use strict'
class Mouser {
    constructor() {
        this.counter = 0
        this.clicked = function (event) {
            this.counter++
            console.log('clicks:', this.counter)
            if (this.counter >= 10) this.remove()
        }
        window.addEventListener('click', this.clicked.bind(this))
    }

    remove() {
        console.log('Removing click listener') // this line runs ..
        window.removeEventListener('click', this.clicked.bind(this))
    }
}

var mouse = new Mouser()
Run Code Online (Sandbox Code Playgroud)

javascript class javascript-events

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

当以编程方式单击按钮与单击DOM时,为什么任务/微任务执行顺序有所不同?

在DOM中单击按钮时,与以编程方式单击时,微任务/任务队列的执行顺序有所不同。

const btn = document.querySelector('#btn');

btn.addEventListener("click", function() {
  Promise.resolve().then(function() { console.log('resolved-1'); });
  console.log('click-1');
});

btn.addEventListener("click", function() {
  Promise.resolve().then(function() { console.log('resolved-2'); });
  console.log('click-2');
});
Run Code Online (Sandbox Code Playgroud)
<button id='btn'>Click me !</button>
Run Code Online (Sandbox Code Playgroud)

我的理解是,当调用栈为空时,事件循环将从微任务队列中获取回调以放置在调用栈中。当调用堆栈和微任务队列均为空时,事件循环开始从任务队列中进行回调。

单击带有id btn的按钮时,两个“ click”事件侦听器都将按声明它们的顺序放入任务队列中。

// representing the callstack and task queues as arrays
callstack: []
microtask queue: []
task queue: ["click-1", "click-2"]
Run Code Online (Sandbox Code Playgroud)

事件循环将其"click-1" callback放置在调用堆栈上。它有一个立即解决的承诺,将其"resolved-1" callback放置在微任务队列上。

callstack: ["click-1"]
microtask queue: ["resolved-1"]
task queue: ["click-2"]
Run Code Online (Sandbox Code Playgroud)

"click-1" callback执行它的console.log,并完成。现在,微任务队列上有内容,因此事件循环接受"resolved-1" callback并将其放置在调用堆栈中。

callstack: ["resolved-1"]
microtask queue: []
task queue: ["click-2"] …
Run Code Online (Sandbox Code Playgroud)

javascript

13
推荐指数
2
解决办法
336
查看次数

bash环境变量$中的字符是什么意思?

我一直在查看一些随各种Linux发行版一起提供的.bashrc和.profile脚本,并且看到有时它们会检查$-.

这是Ubuntu中的一个

case $- in
    *i*) ;;
    *) return;;
esac
Run Code Online (Sandbox Code Playgroud)

在这种情况下,它检查"i"标志是否存在以查看当前shell是否是交互式shell.

我当前的会议给了我这个:

# echo $-
himBH
Run Code Online (Sandbox Code Playgroud)

其他标志/选项是什么意思?某处有完整的清单吗?

linux bash

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

在写入之前从文件读取,然后关闭

在写入之前,我试图从原始空文件中读取,然后关闭它.这在Python中可行吗?

with open("outfile1.txt", 'r+') as f:
    f.write("foobar")
    f.flush()
    print("File contents:", f.read())
Run Code Online (Sandbox Code Playgroud)

冲洗用f.flush()似乎不起作用,因为最终f.read()仍然没有打印.

除了重新打开文件之外,有没有办法从文件中读取"当前数据"?

python file

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

为什么python中的列表操作在函数范围之外运行?

在下面的python代码中,变量number被传递给函数addone,并且操作本地副本.数字的值保持不变.

def addone(num):
    num = num + 1
    print "function: added 1, now %d" % num

number = 5
print "Before:", number
addone(number)
print "After:", number
Run Code Online (Sandbox Code Playgroud)

输出:

Before: 5
function: added 1, now 6
After: 5
Run Code Online (Sandbox Code Playgroud)

但是,行为似乎与pop,append等列表操作不同.这有点让我感到困惑.所有列表操作是否全局运行?如果是这样,背后有什么特别的原因吗?

def pop_first(stuff):
    popped = stuff.pop(0)
    print "function: '%s' was popped!" % popped

words = ["A", "list", "of", "words"]
print "Before:", words
pop_first(words)
print "After:", words
Run Code Online (Sandbox Code Playgroud)

输出:

Before: ['A', 'list', 'of', 'words']
function: 'A' was popped!
After: ['list', 'of', 'words']
Run Code Online (Sandbox Code Playgroud)

python scope list

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

Python:send()在生成器中的行为

我正在python 3中尝试生成器,并编写了这个相当人为的生成器:

def send_gen():
    print("    send_gen(): will yield 1")
    x = yield 1
    print("    send_gen(): sent in '{}'".format(x))
    # yield  # causes StopIteration when left out


gen = send_gen()
print("yielded {}".format(gen.__next__()))

print("running gen.send()")
gen.send("a string")
Run Code Online (Sandbox Code Playgroud)

输出:

    send_gen(): will yield 1
yielded 1
running gen.send()
    send_gen(): sent in 'a string'
Traceback (most recent call last):
  File "gen_test.py", line 12, in <module>
    gen.send("a string")
StopIteration
Run Code Online (Sandbox Code Playgroud)

因此gen.__next__()到达该行x = yield 1并产生1。我认为x将被分配给None,然后gen.send()将查找下一个 yield因为 …

python yield generator coroutine python-3.x

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

如何同时grep多行(从另一个命令输出)?

我有一个在后台运行的Linux驱动程序,能够返回当前系统数据/统计信息。我通过在控制台中运行控制台实用程序(我们称其为dump-data)来查看数据。每次我运行dump-data时,所有数据都会被转储。该实用程序的输出如下所示

Output:
- A=reading1
- B=reading2
- C=reading3
- D=reading4
- E=reading5
...
- variableX=readingX
...
Run Code Online (Sandbox Code Playgroud)

该实用程序返回的读数列表可能会很长。根据情况,某些读数将是有用的,而其他一切都将是无用的。

我需要一种方法来仅对有用名称的grep有用(仅通过bash脚本)进行grep。即有时我需要收集A,D,E;而其他时候我需要C,D,E。

我试图绘制随时间变化的读数以寻找趋势,所以我无法运行以下内容:

# forgive my pseudocode
Loop
    dump-data | grep A
    dump-data | grep D
    dump-data | grep E
End Loop
Run Code Online (Sandbox Code Playgroud)

收集A,D,E,因为这实际上会使我从3次单独的转储数据调用中读取数据,因为那是不准确的。

linux bash scripting grep

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

退出脚本后如何返回到 python shell?

我的 python 脚本有几个 sys.exit() 调用在某些条件下终止。为了测试它,我打开了一个 python shell 并运行exec(open('script.py').read()). 每当我sys.exit()打电话时,脚本都会终止,python shell 也会终止,这很烦人。

因此,我尝试发挥创意,并尝试通过检查堆栈来确定我的脚本是从命令行运行还是从 shell 运行。来源:检测 python 脚本是从 ipython shell 运行,还是从命令行运行

def exit_now(exit_status):
    os.system('pause')

    if PythonShell:  # how do I return to the python shell ?
        ???
    else:  # exit if we are running from the commandline
        sys.exit(exit_status)

...

# check if this was run from inside a python shell
frames = len(inspect.stack())
if frames > 1:
    PythonShell = True
else:
    PythonShell = False

...

if condition1: …
Run Code Online (Sandbox Code Playgroud)

python shell python-3.x

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