<commit_before>#!/usr/bin/python

# fitDialog.py

import wx
from sans.guitools.PlotPanel import PlotPanel

class LinearFit(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(500, 300))
        """
            for the fit window
        """
        self.parent = parent
        panel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
        vbox  = wx.BoxSizer(wx.VERTICAL)
        sizer = wx.GridBagSizer(5,0)
        vbox.Add(panel, 1, wx.EXPAND | wx.ALL)

        self.tcA = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
        self.tcErrA = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
        self.tcB = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
        self.tcErrB = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
        self.tcChi = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
        self.tcXmin = wx.TextCtrl(panel,-1,style=wx.SIMPLE_BORDER)
        self.tcXmax = wx.TextCtrl(panel,-1,style=wx.SIMPLE_BORDER)
        self.btFit =wx.Button(panel,-1,'Fit' )
        btClose =wx.Button(panel, wx.ID_CANCEL,'Close' )

        ix = 1
        iy = 1
        sizer.Add(wx.StaticText(panel, -1, 'y = Ax +B'),(iy, ix))
        ix = 1
        iy += 2
        sizer.Add(wx.StaticText(panel, -1, 'Param A'),(iy, ix))
        ix += 1
        sizer.Add(self.tcA, (iy, ix))
        ix += 1
        sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix))
        ix += 1
        sizer.Add(self.tcErrA, (iy, ix))
        #self.tcErrA.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter)
        iy += 1
        ix = 1
        sizer.Add(wx.StaticText(panel, -1, 'Param B'),(iy, ix))
        ix += 1
        sizer.Add(self.tcB, (iy, ix))
        #self.tcB.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter)
        ix += 1
        # sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix))
        ix += 1
        sizer.Add(self.tcErrB, (iy, ix))
        self.tcErrB.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter)
        iy += 1
        ix = 1
        sizer.Add(wx.StaticText(panel, -1, 'Chi ^{2}'),(iy, ix))
        ix += 1
        sizer.Add(self.tcChi, (iy, ix))
        #self.tcChi.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter)
        iy += 1
        ix = 1
        sizer.Add(wx.StaticText(panel, -1, 'Xmin'),(iy, ix))
        ix += 2
        sizer.Add(wx.StaticText(panel, -1, 'Xmax'),(iy, ix))
        iy += 1
        ix = 1
        sizer.Add(self.tcXmin, (iy, ix))
        #self.tcXmin.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter)
        ix += 2
        sizer.Add(self.tcXmax, (iy, ix))
        #self.tcXmax.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter)
        iy += 1
        ix = 1
        sizer.Add(self.btFit, (iy, ix))
        self.tcXmax.Bind(wx.EVT_KILL_FOCUS, self._onFit)
        ix += 2
        sizer.Add(btClose, (iy, ix))

        panel.SetSizer(sizer)
        self.SetSizer(vbox)
        self.Centre()
    def _onFit(self ,event):
         param_evt1 = PlotPanel.FunctionFitEvent(\
            Xmin=self._checkVal(self.tcXmin.GetValue()),\
            Xmax=self._checkVal(self.tcXmax.GetValue())                                     )
         wx.PostEvent(self, param_evt1)

    def _onsetValues(self,cstA,cstB,errA,errB,Chi):

         self.tcA.SetValue(cstA)
         self.tcB.SetValue(cstB)
         self.tcErrA.SetValue(cstB)
         self.tcErrB.SetValue(cstA)
         self.tcChi.SetValue(Chi)
    def _getXrange(self):
        if self.tcXmin.GetValue() and self.tcXmax.GetValue():
            return float(),float(self.tcXmax.GetValue())
        else:
            return None, None
    def _checkVal(self,value):
        """
                Ensure that fields parameter contains a value
                before sending to fit in Plotter1D
        """
        try:
            param = float(value)
        except:
            param = None
        return param
if __name__ == "__main__":
    app = wx.App()
    dialog=LinearFit(None, -1, 'Fitting')
    dialog.ShowModal()
    app.MainLoop()

<commit_msg>fixing load file<commit_after>#!/usr/bin/python

# fitDialog.py

import wx
from PlotPanel import PlotPanel
from plottables import Theory1D
import math,pylab,fittings
class LinearFit(wx.Dialog):
    #def __init__(self, parent, id, title):
    def __init__(self, parent, plottable, push_data, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(500, 300))
        """
            for the fit window
        """
        self.parent = parent
        #dialog panel self call function to plot the fitting function
        self.push_data = push_data
        #dialog self plottable
        self.plottable = plottable

        #Dialog interface
        panel = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
        vbox  = wx.BoxSizer(wx.VERTICAL)
        sizer = wx.GridBagSizer(5,0)
        vbox.Add(panel, 1, wx.EXPAND | wx.ALL)

        self.tcA = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
        self.tcErrA = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
        self.tcB = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
        self.tcErrB = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
        self.tcChi = wx.TextCtrl(panel, -1,style=wx.SIMPLE_BORDER)
        self.tcXmin = wx.TextCtrl(panel,-1,style=wx.SIMPLE_BORDER)
        self.tcXmax = wx.TextCtrl(panel,-1,style=wx.SIMPLE_BORDER)
        self.btFit =wx.Button(panel,-1,'Fit' )
        btClose =wx.Button(panel, wx.ID_CANCEL,'Close' )

        ix = 1
        iy = 1
        sizer.Add(wx.StaticText(panel, -1, 'y = Ax +B'),(iy, ix))
        ix = 1
        iy += 2
        sizer.Add(wx.StaticText(panel, -1, 'Param A'),(iy, ix))
        ix += 1
        sizer.Add(self.tcA, (iy, ix))
        ix += 1
        sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix))
        ix += 1
        sizer.Add(self.tcErrA, (iy, ix))
        iy += 1
        ix = 1
        sizer.Add(wx.StaticText(panel, -1, 'Param B'),(iy, ix))
        ix += 1
        sizer.Add(self.tcB, (iy, ix))
        ix += 1
        sizer.Add(wx.StaticText(panel, -1, '+/-'),(iy, ix))
        ix += 1
        sizer.Add(self.tcErrB, (iy, ix))
        iy += 1
        ix = 1
        sizer.Add(wx.StaticText(panel, -1, 'Chi ^{2}'),(iy, ix))
        ix += 1
        sizer.Add(self.tcChi, (iy, ix))
        iy += 1
        ix = 1
        sizer.Add(wx.StaticText(panel, -1, 'Xmin'),(iy, ix))
        ix += 2
        sizer.Add(wx.StaticText(panel, -1, 'Xmax'),(iy, ix))
        iy += 1
        ix = 1
        sizer.Add(self.tcXmin, (iy, ix))
        ix += 2
        sizer.Add(self.tcXmax, (iy, ix))
        iy += 1
        ix = 1
        sizer.Add(self.btFit, (iy, ix))
        self.btFit.Bind(wx.EVT_BUTTON, self._onFit)
        ix += 2
        sizer.Add(btClose, (iy, ix))

        panel.SetSizer(sizer)
        self.SetSizer(vbox)
        self.Centre()
        # Receives the type of model for the fitting
        from LineModel import LineModel
        self.model  = LineModel()
        # new data for the fit
        self.file_data1 = Theory1D(x=[], y=[], dy=None)
        self.file_data1.name = "y= exp(A + bx**2)"

    def _onFit(self ,event):

        print "we are on fit"
        temp =[]
        tempdx =[]
        tempdy =[]
        xmin = self._checkVal(self.tcXmin.GetValue())
        xmax = self._checkVal(self.tcXmax.GetValue())
        x= self.plottable.x
        if x:
            if xmin !=None  and xmax != None:
                for j in range(len(x)):
                    if x[j]>xmin and x[j]<xmax:
                        temp.append(self.model.run(x[j]))
                        tempdx.append(math.sqrt(x[j]))
                        for y_i in temp:
                            tempdy.append(math.sqrt(y_i))
            else:
                # x has a default value in case the user doesn't load data
                for x_i in x:
                    temp.append(self.model.run(x_i))
                    tempdx.append(math.sqrt(x_i))
                for y_i in temp:
                    tempdy.append(math.sqrt(y_i))
                    self.tcXmin.SetValue(str(min(self.plottable.x)))
                    self.tcXmax.SetValue(str(max(self.plottable.x)))
                    xmin = self._checkVal(self.tcXmin.GetValue())
                    xmax = self._checkVal(self.tcXmax.GetValue())

            self.file_data1.x =x
            self.file_data1.y =temp
            self.file_data1.dx=tempdx
            self.file_data1.dy=tempdy

            # Display the fittings values
            default_A = self.model.getParam('A')
            default_B = self.model.getParam('B')
            cstA = fittings.Parameter(self.model, 'A', default_A)
            cstB  = fittings.Parameter(self.model, 'B', default_B)
            chisqr, out, cov = fittings.sansfit(self.model,
                [cstA, cstB], self.plottable.x,
                self.plottable.y, self.plottable.dy,xmin,xmax)
            # Create new data plottable with result

            self.file_data1.y = []
            for x_i in self.file_data1.x:
                self.file_data1.y.append(self.model.run(x_i))

            self.push_data(self.file_data1)

            self._onsetValues(str(out[0]),str(out[1]),\
            str(math.sqrt(cov[0][0])),str(math.sqrt(cov[1][1])),str(chisqr))

    def _onsetValues(self,cstA,cstB,errA,errB,Chi):

         self.tcA.SetValue(cstA)
         self.tcB.SetValue(cstB)
         self.tcErrA.SetValue(cstB)
         self.tcErrB.SetValue(cstA)
         self.tcChi.SetValue(Chi)

    def _returnPlottable(self):
        return self.file_data1

    def _checkVal(self,value):
        """
                Ensure that fields parameter contains a value
                before sending to fit in Plotter1D
        """
        try:
            param = float(value)
        except:
            param = None
        return param
if __name__ == "__main__":
    app = wx.App()
    dialog=LinearFit(None, -1, 'Fitting')
    dialog.ShowModal()
    app.MainLoop()

<|endoftext|>


<commit_before># -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.

import logging
import os
import re
import sys
from urllib import unquote

from gunicorn import SERVER_SOFTWARE
import gunicorn.util as util

try:
    # Python 3.3 has os.sendfile().
    from os import sendfile
except ImportError:
    try:
        from _sendfile import sendfile
    except ImportError:
        sendfile = None

NORMALIZE_SPACE = re.compile(r'(?:\r\n)?[ \t]+')

log = logging.getLogger(__name__)

class FileWrapper:

    def __init__(self, filelike, blksize=8192):
        self.filelike = filelike
        self.blksize = blksize
        if hasattr(filelike, 'close'):
            self.close = filelike.close

    def __getitem__(self, key):
        data = self.filelike.read(self.blksize)
        if data:
            return data
        raise IndexError

def create(req, sock, client, server, cfg):
    resp = Response(req, sock)

    environ = {
        "wsgi.input": req.body,
        "wsgi.errors": sys.stderr,
        "wsgi.version": (1, 0),
        "wsgi.multithread": False,
        "wsgi.multiprocess": (cfg.workers > 1),
        "wsgi.run_once": False,
        "wsgi.file_wrapper": FileWrapper,
        "gunicorn.socket": sock,
        "SERVER_SOFTWARE": SERVER_SOFTWARE,
        "REQUEST_METHOD": req.method,
        "QUERY_STRING": req.query,
        "RAW_URI": req.uri,
        "SERVER_PROTOCOL": "HTTP/%s" % ".".join(map(str, req.version))
    }

    # authors should be aware that REMOTE_HOST and REMOTE_ADDR
    # may not qualify the remote addr:
    # http://www.ietf.org/rfc/rfc3875
    client = client or "127.0.0.1"
    forward = client
    url_scheme = "http"
    script_name = os.environ.get("SCRIPT_NAME", "")

    secure_headers = cfg.secure_scheme_headers
    x_forwarded_for_header = cfg.x_forwarded_for_header

    for hdr_name, hdr_value in req.headers:
        if hdr_name == "EXPECT":
            # handle expect
            if hdr_value.lower() == "100-continue":
                sock.send("HTTP/1.1 100 Continue\r\n\r\n")
        elif hdr_name == x_forwarded_for_header:
            forward = hdr_value
        elif (hdr_name.upper() in secure_headers and
              hdr_value == secure_headers[hdr_name.upper()]):
            url_scheme = "https"
        elif hdr_name == "HOST":
            server = hdr_value
        elif hdr_name == "SCRIPT_NAME":
            script_name = hdr_value
        elif hdr_name == "CONTENT-TYPE":
            environ['CONTENT_TYPE'] = hdr_value
            continue
        elif hdr_name == "CONTENT-LENGTH":
            environ['CONTENT_LENGTH'] = hdr_value
            continue

        key = 'HTTP_' + hdr_name.replace('-', '_')
        if key in environ:
            hdr_value = "%s,%s" % (environ[key], hdr_value)
        environ[key] = hdr_value

    environ['wsgi.url_scheme'] = url_scheme

    if isinstance(forward, basestring):
        # we only took the last one
        # http://en.wikipedia.org/wiki/X-Forwarded-For
        if forward.find(",") >= 0:
            forward = forward.rsplit(",", 1)[1].strip()

        # find host and port on ipv6 address
        if '[' in forward and ']' in forward:
            host =  forward.split(']')[0][1:].lower()
        elif ":" in forward and forward.count(":") == 1:
            host = forward.split(":")[0].lower()
        else:
            host = forward

        forward = forward.split(']')[-1]
        if ":" in forward and forward.count(":") == 1:
            port = forward.split(':', 1)[1]
        else:
            port = 80

        remote = (host, port)
    else:
        remote = forward

    environ['REMOTE_ADDR'] = remote[0]
    environ['REMOTE_PORT'] = str(remote[1])

    if isinstance(server, basestring):
        server =  server.split(":")
        if len(server) == 1:
            if url_scheme == "http":
                server.append("80")
            elif url_scheme == "https":
                server.append("443")
            else:
                server.append('')
    environ['SERVER_NAME'] = server[0]
    environ['SERVER_PORT'] = str(server[1])

    path_info = req.path
    if script_name:
        path_info = path_info.split(script_name, 1)[1]
    environ['PATH_INFO'] = unquote(path_info)
    environ['SCRIPT_NAME'] = script_name

    return resp, environ

class Response(object):

    def __init__(self, req, sock):
        self.req = req
        self.sock = sock
        self.version = SERVER_SOFTWARE
        self.status = None
        self.chunked = False
        self.must_close = False
        self.headers = []
        self.headers_sent = False
        self.response_length = None
        self.sent = 0
        self.upgrade = False

    def force_close(self):
        self.must_close = True

    def should_close(self):
        if self.must_close or self.req.should_close():
            return True
        if self.response_length is not None or self.chunked:
            return False
        return True

    def start_response(self, status, headers, exc_info=None):
        if exc_info:
            try:
                if self.status and self.headers_sent:
                    raise exc_info[0], exc_info[1], exc_info[2]
            finally:
                exc_info = None
        elif self.status is not None:
            raise AssertionError("Response headers already set!")

        self.status = status
        self.process_headers(headers)
        self.chunked = self.is_chunked()
        return self.write

    def process_headers(self, headers):
        for name, value in headers:
            assert isinstance(name, basestring), "%r is not a string" % name
            lname = name.lower().strip()
            if lname == "content-length":
                self.response_length = int(value)
            elif util.is_hoppish(name):
                if lname == "connection":
                    # handle websocket
                    if value.lower().strip() == "upgrade":
                        self.upgrade = True

                # ignore hopbyhop headers
                continue
            self.headers.append((name.strip(), str(value).strip()))

    def is_chunked(self):
        # Only use chunked responses when the client is
        # speaking HTTP/1.1 or newer and there was
        # no Content-Length header set.
        if self.response_length is not None:
            return False
        elif self.req.version <= (1,0):
            return False
        elif self.status.startswith("304") or self.status.startswith("204"):
            # Do not use chunked responses when the response is guaranteed to
            # not have a response body.
            return False
        return True

    def default_headers(self):
        # set the connection header
        if self.upgrade:
            connection = "upgrade"
        elif self.should_close():
            connection = "close"
        else:
            connection = "keep-alive"

        headers = [
            "HTTP/%s.%s %s\r\n" % (self.req.version[0],
                self.req.version[1], self.status),
            "Server: %s\r\n" % self.version,
            "Date: %s\r\n" % util.http_date(),
            "Connection: %s\r\n" % connection
        ]
        if self.chunked:
            headers.append("Transfer-Encoding: chunked\r\n")
        return headers

    def send_headers(self):
        if self.headers_sent:
            return
        tosend = self.default_headers()
        tosend.extend(["%s: %s\r\n" % (n, v) for n, v in self.headers])
        print tosend
        util.write(self.sock, "%s\r\n" % "".join(tosend))
        self.headers_sent = True

    def write(self, arg):
        self.send_headers()
        assert isinstance(arg, basestring), "%r is not a string." % arg

        arglen = len(arg)
        tosend = arglen
        if self.response_length is not None:
            if self.sent >= self.response_length:
                # Never write more than self.response_length bytes
                return

            tosend = min(self.response_length - self.sent, tosend)
            if tosend < arglen:
                arg = arg[:tosend]

        # Sending an empty chunk signals the end of the
        # response and prematurely closes the response
        if self.chunked and tosend == 0:
            return

        self.sent += tosend
        util.write(self.sock, arg, self.chunked)

    def sendfile_all(self, fileno, sockno, offset, nbytes):
        # Send file in at most 1GB blocks as some operating
        # systems can have problems with sending files in blocks
        # over 2GB.

        BLKSIZE = 0x3FFFFFFF

        if nbytes > BLKSIZE:
            for m in range(0, nbytes, BLKSIZE):
                self.sendfile_all(fileno, sockno, offset, min(nbytes, BLKSIZE))
                offset += BLKSIZE
                nbytes -= BLKSIZE
        else:
            sent = 0
            sent += sendfile(sockno, fileno, offset+sent, nbytes-sent)
            while sent != nbytes:
                sent += sendfile(sockno, fileno, offset+sent, nbytes-sent)

    def write_file(self, respiter):
        if sendfile is not None and \
                hasattr(respiter.filelike, 'fileno') and \
                hasattr(respiter.filelike, 'tell'):

            fileno = respiter.filelike.fileno()
            fd_offset = os.lseek(fileno, 0, os.SEEK_CUR)
            fo_offset = respiter.filelike.tell()
            nbytes = max(os.fstat(fileno).st_size - fo_offset, 0)

            if self.response_length:
                nbytes = min(nbytes, self.response_length)

            if nbytes == 0:
                return

            self.send_headers()

            if self.is_chunked():
                self.sock.sendall("%X\r\n" % nbytes)

            self.sendfile_all(fileno, self.sock.fileno(), fo_offset, nbytes)

            if self.is_chunked():
                self.sock.sendall("\r\n")

            os.lseek(fileno, fd_offset, os.SEEK_SET)
        else:
            for item in respiter:
                self.write(item)

    def close(self):
        if not self.headers_sent:
            self.send_headers()
        if self.chunked:
            util.write_chunk(self.sock, "")
<commit_msg>remove spurious print<commit_after># -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.

import logging
import os
import re
import sys
from urllib import unquote

from gunicorn import SERVER_SOFTWARE
import gunicorn.util as util

try:
    # Python 3.3 has os.sendfile().
    from os import sendfile
except ImportError:
    try:
        from _sendfile import sendfile
    except ImportError:
        sendfile = None

NORMALIZE_SPACE = re.compile(r'(?:\r\n)?[ \t]+')

log = logging.getLogger(__name__)

class FileWrapper:

    def __init__(self, filelike, blksize=8192):
        self.filelike = filelike
        self.blksize = blksize
        if hasattr(filelike, 'close'):
            self.close = filelike.close

    def __getitem__(self, key):
        data = self.filelike.read(self.blksize)
        if data:
            return data
        raise IndexError

def create(req, sock, client, server, cfg):
    resp = Response(req, sock)

    environ = {
        "wsgi.input": req.body,
        "wsgi.errors": sys.stderr,
        "wsgi.version": (1, 0),
        "wsgi.multithread": False,
        "wsgi.multiprocess": (cfg.workers > 1),
        "wsgi.run_once": False,
        "wsgi.file_wrapper": FileWrapper,
        "gunicorn.socket": sock,
        "SERVER_SOFTWARE": SERVER_SOFTWARE,
        "REQUEST_METHOD": req.method,
        "QUERY_STRING": req.query,
        "RAW_URI": req.uri,
        "SERVER_PROTOCOL": "HTTP/%s" % ".".join(map(str, req.version))
    }

    # authors should be aware that REMOTE_HOST and REMOTE_ADDR
    # may not qualify the remote addr:
    # http://www.ietf.org/rfc/rfc3875
    client = client or "127.0.0.1"
    forward = client
    url_scheme = "http"
    script_name = os.environ.get("SCRIPT_NAME", "")

    secure_headers = cfg.secure_scheme_headers
    x_forwarded_for_header = cfg.x_forwarded_for_header

    for hdr_name, hdr_value in req.headers:
        if hdr_name == "EXPECT":
            # handle expect
            if hdr_value.lower() == "100-continue":
                sock.send("HTTP/1.1 100 Continue\r\n\r\n")
        elif hdr_name == x_forwarded_for_header:
            forward = hdr_value
        elif (hdr_name.upper() in secure_headers and
              hdr_value == secure_headers[hdr_name.upper()]):
            url_scheme = "https"
        elif hdr_name == "HOST":
            server = hdr_value
        elif hdr_name == "SCRIPT_NAME":
            script_name = hdr_value
        elif hdr_name == "CONTENT-TYPE":
            environ['CONTENT_TYPE'] = hdr_value
            continue
        elif hdr_name == "CONTENT-LENGTH":
            environ['CONTENT_LENGTH'] = hdr_value
            continue

        key = 'HTTP_' + hdr_name.replace('-', '_')
        if key in environ:
            hdr_value = "%s,%s" % (environ[key], hdr_value)
        environ[key] = hdr_value

    environ['wsgi.url_scheme'] = url_scheme

    if isinstance(forward, basestring):
        # we only took the last one
        # http://en.wikipedia.org/wiki/X-Forwarded-For
        if forward.find(",") >= 0:
            forward = forward.rsplit(",", 1)[1].strip()

        # find host and port on ipv6 address
        if '[' in forward and ']' in forward:
            host =  forward.split(']')[0][1:].lower()
        elif ":" in forward and forward.count(":") == 1:
            host = forward.split(":")[0].lower()
        else:
            host = forward

        forward = forward.split(']')[-1]
        if ":" in forward and forward.count(":") == 1:
            port = forward.split(':', 1)[1]
        else:
            port = 80

        remote = (host, port)
    else:
        remote = forward

    environ['REMOTE_ADDR'] = remote[0]
    environ['REMOTE_PORT'] = str(remote[1])

    if isinstance(server, basestring):
        server =  server.split(":")
        if len(server) == 1:
            if url_scheme == "http":
                server.append("80")
            elif url_scheme == "https":
                server.append("443")
            else:
                server.append('')
    environ['SERVER_NAME'] = server[0]
    environ['SERVER_PORT'] = str(server[1])

    path_info = req.path
    if script_name:
        path_info = path_info.split(script_name, 1)[1]
    environ['PATH_INFO'] = unquote(path_info)
    environ['SCRIPT_NAME'] = script_name

    return resp, environ

class Response(object):

    def __init__(self, req, sock):
        self.req = req
        self.sock = sock
        self.version = SERVER_SOFTWARE
        self.status = None
        self.chunked = False
        self.must_close = False
        self.headers = []
        self.headers_sent = False
        self.response_length = None
        self.sent = 0
        self.upgrade = False

    def force_close(self):
        self.must_close = True

    def should_close(self):
        if self.must_close or self.req.should_close():
            return True
        if self.response_length is not None or self.chunked:
            return False
        return True

    def start_response(self, status, headers, exc_info=None):
        if exc_info:
            try:
                if self.status and self.headers_sent:
                    raise exc_info[0], exc_info[1], exc_info[2]
            finally:
                exc_info = None
        elif self.status is not None:
            raise AssertionError("Response headers already set!")

        self.status = status
        self.process_headers(headers)
        self.chunked = self.is_chunked()
        return self.write

    def process_headers(self, headers):
        for name, value in headers:
            assert isinstance(name, basestring), "%r is not a string" % name
            lname = name.lower().strip()
            if lname == "content-length":
                self.response_length = int(value)
            elif util.is_hoppish(name):
                if lname == "connection":
                    # handle websocket
                    if value.lower().strip() == "upgrade":
                        self.upgrade = True

                # ignore hopbyhop headers
                continue
            self.headers.append((name.strip(), str(value).strip()))

    def is_chunked(self):
        # Only use chunked responses when the client is
        # speaking HTTP/1.1 or newer and there was
        # no Content-Length header set.
        if self.response_length is not None:
            return False
        elif self.req.version <= (1,0):
            return False
        elif self.status.startswith("304") or self.status.startswith("204"):
            # Do not use chunked responses when the response is guaranteed to
            # not have a response body.
            return False
        return True

    def default_headers(self):
        # set the connection header
        if self.upgrade:
            connection = "upgrade"
        elif self.should_close():
            connection = "close"
        else:
            connection = "keep-alive"

        headers = [
            "HTTP/%s.%s %s\r\n" % (self.req.version[0],
                self.req.version[1], self.status),
            "Server: %s\r\n" % self.version,
            "Date: %s\r\n" % util.http_date(),
            "Connection: %s\r\n" % connection
        ]
        if self.chunked:
            headers.append("Transfer-Encoding: chunked\r\n")
        return headers

    def send_headers(self):
        if self.headers_sent:
            return
        tosend = self.default_headers()
        tosend.extend(["%s: %s\r\n" % (n, v) for n, v in self.headers])
        util.write(self.sock, "%s\r\n" % "".join(tosend))
        self.headers_sent = True

    def write(self, arg):
        self.send_headers()
        assert isinstance(arg, basestring), "%r is not a string." % arg

        arglen = len(arg)
        tosend = arglen
        if self.response_length is not None:
            if self.sent >= self.response_length:
                # Never write more than self.response_length bytes
                return

            tosend = min(self.response_length - self.sent, tosend)
            if tosend < arglen:
                arg = arg[:tosend]

        # Sending an empty chunk signals the end of the
        # response and prematurely closes the response
        if self.chunked and tosend == 0:
            return

        self.sent += tosend
        util.write(self.sock, arg, self.chunked)

    def sendfile_all(self, fileno, sockno, offset, nbytes):
        # Send file in at most 1GB blocks as some operating
        # systems can have problems with sending files in blocks
        # over 2GB.

        BLKSIZE = 0x3FFFFFFF

        if nbytes > BLKSIZE:
            for m in range(0, nbytes, BLKSIZE):
                self.sendfile_all(fileno, sockno, offset, min(nbytes, BLKSIZE))
                offset += BLKSIZE
                nbytes -= BLKSIZE
        else:
            sent = 0
            sent += sendfile(sockno, fileno, offset+sent, nbytes-sent)
            while sent != nbytes:
                sent += sendfile(sockno, fileno, offset+sent, nbytes-sent)

    def write_file(self, respiter):
        if sendfile is not None and \
                hasattr(respiter.filelike, 'fileno') and \
                hasattr(respiter.filelike, 'tell'):

            fileno = respiter.filelike.fileno()
            fd_offset = os.lseek(fileno, 0, os.SEEK_CUR)
            fo_offset = respiter.filelike.tell()
            nbytes = max(os.fstat(fileno).st_size - fo_offset, 0)

            if self.response_length:
                nbytes = min(nbytes, self.response_length)

            if nbytes == 0:
                return

            self.send_headers()

            if self.is_chunked():
                self.sock.sendall("%X\r\n" % nbytes)

            self.sendfile_all(fileno, self.sock.fileno(), fo_offset, nbytes)

            if self.is_chunked():
                self.sock.sendall("\r\n")

            os.lseek(fileno, fd_offset, os.SEEK_SET)
        else:
            for item in respiter:
                self.write(item)

    def close(self):
        if not self.headers_sent:
            self.send_headers()
        if self.chunked:
            util.write_chunk(self.sock, "")
<|endoftext|>


<commit_before># coding=utf-8
# Copyright 2021 The TensorFlow Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""PASS dataset."""

import tensorflow_datasets.public_api as tfds
import tensorflow.compat.v2 as tf
pd = tfds.core.lazy_imports.pandas

_DESCRIPTION = """
PASS is a large-scale image dataset that does not include any humans,
human parts, or other personally identifiable information.
It that can be used for high-quality self-supervised pretraining while significantly reducing privacy concerns.

PASS contains 1,439,719 images without any labels sourced from YFCC-100M.

All images in this dataset are licenced under the CC-BY licence, as is the dataset itself.
For YFCC-100M see  http://www.multimediacommons.org/.
"""

_CITATION = """
@Article{asano21pass,
author = "Yuki M. Asano and Christian Rupprecht and Andrew Zisserman and Andrea Vedaldi",
title = "PASS: An ImageNet replacement for self-supervised pretraining without humans",
journal = "NeurIPS Track on Datasets and Benchmarks",
year = "2021"
}
"""

_URLS = {
    'train_images': [tfds.download.Resource(  # pylint:disable=g-complex-comprehension
        url='https://zenodo.org/record/5570664/files/PASS.%s.tar' % i_,
        extract_method=tfds.download.ExtractMethod.TAR)
        for i_ in '0123456789'],
    'meta_data':tfds.download.Resource(
        url='https://zenodo.org/record/5570664/files/pass_metadata.csv')
}

class PASS(tfds.core.GeneratorBasedBuilder):
    """DatasetBuilder for pass dataset."""

    VERSION = tfds.core.Version('2.0.0')
    RELEASE_NOTES = {
        '2.0.0': 'v2: Removed 472 images from v1 as they contained humans. Also added metadata: datetaken and GPS. ',
    }

    def _info(self):
        """Returns the dataset metadata."""
        return tfds.core.DatasetInfo(
            builder=self,
            description=_DESCRIPTION,
            features=tfds.features.FeaturesDict({
                'image': tfds.features.Image(shape=(None, None, 3)),      # the image
                'image/creator_uname': tf.string,                         # the photographer/creator
                'image/hash':tf.string,                                   # the hash, as computed from YFCC-100M
                'image/gps_lon':tf.float,                                 # Longitude of image if existent, otw. NaN
                'image/gps_lat':tf.float,                                 # Latitude of image if existent, otw. NaN
                'image/date_taken': pd._libs.tslibs.timestamps.Timestamp, # datetime of image
            }),
            supervised_keys=None,
            homepage='https://www.robots.ox.ac.uk/~vgg/research/pass/',
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager: tfds.download.DownloadManager):
        """Returns SplitGenerators."""
        paths = dl_manager.download(_URLS)
        with tf.io.gfile.GFile(paths['meta_data']) as f:
            meta = pd.read_csv(f)
        meta = meta.set_index('hash')
        return [tfds.core.SplitGenerator(
            name=tfds.Split.TRAIN,
            gen_kwargs=dict(
                parts=paths['train_images'],
                meta=meta,
                dl_manager=dl_manager),
        )]

    def _generate_examples(self, dl_manager, parts, meta):
        """Yields examples."""
        i = 0
        for part in parts:
            for fname, fobj in dl_manager.iter_archive(part):
                i += 1
                img_hash = fname.split('/')[-1].split('.')[0]
                meta = meta.loc[img_hash]
                record = {
                    "image": fobj,
                    "image/creator_uname": meta['unickname'],
                    "image/hash": img_hash,
                    "image/gps_lon": meta['longitude'],
                    "image/gps_lat": meta['latitude'],
                    "image/date_taken": pd.to_datetime(meta['datetime']),
                }
                yield i, record
<commit_msg>moved pandas lazy import to functions<commit_after># coding=utf-8
# Copyright 2021 The TensorFlow Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""PASS dataset."""

import tensorflow_datasets.public_api as tfds
import tensorflow.compat.v2 as tf

_DESCRIPTION = """
PASS is a large-scale image dataset that does not include any humans,
human parts, or other personally identifiable information.
It that can be used for high-quality self-supervised pretraining while significantly reducing privacy concerns.

PASS contains 1,439,719 images without any labels sourced from YFCC-100M.

All images in this dataset are licenced under the CC-BY licence, as is the dataset itself.
For YFCC-100M see  http://www.multimediacommons.org/.
"""

_CITATION = """
@Article{asano21pass,
author = "Yuki M. Asano and Christian Rupprecht and Andrew Zisserman and Andrea Vedaldi",
title = "PASS: An ImageNet replacement for self-supervised pretraining without humans",
journal = "NeurIPS Track on Datasets and Benchmarks",
year = "2021"
}
"""

_URLS = {
    'train_images': [tfds.download.Resource(  # pylint:disable=g-complex-comprehension
        url='https://zenodo.org/record/5570664/files/PASS.%s.tar' % i_,
        extract_method=tfds.download.ExtractMethod.TAR)
        for i_ in '0123456789'],
    'meta_data':tfds.download.Resource(
        url='https://zenodo.org/record/5570664/files/pass_metadata.csv')
}

class PASS(tfds.core.GeneratorBasedBuilder):
    """DatasetBuilder for pass dataset."""

    VERSION = tfds.core.Version('2.0.0')
    RELEASE_NOTES = {
        '2.0.0': 'v2: Removed 472 images from v1 as they contained humans. Also added metadata: datetaken and GPS. ',
    }

    def _info(self):
        """Returns the dataset metadata."""
        return tfds.core.DatasetInfo(
            builder=self,
            description=_DESCRIPTION,
            features=tfds.features.FeaturesDict({
                'image': tfds.features.Image(shape=(None, None, 3)),      # the image
                'image/creator_uname': tf.string,                         # the photographer/creator
                'image/hash':tf.string,                                   # the hash, as computed from YFCC-100M
                'image/gps_lon':tf.float,                                 # Longitude of image if existent, otw. NaN
                'image/gps_lat':tf.float,                                 # Latitude of image if existent, otw. NaN
                'image/date_taken': pd._libs.tslibs.timestamps.Timestamp, # datetime of image
            }),
            supervised_keys=None,
            homepage='https://www.robots.ox.ac.uk/~vgg/research/pass/',
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager: tfds.download.DownloadManager):
        """Returns SplitGenerators."""
        pd = tfds.core.lazy_imports.pandas
        paths = dl_manager.download(_URLS)
        with tf.io.gfile.GFile(paths['meta_data']) as f:
            meta = pd.read_csv(f)
        meta = meta.set_index('hash')
        return [tfds.core.SplitGenerator(
            name=tfds.Split.TRAIN,
            gen_kwargs=dict(
                parts=paths['train_images'],
                meta=meta,
                dl_manager=dl_manager),
        )]

    def _generate_examples(self, dl_manager, parts, meta):
        """Yields examples."""
        pd = tfds.core.lazy_imports.pandas
        i = 0
        for part in parts:
            for fname, fobj in dl_manager.iter_archive(part):
                i += 1
                img_hash = fname.split('/')[-1].split('.')[0]
                meta = meta.loc[img_hash]
                record = {
                    "image": fobj,
                    "image/creator_uname": meta['unickname'],
                    "image/hash": img_hash,
                    "image/gps_lon": meta['longitude'],
                    "image/gps_lat": meta['latitude'],
                    "image/date_taken": pd.to_datetime(meta['datetime']),
                }
                yield i, record
<|endoftext|>


<commit_before>import os,commands
import numpy as np
import pylab as plt

""" put all data into a single folder INPATH. The format of the data name
    should be <name>.mpeg. Create folder OUTPATH and run

"""
# Call ezvision and get saliency maps
vp=1
inpath='saliency/input/vp%03d/'%vp
outpath='saliency/output/vp%03d/'%vp
files = os.listdir(inpath)
files.sort()

for f in files:
    print f
    status,output=commands.getstatusoutput('ezvision '+
        '--in='+inpath+f+' --input-frames=0-MAX@85Hz --rescale-input=1024x1024 '+
        ' --crop-input=128,0,1152,1028 --rescale-output=64x64 '+
        '--out=mraw:'+outpath+f[:-5]+' --output-frames=@85Hz ' +
        '--save-channel-outputs --vc-chans=IM --sm-type=None '+
        '--nodisplay-foa --nodisplay-patch --nodisplay-traj '+
        '--nodisplay-additive --wta-type=None --nouse-random '+
        '--direction-sqrt --num-directions=8 --vc-type=Thread:Std ' +
        '-j 2 --nouse-fpe --logverb=Error')
    commands.getstatusoutput('rm '+outpath+f[:-5]+'SOdir_*')
    if status!=0:
        print output

<commit_msg>forgot to save changes...<commit_after>import os,commands
import numpy as np
import pylab as plt

""" put all data into a single folder INPATH. The format of the data name
    should be <name>.mpeg. Create folder OUTPATH and run

"""
# Call ezvision and get saliency maps
vp=1
inpath=os.path.sep.join(['trajectoryData','gao09','saliency','input',''])
outpath=os.path.sep.join(['trajectoryData','gao09','saliency','output',''])
files = os.listdir(inpath)
files.sort()

for f in files:
    print f
    status,output=commands.getstatusoutput('ezvision '+
        '--in='+inpath+f+' --input-frames=0-MAX@60Hz --rescale-input=1024x1024 '+
        ' --crop-input=128,0,1152,1028 --rescale-output=64x64 '+
        '--out=mraw:'+outpath+f[:-5]+' --output-frames=@60Hz ' +
        '--save-channel-outputs --vc-chans=IM --sm-type=None '+
        '--nodisplay-foa --nodisplay-patch --nodisplay-traj '+
        '--nodisplay-additive --wta-type=None --nouse-random '+
        '--direction-sqrt --num-directions=8 --vc-type=Thread:Std ' +
        '-j 2 --nouse-fpe --logverb=Error')
    commands.getstatusoutput('rm '+outpath+f[:-5]+'SOdir_*')
    if status!=0:
        print output

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Django settings for basic pinax project.

import os.path
import datetime
import time

from django.utils.http import http_date

import djcelery

djcelery.setup_loader()

PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))

DEBUG = False
TEMPLATE_DEBUG = True

SERVE_MEDIA = DEBUG
SERVE_LESS = DEBUG

# django-compressor is turned off by default due to deployment overhead for
# most users. See <URL> for more information
COMPRESS = False

INTERNAL_IPS = [
    "127.0.0.1",
]

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "crate",
    }
}

TIME_ZONE = "UTC"
LANGUAGE_CODE = "en-us"

USE_I18N = False
USE_L10N = True
USE_TZ = True

MEDIA_ROOT = os.path.join(PROJECT_ROOT, "site_media", "media")
MEDIA_URL = "/site_media/media/"

STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")
STATIC_URL = "/site_media/static/"

STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, "static"),
]

STATICFILES_FINDERS = [
    "staticfiles.finders.FileSystemFinder",
    "staticfiles.finders.AppDirectoriesFinder",
    "staticfiles.finders.LegacyAppDirectoriesFinder",
    "compressor.finders.CompressorFinder",
]

COMPRESS_OUTPUT_DIR = "cache"

SECRET_KEY = ")ys)^6l18seiq)veen^41ox%l@6#o-2t*9-8n5$7$f+qi*x03$"

TEMPLATE_LOADERS = [
    "django.template.loaders.filesystem.Loader",
    "django.template.loaders.app_directories.Loader",
]

MIDDLEWARE_CLASSES = [
    "django_hosts.middleware.HostsMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django_openid.consumer.SessionConsumer",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",

    "pagination.middleware.PaginationMiddleware",

    "pinax.apps.account.middleware.LocaleMiddleware",
    "pinax.middleware.security.HideSensistiveFieldsMiddleware",
]

ROOT_URLCONF = "crate_project.urls"
ROOT_HOSTCONF = "crate_project.hosts"

DEFAULT_HOST = "default"

WSGI_APPLICATION = "crate_project.wsgi.application"

TEMPLATE_DIRS = [
    os.path.join(PROJECT_ROOT, "templates"),
]

TEMPLATE_CONTEXT_PROCESSORS = [
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
    "django.contrib.messages.context_processors.messages",

    "staticfiles.context_processors.static",

    "pinax.core.context_processors.pinax_settings",

    "pinax.apps.account.context_processors.account",

    # "notification.context_processors.notification",
    "announcements.context_processors.site_wide_announcements",
]

INSTALLED_APPS = [
    # Admin Dashboard
    "admin_tools",
    "admin_tools.theming",
    "admin_tools.menu",
    "admin_tools.dashboard",

    # Django
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.messages",
    "django.contrib.humanize",
    "django.contrib.markup",

    "pinax.templatetags",

    # theme
    "pinax_theme_bootstrap",

    # external (Pinax)
    # "notification",  # must be first
    "staticfiles",
    "pagination",
    "compressor",
    "django_openid",
    "timezones",
    "emailconfirmation",
    "announcements",
    "idios",
    "metron",

    # Pinax
    "pinax.apps.account",
    "pinax.apps.signup_codes",

    # external (Project)
    "south",
    "djcelery",
    "django_hosts",
    "haystack",
    "storages",
    "intercom",
    "celery_haystack",
    "tastypie",

    # project
    "about",
    "profiles",
    "packages",
    "pypi",
    "search",
    "crate",
    "evaluator",
    "privatebeta",
    "helpdocs",
]

FIXTURE_DIRS = [
    os.path.join(PROJECT_ROOT, "fixtures"),
]

MESSAGE_STORAGE = "django.contrib.messages.storage.session.SessionStorage"

ABSOLUTE_URL_OVERRIDES = {
    "auth.user": lambda o: "/profiles/profile/%s/" % o.username,
}

AUTH_PROFILE_MODULE = "profiles.Profile"
NOTIFICATION_LANGUAGE_MODULE = "account.Account"

ACCOUNT_OPEN_SIGNUP = True
ACCOUNT_USE_OPENID = False
ACCOUNT_REQUIRED_EMAIL = True
ACCOUNT_EMAIL_VERIFICATION = True
ACCOUNT_EMAIL_AUTHENTICATION = False
ACCOUNT_UNIQUE_EMAIL = EMAIL_CONFIRMATION_UNIQUE_EMAIL = True

AUTHENTICATION_BACKENDS = [
    "pinax.apps.account.auth_backends.AuthenticationBackend",
]

LOGIN_URL = "/account/login/"
LOGIN_REDIRECT_URLNAME = "what_next"
LOGOUT_REDIRECT_URLNAME = "search"

EMAIL_CONFIRMATION_DAYS = 2
EMAIL_DEBUG = DEBUG

DEBUG_TOOLBAR_CONFIG = {
    "INTERCEPT_REDIRECTS": False,
}

CELERY_SEND_TASK_ERROR_EMAILS = True
CELERY_DISABLE_RATE_LIMITS = True
CELERY_TASK_PUBLISH_RETRY = True

CELERY_TASK_RESULT_EXPIRES = 7 * 24 * 60 * 60  # 7 Days

