<commit_before>import re
import time, threading
import os, subprocess

from flask import Flask
from flask import render_template
from flask import url_for
from flask import request

from twilio import twiml
from twilio.util import TwilioCapability
from twilio.rest import TwilioRestClient
import datetime
import helper

# Declare and configure application
app = Flask(__name__, static_url_path='/static')
app.config.from_pyfile('local_settings.py')

db = {}

db["+16172099765"] = {
    "name":"Santa Claus",
    "dob":datetime.datetime(2014, 7, 05),
    "zipcode":"02139",
    "bcg":datetime.datetime(2014, 7, 06),
    "hepb1":datetime.datetime(2014, 7, 06),
    "hepb2":datetime.datetime(2014, 8, 20),
    "hepb3":0,
    "polio1":datetime.datetime(2014, 7, 06),
    "polio2":datetime.datetime(2014, 8, 20),
    "polio3":datetime.datetime(2014, 9, 20),
    "polio4":0,
    "polio5":0,
    "polio6":0,
    "polio7":0,
    "polio8":0,
    "dtp1":0,
    "dtp2":0,
    "dtp3":0,
    "dtp4":0,
    "dtp5":0,
    "tdap":0,
    "hib1":0,
    "hib2":0,
    "hib3":0,
    "hib4":0,
    "pcv1":datetime.datetime(2014, 8, 20),
    "pcv2":datetime.datetime(2014, 9, 20),
    "pcv3":0,
    "pcv4":0,
    "rv1":datetime.datetime(2014, 8, 20),
    "rv2":datetime.datetime(2014, 9, 20),
    "rv3":0,
    "measles":0,
    "mmr1":0,
    "mmr2":0,
    "var1":0,
    "var2":0,
    "hepa1":0,
    "hepa2":0,
    "typhoid":0,
    "hpv1":0,
    "hpv2":0,
    "hpv3":0,
}

vaccineCode = {
    "9e4f":"bcg",
    "3ifk":"hepb1",
    "36dg":"hepb2",
    "eijl":"hepb3",
    "q52y":"polio1",
    "u0sv":"polio2",
    "26s2":"dtp1",
    "qe10":"dtp2",
}

vaccineTimes = {
    "bcg":datetime.timedelta(days = 0),
    "hepb1":datetime.timedelta(days = 0),
    "hepb2":datetime.timedelta(days = 45),
    "hepb3":datetime.timedelta(days = 185),
    "polio1":datetime.timedelta(days = 0),
    "polio2":datetime.timedelta(days = 45),
    "polio3":datetime.timedelta(days = 70),
    "polio4":datetime.timedelta(days = 100),
    "polio5":datetime.timedelta(days = 185),
    "polio6":datetime.timedelta(days = 275),
    "polio7":datetime.timedelta(days = 365),
    "polio8":datetime.timedelta(days = 1460),
    "dtp1":datetime.timedelta(days = 45),
    "dtp2":datetime.timedelta(days = 70),
    "dtp3":datetime.timedelta(days = 100),
    "dtp4":datetime.timedelta(days = 455),
    "dtp5":datetime.timedelta(days = 1460),
    "tdap":datetime.timedelta(days = 4015),
    "hib1":datetime.timedelta(days = 45),
    "hib2":datetime.timedelta(days = 70),
    "hib3":datetime.timedelta(days = 100),
    "hib4":datetime.timedelta(days = 365),
    "pcv1":datetime.timedelta(days = 45),
    "pcv2":datetime.timedelta(days = 70),
    "pcv3":datetime.timedelta(days = 100),
    "pcv4":datetime.timedelta(days = 365),
    "rv1":datetime.timedelta(days = 45),
    "rv2":datetime.timedelta(days = 70),
    "rv3":datetime.timedelta(days = 100),
    "measles":datetime.timedelta(days = 270),
    "mmr1":datetime.timedelta(days = 365),
    "mmr2":datetime.timedelta(days = 1460),
    "var1":datetime.timedelta(days = 455),
    "var2":datetime.timedelta(days = 1460),
    "hepa1":datetime.timedelta(days = 365),
    "hepa2":datetime.timedelta(days = 365),
    "typhoid":datetime.timedelta(days = 730),
    "hpv1":datetime.timedelta(days = 4015),
    "hpv2":datetime.timedelta(days = 4015),
    "hpv3":datetime.timedelta(days = 4015),
}

def foo():
    client = TwilioRestClient(os.environ.get('TWILIO_ACCOUNT_SID', None), os.environ.get('TWILIO_AUTH_TOKEN', None))
    for number in db:
        needed = []
        for vaccine in vaccineTimes:
            if (datetime.datetime.now() - db[number]["dob"]) >= vaccineTimes[vaccine] and db[number][vaccine] == 0:
                needed.append(vaccine.upper())
        if len(needed) > 0:
            body = "Your child is due for the following vaccines: "
            for need in needed:
                body += need.upper() + ", "
            body = body[:-2]
            message = client.messages.create(to=number, from_="+14088053907", body=body)
    threading.Timer(7*86400, foo).start()

foo()

# Voice Request URL
@app.route('/voice', methods=['GET', 'POST'])
def voice():
    response = twiml.Response()
    response.say("Congratulations! You deployed the Twilio Hackpack "
                 "for Heroku and Flask.")
    return str(response)

# SMS Request URL
@app.route('/sms', methods=['GET', 'POST'])
def sms():
    response = twiml.Response()
    from_number = request.values.get('From', None)
    body = request.form['Body']
    reg = re.compile('[r|R]eg name:[\s|\S]+ dob:[\s|\S]+ zipcode:[\s|\S]+')
    send = re.compile('[s|S]end \+\d\d\d\d\d\d\d\d\d\d\d')
    email = re.compile('[e|E]mail [\s|\S]+@[\s|\S]+.[\s|\S]+')
    if reg.match(body) is not None:
        if from_number in db:
            response.sms("User already exists!")
        else:
            input = re.split(' name:| dob:| zipcode:', body)
            name = input[1]
            dobarray = input[2].split('/')
            month = dobarray[0]
            day = dobarray[1]
            year = dobarray[2]
            dob = datetime.datetime(int(year), int(month), int(day))
            zipcode = input[3]
            db[from_number] = {
                "name":name,
                "dob":dob,
                "zipcode":zipcode,
                "bcg":0,
                "hepb1":0,
                "hepb2":0,
                "hepb3":0,
                "polio1":0,
                "polio2":0,
                "polio3":0,
                "polio4":0,
                "polio5":0,
                "polio6":0,
                "polio7":0,
                "polio8":0,
                "dtp1":0,
                "dtp2":0,
                "dtp3":0,
                "dtp4":0,
                "dtp5":0,
                "tdap":0,
                "hib1":0,
                "hib2":0,
                "hib3":0,
                "hib4":0,
                "pcv1":0,
                "pcv2":0,
                "pcv3":0,
                "pcv4":0,
                "rv1":0,
                "rv2":0,
                "rv3":0,
                "measles":0,
                "mmr1":0,
                "mmr2":0,
                "var1":0,
                "var2":0,
                "hepa1":0,
                "hepa2":0,
                "typhoid":0,
                "hpv1":0,
                "hpv2":0,
                "hpv3":0,
            }
            response.sms(helper.confirmationMsg(name, dob, zipcode))
    elif from_number not in db:
        response.sms("Please register first!")
    elif body in vaccineCode:
        if db[from_number][vaccineCode[body]] != 0:
            response.sms("Already received vaccine!")
        else:
            db[from_number][vaccineCode[body]] = datetime.datetime.now()
            response.sms(helper.responseFromVaccine(vaccineCode[body]))

    elif body == "record" or body =="Record":
        response.sms(helper.info(db[from_number], vaccineTimes))
    elif send.match(body) is not None:
        inputs = body.split('+')
        args = ['curl', '-u', 'zD93Tl5vChhg63gpeRLF8JiSvVBCaKEk:AZmEit4I6WQQ19y5QCHWyOVjU5VV2JrpdPtqaIXc4R8In0xQ', '-vX', 'POST', 'https://api.tigertext.me/v1/message', '--data-urlencode', 'recipient=+' + str(inputs[1]), '--data', 'body=' + helper.sendInfo(db[from_number], vaccineTimes, db[from_number]["name"]), '--data', 'ttl=7200']
        subprocess.call(args)
        response.sms("Success: record sent")
    elif email.match(body) is not None:
        addr = body.split()[1]
        args = ['curl', '--user', '68cd5472024e0b9bd997d7d3ba2ed22f:e7e35bfa50119ca4bf77bc6dd534f43e', 'https://api.mailjet.com/v3/send', '-F', 'from=nkbuduma@mit.edu', '-F', 'to=' + addr, '-F', 'subject=Immunization Record for ' + db[from_number]["name"], '-F', 'text=' + helper.sendInfo(db[from_number], vaccineTimes, db[from_number]["name"])]
        subprocess.call(args)
        response.sms("Success: record sent")
    else:
        response.sms("Error: Ill-formed Submission")

    return str(response)

# Twilio Client demo template
@app.route('/client')
def client():
    configuration_error = None
    for key in ('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN', 'TWILIO_APP_SID',
                'TWILIO_CALLER_ID'):
        if not app.config.get(key, None):
            configuration_error = "Missing from local_settings.py: " \
                                  "{0}".format(key)
            token = None

    if not configuration_error:
        capability = TwilioCapability(app.config['TWILIO_ACCOUNT_SID'],
                                      app.config['TWILIO_AUTH_TOKEN'])
        capability.allow_client_incoming("joey_ramone")
        capability.allow_client_outgoing(app.config['TWILIO_APP_SID'])
        token = capability.generate()
    params = {'token': token}
    return render_template('client.html', params=params,
                           configuration_error=configuration_error)

@app.route('/client/incoming', methods=['POST'])
def client_incoming():
    try:
        from_number = request.values.get('PhoneNumber', None)

        resp = twiml.Response()

        if not from_number:
            resp.say("Your app is missing a Phone Number. "
                     "Make a request with a Phone Number to make outgoing "
                     "calls with the Twilio hack pack.")
            return str(resp)

        if 'TWILIO_CALLER_ID' not in app.config:
            resp.say(
                "Your app is missing a Caller ID parameter. "
                "Please add a Caller ID to make outgoing calls with Twilio "
                "Client")
            return str(resp)

        with resp.dial(callerId=app.config['TWILIO_CALLER_ID']) as r:
            # If we have a number, and it looks like a phone number:
            if from_number and re.search('^[\d\(\)\- \+]+$', from_number):
                r.number(from_number)
            else:
                r.say("We couldn't find a phone number to dial. Make sure "
                      "you are sending a Phone Number when you make a "
                      "request with Twilio Client")

        return str(resp)

    except:
        resp = twiml.Response()
        resp.say("An error occurred. Check your debugger at twilio dot com "
                 "for more information.")
        return str(resp)

# Installation success page
@app.route('/')
def index():
    params = {
        'Voice Request URL': url_for('.voice', _external=True),
        'SMS Request URL': url_for('.sms', _external=True),
        'Client URL': url_for('.client', _external=True)}
    return render_template('index.html', params=params,
                           configuration_error=None)
<commit_msg>updated phone number<commit_after>import re
import time, threading
import os, subprocess

from flask import Flask
from flask import render_template
from flask import url_for
from flask import request

from twilio import twiml
from twilio.util import TwilioCapability
from twilio.rest import TwilioRestClient
import datetime
import helper

# Declare and configure application
app = Flask(__name__, static_url_path='/static')
app.config.from_pyfile('local_settings.py')

db = {}

