Skip to content

Commit abc98d4

Browse files
committed
different
1 parent e6de56e commit abc98d4

File tree

10 files changed

+353
-2
lines changed

10 files changed

+353
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Convert string `1000000` to `1.000.000` without `
2+
3+
def convert(text):
4+
parts = []
5+
while text:
6+
parts.insert(0, text[-3:])
7+
text = text[:-3]
8+
return '.'.join(parts)
9+
10+
print(convert(str(123)))
11+
print(convert(str(1234)))
12+
print(convert(str(12345)))
13+
print(convert(str(123456)))
14+
print(convert(str(1234567)))
15+
16+
Result
17+
18+
123
19+
1.234
20+
12.345
21+
123.456
22+
1.234.567
23+
24+
Using `locale` needs special files in system
25+
26+
import locale
27+
28+
# Set to users preferred locale:
29+
locale.setlocale(locale.LC_ALL, '')
30+
print('{:n}'.format(1000000))
31+
32+
# 1000000
33+
34+
# Or a specific locale:
35+
locale.setlocale(locale.LC_ALL, "en_GB.UTF-8")
36+
print('{:n}'.format(1000000))
37+
38+
# 1,000,000
39+
40+
locale.setlocale(locale.LC_ALL, "de_DE.utf-8")
41+
print('{:n}'.format(1000000))
42+
43+
# error because I don't have `de_DE` in system
44+

__other__/points-of-hundreds/main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def convert(text):
2+
parts = []
3+
while text:
4+
parts.insert(0, text[-3:])
5+
text = text[:-3]
6+
return '.'.join(parts)
7+
8+
print(convert(str(123)))
9+
print(convert(str(1234)))
10+
print(convert(str(12345)))
11+
print(convert(str(123456)))
12+
print(convert(str(1234567)))
13+
14+
15+
'''
16+
123
17+
1.234
18+
12.345
19+
123.456
20+
1.234.567
21+
'''

__scraping__/__templates__/python - scrapy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MySpider(scrapy.Spider):
1010

1111
#allowed_domains = []
1212

13-
start_urls = ['http://quotes.toqoute.com']
13+
start_urls = ['http://quotes.toscrape.com']
1414

1515
#def start_requests(self):
1616
# self.url_template = http://quotes.toscrape.com/tag/{}/page/{}/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
unit_size,unit_type,description,online_price,reg_price,store_address,store_city,store_state,store_postalcode
2+
unit_size,unit_type,description,online_price,reg_price,store_address,store_city,store_state,store_postalcode
3+
5' x 5',Climate Controlled,"Indoor access,Ground Level",$25/mo,$27,7244 Overland Rd,Orlando,FL,32810
4+
5' x 5',,"Outdoor/Drive-up access,Ground Level",Check for Availability,,7244 Overland Rd,Orlando,FL,32810
5+
5' x 10',,"Outdoor/Drive-up access,Ground Level",$46/mo,$50,7244 Overland Rd,Orlando,FL,32810
6+
10' x 5',Climate Controlled,"Indoor access,Ground Level",$57/mo,$62,7244 Overland Rd,Orlando,FL,32810
7+
5' x 10',Climate Controlled,"Indoor access,Ground Level",$67/mo,$73,7244 Overland Rd,Orlando,FL,32810
8+
5' x 10',,"Outdoor/Drive-up access,Ground Level",Check for Availability,,7244 Overland Rd,Orlando,FL,32810
9+
5' x 15',Climate Controlled,"Indoor access,Ground Level",$69/mo,$75,7244 Overland Rd,Orlando,FL,32810
10+
10' x 10',,"Outdoor/Drive-up access,Ground Level",$105/mo,$115,7244 Overland Rd,Orlando,FL,32810
11+
10' x 10',Climate Controlled,"Indoor access,Ground Level",$105/mo,$115,7244 Overland Rd,Orlando,FL,32810
12+
10' x 10',Climate Controlled,"Indoor access,Ground Level",$124/mo,$136,7244 Overland Rd,Orlando,FL,32810
13+
10' x 15',,"Outdoor/Drive-up access,Ground Level",$144/mo,$158,7244 Overland Rd,Orlando,FL,32810
14+
10' x 16',,"Outdoor/Drive-up access,Ground Level",$145/mo,$159,7244 Overland Rd,Orlando,FL,32810
15+
10' x 15',Climate Controlled,"Indoor access,Ground Level",$149/mo,$163,7244 Overland Rd,Orlando,FL,32810
16+
10' x 18',,"Outdoor/Drive-up access,Ground Level",$149/mo,$163,7244 Overland Rd,Orlando,FL,32810
17+
10' x 15',Climate Controlled,"Indoor access,Ground Level",Check for Availability,,7244 Overland Rd,Orlando,FL,32810
18+
10' x 20',,"Outdoor/Drive-up access,Ground Level",$147/mo,$161,7244 Overland Rd,Orlando,FL,32810
19+
10' x 25',Climate Controlled,"Indoor access,Ground Level",$175/mo,$192,7244 Overland Rd,Orlando,FL,32810
20+
10' x 20',Climate Controlled,"Indoor access,Ground Level",Check for Availability,,7244 Overland Rd,Orlando,FL,32810
21+
10' x 28',,"Outdoor/Drive-up access,Ground Level",Check for Availability,,7244 Overland Rd,Orlando,FL,32810
22+
41' x 41',,"Outdoor/Drive-up access,Ground Level",$1400/mo,$1540,7244 Overland Rd,Orlando,FL,32810
23+
22' x 25',,"Outdoor/Drive-up access,Ground Level",Check for Availability,,7244 Overland Rd,Orlando,FL,32810
24+
18' x 38',,"Outdoor/Drive-up access,Ground Level",Check for Availability,,7244 Overland Rd,Orlando,FL,32810

__scraping__/lifestorage.com/main.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from urllib.request import urlopen as uReq
2+
from bs4 import BeautifulSoup as soup
3+
import csv
4+
5+
urls = [
6+
'https://www.lifestorage.com/storage-units/florida/orlando/32810/610-near-lockhart/?size=5x5'
7+
]
8+
9+
filename = 'life_storage.csv'
10+
11+
f = open(filename, 'a+')
12+
csv_writer = csv.writer(f)
13+
14+
headers = ['unit_size', 'unit_type', 'description', 'online_price', 'reg_price', 'store_address', 'store_city', 'store_state', 'store_postalcode' ]
15+
16+
##unit_size = 5'x10' withouth the '
17+
##unit_type = climate controlled or not (this could be blank if non-climate)
18+
##descirption = the level it's on and type of access.
19+
##online_price = $##/mo text
20+
##reg_price = the scratched off $## text
21+
22+
csv_writer.writerow(headers)
23+
24+
for my_url in urls:
25+
uClient = uReq(my_url)
26+
page_html = uClient.read()
27+
uClient.close()
28+
page_soup = soup(page_html, 'html.parser')
29+
30+
store_location = page_soup.find("div", {"itemprop": "address"})
31+
32+
# need `li`
33+
containers = page_soup.find("ul", {"id": "spaceList"}).findAll('li')
34+
print('len(containers):', len(containers))
35+
36+
item = store_location.find("span", {"itemprop": "streetAddress"})
37+
store_address = item.text.strip()
38+
39+
item = store_location.find("span", {"itemprop": "addressLocality"})
40+
store_city = item.text.strip()
41+
42+
item = store_location.find("span", {"itemprop": "addressRegion"})
43+
store_state = item.text.strip()
44+
45+
item = store_location.find("span", {"itemprop": "postalCode"})
46+
store_postalcode = item.text.strip()
47+
48+
for container in containers:
49+
item = container.find("div", {"class": "storesRow"})
50+
51+
if item and item.strong:
52+
text = item.strong.text.strip()
53+
parts = text.split('-')
54+
if len(parts) > 0:
55+
unit_size = parts[0].strip().replace('*', "")
56+
else:
57+
unit_size = ''
58+
59+
if len(parts) > 1:
60+
unit_type = parts[1].strip()
61+
else:
62+
unit_type = ''
63+
else:
64+
continue
65+
66+
item = container.find("ul", {"class": "features"})
67+
68+
if item:
69+
description = item.text.strip().replace("\n", ',')
70+
else:
71+
description = ''
72+
73+
item = container.find("div", {"class": "priceBox"})
74+
75+
if item and item.i:
76+
reg_price = item.i.text.strip()
77+
else:
78+
reg_price = ''
79+
80+
if item and item.strong:
81+
if item.i:
82+
item.i.extract() # remove <i>`
83+
online_price = item.strong.text.strip()
84+
else:
85+
online_price = ''
86+
87+
csv_writer.writerow([unit_size, unit_type, description, online_price, reg_price, store_address, store_city, store_state, store_postalcode])
88+
89+
f.close()

scrapy/__template__/python - scrapy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MySpider(scrapy.Spider):
1010

1111
#allowed_domains = []
1212

13-
start_urls = ['http://quotes.toqoute.com']
13+
start_urls = ['http://quotes.toscrape.com']
1414

1515
#def start_requests(self):
1616
# self.url_template = http://quotes.toscrape.com/tag/{}/page/{}/

scrapy/run-with-argument/main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# $ scrapy crawl myspider -a word="abba"
5+
#
6+
7+
import scrapy
8+
9+
class MySpider(scrapy.Spider):
10+
11+
name = 'myspider'
12+
13+
start_urls = ['http://quotes.toscrape.com']
14+
15+
def __init__(self, word=None, *args, **kwargs): # <--- receive parameter
16+
super().__init__(*args, **kwargs)
17+
self.word = word # <--- receive parameter
18+
19+
def parse(self, response):
20+
print('url:', response.url)
21+
print('word:', self.word) # <--- use parameter
22+
23+
# --- it runs without project and saves in `output.csv` ---
24+
25+
from scrapy.crawler import CrawlerProcess
26+
27+
c = CrawlerProcess({
28+
'USER_AGENT': 'Mozilla/5.0',
29+
})
30+
c.crawl(MySpider, word='tags') # <--- send parameter
31+
c.start()

scrapy/run-with-argument/output.csv

Whitespace-only changes.

tkinter/validate/main-2.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import tkinter as tk
2+
3+
#
4+
# https://stackoverflow.com/questions/4140437/interactively-validating-entry-widget-content-in-tkinter/4140988#4140988
5+
#
6+
7+
def convert(text):
8+
parts = []
9+
while text:
10+
parts.insert(0, text[-3:])
11+
text = text[:-3]
12+
return '.'.join(parts)
13+
14+
def check(d, i, P, s, S, v, V, W):
15+
print("d='%s'" % d)
16+
print("i='%s'" % i)
17+
print("P='%s'" % P)
18+
print("s='%s'" % s)
19+
print("S='%s'" % S)
20+
print("v='%s'" % v)
21+
print("V='%s'" % V)
22+
print("W='%s'" % W)
23+
print('-----')
24+
25+
if V == 'focusin':
26+
print('if focus in')
27+
text = P
28+
29+
# remove points of hundreds
30+
text = text.replace('.', '')
31+
32+
e.delete('0', 'end')
33+
e.insert('end', text)
34+
35+
return True
36+
37+
if V == 'focusout':
38+
print('if focus out')
39+
text = P
40+
41+
# add points of hundreds
42+
parts = text.split(',')
43+
44+
if len(parts) > 0:
45+
parts[0] = convert(parts[0])
46+
text = ','.join(parts)
47+
48+
#e.delete('0', 'end')
49+
#e.insert('end', text)
50+
51+
return True
52+
53+
if V == 'key':
54+
print('if key')
55+
text = P
56+
57+
# validate
58+
parts = text.split(',')
59+
parts_number = len(parts)
60+
61+
if parts_number > 2:
62+
print('too much dots')
63+
return False
64+
65+
if parts_number > 1 and parts[1]: # don't check empty string
66+
if not parts[1].isdecimal() or len(parts[1]) > 2:
67+
print('wrong second part')
68+
return False
69+
70+
if parts_number > 0 and parts[0]: # don't check empty string
71+
if not parts[0].isdecimal() or len(parts[0]) > 8:
72+
print('wrong first part')
73+
return False
74+
75+
return True
76+
77+
# --- main ---
78+
79+
root = tk.Tk()
80+
81+
vcmd = (root.register(check), '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
82+
83+
e = tk.Entry(root, validate='all', validatecommand=vcmd)
84+
e.pack()
85+
86+
b = tk.Button(root, text='Other widget to set focus')
87+
b.pack()
88+
89+
root.mainloop()

tkinter/validate/main.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
#
3+
# https://stackoverflow.com/a/47990190/1832058
4+
#
5+
6+
import tkinter as tk
7+
8+
'''
9+
It lets you input only numbers 8.2 digits.
10+
11+
'''
12+
def check(d, i, P, s, S, v, V, W):
13+
print("d='%s'" % d)
14+
print("i='%s'" % i)
15+
print("P='%s'" % P)
16+
print("s='%s'" % s)
17+
print("S='%s'" % S)
18+
print("v='%s'" % v)
19+
print("V='%s'" % V)
20+
print("W='%s'" % W)
21+
22+
text = P #e.get()
23+
print('text:', text)
24+
25+
parts = text.split('.')
26+
parts_number = len(parts)
27+
28+
if parts_number > 2:
29+
#print('too much dots')
30+
return False
31+
32+
if parts_number > 1 and parts[1]: # don't check empty string
33+
if not parts[1].isdecimal() or len(parts[1]) > 2:
34+
#print('wrong second part')
35+
return False
36+
37+
if parts_number > 0 and parts[0]: # don't check empty string
38+
if not parts[0].isdecimal() or len(parts[0]) > 8:
39+
#print('wrong first part')
40+
return False
41+
42+
return True
43+
44+
# --- main ---
45+
46+
root = tk.Tk()
47+
48+
vcmd = (root.register(check), '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
49+
50+
e = tk.Entry(root, validate='key', validatecommand=vcmd)
51+
e.pack()
52+
53+
root.mainloop()

0 commit comments

Comments
 (0)