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!

Monday, November 26, 2012

Life Moves as Pawns on a Board...And More!

Hi everyone!  It's been a while.  Same excuses - lazy, busy, too much Dota, you know how it is.  I hope.

So anyway, I've been doing some things.  Among other stuff such as guitar and Halo 4, I've been working on my code projects and made a bit of progress on some stuff!  Let's get to it.

As you might recall, my chess clone, last time we visited, was completely stupid.  The pieces knew where they could move, but upon trying to do so they made a pawn where the move would go. There were a number of pawn-based revolts on the basis of their value depreciating from the influx of pawns in the market.  But it's fixed now, as you can see.  Yes, there are also moves that shouldn't be there off the board, but I'm still working on that (if you're curious, it has to do with capture moves being a little wonky with my setup).

So there's that.  It's one step closer to a functional clone that I can actually do stuff with, and that's what matters when I can't commit to this stuff full-time.

I've also made some progress on my corpus study tool.  I've got the framework for discovering probabilities of particular sequences, as shown at left.  To explain this, what we have are sequences of words (with an intermediary separator, don't worry about that) in pairs, followed by a final word and the number of times it occurs after the pair.  You might also notice I'm clearly using The Call of Cthulhu as my test corpus.  The next step is to aggregate by initial words and then make a list of all the possible finals with their counts, and then finding a final probability of some sequence of three words ending with a word can be determined pretty trivially.

Not sure what I'll do with it once I finish that step (which might happen tonight), but it'll be more or less complete for English/Latin symbol languages sans doing a bunch of stress testing on more Lovecraft and making it not an aesthetic mess.  Then I'll have to work on a method for finding word boundaries in languages such as Japanese or Chinese, which will be a huge mess.  Looking forward to it!

The last project I've been working on is the guitar project, to which I haven't added much.  I just made it possible to do lookups on multiple notes and find scales containing all of them (shown at right).  I've also been trying to figure out how best to implement chord lookups, which in and of itself wouldn't be too hard, but I want to be able to find chord variations (like an Asus4 or Gm7 or however the spirit moves you) and show them (and thus, all possible positions) on the virtual fretboard.  I'll also at some point need to start making this guy graphical.

So, that's what I've been doing, as well as Minecraft and Dota, and I'm probably going to use my 2 free months of XBL fairly soon to kill scrubs in Halo, if anyone ever wants to join me on that just lemme know.  Until next time!

Tuesday, September 25, 2012

Some Day I'll Stop Being Dumb.

Maybe today.  We'll see.  I need to stop taking extended breaks.

SO ANYWAY.  Been busy.  College of Engineering had a career fair last Wednesday, so I showed up all hella snazzy with forty resumes, handed out 18 and can only account for handing out fifteen.  Hm.  Could be worse.  Did get an interview on Thursday that they called me back to request an on-site, which was rad!  It'll be my first experience in actual Cleveland!

Have also been playing a fair amount of guitar lately.  And with that, a bit of working on my guitar notes program.  I made a menu interface!  So now it looks like this:


Also pictured: A segment of my Younha wallpaper because my terminal is partly transparent, and a bunch of random secret folders on my desktop, all accurately labelled.  So in addition to doing what it has always done (show scales), it also lets you choose which type of scale you want to see, and as soon as I'm done writing this it'll let you change the number of frets, too (small oversight).  I also need to get the "octave" out of that display, but whatever.  Of course, typing in either 'q' or 'Q' will exit the program from that screen.

So that's been that guy.  My corpora analysis...hasn't really gone anywhere, but I did recruit a dude to help me with it, so it's on Github.  If you're interested in helping with that guy (seriously fucking EVERYONE I talked to at the career fair wanted me to explain it, and after I did they were all like "how about I give you my card, and you apply further, because this is really cool".  shit looks good), drop me a line (sthara216 at gmail dot dot dash com, but minus the dot dash) and we can talk about it more.

I'm also now technically an officer for the Japanese Student Organization and I'm helping them with technical stuff.  WHOO.  Yeah that's not really interesting news for you readers (all half of a person).  Whatevs.

So anyway, going to try and update this more (eg. at all), so if I stop, call me a bitch and tell me to write more blawg.  I won't be offended.  UNTIL NEXT TIME...

Sunday, June 24, 2012

HAYLE, it's about time.

Yeah, soooo, I've kind of been absent for a while from blogging.  But worry not!  I have reasons!

I'll sum it up as school sucks.  Thankfully my last year of undergrad won't be a mess of overachieving - just normal achieving.  Not necessarily easy, but whatever.

So anyway, I didn't have all that much time for personal projects last quarter, besides playing Dota every once in a while, and so I didn't have much to post about on here anyway.  My other blog, FUCK, was a little too time-consuming for me to continue, so I didn't post on it either.

But now it's summer, and even though I'm working while also taking summer intensive Japanese, I have more free time!  Which means more blog posts for my beautiful friends (like you) to read!

I've started a few things since last quarter ended.  I'll just sum them up here, since I don't really wanna get too into the deets.

  • Started working on a website again.  It sucks and I'm totally aware of that, because I'm a programmer and am thus totally awful at aesthetic design from scratch.  It's disastrous.  But I should probably have one by the time I graduate so I look a little more employable.
  • I got tired of losing the pieces of paper that I wrote down major scales on for playing guitar chords/improv, so I've started writing a Python script to do it for me.  Currently it looks like this:

Kinda hard to see from this, but it has a textual fretboard representation, the scale itself, and every single position on the fretboard belonging to that major scale (and subsequently, to each mode of that scale, which also solves that problem I got tired of doing).  I also wrote a nice looking Python list-pivot function because of this little project.  I want it to do more, like display all the major scales (it doesn't like trying to deal with flats, but sharps work just fine - I know they're more or less equivalent, but I'm a bit of a perfectionist when it comes to code projects).

I've also kind of started writing again!  I don't have any substantial text, but I might start another blog for no purpose than sharing my writings if I start writing enough of them.

That's pretty much all I've been up to lately.  As always, comments and the like are appreciated in the boxes below.  And expect a Fruncoki entry sometime this week! 

Sunday, April 15, 2012

WHERE HAVE I BEEN

I'm such a lazy whore.

Anyway, for anyone who's been keeping up with this at all (probably an empty set), I've been really lazy. That's really it.  Also had a fair amount of work to keep up with, but I've also played fucktons of Dota, so I don't really have an excuse.  Oh well.  Whatevs.

So what's been going on in the world of my chess clone lately?  Well, I have done a few things, including but not limited to:

  • Refactored out the move logic
  • Generalized said logic to both players
  • Validating whether some particular move is indeed legit
  • Screwed up my board logic, and subsequently fixed it
The problem is, they don't really do anything at this point.  For some reason it doesn't actually move pieces when the player selects a valid move.  It just kinda...stops doing anything.  It's really strange.  This is basically the priority when I do work on this.

Logic generalization is irrelevant in the face of the previous issue.  And black pieces don't have moves for some reason.  That'll come after I figure out why pieces don't move.  Whatevs.

So uh, yeah, wasn't much here other than a basic update and a quick "WHERE DID I GO" kind of thing.  Should have more next week!
/* Custom junk */