当前位置: 首页>>代码示例>>Python>>正文


Python learn.Main类代码示例

本文整理汇总了Python中learn.Main的典型用法代码示例。如果您正苦于以下问题:Python Main类的具体用法?Python Main怎么用?Python Main使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Main类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Main

class Main(Module):

    pattern = re.compile(u"^\s*(fc|forecast|weather|ws|pws)(?:\s+(.*)$)?")
    require_addressing = True
    help = u"fc|forecast, ws|weather, pws [zipcode|city,state|city,country|pws] - look up weather forecast/conditions"
    error = u"Couldn't find that place, maybe a bomb dropped on it"

    def init(self):
        colorlib = self.madcow.colorlib
        self.weather = Weather(colorlib, self.log)
        self.learn = Learn(madcow=self.madcow)

    def response(self, nick, args, kwargs):
        cmd = args[0]
        query = args[1]

        if not query:
            location = self.learn.lookup("location", nick)
        elif query.startswith("@"):
            location = self.learn.lookup("location", query[1:])
        else:
            location = query
        if location:
            if cmd in ("fc", "forecast"):
                message = self.weather.forecast(location)
            elif cmd in ("weather", "ws"):
                message = self.weather.official_station(location)
            elif cmd == "pws":
                message = self.weather.personal_station(location)
        else:
            message = u"I couldn't look that up"
        return u"%s: %s" % (nick, message)
开发者ID:Jzarecta,项目名称:madcow,代码行数:32,代码来源:weather.py

示例2: Main

class Main(Module):
    pattern = re.compile('^\s*(?:fc|forecast|weather)(?:\s+(.*)$)?')
    require_addressing = True
    help = 'fc [location] - look up weather forecast'

    def __init__(self, madcow=None):
        self.weather = Weather()
        try:
            self.learn = Learn(madcow=madcow)
        except:
            self.learn = None

    def response(self, nick, args, kwargs):

        try:
            args = args[0]
        except:
            args = None

        if args is None or args == '' and self.learn:
            query = self.learn.lookup('location', nick)
        elif args.startswith('@') and self.learn:
            query = self.learn.lookup('location', args[1:])
        else:
            query = args

        if query is None or query == '':
            return '%s: unknown nick. %s' % (nick, __usage__)

        try:
            return '%s: %s' % (nick, self.weather.forecast(query))
        except Exception, e:
            log.warn('error in %s: %s' % (self.__module__, e))
            log.exception(e)
            return "Couldn't find that place, maybe a bomb dropped on it"
开发者ID:gipi,项目名称:Richie,代码行数:35,代码来源:weather.py

示例3: Main

class Main(Module):

    pattern = re.compile(u'^\s*(?:fc|forecast|weather)(?:\s+(.*)$)?')
    require_addressing = True
    help = u'fc [location] - look up weather forecast'
    error = u"Couldn't find that place, maybe a bomb dropped on it"

    def init(self):
        colorlib = self.madcow.colorlib
        self.weather = Weather(colorlib)
        self.learn = Learn(madcow=self.madcow)

    def response(self, nick, args, kwargs):
        query = args[0]
        if not query:
            location = self.learn.lookup('location', nick)
        elif query.startswith('@'):
            location = self.learn.lookup('location', query[1:])
        else:
            location = query
        if location:
            message = self.weather.forecast(location)
        else:
            message = u"I couldn't look that up"
        return u'%s: %s' % (nick, message)
开发者ID:Spacexplosion,项目名称:Quaking-Mad-Cow,代码行数:25,代码来源:weather.py

示例4: Main

class Main(Module):
    pattern = re.compile(u'^\s*(nb|notes?)(?:\s+(.*)$)?')
    require_addressing = True
    dbname = u'note'
    help = u'notes|nb <nick>                        show notes associated to a nick (staff only)\n\
notes|nb <nick> <note>                 add a note to a nick (staff only)'
    
    def init(self):
        self.learn = Learn(madcow=self.madcow)
        self.staff = Staff(madcow=self.madcow)
        
    def set(self, nick, target_nick, note):
        d = datetime.now()
        # append notes, don't overwrite
        old_notes = self.learn.lookup(self.dbname, target_nick) 
        notes = old_notes if old_notes else ""
        self.learn.set(self.dbname, target_nick.lower(), notes + '\n[' + d.strftime("%D") + '] [' + nick + '] ' + note)
        
    def get(self, target_nick):
        return self.learn.lookup(self.dbname, target_nick)
        
    def response(self, nick, args, kwargs):
        cmd = args[0]
        target_nick = False
        note = False
        params = []
        if args[1]:
            params = args[1].partition(' ')
        
        try:
            target_nick = params[0]
        except IndexError:
            target_nick = False
            
        try:
            note = params[2]
        except IndexError:
            note = False

        """ if nick passed, set user as staff """
        if target_nick and (self.staff.is_staff(nick) or settings.OWNER_NICK == nick):
            if note:
                self.set(nick=nick, target_nick=target_nick, note=note)
                return u'%s: Note added to %s\'s record.' % (nick, target_nick)
            else:
                notes = self.get(target_nick=target_nick)
                if notes:
                    return u'%s: Staff notes on %s\n%s' % (nick, target_nick, notes)
                else:
                    return u'%s: No notes found for %s' % (nick, target_nick)
开发者ID:Havvy,项目名称:madcow,代码行数:50,代码来源:notes.py

示例5: Main

class Main(Module):

    pattern = re.compile(r'^\s*summons?\s+(\S+)(?:\s+(.*?))?\s*$')
    require_addressing = True
    help = u'summon <nick> [reason] - summon user'
    error = u"I couldn't make that summon"

    def init(self):
        self.learn = Learn(self.madcow)

    def response(self, nick, args, kwargs):
        sendto, reason = args
        email = self.learn.lookup('email', sendto)
        if email is None:
            return u"%s: I don't know the email for %s" % (nick, sendto)
        body = u'\n'.join((u'To: %s <%s>' % (sendto, email),
                           u'From: ' + settings.SMTP_FROM,
                           u'Subject: Summon from ' + nick,
                           u'',
                           u'You were summoned by %s. Reason: %s' % (nick, reason)))
        smtp = SMTP(settings.SMTP_SERVER)
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()
        if settings.SMTP_USER and settings.SMTP_PASS:
            smtp.login(settings.SMTP_USER, settings.SMTP_PASS)
        smtp.sendmail(settings.SMTP_FROM, [encode(email, 'ascii')], encode(body))
        return u"%s: summoned %s" % (nick, sendto)
开发者ID:Havvy,项目名称:madcow,代码行数:28,代码来源:summon.py

示例6: Main

class Main(Module):

    pattern = re.compile(r'^\s*(sunrise|sunset)(?:\s+(@?)(.+?))?\s*$', re.I)
    help = '(sunrise|sunset) [location|@nick] - get time of sun rise/set'
    error = u"That place doesn't have a sun, sorry"

    def init(self):
        self.colorlib = self.madcow.colorlib
        try:
            self.learn = Learn(madcow=madcow)
        except:
            self.learn = None
        self.google = Google()

    def response(self, nick, args, kwargs):
        query, args = args[0], args[1:]
        if not args[1]:
            args = 1, nick
        if args[0]:
            location = self.learn.lookup('location', args[1])
            if not location:
                return u'%s: Try: set location <nick> <location>' % nick
        else:
            location = args[1]
        response = self.google.sunrise_sunset(query, location)
        return u'%s: %s' % (nick, response)
开发者ID:cjones,项目名称:madcow,代码行数:26,代码来源:sunrise.py

示例7: Main

class Main(Module):
    pattern = re.compile(r'^\s*summons?\s+(\S+)(?:\s+(.*?))?\s*$')
    require_addressing = True
    help = 'summon <nick> [reason] - summon user'

    def __init__(self, madcow):
        self.learn = Learn(madcow)
        self.config = madcow.config

    def response(self, nick, args, kwargs):
        try:
            sendto, reason = args
            email = self.learn.lookup('email', sendto)
            if email is None:
                return "%s: I don't know the email for %s" % (nick, sendto)
            body = 'To: %s <%s>\n' % (sendto, email)
            body += 'From: %s\n' % (self.config.smtp.sender)
            body += 'Subject: Summon from %s' % nick
            body += '\n'
            body += 'You were summoned by %s. Reason: %s' % (nick, reason)

            smtp = SMTP(self.config.smtp.server)
            if len(self.config.smtp.user):
                smtp.login(self.config.smtp.user, self.config.smtp.password)
            smtp.sendmail(self.config.smtp.sender, [email], body)

            return "%s: summoned %s" % (nick, sendto)

        except Exception, e:
            log.warn('error in %s: %s' % (self.__module__, e))
            log.exception(e)
            return "%s: I couldn't make that summon: %s" % (nick, e)
开发者ID:gipi,项目名称:Richie,代码行数:32,代码来源:summon.py

示例8: Main

class Main(Module):

    pattern = re.compile(r'^\s*summons?\s+(\S+)(?:\s+(.*?))?\s*$')
    require_addressing = True
    help = u'summon <nick> [reason] - summon user'
    error = u"I couldn't make that summon"

    def init(self):
        self.learn = Learn(self.madcow)

    def response(self, nick, args, kwargs):
        sendto, reason = args
        email = self.learn.lookup(u'email', sendto)
        if email is None:
            return u"%s: I don't know the email for %s" % (nick, sendto)

        # just make all this shit ASCII, email is best that way...
        email = email.encode('ascii', 'replace')
        if reason:
            reason = reason.encode('ascii', 'replace')
        anick = nick.encode('ascii', 'replace')

        body = 'To: %s <%s>\n' % (sendto.encode('ascii', 'replace'), email)
        body += 'From: %s\n' % settings.SMTP_FROM
        body += 'Subject: Summon from %s' % anick
        body += '\n'
        body += 'You were summoned by %s. Reason: %s' % (anick, reason)

        smtp = SMTP(settings.SMTP_SERVER)
        if settings.SMTP_USER and settings.SMTP_PASS:
            smtp.login(settings.SMTP_USER, settings.SMTP_PASS)
        smtp.sendmail(settings.SMTP_FROM, [email], body)

        return u"%s: summoned %s" % (nick, sendto)
开发者ID:Spacexplosion,项目名称:Quaking-Mad-Cow,代码行数:34,代码来源:summon.py

示例9: Main

class Main(Module):

    pattern = re.compile(r'^\s*yelp\s+(.+?)(?:\[email protected](.+))?\s*$', re.I)
    help = 'yelp <name> [@location] - restaraunt reviews'

    def __init__(self, madcow=None):
        try:
            self.default_location = madcow.config.yelp.default_location
        except:
            self.default_location = DEFAULT_LOCATION
        try:
            self.learn = Learn(madcow=madcow)
        except:
            self.learn = None

    def response(self, nick, args, kwargs):
        try:
            # sanity check args and pick default search location
            desc, loc = args
            if desc.startswith('@') and not loc:
                raise Exception('invalid search')
            if not loc:
                if self.learn:
                    loc = self.learn.lookup(u'location', nick)
                if not loc:
                    loc = self.default_location

            # perform search
            opts = opts={'find_desc': desc, 'ns': 1, 'find_loc': loc, 'rpp': 1}
            page = geturl(SEARCHURL, opts)

            # extract meaningful data from first result
            soup = BeautifulSoup(page, convertEntities='html')
            result = soup.body.find('div', 'businessresult clearfix')
            name = result.find('a', id='bizTitleLink0').findAll(text=True)
            name = clean_re.search(u''.join(name)).group(1)
            cat = result.find('div', 'itemcategories').a.renderContents()
            rating = result.find('div', 'rating').img['alt']
            rating = rating.replace(' star rating', '')
            reviews = result.find('a', 'reviews')
            url = urljoin(BASEURL, reviews['href'])
            reviews = reviews.renderContents()
            address = [i.strip() for i in result.address.findAll(text=True)]
            address = u', '.join(part for part in address if part)

            # return rendered page
            return RESULT_FMT % {'nick': nick, 'name': name, 'cat': cat,
                                 'rating': rating, 'reviews': reviews,
                                 'address': address, 'url': url}

        except Exception, error:
            log.warn('error in module %s' % self.__module__)
            log.exception(error)
            return u"%s: I couldn't look that up" % nick
开发者ID:compbrain,项目名称:madcow,代码行数:54,代码来源:yelp.py

示例10: Main

class Main(Module):

    pattern = re.compile(u'^\s*(?:fc|forecast|weather)(?:\s+(.*)$)?')
    require_addressing = True
    help = u'fc [location] - look up weather forecast'

    def __init__(self, madcow=None):
        if madcow is not None:
            colorlib = madcow.colorlib
        else:
            colorlib = ColorLib(u'ansi')
        self.weather = Weather(colorlib)
        try:
            self.learn = Learn(madcow=madcow)
        except:
            self.learn = None

    def response(self, nick, args, kwargs):

        args = args[0] if args else None

        if not args and self.learn:
            query = self.learn.lookup(u'location', nick)
        elif args.startswith(u'@') and self.learn:
            query = self.learn.lookup(u'location', args[1:])
        else:
            query = args

        if not query:
            return u'%s: unknown nick. %s' % (nick, USAGE)

        try:
            return u'%s: %s' % (nick, self.weather.forecast(query))
        except Exception, error:
            log.warn(u'error in module %s' % self.__module__)
            log.exception(error)
            return u"Couldn't find that place, maybe a bomb dropped on it"
开发者ID:compbrain,项目名称:madcow,代码行数:37,代码来源:weather.py

示例11: Main

class Main(Module):

    pattern = re.compile(r'^\s*summons?\s+(\S+)(?:\s+(.*?))?\s*$')
    require_addressing = True
    help = u'summon <nick> [reason] - summon user'

    def __init__(self, madcow):
        self.learn = Learn(madcow)
        self.config = madcow.config

    def response(self, nick, args, kwargs):
        try:
            sendto, reason = args
            email = self.learn.lookup(u'email', sendto)
            if email is None:
                return u"%s: I don't know the email for %s" % (nick, sendto)

            # just make all this shit ASCII, email is best that way...
            email = email.encode('ascii', 'replace')
            if reason:
                reason = reason.encode('ascii', 'replace')
            anick = nick.encode('ascii', 'replace')

            body = 'To: %s <%s>\n' % (sendto.encode('ascii', 'replace'), email)
            body += 'From: %s\n' % (self.config.smtp.sender)
            body += 'Subject: Summon from %s' % anick
            body += '\n'
            body += 'You were summoned by %s. Reason: %s' % (anick, reason)

            smtp = SMTP(self.config.smtp.server)
            if len(self.config.smtp.user):
                smtp.login(self.config.smtp.user, self.config.smtp.password)
            smtp.sendmail(self.config.smtp.sender, [email], body)

            return u"%s: summoned %s" % (nick, sendto)

        except Exception, error:
            log.warn(u'error in module %s' % self.__module__)
            log.exception(error)
            return u"%s: I couldn't make that summon: %s" % (nick, error)
开发者ID:compbrain,项目名称:madcow,代码行数:40,代码来源:summon.py

示例12: Main

class Main(Module):
    pattern = re.compile(u'^\s*(xray)(?:\s+(.*)$)?')
    require_addressing = False
    help = u'xray <nick>        show all staff-accessible data for <nick> (staff only)'
    
    def init(self):
        self.learn = Learn(madcow=self.madcow)
        self.staff = Staff(madcow=self.madcow)
        self.company = Company(madcow=self.madcow)
        self.realname = Realname(madcow=self.madcow)
        self.notes = Notes(madcow=self.madcow)
    
    def response(self, nick, args, kwargs):
        cmd = args[0]
        target_nick = args[1].strip()
        real_name = False
        
        # only staff members & bot owner are allowed to get xray data
        if self.staff.is_staff(nick) or settings.OWNER_NICK == nick:
            if target_nick:
                name = self.realname.get(target_nick);
                company = self.company.get(target_nick);
                notes = self.notes.get(target_nick);
                email = self.learn.lookup('email', target_nick)
                summary = ''
                if name:
                    summary = summary + 'Name: ' + name + '\n'
                if email:
                    summary = summary + 'Email: ' + email + '\n'
                if company:
                    summary = summary + 'Company: ' + company + '\n'
                if notes:
                    summary = summary + u'=' * 75 + notes + '\n'
                if summary:
                    return u'%s: Here\'s the skinny on %s\n\n%s' % (nick, target_nick, summary)
                else:
                    return u'%s: No data found for %s' % (nick, target_nick)
            else:
                return u'%s: xray only works if you tell me who to scan.' % (nick)
开发者ID:Havvy,项目名称:madcow,代码行数:39,代码来源:xray.py

示例13: Main

class Main(Module):

    pattern = re.compile(r'^\s*noaa(?:\s+(@?)(.+?))?\s*$', re.I)
    help = 'noaa [location|@nick] - alternative weather (us only)'
    noaa_url = 'http://www.weather.gov/'
    noaa_search = 'http://forecast.weather.gov/zipcity.php'
    error = 'Something bad happened'
    fc_re = re.compile(r'^myforecast-current')

    def init(self):
        self.colorlib = self.madcow.colorlib
        try:
            self.learn = Learn(madcow=self.madcow)
        except:
            self.learn = None

    def response(self, nick, args, kwargs):
        if not args[1]:
            args = 1, nick
        if args[0]:
            location = self.learn.lookup('location', args[1])
            if not location:
                return u'%s: Try: set location <nick> <location>' % nick
        else:
            location = args[1]
        response = self.getweather(location)
        if not response:
            response = self.error
        return u'%s: %s' % (nick, response)

    def getweather(self, location):
        """Look up NOAA weather"""
        soup = getsoup(self.noaa_search, {'inputstring': location}, referer=self.noaa_url)
        return u' / '.join(map(self.render, soup.findAll(attrs={'class': self.fc_re})))

    @staticmethod
    def render(node):
        data = strip_html(decode(node.renderContents(), 'utf-8'))
        return data.strip()
开发者ID:Havvy,项目名称:madcow,代码行数:39,代码来源:noaa.py

示例14: Karma

class Karma(Base):
    """Infobot style karma"""

    _adjust_pattern = re.compile(r"^\s*(.*?)[+-]([+-]+)\s*$")
    _query_pattern = re.compile(r"^\s*karma\s+(\S+)\s*\?*\s*$")
    _dbname = "karma"

    def __init__(self, madcow):
        self.learn = Learn(madcow)

    def process(self, nick, input):
        kr = KarmaResponse(reply=None, matched=False)

        # see if someone is trying to adjust karma
        try:
            target, adjustment = Karma._adjust_pattern.search(input).groups()
            # don't let people adjust their own karma ;p
            if nick.lower() != target.lower():
                self.adjust(nick=target, adjustment=adjustment)
            kr.matched = True
        except:
            pass

        # detect a query for someone's karma
        try:
            target = Karma._query_pattern.search(input).group(1)
            karma = self.query(nick=target)
            kr.matched = True
            kr.reply = "%s: %s's karma is %s" % (nick, target, karma)
        except:
            pass

        return kr

    def set(self, nick, karma):
        self.learn.set(Karma._dbname, nick.lower(), str(karma))

    def adjust(self, nick, adjustment):
        karma = self.query(nick)
        adjustment, size = adjustment[0], len(adjustment)
        exec("karma " + adjustment + "= size")
        self.set(nick=nick, karma=karma)

    def query(self, nick):
        karma = self.learn.lookup(Karma._dbname, nick.lower())
        if karma is None:
            karma = 0
            self.set(nick=nick, karma=karma)
        return int(karma)
开发者ID:gipi,项目名称:Richie,代码行数:49,代码来源:karma.py

示例15: init

 def init(self):
     self.colorlib = self.madcow.colorlib
     try:
         self.learn = Learn(madcow=madcow)
     except:
         self.learn = None
     self.google = Google()
开发者ID:cjones,项目名称:madcow,代码行数:7,代码来源:sunrise.py


注:本文中的learn.Main类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。