Skip to content

Commit a369f64

Browse files
committed
black mamba
1 parent 390454c commit a369f64

File tree

36 files changed

+2166
-796
lines changed

36 files changed

+2166
-796
lines changed

apache-bigdata/1.flask-hadoop/app.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,62 @@
99

1010
print(hdfs.hdfs().list_directory('/user'))
1111

12+
1213
@app.route('/')
1314
def hello_world():
1415
return 'Hey, we have Flask in a Docker container!'
1516

17+
1618
@app.route('/test', methods = ['POST'])
1719
def test():
1820
f = request.files['file']
19-
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
21+
f.save(
22+
os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename))
23+
)
2024
return f.filename
2125

26+
2227
@app.route('/wordcount', methods = ['POST'])
2328
def wordcount():
2429
f = request.files['file']
25-
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
26-
with open(f.filename,'r') as fopen:
30+
f.save(
31+
os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename))
32+
)
33+
with open(f.filename, 'r') as fopen:
2734
hdfs.dump(fopen.read(), '/user/input_wordcount/text')
28-
os.system('pydoop script -c combiner wordcount.py /user/input_wordcount /user/output_wordcount')
35+
os.system(
36+
'pydoop script -c combiner wordcount.py /user/input_wordcount /user/output_wordcount'
37+
)
2938
list_files = hdfs.hdfs().list_directory('/user/output_wordcount')
30-
return json.dumps([hdfs.load(file['name'], mode='rt') for file in list_files if 'SUCCESS' not in file['name']])
39+
return json.dumps(
40+
[
41+
hdfs.load(file['name'], mode = 'rt')
42+
for file in list_files
43+
if 'SUCCESS' not in file['name']
44+
]
45+
)
46+
3147

3248
@app.route('/lowercase', methods = ['POST'])
3349
def lowercase():
3450
f = request.files['file']
35-
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
36-
with open(f.filename,'r') as fopen:
51+
f.save(
52+
os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename))
53+
)
54+
with open(f.filename, 'r') as fopen:
3755
hdfs.dump(fopen.read(), '/user/input_lowercase/text')
38-
os.system("pydoop script --num-reducers 0 -t '' lowercase.py /user/input_lowercase /user/output_lowercase")
56+
os.system(
57+
"pydoop script --num-reducers 0 -t '' lowercase.py /user/input_lowercase /user/output_lowercase"
58+
)
3959
list_files = hdfs.hdfs().list_directory('/user/output_lowercase')
40-
return json.dumps([hdfs.load(file['name'], mode='rt') for file in list_files if 'SUCCESS' not in file['name']])
60+
return json.dumps(
61+
[
62+
hdfs.load(file['name'], mode = 'rt')
63+
for file in list_files
64+
if 'SUCCESS' not in file['name']
65+
]
66+
)
67+
4168

4269
if __name__ == '__main__':
43-
app.run(debug=True, host='0.0.0.0',port=5000)
70+
app.run(debug = True, host = '0.0.0.0', port = 5000)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
def mapper(_, record, writer):
2-
writer.emit("", record.lower())
2+
writer.emit('', record.lower())
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
def mapper(_, text, writer):
22
for word in text.split():
3-
writer.emit(word, "1")
3+
writer.emit(word, '1')
4+
45

56
def reducer(word, icounts, writer):
67
writer.emit(word, sum(map(int, icounts)))
78

9+
810
def combiner(word, icounts, writer):
911
writer.count('combiner calls', 1)
1012
reducer(word, icounts, writer)

apache-bigdata/2.flask-kafka/app.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@
22
from kafka import KafkaConsumer
33
import json
44
import os
5+
56
app = Flask(__name__)
67

8+
79
@app.route('/')
810
def hello_world():
911
return 'Hey, we have Flask in a Docker container!'
1012

11-
@app.route("/topic/<string:topic>/")
13+
14+
@app.route('/topic/<string:topic>/')
1215
def get_topics(topic):
13-
consumer = KafkaConsumer(topic, auto_offset_reset='earliest',
14-
bootstrap_servers=['localhost:9092'],
15-
api_version=(0, 10), consumer_timeout_ms=1000)
16-
return json.dumps([json.loads(msg.value.decode('utf-8')) for msg in consumer])
16+
consumer = KafkaConsumer(
17+
topic,
18+
auto_offset_reset = 'earliest',
19+
bootstrap_servers = ['localhost:9092'],
20+
api_version = (0, 10),
21+
consumer_timeout_ms = 1000,
22+
)
23+
return json.dumps(
24+
[json.loads(msg.value.decode('utf-8')) for msg in consumer]
25+
)
26+
1727

