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.

Sunday, September 18, 2016

Date field struggles

Things have been busy and I have not been able to be at my computer lately to work on programming. Looking at my calendar though it feels longer than it actually is as it was only last week I made a fulfilling breakthrough. Although I can't say I still completely understand the date field I feel comfortable using it.

The first trouble I had is that in the date field from Flask forms. I knew how to display that part of the form but when I would enter a date, manually, into the field, upon submission the page would display "Not a valid date" above the field. I actually just commented that code out for a few weeks and worked on other areas of the site. That proved very successful as I basically completed the app in that time and only had the elusive date field to tackle. By that time I was already confident in the app and the date shouldn't be too difficult to handle right?

As it stood the form was not recognizing the data in the field as a valid date. I thought maybe having a date picker would somehow enter correct information. So I searched and found jQuery ui. That would allow me to attach a date picker to an element so that when clicked it would show a date picker. It worked to an extent in that the page would display a date picker widget when clicking the date field but still said it was not valid data.

Many Google and StackOverflow searches later I came to the conclusion the backend was not parsing the data the way it should have been. When sending the data from the form, using the data in the app must be implemented using:

date.strftime(date_variable, "string format") - string format is for the date, i.e. '%Y-%m-%d'

So when sending the data from the web to the back end the data can be entered into an SQL query where the column is for dates.

So what I learned is the Flask date field must convert the entered string into a unix time object because you must enter the date format as a parameter when creating the date field. That data then must be sent to Python script and entered into the database. However date.strfime must be used so the format can be parsed by SQL as a date object and acceptably entered into the database. For a few tries there was a date entered but it was '0000-00-00'. Obviously not what I wanted.

So I suppose I do have an idea of why things were not functioning as I wanted them to. I was so happy to finally figure out the bane of my last few weeks and be so close to an MVP at last. One more roadblock though.

Right now I am having difficulty with multiple browsers. Most specifically Chrome and Safari (I haven't checked Firefox yet though I suppose I should). The trouble is the date picker widget created with jQuery ui. It functions just fine in Safari and technically works in Chrome. The problem arises in the fact that Chrome has its own date picker built in. Thus I get the error of not valid data when using the date picker. If I don't attach the jQuery date picker to the date field, Chrome's date picker will work fine and the data can be entered. The problem is using Safari then, the date picker cannot be used. Catch 22?

So I have made great strides, in my opinion, in understanding datetimes. But browser compatibility is a new challenge. I suppose that may never be solved even by the best developers.

25 words or less: Understanding how to parse datetimes is an invaluable tool and should not be overlooked. Keep trying, you'll get it.

Also I can't say my lack of posts is not entirely due to a recent obsession with Mad Men, but it certainly had a larger impact on my nonproductivity. Maybe someday I'll deviate some and write a post on my view of the show and understanding the ending.

Monday, September 5, 2016

Dependent Select Box (Part 3)

So last time I thought I was done with the dependent select box, but as with most things in programming, I was mistaken and had to learn more. Recently though I've felt a bit of competence as far as troubleshooting goes.

I tried to make a select box where the choices was an empty list and populate it through JSON data with Javascript. The problem was when the form was submitted it always said it was not a valid choice although the html contained the options I desired. Turns out Flask had to know the choices available and then I could write Javascript to only display which options match the above select box. The select boxes can be populated with Javascript but will only be accepted as valid if they are already available in the Flask app.

I also ran in to a lot of trouble with the error of NoneType not being iterable. I searched and searched what was a none type and couldn't find anything. My lambda functions seemed ok and they were supposed to iterate through a list. I was very frustrated for a long time. Even my stack overflow question was down voted. No-one helped me there either, which is surprising to me because I've gotten good information from other people's questions. But I finally figured out I was looking in the completely wrong place. The None type had to do with the select box, not the actual iterable lambda function I was using to populate the choices in the select box. So although I'm not sure what the function was trying to iterate, I do know how to avoid it and make it work.

I also encountered trouble when this piece of code did not work:
SelectField("Select", choices=[("0", "Choose")] + list((lambda to populate rest of list)), coerce=int)
** The lambda function returning tuples of (int, string) pairs **

I was a little proud of finding this error totally on my own. I can't remember the exact error but the problem was the following. The list from the lambda, and what I wanted, returned an (int, string) pair. However the part that would become the first position of the list was a tuple of ("0", "Choose") which is a string, string pair. Then the coerce=int was trying to do so with the string of "0" and not the int of 0. All I had to do was remove the quotes to make it an int. Admittedly a very simple fix but I feel I've been having trouble with type compatibility lately. Especially trying to take form data with Flask, enter it into a MySQL database and have the form communicate with the database so it can enter information of the correct type into the correct column. I also think my comprehension of how it is all working together has greatly improved working on this app.

I've also learned many other things to make this app work how I want. I was able to integrate Google maps API distance matrix for Python and Javascript to find distance between employees and stores. I suppose that will be for another post. hat was a very fun skill to learn and the reason I made the app is completely dependent on that API functioning. My company uses Bing to calculate distances and travel time. I don't know if they offer an API. I will have to research that if they don't accept using Google with my app.

25 words or less:
Pay attention to why errors are being thrown, they can teach you so much about how code works.

Sunday, August 14, 2016

Dependent Select Box (Part 2)

After spending two weeks learning Javascript, JQuery, and AJAX I at last achieved gained the ability to code a dynamically populated and dependent select box! Let's walk through the process.

I found out, after trying to write the JQuery and such, that I need to learn how to write, get, and parse JSON data. JSON is a complicated, and yet easy at the same time, system to comprehend. What I did was, any time a new item or model in the app was created, Python code would run to write that data to a JSON file. That file could be accessed by the JQuery method $.getJSON.

As I've discussed forms and how to populate the select boxes with Flask in other posts, I will explain the JQuery. It's important to note that Flask will set the select tag with an id (currently I'm not exactly sure where it assigns it from). I only found that by looking at the source code for my site when it was running. It was then easy to assign a variable to that html tag with JQuery. Then the #item_type box could be populated with JQuery that pulled data from the JSON file.

This would be done by looping through the file and appending each option to a string:
'<option value="' + item_type.id + '">' + item_type.name + '</option>'

That string, that ended up being multiple options after the loop, would be added to the html with:
$item_select.html(string of option tags)

Then in a different function would be run when the #item_type box was changed: $item_select.change(function() {}); Inside the change function would run basically the same code to populate the #item_type box option tags. The only differences being it would be for the #item_model box and there would be a conditional block that would add the option only if the model.of_type was the same as the $item_select.val(). Then the string of options would be added to the #item_model box.

Dependent select box achieved!


The only trouble is it would not update in real time. For instance if I would add a new type or model, then go to the page with the select boxes, the new data would not be populated into the box. Through trial and error I found the browser was caching the information and I had to delete the cookies that were being set by (I think) Flask, but the browser could have just been doing that too. After a small Google search I found this little bit of code would set the browser to not cache the cookies:

$.ajaxSetup({
    cache:false
});

Thus ends my journey of the dynamically populated and dependent select box! It seemed like a much higher hill to climb in the beginning but as I'm writing this I wonder how I couldn't figure it all out before.

25 words or less: Better is the end of a matter than its beginning - Solomon

Friday, August 5, 2016

Dependent Select Box (Part 1)

Recently I was asked by a friend who works in the IT department of a medical supply company to make a web app where he could keep track of IT inventory. I accepted and he emailed me the data models and how he wanted the app to work. Thus I got to work on my first real project.

Doing a little research I read a few negative things about PHP so I decided to go with Python as the language to construct the backend for the app. To satisfy the needs however I had to learn a plethora of new skills. That is one reason I started learning the Flask library. I eventually ran into a dilemma.


Dynamically populate a select box


If I try to populate a select box in forms.py file the box will not update concurrently with the database. One would have to stop the app from running and start it again to populate the site with any new information. Thus the choices (for the form's class in forms.py) had to be populated in the app.py file. That is how the choices would be populated dynamically and in real time with any update to the database.


Dependent select box


Then came the real trouble. I would like to dynamically populate data in one select box dependent on a selection from another select box. Ideally there will be three select boxes. The first select box has manufacturers listed. When a manufacturer is selected the second box populates with types of equipment that particular manufacturer makes. When a certain type of product is selected a third box is populated with different models of that type of equipment. For example, from the first select box you could select "HP". That would populate the second select box with types of products HP makes such as "keyboards" or "computers". When you select a type, like keyboard, the different models of keyboard made by HP will populate the third select box. Then with text fields and such the user can update data on that particular model, such as adjust inventory.

This led me into countless Google searches and StackOverflow inquiries. After searching in beast mode for longer than I should have I came to the conclusion I needed to learn how to use some different tools, most notably JQuery and AJAX. To use JQuery I first needed to study up on how to use Javascript. I took the Javascript Basics course on Treehouse. I went through that rather quickly because it was essentially just learning different syntax and how to put Javascript into HTML. Much of the topics and principles covered I had already known from learning Python, PHP, and HTML.

Then some new ideas were presented in the JQuery course. This was a pleasantly significant change and quite mentally stimulating. Learning how to add, remove, and manipulate HTML elements will prove to be very useful. I can already visualize being able to change data in a select field. Possibly by adding an id tag to the form and using JQuery to update the child (or next) element so that it populates data based on another select box. This then led me to the next stage that the earlier Googling seemed to point.


Time for AJAX


I started the AJAX Basics course on Treehouse. I learned a little AJAX and how to 'GET' data from a JSON file. Then how to json.parse() the data to a variable so the data can be accessed by some Javascript code. Not sure of exact terminology but, I would like to know how to make an AJAX request to a Python file or write a Python file that gives information to an AJAX request in a format that can be understood by Javascript. Right now I will just have to learn how to have Python write JSON data with information from a database and access the JSON file from the browser.


To conclude (or continue / reiterate)


This is where I currently am in my progress. Still unable to create a dynamically populated dependent select box, but I feel I am so much closer to that goal! I can somewhat visualize how it can be done, I just have to comprehend how to create a JSON file with data from a database created in Python. Then I can write the AJAX commands to access and parse that data to populate the other select boxes in the form. I think I can do it but I feel it would be beneficial to finish this AJAX Basics course first. Only a little bit left. I can't wait to write the post where I successfully write it!

25 words or less: Everything someone else does with code that seems impossible at first is absolutely possible to learn and write yourself in time. Just keep learning!

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'.

Thursday, June 30, 2016

Python Packages, pip, and Other Perplexing Problems

I finished the course on Treehouse about using timezones in Python. Don't get me wrong, it is a good course. I can't say I like using workspaces (Treehouse's IDE) but I can see certain benefits of using it when starting out. So far I have been using PyCharm while following along with Treehouse and for my current Python coding projects (Hockey Shootout). I have no problem with JetBrains, although I hear Sublime is very good.

That sounds like it should be a discussion about bands...

Anyway, everything was progressing fine until the course said to use the pytz package which was already installed on workspaces. The video started by saying how to install it if not using Workspaces. Problem is the instruction was showing on the screen a single command to type to install it on your local machine. Understandably I went to the Terminal (I use a 2014 MacBook Air with El Capitan) and typed the command. Obviously that didn't work. I Googled "pytz install" and ended up downloading a .egg file from Python's website that was mentioned on another website (likely StackOverflow).

Countless Google searches later about errors I was getting I found myself stymied by this pip install that was so simply referenced in this Treehouse video. Some searching on my machine led me to find why I think this is so difficult. I used to use a Windows machine I bought so I could mess around and try programming. Once I tried some SQL and discovered the queries ran twice as fast on the Mac I quickly transferred and forgot about my Windows laptop (save for loading the original Starcraft and playing that for a bit). I remember programming on my Windows machine last year using a book on Python. The book used a graphics library in its lessons to demonstrate some principles. That was very simple. Download the file and save it in the folder with the other preinstalled Python libraries. From there it can be imported in the code. That is not the case with my Mac. At least not within my current computer understanding.

Python 2.x is preinstalled on my Mac. Some time ago I downloaded Python 3.x and that's what I use with PyCharm. Because I read not to uninstall Python 2.x from my computer I aliased Python 3.x in the Terminal and access it using 'python3'. Everything had been going swimmingly, that is until I tried installing this elusive pytz package. I never heard of a .egg file but I think it's what I need. I tried so many commands in the Terminal to install this package, all to no avail. Things I read on StackOverflow do not seem to work as their suggestions usually do. Commands such as 'python3 -m pip install dir/file'(and so many more) always come back with errors. Apparently I also have an outdated pip. I still have no solution.

I am ok with not understanding this. I don't fully comprehend timezones, and not just when it involves programming but that's a different story. I completed the course and am moving on to the next (regular expressions...). When I need dates and times in programming I will try harder to get it and I think I will be able to use them. I know when I start using Python databases it will surely be necessary to know how to use datetimes.

Thus this post is mostly how I've accepted that there are some things I won't understand. It is my impression that it is ok not to understand something. It reinforces the fact that there are many things I don't know and no-one has a monopoly on knowledge. I will draw on my lack of knowledge to bolster my motivation to continue learning.

25 words or less: When something causes too much frustration learn something else and return later to reexamine the problem with fresh eyes and a fresh mind.

Thursday, June 23, 2016

What Developing Means

This post is written in the context of myself working on coding projects alone (and with the help of things like Stack Overflow and Treehouse). I have never worked on a software or web development team and thus cannot write about how that process would work. I think though that the underlying principles would transfer.

For awhile now I never thought much of certain terminology. To me programming, coding, and developing could all be used synonymously, as well as those words' noun counterparts. The past few days, after working on my hockey shootout game more extensively, I have realized that development (of a program) is different, though not totally independent, from coding and programming.

The definition of develop is "grow or cause to grow and become more mature, advanced, or elaborate." Like people. We develop and grow both physically and mentally eventually optimizing ourselves for specific tasks, just like a computer program does. (Not to get all Matrix-y on you)

That makes perfect sense in relation to what I was doing! Previous projects I created, such as a way to organize parts I ordered for work and a simple bouncing ball program, I think I tried to write the whole program at once. I thought of the program as one giant thing and not many smaller parts that worked together to solve a problem. I believe approaching those tasks in that fashion caused me to not understand development the way I should have. As I worked through the hockey shootout game things changed.

I realized that the overall game was made up of many smaller parts. Various functions that performed certain tasks could be called from a main function that would call them in the correct order and frequency. For example in my game the first thing needed is to ask if the user wants to play so I wrote that first. For a short amount of time all the program did was accept the input if the user wanted to play or not. It then had to verify the user entered a 'y'. Then it had to ask what player you wanted to use. To do that there would have to be players to choose from. Thus I had to make a player class that would initialize different players with specific attributes. And so on and so forth.

Continuing to create how the game would function and thinking of new features showed me the value of Trello. I learned about Trello through Treehouse but I didn't realize fully how to use it until I was working on this latest game. As I thought of new features to add I could record it on my Trello board and I could visualize how the game would develop, or become more advanced. I could then sit down later and write it into the program. Thus I continued to add depth to the code, such as a way to select two players instead of one where player two would be the goalie, a way to increase a players stats through using different equipment (its own class), and a way to have a computer goalie make its own moves when a one player game is played.

So what started as one input from the user developed, grew, and became more elaborate. And I was the one that caused that growth.

I think the Karaoke Machine project from the Java course on Treehouse highlighted this process well. The teacher used a Trello board to document what he needed to do, what he was doing, and what had been done so he knew what the program would need to develop and what stage of development it was currently in. It continued to grow. The program would need a song and record aspects of song such as the title and artist. Then it would need a whole songbook, or collection of songs, have a way to add a new song, a way to see the whole songbook, and select a song that someone wanted to sing. He "developed" it.

I think that is a good lesson that course taught and I didn't realize it until I wrote my first program in a way where I could say I actually "developed" it instead of trying to write it all at once.

I realize maybe I should have understood this sooner but I think it's a good point for a new programmer to recognize early. You can think of the end goal of what you need the program to do, but it is usually too difficult to write all at once.

A journey of a thousand miles begins with one step.

I plan on uploading my hockey game to Github soon and I will post on here when I do. I think I have it complete, save for a few features I would like to add but don't know when I will add them.

25 words or less: Write a program piece by piece and build on what you have. It's easier to start small and grow than writing it all at once.

I realize I bent the rules by using a contraction, but I still think it was as succinct as possible.

Thursday, June 16, 2016

Python's Simplicity

I did not post last week, but for good reason. I was involved in some volunteer work that took some planning and forethought so that took up most of my time near the end of last week. Thus I was not able to post here. I was also not able to program either as so many other necessary things required my time, not to mention Stanley Cup playoffs. Go Pens! (I was born and raised near Pittsburgh)

I will admit I had a little programming withdrawal...

So what have I been doing these past two weeks then? I wanted to try my hand at a simple text based RPG. I have been learning Python again so I was thinking of trying to write the program using Python. However since I more recently got into Java I thought I would attempt using it instead. Failure ensued that endeavor.* It seems the two weeks or so I was removed from Java learning (and the re-inputting of Python syntax into my brain) had caused some confusion as far as how certain objects and things interact with each other in Java. So I thought I would just use Python. Here are two things I noticed are more simple in Python. (I don't think simple is the right word)

-----------
User Input
var = input("Input something: ")
Python is quick. You just assign input from the user to a variable and that variable becomes a string of what is entered. Apparently that was upgraded from Python 2 and there is no longer a need for using raw_input. That confused me for a little when I was searching on Google.

Java is more detailed. You must have a scanner or buffered reader imported and it takes at least two lines instead of one. You must have a System.out to show a line of text, then the scanner or reader and readline to get the input from the user and assign it to a variable. I know the details for this are not exact, but it helps to know that more is needed for input when Java coding

-----------
Variables
var_list = ['one', 45, [4, 3, 2], True]
Python does not care what type a variable is when it is assigned. It will recognize it itself what kind of variable it is. I used the above example because this shows a list can be any length and be of any type.

int[] var = new int[5]
Java requires variable type declaration and, for lists, list size declaration. I believe there are ways to adjust the size, even later making it larger or smaller without pop(), but that would require a little more searching on Google.

I am not criticizing or praising either of these languages. I like the precision and the memory allocation in Java. The usage of public and private variables for classes is also vary beneficial. Python's simplicity cannot be underestimated. The writing and debugging is supremely easy to follow. However I'm not totally sure how to allocate the memory better in Python. Will have to research that more at some point.

I know there is no revelation here but I hope this serves as a kind of reference for me if I want to try my hand at Java coding later. At least a more detailed reference than my last post about some differences between Python and Java.

The simple RPG I am writing is a text based hockey shootout. More on that later and soon I will upload it to Github but I want to tweak it some more before doing so. My biggest struggle was how to get a random key from a dictionary. I eventually just searched and copied and pasted some code from Stack Overflow.

25 words or less:
I am going to stick with Python for awhile.

* Failure is not a bad thing, especially in programming.

Thursday, June 2, 2016

Back to Python and my first GitHub commit!

As a quick update to last week, I did upload my first project to Github. It still has work to be done on it, but I hope to expand on it soon.

Anyway, I recently found out a place I would love to work uses Python for much of their software. They produce a lot of animation, among other audio/video products, and many other things. So I decided to revisit Python. Just watching the introduction video on Treehouse and what Python can do helped me understand why this place uses Python for animations, etc. Thus I began the Python track on Treehouse. And a little bit of Javascript, still really need to learn more web development.

Many things are a review as I went through almost a complete book on Python. Things such as data types and methods are all things that came back to me quite quickly. I don't, however, remember some of the things discussed being in the book. So it is nice to have some new methods for certain data types. Things like using .pop() and .remove() on lists seem to be useful. Also having someone explain it while I code in the Terminal or PyCharm is nice too.

The differences between Python and what I've been learning in Java have really been apparent now that I am tinkering with Python again. Not having to declare variables and their type is a little difficult to get used to. In some ways I think I'd almost prefer declaring what type they are so as to keep them clearer in my head and maybe easier for someone to read my code. That may become clearer as time goes on though. Also not ending the line with a semi-colon is proving to be annoying as my right pinky finger tends to press that key before pressing the return key. But that is a minor inconvenience when it comes to the simplicity which is Python.

I used to think programming languages were the same with different syntax and I think that's still true to a large degree. However certain things just seem to feel different. It's difficult to describe at this time and may be easier to explain later.

But at this time I will just leave this post since I'm late this week due to being so busy, and committing to GitHub taking a little more time than I originally thought it would.

So 25 words or less:
First commit to GitHub. Python simple. Java more precise.

Less than 10 words! I will try to make a more meaningful post next time. Then I can have more information for reference in the future.

Wednesday, May 25, 2016

What It Takes to Code

One year ago, almost to the day, I received my first programming book. It was an intro to Python and I previously had zero knowledge of coding or how to program. Now one year later, almost two months worth of treehouse, five more books, and my first app soon to be pushed to my Github account, I think I've figured out what it takes to code. Note this is not about how to find a job as a developer or web designer nor what I think is the best way to go about learning how to code. But rather this is what I think is the most necessary quality of a programmer.

The 25 words or less version:
The insatiable desire to solve a problem no matter how long it takes and you will not give up until the void is satisfied.

The longer version:
Recently I have found closure to a bane in my life that has come and gone for the past six years. It is a completely different subject but I feel the situation is rather illustrative of my recent coding experiences. The situation was not a pressing problem, I couldn't change it, I didn't think about it 24/7, but sometimes it would still come to mind. Last weekend I was at a wedding and it is remarkable the change a 10 minute conversation can effect.

How does this relate to programming? I am still putting the finishing touches on the app I've been writing and as I said I will soon put it to Github once I clean it up slightly. I didn't think about the app 24/7. When I wasn't at my computer I couldn't code. However the problem was still there. Questions such as "Why can't I access this variable from other class? How can I send a list to another activity? How do parcelables work? etc." were going through my mind. When I couldn't figure something out and at the same time had to go to work and do other necessary daily things, the problem was still in the back of my mind and eating away at my sanity. I often thought of this xkcd comic as it seemed to apply to everything I was trying to do. I often thought I would never figure things out. Then somehow I would write a line of code, change a variable type, or change a method. Then, excelsior! it would run without errors! Thus providing closure to the coding issue I was trying to solve. So to revisit the illustration and apply it to coding: It is remarkable the change a 10 minute Google search or line of code or just some sort of coding "magic" can effect on my program, and uncoincidentally my subsequent emotional state.

This is less of a post about how to program and more about what your mindset should be when programming. I have been listening to the Code Newbie podcast and have been loving the fact that at this stage of my learning I am not alone in feeling I can't program or should quit and do something else. Other people learning to code have had the same issues and feelings that I have on a regular basis. But after all the work, late nights, and frustration the feeling of finally figuring out a problem, writing code that runs without errors, and seeing my app on a phone and working the way it should makes me want to get up and dance! (which I sometimes will do when I've spent too long a certain bug)

I've also been rather impatient lately and want to see if I can explain concepts, instructions, and stories in 25 words or less, as sometimes my attention span doesn't last too long. I don't know if it will always be possible to do so but I will try to provide a short and long version of what I want to put in this programming story. I find certain explanations of how some method should work are wordy and unclear and maybe I can do my part to solve that as I go so I can more easily understand them later.

Hope to have my app on Github by next week. Upward not northward!

Wednesday, May 18, 2016

Storing Data

Anything I've written so far has only been good for one instance of the activity in my app. The last course on the Treehouse Android track was about storing information. Every time another activity is stacked on a previous activity, the previous activity is paused and Android may stop and destroy some of the data to free memory. However there are two methods that can be overwritten

OnSaveInstanceState
OnRestoreInstanceState

OnSaveInstanceState bundles information that should be saved. To retrieve that information the method OnRestoreInstanceState would be implemented and in that method you would have to write the code to set the variables in the bundle to its respective member variable. This is absolutely necessary because anytime the screen orientation would change while using the app, android essentially stops and restarts the activity to display it in the correct orientation. It's interesting because in the onCreate method, Android Studio puts in a parameter for a savedInstanceState Bundle. So it will already expect to load previous information.

There is also a data type of SharedPreferences. I still need to experiment with this type, and I have some ideas for my first app I'm writing to try it out. A variable can also be assigned to a SharedPreferences.Editor. This can thus implement edit, putInt/String/etc., and apply. They are basically self descriptive. I may be slightly wrong in my current understanding, but this is how I understand it at present. For example a member variable mSharedPreferences is set to a constant that is a file location in the package. The string would be the package path plus ".preferences". Another member variable mEditor can then be set to mSharedPreferences.edit(). Then you can apply functions to mEditor, such as putInt/String/etc.(key, value). Then after all variables are "put" into the editor, you call the apply method to save it. The location the information is saved in the place initially set up in the mSharedPreferences variable. To live update a preferences edit, you would call notifyDataSetChanged() on say, a list adapter.

I hope I can reread this later and understand it. And if anyone is reading this, I hope it makes sense. If the last paragraph makes absolutely no sense, especially after trying it, I would appreciate a way to explain it better. The more I use these functions the more I will understand them. I'm still unsure of how to implement adapters for list views as I am currently still just copying and pasting code while adapting the variables to match each activity.

So far everything I've ever learned in programming, from last years Python and PHP/SQL to the current Java and Android courses, has been an amazing experience and I feel I've come so far. I'm really looking forward to what more I can do in the coming months. I think too that I should document how many man-hours I am investing in writing this. Then I can look back and know how much time it took to write my first app.

The constant earwigs of how to figure things out however is likely incalculable.

Friday, May 13, 2016

Binding, Views, Public, Static, and Frustration

I'm in the process of writing my first app. A way to arrange products so they can be showed to a potential customer. I'm doing it for a friend with a car detailing business so it is specialized to that. After I finish it for him I plan on generalizing it so it can be used for other businesses.

The second app walk-through in the Treehouse Android course is a weather app. In it you must send information from one page to another and display it in a list. This is the format I used to write the code for the car detailing product app. It should also be noted I'm using Android Studio and have the Butterknife API.

The biggest obstacle I had, spent 5 hours on, and stayed up util 1 am* trying to solve was binding variables to the xml items in the list view. I kept getting a 'Null Pointer Exception - Attempt to get length of a null array'(loosely quoting) and it said to possibly add @Nullable. It was a rough night as searched and searched but couldn't find anything that would make it work. I finally ended up writing the adapter code again and somehow it worked this time. I don't know how I fixed it as I merely duplicated other code and changed variable names, but in the end it finally worked. I was so happy to go to bed.

The following day a challenge I had was creating an array of different services. I wrote a class that would populate arrays of products for the different categories of services. However I couldn't access them from the main activity and I didn't know why. So I ended up just writing methods in the main activity. It worked but it wasn't as organized as it could be. Looking at other code I found if I wrote those functions to populate product information public and static instead of private, they could be accessed by other activities and classes. It was really a simple fix and I should have seen it. However the triumphant feeling of overcoming the challenge overpowered any feelings of inadequacy.

As of now the app functions correctly and only needs to be cosmetically enhanced. I would also like to add the following features:
  • Showing before and after photos of products offered
  • Adding and deleting products
  • A way to edit the product names and descriptions
Also instead of getting the product information from a class, it would probably be better to have a database in the app. However that is beyond my knowledge. I know some SQL and how to integrate it with websites and PHP but I don't know how to have it in an Android app. I do believe there is a course or workshop on Treehouse that discusses that.

* I realize if you are a programmer 1 am is likely considered barely a late night trying to fix a bug.

Wednesday, May 4, 2016

Git

I finished the Java track on Treehouse the other day. My last post was a review of that course and not much has changed since then as far as my viewpoint goes. I still think it is a very good course. And for a $25/month fee it is certainly a good deal. Plenty of knowledge, videos, and forums to learn as much as any beginner needs. Recently I've been highly disappointed with customer service everywhere so until now Treehouse has been a breath of fresh air when it comes to quality product for a good price and good customer service. For example the few times I've tweeted about my learning experiences they liked my tweet and commented on it.

With that said, recently a friend of mine asked me to make for him an app for his car detailing business. I told him awhile back I would do it as I've been learning programming lately. My newly gained knowledge of Java, and again working through the Android development track, I thought now is the time I can start on this product display app my friend asked me to make for him. If you have been starting to program lately I'm sure you've seen the term "Git" and always wanted to learn what it means. I had no idea what it really was let alone learn how to use it. Any Google searched just used terminology that was way over my head and I had no idea how to access anything. Quite frustrating.

That leads me to looking on Treehouse to see if they have a track for this and they do!! It is one of their shorter tracks, saying it is only a two hour course. I'd say it is pretty close to that as it took me about one afternoon to finish it. It was amazing. Very clear and descriptive on how to use it and how beneficial it can be! Of course Git isn't the only version control system (VCS) but it is likely the most popular. I would highly recommend this course if you plan on developing your own projects or obviously if you want to join Github. Incidentally the treehouse course explains how to use Github.

Since I searched awhile ago on how to use Git and couldn't find a set of commands and clear explanations of those commands, I wanted to compile a source of simple Git commands that one would use a lot. I don't remember exactly how I found this site, possibly in teachers notes on treehouse. After using Git for an afternoon I feel this is one of the best explanations of Git commands out there.

One last cool feature I found in Android Studio is that it has a way to use Git from the menu bar! That will certainly be useful in developing projects. I haven't looked but I'm sure regular IntelliJ and Eclipse have a way to have a repository as well.

Last words for this post if you get discouraged in learning how to code, don't give up! It will likely click after you use it some and write code into your own projects.

Thursday, April 28, 2016

Java Course Review

(For the basic review, see last paragraph)

Near the beginning of April I signed up for an account with teamtreehouse.com. I started with the Android development track and enjoyed it. Downloaded Android Studio and was getting used to the instructor's speedy approach to teaching. I found myself however still confused by some of the concepts of how classes could be used, overridden, and imported. Basically anything of the more meaty techniques. He also mentioned that some of the things he discussed was covered in the Java development courses. So I decided to pause my Android venture and try the Java development track.

As of this writing I am almost finished with the course, being about halfway through the last section, which covers creating a GUI. I would rate this course a 4 out of 5. I liked how in the beginning he went slow and I could ubderstand much of what he was talking about and could pass many of the challenges fairly easy. This could also be because much of it was basic. Variable initialization, constructors, data types, etc. I did not like workspaces. Sometimes it would not open correctly and I don't think it mimicked IDE interfaces. So I ended up doing a little research and downloaded Eclipse and used that to write the code and ran the programs through the terminal. Finally one of the last courses was about using an IDE. I really enjoyed this section because it explained how to use IntelliJ IDEA and explained many tools that are included in that IDE as well as many keyboard shortcuts. I can see they prefer IntelliJ products as the Android track uses Android Studio for its lessons.

With that I think it was a good course... so far as I'm not really done yet but just about done. I do think later on in the course the teacher would just type and say what he was typing without much explanation. During the challenges is where it really took a down turn. The objectives really weren't clear as to what they wanted me to do most of the time. I don't think it was just me either because many of the questions posted on the course seemed to be about not understanding what was wanted in the challenges.

So those are my two biggest complaints that could be better on the course, 1) talk a little slower, 2) ask questions in a better way. However my phraseology when I ask questions is usually atrocious so I don't think I can criticize. I loved the course and learned tons. I really want to delve into using it on some applications I want to make. The teacher did an excellent job of teaching how to use Java documentation and IntelliJ IDEA to find how to do something, which is certainly better. Teach how to find answers for yourself instead of just giving the answer. So until next time I will be finishing this course.

Sunday, April 24, 2016

Treehouse

So I've not been posting here, but I have been coding. I was using C++ Institute, but got bored with it. It doesn't use very many real life examples and the tests are very confusing. For instance, in the quizzes you have to answer questions similar to this. You are supposed to find out what this will output, or if it will not compile.

 class X {
    private: 
        int v;
    };
    
    class Y : public X {
        Y() : v(0) {}
    }
    
    int main() {
        Y y;
        cout << y.v;
        return 0;
    }

I know this isn't a super complex problem but I find it very hard to follow the X's and Y's. And all the questions use names like that and  are very hard to follow. I also found the wording in the lessons to be not clear and I don't think they explain the concepts of certain things very well. So although it seems like a good idea, it's not for me. So I tried something different.

Teamtreehouse.com
This seems to be better for me. The videos are very nice. My main complaint is sometimes they go too fast without explaining thoroughly and just say things like, "We're going to type this function here." then they type it. After I type it myself, pause the video, and study the code I just wrote I can follow the logic. So I think it's a good program for learning and I plan on sticking with it. Everyone learns in different ways so someone else may find it better. They also have different teachers for different languages

I think I had the same issue when I was studying my Python book. At first I didn't understand at all what things meant. Then after using it with some of my own programs I wanted to write, I understood concepts such as using different classes and methods.

One thing I like about Treehouse is they teach best practices with the programs. I like the fact of making different methods for basically every task. I can imagine that is a basic concept for programmers, but being self taught it will be helpful as I go to write more programs so as not to make my code too complicated by writing a crazy long method.

I haven't updated this for a while because I've been so busy programming. I think though I may change the format of this and make it some tips I've come across for programming. That way if any coding noob may stumble across this in the internet they can glean things that I found helpful in my quest for more programming knowledge. Or they can just go to stackoverflow.com. I don't know if anyone would find anything on this little blog that can't be found on that website.

And by the way, I've been doing the courses for Android Development and Java.

Thursday, March 3, 2016

On to C++

One reason I have not updated this is because I was very focused on finishing the book on PHP and SQL. Very informative and I created a few basic web based apps for myself. They are very basic and still a little buggy. I also enjoyed experimenting some more with CSS but that will have to be put a little away for the time being. Another reason I haven't updated this is because...



I was in Dominican Republic for 10 days at the end of February. That was truly a culture shock and an incredibly amazing experience.

Now that I'm back I've entered the world of C++. I'm using the website www.cppinstitute.com. I finished chapter one rather quickly. The knowledge I gained from the Python book seems to have really helped. Variables, data types, Unicode, functions, etc. are all things discussed in the first lesson on the C++ website and I know about this terminology from the Python book. I'm hoping there are more similarities. I'm beginning to think many programming languages are somewhat alike in theory but differ in syntax and how easy it is to perform certain tasks.

As a compiler I'm using Xcode. I have very little experience in using different compilers and IDE's so it's hard for me to have an opinion on which would be a better avenue. I used Python's IDLE for all my Python learning and apparently that is an awful IDE based on the opinion of certain people on the internet. I have since downloaded PyCharm. I want to focus on C++ right now so I may take a break from Python. Xcode seems to be doing ok for my one simple square root program I wrote in C++ but I had to look up a youtube video to find out how to compile the code. The frustration of something not working and not knowing why is easily eclipsed by the feeling of finally figuring out how to make it work.

I'm excited to get into C++. I did some in high school but forget everything and I'm sure it's changed at least some since 2005. So I'll update this again once I learn more. I've also made a Twitter to follow other programmers and developers to share and gain more information. Who knows, if I learn enough and can get a job, maybe I can move to Dominican more permanently. Or at least for a much longer stay than 10 days.