Skip to content

Instantly share code, notes, and snippets.

View jorge-lavin's full-sized avatar
:bowtie:
Focus

Jorge Lavin jorge-lavin

:bowtie:
Focus
View GitHub Profile
@jorge-lavin
jorge-lavin / tree.py
Created March 2, 2015 10:07
Print tree structure of folder with indentation
import os
def list_files(startpath):
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
yield ('{}{}/'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
yield ('{}{}'.format(subindent, f))
@jorge-lavin
jorge-lavin / list_zipper.py
Last active August 29, 2015 14:16
Combine two lists with a default value
def list_zipper(list_1, list_2):
for (x_1, x_2) in zip(list_1, list_2):
if not x_1:
yield '{x_2}_vacio'.format(x_2=x_2)
else:
yield x_1
if __name__ == '__main__':
list_1 = ['aaa', '', 'ccc']
list_2 = ['titulo_1', 'titulo_2', 'titulo_3']
@jorge-lavin
jorge-lavin / curl_oracle_jdk.sh
Created February 16, 2015 14:47
download java jdk (oracle) with curl
curl -v -j -k -L -H "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u25-b18/jdk-8u25-windows-i586.exe > jdk-8u25-windows-i586.exe
@jorge-lavin
jorge-lavin / where_am_i.sh
Created February 13, 2015 08:12
Where is mounted a folder or a file
df -P file/goes/here | tail -1 | cut -d' ' -f 1
@jorge-lavin
jorge-lavin / linux_info.sh
Created February 10, 2015 08:19
Linux characteristics
##################
##### RedHat #####
##################
## 32 or 64 bits?
uname -i
### i386: 32 bits
### x86_64: 64 bits
#!/usr/bin/env python
import logging
import os
import subprocess
import sys
VPN_NAME='vpnc'
"""
A simple HTTP Server. It admits port and ip as optional parameters
"""
import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler
@jorge-lavin
jorge-lavin / re_raise_exceptions.py
Created February 3, 2015 11:05
Re raising exceptions sandbox
"""
In this example we try to re raise an exception changing its class
"""
import sys
class InputFileNotFoundError(IOError):
"""
Input file not found
@jorge-lavin
jorge-lavin / ask_boolean.py
Created January 30, 2015 12:09
Ask user and return boolean.
def ask_boolean(message):
"""
Asks a yes/no question to the user and looks if the answer is yes or no
@return answer: A boolean representing the user answer
"""
yes = ['YES', 'Yes', 'yes', 'y']
no = ['NO', 'No', 'n']
answer = raw_input(message+'{new_line}Choose one of [{yes}] or [{no}] {new_line}'.format(new_line=os.linesep, yes=' '.join(yes), no=' '.join(no)))
@jorge-lavin
jorge-lavin / default_logger.py
Last active August 29, 2015 14:14
A simple debug stdout logger for python2 and 3
import logging
def setup_logging(name=__name__, level=logging.DEBUG, formatter=logging.Formatter('%(asctime)s %(levelname)s : %(message)s - (%(module)s, line %(lineno)s)')):
"""
Setups a logger with a `StreamHandler` that prints to `stdout` with `debug` logging
level and the following formatter
%(asctime)s %(levelname)s : %(message)s - (%(module)s, line %(lineno)s')
@return logger: The previously formatted logger