Month: February 2019
/bin/sh^M : bad interpreter
dos2unix run.sh
References
https://stackoverflow.com/questions/2920416/configure-bin-shm-bad-interpreter
Terminating a Python script
import sys sys.exit()
os._exit(errorcode)
References
https://stackoverflow.com/questions/73663/terminating-a-python-script
Detect Ctrl+C in Python
#!/usr/bin/env python import signal import sys def signal_handler(sig, frame): print('You pressed Ctrl+C!') sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print('Press Ctrl+C') signal.pause()
References
https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python
Preventing Windows OS from sleeping while your python code runs
class WindowsInhibitor: '''Prevent OS sleep/hibernate in windows; code from: https://github.com/h3llrais3r/Deluge-PreventSuspendPlus/blob/master/preventsuspendplus/core.py API documentation: https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx''' ES_CONTINUOUS = 0x80000000 ES_SYSTEM_REQUIRED = 0x00000001 def __init__(self): pass def inhibit(self): import ctypes print("Preventing Windows from going to sleep") ctypes.windll.kernel32.SetThreadExecutionState( WindowsInhibitor.ES_CONTINUOUS | \ WindowsInhibitor.ES_SYSTEM_REQUIRED) def uninhibit(self): import ctypes print("Allowing Windows to go to sleep") ctypes.windll.kernel32.SetThreadExecutionState( WindowsInhibitor.ES_CONTINUOUS)
import os osSleep = None # in Windows, prevent the OS from sleeping while we run if os.name == 'nt': osSleep = WindowsInhibitor() osSleep.inhibit() # do slow stuff if osSleep: osSleep.uninhibit()
References
https://trialstravails.blogspot.com/2017/03/preventing-windows-os-from-sleeping.html
Create an Enum in Python
from enum import Enum class Color(Enum): RED = 1 GREEN = 2 BLUE = 3
References
https://docs.python.org/3/library/enum.html
Calculate the difference between two datetime objects in Python
dateTimeDifference = dateTimeA - dateTimeB # Divide difference in seconds by number of seconds in hour (3600) dateTimeDifferenceInHours = dateTimeDifference.total_seconds() / 3600
Get chat id from Telegram bot
1- Add the bot to the group.
Go to the group, click on group name, click on Add members, in the searchbox search for your bot like this: @my_bot, select your bot and click add.
2- Send a dummy message to the bot.
You can use this example: /my_id @my_bot
(I tried a few messages, not all the messages work. The example above works fine. Maybe the message should start with /)
3- Go to following url: https://api.telegram.org/botXXX:YYYY/getUpdates
replace XXX:YYYY with your bot token
4- Look for “chat”:{“id”:-zzzzzzzzzz,
-zzzzzzzzzz is your chat id (with the negative sign).
References
https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id
Python String Formatting
1 –
'Hello, {}'.format(name)
Output
'Hello, Bob'
2 –
'Hey {name}, there is a 0x{errno:x} error!'.format(name=name, errno=errno)
Output
'Hey Bob, there is a 0xbadc0ffee error!'
3 –
f'Hello, {name}!'
Output
'Hello, Bob!'
Convert a PIL Image into a numpy array
pix = numpy.array(pic)
References
https://stackoverflow.com/questions/384759/how-to-convert-a-pil-image-into-a-numpy-array