Mario Orlandi's Snippets

16/02/2012

Sorting and duplicates removal in Python

Filed under: Uncategorized — Tags: — morlandi @ 09:36

Sort dictionary by key

session_timings = sorted(session_timings, key=lambda k: k['time'])

Remove duplicates from list

First make a set from the list and then make a list for that set:

my_list = list(set(my_list))

Remove duplicates and sort

durations = sorted({}.fromkeys(durations).keys())

07/02/2012

Conversion between ISO 8601 Time Representation and datetime

Filed under: Uncategorized — Tags: , — morlandi @ 18:46
>>> from datetime import datetime
>>> datetime.strptime("2007-03-04 21:08:12", "%Y-%m-%d %H:%M:%S")
datetime.datetime(2007, 3, 4, 21, 8, 12)

>>> from datetime import datetime
>>> print datetime(2007, 3, 4, 21, 8, 12).strftime("%Y-%m-%d %H:%M:%S")
2007-03-04 21:08:12

Sample Python script for command line usage

Filed under: Uncategorized — Tags: — morlandi @ 11:22
import os
import sys
import getopt
import ConfigParser
from urllib2 import urlopen


def download_file(url, filepath):
    """ Download file from remote url """
    try:
        print ''
        print 'Downloading "%s" from url:\n"%s" ...' % (filepath, url)
        response = urlopen(url)
        local_file = open(filepath, 'wb')
        local_file.write(response.read())
        response.close()
        local_file.close()
        return True
    except:
        return False


def retrieve_single_file(url, target_folder, skip_existing):

    return_value = True
    message = ''

    # remove parameters from query string
    tokens = url.split('?')
    if len(tokens) > 1:
        url = tokens[0]

    url = url.replace(' ', '%20')

    # # remove protocol (i.e. "http://")
    # tokens = url.split('//')
    # if len(tokens) > 1:
    #     url = tokens[len(tokens) - 1]

    # build filepath
    filename = url.split('/')[-1]
    filepath = os.sep.join([target_folder, filename, ])

    try:
        os.makedirs(target_folder)
    except:
        pass

    if skip_existing and os.path.exists(filepath):
        message = '--> SKIPPED    : %s' % filename
    else:
        if download_file(url, filepath):
            message = '--> DOWNLOADED : %s' % filename
        else:
            return_value = False
            message = '--> NOT FOUND  : %s' % filename

    return (return_value, message)


def usage():
    print """
Downloads a list of remote files

Usage:   $ python batch_download.py  [options]

Options:
    -h = help
    -i = debug with ipython

The list of files to be downloaded is deduced from config file "batch_download.cfg"
Sample config. file:

    [general]
    target_folder = ./output
    skip_existing = False
    files = files

    [files]
    url_01=http://www.brainstorm.it/downloads/setup_dbkit_3.1.0.exe
    url_02=http://www.brainstorm.it/downloads/setup_protocol_simulator_3.3.0.exe
"""


def fail(message):
    print 'ERROR: ' + message
    # Eventually raise exception
    # raise Exception(message)
    sys.exit(1)


def main():

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hi?", ["help", ])
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    # scan options
    for o, a in opts:
        if o in ("-h", "--help", "-?"):
            usage()
            sys.exit()
        elif o in ("-i",):
            try:
                import IPython
                from IPython.Debugger import Tracer
                IPython.Shell.IPShell(argv=[])
                ipdb_set_trace = Tracer(colors='Linux')
                import pdb
                pdb.set_trace = ipdb_set_trace
            except:
                pass
        else:
            assert False, "unhandled option"

    # read config. file
    config_filename = os.path.splitext(sys.argv[0])[0] + os.path.extsep + "cfg"
    config = ConfigParser.ConfigParser()
    if len(config.read(config_filename)) <= 0:
        fail('Config. file "%s" not found' % (config_filename))

    # process files
    general_section = dict(config.items('general'))
    target_folder = general_section['target_folder']
    skip_existing = general_section['skip_existing'].lower() in ['true', '1', 't', 'y', 'yes', ]
    files_section = general_section['files']
    urls = [item[1] for item in config.items(files_section)]
    for url in urls:
        (return_value, message) = retrieve_single_file(url, target_folder, skip_existing)
        print message

    print 'Done'

if __name__ == "__main__":
    main()

04/03/2011

fix encoding

Filed under: Uncategorized — Tags: , — morlandi @ 15:03
def fix_encoding(text):
    """ force utf-8 encoding """
    encodings = ('iso-8859-15','utf-8','ascii')
    success = False
    for encoding in encodings:
        try:
            utext = text.decode(encoding)
            success = True
            break
        except:
            success = False
    if success:
        return utext.encode('utf-8')
    return text

super

Filed under: Uncategorized — Tags: — morlandi @ 11:56

Calling base class constructor

class ContactForm(forms.Form):

    def __init__(self, user, *args, **kwargs):
        super(ContactForm, self).__init__(*args, **kwargs)
        if not user.is_authenticated():
            self.fields['captcha'] = CaptchaField()
                self.fields['captcha'] = CaptchaField()

lambda example

Filed under: Uncategorized — Tags: — morlandi @ 11:53
mylist = [1,3,5,7,]
text = ','.join(map(lambda x: str(x),mylist))
print text

*result*:
'1,3,5,7'

Pretty print

Filed under: Uncategorized — Tags: — morlandi @ 11:19
from pprint import pprint
a = [0, 1, ['a', 'b', 'c'], 2, 3, 4]
pprint(a)

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.