<jupyter_start><jupyter_text>Python 3 - what to take into account *Largely inspired by http://python-3-for-scientists.readthedocs.io/en/latest/python3_user_features.html*  Integer division not default anymore<jupyter_code>2/3
2//3<jupyter_output><empty_output><jupyter_text>Recursive search for filenames in a directory and its subdirectories<jupyter_code>import os
import glob<jupyter_output><empty_output><jupyter_text>Check all the Excel-files inside my github repos, recusively into the different repos:<jupyter_code>glob.glob(os.path.join("/home/stijn_vanhoey/githubs", "**", "*.xlsx"), recursive=True)<jupyter_output><empty_output><jupyter_text>Cleaning lists now similar to dictionaries For dictionaries: `clear` function<jupyter_code>d = {'flux': 1}
d.clear()
d<jupyter_output><empty_output><jupyter_text>Also available to lists:<jupyter_code>d = ["bird", "plant", "mammal"]
d.clear()
d<jupyter_output><empty_output><jupyter_text>Print is a function `print` is a function. It allows you to customize aspects such as what separator to use between variables, and whether to go to the next line between successive print statements:<jupyter_code>a, b = 1, 2
print(a, b)<jupyter_output>1 2<jupyter_text>Adding separators and end of line statements:<jupyter_code>a, b = 1, 2
print(a, b, sep="\t,", end=";\n")<jupyter_output>1	,2;<jupyter_text>Print can also be used to print everything in a file:<jupyter_code>with open('data.txt', 'w') as f:
    print(a, b, file=f)<jupyter_output><empty_output><jupyter_text>is equivalent to e.g.<jupyter_code>with open('data.txt', 'w') as f:
    f.write("{} {}".format(a, b))<jupyter_output><empty_output><jupyter_text>Unpacking of variables<jupyter_code>a, b, *rest = range(5)
print(a, b, rest, sep="\t;\t")
*rest, a, b = range(5)
print(a, b, rest, sep="\t;\t")<jupyter_output>3	;	4	;	[0, 1, 2]<jupyter_text>You can use this to read specific lines of a datafile:<jupyter_code>with open('data.txt', 'w') as f:
    a, b, c, *rest  = range(8)
    print(a, b, c, rest, file=f, sep="\n")
with open('data.txt', 'r') as f:
    first, second, *rest, last = f.readlines()
print(first, second, last)<jupyter_output>0
 1
 [3, 4, 5, 6, 7]<jupyter_text>String interpolation<jupyter_code>a, b = 10, 20
'The sum of the values is {}.'.format(a+b)<jupyter_output><empty_output><jupyter_text>For Python 3.6, the following syntax will be possible as well:<jupyter_code>a, b = 10, 20
f'The sum of the values is {a + b}.'<jupyter_output><empty_output><jupyter_text>Unicode Unicode strings are the default in Python 3.<jupyter_code>s4 = "unicode strings are great! 😍"
print(s4)<jupyter_output>unicode strings are great! 😍<jupyter_text>Error catching<jupyter_code>try:
    f = open('data.txt')
except FileNotFoundError:
    print("File not found...")
try:
    f = open('data_2.txt')
except FileNotFoundError:
    print("File not found...")<jupyter_output>File not found...<jupyter_text>Other new errors:<jupyter_code>PermissionError, IsADirectoryError and TimeoutError<jupyter_output><empty_output><jupyter_text>Check for new errors: https://docs.python.org/3/whatsnew/3.3.htmlpep-3151-reworking-the-os-and-io-exception-hierarchy  Function annotations Check the data types that go in and out of a function:<jupyter_code>def remove_spaces(x: str) -> str:
    return x.replace(' ', '')
remove_spaces("<NAME>")<jupyter_output><empty_output><jupyter_text>Check http://mypy-lang.org/ to make this really work, as currenlty this is just good for devs...  Closing...<jupyter_code># remove the dummy data file
os.remove("data.txt")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14065564583917065, 'fraction_numerical': 0.014569907537125245, 'mean_word_length': 6.37603305785124, 'pattern_counts': {'":': 0, '<': 68, '<?xml version=': 0, '>': 69, 'https://': 1, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '134331', 'license': 'MIT', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1322, 'n_words': 327, 'path': 'snippets/python-3-snippets.ipynb', 'repo_name': 'inbo/python-utilities', 'size': '3572'}