While I while away the javascrours

This post is mostly reference, with some thoughts on structure at the end!

Pythonic

while i < 3:
    print "still less than three!"
    i = i + 1                       # i += 1

Javascripture

var i = 0;

var whileloopfunction = function (counter) {
    while (i < counter) {
        console.log("a way more complicated way to do this, it seems!");
        i++;
    }
}

whileloopfunction(3)

Not sure exactly how to simplify this. The reason that I’ve made the parameter counter is because it seems that I must create a function with a parameter so that it can be called.

Ah – I just tried it taking the counter param out altogether and made the while loop’s condition (i < 3), calling whileloopfunction() with no argument and it works just fine. Experimentation! Sliiiightly simpler than previously, though it was a good lesson all the same.

This is looking so much to me like a simplified object, at least, simplified from the perspective of Python. This statement may confuse people – after all, Python is a more human-readable language, right? Well, its objects are a mess if you ask me, though the rest of it is lovely. I was once told that you start to really understand OOP when you learn a second OOP language. I think that is becoming true. What a relief – it's been frustrating not knowing.

Ah, one last thing – I'm pleased to present these posts to you, now written in Markdown instead of the hammer-where-you-need-a-toothpick html formatting! Thanks, WordPress!

javascript and python’s range()

So in python, you can use a function called range() to (conventionally – I know there are other uses) easily iterate over, yknow, a range of numbers or through a list/array/yaddayadda. It works like this:

for i in range(13): # you can also limit it on the lower bound, a la range(7,13), 
    print i         # but be ye wary of yon fenceepostee

While going through some javascript tutorialling, I found a typical learner problem that I found later that I’d been approaching the wrong way, but if you’ll bear with me & restrain thyself from punching angrily through your computer/rotary telephone, COME WITH ME ON THIS JOURNEY:

For a Rock Paper Scissors game, lesson 11 or 12 of the Functions lesson of Codecademy‘s javascript class, it asks the student to use a randomize function (Math.random()) to call “rock” when the first third of the number, “paper” when the second, and “scissors” when the third. My FIRST thought was to use a range function, like if i in range(.33) and if i in range(.34,.66)! So I tried a couple of different syntactial approaches that seemed javascriptey, they didn’t work, so I went a-googlin’ (how you do) and found the following solution:

Array.apply(null, Array(5)).map(function (_, i) {return i;});
[0,1,2,3,4]

I know I’m new to javascript, but that is honestly barely parseable. I’m not here to wail about things that one language does that another doesn’t, but this was a difference that frustrated me – – until (and you patient few, I know you’ve been waiting for this) I realized that there’s another way to solve this problem! And that’s the beautiful thing about programming, particularly learning different languages. I remember using this syntax in other learning situations, back in the day when I first learned Python. The resolution is to use the (javascript) format of:

if (i < .33) {
    return "computer chose rock";
}
else if (i <= .66) {
    return "computer chose paper";
} else {
    return "computer chose scissors";
}

which looks (save the curly braces, ;, & s/else_if/elif) nearly the same as pythonic syntax.

MORAL OF THE STORY: there are many ways to do many different things! fabulous!

Forever Django

Yessss, I’ve found another giant tutorial that I am so excited about & have heard such good things about. How to Tango with Django! Hooray! Gonna go through it, lesson by lesson, just like LPTHW. I’ve already gone through the official Django Project tutorial a handful of times and have gotten a lot out of it, and I did another one that I’m not crazy about but that explained something that hadn’t been clear.

Oh, and the job? The job is great. I’m orchestrating a bunch of independent projects that all need to come together at the same time, and it’s pretty great to be the one pulling so many of the strings.

Aaahh aaaahhh aaaaaaahhhh I have to go, I will write my first entry on this soon!

EDIT: So I got to page 6! It’s awesome. But I’m going to move on anyway. More in the next post!

Edits to Beachmeals and more with Rinance

I am working on several projects right now, namely a new version of beachmeals as well as two new versions altogether of rinance, a personal finance program fitted to python (pynance already exists, and of course – what a cute name).

