Em Tue, May 19, 2015 at 04:34:27PM -0300, Arnaldo Carvalho de Melo escreveu:
Em Tue, May 19, 2015 at 05:20:21PM +0200, Petr Oros escreveu:
> > > It's conversion config <-> filesystem. Direct access is bad.
Tuna
> > > can
> > Why is it bad?
> > > use config ONLY over this class. I don't know
where can use this
> > > conversion directly. Class method protect to potential bad use.
> > Can you think of one?
> These helpers are related to config class code. Python is object
That is perfectly ok to leave them in that file then, just like lots
of
other global functions in the tuna code.
> oriented language and, i just don't see reason why move
functions
They are not related to the config class code, if they were it would
be
using self, no? I.e. are those methods dealing with internal state of
this class?
The fact that a language is object oriented doesn't preclude
using
normal functions.
> somewhere into space. I created them for better outline and for
> specific usage, not globally for some magic anywhere.
There is no magic involved, its just logic, the code is not related
to
the class as it doesn't touch its internal state.
But if you insist that it be tied to the class, at least lets use the
right mechanism, static methods:
https://julien.danjou.info/blog/2013/guide-python-static-class-abstract-m...
-----------------------------------------------------------------------
Static methods
Static methods are a special case of methods. Sometimes, you'll write
code that belongs to a class, but that doesn't use the object itself at
all. For example:
class Pizza(object):
@staticmethod
def mix_ingredients(x, y):
return x + y
def cook(self):
return self.mix_ingredients(self.cheese, self.vegetables)
-----------------------------------------------------------------------
I.e. remove that self, is has no business being there, it is unused.
So it would be:
def FileNameToConfigPath(filename):
return filename.replace(".", "\\.").replace("/",
".")
def ConfigPathToFileName(configpath):
return configpath.replace(".", "/").replace("\\/",
".")
Testing here with this code:
---------------------------------------------------------------------------
[acme@zoo ~]$ cat static_method.py
import sys
class Config:
def __init__(self, filename):
self.configpath = self.FileNameToConfigPath(filename)
@staticmethod
def FileNameToConfigPath(filename):
return filename.replace(".",
"\\.").replace("/", ".")
@staticmethod
def ConfigPathToFileName(configpath):
return configpath.replace(".",
"/").replace("\\/", ".")
def show(self):
print "config path=%s\n filename=%s" % (self.configpath,
self.ConfigPathToFileName(self.configpath))
def main(argv):
cfg = Config(argv[1])
cfg.show()
if __name__ == '__main__':
main(sys.argv)
[acme@zoo ~]$ python static_method.py /proc/sys/net/ipv4/conf/tuna.1
config path=.proc.sys.net.ipv4.conf.tuna\.1
filename=/proc/sys/net/ipv4/conf/tuna.1
[acme@zoo ~]$
---------------------------------------------------------------------------
Then, as soon as, if ever, we need that function elsewhere, we either make it a
global function or we do some copy and paste and have a replica of this static
method, which would be a waste :-)
- Arnaldo