Skip to content

Instantly share code, notes, and snippets.

@nickyreinert
Last active March 19, 2022 12:52
Show Gist options
  • Save nickyreinert/45c019f318fe7a10232327cdf8b119f4 to your computer and use it in GitHub Desktop.
Save nickyreinert/45c019f318fe7a10232327cdf8b119f4 to your computer and use it in GitHub Desktop.
Python script boilerplate with environment creation, code examples & telegram logging
#!./bin/python
# prepare your virtual environment
######################################################################
# python3 -m venv ~/python_dev
# cd ~/python_dev
# source bin/activate
# manager requirements
######################################################################
# if your script is ready
# use pipreqs to manage requirements
# pip install pipreqs
# the following command will create a requirements.txt
# pipreqs yourscript.py
# or for every file in the folder
# pipreqs .
# later you can install requirements with
# pip install -r requirements.txt
# telegram logging
######################################################################
# if outsourced to telegram_notifier.py, import with
# from telegram_notifier import *
#
# start a telegram chat with BotFather and create a new bot to get token
# start a chat with your bot
# call https://api.telegram.org/bot<bot token>/getUpdates
# to get the chat id
import requests
bot_token = 'secret token'
bot_chat_id = 'secret chat id'
def send_text_to_telegram(bot_message):
bot_token = bot_token
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chat_id + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
def send_image_to_telegram(image):
bot_token = bot_token
data = {'chat_id': bot_chat_id}
url = 'https://api.telegram.org/bot' + bot_token + '/sendPhoto?chat_id=' + bot_chat_id + '&parse_mode=Markdown'
with open(image, "rb") as image_file:
response = requests.post(url, data=data, files={"photo": image_file})
return response.json()
# code
######################################################################
import time
# command line arguments
import argparse
parser=argparse.ArgumentParser()
parser.add_argument('--limit', help='Source files in CSV format', required=True, type=int)
parser.add_argument('--loop_length', help='Source files in CSV format', required=False, type=int)
parser.add_argument('--message', help='Give me a message', required=False, default="Hello World", type=str)
args = parser.parse_args()
loop_length = 1000000 if args.loop_length == None else args.loop_length
limit = args.limit
feedback_frequency = 10
count = 1
start = time.time()
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# colored text:
print(f'{bcolors.FAIL}{bcolors.BOLD}Bold red text{bcolors.ENDC}')
# fully featured list / dict comprehension - sometimes you
# need a compact but loop
default_value = 'x'
a_list = [
index
if index < 50 else default_value
for index in range(1, loop_length)
if index > 10
]
duration = time.time() - start
message = '\r\nDone with list comprehension after %s seconds ' % (
'{:.2f}'.format(duration) # formatting decimals with two decimals
)
send_text_to_telegram(message)
print(message)
# and a full loop, sometimes
# you need more control
for i in range(1, loop_length):
if i > limit - 1 and limit > 0: break
count += 1
if i % (limit / feedback_frequency) == 0 or i % (round(loop_length / feedback_frequency, 0)) == 0:
# if no limit is given, calc process from overall sum
progress = i / limit if limit > 0 else i / loop_length
message = (
'%s%%...' %
('{:.1f}'.format(100 * progress))
)
print(
message,
end = '', # no line break
flush=True # don't buffer output
)
# will create a lot of traffic, so heads up
# send_text_to_telegram(message)
duration = time.time() - start
message = '\r\nDone with processing %s iterations after %s seconds ' % (
'{:0,.0f}'.format(count), # formatting integers with thousand separator
'{:.2f}'.format(duration) # formatting decimals with two decimals
)
send_text_to_telegram(message)
print(message)
# creating a random image
from pylab import imshow, show, get_cmap
import matplotlib.pyplot as plt
from numpy import random
Z = random.random((50,50))
imshow(Z, cmap=get_cmap('Spectral'), interpolation='nearest')
plt.savefig('output.png')
message = '\r\nCreated image after %s seconds ' % (
'{:.2f}'.format(duration) # formatting decimals with two decimals
)
send_image_to_telegram('output.png')
send_text_to_telegram(message)
# Play a sound after loop is finished
os.system('afplay /System/Library/Sounds/Hero.aiff')
print(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment