I have to write a library in python which was written in Java before. Coming from a Java background python is giving me a little difficult time. I am stuck with choosing the right pythonic way of doing something..
So, My java code is something like:
import java.util.Collection;
public abstract class MyEnumBaseClass
{
protected int value;
protected String description = null;
protected MyEnumBaseClass(int iValue, String iDescription)
{
value = iValue;
description = iDescription;
}
public int getValue()
{
return value;
}
public String getDescription()
{
return description;
}
protected static MyEnumBaseClass getEnum(Collection<MyEnumBaseClass> iter, int value)
{
for (MyEnumBaseClass enumObj : iter)
{
if (enumObj.getValue() == value)
{
return enumObj;
}
}
return null;
}
}
import java.util.ArrayList;
import java.util.Collection;
public class MyEnumClass extends MyEnumBaseClass
{
private final static Collection<MyEnumBaseClass> enumList = new ArrayList<MyEnumBaseClass>();
public final static int ERROR1 = 1;
public final static int ERROR2 = 2;
public final static int ERROR3 = 3;
public final static int ERROR4 = 4;
public final static MyEnumClass ErrorEnum1 = new MyEnumClass(ERROR1, "ERROR1");
public final static MyEnumClass ErrorEnum2 = new MyEnumClass(ERROR2, "ERROR1");
public final static MyEnumClass ErrorEnum3 = new MyEnumClass(ERROR3, "ERROR1");
public final static MyEnumClass ErrorEnum4 = new MyEnumClass(ERROR4, "ERROR1");
protected MyEnumClass(int iValue, String iDescription)
{
super(iValue, iDescription);
}
public static int getCount()
{
return enumList.size();
}
public static Collection<MyEnumBaseClass> getList()
{
return enumList;
}
public static MyEnumBaseClass getEnum(int value)
{
return getEnum(enumList, value);
}
}
I want to write something this in python. I understand both languages are totally different. I don't want to replicate exact code but I want to write something in python which give me the functionality Java code is giving.
So I came up with something like:
# MODULE MYENUMBASECLASS:::
class MyEnumBaseClass(object):
def __init__(self, iValue, iDescription, ui = None):
self._value = iValue
self._description = iDescription
def getValue(self):
return self._value
def getDescription(self):
return self._description
@classmethod
def getEnum(cls, value, itr):
for enumObj in itr:
if enumObj.getValue() == value:
return enumObj
return None
# MODULE: ENUMS:::
from MyEnumBaseClass import MyEnumBaseClass
__all__ = ["MyEnumClassConstants", "MyEnumClass", "MyEnums"]
_enumList = []
class MyEnumClassConstants(object):
ERROR1 = 1
ERROR2 = 2
ERROR3 = 3
ERROR4 = 4
class MyEnumClass(MyEnumBaseClass):
def __init__(self, v, d, ui):
global _enumList
super(MyEnumClass, self).__init__(v, d, ui)
_enumList.append(self)
@staticmethod
def getCount():
return len(_enumList)
@staticmethod
def getList():
return _enumList
@classmethod
def getEmum(cls, value, itr = None):
return super(MyEnumClass, cls).getEnum(value, _enumList)
class MyEnums(object):
ErrorEnum1 = MyEnumClass(MyEnumClassConstants.ERROR1, "ERROR1");
ErrorEnum2 = MyEnumClass(MyEnumClassConstants.ERROR2, "ERROR2");
ErrorEnum3 = MyEnumClass(MyEnumClassConstants.ERROR3, "ERROR3");
ErrorEnum4 = MyEnumClass(MyEnumClassConstants.ERROR4, "ERROR4");
I Want to know:
Is it the correct pythonic way of doing it?
I wanted to move the ErrorEnum1,2,3,4 and constants out of the MyEnums class as a module variable. But that way I will have a long list in my all variable. also I have a risk of variable name clash when I will import Enums module in other module (some other Enums2 module may also have ErrorEnum1,2,3.. But that is not a big problem. We can always use Enums.ErrorEnum1 and Enums2.ErrorEnum1). Am I thinking right?
I know this is not perfect (my first python code ever). So I invite you guys for giving me ideas.
Thanks