简体   繁体   English

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

[英]String slicing in python with curses

Note. 注意。 This question relates to code originally submitted to codereview 这个问题与最初提交给codereview的代码有关

See oringinal link at https://codereview.stackexchange.com/questions/101011/mengenlehreuhr-in-python 参见https://codereview.stackexchange.com/questions/101011/mengenlehreuhr-in-python中的原始链接

So I have written a clock. 所以我写了一个时钟。 Not just any clock, the purpose of this clock is to crack Kryptos section K4 . 不只是任何时钟,此时钟的目的是破解Kryptos部分K4

Whilst the full source of the application is now on GitHub , the specific problem I am experiencing relates to the following method: 虽然该应用程序的完整源代码现在位于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()

The problem is described as follows. 问题描述如下。

When self.index < len(self.time_decipher.ciphertext) (97 characters), the highlighting works as anticipated. self.index < len(self.time_decipher.ciphertext) (97个字符)时,突出显示将按预期进行。

OBKRUOX|O|GHULBSOLIFBBWFL...

However, when self.index == ~96 I see this: 但是,当self.index == ~96时,我看到了:

|AR|OBKRUOX...

Basically the last 2 characters are cut from the end of the printed string and dropped at the start. 基本上,最后2个字符是从打印字符串的末尾截取的,并在开头处删除。

Please can someone explain why this is happening and what I can do to overcome this? 请有人可以解释为什么会发生这种情况,我能做些什么来克服这一点?

Problem solved - There was an issue with counting. 问题已解决-计数有问题。

The method which set self.index was being passed that information from another method meaning it was always 1 behind the value required for splicing and reset to 0 when it reached 97 (the length of the string). 设置self.index的方法正在从另一种方法传递该信息,这意味着它始终self.index所需的值落后1,并在达到97(字符串的长度)时重置为0。

Because the slicing works from 1 to return, when it reached 96, the index was actually -1 and 97 was 0. 因为切片从1到返回工作,所以当切片达到96时,索引实际上为-1,而97为0。

Updated Log::__add__ method to set index as: 更新了Log::__add__方法,将索引设置为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM