如何实现“EXIT_CODES”?在Python中?

发布于 2024-09-20 00:29:05 字数 440 浏览 5 评论 0原文

最初我想做类似的事情:

#EXIT CODES
class ExitCode(object):
    (USERHOME_INVALID, \
    USERHOME_CANNOT_WRITE, \
    USERHOME_CANNOT_READ, \
    BASHRC_INVALID) = range(-1, -5, -1)

但后来我意识到我必须确切地知道 EXIT_CODES 的总数,以便我可以将其传递给 range() 函数。假设我有 87 个(任意)EXIT_CODES...我不想数到 87(并不是说这很难),但我正在寻找一个更优雅的解决方案。

有什么建议吗?

编辑: EXIT_CODE 是一个负整数,将传递给 sys.exit 。我不喜欢写数字,而是更喜欢使用某种常量(例如 C 中的#defines 或枚举,或者 Java 中的枚举)。

Initially i thought to do something like:

#EXIT CODES
class ExitCode(object):
    (USERHOME_INVALID, \
    USERHOME_CANNOT_WRITE, \
    USERHOME_CANNOT_READ, \
    BASHRC_INVALID) = range(-1, -5, -1)

But than I've realized that I'll have to know exactly the total number of EXIT_CODES, so that I can pass it to the range() function. Let's suppose I'll have 87 (arbitrary) EXIT_CODES... I don't want to count to 87 (not that it's hard) but I am looking for a more elegant solution.

Any suggestions ?

EDIT:
EXIT_CODE is a negative int that will be passed to sys.exit . Instead of writing the number I prefer to use some sort of constants (something like #defines or enums in C, or enums in Java).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痴意少年 2024-09-27 00:29:05

听起来您想要的是 C# 或其他类似语言中的枚举的 Python 等效项。 如何在 Python 中表示“枚举” ? 提供了多种解决方案,但它们仍然需要您拥有的项目数量。
编辑: 我如何表示 ' Python 中的 Enum'? 看起来方式更好。

或者你可以尝试这样的事情(但可能不是最好的解决方案):

class _ExitCode:
    _exit_codes=["EXIT_CODE","EXIT_CODE_TWO"]
    def __getattr__(self, name):
        if name in _ExitCode._exit_codes:
            return -(_ExitCode._exit_codes.index(name)+1)
        raise AttributeError("Exit code %s not found" % name)

ExitCode=_ExitCode()
print ExitCode.EXIT_CODE #-1

Sounds like what you want is the Python equivalent of an enumeration in C# or other similar languages. How can I represent an 'Enum' in Python? provides several solutions, though they still require the number of items you have.
EDIT: How can I represent an 'Enum' in Python? looks way better.

Or you could try something like this (probably not the best solution, though):

class _ExitCode:
    _exit_codes=["EXIT_CODE","EXIT_CODE_TWO"]
    def __getattr__(self, name):
        if name in _ExitCode._exit_codes:
            return -(_ExitCode._exit_codes.index(name)+1)
        raise AttributeError("Exit code %s not found" % name)

ExitCode=_ExitCode()
print ExitCode.EXIT_CODE #-1
娇女薄笑 2024-09-27 00:29:05

也许我不明白这个问题,但为什么不简单地制作一个退出代码字典并在函数中实现所需的行为呢?

EXIT_CODES = dict(SUCCESS=0,
                  USER_NAME_INVALID=-1,
                  OTHER_ERROR=-2)

def exit(code):
   try:
      return EXIT_CODES[code]  
   except KeyError:
      raise KeyError("exit code %s is not implemented" % code)

所以你可以像这样使用它

# some computation goes here
return exit("SUCCESS")

如果你想“自动”分配数字(我不推荐这样做)你可以简单地创建一个退出代码列表并返回索引的负数:(

EXIT_CODES = ['SUCCESS', 'ERROR_1', 'ERROR_2']
return -EXIT_CODES.index('ERROR_1')
# will return -1

对于最后一个,你可以实现类似基于字典的功能)

Maybe I don't understand the question, but why don't you simply make a dictionary of exit codes and implement the desired behaviour in a function?

EXIT_CODES = dict(SUCCESS=0,
                  USER_NAME_INVALID=-1,
                  OTHER_ERROR=-2)

def exit(code):
   try:
      return EXIT_CODES[code]  
   except KeyError:
      raise KeyError("exit code %s is not implemented" % code)

So you can use it like

# some computation goes here
return exit("SUCCESS")

And if you want to make "automatic" assignment of numbers (I don't recommend this) you can simply create a list of exit codes and return the negative of the index:

EXIT_CODES = ['SUCCESS', 'ERROR_1', 'ERROR_2']
return -EXIT_CODES.index('ERROR_1')
# will return -1

(for the last one, you can implement a function similar to the dictionary-based one)

歌入人心 2024-09-27 00:29:05

我必须指出,根本不确定负状态对于 sys.exit(); 是否有意义。至少在 Linux 上,它将被解释为无符号 8 位值(范围 0-255)。对于枚举类型,可以执行如下操作:

class ExitStatus: pass
for code, name in enumerate("Success Failure CriticalFailure".split()):
    setattr(ExitStatus, name, code)

导致如下结果:

>>> ExitStatus.__dict__
{'CriticalFailure': 2, 'Failure': 1, '__module__': '__main__',
'__doc__': None, 'Success': 0}

普通 Unix 系统中的预定义值为 EXIT_FAILURE=1 和 EXIT_SUCCESS=0。

附录:考虑到对标识符的 IDE 识别的担忧,还可以执行以下操作:

class EnumItem: pass
def adjustEnum(enum):
    value=0
    enumdict=enum.__dict__
    for k,v in enumdict.items():
        if isinstance(v,int):
            if v>=value:
                value=v+1
    for k,v in enumdict.items():
        if v is EnumItem:
            enumdict[k]=value
            value+=1

class ExitStatus:
    Success=0
    Failure=EnumItem
    CriticalFailure=EnumItem
adjustEnum(ExitStatus)

第二次编辑:无法避免。这是一个变体,它按照您编写名称的顺序分配值。

class EnumItem:
    serial=0
    def __init__(self):
        self.serial=self.__class__.serial
        self.__class__.serial+=1

def adjustEnum(enum):
    enumdict=enum.__dict__
    value=0
    unknowns={}
    for k,v in enumdict.items():
        if isinstance(v,int):
            if v>=value:
                value=v+1
        elif isinstance(v,EnumItem):
            unknowns[v.serial]=k
    for i,k in sorted(unknowns.items()):
        enumdict[k]=value
        value+=1
    return enum

@adjustEnum
class ExitStatus:
    Success=0
    Failure=EnumItem()
    CriticalFailure=EnumItem()

显然,不断增长的复杂性是不优雅的,但它确实有效。

I must note that it's not at all certain a negative status makes sense for sys.exit(); at least on Linux, it will be interpreted as an unsigned 8-bit value (range 0-255). As for an enumerated type, it's possible to do something like:

class ExitStatus: pass
for code, name in enumerate("Success Failure CriticalFailure".split()):
    setattr(ExitStatus, name, code)

Resulting in something like:

>>> ExitStatus.__dict__
{'CriticalFailure': 2, 'Failure': 1, '__module__': '__main__',
'__doc__': None, 'Success': 0}

The predefined values in normal Unix systems are EXIT_FAILURE=1 and EXIT_SUCCESS=0.

Addendum: Considering the concern about IDE identification of identifiers, one could also do something like:

class EnumItem: pass
def adjustEnum(enum):
    value=0
    enumdict=enum.__dict__
    for k,v in enumdict.items():
        if isinstance(v,int):
            if v>=value:
                value=v+1
    for k,v in enumdict.items():
        if v is EnumItem:
            enumdict[k]=value
            value+=1

class ExitStatus:
    Success=0
    Failure=EnumItem
    CriticalFailure=EnumItem
adjustEnum(ExitStatus)

Second edit: Couldn't keep away. Here's a variant that assigns the values in the order you've written the names.

class EnumItem:
    serial=0
    def __init__(self):
        self.serial=self.__class__.serial
        self.__class__.serial+=1

def adjustEnum(enum):
    enumdict=enum.__dict__
    value=0
    unknowns={}
    for k,v in enumdict.items():
        if isinstance(v,int):
            if v>=value:
                value=v+1
        elif isinstance(v,EnumItem):
            unknowns[v.serial]=k
    for i,k in sorted(unknowns.items()):
        enumdict[k]=value
        value+=1
    return enum

@adjustEnum
class ExitStatus:
    Success=0
    Failure=EnumItem()
    CriticalFailure=EnumItem()

Obviously the growing complexity is inelegant, but it does work.

江南烟雨〆相思醉 2024-09-27 00:29:05

我想我之前看过这个问题但没有看到它,但显而易见的一件事就是使用字典。

def make_exit_codes(*exit_codes):
    return dict((name, -value - 1) for name, value in enumerate(exit_codes)) 

EXIT_CODES = make_exit_codes('USERHOME_INVALID', 'USERHOME_CANNOT_WRITE',
                             'USERHOME_CANNOT_READ', 'BASHRC_INVALID')

I guess I looked at this question earlier and didn't see it, but one obvious thing to do is use a dict.

def make_exit_codes(*exit_codes):
    return dict((name, -value - 1) for name, value in enumerate(exit_codes)) 

EXIT_CODES = make_exit_codes('USERHOME_INVALID', 'USERHOME_CANNOT_WRITE',
                             'USERHOME_CANNOT_READ', 'BASHRC_INVALID')
剧终人散尽 2024-09-27 00:29:05

您可以使用 Python 即时创建变量(或类属性)。例如

ExitCodes = '''USERHOME_INVALID, USERHOME_CANNOT_WRITE,
               USERHOME_CANNOT_READ, BASHRC_INVALID'''

for i, s in enumerate(ExitCodes.split(','), 1):
    exec('%s = %d' % (s.strip(), -i))

print USERHOME_INVALID
print USERHOME_CANNOT_WRITE
print USERHOME_CANNOT_READ
print BASHRC_INVALID

sys.exit(USERHOME_INVALID)

>>> -1
>>> -2
>>> -3
>>> -4

You can create variables (or class attributes) on-the-fly with Python. For example

ExitCodes = '''USERHOME_INVALID, USERHOME_CANNOT_WRITE,
               USERHOME_CANNOT_READ, BASHRC_INVALID'''

for i, s in enumerate(ExitCodes.split(','), 1):
    exec('%s = %d' % (s.strip(), -i))

print USERHOME_INVALID
print USERHOME_CANNOT_WRITE
print USERHOME_CANNOT_READ
print BASHRC_INVALID

sys.exit(USERHOME_INVALID)

>>> -1
>>> -2
>>> -3
>>> -4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文