tag:blogger.com,1999:blog-6692866151143183230.post7870429189301481611..comments2024-03-19T16:22:18.277+05:30Comments on jugad2 - Vasudev Ram on software innovation: tabtospaces, utility to change tabs to spaces in Python filesVasudev Ramhttp://www.blogger.com/profile/13568740634188042591noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-6692866151143183230.post-31418440771878039632015-05-12T01:54:52.499+05:302015-05-12T01:54:52.499+05:30Also, another reason why I didn't add support ...Also, another reason why I didn't add support to the tool to handle an inconsistent number of spaces in different parts of the file, was because I thought that good programmers would not use that. That is, they would only use either a mix of 4 spaces and tabs, or 8 spaces and tabs, exclusively, if they used a mix at all.<br /><br />And finally, the tool was not really meant as a production tool that had the goal of changing tabs to spaces. It was meant more as an example of the technique of using a Python script in a Unix pipeline (using the for loop over sys.stdin), and that's why I mentioned and linked to my earlier post on that topic (about asciiflow.com), from this post.<br /><br />Vasudev Ramhttps://www.blogger.com/profile/13568740634188042591noreply@blogger.comtag:blogger.com,1999:blog-6692866151143183230.post-17429322070804837392015-05-12T01:34:13.553+05:302015-05-12T01:34:13.553+05:30Thanks for your comment.
You're right that us...Thanks for your comment.<br /><br />You're right that using str.replace() may not work, if the user has used an inconsistent number of spaces in different parts of the source .py file. I was aware of the way tabs expand (to the next tab stop, such as 0, 8, 16, etc.), and so did think of that issue when first writing the code for tabtospaces.py, but to keep the code simple and the post short, decided not to handle it then, and to maybe consider adding it in a later post or just mentioning the possibility (as a limitation of the tool) in the current post. However, ended up not mentioning it.<br /><br />BTW, it is str.expandtabs (at least in Python 2), not expandtab. I guess it was a typo.<br /><br />Good to know about reindent - will check it out.<br />Vasudev Ramhttps://www.blogger.com/profile/13568740634188042591noreply@blogger.comtag:blogger.com,1999:blog-6692866151143183230.post-91306909996282495012015-05-11T13:01:12.145+05:302015-05-11T13:01:12.145+05:30Don't use str.replace() for this; this does no...Don't use str.replace() for this; this does not reflect how tabs work. They don't expand to exactly n spaces, they expand to the next multiple of n spaces. So this will break working Python code by changing its indentation.<br /><br />To correctly convert tabs to spaces, use str.expandtab(8) or the reindent.py tool from the Python Hg repo (https://hg.python.org/cpython/file/tip/Tools/scripts/reindent.py).Daniel Popehttps://www.blogger.com/profile/15593617022625859680noreply@blogger.com