0

Okay, so I made this little function that allows me to make a string into multiplier of 32 characters, but when I use String .replace I get some really, really weird bug. Since its making me pull my hair, can you guys take a look and see what I'm missing.

Variables:
        self.blockSize = 32
        self.interrupt = '$^EnD#Block^$'
        self.filler = '#'

Functions:
    def pad(self, data):
        joint1 = ''.join([data, self.interrupt])
        joint2 = self.filler * ((self.blockSize - len(joint1)) % self.blockSize) 
        return ''.join([joint1, joint2])

    def unpad(self, data):
        data = str(data).rstrip(self.interrupt)
        return data.replace(self.filler, '')

Call:
p = e.pad('this is not a very good idea  yo')
print(p)
print(e.unpad(p))


Output:
    Jans-MacBook-Pro:test2 jan$ ../../bin/python3 data.py 
    this is not a very good idea  yo123$^EnD#Block^$################
    this is not a very good idea  yo123
    Jans-MacBook-Pro:test2 jan$ ../../bin/python3 data.py 
    this is not a very good idea  yo$^EnD#Block^$###################
    this is not a very good idea  y
    Jans-MacBook-Pro:test2 jan$ 

It makes o in yo disappear. Ahhhh! But nothing disappears if I add some random numbers after.

SOLUTION - EDIT: My bad. I have misplaced self.filler and self.interrupt. I am so embarrassed now. The code should have been:

def unpad(self, data):
    data = str(data).rstrip(self.filler)
    return data.replace(self.interrupt, '')
6
  • 1
    As an aside, s1 + s2 is a shorter equivalent of ''.join([s1, s2]). Commented Jan 6, 2013 at 23:01
  • You should make the title of this question a bit more descriptive in case it is searched for later. Commented Jan 6, 2013 at 23:13
  • some say join is faster... Commented Jan 6, 2013 at 23:19
  • @JanNetherdrake depends on the size of the string Commented Jan 6, 2013 at 23:25
  • In [1]: %timeit "test" + "test" 100000000 loops, best of 3: 15.2 ns per loop In [2]: %timeit "".join(("test", "test")) 10000000 loops, best of 3: 98.9 ns per loop Commented Jan 6, 2013 at 23:26

1 Answer 1

5

Read the documentation for rstrip:

The chars argument is a string specifying the set of characters to be removed.

rstrip removes all trailing characters present in the passed set of characters. It doesn't remove a trailing substring consisting of those characters in that order. 'abczyx'.rstrip('xyz') gives 'abc', and 'abczyx'.rstrip('zyx') also gives 'abc'.

Sign up to request clarification or add additional context in comments.

3 Comments

Okay. But why does .replace and .rstrip remove o, where o is nowhere defined to be removed in first place?
When your string ends with "yo", it ends with "o". "o" is present in your "interrupt" string, which means it is one of the characters to be stripped. So it is stripped.
Oh my bad, I figured out why the problem is there :) I misplaced self.filler and self.interrupt respectively. How stupid is that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.