Monthly Archives

October 2015

Classes

10 Lessons Needed To Learn Programming In Any Language*

October 19, 2015

Without farther delay, here is my top 10 lessons to learn programming in the order they should be taught:

  1. Environment Setup, Input/Output
  2. Integers and basic arithmetic (+,-,*,/,%) + order of operations ()
  3. Float Arithmetic
  4. Variables
  5. Booleans
  6. Conditional Statements & Control Structures
  7. Functions
  8. Strings & Arrays
  9. Classes
  10. Exceptions

Ok, I lied. A lot of these have to be broken down and cross referenced. However, I think that should be the order in which people are introduced to programming. Too often people try to learn everything all at once. You can’t learn how to swim by jumping into the deep end. Well, some of us can. Learning a new skill isn’t always as easy to other people as it was to the teacher. Whenever I get a chance to be a bookstore, I like to look at beginner programming books. They all promise to teach a lot in a very short time. The honest truth is there is no shortcut. It takes a couple of years of programming in order to become a half dissent programmer. It is not because it is hard, it is just because there are too many variables. There are so many different things that you need to learn, that it can’t occupy just one book. It can’t even occupy the whole Internet. New things come up everyday that change what you knew before. Becoming a programmer is just one of these thing that require time.

So what is with this list? I got asked too many times at this point how I started programming and where/how to start. I have decided to write my own intro to programming. Over the next weeks I will publish small lessons that will build up to an ebook on programming. I will use Python-ish style code and most of the content will be generalized for almost any language. By the end of the book, you will be able to learn the syntax to a new language and program in any language you want. Best of all, the ebook and lessons will all be posted on this site and will be FREE. No strings attached. I believe anyone can learn programming at any age and that education should be available to all.

I have taught a few students before and there are several reasons why my list is different from other learn programming classes. The main objective is to start with something familiar that everyone in the 5th grade should know. Basic arithmetic and numbers. Than slowly build up while introducing new ideas and concepts. The secondary objective is to fully learn a topic before moving forward. That means when we will talk about a new concept we will go back and see how it reflects on other topics we have learned before. We are going to aim to turn fragile knowledge into concrete building blocks that will help us move forward and ultimately complete our objectives.

It should be mentioned what this course will not achieve. Beyond all else, you will not become a certified programmer or a hacker. Like I said before, that will take time. There is no class in the universe that can teach time. After you have completed the lessons, you will have to continuously learn new skills and develop yourself as a programmer. Think about learning programming more like going to the gym. You will not get a six pack and lose 50 lbs. in one setting, it takes time. Same thing with programming. You need to invest time to become a programmer. Just like going to the gym everyday, it will pay off in the long run and you future self will look back and thank you.

There are a bunch of reasons of why you should learn programming, I don’t feel like repeating most of them. I will just mention one. Programming is everywhere and it is here to stay. Programming has become a tool that used in virtually every field, even philosophy and history. Programming is almost equivalent to using excel. It is a skill more and more employers are looking for and will make you more valuable and your job easier. There are endless examples of how programming can shorten your job and sometimes turn an impossible human task into a simple short snippet of code. I believe that if you are reading up until this point, you already have a goal to learn programming. Or maybe you have already been through a class, but feel like you have missed the points and you want to know more. In programming there is always more to learn. Remember that.

First lesson will be published within a week. Make sure to follow me on Twitter @CptDeadBones to get notified.

* Ok. So technical, the class will teach you how to program in almost any language.

Latex, Mathematics, Python

PyTex2png Make Pretty Math PNG Files With Latex, Python and C++

October 9, 2015

A few months back I posted some code on GitHub to convert Latex markup code into PNG files using Python named it PyTex2png. I know realize that I didn’t explain one bit of the code. I did however write a very clear (to me) README.md file explaining how to use the code if you wanted to download and use it. I even included a couple of cool examples. In this post I want to dive into the code that make it work.

Lets start by explaining exactly what Latex is and what this module does. Latex is a markup language used for scientific notation and papers. I came across it in my second year in college and science then I have never looked back. It is by far my favorite word processor. I even have my resume and letters typed up in Latex. It just make pretty things. Among other things, Latex can create beautiful Mathematical typography like the one below. Latex does have a bit of learning curve, but it shouldn’t be too difficult to master. We are only looking at the math portion of it anyway. Here is some Latex code and the image that got generated from it. Note that this post will not teach you Latex. There are plenty of books and information about Latex online already.

Code:

\cos (2\theta) = \cos^2 \theta - \sin^2 \theta \\
\lim_{x \to \infty} \exp(-x) = 0 \\
a \bmod b \\
x \equiv a \pmod b

[\code]

Pretty Output:

 

wpid-operators-2015-10-9-00-48.png

 

So how does that happen? Well there are a few things to look at. The first is a C++ code that was written by Bruno Bachelet. It is available here (and here as backup). There is also and online version of the code running here. This code is the main power behind the software. Everything else is just a Python wrapper to add some fancy functions like multiple files, remove background and invert colors.

Within pytex2png.py you will see a function name covert(). This function calls the C++ module to convert images, creates the PNG, removes any background and finally converts the math to white. The last portion is not really required. I was coding this up for a larger project and in that one the background is not white, so I need white fonts.


def convert(source,output,display=False):
	if(display): print "Coverting LaTeX from " + source + " to " + output
	data = get_file(source).replace("\\","\\\\")
	make_png(data,output,display)
	make_transparent_bg(output,display)
	make_white_text(output,display)

This doesn’t tell us anything about how this program works. It is just a plain function that makes calls to other functions. We will have to look at the other functions to understand how this one ties everything together. Lets take a look at the code:


# read file content as a single variable
def get_file(filename):
	file = open(filename, "r")
	lines = file.read()
	file.close()
	return lines

# create png from latex
def make_png(data,image,display):
	if(display): print "Making image..."
	command_line = "./tex2png -r* -lq \""+data+"\" " + image
	exe_command(command_line,display)

# remove white background
def make_transparent_bg(image,display):
	if(display): print "Removing white background..."
	command_line = "convert "+image+" -channel rgba -fill \"rgba(255,255,255,0)\" -opaque \"rgb(255,255,255)\" " + image
	exe_command(command_line)

# convert output text to white
def make_white_text(image,display):
	if(display): print "Converting output image text to white..."
	command_line = "convert "+image+" -channel rgb -fill \"rgba(255,255,255)\" -opaque \"rgb(0,0,0)\" " + image
	exe_command(command_line)

After a careful inspection of these functions, you should see a pattern. The first one is a get_file function. All it does is open a file and reads it’s content. Then returns the data from the file as a single object. You can read more about reading a writing from files in the Python Documentation. The other 3 functions, make_png, make_tansparent_bg, and make_white_text follow a templet:

display output
build command line command
execute command

That’s all folks! it is pretty straight forward. The commands each one execute are based off of what we need done. The first one, make_png calls the executable made from the C++ module. The remanning 2 functions, make_transparent_bg and make_white_text use a command line tool called convert. This is a Unix utility and you can read all about it in the convert manual file or on this site.

You will notice that there is one function we haven’t mentioned yet, exe_command. Let’s look at that function:


# runs a command line command and waits for it to complete
# by default it will not display any output
def exe_command(cmd, display=False):
	args = shlex.split(cmd)
	if(display):
		p = subprocess.Popen(args)
	else:
		p = subprocess.Popen(args,stdout=subprocess.PIPE)
	p.wait() 


This one should be taken as is. It takes in a command line command and excites it using a Python module called subprocess. It can be used to execute and command in the command line and can retune the results if needed. It is a good little function to keep handy for other programs in the future...

That is all the magic that ties this thing together. There are some more files and notable mentions.

  • Examples folder holds text files with Latex markup to be converted.
  • Output folder will contain PNG’s that have been converted already.
  • Source has the C++ file need to make the PNG files in the first place.
  • Makefile is used to compile the C++ file and create and executable for your computer architecture.
  • examples.py is a simple Python module to load the example text files from the folder and run them through PyTex2png converter.
  • gpl-3.0.txt is a GPL License under which the code is distributed.

Here is a PyTex2png zip file with all the files together. It is also available via GitHub (pytex2png).

Personal, Random

Top Reasons People Give Up When They Shouldn't

October 1, 2015

We all face challenges, not only in coding, but in life. There are many points I find myself almost giving up. I find myself loosing grip and almost saying “fuck it”, but i don’t. I came across this list and realized, sometimes seeing the reason why people give up can show me how I can overcome giving up. Knowing is half the battle, right?

Top Reasons People Give Up

  1. Expect Fast Results - Nothing in life come easy, today more than ever. Everyone today excepts fast turn arounds. I built a site, now where is my million visitors? Results requires hard work. Hang in long enough to see the results. Life isn’t a video game that can be finished in a day.
  2. Stop Believing in Themselves - I really think this was a top contender for number 1 on this list. I have worked with students and startups that stop believing, at the most critical point. It is easy to believe in the beginning, but as time goes through doubt comes to mind. A lot of people tell you that you are no good. That your idea is wrong. You are caving in and starting to believe them. Write things down, so when you stop believing you will have someone to make you believe.
  3. Get Stuck in the Past - Too many people are caught up in what happened in the past. What is done, is done. Life will move forward and you should too.
  4. Dwell on Mistakes - It might seem similar to number 3, but it is not. The path to success often requires failure (and experimentation). No one is perfect. Everyone makes mistakes. It is what makes us human. Hell, even my calculator is wrong sometimes.
  5. Fear The Future - The marines have a saying: “The only thing we have to fear is fear itself”. The future will come, you need to brace yourself and move forward. Things will work out and soon enough the thing you fear the most will become a thing of the past.
  6. Resist Change - I can probably go on about this forever. This is one I do to myself. Resisting change. Not going with the times. It is hard, but the world today works fast. In order to accomplish your goals and reach success, you have to adapt. We had to adapt to live on this planet, the least you can do is use your smart phone.
  7. Feel the World Owes Them Something - Another common belief this generation has. The World doesn’t owe you a thing. Get over it.
  8. Never Visualize What is Possible - Anything is possible. If one man can build Facebook and enslave the World to it, you can do anything. Visualize what you want, all of what you want. There are no limits to your imagination.
  9. Overwork - Take a brake from things. Life can’t and shouldn’t be all work. You can actually give up because you are overworked. Take a vacation once in a while. Communicate with other people. Get inspired. Then come back to work and finish what you started. It doesn’t have to be a long brake. It could be just an hour. A day trip. Something that will make you miss your work and have you come running back to finish it off.
  10. Assume their Problems are Unique - Guess what? Everyone has problems. 99% of the time, someone else had the same problem as you. No money. Lack of tools. There are allays problem. Problems are meant to be solved. Seek help. There are many online tools and forums that can help you figure out a solution to your problems.

What do you think? Are there other reasons pope give up? Post it in the comments bellow.