CELERYD_HIJACK_ROOT_LOGGER = False

CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler"

HAYSTACK_SEARCH_RESULTS_PER_PAGE = 15

AWS_QUERYSTRING_AUTH = False
AWS_S3_SECURE_URLS = False

AWS_HEADERS = {
    "Expires": lambda: http_date(time.mktime((datetime.datetime.now() + datetime.timedelta(days=365)).timetuple())),
    "Cache-Control": "max-age=31556926",
}

METRON_SETTINGS = {
    "mixpanel": {3: "a280fbfb3039709d8a5ac15834a16e5e"},
    "google": {3: "UA-28759418-1"},
    "gauges": {3: "4f1e4cd0613f5d7003000002"}
}
<commit_msg>add back in the deprecated ADMIN_MEDIA_PREFIX for django-admin-tools<commit_after># -*- coding: utf-8 -*-
# Django settings for basic pinax project.

import os.path
import datetime
import posixpath
import time

from django.utils.http import http_date

import djcelery

djcelery.setup_loader()

PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))

DEBUG = False
TEMPLATE_DEBUG = True

SERVE_MEDIA = DEBUG
SERVE_LESS = DEBUG

# django-compressor is turned off by default due to deployment overhead for
# most users. See <URL> for more information
COMPRESS = False

INTERNAL_IPS = [
    "127.0.0.1",
]

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "crate",
    }
}

TIME_ZONE = "UTC"
LANGUAGE_CODE = "en-us"

USE_I18N = False
USE_L10N = True
USE_TZ = True

MEDIA_ROOT = os.path.join(PROJECT_ROOT, "site_media", "media")
MEDIA_URL = "/site_media/media/"

STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")
STATIC_URL = "/site_media/static/"

ADMIN_MEDIA_PREFIX = posixpath.join(STATIC_URL, "admin/")

STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, "static"),
]

STATICFILES_FINDERS = [
    "staticfiles.finders.FileSystemFinder",
    "staticfiles.finders.AppDirectoriesFinder",
    "staticfiles.finders.LegacyAppDirectoriesFinder",
    "compressor.finders.CompressorFinder",
]

COMPRESS_OUTPUT_DIR = "cache"

SECRET_KEY = ")ys)^6l18seiq)veen^41ox%l@6#o-2t*9-8n5$7$f+qi*x03$"

TEMPLATE_LOADERS = [
    "django.template.loaders.filesystem.Loader",
    "django.template.loaders.app_directories.Loader",
]

MIDDLEWARE_CLASSES = [
    "django_hosts.middleware.HostsMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django_openid.consumer.SessionConsumer",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",

    "pagination.middleware.PaginationMiddleware",

    "pinax.apps.account.middleware.LocaleMiddleware",
    "pinax.middleware.security.HideSensistiveFieldsMiddleware",
]

ROOT_URLCONF = "crate_project.urls"
ROOT_HOSTCONF = "crate_project.hosts"

DEFAULT_HOST = "default"

WSGI_APPLICATION = "crate_project.wsgi.application"

TEMPLATE_DIRS = [
    os.path.join(PROJECT_ROOT, "templates"),
]

TEMPLATE_CONTEXT_PROCESSORS = [
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
    "django.contrib.messages.context_processors.messages",

    "staticfiles.context_processors.static",

    "pinax.core.context_processors.pinax_settings",

    "pinax.apps.account.context_processors.account",

    # "notification.context_processors.notification",
    "announcements.context_processors.site_wide_announcements",
]

INSTALLED_APPS = [
    # Admin Dashboard
    "admin_tools",
    "admin_tools.theming",
    "admin_tools.menu",
    "admin_tools.dashboard",

    # Django
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.messages",
    "django.contrib.humanize",
    "django.contrib.markup",

    "pinax.templatetags",

    # theme
    "pinax_theme_bootstrap",

    # external (Pinax)
    # "notification",  # must be first
    "staticfiles",
    "pagination",
    "compressor",
    "django_openid",
    "timezones",
    "emailconfirmation",
    "announcements",
    "idios",
    "metron",

    # Pinax
    "pinax.apps.account",
    "pinax.apps.signup_codes",

    # external (Project)
    "south",
    "djcelery",
    "django_hosts",
    "haystack",
    "storages",
    "intercom",
    "celery_haystack",
    "tastypie",

    # project
    "about",
    "profiles",
    "packages",
    "pypi",
    "search",
    "crate",
    "evaluator",
    "privatebeta",
    "helpdocs",
]

FIXTURE_DIRS = [
    os.path.join(PROJECT_ROOT, "fixtures"),
]

MESSAGE_STORAGE = "django.contrib.messages.storage.session.SessionStorage"

ABSOLUTE_URL_OVERRIDES = {
    "auth.user": lambda o: "/profiles/profile/%s/" % o.username,
}

AUTH_PROFILE_MODULE = "profiles.Profile"
NOTIFICATION_LANGUAGE_MODULE = "account.Account"

ACCOUNT_OPEN_SIGNUP = True
ACCOUNT_USE_OPENID = False
ACCOUNT_REQUIRED_EMAIL = True
ACCOUNT_EMAIL_VERIFICATION = True
ACCOUNT_EMAIL_AUTHENTICATION = False
ACCOUNT_UNIQUE_EMAIL = EMAIL_CONFIRMATION_UNIQUE_EMAIL = True

AUTHENTICATION_BACKENDS = [
    "pinax.apps.account.auth_backends.AuthenticationBackend",
]

LOGIN_URL = "/account/login/"
LOGIN_REDIRECT_URLNAME = "what_next"
LOGOUT_REDIRECT_URLNAME = "search"

EMAIL_CONFIRMATION_DAYS = 2
EMAIL_DEBUG = DEBUG

DEBUG_TOOLBAR_CONFIG = {
    "INTERCEPT_REDIRECTS": False,
}

CELERY_SEND_TASK_ERROR_EMAILS = True
CELERY_DISABLE_RATE_LIMITS = True
CELERY_TASK_PUBLISH_RETRY = True

CELERY_TASK_RESULT_EXPIRES = 7 * 24 * 60 * 60  # 7 Days

CELERYD_HIJACK_ROOT_LOGGER = False

CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler"

HAYSTACK_SEARCH_RESULTS_PER_PAGE = 15

AWS_QUERYSTRING_AUTH = False
AWS_S3_SECURE_URLS = False

AWS_HEADERS = {
    "Expires": lambda: http_date(time.mktime((datetime.datetime.now() + datetime.timedelta(days=365)).timetuple())),
    "Cache-Control": "max-age=31556926",
}

METRON_SETTINGS = {
    "mixpanel": {3: "a280fbfb3039709d8a5ac15834a16e5e"},
    "google": {3: "UA-28759418-1"},
    "gauges": {3: "4f1e4cd0613f5d7003000002"}
}
<|endoftext|>


<commit_before># TODO
# StanfordNERTagger to tag the words

from nltk.tag import StanfordNERTagger
from nltk.tokenize import word_tokenize
from nltk import pos_tag
from nltk.chunk import conlltags2tree
from nltk.tree import Tree
from newspaper import Article

from bs4 import BeautifulSoup
import urllib2
import unicodedata
from datetime import datetime
import time

stanford_classifier = 'D:\StanfordParser\stanford-ner-2015-12-09\classifiers\english.all.3class.distsim.crf.ser.gz'
stanford_classifier2 = 'D:\StanfordParser\stanford-ner-2015-12-09\classifiers\english.muc.7class.distsim.crf.ser.gz'
stanford_ner_path = 'D:\StanfordParser\stanford-ner-2015-12-09\stanford-ner.jar'

st = StanfordNERTagger(stanford_classifier2, stanford_ner_path, encoding='utf-8')
st2 = StanfordNERTagger(stanford_classifier, stanford_ner_path, encoding='utf-8')

def stanfordNE2BIO(tagged_sent):
	bio_tagged_sent = []
	prev_tag = "O"
	for token, tag in tagged_sent:
		if tag == "O": #O
			bio_tagged_sent.append((token, tag))
			prev_tag = tag
			continue
		if tag != "O" and prev_tag == "O": # Begin NE
			bio_tagged_sent.append((token, "B-"+tag))
			prev_tag = tag
		elif prev_tag != "O" and prev_tag == tag: # Inside NE
			bio_tagged_sent.append((token, "I-"+tag))
			prev_tag = tag
		elif prev_tag != "O" and prev_tag != tag: # Adjacent NE
			bio_tagged_sent.append((token, "B-"+tag))
			prev_tag = tag

	return bio_tagged_sent

def stanfordNE2tree(ne_tagged_sent):
	bio_tagged_sent = stanfordNE2BIO(ne_tagged_sent)
	sent_tokens, sent_ne_tags = zip(*bio_tagged_sent)
	sent_pos_tags = [pos for token, pos in pos_tag(sent_tokens)]

	sent_conlltags = [(token, pos, ne) for token, pos, ne in zip(sent_tokens, sent_pos_tags, sent_ne_tags)]
	ne_tree = conlltags2tree(sent_conlltags)
	return ne_tree

def create_ner_entities_tuple(text):
	ne_tagged_sent = st.tag(text.split())
	ne_tree = stanfordNE2tree(ne_tagged_sent)
	ne_in_sent = []
	for subtree in ne_tree:
		if type(subtree) == Tree: # If subtree is a noun chunk, i.e. NE != "O"
			ne_label = subtree.label()
			ne_string = " ".join([token for token, pos in subtree.leaves()])
			ne_in_sent.append((ne_string, ne_label))
	return ne_in_sent

def create_ner_entities_tuple_2(text):
    ne_tagged_sent = st2.tag(text.split())
    ne_tree = stanfordNE2tree(ne_tagged_sent)
    ne_in_sent = []
    for subtree in ne_tree:
    	if type(subtree) == Tree: # If subtree is a noun chunk, i.e. NE != "O"
    		ne_label = subtree.label()
    		ne_string = " ".join([token for token, pos in subtree.leaves()])
    		ne_in_sent.append((ne_string, ne_label))
    return ne_in_sent

# Header
_header = { 'User-agent': 'Mozilla/5.0' }

# Converts 24h format into 12h format
def get_12h_time(t):
    time = datetime.strptime(t, "%H:%M")
    return time.strftime("%I:%M %p")

# Returns current time
def get_current_time():
    return time.strftime("%d-%m-%Y %H:%M", time.gmtime())

# Returns Formatted news update date
def get_update_time(unformatted_time):
    return unformatted_time.replace(unformatted_time[:5], get_12h_time(unformatted_time[:5]))

# Returns news category
def get_news_category(news_url):
    url = news_url[26:]
    cat = ''
    for letter in url:
        if letter == '/':
            return cat.capitalize()
        if letter == '-':
            return "Science & Tech"
        cat += letter
    return cat.capitalize()

def get_latest_news():
    # baseurl
    news_paper_url = "http://en.prothom-alo.com/archive"
    baseurl = 'http://en.prothom-alo.com/'

    #Adding header to avoid banning
    req = urllib2.Request(news_paper_url, headers=_header)

    # request
    prothom_alo_request = urllib2.urlopen(req)

    # Creating soup
    prothomalo_soup = BeautifulSoup(prothom_alo_request, 'lxml')

    # Getting all news links
    news_links = [baseurl + link['href'] for link in prothomalo_soup.find('div', {'class' : 'list list_square mb20'}).findAll('a')]

    # selecting first news
    first_news = news_links[0]

    #Adding a header to avoid banning
    req2 = urllib2.Request(first_news, headers=_header)

    # Request of the first news
    news_request = urllib2.urlopen(req2)

    # soup
    first_news_soup = BeautifulSoup(news_request)

    # getting news items
    news_title = first_news_soup.title.get_text()
    news_text = unicodedata.normalize('NFKD', first_news_soup.find(itemprop="articleBody").get_text()).encode('ascii', 'ignore')
    news_update = get_update_time(first_news_soup.find(itemprop="datePublished").get_text())
    news_reporter = first_news_soup.find('span', {'class' : 'author'}).get_text()
    news_scrape_time = get_current_time()
    news_category = get_news_category(first_news)

    print "News title: " +  first_news_soup.title.get_text()
    print "Last Update: " + news_update
    print "Reporter: "  + news_reporter
    print "Category: " + news_category
    print "\n\n"
    print "Content: \n-----------------\n"
    print news_text
    print "--------------------------\n\n"
    print "Scrape time: " + news_scrape_time

    return news_text

news_text = "Nearly 7,000 imported Infiniti and Nissan Fuga automobiles will be recalled from China, the countrys quality watchdog said on Tuesday. According to the General Administration of Quality Supervision, Inspection and Quarantine, the 6,961 recalled autos were installed with problematic gas generators, Xinhua news agency reported. Global and Chinese automakers recalled a record 8.8 million 100% defective vehicles in the first half of 2016, more than double the number for the same period last year. Japanese automakers recalled the largest number of vehicles at nearly 5.7 million units in 40 batches, followed by the US and German automakers. My name is Manash and it is 100% accurate."

news_text = "While in France, Christine Lagarde discussed 100% short-term stimulus efforts in a recent interview with the Wall Street Journal."

# Sums up discrete tags
def entity_group(text):
    # Getting rid of tokens with Object Tag
    tokenized_words = [token for token in st.tag(word_tokenize(text)) if token[1] != 'O']

    comparator = tokenized_words[0]
    list_index = 0
    append_index = 0

    # Store the element from the beginning of the list
    comparator = tokenized_words[0]

    # Insert first element to the tag_touple list
    tag_touple_list = [(comparator[0], comparator[1])]

    # Increase the list index since we've added an element at the beginning
    list_index += 1

    # Continue iteration until the index has reached to the end of the list
    while (list_index < len(tokenized_words)):

        if (comparator[1] == tokenized_words[list_index][1]):

            # If it's a Percentage symbol skip the space
            space_or_not = '' if tokenized_words[list_index][0] == '%' else ' '

            # Get the previous element then append it with the new one
            to_be_appended = comparator[0] + space_or_not + tokenized_words[list_index][0]

            # Set the modified value to the touple list
            tag_touple_list[append_index] = (to_be_appended, comparator[1])

            # Increase list index
            list_index += 1

        else:
            # Replace the comparator with new one
            comparator = tokenized_words[list_index]

            # Place the comparator to the list
            tag_touple_list.append((comparator[0], comparator[1]))

            # Increase index
            list_index += 1

            # Since it's a new entity we need to increase the location of the insertion
            append_index += 1

    print tag_touple_list

if __name__ == '__main__':
    news_ner_tags = {}
    ner_person = []
    ner_location = []
    ner_organization = []
    ner_date = []
    ner_money = []
    ner_percent = []
    ner_time = []

    entity_group(news_text)
<commit_msg>fixed tagger function<commit_after># TODO
# StanfordNERTagger to tag the words

from nltk.tag import StanfordNERTagger
from nltk.tokenize import word_tokenize
from nltk import pos_tag
from nltk.chunk import conlltags2tree
from nltk.tree import Tree
from newspaper import Article

from bs4 import BeautifulSoup
import urllib2
import unicodedata
from datetime import datetime
import time

stanford_classifier = 'D:\StanfordParser\stanford-ner-2015-12-09\classifiers\english.all.3class.distsim.crf.ser.gz'
stanford_classifier2 = 'D:\StanfordParser\stanford-ner-2015-12-09\classifiers\english.muc.7class.distsim.crf.ser.gz'
stanford_ner_path = 'D:\StanfordParser\stanford-ner-2015-12-09\stanford-ner.jar'

st = StanfordNERTagger(stanford_classifier2, stanford_ner_path, encoding='utf-8')
st2 = StanfordNERTagger(stanford_classifier, stanford_ner_path, encoding='utf-8')

def stanfordNE2BIO(tagged_sent):
	bio_tagged_sent = []
	prev_tag = "O"
	for token, tag in tagged_sent:
		if tag == "O": #O
			bio_tagged_sent.append((token, tag))
			prev_tag = tag
			continue
		if tag != "O" and prev_tag == "O": # Begin NE
			bio_tagged_sent.append((token, "B-"+tag))
			prev_tag = tag
		elif prev_tag != "O" and prev_tag == tag: # Inside NE
			bio_tagged_sent.append((token, "I-"+tag))
			prev_tag = tag
		elif prev_tag != "O" and prev_tag != tag: # Adjacent NE
			bio_tagged_sent.append((token, "B-"+tag))
			prev_tag = tag

	return bio_tagged_sent

def stanfordNE2tree(ne_tagged_sent):
	bio_tagged_sent = stanfordNE2BIO(ne_tagged_sent)
	sent_tokens, sent_ne_tags = zip(*bio_tagged_sent)
	sent_pos_tags = [pos for token, pos in pos_tag(sent_tokens)]

	sent_conlltags = [(token, pos, ne) for token, pos, ne in zip(sent_tokens, sent_pos_tags, sent_ne_tags)]
	ne_tree = conlltags2tree(sent_conlltags)
	return ne_tree

def create_ner_entities_tuple(text):
	ne_tagged_sent = st.tag(text.split())
	ne_tree = stanfordNE2tree(ne_tagged_sent)
	ne_in_sent = []
	for subtree in ne_tree:
		if type(subtree) == Tree: # If subtree is a noun chunk, i.e. NE != "O"
			ne_label = subtree.label()
			ne_string = " ".join([token for token, pos in subtree.leaves()])
			ne_in_sent.append((ne_string, ne_label))
	return ne_in_sent

def create_ner_entities_tuple_2(text):
    ne_tagged_sent = st2.tag(text.split())
    ne_tree = stanfordNE2tree(ne_tagged_sent)
    ne_in_sent = []
    for subtree in ne_tree:
    	if type(subtree) == Tree: # If subtree is a noun chunk, i.e. NE != "O"
    		ne_label = subtree.label()
    		ne_string = " ".join([token for token, pos in subtree.leaves()])
    		ne_in_sent.append((ne_string, ne_label))
    return ne_in_sent

# Header
_header = { 'User-agent': 'Mozilla/5.0' }

# Converts 24h format into 12h format
def get_12h_time(t):
    time = datetime.strptime(t, "%H:%M")
    return time.strftime("%I:%M %p")

# Returns current time
def get_current_time():
    return time.strftime("%d-%m-%Y %H:%M", time.gmtime())

# Returns Formatted news update date
def get_update_time(unformatted_time):
    return unformatted_time.replace(unformatted_time[:5], get_12h_time(unformatted_time[:5]))

# Returns news category
def get_news_category(news_url):
    url = news_url[26:]
    cat = ''
    for letter in url:
        if letter == '/':
            return cat.capitalize()
        if letter == '-':
            return "Science & Tech"
        cat += letter
    return cat.capitalize()

def get_latest_news():
    # baseurl
    news_paper_url = "http://en.prothom-alo.com/archive"
    baseurl = 'http://en.prothom-alo.com/'

    #Adding header to avoid banning
    req = urllib2.Request(news_paper_url, headers=_header)

    # request
    prothom_alo_request = urllib2.urlopen(req)

    # Creating soup
    prothomalo_soup = BeautifulSoup(prothom_alo_request, 'lxml')

    # Getting all news links
    news_links = [baseurl + link['href'] for link in prothomalo_soup.find('div', {'class' : 'list list_square mb20'}).findAll('a')]

    # selecting first news
    first_news = news_links[0]

    #Adding a header to avoid banning
    req2 = urllib2.Request(first_news, headers=_header)

    # Request of the first news
    news_request = urllib2.urlopen(req2)

    # soup
    first_news_soup = BeautifulSoup(news_request)

    # getting news items
    news_title = first_news_soup.title.get_text()
    news_text = unicodedata.normalize('NFKD', first_news_soup.find(itemprop="articleBody").get_text()).encode('ascii', 'ignore')
    news_update = get_update_time(first_news_soup.find(itemprop="datePublished").get_text())
    news_reporter = first_news_soup.find('span', {'class' : 'author'}).get_text()
    news_scrape_time = get_current_time()
    news_category = get_news_category(first_news)

    print "News title: " +  first_news_soup.title.get_text()
    print "Last Update: " + news_update
    print "Reporter: "  + news_reporter
    print "Category: " + news_category
    print "\n\n"
    print "Content: \n-----------------\n"
    print news_text
    print "--------------------------\n\n"
    print "Scrape time: " + news_scrape_time

    return news_text

news_text = "Nearly 7,000 imported Infiniti and Nissan Fuga automobiles will be recalled from China, the countrys quality watchdog said on Tuesday. According to the General Administration of Quality Supervision, Inspection and Quarantine, the 6,961 recalled autos were installed with problematic gas generators, Xinhua news agency reported. Global and Chinese automakers recalled a record 8.8 million 100% defective vehicles in the first half of 2016, more than double the number for the same period last year. Japanese automakers recalled the largest number of vehicles at nearly 5.7 million units in 40 batches, followed by the US and German automakers. My name is Manash and it is 100% accurate."

news_text = "While in France, Christine Lagarde discussed 100% short-term stimulus efforts in a recent interview with the Wall Street Journal."

# Sums up discrete tags
def entity_group(text):
    # Getting rid of tokens with Object Tag
    tokenized_words = [token for token in st.tag(word_tokenize(text)) if token[1] != 'O']

    comparator = tokenized_words[0]
    list_index = 0
    append_index = 0

    # Store the element from the beginning of the list
    comparator = tokenized_words[0]

    # Insert first element to the tag_touple list
    tag_touple_list = [(comparator[0], comparator[1])]

    # Increase the list index since we've added an element at the beginning
    list_index += 1

    # Continue iteration until the index has reached to the end of the list
    while (list_index < len(tokenized_words)):

        if (comparator[1] == tokenized_words[list_index][1]):

            # If it's a Percentage symbol skip the space
            space_or_not = '' if tokenized_words[list_index][0] == '%' else ' '

            # which is to be appended
            to_be_appended = tag_touple_list[append_index][0] + space_or_not + tokenized_words[list_index][0]

            # Set the modified value to the touple list
            tag_touple_list[append_index] = (to_be_appended, comparator[1])

            # Increase list index
            list_index += 1

            continue

        else:
            # Replace the comparator with new one
            comparator = tokenized_words[list_index]

            # print "Comparator got changed: {0}".format(comparator)

            # Place the comparator to the list
            tag_touple_list.append((comparator[0], comparator[1]))

            # Increase index
            list_index += 1

            # Since it's a new entity we need to increase the location of the insertion
            append_index += 1

    print tag_touple_list

if __name__ == '__main__':
    news_ner_tags = {}
    ner_person = []
    ner_location = []
    ner_organization = []
    ner_date = []
    ner_money = []
    ner_percent = []
    ner_time = []

    # news_text = "While in France, John Cena Reese discussed 100% short-term stimulus efforts in a recent interview with the Wall Street Journal."
    news_text = "Police in Singapore have detained a Hizb ut-Tahir member named Zulfikar Mohamad Shariff for spreading radical Islamist ideology online and radicalising at least two other citizens.The 44-year-old man was arrested in Singapore this month and ordered two years' detention, according to a statement from Singaporean Home Ministry on Friday."

    # My algorithm
    entity_group(news_text)

    # Algorithm from StackOverflow
    print create_ner_entities_tuple(news_text)
<|endoftext|>


<commit_before>#!/usr/bin/env python

import os, sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'test_settings'
parent = os.path.dirname(os.path.dirname(os.path.dirname(
            os.path.abspath(__file__))))

sys.path.insert(0, parent)

from django.test.simple import DjangoTestSuiteRunner
from django.conf import settings

def runtests():
    DjangoTestSuiteRunner(failfast=False).run_tests([
        'crispy_forms.TestBasicFunctionalityTags',
        'crispy_forms.TestFormHelpers',
        'crispy_forms.TestFormLayout',
        ], verbosity=1, interactive=True)

if __name__ == '__main__':
    runtests()
<commit_msg>Fixing indentation style<commit_after>#!/usr/bin/env python

import os, sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'test_settings'
parent = os.path.dirname(os.path.dirname(os.path.dirname(
            os.path.abspath(__file__))))

sys.path.insert(0, parent)

from django.test.simple import DjangoTestSuiteRunner
from django.conf import settings

def runtests():
    DjangoTestSuiteRunner(failfast=False).run_tests([
        'crispy_forms.TestBasicFunctionalityTags',
        'crispy_forms.TestFormHelpers',
        'crispy_forms.TestFormLayout',
    ], verbosity=1, interactive=True)

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


<commit_before>import unittest
import transaction

from pyramid import testing

from models import DBSession

