2

I've the following function, createFreeSpaces(first_byte, last_byte) that inputs two numbers first_byte and last_byte (always integers), and creates a list with the numbers between those two numbers on a specific format. It's very easy, but a bit hard for me to explain, so let's see my try and an example.

Ex:
createFreeSpaces(4, 7)

Output:

555555 0 0 "FREE: [5.0]"
555555 0 0 "FREE: [5.1]"
555555 0 0 "FREE: [5.2]"
555555 0 0 "FREE: [5.3]"
555555 0 0 "FREE: [5.4]"
555555 0 0 "FREE: [5.5]"
555555 0 0 "FREE: [5.6]"
555555 0 0 "FREE: [5.7]"
555555 0 0 "FREE: [6.0]"
555555 0 0 "FREE: [6.1]"
555555 0 0 "FREE: [6.2]"
555555 0 0 "FREE: [6.3]"
555555 0 0 "FREE: [6.4]"
555555 0 0 "FREE: [6.5]"
555555 0 0 "FREE: [6.6]"
555555 0 0 "FREE: [6.7]"

This is my try, as you can see it seems a bit dirty and not so Pythonic.

def createFreeSpaces(first_byte, last_byte):
    start_bit = 0
    end_bit = 7

    b_start = first_byte + 1
    b_end = last_byte
    b_diff = b_end - b_start

    h = 0
    final_list = []
    while h < b_diff * 8:
        if start_bit == 8:
            start_bit = 0
            b_start = b_start + 1
        final_list.append('555555 0 0 "FREE: [' + str(b_start) + '.' + str(start_bit) + ']"')
        s_start = b_start + 1
        start_bit = start_bit + 1
        h = h + 1
    return final_list

I'm cleaning my code so I would like to know if someone can someone give me a hand and tell me how can I make this simple function on a more pythonic way?

2
  • is the input always integer? Commented Oct 1, 2015 at 7:13
  • Yes, always integer. Commented Oct 1, 2015 at 7:14

1 Answer 1

6

Since you say that the input will always be integer (according to the comments). You can use a single line list comprehension for this. Example -

def createFreeSpaces(first_byte, last_byte):
    return ['555555 0 0 "FREE: [{}.{}]"'.format(x,y) for x in range(first_byte + 1, last_byte) for y in range(8)]

Making the list comprehension line a bit smaller -

def createFreeSpaces(fbyte, lbyte):
    fmt = '555555 0 0 "FREE: [{}.{}]"'
    return [fmt.format(x,y) for x in range(fbyte + 1, lbyte) for y in range(8)]

Demo -

>>> def createFreeSpacesNew(first_byte, last_byte):
...     return ['555555 0 0 "FREE: [{}.{}]"'.format(x,y) for x in range(first_byte + 1, last_byte) for y in range(8)]
...
>>> pprint.pprint(createFreeSpacesNew(4,7))
['555555 0 0 "FREE: [5.0]"',
 '555555 0 0 "FREE: [5.1]"',
 '555555 0 0 "FREE: [5.2]"',
 '555555 0 0 "FREE: [5.3]"',
 '555555 0 0 "FREE: [5.4]"',
 '555555 0 0 "FREE: [5.5]"',
 '555555 0 0 "FREE: [5.6]"',
 '555555 0 0 "FREE: [5.7]"',
 '555555 0 0 "FREE: [6.0]"',
 '555555 0 0 "FREE: [6.1]"',
 '555555 0 0 "FREE: [6.2]"',
 '555555 0 0 "FREE: [6.3]"',
 '555555 0 0 "FREE: [6.4]"',
 '555555 0 0 "FREE: [6.5]"',
 '555555 0 0 "FREE: [6.6]"',
 '555555 0 0 "FREE: [6.7]"']
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for. Thanks a lot, Anand.

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.