Skip to content

Instantly share code, notes, and snippets.

View miklevin's full-sized avatar
💭
Discovering NixOS

Mike Levin http://MikeLev.in miklevin

💭
Discovering NixOS
View GitHub Profile
@miklevin
miklevin / localtime.py
Last active July 26, 2019 17:27
Get a timezone-aware local time object from Python
import pytz
import datetime
nowz = datetime.datetime.utcnow()
dt = nowz.astimezone(pytz.timezone('US/Eastern'))
print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
@miklevin
miklevin / errornames.py
Last active July 19, 2019 16:49
Problems figuring out what error name to use in except? Try this!
from sys import exc_info as error
trapped = lambda x: print("Trapped: %s" % x)
err = lambda : print(error()[0].__name__)
try :
#1/0
#1 + 'foo'
#int('foo')
foo
@miklevin
miklevin / pushable.sh
Created July 19, 2019 14:46
Pushing a git repo up for the first time and tired of remembering git remote add origin git@github.user/repome.git ?
echo "# repome" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:miklevin/repome.git
git push -u origin master
@miklevin
miklevin / update.sh
Created July 19, 2019 11:23
apt-get update got you down? ** (appstreamcli:1244): CRITICAL **: Error while moving old database out of the way. AppStream cache update failed.
#!/bin/bash
# Are you getting this message when you apt-get update?
# ** (appstreamcli:1244): CRITICAL **: Error while moving old database out of the way.
# AppStream cache update failed.
# Then run this:
sudo chmod -R a+rX,u+w /var/cache/app-info/xapian/default
@miklevin
miklevin / gmail.py
Created July 16, 2019 20:47
Bare minimum to send GMails
import smtplib
from pathlib import Path
tos = Path("emails_test.txt").read_text().strip()
tos_list = tos.split('\n')
tos = ", ".join(tos_list)
SUBJECT = 'TEST MAIL'
TEXT = 'Here is a message from python.'
@miklevin
miklevin / cleanurls.py
Created July 16, 2019 19:24
Got to clean up a sloppy list of URLs for API-use? You're welcome.
sites = '''
www.google.com
http://www.amazon.com
apple.com
'''.split('\n')[1:-1]
for i, site in enumerate(sites):
if site[:4].lower() != 'http':
sites[i] = 'http://%s' % site
@miklevin
miklevin / figfunc.py
Created July 16, 2019 19:21
Add a print() like function but big ASCII text-art, great for delineating program-sections
from pyfiglet import Figlet
fig = lambda x : print(Figlet(font='standard', width=200).renderText(x))
@miklevin
miklevin / async_example.py
Last active July 16, 2019 19:27
Don't really want to use Python asyncio but have to? Here's one way.
import asyncio
async def in_the_future(fut):
result = await fut
print("Side-effect: %s" % result)
return result
async def side_effects(url, name):
@miklevin
miklevin / stager_proxy_cblocks.py
Created August 11, 2017 14:42
Takes a text file of web proxies and makes sure you round-robin between C-blocks
proxies = []
with open('goodproxies.txt', 'r') as f:
for line in f:
proxies.append(line.strip())
block_dict = dict()
blocks = set()
staggered = []
for proxy in proxies:
block = '.'.join(proxy.split('.')[:3])
@miklevin
miklevin / requests.py
Last active October 26, 2016 21:03
A reminder to myself about handling Requests exceptions
import requests
r = None
try:
r = requests.get("http://www.yahoofdsdsf.com/", proxies={'http': '127.0.0.1:8800'})
except requests.exceptions.ProxyError:
print("Proxy Error")
except requests.exceptions.ConnectTimeout:
print("Taking too long to connect")
except requests.exceptions.ReadTimeout:
print("Taking too long to read back")