About Me

Thursday, November 24, 2011

Python, shit what did we do before it????

So I've been playing with some string formatting for one of our legacy UI's which is basically just a textScroller that gets filled in a loop with all the export Tag information for the current scene, but I wanted to line up the data nicely. In mel this was just horrible but Python..... 1 line!

The format calls on strings are just fabulous, if you've not taken a look at them them I recommend you do so, here's the Python docs that relate to them:


So in the example here we have some string data that we want to carefully format. We want to make sure each entry in the line is the same length and some of them want to be left, right or center justified.

this='This is'

print '{this:.<15}:{nice:_^12}:{form:<20}:{data:>7}'.format(\

#This is........:___nicely___:formatted           :   data
So :

The above tells the format to replace the key 'this' with the passed in arg, then the '.<' tells it that it's left justified and any gaps should be replaced with '.' finally the number after it is the length to pad the data out to.
Similarly the '_^12' is formatting to center justify and adding '_' to the padding. Now that line fed to a loop thats filling the textScroll with scene data formats and lines everything up (as long as you set the fontType to the scroll to be fixedWidth that is)

Now that in Mel was the length of this blog!

No comments:

Post a Comment