Why Javer should know some Python?

I am javer, during worktime I am using Java syntax with all Java micro and macroworld. But for some reason I started to learn that language. Reason? There are two people around me who use it (work/unofficially) and to coopearate with them I had to learn it.
While making some little Python project I encountered feelings like that: there are some things I love in Python comparing to Java, some I miss

Why I miss Java writing Python

Static typing

As dev I learned how to statically type. I was static in Delphi, I am static in Java. I prefer to be static. I feel safer then.

Virtual methods

Writing piece of code that should be done according to OOP rules and design patterns may not be easy with Python. You will not have a chance to write code that is virtual function-like. Strategy method can be hard.

Why do I love in Python

Lack of static typing

Yes, the sam reason why I prefer Java. But think about it: you just write. Your variable is exactly that what is. You easily bring to reality all your ideas. You just type list=[] and that is list! You type a = {'a':1} and you have a map (in Pythonish: ‚dict’). Life is easier but don’t forget about consequences!

Ease with daily tasks

For tasks like json, file IO, network and other issue Python is just less verbose. Any examples? Here we go.

Json made simple

No more thinking how to materialize your ideas as file. You may easy save dict/object as json or save it directly as file

[code lang=”python”]
with open(output, 'w', encoding='utf-8') as f :
json.dump(list_of_objects, f, ensure_ascii=False, indent=4)
f.close()
[/code]

File is something you open

Nothing easier more than read the file. What do you need to read the file? Just open it

[code lang=”python”]
f = open(path, 'a')
f.write("string")
f.close()
[/code]

While opening just type the mode (append/read/write) and act!

Network

No more thinking about immortal IOExceptions, networking is way of requesting the space, like that

[code lang=”python”]
page = requests.get(self.url)
page_tree = html.fromstring(page.content)
element_nodes = page_tree.cssselect(self.element_parent)
[/code]

Above you just GET the url, then take content as text, after that converting to DOM elements, easily traversable. Poetry.

No superheros

I am not trying to say that Python rules. But what I want to remember is fact, that knowing one language you are tight, wider perspective is much more attractive. There are no super-languages, just take what is good and use it in your project. Before that take some time to learn at least foundations of languages available.

why TDD is easy with Node app

motivation

I am not a node dev. I am not. But I like to do things using easy tools. I am lazy – in the same way as all of you are lazy

TDD?

Using experience of my colleagues I have come to the conclusion that TDD is nice. No more checking by yourself does the app runs, no more manual smoke test performed. I want to run tests and be sure that positive outcome of them says : app is running OK

runner : Node

I have been using following „language-frameworks” for past ages: Delphi and Java. When using some lanugage you are tied to possibilities of some framework. Every interpreted language need some interpreter. For java it is java for python it is python for Node it is .. node.
I have decided to use it because Node makes writing web-server very easy. First of all get it from web and try running any Node hello

dependency management : npm

Every framework has to have any dependency management tool. I can’t forget how bad it was in Delphi, how many days you had to spend to run app copied to pendrive from your colleague laptop. Npm has is just installing everything you need. You will need npm install to install all dependencies laying in package.json, npm test to run tests and npm start to start your node app.

webserver : express-js

How to make webserver easily? In your node app just write this in your js code

[code lang=”javascript”]
app.get('/', function (req, res) {
res.send('Hello World!');
});
[/code]

Context please check it here. Basically now you have running web-server that serves get at root. You also easily have sent text response.

test

runner – npm

First of all to acomplish test you have to have a runner for it. In npm config package.json you may just define your test runner. I have chosen mocha as my runner and I will not argue that is the best choice. Any runner may be here.

test description – mocha way

Mocha allows for nice test description. Using describe it pair you may do anything you want. One step for success is to make your test descriptive, in the same time they may become your documentation.

assertions with chai

With chai’s expect you may write your test very effective descriptive way which is also readable, isn’t it?

[code lang=”javascript”]
expect(res.status).to.equal(200);
[/code]

calls by superagent

This is easy way how to make web requests. You have nice availibity of textual response or body of the response message (if json is expected). Just shoot and listen 🙂

[code lang=”javascript”]
superagent
.get(SERVER_URL+'/api/last/2')
.end(function (res) {
expect(res.status).to.equal(200);
expect(res.body).to.have.length(2);
done();
});
[/code]

The shortest story about being dev

Are you developer?  Or maybe you think you are?  I also think that. I will not be extremely original.  I like my job.  I like because I believe I was created in the way I love creating.

Language?  It’s only the tool, I used Delphi cause old guys told this is only proper language to use. I switched to Java because they pay more and there is larger community. I use Python because it makes things easy. I use Javascript because Node rocks and nosql family loves it (CouchDb, Elastic and others)