Last active
July 31, 2018 12:52
-
-
Save insightcoder/9d70c54aef61d6020af791e2728b3525 to your computer and use it in GitHub Desktop.
IC84 - Tips for Writing Reusable Python Scripts https://www.insightcoder.com/84/5-tips-for-writing-reusable-python-scripts/
This file contains 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 sys | |
def add_nums(nums): | |
return sum(nums) | |
args = sys.argv[1:] | |
nums = [float(arg) for arg in args] | |
result = add_nums(nums) | |
print(result) |
This file contains 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 add_nums | |
my_nums = [1, 2, 3] | |
result = add_nums.add_nums(my_nums) | |
print(result) |
This file contains 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 sys | |
def add_nums(nums): | |
return sum(nums) | |
if __name__ == '__main__': | |
args = sys.argv[1:] | |
nums = [float(arg) for arg in args] | |
result = add_nums(nums) | |
print(result) |
This file contains 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 | |
def delete_pyc(): | |
pyc_files = [] | |
for (path, dirs, files) in os.walk('.'): | |
for f in files: | |
if f.endswith('.pyc'): | |
pyc_file = os.path.join(path, f) | |
os.remove(pyc_file) | |
if __name__ == '__main__': | |
delete_pyc() |
This file contains 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 sys | |
def delete_pyc(root_dir): | |
pyc_files = [] | |
for (path, dirs, files) in os.walk(root_dir): | |
for f in files: | |
if f.endswith('.pyc'): | |
pyc_file = os.path.join(path, f) | |
os.remove(pyc_file) | |
if __name__ == '__main__': | |
root_dir = '.' | |
if len(sys.argv) > 1: | |
root_dir = sys.argv[1] | |
delete_pyc(root_dir) |
This file contains 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 sys | |
class FooWindows: | |
# Windows implementation | |
def __init__(self): | |
print('Windows') | |
class FooLinux: | |
# Linux implementation | |
def __init__(self): | |
print('Linux') | |
Foo = FooWindows if sys.platform == 'win32' else FooLinux |
This file contains 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 sys | |
root_dir = '.' | |
file_ext = '.pyc' | |
def delete_files(): | |
for (path, dirs, files) in os.walk(root_dir): | |
for f in files: | |
if f.endswith(file_ext): | |
file_to_delete = os.path.join(path, f) | |
os.remove(file_to_delete) | |
if __name__ == '__main__': | |
delete_files() |
This file contains 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 sys | |
ROOT_DIR = '.' | |
FILE_EXT = '.pyc' | |
def delete_files(root_dir, file_ext): | |
for (path, dirs, files) in os.walk(root_dir): | |
for f in files: | |
if f.endswith(file_ext): | |
file_to_delete = os.path.join(path, f) | |
os.remove(file_to_delete) | |
if __name__ == '__main__': | |
delete_files(ROOT_DIR, FILE_EXT) |
This file contains 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 sys | |
ROOT_DIR = '.' | |
FILE_EXT = '.pyc' | |
def find_files(root_dir, file_ext): | |
found_files = [] | |
for (path, dirs, files) in os.walk(root_dir): | |
for f in files: | |
if f.endswith(file_ext): | |
found_file = os.path.join(path, f) | |
found_files.append(found_file) | |
return found_files | |
def delete_files(files_to_delete): | |
for f in files_to_delete: | |
os.remove(f) | |
if __name__ == '__main__': | |
files_to_delete = find_files(ROOT_DIR, FILE_EXT) | |
delete_files(files_to_delete) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment