Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lesson 3 - Development Tools

Lesson 3 - Development Tools

Dana Spiegel

October 16, 2012
Tweet

More Decks by Dana Spiegel

Other Decks in Technology

Transcript

  1. Documentation • Good code is self documenting, but even then,

    it should have comments • Document each method your write, and intersperse comments describing why you are doing something • Clearly define what inputs you expect and what outputs you will provide • You will be reading and revising your own code, and after 6 months you won’t remember what you did and why! 2 def __resize__(self): """ Resizes the array by creating doubling the storage_length, creating a new list and re-hashing all stored items """ new_storage_length = self.storage_length * 2 # initialize a new list and pre-fill it with empty lists new_items = [[] for i in range(new_storage_length)] for item_list in self.items: ....
  2. Testing • There are only 2 ways to know that

    your code does what you believe it does: • Write unit tests • Pray • Writing units tests is the only way to “guarantee” that your code does what it is supposed to do • Unit tests are a form of software testing that validates code logic • They aren’t the only type of testing you need to do! • Unit tests are best at testing the outputs of a method for a variety of inputs • They must be thorough! 3
  3. Example: Unit Tests 4 import unittest class TestHashMap(unittest.TestCase): def test_init(self):

    h = HashMap() self.assertEqual(0.75, h.load) self.assertEqual(64, h.storage_length) self.assertEqual(0, h.item_count) self.assertEqual([[] for i in range(64)], h.items) def test_index_for_key(self): h = HashMap() self.assertEqual(h.__index_for_key__('abc', l=10), 1) self.assertEqual(h.__index_for_key__('bcd', l=10), 2) self.assertEqual(h.__index_for_key__('abc'), 35) self.assertEqual(h.__index_for_key__('bcd'), 40) h.storage_length = 100 self.assertEqual(h.__index_for_key__('abc'), 11) self.assertEqual(h.__index_for_key__('bcd'), 72) 1. https://gist.github.com/eb681f836648cac4fd3b
  4. Good methodology • Spend time before writing code to ensure

    complete understanding of problem and solution • Write code that anyone can understand • Keep methods as short as possible (easier to test) • Refactor on a regular basis • Use variable names that are descriptive! • Test your code, even against unlikely circumstances • Read other people’s code as much as possible • This is the only way to become a good developer • Read about developing software • Challenge yourself • This is the only way to become a good developer • Most important: Have fun! 5
  5. Server • Most server run Unix or Linux (*nix) •

    Your laptop! (Mac OS X is Unix), Sun, HP, IBM... • Provides all resources • Networking • Web server • Logging • File system • Multi-user • Home directory (~) • “Cloud” is really just servers in a data center • AWS - Amazon Web Services (free micro instance) • Heroku (free web dyno) • Rackspace • Google AppEngine 6
  6. The command shell (and *nix) • Shell or sh •

    csh, bash, zsh, ksh are variants; all have differences • Most basic tool for getting work done on a *nix server • Shell is your UI for your server • Commands are interpreted when pressing the <return> key • File streams: stdin, stdout, stderr • A command output can be piped to another command’s input • Shell scripts are programs too! • Some hints: • <ctrl-c> or q: quit or kill current program (usually) • ls: list files • cd: change directory • less: print the file contents to the terminal • grep: find text in a file 7
  7. Secure Shell (SSH) • Secure network connection to a network

    attached server shell • Provides interactive shell access, safely • Proves line-based encryption • Usually requires username and password • Can use certificate-based login • Secure FTP: same as FTP, but securely • Most graphical FTP clients offer SFTP connection • Built in on *nix, Mac OS X • Putty on Windows 8 ssh <user>@<host>
  8. Example: SSH on *nix 9 $ ssh [email protected] Welcome to

    Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-30-virtual x86_64) * Documentation: https://help.ubuntu.com/ System information as of Sun Oct 14 04:09:12 UTC 2012 System load: 0.0 Processes: 63 Usage of /: 15.2% of 7.87GB Users logged in: 0 Memory usage: 37% IP address for eth0: 10.80.238.210 Swap usage: 0% 43 packages can be updated. 27 updates are security updates. Last login: Mon Sep 17 02:24:51 2012 from xxx.verizon.net ubuntu@dev:~$
  9. Text Editors • One of the most hotly contested arguments

    between developers • In a shell, you’ve got basically 2 choices: • Emacs • vi • Both are good, but very different • We’re going to use emacs, because that’s what I use • Pay attention to the minibuffer • Ctrl-key commands • Open file: <ctrl-x><ctrl-f> • Save file: <ctrl-x><ctrl-s> • Exit: <ctrl-x><ctrl-c> • Cancel command: <ctrl-g> 11
  10. Cloud-based file storage • Store and retrieve files online instead

    of on a server’s local disk • Like FTP, but provides easier security and web serving • Usually much safer • Distributed • Guaranteed uptime • Automatically backed up • Can be connected to a CDN • Provide a URL for a stored file so anyone can retrieve it • Common services: • Amazon S3 • Rackspace CloudFiles 12
  11. DNS: the Domain Name System • Every server has at

    least 1 IP address • DNS maps names to numbers • How your most people find things on the Internet • Different types of records: • CNAME • A • MX • Tools • dig • host 13
  12. Example: dig 14 $ dig yahoo.com ANY ; <<>> DiG

    9.8.3-P1 <<>> yahoo.com ANY ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44362 ;; flags: qr rd ra; QUERY: 1, ANSWER: 11, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;yahoo.com." " " IN" ANY ;; ANSWER SECTION: yahoo.com." " 1195" IN" A" 98.138.253.109 yahoo.com." " 1195" IN" A" 98.139.183.24 yahoo.com." " 1195" IN" A" 72.30.38.140 yahoo.com." " 1204" IN" MX" 1 mta6.am0.yahoodns.net. yahoo.com." " 1204" IN" MX" 1 mta7.am0.yahoodns.net. yahoo.com." " 1204" IN" MX" 1 mta5.am0.yahoodns.net. yahoo.com." " 171080" IN" NS" ns1.yahoo.com. yahoo.com." " 171080" IN" NS" ns5.yahoo.com. yahoo.com." " 171080" IN" NS" ns2.yahoo.com. yahoo.com." " 171080" IN" NS" ns3.yahoo.com. yahoo.com." " 171080" IN" NS" ns4.yahoo.com.
  13. Example: host 15 $ host yahoo.com yahoo.com has address 98.138.253.109

    yahoo.com has address 72.30.38.140 yahoo.com has address 98.139.183.24 yahoo.com mail is handled by 1 mta6.am0.yahoodns.net. yahoo.com mail is handled by 1 mta7.am0.yahoodns.net. yahoo.com mail is handled by 1 mta5.am0.yahoodns.net.
  14. Source Control • “VCS” or Version Control System • Way

    to keep and access the changes made to (text) files • Beyond the editor, this is the second most important tool you will use • Lots of different ones, but we are going to use Git (vs. Mercurial/Hg) • Git • Invented by Linux Torvalds (yes, the Linux guy) to help manage changes in the Linux kernel • Add new files: git add <file> • Save versions of files: git commit -a • Everything is a changeset with a unique ID • Push your local changes to remote: git push • Get remote changes: git pull • Github • Hosted Git, with lots of nice features 16
  15. Git • “Remote” vs. local • Changeset are represented by

    an id, like: • 8357dea69d8b3a4bd6448c1d86fa4250dfffa830 • Commands • Make a local copy of a repo: git clone <url> • Add a file to a repo: git add <file(s)> • Save changes to files into repo: git commit [-a|<files>] • Publish your changes: git push • Pull down the most recent changes from remote: git pull • Show uncommited changes: git show • Show recently commited changes: git log • Lots more! • Learn more git: http://try.github.com/levels/1/challenges/4 • Use Github for code reviews! 17
  16. Good places to find help • Me! • Google •

    stackoverflow.com • python.org • djangoproject.com • email lists • IRC chat: irc.freenode.net 18
  17. In Class: Using tools • Get access to dev server

    • Play with tools: • shell • python • git • Homework: • Write a file in your home directory with the name “hw2” and the file contents “Hello World” • Read file “hw2” in my (in directory /home/Users/ubuntu) • Get a Github account and email me ([email protected]) your username • Email me your Github username and the text in my hw2 file • Read and follow Github getting started doc: https://help.github.com/ categories/54/articles 19