db["+14082195981"] = {
    "name":"Little Nikhil",
    "dob":datetime.datetime(2014, 7, 05),
    "zipcode":"02139",
    "bcg":datetime.datetime(2014, 7, 06),
    "hepb1":datetime.datetime(2014, 7, 06),
    "hepb2":datetime.datetime(2014, 8, 20),
    "hepb3":0,
    "polio1":datetime.datetime(2014, 7, 06),
    "polio2":datetime.datetime(2014, 8, 20),
    "polio3":datetime.datetime(2014, 9, 20),
    "polio4":0,
    "polio5":0,
    "polio6":0,
    "polio7":0,
    "polio8":0,
    "dtp1":0,
    "dtp2":0,
    "dtp3":0,
    "dtp4":0,
    "dtp5":0,
    "tdap":0,
    "hib1":0,
    "hib2":0,
    "hib3":0,
    "hib4":0,
    "pcv1":datetime.datetime(2014, 8, 20),
    "pcv2":datetime.datetime(2014, 9, 20),
    "pcv3":0,
    "pcv4":0,
    "rv1":datetime.datetime(2014, 8, 20),
    "rv2":datetime.datetime(2014, 9, 20),
    "rv3":0,
    "measles":0,
    "mmr1":0,
    "mmr2":0,
    "var1":0,
    "var2":0,
    "hepa1":0,
    "hepa2":0,
    "typhoid":0,
    "hpv1":0,
    "hpv2":0,
    "hpv3":0,
}

vaccineCode = {
    "9e4f":"bcg",
    "3ifk":"hepb1",
    "36dg":"hepb2",
    "eijl":"hepb3",
    "q52y":"polio1",
    "u0sv":"polio2",
    "26s2":"dtp1",
    "qe10":"dtp2",
}

vaccineTimes = {
    "bcg":datetime.timedelta(days = 0),
    "hepb1":datetime.timedelta(days = 0),
    "hepb2":datetime.timedelta(days = 45),
    "hepb3":datetime.timedelta(days = 185),
    "polio1":datetime.timedelta(days = 0),
    "polio2":datetime.timedelta(days = 45),
    "polio3":datetime.timedelta(days = 70),
    "polio4":datetime.timedelta(days = 100),
    "polio5":datetime.timedelta(days = 185),
    "polio6":datetime.timedelta(days = 275),
    "polio7":datetime.timedelta(days = 365),
    "polio8":datetime.timedelta(days = 1460),
    "dtp1":datetime.timedelta(days = 45),
    "dtp2":datetime.timedelta(days = 70),
    "dtp3":datetime.timedelta(days = 100),
    "dtp4":datetime.timedelta(days = 455),
    "dtp5":datetime.timedelta(days = 1460),
    "tdap":datetime.timedelta(days = 4015),
    "hib1":datetime.timedelta(days = 45),
    "hib2":datetime.timedelta(days = 70),
    "hib3":datetime.timedelta(days = 100),
    "hib4":datetime.timedelta(days = 365),
    "pcv1":datetime.timedelta(days = 45),
    "pcv2":datetime.timedelta(days = 70),
    "pcv3":datetime.timedelta(days = 100),
    "pcv4":datetime.timedelta(days = 365),
    "rv1":datetime.timedelta(days = 45),
    "rv2":datetime.timedelta(days = 70),
    "rv3":datetime.timedelta(days = 100),
    "measles":datetime.timedelta(days = 270),
    "mmr1":datetime.timedelta(days = 365),
    "mmr2":datetime.timedelta(days = 1460),
    "var1":datetime.timedelta(days = 455),
    "var2":datetime.timedelta(days = 1460),
    "hepa1":datetime.timedelta(days = 365),
    "hepa2":datetime.timedelta(days = 365),
    "typhoid":datetime.timedelta(days = 730),
    "hpv1":datetime.timedelta(days = 4015),
    "hpv2":datetime.timedelta(days = 4015),
    "hpv3":datetime.timedelta(days = 4015),
}

def foo():
    client = TwilioRestClient(os.environ.get('TWILIO_ACCOUNT_SID', None), os.environ.get('TWILIO_AUTH_TOKEN', None))
    for number in db:
        needed = []
        for vaccine in vaccineTimes:
            if (datetime.datetime.now() - db[number]["dob"]) >= vaccineTimes[vaccine] and db[number][vaccine] == 0:
                needed.append(vaccine.upper())
        if len(needed) > 0:
            body = "Your child is due for the following vaccines: "
            for need in needed:
                body += need.upper() + ", "
            body = body[:-2]
            message = client.messages.create(to=number, from_="+14088053907", body=body)
    threading.Timer(7*86400, foo).start()

foo()

# Voice Request URL
@app.route('/voice', methods=['GET', 'POST'])
def voice():
    response = twiml.Response()
    response.say("Congratulations! You deployed the Twilio Hackpack "
                 "for Heroku and Flask.")
    return str(response)

# SMS Request URL
@app.route('/sms', methods=['GET', 'POST'])
def sms():
    response = twiml.Response()
    from_number = request.values.get('From', None)
    body = request.form['Body']
    reg = re.compile('[r|R]eg name:[\s|\S]+ dob:[\s|\S]+ zipcode:[\s|\S]+')
    send = re.compile('[s|S]end \+\d\d\d\d\d\d\d\d\d\d\d')
    email = re.compile('[e|E]mail [\s|\S]+@[\s|\S]+.[\s|\S]+')
    if reg.match(body) is not None:
        if from_number in db:
            response.sms("User already exists!")
        else:
            input = re.split(' name:| dob:| zipcode:', body)
            name = input[1]
            dobarray = input[2].split('/')
            month = dobarray[0]
            day = dobarray[1]
            year = dobarray[2]
            dob = datetime.datetime(int(year), int(month), int(day))
            zipcode = input[3]
            db[from_number] = {
                "name":name,
                "dob":dob,
                "zipcode":zipcode,
                "bcg":0,
                "hepb1":0,
                "hepb2":0,
                "hepb3":0,
                "polio1":0,
                "polio2":0,
                "polio3":0,
                "polio4":0,
                "polio5":0,
                "polio6":0,
                "polio7":0,
                "polio8":0,
                "dtp1":0,
                "dtp2":0,
                "dtp3":0,
                "dtp4":0,
                "dtp5":0,
                "tdap":0,
                "hib1":0,
                "hib2":0,
                "hib3":0,
                "hib4":0,
                "pcv1":0,
                "pcv2":0,
                "pcv3":0,
                "pcv4":0,
                "rv1":0,
                "rv2":0,
                "rv3":0,
                "measles":0,
                "mmr1":0,
                "mmr2":0,
                "var1":0,
                "var2":0,
                "hepa1":0,
                "hepa2":0,
                "typhoid":0,
                "hpv1":0,
                "hpv2":0,
                "hpv3":0,
            }
            response.sms(helper.confirmationMsg(name, dob, zipcode))
    elif from_number not in db:
        response.sms("Please register first!")
    elif body in vaccineCode:
        if db[from_number][vaccineCode[body]] != 0:
            response.sms("Already received vaccine!")
        else:
            db[from_number][vaccineCode[body]] = datetime.datetime.now()
            response.sms(helper.responseFromVaccine(vaccineCode[body]))

    elif body == "record" or body =="Record":
        response.sms(helper.info(db[from_number], vaccineTimes))
    elif send.match(body) is not None:
        inputs = body.split('+')
        args = ['curl', '-u', 'zD93Tl5vChhg63gpeRLF8JiSvVBCaKEk:AZmEit4I6WQQ19y5QCHWyOVjU5VV2JrpdPtqaIXc4R8In0xQ', '-vX', 'POST', 'https://api.tigertext.me/v1/message', '--data-urlencode', 'recipient=+' + str(inputs[1]), '--data', 'body=' + helper.sendInfo(db[from_number], vaccineTimes, db[from_number]["name"]), '--data', 'ttl=7200']
        subprocess.call(args)
        response.sms("Success: record sent")
    elif email.match(body) is not None:
        addr = body.split()[1]
        args = ['curl', '--user', '68cd5472024e0b9bd997d7d3ba2ed22f:e7e35bfa50119ca4bf77bc6dd534f43e', 'https://api.mailjet.com/v3/send', '-F', 'from=nkbuduma@mit.edu', '-F', 'to=' + addr, '-F', 'subject=Immunization Record for ' + db[from_number]["name"], '-F', 'text=' + helper.sendInfo(db[from_number], vaccineTimes, db[from_number]["name"])]
        subprocess.call(args)
        response.sms("Success: record sent")
    else:
        response.sms("Error: Ill-formed Submission")

    return str(response)

# Twilio Client demo template
@app.route('/client')
def client():
    configuration_error = None
    for key in ('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN', 'TWILIO_APP_SID',
                'TWILIO_CALLER_ID'):
        if not app.config.get(key, None):
            configuration_error = "Missing from local_settings.py: " \
                                  "{0}".format(key)
            token = None

    if not configuration_error:
        capability = TwilioCapability(app.config['TWILIO_ACCOUNT_SID'],
                                      app.config['TWILIO_AUTH_TOKEN'])
        capability.allow_client_incoming("joey_ramone")
        capability.allow_client_outgoing(app.config['TWILIO_APP_SID'])
        token = capability.generate()
    params = {'token': token}
    return render_template('client.html', params=params,
                           configuration_error=configuration_error)

@app.route('/client/incoming', methods=['POST'])
def client_incoming():
    try:
        from_number = request.values.get('PhoneNumber', None)

        resp = twiml.Response()

        if not from_number:
            resp.say("Your app is missing a Phone Number. "
                     "Make a request with a Phone Number to make outgoing "
                     "calls with the Twilio hack pack.")
            return str(resp)

        if 'TWILIO_CALLER_ID' not in app.config:
            resp.say(
                "Your app is missing a Caller ID parameter. "
                "Please add a Caller ID to make outgoing calls with Twilio "
                "Client")
            return str(resp)

        with resp.dial(callerId=app.config['TWILIO_CALLER_ID']) as r:
            # If we have a number, and it looks like a phone number:
            if from_number and re.search('^[\d\(\)\- \+]+$', from_number):
                r.number(from_number)
            else:
                r.say("We couldn't find a phone number to dial. Make sure "
                      "you are sending a Phone Number when you make a "
                      "request with Twilio Client")

        return str(resp)

    except:
        resp = twiml.Response()
        resp.say("An error occurred. Check your debugger at twilio dot com "
                 "for more information.")
        return str(resp)

# Installation success page
@app.route('/')
def index():
    params = {
        'Voice Request URL': url_for('.voice', _external=True),
        'SMS Request URL': url_for('.sms', _external=True),
        'Client URL': url_for('.client', _external=True)}
    return render_template('index.html', params=params,
                           configuration_error=None)
<|endoftext|>


<commit_before>from django import template
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
from django.template.loader import render_to_string
from django.utils.encoding import force_text

register = template.Library()

@register.filter(is_safe=True, needs_autoescape=True)
def format_threaded_comments(value, autoescape=None):
    """
    Recursively takes a self-nested list and returns an HTML unordered list --
    WITHOUT opening and closing
 tags.

    The list is assumed to be in the proper format. For example, if ``var``
    contains: ``['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]``,
    then ``{{ var|unordered_list }}`` would return::

States

Kansas

Lawrence

Topeka

Illinois

    """
    if autoescape:
        escaper = conditional_escape
    else:
        escaper = lambda x: x
    def convert_old_style_list(list_):
        """
        Converts old style lists to the new easier to understand format.

        The old list format looked like:
            ['Item 1', [['Item 1.1', []], ['Item 1.2', []]]

        And it is converted to:
            ['Item 1', ['Item 1.1', 'Item 1.2]]
        """
        if not isinstance(list_, (tuple, list)) or len(list_) != 2:
            return list_, False
        first_item, second_item = list_
        if second_item == []:
            return [first_item], True
        try:
            # see if second item is iterable
            iter(second_item)
        except TypeError:
            return list_, False
        old_style_list = True
        new_second_item = []
        for sublist in second_item:
            item, old_style_list = convert_old_style_list(sublist)
            if not old_style_list:
                break
            new_second_item.extend(item)
        if old_style_list:
            second_item = new_second_item
        return [first_item, second_item], old_style_list
    def _helper(list_, tabs=1):
        indent = '\t' * tabs
        output = []

        list_length = len(list_)
        i = 0
        while i < list_length:
            title = list_[i]
            sublist = ''
            sublist_item = None
            if isinstance(title, (list, tuple)):
                sublist_item = title
                title = ''
            elif i < list_length - 1:
                next_item = list_[i+1]
                if next_item and isinstance(next_item, (list, tuple)):
                    # The next item is a sub-list.
                    sublist_item = next_item
                    # We've processed the next item now too.
                    i += 1
            if sublist_item:
                sublist = _helper(sublist_item, tabs+1)
                sublist = '\n%s