class TestMyView(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()
        from sqlalchemy import create_engine
        engine = create_engine('sqlite://')
        from models import Base, MyModel
        DBSession.configure(bind=engine)
        Base.metadata.create_all(engine)
        with transaction.manager:
            model = MyModel(name='one', value=55)
            DBSession.add(model)

    def tearDown(self):
        DBSession.remove()
        testing.tearDown()

    def test_it(self):
        from views import my_view
        request = testing.DummyRequest()
        #info = my_view(request)
        #self.assertEqual(info['one'].name, 'one')
        #self.assertEqual(info['project'], 'TestProject')

<commit_msg>remove test views<commit_after><|endoftext|>


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

import base_classes as bc
from struct import pack, unpack

# GPIB Instruments
ARCEUS   = {
    'gpib_nickname'    : 'arceus',
    'name'             : 'Agilent 8753ES S-Parameter Network Analyzer',
    'get_byte_order'   : '',
    'byte_order_little': '',
    }
MELOETTA = {
    'gpib_nickname'    : 'meloetta',
    'name'             : 'Hewlett-Packard 6623A System DC Power Supply',
    'get_byte_order'   : '',
    'byte_order_little': '',
    }
XERNEAS  = {
    'gpib_nickname'    : 'xerneas',
    'name'             : 'Hewlett-Packard 4156A Precision Semiconductor Parameter Analyzer',
    'get_byte_order'   : '',
    'byte_order_little': '',
    }

# TCPIP Instruments
DARKRAI  = {
    'nickname'          : 'darkrai',
    'name'              : 'Agilent N9020A MXA Signal Analyzer',
    'socket'            : ('192.168.1.5', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
DEOXYS   = {
    'nickname'          : 'deoxys',
    'name'              : 'Agilent InfiniiVision MSO7104A Mixed Signal Oscilloscope',
    'socket'            : ('192.168.1.10', 5025),
    'get_byte_order'    : ':waveform:byteorder?',
    'byte_order_little' : 'LSBF',
    }
GENESECT = {
    'nickname'          : 'genesect',
    'name'              : 'Agilent B2962A Power Source',
    'socket'            : ('192.168.1.9', 5025),
    'get_byte_order'    : ':format:border?',
    'byte_order_little' : 'NORM',
    'get_data_format'   : ':format:data?',
    'data_format_single': 'REAL,32',
    'data_format_double': 'REAL,64',
    }
GIRATINA = {
    'nickname'          : 'giratina',
    'name'              : 'Agilent B2962A Power Source',
    'socket'            : ('192.168.1.8', 5025),
    'get_byte_order'    : ':format:border?',
    'byte_order_little' : 'NORM',
    'get_data_format'   : ':format:data?',
    'data_format_single': 'REAL,32',
    'data_format_double': 'REAL,64',
    }
HEATRAN  = {
    'nickname'          : 'heatran',
    'name'              : 'Agilent 16803A Logic Analyzer',
    'socket'            : ('192.168.1.11', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
HO_OH    = {
    'nickname'          : 'ho_oh',
    'name'              : 'Agilent N5182A MXG Vector Signal Generator',
    'socket'            : ('192.168.1.4', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
KYUREM   = {
    'nickname'          : 'kyurem',
    'name'              : 'Agilent N5183A MXG Analog Signal Generator',
    'socket'            : ('192.168.1.3', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
RAYQUAZA = {
    'nickname'          : 'rayquaza',
    'name'              : 'Agilent E4443A PSA Series Spectrum Analyzer',
    'socket'            : ('192.168.1.2', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
YVELTAL  = {
    'nickname'          : 'yveltal',
    'name'              : 'Agilent B2902A Precision Source/Measure Unit',
    'socket'            : ('192.168.1.7', 5025),
    'get_byte_order'    : ':format:border?',
    'byte_order_little' : 'NORM',
    'get_data_format'   : ':format:data?',
    'data_format_single': 'REAL,32',
    'data_format_double': 'REAL,64',
    }
ZYGARDE  = {
    'nickname'          : 'zygarde',
    'name'              : 'Agilent E5071C ENA Series Network Analyzer',
    'socket'            : ('192.168.1.6', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }

class Arceus(bc.GPIBInstrument):
    def __init__(self):
        self.DATA = ARCEUS
        super(Arceus, self).__init__(nickname=self.DATA['gpib_nickname'])

class Meloetta(bc.GPIBInstrument):
    def __init__(self):
        self.DATA = MELOETTA
        super(Meloetta, self).__init__(nickname=self.DATA['gpib_nickname'])

class Xerneas(bc.GPIBInstrument):
    def __init__(self):
        self.DATA = XERNEAS
        super(Xerneas, self).__init__(nickname=self.DATA['gpib_nickname'])

class Darkrai(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = DARKRAI
        super(Darkrai, self).__init__(socket_pair=self.DATA['socket'])

class Deoxys(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = DEOXYS
        super(Deoxys, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':waveform:byteorder msbfirst')
        self.write(':waveform:format word')
        self.write('*OPC')

    def _chop16(self, s):
        """A generator that, given a string, yields its 16-bit slices.

        :param str s:
            The string to be chopped
        :returns out:
            A two-character (16-bit) string.
        :rtype: str
        """
        n = 0
        while True:
            k = s[n:n+2]
            if not k:
                break
            yield k
            n += 2

    def _half_to_float(self, half):
        """Converts half-precision floating-point (16-bit) binary data to
        Python ``float``\ .

        :param str half:
            A 16-bit string to be converted to a Python float
        :returns out:
            The actual floating point number represented by the 16-bit string.
        :rtype: float

        This was copied from `fpmurphy`_

        .. _fpmurphy: http://fpmurphy.blogspot.com/2008/12/half-precision-floating-point-format_14.html
        """
        # Get byte order of input
        bo = '<' if self._is_little_endian() else '>'

        # Preliminary unpacking
        fmt = '{0}H'.format(bo)
        h = unpack(fmt, half)[0]

        # Pad 16 bits to 32 bits
        s = int((h >> 15) & 0x00000001)  # sign
        e = int((h >> 10) & 0x0000001F)  # exponent
        f = int(h         & 0x000003FF)  # fraction
        if e == 0x00:   # exponent is 0
            if f == 0x00:
                hpad = int(s << 31)
            else:
                while not (f & 0x00000400):
                    f <<= 1
                    e -= 1
                e += 1
                f &= ~0x00000400
        elif e == 0x1F: # exponent is 31
            if f == 0x00:
                hpad = int((s << 31) | 0x7F800000)
            else:
                hpad = int((s << 31) | 0x7F800000 | (f << 13))
        e = e + (127 - 15)
        f = f << 13
        hpad = int((s << 31) | (e << 23) | f)

        # struct.pack hack
        st = pack('I', hpad)
        out = unpack('f', st)
        return out

    def read_preamble(self):
        """Read the waveform preamble from Deoxys.  It contains the following
        metadata about the waveform data:

        :returns out:
        :rtype: dict
        """
        # TODO Combine write, preamble and data in one function
        # TODO Read :waveform:preamble
        #           format WORD this is two bytes for each data point
        #           type   :waveform:type?
        #           points :waveform:points? can be found in the header of :waveform:data?
        #           count  :acquire:count? averaging for one data point, etc
        #           xincrement
        #           xorigin
        #           xreference
        #           yincrement
        #           yorigin
        #           yreference
        # TODO Read :save:waveform:start I do not know how to transfer a file
        pass

    def compose_waveform_xy(self, waveform_y, waveform_preamble):
        """Compose the (x,y) data list according to the y data and preamble
        obtained from the instrument.

        :returns out:
            A 2-column list.  The first column holds the x values and the
            second column holds the y values.
        :rtype: list
        """
        # TODO Read :waveform:data
        #           :waveform:byteorder DONE
        #           :waveform:unsigned  DONE
        #           :waveform:format    DONE
        #           :waveform:source    channel | function | math | pod | bus | sbus
        #           :system:precision
        #           0x0000 hole
        #           0x0001 clipped low
        #           0xFFFF clipped high

        # TODO Need to adjust waveform_x for special values (clipped, etc)
        # TODO Need to adjust waveform_x according to preamble
        # TODO Need to create waveform_y according to preamble
        # TODO Need to compose X and Y values
        pass

    def ask_waveform_data(self):
        """A convenience function to query the waveform preamble and waveform
        data in one call.  Additionally, it also composes the (x,y) data list.

        :returns out:
            A 2-column list.  The first column holds the x values and the
            second column holds the y values.
        :rtype: list
        """
        self.write(':waveform:preamble?')
        waveform_preamble = self.read_preamble()
        self.write(':waveform:data?')
        waveform_y = self.read_ieee754()
        out = self.compose_waveform_xy(waveform_y, waveform_preamble)
        return out

class Genesect(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = GENESECT
        super(Genesect, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':format:data real,32')
        self.write('*OPC')

class Giratina(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = GIRATINA
        super(Giratina, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':format:data real,32')
        self.write('*OPC')

class Heatran(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = HEATRAN
        super(Heatran, self).__init__(socket_pair=self.DATA['socket'])

class Ho_oh(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = HO_OH
        super(Ho_oh, self).__init__(socket_pair=self.DATA['socket'])

class Kyurem(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = KYUREM
        super(Kyurem, self).__init__(socket_pair=self.DATA['socket'])

class Rayquaza(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = RAYQUAZA
        super(Rayquaza, self).__init__(socket_pair=self.DATA['socket'])

class Yveltal(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = YVELTAL
        super(Yveltal, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':format:data real,32')
        self.write('*OPC')

class Zygarde(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = ZYGARDE
        super(Zygarde, self).__init__(socket_pair=self.DATA['socket'])
<commit_msg>Whitespace between class definitions<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import base_classes as bc
from struct import pack, unpack

# GPIB Instruments
ARCEUS   = {
    'gpib_nickname'    : 'arceus',
    'name'             : 'Agilent 8753ES S-Parameter Network Analyzer',
    'get_byte_order'   : '',
    'byte_order_little': '',
    }
MELOETTA = {
    'gpib_nickname'    : 'meloetta',
    'name'             : 'Hewlett-Packard 6623A System DC Power Supply',
    'get_byte_order'   : '',
    'byte_order_little': '',
    }
XERNEAS  = {
    'gpib_nickname'    : 'xerneas',
    'name'             : 'Hewlett-Packard 4156A Precision Semiconductor Parameter Analyzer',
    'get_byte_order'   : '',
    'byte_order_little': '',
    }

# TCPIP Instruments
DARKRAI  = {
    'nickname'          : 'darkrai',
    'name'              : 'Agilent N9020A MXA Signal Analyzer',
    'socket'            : ('192.168.1.5', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
DEOXYS   = {
    'nickname'          : 'deoxys',
    'name'              : 'Agilent InfiniiVision MSO7104A Mixed Signal Oscilloscope',
    'socket'            : ('192.168.1.10', 5025),
    'get_byte_order'    : ':waveform:byteorder?',
    'byte_order_little' : 'LSBF',
    }
GENESECT = {
    'nickname'          : 'genesect',
    'name'              : 'Agilent B2962A Power Source',
    'socket'            : ('192.168.1.9', 5025),
    'get_byte_order'    : ':format:border?',
    'byte_order_little' : 'NORM',
    'get_data_format'   : ':format:data?',
    'data_format_single': 'REAL,32',
    'data_format_double': 'REAL,64',
    }
GIRATINA = {
    'nickname'          : 'giratina',
    'name'              : 'Agilent B2962A Power Source',
    'socket'            : ('192.168.1.8', 5025),
    'get_byte_order'    : ':format:border?',
    'byte_order_little' : 'NORM',
    'get_data_format'   : ':format:data?',
    'data_format_single': 'REAL,32',
    'data_format_double': 'REAL,64',
    }
HEATRAN  = {
    'nickname'          : 'heatran',
    'name'              : 'Agilent 16803A Logic Analyzer',
    'socket'            : ('192.168.1.11', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
HO_OH    = {
    'nickname'          : 'ho_oh',
    'name'              : 'Agilent N5182A MXG Vector Signal Generator',
    'socket'            : ('192.168.1.4', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
KYUREM   = {
    'nickname'          : 'kyurem',
    'name'              : 'Agilent N5183A MXG Analog Signal Generator',
    'socket'            : ('192.168.1.3', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
RAYQUAZA = {
    'nickname'          : 'rayquaza',
    'name'              : 'Agilent E4443A PSA Series Spectrum Analyzer',
    'socket'            : ('192.168.1.2', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }
YVELTAL  = {
    'nickname'          : 'yveltal',
    'name'              : 'Agilent B2902A Precision Source/Measure Unit',
    'socket'            : ('192.168.1.7', 5025),
    'get_byte_order'    : ':format:border?',
    'byte_order_little' : 'NORM',
    'get_data_format'   : ':format:data?',
    'data_format_single': 'REAL,32',
    'data_format_double': 'REAL,64',
    }
ZYGARDE  = {
    'nickname'          : 'zygarde',
    'name'              : 'Agilent E5071C ENA Series Network Analyzer',
    'socket'            : ('192.168.1.6', 5025),
    'get_byte_order'    : '',
    'byte_order_little' : '',
    }

class Arceus(bc.GPIBInstrument):
    def __init__(self):
        self.DATA = ARCEUS
        super(Arceus, self).__init__(nickname=self.DATA['gpib_nickname'])

class Meloetta(bc.GPIBInstrument):
    def __init__(self):
        self.DATA = MELOETTA
        super(Meloetta, self).__init__(nickname=self.DATA['gpib_nickname'])

class Xerneas(bc.GPIBInstrument):
    def __init__(self):
        self.DATA = XERNEAS
        super(Xerneas, self).__init__(nickname=self.DATA['gpib_nickname'])

class Darkrai(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = DARKRAI
        super(Darkrai, self).__init__(socket_pair=self.DATA['socket'])

class Deoxys(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = DEOXYS
        super(Deoxys, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':waveform:byteorder msbfirst')
        self.write(':waveform:format word')
        self.write('*OPC')

    def _chop16(self, s):
        """A generator that, given a string, yields its 16-bit slices.

        :param str s:
            The string to be chopped
        :returns out:
            A two-character (16-bit) string.
        :rtype: str
        """
        n = 0
        while True:
            k = s[n:n+2]
            if not k:
                break
            yield k
            n += 2

    def _half_to_float(self, half):
        """Converts half-precision floating-point (16-bit) binary data to
        Python ``float``\ .

        :param str half:
            A 16-bit string to be converted to a Python float
        :returns out:
            The actual floating point number represented by the 16-bit string.
        :rtype: float

        This was copied from `fpmurphy`_

        .. _fpmurphy: http://fpmurphy.blogspot.com/2008/12/half-precision-floating-point-format_14.html
        """
        # Get byte order of input
        bo = '<' if self._is_little_endian() else '>'

        # Preliminary unpacking
        fmt = '{0}H'.format(bo)
        h = unpack(fmt, half)[0]

        # Pad 16 bits to 32 bits
        s = int((h >> 15) & 0x00000001)  # sign
        e = int((h >> 10) & 0x0000001F)  # exponent
        f = int(h         & 0x000003FF)  # fraction
        if e == 0x00:   # exponent is 0
            if f == 0x00:
                hpad = int(s << 31)
            else:
                while not (f & 0x00000400):
                    f <<= 1
                    e -= 1
                e += 1
                f &= ~0x00000400
        elif e == 0x1F: # exponent is 31
            if f == 0x00:
                hpad = int((s << 31) | 0x7F800000)
            else:
                hpad = int((s << 31) | 0x7F800000 | (f << 13))
        e = e + (127 - 15)
        f = f << 13
        hpad = int((s << 31) | (e << 23) | f)

        # struct.pack hack
        st = pack('I', hpad)
        out = unpack('f', st)
        return out

    def read_preamble(self):
        """Read the waveform preamble from Deoxys.  It contains the following
        metadata about the waveform data:

        :returns out:
        :rtype: dict
        """
        # TODO Combine write, preamble and data in one function
        # TODO Read :waveform:preamble
        #           format WORD this is two bytes for each data point
        #           type   :waveform:type?
        #           points :waveform:points? can be found in the header of :waveform:data?
        #           count  :acquire:count? averaging for one data point, etc
        #           xincrement
        #           xorigin
        #           xreference
        #           yincrement
        #           yorigin
        #           yreference
        # TODO Read :save:waveform:start I do not know how to transfer a file
        pass

    def compose_waveform_xy(self, waveform_y, waveform_preamble):
        """Compose the (x,y) data list according to the y data and preamble
        obtained from the instrument.

        :returns out:
            A 2-column list.  The first column holds the x values and the
            second column holds the y values.
        :rtype: list
        """
        # TODO Read :waveform:data
        #           :waveform:byteorder DONE
        #           :waveform:unsigned  DONE
        #           :waveform:format    DONE
        #           :waveform:source    channel | function | math | pod | bus | sbus
        #           :system:precision
        #           0x0000 hole
        #           0x0001 clipped low
        #           0xFFFF clipped high

        # TODO Need to adjust waveform_x for special values (clipped, etc)
        # TODO Need to adjust waveform_x according to preamble
        # TODO Need to create waveform_y according to preamble
        # TODO Need to compose X and Y values
        pass

    def ask_waveform_data(self):
        """A convenience function to query the waveform preamble and waveform
        data in one call.  Additionally, it also composes the (x,y) data list.

        :returns out:
            A 2-column list.  The first column holds the x values and the
            second column holds the y values.
        :rtype: list
        """
        self.write(':waveform:preamble?')
        waveform_preamble = self.read_preamble()
        self.write(':waveform:data?')
        waveform_y = self.read_ieee754()
        out = self.compose_waveform_xy(waveform_y, waveform_preamble)
        return out

class Genesect(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = GENESECT
        super(Genesect, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':format:data real,32')
        self.write('*OPC')

class Giratina(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = GIRATINA
        super(Giratina, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':format:data real,32')
        self.write('*OPC')

class Heatran(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = HEATRAN
        super(Heatran, self).__init__(socket_pair=self.DATA['socket'])

class Ho_oh(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = HO_OH
        super(Ho_oh, self).__init__(socket_pair=self.DATA['socket'])

class Kyurem(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = KYUREM
        super(Kyurem, self).__init__(socket_pair=self.DATA['socket'])

class Rayquaza(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = RAYQUAZA
        super(Rayquaza, self).__init__(socket_pair=self.DATA['socket'])

class Yveltal(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = YVELTAL
        super(Yveltal, self).__init__(socket_pair=self.DATA['socket'])
        self.write(':format:data real,32')
        self.write('*OPC')

class Zygarde(bc.TCPIPInstrument):
    def __init__(self):
        self.DATA = ZYGARDE
        super(Zygarde, self).__init__(socket_pair=self.DATA['socket'])

<|endoftext|>