In [1]:
from IPython.display import YouTubeVideo
YouTubeVideo('EWOIf0_tCnk')
Out[1]:
In [7]:
def evaluate_data(data, lower=100, upper=300):
'''Counts data points in three bins.'''
smaller = 0
between = 0
bigger = 0
for length in data:
if length < lower:
smaller = smaller + 1
elif lower < length < upper:
between = between + 1
elif length > upper:
bigger = 1
return smaller, between, bigger
def read_data(filename):
'''Reads neuron lengths from a text file.'''
primary, secondary = [], []
with open(filename) as f:
for line in f:
category, length = line.split('\t')
length = float(length)
if category == 'Primary':
primary.append(length)
elif category == 'Secondary':
secondary.append(length)
return primary, secondary
def write_output(filename, count_pri, count_sec):
'''Writes counted values to a file.'''
with open(filename, 'w') as output:
output.write('category <100 100-300 >300\n')
output.write('Primary : %5i %5i %5i\n' % count_pri)
output.write('Secondary: %5i %5i %5i\n' % count_sec)
output.close()
primary, secondary = read_data('12-debugging/neuron_data.txt')
count_pri = evaluate_data(primary)
count_sec = evaluate_data(secondary)
write_output('12-debugging/results.txt', count_pri, count_sec)
SyntaxError: invalid syntax
File "<ipython-input-9-5b4413ced60a>", line 24
if category == 'Primary'
^
SyntaxError: invalid syntax
IOError: [Errno 2] No such file or directory: 'neuron_data1.txt'
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-14-f0f285f7761d> in <module>()
36 output.close()
37
---> 38 primary, secondary = read_data('12-debugging/neuron_data1.txt')
39 count_pri = evaluate_data(primary)
40 count_sec = evaluate_data(secondary)
<ipython-input-14-f0f285f7761d> in read_data(filename)
18 primary, secondary = [], []
19
---> 20 with open(filename) as f:
21 for line in f:
22 category, length = line.split('\t')
IOError: [Errno 2] No such file or directory: '12-debugging/neuron_data1.txt'
NameError: name 'write_output_file' is not defined
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-10-e355adcef24f> in <module>()
39 count_pri = evaluate_data(primary)
40 count_sec = evaluate_data(secondary)
---> 41 write_output_file('12-debugging/results.txt', count_pri, count_sec)
NameError: name 'write_output_file' is not defined
File "<ipython-input-9-5b4413ced60a>", line 24
if category == 'Primary'
^(이 symbole이 위치를 나타낸다.)
SyntaxError: invalid syntax
if category == 'Primary'
if category == 'Primary':
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-14-f0f285f7761d> in <module>()
36 output.close()
37
---> 38 primary, secondary = read_data('12-debugging/neuron_data1.txt')
39 count_pri = evaluate_data(primary)
40 count_sec = evaluate_data(secondary)
<ipython-input-14-f0f285f7761d> in read_data(filename)
18 primary, secondary = [], []
19
---> 20 with open(filename) as f:
21 for line in f:
22 category, length = line.split('\t')
IOError: [Errno 2] No such file or directory: '12-debugging/neuron_data1.txt'
파이썬이 어떻게 에러 발생한 부분을 정확히 추적해서 보여주나 봤더니 stack 에 쌓는걸 생각하면 쉽게 이해가 된다. 예전 초보때는 굉장히 신기했는데 지금 보니까 당연히 저렇게 stack에 쌓을 수 밖에 없겠구나 싶다.
syntax 문제는 없었고 실행할 때 문제가 발생했다.
이 문제를 해결하기 위해서 어떤 일이 발생하는지 분석해야 한다.
IOError(Input Output Error): file, program or website 등과 통신할 때 발생,
print filename
for line in open(filename):
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-15-e355adcef24f> in <module>()
39 count_pri = evaluate_data(primary)
40 count_sec = evaluate_data(secondary)
---> 41 write_output_file('12-debugging/results.txt', count_pri, count_sec)
NameError: name 'write_output_file' is not defined
if a > 5:
counter = 0
counter = counter + 1
In [17]:
help(dir)
In [16]:
print dir()
In [13]:
def evaluate_data(data, lower=100, upper=300):
'''Counts data points in three bins.'''
smaller = 0
between = 0
bigger = 0
for length in data:
if length < lower:
smaller = smaller + 1
elif lower < length < upper:
between = between + 1
elif length > upper:
bigger = 1
return smaller, between, bigger
def read_data(filename):
'''Reads neuron lengths from a text file.'''
primary, secondary = [], []
with open(filename) as f:
for line in f:
category, length = line.split('\t')
length = float(length)
if category == 'Primary':
primary.append(length)
elif category == 'Secondary':
secondary.append(length)
return primary, secondary
def write_output(filename, count_pri, count_sec):
'''Writes counted values to a file.'''
with open(filename, 'w') as output:
output.write('category <100 100-300 >300\n')
output.write('Primary : %5i %5i %5i\n' % count_pri)
output.write('Secondary: %5i %5i %5i\n' % count_sec)
primary, secondary = read_data('12-debugging/neuron_data.txt')
count_pri = evaluate_data(primary)
print count_pri
count_sec = evaluate_data(secondary)
write_output('12-debugging/results.txt', count_pri, count_sec)
In [25]:
!head -n 9 12-debugging/neuron_data.txt
In [8]:
!cat 12-debugging/results.txt
Exception 처리를 하지 않으면 매번 오류가 발생할 때마다 디버깅을 해야 한다.
try...except
In [15]:
try:
a = float(raw_input('Insert a number:'))
print a
except ValueError:
print "You haven't inserted a number. Please retry."
except ArithmeticError, e:
print 'ArithmeticError', e
except Exception as e:
print e
In [17]:
a = float(raw_input('Insert a number:'))
In [19]:
try:
a = float(raw_input('Insert a number:'))
print a
except ValueError as e:
print 'value error', e
except Exception as e:
print e
In [21]:
try:
print '예외를 유발할 수 있는 구문'
except:
print '예외 처리를 수행하는 구문'
else:
print '예외가 발생하지 않을 경우 수행할 구문'
In [22]:
try:
f = open('ch10.ipynb', 'r')
except IOError as e:
print e
else:
print 'ok. file open success.\n', f.read(100)
In [26]:
try:
f = open('ch.ipynb', 'r')
except IOError as e:
print e
else:
print 'ok. file open success.\n', f.read(100)
In [33]:
try:
filename = raw_input('Insert a filename:')
in_file = open(filename)
except IOError:
print 'The filename %s has not been found.' % filename
else:
for line in in_file:
print line
in_file.close()
In [34]:
!cat 12-debugging/neuron_data.txt
In [35]:
!cat 12-debugging/results.txt
In [44]:
primary, secondary = read_data('12-debugging/neuron_data.txt')
In [46]:
print primary
In [45]:
print secondary
In [47]:
count_pri = evaluate_data(primary)
In [48]:
count_sec = evaluate_data(secondary)
In [49]:
count_pri
Out[49]:
In [50]:
count_sec
Out[50]:
In [54]:
print count_sec
In [52]:
write_output('12-debugging/results.txt', count_pri, count_sec)
In [53]:
!cat 12-debugging/results.txt
In [2]:
import sys
from pprint import pprint
pprint(sys.path)
In [3]:
a = '1'
b = '2'
print a, b
result = a + b
In [5]:
print type(a), type(b)
result = a + b
In [6]:
type([1,2,3])
Out[6]:
In [7]:
type(4)
Out[7]:
In [23]:
data = [1,2,3]
In [24]:
print data[3]
In [13]:
print data
In [20]:
d = {'name':'Jo',
'Age':20,
'Sex':'M'}
In [21]:
print d
In [22]:
print d.keys()
for line in sequence_file:
for l in f:
from math import pi, sin, cos ```
In [ ]: