🏣 Clean up for arxiv

This commit is contained in:
Mia von Steinkirch 2020-03-04 17:47:53 -08:00
parent 1b969e7db3
commit 41756cb10c
280 changed files with 2 additions and 11 deletions

View file

@ -0,0 +1,30 @@
#!/usr/bin/env python
__author__ = "bt3"
import os
import sys
import shutil
def change_file_ext():
""" read a file and an extension from the command line and produces a copy with its extension changed"""
if len(sys.argv) < 2:
print("Usage: change_ext.py filename.old_ext 'new_ext'")
sys.exit()
name = os.path.splitext(sys.argv[1])[0] + "." + sys.argv[2]
print (name)
try:
shutil.copyfile(sys.argv[1], name)
except OSError as err:
print (err)
if __name__ == '__main__':
change_file_ext()

View file

@ -0,0 +1,28 @@
#!/usr/bin/python
__author__ = "bt3"
import collections
import string
import sys
def count_unique_word_file():
if len(sys.argv) < 2:
print "Usage: python count_unique_word.py NAMEFILE"
words = collections.defaultdict(int)
strip = string.whitespace + string.punctuation + string.digits + "\"'"
for filename in sys.argv[1:]:
with open(filename) as file:
for line in file:
for word in line.lower().split():
word = word.strip(strip)
if len(word) > 2:
words[word] = +1
for word in sorted(words):
print("'{0}' occurs {1} times.".format(word, words[word]))
if __name__ == '__main__':
count_unique_word_file()

View file

@ -0,0 +1,15 @@
#!/usr/bin/python
__author__ = "bt3"
import collections
import sys
def count_unique_word_freq():
return collections.Counter(\
sys.stdin.read().lower().split()).most_common(n)
if __name__ == '__main__':
count_unique_word_freq()

View file

@ -0,0 +1,27 @@
#!/usr/bin/env python
__author__ = "bt3"
import sys
def grep_word_from_files():
''' using iterator enumerate to create a grep command '''
word = sys.argv[1]
for filename in sys.argv[2:]:
with open(filename) as file:
for lino, line in enumerate(file, start=1):
if word in line:
print("{0}:{1}:{2:.40}".format(filename, lino, line.rstrip()))
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: grep_word_from_files.py word infile1 [infile2...]")
sys.exit()
else:
grep_word_from_files()

View file

@ -0,0 +1,54 @@
#!/usr/bin/env python
__author__ = "bt3"
import os
import sys
def read_data(filename):
lines = []
fh = None
try:
fh = open(filename)
for line in fh:
if line.strip():
lines.append(line)
except (IOError, OSError) as err:
print(err)
finally:
if fh is not None:
fh.close()
return lines
def write_data(lines, filename):
fh = None
try:
fh = open(filename, "w")
for line in lines:
fh.write(line)
except (EnvironmentError) as err:
print(err)
finally:
if fh is not None:
fh.close()
def remove_blank_lines():
""" read a list of filenames on the command line and for each one produces another file with the same content but with no blank lines """
if len(sys.argv) < 2:
print ("Usage: noblank.py infile1 [infile2...]")
for filename in sys.argv[1:]:
lines = read_data(filename)
if lines:
write_data(lines, filename)
if __name__ == '__main__':
remove_blank_lines()