Slides from an internal workshop at George Washington University Library on 12 June 2012. The goal of this workshop was to increase the number of people who can “work” on technical issues in the library. Topics were grouped into three main parts: "Development Lifecycle & Where You Fit In", "Computer Programming Basics", and "Python in Particular".
Speaking 'Development Language' (Or, how to get your hands dirty with technical stuff.)
1. Speaking “Development Language”
Or, how to get your hands dirty
with technical stuff.
GWU Libraries ● 12 June 2012
Julie Meloni // @jcmeloni // jcmeloni@gmail.com
2. Today’s Goal
• To increase the number of people who can
“work” on technical issues in the library
• Technical “work” in the future come from the
needs of the present: your needs.
▫ When you can articulate them to someone who
can do the codework, we all win.
▫ If YOU can do the codework, you win even more.
3. Today’s General Outline
• Development Lifecycle & Where You Fit In
• Computer Programming Basics
• Python in Particular
• Where to Learn More
5. General Software Development Lifecycle
• Define
▫ What you want to do
• Design
▫ How you want to do it
• Implement
▫ Actually do it
• Test
▫ Did what you do actually work
• Deploy
▫ Send it off into the wild
• Maintain
▫ Don‟t forget about it!
6. Design Phase Needs Domain Knowledge
• Functional requirements define the functionality
of the system, in terms of inputs, behaviors,
outputs.
▫ What is the system supposed to accomplish?
• Functional requirements come from
stakeholders (users), not (necessarily)
developers.
▫ stakeholder request -> feature -> use case ->
business rule
7. Example Functional Requirement
• Example functionality: representation and manipulation of
hierarchy
• Description: The GUI should allow users to view and interact with
hierarchical structures representing the intellectual arrangement
and the original arrangement of files and directories within ingested
accessions. For each component level in the intellectual
arrangement, the user interface should present associated digital
assets and an interface to view and edit descriptive metadata
elements.
• Specific Components: collapse and expand record nodes for
viewing (applies to both the original ingest and the intellectual
arrangement), add new child record, add new sibling record, copy
all or part of the existing structure to the intellectual arrangement,
delete a record in intellectual arrangement.
8. • An epic is a long story that can be broken into smaller stories.
• It is a narrative; it describes interactions between people and
a system
▫ WHO the actors are
▫ WHAT the actors are trying to accomplish
▫ The OUTPUT at the end
• Narrative should:
▫ Be chronological
▫ Be complete (the who, what, AND the why)
▫ NOT reference specific software or other tools
▫ NOT describe a user interface
Writing Use Cases (or Epics)
9. • Stories are the pieces of an epic that begin to get to the heart of the
matter.
• Still written in non-technical language, but move toward a technical
structure.
• Given/When/Then scenarios
▫ GIVEN the system is in a known state WHEN an action is performed
THEN these outcomes should exist
▫ EXAMPLE:
GIVEN one thing
AND an other thing
AND yet an other thing
WHEN I open my eyes
THEN I see something
But I don't see something else
Writing User Stories
10. • Scenario: User attempting to add an object
▫ GIVEN I am logged in
AND I have selected the “add” form
AND I am attempting to upload a file
▫ WHEN I invoke the file upload button
▫ THEN validate file type on client side
AND return alert message if not valid
AND continue if is valid
▫ THEN validate file type on server side
AND return alert message if not valid
AND finish process if is valid
Actual Story Example
11. Now You Do One!
• Think of a problem you want to solve (a batch
process, something displayed in the OPAC, etc)
• Think of the use case:
▫ WHO is doing WHAT to achieve OUTPUT
• Break it down into a story:
▫ GIVEN something WHEN something happens
THEN do something else
12. That Was the Define Phase…
• From a set of stories, developers begin to
DESIGN a way to bring the stories to life.
• At some point, programming begins and the
stories are IMPLEMENTED in code.
• During the coding process, TESTS are written
and code is TESTED.
• When the tests pass, the code is DEPLOYED.
• As time goes on, the code is MAINTAINED.
13.
14. Why Program?
• Express complex logic and perform
computations.
▫ We make the computer do what we want it to do.
▫ These behaviors come from our imaginations.
▫ The processes come from our needs and desires.
• Do things that take a long time or are difficult
for humans to do (counting, comparing,
repeating)
15. What is a “Programming Language”?
• An artificial language with a limited purpose
• A means of expressing computations (math) and
algorithms (logic)
16. What Does a Programming Language
Look Like?
• ...a lot like human language, as it has:
▫ Syntax (form)
▫ Semantics (meaning)
signs/words (variables, symbols, numbers, strings)
expressions
flow control (decisions, conditions, loops, narrative)
complex entities (methods, structures, & objects)
17. A Few Basic Programming Components
• Variables & Arrays
• Operators
• Flow Control
• Functions
Putting together these pieces adds up to
programming (or scripting, or in general “writing
some stuff to tell the computer what to do”)
18. Variables & Arrays
• A variable is a bucket that holds one piece of
information.
• Examples:
▫ $string_variable = “The Library”;
▫ $numeric_variable= 4;
▫ $myname = “Julie”;
19. Variables & Arrays
• An array is a type of variable (or bucket) that
holds many pieces of information.
• Example:
▫ $rainbow = array(“red”, “orange”, “yellow”,
“green”, “blue”, “indigo”, “violet”)
$rainbow[0] holds “red”
$rainbow[1] holds “orange”
20. Operators
• Arithmetic
▫ +, -, *, / (add, subtract, multiply, divide)
• Assignment
▫ = (“Assign the value of 4 to the variable called a”)
$a = 4;
▫ += (“Add the value of 5 to the variable that already
holds 4”)
$a += 5; // $a now holds 9
▫ .= (“Attach the value „World‟ to the end of „Hello‟ to
make a new value for the string variable”)
$string = “Hello”;
$string .= “World”; // would print “HelloWorld” (no
space because we didn‟t add that!)
21. Operators
• Comparison
▫ == (“when I compare the value in variable a to the
value in variable be, that comparison is true”)
$a == $b
▫ != (“when I compare the value in variable a to the
value in variable be, that comparison is not true”)
$a != $b
▫ >, >= (“the value of variable a is greater than (or
greater than or equal to) the value of variable b”)
$a > $b
▫ <, <= (“the value of variable a is less than (or less
than or equal to) the value of variable b”)
$a < b
23. Flow Control
• if
if (something is true) {
do something here
}
• if ... else ... else if
if (something is true) {
do something here
} else if (something is true) {
do something here
} else {
do something here
}
24. Flow Control
• while
while (something is true) {
do something here
}
• for
for (something is true) {
do something here
}
25. Procedures and Functions
• Scripts can contain linear, procedural code.
• Scripts can also contain references to reusable
bits of code, called functions.
▫ Built-in language functions
▫ Functions you write yourself.
26.
27. Why Python?
• It is a general-purpose language
• It has been around for a long time (20+ years)
• It has a strong developer community
• It includes a large built-in library of functionality
• It is readable
• It is expressive (you can do a lot with a little)
28. Uncluttered Layout
• Less punctuation
▫ While some languages use $ to indicate variables, or
brackets around logical constructs, Python does not.
• More whitespace
▫ Instead of brackets to set off blocks, indentation
means something in Python.
29. Variables in Python
• Do not begin with a symbol and do not end with
terminating punctuation.
• Examples:
▫ string_variable = “The Library”
▫ numeric_variable= 4
▫ myname = “Julie”
30. Set and Print Variables
# set up the variables
string_variable = "The Library";
numeric_variable = 4;
myname = "Julie";
# print the variables
print string_variable
print numeric_variable
print myname
33. Flow Control in Python
• if
if something is true:
INDENT and do something here
# here’s an example
people = 20
space_aliens = 30
if people < space_aliens:
print "Oh no! The world is doomed"
if people > space_aliens:
print "We're cool."
34. Flow Control
• if ... elif ... else
if something is true:
INDENT and do something here
elif something is true:
INDENT and do something here
else:
INDENT and do something here
35. Flow Control
• EXAMPLE
people = 30
cars = 40
if cars > people:
print "We should take the cars."
elif cars < people:
print "We should not take the cars."
else:
print "We can't decide."
36. Flow Control
• while
while something is true:
INDENT and do something here
# here’s an example
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
print "...and we're done!"
37. Flow Control
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
...and we're done!
38. Flow Control
• for
for there are things in a sequence:
INDENT and do something here
#here’s an example
rainbow = ["red", "orange", "yellow", "green",
"blue", "indigo", "violet"]
for color in rainbow:
print color
40. Functions in Python
• Start with the keyword def
• Accepts parameters
• There‟s indentation
• You get something in return
41. Functions in Python
def fibonacci(n):
a, b = 0, 1
while a < n:
print a,
a, b = b, a+b
fibonacci(1000)
/////////////////////////////////////////////////
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
42. So How Does Your Group Use Python?
• As batch scripts, with or without a web interface
▫ One-offs, utilities, etc
• Using a Web Framework (Django)
▫ Frameworks allow you to write web applications
quickly because they include, well, a framework
for doing so.
Reusable libraries common to web applications
Coding standards
Template and templating processes
43. Sample Utility (pymarc)
• For manipulation of MARC records
▫ In GitHub at https://github.com/edsu/pymarc/
▫ From command-line or wrapped within an app
• Example 1:
from pymarc import MARCReader
reader = MARCReader(open('marc.txt'))
for record in reader:
print record['245']['a']
44. Sample Utility (pymarc)
• Example 2:
from pymarc import Record, Field
record = Record()
record.addField(
Field(
tag = '245',
indicators = ['0','1'],
subfields = [
'a', 'The pragmatic programmer : ',
'b', 'from journeyman to master /',
'c', 'Andrew Hunt, David Thomas.'
]))
out = open('file.dat', 'w')
out.write(record.asMARC21())
out.close()
45. Launchpad
• Works within a Django framework
• Has a directory structure you can follow to find
how things are pieced together
• Even in a framework, it is still readable code
46. Launchpad (example from a template)
{% extends "base.html" %}
{% load launchpad_extras %}
{% block title %}{{ bib.TITLE }}{% endblock
title %}
…
{% if bib.ISBN|clean_isbn %}
ISBN: <a href='{% url isbn bib.ISBN|clean_isbn
%}'>{% url isbn bib.ISBN|clean_isbn %}</a>
{% endif %}
47. Launchpad (example from a template)
def clean_isbn(value):
isbn,sep,remainder = value.strip().partition('')
if len(isbn) < 10:
return ''
isbn = isbn.replace('-', '')
isbn = isbn.replace(':', '')
return isbn
48.
49. Additional Resources
• Learn Python the Hard Way
▫ http://learnpythonthehardway.org/book/
• The Python Tutorial
▫ http://docs.python.org/tutorial/index.html
• Django
▫ https://www.djangoproject.com/
• GWU Libraries GitHub repositories
▫ https://github.com/gwu-libraries/