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, '')
s1 + s2is a shorter equivalent of''.join([s1, s2]).In [1]: %timeit "test" + "test"100000000 loops, best of 3: 15.2 ns per loopIn [2]: %timeit "".join(("test", "test"))10000000 loops, best of 3: 98.9 ns per loop