Pages

Thursday, June 27, 2013

Let's Learn Something! Python - Part 1

Hi everyone!  Been busy the past couple months and junk.  But now I'm back and uh, doing stuff and things.

So the past while I've actually been wanting to do a little introduction to programming, because I have a fair amount of friends who will sometimes ask me what it's like to be a wizard and I have to tell them it's not as magical as they think (I mean, it is to me, but I'm also fucking weird).  Keep in mind I haven't had to "learn to program" in ten years-ish, so if anything I try to explain just is NOT making sense for you, let me know somehow!

I can't think of anything else necessary before diving right into the nitty gritty, so heeeeere we go!

First Steps

1) Acquire computer

This is left as an exercise to the reader.

2) Acquire Python

Okay, there's some technical voodoo associated with this.  Feel free to skip the next paragraph if you just want to get to the "how do I hack the Pentagon's wifi" part, which will be covered in the next millennia after the Pentagon of then has no relation to the Pentagon today.

EXTRA EDUCATIONAL BULLSHIT STARTS HERE
First, there are a lot of different types of programming languages.  One of the more common distinctions is compiled vs. interpreted.  A compiled language is one that is taken from its complete sequence of instructions in source code (human-readable instructions for that language) and translated by a compiler into a sequence of machine code instructions (human-readable for exceptionally machine-like humans).  An interpreted language is one with sequences of instructions (which can be called "programs" when talking about either of these types of languages) that are executed one-by-one by an interpreter into machine code instructions.  If you're reading this, you probably don't care about why this is important, but if you're curious, just ask!
EXTRA BULLSHIT STOPS HERE

Python happens to fall under the category of an interpreted language, so when you run a Python program, you're pushing a sequence of instructions through an interpreter, which your machine will run one at a time.  Because of this, you need the Python stuff, which will include an interpreter.  (Windows, get the MSI installer for whichever type of CPU you have; Mac, you only have the installer as an option.  As far as I know Mac OS X comes with Python installed, but I can't guarantee that the version it comes with will support every line of code I'll have here.  Linux users can use their package manager, eg. apt-get or yum or whatever other distros use).

Once you have your installer downloaded, the process is pretty easy.  Just hit "Next" about four times:




Then you just kinda wait, tell anything that pops up "Yes", and you eventually get treated to a "Python has finished installing" dialog.  Afterwards, you should see a nifty new entry in your Start Menu (right).  Give it a click and you get something you've probably never seen before.
Now hold on, before you grab your pitchforks and call me stark-raving mad (true), let me explain.  What I said earlier about Python being an interpreted language?  Well, this is the interpreter.  It is your friend in this crazy world of programming.  This is where you can input those individual lines of code and the Python interpreter will tell you what it thinks of your code.  Now, a lot of programmers will tell you that your first program should be the "Hello World" program, which literally just prints "Hello World!" to the screen.

But fuck those guys.  I'm not here to turn my friends into run of the mill coders.  I'm going to teach you how to do something cooler than that for your first program.

But that's a little bit ahead, because we must take baby steps on our path to automating office jobs.  You should have this Python interpreter open, as signified by the >>> in front of your text cursor.  Now, the first thing you will discover from this tutorial: Python is good at math.  Try it.  Just type in "2+2" (without the quotes), so you see this:
>>> 2+2

It didn't even have to think about it for your answer.  Get a little trickier, give it some multiplication, subtract some numbers, you have a really awesome calculator here.  Now give it some division that won't turn out as an integer.  Like:
>>> 3/2

Second thing you will discover: Python is very bad at capturing intent.  It told you "1", which is not incorrect - it's just not the answer you were expecting because Python, in order to work as deterministically as possible, has to treat some operations different from intuition.  In fact, you'll find this kind of thing in every programming language I've ever worked with that supports division.  So now try typing:
>>> 3.0/2

And it works!  You should get 1.5 as the answer.  If not, check sanity. But with this you've come across, very simplistically and transparently, the programming concept of "typing".  Not what I'm doing in this blog post, but data typing.  The basic idea is that every piece of data belongs to a category, and in order to make sure you don't accidentally blow up your computer, Python has to stay consistent with how it does things to particular types of data.  So when you typed:
>>> 3/2

Python was basically looking at it as "integer divides integer".  The result, probably because removing fractional parts of a non-integer is annoying, is an integer rounded down from the actual result.  Then, when you used "3.0" instead of "3", Python saw "real number divides integer" and gave you a real number.  Now, by no means are these the only kinds of data you can deal with.  Say you wanted to tell Python you love it.  Just do this:
>>> "I love you"

Including the quotation marks.  Python should just spit that right back out at you, maybe replacing double-quotes with single-quotes.  Want to put two strings together?  Easy.
>>> "hello" + "world"

Note the lack of a space in between them, but that's easy enough to fix.  Now, before moving on to other data types, there's something else good to know: variables.

Spare me your lamentations from dealing with variables in algebra, we all went through that.  But there is no solving for them here (unless you're doing mathematical programming, which is fun too)!  The point of a variable is to assign a name to pieces of data for easy reference, storage, whatever.  How do we create variables in Python?  It's very difficult.
>>> x = 2

Python shouldn't say anything to you if you type that.  But then type just the letter 'x' (no quotes).  It tells you the value of the variable!  But...but what did you do here?

So what you did is you decided on an arbitrary label for your variable (here, just 'x').  Then, you gave it a value (here, 2).  That's preeeetty much it.  You'll usually want to have more interesting names for your variables, like currentNote (music) or thisDuder (game character) or countHotLadies (harem management), but that's really up to you.  Also, they are case sensitive in Python and most languages, so 'x' and 'X' are totally different and not tied together unless you make them that way.

Cool.  Awesome.  We have a variable.  Now what do we do?  Well, another thing you should remember from algebra in high school are things called functions, such as sin x and log x* and other such things.  Those basically take a number, represented by x in the common notation, do something with it, and give you another number.

*I acknowledge that, technically, logarithms are not a function but an operation, but this distinction isn't important from the perspective of a programmer.

Why are they important?  Well, with programming, functions are extremely common.  I haven't written more than twenty consecutive lines of code after my first month of coding that did not have at least one use or definition of a function.  You can use them on more than just numbers - you can operate on strings, on lists of values, you can use a function on just about anything you can possibly imagine, including, in some languages, on functions.

One pair of functions that gets a fair amount of use is the str.upper() and str.lower() functions.  Note this notation - the "str." in front indicates it's a string function, the "()" at the end indicates that it's most definitely a function.  The functions, as you might expect, turn strings into their uppercase or lowercase equivalents.  Usage would be:
>>> "hello".upper()

Note that the string you're working with goes in front in its rawest form, quotes and all.  More on that later, but check out the result.  An uppercase version of "hello"!  Sick nasty.

I think that's it for this post.  Comment with any questions, complaints, or do so on my Facebook and I'll try to get to you ASAP.  Happy coding, until next time!

Wednesday, May 29, 2013

Hiatuses are Lame

Hi everyone.  So uh, I graduated college almost a month ago.  I haven't really done anything productive since then.

That's a lie.  I've done a fair amount of programming, some for work (I'm working with an Android team now, heck yes), some for funsies.  My 2D engine is more functional than it was last time I posted, and now plays music dynamically (this is a huge pain in C++, to handle file structures and strings and junk), loads images dynamically, has a resource manager to do those things + look them up when needed, and other stuff.  Plus gravity.  That's fun.

I've also been playing lots of guitar, and (poorly) attempting to redevelop my ability to sing and play.  Nyeh.

Well, okay, I haven't really done much other than that.  Which has been awesome.

Anyway, this post is mostly here to indicate there should be more coming, now that I'm out of my "wow I don't need to do anything" phase and back to starting to do stuff.  Like lots of programming!

Also, I've heard interest from a few of my friends about learning how to program, so my next post might be the start of a basic Python tutorial.  We'll see.  myarharhar

Wednesday, January 2, 2013

Obligatory New Years Post and Stuff and Things

A happy (day late) New Year to everyone out there!  I haven't posted in more than a month because of finals, and I'm lazy, and I'm lazy.  But hey.

I made a few resolutions that are really open ended and stuff so I'm sure to at least make progress on them.  A couple concrete ones have been made, but there's no real reason to share what they are (also, this).  Also, I got a Kinect for Christmas, and Dance Central 3 is awesome.  Just sayin'.

Also also I got a gift card to Guitar Center from some of my friends, which is rather fortuitous (and they totally just knew this, I didn't say a word to any of them.  nope.  not even one.) since I've been in need of a new electric!  Super good times.

Moving along, I haven't done much cool stuff with any of my projects this break.  I kind of started on a level generator that had a couple rough spots, such as it deciding that when it generated sand, it would actually put water there.  It was kind of stupid, but I put it in its place.  *puts whip away*

I also started working on a script to pull apart dance videos and generate wireframe models of them to make some stuff easier.  It's not going so well because image analysis algorithms are pretty crazy.  But we'll see where that goes.

Now, I can't just leave my dear readers with nothing but vague updates on this post.  So I'd like to take a couple minutes (just sit right there) and I'll share a couple pieces of technology that have made me want to punch babies this break.

Ogre 3D


Ogre3D is a 3D graphics engine primarily made for C++, though wrappers have been made for other languages such as Python.  It's been used for a lot of games, including the Torchlight series, Pacific Storm, and others (here's a list that I'm sure isn't comprehensive).  So it's obviously pretty useful.

What's wrong with it then?  Well, the introductory tutorial is was inconsistent yesterday...it uh...it seems to be fixed today.  That...welp.  I'll try it again when I get home and see if it's still wonky.

Anyway, it tells you to download a zip file that has four files: BaseApplication.cpp, BaseApplication.h, TutorialApplication.cpp, and TutorialApplication.h.  They're to be used so you can just get all the compiling and stuff done with a very basic project, which is fine.  The problem I had was that the same page with the download was under the impression that the zip file had a whole different set of files, in a marvelous subdirectory structure of ./include, ./res, and ./src.  The four files I downloaded were just kinda there, splayed out in their glory.

This annoying page also said some .cfg files had to be moved from the OgreSDK directory itself, which is fine, but it didn't mention the .dlls that also needed moved.  And THEN the .cfg files, which were basically lists of resource locations, were all based on the SDK directory and had nothing to do with the project structure that it had the user set up.  At this point I got so frustrated (partly due to working in Windows, I kind of use bash scripting religiously now) I just played more KotOR, because I decided to run through it for old time's sake.  Fuck kath hounds.



Ubuntu's Unity


Unity is a desktop shell (or something, I'm bad at UI terms) that aims to bring a more innovative user experience to the forefront of "easy Linux", Ubuntu.  I'd say "read the picture", but it's about as unintuitive as Unity itself.

Don't get me wrong, I absolutely love Ubuntu.  I've had every computer I've owned dual-boot it since freshman year of high school because it's easy to install, has always looked pretty, and it comes with a lot of stuff so I don't have to sit around running "sudo apt-get update && sudo apt-get install only-getting-used-once" for absolutely everything ever.  It IS easy Linux, and I am okay with that.  If I need something lighter, I have Crunchbang on my laptop as well.  It still doesn't run Dota 2, so I'll need to wait for that before I can just never use Windows again.  

Even so, I can not get used to Unity.  You can't move the launcher dock.  You can hide it, but that's worse than the old GNOME option of putting it on a panel.  The lack of panels forces you to relegate your shortcuts to the desktop or deal with the incredibly obnoxious Unity launcher.  That, or you can enjoy searching for your desired program every time you need to run it.  Sure, there's Alt+F2, but it's hard to beat the "point-click" simplicity, especially if you're not a frequent Unix-er.  It's also bloated and alt-tabbing takes noticeably longer with Unity than it ever did previously.

So instead of dealing with it for the rest of the work day until I get home to put a new distro on this guy, I downloaded Gnome 3, which basically does what Unity was trying to do, but it's so much...better.  Rather than the pervasive launcher that makes you carefully edge your mouse to that thing you need to click on the left of the screen, you get an Overview that pops up when you mouse to the top-left corner and shows you...pretty much everything you might need.  Your current workspace windows, your Favorites launcher, windows on other workspaces, a notification space at the bottom-right, probably some other things I'm missing.  

But anyway.  Rant has gone on long enough.  Long story short, if you decide to get Ubuntu and don't like Unity (if you do, that's fine, I personally just can't stand it), try Gnome 3 before discounting Ubuntu for good.  It's reeeeeeally pretty.

I should have some actual updates on projects next time.  Until then, my readers!
/* Custom junk */