\n%s\n%s
\n%s' % (indent, sublist,
                                                         indent, indent)

            output.append('%s
%s%s
' % (indent,
                    escaper(force_text(title)), sublist))
            i += 1
        return '\n'.join(output)
    value, converted = convert_old_style_list(value)
    return mark_safe(_helper(value))<commit_msg>Removing force_text import which seems to break things.<commit_after>from django import template
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
from django.template.loader import render_to_string

register = template.Library()

@register.filter(is_safe=True, needs_autoescape=True)
def format_threaded_comments(value, autoescape=None):
    """
    Recursively takes a self-nested list and returns an HTML unordered list --
    WITHOUT opening and closing
 tags.

    The list is assumed to be in the proper format. For example, if ``var``
    contains: ``['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]``,
    then ``{{ var|unordered_list }}`` would return::

States

Kansas

Lawrence

Topeka

Illinois

    """
    if autoescape:
        escaper = conditional_escape
    else:
        escaper = lambda x: x
    def convert_old_style_list(list_):
        """
        Converts old style lists to the new easier to understand format.

        The old list format looked like:
            ['Item 1', [['Item 1.1', []], ['Item 1.2', []]]

        And it is converted to:
            ['Item 1', ['Item 1.1', 'Item 1.2]]
        """
        if not isinstance(list_, (tuple, list)) or len(list_) != 2:
            return list_, False
        first_item, second_item = list_
        if second_item == []:
            return [first_item], True
        try:
            # see if second item is iterable
            iter(second_item)
        except TypeError:
            return list_, False
        old_style_list = True
        new_second_item = []
        for sublist in second_item:
            item, old_style_list = convert_old_style_list(sublist)
            if not old_style_list:
                break
            new_second_item.extend(item)
        if old_style_list:
            second_item = new_second_item
        return [first_item, second_item], old_style_list
    def _helper(list_, tabs=1):
        indent = '\t' * tabs
        output = []

        list_length = len(list_)
        i = 0
        while i < list_length:
            title = list_[i]
            sublist = ''
            sublist_item = None
            if isinstance(title, (list, tuple)):
                sublist_item = title
                title = ''
            elif i < list_length - 1:
                next_item = list_[i+1]
                if next_item and isinstance(next_item, (list, tuple)):
                    # The next item is a sub-list.
                    sublist_item = next_item
                    # We've processed the next item now too.
                    i += 1
            if sublist_item:
                sublist = _helper(sublist_item, tabs+1)
                sublist = '\n%s
\n%s\n%s
\n%s' % (indent, sublist,
                                                         indent, indent)

            output.append('%s
%s%s
' % (indent,
                    escaper(title), sublist))
            i += 1
        return '\n'.join(output)
    value, converted = convert_old_style_list(value)
    return mark_safe(_helper(value))<|endoftext|>


<commit_before>import translator
import os
import sys
import util

LIB_PATH = os.path.join(os.path.dirname(__file__), 'lib')
BUILTIN_PATH = os.path.join(os.path.dirname(__file__), 'builtin')

_path_cache= {}
def module_path(name, path):
    global _path_cache
    k = (name, tuple(sorted(path)))
    if k in _path_cache:
        return _path_cache[k]
    parts = name.split('.')
    candidates = []
    tail = []
    packages = {}
    modules = {}
    for pn in parts:
        tail.append(pn)
        for p in path:
            cp = os.path.join(*([p] + tail))
            if os.path.isdir(cp) and os.path.exists(
                os.path.join(cp, '__init__.py')):
                packages['.'.join(tail)] = os.path.join(cp, '__init__.py')
            elif os.path.exists(cp + '.py'):
                modules['.'.join(tail)] = cp + '.py'
    res = None
    if modules:
        assert len(modules)==1
        res = modules.get(name)
        if not res and '.'.join(name.split('.')[:-1]) not in modules:
            raise RuntimeError, "Module %r not found" % name

    elif packages:
        res = packages[sorted(packages)[-1]]
    _path_cache[k] = res
    return res

class BaseLinker(object):

    platform_parents = {}

    def __init__(self, top_module, output='output',
                 debug=False, js_libs=[], platforms=[], path=[],
                 translator_arguments={},
                 compile_inplace=False):
        self.js_path = os.path.abspath(output)
        self.path = path + [LIB_PATH]
        self.platforms = platforms
        self.top_module = top_module
        self.output = os.path.abspath(output)
        self.js_libs = js_libs
        self.translator_arguments = translator_arguments
        self.compile_inplace = compile_inplace
        self.top_module_path = None

    def __call__(self):
        self.visited_modules = {}
        self.done = {}
        self.dependencies = {}
        self.visit_start()
        for platform in [None] + self.platforms:
            self.visit_start_platform(platform)
            old_path = self.path
            self.path = [BUILTIN_PATH, LIB_PATH]
            self.visit_modules(['pyjslib'], platform)
            self.path = old_path
            self.visit_modules([self.top_module], platform)
            self.visit_end_platform(platform)
        self.visit_end()

    def visit_modules(self, module_names, platform=None):
        prefix = ''
        all_names = []
        for mn in module_names:
            prefix = ''
            for part in mn.split('.')[:-1]:
                pn = prefix + part
                prefix = pn + '.'
                if pn not in all_names:
                    all_names.append(pn)
            all_names.append(mn)

        for mn in all_names:
            p = module_path(mn, self.path)
            if not p:
                continue
                raise RuntimeError, "Module %r found. Dep of %r" % (
                    mn, self.dependencies)
            if mn==self.top_module:
                self.top_module_path = p
            override_paths=[]
            if platform:
                for pl in [platform] + self.platform_parents.get(platform, []):
                    override_path = module_path('__%s__.%s' % (pl, mn),
                                                self.path)
                    # prevent package overrides
                    if override_path and not override_path.endswith('__init__.py'):
                        override_paths.append(override_path)
            self.visit_module(p, override_paths, platform, module_name=mn)

    def visit_module(self, file_path, overrides, platform,
                     module_name):
        # look if we have a public dir
        dir_name, file_name = os.path.split(file_path)
        if file_name.split('.')[0] != module_name.split('.')[-1]:
            if file_name == "__init__.py":
                if os.path.basename(dir_name) != module_name.split('.')[-1]:
                    return
            else:
                return
        self.merge_resources(dir_name)
        if platform and overrides:
            plat_suffix = '.__%s__' % platform
        else:
            plat_suffix = ''
        if self.compile_inplace:
            mod_part, extension = os.path.splitext(file_path)
            out_file = '%s%s.js' % (mod_part, plat_suffix)
        else:
            out_file = os.path.join(self.output, 'lib',
                                    '%s%s.js' % (module_name, plat_suffix))
        if out_file in self.done.get(platform, []):
            return
        # translate if
        #  -    no platform
        #  - or if we have an override
        #  - or the module is used in an override only
        if (   platform is None
            or (platform and overrides)
            or (out_file not in self.done.get(None,[]))
           ):
            deps = translator.translate([file_path] +  overrides,
                                        out_file,
                                        module_name=module_name,
                                        **self.translator_arguments)
            self.dependencies[out_file] = deps
            if '.' in module_name:
                for i, dep in enumerate(deps):
                    if module_path(dep, path=[dir_name]):
                        deps[i] = '.'.join(module_name.split('.')[:-1] + [dep])
        else:
            deps = self.dependencies[out_file]
        if out_file not in self.done.setdefault(platform, []):
            self.done[platform].append(out_file)
        if module_name not in self.visited_modules.setdefault(platform, []):
            self.visited_modules[platform].append(module_name)
        if deps:
            self.visit_modules(deps, platform)

    def merge_resources(self, dir_name):
        """gets a directory path for each module visited, this can be
        used to collect resources e.g. public folders"""
        pass

    def visit_start(self):
        if not os.path.exists(self.output):
            os.mkdir(self.output)
        if not self.compile_inplace:
            lib_dir = os.path.join(self.output, 'lib')
            if not os.path.exists(lib_dir):
                os.mkdir(lib_dir)

    def visit_start_platform(self, platform):
        pass

    def visit_end_platform(self, platform):
        pass

    def visit_end(self):
        pass

def add_linker_options(parser):
    parser.add_option("-o", "--output", dest="output", default='output',
                      help="directory to which the app should be written")

    parser.add_option("-j", "--include-js", dest="js_includes",
                      action="append", default=[],
                      help="javascripts to load into the same frame as the rest of the script")
    parser.add_option("-I", "--library_dir", dest="library_dirs",
                      default=[],
                      action="append", help="additional paths appended to PYJSPATH")

<commit_msg>platform-specific overrides must come _after_ the parents, so that they get merged on top<commit_after>import translator
import os
import sys
import util

LIB_PATH = os.path.join(os.path.dirname(__file__), 'lib')
BUILTIN_PATH = os.path.join(os.path.dirname(__file__), 'builtin')

_path_cache= {}
def module_path(name, path):
    global _path_cache
    k = (name, tuple(sorted(path)))
    if k in _path_cache:
        return _path_cache[k]
    parts = name.split('.')
    candidates = []
    tail = []
    packages = {}
    modules = {}
    for pn in parts:
        tail.append(pn)
        for p in path:
            cp = os.path.join(*([p] + tail))
            if os.path.isdir(cp) and os.path.exists(
                os.path.join(cp, '__init__.py')):
                packages['.'.join(tail)] = os.path.join(cp, '__init__.py')
            elif os.path.exists(cp + '.py'):
                modules['.'.join(tail)] = cp + '.py'
    res = None
    if modules:
        assert len(modules)==1
        res = modules.get(name)
        if not res and '.'.join(name.split('.')[:-1]) not in modules:
            raise RuntimeError, "Module %r not found" % name

    elif packages:
        res = packages[sorted(packages)[-1]]
    _path_cache[k] = res
    return res

class BaseLinker(object):

    platform_parents = {}

    def __init__(self, top_module, output='output',
                 debug=False, js_libs=[], platforms=[], path=[],
                 translator_arguments={},
                 compile_inplace=False):
        self.js_path = os.path.abspath(output)
        self.path = path + [LIB_PATH]
        self.platforms = platforms
        self.top_module = top_module
        self.output = os.path.abspath(output)
        self.js_libs = js_libs
        self.translator_arguments = translator_arguments
        self.compile_inplace = compile_inplace
        self.top_module_path = None

    def __call__(self):
        self.visited_modules = {}
        self.done = {}
        self.dependencies = {}
        self.visit_start()
        for platform in [None] + self.platforms:
            self.visit_start_platform(platform)
            old_path = self.path
            self.path = [BUILTIN_PATH, LIB_PATH]
            self.visit_modules(['pyjslib'], platform)
            self.path = old_path
            self.visit_modules([self.top_module], platform)
            self.visit_end_platform(platform)
        self.visit_end()

    def visit_modules(self, module_names, platform=None):
        prefix = ''
        all_names = []
        for mn in module_names:
            prefix = ''
            for part in mn.split('.')[:-1]:
                pn = prefix + part
                prefix = pn + '.'
                if pn not in all_names:
                    all_names.append(pn)
            all_names.append(mn)

        for mn in all_names:
            p = module_path(mn, self.path)
            if not p:
                continue
                raise RuntimeError, "Module %r found. Dep of %r" % (
                    mn, self.dependencies)
            if mn==self.top_module:
                self.top_module_path = p
            override_paths=[]
            if platform:
                for pl in self.platform_parents.get(platform, []) + [platform]:
                    override_path = module_path('__%s__.%s' % (pl, mn),
                                                self.path)
                    # prevent package overrides
                    if override_path and not override_path.endswith('__init__.py'):
                        override_paths.append(override_path)
            self.visit_module(p, override_paths, platform, module_name=mn)

    def visit_module(self, file_path, overrides, platform,
                     module_name):
        # look if we have a public dir
        dir_name, file_name = os.path.split(file_path)
        if file_name.split('.')[0] != module_name.split('.')[-1]:
            if file_name == "__init__.py":
                if os.path.basename(dir_name) != module_name.split('.')[-1]:
                    return
            else:
                return
        self.merge_resources(dir_name)
        if platform and overrides:
            plat_suffix = '.__%s__' % platform
        else:
            plat_suffix = ''
        if self.compile_inplace:
            mod_part, extension = os.path.splitext(file_path)
            out_file = '%s%s.js' % (mod_part, plat_suffix)
        else:
            out_file = os.path.join(self.output, 'lib',
                                    '%s%s.js' % (module_name, plat_suffix))
        if out_file in self.done.get(platform, []):
            return
        # translate if
        #  -    no platform
        #  - or if we have an override
        #  - or the module is used in an override only
        if (   platform is None
            or (platform and overrides)
            or (out_file not in self.done.get(None,[]))
           ):
            deps = translator.translate([file_path] +  overrides,
                                        out_file,
                                        module_name=module_name,
                                        **self.translator_arguments)
            self.dependencies[out_file] = deps
            if '.' in module_name:
                for i, dep in enumerate(deps):
                    if module_path(dep, path=[dir_name]):
                        deps[i] = '.'.join(module_name.split('.')[:-1] + [dep])
        else:
            deps = self.dependencies[out_file]
        if out_file not in self.done.setdefault(platform, []):
            self.done[platform].append(out_file)
        if module_name not in self.visited_modules.setdefault(platform, []):
            self.visited_modules[platform].append(module_name)
        if deps:
            self.visit_modules(deps, platform)

    def merge_resources(self, dir_name):
        """gets a directory path for each module visited, this can be
        used to collect resources e.g. public folders"""
        pass

    def visit_start(self):
        if not os.path.exists(self.output):
            os.mkdir(self.output)
        if not self.compile_inplace:
            lib_dir = os.path.join(self.output, 'lib')
            if not os.path.exists(lib_dir):
                os.mkdir(lib_dir)

    def visit_start_platform(self, platform):
        pass

    def visit_end_platform(self, platform):
        pass

    def visit_end(self):
        pass

def add_linker_options(parser):
    parser.add_option("-o", "--output", dest="output", default='output',
                      help="directory to which the app should be written")

    parser.add_option("-j", "--include-js", dest="js_includes",
                      action="append", default=[],
                      help="javascripts to load into the same frame as the rest of the script")
    parser.add_option("-I", "--library_dir", dest="library_dirs",
                      default=[],
                      action="append", help="additional paths appended to PYJSPATH")

<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function, unicode_literals

import copy
import getopt
import os, os.path
import re
import shlex
import subprocess
import sys

from pprint import pprint, pformat

__version__ = '0.1'

class Config:
    def __init__(self, **kw):
        self.regexp = False
        self.pattern = None
        self.mode = 'all'
        self.source = None
        self.ignorecase = True
        self.regex_multiline = False
        self.regex_dotall = False
        self.fnmatch_begin = False
        self.fnmatch_end = False
        self.prefix = False
        self.execute = None
        self.verbose_exec = False
        self.interactive_exec = False
        self.invert_match = False
        self.display = True
        self.delim = "\n";
        self.help = False
        self.vcs = False

        for key in kw:
            setattr(self, key, kw[key])

def ask(question, replies, default):
    replies = [ reply.lower() for reply in replies ]
    question += ' (' + ','.join(reply.upper() if reply == default else reply for reply in replies) + ') '
    while True:
        reply = raw_input(question).lower()
        if reply in replies:
            return reply

def parse_input_args(args):
    cfg = Config()

    opts_short = 'gp:m:s:ildBEhx:v0'
    opts_long  = ('regexp', 'pattern=', 'mode=', 'source=', 'ignorecase', 'regex-multiline', 'regex-dotall',
                'begin', 'end', 'prefix', 'help', 'exec=', 'invert-match', 'print0', 'no-display', 'verbose-exec', 'interactive-exec',
                 'vcs')
    opts, args = getopt.gnu_getopt(args, opts_short, opts_long)

    for o, a in opts:
        if o in ('-g', '--regexp'):
            cfg.regexp = True
        elif o in ('-p', '--pattern'):
            cfg.pattern = a
        elif o in ('-m', '--mode'):
            if a not in ('files', 'dirs', 'all'):
                raise getopt.error('Mode must be one of: "files", "dirs", "all".')

            cfg.mode = a
        elif o in ('-s', '--source'):
            if not os.path.isdir(a):
                raise getopt.error('Source "%s" doesn\'t exists or is not a directory' % a)

            try:
                cfg.source.append(a)
            except AttributeError:
                cfg.source = [a]
        elif o in ('-i', '--ignorecase'):
            cfg.ignorecase = True
        elif o in ('-l', '--regex-multiline'):
            cfg.regex_multiline = True
        elif o in ('-d', '--regex-dotall'):
            cfg.regex_dotall = True
        elif o in ('-B', '--begin'):
            cfg.fnmatch_begin = True
        elif o in ('-E', '--end'):
            cfg.fnmatch_end = True
        elif o == '--prefix':
            cfg.prefix = True
        elif o in ('-x', '--exec'):
            cfg.execute = shlex.split(a)
        elif  o in ('--verbose-exec'):
            cfg.verbose_exec = True
        elif o in ('-v', '--invert-match'):
            cfg.invert_match = True
        elif o in ('--interactive-exec'):
            cfg.interactive_exec = True
        elif o in ('--no-display'):
            cfg.display = False
        elif o in ('-0', '--print0'):
            cfg.delim = chr(0)
        elif o in ('--vcs'):
            cfg.vcs = True
        elif o in ('-h', '--help'):
            return Config(help=True)

    if cfg.pattern is None:
        if args:
            cfg.pattern = args.pop(0)
        else:
            raise getopt.error('Pattern is missing')

    if cfg.source is None:
        if args:
            cfg.source = args
        else:
            cfg.source = ['.']

    for i, src in enumerate(cfg.source):
        cfg.source[i] = os.path.abspath(src)

    return cfg

def prepare_execute(exe, path, dirname, basename):
    exe = copy.copy(exe)
    for i, elem in enumerate(exe):
        exe[i] = exe[i].replace('{path}', path)
        exe[i] = exe[i].replace('{dirname}', dirname)
        exe[i] = exe[i].replace('{basename}', basename)

    return exe

def prepare_pattern(cfg):
    pattern = cfg.pattern
    flags = 0

    if cfg.regexp:
        if cfg.ignorecase:
            flags = flags | re.IGNORECASE
        if cfg.regex_dotall:
            flags = flags | re.DOTALL
        if cfg.regex_multiline:
            flags = flags | re.MULTILINE
    else:
        import fnmatch

        if cfg.ignorecase:
            flags = flags | re.IGNORECASE

        pattern = fnmatch.translate(pattern)
        if cfg.fnmatch_begin:
            pattern = '^' + pattern
        if cfg.fnmatch_end:
            pattern = pattern + '$'

        pattern = re.sub(r'\\Z (?: \( [^)]+ \) )? $', '', pattern, flags=re.VERBOSE)

    pattern = re.compile(pattern, flags)
    return pattern

def process_item(cfg, path):
    m = cfg.pattern.search(os.path.basename(path))
    if (not cfg.invert_match and m) or (cfg.invert_match and not m):
        if cfg.display:
            prefix = ''
            if cfg.prefix:
                prefix = 'd: ' if os.path.isdir(path) else 'f: '
            print(prefix, path, sep='', end=cfg.delim)
        if cfg.execute:
            exe = prepare_execute(cfg.execute, path, os.path.dirname(path), os.path.basename(path))
            if cfg.verbose_exec:
                print(' '.join(exe))
            if not cfg.interactive_exec or ask('Execute command on %s?' % path, 'yn', 'n') == 'y':
                subprocess.call(exe)

def main():
    try:
        config = parse_input_args(sys.argv[1:])
    except getopt.error as e:
        print(e, file=sys.stderr)
        sys.exit(1)

    if config.help:
        print('''%s pattern
        [-0|--print0] split results by binary zero instead of new line (useful to work with xargs)
        [-i|--ignorecase]
        *[-s|--source source] - optional, see: pattern below
        *[-p|--pattern]
        [-g|--regexp] - treat pattern as regular expression (uses Python regexp engine)
        [-l|--regex-multiline]
        [-d|--regex-dotall]
        [-B|--begin] - match pattern to begin of item name (ignored in regexp mode)
        [-E|--end] - match pattern to end of item name (ignored in regexp mode)
        [-v|--invert-match]
        [-m|--mode] - one of: 'all' (default), 'dirs', 'files'
        [-x|--exec] - execute some command on every found item. In command, placeholders: {path}, {dirname}, {basename} are replaced with correct value
        [--prefix=PREFIX] - add prefix 'd: ' (directory) or 'f: ' (file) to every found item
        [--no-display] - don't display element (useful with --exec argument)
        [--verbose-exec] - show command before execute it
        [--interactive-exec] - ask before execute command on every item
        [--vcs]
        [-h|--help]
        pattern - pattern to search
        [source1 .. sourceN] - optional source (if missing, use current directory)''' % os.path.basename(sys.argv[0]))
        sys.exit()

    config.pattern = prepare_pattern(config)

    rxp_vcs = re.compile('(?:^|/)(?:\.git|\.svn|\.CVS|\.hg|_MTN|CVS|RCS|SCCS|_darcs|_sgbak)(?:$|/)')

    for source in config.source:
        for root, dirs, files in os.walk(source):
            if config.mode in ('dirs', 'all'):
                if config.vcs or not rxp_vcs.search(root):
                    process_item(config, root)

            if config.mode in ('files', 'all'):
                for file_ in files:
                    path = os.path.join(root, file_)
                    if config.vcs or not rxp_vcs.search(path):
                        process_item(config, path)

if __name__ == '__main__':
    main()
<commit_msg>add explanation to vcs option<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function, unicode_literals

import copy
import getopt
import os, os.path
import re
import shlex
import subprocess
import sys

from pprint import pprint, pformat

__version__ = '0.1'

class Config:
    def __init__(self, **kw):
        self.regexp = False
        self.pattern = None
        self.mode = 'all'
        self.source = None
        self.ignorecase = True
        self.regex_multiline = False
        self.regex_dotall = False
        self.fnmatch_begin = False
        self.fnmatch_end = False
        self.prefix = False
        self.execute = None
        self.verbose_exec = False
        self.interactive_exec = False
        self.invert_match = False
        self.display = True
        self.delim = "\n";
        self.help = False
        self.vcs = False

        for key in kw:
            setattr(self, key, kw[key])

def ask(question, replies, default):
    replies = [ reply.lower() for reply in replies ]
    question += ' (' + ','.join(reply.upper() if reply == default else reply for reply in replies) + ') '
    while True:
        reply = raw_input(question).lower()
        if reply in replies:
            return reply

def parse_input_args(args):
    cfg = Config()

    opts_short = 'gp:m:s:ildBEhx:v0'
    opts_long  = ('regexp', 'pattern=', 'mode=', 'source=', 'ignorecase', 'regex-multiline', 'regex-dotall',
                'begin', 'end', 'prefix', 'help', 'exec=', 'invert-match', 'print0', 'no-display', 'verbose-exec', 'interactive-exec',
                 'vcs')
    opts, args = getopt.gnu_getopt(args, opts_short, opts_long)

    for o, a in opts:
        if o in ('-g', '--regexp'):
            cfg.regexp = True
        elif o in ('-p', '--pattern'):
            cfg.pattern = a
        elif o in ('-m', '--mode'):
            if a not in ('files', 'dirs', 'all'):
                raise getopt.error('Mode must be one of: "files", "dirs", "all".')

            cfg.mode = a
        elif o in ('-s', '--source'):
            if not os.path.isdir(a):
                raise getopt.error('Source "%s" doesn\'t exists or is not a directory' % a)

            try:
                cfg.source.append(a)
            except AttributeError:
                cfg.source = [a]
        elif o in ('-i', '--ignorecase'):
            cfg.ignorecase = True
        elif o in ('-l', '--regex-multiline'):
            cfg.regex_multiline = True
        elif o in ('-d', '--regex-dotall'):
            cfg.regex_dotall = True
        elif o in ('-B', '--begin'):
            cfg.fnmatch_begin = True
        elif o in ('-E', '--end'):
            cfg.fnmatch_end = True
        elif o == '--prefix':
            cfg.prefix = True
        elif o in ('-x', '--exec'):
            cfg.execute = shlex.split(a)
        elif  o in ('--verbose-exec'):
            cfg.verbose_exec = True
        elif o in ('-v', '--invert-match'):
            cfg.invert_match = True
        elif o in ('--interactive-exec'):
            cfg.interactive_exec = True
        elif o in ('--no-display'):
            cfg.display = False
        elif o in ('-0', '--print0'):
            cfg.delim = chr(0)
        elif o in ('--vcs'):
            cfg.vcs = True
        elif o in ('-h', '--help'):
            return Config(help=True)

    if cfg.pattern is None:
        if args:
            cfg.pattern = args.pop(0)
        else:
            raise getopt.error('Pattern is missing')

    if cfg.source is None:
        if args:
            cfg.source = args
        else:
            cfg.source = ['.']

    for i, src in enumerate(cfg.source):
        cfg.source[i] = os.path.abspath(src)

    return cfg

