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

Lesson 1 - An Introduction to Software Engineering

Lesson 1 - An Introduction to Software Engineering

Avatar for Dana Spiegel

Dana Spiegel

October 14, 2012
Tweet

More Decks by Dana Spiegel

Other Decks in Technology

Transcript

  1. What is a Server? 3 Internet Web Server Web Server

    Database Server 0000000000111001111 1001110000100111000 1111011011110100001 0000101110010010000 0000000000111001111 1001110000100111000 1111011011110100001 0000101110010010000 0000000000111001111 1001110000100111000 1111011011110100001 0000101110010010000 0111000 0111000
  2. How do Servers work? • Container for “business logic” and

    “application logic” • Web application server: • Handling a web request • Authenticating a user • Processing the data submitted by the user • Generating an appropriate response • Delivering the response to the user’s web browser • Handling any errors • This is the standard “Request/Response cycle” of a web application • Every web application follows this cycle! 4
  3. All Servers are built out of code • Code can

    be written in many languages, each with its own set of features • JavaScript (sometimes abbreviated JS) is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles.1 • Languages can be compiled or interpreted • JavaScript, Python, SQL are interpreted (dynamic) • C, Java are compiled • Languages have Syntax, the form of the language • Languages are strongly or weakly typed • Languages are formalized, and have different performance and usage characteristics, and thus each is better/worse at different jobs 1. http://en.wikipedia.org/wiki/Javascript 5
  4. Languages we will be using • Python • General purpose

    interpreted language that emphasizes code readability • Object oriented (not required, though) • Dynamically typed • Imperative (with a little bit of functional programming style) • Created by Guido van Rossum in 1989 • SQL • Function specific interpreted language that is built for data management in Relational Database Management Systems • Not object oriented • Statically typed • Functional programming style • Created by Don Chamberlin and Ray Boyce in 1974 6
  5. Example: Python 7 def process_data(data): print 'Starting to process' result

    = [] for d in data: print 'item: {0}'.format(d) if d > 0: result.append(d * 2) print 'Done' return result >>> r = process_data([0, 1, 2, 3]) Starting to process item: 0 item: 1 item: 2 item: 3 Done >>> print r [2, 4, 6]
  6. Example: SQL 8 CREATE TABLE users ( id INT, created_on

    DATE NOT NULL, email VARCHAR(200) NOT NULL, first_name VARCHAR(50), last_name VARCHAR(50), PRIMARY KEY (id) ); > SELECT * FROM users; id | created_on | email | firs | las ---+---------------------+-------------------+------+---- 1 | 2012-09-23 22:16:19 | [email protected] | John | Doe 2 | 2012-09-23 22:16:33 | [email protected] | Jane | Doe
  7. Python syntax 9 • Highly readable (more so than JavaScript)

    • Similar to JavaScript, but: • No braces • No semicolons • Everything is an object (including methods; more later) • Spaces (indentation) and newlines matter! • Easy output to screen using print command (but be careful, this won’t show up in a browser; more on this later) • Comments are prefixed by a hash (#) or triple quotes (""") • Single and double quotes can be used interchangeably • Interpreted line by line • Programs almost always split functionality into different files, which themselves define scope • http://cottagelabs.com/news/python-language-syntax-cheat-sheet
  8. Python: Data types 10 • Python defines different types of

    data types • String (str, unicode) • Numeric (int, float) • Date • Collection (tuple, list, set, dict) • Types are assigned based on the data representation • '123' vs. 123 vs. 1.23 • Data is usually assigned to a named variable • name = 'John Doe' • Variables are scoped, but don’t need a declaration (so no “var”) • Python is dynamically typed, which means that a variable can be reassigned different data types • name = 'John Doe' • name = 123
  9. Python: Data types (cont.) • Type can be discovered by

    using type() method • String • Defined by a quote character • Represent textual values • Numeric • Defined by numbers • int - integer • float - floating point • autoconvert up (int -> float) • Bool • Defined by True/False • Date • Provided by a module (datetime) • datetime.datetime(2012, 1, 30, 20, 15, 59) 11
  10. Python: Data types (cont.) • Collections • Can contain any

    number of primitive types (mix and match) • tuple: () • Immutable ordered listing of data • Duplicates allowed • list: [] • Mutable ordered listing of data • Duplicates allowed • set: set() • Mutable unordered listing of data • Duplicates not allowed • dict: {key: value} • Mutable key and value storage • Duplicates allowed for values 12
  11. Python: Data type examples 13 str 'Welcome message' unicode u'Welcome

    Message' int 123 float 1.23 bool True, False datetime datetime.datetime(2012, 1, 30, 20, 15, 59) tuple (1, 1, 2, 3, ) list [1, 1, 2, 3] set set([1, 2, 3]) dict { 'name': 'John Doe', ‘is_member’: True, } None
  12. Python: Control flow • Interpreted line by line, with logic

    flowing mostly based upon tests and iterations • if/elif/else • Tests if an expression evaluates to True and if so, executes code • while • Executes code while <expr> is True 14 if <expr>: # do something elif <expr>: # do something else else: # otherwise do this while <expr>: # do something # set <expr> to False
  13. Python: Control flow (cont.) • for • Iterates over a

    collection, assigning current item to named variable in iteration context, executing code • try/except/finally • Executes code until Exception is raised, optionally catching specified exception and processing it 15 for i in [1, 2, 3,]: print i try: 'a' + 1 except TypeError, e: # handle exception TypeError
  14. Example: Fibonacci numbers • The Fibonacci numbers are the integer

    sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, ..., in which each item is formed by adding the previous two.1 • Uses recursion! 16 def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2) fib(10) => 55 1. http://en.literateprograms.org/Fibonacci_numbers_(Python)
  15. Python: methods and modules • A method is a contained,

    scoped, named collection of program logic • Defined by keyword def • Can be defined anywhere • Are callable from anywhere, as long as they are imported into the current context or are locally defined • A module is a collection of collection of variables and methods defined in a file • Each module has its own scope, and defined variables and methods are defined within the module’s scope • Python modules end in .py • A module can be imported into another module, bringing the imported modules variables and methods into the importing module’s scope • Modules can be nested hierarchically • Must define an __init__.py module in each folder • Nested modules are often times called libraries 17
  16. Example: modules and methods 18 • foo/__init__.py • foo/foofunc.py •

    task.py • baz.py def get(id, name): r = db.get(id) r.name = name return r import taskrunner def run(b): taskrunner.run(b) import foo.foofunc from . import task bar = foo.foofunc.get(id, name) bar.test() task.run(bar)
  17. In Class: Start Working with Python • Read and follow

    Exercise 0 of “Learn Python the Hard Way” • This is your best resource about how to write python code! • http://learnpythonthehardway.org/book/ex0.html • Homework: • Read more from “Learn Python the Hard Way” • Email me ([email protected]) so I have your email address and can send you an account on the dev server (for use next time) 19