Pythonic way of rewrite Java code in Python -
i have write library in python written in java before. coming java background python giving me little difficult time. stuck choosing right pythonic way of doing something..
so, java code 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) { (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 write in python. understand both languages totally different. don't want replicate exact code want write in python give me functionality java code giving.
so came 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): enumobj in itr: if enumobj.getvalue() == value: return enumobj return none # module: enums::: 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 know:
is correct pythonic way of doing it?
i wanted move errorenum1,2,3,4 , constants out of myenums class module variable. way have long list in all variable. have risk of variable name clash when import enums module in other module (some other enums2 module may have errorenum1,2,3.. not big problem. can use enums.errorenum1 , enums2.errorenum1). thinking right?
i know not perfect (my first python code ever). invite guys giving me ideas.
thanks
well, suppose aware code less optimal , not way go. problem cannot how can "write in python give me functionality java code giving" because not know trying do. said, there lot of obvious java bias in code 1 can remove without problem:
first, why have
myenumbaseclass
,myenumclass
? can have one. reduce number of classes , number of modules. if want extend enum, see that, after simplifying code,myenumclass
simple can extend without problems.now, please, no getters , setters. have no reason use them, since have properties. if properties , set values, not use properties anyway:
class myenumclass(object): def __init__(self, value, description, ui = none): self.value = value self.description = description
it ugly create class hold constant-like values, such
myenumclassconstants
,myenums
. create variables in module level. let see how after item below.also, why
getenum()
classmethod? can mere function , have not worry conflicts because inside module:def getenum(value, itr): enumobj in itr: if enumobj.value == value: return enumobj return none
if going set variables sequential numbers, may want use unpacking idiom:
( error1, error2, error3, error4 ) = range(1, 5)
this idiom can used create list of enums, too:
_enums = ( errorenum1, errorenum2, errorenum3, errorenum4 ) = ( myenumclass(error1, "error1"), myenumclass(error2, "error2"), myenumclass(error3, "error3"), myenumclass(error4, "error4") )
to honest, happily leave
_enums
public member of module let take easy java-itis :p didgetenum()
, let other classmethods: declare them functions in module:def getcount(): return len(_enums) def getlist(): return _enums
we can improve
getenum()
changing default parameter:def getenum(value, itr=_enums): enumobj in itr: if enumobj.value == value: return enumobj return none
i happily ban
__all__
declaration here. thing not part of interface_enums
tuple, , preceded_
which, according pep-8, means should not used externally. let stay. module has new interface, more constants , functions:__all__ = ["myenumclass", "error1", "error2", "error3", "error4", "errorenum1", "errorenum2", "errorenum3", "errorenum4", "getcount", "getlist", "getenum"]
it seems better remove
myenumclass
interface well, may want use leave it. note,__all__
value does not avoid access components of module. changes documentation
the end result this:
__all__ = ["myenumclass", "error1", "error2", "error3", "error4", "errorenum1", "errorenum2", "errorenum3", "errorenum4", "getcount", "getlist", "getenum"] class myenumclass(object): def __init__(self, value, description, ui = none): self.value = value self.description = description ( error1, error2, error3, error4 ) = range(1, 5) _enums = ( errorenum1, errorenum2, errorenum3, errorenum4 ) = ( myenumclass(error1, "error1"), myenumclass(error2, "error2"), myenumclass(error3, "error3"), myenumclass(error4, "error4") ) def getcount(): return len(_enums) def getlist(): return _enums def getenum(value, itr=_enums): enumobj in itr: if enumobj.value == value: return enumobj return none
(edited) not simpler. if create error codes, merely create error1
, error2
etc. variables - no classes, no functions, values in variables. actually, idea of creating error codes seems inappropriate: should prefer create exceptions since, zen of python states, errors should never pass silently (or, eric raymond's unix philosophy says, when must fail, fail noisily , possible). nonetheless, bet changes i've made can give more precise bit of taste of writing python.
you may feel itches doing things way, believe me, best way. people may disagree in points me idea 1 presented. java developer, important dance music of language - not try force foreign concepts it.
finally, important references:
- pep-20 - zen of python: poetic listing of core values in developing in python , python.
- pep-8 - style guide python. read right now.
- python not java - 1 of best guides avoiding inappropriate java habits in python.
Comments
Post a Comment