mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-22 18:14:41 +01:00
cleaner implementation of getLong
This commit is contained in:
parent
2f3fa324f0
commit
a71d074e46
@ -147,11 +147,7 @@ def urlSnarfer(f):
|
|||||||
###
|
###
|
||||||
|
|
||||||
# This is just so we can centralize this, since it may change.
|
# This is just so we can centralize this, since it may change.
|
||||||
def _int(s, Long=False):
|
def _int(s):
|
||||||
if not Long:
|
|
||||||
Type = int
|
|
||||||
else:
|
|
||||||
Type = long
|
|
||||||
base = 10
|
base = 10
|
||||||
if s.startswith('0x'):
|
if s.startswith('0x'):
|
||||||
base = 16
|
base = 16
|
||||||
@ -163,62 +159,63 @@ def _int(s, Long=False):
|
|||||||
base = 8
|
base = 8
|
||||||
s = s[1:]
|
s = s[1:]
|
||||||
try:
|
try:
|
||||||
return Type(s, base)
|
return int(s, base)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
if base == 10:
|
if base == 10:
|
||||||
return Type(float(s))
|
return int(float(s))
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def getInt(irc, msg, args, state, Type='integer', p=None, Long=False):
|
def getInt(irc, msg, args, state, type='integer', p=None):
|
||||||
try:
|
try:
|
||||||
i = _int(args[0], Long)
|
i = _int(args[0])
|
||||||
if p is not None:
|
if p is not None:
|
||||||
if not p(i):
|
if not p(i):
|
||||||
irc.errorInvalid(Type, args[0])
|
irc.errorInvalid(type, args[0])
|
||||||
state.args.append(i)
|
state.args.append(i)
|
||||||
del args[0]
|
del args[0]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.errorInvalid(Type, args[0])
|
irc.errorInvalid(type, args[0])
|
||||||
|
|
||||||
def getNonInt(irc, msg, args, state, Type='non-integer value'):
|
def getNonInt(irc, msg, args, state, type='non-integer value'):
|
||||||
try:
|
try:
|
||||||
i = _int(args[0])
|
i = _int(args[0])
|
||||||
irc.errorInvalid(Type, args[0])
|
irc.errorInvalid(type, args[0])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
state.args.append(args.pop(0))
|
state.args.append(args.pop(0))
|
||||||
|
|
||||||
def getLong(irc, msg, args, state, Type='long number'):
|
def getLong(irc, msg, args, state, type='long'):
|
||||||
getInt(irc, msg, args, state, Type, Long=True)
|
getInt(irc, msg, args, state, type)
|
||||||
|
state.args[-1] = long(state.args[-1])
|
||||||
|
|
||||||
def getFloat(irc, msg, args, state, Type='floating point number'):
|
def getFloat(irc, msg, args, state, type='floating point number'):
|
||||||
try:
|
try:
|
||||||
state.args.append(float(args[0]))
|
state.args.append(float(args[0]))
|
||||||
del args[0]
|
del args[0]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.errorInvalid(Type, args[0])
|
irc.errorInvalid(type, args[0])
|
||||||
|
|
||||||
def getPositiveInt(irc, msg, args, state, *L):
|
def getPositiveInt(irc, msg, args, state, *L):
|
||||||
getInt(irc, msg, args, state,
|
getInt(irc, msg, args, state,
|
||||||
p=lambda i: i>0, Type='positive integer', *L)
|
p=lambda i: i>0, type='positive integer', *L)
|
||||||
|
|
||||||
def getNonNegativeInt(irc, msg, args, state, *L):
|
def getNonNegativeInt(irc, msg, args, state, *L):
|
||||||
getInt(irc, msg, args, state,
|
getInt(irc, msg, args, state,
|
||||||
p=lambda i: i>=0, Type='non-negative integer', *L)
|
p=lambda i: i>=0, type='non-negative integer', *L)
|
||||||
|
|
||||||
def getIndex(irc, msg, args, state):
|
def getIndex(irc, msg, args, state):
|
||||||
getInt(irc, msg, args, state, Type='index')
|
getInt(irc, msg, args, state, type='index')
|
||||||
if state.args[-1] > 0:
|
if state.args[-1] > 0:
|
||||||
state.args[-1] -= 1
|
state.args[-1] -= 1
|
||||||
|
|
||||||
def getId(irc, msg, args, state, kind=None):
|
def getId(irc, msg, args, state, kind=None):
|
||||||
Type = 'id'
|
type = 'id'
|
||||||
if kind is not None and not kind.endswith('id'):
|
if kind is not None and not kind.endswith('id'):
|
||||||
Type = kind + ' id'
|
type = kind + ' id'
|
||||||
original = args[0]
|
original = args[0]
|
||||||
try:
|
try:
|
||||||
args[0] = args[0].lstrip('#')
|
args[0] = args[0].lstrip('#')
|
||||||
getInt(irc, msg, args, state, Type=Type)
|
getInt(irc, msg, args, state, type=type)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
args[0] = original
|
args[0] = original
|
||||||
raise
|
raise
|
||||||
@ -533,8 +530,8 @@ wrappers = ircutils.IrcDict({
|
|||||||
'now': getNow,
|
'now': getNow,
|
||||||
'url': getUrl,
|
'url': getUrl,
|
||||||
'httpUrl': getHttpUrl,
|
'httpUrl': getHttpUrl,
|
||||||
'float': getFloat,
|
|
||||||
'long': getLong,
|
'long': getLong,
|
||||||
|
'float': getFloat,
|
||||||
'nonInt': getNonInt,
|
'nonInt': getNonInt,
|
||||||
'positiveInt': getPositiveInt,
|
'positiveInt': getPositiveInt,
|
||||||
'nonNegativeInt': getNonNegativeInt,
|
'nonNegativeInt': getNonNegativeInt,
|
||||||
|
Loading…
Reference in New Issue
Block a user