1828
if __name__ == '__main__':
19-
app.run(debug=True, host='0.0.0.0',port=5000)
29+
app.run(debug = True, host = '0.0.0.0', port = 5000)

apache-bigdata/2.flask-kafka/producer.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from bs4 import BeautifulSoup
55
from kafka import KafkaProducer
66

7+
78
def parse(markup):
89
title = '-'
910
submit_by = '-'
@@ -24,7 +25,10 @@ def parse(markup):
2425
if ingredients_section:
2526
for ingredient in ingredients_section:
2627
ingredient_text = ingredient.text.strip()
27-
if 'Add all ingredients to list' not in ingredient_text and ingredient_text != '':
28+
if (
29+
'Add all ingredients to list' not in ingredient_text
30+
and ingredient_text != ''
31+
):
2832
ingredients.append({'step': ingredient.text.strip()})
2933

3034
if description_section:
@@ -36,20 +40,26 @@ def parse(markup):
3640
if title_section:
3741
title = title_section[0].text
3842

39-
rec = {'title': title, 'submitter': submit_by, 'description': description, 'calories': calories,
40-
'ingredients': ingredients}
43+
rec = {
44+
'title': title,
45+
'submitter': submit_by,
46+
'description': description,
47+
'calories': calories,
48+
'ingredients': ingredients,
49+
}
4150

4251
except Exception as ex:
4352
print('Exception while parsing')
4453
print(str(ex))
4554
finally:
4655
return json.dumps(rec)
4756

57+
4858
def publish_message(producer_instance, topic_name, key, value):
4959
try:
50-
key_bytes = bytes(key, encoding='utf-8')
51-
value_bytes = bytes(value, encoding='utf-8')
52-
producer_instance.send(topic_name, key=key_bytes, value=value_bytes)
60+
key_bytes = bytes(key, encoding = 'utf-8')
61+
value_bytes = bytes(value, encoding = 'utf-8')
62+
producer_instance.send(topic_name, key = key_bytes, value = value_bytes)
5363
producer_instance.flush()
5464
print('Message published successfully.')
5565
except Exception as ex:
@@ -60,7 +70,9 @@ def publish_message(producer_instance, topic_name, key, value):
6070
def connect_kafka_producer():
6171
_producer = None
6272
try:
63-
_producer = KafkaProducer(bootstrap_servers=['localhost:9092'], api_version=(0, 10))
73+
_producer = KafkaProducer(
74+
bootstrap_servers = ['localhost:9092'], api_version = (0, 10)
75+
)
6476
except Exception as ex:
6577
print('Exception while connecting Kafka')
6678
print(str(ex))
@@ -72,7 +84,7 @@ def fetch_raw(recipe_url):
7284
html = None
7385
print('Processing..{}'.format(recipe_url))
7486
try:
75-
r = requests.get(recipe_url, headers=headers)
87+
r = requests.get(recipe_url, headers = headers)
7688
if r.status_code == 200:
7789
html = r.text
7890
except Exception as ex:
@@ -89,7 +101,7 @@ def get_recipes():
89101
print('Accessing list')
90102

91103
try:
92-
r = requests.get(url, headers=headers)
104+
r = requests.get(url, headers = headers)
93105
if r.status_code == 200:
94106
html = r.text
95107
soup = BeautifulSoup(html, 'lxml')
@@ -110,7 +122,7 @@ def get_recipes():
110122
if __name__ == '__main__':
111123
headers = {
112124
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
113-
'Pragma': 'no-cache'
125+
'Pragma': 'no-cache',
114126
}
115127

116128
all_recipes = get_recipes()

apache-bigdata/3.flask-hadoop-hive/app.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,26 @@
44
from pyhive import hive
55

66
cursor = hive.connect('localhost').cursor()
7-
cursor.execute("CREATE TABLE IF NOT EXISTS employee ( eid int, name String, salary String, destignation String) COMMENT 'employee details' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' STORED AS TEXTFILE")
8-
cursor.execute("LOAD DATA LOCAL INPATH 'sample.txt' OVERWRITE INTO TABLE employee")
7+
cursor.execute(
8+
"CREATE TABLE IF NOT EXISTS employee ( eid int, name String, salary String, destignation String) COMMENT 'employee details' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' STORED AS TEXTFILE"
9+
)
10+
cursor.execute(
11+
"LOAD DATA LOCAL INPATH 'sample.txt' OVERWRITE INTO TABLE employee"
12+
)
913

1014
app = Flask(__name__)
1115

16+
1217
@app.route('/')
1318
def hello_world():
1419
return 'Hey, we have Flask in a Docker container!'
1520

21+
1622
@app.route('/employee/<string:person>/')
1723
def get(person):
18-
cursor.execute("SELECT * FROM employee WHERE name = '%s'"%(person))
24+
cursor.execute("SELECT * FROM employee WHERE name = '%s'" % (person))
1925
return json.dumps(list(cursor.fetchall()))
2026

27+
2128
if __name__ == '__main__':
22-
app.run(debug=True, host='0.0.0.0',port=5000)
29+
app.run(debug = True, host = '0.0.0.0', port = 5000)

basic-backend/1.flask-hello/app.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
from flask import Flask
2+
23
app = Flask(__name__)
34

5+
46
@app.route('/')
57
def hello_world():
68
return 'Hey, we have Flask in a Docker container!'
79

8-
@app.route("/members")
10+
11+
@app.route('/members')
912
def members():
10-
return "you can put anything after /members/"
11-
12-
@app.route("/members/<string:name>/")
13+
return 'you can put anything after /members/'
14+
15+
16+
@app.route('/members/<string:name>/')
1317
def getMember(name):
1418
return name
1519

20+
1621
if __name__ == '__main__':
17-
app.run(debug=True, host='0.0.0.0',port=5000)
22+
app.run(debug = True, host = '0.0.0.0', port = 5000)
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
from flask import Flask, request
22
from pymongo import MongoClient
33
import json
4+
45
app = Flask(__name__)
56
client = MongoClient('localhost', 27017)
67
db = client.test_database
78
posts = db.posts
89

10+
911
@app.route('/')
1012
def hello_world():
1113
return 'Hey, we have Flask with MongoDB in a Docker container!'
1214

13-
@app.route('/insert',methods = ['GET'])
15+
16+
@app.route('/insert', methods = ['GET'])
1417
def insert():
1518
if not request.args.get('name'):
1619
return 'insert name'
17-
posts.insert_one({'name':request.args.get('name')})
18-
return 'done inserted '+ request.args.get('name')
20+
posts.insert_one({'name': request.args.get('name')})
21+
return 'done inserted ' + request.args.get('name')
1922

20-
@app.route('/get',methods = ['GET'])
23+
24+
@app.route('/get', methods = ['GET'])
2125
def get():
2226
if not request.args.get('name'):
2327
return 'insert name'
@@ -26,5 +30,6 @@ def get():
2630
except:
2731
return 'not found'
2832

33+
2934
if __name__ == '__main__':
30-
app.run(debug=True, host='0.0.0.0',port=5000)
35+
app.run(debug = True, host = '0.0.0.0', port = 5000)

basic-backend/3.flask-rest-api/app.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from flask import Flask, request
22
from flask_restful import reqparse, abort, Api, Resource
3+
34
app = Flask(__name__)
45
api = Api(app)
56

@@ -11,28 +12,33 @@
1112
'todo3': {'task': 'profit!'},
1213
}
1314

15+
1416
def abort_if_todo_doesnt_exist(todo_id):
1517
if todo_id not in TODOS:
16-
abort(404, message="Todo {} doesn't exist".format(todo_id))
18+
abort(404, message = "Todo {} doesn't exist".format(todo_id))
19+
1720

1821
parser = reqparse.RequestParser()
1922
parser.add_argument('task')
2023

24+
2125
class HelloWorld(Resource):
2226
def get(self):
2327
return {'hello': 'world'}
2428

29+
2530
class TodoSimple(Resource):
2631
def get(self, todo_id):
2732
try:
2833
return {todo_id: todos[todo_id]}
2934
except:
30-
return {'error':'todo not found'}
35+
return {'error': 'todo not found'}
3136

3237
def put(self, todo_id):
3338
todos[todo_id] = request.form['data']
3439
return {todo_id: todos[todo_id]}
3540

41+
3642
class Todo(Resource):
3743
def get(self, todo_id):
3844
abort_if_todo_doesnt_exist(todo_id)
@@ -49,6 +55,7 @@ def put(self, todo_id):
4955
TODOS[todo_id] = task
5056
return task, 201
5157

58+
5259
class TodoList(Resource):
5360
def get(self):
5461
return TODOS
@@ -60,10 +67,11 @@ def post(self):
6067
TODOS[todo_id] = {'task': args['task']}
6168
return TODOS[todo_id], 201
6269

70+
6371
api.add_resource(HelloWorld, '/')
6472
api.add_resource(TodoSimple, '/<string:todo_id>')
6573
api.add_resource(Todo, '/todos/<todo_id>')
6674
api.add_resource(TodoList, '/todos')
6775

6876
if __name__ == '__main__':
69-
app.run(debug=True, host='0.0.0.0',port=5000)
77+
app.run(debug = True, host = '0.0.0.0', port = 5000)

0 commit comments

Comments
 (0)