Showing posts from 2018

Spaghetti code

Once upon a time, many years ago, a colleague came to me and asked me what I knew about a code-base I had never heard of. "Nothing", I replied. He told me it was urgent to get the code up and running again as it had broken down in February or March. It was July, I think.

I called upon another colleague who didn't work at the department anymore and he told me that the code was stored at an old desktop PC which had a post-it note attached: "Do not delete". He also told me that the programmers, who had left the company already, were incredible professionals and that he was impressed by the job they had done.

Long story short, we recovered the code from the PC and stored it in the cloud.

Then, I had the "privilege" to look at the code. It was not pretty.

If you know anything about Model-View-Controller, you know that it is a three tier philosophy meant to simplify code development. All security work is done by the Controller, all business logic is perform…

Setting up a HTTPS server in Python (WSGI)

The situation Setting up a HTTP server in Python is quite easy. You overload the handlers.CGIHandler class, and Bob's your uncle... You have to overload the run function, and that's about it.

def run(self, host='localhost', port=8000):# Initialize the server deamon# HTTPself._httpd = simple_server.make_server(host, port, self.__call__) print("Serving on port {0}...".format(port)) # Start the serverself._httpd.serve_forever()
Nothing to it, right?

So, you'd expect that setting up a HTTPS server would also be a piece of cake... but it isn't that straightforward. Luckily, for you, I have spent some time to make this easy as frig.

The idea is to make a SSL socket, and to do that, you have to use the wrap_socket command... but where?

Look at this line of code from the run function

self._httpd = simple_server.make_server(host, port, self.__call__)
When you open the simple_server package, you see that there's an optional server_class argument.


Optimizing date calculations

I wrote a naive little algorithm to calculate the end date of a scheduled event.
There are the following restraints:

Total number of hoursNumber of hours per dayWorkdays per weekSkip holidays
So, the pseudo-code looks like this:
While there are hours
    If it's a workday
         diminish the hour counter by the amount of hours per day
    Go to the next day
This works, but it's slow. Let's do this moar better!

New pseudo-code:
Hours / Hours per day                   ===> Days
Days / Workdays per week            ===> Weeks
(7 - workdays per week) * Weeks ===> More Days  Add the number of days to the initial date to get the end date.  Calculate the number of holidays in the range ===> Days  While there are days
    If it's a workday
         diminish the day counter by one
    Move the end date on day forward. This improved algorithm is 150 times faster than the original for 630 hours, 5 hours per day (or 126 days)