Beachmeals is probably where it’s going to stay until I talk some more with a mentor friend of mine, but Rinance is presenting a pretty good challenge to me. The first version, well, it just doesn’t work, ha ha! Looking back I’m not at all happy with what I was trying, there. So a weekish ago, I started over and it’s just top to bottom with a handful of conditionals, in the fixinto branch. Notice the to-do list at the top! The distribution of funds goes ok, but I still can’t quite figure out the importing of the file, turning them all into variables & manipulating them. No hints please! I know it is an easy answer & I’m still working on it πŸ™‚

Because the to-do list in the fixinto branch is enormous, I started a whole new file – I wasn’t happy with the “functional” look of the fixinto branch, so, ha ha! I started over again! this objectish branch now contains a program map and the first piece of that is in there & complete. To get started, the program asks the user if the money'll be going in or out & then sends the user to the appropriate function based on their response. Easy-peasy, coming along!

Jobbed!

just to let y’all know, I have a job! wow! it can actually happen, to go from zero computer knowledge to some (what’s the axiom, “the more I know the more I know I don’t know,” right?) and then BE EMPLOYED is actually possible. seriously, I started with so little. and I have so much more to learn, good god. I want to explore the command line more, as well as security, web frameworks (hello django!), JSON, databases, vim, a different terminal, another linux install, a home server, learn about big people in tech & watch their tutorials & learn, AND maybe even do some tutorials! I don’t know what the tech community NEEDS, but I think it could be handy to find out what kinds of tools are out there to help “level up” from learning Python in whatever essential way most learn it.

Me, I learned Python in IDLE on Windows 7, a program that will get ANYONE up and running in 20 minutes, regardless of operating system. and now I operate nearly everything from the command line in a Linux Ubuntu 12.04 dual-install that I figured out & did myself. I use sublimetext to edit code (and a little bit of nano if it’s something small) and irc to troubleshoot with local coding buddies, and both virtualenv and miniconda to create environments on my machine to play around with. I plan on updating to ubuntu 14.04 in a few days, and the challenge that that will bring.

so! overall message! keep going!!

EOY Recap and 2014 Goals

I love end of year posts, like Scalzi’s and probably others whose blogs I do actually read but can’t think of, ha ha! This has been the year of transferring my nerdiness to generic pop-culture things like reading a ton of scifi and guzzling Star Trek, Doctor Who, Firefly, etc, to some real gritty stuff like some actual facility with computer systems. Very exciting.

I have made a zillion miles of progress and Big Life Decisions this year, and programming-wise, were and are incredibly impactful. My goal that I have been working toward for the last few years has been to teach high school math, I figure I have enough of an aptitude and teenagers are (don’t panic!) actually pretty cool to work with. And I knew I needed to be studying toward something that would get me an actual, adult, non-entry-level job, the contrast to which I worked in for four years, and let me tell you, hearing “well, we think you do deserve a raise, but there’s just no money” is pretty hard to hear/believe when you work for one of the biggest and most successful companies in the world, earning record profits quarter after quarter even in the most difficult of times (see 2009, when I saw coworkers’ 401(k)s slashed to 20-40% of what they had been).

Rant over, getting back to it, part of the coursework for the high school math teaching program at one of the graduate programs I was looking at included an introductory programming course. As I’ve mentioned elsewhere, this really struck me and I joined a ladies’ programming group and stuck with it. After hearing that one definition of a master is 10,000 hours of work put into it, I tried to come up with a figure of how much I’ve put into python and related projects, and I think I’m right around 250h, give or take. So I have a long way to go, but I’m very pleased with having allotted that much over the course of a year. It’s 250 more than I had by last year, and that’s the only metric that’s important right now!

And now, for your handy-dandy list-format of what I’ve started and accomplished this year, under the cut. Wahoo!

Continue reading

Lesson 25

Here we’re getting into writing programs to be used, actually used, on the command line, and that is so exciting. It’s been almost seven months since I made something to actually use, and while I have no real need for a sentence sorter, to have written a program that I can then manipulate in the py shell is REALLY REALLY NEAT, to me.

At first, I struggled a bit with some of the definitions, but I really enjoy separating the definitions from the actual what-you-want-it-to-do-ness of the program, as it were πŸ™‚

Seeing this:
def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word

def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word

was pretty exciting, as I recall the idea of “pop”ping from the approximate third of Douglas Hofstadter’s GΓΆdel, Escher, Bach: An Eternal Golden Braid that I read, in one of the interchapter dialogues. Pushing and popping have a very clear analogue in computer science, and while, looking at this particular exercise of Mr Shaw’s, I think that it is not actually the same idea, ha ha! it is still a nice callback even if unintentional and unrelated!

One of the Common Student Questions for this exercise, too, was “what is the difference between print and return, and his answer is somewhat crummy, emphasis mine:

When should I print vs. return in a function?
You need to understand that print is only for printing to the screen and that you can actually do both print and return a value. When you understand this then you'll see that the question is kind of a pointless question. You use print when you want to print. You use return when you want to return.

So I do not really have a clear idea. It seems like once the function has done f(banana) to the stuff, you can either return the result or print it. return seems more verb-y, but the end result seems identical. I will have to read about this!

As a whole this was not a challenging exercise, but it was really fun. I love that Zed Shaw focuses on being able to manipulate python via the structure-level command line rather than via an IDE, like the very popular IDLE which I will not link here. I’ve never imported a function in the shell to play with, and I feel like the possibilities have just expanded. Yeah!

Lesson 21

Moving along, here! This lesson was mostly a refresher on function ordering, for me. At the very end, there is an extra credit portion where he recommends resolving a nested set of functions by hand before running the program.

print "Here is a puzzle."

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print "That becomes: ", what, "Can you do it by hand?"

I knew that it was necessary to resolve inner parentheses before moving on to the next inner set of parentheses as just a giant composed function, but once I got to the non-commutative operations (ie, subtraction and division, in which order of operation matters) I flubbed it up a bit, and came up with a different answer than when I ran the program. Then I examined it a bit, and with a bit more analysis (that’s an overly heady word for it, ha ha!) in the form of the following:

age = add(30, 5) #35
height = subtract(78, 4) #74
weight = multiply(90, 2) #180
iq = divide(100, 2) #50

and then resolved the commutativity issue, I came up with the same answer that he did! woo-hoo!

Woo-hoo! Another lesson down!

Lesson 20

At first glance, this exercise appeared simple for me, until I got into it a bit. First,

def print_all(f):

That f throws me! I tried to see in the code what it was doing, but I couldn’t, and besides, the point of each lesson is to teach NEW ideas, not expect the student (that’s me!) to understand the language of code intuitively – otherwise, hey, who needs lessons?

So I didn’t understand that right at first, but that’s okay, because further on, we’re given:

print "Let's print three lines:"

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

Hey! Awesome! Given my experience with looping, whether WHILE or IF and maybe some other type I am forgetting right now, I can see a mile away that this can be WAY cleaned up per my specifications! This hugely makes up for the not-knowing of the utility of f previously!

So I go ahead and run the program, something that will print out a text file line by line as well as “rewinding” it, and then I set immediately to cleaning it up:

i = 0
current_line = 1
while i < 3:
print_a_line(current_line, current_file)
current_line += 1
i += 1

Woo-hoo! Nine lines condensed down to six, and while that may not sound like much, a) it can probably be condensed more, and b) it’s a much simpler process, at least for me, to follow. I am thinking I may not even need the i indexing!

Let’s play around, then, a bit. I’m going to take out the i index and make the while loop dependent on current_line rather than on the index.

TOTALLY WORKS, OH YEAH, CHECK IT:


current_line = 1
while current_line <= 3:
print_a_line(current_line, current_file)
current_line += 1

FOUR LINES! So slim!

One last thing: re the f code question from before, well, it was addressed by Mr Shaw in the Common Student Questions portion! The f acts like a VHS-style “read head,” as in, the reading of the file stops at the line you read, and then our defined function rewind moves it back to the position we specify, in this case, the 0th byte, a.k.a. the beginning of the file!

EXERCISE CONQUERED!

EDIT of 13 October 2013: It occurs to me that altering variables is not the best practice for looping. Rather than adding values to current_line, it is a better idea to create an index like i and set current_line equal to the index, and then let the index do all the heavy lifting! Then, if I want to call the variable again, it will be unchanged! Woo-hoo!