I'm stuck dealing with strings which is a pain when porting code...
I'm doing stuff such as porting this:

enum { VAL_NULL = 0, VAL_INT, VAL_FLOAT, VAL_STR, VAL_ANY, VAL_CODE, VAL_MACRO, VAL_IDENT };

to this:

for i,v in enumerate([
    'VAL_NULL','VAL_INT','VAL_FLOAT','VAL_STR','VAL_ANY','VAL_CODE','VAL_MACRO','VAL_IDENT'
]): globals()[v]=i

anyone know of a method I could use to enumerate globals w/o pasting the varnames twice or using strings??
thanks :)

Edited 1 Year Ago by Tcll

here's an idea that only needs slight modification:

for i,v in enumerate(
'VAL_NULL, VAL_INT, VAL_FLOAT, VAL_STR, VAL_ANY, VAL_CODE, VAL_MACRO, VAL_IDENT'.split(',')
): globals()[v]=i

not sure if I'm right on this or not, but ' ' and '\n' should be ignored right??
if not I could just do a simple replace... heh

EDIT: nope:

>>> globals()[' a']=10
>>> a

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    a
NameError: name 'a' is not defined
>>> 

Edited 1 Year Ago by Tcll

ok, so here's a nice little function that does just what I need it to :)
(note, it doesn't apply every aspect of an enum as expected, but that can be easily worked around) ;)

def enum(__enumerations='',__start=0, **kw):
    for i,v in enumerate(__enumerations.replace('\n','').replace(' ','').split(','),__start): globals()[v]=i
    for var,val in kw.items(): globals()[var]=val

so to show off, here's my converted python code:

enum('VAL_NULL, VAL_INT, VAL_FLOAT, VAL_STR, VAL_ANY, VAL_CODE, VAL_MACRO, VAL_IDENT')

enum('''
    CODE_START,
    CODE_OFFSET,
    CODE_POP,
    CODE_ENTER,
    CODE_EXIT,
    CODE_VAL,
    CODE_VALI,
    CODE_MACRO,
    CODE_BOOL,
    CODE_BLOCK,
    CODE_COMPILE,
    CODE_FORCE,
    CODE_RESULT,
    CODE_IDENT, CODE_IDENTU, CODE_IDENTARG,
    CODE_COM, CODE_COMD, CODE_COMC, CODE_COMV,
    CODE_CONC, CODE_CONCW, CODE_CONCM, CODE_DOWN,
    CODE_SVAR, CODE_SVAR1,
    CODE_IVAR, CODE_IVAR1, CODE_IVAR2, CODE_IVAR3,
    CODE_FVAR, CODE_FVAR1,
    CODE_LOOKUP, CODE_LOOKUPU, CODE_LOOKUPARG, CODE_ALIAS, CODE_ALIASU, CODE_ALIASARG, CODE_CALL, CODE_CALLU, CODE_CALLARG,
    CODE_PRINT,
    CODE_LOCAL''',

    CODE_OP_MASK = 0x3F,
    CODE_RET = 6,
    CODE_RET_MASK = 0xC0 ) # defines CODE_RET for the next enum

enum( #/* return type flags */
    RET_NULL   = VAL_NULL<<CODE_RET,
    RET_STR    = VAL_STR<<CODE_RET,
    RET_INT    = VAL_INT<<CODE_RET,
    RET_FLOAT  = VAL_FLOAT<<CODE_RET)

enum('ID_VAR, ID_FVAR, ID_SVAR, ID_COMMAND, ID_ALIAS, ID_LOCAL')

and here's the original C++ code:

enum { VAL_NULL = 0, VAL_INT, VAL_FLOAT, VAL_STR, VAL_ANY, VAL_CODE, VAL_MACRO, VAL_IDENT };

enum
{
    CODE_START = 0,
    CODE_OFFSET,
    CODE_POP,
    CODE_ENTER,
    CODE_EXIT,
    CODE_VAL,
    CODE_VALI,
    CODE_MACRO,
    CODE_BOOL,
    CODE_BLOCK,
    CODE_COMPILE,
    CODE_FORCE,
    CODE_RESULT,
    CODE_IDENT, CODE_IDENTU, CODE_IDENTARG,
    CODE_COM, CODE_COMD, CODE_COMC, CODE_COMV,
    CODE_CONC, CODE_CONCW, CODE_CONCM, CODE_DOWN,
    CODE_SVAR, CODE_SVAR1,
    CODE_IVAR, CODE_IVAR1, CODE_IVAR2, CODE_IVAR3,
    CODE_FVAR, CODE_FVAR1,
    CODE_LOOKUP, CODE_LOOKUPU, CODE_LOOKUPARG, CODE_ALIAS, CODE_ALIASU, CODE_ALIASARG, CODE_CALL, CODE_CALLU, CODE_CALLARG,
    CODE_PRINT,
    CODE_LOCAL,

    CODE_OP_MASK = 0x3F,
    CODE_RET = 6,
    CODE_RET_MASK = 0xC0,

    /* return type flags */
    RET_NULL   = VAL_NULL<<CODE_RET,
    RET_STR    = VAL_STR<<CODE_RET,
    RET_INT    = VAL_INT<<CODE_RET,
    RET_FLOAT  = VAL_FLOAT<<CODE_RET,
};

enum { ID_VAR, ID_FVAR, ID_SVAR, ID_COMMAND, ID_ALIAS, ID_LOCAL };

Edited 1 Year Ago by Tcll

Module enum is new in Python34

believe me, I've looked at this ;)
it's really no different from this except for a little less code:

for i,v in enumerate([
'VAL_NULL','VAL_INT','VAL_FLOAT','VAL_STR','VAL_ANY','VAL_CODE','VAL_MACRO','VAL_IDENT'
]): globals()[v]=i

good luck turning 150 variables into strings :P

the only good thing about it is it also doubles with this part of my method:

enum( #/* return type flags */
    RET_NULL   = VAL_NULL<<CODE_RET,
    RET_STR    = VAL_STR<<CODE_RET,
    RET_INT    = VAL_INT<<CODE_RET,
    RET_FLOAT  = VAL_FLOAT<<CODE_RET)

EDIT:
also, I prefer python27 for PyOpenGL's performance (dragged down in python3)

Edited 1 Year Ago by Tcll

Question Answered as of 2 Days Ago by vegaseat

uhh excuse me daniweb, but I do believe I answered my own question, and marked it as such when I posted my answer.

This question has already been answered. Start a new discussion instead.