Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Tuesday, February 13, 2024

Writing Your First Computer Program

I have someone that I am working with and we need to write a complex computer program. We have settled on Python as the computer language and we get together frequently to work on the code together. While I have done some programming using Python, I am by no means an expert. Basically I know enough to be dangerous.

If I had 30 years slashed from my age and just started writing software, I would start with Python as it is such a good language to do a lot with using a minimal amount of code. That makes the programs easy to write and easy to understand. The only problem is that there are quite a few libraries to learn. Without the help of the Internet to help you look up possible solutions, doing anything useful in Python might not be so simple.

My coworker and I have a rather complex problem we are trying to solve but have broken it down into very simple pieces. The first thing we want to do is take an e-mail address and modify it. It didn't take that long to write the code. Next we expanded our problem so that we passed in multiple e-mail addresses and did the same modification to all of them. Then we worked on saving the results into a database. We have continued adding pieces to our code to ultimately get the program we need.

Our code writing is an iterative approach. We focus on one task and make sure the task is completed as designed before moving onto the next item. When we got the code modifying e-mail addresses and writing them into the database, my coworker felt a huge sense of accomplishment and thought we could be done. Today I reminded him that there are still a few more steps we need to do before we can say we have completed everything.

Should you decide to give coding a try. Here are these simple steps put into an easy-to-follow outline:

  1. Write down what you want your program to do. This is often called a specification or spec.
  2. Break the task down into very small steps.
  3. Write the code for each step in the process and test it to make sure it works before moving to the next step.
  4. When you think you are done, go back to your specification to make sure you have completed what you said you would.
Interestingly enough, these are the same steps to follow in trying to accomplish any goal you have. They work for more than just writing computer software.

Sunday, November 19, 2023

Coding in Python

I have a new project at work that has me interested in doing some Python programing. I always say that Python is one of those languages where even the largest programs are only 50 lines long. That is a bit of an exaggeration but most of the code I have seen fall into that categorization. The reason programs can be so small is because there is an ever-growing list of libraries to help. If you are trying to read in a comma separated values (CSV) list and process it, somebody wrote a library for it. You don't need to reinvent the wheel. Instead you can use the CSV library and focus on what you need to do.

The project at work is kind of simple. We just need to read in a CSV file and apply a hashing algorithm to one of the columns in the file. Then we will write the resulting modified file into a database. As with the CSV library, there is a library to do the hashing for us. There is also a library to write to the database. Furthermore we have source code from one of the teams in Japan that serves as a great sample code to follow.

There is another engineer on the team who is responsible for writing the code and I am merely to act as an advisor. We started by reviewing the sample program and I stepped through each line of code with my coworker. At first I felt glad that he is responsible for doing the coding on the project. The more we reviewed though, the more I felt I wanted to take a shot at doing the work. I don't think it would take me more than a day and I think I would do a great job. Unfortunately that would be overstepping my bounds and my job really is to serve as a mentor. I wonder if it would be okay to add a piece or two to the sample code we already have as there is one difference that I think would be fun to address. Tomorrow I have a progress meeting and will see how far the other engineer has gotten. Hopefully he has figured it out on his own. If not, I'll play with it on Tuesday.

Wednesday, March 23, 2022

Coding Tests vs. Accomplishments

This week I was asked to take a coding test for a potential position as a Director of Software Development. I looked at the company providing the test and even ran through a number of sample questions. Ultimately I decided that I really don't want to work for a company that asks everyone to take such tests. I understand that organizations hiring software engineers need to gather some level of proficiency but think coding tests are the wrong way to go about it.

If you look at how world-class software organizations like Google gauge proficiency, they do it with white-boarding sessions. You are presented a problem and write code on a whiteboard, explaining your thought process as you jot things down. This allows the job candidate to ask very important questions. It also allows the interviewers to gain tremendous insight into the candidate's thought process.

One of the questions I was presented on the sample coding test was FizzBuzz. The program asks for an upper integer and then prints the whole numbers from 1 to the upper integer. If the number printed is evenly divisible by 3, then the word "Fizz" is also printed next to the number. If the number is evenly divisible by 5, then "Buzz" is printed next to the number. Finally, if the number is evenly divisible by 3 and 5, the word "FizzBuzz" is printed. There are some variations but hopefully you get the idea.

This is a great programming problem to solve as it does not require a lot of code but does expose some fundamental programming ideas. The first is if you understand the mod function, which returns a division remainder and is usually denoted by the percent (%) symbol. The equation "5 % 3" would return 2 as 5 divided by 3 has a remainder of 2. When you find a number with a remainder of 0 when divided by 5 and/or 3, you can print the appropriate word.

Next you have to know how to do loops in your language of choice. The testing software gave me something like 27 different computer programming languages to choose from. My preference would have been to use a FOR loop and I think most developers would start with that as well. For my sample test I used Python and made sure to set the range from 1 to n+1, where n represents the upper integer limit. Why n+1? Well the FOR loop goes up to but doesn't include the upper number. If you put in the upper limit of 15 and run the FOR loop, it will only go to 14 and so you need to add 1 to the upper limit so it goes high enough.

Both of those concepts being tested are fairly simple and an hour of coding instruction should be all that is needed to get that part of the test correct. Now is where it gets complicated. In my sample coding test, I was only going to be given 30 minutes to complete 5 such examples. My first thought would be to brute-force it an get something running. That would look like a nested IF-THEN-ELSE statement where you test the 3 and 5 first, followed by the 3, then followed by the 5. Then I would move onto the next question.

The advantage of white-boarding this problem is that the candidate can ask some important questions that show even greater insight in his or her experience. For instance, "Is performance important?" IF-THEN and IF-THEN-ELSE statements are very expensive in today's modern processors. You can write the code to use 2 instead of 3, which can be significant when your upper limit is a really large number. The downside is you sacrifice code readability which makes it more difficult for someone new to come in and maintain your code. Unfortunately in an automated test, the candidate is forced to make a choice without any feedback from the testing software.

As I mentioned, I opted not to take the coding exam. It is not that I felt I would do poorly. Instead I feel like having my code running on over 100 Million PlayStation 3's, over 100 Million PlayStation 4's, and over 17 Million PlayStation 5's should give some indication that I know how to write code. I think it is much better than taking a test that favors trivial details and syntax. Perhaps you may disagree.

Thursday, January 6, 2022

The Art of Debugging

I mentioned in a previous post that I am in the process of creating a prototype web application using React. Unfortunately I am also learning how to debug my React code. I created a base version of the application using the tutorial I mentioned in my previous post. Once I got all of that working, I started expanding.

React takes the code you write and compiles the code so that it runs quickly. This compile step is where I find a lot of errors. I have a console window open that spits out a bunch of important information each time I save a file using my interactive development environment (IDE). Sometimes I will get errors and other times I get warnings. While the code is supposed to run with warnings, it generally means something is wrong and is best to fix it. Otherwise your application may not behave exactly as you thought it should.

The worst problems I have encountered are runtime errors. That means my code compiles just fine but the application doesn't look right or doesn't render at all. The first such problem that took me a while to solve involved the HTML break tag. I had a picture and wanted to have a caption underneath it. To make everything look clean, I laid out everything in a table. To get the layout perfect, I used a test HTML page and then just transferred everything to React when I was happy. I didn't know that React treats the break tag a little different and required a slash in the tag. Instead of using <br>, I had to use <br />. That looks pretty simple, right? Well it broke the whole application. Instead of seeing a funny-looking table, I didn't see anything. In order to find the offending tag, I had to copy each part of the HTML table into React and discover when things stopped working.

Today I ran into another problem. I have a button component that looks very integrated with the entire site. It is something I copied from the tutorial mentioned earlier. The guy giving the tutorial didn't bother to do anything special with the button other than to have it jump to a single page. I have put a number of those buttons all over the place but no matter which button you click on, it goes to the same page. Today I wanted to connect one of the buttons to a different page. I looked through the code and figured out a way to make it happen. Unfortunately when I added the code, it broke everything. Not a single page would load. It turns out that by adding the destination page as a parameter to the button, I broke every instance of that button. Seeing as I had a button on the heading of every page and on the footer, that should have been a sign to add the destination page as a parameter to all of the buttons in the application. Once I did that, everything started working again.

The Internet has been very helpful in solving a number of problems. I tried to embed a YouTube video into my application only to get an error from my browser saying that it can't show the video as a security precaution. A quick search showed that I needed to add a library to React and then things started working great.

I'm sure there will be many more problems I run across, but so far I have been able to solve them in a timely fashion. If I feel I am spending too much time on an issue, I put it on hold and work on something else. Sometimes I will go for a walk and the solution will come to me. Ultimately there are a number of tricks to debugging computer programs and it doesn't matter if you are coding in Python or React. I suppose the most important trick is to not get frustrated. I'm still working on that one.

Tuesday, August 20, 2019

Everyone Should Learn Python

If someone who wanted to learn a computer programming language asked me which one to learn, right now I would recommend Python. Back when dirt was new, dinosaurs roamed the earth, and I first started learning how to program a computer, BASIC was the language everyone learned. Then I moved onto Pascal before tackling C and C++. Nowadays I find that most of the coding I do is using Python and I love it.

I am currently working on a project where I need to display the size of a file on a web page. Most languages would require me to use IF..THEN logic show GB for gigabytes, MB for megabytes, or KB for  kilobytes. In Python, I just need to use the "filesizeformat" filter. It automatically converts the number 2600000000 into 2.4 GB (don't worry about it not being 2.6 GB as 1 KB = 1024 Bytes, not 1000).

Python is a very compact language and doesn't require a lot of coding to get a lot done. Chances are that someone has already figured out how to do something that used to require thousands of lines of code and reduced it to a single line in your program. The only problem with that is you need to know what line to use. That is an easy-enough problem to solve though. Just type a short description of what you are trying to do into your favorite search engine and you will get a number of suggestions. Most of the time one of them will work perfectly.

Python also has the advantage that is the computer programming language of choice for data science and data analytics, which is what I do a lot of. For a while, people used R but now Python has overtaken it.

Yes I still remember BASIC and Pascal really helped me understand a lot of fundamentals for computer programming. But if you are starting out on the path of learning your first programming language, my recommendation would be Python.

Wednesday, January 10, 2018

Getting Technical

I flew back to the Bay Area this week and started working in earnest. That can be tough after a few weeks of Christmas and New Year's celebration events. Yesterday I quickly got through my morning ritual and starting working on a design document for one of my projects. I really got into it and didn't notice time passing. One of my regular lunch buddies had to remind me that it was time to go get food. It has been a long time since I got so involved in a project that I needed to be reminded about food.

When I first started this job almost 8 years ago, I didn't think I would be here this long. I got hired as an individual contributor and did technical things on a daily basis. Then I slowly got moved into management. Now I am several levels deep and rarely get to spend my days doing technical things. That is one of the things I want to change and am challenging myself to spend half my time doing technical tasks. That forces me to quickly move through my management tasks. Then I jump into my technical work and am finding it to be a lot of fun.

The scope of my technical project is rather large and so I get to do a bit of dabbling in various technologies. Yesterday I created a document spec for a NoSQL database. Then today I started doing some Python programming to populate that document. Tomorrow I will be looking at machine learning for another part of the project. This sort of reminds me of the weather in the mountains: if you don't like it, wait a minute.

Thursday, May 4, 2017

AI is Still Pretty Stupid

This past week I have been doing some research on artificial intelligence and it has been a lot of fun. I created one program to tell knock-knock jokes. Then I created another to play something similar to the old text-based game called Adventure. Both of those programs I create with the help of AIML and they each only took a few hours.

Next I looked at doing something a bit more advanced and utilized scikit-learn in Python with some machine learning. This time I played with creating a program to converse with me. I downloaded a number of training files that are used to train the program in the English language. The training took less than an hour and soon I could start interacting with my program. I typed in my first statement and it thought about it for a while. After several minutes, it spit out something in English that didn't match my initial comment. I figured my program needed a bit more training and so I taught it a few facts like "2 + 2 = 4". It responded with the correct answer when I asked what "two plus two" was. Then I asked about 2 plus 3 and it returned "2 + 2 = 4".

I played around a bit more and came to the conclusion that simple AI programs with an hour of training are really stupid. So how to do you spend more time teaching an AI program? Microsoft created Tay and let it loose on the Internet. In less than a day, it became incredibly racist. Naturally they quickly stopped the program. Think about it, would you really want your child to learn all its knowledge from the Internet? Of course not and so allowing your AI program to roam free on the Internet is probably not a good idea. The reason we send kids to school is that we want them to learn from a trusted source and for certain knowledge we don't even trust ourselves.


Wednesday, December 14, 2016

Technology Innovation

This morning I met with the head of development for a large technology organization and we talked about some of the latest trends in software development. I work in a research and development group and so my team constantly looks at emerging products and how they can best be used to solve problems. The person I met with came with questions about how his organization can leverage some of these new tools to increase efficiencies and streamline software development.

I find it interesting that I found myself championing technology innovation. I am one of those people that resists change and that is one of the problems with the staff of the organization I met with. Software developers get comfortable with the tools and languages they know. The longer someone uses a technology, the more they resist moving to something else. While Java is a great software programming language, there are problems that can be solved quicker and with less effort simply by choosing Python or Erlang.

I have had to force myself to constantly ask if there is a better way of doing something. A simple example is when I had to cut up some old rags this past weekend. I had a dull pair of scissors and a large pile of rags to cut into smaller sizes. At first I tried to make a quick snip with the scissors and then rip the fabric. Things sped up when I simply just got another pair of sharp scissors. Taking 30 seconds to change my tool cut my project time in half.

Looking for a better way to do a task is not something I developed on my own. I have surrounded myself with people who are always looking for a better way of accomplishing things. I have tried to remember to ask for help but often times my coworkers volunteer better ideas for me. That takes a bit of humility but I find it also saves a lot of time. Sometimes if it wasn't for them, I would still be retyping everything instead of cutting and pasting.


Friday, May 21, 2010

Learning Python

One of the nice things about my new job is that I get time set aside each day to learn a new skill. Most of the developers I work with here are using Python and so I figured I would learn this computer programming language. What better way to learn than to work on an actual project. One of my coworkers was more than happy to volunteer a project and after a week, I am mostly finished. There is still some clean-up work to do, but the program works.

I do have to confess that I am not any better at knowing Python now than when I started the project. My program interacts with so many other pieces of the system that I feel like I am working with Lego blocks. When I need to figure out how to interact with one system, I do a search on the Internet, find some sample code, and work it into my program. I know just enough to make changes so that it works. When I get errors, I search the Internet and get an immediate fix.

Today I have a code review and I have to explain how my code works and why I wrote it the way I did. The only problem is that I have no idea what I did. The program just works and does what it is supposed to do. Oh well, maybe I will learn Python next week.