From: Jason Robinson Date: Wed, 3 Jul 2013 09:52:57 +0000 (+0300) Subject: Allow initializing Aspect class also using name. Add GitHub standard Python .gitignore X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=4b7d7125d9e17d45c6666e304949e114da996c64;p=diaspy.git Allow initializing Aspect class also using name. Add GitHub standard Python .gitignore --- diff --git a/.gitignore b/.gitignore index 87538bd..dd5cdc1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,10 +3,45 @@ docs/build/* */__pycache__/* __pycache__/* -*.pyc .env check-*.py testconf.py TEST_COUNT + +*.py[cod] + +# C extensions +*.so + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg +lib +lib64 + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox +nosetests.xml + +# Translations +*.mo + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject diff --git a/diaspy/models.py b/diaspy/models.py index cc4b0a4..d4f6d7d 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -12,22 +12,42 @@ MUST NOT import anything. class Aspect(): """This class represents an aspect. + + Class can be initialized by passing either an id and/or name as + parameters. + If both are missing, an exception will be raised. """ - def __init__(self, connection, id=-1): + def __init__(self, connection, id=None, name=None): self._connection = connection - self.id = id - self.name = self._findname() + self.id, self.name = id, name + if id and not name: + self.name = self._findname() + elif name and not id: + self.id = self._findid() + elif not id and not name: + raise Exception("Aspect must be initialized with either an id or name") def _findname(self): """Finds name for aspect. """ - name = '' + name = None aspects = self._connection.getUserInfo()['aspects'] for a in aspects: if a['id'] == self.id: name = a['name'] break return name + + def _findid(self): + """Finds id for aspect. + """ + id = None + aspects = self._connection.getUserInfo()['aspects'] + for a in aspects: + if a['name'] == self.name: + id = a['id'] + break + return id def getUsers(self): """Returns list of GUIDs of users who are listed in this aspect.