Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

AttributeError: 'Candidate' object has no attribute '__unicode__' #317

Open
actongorton opened this issue May 24, 2018 · 3 comments
Open
Labels

Comments

@actongorton
Copy link

To recreate:

from elex import api
e = api.Election(electiondate='2015-11-03', datafile=None, testresults=False, liveresults=True, is_test=False)
candidates = e.candidates
candidates

Traceback:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/venv-science/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/venv-science/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    381                 if cls in self.type_pprinters:
    382                     # printer registered in self.type_pprinters
--> 383                     return self.type_pprinters[cls](obj, self, cycle)
    384                 else:
    385                     # deferred printer

~/venv-science/lib/python3.6/site-packages/IPython/lib/pretty.py in inner(obj, p, cycle)
    559                 p.text(',')
    560                 p.breakable()
--> 561             p.pretty(x)
    562         if len(obj) == 1 and type(obj) is tuple:
    563             # Special case for 1-item tuples.

~/venv-science/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    398                         if cls is not object \
    399                                 and callable(cls.__dict__.get('__repr__')):
--> 400                             return _repr_pprint(obj, self, cycle)
    401 
    402             return _default_pprint(obj, self, cycle)

~/venv-science/lib/python3.6/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    693     """A pprint that just redirects to the normal repr function."""
    694     # Find newlines and replace them with p.break_()
--> 695     output = repr(obj)
    696     for idx,output_line in enumerate(output.splitlines()):
    697         if idx:

~/venv-science/lib/python3.6/site-packages/elex/api/utils.py in __repr__(self)
     26 
     27     def __repr__(self):
---> 28         return '<{}: {}>'.format(self.__class__.__name__, self.__str__())
     29 
     30 

~/venv-science/lib/python3.6/site-packages/elex/api/utils.py in <lambda>(x)
     21     """
     22     if sys.version_info > (3, 0):
---> 23         __str__ = lambda x: x.__unicode__()
     24     else:
     25         __str__ = lambda x: six.text_type(x).encode('utf-8')

AttributeError: 'Candidate' object has no attribute '__unicode__'

@mileswwatkins
Copy link
Contributor

mileswwatkins commented Jun 8, 2018

Looks like most of the classes in models.py lack __unicode__ methods, which are how they're stringified when printed. A PR adding those methods should be pretty quick for you to whip up, if debug prints like that are valuable on your end.

eg, https://github.com/newsdev/elex/blob/master/elex/api/models.py#L788

__unicode__ seems to be the best bet (vs __str__ or __repr__) for support across both Python 2 and 3.

@palewire
Copy link
Contributor

This would be a good add, if anybody is interested in crafting a pull request.

@palewire palewire added the bug label Jun 16, 2018
@shmcminn
Copy link

Does class elex.api.utils.UnicodeMixin address this? If so, how would you use it to print(e.candidates)?

https://elex.readthedocs.io/en/stable/reference/api.html?highlight=python#elex.api.utils.UnicodeMixin

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants