Пример #1
0
def test():
    global OVERRIDE_PROMPTS 
    OVERRIDE_PROMPTS = True
    # init cursor and connection then clear
    init_db()
    clear_db()

    init_db()
    success = add_entry_db('A002,R051,02-00-00,LEXINGTON AVE,NQR456,BMT,09/19/2015,00:00:00,REGULAR,0005317608,0001797091')
    commit_db()
    assert success == True
    assert len(cursor.execute('SELECT * FROM entries WHERE CA=?', ('A002',)).fetchall()) == 1
    
    # bad format
    success = add_entry_db('A002,R051,02-00-00,LEXINGTON AVE,NQR456,BMT,09/19/2015,00:00:00,REGU')
    assert success == False
    
    # test file
    clear_db()
    init_db()
    file_to_db('test/turnstile_150926.txt')
    num_entries = len(cursor.execute('SELECT * FROM entries').fetchall())
    assert num_entries == 194625 
    
    # test url
    clear_db()
    init_db()
    url_to_db('http://web.mta.info/developers/data/nyct/turnstile/turnstile_150926.txt')
    num_entries = len(cursor.execute('SELECT * FROM entries').fetchall())
    assert num_entries == 194625 

    OVERRIDE_PROMPTS = False

    trace('tests pass')
Пример #2
0
def main():
    dbname = ""
    start_date = datetime(2010, 5, 5)
    end_date = datetime.now()
    date_format = "%Y-%m-%d"

    if (len(sys.argv) == 4):
        dbname = sys.argv[1]
        start_date = datetime.strptime(sys.argv[2], date_format)
        end_date = datetime.strptime(sys.argv[3], date_format)
    else:
        print(
            "Usage: mta_main.py <db name> <start date: YYYY-MM-DD> <end date: YYYY-MM-DD>"
        )
        return

    # init db
    trace('initializing db:', dbname)
    db.init_db(dbname)

    # get links
    links = scraper.get_links_by_date(start_date, end_date)

    # for each link, do url_to_db
    for date, link in links:
        trace('loading file:', link)
        db.url_to_db(link)
Пример #3
0
 def removeLiberty(self, lib):
     try:
         self.liberties.remove(lib)
     except KeyError:
         trace(
             " >>> Warning : tried to remove a liberty that doesn't exist <<<",
             0)
Пример #4
0
def main():
    dbname = ""
    start_date = datetime(2010, 5, 5)
    end_date = datetime.now()
    date_format = "%Y-%m-%d"

    if(len(sys.argv) == 4):
        dbname = sys.argv[1]
        start_date = datetime.strptime(sys.argv[2], date_format)
        end_date = datetime.strptime(sys.argv[3], date_format)
    else:
        print("Usage: mta_main.py <db name> <start date: YYYY-MM-DD> <end date: YYYY-MM-DD>")
        return

    # init db
    trace('initializing db:', dbname)
    db.init_db(dbname)
    
    # get links
    links = scraper.get_links_by_date(start_date, end_date)
    
    # for each link, do url_to_db
    for date,link in links:
        trace('loading file:', link)
        db.url_to_db(link)
Пример #5
0
 def checkConnection(self):
     currentTime = datetime.datetime.now()
     if (currentTime - self.lastPing).total_seconds() > 320:
         trace(
             "No ping received in the last 5 minutes, reconnecting to twitch chat ...",
             0)
         self.initSocket()
Пример #6
0
 def removeGroup(self, group):
     trace("Killing group %d" % group.id, 2)
     for move in group.moves:
         _, enemies, _ = self.adjacent(move, group.color)
         x, y = move
         self.board[x][y] = None
         for enemy in enemies:
             enemy.addLiberty(move)
     self.groups.pop(group.id)
Пример #7
0
def startSabakiCommunication():
    application = tornado.web.Application( [(r'/', WSHandler), ] )
        
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(4257, address="localhost")
    for sock in http_server._sockets.values():
        trace(sock.getsockname(), 2)
        trace("#####", 2)
        
    return comInstance
Пример #8
0
 def generateOverlay(self, variation, user, colors=False):
     trace("Generating Overlay image for variation %s" % variation, 2)
     drawSize = (self.ovBaseImageSize[0] * self.antialias, self.ovBaseImageSize[1] * self.antialias)
     output = PIL.Image.new(mode="RGBA", size=drawSize, color=(0, 0, 0, 0))
     drawContext = PIL.ImageDraw.Draw(output)
     for i in range(len(variation)):
         move, color = variation[i]
         x, y = move
         
         offset = self.antialias * 3
         
         left = self.ovTopleft[0] + self.ovDimensions[0] * x / 19 + offset
         top = self.ovTopleft[1] + self.ovDimensions[1] * y / 19 + offset
         right = self.ovTopleft[0] + self.ovDimensions[0] * (x + 1) / 19 - offset
         bottom = self.ovTopleft[1] + self.ovDimensions[1] * (y + 1) / 19 - offset
         
         # Draw an outlined circle
         if colors:
             if color == COLOR_BLACK:
                 drawContext.ellipse( (left, top, right, bottom), outline="white", fill="white" )
                 drawContext.ellipse( (left + offset, top + offset, right - offset, bottom - offset), outline="black", fill="black" )
             else:
                 drawContext.ellipse( (left, top, right, bottom), outline="black", fill="black" )
                 drawContext.ellipse( (left + offset, top + offset, right - offset, bottom - offset), outline="white", fill="white" )
         else:
             drawContext.ellipse( (left + offset*6, top + offset*6, right - offset*6, bottom - offset*6), outline="black", fill="black" )
             drawContext.ellipse( (left + offset*7, top  + offset*7, right - offset*7, bottom - offset*7), outline=(205,200,60,0), fill=(205,200,60,0) )
         
         # Draw the move number
         moveNumber = str(i+1)
         w, h = drawContext.textsize(moveNumber, font=self.ovFont)
         W = right - left
         H = bottom - top
         textX = left + self.antialias + (W - w) / 2
         textY = top - self.antialias + (H - h) / 2
         drawContext.text( (textX - offset, textY - offset), moveNumber, font=self.ovFont, fill="black")
         drawContext.text( (textX + offset, textY - offset), moveNumber, font=self.ovFont, fill="black")
         drawContext.text( (textX - offset, textY + offset), moveNumber, font=self.ovFont, fill="black")
         drawContext.text( (textX + offset, textY + offset), moveNumber, font=self.ovFont, fill="black")
         drawContext.text( (textX - offset, textY), moveNumber, font=self.ovFont, fill="black")
         drawContext.text( (textX + offset, textY), moveNumber, font=self.ovFont, fill="black")
         drawContext.text( (textX, textY - offset), moveNumber, font=self.ovFont, fill="black")
         drawContext.text( (textX, textY + offset), moveNumber, font=self.ovFont, fill="black")
         drawContext.text( (textX, textY), moveNumber, font=self.ovFont, fill="white")
     
     output = output.resize(self.ovBaseImageSize, PIL.Image.LANCZOS)
     # output.save(self.ovImagePath)
     
     t = self.baseTimeVariation + len(variation) * self.timePerStone
     if self.timer.running:
         self.timer.addImage(output, t)
     else:
         self.timer = OverlayTimer(self)
         self.timer.addImage(output, t)
         self.timer.start()
Пример #9
0
 def toggleCommunication(self):
     if not self.useSabaki:
         return
     if self.communicationActive:
         trace("Pausing updates to sabaki", 0)
         self.comThread.pauseComms()
         self.communicationActive = False
     else:
         trace("Resuming updates to sabaki", 0)
         self.comThread.resumeComms()
         self.communicationActive = True
Пример #10
0
def test():
    open_db('db/mta-2015-09-12.db')
    turnstiles = get_turnstiles()
    assert len(turnstiles) == 4579

    stations = get_stations()
    assert len(stations) == 465

    rows = per_station(stations[0])
    assert len(rows) > 0
    trace('tests pass')
Пример #11
0
def test():
    content = get_site()
    links = get_turnstile_links(content)
    assert len(links) == 282 #legit as of 30 Sept 2015

    r = get_links_by_date(datetime(2015, 8, 1), datetime(2015, 8, 31))
    assert len(r) == 5 # 5 entries for August 2015 

    s = get_links_by_date(datetime(2013, 4, 6), datetime(2013, 4, 27))
    assert len(s) == 4 # test edge case

    trace('test pass')
Пример #12
0
    def __init__(self,parent):
        wx.Panel.__init__(self,parent,-1)

        self.Sizer=wx.BoxSizer(wx.VERTICAL)

        from util import trace
        trace(ExpandoTextCtrl)

        #profile = ExpandoTextCtrl(self,style= wx.TE_MULTILINE|wx.TE_CHARWRAP|wx.TE_PROCESS_ENTER|wx.NO_BORDER|wx.WANTS_CHARS|wx.TE_NOHIDESEL|wx.TE_RICH)
        profile=FormattedInput(self)
        self.Sizer.Add(profile,0)
        self.Layout()
Пример #13
0
def test():
    content = get_site()
    links = get_turnstile_links(content)
    assert len(links) == 282  #legit as of 30 Sept 2015

    r = get_links_by_date(datetime(2015, 8, 1), datetime(2015, 8, 31))
    assert len(r) == 5  # 5 entries for August 2015

    s = get_links_by_date(datetime(2013, 4, 6), datetime(2013, 4, 27))
    assert len(s) == 4  # test edge case

    trace('test pass')
Пример #14
0
 def endProgram(self):
     trace("Ending program", 0)
     self.twitchBot.stop()
     del self.twitchBot
     self.twitchBot = None
     if self.useSabaki:
         self.comThread.closeSabaki()
         self.daemonThread.stop()
         del self.daemonThread
         self.daemonThread = None
         self.comThread.stop()
         del self.comThread
         self.comThread = None
Пример #15
0
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        self.Sizer = wx.BoxSizer(wx.VERTICAL)

        from util import trace

        trace(ExpandoTextCtrl)

        # profile = ExpandoTextCtrl(self,style= wx.TE_MULTILINE|wx.TE_CHARWRAP|wx.TE_PROCESS_ENTER|wx.NO_BORDER|wx.WANTS_CHARS|wx.TE_NOHIDESEL|wx.TE_RICH)
        profile = FormattedInput(self)
        self.Sizer.Add(profile, 0)
        self.Layout()
Пример #16
0
 def __init__(self, capture=None):
     self.groups = {}
     self.nextGroupId = 0
     self.board = []
     for _ in range(19):
         col = []
         for _ in range(19):
             col.append(None)
         self.board.append(col)
     if capture is not None:
         for x in range(19):
             for y in range(19):
                 color = capture[x][y]
                 if color != 0:
                     trace("Adding stone %d at %d - %d" % (color, x, y), 2)
                     self.addStone((x, y), color)
Пример #17
0
def test_util():
    # test conversion
    old = "A022,R022,01-00-01,04-23-10,04:00:00,RECOVR,012277581,004593025,04-23-10,08:00:00,AUD,012277627,004593158,04-23-10,12:00:00,REGULAR,012278037,004593983,04-23-10,16:00:00,REGULAR,012279285,004594519,04-23-10,20:00:00,REGULAR,012281573,004594935"

    converted = convert_format(old)
    assert converted[0] == "A022,R022,01-00-01,NULL,NULL,NULL,04-23-10,04:00:00,RECOVR,012277581,004593025"
    assert converted[2] == "A022,R022,01-00-01,NULL,NULL,NULL,04-23-10,12:00:00,REGULAR,012278037,004593983"
    
    # test insertion of old format into db
    init_db()
    clear_db()
    init_db()
    url_to_db('http://web.mta.info/developers/data/nyct/turnstile/turnstile_120128.txt')
    success_entries = cursor.execute('SELECT COUNT(*) FROM entries').fetchone()[0]
    assert success_entries == 206758

    trace('tests pass')
Пример #18
0
 def getMessages(self):
     # twitch message structure ->   :<user>!<user>@<user>.tmi.twitch.tv PRIVMSG #<channel> :This is a sample message
     messages = []
     data = self.getIRCData()
     if data is None:
         return messages
     trace("Got message %s" % data, 2)
     if "PING :tmi.twitch.tv" in data:
         trace("Sending pong", 3)
         self.ircSend("PONG :tmi.twitch.tv")
     matches = re.findall(
         ":(.+)\!(.+)\@(.+).tmi.twitch.tv PRIVMSG #%s :(.+)$" %
         self.channel, data, re.MULTILINE)
     for match in matches:
         user = match[0]
         content = match[3].rstrip()
         messages.append((content.lower(), user))
     return messages
Пример #19
0
def test():
    global OVERRIDE_PROMPTS
    OVERRIDE_PROMPTS = True
    # init cursor and connection then clear
    init_db()
    clear_db()

    init_db()
    success = add_entry_db(
        'A002,R051,02-00-00,LEXINGTON AVE,NQR456,BMT,09/19/2015,00:00:00,REGULAR,0005317608,0001797091'
    )
    commit_db()
    assert success == True
    assert len(
        cursor.execute('SELECT * FROM entries WHERE CA=?',
                       ('A002', )).fetchall()) == 1

    # bad format
    success = add_entry_db(
        'A002,R051,02-00-00,LEXINGTON AVE,NQR456,BMT,09/19/2015,00:00:00,REGU')
    assert success == False

    # test file
    clear_db()
    init_db()
    file_to_db('test/turnstile_150926.txt')
    num_entries = len(cursor.execute('SELECT * FROM entries').fetchall())
    assert num_entries == 194625

    # test url
    clear_db()
    init_db()
    url_to_db(
        'http://web.mta.info/developers/data/nyct/turnstile/turnstile_150926.txt'
    )
    num_entries = len(cursor.execute('SELECT * FROM entries').fetchall())
    assert num_entries == 194625

    OVERRIDE_PROMPTS = False

    trace('tests pass')
Пример #20
0
    def initSocket(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(2)

        try:
            sock.connect((settings["twitch_host"], settings["twitch_port"]))
        except:
            trace(
                "Cannot connect to server %s:%s" %
                (settings["twitch_host"], settings["twitch_port"]), 0)
            return

        self.socket = sock
        # sock.settimeout(None)

        self.ircSend("PASS %s\r\n" % settings["twitch_bot_oauth"])
        self.ircSend("NICK %s\r\n" % settings["twitch_bot_name"])
        self.ircSend(
            "USER %s 8 * %s\r\n" %
            (settings["twitch_bot_name"], settings["twitch_bot_name"]))

        data = sock.recv(1024)
        if "Login unsuccessful" in data:
            trace(
                "Couldn't login to twitch chat, your bot's password is probably wrong.",
                0)
            return
        else:
            trace("Logged in to twitch chat.", 0)

        self.ircSend("JOIN #%s\r\n" % self.channel)
        self.running = True
        self.lastPing = datetime.datetime.now()
Пример #21
0
def test_util():
    # test conversion
    old = "A022,R022,01-00-01,04-23-10,04:00:00,RECOVR,012277581,004593025,04-23-10,08:00:00,AUD,012277627,004593158,04-23-10,12:00:00,REGULAR,012278037,004593983,04-23-10,16:00:00,REGULAR,012279285,004594519,04-23-10,20:00:00,REGULAR,012281573,004594935"

    converted = convert_format(old)
    assert converted[
        0] == "A022,R022,01-00-01,NULL,NULL,NULL,04-23-10,04:00:00,RECOVR,012277581,004593025"
    assert converted[
        2] == "A022,R022,01-00-01,NULL,NULL,NULL,04-23-10,12:00:00,REGULAR,012278037,004593983"

    # test insertion of old format into db
    init_db()
    clear_db()
    init_db()
    url_to_db(
        'http://web.mta.info/developers/data/nyct/turnstile/turnstile_120128.txt'
    )
    success_entries = cursor.execute(
        'SELECT COUNT(*) FROM entries').fetchone()[0]
    assert success_entries == 206758

    trace('tests pass')
Пример #22
0
def test_std_dev():
    data = """[(69660,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-09 08:22:00","08:22:00","REGULAR",2177371,742200,0,115),
(69661,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-09 12:22:00","12:22:00","REGULAR",2177371,742420,0,220),
(69662,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-09 16:22:00","16:22:00","REGULAR",2177371,742781,0,361),
(69663,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-09 20:22:00","20:22:00","REGULAR",2177371,743341,0,560),
(69664,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-10 00:22:00","00:22:00","REGULAR",2177371,743469,0,128),
(69665,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-10 04:22:00","04:22:00","REGULAR",2177371,743510,0,41),
(69666,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-10 08:22:00","08:22:00","REGULAR",553671722,184576396,551494351,183832886),
(69667,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-10 12:22:00","12:22:00","REGULAR",553672337,184576516,615,120),
(69668,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-10 16:22:00","16:22:00","REGULAR",553673019,184576738,682,222),
(69669,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-10 20:22:00","20:22:00","REGULAR",553673853,184577021,834,283),
(69670,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-11 00:22:00","00:22:00","REGULAR",553674073,184577133,220,112),
(69671,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-11 04:22:00","04:22:00","REGULAR",553674104,184577158,31,25),
(69672,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-11 08:22:00","08:22:00","REGULAR",553674630,184577234,526,76),
(69673,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-11 12:22:00","12:22:00","REGULAR",553675325,184577382,695,148),
(69674,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-11 16:22:00","16:22:00","REGULAR",553676083,184577614,758,232),
(69675,"N203","R195","00-00-03","161 ST-YANKEE","BD4","IND","2015-09-11 20:22:00","20:22:00","REGULAR",553676556,184578274,473,660)]"""

    dataset = eval(data)

    removed = remove_outliers(dataset)
    assert removed == 1
    trace('tests pass')
Пример #23
0
def main():
    numargs = len(sys.argv)
    if numargs < 2 or numargs > 3:
        print('Usage: python mta_cleaner <db_path> <optional: clean>')
        return

    open_db(sys.argv[1])

    if numargs == 2:
        add_columns()

        turnstiles = get_turnstiles()

        count = 0
        for ts in turnstiles:
            rows = per_turnstile(ts)
            crunch_turnstile_rows(rows)
            count += 1
            if count % 100 == 0:
                trace(dt.datetime.now(), count, 'of', len(turnstiles))

    if numargs == 3 and sys.argv[2] == 'clean':
        turnstiles = get_turnstiles()
        remove_outliers_by(turnstiles, per_turnstile)
Пример #24
0
 def run(self):
     trace("Twitch bot start", 1)
     while self.running:
         trace("Twitch bot update", 2)
         messages = self.getMessages()
         for message in messages:
             self.parseMessage(message)
         self.checkConnection()
         time.sleep(self.refreshRate)
     trace("Twitch bot end", 1)
Пример #25
0
def remove_outliers_by(things, per_thing):
    trace('Removing outliers...')
    count = 0
    count_thing = 0
    for ts in things:
        rows = per_thing(ts)
        removed = remove_outliers(rows)

        if len(removed) > 0:
            one = removed[0]
            trace('Removed %d outliers from %s, station %s, id %d, %d %d' %
                  (len(removed), str(ts), one[4], one[0],
                   int(one[index_entries] or 0), int(one[index_exits] or 0)))

            count += 1
        count_thing += 1

        if (count_thing % 100 == 0):
            trace(dt.datetime.now(),
                  'Thing %d of %d' % (count_thing, len(things)))

    trace(dt.datetime.now(), 'Done. Removed %d points' % count)
    connection.commit()
def test_update(collection1, collection2, verbose=False):
    for i in range(1, 10):
        exceptionOne = None
        exceptionTwo = None
        update = gen.random_update(collection1)

        util.trace('debug', '\n========== Update No.', i, '==========')
        util.trace('debug', 'Query:', update['query'])
        util.trace('debug', 'Update:', str(update['update']))
        util.trace('debug', 'Number results from collection: ',
                   gen.count_query_results(collection1, update['query']))
        for item in collection1.find(update['query']):
            util.trace('debug', 'Find Result1:', item)
        for item in collection2.find(update['query']):
            util.trace('debug', 'Find Result2:', item)

        try:
            if verbose:
                all = [x for x in collection1.find(dict())]
                for item in collection1.find(update['query']):
                    print '[{}] Before update doc:{}'.format(
                        type(collection1), item)
                print 'Before update collection1 size: ', len(all)
            collection1.update(update['query'],
                               update['update'],
                               upsert=update['upsert'],
                               multi=update['multi'])
        except pymongo.errors.OperationFailure as e:
            exceptionOne = e
        except MongoModelException as e:
            exceptionOne = e
        try:
            if verbose:
                all = [x for x in collection2.find(dict())]
                for item in collection2.find(update['query']):
                    print '[{}]Before update doc:{}'.format(
                        type(collection2), item)
                print 'Before update collection2 size: ', len(all)
            collection2.update(update['query'],
                               update['update'],
                               upsert=update['upsert'],
                               multi=update['multi'])
        except pymongo.errors.OperationFailure as e:
            exceptionTwo = e
        except MongoModelException as e:
            exceptionTwo = e

        if (exceptionOne is None and exceptionTwo is None):
            # happy case, proceed to consistency check
            pass
        elif exceptionOne is not None and exceptionTwo is not None:
            # or (exceptionOne is not None and exceptionTwo is not None and exceptionOne.code == exceptionTwo.code)):
            # TODO re-enable the exact error check.
            # TODO re-enable consistency check when failure happened
            return (True, True)
        else:
            print 'Unmatched result: '
            print type(exceptionOne), ': ', str(exceptionOne)
            print type(exceptionTwo), ': ', str(exceptionTwo)
            ignored_exception_check(exceptionOne)
            ignored_exception_check(exceptionTwo)
            return (False, False)

        if not check_query(dict(), collection1, collection2):
            return (False, False)

    return (True, False)
def check_query(query,
                collection1,
                collection2,
                projection=None,
                sort=None,
                limit=0,
                skip=0):
    util.trace('debug', '\n==================================================')
    util.trace('debug', 'checking consistency bettwen the two collections...')
    util.trace('debug', 'query:', query)
    util.trace('debug', 'sort:', sort)
    util.trace('debug', 'limit:', limit)
    util.trace('debug', 'skip:', skip)

    exception_msg = list()

    ret1 = get_result(query, collection1, projection, sort, limit, skip,
                      exception_msg)
    ret2 = get_result(query, collection2, projection, sort, limit, skip,
                      exception_msg)
    if len(exception_msg) == 1:
        print '\033[91m\n', exception_msg[0], '\033[0m'
        return False

    global total_queries
    total_queries += 1
    if len(ret1) == 0 and len(ret2) == 0:
        global zero_resp_queries
        zero_resp_queries += 1
        # print 'Zero responses so far: {}/{}'.format(zero_resp_queries, total_queries)

    if isinstance(ret1, util.MongoModelNondeterministicList):
        return ret1.compare(ret2)
    elif isinstance(ret2, util.MongoModelNondeterministicList):
        return ret2.compare(ret1)

    i = 0
    try:
        for i in range(0, max(len(ret1), len(ret2))):
            assert ret1[i] == ret2[i]
        return True
    except AssertionError:
        print '\nQuery results didn\'t match at index %d!' % i
        print 'Query: %r' % query
        print 'Projection: %r' % projection
        print '\n  %s' % format_result(collection1, ret1, i)
        print '  %s\n' % format_result(collection2, ret2, i)

        diff_results(collection1, ret1, collection2, ret2)

        # for i in range(0, max(len(ret1), len(ret2))):
        #    print '\n%d: %s' % (i, format_result(collection1, ret1, i))
        #    print '%d: %s' % (i, format_result(collection2, ret2, i))

        return False
    except IndexError:
        print 'Query results didn\'t match!'
        print 'Query: %r' % query
        print 'Projection: %r' % projection

        print '\n  %s' % format_result(collection1, ret1, i)
        print '  %s\n' % format_result(collection2, ret2, i)

        diff_results(collection1, ret1, collection2, ret2)

        # for i in range(0, max(len(ret1), len(ret2))):
        #    print '\n%d: %s' % (i, formatResult(collection1, ret1, i))
        #    print '%d: %s' % (i, formatResult(collection2, ret2, i))

        return False
Пример #28
0
 def getIRCData(self):
     try:
         data = self.socket.recv(self.socketBufferSize)
         return data
     except socket.timeout:
         trace("No message from twitch", 2)
Пример #29
0
    def NavigateComplete2(self, this, pDisp, URL, *a):
        self.OnNav(URL[0])

    def DocumentComplete(self, this, pDisp, URL, *a):
        self.OnDoc(URL[0])


if __name__ == '__main__':
    a = wx.PySimpleApp()
    _ = lambda s: s

    fbSize = (646, 436)
    url = 'http://www.google.com/'

    from util import trace
    trace(IEWindow)

    f = wx.Frame(None, size = fbSize, title = 'ie test')
    ie = IEWindow(f, url = url)

    def ondoc(e):
        print type(e)
        print e
        print e.URL

    ie.Bind(iewin.EVT_DocumentComplete,  ondoc)

    f.Show()
    a.MainLoop()
Пример #30
0
    def parseMessage(self, message):
        content, user = message
        trace("Parsing message %s from %s" % (content, user), 2)
        # Check for game sequence
        coordMatches = re.findall("[a-z][0-9]{1,2}", content)
        trace(coordMatches, 1)
        if len(coordMatches) > 0:
            trace("Found coordinates from twitch chat !", 1)

            forcedColor = False
            # Check if there is a color for the first move
            if re.match("b [a-z][0-9]{1,2}", content):
                firstMove = COLOR_BLACK
                forcedColor = True
            elif re.match("w [a-z][0-9]{1,2}", content):
                firstMove = COLOR_WHITE
                forcedColor = True
            else:
                firstMove = 0
            trace("First player %d" % firstMove, 2)

            # Change the coordinates into (col, row) tuples
            moves = []
            color = self.game.nextPlayer() if firstMove == 0 else firstMove
            for match in coordMatches:
                coords = self.parseCoordinates(match)
                moves.append((coords, color))
                color = otherColor(color)
            trace("Moves %s" % str(moves), 2)

            # Check if the variation has a specific origin in the game tree, then send it to the game state
            variationIndex = None
            hasOrigin = re.match("(move)|(variation) ([0-9]+)", content)
            if hasOrigin:
                origin = re.match("move ([0-9]+)", content)
                if not origin:
                    variation = re.match("variation ([0-9]+)", content)
                    variationNumber = int(variation[0])
                    variationIndex = self.game.expandVariation(
                        moves, variationNumber)
                    trace("Expanding previous variation %d" % variationNumber,
                          1)
                else:
                    moveNumber = int(origin[0])
                    variationIndex = self.game.addVariation(moves, moveNumber)
                    trace(
                        "Creating variation %d from move %d" %
                        (variationIndex, moveNumber), 1)
            else:
                variationIndex = self.game.addVariation(moves)
                trace("Creating variation %d" % variationIndex, 1)

            # if len(moves) == 0 and not hasOrigin:
            # return
            variationSgf, nMoves = self.game.getVariation(variationIndex)
            if self.useSabaki:
                sabakiCom.requestVariation(variationSgf, user, nMoves)
            if self.overlayActive:
                self.variationOverlayGenerator.generateOverlay(
                    moves, user, forcedColor)
Пример #31
0
 def EnumStackFrames(self):
     trace("DebugStackFrameSniffer.EnumStackFrames called")
     return _wrap(EnumDebugStackFrames(self.debugger),
                  axdebug.IID_IEnumDebugStackFrames)
Пример #32
0
 def merge(self, other):
     """ Merge 2 groups together """
     trace("Merging group %d into group %d" % (other.id, self.id), 2)
     self.moves += other.moves
     self.liberties |= other.liberties
Пример #33
0
 def GetPositionOfContext(self, context):
     trace("GetPositionOfContext", context)
     context = unwrap(context)
     return context.offset, context.length
Пример #34
0
 def __init__(self, debugger):
     self.debugger = debugger
     trace("DebugStackFrameSniffer instantiated")
Пример #35
0
 def __init__(self, debugger):
     self.debugger = debugger
     trace("DebugStackFrameSniffer instantiated")
Пример #36
0
        except AttributeError:
            return self.seturl

    #SetPage = iewin.IEHtmlWindow.LoadString

    def SetPage(self, content):
        return self.SetPageSource(content)

    def BeforeLoad(self, event):
        self.OnNav(event.GetURL())

    def StateChanged(self, event):
        if event.GetState() == webkit.WEBKIT_STATE_STOP:
            self.OnDoc(event.GetURL())

if __name__ == '__main__':
    a = wx.PySimpleApp()
    _ = lambda s: s

    fbSize = (646, 436)
    url = 'http://www.google.com/'

    from util import trace
    trace(WebKitWindow)

    f = wx.Frame(None, size = fbSize, title = 'ie test')
    wk = WebKitWindow(f, url = url)

    f.Show()
    a.MainLoop()
Пример #37
0
 def updateGame(self, capture):
     newMoves = []
     potentialCapture = False
     for i in range(19):
         for j in range(19):
             color = capture[i][j]
             if color != self.board[i, j]:
                 if color == 0:
                     potentialCapture = True
                 newMoves.append(((i, j), color))
     if potentialCapture:
         groups = set([])
         addedStones = []
         colors = []
         for pos, color in newMoves:
             if color == 0:
                 groups.add(self.board.getGroupAt(pos))
             else:
                 addedStones.append(pos)
                 colors.append(color)
         for group in groups:
             for lib in group.liberties:
                 if lib not in addedStones:
                     trace(
                         "Liberties %s were never filled" %
                         str(group.liberties), 0)
                     trace(
                         "Warning : couldn't find the order of moves, resetting game",
                         0)
                     self.reset(capture)
                     trace("Game reset complete", 0)
                     return
         newMoves = []
         for i in range(len(addedStones)):
             newMoves.append((addedStones[i], colors[i]))
     if len(newMoves) == 0:
         return
     elif len(newMoves) > 2:
         import time
         time.sleep(2)
         trace(
             "Warning : too many moves were played before last update, resetting game",
             0)
         self.reset(capture)
         trace("Game reset complete", 0)
     elif len(newMoves) == 1:
         pos, color = newMoves[0]
         self.addMove(pos, color)
     else:
         # FIXME check for snapback
         knowWhoPlaysNext = True
         if self.nextToPlay == 0:
             knowWhoPlaysNext = False
         firstMove = newMoves[0]
         secondMove = newMoves[1]
         if self.nextToPlay == secondMove[1]:
             tmp = firstMove
             firstMove = secondMove
             secondMove = tmp
         self.addMove(firstMove[0], firstMove[1])
         self.addMove(secondMove[0], secondMove[1])
         if not knowWhoPlaysNext:
             self.nextToPlay = 0
Пример #38
0
 def createGroup(self, pos, color, libs):
     group = Group(self.nextGroupId, pos, color, libs)
     self.groups[group.id] = group
     trace("Created group %d at %d - %d" % (group.id, pos[0], pos[1]), 2)
     self.nextGroupId += 1
     return group.id
Пример #39
0
def test_update(collections, verbose=False):
    okay = True
    for i in range(1, 10):
        update = gen.random_update(collections[0])

        util.trace('debug', '\n========== Update No.', i, '==========')
        util.trace('debug', 'Query:', update['query'])
        util.trace('debug', 'Update:', str(update['update']))
        util.trace('debug', 'Number results from collection: ',
                   gen.count_query_results(collections[0], update['query']))
        for item in transactional_shim.find(collections[0], update['query']):
            util.trace('debug', 'Find Result0:', item)

        exception = []
        exception_msg = []
        for coll in collections:
            try:
                if verbose:
                    all = [x for x in coll.find(dict())]
                    for item in transactional_shim.find(coll, update['query']):
                        print 'Before update doc:', item
                    print 'Before update coll size: ', len(all)

                transactional_shim.update(coll,
                                          update['query'],
                                          update['update'],
                                          upsert=update['upsert'],
                                          multi=update['multi'])

                if verbose:
                    all = [x for x in coll.find(dict())]
                    for item in coll.find(update['query']):
                        print 'After update doc:', item
                    print 'After update coll size: ', len(all)

            except pymongo.errors.OperationFailure as e:
                exception.append(e)
                exception_msg.append(
                    util.join_n(
                        'Caught PyMongo error while attempting update: %s' %
                        e[0], 'Query: %s' % update['query'],
                        'Update: %s' % update['update'],
                        'Upsert: {0}, Multi: {1}'.format(
                            update['upsert'], update['multi'])))
            except MongoModelException as e:
                exception.append(e)
                exception_msg.append(
                    util.join_n('Caught MongoModel error. Offending update(',
                                str(update['query']), str(update['update']),
                                str(update['upsert']), str(update['multi']),
                                ')'))

        if len(exception_msg) == 1:
            print 'Update: ' + str(update['update'])
            print '\033[91m', exception[0], '\033[0m'
            print '\033[91m', exception_msg[0], '\033[0m'
            return False

        if not check_query(dict(), collections[0], collections[1]):
            print 'Update: ' + str(update['update'])
            return False

    return okay
Пример #40
0
    b = wx.Button(f, -1, "vars")

    f.Sizer = sz = wx.BoxSizer(wx.VERTICAL)
    sz.Add(b)

    f.docker = Docker(f)

    def printvars(e):
        from pprint import pprint

        pprint(vars(f.docker))

    b.Bind(wx.EVT_BUTTON, printvars)

    trace(Docker)
    f.Bind(wx.EVT_LEFT_DOWN, lambda e: sys.stdout.write("docked: %s\n" % f.docker.docked))
    f.docker.Enabled = False

    p = wx.Panel(f2)
    sz = p.Sizer = wx.BoxSizer(wx.VERTICAL)

    text = wx.StaticText(p, -1, "Not Docked")
    f.docker.OnDock += lambda docked: text.SetLabel("Docked!" if docked else "Not Docked")

    b = wx.CheckBox(p, -1, "&Docking")
    c = wx.CheckBox(p, -1, "&Auto Hiding")
    c.Enabled = False

    def on_dock(e):
        c.Enabled = f.docker.Enabled = e.IsChecked()
Пример #41
0
 def EnumStackFrames(self):
     trace("DebugStackFrameSniffer.EnumStackFrames called")
     return _wrap(EnumDebugStackFrames(self.debugger), axdebug.IID_IEnumDebugStackFrames)