Skip to content

Commit dc9f66c

Browse files
committed
new examples
1 parent ca96ff7 commit dc9f66c

File tree

45 files changed

+952
-54
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+952
-54
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
3+
# date: 2019.08.14
4+
# https://stackoverflow.com/questions/57490072/how-to-crawl-a-website-to-get-all-the-links-in-a-website-using-scrapy-in-python/57490431#57490072
5+
6+
import scrapy
7+
8+
class MySpider(scrapy.Spider):
9+
10+
name = "MySpider"
11+
12+
def __init__(self, allowed_domains=None, start_urls=None):
13+
super().__init__()
14+
15+
# self.name = name
16+
if allowed_domains is None:
17+
self.allowed_domains = []
18+
else:
19+
self.allowed_domains = allowed_domains
20+
21+
if start_urls is None:
22+
self.start_urls = []
23+
else:
24+
self.start_urls = start_urls
25+
26+
def parse(self, response):
27+
print('[parse] url:', response.url)
28+
29+
# extract all links from page
30+
all_links = response.xpath('*//a/@href').extract()
31+
32+
# iterate over links
33+
for link in all_links:
34+
print('[+] link:', link)
35+
#yield scrapy.http.Request(url="http://www.myntra.com" + link, callback=self.print_this_link)
36+
full_link = response.urljoin(link)
37+
yield scrapy.http.Request(url=full_link, callback=self.print_this_link)
38+
39+
40+
def print_this_link(self, response):
41+
print('[print_this_link] url:', response.url)
42+
title = response.xpath('//title/text()').get() # get() will replace extract() in the future
43+
body = response.xpath('//body//text()').getall()
44+
yield {'url': response.url, 'title': title, 'body': body}
45+
46+
47+
# --- run without creating project and save in `output.csv` ---
48+
49+
from scrapy.crawler import CrawlerProcess
50+
51+
c = CrawlerProcess({
52+
'USER_AGENT': 'Mozilla/5.0',
53+
54+
# save in file as CSV, JSON or XML
55+
'FEED_FORMAT': 'csv', # csv, json, xml
56+
'FEED_URI': 'output.csv', #
57+
})
58+
c.crawl(MySpider)
59+
c.crawl(MySpider, allowed_domains=["myntra.com"], start_urls=["http://www.myntra.com/"])
60+
c.start()
61+

class/cricket-player-team/main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import random
2+
3+
class Player():
4+
5+
def __init__(self, name):
6+
self.name = name
7+
self.score = 0
8+
9+
def run(self):
10+
self.score += random.randint(0, 6)
11+
12+
def __str__(self):
13+
return "{} (score: {})".format(self.name, self.score)
14+
15+
class Team():
16+
17+
def __init__(self, players):
18+
self.players = players
19+
self.current_batsman = 0
20+
self.current_run = 0
21+
22+
def set_next_batsman(self):
23+
self.current_batsman += 1
24+
if self.current_batsman >= len(self.players):
25+
self.current_batsman = 0
26+
27+
def get_current_batsman(self):
28+
return self.players[self.current_batsman]
29+
30+
def run(self):
31+
self.players[self.current_batsman].run()
32+
33+
if self.current_run % 2 != 0:
34+
self.set_next_batsman()
35+
36+
self.current_run += 1
37+
38+
def __str__(self):
39+
return "Player: " + ", ".join(str(p) for p in self.players)
40+
41+
def total_score(self):
42+
return sum(p.score for p in self.players)
43+
44+
team1 = Team( [Player("a"), Player("b"), Player("c")] )
45+
team2 = Team( [Player("x"), Player("y"), Player("z")] )
46+
47+
print('Team1:', team1)
48+
print('Team2:', team2)
49+
50+
for number in range(1, 5):
51+
print('Round:', number)
52+
print('Team1 current batsman:', team1.get_current_batsman())
53+
team1.run()
54+
print('Team2 current batsman:', team2.get_current_batsman())
55+
team2.run()
56+
57+
print('Team1:', team1)
58+
print('Team2:', team2)
59+
60+
print('Team1 total score:', team1.total_score())
61+
print('Team2 total score:', team2.total_score())
62+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import re
2+
3+
data = ['2d1h39m53s', '1h39m53s', '39m53s', '53s']
4+
5+
for item in data:
6+
rs = re.search('(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?', item)
7+
print(rs.groups())
8+

decode-encode/base64-tiff-html/main-png.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

decode-encode/base64-tiff-html/main-tiff.py

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
#------------------------------------------------------------------------------
3+
# decode from base64 to png
4+
#------------------------------------------------------------------------------
5+
6+
import base64
7+
8+
html = '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAAAAACoWZBhAAAAMklEQVR4nEXNwQkAMBACwfFI/y2bxwXia8FFUwF1pCCNJTIIwaD04ctsKRyaldI/9i8u1iwOE6FA880AAAAASUVORK5CYII=">'
9+
10+
data_base64 = html.split('base64,')[1] # remove text before encoded data
11+
data_base64 = data_base64[:-2] # remove text after encoded data
12+
13+
#data_base64 = 'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAAAAACoWZBhAAAAMklEQVR4nEXNwQkAMBACwfFI/y2bxwXia8FFUwF1pCCNJTIIwaD04ctsKRyaldI/9i8u1iwOE6FA880AAAAASUVORK5CYII='
14+
15+
data_base64 = data_base64.encode() # convert string to bytes
16+
data = base64.b64decode(data_base64) # decode from base64 (bytes)
17+
open('image.png', 'wb').write(data) # write bytes to file
18+
19+
20+
21+
#------------------------------------------------------------------------------
22+
# encode png to base64 and embed in html
23+
#------------------------------------------------------------------------------
24+
25+
import base64
26+
27+
data = open('image.png', 'rb').read() # read bytes from file
28+
data_base64 = base64.b64encode(data) # encode to base64 (bytes)
29+
data_base64 = data_base64.decode() # convert bytes to string
30+
31+
print(data_base64)
32+
33+
html = '<img src="data:image/png;base64,' + data_base64 + '">' # embed in html
34+
open('output-png.html', 'w').write(html)
35+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
#------------------------------------------------------------------------------
3+
# decode from base64 to tiff
4+
#------------------------------------------------------------------------------
5+
6+
import base64
7+
8+
html = '<img src="data:image/tiff;base64,SUkqAAgAAAASAP4ABAABAA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YFAwRsMzKeuyIyKe5mZ80MoVKtCjXo0ayatSsoUoQlcAtQDHQgIx8EYiIiIj5ER4H/9l4nPv//z8DAwMjIyMDKvgPFgeSQCkIGxnABTGl4ArgsnDFyAwi9eKRRXMYhItmL5p34Gpw+QhTI7J2AOTAPvUA">'
9+
10+
data_base64 = html.split('base64,')[1] # remove text before encoded data
11+
data_base64 = data_base64[:-2] # remove text after encoded data
12+
13+
#data_base64 = 'SUkqAAgAAAASAP4ABAABAAAAAAAAAAABAwABAAAACgAAAAEBAwABAAAACgAAAAIBAwADAAAA5gAAAAMBAwABAAAAsoAAAAYBAwABAAAAAgAAABEBBAABAAAAgAMAABIBCQABAAAAAQAAABUBAwABAAAAAwAAABYBAwABAAAAKAAAABcBBAABAAAAPwAAABoBBQABAAAA7AAAABsBBQABAAAA9AAAABwBAwABAAAAAQAAACgBAwABAAAAAgAAADIBAgAUAAAA/AAAAD0BAwABAAAAAgAAAGmHBAABAAAAEAEAAF4BAAAIAAgACABIAAAAAQAAAEgAAAABAAAAMjAxOTowNzoyMyAyMTozNDoxOQAGAACQBwAEAAAAMDIyMQGRBwAEAAAAAQIDAACgBwAEAAAAMDEwMAGgCQABAAAAAQAAAAKgCQABAAAACgAAAAOgCQABAAAACgAAAAAAAAAGAAMBAwABAAAABgAAABoBCQABAAAASAAAABsBCQABAAAASAAAACgBCQABAAAAAgAAAAECBAABAAAArAEAAAICBAABAAAA1AEAAAAAAAD/2P/gABBKRklGAAEBAAABAAEAAP/bAEMABQMEBAQDBQQEBAUFBQYHDAgHBwcHDwsLCQwRDxISEQ8RERMWHBcTFBoVEREYIRgaHR0fHx8TFyIkIh4kHB4fHv/bAEMBBQUFBwYHDggIDh4UERQeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHv/AABEIAAoACgMBIgACEQEDEQH/xAAXAAADAQAAAAAAAAAAAAAAAAAABwgJ/8QAIBAAAgICAgMBAQAAAAAAAAAAAgMBBAUGBxEACCETEv/EABQBAQAAAAAAAAAAAAAAAAAAAAD/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwBi60fFuU1XB5beI0a3tWVxGMv2RzeECzk77nALXymDn9LIsE4UkEjIrMJWMH/H4hOfJXsNzHq3Iuy6xhd0cnF4jL2qFJbqVawwEpcS1wTWrJjCgRjszIiKfszMzM+Htlu+6at7CbVhdY2/YMHi1NQ5dLHZJ1ZAG2sprThYFAwRsMzKeuyIyKe5mZ80MoVKtCjXo0ayatSsoUoQlcAtQDHQgIx8EYiIiIj5ER4H/9l4nPv//z8DAwMjIyMDKvgPFgeSQCkIGxnABTGl4ArgsnDFyAwi9eKRRXMYhItmL5p34Gpw+QhTI7J2AOTAPvUA'
14+
15+
data_base64 = data_base64.encode() # convert string to bytes
16+
data = base64.b64decode(data_base64) # decode from base64 (bytes)
17+
open('image.tiff', 'wb').write(data) # write bytes to file
18+
19+
20+
21+
#------------------------------------------------------------------------------
22+
# encode tiff to base64 and embed in html
23+
#------------------------------------------------------------------------------
24+
25+
import base64
26+
27+
data = open('image.tiff', 'rb').read() # read bytes from file
28+
data_base64 = base64.b64encode(data) # encode to base64 (bytes)
29+
data_base64 = data_base64.decode() # convert bytes to string
30+
31+
print(data_base64)
32+
33+
html = '<img src="data:image/tiff;base64,' + data_base64 + '">' # embed in html
34+
open('output-tiff.html', 'w').write(html)
35+
36+
# browsers don't display tiff so html can show broken image.
37+
# you have to save from page to file and then you can see tiff.

flask/upload-file/main-args.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from flask import Flask, request, redirect, make_response, render_template_string
2+
3+
4+
app = Flask(__name__)
5+
6+
7+
@app.route("/")
8+
def index():
9+
args_filename = request.args.get('filename', '')
10+
11+
return render_template_string('''
12+
Previous filename: {{ filename }}
13+
<hr>
14+
<form action="getfile" method="POST" enctype="multipart/form-data">
15+
Project file path: <input type="file" name="config_file"><br>
16+
<input type="submit" value="Submit">
17+
</form>
18+
''', filename=args_filename)
19+
20+
21+
@app.route('/getfile', methods=['GET','POST'])
22+
def getfile():
23+
#print('args:', request.args)
24+
#print('form:', request.form)
25+
#print('files:', request.files)
26+
27+
result = request.files.get('config_file')
28+
29+
if result:
30+
result.save(result.filename) # TODO: save with unique name which couldn't overwrite file with passwords
31+
return redirect('/?filename={}'.format(result.filename))
32+
33+
return redirect('/')
34+
35+
36+
app.run()

flask/upload-file/main-cookie.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from flask import Flask, request, redirect, make_response, render_template_string
2+
3+
4+
app = Flask(__name__)
5+
6+
7+
@app.route("/")
8+
def index():
9+
10+
cookies_filename = request.cookies.get('filename', '')
11+
12+
return render_template_string('''
13+
Previous filename: {{ filename }}
14+
<hr>
15+
<form action="getfile" method="POST" enctype="multipart/form-data">
16+
Project file path: <input type="file" name="config_file"><br>
17+
<input type="submit" value="Submit">
18+
</form>
19+
''', filename=cookies_filename)
20+
21+
22+
@app.route('/getfile', methods=['GET','POST'])
23+
def getfile():
24+
#print('args:', request.args)
25+
#print('form:', request.form)
26+
#print('files:', request.files)
27+
28+
response = make_response(redirect('/'))
29+
30+
result = request.files.get('config_file')
31+
32+
if result:
33+
result.save(result.filename) # TODO: save with unique name which couldn't overwrite file with passwords
34+
response.set_cookie('filename', result.filename)
35+
36+
return response
37+
38+
39+
app.run()

flask/waitress-server/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
```
3+
from flask import Flask
4+
5+
def create_app():
6+
app = Flask(__name__)
7+
8+
@app.route('/')
9+
def index():
10+
return "Hello World!"
11+
12+
return app
13+
14+
if __name__ == '__main__':
15+
app = create_app()
16+
app.run()
17+
```
18+
19+
If code is in file `main.py` then you can run it as
20+
21+
waitress-serve --call "main:create_app"
22+
23+
If you forget `:` then you can see `"Malformed application"`
24+
25+
---
26+
27+
You can still run it as
28+
29+
python main.py
30+
31+
----
32+
33+
[Waitress 1.3.0](https://docs.pylonsproject.org/projects/waitress/en/stable/runner.html)
34+
35+
---
36+
37+
https://stackoverflow.com/questions/25254022/flask-are-blueprints-necessary-for-app-factories
38+
39+
https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/

flask/waitress-server/main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from flask import Flask
2+
3+
def create_app():
4+
app = Flask(__name__)
5+
6+
@app.route('/')
7+
def index():
8+
return "HELLO WORLD"
9+
10+
return app
11+
12+
if __name__ == '__main__':
13+
app = create_app()
14+
app.run()

0 commit comments

Comments
 (0)