<jupyter_start><jupyter_text>Debugging<jupyter_code># Get Cheatsheet 


def bad_function():
    
    for i in range(4):
        i += 2
    
    if i == 3:
        print('Finished')

bad_function()<jupyter_output><empty_output><jupyter_text>Print<jupyter_code>def meh_function():
    
    for i in range(10):
        print(i)
        i += 2
        print(i)
    
    if i == 10:
        print('Finished')

meh_function()<jupyter_output><empty_output><jupyter_text>Logging<jupyter_code>import logging

def main():
    logging.basicConfig(filename='sample.log', level=logging.INFO)
    
    logging.error('Start looping...')
    for item in range(10):
        item += 1
        logging.debug(f'item = {item}')
    logging.debug('...end looping.')
    
    if item == 10:
        print('Finished')
        logging.info('Finished')
        
    logging.info('~ Fin ~')

if __name__ == '__main__':
    main()<jupyter_output><empty_output><jupyter_text>Use logging levels.| Level |  ||:---:|:---:|| Critical | 50|| Error | 40 || Warning | 30 || Info | 20 || Debug | 10 || Notset | 0 |<jupyter_code>import logging

def main():
    logging.basicConfig(filename='sample.log',
                        level=logging.INFO)
    
    logging.info('Start looping...')
    for item in range(10):
        item += 1
        logging.debug(f'item = {item}')
    logging.debug('...end looping.')
    
    if item == 10:
        print('Finished')
        logging.debug('Finished')
        
    logging.info('~ Fin ~')

if __name__ == '__main__':
    main()<jupyter_output><empty_output><jupyter_text>Too much logging...<jupyter_code>with open('list_of_numbers.txt', 'w') as f:
    for i in range(1, 200000):
        f.write(f'{i},')
def broken_sum():
    results = []
    with open('list_of_numbers.txt') as f:
        for number in f:
            results.append(number)

    print(f'The sum is {sum(results)}')

broken_sum()
def still_broken_sum():
    results = []
    with open('list_of_numbers.txt') as f:
        for number in f:
            print(number)
            results.append(number)

    print(f'The sum is {sum(results)}')

still_broken_sum()<jupyter_output><empty_output><jupyter_text>Debugging* `$ python -m pbd file_name.py`* `import pdb; pdb.set_trace()`<jupyter_code>def debug_sum():
    results = []
    with open('list_of_numbers.txt') as f:
        import pdb; pdb.set_trace()
        for number in f:
            results.append(number)

    print(f'The sum is {sum(results)}')

debug_sum()<jupyter_output><empty_output><jupyter_text>Catching Errors<jupyter_code>def sum_all_the_things():
    results = []
    import pdb; pdb.set_trace()
    with open('list_of_numbers.txt') as f:
        for number in f:
            try:
                results.append(number)
            except ValueError:
                pass
            
    print(f'The sum is {sum(results)}')

sum_all_the_things()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14501199862872816, 'fraction_numerical': 0.012341446691806651, 'mean_word_length': 2.679697351828499, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '570289', 'license': 'MIT', 'n_tokens_mistral': 1057, 'n_tokens_neox': 1003, 'n_words': 199, 'path': '2018-02-01-TUT-DFW-Debugging.ipynb', 'repo_name': 'whiterd/Tutorial-Notebooks', 'size': '2917'}