0

so I'm trying to allow using ! as a prefix for something. Here I have some regular expressions, but I almost have no idea how to do so:

if inp.chan == inp.nick:  # private message, no command prefix
    prefix = r'^(?:[!!]?|'
else:
    prefix = r'^(?:[!]|'

command_re = prefix + inp.conn.nick
command_re += r'[:,]+\s+)(\w+)(?:$|\s+)(.*)'

I can change command's prefix by changing [!], but I want to make it so I can make the prefix double !!'ed, such as !!test will work. Thanks.

Edit:

import re
import random
from util import hook, http

re_lineends = re.compile(r'[\r\n]*')
command_prefix = re.compile(r'^\!+')

@hook.command(command_prefix)
def exl(inp,nick=""):
    ""
res = http.get("http://eval.appspot.com/eval", statement=inp).splitlines()

if len(res) == 0:
    return
res[0] = re_lineends.split(res[0])[0]
if not res[0] == 'Traceback (most recent call last):':
    return res[0]
else:
    return res[-1]

@hook.command:

def _hook_add(func, add, name=''):
    if not hasattr(func, '_hook'):
        func._hook = []
    func._hook.append(add)

    if not hasattr(func, '_filename'):
        func._filename = func.func_code.co_filename

    if not hasattr(func, '_args'):
        argspec = inspect.getargspec(func)
        if name:
            n_args = len(argspec.args)
            if argspec.defaults:
                n_args -= len(argspec.defaults)
            if argspec.keywords:
                n_args -= 1
            if argspec.varargs:
                n_args -= 1
            if n_args != 1:
                err = '%ss must take 1 non-keyword argument (%s)' % (name,
                            func.__name__)
                raise ValueError(err)

        args = []
        if argspec.defaults:
            end = bool(argspec.keywords) + bool(argspec.varargs)
            args.extend(argspec.args[-len(argspec.defaults):
                        end if end else None])
        if argspec.keywords:
            args.append(0)  # means kwargs present
        func._args = args

    if not hasattr(func, '_thread'):  # does function run in its own thread?
        func._thread = False
RewriteRule
  • 41
  • 1
  • 6

1 Answers1

0

Do you mean something like r'^\!+'? This will match any number of exclamation points at the start of a string.

>>> import re
>>> regex = re.compile(r'^\!+')
>>> regex.match("!foo")
<_sre.SRE_Match object at 0xcb6b0>
>>> regex.match("!!foo")
<_sre.SRE_Match object at 0xcb6e8>
>>> regex.match("!!!foo")
<_sre.SRE_Match object at 0xcb6b0>

If you want to limit yourself to 1 or 2 !, then you could use r'^\!{1,2}':

>>> regex = re.compile(r'^\!{1,2}')
>>> regex.match('!!!foo').group(0)  #only matches 2 of the exclamation points.
'!!'
>>> regex.match('!foo').group(0)
'!'
>>> regex.match('!!foo').group(0)
'!!'
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • You certainly mean `r"^\!{1,2}"` but not `r"$\!{1,2}"`. – pemistahl Jan 10 '13 at 18:48
  • @PeterStahl -- I got it right in my code snippet :). Thanks. – mgilson Jan 10 '13 at 18:49
  • Ye, I tried what you told me, but my script returns invalid command. Shall I post the command prefix regexes? – RewriteRule Jan 10 '13 at 18:50
  • @RewriteRule -- See the comment by Peter Stahl above. I had a typo in my second regex (although my code snippets were correct). – mgilson Jan 10 '13 at 18:51
  • @mgilson, I saw it and changed it, but it gave me an error "return outside function". Pretty weird, since the code worked before I tried to change the command prefix. Do you have an idea? – RewriteRule Jan 10 '13 at 18:53
  • @RewriteRule -- Not really. I don't know what `hook.command` is or does, so it's pretty tough to guess. – mgilson Jan 10 '13 at 18:54
  • @RewriteRule -- decorators with parameters are a [bit complicated](http://stackoverflow.com/questions/5929107/python-decorators-with-parameters) (I'm not even sure I understand it yet :-P). However, as I don't see any `return` statement inside `_hook_add`, I'm pretty sure that isn't right (even for a regular decorator). – mgilson Jan 10 '13 at 19:02
  • mgilson you should have an idea what "return outside of function" means :) @RewriteRule, you are using a return statement outside of a function definition, which is obviously invalid and leads to a parsing error. You probably messed up the indentation while you edited your code, like in the code sample in your question - your `exl` function is currently empty except for an empty string literal. – l4mpi Jan 10 '13 at 19:02
  • @l4mpi -- You're right that the error is obviously the result of a return statement that isn't in a function block. However, that definitely isn't the only error here ... – mgilson Jan 10 '13 at 19:05
  • Yeh I fixed the identation, seems like I forgot. Also mgilson's code worked. Thank you guys! – RewriteRule Jan 10 '13 at 19:05