Lyxcoder
Lyxcoder

Reputation: 33

Transcrypt not converting strip() function

So, I have this python file that generates a list of numbers and converts them into a string:

def gen():

    array = []
    for value in range(10):
        array.append(value)
    random.seed(20)
    random.shuffle(array)

    array_str = str(array)
    array_str = array_str.strip('[]')
    array_str = array_str + '.'

where there is a line in a HTML code in an HTML file elsewhere that runs the gen() function stated here.

However, when Transcrypt is run on that Python code, the array_str.strip('[]') part of the code doesn't seem to be converting well as the outer square brackets of the generated list [] is still seen on the webpage when I run the server. Is there a way to fix this?

Upvotes: 1

Views: 68

Answers (1)

Matthias
Matthias

Reputation: 3970

It's simply not supported to use .strip for anything other than whitespace at the moment.

Looking at the generated javascript code you can see Transcrypt runs:

var array_str = array_str.strip ('[]');

with .strip defined as

String.prototype.strip = function () {
    return this.trim ();
};

You can try just removing the first and last characters as a workaround if the brackets are always at beginning and end (which I suppose they should be), something like this:

def gen():
    array = []
    for value in range(10):
        array.append(value)
    random.seed(20)
    random.shuffle(array)

    array_str = str(array)
    array_str = array_str[1:-1]
    array_str = array_str + '.'

Upvotes: 2

Related Questions