hayley 365


today's work: more pythonista

13 Jan 2014

2014-01-13 02:25:17 -0600

I have to be frank that yesterday and today have felt a lot less productive.

For one, since I'm working in Python, and I'm super rusty in Python, it literally is just less productive for me at the moment.

I think the larger problem though is that I'm doing less clearly defined tasks.

Like, today, I can't specifically say "I finished X". Instead, it's that I worked more on my public library scraper script. I made it better bit by bit. So I don't have the feeling of a job well done because the task was too loosely defined. Something like that.

I am definitely enjoying Pythonista. As someone who never wanted to drop the $100/year in order to develop apps for ios, I find it incredibly fulfilling to actually be able to write something that I can run write right there on my ipad/iphone. I still really feel like this might end up being the thing that gets me into doing app development, because there's huge power in a prototype. Even a bare prototype can really push you because you have something you can actually touch (or click or tap or whatever).

why I love Ruby

Going back to Python has been eye opening for really spelling out to me why it was that I had a fling with Ruby and then never came back.

thinking left to right

I have this problem too with JavaScript.

I hadn't even really realized how much I love this, but in Ruby you basically always get to be thinking from left to right. The perfect example to me is converting between integers and strings.


"7 cm".to_i
=> 7
=> "7"

In Ruby, both methods involve starting with your data on the left side and then manipulating it as you go (left to right thinking).


=> "7"
parseInt("7 cm")
=> 7

So for converting from a number to string in JS, you get to think from left to right, but for converting from a string to a number, you have to think of the function name first. In my mind, you're basically having to think from right to left in that case.


=> 7
=> '7'

# want to actually do the "7 cm" as before? it appears you need a regex
int(re.search("\d+", "7 cm").group(0))
=> 7

Lastly, there's Python. Python is right to left in both cases. Frankly, I feel this is better than JS because at least they're consistent with the direction in which you need to work.

But in all cases, I greatly prefer Ruby's way of handling things. Basically, I enjoy writing crazy one liners where I can actually start with something on the left and filter it through a whole bunch of methods and get some interesting result without having to constantly backtrack within the line to prepend a function to something. It doesn't match my way of thinking about things.

string interpolation

Unless I'm missing something, this is another big win for me for Ruby.

Python appears to only have the C-style printf style of string interpolation. I'm sure there's probably plenty of template options available, but as far as I know none of them are part of the standard library, and given that Pythonista is going to be my main python tool, then I need something that Pythonista has.

You can do printf style stuff in Ruby too, but I tend to hardly see it written that way.

# python
print "%s\t%s" % (some_var, another_var")
# ruby
puts "%s\t%s" % [some_var, another_var]
# idiomatic ruby
puts "#{some_var}\t#{another_var}"

I guess this would be another case where you get to think from left to right. Like with the printf style, you have to think about the formatting of the variables on the left side and then re-remind yourself which variables are what when you go to right the tuple (I think that's the name for the thing in parentheses).

In the idiomatic ruby, you get to think about the formatting and put the variable names right then and there.

Oddly enough, when it comes to writing regexes, I don't think I've done a single named regex in Ruby. Part of that would be the fact that 1.9 was still pretty new when I started using Ruby (if I remember right, Sinatra was not yet compatible with 1.9 at the time I started using Ruby), and named captures are apparently a feature of 1.9 and above. So there's that.

But really, I find named capture regexes horribly cluttered and hard to read. So apparently, I'm not always a fan of left to right thinking.

code style - camel case, snake case, pick one

I prefer snake_case. If I were German, maybe I'd prefer camelCase. The German language capitalizes all nouns. In English, few things are capitalized, so there's just something about camel case that offends my sense of balance. It just doesn't look right.

Python seems to be mostly about snake case, but I've seen plenty of major libraries (okay Beautiful Soup is the only thing I can think of) that seem to be in love with camel case. It just seems inconsistent.

Frankly, to me, I'd prefer the inconsistency over something like JavaScript where camel case appears to be the official standard.

for loops

Once upon a time, like circa 2005 when I was initially playing with Ruby on Rails, I was weirded out by what I think are called "anonymous blocks". Whatever it is, it's this kind of code:

my_array.each do |item|
  # do something

Ya know, as opposed to the for loop way of handling it:

for item in my_array:
  # do something

And if you're paying attention, you might notice that I wrote that previous block in Python. And that's because it has been so long since I've actually used a for loop in Ruby that I would've had to look up the syntax in Ruby (I think it's roughly the same but probably ends with end, but I couldn't remember).

As odd as I found the anonymous blocks in the very very beginning (it was actually after my dip into Rails that I ended up with Python and Django for quite awhile), apparently I don't miss them at all.

And knowing what I know from my past few days with Python, it's probably again the left to right thinking. You start with your initial data, then you think of what you want to call the items that you're going to pull out of it, so on and so forth.

but really, Python is pretty cool

Ruby is definitely the language for me, but Python is pretty cool too.

Frankly, I think part of me was hesitant to get back into Python either over confusion because of how similar it is to Ruby in some ways, or out of fear that I might actually want to switch everything back to Python.

So far, I definitely don't want to switch to Python full-time and I seem to be able to juggle the two languages in my head without too much of a problem. Well, I suppose that implies that I actually have some Python code to store in my head. Which isn't really true at this point.

And frankly, I tend to be a programmer who would be absolutely lost without access to one or both things:

  1. access to previous code I've written
  2. access to the internet

Because I store very few things in my head.

Take, for instance, datetime parsing. I get tons of practice with doing this for Cyclocane, yet somehow I have to google things every time I want to parse a date. Perhaps I need to write myself a tutorial.

Tomorrow though, I think I need to work on something other than pythonista. A task more clearly defined.

I'm sure I'll still do something in Pythonista though. It just shouldn't be my official work for the day.