Dictionary of "structs" with functions as components?
Roman Suzi
rnd at onego.ru
Sun Apr 29 16:41:53 EDT 2001
On Sun, 29 Apr 2001, Eric Sandeen wrote:
>First a disclaimer - I'm pretty new to Python, and OOP is not really my
>bag, so forgive me if this is a silly question...
With Python OOP is everybody's bag, look below.
>I'm trying to create a data structure which holds information about
>several different filesystems - for example: ext2, reiserfs, and xfs. I'd
>like this to be easily extensible to add other filesystems, as well. The
>type of information I'm including is name, magic number, position of magic
>number in superblock, etc. So far, I'm doing something like this (these
>values aren't right, just an example) :
>
>FSystems['ext2'].PrettyName = "ext2 filesystem"
>FSystems['ext2'].MagicNo = 0xce32
>FSystems['ext2'].MagicNoPos = 1080
>
>FSystems['xfs'].PrettyName = "XFS filesystem"
>FSystems['xfs'].MagicNo = "XFSB"
>FSystems['xfs'].MagicNoPos = 0
Why not to use objects?
class FS:
def __init__(self, name, **ats):
self.name = name
self.__dict__.update(ats) # ;-)
class FSystems:
def __init__(self, list=None):
self.fs = {}
for k in list or []:
self.fs[k.name] = k
FSystems([
FS('xfs',
PrettyName="XFS filesystem",
MagicNo="XFSB",
MagicNoPos=0,
create=fs_creation("man xfs"), # ;-)
),
FS('ext2',
PrettyName="ext2 filesystem",
MagicNo=0xce32,
MagicNoPos=1080,
create=fs_creation("mkfs.ext2"),
),
])
(Of course, FSystems is not necessary: you can put FS(..)'s into dict directly.)
>The problem comes when I'd like to define a unique function for each
>filesystem to actually create the filesystem, since this will vary quite a
>bit from fs to fs. I'd like to access it via something like
>
>FSystems[<fstype>].unique_fs_function()
If I understood correctly, you need just this:
import os
def fs_creation(cmd):
def template(keys="", os_cmd=cmd):
os.system(os_cmd+" "+keys)
return template
Of course, it's overkill.
You can simply do it like this:
def ext2_fs_creation(keys=""):
return # to play safe
os.system("mkfs.ext2 "+keys) # error handling to be added!
And you will be able to use it like this:
FS('ext2',
PrettyName = "ext2 filesystem",
MagicNo = 0xce32,
MagicNoPos = 1080,
create_fs = ext2_fs_creation,
),
And then you can call it:
a = FSystems.fs['ext2'].create("/dev/sda1")
>In C, I'd have a pointer to the function I want. Is something like this
>possible in Python?
OK. Overall idea is that function is no different
from dictionary or other object in this respect: you can bind it
names you like and put it into containers you like!
Sincerely yours, Roman Suzi
--
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Sunday, April 29, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "Macho does not prove Mucho." _/
Working listing:
#!/usr/bin/env python
class FS:
def __init__(self, name, **ats):
self.name = name
self.__dict__.update(ats) # ;-)
class FSystems:
def __init__(self, list=None):
self.fs = {}
for k in list or []:
self.fs[k.name] = k
import os
def fs_creation(cmd):
def template(keys="", os_cmd=cmd):
os.system("echo "+os_cmd+" "+keys) # safety added!!!
return template
# description must be compact:
myfss = FSystems([
FS('xfs',
PrettyName="XFS filesystem",
MagicNo="XFSB",
MagicNoPos=0,
create=fs_creation("man xfs"), # ;-)
),
FS('ext2',
PrettyName="ext2 filesystem",
MagicNo=0xce32,
MagicNoPos=1080,
create=fs_creation("mkfs.ext2"),
),
FS('ntfs',
PrettyName="ntfs filesystem",
MagicNo=0x0000,
MagicNoPos=666,
create=None, # except M$
),
])
myfss.fs['ext2'].create("/dev/sda1")
More information about the Python-list
mailing list