Created
July 30, 2012 14:45
-
-
Save Gab-km/3207468 to your computer and use it in GitHub Desktop.
Python初級ハンズオン in #pyfes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 課題2-3 | |
import sys | |
import sqlite3 | |
DBNAME = 'items.db' | |
def filter_by_cost(conn, lower_bound): | |
c = None | |
try: | |
c = conn.cursor() | |
c.execute('select * from items where cost >= ?', (lower_bound,)) | |
return [(row[1], row[2]) for row in c] | |
finally: | |
if c != None: | |
c.close() | |
if __name__ == '__main__': | |
conn = None | |
try: | |
conn = sqlite3.connect(DBNAME) | |
rows = filter_by_cost(conn, sys.argv[1]) | |
print("100円以上の品物は、") | |
if len(rows) > 0: | |
for row in rows: | |
print("{0} {1}円".format(row[0], row[1])) | |
else: | |
print("ありませんでした。") | |
finally: | |
if conn != None: | |
print('Close') | |
conn.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 課題2-1 | |
import sys | |
import os.path | |
import sqlite3 | |
DBNAME = 'items.db' | |
def put(conn, name, cost): | |
c = None | |
try: | |
c = conn.cursor() | |
t = (name, int(cost)) | |
print(t) | |
c.execute('INSERT INTO items(name, cost) VALUES(?, ?)', t) | |
conn.commit() | |
finally: | |
if c != None: | |
c.close() | |
def create_items_table(conn): | |
c = None | |
try: | |
c = conn.cursor() | |
c.execute("""CREATE TABLE items | |
(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, cost INTEGER)""") | |
finally: | |
if c != None: | |
c.close() | |
if __name__ == '__main__': | |
conn = None | |
try: | |
if not os.path.exists(DBNAME): | |
conn = sqlite3.connect(DBNAME) | |
create_items_table(conn) | |
else: | |
conn = sqlite3.connect(DBNAME) | |
[name, cost] = sys.argv[1:] | |
put(conn, name, cost) | |
finally: | |
if conn != None: | |
print('Close') | |
conn.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 課題3-1 | |
import csv | |
CSV_NAME = 'items.csv' | |
if __name__ == '__main__': | |
with open(CSV_NAME, encoding='cp932') as f: | |
reader = csv.reader(f) | |
kv = {} | |
for (day, name, str_count, str_price) in reader: | |
count = int(str_count) | |
if name in kv: | |
kv[name] += count | |
else: | |
kv[name] = count | |
for (k, v) in kv.items(): | |
print("{0} {1}個".format(k, v)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 課題2-2 | |
import sys | |
import sqlite3 | |
DBNAME = 'items.db' | |
def get_all(conn): | |
c = None | |
try: | |
c = conn.cursor() | |
c.execute('select * from items') | |
rows = [] | |
for row in c: | |
rows.append((row[1], row[2])) | |
return rows | |
finally: | |
if c != None: | |
c.close() | |
if __name__ == '__main__': | |
conn = None | |
try: | |
conn = sqlite3.connect(DBNAME) | |
rows = get_all(conn) | |
for row in rows: | |
(name, cost) = row | |
print("{0} {1}円".format(name, cost)) | |
finally: | |
if conn != None: | |
print('Close') | |
conn.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 課題3-2 | |
import csv | |
CSV_NAME = 'items.csv' | |
if __name__ == '__main__': | |
with open(CSV_NAME, encoding='cp932') as f: | |
reader = csv.reader(f) | |
kv = {} | |
for (day, name, str_count, str_price) in reader: | |
count = int(str_count) | |
price = int(str_price) | |
if day in kv: | |
kv[day] += price * count | |
else: | |
kv[day] = price * count | |
for k in sorted(kv): | |
print("{0} {1}円".format(k, kv[k])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 課題1-2 | |
import socket | |
import sys | |
HOST = 'localhost' | |
PORT = 5000 | |
def add(inStr): | |
conn = None | |
try: | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((HOST, PORT)) | |
s.sendall(inStr.encode('utf-8')) | |
print(s.recv(1024).decode('utf-8')) | |
finally: | |
if conn != None: | |
print('Close') | |
conn.close() | |
if __name__ == '__main__': | |
add(sys.argv[1] + '\r\n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 課題1-1 | |
import socket | |
HOST = '' | |
PORT = 5000 | |
conn = None | |
acc = b'' | |
try: | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((HOST, PORT)) | |
s.listen(1) | |
conn, addr = s.accept() | |
print('Connected by', addr) | |
while True: | |
data = conn.recv(1024) | |
print(data) | |
if not data: break | |
if data.endswith(b'\r\n'): | |
acc = acc + data[:-2] | |
number_bytes = acc.split(b'+') | |
print(number_bytes) | |
numbers = [int(x) for x in number_bytes] | |
print(numbers) | |
conn.send(str(sum(numbers)).encode('utf-8')) | |
acc = b'' | |
else: | |
acc = acc + data | |
conn.send(b'') | |
conn.sendall(data) | |
finally: | |
if conn != None: | |
print('Close') | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment