This feed contains pages with tag "python".
Overview
The goal of the assignment is to develop a simple query language that lets the user select rows and columns from a CSV File, in effect treating it like database.
- Make sure you commit and push all your work using coursegit before 16:30 on Friday November 27.
General Instructions
Every non-test function should have a docstring
Feel free to add docstrings for tests if you think they need explanation
Use list and dictionary comprehensions as much as reasonable.
Your code should pass all of the given tests, plus some of your own with different data. If you want, you can use some of the sample data from the US Government College Scorecard. I’ve selected some of the data into smaller files:
- A marking rubric is available.
Reading CSV Files
We will use the builtin Python CSV module to read CSV files.
def read_csv(filename): '''Read a CSV file, return list of rows''' import csv with open(filename,'rt',newline='') as f: reader = csv.reader(f, skipinitialspace=True) return [ row for row in reader ]
Save the following as “~/fcshome/assignments/A4/test1.csv”; we will use it several tests. You should also construct your own example CSV files and corresponding tests.
name, age, eye colour Bob, 5, blue Mary, 27, brown Vij, 54, green
Here is a test to give you the idea of the returned data structure from
read_csv.def test_read_csv(): assert read_csv('test1.csv') == [['name', 'age', 'eye colour'], ['Bob', '5', 'blue'], ['Mary', '27', 'brown'], ['Vij', '54', 'green']]
Parsing Headers
The first row most in most CSV files consists of column labels. We will use this to help the user access columns by name rather than by counting columns.
Write a function
header_mapthat builds a dictionary from labels to column numbers.table = read_csv('test1.csv') def test_header_map_1(): hmap = header_map(table[0]) assert hmap == { 'name': 0, 'age': 1, 'eye colour': 2 }
Transforming rows into dictionaries
Sometimes it’s more convenient to work with rows of the table as dictionaries, rather than passing around the map of column labels everwhere. Write a function
row2dictthat takes the output from headermap, and a row, and returns a dictionary representing that row (column order is lost here, but that will be ok in our application).def test_row2dict(): hmap = header_map(table[0]) assert row2dict(hmap, table[1]) == {'name': 'Bob', 'age': '5', 'eye colour': 'blue'}
Matching rows
We are going to write a simple query languge where each query is a 3-tuple
(left, op, right), andopis one of=,<, and>. In the initial version,leftandrightare numbers or strings. Strings are interpreted as follows: if they are column labels, retrieve the value in that column; otherwise treat it as a literal string. With this in mind, write a functioncheck_rowthat takes a row in dictionary form, and checks if it matches a query tuple.def test_check_row(): row = {'name': 'Bob', 'age': '5', 'eye colour': 'blue'} assert check_row(row, ('age', '=', 5)) assert not check_row(row, ('eye colour', '=', 5)) assert check_row(row, ('eye colour', '=', 'blue')) assert check_row(row, ('age', '>', 4)) assert check_row(row, ('age', '<', 1000))
Extending the query language
Extend
check_rowso that it supports operationsANDandOR. For these cases both left and right operands must be queries. Hint: this should only be a few more lines of code.def test_check_row_logical(): row = {'name': 'Bob', 'age': '5', 'eye colour': 'blue'} assert check_row(row, (('age', '=', 5),'OR',('eye colour', '=', 5))) assert not check_row(row, (('age', '=', 5),'AND',('eye colour', '=', 5)))
Filtering tables
Use you previously developed functions to impliment a function
filter_tablethat selects certain rows of the table according to a query.def test_filter_table1(): assert filter_table(table,('age', '>', 0)) == [['name', 'age', 'eye colour'], ['Bob', '5', 'blue'], ['Mary', '27', 'brown'], ['Vij', '54', 'green']] assert filter_table(table,('age', '<', 28)) == [['name', 'age', 'eye colour'], ['Bob', '5', 'blue'], ['Mary', '27', 'brown']] assert filter_table(table,('eye colour', '=', 'brown')) == [['name', 'age', 'eye colour'], ['Mary', '27', 'brown']] assert filter_table(table,('name', '=', 'Vij')) == [['name', 'age', 'eye colour'], ['Vij', '54', 'green']] def test_filter_table2(): assert filter_table(table,(('age', '>', 0),'AND',('age','>','26'))) == [['name', 'age', 'eye colour'], ['Mary', '27', 'brown'], ['Vij', '54', 'green']] assert filter_table(table,(('age', '<', 28),'AND',('age','>','26'))) == [['name', 'age', 'eye colour'], ['Mary', '27', 'brown']] assert filter_table(table,(('eye colour', '=', 'brown'), 'OR', ('name','=','Vij'))) == [['name', 'age', 'eye colour'], ['Mary', '27', 'brown'], ['Vij', '54', 'green']]
Quiz
- Time
- 50 minutes
- Activity
- Quiz
The first half of the lab will be a Python quiz
Getting started
- Make a directory
~/cs2613/labs/L18 - All of your work from this half of the lab should be saved to this directory, committed and pushed before the end of the lab.
Hello Go
- Time
- 10 minutes
- Activity
- Demo
// Our first program will print the classic "hello world"
// message. Here's the full source code.
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
Running go code is a bit more complicated than the other languages we studied so far.
# To run the program, put the code in `hello-world.go` and
# use `go run`.
$ go run hello-world.go
hello world
# Sometimes we'll want to build our programs into
# binaries. We can do this using `go build`.
$ go build hello-world.go
$ ls
hello-world hello-world.go
# We can then execute the built binary directly.
$ ./hello-world
hello world
# Now that we can run and build basic Go programs, let's
# learn more about the language.