In [1]:
import logging
import functools
import itertools
import numpy as np
logging.root.setLevel(logging.DEBUG)
In [2]:
import this
In [3]:
title = "title"
print("<html>")
print("<head>")
print("<title>" + title + "</title>")
print("</head>")
print("<body>")
print("<h1>" + title + "</h1>")
print("</body>")
print("</html>")
In [4]:
template = """<html>
<head>
<title>{title:s}</title>
</head>
<body>
<h1>{title:s}</h1>
</body>
</html>
"""
print(template.format(title=title))
In [5]:
a = [2,3,5]
sum_of_squares = 0
for i in range(len(a)):
sum_of_squares = sum_of_squares + a[i] * a[i]
sum_of_squares
Out[5]:
In [6]:
sum(x**2 for x in a)
Out[6]:
In [11]:
(letter for letter in 'word')
Out[11]:
In [7]:
arr = np.array(a)
(arr**2).sum()
Out[7]:
In [13]:
colors = ["red", "yellow", "blue", "purple"]
In [14]:
for i in range(len(colors)):
print(colors[i])
In [15]:
for color in colors:
print(color)
In [18]:
sorted_colors = colors[:]
sorted_colors.sort()
for color in sorted_colors:
print(color)
In [12]:
for color in sorted(colors):
print(color)
In [13]:
for i in range(len(colors)):
print("{}: {}".format(i, colors[i]))
In [14]:
for i, color in enumerate(colors):
print("{}: {}".format(i, color))
In [15]:
variables = ["height", "length", "width", "volume"]
for i in range(len(variables)):
variable = variables[i]
color = colors[i]
print("{i}: {color}, {variable}".format(i=i, color=color, variable=variable))
In [19]:
zip([1,2,3], ['a', 'b', 'c'])
Out[19]:
In [16]:
variables = ["height", "length", "width", "volume"]
for i, (variable, color) in enumerate(zip(variables, colors)):
print("{i}: {color}, {variable}".format(i=i, color=color, variable=variable))
In [17]:
letters = ["C","a", "b"]
list(sorted(letters))
Out[17]:
In [18]:
def case_independent_cmp(a, b):
return cmp(a.lower(), b.lower())
list(sorted(letters, cmp=case_independent_cmp))
Out[18]:
In [19]:
list(sorted(letters, key=str.lower))
Out[19]:
In [22]:
from math import *
from numpy import *
import logging
log([1,2,3, 2])
Out[22]:
In [23]:
from numpy import *
from math import *
try:
log([1,2,3, 2])
except:
logging.exception("that does not compute")
In [22]:
import numpy as np
np.log([1,2,3, 2])
Out[22]:
In [24]:
"it's %d o'clock" % (3,)
Out[24]:
In [23]:
try:
"It is " +3 + " o'clock"
except:
logging.exception("Strong typing please!")
In [24]:
"It is {hours:d} o'clock".format(hours=3)
Out[24]:
In [25]:
DEBUG = True
def calculate():
if DEBUG:
print("starting calculate")
1 + 1
if DEBUG:
print("finished calculate")
calculate()
In [26]:
def log_call(func):
logging.debug("starting {}")
func()
logging.debug("finished {}")
@log_call
def calculate():
1 + 1
In [27]:
# in java:
# import org.apache.xerces.impl.xpath.regex.EXIRegularExpression;
import re
In [28]:
import requests
import xml.etree.ElementTree
url = 'http://live.waterbase.nl/metis/cgi-bin/mivd.pl?action=value&code=DOOVBWT&format=xml&lang=nl&order=code&type=loc'
doc = xml.etree.ElementTree.fromstring(requests.get(url).content)
doc.getchildren()[0].\
getchildren()[0].\
getchildren()[0].\
getchildren()[2].\
getchildren()[0].\
getchildren()[0].\
getchildren()[0].\
getchildren()[0].\
text
Out[28]:
In [29]:
doc.find('.//gml:Coordinates', namespaces={'gml': 'http://www.opengis.net/gml'}).text
Out[29]:
In [30]:
try:
pay_salary(amount)
except:
pass
In [25]:
def transfer(amount, account):
if not isinstance(amount, int):
raise TypeError("expected integer, got %s" % (type(amount), ))
print("paying %d to %s" % (amount, account))
extra = 0
amount = 23.43
for i in range(5):
try:
# try if we can transfer
transfer(amount, "bank")
except TypeError:
# amount is a floating point
transfer(int(amount), "bank")
extra += amount - int(amount)
if extra > 1:
transfer(int(extra), "fedor")
extra -= int(extra)
In [ ]: