问:
(Twisted-Autobahn) exceptions.AttributeError: 'WsProtocol' object has no attribute 'factory'
Currently I am getting the following error as the title suggests. My code for a Web Socket using Autobahn's twisted library is below. When I instantiate the WsProtocolFactory, I want to set the protocol to WsProtocol and initialize the factory with a defaultdict for topics to subscribe to.

My initial thought was to add a factory argument to the WsProtocol __init_, but have not been able to fix the error.

Any help would be greatly appreciated.

Thank you!

EDIT: added traceback error

```<code>Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
    why = selectable.doRead()
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 1074, in doRead
    protocol.makeConnection(transport)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/protocol.py", line 494, in makeConnection
    self.connectionMade()
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 95, in connectionMade
    self._connectionMade()
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2426, in _connectionMade
    WebSocketProtocol._connectionMade(self)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 962, in _connectionMade
    setattr(self, configAttr, getattr(self.factory, configAttr))
exceptions.AttributeError: 'WsProtocol' object has no attribute 'factory'

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
    why = selectable.doRead()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 209, in doRead
    return self._dataReceived(data)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 132, in dataReceived
    self._dataReceived(data)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1175, in _dataReceived
    if self.state == WebSocketProtocol.STATE_OPEN:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'state'

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/internet/task.py", line 936, in react
    _reactor.run()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1194, in run
    self.mainLoop()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1206, in mainLoop
    self.doIteration(t)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/epollreactor.py", line 396, in doPoll
    log.callWithLogger(selectable, _drdw, selectable, fd, event)
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 610, in _doReadOrWrite
    self._disconnectSelectable(selectable, why, inRead)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 258, in _disconnectSelectable
    selectable.connectionLost(failure.Failure(why))
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 293, in connectionLost
    protocol.connectionLost(reason)
  File "web_autobahn.py", line 77, in connectionLost
    WebSocketServerProtocol.connectionLost(self, reason)
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 129, in connectionLost
    self._connectionLost(reason)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2437, in _connectionLost
    WebSocketProtocol._connectionLost(self, reason)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1075, in _connectionLost
    if not self.factory.isServer and self.serverConnectionDropTimeoutCall is not None:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'factory'
</code>```

Here is my code

```<code>from datetime import datetime
from collections import defaultdict

from twisted.web import resource, server
from twisted.internet.task import react
from twisted.internet import reactor
from twisted.internet.defer import DeferredList, Deferred
from twisted.internet.endpoints import TCP4ServerEndpoint
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from schema import schema
import json
import os
import sys
from twisted.python import log

def log(msg):
    print("{}: {}".format(str(datetime.now()), msg))

class WsProtocol(WebSocketServerProtocol):
    """Dummy websocket protocol"""
    def __init__(self, topics):
        super(WsProtocol, self).__init__()
        self.topics = topics
        self.subscribed_topic = None
        log("WS protocol init")

    def onConnect(self, request):
        print("WebSocket connected.")

    def onOpen(self):
        print("WebSocket connection open.")

    def onMessage(self, payload, isBinary):
        log(payload)
        self.sendMessage("Message received")

    def onClose(self, wasClean, code, reason):
        print("WebSocket connection closed: {}".format(reason))

    def connectionLost(self, reason):
        WebSocketServerProtocol.connectionLost(self, reason)
        log("Connection closed: Reason is {}".format(reason))

class WsProtocolFactory(WebSocketServerFactory):

    def __init__(self):
        self.topics = defaultdict(set)

    def buildProtocol(self, *args, **kwargs):
        return WsProtocol(self.topics)

def setup_protocols(reactor):
    factories = {
        WsProtocolFactory: 9001,
    }

    for factory, port in factories.items():
        endpoint = TCP4ServerEndpoint(reactor, port)
        endpoint.listen(factory())
        log("Bound port {} to protocol {}".format(str(port), factory))
    return Deferred()

if __name__ == '__main__':
    react(setup_protocols)
</code>```

EDIT: New traceback

```<code>Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
    why = selectable.doRead()
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 1074, in doRead
    protocol.makeConnection(transport)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/protocol.py", line 494, in makeConnection
    self.connectionMade()
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 95, in connectionMade
    self._connectionMade()
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2426, in _connectionMade
    WebSocketProtocol._connectionMade(self)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 962, in _connectionMade
    setattr(self, configAttr, getattr(self.factory, configAttr))
exceptions.AttributeError: 'WsProtocolFactory' object has no attribute 'logOctets'

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
    why = selectable.doRead()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 209, in doRead
    return self._dataReceived(data)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 132, in dataReceived
    self._dataReceived(data)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1175, in _dataReceived
    if self.state == WebSocketProtocol.STATE_OPEN:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'state'

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/internet/task.py", line 936, in react
    _reactor.run()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1194, in run
    self.mainLoop()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1206, in mainLoop
    self.doIteration(t)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/epollreactor.py", line 396, in doPoll
    log.callWithLogger(selectable, _drdw, selectable, fd, event)
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 610, in _doReadOrWrite
    self._disconnectSelectable(selectable, why, inRead)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 258, in _disconnectSelectable
    selectable.connectionLost(failure.Failure(why))
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 293, in connectionLost
    protocol.connectionLost(reason)
  File "web_autobahn.py", line 77, in connectionLost
    WebSocketServerProtocol.connectionLost(self, reason)
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 129, in connectionLost
    self._connectionLost(reason)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2437, in _connectionLost
    WebSocketProtocol._connectionLost(self, reason)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1082, in _connectionLost
    if self.autoPingPendingCall:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'autoPingPendingCall'
</code>```

EDIT: Correct code that works

```<code>from datetime import datetime
from collections import defaultdict

from twisted.web import resource, server
from twisted.internet.task import react
from twisted.internet import reactor
from twisted.internet.defer import DeferredList, Deferred
from twisted.internet.endpoints import TCP4ServerEndpoint
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from schema import schema
import json
import os
import sys
from twisted.python import log

def log(msg):
    print("{}: {}".format(str(datetime.now()), msg))

class WsProtocol(WebSocketServerProtocol):
    """Dummy websocket protocol"""
    def __init__(self, topics):
        super(WsProtocol, self).__init__()
        self.topics = topics
        self.subscribed_topic = None
        log("WS protocol init")

    def onConnect(self, request):
        print("WebSocket connected.")

    def onOpen(self):
        print("WebSocket connection open.")

    def onMessage(self, payload, isBinary):
        log(payload)
        self.sendMessage("Message received")

    def onClose(self, wasClean, code, reason):
        print("WebSocket connection closed: {}".format(reason))

    def connectionLost(self, reason):
        WebSocketServerProtocol.connectionLost(self, reason)
        log("Connection closed: Reason is {}".format(reason))

class WsProtocolFactory(WebSocketServerFactory):

    def __init__(self):
        super(WsProtocolFactory, self).__init__()
        self.topics = defaultdict(set)

    def buildProtocol(self, *args, **kwargs):
        protocol = WsProtocol(self.topics)
        protocol.factory = self
        return protocol

def setup_protocols(reactor):
    factories = {
        WsProtocolFactory: 9001,
    }

    for factory, port in factories.items():
        endpoint = TCP4ServerEndpoint(reactor, port)
        endpoint.listen(factory())
        log("Bound port {} to protocol {}".format(str(port), factory))
    return Deferred()

if __name__ == '__main__':
    react(setup_protocols)
</code>```

