# Python
## url
https://bitbucket.org/
https://jsfiddle.net/
https://ideone.com/
https://www.codeground.org/
http://pythonfiddle.com/
https://www.tutorialspoint.com/
https://replit.com/
https://www.heroku.com/
https://www.pythonanywhere.com/
https://www.digitalocean.com/
https://www.linode.com/
https://ngrok.com/
https://jsonformatter.curiousconcept.com/
https://firebase.google.com/
## main
```python=
if __name__ == '__main__':
start_time = time.perf_counter()
main()
end_time = time.perf_counter()
print(f'[Finished in {(end_time - start_time):.1f}s]')
```
## today
```python=
today = datetime.date.today()
print('today:', today)
# today: 2021-10-12
today = datetime.datetime.today()
print('today:', today)
# today: 2021-10-12 21:59:05.578499
print('today:', datetime.date.today().isocalendar())
# today: datetime.IsoCalendarDate(year=2021, week=41, weekday=2)
today = datetime.date(today.year, today.month, today.day)
print('today:', today)
# today: 2021-10-12
```
## disk
```python=
total, used, free = shutil.disk_usage('/')
print('total:', total / (2**30))
print('used:', used / (2**30))
print('free:', free / (2**30))
log_files = glob.glob('C:\Windows\*.log')
print('log_files:', log_files)
for f in log_files:
print(os.stat(f).st_size)
print(os.path.getsize(f))
from pathlib import Path
print(Path(f).stat().st_size) # better
for dirname, subdir, files in os.walk('C:\Windows')
print(dirname)
print(subdir)
print(files)
for f in p.glob('**/*.*'):
print(f.name)
from pathlib import Path
for x in Path('/tmp').iterdir():
print(type(x), x)
```
## type
```python=
list: a = ['A', 'B', 'C']
tuple: b = ('A', 'B', 'C')
dict: c = {'A':1, 'B':2, 'C':3}
set: d = {'A', 'B', 'C'}
```
## build-in functions
https://docs.python.org/3/library/functions.html

## file
```python=
with open('filename', 'r') as fp:
fp.readlines()
with open('file.json', 'r') as fp:
xxx = json.load(fp)
```
## db
```python=
dirname = os.path.dirname(os.path.abspath(__file__))
print(dirname)
con = sqlite3.connect(os.path.join(dirname, 'MySQLite.db'))
cur = con.cursor()
cur.execute('select * from scores')
rows = cur.fetchall()
for row in rows:
print(row[0], row[1], row[2])
con.close()
# efficient way
import sqlite3
con = sqlite3.connect(":memory:")
con.execute("create table lang (id integer primary key, name varchar unique)")
# Successful, con.commit() is called automatically afterwards
with con:
con.execute("insert into lang(name) values (?)", ("Python",))
# con.rollback() is called after the with block finishes with an exception, the
# exception is still raised and must be caught
try:
with con:
con.execute("insert into lang(name) values (?)", ("Python",))
except sqlite3.IntegrityError:
print("couldn't add Python twice")
# Connection object used as context manager only commits or rollbacks transactions,
# so the connection object should be closed manually
con.close()
```
## web
```python=
urllib.parse
url = 'https://tw.yahoo.com/'
uc = urlparse(url)
html = requests.get(url).text
bs = BeautifulSoup(html, 'html.parser')
all_links = bs.find_all(['a', 'img'])
web = webdriver.Firefox()
web.get('https://tw.yahoo.com')
web.quit()
```
## sort
https://docs.python.org/3/howto/sorting.html#sortinghowto
a is a list
```python=
b = sorted(a) # return new list
a.list() # sort in-place
```
## lambda
```python=
x = range(1, 10)
y = map(lambda i: i**3, x)
for i, value in enumerate(y):
...
```