0

I had to write a script that generates some fixture file with increasing fake MAC addresses.

To do that, I decided to have some fun and try to make it as compact as I could. I ended up with:

def mac_address(i):
    return ':'.join(['%02x'] * 6) % tuple([(i >> (8 * j)) & 0xFF for j in reversed(range(6))])

Which actually works pretty well. Obviously, writing this that way is the best way to get slapped by the future person that must work on it, but I did it for the fun (and wrote a more readable version in comment).

But now I'm curious, can you think of any more compact way of writing that ? (That is without removing the spaces).

ereOn
  • 53,676
  • 39
  • 161
  • 238
  • 6
    This question appears to be off-topic because it is about improving a working solution. You might try http://codegolf.stackexchange.com/. – Robᵩ Jul 24 '13 at 15:57
  • @Robᵩ Since when are questions regarding improving working solutions off-topic? – arshajii Jul 24 '13 at 15:58
  • http://stackoverflow.com/help/dont-ask - "*You should only ask practical, answerable questions based on actual problems that you face.*" What is the actual problem? "*avoid asking subjective questions where your answer is provided along with the question*" As the author did here. "*Constructive subjective questions: are more than just mindless social fun*" which the author admits is his motivation. – Robᵩ Jul 24 '13 at 16:03
  • @Robᵩ: I agree. While this question is fun, SO is a bad place to ask it. I don't have the reputation to close this, but I think it should be. –  Jul 24 '13 at 16:05
  • @Robᵩ Putting this particular questions aside, I disagree with the general statement "questions about improving a working solution are off-topic". Such questions are indeed practical and answerable (and can undoubtedly be based on actual problems that one may face). Moreover, they don't provide an answer along with the question: they ask how to something *differently*. – arshajii Jul 24 '13 at 16:18
  • 1
    @arshajii, you are correct. I worded my objection poorly; my original comment is generally inapplicable. I grant that questions relating to improving working solutions are often practical and answerable. – Robᵩ Jul 24 '13 at 16:22

3 Answers3

2

What about

':'.join('%02x' % (i>>(8*j) & 0xFF) for j in reversed(range(6)))

That is more compact and easier to understand.

arshajii
  • 127,459
  • 24
  • 238
  • 287
1
def mac_address(i):
    return ':'.join(a+b for a, b in zip(*[iter('{:012x}'.format(i))]*2))

The first step is to get a hex string zero filled so that it is exactly 12 digits, which is what '{:012x}'.format(i) does. Then we break that string up in two-character chunks using the method of grouping items from the zip() documentation, and join the result on ':'.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
0

Maybe:

from struct import pack, unpack
def mac_address(i):
    return ":".join(["%02x"] * 6) % unpack("BBBBBB", pack("!Q", i)[2:])
kindall
  • 178,883
  • 35
  • 278
  • 309