Skip to content

Instantly share code, notes, and snippets.

@Hammer2900
Hammer2900 / time_function
Created June 7, 2015 19:18
Function time calculate, decorator
import time
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
print '%r (%r, %r) %2.2f sec' % \
def formater(simbol, text):
doc.delete_selected_lines()
doc.insert_text("{n}{t}".format(n=simbol,t=text))
TEXT = doc.get_line_text()
if TEXT[0] == "[":
if TEXT[0:3] == "[ ]":
formater("[+]",TEXT[3:])
elif TEXT[0:3] == "[+]":
formater("[-]",TEXT[3:])
@Hammer2900
Hammer2900 / multiple_replace
Created July 17, 2015 09:50
mass replace dict func
def multiple_replace(dict, text):
# Create a regular expression from the dictionary keys
regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
# For each match, look-up corresponding value in dictionary
return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
@Hammer2900
Hammer2900 / py
Created September 21, 2015 09:43
Singelton
def singleton(cls):
obj = cls()
# Always return the same object
cls.__new__ = staticmethod(lambda cls: obj)
# Disable __init__
try:
del cls.__init__
except AttributeError:
pass
return cls
@Hammer2900
Hammer2900 / deploy_to_s3.py
Created November 30, 2015 07:36 — forked from robert-b-clarke/deploy_to_s3.py
A simple python script for copying static web resources to an S3 bucket and advance gzipping JS and CSS. Let me know if it's useful (and not already implemented by something else), I may make it into a proper repo
"""
===========
Description
===========
Simple script to copy and gzip static web files to an AWS S3 bucket. S3 is great for cheap hosting of static web content, but by default it does not gzip CSS and JavaScript, which results in much larger data transfer and longer load times for many applications
When using this script CSS and JavaScript files are gzipped in transition, and appropriate headers set as per the technique described here: http://www.jamiebegin.com/serving-compressed-gzipped-static-files-from-amazon-s3-or-cloudfront/
* Files overwrite old versions
@Hammer2900
Hammer2900 / postgres_creator_db.py
Created September 3, 2016 09:34
Create postgres db and delete from python script.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from psycopg2 import connect
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
class PostgresDbHelper(object):
def __init__(self, host, user, password, dbname):
self.host = host
@Hammer2900
Hammer2900 / download_multiple.py
Created September 28, 2016 17:51 — forked from harrisont/download_multiple.py
Use asyncio and aiohttp to asynchronously download multiple files at once and handle the responses as they finish
import asyncio
from contextlib import closing
import aiohttp
async def download_file(session: aiohttp.ClientSession, url: str):
async with session.get(url) as response:
assert response.status == 200
# For large files use response.content.read(chunk_size) instead.
@Hammer2900
Hammer2900 / async_scraper.py
Created October 2, 2016 11:08 — forked from wtneal/async_scraper.py
asyncio scraper
import asyncio
import aiofiles
import aiohttp
import logging
import re
import sys
import os
import lxml.html
@Hammer2900
Hammer2900 / organize_directory.py
Created October 2, 2016 15:23 — forked from konstantinfarrell/organize_directory.py
Python script that cleans up my downloads folder
import shutil
import mimetypes
from os import listdir
from os.path import isfile, join
ORIGIN = r"C:\Users\Konstantin\Downloads\\" # Change these. Unless your system
# happens to have the same configuration.
MUSIC_DESTINATION = r"C:\Users\Konstantin\Music\\"
PICTURE_DESTINATION = r"C:\Users\Konstantin\Pictures\\"
PDF_DESTINATION = r"C:\Users\Konstantin\Documents\\"
@Hammer2900
Hammer2900 / example.py
Created October 4, 2016 18:22 — forked from diosmosis/example.py
Python decorator that catches exceptions and logs a traceback that includes every local variable of each frame.
import os
from log_exceptions import log_exceptions
def throw_something(a1, a2):
raise Exception('Whoops!')
@log_exceptions(log_if = os.getenv('MYAPP_DEBUG') is not None)
def my_function(arg1, arg2):
throw_something(arg1 + 24, arg2 - 24)