hw 4, 5, 6
if log.rfind("dummy connection") != -1:
...
if "dummy connection" in log:
...
my_dict = {
'2007.01.01': 14,
'2007.02.01': 17,
}
if key in my_dict.keys():
...
my_dict = {
'2007.01.01': 14,
'2007.02.01': 17,
}
if key in my_dict:
...
number_to_name = {
"Jan": 1, "Feb": 2, "Mar": 3,
"Apr": 4, "May": 5, "Jun": 6,
"Jul": 7, "Aug": 8, "Sep": 9,
"Oct": 10, "Nov": 11, "Dec": 12,
}
import calendar
monthes = list(calendar.month_abbr)
# >> ['', 'Jan', 'Feb', 'Mar', 'Apr',
# 'May', 'Jun', 'Jul', 'Aug', 'Sep',
# 'Oct', 'Nov', 'Dec']
import calendar
monthes = list(calendar.month_abbr)
# >> ['', 'Jan', 'Feb', 'Mar', 'Apr',
# 'May', 'Jun', 'Jul', 'Aug', 'Sep',
# 'Oct', 'Nov', 'Dec']
number_to_name = {
month: idx
for idx, month in
enumerate(monthes[1:], start=1)
}
# {'Jan': 1,
# 'Feb': 2,
# 'Mar': 3,
# ...
def get_next_or_none(it):
try:
obj = next(it)
except StopIteration:
return None
return obj
def get_next_or_none(it):
return next(it, None)
from collections import OrderedDict
- python3.6: обычные словари упорядочены из-за деталей реализации (грубо говоря, так случайно получилось)
- python3.7+: обычные словари упорядочены, и это гарантированная фича языка
real = [i for i in merge(f1, f2)]
real = list(merge(f1, f2))
def all_is_none(seq):
not_none = list(
filter(lambda x: x is None, seq)
)
return len(not_none) == len(seq)
def all_is_none2(seq):
return all(x is None for x in seq)
all_is_none([]), all_is_none2([])
# True, True
class TestMerge(unittest.TestCase):
def test_one_sequence(self):
f = [1, 2, 3]
for i, el in enumerate(merge(f)):
self.assertEqual(el, f[i])
def merge(*iterables, key=None):
yield from []
def merge(*iterables, key=None):
yield from []
from itertools import zip_longest
class TestMerge(unittest.TestCase):
def test_one_sequence(self):
f = [1, 2, 3]
for i1, i2 in zip_longest(f, merge(f)):
self.assertEqual(i1, i2)
class TestMerge(unittest.TestCase):
def test_one_sequence(self):
f = [1, 2, 3]
real = list(merge(f))
self.assertEqual(f, real)
class TestMerge(unittest.TestCase):
def test1(self):
a, b = [1, 2], [3, 4]
self.assertEqual(...)
def test1(self):
a = [4, 3, 2, 1]
b = [6, 5, 4, 3]
self.assertEqual(...)
def test1():
print(1)
def test1():
print(2)
test1() # 2
-
pep8
- flake8
- Обращайте внимание на подсказки IDE
- Используйте re.compile (но объявляйте его до цикла)
- Открывайте файлы через with
- collections.defaultdict и collections.Counter
08 hw456
By persi
08 hw456
- 115