在使用Python的GTK3程序上我实现了一个日志.这是一个带有TextBuffer的TextView,位于ScrolledWindow中.通过例程,我在此日志中添加了一个新行.之后它应该滚动到最后一行.
def append_log(self, line):
self.textbuffer.insert(self.textbuffer.get_end_iter(), "\n"+line, -1)
# scrolling down
Run Code Online (Sandbox Code Playgroud)
它应该是这样的:http://www.physik.tu-dresden.de/~s9472632/log_manual.png
但它不起作用.我试过以下代码.
# nothing happens
adj = self.scrolledwindow.get_vadjustment()
adj.set_value(adj.get_upper()-adj.get_page_size()) # same without subtraction (only upper or page_size)
self.scrolledwindow.set_vadjustment(adj) # with and without this line
Run Code Online (Sandbox Code Playgroud)
.
# nothing happens
self.textview.scroll_to_iter(self.textbuffer.get_end_iter(), 0.0, False, 0.5, 0.5)
Run Code Online (Sandbox Code Playgroud)
.
# works a bit but scroll bars cover the text (see picture below)
self.textview.scroll_to_mark(self.textbuffer.get_insert(), 0.0, False, 0.5, 0.5)
Run Code Online (Sandbox Code Playgroud)
图片:http://www.physik.tu-dresden.de/~s9472632/log_scroll.png
似乎scroll_to*(within_margin,use_align,xalign,yalign)的最后四个参数不会影响结果.
怎么运作?
再见马库斯
我正在通过 Python 的元类操纵类的创建。但是,尽管类由于其父类而具有属性,但我无法删除它。
class Meta(type):
def __init__(cls, name, bases, dct):
super().__init__(name, bases, dct)
if hasattr(cls, "x"):
print(cls.__name__, "has x, deleting")
delattr(cls, "x")
else:
print(cls.__name__, "has no x, creating")
cls.x = 13
class A(metaclass=Meta):
pass
class B(A):
pass
Run Code Online (Sandbox Code Playgroud)
上面代码的执行产生一个AttributeErrorwhen 类B被创建:
A has no x, creating
B has x, deleting
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-49e93612dcb8> in <module>()
10 class A(metaclass=Meta):
11 pass
---> 12 class B(A):
13 pass
14 class C(B):
<ipython-input-3-49e93612dcb8> in __init__(cls, name, …Run Code Online (Sandbox Code Playgroud) 我发现了很多关于这个问题的线索,但所有这些问题都是命名空间.我的问题与命名空间无关.
一个小例子:
import cPickle as pickle
from uncertainties import Variable
class value(Variable):
def __init__(self, args, showing=False):
self.show = showing
Variable.__init__(self, args[0], args[1])
val = value((3,1), True)
print val.nominal_value, val.std_dev(), val.show
fobj = file("pickle.file", "w")
pickle.dump(val, fobj)
fobj.close()
fobj = file("pickle.file", "r")
val = pickle.load(fobj)
fobj.close()
print val.nominal_value, val.std_dev(), val.show
Run Code Online (Sandbox Code Playgroud)
这段代码的输出:
3.0 1.0 True
3.0 1.0
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
173 else:
174 filename = fname
--> 175 __builtin__.execfile(filename, *where)
/home/markus/pickle.py in <module>()
19 …Run Code Online (Sandbox Code Playgroud) 为了解决一个只能逐个元素出现的问题,我需要将 NumPy 的元组索引与显式切片相结合。
def f(shape, n):
"""
:param shape: any shape of an array
:type shape: tuple
:type n: int
"""
x = numpy.zeros( (n,) + shape )
for i in numpy.ndindex(shape): # i = (k, l, ...)
x[:, k, l, ...] = numpy.random.random(n)
Run Code Online (Sandbox Code Playgroud)
x[:, *i]结果为 aSyntaxError并被x[:, i]解释为numpy.array([ x[:, k] for k in i ])。Unfortunally这是不可能有n维作为最后一个(x = numpy.zeros(shape+(n,))对x[i] = numpy.random.random(n)由于进一步使用的)x。
编辑:这里有一些例子希望在评论中。
>>> n, shape = 2, (3,4)
>>> …Run Code Online (Sandbox Code Playgroud) 我尝试通过简短的 C 代码片段来计算单个进程的 CPU 周期。MWE 是cpucycles.c。
\n\ncpucycles.c(主要基于手册页示例)
\n\n#include <stdlib.h>\n#include <stdio.h>\n#include <unistd.h>\n#include <string.h>\n#include <sys/ioctl.h>\n#include <linux/perf_event.h>\n#include <asm/unistd.h>\n\nstatic long\nperf_event_open(struct perf_event_attr *hw_event, pid_t pid,\n int cpu, int group_fd, unsigned long flags)\n{\n int ret;\n ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,\n group_fd, flags);\n return ret;\n}\n\nlong long\ncpu_cycles(pid_t pid, unsigned int microseconds)\n{\n struct perf_event_attr pe;\n long long count;\n int fd;\n\n memset(&pe, 0, sizeof(struct perf_event_attr));\n pe.type = PERF_TYPE_HARDWARE;\n pe.size = sizeof(struct perf_event_attr);\n pe.config = PERF_COUNT_HW_CPU_CYCLES;\n pe.disabled = 1;\n pe.exclude_kernel = 1;\n pe.exclude_hv …Run Code Online (Sandbox Code Playgroud) 我有几台服务器使用以下sshd配置.
# Authentication:
PermitRootLogin no
AllowGroups ssh
PubkeyAuthentication yes
PasswordAuthentication no
Run Code Online (Sandbox Code Playgroud)
这意味着组"ssh"中的每个用户都可以登录,但只能使用pubkey.不允许登录root.
但是root必须有一个例外:我的$ ip备份服务器必须以root身份登录.
我试过了:
AllowUsers root@$ip
AllowGroups ssh
Run Code Online (Sandbox Code Playgroud)
但AllowUsers会覆盖AllowGroups语句.所以只有来自$ ip的root才能登录.
Match User root, Address $ip
PermitRootLogin {yes|without-password}
AllowUsers root
Run Code Online (Sandbox Code Playgroud)
和
Match Address $ip
PermitRootLogin {yes|without-password}
AllowUsers *
Run Code Online (Sandbox Code Playgroud)
两者都被完全忽略了.组"ssh"中的正常用户只能登录.
这是一个简单的场景,用户登录限制为pubkey,root登录限制为pubkey和某些ip.怎么解决?