Shawna C. Scott

Learning to hack, one day at a time

Menu Close

Category: Portland Code School

Regular Expressions

When I was first learning about what regular expressions are and how to use them, I was having a harder than average time finding good resources to explain the basics. Luckily for me, I have an amazing community that came to my aid. Erin Call wrote this awesome post, and Cory Kolbeck wrote this one explaining the bones of using regexes.

I wanted to write about regexes, but the write ups from Erin and Cory are far and away better than any intro I could write. So, I wondered for a while what I could contribute to the discussion. Then a friend who just graduated from Epicodus mentioned that she understood how to construct regexes (use Rubular!), but not really how they would integrate with an actual program. And I have done that! So, here is a description of how my pair partner and I used regexes as an integral part of solving PCS’s code challenge 4.

For our code challenge, we had to parse and clean a text file of data in this format:

[prefix] [first_name] [middle_name | middle_initial] last_name [suffix] phone_number phone_extension

where anything in square brackets is optional, and then appropriately assign the parts that are present to the correct column in a CSV. We used OptionParser to create our own flags to run a command line script that would do all this.

The way my pair partner and I ended up solving this problem was by creating two scripts. The first script read in the raw data, stripped off the first word in every line of that data and created a reverse sorted histogram*, and then wrote that histogram data to a CSV. We then examined the output and determined which of the words were legitimately prefixes. This is the script we used:

histogram = Hash.new(0)
 
while line = gets
  prefix = (/^S*/).match(line)
  prefix = prefix.to_s
  histogram[prefix.to_sym] += 1
end
 
histogram = Hash[histogram.sort_by { |prefix, count| count }.reverse]
histogram.each { |prefix, count| puts "#{prefix} #{count}" }

We needed to do the same for the suffixes, so we had to split the raw data into the name portion and phone number portion in order to expose the last word of the name section. At this point, we used a regex to match the tab whitespace separating those two parts of the data, saved the first part to a variable, then ran it through a similar script to create a histogram for the suffixes.

Once we’d done that, we set up OptionParser to take the prefixes and suffixes files as input, used those lists to break up the name parts of the data, then used digit matching regexes to both match the existing phone information and reformat it in a consistent way. You can see the source code for the cleaning script. Please forgive our horribly verbose code. Hopefully I’ll get around to refactoring it soon, but for now, done is better than perfect.

So, hopefully this will be helpful to someone looking for some way to actually use regexes in their code! If you feel like playing around with the scripts we wrote, the regexes therein, and OptionParser, you can clone the project and run it in your command line. In your terminal, follow these steps:

shawnacscott in ~/Code on master #%
+ git clone https://github.com/shawnacscott/pcs_code_challenge_04.git
Cloning into 'pcs_code_challenge_04'...
remote: Counting objects: 128, done.
remote: Compressing objects: 100% (86/86), done.
remote: Total 128 (delta 54), reused 103 (delta 32)
Receiving objects: 100% (128/128), 382.55 KiB | 541.00 KiB/s, done.
Resolving deltas: 100% (54/54), done.
Checking connectivity... done
 
shawnacscott in ~/Code on master #%
+ cd pcs_code_challenge_04
 
shawnacscott in ~/Code/pcs_code_challenge_04 on master<>
+ ruby clean.rb --prefixes prefix_words.txt --suffixes suffix_words.txt --input raw_customers.txt --output customers.csv

*a hash where the first word is the key, and the value is the count of how many times that word is present.

Portland Code School, Month 2

The confluence of my horrible default theme, needing to troubleshoot upgrading/downloading anything WordPress related, as well as, you know, all the classwork I’ve had conspired to make it easy not to keep up here. I’m sorry for that! Luckily, thanks to a friend at Hacker School, I got the poke I needed to get back over here:

You may have noticed that my theme is not horrible anymore, that my links are prettified, and that I’ve disabled all comments, in addition to actually writing. If I keep going at this rate, I’ll be blogging from my homemade space station in no time! In all seriousness, though, what I hope to do now is write something every day. What I will probably achieve is something every week, but I’m shooting for every day. As you can see from above, poking at me will get me to do things, so feel free!

Last time, I gave you a quick and dirty rundown of where we were in the Portland Code School: Ruby on Rails class, so let’s keep a good thing going. In week five we finished up our Sinatra app and our intro to routes. We also cleaned up some of our last vanilla Ruby learning tools, finishing up the Codecademy Ruby track, the Treehouse Ruby deep dive, and Ruby Koans.

Week six had us diving into Michael Hartl’s Ruby on Rails tutorial, and getting a brief overview of database structures and design. We’re on track to finish up Hartl by next Monday, and I have to say, thank goodness. Personally, I haven’t liked it overly much. It’s a great bird’s-eye view of a lot of different built-in Rails functions, but because it tries to really hit everything, it lacks a certain depth, in my opinion. It’s been a slog for me. Once I finish, I’ll write up a more in-depth overview of my thoughts on it.

In our seventh week we kept working through Hartl and started our fourth code challenge, learning more about scripts, regular expressions, and Ruby’s OptionParser class. That continued through week eight. A lot of us PCS students were a bit confused, I think, about where we were heading with this one, but once we got into the groove of it, I think we learned a lot. I love that we now have an understanding of regular expressions (which are awesome, and totally not scary!), a taste of what’s necessary to clean and parse incoming data, and a framework for writing scripts to automate processes.

As an aside, the OptionParser class has some terrifically bad documentation, but it’s worth it to slog through. It’s quite a powerful and easy to use (once you eventually figure out how) little tool. It allows you to create your own command line flags and do all sorts of fun manipulations of files quickly.

Week nine is where we are now, and this week was all about getting us ready for our capstone projects! Can’t lie, I am excited for this. We actually pitched our project ideas last week, but this week we wrote our briefs, user stories, wireframes, and page flows. The tl;dr is that I’m making an ally email moderation app. It will allow people who receive a lot of hate mail to filter out harassing messages and let their trusted allies catalog them. The allies can then immediately alert the intended recipient if there are any actionable threats in a message, and log all the rest.

I’m a little nervous, since I’ll be writing this app on my own, and it will require a lot of different and somewhat complex Rails functionality. I’ll need to be able to use encryption, authorization, authentication, and any and all general security chops I can muster to make this a thing that could be used some day. I’m confident, though, that I will learn a huge amount of practical Ruby on Rails knowledge by implementing all of these things, though, and that is really thrilling!

What I’ve Learned So Far

The days (and hours, and weeks!) are flying by now that I’m able to make learning and coding a priority. I can’t believe my time at Portland Code School is one-third over!

I haven’t been as diligent as I would like to be about blogging here, so I wanted to skim the surface, and make sure I’m keeping some perspective on how much I’ve learned and how far I still have to go. So, here’s the run down:

We started out with the basics in week one. Getting our environment fine-tuned, installing things, learning about version control, and getting hooked in to the Portland developer community by leveraging social media and local user groups. I had already gotten a lot of those things done in the weeks leading up to school, which gave me time to focus on Treehouse and Ruby basics.

In the next week, we got our first code challenge, and I had a lot of fun finally getting in the mud with some actual Ruby. Well, actually, I had a lot of hair-pulling and late nights reading and watching screencasts to force the basics of methods and classes in my brain. But fun too! We learned to create classes and methods and started working with simple internal data structures like arrays and hashes. We built our knowledge base on other basic programming constructs like loops, iterators, and branching, as well as working on our git workflow to learn more about pair programming and working with remotes. We started getting our feet wet with the concepts of debugging and test driven development, too!

Our second code challenge (and third week) introduced us to modules and extending the Kernel, and worked to mentally reinforce the amount of room and flexibility that Ruby has. Everything really IS an object! Unit testing became an integral part of writing our code. We continued the basic introduction to Ruby concepts we’d been working through.

Currently, we’re on code challenge number three, and about to head into our fifth week. We jumped headlong from the end of our Ruby concepts (procs, blocks, lambdas, libraries, and core!) to building a basic Sinatra app and manipulating HTTP requests. We’ve gone from taking stairs two at a time to four at a time, and I can only imagine we’ll be leaping whole flights by next month!

Looking back, I’m so excited to see all the things I’ve been able to encounter, struggle with, and then finally understand. It has given me a lot more confidence in my ability to learn. Now when I encounter something complicated and foreign, I feel more often like the reason I don’t instantly understand is that I need to read and ask more questions, rather than thinking that I must just be inherently flawed and unable to comprehend.

I don’t have any illusions that I will come out of this program with the ability to write brilliant code as an island. But I am confident that I will come out of it with well-developed tools to write what I can, collaborate well, and keep learning voraciously. It seems to me, those might be the best tools I could have.

Community is Everything

This past week has been a whirlwind. I’ve had tons of demands on my time from my personal life, on top of watching the firestorm that has been the Ruby community on Twitter these past few weeks*, and trying to learn ALL THE RUBY for Portland Code School.

I have to say, in a week where community has not been safe for many people*, I have never felt more supported. The men and women who are part of my network of friends who are also in tech have gone above and beyond to help me learn and grow in the ways that I need to succeed.

