-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment_20.py
More file actions
129 lines (95 loc) · 3.34 KB
/
Assignment_20.py
File metadata and controls
129 lines (95 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
1. Set the variable `test1` to the string `'This is a test of the emergency text system,'` and save it to a file named `test.txt`:
```python
test1 = 'This is a test of the emergency text system,'
with open('test.txt', 'w') as file:
file.write(test1)
```
2. Read the contents of the file `test.txt` into the variable `test2` and check if there's a difference between `test1` and `test2`:
```python
with open('test.txt', 'r') as file:
test2 = file.read()
if test1 == test2:
print("There is no difference between test1 and test2.")
else:
print("There is a difference between test1 and test2.")
```
3. Create a CSV file called `books.csv` with the given data:
```python
with open('books.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['title', 'author', 'year'])
writer.writerow(['The Weirdstone of Brisingamen', 'Alan Garner', 1960])
writer.writerow(['Perdido Street Station', 'China Miéville', 2000])
writer.writerow(['Thud!', 'Terry Pratchett', 2005])
writer.writerow(['The Spellman Files', 'Lisa Lutz', 2007])
writer.writerow(['Small Gods', 'Terry Pratchett', 1992])
```
4. Create a SQLite database called `books.db` and a table called `books` with the specified fields:
```python
import sqlite3
conn = sqlite3.connect('books.db')
c = conn.cursor()
c.execute('''CREATE TABLE books
(title TEXT, author TEXT, year INTEGER)''')
conn.commit()
conn.close()
```
5. Read `books.csv` and insert its data into the `books` table:
```python
import csv
conn = sqlite3.connect('books.db')
c = conn.cursor()
with open('books.csv', 'r') as file:
reader = csv.reader(file)
next(reader) # Skip the header row
for row in reader:
c.execute("INSERT INTO books VALUES (?, ?, ?)", row)
conn.commit()
conn.close()
```
6. Select and print the title column from the `books` table in alphabetical order:
```python
conn = sqlite3.connect('books.db')
c = conn.cursor()
c.execute("SELECT title FROM books ORDER BY title")
for row in c:
print(row[0])
conn.close()
```
7. Select and print all columns from the `books` table in the order of publication:
```python
conn = sqlite3.connect('books.db')
c = conn.cursor()
c.execute("SELECT * FROM books ORDER BY year")
for row in c:
print(row)
conn.close()
```
8. Use the SQLAlchemy module to connect to the SQLite database `books.db`:
```python
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///books.db')
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class Book(Base):
__tablename__ = 'books'
title = Column(String)
author = Column(String)
year = Column(Integer)
```
9. Install the Redis server and the Python redis library, create a Redis hash called `test` with the fields `count` (1) and `name` ('Fester Bestertester'), and print all the fields for `test`:
```python
import redis
r = redis.Redis()
r.hset('test', 'count', 1)
r.hset('test', 'name', 'Fester Bestertester')
print(r.hgetall('test'))
```
10. Increment the `count` field of `test` and print it:
```python
r.hincrby('test', 'count', 1)
print(r.hget('test', 'count'))
```