CSVファイルのヘッダー行があり、nameを含んでいる場合、
nameをハッシュ する。
import csv
"""おなじみ動的クラス"""
def Dynamic(name, fields):
""" returns a class object which has fields attribute.
the class is iterable."""
dic = dict([x,None] for x in fields)
o = type(name, (_dynamic,), dic)
o._fields = fields
return o
class _dynamic(object):
_fields = []
def __init__(self, *args, **kwargs):
for i,value in enumerate(args[0]):
setattr(self, self._fields[i], value)
def __iter__(self):
for field in self._fields:
yield getattr(self, field)
def __str__(self):
if len(self._fields) == 0:
return "[]"
l = "["
for field in self._fields:
l += getattr(self, field).__str__() + ", "
return l[0:-2] + "]"
def __setattr__(self, name, value):
if name == "_fields":
for field in set(self._fields) - set(value):
delattr(self, field)
for field in set(value) - set(self._fields):
super(_dynamic, self).__setattr__(field, None)
super(_dynamic, self).__setattr__(name, value)
if __name__ == "__main__":
reader = csv.reader(file(inputfilename))
writer = csv.writer(file(outputfilename, "w"))
Personal = Dynamic("Personal", reader.next())
for line in reader:
person = Personal(line)
person.name=hash(person.name)
writer.writerow(list(person))
2012年6月30日土曜日
動的に属性を変更可能なクラス (その2)
前回の内容では field を変更したときに値がすべて削除された。
できれば field が a, b, cから b, c, dに変更されたときにはb, c の値を保存しておきたいのでプログラムを修正。
def Dynamic(name, fields):
""" returns a class object which has fields attribute.
the class is iterable."""
dic = dict([x,None] for x in fields)
o = type(name, (_dynamic,), dic)
o._fields = fields
return o
class _dynamic(object):
_fields = []
def __init__(self, *args, **kwargs):
for i,value in enumerate(args[0]):
setattr(self, self._fields[i], value)
def __iter__(self):
for field in self._fields:
yield getattr(self, field)
def __setattr__(self, name, value):
if name == "_fields":
for field in set(self._fields) - set(value):
delattr(self, field)
for field in set(value) - set(self._fields):
super(_dynamic, self).__setattr__(field, None)
super(_dynamic, self).__setattr__(name, value)
できれば field が a, b, cから b, c, dに変更されたときにはb, c の値を保存しておきたいのでプログラムを修正。
def Dynamic(name, fields):
""" returns a class object which has fields attribute.
the class is iterable."""
dic = dict([x,None] for x in fields)
o = type(name, (_dynamic,), dic)
o._fields = fields
return o
class _dynamic(object):
_fields = []
def __init__(self, *args, **kwargs):
for i,value in enumerate(args[0]):
setattr(self, self._fields[i], value)
def __iter__(self):
for field in self._fields:
yield getattr(self, field)
def __setattr__(self, name, value):
if name == "_fields":
for field in set(self._fields) - set(value):
delattr(self, field)
for field in set(value) - set(self._fields):
super(_dynamic, self).__setattr__(field, None)
super(_dynamic, self).__setattr__(name, value)
2012年6月24日日曜日
動的に属性を変更可能なクラス
namedtupleはとても便利だが、値の変更ができない。
値の変更可能な、エセnamedtuple を作ってみました。
namedtuple にはあってこれにはない属性がいろいろあるけど、そこは追々作るということで。
def Dynamic(name, fields):
""" returns a class object which has fields attribute.
the class is iterable."""
dic = dict([x,None] for x in fields)
o = type(name, (_dynamic,), dic)
o._fields = fields
return o
class _dynamic(object):
_fields = []
def __init__(self, *args, **kwargs):
for i,value in enumerate(args[0]):
setattr(self, self._fields[i], value)
def __iter__(self):
for field in self._fields:
yield getattr(self, field)
def __setattr__(self, name, value):
if name == "_fields":
for field in self._fields:
delattr(self, field)
for field in value:
super(_dynamic, self).__setattr__(field, None)
super(_dynamic, self).__setattr__(name, value)
値の変更可能な、エセnamedtuple を作ってみました。
namedtuple にはあってこれにはない属性がいろいろあるけど、そこは追々作るということで。
def Dynamic(name, fields):
""" returns a class object which has fields attribute.
the class is iterable."""
dic = dict([x,None] for x in fields)
o = type(name, (_dynamic,), dic)
o._fields = fields
return o
class _dynamic(object):
_fields = []
def __init__(self, *args, **kwargs):
for i,value in enumerate(args[0]):
setattr(self, self._fields[i], value)
def __iter__(self):
for field in self._fields:
yield getattr(self, field)
def __setattr__(self, name, value):
if name == "_fields":
for field in self._fields:
delattr(self, field)
for field in value:
super(_dynamic, self).__setattr__(field, None)
super(_dynamic, self).__setattr__(name, value)
登録:
投稿 (Atom)