使用 curses 在 python 中进行字符串切片

String slicing in python with curses

注意。此问题与最初提交给 codereview

的代码有关

https://codereview.stackexchange.com/questions/101011/mengenlehreuhr-in-python

上查看原始 link

所以我写了一个时钟。不只是任何时钟,这个时钟的目的是破解Kryptos section K4

虽然应用程序的完整源代码现在是 on GitHub,但我遇到的具体问题与以下方法有关:

    def _update(self):
        self.window.erase()
        self.window.box()
        self.window.addstr(0, 1, str(self.index).zfill(2) + ' - ' + TimeDecipher.ciphertext[0:self.index])
        self.window.addstr(1, self.cipher_offset, TimeDecipher.ciphertext[0:self.index-1], self.color)
        self.window.addstr(
            1,
            (self.cipher_offset + self.index-1),
            TimeDecipher.ciphertext[self.index-1:self.index],
            self.highlight
        )
        self.window.addstr(1,
            (self.cipher_offset + self.index),
            TimeDecipher.ciphertext[self.index:],
            self.color
        )
        self.window.addstr(2, 1, ''.join(['-' for i in range(self.width - 2)]))
        for i, item in enumerate(self.lines):
            self.window.addstr((i + 3), 1, str(item))
        self.window.refresh()

问题描述如下

self.index < len(self.time_decipher.ciphertext)(97 个字符)时,突出显示按预期工作。

OBKRUOX|O|GHULBSOLIFBBWFL...

然而,当 self.index == ~96 我看到这个:

|AR|OBKRUOX...

基本上最后 2 个字符从打印字符串的末尾切掉并放在开头。

请有人解释为什么会这样,我可以做些什么来克服这个问题?

问题已解决 - 计数出现问题。

设置 self.index 的方法正在从另一个方法传递信息,这意味着它总是比拼接所需的值晚 1,并在达到 97(字符串的长度)时重置为 0。

因为切片是从1到return,到了96的时候,index其实是-1,97是0。

更新了 Log::__add__ 方法以将索引设置为:

    self.index = other.index if other.index > 0 else len(TimeDecipher.ciphertext) + other.index