<commit_before>from urllib import urlopen
from sys import stdin
import Axon

"""
Set of components to answer queries about the weather in
York over the last few years :)
*See: http://www.amp.york.ac.uk/external/weather/newdata.html

TODO: add caching component
"""

class GetData(Axon.Component.component):
    """
    Connects to weather data source and formats retrieved data
    """
    
    Inboxes = { "inbox": "incoming data requests of (day, month, year)",
                        "control": "receive shutdown messages"}
    Outboxes = { "outbox": "outgoing data retrieved" }
    
    def __init__(self):
        super(GetData, self).__init__()
        self.baseaddr = "http://www.amp.york.ac.uk/external/weather/"
    
    def getFile(self, month, year):
        y, m = str(year).zfill(2)[-2:], str(month).zfill(2)[-2:] # check format of month and year
        return urlopen(self.baseaddr + y + m + ".txt").readlines() # return list of lines
    
    # select relevant day from list of file lines
    def format(self, textlines, dayrow):
        while not textlines[0][0].isdigit():
            textlines.pop(0)
        data = textlines[dayrow-1].lstrip('0123456789/')
        return data.split() # return line as list of (string) figures
    
    def main(self):
        while True:
            if self.dataReady("control"): # handle shutdown
                self.stop()
            elif self.dataReady(): # handle data requests
                d, m, y = self.recv()
                msg = self.format( self.getFile(m, y) , int(d))
                self.send(msg)
            yield 1
            

class Query(Axon.Component.component):
    """
    Answers queries by requesting data and constructing response
    """
    
    Inboxes = { "requests": "incoming data requests of (day, month, year, v, st)",
                        "data": "incoming data list from files",
                        "format": "loopback for handling data",
                        "control": "receive shutdown messages"}
    Outboxes = { "queries": "outgoing file requests of (day, month, year)",
                           "answers": "outgoing answers to data requests",
                           "loopback": "for passing back format information",
                           "signal": "send shutdown messages"}
    
    def __init__(self):
        super(Query, self).__init__()
        self.columns = ['tempavg', 'tempmax', 'tempmin', 'windavg', 'windmax',
                                 'presmax', 'presmin', 'raintot']
        self.units = [' degrees C', ' degrees C', ' degrees C', 'mph', 'mph',
                             ' mb', ' mb', 'mm']
        self.nodata = ["data not available"]
    
    # formatting functions for answering queries
    def fulllist(self, data):
        dat = zip([v+': ' for v in self.columns], data, self.units)
        return [s+t+u for s, t, u in dat]<commit_msg>This change to a threaded component breaks this code.<commit_after>from urllib import urlopen
from sys import stdin
import Axon

"""
Set of components to answer queries about the weather in
York over the last few years :)
*See: http://www.amp.york.ac.uk/external/weather/newdata.html

TODO: add caching component
"""

class GetData(Axon.ThreadedComponent.threadedcomponent):
    """
    Connects to weather data source and formats retrieved data
    """
    
    Inboxes = { "inbox": "incoming data requests of (day, month, year)",
                        "control": "receive shutdown messages"}
    Outboxes = { "outbox": "outgoing data retrieved" }
    
    def __init__(self):
        super(GetData, self).__init__()
        self.baseaddr = "http://www.amp.york.ac.uk/external/weather/"
    
    def getFile(self, month, year):
        y, m = str(year).zfill(2)[-2:], str(month).zfill(2)[-2:] # check format of month and year
        return urlopen(self.baseaddr + y + m + ".txt").readlines() # return list of lines
    
    # select relevant day from list of file lines
    def format(self, textlines, dayrow):
        while not textlines[0][0].isdigit():
            textlines.pop(0)
        data = textlines[dayrow-1].lstrip('0123456789/')
        return data.split() # return line as list of (string) figures
    
    def main(self):
        while True:
            if self.dataReady("control"): # handle shutdown
                self.stop()
            elif self.dataReady(): # handle data requests
                d, m, y = self.recv()
                msg = self.format( self.getFile(m, y) , int(d))
                self.send(msg)
            

class Query(Axon.Component.component):
    """
    Answers queries by requesting data and constructing response
    """
    
    Inboxes = { "requests": "incoming data requests of (day, month, year, v, st)",
                        "data": "incoming data list from files",
                        "format": "loopback for handling data",
                        "control": "receive shutdown messages"}
    Outboxes = { "queries": "outgoing file requests of (day, month, year)",
                           "answers": "outgoing answers to data requests",
                           "loopback": "for passing back format information",
                           "signal": "send shutdown messages"}
    
    def __init__(self):
        super(Query, self).__init__()
        self.columns = ['tempavg', 'tempmax', 'tempmin', 'windavg', 'windmax',
                                 'presmax', 'presmin', 'raintot']
        self.units = [' degrees C', ' degrees C', ' degrees C', 'mph', 'mph',
                             ' mb', ' mb', 'mm']
        self.nodata = ["data not available"]
    
    # formatting functions for answering queries
    def fulllist(self, data):
        dat = zip([v+': ' for v in self.columns], data, self.units)
        return [s+t+u for s, t, u in dat]
{'fraction_non_alphanumeric': 0.12508602890571233, 'fraction_numerical': 0.006366139022711631, 'mean_word_length': 2.09531416400426, 'pattern_counts': {'":': 22, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6169646', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1607, 'n_words': 519}