<jupyter_start><jupyter_text>constructing a named tuple, like OOP<jupyter_code>Dog = namedtuple('Dog', ['age', 'breed', 'name'])
sammy = Dog(age=5, breed='Husky', name='Sam')
type(sammy)
sammy
sammy[0] == sammy.age<jupyter_output><empty_output><jupyter_text>Second Lecture<jupyter_code>pwd
f = open('practice.txt', 'w+')
f.write('This is a test string')
f.close()
import os
os.getcwd()
os.listdir()
os.listdir('..\\')
os.listdir('d:\\')
import shutil
shutil.move('./practice.txt','../')
os.listdir('../')
dir(os)
!pip install send2trash
import send2trash
os.unlink('../practice.txt')
os.listdir('../')
with open('./practice.txt', 'w+') as f:
    f.write('yo world vitches.')
os.listdir('./')
send2trash.send2trash('./practice.txt')
os.listdir()
os.walk('../../../') # a generator
os.listdir('d:/')
for folder, sub_folder, files in os.walk('d:/Projects/'):
    print(f"Folder, currently going inside are: {folder}")
    print("The subfolders are: ")
    for sub_fold in sub_folder:
        print(f"\tSubfolder: {sub_fold}")
    print("\n")
    print("The files are:")
    for f in files:
        print(f"\tFile: {f}")
    print("\n")<jupyter_output>Folder, currently going inside are: d:/Projects/
The subfolders are: 
	Subfolder: BlockChains
	Subfolder: computational-astrophysics
	Subfolder: ComputationalAstrophysics
	Subfolder: ComputationalAstrophysics_nogit
	Subfolder: Coursera
	Subfolder: DeepDiveIntoAlgorithms
	Subfolder: DSA_Coursera
	Subfolder: FormalLogic
	Subfolder: GameOfBlocks
	Subfolder: GameOfBlocks2021
	Subfolder: InfoSec
	Subfolder: MathematicalThinking_Coursera
	Subfolder: ML_Julia
	Subfolder: NumbersMadeDumber
	Subfolder: Projects_Backup
	Subfolder: Udemy


The files are:


Folder, currently going inside are: d:/Projects/BlockChains
The subfolders are: 


The files are:
	File: assignments.md
	File: GameOfBlocks2021_MidSemEval.pdf


Folder, currently going inside are: d:/Projects/computational-astrophysics
The subfolders are: 
	Subfolder: .git
	Subfolder: Week-1
	Subfolder: Week-2
	Subfolder: Week-3
	Subfolder: Week-4


The files are:
	File: README.md


Folder, currently going inside are: d:/Projects/computational-[...]<jupyter_text>DateTime Module<jupyter_code>import datetime
mytime = datetime.time(3,40)
mytime.minute
print(mytime)
type(mytime)
today = datetime.date.today()
print(today)
today.ctime()
from datetime import datetime
mydatetime = datetime(2021, 10, 3, 14, 20, 1)
print(mydatetime)
mydatetime = mydatetime.replace(year=2022)
mydatetime
print(mydatetime)<jupyter_output>2022-10-03 14:20:01<jupyter_text>you can do simple arithmetic with date or datetime objects  Math and Random Modules<jupyter_code>import math
help(math)
value = 4.35
math.floor(value)
math.ceil(value)
round(value)
math.pi
math.inf
math.nan
import random
random.randint(0,100)
random.seed(101)

random.randint(0,100)
random.randint(0,100)
random.seed(101) # A fixed seed.
print(random.randint(0,100))
print(random.randint(0,100))
print(random.randint(0,100))
print(random.randint(0,100))
print(random.randint(0,100))
print(random.randint(0,100))
mylist = [i for i in range(20)]
random.choice(mylist)
#Sample with replacement
random.choices(population=mylist, k=10)
#Sample without replacements
random.sample(population=mylist, k=10)
mylist
random.shuffle(mylist)
mylist
random.uniform(a=0, b=100) # OP...all numbers between the limits have same likelihood of being chosen..includes decimals
random.gauss(mu=0, sigma=1)<jupyter_output><empty_output><jupyter_text>Python Debugger<jupyter_code>x = [1,2,3]
y = 2
z = 3

result = y + z
# result2 = x + y
import pdb
x = [1,2,3]
y = 2
z = 3

result1 = y + z

pdb.set_trace()
result2 = y + x<jupyter_output>--Return--
None
> [1;32m<ipython-input-87-1e807b1b69eb>[0m(7)[0;36m<module>[1;34m()[0m
[1;32m      4 [1;33m[1;33m[0m[0m
[0m[1;32m      5 [1;33m[0mresult1[0m [1;33m=[0m [0my[0m [1;33m+[0m [0mz[0m[1;33m[0m[1;33m[0m[0m
[0m[1;32m      6 [1;33m[1;33m[0m[0m
[0m[1;32m----> 7 [1;33m[0mpdb[0m[1;33m.[0m[0mset_trace[0m[1;33m([0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m
[0m[1;32m      8 [1;33m[0mresult2[0m [1;33m=[0m [0my[0m [1;33m+[0m [0mx[0m[1;33m[0m[1;33m[0m[0m
[0m<jupyter_text>Regex<jupyter_code>'dog' in 'dog is in dog'
import re
text = "the agent's phone number is 408-900-7826, phone Call Soon! Yup, on Phone"
pattern = 'phone'
re.search(pattern, text)
pattern = "not in text"
re.search(pattern, text)
pattern = 'phone'
match = re.search(pattern, text)
match.end()
matches = re.findall(pattern, text)
matches
for match in re.finditer(pattern, text):
    print(match.span())
for match in re.finditer(pattern, text):
    print(match.group())
text = 'My phone number is 408-555-1234'
pattern = r'\d\d\d-\d\d\d-\d\d\d\d'
match = re.search(pattern, text)
match.span()<jupyter_output><empty_output><jupyter_text>- \+ Occurs one or more times
- {3} Occurs exactly 3 times
- {2,4} Occurs 2 to 4 times
- {3,} Occurs 3 or more times
- \* Occurs zero or more times
- \? Once or None<jupyter_code>pattern = r'\d{3}-\d{3}-\d{4}'
phonenumber = re.search(pattern, text)
phonenumber
phone_pattern = re.compile(r'(\d{3})-(\d{3})-(\d{4})') # makes it seperable. We are still searching the normal way, but this is more flexible.
result = re.search(phone_pattern, text)
result.group()
print(result.group(1))
print(result.group(2))
print(result.group(3))
re.search(r'cat', 'The cat is here')
re.search(r'cat|dog', 'The dog is here')
re.findall(r'at', 'The cat in the hat sat there.')
re.findall(r'...at', 'The cat dog in the hat ogre sat there with og') # including wild cards.<jupyter_output><empty_output><jupyter_text>wild card wasn't working in case of using | as well<jupyter_code>re.findall(r'^\d', '1 is a number,\n The number is 2\n 3 is number') #when entire text starts with a number
re.findall(r'\d$', 'The number is 2') #when entire text ends with a number
phrase = 'there are 3 numbers 34 inside 5 this sentence'
# Excluding 
pattern = r'[^\d]+'
re.findall(pattern, phrase)
test_phrase = 'This is a string! But has punctuations, How can we remove that?!'
sanitised = re.findall(r'[^!.?,]+', test_phrase)
sanitised
' '.join(sanitised)<jupyter_output><empty_output><jupyter_text>[] is for grouping in regex.<jupyter_code>text = 'Only find the hyphen-words in this sentence, but dont know how long this hypenated-words are'
pattern = r'[\w]+'
re.findall(pattern, text)<jupyter_output><empty_output><jupyter_text>- \\d Digit
- \\w Alphanumeric
- \\s White space
- \\D Non-Digit
- \\W Non-Alphanumeric
- \\S Non-Whitespace<jupyter_code>pattern = re.compile(r'(\w+)-(\w+)')
re.findall(pattern, text)
text = 'Hello, would you like some catfish?'
text2 = 'Hello, would you like to take a catnap?'
text3 = 'Hello, have you seen this caterpillar?'
print(re.search(r'cat[\w]+', text))
print(re.search(r'cat[\w]+', text2))
print(re.search(r'cat[\w]+', text3))<jupyter_output><re.Match object; span=(27, 34), match='catfish'>
<re.Match object; span=(32, 38), match='catnap'>
<re.Match object; span=(26, 37), match='caterpillar'><jupyter_text>Timing Python Code<jupyter_code>def func(n):
    return [str(i) for i in range(n)]
func(10)
def func2(n):
    return list(map(str, range(n)))
func2(10)
import time
startTime = time.time()
result = func(100000)
endTime = time.time()
elapsedTime = endTime - startTime
print(elapsedTime)
startTime = time.time()
result = func2(100000)
endTime = time.time()
elapsedTime = endTime - startTime
print(elapsedTime)<jupyter_output>0.016001224517822266<jupyter_text>Time it module<jupyter_code>import timeit
statement1 = '''
func(100)
'''
setup1 = '''
def func(n):
    return [str(i) for i in range(n)]
'''
timeit.timeit(statement1, setup1, number=100000)
statement2 = '''
func2(100)
'''
setup2 = '''
def func2(n):
    return list(map(str, range(n)))
'''
timeit.timeit(statement2, setup2, number=100000)<jupyter_output><empty_output><jupyter_text>Timeit only accepts strings as statement (code to execute) and setup (all definitions for that code) so is a bit cumbersome. Using magic of Jupyter, we can use timeit more efficiently.<jupyter_code>%%timeit #needs to be the first line of the cell
func2(100)
def func2(n):
    return list(map(str, range(n)))
%%timeit #needs to be the first line of the cell
func(100)<jupyter_output>16.5 µs ± 119 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)<jupyter_text>Zipping and Unzipping Files<jupyter_code>with open('file1.txt', 'w+') as f:
    f.write('This is the first file')
with open('file2.txt', 'w+') as f:
    f.write('This is the second file')
import zipfile
comp_file = zipfile.ZipFile('comp_file.zip', 'w')
comp_file.write('file1.txt', compress_type=zipfile.ZIP_DEFLATED)
comp_file.write('file2.txt', compress_type=zipfile.ZIP_DEFLATED)
comp_file.close()
zip_obj = zipfile.ZipFile('comp_file.zip', 'r')
zip_obj.extractall('extracted_content') # A folder created containing all extracted files.
import shutil
# Now converting a dir to a zip
dir_to_zip = './extracted_content'
output_filename = 'example'
shutil.make_archive(output_filename, 'zip', dir_to_zip)
shutil.unpack_archive('example.zip', 'final_unzip', 'zip')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16001301800824475, 'fraction_numerical': 0.04523757865046648, 'mean_word_length': 5.336082474226804, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 58, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '643416', 'license': 'Apache-2.0', 'n_tokens_mistral': 3856, 'n_tokens_neox': 3540, 'n_words': 838, 'path': 'advancedModules.ipynb', 'repo_name': 'ba-13/PythonBasics', 'size': '9218'}