Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update exceptions.py #30

Merged
merged 1 commit into from May 23, 2014
Merged

update exceptions.py #30

merged 1 commit into from May 23, 2014

Conversation

@hsum
Copy link
Contributor

@hsum hsum commented May 19, 2014

Now, ResourceAttrError is a subclass of AttributeError. These exceptions can now be caught as AttributeError exceptions by application code or standard library stuff. getattr comes to mind.

Now, ResourceAttrError is a subclass of AttributeError.  These exceptions can now be caught as AttributeError exceptions by application code or standard library stuff.  getattr comes to mind.
@maxtepkeev
Copy link
Owner

@maxtepkeev maxtepkeev commented May 19, 2014

Can you provide a use case for this ?

@hsum
Copy link
Contributor Author

@hsum hsum commented May 20, 2014

I shall do my best! I'm new to this, so please bare with me here.

from itertools import groupby
from redminereports import make_redmine

getter_issue = lambda i: (i.project.id, i.project.name)

columns = (
    'start_date',
    'subject',
)

project_id = 133

redmine = make_redmine()

def render_value(v):
    try:
        return v.isoformat()
    except AttributeError:
        return v

for k, g in groupby(sorted(redmine.issue.filter(project_id = project_id), key = getter_issue), key = getter_issue):
    for i in g:
        print('\n'.join(
            render_value(getattr(i, c, "Column not found: %s" % c)) for c in columns
        ))

Without AttributeError as a base class getattr() doesn't know to use the default value and you get this:

(.ve)2014-05-20 10:04:16 [hsum@hqdc031 RedmineReports]$ python /tmp/test.py
Traceback (most recent call last):
  File "/tmp/test.py", line 26, in <module>
    render_value(getattr(i, c, "Column not found: %s" % c)) for c in columns
  File "/tmp/test.py", line 26, in <genexpr>
    render_value(getattr(i, c, "Column not found: %s" % c)) for c in columns
  File "/home/hsum/D/RedmineReports/.ve/local/lib/python2.7/site-packages/redmine/resources.py", line 365, in __getattr__
    return super(Issue, self).__getattr__(item)
  File "/home/hsum/D/RedmineReports/.ve/local/lib/python2.7/site-packages/redmine/resources.py", line 166, in __getattr__
    return self._action_if_attribute_absent()
  File "/home/hsum/D/RedmineReports/.ve/local/lib/python2.7/site-packages/redmine/resources.py", line 264, in _action_if_attribute_absent
    raise ResourceAttrError()
redmine.exceptions.ResourceAttrError: Resource doesn't have the requested attribute

When ResourceAttrError is an AttributeError, getattr() does it's job:

(.ve)2014-05-20 10:12:09 [hsum@hqdc031 RedmineReports]$ python /tmp/test.py
Column not found: start_date
八月安排輔訪觀察林昱圻
Column not found: start_date
八月進度調整與學習協助
Column not found: start_date
七月進度調整與學習協助
2014-05-19
五月份鎖定五位新入一年內數學受輔者名單
Column not found: start_date
八月安排輔訪觀察幼兒學習國語
Column not found: start_date
七月安排輔訪觀察學習國語的幼兒
Column not found: start_date
八月安排輔訪觀察林昱圻
Column not found: start_date
104年7月安排輔訪觀察林昱圻
2014-06-02
104年6月安排輔訪觀察林昱圻
2014-06-02
second time
Column not found: start_date
<stuff clipped>
@maxtepkeev maxtepkeev self-assigned this May 23, 2014
maxtepkeev added a commit that referenced this pull request May 23, 2014
issue #30
@maxtepkeev maxtepkeev merged commit 6425cb6 into maxtepkeev:master May 23, 2014
1 check passed
1 check passed
continuous-integration/travis-ci The Travis CI build passed
Details
@maxtepkeev
Copy link
Owner

@maxtepkeev maxtepkeev commented May 23, 2014

Agree, that's a needed feature. Thanks.

@hsum
Copy link
Contributor Author

@hsum hsum commented May 24, 2014

I'm glad I could chip in. Thank you for your work on this wonderful project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.