Python Forum
How to modernise an ancient code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to modernise an ancient code?
#1
In my earlier post, Customised Learning, I tried to run a GUI code. Its an ancient code. Didn't run.
What i further observed is that python platform is very dynamic and fast changing. So some codes become redundant.
So I am now trying to update the code to my installed version. i run:
python3.3 --version
i get:
Output:
Python 3.3.2+
So switched to the page: The Python Standard Library 3.3.2
Then searched the in-built function, raw_input
Found here. InteractiveConsole.raw_input(prompt="")
The CLI code with proper indentation is here:
# tutCLI.py:
# Code:
class adder:
	result = 0

def __init__( self, number1, number2 ):
	self.result = int( number1 ) + int( number2 )

def giveResult( self ):
	return str(self.result)

endIt = False
while ( endIt == False ):
	print('Please input two intergers you wish to add: ')
	number1 = raw_input("Enter the first number: ")
	number2 = raw_input("Enter the second number: ")
	try:
		thistime = adder( number1, number2 )
	except	ValueError:
		print ("Sorry, one of your values was not a valid integer.")
		continue
		print ("Your result is: " + thistime.giveResult())
		goagain = raw_input( "Do you want to eXit or go again? ('X' to eXit, anything else to continue): " )
		if ( goagain == "x" or goagain == "X" ):
			endIt = True
i have not learnt to look at specific examples for the classes, like for example, InteractiveConsole.raw_input() I wish to replace the ancient raw_input() here.

Without examples, i am stuck. When i write:
python3.3 tutCLI.py
i get the output:
Output:
Please input two integers you wish to add: Traceback (most recent call last): File "tutCLI.py", line 18, in <module> number1 = raw_input("Enter the first number: ") NameError: name 'raw_input' is not defined
How to look up a modern version of the ancient classes? And then specific examples for that class?
Freedom is impossible to conceive.
Books that help:
Dale Carnegie's How To Win Friends And Influence People and Emilie Post's Etiquette In Society, In Business, In Politics, And At Home
Have a good day :)
Reply
#2
In Python 3 input() replaces raw_input()
Reply
#3
When in doubt, there's a utility that'll look at python2 code, and try to convert it to python3 code: https://docs.python.org/3/library/2to3.html
Reply
#4
(Sep-28-2018, 05:38 PM)j.crater Wrote: In Python 3 input() replaces raw_input()
Please forgive my apparent lack of gratitude. I could easily have said thanks and closed. Because I would have and should, in normal circumstances.

But as a learner my immediate query would be: How did you know this? Where did you look up for these kind of changes? If you say, by experience, In that case, the Python documentation is probably not user-friendly or complete. Then, I could help with adding to the existing documentations or clarifying the already existing documents.

Otherwise, there would be specific locations on particular/specific "URL"s where these notes would be present.

Could you please advise?

And in the end, please pardon my apparent improper etiquette, and accept my thanks.

(Sep-28-2018, 06:19 PM)nilamo Wrote: When in doubt, there's a utility ... ... https://docs.python.org/3/library/2to3.html

Thank you, Sir. But I would not like to use these utilities. They hide the uneasy experiences from new learners. With such an empowering forum existing for my active participation, I should use the facility to the maximum possible extent.
Thank you for replying to my post, Sir. I acknowledge your intent to help me.

Next issue:
python3.3 tutCLI.py
Output:
Please input two integers you wish to add: Enter the first number: 1 Enter the second number: 2 Traceback (most recent call last): File "tutCLI.py", line 21, in <module> thistime = adder(number1, number2) TypeError: object() takes no parameters
Question is: Why? Of course, I would look up the reference, and I am doing so at present. Just jotting down my thoughts for my future reference.
Freedom is impossible to conceive.
Books that help:
Dale Carnegie's How To Win Friends And Influence People and Emilie Post's Etiquette In Society, In Business, In Politics, And At Home
Have a good day :)
Reply
#5
Just read all 'What's new' Documents:
https://docs.python.org/release/3.7.0/whatsnew/3.0.html
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Why? TypeError: object() takes no parameters
Freedom is impossible to conceive.
Books that help:
Dale Carnegie's How To Win Friends And Influence People and Emilie Post's Etiquette In Society, In Business, In Politics, And At Home
Have a good day :)
Reply
#7
(Sep-29-2018, 12:49 PM)bkpsusmitaa Wrote: Why? TypeError: object() takes no parameters
Your class + indentation is wrong.
When you make class the normal way is to create a instance(calc).
Then call method from class object in case result.
class Adder:
    def __init__(self, number1, number2):
        self.number1 = number1
        self.number2 = number2

    def result(self):
        result = int(self.number1) + int(self.number2)
        return result
>>> calc = Adder(4, 5)
>>> calc.result()
9
Could just have been a function.
def adder(number1, number2):
    result = int(number1) + int(number2)
    return result
>>> adder(7, 5)
12
Quote:i get:
Output:
Python 3.3.2+
3.3 is old now,you should use 3.6 or 3.7.
Ubuntu 18 and Mint 19 is Python 3.6.5 default Python 3 version.
Python 3.6/3.7 and pip installation under Windows
Reply
#8
(Sep-29-2018, 03:31 AM)bkpsusmitaa Wrote: But as a learner my immediate query would be: How did you know this? Where did you look up for these kind of changes? If you say, by experience, In that case, the Python documentation is probably not user-friendly or complete. Then, I could help with adding to the existing documentations or clarifying the already existing documents.
Python 2.x and python 3.x has code breaking changes. Python 3.x fixes a lot of issues which should have done long ago. The change had to occur at some point though. It can be quite confusing if your new. Luckily python2.x is coming to its end of life. But most people use 3.x, and are still a lot of lingering tutorials in 2.x. In case of that it still might be useful to learn the differences of 2 and 3.
Recommended Tutorials:
Reply
#9
(Sep-29-2018, 03:31 AM)bkpsusmitaa Wrote: Please forgive my apparent lack of gratitude. I could easily have said thanks and closed. Because I would have and should, in normal circumstances.

But as a learner my immediate query would be: How did you know this? Where did you look up for these kind of changes? If you say, by experience, In that case, the Python documentation is probably not user-friendly or complete. Then, I could help with adding to the existing documentations or clarifying the already existing documents.

Otherwise, there would be specific locations on particular/specific "URL"s where these notes would be present.

Could you please advise?

And in the end, please pardon my apparent improper etiquette, and accept my thanks.

There is nothing to apologize for!
If you say, by experience, In that case, the Python documentation is probably not user-friendly or complete.
The answer really is I knew this by experience. Or rather, I found it out by an experience, i.e. hitting a wall like you did!
The What's new (from Python 2 to 3) docs is maybe not the nicest looking summary of changes. But I think it is good, and is detailed enough, as official docs should be.
If you find it intimidating or confusing, skim through it for a second time, or third. You will see it is not that "bad" :)
Anyway, the biggest changes most people run into (at least initially) are:
- in Py3 print is no longer a statement, but a function
- raw_input() is replaced by input() in Py3
- in Py3 "/" operator on integers returns a float, whereas "//" does a floor division (as "/" did in Py2)
Reply
#10
First of all, I would like to thank you, Sir, Mr.snippsat. You have made me understand, with codes as evidence, that old codes available on the internet could be garbage. So I would work no longer on such programs on the internet unless they work the first time. Please once again note the program: Tutorial: Using Python/Glade to create a simple GUI application. The third page has a so-declared correct implementation. I will however, not try that now.
Perhaps without your clear codes I wouldn't have understood that the ancient program was mostly garbage. I would have remained skeptical. I am lucky to have you.
metulburr Wrote:... Luckily python2.x is coming to its end of life. But most people use 3.x, and are still a lot of lingering tutorials in 2.x ...
Thank you, Sir, Mr. metulburr.
Like every tool, python appears to have its pros and cons. Like the Atomic Power. Or Dynamite. Or a Passenger Aircraft.
However, such changes from the versions 1 to 2 to 3 shows a great deal of dynamism, which is good for the Program's future, and for the Creator, but not so good for the programmers. One would never overcome the learning curve this way.
To overcome this difficulty for programmers, there should be a backward compatibility clause in the Guidelines of the Development Documents. So that programmers could continue to use the old reserved keywords and methods along with the new ones, while having the option to understand the utility of the new ones in relation to the old ones, and then smoothly switch over to the new ones.
I observed that Python has Class, like C++(which I was lucky not to learn). Class didn't appear in C.
Luckily, I find that the books I procured, even though second hand — thus a little less heavy on the pocket — are not useless mostly. At least, Al Sweigert's ebook, Automate The Boring Stuff ... is not. Nor is Learn Python in One Day and Learn It Well, by jamie Chan, copyrighted on 2014. But Mark Lutz's Learning Python 4th Edition has to be returned to the seller. It was released on 2008.
j.crater Wrote:... If you find it intimidating or confusing, skim through it for a second time, or third. You will see it is not that "bad" :) ...
and your other advices.
Thank you very much, Sir, Mr. J.Crater.
I found a wikibook, which too, unfortunately, addresses Python2.
Now I shall slow down and finish Jamie Chan's book first.
Thanks You, once again. My community is really positive, cooperative and vibrant. Perhaps a vibrant language results in a vibrant community. As soon as a Language matures and becomes stagnant, the community becomes closeted, secretive and autocratic. I am indeed lucky to have chosen to learn Python and have joined this community.

I apologise. I forgot to mention and thank Mr.nilamo. I am doing it now. Thanks, Mr. nilamo.
Freedom is impossible to conceive.
Books that help:
Dale Carnegie's How To Win Friends And Influence People and Emilie Post's Etiquette In Society, In Business, In Politics, And At Home
Have a good day :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Collating ancient greek arbiel 8 4,047 Mar-29-2020, 06:19 PM
Last Post: snippsat
  Can I upload a new version without previously deleting ancient version sylas 6 4,276 Nov-08-2017, 03:26 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020