fbpx

Python vs Java: How I Tried Python For the First Time

Evgeniy Ekgardt

Evgeniy Ekgardt

Mobile Developer

#Insights

6 Oct 2016

Reading time:

6 Oct 2016

I once read somewhere: “First, you learn a programming language to solve a certain problem, then you realize there’s a language better suited for solving this problem, so you learn it as well, but 10 years later you write your own programming language specifically for this problem”.

Perhaps time has distorted this quote in my mind and I can’t remember the exact words, so I paraphrased. Point is, we, programmers, shouldn’t stand in place and code in the same language our whole lives. Instead, we should constantly experiment and learn new skills. So, I’d like to share my experience how I used Python instead of Java for solving a particular problem and how it all turned out.

Python vs Java: What’s the problem

We had a JSON file with descriptions of country codes. The task at hand was to take the names of each country from JSON, make a request on web service (in my case it was yandex.translate) with a request to translate this line into a different language. Then, having returned the result, write it in the same JSON file as a new additional field. Afterward, I had to do the same task, but to write the results into plist file for iOS version.

Python vs Java: The solution I’ve got

Since this task went beyond the project’s Android development part, I decided to try and do this job myself, using a different programming language. In my opinion, writing this amount of code in Java would be too long and wouldn’t be at all interesting. So I chose Python. I heard lots of good things about this programming language but never got around to try it for myself. So, I wrote two short programs: one for Android, one for iOS version of the project. Both were written in the usual notepad and didn’t take a lot of code.

Here’s what I got.

For Android:

import requests 
import json 
with open('codes.json') as data_file: 
 data = json.load(data_file) 
data_file.close() 
for i in data['codes']: 
 payload = {'key': 'trnsl.1.1.20150901T124624Z.e170a12817a8be05.0addea6cf692e5b3f6fac59322a7a9d433ff21e2', 'lang': 'ru-en', 'text': i['title_rus']} 
 r = requests.get('https://translate.yandex.net/api/v1.5/tr.json/translate', params=payload) 
 i['title_eng'] = r.json()['text'][0] 
 print(r.json()['text'][0]) 
for i in data['codes']: 
 payload = {'key': 'trnsl.1.1.20150901T124624Z.e170a12817a8be05.0addea6cf692e5b3f6fac59322a7a9d433ff21e2', 'lang': 'ru-es', 'text': i['title_rus']} 
 r = requests.get('https://translate.yandex.net/api/v1.5/tr.json/translate', params=payload) 
 i['title_esp'] = r.json()['text'][0] 
 print(r.json()['text'][0]) 
with open('full_codes.json', 'w') as json_file: 
 json.dump(data, json_file) 
raw_input()

For iOS:

import requests 
import json 
import plistlib 
with open('codes.json') as data_file: 
 data = json.load(data_file) 
data_file.close() 
f = [] 
for i in data['codes']: 
 #payload = {'key': 'trnsl.1.1.20150901T124624Z.e170a12817a8be05.0addea6cf692e5b3f6fac59322a7a9d433ff21e2', 'lang': 'ru-en', 'text': i['title_rus']} 
 payload = {'key': 'trnsl.1.1.20150901T124624Z.e170a12817a8be05.0addea6cf692e5b3f6fac59322a7a9d433ff21e2', 'lang': 'ru-es', 'text': i['title_rus']} 
 res = requests.get('https://translate.yandex.net/api/v1.5/tr.json/translate', params=payload) 
 d = {'ISO' : i['country_iso'], 'PhoneCode' : i['country_code'], 'Title' : res.json()['text'][0]} 
 f.append(d) 
 print(res.json()['text'][0]) 
with open('ios_codes_es.plist', 'w') as plist_file: 
 plistlib.writePlist(f, plist_file) 
plist_file.close(); 
raw_input()

Python vs Java: Takeaways

I think this experience was a good exercise for stepping out of my comfort zone, going beyond the thinking of object-oriented language, and testing out Python for myself. I have to say I was pleased with the results and even surprised at how well it worked out. Of course, if we consider Python as an alternative to Java for a full-scale project, it will be impossible: all interaction with the UI will still have to be through Java, as is the case with C++ and Ruby. But as it turns out, Python is a great choice for developing certain parts of the project.

To summarize, I can say that it was a good workout for the brain. I liked how quickly Python worked and how short and simple the code looked. Not to mention, this is just another confirmation that you should always choose a programming language that is most suitable for a particular task.

Comments

Filter by

close

TECHNOLOGIES

INDUSTRIES