indentation preservation/restoration

Steven D'Aprano steve at REMOVETHIScyber.com.au
Mon Jan 2 19:15:18 EST 2006


On Mon, 02 Jan 2006 14:19:15 -0800, smichr wrote:

> I have (inadvertently) wiped out the functionality of my personal
> python snippets by eliminating leading space. I have also just visited
> http://www.python.org/tim_one/000419.html and saw a piece of code with
> the indentation gone. Python code is fragile in this regard. One
> solution that occurs to me is that a "indentation code" could be
> appended to a script which could be used to rebuild the script if
> necessary. e.g. if we let letters represent the number of spaces before
> a line and numbers to indicate how many lines have that spacing, then
> A2E2A1 could represent the leading space on the code,

What a great idea! I had a similar problem, your scheme could be adapted
to solve that too. I accidentally deleted all the numeric literals from my
Python code, and Python is fragile in this regard. So we could have a
"number code" appended to a script which could be used to rebuild the
script if necessary: e.g. a code like "E,D,CA" means the fourth token of
the fifth line is 31.

With a little bit of work, this could be expanded to add redundancy for
not just indentation and numeric literals, but also string literals,
keywords, operators, and anything else. Using compression and
error-correcting codes should mean that your Python scripts will then be
safe from absolutely anything short of deleting the entire file -- and for
that, you have backups.



-- 
Steven.




More information about the Python-list mailing list