Skip to content

Instantly share code, notes, and snippets.

@podhmo
Last active July 3, 2018 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/7afde6fb4de1c2bc8387c51f76473d18 to your computer and use it in GitHub Desktop.
Save podhmo/7afde6fb4de1c2bc8387c51f76473d18 to your computer and use it in GitHub Desktop.

動きを試してみるとき

$ make one
python fetch.py -c config.json  -x 1 -y 2
id,x,y,x_y_rate
1,1,2,0.5

$ make many
python fetch.py -c config.json -x 1 -x 2 -x 3 -x 4 -x 5 -y 2 -y 1 -y 0 -y 1 -y 2
id,x,y,x_y_rate
1,1,2,0.5
2,4,1,4.0
3,9,0,-
4,16,1,16.0
5,25,2,12.5

$ SIZE=3 make
python fetch.py -c config.json --size=3 --input ./test-input.csv | tee dist/test-output.csv
id,x,y,x_y_rate
1,1,-50,-0.02
2,4,-49,-0.08163265306122448
3,9,-48,-0.1875

全部実行する時

# どういうコマンドが実行されるかは -n 付きで実行
$ make -n
mkdir -p dist
python fetch.py -c config.json  --input ./test-input.csv | tee dist/test-output.csv

# 全部実行
$ PREFIX=test make
id,x,y,x_y_rate
1,1,-50,-0.02
2,4,-49,-0.08163265306122448
3,9,-48,-0.1875
4,16,-47,-0.3404255319148936
5,25,-46,-0.5434782608695652
...
40,1600,-11,-145.45454545454547
41,1681,-10,-168.1
42,1764,-9,-196.0
43,1849,-8,-231.125
ERROR

# resume
$ RESUME=1 PREFIX=test make
44,1936,-7,-276.57142857142856
45,2025,-6,-337.5
46,2116,-5,-423.2
47,2209,-4,-552.25
48,2304,-3,-768.0
49,2401,-2,-1200.5
50,2500,-1,-2500.0
...
96,9216,45,204.8
97,9409,46,204.54347826086956
98,9604,47,204.3404255319149
99,9801,48,204.1875
100,10000,49,204.08163265306123
101,10201,50,204.02
import sys
import itertools
try:
import csvresumable as csv
except ImportError:
import csv
def collect(itr):
for x, y in itr:
x = int(x)
y = int(y)
yield {"id": x, "x": x * x, "y": y}
def run(*, config, size=None, x_list=None, y_list=None, input=None, decoration=None):
if input is not None:
r = ((row["x"], row["y"]) for row in csv.DictReader(input))
else:
r = zip((x_list or []), (y_list or []))
if size is not None:
r = itertools.islice(r, size)
itr = collect(r)
if decoration is not None:
itr = decoration(itr)
itr = decoration_for_calculation(itr)
yield from itr
def decoration_for_calculation(itr):
for row in itr:
if row["y"] == 0:
yield row
continue
row["x_y_rate"] = row["x"] / row["y"]
yield row
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", required=True)
parser.add_argument("-x", action="append", dest="x_list")
parser.add_argument("-y", action="append", dest="y_list")
parser.add_argument("--size", type=int)
parser.add_argument("--input", type=argparse.FileType("r"))
args = parser.parse_args()
itr = run(**vars(args))
w = csv.DictWriter(sys.stdout, fieldnames=["id", "x", "y", "x_y_rate"], restval="-")
w.writeheader()
w.writerows(itr)
if __name__ == "__main__":
main()
PREFIX ?= test
SIZE ?=
CMD := python fetch.py -c config.json $(addprefix --size=,${SIZE})
default:
mkdir -p dist
${CMD} --input ./${PREFIX}-input.csv | tee dist/${PREFIX}-output.csv
one:
${CMD} -x 1 -y 2
many:
${CMD} -x 1 -x 2 -x 3 -x 4 -x 5 -y 2 -y 1 -y 0 -y 1 -y 2
x y
1 -50
2 -49
3 -48
4 -47
5 -46
6 -45
7 -44
8 -43
9 -42
10 -41
11 -40
12 -39
13 -38
14 -37
15 -36
16 -35
17 -34
18 -33
19 -32
20 -31
21 -30
22 -29
23 -28
24 -27
25 -26
26 -25
27 -24
28 -23
29 -22
30 -21
31 -20
32 -19
33 -18
34 -17
35 -16
36 -15
37 -14
38 -13
39 -12
40 -11
41 -10
42 -9
43 -8
44 -7
45 -6
46 -5
47 -4
48 -3
49 -2
50 -1
51 0
52 1
53 2
54 3
55 4
56 5
57 6
58 7
59 8
60 9
61 10
62 11
63 12
64 13
65 14
66 15
67 16
68 17
69 18
70 19
71 20
72 21
73 22
74 23
75 24
76 25
77 26
78 27
79 28
80 29
81 30
82 31
83 32
84 33
85 34
86 35
87 36
88 37
89 38
90 39
91 40
92 41
93 42
94 43
95 44
96 45
97 46
98 47
99 48
100 49
101 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment