This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
def validate(fqdn): | |
"""docstring for validate""" | |
valid_chars = list(string.ascii_letters + string.digits + '-' + '.') | |
if all(char in valid_chars for char in fqdn): | |
return True | |
else: | |
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Symlinks to dotfiles folder | |
function file_exists(){ | |
file_to_check=$1 | |
stat ${file_to_check} &> /dev/null | |
retval=$? | |
if [ ${retval} -eq 0 ]; then |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
with open('datos.csv') as fichero_de_datos: | |
reader = csv.reader(fichero_de_datos, delimiter=';') | |
for lines in reader: | |
print [ items for items in lines if items != '' ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import logging | |
import logging.config | |
''' | |
If you try to log to a non existing folder, this shows how an IOError exception is raised. The non existing folder may be /logs in current working directory. | |
IMPORTANT: This assumes a logging.ini configuration file for the logging exists with a log_file_path variable pointing at the file to be logged at. | |
The contents of logging.ini are: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Taken from http://www.jeffknupp.com/blog/2013/04/07/improve-your-python-yield-and-generators-explained/ | |
Just added a python2 check | |
""" | |
import sys | |
if sys.version[0] == '2': | |
print('This code is not Python 2 compliant exiting ...') | |
exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def file_to_string(file_path): | |
""" | |
Converts a file into a string. \\t\\n join is used for format purposes in Outlook | |
""" | |
with open(file_path) as file_: | |
reader = csv.reader(file_, delimiter=';') | |
#If no delimiter is specified and are commas, ;, or other csv reader delimiters, this function may not | |
#work as expected. Therefore we force it to have ; because are not likely to be present | |
line = [] | |
for lines in reader: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Python 2 | |
python -m SimpleHTTPServer 8080 | |
#Python 3 | |
python3 -m http.server 8113 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code taken from http://codereview.stackexchange.com/a/61539/52090 | |
import sys | |
exit_codes = [ | |
(ValueError, 10), | |
(KeyError, 25) | |
] | |
# now you can also use a dictionary here, but |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Slightly adapted from http://www.artima.com/weblogs/viewpost.jsp?thread=240808 | |
# Tested under Python 2.6.6, 3.4.1 | |
def entryExit(f): | |
""" | |
This decorator adds the funcionality defined in new_f() | |
""" | |
def new_f(): | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function # only for module main() test function | |
import csv | |
import sys | |
PY3 = sys.version_info[0] > 2 | |
def read_properties(filename, delimiter=':'): | |
''' Reads a given properties file with each line of the format key=value. | |
Returns a dictionary containing the pairs. | |
filename -- the name of the file to be read | |
''' |
OlderNewer