[Tutor] String switch
Dave Angel
d at davea.name
Sat Oct 8 22:18:57 CEST 2011
On 10/08/2011 03:03 PM, Christopher King wrote:
> Okay, here's what I what to do
> *--Target.py--*
> print "Hello World"
> *--Target.py Output--*
> *Hello World*
> *--Main.py--*
> target=open("target.py", "r")
> old=target.read()
> target.close()
> target=open("target.py", "w")
> target.write('''
> Str.__repr__ = Str.__repr__.swapcase()'''+old)
> target.close()
> *--Modified Target.py--*
> Str.__repr__ = Str.__repr__.swapcase()
> print "Hello World"
> *--Modified Target.py Output--*
> *hELLO wORLD*
>
> The only problem is I don't think that "Str.__repr__ =
> Str.__repr__.swapcase()" works. What should I use in its place to make the
> above a reality.
>
That certainly won't work in Python 2.x, since Str isn't a defined
symbol. In any case, I can't see what you intended it to do, since
__repr__ isn't called anywhere in that code.
What are your constraints? Which Python version? Are the only
allowable changes a prefix to the code in the input file?
What strings do you want modified, only those inside literals, or those
that are generated by the program? For example, if the program says
x = raw_input("What's your name"); print x
do you want that prompt string swapcased as well? What about the name
the person types in?
How about a program like
z = 3.14
print z.hex()
Do you want the output changed from 0x1.91eb851eb851fp+1
to 0X1.91EB851EB851FP+1 ?
Do you only want things swapped that are printed with "print", or do you
want anything that goes to stdout? What about things that are written
to file? What about things that are written to a file, but that file
happens to be stdout ? Or vice versa?
There are lots of other possible questions, but perhaps that's enough to
convince you that you haven't begun to specify what you want.
--
DaveA
More information about the Tutor
mailing list