Speaking 'Development Language' (Or, how to get your hands dirty with technical stuff.)

Post on 12-May-2015

1.818 views 6 download

Tags:

description

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".

Transcript of Speaking 'Development Language' (Or, how to get your hands dirty with technical stuff.)

Speaking “Development Language”Or, how to get your hands dirty with technical stuff.

GWU Libraries ● 12 June 2012Julie Meloni // @jcmeloni // jcmeloni@gmail.com

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.

Today’s General Outline

•Development Lifecycle & Where You Fit In

•Computer Programming Basics

•Python in Particular

•Where to Learn More

Development Lifecycleand Where You Fit In…

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!

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

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.

• 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)

• 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

•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

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

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.

Computer Programming Basics

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)

What is a “Programming Language”?

•An artificial language with a limited purpose

•A means of expressing computations (math) and algorithms (logic)

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)

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”)

Variables & Arrays

• A variable is a bucket that holds one piece of information.

• Examples:

▫ $string_variable = “The Library”;

▫ $numeric_variable= 4;

▫ $myname = “Julie”;

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”

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!)

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

Operators

• Concatenation• + (string + string = stringstring)

• Logical• && (and)

• || (or)

• ! (not)

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

}

Flow Control

• whilewhile (something is true) {

do something here}

• forfor (something is true) {

do something here}

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.

Python in Particular

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)

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.

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”

Set and Print Variables

# set up the variablesstring_variable = "The Library";numeric_variable = 4;myname = "Julie";

# print the variablesprint string_variableprint numeric_variableprint myname

Arrays in Python

• …are called lists.

• Example:

▫ rainbow = [“red”, “orange”, “yellow”, “green”, “blue”, “indigo”, “violet”]

print rainbow[0] shows “red”

print rainbow[1] shows “orange”

Operators in Python (are not terribly special)

• Arithmetic ▫ +, -, *, /

• Assignment▫ = ▫ +=, -=, *=, /=

• Comparison▫ >, <, >=, <=

• Logical▫ and, or, not

Flow Control in Python• if

if something is true:INDENT and do something here

# here’s an examplepeople = 20space_aliens = 30

if people < space_aliens: print "Oh no! The world is doomed"

if people > space_aliens: print "We're cool."

Flow Control

• if ... elif ... else if something is true:

INDENT and do something hereelif something is true:

INDENT and do something hereelse:

INDENT and do something here

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."

Flow Control

• whilewhile something is true:

INDENT and do something here

# here’s an examplecount = 0

while (count < 9): print 'The count is:', count count = count + 1

print "...and we're done!"

Flow Control

The count is: 0The count is: 1The count is: 2The count is: 3The count is: 4The count is: 5The count is: 6The count is: 7The count is: 8...and we're done!

Flow Control

• forfor there are things in a sequence:

INDENT and do something here

#here’s an examplerainbow = ["red", "orange", "yellow", "green",

"blue", "indigo", "violet"]

for color in rainbow: print color

Flow Control

redorangeyellowgreenblueindigoviolet

Functions in Python

• Start with the keyword def

• Accepts parameters

• There’s indentation

• You get something in return

Functions in Pythondef 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

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

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 MARCReaderreader = MARCReader(open('marc.txt'))for record in reader: print record['245']['a']

Sample Utility (pymarc)• Example 2:

from pymarc import Record, Fieldrecord = 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()

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

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 %}

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

Where to Learn More

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/