Categories
Personal Technology and Internet

Moving to Python 3

A week ago the support period for the last version of Python 2 was extended by 5 years (from 2015 to 2020) and this event just ignited once again the discussion about the fragmentation in the python ecosystem. Some have the opinion that version 2 should have a 2.8 release while others keep saying that the future is python 3 and this event will delay even more the adoption of the new version.

The truth is, version 3 already has almost 5 and half years (released in December 2008) and it seem it didn’t have yet conquered enough number of users to dethrone the old version. While the first iterations of the new major version met many critics (3.0 until 3.2),  the last 2 releases seems to have conquered very good reviews and after many years the majority of the most important libraries and modules seems to have support for python 3 (can be checked here ).

This way and after some thought, i decided that it is time (maybe a little late) to change my default development version for new projects to python3, since it really is the future of this programming language and it is time to move on. There will be exceptions of course, like old projects that need to be maintained,  new ones where the requirements do not allow the new version or where the needed packages do not yet support python 3.

So lets check what this “new” version has to offer.

Categories
Technology and Internet

Django Resources

As I said in earlier posts in this blog, when i build websites or webapps where there are no technology impositions, i usually choose to do it in Python and in most of the cases, that’s the equivalent to say i choose to do it in Django.

Over the last year, since i started using Bundlr,  I’ve been aggregating some resources like blog entries, tutorials and videos that i found useful and that could become handy in the future.

Today I’m sharing the collection here, since it might helpful to someone else. I hope you like it and if you know more references that should be included in the list, please share it in the comments or send me an email.

The list can be found here.

Edit July 2016: Since I removed my account, the list is not longer available on Bundlr. Check recent posts, it will be published again soon.

Categories
Old Posts

First experience with MOOC

The year of 2012 for the Internet was definitely the year of the “massive open online courses” with some startups of online education stepping up to the big stage (Coursera, Udacity, etc) and some well know names coming up with their own initiatives (MIT, Harvard and Berkeley at Edx). So in the beginning of this year there were many opportunities to learn something new or update your knowledge with college level quality, where the only prerequisite is your motivation.

So i decide to give it a try, in January I picked up a topic that i wanted to learn/improve and signed up for it. The course wasn’t taken in any of that major sites that i previously mentioned but the system was based on Edx. At the end of the month, i started the 10gen‘s 7 week course on “MongoDB for Developers” (Given in Python) and followed the weekly classes flawlessly till the final exam in the middle of March.

In the next few paragraphs i will describe my experience based on some notes that i took during that period, basically, i will tell what i liked and what i think that should be improved.

On the first week in a course for developers, the rythm was kinda slow with the instructors wasting too much time with the basics of python and how to install some libraries. At first i thought everyone would think the same, but in the discussions i noticed that many of the fellow students didn’t even knew how to install python on their machines. Even though it was a nice thing to do, in my opinion for this kind of course previous python experience should be a prerequisite.

In the next few weeks things started to get interesting when we focused more on mongodb and talked about its operations, design, performance, the aggregation framework, etc. Every week a new batch of 3 to 10 minute videos (with few exceptions), covering each one a new concept or use case about the week’s topic was released, plus some questions to make sure you understood the what was being explained in each video. Personally i like this approach, i didn’t move to the next video until i completely understood the previous one, and if i had doubts it was as simple as watch the video again and use the discussions in the case the doubts persists. The responses to your questions were posted generally pretty fast, many times by the instructor but most of the times by fellow students.

To complete the week you had to complete some kind of homework that weighed 50% of your final grade. Some people complained that it was relatively easy to complete these tasks, but in my opinion the purpose of this homework is to certify that you, at the end of each week, understood the key concepts lectured and not to test the capacity and expertise of the participants.

In the last week of the course, you only had to complete the exam, the content posted by the instructor were optional and consisted in interviews with professionals talking about mongodb implementations in production right now on codecademy and foursquare.

One improvement that i would like to see in future courses is a discussion box per video where you didn’t have to leave the video page to ask questions or to answer the ones you know.

In conclusion, i really liked the experience and i will certainly put my new “mongodb” skills in action on a future project. Right now I’m already aiming to a new course for the summer (when my weekly schedule is lighter). If you already took one of these online courses, I would like to listen what you have to say about them. Feel free to use the comments.

Categories
Old Posts

Recovering your bookmarks

Some time ago, while cleaning stuff in my computer, I decided to switch my browser to Opera and delete the version of Firefox that I was using at the time. While doing that and removing all the Firefox folders that are left behind, I accidentally erased all my bookmarks and I didn’t had them synced with some on-line service. Well that wasn’t good, I had references stored there that I wanted to keep.

When trying to recover the file ‘places.sqlite’ I found an bookmark backup generated by Firefox. When I opened the file I found that it was a mess, basically it was bunch of big JSON objects stored in one line containing lots of garbage (I only needed the urls).

I kept that file until today, when I finally decided that I would put those bookmarks again in my browser. As Opera doesn’t import this kind of files, I made a little python script that extracts the names and urls of the backup and generates a single file that opera can import, while keeping the folder structure.

Well, it worked, so I tought it might be usefull to someone else and pushed it to github. If any of you ever have the same problem give it a shoot and use this “quick fix”. You can find it here with some instructions on how to use it. If you find any problem, use the comments and github issues.

Categories
Old Posts

Generators, Decorators and Metaclasses

For some time now, I’ve been trying to improve my python skills and learn a little bit more deeply the how the language works. The objective of this quest is to write more efficient and structured code, because it seems to me that I’m not using the full potential of this programing language.

Yesterday i found at stackoverflow 3 comments from the same person answering 3 different questions, one about the yield statement in python, other about decorators and another explaining metaclasses. The posts are long but the explanation very good and with several examples, I thought that they were so good that I must share them with those who are trying to learn more advanced python.So here they are, in chronological order:

Categories
Old Posts

Simple JSON Parser for Python

Some time ago i started to follow a Blog that weekly proposes some programming exercices and i solved one of their problems (an old one, from 2009) . So today i’m posting here my solution for the problem of this week. Basically they ask us to write a JSON parser in our favorite computer language, so i chose “Python” and tried to complete the task.

For those who don’t know what JSON is:

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

My implementation is quite simple and it can contain some bugs (and is not optimized), so if you discover any error just leave a reply (we are always learning). Bellow is my code and a link to Github where you can also comment the code. In the following weeks i’ll try to solve more of their problems.

class json_parser:

    def __init__(self, string):
        self.json_data = self.__remove_blanks(string)
        self.pointer = 0

    def __remove_blanks(self, string):
        new_list = []
        inside_string = False
        for i in list(string):
            if inside_string or i != ' ':
                new_list.append(i)
            if i == '"':
                inside_string = not inside_string

        return "".join(n for n in new_list)

    def __parse_obj(self):
        new_dic = {}
        self.pointer += 1
        while self.json_data[self.pointer] != '}':
            if self.json_data[self.pointer] == '"':
                key = self.__parse_string()
            else:
                raise Exception  # The only possible type of value for a key is String

            if self.json_data[self.pointer] == ':':
                self.pointer += 1
            else:
                raise Exception  # invalid object

            value = self.__parse_value()
            if value == -1:
                return -1

            new_dic[key] = value
            if self.json_data[self.pointer] == ',':
                self.pointer += 1

        self.pointer += 1
        return new_dic

    def __parse_array(self):
        new_array = []
        self.pointer += 1
        while self.json_data[self.pointer] != ']':
            value = self.__parse_value()
            if value == -1:
                return -1
            else:
                new_array.append(value)

            if self.json_data[self.pointer] == ',':
                self.pointer += 1
        self.pointer += 1
        return new_array

    def __parse_string(self):
        self.pointer += 1
        start = self.pointer
        while self.json_data[self.pointer] != '"':
            self.pointer += 1
            if self.pointer == len(self.json_data):
                raise Exception  # the string isn't closed
        self.pointer += 1
        return self.json_data[start:self.pointer - 1]

    def __parse_other(self):
        if self.json_data[self.pointer:self.pointer + 4] == 'true':
            self.pointer += 4
            return True

        if self.json_data[self.pointer:self.pointer + 4] == 'null':
            self.pointer += 4
            return None

        if self.json_data[self.pointer:self.pointer + 5] == 'false':
            self.pointer += 5
            return False

        start = self.pointer
        while (self.json_data[self.pointer].isdigit()) or (self.json_data[self.pointer] in (['-', '.', 'e', 'E'])):
            self.pointer += 1

        if '.' in self.json_data[start:self.pointer]:
            return float(self.json_data[start:self.pointer])
        else:
            return int(self.json_data[start:self.pointer])

    def __parse_value(self):
        try:
            if self.json_data[self.pointer] == '{':
                new_value = self.__parse_obj()
            elif self.json_data[self.pointer] == '[':
                new_value = self.__parse_array()
            elif self.json_data[self.pointer] == '"':
                new_value = self.__parse_string()
            else:
                new_value = self.__parse_other()
        except Exception:
                print 'Error:: Invalid Data Format, unknown character at position', self.pointer
                return -1
        return new_value

    def parse(self):
        if self.json_data[self.pointer] == '{' or self.json_data[self.pointer] == '[':
            final_object = self.__parse_value()
        else:
            print 'Error:: Invalid inicial Data Format'
            final_object = None

        return final_object

[EDIT: The previous code has several issues, so please do not use it. Python has many great packages to handle JSON documents the right way, like simplejson.]

Categories
Old Posts

E-mails: hours to seconds

I remember those old times when i didn’t know nothing about computer programming and i was a member of the organization of the National Meeting of students of physiotherapy and after a conference about physiotherapy and spine related problems. At that time when we had to send some hundreds or even thousands of emails for other students and professional on that field, we spent hours or even days.

One of the problems was that our email provider (Gmail) didn’t allow to send a single email for more than ‘x’ people or send more than ‘y’ emails a day,if you had exceeded the limit they would block your account for the next 24 hours. For that problem the solution is don’t use Gmail for what it isn’t intended for. There are plenty other services to do that.

The other problem was the use of large files, full of emails adresses that we had to split through many emails. For this one, here is a possible solution, a script that connects to a SMTP server and sends the message to every contact in the “.txt” file.

import smtplib
from email.MIMEText import MIMEText

def send_to_all(server,login,passwd,subject,msg, cfile):
	contacts=open(cfile, 'r')
	email=MIMEText(msg)
	email['Subject']=subject
	email['From']=login
	
	smtp=smtplib.SMTP(server, 465) #change for 587 if doesnt work
	smtp.ehlo()
	smtp.starttls() #comment this line if the server doesnt support TLS
	smtp.login(login,passwd)
	
	for item in contacts:
		smtp.sendmail(login,item,email.as_string())
		
	smtp.close()

def main():
	#Asks the info to the user
	print "Server: "
	server=raw_input()
	print "Login: "
	login=raw_input()
	print "Password: "
	passwd=raw_input()
	print "Subject: "
	subject=raw_input()
	print "Message: "
	msg=raw_input()
	print "Contacts file: "
	cfile=raw_input()
	#sends the message to all contacts in the file
	send_to_all(server, login, passwd, subject, msg, cfile)
	return 0

if __name__ == '__main__':
	main()

With this script you can only send simple text messages, but it might be usefull for someone anyway.