0

I am trying to write out a code for doing a number/ version padding but as I tried to search thru the internet, I am however, only able to find a MEL example in which it works but it doesn't make sense to me (most probably I do not understand how it works)

$padding = 3;
$num = 5;
string $pad = `python ("'%0"+$padding+"d' % "+$num)`;
// Results is: 005

However when I tried to convert it into Python style, I got the following result:

padding = '3'
num = '5'
pad = ("%0"+padding+"d' % "+num)
Result is: %03d' % 5

Or even as I tried to rearrange the code around, the results is either wrong (totally wrong, as you can see) or Maya errors like TypeError: cannot concatenate 'str' and 'int' objects

Any pointers?

dissidia
  • 1,531
  • 3
  • 23
  • 53

1 Answers1

1
padding = '3'
num = '5'
pad = ("%%0%si" % padding) % int(num)
print pad # prints '005'

how this works:

you use %% to escape the %% to % after string processing::

In [17]: step1 = ("%%0%si" % padding)

In [18]: step1
Out[18]: '%03i'

In [19]: step2 = step1 % int(num)

In [20]: step2
Out[20]: '005'

how this works: (second try ;))

when we do string processing the "%" has special meaning

"%s" Replaced with a string
"%i" Replaced with an integer
"%%" means I am an "%", kinda like \n is newline and \\ is \

The %x can be furether modified by putting numbers between

"%10s" means a string padded to 10 with spaces
"%010i" means an integer padded to 10 with zeros

what we want is the integer one, since the numbers zero part has to come from a variable we have to do two steps of string processing, thus we use the %%->% trick in the first round of string processing th get the %03i'

Here I use parenthesis to group stuff together logically, there are of course no parenthesis in the real code :)

"(%%)0(%s)i" % num

%% => %
"%s" % num => '3'

(%)0(3)i
%03i
jcr
  • 1,015
  • 6
  • 18
  • cool, it works.. but what happens if both the `padding` and `num` are user defined? Yours is pretty much hard-coded right? I tried using yours but as I convert it to `'3'` or `'5'`, I got the error `TypeError: %d format: a number is required, not str` – dissidia Mar 24 '14 at 10:56
  • your error is because padding and num are strings, "num" has to be an integer because only "%03i" will padd with zeros, "%03s" will pad with spaces instead, I have changed the code so it casts as you expect :) – jcr Mar 24 '14 at 11:00
  • I see. I keep thinking that I may need to do something towards the value at `padding` or `num` instead of `pad`. By the way, can you explain more of how `%%0%ii` works? Though you have kindly enough to put a visual explanation for me but I am still unable to grasp the concept.. Perhaps do you have some links that I can refer to? – dissidia Mar 24 '14 at 11:04
  • does it make sense now? – jcr Mar 24 '14 at 11:20
  • Btw, I think you have missed out `int` for the padding in `pad = ("%%0%si" % padding) % int(num)` and thanks for the breakdown. Yup I guess now it kinda makes sense to me but I will still need some digesting to do :) – dissidia Mar 25 '14 at 02:33