我正在阅读doubled链表的linux内核实现.我不明白宏的用法WRITE_ONCE(x, val).它在compiler.h中定义如下:
#define WRITE_ONCE(x, val) x=(val)
Run Code Online (Sandbox Code Playgroud)
它在文件中使用了七次,例如
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
WRITE_ONCE(prev->next, new);
}
Run Code Online (Sandbox Code Playgroud)
我读过它用于避免竞争条件.
我有两个问题:
1 /我认为宏在编译时被代码替换.那么这段代码与以下代码有何不同?这个宏如何避免竞争条件?
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
Run Code Online (Sandbox Code Playgroud)
2 /如何知道何时应该使用它?例如,它用于__lst_add()但不用于__lst_splice():
static inline void __list_splice(const struct list_head *list,
struct …Run Code Online (Sandbox Code Playgroud) 我正在阅读有关gcc预处理的文档,我读了下面的句子(这里):
如果任何输入文件的最后一行缺少行尾标记,则认为文件末尾隐式提供一个.C标准表示这种情况会引发未定义的行为,因此GCC会发出警告信息.
我尝试通过以下方式产生警告:
> echo -n "int main(void) {return 0;}" > test.c
> gcc -Wall -Wextra -Werror test.c
Run Code Online (Sandbox Code Playgroud)
但没有概率,它编译.我将行尾标记理解为新行字符,但它似乎是其他任何东西.
我怎么能发出警告?
class Test(object):
def __init__(self, store):
assert isinstance(store, dict)
self.store = store
def __getitem__(self, key):
return self.store[key]
Run Code Online (Sandbox Code Playgroud)
我试着在这堂课上喋喋不休.在本文档中 说,实现__getitem__应该足以让我的Test类.实际上,当我试图通过它时,它并没有告诉我我不能,但我有一个KeyError:
In [10]: a = Test({1:1,2:2})
In [11]: for i in a: print i
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-11-8c9c9a8afa41> in <module>()
----> 1 for i in a: print i
<ipython-input-9-17212ae08f42> in __getitem__(self, key)
4 self.store = store
5 def __getitem__(self, key):
----> 6 return self.store[key]
7
KeyError: 0
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过添加__iter__函数来解决它:
def __iter__(self):
return dict.__iter__(self.store)
Run Code Online (Sandbox Code Playgroud)
您将如何测试可能导致随机选择的函数?
例如:
from random import shuffle
def getMaxIndices(lst):
'''
:lst: list of int
Return indices of max value. If max value appears more than once,
we chose one of its indices randomly.
'''
index_lst = [(i, j) for i, j in enumerate(lst)]
shuffle(index_lst)
index_lst.sort(key=lambda x: x[1])
max_index = index_lst.pop()[0]
return max_index
Run Code Online (Sandbox Code Playgroud)
你会如何测试它?
我正在用 cypress 测试一个网络应用程序。
我登录了我的beforeEach函数。然后,在我的不同测试中,我从cy.visit('mysite.com/url').
我的问题是登录后,网站重定向到网站的特定页面。这种重定向发生在cy.visit我的测试之后。因此,我的测试在重定向页面上运行,它们失败了。
重定向似乎与我可以等待的任何请求无关。
我最终得到了,cy.wait(3000)但不是很令人满意。我的测试有时会失败,因为重定向可能需要 3 秒以上。我不想增加那个时间,因为运行我的测试需要很长时间。
有没有办法做这样的事情:
while (!cy.url().eq('mysite.com/redirection-url')) {
cy.wait(300);
}
Run Code Online (Sandbox Code Playgroud) 假设我想初始化这个元组:
t = (
#(id, name)
(1, 'aasd'),
(2, 'bsfd'),
(3, 'asf'),
...
(21, 'aefae'),
)
Run Code Online (Sandbox Code Playgroud)
我相信我可以按照vim做.
1 /输入:
t = (
#(id, name)
(, 'aasd'),
(, 'bsfd'),
(, 'asf'),
...
(, 'aefae'),
)
Run Code Online (Sandbox Code Playgroud)
2 /视觉选择昏迷行,并键入一个棘手的键序列,它将写入连续的数字
有谁知道我应该键入什么是棘手的键序列?
我正在学习matplolib。我尝试在表面上显示特定点,并得到以下结果:

两个屏幕截图均来自同一图。在第一个上,我们几乎看不到要显示的点,而在第二个上。我想让它出现在每个。这是我的代码:
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
hours, pauses, results = [], [], []
with open('./data.txt', 'r') as f:
for line in f:
value = line.split()
for i, l in enumerate([hours, pauses, results]):
l.append(int(value[i]))
for l in [hours, pauses, results]:
l = np.asarray(l, dtype=float)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
m = len(hours)
WP_init = 5 * np.random.rand()
WH_init = 5 * np.random.rand()
Z_init = 0
for i …Run Code Online (Sandbox Code Playgroud) python ×3
c ×2
cypress ×1
dictionary ×1
gcc ×1
iterator ×1
javascript ×1
linux ×1
macros ×1
matplotlib ×1
random ×1
testing ×1
vim ×1
wait ×1