Skip to content

Instantly share code, notes, and snippets.

View dmiro's full-sized avatar

David Miró dmiro

View GitHub Profile
@dmiro
dmiro / locate_usb.py
Created April 26, 2016 08:31
locate USB
def locate_usb():
import win32file
drive_list = []
drivebits=win32file.GetLogicalDrives()
for d in range(1,26):
mask=1 << d
if drivebits & mask:
# here if the drive is at least there
drname='%c:\\' % chr(ord('A')+d)
t=win32file.GetDriveType(drname)
@dmiro
dmiro / ServerHandler.py
Last active March 18, 2016 13:24
How to dump POST request with Python
#### http://georgik.sinusgear.com/2011/01/07/how-to-dump-post-request-with-python/
import SimpleHTTPServer
import SocketServer
import logging
import cgi
PORT = 8000
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
@dmiro
dmiro / gist:42e0fc6b8cac97363305
Last active March 14, 2016 11:09
QString to Unicode
http://forthescience.org/blog/2013/08/15/how-to-convert-a-qstring-to-unicode-object-in-python-2/
I had this problem to solve, and I tried to find the safest way. This program illustrates the solution
from PyQt4 import QtCore, QtGui
import sys
app = QtGui.QApplication(sys.argv)
ed = QtGui.QLineEdit()
var normalizarTexto = function(texto){
var accents = {
'à':'a','á':'a','â':'a','ã':'a','ä':'a','å':'a','æ':'e','è':'e','é':'e','ê':'e','ë':'e','ì':'i',
'í':'i','î':'i','ò':'o','ó':'o','ô':'o','ö':'o','ù':'u','ú':'u','û':'u','ü':'u','ñ':'n'
};
texto = texto.toLowerCase();
texto = texto.replace(/[àáâãäåæèéêëìíîòóôöùúûüñ]/gi, function(m){return accents[m];});
texto = texto.replace(/\s/gi,"");
return texto;
};
@dmiro
dmiro / findout_encoding_file.py
Last active August 29, 2015 14:17
how to find out encoding from file
encodings = ['utf-8', 'windows-1250', 'windows-1252' ...etc]
for e in encodings:
try:
fh = codecs.open('file.txt', 'r', encoding=e)
fh.readlines()
fh.seek(0)
except UnicodeDecodeError:
print('got unicode error with %s , trying different encoding' % e)
else:
print('opening the file with encoding: %s ' % e)
@dmiro
dmiro / in_memory_zip.py
Created March 1, 2015 08:47
in memory zip
import zipfile
import StringIO
class InMemoryZip(object):
def __init__(self):
# Create the in-memory file-like object for working w/imz
self.in_memory_zip = StringIO.StringIO()
@dmiro
dmiro / HTML2Text.py
Created February 27, 2015 17:39
HTML2Text
def HTML2Text(html):
class _HTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self._text = []
def handle_data(self, data):
append = True
@dmiro
dmiro / gist:8a38c3af11654d24141b
Created February 13, 2015 17:17
Para tener un read-only @Property decorator DEBES heredar del nuevo estilo de clases "object"
To have a 'read-only' property in a class you can make use of the @property decoration, you'll need to inherit from object when you do so to make use of the new-style classes.
Example:
>>> class A(object):
... def __init__(self, a):
... self.__a = a
... @property
... def a(self):
@dmiro
dmiro / gist:110a9510426af8e6769a
Created February 10, 2015 15:50
ex_bag_of_words.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Author: Dev Mehta / [email protected]
Description: Create a simple bag-of-words. Accepts a csv of strings, writes a BoW to a CSV named 'matrix.csv.'
Usage: python bagOfWords.py
"""
import textmining
def main():
# https://pypi.python.org/pypi/textmining
@dmiro
dmiro / re_youtube_links.py
Last active August 29, 2015 14:14
Regular expression for youtube links