Friday, July 22, 2016

Flask Forms

So I missed a post last week. It's not entirely due to Pokemon Go but I can't say it isn't partially to blame. I grew up playing Pokemon, so in exchange for missing a blog post I was able to indulge in a little nostalgia.

I have however been able to learn some awesome things for web development!

I started the course on Django on Treehouse and so far it has been very interesting. Previously I was using MAMP when learning PHP and MySQL. To start the server on my local machine I opened MAMP and clicked start servers which is perfectly fine. Before I go further let me pause and say that I currently took a break from Django and detoured to developing with Flask, which I will discuss in this post, because it seemed like what I would learn in Flask would be a good basis for Django. I'm not sure of the exact differences between Django and Flask yet (meaning the inner working differences, it's obvious what the differences are when writing the code), so I will mostly discuss what I learned in Flask compared to PHP. Back to starting servers, the command is written into the Python code and started with the Terminal (on a Mac) with the command (on my machine) "python3 file_name.py".

I'm sure there are other uses for Flask but so far I've only learned how to write forms. I must say I really like the format. Each form is written as a class with each field in the form being a variable in the class. Each variable/form field is given properties such as the kind of data, i.e. password or string, and other requirements the field must meet, i.e. minimum length, match a regular expression. In turn this class/form is referenced in a function in the main app file, decorated with the url route, and the function can handle the data entered into the form, such as make a query to a database.

I really like this setup. I do appreciate the fact I learned SQL from my one book. Learning the terminology to manually create and edit databases provides a good foundation for understanding how databases work. Without that knowledge I think learning Flask first could have been a crutch that would have made SQL structure more difficult to learn later. When I was learning PHP I had to write all my SQL queries out in the code. Python's Flask makes the queries for me.

What else I like about Python in web development is the syntax. In PHP I had to write much of the HTML inside PHP, which could be confusing especially when using the text editor I was using. At least that is how the book taught. But with Python I can write an HTML page and write Python code into it with {{ }} brackets. It is also nice to write a template and just swap out content with whatever is needed on a particular page.

I never understood what a web app was but now I do. What is displayed on a webpage is controlled by the program so it functions as an app, not just a static page, and can be interacted with more easily.

Currently I am using SQLite for my database in my little web app I'm writing with Treehouse videos. When writing my own I would like to use a better(more competent?) database such as MySQL instead of SQLite although I'm not sure the differences between them. I still have to figure out how to use MySQL with Python, but it can't be that much different right?

25 words or less:
Making a Python app with Flask seems very smooth but having a knowledge of raw SQL queries is invaluable when handling data.

Thursday, July 7, 2016

What It Does != How It Works

I've adopted a new philosophy, at least at the time, when it comes to programming. I will begin this post with the 25 words or less statement: When coding, think about what a function does and when to use it rather than how it works.

Let me explain. I have over analyzed things for as long as I can remember. Sometimes the endless circles of analyzation can drive a person mad. I believe that is how I was when coding. Any function, library, or package, I wondered how it worked. What process was the computer going through to execute what I commanded it to do? I think I meditated too deeply on such things. To write a functioning program and knowing what tools(methods/functions) to use does not require knowing exactly everything going on in its minutest detail.

I will say that kind of deep analyzation has its place. Memory allocation for example is important to how smoothly a computer can execute a program and knowing how a function works could probably aid in making the most of memory. (I know memory allocation isn't that deep of a concept, but when I was doing a little C++ the way it was explained in the lesson made it seem difficult) And if you were to write a framework or package I'm sure knowing more of how computers work would be beneficial. Certainly computer science degrees and courses can make one a better programmer. It has to right? I've heard podcasts where some with CS degrees said programming is a different animal, but I can't imagine CS knowledge not being incredibly useful to programing.

I came to this conclusion as I finished the Python track from Treehouse. Some of the concepts in the functional Python course could have meant endless analyzation as to what was going on. Then it clicked that when I'm writing code, knowing how the method/function is coming up with the data behind the scenes is likely irrelevant. I only need to know what it does and how to use it.

Take the map() function for example. I need to know what parameters pass into it and what kind of data will be returned. The function call is this: map(function, iterable). I know that it will take the iterable through the function and return an altered list of that data. I don't need to know how map() does this, only what parameters to put into it and having it return the data I need passed through the function. Especially since I would have likely written the function to alter the data.

Again, knowing what exactly is going on has its place. However if you are overly concerned about how certain functions and packages work you may never progress, and thus never write a program in a reasonable amount of time. That is the current mindset I will be proceeding with right now.

For future reference:
altered_list = map(function, iterable)
true_item_list = filter(function, iterable)
cumulative_data = reduce(function, iterable)
returned_data = g = lambda x: x**2

The 'function' in each of these functions can be replaced with a lambda.

Also of note, it would be nice if the word 'iterable' was recognized by my computer so I could type it without it auto correcting to 'utterable'.