def prepare_execute(exe, path, dirname, basename):
    exe = copy.copy(exe)
    for i, elem in enumerate(exe):
        exe[i] = exe[i].replace('{path}', path)
        exe[i] = exe[i].replace('{dirname}', dirname)
        exe[i] = exe[i].replace('{basename}', basename)

    return exe

def prepare_pattern(cfg):
    pattern = cfg.pattern
    flags = 0

    if cfg.regexp:
        if cfg.ignorecase:
            flags = flags | re.IGNORECASE
        if cfg.regex_dotall:
            flags = flags | re.DOTALL
        if cfg.regex_multiline:
            flags = flags | re.MULTILINE
    else:
        import fnmatch

        if cfg.ignorecase:
            flags = flags | re.IGNORECASE

        pattern = fnmatch.translate(pattern)
        if cfg.fnmatch_begin:
            pattern = '^' + pattern
        if cfg.fnmatch_end:
            pattern = pattern + '$'

        pattern = re.sub(r'\\Z (?: \( [^)]+ \) )? $', '', pattern, flags=re.VERBOSE)

    pattern = re.compile(pattern, flags)
    return pattern

def process_item(cfg, path):
    m = cfg.pattern.search(os.path.basename(path))
    if (not cfg.invert_match and m) or (cfg.invert_match and not m):
        if cfg.display:
            prefix = ''
            if cfg.prefix:
                prefix = 'd: ' if os.path.isdir(path) else 'f: '
            print(prefix, path, sep='', end=cfg.delim)
        if cfg.execute:
            exe = prepare_execute(cfg.execute, path, os.path.dirname(path), os.path.basename(path))
            if cfg.verbose_exec:
                print(' '.join(exe))
            if not cfg.interactive_exec or ask('Execute command on %s?' % path, 'yn', 'n') == 'y':
                subprocess.call(exe)

def main():
    try:
        config = parse_input_args(sys.argv[1:])
    except getopt.error as e:
        print(e, file=sys.stderr)
        sys.exit(1)

    if config.help:
        print('''%s pattern
        [-0|--print0] split results by binary zero instead of new line (useful to work with xargs)
        [-i|--ignorecase]
        *[-s|--source source] - optional, see: pattern below
        *[-p|--pattern]
        [-g|--regexp] - treat pattern as regular expression (uses Python regexp engine)
        [-l|--regex-multiline]
        [-d|--regex-dotall]
        [-B|--begin] - match pattern to begin of item name (ignored in regexp mode)
        [-E|--end] - match pattern to end of item name (ignored in regexp mode)
        [-v|--invert-match]
        [-m|--mode] - one of: 'all' (default), 'dirs', 'files'
        [-x|--exec] - execute some command on every found item. In command, placeholders: {path}, {dirname}, {basename} are replaced with correct value
        [--prefix=PREFIX] - add prefix 'd: ' (directory) or 'f: ' (file) to every found item
        [--no-display] - don't display element (useful with --exec argument)
        [--verbose-exec] - show command before execute it
        [--interactive-exec] - ask before execute command on every item
        [--vcs] - do not skip VCS directories (.git, .svn etc)
        [-h|--help]
        pattern - pattern to search
        [source1 .. sourceN] - optional source (if missing, use current directory)''' % os.path.basename(sys.argv[0]))
        sys.exit()

    config.pattern = prepare_pattern(config)

    rxp_vcs = re.compile('(?:^|/)(?:\.git|\.svn|\.CVS|\.hg|_MTN|CVS|RCS|SCCS|_darcs|_sgbak)(?:$|/)')

    for source in config.source:
        for root, dirs, files in os.walk(source):
            if config.mode in ('dirs', 'all'):
                if config.vcs or not rxp_vcs.search(root):
                    process_item(config, root)

            if config.mode in ('files', 'all'):
                for file_ in files:
                    path = os.path.join(root, file_)
                    if config.vcs or not rxp_vcs.search(path):
                        process_item(config, path)

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>__version__ = '0.1.1'
<commit_msg>bumping version<commit_after>__version__ = '0.2.1'
<|endoftext|>


<commit_before># Copyright (c) Anand Patil, 2007

__docformat__ = 'reStructuredText'

import pymc as pm
from . import linalg_utils
import copy
import types
import numpy as np
from .gp_submodel import *
import warnings
from pymc.six import print_

from .Realization import Realization
from .Mean import Mean
from .Covariance import Covariance
from .GPutils import observe, regularize_array

__all__ = ['wrap_metropolis_for_gp_parents', 'GPEvaluationGibbs', 'GPParentAdaptiveMetropolis', 'GPStepMethod', 'GPEvaluationMetropolis', 'MeshlessGPMetropolis']

class GPStepMethod(pm.NoStepper):
    @staticmethod
    def competence(stochastic):
        if isinstance(stochastic, GaussianProcess):
            return 1
        else:
            return 0

def wrap_metropolis_for_gp_parents(metro_class):
    """
    Wraps Metropolis step methods so they can handle extended parents of
    Gaussian processes.
    """
    class wrapper(metro_class):
        def __init__(self, stochastic, *args, **kwds):

            self.metro_class.__init__(self, stochastic, *args, **kwds)

            mb = set(self.markov_blanket)
            for c in list(self.children):
                if isinstance(c, GaussianProcess):
                    self.children |= c.extended_children
                    mb |= c.extended_children
            self.markov_blanket = list(mb)

            # Remove f from the set that will be used to compute logp_plus_loglike.
            self.markov_blanket_no_f = set(filter(lambda x: not isinstance(x, GaussianProcess), self.markov_blanket))
            self.fs = filter(lambda x: isinstance(x, GaussianProcess), self.markov_blanket)
            self.fr_checks = [f.submodel.fr_check for f in self.fs]

        def get_logp_plus_loglike(self):
            return pm.logp_of_set(self.markov_blanket_no_f)
        logp_plus_loglike = property(get_logp_plus_loglike)

        def propose(self):
            self.metro_class.propose(self)
            try:
                # First make sure none of the stochastics handled by metro_method forbid their current values.
                for s in self.stochastics:
                    s.logp
                # Then make sure the covariances are all still full-rank on the observation locations.
                for frc in self.fr_checks:
                    frc.logp
                for f in self.fs:
                    f.rand()
                self.f_proposed = True
            except pm.ZeroProbability:
                self.f_proposed = False

        def reject(self):
            self.metro_class.reject(self)
            if self.f_proposed:
                for f in self.fs:
                    f.revert()

        @staticmethod
        def competence(stochastic, metro_class=metro_class):
            if any([isinstance(child, GaussianProcess) for child in stochastic.extended_children]):
                return metro_class.competence(stochastic)+.01
            else:
                return 0

    wrapper.__name__ = 'GPParent%s'%metro_class.__name__
    wrapper.metro_class = metro_class
    wrapper.__doc__ = """A modified version of class %s that handles parents of Gaussian processes.
Docstring of class %s: \n\n%s"""%(metro_class.__name__,metro_class.__name__,metro_class.__doc__)

    return wrapper

# Wrap all registered Metropolis step methods to use GP parents.
new_sm_dict = {}
filtered_registry = filter(lambda x: issubclass(x, pm.Metropolis), pm.StepMethodRegistry)
for sm in filtered_registry:
    wrapped_method = wrap_metropolis_for_gp_parents(sm)
    new_sm_dict[wrapped_method.__name__] = wrapped_method
GPParentAdaptiveMetropolis = wrap_metropolis_for_gp_parents(pm.AdaptiveMetropolis)
__all__ += new_sm_dict.keys()
locals().update(new_sm_dict)

class MeshlessGPMetropolis(pm.Metropolis):
    def __init__(self, gp):
        pm.Metropolis.__init__(self, gp, proposal_distribution='Prior', check_before_accepting=False)

    def propose(self):
        self.stochastic.rand()

    @staticmethod
    def competence(stochastic):
        if isinstance(stochastic, GaussianProcess):
            if len(stochastic.submodel.mesh)==0:
                return 3
            else:
                return 0
        else:
            return 0

class _GPEvaluationMetropolis(pm.Metropolis):
    """
    Updates a GP evaluation, the 'f_eval' attribute of a GP submodel.
    The stationary distribution of the assymetric proposal is equal
    to the prior distribution, an attempt to minimize jumps to values
    forbidden by the prior.
    """
    def __init__(self, stochastic, proposal_sd=1, **kwds):
        pm.Metropolis.__init__(self, stochastic, proposal_sd=proposal_sd, **kwds)

    def propose(self):
        sig = pm.utils.value(self.stochastic.parents['sig'])
        mu = pm.utils.value(self.stochastic.parents['mu'])

        delta = pm.rmv_normal_chol(0*mu, sig)

        beta = np.minimum(1, self.proposal_sd * self.adaptive_scale_factor)
        bsig = beta*sig
        sb2 = np.sqrt(1-beta**2)
        self.stochastic.value = (self.stochastic.value - mu)*sb2+beta*delta+mu
        xp,x = self.stochastic.value, self.stochastic.last_value
        self._hastings_factor = pm.mv_normal_chol_like(x,(xp-mu)*sb2+mu,bsig) - pm.mv_normal_chol_like(xp,(x-mu)*sb2+mu,bsig)

        # self.stochastic.value = self.stochastic.value + self.adaptive_scale_factor*self.proposal_sd*delta
        # self._hastings_factor = 0

    def hastings_factor(self):
        return self._hastings_factor

    @staticmethod
    def competence(stochastic):
        if isinstance(stochastic, GPEvaluation):
            return 3
        else:
            return 0

GPEvaluationMetropolis = wrap_metropolis_for_gp_parents(_GPEvaluationMetropolis)

class GPEvaluationGibbs(pm.Metropolis):
    """
    Updates a GP evaluation f_eval. Assumes the only children of f_eval
    are as distributed follows:

    eps_p_f ~ Normal(f_eval[ti], 1./V)

    or

    eps_p_f ~ Normal(f_eval, 1./V)

    if ti is None.
    """
    def __init__(self, submod, V, eps_p_f, ti=None, tally=True, verbose=0):

        self.f_eval = submod.f_eval
        self.f = submod.f
        pm.StepMethod.__init__(self, [self.f, self.f_eval], tally=tally)

        self.children_no_data = copy.copy(self.children)
        if isinstance(eps_p_f, pm.Variable):
            self.children_no_data.remove(eps_p_f)
            self.eps_p_f = eps_p_f
        else:
            for epf in eps_p_f:
                self.children_no_data.remove(epf)
            self.eps_p_f = pm.Lambda('eps_p_f', lambda e=eps_p_f: np.hstack(e), trace=False)

        self.V = pm.Lambda('%s_vect'%V.__name__, lambda V=V: np.resize(V, len(submod.mesh)))
        self.C_eval = submod.C_eval
        self.M_eval = submod.M_eval
        self.S_eval = submod.S_eval

        M_eval_shape = pm.utils.value(self.M_eval).shape
        C_eval_shape = pm.utils.value(self.C_eval).shape
        self.ti = ti or np.arange(M_eval_shape[0])

        # Work arrays
        self.scratch1 = np.asmatrix(np.empty(C_eval_shape, order='F'))
        self.scratch2 = np.asmatrix(np.empty(C_eval_shape, order='F'))
        self.scratch3 = np.empty(M_eval_shape)

        # Initialize hidden attributes
        self.accepted = 0.
        self.rejected = 0.
        self._state = ['rejected', 'accepted', 'proposal_distribution']
        self._tuning_info = []
        self.proposal_distribution=None

    def get_logp(self):
        return 0.
    logp = property(get_logp)

    def get_loglike(self):
        return pm.utils.logp_of_set(self.children_no_data)
    loglike = property(get_loglike)

    def get_logp_plus_loglike(self):
        return self.get_loglike()
    logp_plus_loglike = property(get_logp_plus_loglike)

    def reject(self):
        self.rejected += 1
        if self.verbose:
            print_(self._id + ' rejecting')
        # Revert the field evaluation and the rest of the field.
        self.f_eval.revert()
        self.f.revert()

    def tune(self, verbose=0):
        return False

    def propose(self):

        if self.verbose:
            print_(self._id + ' proposing')

        fc = pm.gp.fast_matrix_copy

        eps_p_f = pm.utils.value(self.eps_p_f)
        f = pm.utils.value(self.f_eval)
        for i in xrange(len(self.scratch3)):
            self.scratch3[i] = np.sum(eps_p_f[self.ti[i]] - f[i])

        # Compute Cholesky factor of covariance of eps_p_f, C(x,x) + V
        C_eval_value = pm.utils.value(self.C_eval)
        C_eval_shape = C_eval_value.shape

        # Get the Cholesky factor of C_eval, plus the nugget.
        # I don't think you can use S_eval for speed, unfortunately.
        in_chol = fc(C_eval_value, self.scratch1)

        v_val = pm.utils.value(self.V)
        for i in xrange(pm.utils.value(C_eval_shape)[0]):
            in_chol[i,i] += v_val[i] / np.alen(self.ti[i])

        info = pm.gp.linalg_utils.dpotrf_wrap(in_chol)
        if info > 0:
            raise np.linalg.LinAlgError

        # Compute covariance of f conditional on eps_p_f.
        offdiag = fc(C_eval_value, self.scratch2)
        offdiag = pm.gp.trisolve(in_chol, offdiag, uplo='U', transa='T', inplace=True)

        C_step = offdiag.T * offdiag
        C_step *= -1
        C_step += C_eval_value

        # Compute mean of f conditional on eps_p_f.
        for i in xrange(len(self.scratch3)):
            self.scratch3[i] = np.mean(eps_p_f[self.ti[i]])
        m_step = pm.utils.value(self.M_eval) + np.dot(offdiag.T, pm.gp.trisolve(in_chol,(self.scratch3 - self.M_eval.value),uplo='U',transa='T')).view(np.ndarray).ravel()

        sig_step = C_step
        info = pm.gp.linalg_utils.dpotrf_wrap(C_step.T)
        if info > 0:
            warnings.warn('Full conditional covariance was not positive definite.')
            return

        # Update value of f.
        self.f_eval.value = m_step+np.dot(sig_step,np.random.normal(size=sig_step.shape[1])).view(np.ndarray).ravel()
        # Propose the rest of the field from its conditional prior.
        self.f.rand()
<commit_msg>Set __doc__ inside class definition, rather than after.<commit_after># Copyright (c) Anand Patil, 2007

__docformat__ = 'reStructuredText'

import pymc as pm
from . import linalg_utils
import copy
import types
import numpy as np
from .gp_submodel import *
import warnings
from pymc.six import print_

from .Realization import Realization
from .Mean import Mean
from .Covariance import Covariance
from .GPutils import observe, regularize_array

__all__ = ['wrap_metropolis_for_gp_parents', 'GPEvaluationGibbs', 'GPParentAdaptiveMetropolis', 'GPStepMethod', 'GPEvaluationMetropolis', 'MeshlessGPMetropolis']

class GPStepMethod(pm.NoStepper):
    @staticmethod
    def competence(stochastic):
        if isinstance(stochastic, GaussianProcess):
            return 1
        else:
            return 0

def wrap_metropolis_for_gp_parents(metro_class):
    """
    Wraps Metropolis step methods so they can handle extended parents of
    Gaussian processes.
    """
    class wrapper(metro_class):
        __doc__ = """A modified version of class %s that handles parents of Gaussian processes.
Docstring of class %s: \n\n%s"""%(metro_class.__name__,metro_class.__name__,metro_class.__doc__)

        def __init__(self, stochastic, *args, **kwds):

            self.metro_class.__init__(self, stochastic, *args, **kwds)

            mb = set(self.markov_blanket)
            for c in list(self.children):
                if isinstance(c, GaussianProcess):
                    self.children |= c.extended_children
                    mb |= c.extended_children
            self.markov_blanket = list(mb)

            # Remove f from the set that will be used to compute logp_plus_loglike.
            self.markov_blanket_no_f = set(filter(lambda x: not isinstance(x, GaussianProcess), self.markov_blanket))
            self.fs = filter(lambda x: isinstance(x, GaussianProcess), self.markov_blanket)
            self.fr_checks = [f.submodel.fr_check for f in self.fs]

        def get_logp_plus_loglike(self):
            return pm.logp_of_set(self.markov_blanket_no_f)
        logp_plus_loglike = property(get_logp_plus_loglike)

        def propose(self):
            self.metro_class.propose(self)
            try:
                # First make sure none of the stochastics handled by metro_method forbid their current values.
                for s in self.stochastics:
                    s.logp
                # Then make sure the covariances are all still full-rank on the observation locations.
                for frc in self.fr_checks:
                    frc.logp
                for f in self.fs:
                    f.rand()
                self.f_proposed = True
            except pm.ZeroProbability:
                self.f_proposed = False

        def reject(self):
            self.metro_class.reject(self)
            if self.f_proposed:
                for f in self.fs:
                    f.revert()

        @staticmethod
        def competence(stochastic, metro_class=metro_class):
            if any([isinstance(child, GaussianProcess) for child in stochastic.extended_children]):
                return metro_class.competence(stochastic)+.01
            else:
                return 0

    wrapper.__name__ = 'GPParent%s'%metro_class.__name__
    wrapper.metro_class = metro_class

    return wrapper

# Wrap all registered Metropolis step methods to use GP parents.
new_sm_dict = {}
filtered_registry = filter(lambda x: issubclass(x, pm.Metropolis), pm.StepMethodRegistry)
for sm in filtered_registry:
    wrapped_method = wrap_metropolis_for_gp_parents(sm)
    new_sm_dict[wrapped_method.__name__] = wrapped_method
GPParentAdaptiveMetropolis = wrap_metropolis_for_gp_parents(pm.AdaptiveMetropolis)
__all__ += new_sm_dict.keys()
locals().update(new_sm_dict)

class MeshlessGPMetropolis(pm.Metropolis):
    def __init__(self, gp):
        pm.Metropolis.__init__(self, gp, proposal_distribution='Prior', check_before_accepting=False)

    def propose(self):
        self.stochastic.rand()

    @staticmethod
    def competence(stochastic):
        if isinstance(stochastic, GaussianProcess):
            if len(stochastic.submodel.mesh)==0:
                return 3
            else:
                return 0
        else:
            return 0

class _GPEvaluationMetropolis(pm.Metropolis):
    """
    Updates a GP evaluation, the 'f_eval' attribute of a GP submodel.
    The stationary distribution of the assymetric proposal is equal
    to the prior distribution, an attempt to minimize jumps to values
    forbidden by the prior.
    """
    def __init__(self, stochastic, proposal_sd=1, **kwds):
        pm.Metropolis.__init__(self, stochastic, proposal_sd=proposal_sd, **kwds)

    def propose(self):
        sig = pm.utils.value(self.stochastic.parents['sig'])
        mu = pm.utils.value(self.stochastic.parents['mu'])

        delta = pm.rmv_normal_chol(0*mu, sig)

        beta = np.minimum(1, self.proposal_sd * self.adaptive_scale_factor)
        bsig = beta*sig
        sb2 = np.sqrt(1-beta**2)
        self.stochastic.value = (self.stochastic.value - mu)*sb2+beta*delta+mu
        xp,x = self.stochastic.value, self.stochastic.last_value
        self._hastings_factor = pm.mv_normal_chol_like(x,(xp-mu)*sb2+mu,bsig) - pm.mv_normal_chol_like(xp,(x-mu)*sb2+mu,bsig)

        # self.stochastic.value = self.stochastic.value + self.adaptive_scale_factor*self.proposal_sd*delta
        # self._hastings_factor = 0

    def hastings_factor(self):
        return self._hastings_factor

    @staticmethod
    def competence(stochastic):
        if isinstance(stochastic, GPEvaluation):
            return 3
        else:
            return 0

GPEvaluationMetropolis = wrap_metropolis_for_gp_parents(_GPEvaluationMetropolis)

class GPEvaluationGibbs(pm.Metropolis):
    """
    Updates a GP evaluation f_eval. Assumes the only children of f_eval
    are as distributed follows:

    eps_p_f ~ Normal(f_eval[ti], 1./V)

    or

    eps_p_f ~ Normal(f_eval, 1./V)

    if ti is None.
    """
    def __init__(self, submod, V, eps_p_f, ti=None, tally=True, verbose=0):

        self.f_eval = submod.f_eval
        self.f = submod.f
        pm.StepMethod.__init__(self, [self.f, self.f_eval], tally=tally)

        self.children_no_data = copy.copy(self.children)
        if isinstance(eps_p_f, pm.Variable):
            self.children_no_data.remove(eps_p_f)
            self.eps_p_f = eps_p_f
        else:
            for epf in eps_p_f:
                self.children_no_data.remove(epf)
            self.eps_p_f = pm.Lambda('eps_p_f', lambda e=eps_p_f: np.hstack(e), trace=False)

        self.V = pm.Lambda('%s_vect'%V.__name__, lambda V=V: np.resize(V, len(submod.mesh)))
        self.C_eval = submod.C_eval
        self.M_eval = submod.M_eval
        self.S_eval = submod.S_eval

        M_eval_shape = pm.utils.value(self.M_eval).shape
        C_eval_shape = pm.utils.value(self.C_eval).shape
        self.ti = ti or np.arange(M_eval_shape[0])

        # Work arrays
        self.scratch1 = np.asmatrix(np.empty(C_eval_shape, order='F'))
        self.scratch2 = np.asmatrix(np.empty(C_eval_shape, order='F'))
        self.scratch3 = np.empty(M_eval_shape)

        # Initialize hidden attributes
        self.accepted = 0.
        self.rejected = 0.
        self._state = ['rejected', 'accepted', 'proposal_distribution']
        self._tuning_info = []
        self.proposal_distribution=None

    def get_logp(self):
        return 0.
    logp = property(get_logp)

    def get_loglike(self):
        return pm.utils.logp_of_set(self.children_no_data)
    loglike = property(get_loglike)

    def get_logp_plus_loglike(self):
        return self.get_loglike()
    logp_plus_loglike = property(get_logp_plus_loglike)

    def reject(self):
        self.rejected += 1
        if self.verbose:
            print_(self._id + ' rejecting')
        # Revert the field evaluation and the rest of the field.
        self.f_eval.revert()
        self.f.revert()

    def tune(self, verbose=0):
        return False

    def propose(self):

        if self.verbose:
            print_(self._id + ' proposing')

        fc = pm.gp.fast_matrix_copy

        eps_p_f = pm.utils.value(self.eps_p_f)
        f = pm.utils.value(self.f_eval)
        for i in xrange(len(self.scratch3)):
            self.scratch3[i] = np.sum(eps_p_f[self.ti[i]] - f[i])

        # Compute Cholesky factor of covariance of eps_p_f, C(x,x) + V
        C_eval_value = pm.utils.value(self.C_eval)
        C_eval_shape = C_eval_value.shape

        # Get the Cholesky factor of C_eval, plus the nugget.
        # I don't think you can use S_eval for speed, unfortunately.
        in_chol = fc(C_eval_value, self.scratch1)

        v_val = pm.utils.value(self.V)
        for i in xrange(pm.utils.value(C_eval_shape)[0]):
            in_chol[i,i] += v_val[i] / np.alen(self.ti[i])

        info = pm.gp.linalg_utils.dpotrf_wrap(in_chol)
        if info > 0:
            raise np.linalg.LinAlgError

        # Compute covariance of f conditional on eps_p_f.
        offdiag = fc(C_eval_value, self.scratch2)
        offdiag = pm.gp.trisolve(in_chol, offdiag, uplo='U', transa='T', inplace=True)

        C_step = offdiag.T * offdiag
        C_step *= -1
        C_step += C_eval_value

        # Compute mean of f conditional on eps_p_f.
        for i in xrange(len(self.scratch3)):
            self.scratch3[i] = np.mean(eps_p_f[self.ti[i]])
        m_step = pm.utils.value(self.M_eval) + np.dot(offdiag.T, pm.gp.trisolve(in_chol,(self.scratch3 - self.M_eval.value),uplo='U',transa='T')).view(np.ndarray).ravel()

        sig_step = C_step
        info = pm.gp.linalg_utils.dpotrf_wrap(C_step.T)
        if info > 0:
            warnings.warn('Full conditional covariance was not positive definite.')
            return

        # Update value of f.
        self.f_eval.value = m_step+np.dot(sig_step,np.random.normal(size=sig_step.shape[1])).view(np.ndarray).ravel()
        # Propose the rest of the field from its conditional prior.
        self.f.rand()
<|endoftext|>


<commit_before>'''
Created on Mar 12, 2011

@author: johnsalvatier
'''
from scipy.optimize import fmin_bfgs, fmin_ncg
import numpy as np
from numpy import isfinite, nan_to_num
from ..core import *

__all__ = ['find_MAP']
def find_MAP(model, start = None, vars=None, min_alg=fmin_bfgs, disp=False, return_all=False):
    """
    Sets state to the local maximum a posteriori point given a model.
    Current default of fmin_Hessian does not deal well with optimizing close
    to sharp edges, especially if they are the minimum.

    Parameters
    ----------
    model : Model
    start : dict of parameter values (Defaults to model.test_point())
    vars : list or array
        List of variables to set to MAP point (Defaults to all continuous).
    min_alg : function
        Optimization algorithm (Defaults to `fmin_bfgs`).
    disp : bool
        Print convergence message if True (Defaults to False).
    return_all : bool
        Return a list of results at each iteration if True (Defaults
        to False).
    """
    if start is None:
        start = model.test_point

    if vars is None:
        vars = model.cont_vars

    start = clean_point(start)
    bij = DictToArrayBijection(IdxMap(vars), start)

    logp = bij.mapf(model.logpc)
    dlogp = bij.mapf(model.dlogpc(vars))

    def logp_o(point):
        return nan_to_high(-logp(point))

    def grad_logp_o(point):
        return nan_to_num(-dlogp(point))

    results = min_alg(logp_o, bij.map(start), grad_logp_o, disp=disp, retall=return_all, full_output=True)
    mx = results[0]
    log = results[-1]

    if not np.all(isfinite(mx)):
        raise ValueError("Optimization error, found optima has some bad values " + repr(mx))

    if not isfinite(logp(mx)) or not np.all(isfinite(dlogp(mx))):
        raise ValueError("Optimization error, logp or dlogp at max have bad values. max: " + repr(mx) + " logp: " + repr(logp(mx)) + " dlogp: " + repr(dlogp(mx)))

    max_point = bij.rmap(mx)

    if return_all:
        return max_point, log
    else:
        return max_point

def nan_to_high(x):
    return np.where(isfinite(x), x, 1.0e100)
<commit_msg>fixed up find_MAP to work better with various optimizers<commit_after>'''
Created on Mar 12, 2011

@author: johnsalvatier
'''
from scipy import optimize
import numpy as np
from numpy import isfinite, nan_to_num
from ..core import *

__all__ = ['find_MAP', 'scipyminimize']

def find_MAP(model, start = None, vars=None, fmin = optimize.fmin_bfgs, return_raw = False, disp = False, *args, **kwargs):
    """
    Sets state to the local maximum a posteriori point given a model.
    Current default of fmin_Hessian does not deal well with optimizing close
    to sharp edges, especially if they are the minimum.

    Parameters
    ----------
    model : Model
    start : dict of parameter values (Defaults to model.test_point)
    vars : list or array
        List of variables to set to MAP point (Defaults to all continuous).
    fmin : function
        Optimization algorithm (Defaults to `scipy.optimize.fmin_l_bfgs_b`).
    return_raw : Bool
        Whether to return extra value returned by fmin (Defaults to False)
    *args, **kwargs
        Extra args passed to fmin
    """
    if start is None:
        start = model.test_point

    if vars is None:
        vars = model.cont_vars

    start = clean_point(start)
    bij = DictToArrayBijection(IdxMap(vars), start)

    logp = bij.mapf(model.logpc)
    dlogp = bij.mapf(model.dlogpc(vars))

    def logp_o(point):
        return nan_to_high(-logp(point))

    def grad_logp_o(point):
        return nan_to_num(-dlogp(point))

    r = fmin(logp_o, bij.map(start), fprime = grad_logp_o, disp = disp, *args, **kwargs)
    if isinstance(r, tuple):
        mx = r[0]
    else:
        mx = r

    if (not allfinite(mx) or
       not allfinite(logp(mx)) or
       not allfinite(dlogp(mx))) :
            raise ValueError("Optimization error: max, logp or dlogp at max have bad values. max: " + repr(mx) + " logp: " + repr(logp(mx)) + " dlogp: " + repr(dlogp(mx)))

    mx = bij.rmap(mx)
    if return_raw:
        return mx, r
    else:
        return mx

def allfinite(x):
    return np.all(isfinite(x))

def nan_to_high(x):
    return np.where(isfinite(x), x, 1.0e100)

def scipyminimize(f, x0, fprime, *args, **kwargs):
    r = scipy.optimize.minimize(f, x0, jac = fprime, *args, **kwargs)
    return r.x, r

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import unittest
from pynes.compiler import lexical, syntax, semantic

class BitTest(unittest.TestCase):

    def test_bit_zp(self):
        tokens = lexical('BIT $00')
        self.assertEquals(2 , len(tokens))
        self.assertEquals('T_INSTRUCTION', tokens[0]['type'])
        self.assertEquals('T_ADDRESS', tokens[1]['type'])
        ast = syntax(tokens)
        self.assertEquals(1 , len(ast))
        self.assertEquals('S_ZEROPAGE', ast[0]['type'])
        code = semantic(ast)
        self.assertEquals(code, [0x24, 0x00])

    def test_bit_abs(self):
        tokens = lexical('BIT $1234')
        self.assertEquals(2 , len(tokens))
        self.assertEquals('T_INSTRUCTION', tokens[0]['type'])
        self.assertEquals('T_ADDRESS', tokens[1]['type'])
        self.assertEquals('$1234', tokens[1]['value'])
        ast = syntax(tokens)
        self.assertEquals(1 , len(ast))
        self.assertEquals('S_ABSOLUTE', ast[0]['type'])
        code = semantic(ast)
        self.assertEquals(code, [0x2c, 0x34, 0x12])<commit_msg>PEP8 fixes on tests/bit_test.py<commit_after># -*- coding: utf-8 -*-

import unittest
from pynes.compiler import lexical, syntax, semantic

class BitTest(unittest.TestCase):

    def test_bit_zp(self):
        tokens = lexical('BIT $00')
        self.assertEquals(2, len(tokens))
        self.assertEquals('T_INSTRUCTION', tokens[0]['type'])
        self.assertEquals('T_ADDRESS', tokens[1]['type'])
        ast = syntax(tokens)
        self.assertEquals(1, len(ast))
        self.assertEquals('S_ZEROPAGE', ast[0]['type'])
        code = semantic(ast)
        self.assertEquals(code, [0x24, 0x00])

    def test_bit_abs(self):
        tokens = lexical('BIT $1234')
        self.assertEquals(2, len(tokens))
        self.assertEquals('T_INSTRUCTION', tokens[0]['type'])
        self.assertEquals('T_ADDRESS', tokens[1]['type'])
        self.assertEquals('$1234', tokens[1]['value'])
        ast = syntax(tokens)
        self.assertEquals(1, len(ast))
        self.assertEquals('S_ABSOLUTE', ast[0]['type'])
        code = semantic(ast)
        self.assertEquals(code, [0x2c, 0x34, 0x12])
<|endoftext|>


<commit_before>"""\
 cgbf.py Perform basic operations over contracted gaussian basis
  functions. Uses the functions in pgbf.py.

 References:
  OHT = K. O-ohata, H. Taketa, S. Huzinaga. J. Phys. Soc. Jap. 21, 2306 (1966).
  THO = Taketa, Huzinaga, O-ohata, J. Phys. Soc. Jap. 21,2313 (1966).

 This program is part of the PyQuante quantum chemistry program suite
"""

import numpy as np
from pyquante2.utils import fact2

class cgbf(object):
    """
    Class for a contracted Gaussian basis function
    >>> s = cgbf(exps=[1],coefs=[1])
    >>> print(s)
    cgbf((0.0, 0.0, 0.0),(0, 0, 0),[1.0],[1.0000000000000002])
    >>> np.isclose(s(0,0,0),0.712705)
    True
    """
    contracted = True
    def __init__(self, origin=(0,0,0), powers=(0,0,0), exps=[], coefs=[]):
        assert len(origin)==3
        assert len(powers)==3

        self.origin = np.asarray(origin, 'd')
        self.powers = powers
        # TODO: temporary
        self.is_first = (powers[1] + powers[2] == 0)
        self.nfunc = (sum(powers)+1)*(sum(powers)+2)/2

        self.coefs = np.asarray(coefs, 'd')
        self.pexps = np.asarray(exps, 'd')

        self.pgbfs = []
        for expn, coef in zip(exps, coefs):
            self.add_pgbf(expn, coef, False)

        self.pnorms = np.array([self.normalization(expn) for expn in self.pexps], 'd')

    def normalization(self, expn):
        l,m,n = self.powers
        result = pow(2, 2*(l+m+n)+1.5) * pow(expn, l+m+n+1.5) / pow(np.pi, 1.5)
        result /= fact2(2*l-1) * fact2(2*m-1) * fact2(2*n-1)
        return np.sqrt(result)

    def __getitem__(self, item):
        return list(zip(self.coefs, self.pgbfs)).__getitem__(item)

    def __call__(self, *args, **kwargs):
        return sum(c*p(*args,**kwargs) for c,p in self)

    def __repr__(self):
        return "cgbf(%s,%s,%s,%s)" % (tuple(self.origin), self.powers, list(self.pexps), list(self.coefs))

    def mesh(self,xyzs):
        """
        Evaluate basis function on a mesh of points *xyz*.
        """
        return sum(c*p.mesh(xyzs) for c,p in self)

    def cne_list(self):
        return self.coefs,self.pnorms,self.pexps

    def add_pgbf(self, expn, coef, renormalize=True):
        from pyquante2.basis.pgbf import pgbf

        self.pgbfs.append(pgbf(expn, self.origin,self.powers))

        if renormalize:
            self.normalize()

if __name__ == '__main__':
    import doctest
    doctest.testmod()
<commit_msg>remove unused method<commit_after>"""\
 cgbf.py Perform basic operations over contracted gaussian basis
  functions. Uses the functions in pgbf.py.

 References:
  OHT = K. O-ohata, H. Taketa, S. Huzinaga. J. Phys. Soc. Jap. 21, 2306 (1966).
  THO = Taketa, Huzinaga, O-ohata, J. Phys. Soc. Jap. 21,2313 (1966).

 This program is part of the PyQuante quantum chemistry program suite
"""

import numpy as np
from pyquante2.utils import fact2

class cgbf(object):
    """
    Class for a contracted Gaussian basis function
    >>> s = cgbf(exps=[1],coefs=[1])
    >>> print(s)
    cgbf((0.0, 0.0, 0.0),(0, 0, 0),[1.0],[1.0000000000000002])
    >>> np.isclose(s(0,0,0),0.712705)
    True
    """
    contracted = True
    def __init__(self, origin=(0,0,0), powers=(0,0,0), exps=[], coefs=[]):
        assert len(origin)==3
        assert len(powers)==3

        self.origin = np.asarray(origin, 'd')
        self.powers = powers
        # TODO: temporary
        self.is_first = (powers[1] + powers[2] == 0)
        self.nfunc = (sum(powers)+1)*(sum(powers)+2)/2

        self.coefs = np.asarray(coefs, 'd')
        self.pexps = np.asarray(exps, 'd')

        self.pgbfs = []
        for expn, coef in zip(exps, coefs):
            self.add_pgbf(expn, coef, False)

        self.pnorms = np.array([self.normalization(expn) for expn in self.pexps], 'd')

    def normalization(self, expn):
        l,m,n = self.powers
        result = pow(2, 2*(l+m+n)+1.5) * pow(expn, l+m+n+1.5) / pow(np.pi, 1.5)
        result /= fact2(2*l-1) * fact2(2*m-1) * fact2(2*n-1)
        return np.sqrt(result)

    def __getitem__(self, item):
        return list(zip(self.coefs, self.pgbfs)).__getitem__(item)

    def __call__(self, *args, **kwargs):
        return sum(c*p(*args,**kwargs) for c,p in self)

    def __repr__(self):
        return "cgbf(%s,%s,%s,%s)" % (tuple(self.origin), self.powers, list(self.pexps), list(self.coefs))

    def mesh(self,xyzs):
        """
        Evaluate basis function on a mesh of points *xyz*.
        """
        return sum(c*p.mesh(xyzs) for c,p in self)

    def add_pgbf(self, expn, coef, renormalize=True):
        from pyquante2.basis.pgbf import pgbf

        self.pgbfs.append(pgbf(expn, self.origin,self.powers))

        if renormalize:
            self.normalize()

if __name__ == '__main__':
    import doctest
    doctest.testmod()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
# file: $Id$
# auth: Philip J Grabner <grabner@cadit.com>
# date: 2013/09/09
# copy: (C) Copyright 2013 Cadit Health Inc., All Rights Reserved.
#------------------------------------------------------------------------------

import sys, argparse
import pyramid_iniherit.install
from pyramid.paster import bootstrap

from .i18n import _

#------------------------------------------------------------------------------
def describe_from_config(config, output, root=None, format=None, settings=None):
  env = bootstrap(config)
  return describe_from_app(
    env['app'], output, root=root, format=format, settings=settings)

#------------------------------------------------------------------------------
def describe_from_app(app, output, root=None, format=None, settings=None):

  # todo: is this necessary?
  root = root or '/'

  from pyramid.scripts.pviews import PViewsCommand
  pvcomm = PViewsCommand([])
  view = pvcomm._find_view(root, app.registry)
  from .describer import Describer
  desc = Describer(settings=settings)
  res = desc.describe(view, request=None, format=format, root=root)
  output.write(res.encode('UTF-8'))

#------------------------------------------------------------------------------
def main(argv=None):

  cli = argparse.ArgumentParser(
    description = _('Describe a pyramid application URL hierarchy.')
    )

  cli.add_argument(
    _('-v'), _('--verbose'),
    dest='verbose', action='count', default=0,
    help=_('increase verbosity (can be specified multiple times)'))

  cli.add_argument(
    _('-s'), _('--setting'), metavar=_('KEY=VALUE'),
    dest='settings', action='append', default=[],
    help=_('add a Describer setting formatted as "KEY=VALUE", e.g.'
           ' ``--setting format.default=rst``'))

  cli.add_argument(
    _('-f'), _('--format'), metavar=_('FORMAT'),
    dest='format', action='store',
    choices=['txt', 'rst', 'html', 'yaml', 'json', 'wadl', 'xml'],
    help=_('specify the output format to be generated'))

  cli.add_argument(
    _('-T'), _('--txt'),
    dest='format', action='store_const', const='txt',
    help=_('output a text-based tree hierarchy ("--format txt")'))

  cli.add_argument(
    _('-R'), _('--rst'),
    dest='format', action='store_const', const='rst',
    help=_('output a reStructuredText document ("--format rst")'))

  cli.add_argument(
    _('-H'), _('--html'),
    dest='format', action='store_const', const='html',
    help=_('output an HTML document ("--format html")'))

  cli.add_argument(
    _('-Y'), _('--yaml'),
    dest='format', action='store_const', const='yaml',
    help=_('output a YAML document ("--format yaml")'))

  cli.add_argument(
    _('-J'), _('--json'),
    dest='format', action='store_const', const='json',
    help=_('output a JSON document ("--format json")'))

  cli.add_argument(
    _('-W'), _('--wadl'),
    dest='format', action='store_const', const='wadl',
    help=_('output a WADL document ("--format wadl")'))

  cli.add_argument(
    _('-X'), _('--xml'),
    dest='format', action='store_const', const='xml',
    help=_('output an XML document ("--format xml")'))

  cli.add_argument(
    'config', metavar=_('CONFIG'),
    help=_('PasteDeploy configuration file in "FILENAME#APPNAME" format,'
           ' where "#APPNAME" can be omitted and will default to "#main"'))

  cli.add_argument(
    'url', metavar=_('ROOT-URL'),
    nargs='?', default=None,
    help=_('host-relative URL to begin inspection at, i.e. all routes that'
           ' match this URL (and below) will be inspected; if omitted, will'
           ' default to "%(default)s"'))

  options = cli.parse_args(argv)

  try:
    options.settings = dict([val.split('=', 1) for val in options.settings])
  except Exception:
    cli.error('the setting "%r" is invalid - it must be specified'
              ' exactly as "KEY=VALUE"' % (options.settings,))

  describe_from_config(
    options.config, sys.stdout, root=options.url, format=options.format,
    settings=options.settings)

#------------------------------------------------------------------------------
# end of $Id$
#------------------------------------------------------------------------------
<commit_msg>using "request" object created by bootstrap()<commit_after># -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
# file: $Id$
# auth: Philip J Grabner <grabner@cadit.com>
# date: 2013/09/09
# copy: (C) Copyright 2013 Cadit Health Inc., All Rights Reserved.
#------------------------------------------------------------------------------

import sys, argparse
import pyramid_iniherit.install
from pyramid.request import Request
from pyramid.paster import bootstrap

from .i18n import _

#------------------------------------------------------------------------------
def describe_from_config(config, output, root=None, format=None, settings=None):
  env = bootstrap(config)
  return describe_from_app(
    env['app'], output,
    request=env['request'], root=root, format=format, settings=settings)

#------------------------------------------------------------------------------
def describe_from_app(app, output, root=None, format=None, settings=None, request=None):

  # todo: is this necessary?
  root = root or '/'

  from pyramid.scripts.pviews import PViewsCommand
  pvcomm = PViewsCommand([])
  view = pvcomm._find_view(root, app.registry)
  from .describer import Describer
  desc = Describer(settings=settings)
  res = desc.describe(view, request=request, format=format, root=root)
  output.write(res.encode('UTF-8'))

#------------------------------------------------------------------------------
def main(argv=None):

  cli = argparse.ArgumentParser(
    description = _('Describe a pyramid application URL hierarchy.')
    )

  cli.add_argument(
    _('-v'), _('--verbose'),
    dest='verbose', action='count', default=0,
    help=_('increase verbosity (can be specified multiple times)'))

  cli.add_argument(
    _('-s'), _('--setting'), metavar=_('KEY=VALUE'),
    dest='settings', action='append', default=[],
    help=_('add a Describer setting formatted as "KEY=VALUE", e.g.'
           ' ``--setting format.default=rst``'))

  cli.add_argument(
    _('-f'), _('--format'), metavar=_('FORMAT'),
    dest='format', action='store',
    choices=['txt', 'rst', 'html', 'yaml', 'json', 'wadl', 'xml'],
    help=_('specify the output format to be generated'))

  cli.add_argument(
    _('-T'), _('--txt'),
    dest='format', action='store_const', const='txt',
    help=_('output a text-based tree hierarchy ("--format txt")'))

  cli.add_argument(
    _('-R'), _('--rst'),
    dest='format', action='store_const', const='rst',
    help=_('output a reStructuredText document ("--format rst")'))

  cli.add_argument(
    _('-H'), _('--html'),
    dest='format', action='store_const', const='html',
    help=_('output an HTML document ("--format html")'))

  cli.add_argument(
    _('-Y'), _('--yaml'),
    dest='format', action='store_const', const='yaml',
    help=_('output a YAML document ("--format yaml")'))

  cli.add_argument(
    _('-J'), _('--json'),
    dest='format', action='store_const', const='json',
    help=_('output a JSON document ("--format json")'))

  cli.add_argument(
    _('-W'), _('--wadl'),
    dest='format', action='store_const', const='wadl',
    help=_('output a WADL document ("--format wadl")'))

  cli.add_argument(
    _('-X'), _('--xml'),
    dest='format', action='store_const', const='xml',
    help=_('output an XML document ("--format xml")'))

  cli.add_argument(
    'config', metavar=_('CONFIG'),
    help=_('PasteDeploy configuration file in "FILENAME#APPNAME" format,'
           ' where "#APPNAME" can be omitted and will default to "#main"'))

  cli.add_argument(
    'url', metavar=_('ROOT-URL'),
    nargs='?', default=None,
    help=_('host-relative URL to begin inspection at, i.e. all routes that'
           ' match this URL (and below) will be inspected; if omitted, will'
           ' default to "%(default)s"'))

  options = cli.parse_args(argv)

  try:
    options.settings = dict([val.split('=', 1) for val in options.settings])
  except Exception:
    cli.error('the setting "%r" is invalid - it must be specified'
              ' exactly as "KEY=VALUE"' % (options.settings,))

  describe_from_config(
    options.config, sys.stdout, root=options.url, format=options.format,
    settings=options.settings)

#------------------------------------------------------------------------------
# end of $Id$
#------------------------------------------------------------------------------
<|endoftext|>


<commit_before># minNEURON.py

cell = h.SectionList()
soma = h.Section(name='soma') #create soma
soma.push()
#h.topology()

# Geometry
soma.nseg = 1
soma.L = 20
soma.diam = 20

# Biophysics
for sec in h.allsec():
    sec.Ra = 100
    sec.cm = 1
    sec.insert('pas')
    #sec.insert('hh') #    insert hh
    cell.append(sec)

#h('objref rho')
#h('rho = new ChR(0.5)')

#h.rho.Er = Prot.phis[0]
#setattr(h.rho, 'del', Prot.pulses[0][0]) # rho.del will not work because del is reserved word in python
#h.rho.ton = Prot.onDs[0]
#h.rho.toff = Prot.offDs[0]
#h.rho.num = Prot.nPulses
#h.rho.gbar = RhO.g/20000

# Pick a rhodopsin to record from
#rhoRec = h.ChR_apic.o(7)

h.pop_section()

return cell<commit_msg>Add import to stop warnings<commit_after># minNEURON.py
from neuron import h

cell = h.SectionList()
soma = h.Section(name='soma') #create soma
soma.push()
#h.topology()

# Geometry
soma.nseg = 1
soma.L = 20
soma.diam = 20

# Biophysics
for sec in h.allsec():
    sec.Ra = 100
    sec.cm = 1
    sec.insert('pas')
    #sec.insert('hh') #    insert hh
    cell.append(sec)

#h('objref rho')
#h('rho = new ChR(0.5)')

#h.rho.Er = Prot.phis[0]
#setattr(h.rho, 'del', Prot.pulses[0][0]) # rho.del will not work because del is reserved word in python
#h.rho.ton = Prot.onDs[0]
#h.rho.toff = Prot.offDs[0]
#h.rho.num = Prot.nPulses
#h.rho.gbar = RhO.g/20000

# Pick a rhodopsin to record from
#rhoRec = h.ChR_apic.o(7)

h.pop_section()

#return cell<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
pysat.utils - utilities for running pysat
=========================================

pysat.utils contains a number of functions used
throughout the pysat package.  This includes conversion
of formats, loading of files, and user-supplied info
for the pysat data directory structure.
"""

from . import coords, time, registry
from pysat._core import set_data_dir, scale_units, load_netcdf4
<commit_msg>STY: absolute import<commit_after># -*- coding: utf-8 -*-
"""
pysat.utils - utilities for running pysat
=========================================

pysat.utils contains a number of functions used
throughout the pysat package.  This includes conversion
of formats, loading of files, and user-supplied info
for the pysat data directory structure.
"""

from pysat.utils import coords, time, registry
from pysat.utils._core import set_data_dir, scale_units, load_netcdf4
<|endoftext|>