Skip to content

Instantly share code, notes, and snippets.

##########
# Tweaked Win10 Initial Setup Script
# Primary Author: Disassembler <disassembler@dasm.cz>
# Original Version: 1.4, 2016-01-16
# Tweaked based on personal preferences for @alirobe 2016-03-23 - v1.4.1
# NOTE: MAKE SURE YOU READ THIS SCRIPT CAREFULLY BEFORE RUNNING IT + ADJUST COMMENTING AS APPROPRIATE
# This script will reboot your machine when completed.
##########
# Ask for elevated permissions if required
@tadone
tadone / 0_reuse_code.js
Created March 13, 2017 18:35
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@tadone
tadone / snippets.py
Created October 6, 2017 10:19
Print String
print('String')
@tadone
tadone / snippets.vim
Created October 6, 2017 12:33
Search and replace
:%s/foo/bar/g
Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.
:s/foo/bar/g
Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'.
:%s/foo/bar/gc
Change each 'foo' to 'bar', but ask for confirmation first.
:%s/\<foo\>/bar/gc
Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation.
@tadone
tadone / snippets.py
Created October 12, 2017 12:48
Read/Write to/from file
# Wrtie to file (w write)
f = open('helloworld.txt','w')
f.write('hello world')
f.close()
# Write to BINARY file (wb write binary)
f = open('database_file','wb')
f.write('hello world')
f.close()
@tadone
tadone / snippets.py
Last active October 13, 2017 14:48
Subprocess
# Import whole module
import subprocess
# Import selected calls from module
from subprocess import Popen, PIPE
process = Popen(['cat', 'test.py'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
print(stdout)
@tadone
tadone / snippets.py
Created October 16, 2017 11:31
[Read password lines from file] Read values #python
# Use this format
# username:value
# password:value
# Assuming no : characters in your usernames or passwords
with open('myfile.txt') as f:
credentials = [x.strip().split(':') for x in f.readlines()]
for username,password in credentials:
# your code here
@tadone
tadone / snippets.py
Created October 16, 2017 13:57
[Replacing shell pipeline] #python
# Replacing shell pipeline
# output=`dmesg | grep hda`
# becomes:
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]
#Alternatively:
@tadone
tadone / snippets.py
Created October 16, 2017 14:03
[Logging] Basic logging
# Import module
import logging
# Logging to console
logging.warning('Watch out!') # will print a message to the console
logging.info('I told you so') # will not print anything
>>>WARNING:root:Watch out!
# Logging to a file
logging.basicConfig(filename='example.log',level=logging.DEBUG)
@tadone
tadone / snippets.py
Created October 30, 2017 13:37
[Zip dir function w/ exclude dirs] Zip up directory excluding some dirs #python
exclude = ['dir1','dir2']
def zipdir(path, ziph, exclude):
'''Zip up the directory excluding some subdirs'''
# ziph is zipfile handle
for dirname, subdirs, files in os.walk(path):
subdirs[:] = [d for d in subdirs if d not in exclude]
for file in files:
ziph.write(os.path.join(dirname, file))