Last active
June 6, 2022 08:09
-
-
Save hardikmdev/682e2bcb0ab66a520cfd878511903267 to your computer and use it in GitHub Desktop.
Python :: time compare :: list files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pathlib | |
import timeit | |
import glob | |
def a(): | |
path = pathlib.Path().cwd() | |
list_log_files = [str(f) for f in path.glob("*.log")] | |
def b(): | |
path = os.getcwd() | |
list_log_files = [f.path for f in os.scandir(path) if os.path.splitext(f)[1] == ".log"] | |
def c(): | |
path = os.getcwd() | |
list_log_files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith(".log")] | |
def d(): | |
path = os.getcwd() | |
os.chdir(path) | |
list_log_files = [os.path.join(path, f) for f in glob.glob("*.log")] | |
def e(): | |
path = os.getcwd() | |
list_log_files = [os.path.join(path, f) for f in glob.glob1(str(path), "*.log")] | |
def f(): | |
path = os.getcwd() | |
list_log_files = [] | |
for root, dirs, files in os.walk(path): | |
for file in files: | |
if file.endswith(".log"): | |
list_log_files.append( os.path.join(root, file) ) | |
break | |
def g(dir,mask): | |
files = os.listdir(dir); | |
list_log_files = filter(lambda x: x.endswith(mask), files); | |
# print list_log_files | |
import os | |
import sys | |
if len(sys.argv)==2: | |
print('no params') | |
sys.exit(1) | |
dir = sys.argv[1] | |
mask= sys.argv[2] | |
print(timeit.timeit(a, number=1000)) | |
print(timeit.timeit(b, number=1000)) | |
print(timeit.timeit(c, number=1000)) | |
print(timeit.timeit(d, number=1000)) | |
print(timeit.timeit(e, number=1000)) | |
print(timeit.timeit(f, number=1000)) | |
print(timeit.timeit(g, number=1000)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ```python | |
#!/usr/bin/env python | |
import re | |
import sys | |
x=sys.argv[1] | |
# Check to see if the string is only binary | |
nonbin = re.search(r'[2-9A-Fa-f]', x) | |
# First, check to see if there were matches in binary, if not | |
# Attempt to convert to an integer assuming base 10 if it | |
# fails, attempt base 16 (hex) | |
if nonbin is None: | |
try: | |
num = int(x, 2) | |
except: | |
print("Problem parsing value from", x) | |
sys.exit(1) | |
else: | |
try: | |
num = int(x) | |
except: | |
num = int(x, 16) | |
hnum = ('{:010X}'.format(num)) | |
print('Integer: {:40d}'.format(num)) | |
print('Hex: {:>40s}'.format(hnum)) | |
print('Binary: {0:40b}'.format(num)) | |
# ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment