Skip to content

Instantly share code, notes, and snippets.

@mkeneqa
Last active April 16, 2022 21:34
Show Gist options
  • Save mkeneqa/685de1887ecadebea997f66159250d9d to your computer and use it in GitHub Desktop.
Save mkeneqa/685de1887ecadebea997f66159250d9d to your computer and use it in GitHub Desktop.
Useful Python Code Snippets

Python Snippets

Handy Python Code Snippets

import os
from os import listdir
from os.path import isfile, join
# Quick Script to rename files using Python3
if __name__ == '__main__':
mypath = '/Path/To/Directory'
dry_run = True
find_term = '-proxy'
change_to = '_proxy'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
os.chdir(mypath)
for _f in onlyfiles:
if find_term in _f:
print("old name: " + _f)
new_name = _f.replace(find_term, change_to)
if not dry_run:
os.rename(_f,new_name)
print("new name: " + new_name)
print('')
print('')
print("DONE!")
"""
used when mapping coloumn names to vars in application code
From:
"reportDefinitionId",
"columnDefinitionId",
"sortOrder",
"headerOverride",
"convertTo",
"altConvertTo",
"noConvertUnit",
"altNoConvertTo",
"hideUnit"
TO:
public const REPORT_DEFINITION_ID_COL
public const COLUMN_DEFINITION_ID_COL
public const SORT_ORDER_COL
public const HEADER_OVERRIDE_COL
public const CONVERT_TO_COL
public const ALT_CONVERT_TO_COL
public const NO_CONVERT_UNIT_COL
public const ALT_NO_CONVERT_TO_COL
public const HIDE_UNIT_COL
"""
words = [
"reportDefinitionId",
"columnDefinitionId",
"sortOrder",
"headerOverride",
"convertTo",
"altConvertTo",
"noConvertUnit",
"altNoConvertTo",
"hideUnit"
]
word_bank = []
for word in words:
new_word = []
_first_time = True
for letter in word:
if str(letter).isupper() and not _first_time:
new_word.append("_" + str(letter))
else:
_first_time = False
new_word.append(str(letter))
new_word.append('_col')
new_word_str = "".join(new_word)
word_bank.append(new_word_str.upper())
for word in word_bank:
print("public const " + str(word))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment