On Thu, May 26, 2011 at 9:17 PM, Masklinn <masklinn@masklinn.net> wrote:
Considering the original use case, which seems to be mostly about being able to use .format, would it make more sense to be able to create "byte patterns", with formats similar to those of str.format but not identical (e.g. better control on layout would be nice, something similar to Erlang's bit syntax for putting binaries together).
This would be useful to put together byte sequences from existing values to e.g. output binary formats.
We already have an entire module dedicated to the task of handling binary formats: http://docs.python.org/py3k/library/struct "format(n, '6d').encode('ascii')" is the right way to get the string representation of a number as ASCII bytes. However, the programmer needs to be aware that concatenating those bytes with an encoding that is not ASCII compatible (such as UTF-16, UTF-32, or many of the Asian encodings) will result in a sequence of unusable garbage. It is far, far safer to transform everything into the text domain, work with it there, then encode back when the manipulation is complete. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia