Uke 3 #2 utkast Trygve
This commit is contained in:
parent
27b4466948
commit
12530126dc
26
uke3.py
26
uke3.py
@ -26,18 +26,28 @@ print(student_information("data.txt"))
|
|||||||
|
|
||||||
#%% Task 2
|
#%% Task 2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import pathlib import path
|
from pathlib import Path
|
||||||
|
|
||||||
def get_imp_file(file):
|
def get_imp_file(file):
|
||||||
with open(file, 'r', encoding='utf-8') as f:
|
with open(file, 'r', encoding='utf-8') as f:
|
||||||
#lines = f.readlines()
|
txt = f.read()
|
||||||
imp=re.findall(r"import\s(\w+)", f.read())
|
# re.M gjør at ^ matcher starten av hver linje istedet for bare starten av stringen
|
||||||
return imp
|
ptr1 = re.compile(r"^import\s(\w+)", flags=re.M)
|
||||||
|
ptr2 = re.compile(r"^from\s(\w+)", flags=re.M)
|
||||||
|
imports = re.findall(ptr1, txt)
|
||||||
|
imports += re.findall(ptr2, txt)
|
||||||
|
|
||||||
def get_imp_dir(dir="./"):
|
# vi filtrerer ut duplikater:
|
||||||
|
res = []
|
||||||
|
[res.append(x) for x in imports if x not in res]
|
||||||
|
return res
|
||||||
|
|
||||||
|
def print_imp_dir(path="./"):
|
||||||
|
p = Path(path)
|
||||||
|
files = list(p.glob('*.py'))
|
||||||
|
for f in files:
|
||||||
|
print(f'{Path.cwd()}+{f}: {get_imp_file(f)}')
|
||||||
|
|
||||||
get_imp_file("exercises2.py")
|
print_imp_dir()
|
||||||
|
# %%
|
||||||
|
Loading…
Reference in New Issue
Block a user