Post on 15-Jan-2015
description
Everything you need to know about programming
Without any programming
Danny Mulligandanny@dannymulligan.com
Overview• Editors/IDEs• Revision Control• Testing• Debugging• Common errors• Performance
• Libraries• Documentation• Getting help• Practicing• Q&A
Editors/IDEs• Use what you already know• Use something simple• Use something that everybody else is using• Learn to walk before you learn to run• The right choice for an expert is probably not
the right choice for you (at least right now)
• Learn to type• Learn keyboard short cuts
Editors/IDEs• Common editor choices
– Textedit (standard on Macs)– Notepad (standard on Window)– EMACS http://emacsformacosx.com/– vim https://code.google.com/p/macvim/– Notepad++ http://notepad-plus-plus.org/– BBEdit http://www.barebones.com/products/bbedit/index.html– TextMate http://macromates.com/
• IDEs and other stuff you might try– Eclipse http://www.eclipse.org/– Pycharm https://www.jetbrains.com/pycharm/– Spyder IDE https://code.google.com/p/spyderlib/– iPython http://ipython.org/ (iPython notebooks are great!)– iTerm 2 http://www.iterm2.com/ (Replacement for Mac’s terminal)– 10 Fast Fingers http://10fastfingers.com/
Revision Control• Absolutely essential for teams• A good idea for you• Unfortunately, not the simplest stuff to master
• Popular tools: GIT, Hg, SVN, CVS, Perforce, Visual SourceSafe
• GitHib https://github.com/– Allows you to share code easily– Windows app http://windows.github.com/– Mac app http://mac.github.com/
• Related #1: when was your last backup?• Related #2: bug tracking is important too (especially for teams)
Testing• How do you know if you code has any bugs?
– Easy = it has bugs, you haven’t found them yet
• Writing tests is every bit as important as writing code– Pro move = write the tests FIRST!
• Keep the test code after you’ve written it• Unit tests & regression tests• Write defensive code (use assert statements)• Testing your code is a HARD problem, don’t underestimate it• Dividing coding and testing between multiple people can
help
Example of unit tests
Debugging• Print statements – show you what’s going on• Assert statements – verify your assumptions• Fancy debuggers – less useful than you’d think• Write documentation on your code• Code reviews - explain the code to someone else• Avoid putting the bugs in in the first place– Shorter code has fewer bugs– Simpler code has fewer bugs– Code that’s not there has 0 bugs = use the libraries
Common Errors• Off by 1 errors (AKA fencepost errors)• = vs. ==• Logical (and, or) vs. bitwise (&, |) operators, (use logical almost always)• integers vs. floats
a = 5/(1/3) # a should be equal to 15, or is it?
• Integer or float?a = 1/3; b = 3/9; c = a/b; print c # c is equal to 1, right?
• Floats are imprecisea = 2.15*3; b = 6.45; print (a == b) # True or False?
• Division by zero• Logic errors – easy to make mistakes with nested if statements• Are you working with a copy, or the original object?
colors = [‘red’, ‘green’, ‘blue’]b = colors; b[0] = ‘black’print colors # Did colors change?
• Inadequate or no error handling• Complexity is the enemy of correctness
Performance• Your performance matters a LOT more than
the computer’s performance• So don’t worry about performance
Performance optimization• Simple is usually faster• Use library functions whenever you can• Use the pareto principle, AKA the 80/20 rule– Profile first, optimize later– “premature optimization is the root of all evil”
• If you MUST optimize for performance– Do easy optimizations first– Your brain is the best optimization tool– Make sure you don’t break your program in the
process
Libraries• Learn what is in the standard library• Get the documentation• Some important libraries:– Python Standard Library: by far the most important– Python Image Library: image manipulation– Matplotlib: charts & graphs– Numpy: high performance data processing– Django: web framework– Scikit-learn: machine learning– Pandas: data analysis– NLTK: natural language
Python Standard Library• string – Common string operations• re – Regular expression operators• math – Mathematical functions• csv – CSV File Reading and Writing• datetime – Basic data and time types• random – Generate pseudo-random numbers• itertools – Functions creating iterators for efficient looping• collections – High-performance container datatypes• os – Miscellaneous operating system interfaces• threading – Higher-level threading interface• pdb – The Python Debugger• profile & cProfile – Profiling tools• test – Regression tests package for Python
Documentation• Python docs online at
http://docs.python.org/2/library/• I keep PDFs of the Python docs on my laptop
http://docs.python.org/2/download.html• The standard docs include a nice Tutorial• “The Python Library Reference” is by far the
most useful doc– 1,457 pages, but do not read (except the ToC)– Look up what you need when you need it
Getting help• Read the docs, read other people’s code• Google your question, look for similar code• When you google, you will often end up on StackOverflow
– Best site for programming questions http://stackoverflow.com• Watch tutorials on YouTube - examples
– 3 hour iPython tutorial https://www.youtube.com/watch?v=2G5YTlheCbw
– 83 video playlist from PyCon 2012 https://www.youtube.com/playlist?list=PL2814D3290BAA8837
– 30 minute overview of Pandas https://www.youtube.com/watch?v=qbYYamU42Sw
– 3 hour tutorial on Pandas https://www.youtube.com/watch?v=w26x-z-BdWQ
Practicing• How do you get better at golf?– Read a book or play golf?
• How do you get better at programming?– Use it in your job– Solve some real world problems– Read other people’s code
• Some real world programming exercises– Coding Bat http://codingbat.com/python– Project Euler https://projecteuler.net/
Q&A