This week, I struggled mightily in understanding the chain of events in Ruby. Classes and methods and instance variables had me all tied up in knots, and I was stuck. A friend directed me to the online overview of Chris Pine’s Learn to Program, and I finally started to trace the threads. I’ve now received the updated, paperback version (as well as Eloquent Ruby!) as a birthday present, and I can’t wait to dive in! The next day, another friend lent me her copy of the PickAxe book by Dave Thomas.

Later, while trying to work through the Code Challenge this week from Portland Code School, I got myself all twisted in knots again thinking I needed to use regular expressions. Turns out I didn’t, but as a result of mentioning I had had trouble finding good resources for beginning to use them, three different members of the community started writing blog posts to fill that need. So far, Erin Call, and Cory Kolbeck have both written up some awesome stuff about them, which I still need to carefully read and review!

Then this past weekend, I hacked on stuff with other awesome people for seven hours in various parts of town. To be able to have so much knowledge and such generous giving of time and expertise so readily available is a gift I will never be able to repay.

So thank you.

Thank you to Christie, who founded Women Who Hack, the first technical event I ever attended. Thank you to all the amazing women I met there. Thank you to Melissa and Ian who donated to my fundraiser so I could even go to Code School. Thank you to Liene for studying Python with me and sending me well-timed resources. Thank you to Rachel for her unending kindness and consideration matched with extreme insight. Thank you to Kronda for her WordPress wizardry and generally amazing sense of humor. Thank you to Michelle, the founder of Code Scouts for helping me meet even more amazing people. Thank you to Ashe and Julie and Shanley for doing the blisteringly hard work of carrying us all forward. Thank you to Chuck and Erica for creating Portland Codes School and for helping me find a direction. Thank you to Sam, for inviting me into his home to help me learn. Thank you to Cris for giving me the benefit of the doubt. Thank you to Al for always working to be accountable and give us the best possible class. Thank you to Erin for being the most generous and understanding technical lead, mentor, and friend. And thank you to Emily, who pestered me into attending that first Women Who Hack meeting, and has been there to cheer me on every step of the way, and to walk me through the scary parts.

 

 

*I will not be discussing the details of that here. Please go google around if you want to know and don’t already, but be forewarned, massive trigger warning for everything bad you could say to and about sexual assault

First Week at Portland Code School!

This week has been quite the whirlwind!

Monday was my first class with Portland Code School for their Ruby on Rails class. We spent a lot of time getting to know each other, since the plan is to spend the next three months eating, sleeping, and coding together. We talked about the tech experience (or lack thereof) we each have, and discussed the structure of the class to follow. As I feared, I’m the only woman in the class, and possibly the only queer person (though that’s much harder to determine). Overall, our class of seven is somewhat more diverse and somewhat less privileged than I expected, so I actually think it won’t be a barrier.

Tuesday we were asked to attend the Portland Ruby Brigade meeting, which was not my first time to their events, but was my first time attending one of the sessions featuring speakers. Overall, I liked the first speaker, Markus, the best. It’s hard to even describe the topic of his talk, since he seems to integrate his thoughts across multiple topics quite deeply. Overall, he was discussing this forum post. Definitely worth a read.

The second talk was honestly quite over my head, and I didn’t really have a framework yet for relating to it. Talking with others afterward, though, I discovered I definitely wasn’t the only one who felt a bit lost. Not to say the speakers weren’t engaging or funny or smart. They just had a target audience that didn’t happen to include me.

The last speaker was talking about different models of inheritance, and it was actually quite interesting. I’ll have to do some research at some point.

Wednesday was class number two, and we covered web applications, how the web works, the model view controller structure, and a bit of standards based development.

Thursday I went to the Green Mercury hack night, and proceeded to spend most of my time there fixing things I had broken on my own system. I was trying to customize my bash prompt in iTerm, and I managed to overwrite the contents of the .git-prompt.sh file (which I was using as a source) with the contents of my .bash_profile. Which resulted in an infinite loop, locking me completely out of bash. Which, by the way, makes it somewhat difficult to access your hidden files. Eventually, with massive amounts of help from Erin, we were able to use zsh to get into the files and change them in order to break the loop.

After that, I discovered that the link between my local Green Mercury repo and my remote was somehow severed, and had to spend a bit troubleshooting that. Then I made the brilliant move of deleting a file both through Github’s web interface, and also on my local branch and had to once again ask Erin to rescue me. Ultimately, I learned about git reset, my reflog, cherry picking, and how exactly git structures and conceptualizes branches, so ultimately it wasn’t so bad.

Between working and code related activities, I was outside my house at least fifteen hours each day this week, and I was glad we didn’t have anything planned for Friday! This weekend, I’ll spend a lot of time working on getting up to speed on all my homework, and hopefully getting a workspace set up for myself at home. This coding/learning while in bed thing doesn’t work for me. All in all, though, I can’t wait until class on Monday!