No tests, no refactoring!

Before proceeding with development I wanted to create some basic structure of packages in the code. The plan was simply to create a package for tests and move all test code there and later on to divide the application code into layers of db access, web application and Tweeter publisher. Unfortunately, this task was far tougher than I expected. Lack of test in the project hit me hard. The only way to see if the whole system works after moving the code from one place to another was to run the whole twitter client against my test account. This shows how crucial tests are for any system, even if it is a hobby project. But thish is not the whole story. After I went to my Twitter account and fetched all the credentials for proverbaro I got following traceback.

OperationalError: (sqlite3.OperationalError) no such table: Proverbs [SQL: u'SELECT "Proverbs".id AS "Proverbs_id", "Proverbs".text AS "Proverbs_text", "Proverbs".shown_times AS "Proverbs_shown_times", "Proverbs".shown_last_time AS "Proverbs_shown_last_time" \nFROM "Proverbs" \nWHERE "Proverbs".shown_times = (SELECT min("Proverbs".shown_times) AS min_1 \nFROM "Proverbs") ORDER BY random()\n LIMIT ? OFFSET ?'] [parameters: (1, 0)]
2017-03-27 22:21:53,425 - proverbaro: Exception while posting

Damn I forgot about the database… I have to recreate it or copy from my production server. That will be most probably the first thing I do in my next session.

One more thing before I finish, since I resurrected proverbaro it gained five followers, which means it got noticed a bit and that’s the goal. 😉

Advertisements

How to split lines in Python

“ Any code of your own that you haven’t looked at for six or more months might as well have been written by someone else. ” – Eagleson’s law

As I mentioned in my previous posts I hadn’t worked on proverbaro for a year.  During this time I began to work as a professional Python developer and right now I know that my code practices used to be at least sub-optimal.  Browsing the files showed that I was not aware of how the modules should be organised into packages and the code itself is supposed to be formatted.  In this post I’d like to focus solely on formatting.

Everyone knows that when you write Python code you need to apply the PEP8 convention.  But unfortunately, code can be written in a way that fulfils the standard but is still unreadable. Especially  line breaking is a bit tricky.   There is a great presentation of my former colleague – Ignacy Sokołowski, where he explains how to properly structure your Python code.  In this post I’d like to focus on proper line breaking. The rule is trivial,  break the line right after an opening bracket. Here’s an example.

Instead of this:

publisher = proverbaro.TwitterPublisher(consumer_key, consumer_secret,
                                        access_token, access_token_key,
                                        hashtag)

Write this:

publisher = proverbaro.TwitterPublisher(
    consumer_key, consumer_secret,
    access_token, access_token_key,
    hashtag)

Following this rule  will ensure 4 spaces indentation and will not force you to reformat your code whenever you change the name of your function and/or variable. I applied this principle to all modules on provebaro and you can check the result in my last commit. Next time I’ll make an attempt to structure modules of proverbaro in a meaningful way. See you next time and if you’d like to go deeper into the area of code formatting check Ignacy’s presentation, here is the link: https://github.com/ignacysokolowski/readability-counts-talk

Resurrection

Before digging into the code I’d like to resurrect first the Twitter client.  Easier said than done.  Firstly, I needed to find my production server in the pile of cardboard boxes that still hadn’t been unpacked after we’d moved in in January.  Actually it was not that difficult as I thought in the beginning- after a quarter I was ready to proceed.

IMG_0347
It’s so cool isn’t it?

After connecting to our TV set everything the system started as expected and I was ready for the toughest part – connecting via remote desktop.  Here, the biggest problem for me is always to recall the name of the program I’m using. A quick look in the browser and  VNC ringed a bell. I installed the client on my laptop and voila- I saw the desktop of my RaspberryPI.

Now it was the time to run provebaro. In the main folder of the project I found a file named run_proverbaro.  I executed it and here is what I saw.

proverbaro_run_1

I seems it hasn’t been ran for a while and why only one proverb?  A quick look into the script and I knew that, it does only one thing – runs the proverbaro_main module with two secret keys Thankfully I found my production configuration.  This explains, why this file is not present in my repository. 😉 Good, now it was time to check, what was inside proverbaro_main.  Good Lord, I wrote a quite nice argparse but I did not know how to break lines in Python code.  Oh, I almost forgot, the posted proverb was – Rapide iras vortoj, sed ne rapide la faroj. This can be translated into English as ‘Words go fast, but actions don’t go fast.’ – I need to remember that.  But coming back to proverbaro_main, there is a limit option and in run_proverbaro it was set to 1. I didn’t have a clue, but I must have been experimenting, because the delay between each proverb was set to 1 second.  I removed the limit and set the delay to 6 hours which is 21600 seconds and this is what I saw.

proverbaro_run_2

It seemed to work, I checked the Twitter and… there was nothing there. Actually there was another file called proverbaro_prod that was responsible for posting to the production account. So I ran the command again and  hooray proverbaro is alive! There are still things that need to be improved like deployment and connection but my project is on back on track. Next time I’ll look into the code and this part might be bit more terryfying as I am not sure what to expect.

See you next time!

Hello ‘Get Noticed’

The first post in the competition should be a brief introduction to the project and it’s history.  Everything started nine years age.  Right after my graduation I began to work as a Java developer but after six years in three different companies I became tired with the language.  I wanted to try a new technology, something open-source and syntactically succinct.  Python with it’s dynamic nature and vibrant community seemed like a good choice.  The problem was, that I hardly had experience with the language.  I wrote a few projects for example pynigma which was a simulator of the enigma machine.  But none of them were web based, which was something potential employers were looking for.  Around that time I got also interested in Esperanto and tried to learn it myself.  So I thought, that I could achieve three goals at the same time enhance my Python portfolio, learn Esperanto a bit and last but not least I wanted to contribute to the Esperanto community.  So the idea of proverbaro was born.

The word ‘proverbaro’ in English means ‘a collection of proverbs’.  I wanted it to consist of two parts:
– a twitter client that periodically posts a proverb,
– a web application were users could browse proverbs ordered by their publication date and translate words they didn’t  understand.

After defining the scope of the project I could begin the implementation, which was actually a nice adventure. I got to learn a bit of Flask and SQLAlchemy. Everything was going smoothly, I first implemented the twitter client and used a RaspberryPi computer as my production server. Later I started the implementation of the web application.  Unfortunately, due to personal reasons, I ceased the development some time around November 2015 and the twitter client remained the only fully functional part of the system.  The server was running until March last year, when a I had to shut it down because I was traveling somewhere and then I forgot to start it up again. That was almost the end of the project.  I thaught, that I would never return to proverbaro.  Luckily the motivation came up with the ‘Get Noticed’ competition and here I am. 🙂

Finally I’d like to set the goals for the next three months for the development of provebaro. Firstly I’d like to have it up and running again. Secondly it would be nice to finish the web part and add some new cool features like post a proverb to fb and so on. Last but not least, I’d like to become a better Python developer and check if writing a blog is something for me.

Thanks and see you soon!