Skip to content

Instantly share code, notes, and snippets.

@alice1017
Created December 16, 2010 10:04
Show Gist options
  • Save alice1017/743250 to your computer and use it in GitHub Desktop.
Save alice1017/743250 to your computer and use it in GitHub Desktop.
リストの要素の中に=がついているものを、=の前の要素:=後の要素という形の辞書に変換するクラス。
#!/usr/bin/python
#coding:utf-8
class toDict():
"""
This class make dictionary from list.
if list have not '=' string, this class say error.
ex.)
>>> a = ['a=b','c=d']
>>> e = toDict(a)
>>> e.dict
{'a':'b', 'c':'d'}
This class make variable::
self.dict : this was maked by this class.
self.comments : this is a comments in list. (comment->';')
"""
__author__ = "Alice (http://twitter.com/Alice_Himmel)"
__vesion__ = "1.0"
__copyright__ = "Copyright (c) 2010 Alice "
def __init__(self,list):
_list = list
self.dict = {}
self.comments = []
if ( len(_list) == 0 ):
print "ListError! : list is empty"
else:
for i in _list:
if ( str(i)[0] == ";"):
unnver = str(i).strip()
self.comments.append(unnver)
else:
sp = i.split("=")
if ( (len(sp) == 1) or (len(sp) > 2) ):
pass
continue
else:
key = str(sp[0]).strip()
element = str(sp[1]).strip()
self.dict[key] = element
def help(self):
print self.__doc__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment