Skip to content

Instantly share code, notes, and snippets.

@dhavalsavalia
Created February 10, 2021 18:15
Show Gist options
  • Save dhavalsavalia/1930d0753ae0302805e9855044f68a42 to your computer and use it in GitHub Desktop.
Save dhavalsavalia/1930d0753ae0302805e9855044f68a42 to your computer and use it in GitHub Desktop.
Supercharged file handler
class FileHandler():
"""Handles file like never before. Possible overkill for what it does.
- Want to open a file? You got covered.
- Want to remove lines from the file? You got covered.
- Want to add test to the file? You got covered.
Attributes
----------
file_name : Name of the file
results : Whatever `file_to_set` returns. Don't call the method like
a moron. I did it for you
Methods
-------
file_to_set : Takes a file name and returns a set
set_to_file : Takes a set and writes to a file
append_to_file : Appends `text` to the file
remove_last : I don't know what this does, but I will. Eventually.
"""
def __init__(self,
file_name: str,
line: list = None,
text: str = None,
type_op: str = None):
"""
Parameters
----------
file_name : Name of the file
line : Line, mostly to remove from the file, set `type_op` to `None`
text : Text to be appended to the file
type_op : Type of Operation, use "append"
"""
self.file_name = file_name
self.line = line
self.text = text
self.type_op = type_op
if self.line:
self.set_to_file(self.line, self.file_name)
elif self.text:
if self.type_op == 'append':
self.append_to_file(self.text, self.file_name)
else:
self.remove_last(self.text, self.file_name)
else:
self.results = self.file_to_set(self.file_name)
def file_to_set(self, file_name: str = self.file_name) -> set:
"""Read a file and convert each line to set items
Parameters
----------
file_name : Name of the file. Takes `self.file_name` by default.
Don't change. Please. Why would you?
Returns
-------
results : A set with lines as elements.
"""
results = set()
with open(file_name, 'r', encoding='u8', errors='ignore') as f:
for line in f:
results.add(line.replace('\n', ''))
return results
def set_to_file(self, line, file_name: str = self.file_name):
"""Iterate through a set, each item will be a line in a file
Parameters
----------
line : Must be a set. I don't know.
file_name : Name of the file.
"""
with open(file_name, "w") as f:
for l in sorted(line):
f.write(l + "\n")
def remove_last(self, text: str, file_name: str = self.file_name):
"""Does something.
Parameters
----------
text : Text to be removed from the file.
file_name : Name of the file.
"""
with open(file_name, "r+") as f:
new_f = f.readlines()
f.seek(0)
for line in new_f:
if text not in line:
f.write(line)
f.truncate()
def append_to_file(self, text: str, file_name: str = self.file_name):
"""Appends given `text` to the file.
Parameters
----------
text : Test to be removed from the file.
file_name : Name of the file.
"""
with open(file_name, 'a') as file:
file.write(text + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment