Skip to content

Instantly share code, notes, and snippets.

@immmdreza
Created August 20, 2021 07:39
Show Gist options
  • Save immmdreza/2bb42e6c3962804024c06fdbea579788 to your computer and use it in GitHub Desktop.
Save immmdreza/2bb42e6c3962804024c06fdbea579788 to your computer and use it in GitHub Desktop.
Generate nice looking comments for your code
import os
import textwrap
class Generator:
def __init__(self, file_name):
self.filename, self.file_extension = os.path.splitext(file_name)
if not self.file_extension:
self.file_extension = '.py'
self.full_name = self.filename + self.file_extension
self.starting = ' #'
self.break_line = '-'*76 + '#'
self.saperator = self.starting + self.break_line + '\n'
self.wide = 80
def __enter__(self):
self.file = open(self.full_name, 'w', encoding= 'utf8')
def __exit__(self, _, __, ___):
if not self.file.closed:
self.file.close()
def adjust_center(self, string: str, full_len = 80):
full_len -= 4
total_space = full_len - 1
result = ''
for x in textwrap.wrap(string, total_space, break_long_words=True):
remaining = full_len - len(x)
right_side = remaining//2
left_side = right_side
if right_side+left_side != remaining:
left_side += 1
result += self.starting + ' '*(right_side) + x + ' '*(left_side) + '#' + '\n'
return result
def adjust_left(self, string: str, full_len = 80):
full_len -= 4
total_space = full_len - 1
result = ''
for x in textwrap.wrap(string, total_space, break_long_words=True):
remaining = full_len - len(x)
left_side = remaining
result += self.starting + ' ' + x + ' '*(left_side - 1) + '#' + '\n'
return result
def list_item(self, string):
return self.starting + ' '*5 + string
def write_center(self, string):
if not self.file.closed:
self.file.write(self.adjust_center(string, self.wide))
def write_separator(self):
if not self.file.closed:
self.file.write(self.saperator)
def write_list_item(self, string):
if not self.file.closed:
self.file.write(self.list_item(string))
def write_left(self, string):
if not self.file.closed:
self.file.write(self.adjust_left(string, self.wide))
def write_line(self, string = ''):
if not self.file.closed:
self.file.write(self.starting + ' ' + string + '\n')
def next_line(self):
if not self.file.closed:
self.file.write('\n')
# Example
gen = Generator('test.py')
with gen:
gen.write_separator()
gen.write_center('In the name of GOD')
gen.write_separator()
gen.next_line()
gen.write_line('Your code here')
gen.next_line()
gen.write_separator()
gen.write_center('Written with ❤️ by AraSH')
gen.write_separator()
@immmdreza
Copy link
Author

example of result:

  #----------------------------------------------------------------------------#
  #                             In the name of GOD                             #
  #----------------------------------------------------------------------------#

print('Hello World!')

  #----------------------------------------------------------------------------#
  #                         Written with ❤️ by  AraSH                          #
  #----------------------------------------------------------------------------#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment