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


Python Main.set方法代码示例

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


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

示例1: Main

# 需要导入模块: from learn import Main [as 别名]
# 或者: from learn.Main import set [as 别名]
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,代码行数:52,代码来源:notes.py

示例2: Karma

# 需要导入模块: from learn import Main [as 别名]
# 或者: from learn.Main import set [as 别名]
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,代码行数:51,代码来源:karma.py

示例3: Main

# 需要导入模块: from learn import Main [as 别名]
# 或者: from learn.Main import set [as 别名]
class Main(Module):
    pattern = re.compile(u'^\s*(names?)(?:\s+(.*)$)?')
    require_addressing = False
    dbname = u'realname'
    help = u'names                          show list of associated nicks and names (staff only)\n\
name <nick>                    show real name associated with user (staff only)\n\
name <nick> <real_name>        set real name for user (staff only)'
    
    def init(self):
        self.learn = Learn(madcow=self.madcow)
        self.staff = Staff(madcow=self.madcow)
        
    def set(self, nick, name):
        self.learn.set(self.dbname, nick.lower(), name)
        
    def unset(self, nick):
        dbm = self.learn.dbm(self.dbname)
        try:
            key = encode(nick.lower())
            if dbm.has_key(nick):
                del dbm[nick]
                return True
            return False
        finally:
            dbm.close()
    
    def get_names(self):
        name_db = self.learn.get_db('realname');
        return name_db
    
    def has_name(self, nick):
        name_db = self.get_names()
        return nick in name_db
   
    def get(self, nick):
        return self.learn.lookup(self.dbname, nick)
    
    def response(self, nick, args, kwargs):
        cmd = args[0]
        target_nick = False
        real_name = False
        params = []
        if args[1]:
            params = args[1].partition(' ')
        
        try:
            target_nick = params[0]
        except IndexError:
            target_nick = False
            
        try:
            real_name = params[2]
        except IndexError:
            real_name = False
            
        # only staff members & bot owner are allowed to get & set real_name data
        if self.staff.is_staff(nick) or settings.OWNER_NICK == nick:
            if target_nick:
                if real_name:
                    self.set(target_nick, real_name)
                    return u'%s: Setting name for %s to %s' % ( nick, target_nick, real_name )
                else:
                    name = self.get(nick=target_nick)
                    if name:
                        return u'%s: %s is %s' % ( nick, target_nick, name )
                    else:
                        return u'%s: Sorry, I don\'t who %s is.' % ( nick, target_nick )
            else:
                name_list = "\n\nRecorded names:\n"
                for user_nick, name in self.get_names().iteritems():
                    name_list = name_list + user_nick + ": " + name + "\n"
                return u'%s: %s' % (nick, name_list)
开发者ID:Havvy,项目名称:madcow,代码行数:74,代码来源:realname.py

示例4: Main

# 需要导入模块: from learn import Main [as 别名]
# 或者: from learn.Main import set [as 别名]
class Main(Module):
    pattern = re.compile(u'^\s*(compan[y|ies])(?:\s+(.*)$)?')
    require_addressing = False
    dbname = u'company'
    help = u'companies                          list all company/nick associations (staff only)\n\
company <nick>                     show company associated with user (staff only)\n\
company <nick> <company_name>      set company information for user (staff only)'
    
    def init(self):
        self.learn = Learn(madcow=self.madcow)
        self.staff = Staff(madcow=self.madcow)
        
    def set(self, nick, company):
        self.learn.set(self.dbname, nick.lower(), company)
        
    def unset(self, nick):
        dbm = self.learn.dbm(self.dbname)
        try:
            key = encode(nick.lower())
            if dbm.has_key(nick):
                del dbm[nick]
                return True
            return False
        finally:
            dbm.close()
    
    def get_companies(self):
        company_db = self.learn.get_db('company');
        return company_db
    
    def has_company(self, nick):
        company_db = self.get_companies()
        return nick in company_db
   
    def get(self, nick):
        return self.learn.lookup(self.dbname, nick)
    
    def response(self, nick, args, kwargs):
        cmd = args[0]
        target_nick = False
        company = False
        params = []
        if args[1]:
            params = args[1].partition(' ')
        
        try:
            target_nick = params[0]
        except IndexError:
            target_nick = False
            
        try:
            company = params[2]
        except IndexError:
            company = False
            
        # only staff members & bot owner are allowed to get & set company data
        if self.staff.is_staff(nick) or settings.OWNER_NICK == nick:
            if target_nick:
                if company:
                    self.set(target_nick, company)
                    return u'%s: setting company for %s to %s' % ( nick, target_nick, company )
                else:
                    company_name = self.get(target_nick)
                    if company_name:
                        return u'%s: %s works at %s' % ( nick, target_nick, company_name )
                    else:
                        return u'%s: I don\'t know where %s works' % ( nick, target_nick )
            else:
                company_list = "\n\nRecorded companies:\n"
                for user_nick, company_name in self.get_companies().iteritems():
                    company_list = company_list + user_nick + " works at " + company_name + "\n"
                return u'%s: %s' % (nick, company_list)
开发者ID:Havvy,项目名称:madcow,代码行数:74,代码来源:company.py

示例5: Main

# 需要导入模块: from learn import Main [as 别名]
# 或者: from learn.Main import set [as 别名]
class Main(Module):
    pattern = re.compile(u'^\s*(staff)(?:\s+(.*)$)?')
    require_addressing = False
    dbname = u'staff'
    help = u'staff                          list staff members\n\
staff <nick>                   check if specific user is a staff member\n\
staff <nick> add/remove        add/remove user from staff list (staff only)'
    
    def init(self):
        self.learn = Learn(madcow=self.madcow)
        
    def set(self, nick):
        self.learn.set(self.dbname, nick.lower(), True)
        
    def unset(self, nick):
        dbm = self.learn.dbm(self.dbname)
        try:
            key = encode(nick.lower())
            if dbm.has_key(key):
                del dbm[key]
                return True
            return False
        finally:
            dbm.close()
        
    def get_staff(self):
        staff_db = self.learn.get_db('staff');
        return staff_db
    
    def is_staff(self, nick):
        staff_db = self.get_staff()
        return nick in staff_db
        
    def response(self, nick, args, kwargs):
        cmd = args[0]
        target_nick = False
        action = False
        params = []
        if args[1]:
            params = args[1].split()
        try:
            target_nick = params[0]
        except IndexError:
            target_nick = False
            
        try:
            action = params[1]
        except IndexError:
            action = False

        """ if nick passed, set user as staff """
        if target_nick:
            # if an action is passed, and requesting user is staff or owner, perform the action
            if action and (self.is_staff(nick) or settings.OWNER_NICK == nick):
                if action == 'remove':
                    self.unset(nick=target_nick)
                    return u'%s: %s is no longer marked as staff' % (nick, target_nick)
                elif action == "add":
                    self.set(nick=target_nick)
                    return u'%s: %s is now marked as staff' % (nick, target_nick)
                else:
                    return u'%s: I\'m not sure what you want me to do here. \'%s\' is not a valid staff action.' %(nick, action)
            
            # otherwise, respond with staff membership status for nick, ignoring the action
            if self.is_staff(target_nick):
                return u'%s: Yes, %s is a staff member' %(nick, target_nick)
            else:
                return u'%s: No, %s is not a staff member' %(nick, target_nick)
        else:
            staff_db = self.get_staff()
            staff_nicks = staff_db.keys()
            if len(staff_nicks):
                return u'%s: Staff members are: %s' % (nick, ", ".join(staff_nicks))
            else:
                return u'%s: There are currently no users marked as staff members.' % (nick)
开发者ID:Havvy,项目名称:madcow,代码行数:77,代码来源:staff.py

示例6: Main

# 需要导入模块: from learn import Main [as 别名]
# 或者: from learn.Main import set [as 别名]
class Main(Module):
    pattern = re.compile(u'^\s*(link|shortcut)s?(?:\s+(.*)$)?')
    require_addressing = False
    dbname = u'links'
    help = u'shortcuts                         show list of resource shortcuts\n\
link <shortcut>                   show link for specified shortcut \n\
link <shortcut> <url>             set link for specified shortcut (staff only)\n\
link <shortcut> delete           remove link for specified shortcut (staff only)'
    
    def init(self):
        self.learn = Learn(madcow=self.madcow)
        self.staff = Staff(madcow=self.madcow)
        
    def set(self, shortcut, url):
        self.learn.set(self.dbname, shortcut.lower(), url)
        
    def unset(self, shortcut):
        dbm = self.learn.dbm(self.dbname)
        try:
            key = encode(shortcut.lower())
            if dbm.has_key(shortcut):
                del dbm[shortcut]
                return True
            return False
        finally:
            dbm.close()
    
    def get_shortcuts(self):
        link_db = self.learn.get_db(self.dbname);
        return link_db
    
    def has_name(self, shortcut):
        link_db = self.get_shortcuts()
        return shortcut in link_db
   
    def get(self, shortcut):
        return self.learn.lookup(self.dbname, shortcut)
    
    def response(self, nick, args, kwargs):
        cmd = args[0]
        shortcut = False
        link = False
        params = []
        if args[1]:
            params = args[1].partition(' ')
        
        try:
            shortcut = params[0]
        except IndexError:
            shortcut = False
            
        try:
            link = params[2]
        except IndexError:
            link = False
            
        # only staff members & bot owner are allowed to set/change shortcuts
        if shortcut and link and (self.staff.is_staff(nick) or settings.OWNER_NICK == nick):
            if link == "delete":
                self.unset(shortcut)
                return u'%s: Okay, I\'ve deleted the link for %s.  It used to be %s, and now it\'s nothing. How sad.' % ( nick, shortcut, link )
            else:
                self.set(shortcut, link)
                return u'%s: Okay, I\'ve set the link for %s to %s' % ( nick, shortcut, link )
        elif shortcut:
            link_url = self.get(shortcut)
            if link_url:
                return u'%s: %s - %s' % (nick, shortcut, link_url)
            else:
                return u'%s: Sorry, there is no \'%s\' shortcut.' % (nick, shortcut)
        else:
            kwargs['req'].make_private() # don't spam the channel with all the links.  there could be a lot of them.
            links = self.get_shortcuts()
            if len(links):
                link_list = "Here are all the current shortcuts:\n"
                for shortcut, url in links.iteritems():
                    link_list = link_list + shortcut + ": " + url + "\n"
                return u'%s' % (link_list)
            else:
                return u'Sorry, no shortcuts have been defined yet.'
开发者ID:Havvy,项目名称:madcow,代码行数:82,代码来源:links.py


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