Saturday, October 8, 2016

It's been too long

I don't feel I should post unless I have something worthwhile to write about. Something that I need to document for future reference or that will help someone else. Unfortunately being busy with some necessary things, and admittedly a little lazy unfortunately, I have not been coding the way I want and should. That must change as soon as possible. I hope this post will spark in me the determination not to give up and to keep learning!

While finishing my minimum viable product(MVP) I had to learn a few things. Ways to fine tune the functionality.

Problem 1

When entering certain text into a textfield some characters can create problems, especially an apostrophe. When making SQL queries an apostrophe is read as a quotation mark. Since this is a post mostly concerning Python and not SQL I won't go into too much detail as to why that causes problems. I fully expected this to happen so I wasn't disappointed when the error occurred. I just had to find a way to fix it. I should note I tried placing backslashes where I thought it would work but that was futile.

Solution: re.escape

It was not difficult to find re.escape(string). This will escape non-alphanumeric characters. So when making the query just surround what is entered in the text field with re.escape and it will be entered into the row.

I'm sure making SQL queries with functions instead of what is essentially straight SQL would have a way of doing it, but for now I'll just write the SQL.

Problem 2

I think I mentioned in a previous post about using jQuery-ui to create a date picker on a date field. That worked just fine until I tried to use Chrome which has it's own date picker on html elements that have a certain type. So it was a catch 22: if I don't use the jQuery date picker it will work only with Chrome and if I do use the jQuery date picker it will work everywhere but Chrome.

Solution: $( "#element" ).attr("type", "text")

Again with a Google search I found that when Chrome finds a date element in a form it will use its own built in date picker. So changing the element's type to text will fool Chrome into not reading it as needing the date picker. 

Originally I tried making the date field a string field in forms.py so Flask would generate that field as a text type but that didn't work. It always submitted a string rather than a datetime so it did not match the data type of the column in the database table.

I found that altering the html with jQuery did work. Changing the element's attribute of type to be text instead of date apparently only changes the html and does not affect the type of data being sent to the database. At the same time Chrome does not read it as being a date type and thus will not display its date picker. Then the jQuery date picker is free to be used in all browsers. (It'd be nice if woman saw me as a date type... I'm sure that joke is not overused.)

A few extra notes
Those were the two biggest issues I recently faced. I've been learning that when an error happens the stack trace is invaluable. Knowing how to read it is very helpful. Although I don't know all the internal modules that also throw errors I've usually been able to find what line of my code is the genesis of the error.

Lately I have been listening to Talk Python To Me, which has been very helpful. Recently they had an  interesting interview with the creater of CheckiO. It is a site with coding challenges. I've tried some and I like it. I think it will help getting familiar with some of the tools Python has to offer and how to use them.

App Demo!


I've also uploaded to YouTube a demonstration of using my web app. It is longer than I thought it would be but it's my first time trying this kind of thing. The video shows the basic functionality and benefits of it. It is merely an MVP and I want to add more to it soon. Here is a link to my App Demo.

25 words or less: Errors in your code is a great way to learn innumerable things. Welcome them and be patient when fixing them.

A side point:
I think I'm going to stop saying when I search Google to learn how to solve my errors. It should just be understood.