答:
Define your factory's buildProtocol so it gives the protocols it builds a factory attribute:

```<code>def buildProtocol(self, *args, **kwargs):
    protocol = WsProtocol(self.topics)
    protocol.factory = self
    return protocol
</code>```

Setting the factory this way is a widespread convention assumed by a lot of different Twisted-using code.

I'm not sure what logOctets is but my guess is that this is caused by a similar issue.

Let's look at buildProtocol again.  Your class overrides the base implementation of buildProtocol which does something like:

```<code>def buildProtocol(self, *args, **kwargs):
    protocol = self.protocol()
    protocol.factory = self
    return protocol
</code>```

Your version caused the first AttributeError (for factory) by not re-implementing quite all of this behavior and the fix was to update your version so it was closer to the original.

My guess is that WebSocketServerFactory.__init__ has some additional behavior and your subclass's __init__ has clobbered that.  The fix is either to replicate it or (better) call the base implementation.

Body:
Currently I am getting the following error as the title suggests. My code for a Web Socket using Autobahn's twisted library is below. When I instantiate the WsProtocolFactory, I want to set the protocol to WsProtocol and initialize the factory with a defaultdict for topics to subscribe to.

My initial thought was to add a factory argument to the WsProtocol __init_, but have not been able to fix the error.

Any help would be greatly appreciated.

Thank you!

EDIT: added traceback error

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
    why = selectable.doRead()
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 1074, in doRead
    protocol.makeConnection(transport)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/protocol.py", line 494, in makeConnection
    self.connectionMade()
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 95, in connectionMade
    self._connectionMade()
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2426, in _connectionMade
    WebSocketProtocol._connectionMade(self)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 962, in _connectionMade
    setattr(self, configAttr, getattr(self.factory, configAttr))
exceptions.AttributeError: 'WsProtocol' object has no attribute 'factory'

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
    why = selectable.doRead()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 209, in doRead
    return self._dataReceived(data)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 132, in dataReceived
    self._dataReceived(data)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1175, in _dataReceived
    if self.state == WebSocketProtocol.STATE_OPEN:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'state'

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/internet/task.py", line 936, in react
    _reactor.run()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1194, in run
    self.mainLoop()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1206, in mainLoop
    self.doIteration(t)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/epollreactor.py", line 396, in doPoll
    log.callWithLogger(selectable, _drdw, selectable, fd, event)
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 610, in _doReadOrWrite
    self._disconnectSelectable(selectable, why, inRead)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 258, in _disconnectSelectable
    selectable.connectionLost(failure.Failure(why))
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 293, in connectionLost
    protocol.connectionLost(reason)
  File "web_autobahn.py", line 77, in connectionLost
    WebSocketServerProtocol.connectionLost(self, reason)
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 129, in connectionLost
    self._connectionLost(reason)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2437, in _connectionLost
    WebSocketProtocol._connectionLost(self, reason)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1075, in _connectionLost
    if not self.factory.isServer and self.serverConnectionDropTimeoutCall is not None:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'factory'

Here is my code

from datetime import datetime
from collections import defaultdict

from twisted.web import resource, server
from twisted.internet.task import react
from twisted.internet import reactor
from twisted.internet.defer import DeferredList, Deferred
from twisted.internet.endpoints import TCP4ServerEndpoint
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from schema import schema
import json
import os
import sys
from twisted.python import log

def log(msg):
    print("{}: {}".format(str(datetime.now()), msg))

class WsProtocol(WebSocketServerProtocol):
    """Dummy websocket protocol"""
    def __init__(self, topics):
        super(WsProtocol, self).__init__()
        self.topics = topics
        self.subscribed_topic = None
        log("WS protocol init")

    def onConnect(self, request):
        print("WebSocket connected.")

    def onOpen(self):
        print("WebSocket connection open.")

    def onMessage(self, payload, isBinary):
        log(payload)
        self.sendMessage("Message received")

    def onClose(self, wasClean, code, reason):
        print("WebSocket connection closed: {}".format(reason))

    def connectionLost(self, reason):
        WebSocketServerProtocol.connectionLost(self, reason)
        log("Connection closed: Reason is {}".format(reason))

class WsProtocolFactory(WebSocketServerFactory):

    def __init__(self):
        self.topics = defaultdict(set)

    def buildProtocol(self, *args, **kwargs):
        return WsProtocol(self.topics)

def setup_protocols(reactor):
    factories = {
        WsProtocolFactory: 9001,
    }

    for factory, port in factories.items():
        endpoint = TCP4ServerEndpoint(reactor, port)
        endpoint.listen(factory())
        log("Bound port {} to protocol {}".format(str(port), factory))
    return Deferred()

if __name__ == '__main__':
    react(setup_protocols)

EDIT: New traceback

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
    why = selectable.doRead()
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 1074, in doRead
    protocol.makeConnection(transport)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/protocol.py", line 494, in makeConnection
    self.connectionMade()
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 95, in connectionMade
    self._connectionMade()
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2426, in _connectionMade
    WebSocketProtocol._connectionMade(self)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 962, in _connectionMade
    setattr(self, configAttr, getattr(self.factory, configAttr))
exceptions.AttributeError: 'WsProtocolFactory' object has no attribute 'logOctets'

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
    why = selectable.doRead()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 209, in doRead
    return self._dataReceived(data)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 215, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 132, in dataReceived
    self._dataReceived(data)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1175, in _dataReceived
    if self.state == WebSocketProtocol.STATE_OPEN:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'state'

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/twisted/internet/task.py", line 936, in react
    _reactor.run()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1194, in run
    self.mainLoop()
  File "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1206, in mainLoop
    self.doIteration(t)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/epollreactor.py", line 396, in doPoll
    log.callWithLogger(selectable, _drdw, selectable, fd, event)
--- <exception caught here> ---
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib64/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 610, in _doReadOrWrite
    self._disconnectSelectable(selectable, why, inRead)
  File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 258, in _disconnectSelectable
    selectable.connectionLost(failure.Failure(why))
  File "/usr/lib64/python2.7/site-packages/twisted/internet/tcp.py", line 293, in connectionLost
    protocol.connectionLost(reason)
  File "web_autobahn.py", line 77, in connectionLost
    WebSocketServerProtocol.connectionLost(self, reason)
  File "/usr/lib/python2.7/site-packages/autobahn/twisted/websocket.py", line 129, in connectionLost
    self._connectionLost(reason)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 2437, in _connectionLost
    WebSocketProtocol._connectionLost(self, reason)
  File "/usr/lib/python2.7/site-packages/autobahn/websocket/protocol.py", line 1082, in _connectionLost
    if self.autoPingPendingCall:
exceptions.AttributeError: 'WsProtocol' object has no attribute 'autoPingPendingCall'

EDIT: Correct code that works

from datetime import datetime
from collections import defaultdict

from twisted.web import resource, server
from twisted.internet.task import react
from twisted.internet import reactor
from twisted.internet.defer import DeferredList, Deferred
from twisted.internet.endpoints import TCP4ServerEndpoint
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
from schema import schema
import json
import os
import sys
from twisted.python import log

def log(msg):
    print("{}: {}".format(str(datetime.now()), msg))

class WsProtocol(WebSocketServerProtocol):
    """Dummy websocket protocol"""
    def __init__(self, topics):
        super(WsProtocol, self).__init__()
        self.topics = topics
        self.subscribed_topic = None
        log("WS protocol init")

    def onConnect(self, request):
        print("WebSocket connected.")

    def onOpen(self):
        print("WebSocket connection open.")

    def onMessage(self, payload, isBinary):
        log(payload)
        self.sendMessage("Message received")

    def onClose(self, wasClean, code, reason):
        print("WebSocket connection closed: {}".format(reason))

    def connectionLost(self, reason):
        WebSocketServerProtocol.connectionLost(self, reason)
        log("Connection closed: Reason is {}".format(reason))

class WsProtocolFactory(WebSocketServerFactory):

    def __init__(self):
        super(WsProtocolFactory, self).__init__()
        self.topics = defaultdict(set)

    def buildProtocol(self, *args, **kwargs):
        protocol = WsProtocol(self.topics)
        protocol.factory = self
        return protocol

def setup_protocols(reactor):
    factories = {
        WsProtocolFactory: 9001,
    }

    for factory, port in factories.items():
        endpoint = TCP4ServerEndpoint(reactor, port)
        endpoint.listen(factory())
        log("Bound port {} to protocol {}".format(str(port), factory))
    return Deferred()

if __name__ == '__main__':
    react(setup_protocols)

Body:
Define your factory's buildProtocol so it gives the protocols it builds a factory attribute:

def buildProtocol(self, *args, **kwargs):
    protocol = WsProtocol(self.topics)
    protocol.factory = self
    return protocol

Setting the factory this way is a widespread convention assumed by a lot of different Twisted-using code.

I'm not sure what logOctets is but my guess is that this is caused by a similar issue.

Let's look at buildProtocol again.  Your class overrides the base implementation of buildProtocol which does something like:

def buildProtocol(self, *args, **kwargs):
    protocol = self.protocol()
    protocol.factory = self
    return protocol

Your version caused the first AttributeError (for factory) by not re-implementing quite all of this behavior and the fix was to update your version so it was closer to the original.

My guess is that WebSocketServerFactory.__init__ has some additional behavior and your subclass's __init__ has clobbered that.  The fix is either to replicate it or (better) call the base implementation.


来源: stackexchange

问:
Autofac Dependency Injection in Azure Function
I am trying to implement DI using Autofac IOC in Azure function.
I need to build the container, but not sure where to put the code to build the container</p>

答:
I think for now you would need to do something ugly like:

public static string MyAwesomeFunction(string message)
{
    if (MyService == null)
    {
        var instantiator = Initialize();
        MyService = instantiator.Resolve<IService>();
    }

    return MyService.Hello(message);
}

private static IService MyService = null;

private static IContainer Initialize()
{
    // Do your IoC magic here
}
```

ContentLicense: CC BY-SA 3.0


问:
Iterate array based on operations on elements
Suppose I have two arrays.

```<code>a = ["A1", "A2", "B1"]
b = ["C2", "B1", "C1"]
</code>```

What I want is to iterate over the array 'a' first,

Get the element from array 'a' and do some operation on that.

1.a If that operation is successful, then move to next element of array 'a'.

1.b If that operation is unsuccessful, then start iterating the array 'b'.

Get the element from array 'b' and do some operation on that.

2.a If that operation is successful, then move to next element of array 'b'.

2.b If that operation is unsuccessful, then start iterating the array 'a' from the position where we left in 1.a and so on...

All of this would go on till all elements in both arrays are traversed. What is the approach I should use to code? I would be happy to write in case more details are needed.

答:
This is very similar to the merging process in merge sort/merging two sorted arrays. Look into that too if you haven't already. Just maintain a flag variable to know which array to work on next.

```<code>iterA := 0
iterB := 0
flag := 0

while (iterA < size(A) && iterB < size(B)):

    if (flag == 0):

        if(doOperation(A[iterA]) == success):
            iterA := iterA + 1
        else:
            flag := 1
            iterA := iterA + 1

    if (flag == 1):

        if(doOperation(B[iterB]) == success):
            iterA := iterA + 1
        else:
            flag := 0
            iterA := iterA + 1

while (iterA < size(A)):
    doOperation(A[iterA])
    iterA := iterA + 1

while (iterB < size(B)):
    doOperation(B[iterB])
    iterB := iterB + 1
</code>```

I'm assuming if all elements of an array are successfully operated on, we do the operations on only the remaining elements of the other array.

Body:
Suppose I have two arrays.

a = ["A1", "A2", "B1"]
b = ["C2", "B1", "C1"]

What I want is to iterate over the array 'a' first,

Get the element from array 'a' and do some operation on that.

1.a If that operation is successful, then move to next element of array 'a'.

1.b If that operation is unsuccessful, then start iterating the array 'b'.

Get the element from array 'b' and do some operation on that.

2.a If that operation is successful, then move to next element of array 'b'.

2.b If that operation is unsuccessful, then start iterating the array 'a' from the position where we left in 1.a and so on...

All of this would go on till all elements in both arrays are traversed. What is the approach I should use to code? I would be happy to write in case more details are needed.

Body:
This is very similar to the merging process in merge sort/merging two sorted arrays. Look into that too if you haven't already. Just maintain a flag variable to know which array to work on next.

iterA := 0
iterB := 0
flag := 0

while (iterA < size(A) && iterB < size(B)):

    if (flag == 0):

        if(doOperation(A[iterA]) == success):
            iterA := iterA + 1
        else:
            flag := 1
            iterA := iterA + 1

    if (flag == 1):

        if(doOperation(B[iterB]) == success):
            iterA := iterA + 1
        else:
            flag := 0
            iterA := iterA + 1

while (iterA < size(A)):
    doOperation(A[iterA])
    iterA := iterA + 1

while (iterB < size(B)):
    doOperation(B[iterB])
    iterB := iterB + 1

I'm assuming if all elements of an array are successfully operated on, we do the operations on only the remaining elements of the other array.


问:
cohort analysis in R
I have a dataset test of user ids and the quarter in which user ids made a transaction:

```<code>> test
   id quarter
1   7      Q1
2   7      Q1
3   5      Q1
4   8      Q1
5   3      Q1
6   6      Q1
7  10      Q1
8   3      Q2
9  10      Q2
10  8      Q2
11  2      Q2
12  7      Q2
13  6      Q2
14  6      Q3
15  9      Q3
16  6      Q3
17  4      Q3
18  9      Q3
19  2      Q3
20  5      Q4
21  8      Q4
22 10      Q4
23  7      Q4
24  1      Q4
25  8      Q4
</code>```

My objective is to find the number of unique users that were active in Quarter 1 and, of those users, get the number of users that made transactions in subsequent quarters.

The output from the above sample should be:

```<code>> output
  quarter unique_cohort_ids
1      Q1                 6
2      Q2                 5
3      Q3                 1
4      Q4                 4
</code>```

The following code works, but it is verbose and repetitive, and is not ideal for a larger dataset with dozens of quarters.  Any suggestions on how to improve it would be appreciated:

```<code>subsets <-split(test, test$quarter, drop=TRUE)

for (i in 1:length(subsets)) {
  assign(paste0("Q", i), as.data.frame(subsets[[i]]))
}

Q1_ids <- sum(uniqueQ1$id)
Q2_ids <- sum(unique(Q1$id) %in% unique(Q2$id))
Q3_ids <- sum(unique(Q1$id) %in% unique(Q3$id))
Q4_ids <- sum(unique(Q1$id) %in% unique(Q4$id))
</code>```

Cohort analysis also includes counting the number of new users
per quarter.  As opposed to manually counting the unique user ids that appear per quarter that had not appeared in any previous quarters, I am seeking a programmatic solution.  In this case, the output would be the following list:

```<code>> mylist
[[1]]
[1]  7  5  8  3  6 10

[[2]]
[1] 2

[[3]]
[1] 9 4

[[4]]
[1] 1
</code>```

Any suggestions would be appreciated

Here is the sample data:

```<code>> dput(test)
structure(list(id = c(7, 7, 5, 8, 3, 6, 10, 3, 10, 8, 2, 7, 6,
6, 9, 6, 4, 9, 2, 5, 8, 10, 7, 1, 8), quarter = c("Q1", "Q1",
"Q1", "Q1", "Q1", "Q1", "Q1", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2",
"Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q4", "Q4", "Q4", "Q4", "Q4",
"Q4")), .Names = c("id", "quarter"), row.names = c(NA, -25L), class = "data.frame")
</code>```

答:
Here's an example using data.table

```<code>library(data.table)
setDT(test)[, sum(unique(id) %in% test[quarter=="Q1", id]), by = quarter]

#   quarter V1
#1:      Q1  6
#2:      Q2  5
#3:      Q3  1
#4:      Q4  4
</code>```

And for the second part of the analysis:

```<code>split(test$id[!duplicated(test$id)], test$quarter[!duplicated(test$id)])

#$Q1
#[1]  7  5  8  3  6 10

#$Q2
#[1] 2

#$Q3
#[1] 9 4

#$Q4
#[1] 1
</code>```

Update based on new requirements:

This seems a bit clunky to me but was the easiest way to keep track of the ids seen as you progress through the quarters.

```<code>quarts <- sort(unique(test$quarter))
test$occur <- 1
mat <- dcast.data.table(test, id ~ quarter, value.var = "occur", fun.aggregate = sum)

res <- mat[Q1 >0, lapply(.SD, function(x) sum(x>0)), .SDcols=colnames(mat)[-1]] ##initalize the results with "Q1"
cumMat <- mat$Q1 ##this will keep track of the ids that were seen in previous quarters

for(i in quarts[-1]){                                  ##foreach quarter (except ("Q1") we will count the number of unique ids that are in that quarter and not in any previous quarters.
  res <- rbind(res, mat[cumMat == 0 & get(i) > 0, lapply(.SD, function(x) sum(x>0)), .SDcols=colnames(mat)[-1]])
  cumMat <- rowSums(cbind(cumMat, mat[, i, with = F])) ##update the ids seen
}
t(res)

#   [,1] [,2] [,3] [,4]
#Q1    6    0    0    0
#Q2    5    1    0    0
#Q3    1    1    2    0
#Q4    4    0    0    1
</code>```

Title: cohort analysis in R

Body:
I have a dataset test of user ids and the quarter in which user ids made a transaction:

> test
   id quarter
1   7      Q1
2   7      Q1
3   5      Q1
4   8      Q1
5   3      Q1
6   6      Q1
7  10      Q1
8   3      Q2
9  10      Q2
10  8      Q2
11  2      Q2
12  7      Q2
13  6      Q2
14  6      Q3
15  9      Q3
16  6      Q3
17  4      Q3
18  9      Q3
19  2      Q3
20  5      Q4
21  8      Q4
22 10      Q4
23  7      Q4
24  1      Q4
25  8      Q4

My objective is to find the number of unique users that were active in Quarter 1 and, of those users, get the number of users that made transactions in subsequent quarters.

The output from the above sample should be:

> output
  quarter unique_cohort_ids
1      Q1                 6
2      Q2                 5
3      Q3                 1
4      Q4                 4

The following code works, but it is verbose and repetitive, and is not ideal for a larger dataset with dozens of quarters.  Any suggestions on how to improve it would be appreciated:

subsets <-split(test, test$quarter, drop=TRUE)

for (i in 1:length(subsets)) {
  assign(paste0("Q", i), as.data.frame(subsets[[i]]))
}

Q1_ids <- sum(uniqueQ1$id)
Q2_ids <- sum(unique(Q1$id) %in% unique(Q2$id))
Q3_ids <- sum(unique(Q1$id) %in% unique(Q3$id))
Q4_ids <- sum(unique(Q1$id) %in% unique(Q4$id))

Cohort analysis also includes counting the number of new users
per quarter.  As opposed to manually counting the unique user ids that appear per quarter that had not appeared in any previous quarters, I am seeking a programmatic solution.  In this case, the output would be the following list:

> mylist
[[1]]
[1]  7  5  8  3  6 10

[[2]]
[1] 2

[[3]]
[1] 9 4

[[4]]
[1] 1

Any suggestions would be appreciated

Here is the sample data:

> dput(test)
structure(list(id = c(7, 7, 5, 8, 3, 6, 10, 3, 10, 8, 2, 7, 6,
6, 9, 6, 4, 9, 2, 5, 8, 10, 7, 1, 8), quarter = c("Q1", "Q1",
"Q1", "Q1", "Q1", "Q1", "Q1", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2",
"Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q4", "Q4", "Q4", "Q4", "Q4",
"Q4")), .Names = c("id", "quarter"), row.names = c(NA, -25L), class = "data.frame")

Body:
Here's an example using data.table

library(data.table)
setDT(test)[, sum(unique(id) %in% test[quarter=="Q1", id]), by = quarter]

#   quarter V1
#1:      Q1  6
#2:      Q2  5
#3:      Q3  1
#4:      Q4  4

And for the second part of the analysis:

split(test$id[!duplicated(test$id)], test$quarter[!duplicated(test$id)])

#$Q1
#[1]  7  5  8  3  6 10

#$Q2
#[1] 2

#$Q3
#[1] 9 4

#$Q4
#[1] 1

Update based on new requirements:

This seems a bit clunky to me but was the easiest way to keep track of the ids seen as you progress through the quarters.

quarts <- sort(unique(test$quarter))
test$occur <- 1
mat <- dcast.data.table(test, id ~ quarter, value.var = "occur", fun.aggregate = sum)

res <- mat[Q1 >0, lapply(.SD, function(x) sum(x>0)), .SDcols=colnames(mat)[-1]] ##initalize the results with "Q1"
cumMat <- mat$Q1 ##this will keep track of the ids that were seen in previous quarters

for(i in quarts[-1]){                                  ##foreach quarter (except ("Q1") we will count the number of unique ids that are in that quarter and not in any previous quarters.
  res <- rbind(res, mat[cumMat == 0 & get(i) > 0, lapply(.SD, function(x) sum(x>0)), .SDcols=colnames(mat)[-1]])
  cumMat <- rowSums(cbind(cumMat, mat[, i, with = F])) ##update the ids seen
}
t(res)

#   [,1] [,2] [,3] [,4]
#Q1    6    0    0    0
#Q2    5    1    0    0
#Q3    1    1    2    0
#Q4    4    0    0    1


问:
Max Date and Concatenate on separate column
Could someone help me with a solution to this problem? I need to do two things with my data.

I need to get the max date

I need to concatenate the values in the Proj_Use column

My data looks like this:

```<code>Report_Date  |  Program  |  Project  |  Proj_Mgr  |  Region  |  Proj_Use

6/5/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU1

6/4/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU2

6/3/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU3

6/5/2017  |  PG2  |  Prj02  |  Monica  |  West  | PU4

6/4/2017  |  PG2  |  Prj02  |  Monica  |  West  |  PU5

6/3/2017  |  PG2  |  Prj02  |  Monica  |  West  |  PU6
</code>```

My results should look like this:

```<code>Report_Date Program Project Proj_MgrRegion  Proj_Use

6/5/2017 | PG1 | Prj01 | Anne | East | PU1, PU2, PU3

6/5/2017 | PG2 | Prj02 | Monica | West | PU4, PU5, PU6
</code>```

I can get the max date by using the following code:

```<code>    SELECT   t.[Report_Date]
        ,t.[Program]
        ,t.[Project]
        ,t.[Proj_Mgr]
        ,t.[Region]
        ,t.[Proj_Use]
    FROM project_table t
    JOIN
    (SELECT MAX([Report_Date]) as [Report Date], [Program]
        FROM project_table
        GROUP BY [Program]) max
        ON t.[Report_Date] = max.[Report Date]
        AND t.[Program] = max.[Program]
</code>```

I searched on the forum for a way to concatenate and found the group_concat method for sql server. I've tried to add that to the code and came up with this:

```<code>    SELECT   t.[Report_Date]
        ,t.[Program]
        ,t.[Project]
        ,t.[Proj_Mgr]
        ,t.[Region]
        ,STUFF ((
            SELECT ',' + t.[Proj_Use]
                FROM project_table
                FOR XML PATH ('')), 1,1, '') AS [Project Use]
    FROM project_table t
    JOIN
    (SELECT MAX([Report_Date]) as [Report Date], [Program]
        FROM project_table
        GROUP BY [Program]) max
        ON t.[Report_Date] = max.[Report Date]
        AND t.[Program] = max.[Program]
</code>```

When I tried to run this using my real data the query ran for over 20 minutes without producing results.

Can anyone let me know if I've structured this query correctly or if there is a better way to get the results I'm looking for?

Thanks for your help.

答:
One big thing you are missing is the where clause inside your subquery.

Using the stuff() with select ... for xml path ('') method of string concatenation.

Try this:

```<code>select
    ReportDate = max(t.Report_Date)
  , t.Program
  , t.Project
  , t.Proj_Mgr
  , t.Region
  , [Project_Use] = stuff((
   select ',' + i.Proj_Use
   from project_table i
   where i.Program = t.Program
     and i.Project = t.Project
     and i.Proj_Mgr = t.Proj_Mgr
     and i.Region = t.Region
   for xml path(''), type).value('.','nvarchar(max)')
   , 1, 1, '')
from project_table t
group by t.Program, t.Project, t.Proj_Mgr, t.Region
</code>```

rextester demo: http://rextester.com/SEBKZD40038

returns:

```<code>+------------+---------+---------+----------+--------+-------------+
| ReportDate | Program | Project | Proj_Mgr | Region | Project_Use |
+------------+---------+---------+----------+--------+-------------+
| 2017-06-04 | pg1     | Prj01   | Anne     | East   | pu1,pu2,pu3 |
| 2017-06-04 | pg2     | Prj02   | Monica   | West   | pu4,pu5,pu6 |
+------------+---------+---------+----------+--------+-------------+
</code>```

Body:
Could someone help me with a solution to this problem? I need to do two things with my data.

I need to get the max date

I need to concatenate the values in the Proj_Use column

My data looks like this:

Report_Date  |  Program  |  Project  |  Proj_Mgr  |  Region  |  Proj_Use

6/5/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU1

6/4/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU2

6/3/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU3

6/5/2017  |  PG2  |  Prj02  |  Monica  |  West  | PU4

6/4/2017  |  PG2  |  Prj02  |  Monica  |  West  |  PU5

6/3/2017  |  PG2  |  Prj02  |  Monica  |  West  |  PU6

My results should look like this:

Report_Date Program Project Proj_MgrRegion  Proj_Use

6/5/2017 | PG1 | Prj01 | Anne | East | PU1, PU2, PU3

6/5/2017 | PG2 | Prj02 | Monica | West | PU4, PU5, PU6

I can get the max date by using the following code:

    SELECT   t.[Report_Date]
        ,t.[Program]
        ,t.[Project]
        ,t.[Proj_Mgr]
        ,t.[Region]
        ,t.[Proj_Use]
    FROM project_table t
    JOIN
    (SELECT MAX([Report_Date]) as [Report Date], [Program]
        FROM project_table
        GROUP BY [Program]) max
        ON t.[Report_Date] = max.[Report Date]
        AND t.[Program] = max.[Program]

I searched on the forum for a way to concatenate and found the group_concat method for sql server. I've tried to add that to the code and came up with this:

    SELECT   t.[Report_Date]
        ,t.[Program]
        ,t.[Project]
        ,t.[Proj_Mgr]
        ,t.[Region]
        ,STUFF ((
            SELECT ',' + t.[Proj_Use]
                FROM project_table
                FOR XML PATH ('')), 1,1, '') AS [Project Use]
    FROM project_table t
    JOIN
    (SELECT MAX([Report_Date]) as [Report Date], [Program]
        FROM project_table
        GROUP BY [Program]) max
        ON t.[Report_Date] = max.[Report Date]
        AND t.[Program] = max.[Program]

When I tried to run this using my real data the query ran for over 20 minutes without producing results.

Can anyone let me know if I've structured this query correctly or if there is a better way to get the results I'm looking for?

Thanks for your help.

Body:
One big thing you are missing is the where clause inside your subquery.

Using the stuff() with select ... for xml path ('') method of string concatenation.

Try this:

select
    ReportDate = max(t.Report_Date)
  , t.Program
  , t.Project
  , t.Proj_Mgr
  , t.Region
  , [Project_Use] = stuff((
   select ',' + i.Proj_Use
   from project_table i
   where i.Program = t.Program
     and i.Project = t.Project
     and i.Proj_Mgr = t.Proj_Mgr
     and i.Region = t.Region
   for xml path(''), type).value('.','nvarchar(max)')
   , 1, 1, '')
from project_table t
group by t.Program, t.Project, t.Proj_Mgr, t.Region

rextester demo: http://rextester.com/SEBKZD40038

returns:

+------------+---------+---------+----------+--------+-------------+
| ReportDate | Program | Project | Proj_Mgr | Region | Project_Use |
+------------+---------+---------+----------+--------+-------------+
| 2017-06-04 | pg1     | Prj01   | Anne     | East   | pu1,pu2,pu3 |
| 2017-06-04 | pg2     | Prj02   | Monica   | West   | pu4,pu5,pu6 |
+------------+---------+---------+----------+--------+-------------+


问:
How Haskell handles parallel computing on a multicore machine/cluster
I'm considering a new language to learn those days to be used in high performance computing on a cluster of computers we have, among those languages, I'm considering Haskell.

I have read some about Haskell, but still have questions about using Haskell in high performance and distributed computing, which the language is known for, but I read some debates about Haskell is not good for those type of systems due to laziness, I can summarize my questions in the following lines:

Haskell uses green threads, which is great for handling big number of concurrent connections, but what happens when one of tasks takes longer than average and blocks the rest, does the whole thread block (Node.js style), forward the next task to another processor/thread (Golang), use reductions technique (Erlang), which kicks the task out of processing context after a pre-determined number of ticks, or else?

In a distributed computing environment, what happens to lazily-evaluated functions, do they have to be forced strict?

If one function/module requires strict evaluation, but it depends on other lazy functions/modules, shall I modify the code of other functions/modules to make them strict as well, or the compiler will handle this to me and force everything in that chain to strict or lazy.

When processing a very large sequence of data, how does Haskell handle parallel processing, is it by following some kind of implicit map-reduce technique, or I have do it by myself.

Is there a clustering abstract in the language, that handles the computing power for me, that automatically forwards the next task to the free processor wherever it is, be it on the same computer or another computer in the same cluster.

How does Haskell ensure fair-share of work is evenly distributed to all the available cores on the same computer or on the available cluster.

答:
Multithreading

As far as SMP parallelism† is concerned, Haskell is very effective. It's not quite automatic, but the parallel library makes it really easy to parallelise just about anything. Because the sparks are so cheap, you can be pretty careless and just ask for lots of parallelism; the runtime will then know what to do.

Unlike in most other languages, it is not a big problem if you have highly branched data structures, tricky dynamic algorithms etc. – thanks to the purely functional paradigm,  parallel Haskell never needs to worry about locks when accessed data is shared.

I think the biggest caveat is memory: GHC's garbage collector is not concurrent, and the functional style is rather allocation-happy.

Apart from that, it's possible to write programs that look like they're parallel, but really don't do any work at all but just start and immediately return because of laziness. Some testing and experience is still necessary. But laziness and parallelism are not incompatible; at least not if you make sure you have big enough “chunks” of strictness in it. And forcing something strict is largely trivial.

Simpler, common parallelism tasks (which could be expressed in a map-reduce manner, or the classic array-vector stuff – the ones which are also easy in many languages) can generally be handled even easier in Haskell with libraries that parallelise the data structures; the best-known of these is repa.

Distributed computing

There has been quite some work on Cloud Haskell, which is basically Erlang in library form. This kind of task is less straightforward: the idea of any explicit message sending is a bit against Haskell's grain, and many aspects of the workflow become more cumbersome if the language is so heavily focused on its strong static typing (which is in Haskell otherwise often a huge bonus that doesn't just improve safety and performance but also makes it easier to write).

I think it's not far off to use Haskell in a distributed concurrent manner, but we can't say it's mature in that role yet. For distributed concurrent tasks, Erlang itself is certainly the way to go.

Clusters

Honestly, Haskell won't help you much at all here. A cluster is of course in principle a special case of a distributed setup, so you could employ Cloud Haskell; but in practice the needs are very different. The HPC world today (and probably quite some time into the future) hinges on MPI, and though there is a bit of existing work on MPI bindings, I haven't found them usable, at least not just like that.

MPI is definitely also quite against Haskell's grain, what with it's FORTRAN-oriented array centrism, weird ways of handling types and so on. But unless you go nuts with Haskell's cool features (though often it is so tempting!) there is no reason you couldn't write typical number-crunching code also in Haskell. The only problem is again support/maturity, but it's a considerable problem; so for cluster computing I'd recommend C++, Python or Julia instead.

An interesting alternative is to generate MPI-parallelised C or C++ code from Haskell. Paraiso is one nice project that does this.

Pipe dreams

I have often though about what could be done to make the distributed computing feasible in idiomatic Haskell. In principle I believe laziness could be a big help there. The model I'd envision is to let all machines compute independently the same program, but make use of the fact that Haskell evaluation has generally no predetermined order. The order would be randomised on each machine. Also the runtime would track how long some computation branch took to complete, and how big the result is. If a result is deemed both expensive and compact enough to warrant it, it would then be broadcast to the other nodes, together with some suitable hash that would allow them to shortcut that computation.

Such a system would never be quite as efficient as a hand-optimised MPI application, but it could at least offer the same asymptotics in many cases. And it could handle vastly more complex algorithms with ease.

But again, that's totally just my vague hopes for the not-so-near future.

†You said concurrency (which isn't so much about computation as about interaction), but it seems your question is in essence about pure computations?


问:
How to get element attribute value in webdriverio using typescript with await syntax?
Subj.

More info:

The following code examples does not compile:

```<code>let id = (await browser.element(selector)).getAttribute('id');
</code>```

TSError: ⨯ Unable to compile TypeScript ...: Property 'getAttribute'
  does not exist on type 'RawResult'. (2339)

```<code>let id = (await browser.element(selector).getAttribute('id'));
</code>```

TSError: ⨯ Unable to compile TypeScript ...: Type of 'await' operand
  must either be a valid promise or must not contain a callable 'then'
  member. (1320)

答:
I don't really know why but the webdriverio definitions declares an object that looks like a promise but is called Client.

In TypeScript you can await functions that return a Promise. You can wrap the webdriverio API using your own functions and promises:

```<code>import * as webdriverio from "webdriverio";

function getTitleAsync(url: string) {
    return new Promise((resolve, reject) => {
        const options = { desiredCapabilities: { browserName: "chrome" } };
        const client = webdriverio.remote(options);
        client
            .init()
            .url(url)
            .getTitle()
            .then(function (title) {
                resolve(title);
            })
            .end();
    });
}
</code>```

Then you can await your function:

```<code>(async () => {
    const title = await getTitleAsync("https://duckduckgo.com/");
    console.log(title);
})();
</code>```

Body:
Subj.

More info:

The following code examples does not compile:

let id = (await browser.element(selector)).getAttribute('id');

TSError: ⨯ Unable to compile TypeScript ...: Property 'getAttribute'
  does not exist on type 'RawResult'. (2339)

let id = (await browser.element(selector).getAttribute('id'));

TSError: ⨯ Unable to compile TypeScript ...: Type of 'await' operand
  must either be a valid promise or must not contain a callable 'then'
  member. (1320)

Body:
I don't really know why but the webdriverio definitions declares an object that looks like a promise but is called Client.

In TypeScript you can await functions that return a Promise. You can wrap the webdriverio API using your own functions and promises:

import * as webdriverio from "webdriverio";

function getTitleAsync(url: string) {
    return new Promise((resolve, reject) => {
        const options = { desiredCapabilities: { browserName: "chrome" } };
        const client = webdriverio.remote(options);
        client
            .init()
            .url(url)
            .getTitle()
            .then(function (title) {
                resolve(title);
            })
            .end();
    });
}

Then you can await your function:

(async () => {
    const title = await getTitleAsync("https://duckduckgo.com/");
    console.log(title);
})();


问:
How to show a directory structure(explorer) in tcl tk?
I am not able to add nodes recursively in a treectrl. i.e. I want to show all the directories and files as in an explorer. </p>

<p>Till now, this is what I have. I am adding dummy nodes in each folder currently. </p>

```<code>package require treectrl
treectrl .t -showheader 0 -selectmode single -showroot 0 -yscrollcommand {.y set}
scrollbar .y -ori vert -command ".t yview"
pack .y  -side right -fill y
pack .t  -side right -fill both -expand 1
set columnID [.t column create -text "Column 0"]
.t configure -treecolumn $columnID
.t element create el1 text
.t element create el2 rect -showfocus yes
.t style create s1
.t style elements s1 [list el1 el2]
.t style layout s1 el2 -union el1
.t configure -defaultstyle s1

proc add_node {parent text} {
    set itemID [.t item create -button yes ]
    .t item element configure $itemID 0 el1 -text $text
    .t item collapse $itemID
    .t item lastchild $parent $itemID
    return $itemID
}

set images [glob -nocomplain -directory "D:/Explore" "*"]
for {set i 0} {$i<=[llength $images]} {incr i} {
    set root [lsearch $images [lindex $images $i]]
    add_node [add_node [add_node root [list directory [file tail [lindex $images $i]]]] dummy] dummy2
}
</code>```

答:
First of all, you need to use option -tipes of glob command. This way you can separate directories and files. And second, you need recursion to process nested files and directories.

```<code>package require Tk
package require treectrl
treectrl .t -showheader 0 -selectmode single -showroot 0 -yscrollcommand {.y set}
scrollbar .y -ori vert -command ".t yview"
pack .y  -side right -fill y
pack .t  -side right -fill both -expand 1
set columnID [.t column create -text "Column 0"]
.t configure -treecolumn $columnID
.t element create el1 text
.t element create el2 rect -showfocus yes
.t style create s1
.t style elements s1 [list el1 el2]
.t style layout s1 el2 -union el1
.t configure -defaultstyle s1

proc add_node {parent text button} {
    set itemID [.t item create -button $button ]
    .t item element configure $itemID 0 el1 -text $text
    .t item collapse $itemID
    .t item lastchild $parent $itemID
    return $itemID
}

proc add_directory {path parent} {
  set directory_list [glob -nocomplain -types d -directory $path "*"]
  foreach directory $directory_list {
    set n [add_node $parent [file tail $directory] yes]
    add_directory $directory $n
  }
  set files_list [glob -nocomplain -types f -directory $path "*"]
  foreach file $files_list {
    set n [add_node $parent [file tail $file] no]
  }
}

add_directory "D:/Explore" root
</code>```

Body:
I am not able to add nodes recursively in a treectrl. i.e. I want to show all the directories and files as in an explorer.

Till now, this is what I have. I am adding dummy nodes in each folder currently.

package require treectrl
treectrl .t -showheader 0 -selectmode single -showroot 0 -yscrollcommand {.y set}
scrollbar .y -ori vert -command ".t yview"
pack .y  -side right -fill y
pack .t  -side right -fill both -expand 1
set columnID [.t column create -text "Column 0"]
.t configure -treecolumn $columnID
.t element create el1 text
.t element create el2 rect -showfocus yes
.t style create s1
.t style elements s1 [list el1 el2]
.t style layout s1 el2 -union el1
.t configure -defaultstyle s1

proc add_node {parent text} {
    set itemID [.t item create -button yes ]
    .t item element configure $itemID 0 el1 -text $text
    .t item collapse $itemID
    .t item lastchild $parent $itemID
    return $itemID
}

set images [glob -nocomplain -directory "D:/Explore" "*"]
for {set i 0} {$i<=[llength $images]} {incr i} {
    set root [lsearch $images [lindex $images $i]]
    add_node [add_node [add_node root [list directory [file tail [lindex $images $i]]]] dummy] dummy2
}

Body:
First of all, you need to use option -tipes of glob command. This way you can separate directories and files. And second, you need recursion to process nested files and directories.

package require Tk
package require treectrl
treectrl .t -showheader 0 -selectmode single -showroot 0 -yscrollcommand {.y set}
scrollbar .y -ori vert -command ".t yview"
pack .y  -side right -fill y
pack .t  -side right -fill both -expand 1
set columnID [.t column create -text "Column 0"]
.t configure -treecolumn $columnID
.t element create el1 text
.t element create el2 rect -showfocus yes
.t style create s1
.t style elements s1 [list el1 el2]
.t style layout s1 el2 -union el1
.t configure -defaultstyle s1

proc add_node {parent text button} {
    set itemID [.t item create -button $button ]
    .t item element configure $itemID 0 el1 -text $text
    .t item collapse $itemID
    .t item lastchild $parent $itemID
    return $itemID
}

proc add_directory {path parent} {
  set directory_list [glob -nocomplain -types d -directory $path "*"]
  foreach directory $directory_list {
    set n [add_node $parent [file tail $directory] yes]
    add_directory $directory $n
  }
  set files_list [glob -nocomplain -types f -directory $path "*"]
  foreach file $files_list {
    set n [add_node $parent [file tail $file] no]
  }
}

add_directory "D:/Explore" root


问:
using ajax with javascript to put in database
I'm trying to put the infos into my database, for that, this code is in the connexion.jsp which is a page that ask to log with facebook.
the code is supposed to be called into the Controller which is a file in java, and go into the databse.

So my problem is the fact that the code $.ajax doesn't seems to work. It doesn't give a success or error window.alert with or without function(){}.
I might have missed some information about ajax, but i can't find more info about my error.

            function fbLogin() {
                FB.login(function (response) {
                    if (response.authResponse) {
                        // Get and display the user profile data
                        getFbUserData();
                        $.ajax({
                            method: 'post',
                            url: '/hello/facebookconnection',
                            first_name: response.first_name,
                            last_name: response.last_name,
                            email: response.email,
                            success:
                            //success: function(){
                                window.alert('Sent User data!')//;}
                            ,
                            error:
                                window.alert('Error in sending ajax data')
                        });
                    else {
                        document.getElementById('status').innerHTML = 'User cancelled';
                    }
                }, {scope: 'email'});
            }```

答:
```<code> function fbLogin() {
            FB.login(function (response) {
                if (response.authResponse) {
                    // Get and display the user profile data
                    getFbUserData();
                    $.ajax({
                        method: 'post',
                        url: '/hello/facebookconnection',
                        first_name: response.first_name,
                        last_name: response.last_name,
                        email: response.email,
                        success:
                        //success: function(){
                            window.alert('Sent User data!')//;}
                        ,
                        error:
                            window.alert('Error in sending ajax data')
                    });
                } else {
                    document.getElementById('status').innerHTML = 'User cancelled';
                }
            }, {scope: 'email'});
        }
</code>```

<p>You have a syntax error in front of your else. </p>

Body:
 function fbLogin() {
            FB.login(function (response) {
                if (response.authResponse) {
                    // Get and display the user profile data
                    getFbUserData();
                    $.ajax({
                        method: 'post',
                        url: '/hello/facebookconnection',
                        first_name: response.first_name,
                        last_name: response.last_name,
                        email: response.email,
                        success:
                        //success: function(){
                            window.alert('Sent User data!')//;}
                        ,
                        error:
                            window.alert('Error in sending ajax data')
                    });
                } else {
                    document.getElementById('status').innerHTML = 'User cancelled';
                }
            }, {scope: 'email'});
        }

You have a syntax error in front of your else.


问:
How to configure Mongodb with Laravel 5.4?
I tried to install "jenssegers/laravel-mongodb" from git
https://github.com/jenssegers/laravel-mongodb
and all the configuration has been done. but how can I know that my Laravel project successfully connected to MongoDB database?</p>

答:
Have you installed mongo? if not, here's a guide:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

if you want to try if it works, simply try to save something to the db using eloquent.

Just remember that in your model, extend "Jenssegers\Mongodb\Eloquent\Model" instead of "Illuminate\Database\Eloquent\Model"


问:
Using factory girl to create HABTM association
I've been trying now for hours to get factorygirl to create two factories - one for users, one for organizations.

But I don't seem to understand how I can reflect a 'has_and_belongs_to_many' relationship in factories, as soon as I try to create an organization and associate it with an admin user, I run into various error messages (depending on the approach I use).
My model seems to be working fine, my seed file populates the dev DB and all the associations are created.

Right now my files look like this:

user factory

```<code>FactoryGirl.define do

  factory :user do
    email 'example@example.com'
    password 'password'
    password_confirmation 'password'
    after(:create) {|user| user.add_role(:user)}

    factory :owner do
      after(:create) {|user| user.add_role(:owner)}
    end

    factory :admin do
      after(:create) {|user| user.add_role(:admin)}
    end

    factory :superadmin do
      after(:create) {|user| user.add_role(:superadmin)}
    end
  end
end
</code>```

Organization factory

```<code>FactoryGirl.define do

  factory :organization do |f|
    f.name "example"
    f.website "www.aquarterit.com"
    f.association :users, :factory => :admin
  end
end
</code>```

in my specs I test this:

```<code>describe Organization do

  it "has a valid factory" do
    FactoryGirl.create(:organization).should be_valid
  end

  it "is invalid without a name" do
    FactoryGirl.build(:organization, name: nil).should_not be_valid
  end

  it "is associated with at least one admin user" do
    FactoryGirl.create(:organization)
    it { should have_and_belong_to_many(:users)}
  end

end
</code>```

all three tests are failing, here are the error message:

```<code>1) Organization has a valid factory
     Failure/Error: FactoryGirl.create(:organization).should be_valid
     NoMethodError:
       undefined method `each' for #<User:0x007fadbefda688>
     # ./spec/models/organization_spec.rb:7:in `block (2 levels) in <top (required)>'

  2) Organization is invalid without a name
     Failure/Error: FactoryGirl.build(:organization, name: nil).should_not be_valid
     NoMethodError:
       undefined method `each' for #<User:0x007fadc29406c0>
     # ./spec/models/organization_spec.rb:11:in `block (2 levels) in <top (required)>'

  3) Organization is associated with at least one admin user
     Failure/Error: organization = FactoryGirl.create(:organization)
     NoMethodError:
       undefined method `each' for #<User:0x007fadc2a3bf20>
     # ./spec/models/organization_spec.rb:15:in `block (2 levels) in <top (required)>'
</code>```

Any help is as always very much appreciated!

Update

In theory the same thing that works for assigning roles to the user should work for assigning an admin to the organization. But if I change organizations.rb to

```<code>FactoryGirl.define do

  factory :organization do
    name "example"
    website "www.aquarterit.com"
    after(:create) {|organization| organization.add_user(:admin)}
  end
end
</code>```

I get following error (I do have gem shoulda installed):

```<code>1) Organization is associated with at least one admin user
     Failure/Error: it { should have_and_belong_to_many(:users)}
     NoMethodError:
       undefined method `it' for #<RSpec::Core::ExampleGroup::Nested_1:0x007ff2395f9000>
     # ./spec/models/organization_spec.rb:16:in `block (2 levels) in <top (required)>'
</code>```

答:
I do it this way, questions and tests have a HABTM relationship so:

```<code>FactoryGirl.define do
  factory :question do
   question  'Some stupid question'
   user nil
   factory :question_with_test do
     # factory_girl's dynamic attributes, ignore it and pass it to evaluator
     transient do
       test nil
     end

     after(:create) do |question, evaluator|
       create(:question_tests, question: question, test: evaluator.test)
     end
   end
  end
end
</code>```

Now I can create a question with HABTM to the Test model:

```<code>let(:test)             { FactoryGirl.create :test, user: user }
let(:question_test_1)  { FactoryGirl.create :question_with_test, user: user, test: test }
</code>```

The question_tests factory is very basic:

```<code>FactoryGirl.define do
  factory :question_tests, class: 'QuestionTest' do
    question
    test
  end
end
</code>```

Body:
I've been trying now for hours to get factorygirl to create two factories - one for users, one for organizations.

But I don't seem to understand how I can reflect a 'has_and_belongs_to_many' relationship in factories, as soon as I try to create an organization and associate it with an admin user, I run into various error messages (depending on the approach I use).
My model seems to be working fine, my seed file populates the dev DB and all the associations are created.

Right now my files look like this:

user factory

FactoryGirl.define do

  factory :user do
    email 'example@example.com'
    password 'password'
    password_confirmation 'password'
    after(:create) {|user| user.add_role(:user)}

    factory :owner do
      after(:create) {|user| user.add_role(:owner)}
    end

    factory :admin do
      after(:create) {|user| user.add_role(:admin)}
    end

    factory :superadmin do
      after(:create) {|user| user.add_role(:superadmin)}
    end
  end
end

Organization factory

FactoryGirl.define do

  factory :organization do |f|
    f.name "example"
    f.website "www.aquarterit.com"
    f.association :users, :factory => :admin
  end
end

in my specs I test this:

describe Organization do

  it "has a valid factory" do
    FactoryGirl.create(:organization).should be_valid
  end

  it "is invalid without a name" do
    FactoryGirl.build(:organization, name: nil).should_not be_valid
  end

  it "is associated with at least one admin user" do
    FactoryGirl.create(:organization)
    it { should have_and_belong_to_many(:users)}
  end

end

all three tests are failing, here are the error message:

1) Organization has a valid factory
     Failure/Error: FactoryGirl.create(:organization).should be_valid
     NoMethodError:
       undefined method `each' for #<User:0x007fadbefda688>
     # ./spec/models/organization_spec.rb:7:in `block (2 levels) in <top (required)>'

  2) Organization is invalid without a name
     Failure/Error: FactoryGirl.build(:organization, name: nil).should_not be_valid
     NoMethodError:
       undefined method `each' for #<User:0x007fadc29406c0>
     # ./spec/models/organization_spec.rb:11:in `block (2 levels) in <top (required)>'

  3) Organization is associated with at least one admin user
     Failure/Error: organization = FactoryGirl.create(:organization)
     NoMethodError:
       undefined method `each' for #<User:0x007fadc2a3bf20>
     # ./spec/models/organization_spec.rb:15:in `block (2 levels) in <top (required)>'

Any help is as always very much appreciated!

Update

In theory the same thing that works for assigning roles to the user should work for assigning an admin to the organization. But if I change organizations.rb to

FactoryGirl.define do

  factory :organization do
    name "example"
    website "www.aquarterit.com"
    after(:create) {|organization| organization.add_user(:admin)}
  end
end

I get following error (I do have gem shoulda installed):

1) Organization is associated with at least one admin user
     Failure/Error: it { should have_and_belong_to_many(:users)}
     NoMethodError:
       undefined method `it' for #<RSpec::Core::ExampleGroup::Nested_1:0x007ff2395f9000>
     # ./spec/models/organization_spec.rb:16:in `block (2 levels) in <top (required)>'

Body:
I do it this way, questions and tests have a HABTM relationship so:

FactoryGirl.define do
  factory :question do
   question  'Some stupid question'
   user nil
   factory :question_with_test do
     # factory_girl's dynamic attributes, ignore it and pass it to evaluator
     transient do
       test nil
     end

     after(:create) do |question, evaluator|
       create(:question_tests, question: question, test: evaluator.test)
     end
   end
  end
end

Now I can create a question with HABTM to the Test model:

let(:test)             { FactoryGirl.create :test, user: user }
let(:question_test_1)  { FactoryGirl.create :question_with_test, user: user, test: test }

The question_tests factory is very basic:

FactoryGirl.define do
  factory :question_tests, class: 'QuestionTest' do
    question
    test
  end
end