#!/usr/bin/python # # Urwid split_repr helper functions # Copyright (C) 2004-2011 Ian Ward # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Urwid web site: http://excess.org/urwid/ from __future__ import division, print_function from urwid.compat import PYTHON3, bytes if not PYTHON3: from inspect import getargspec else: from inspect import getfullargspec def split_repr(self): """ Return a helpful description of the object using self._repr_words() and self._repr_attrs() to add to the description. This function may be used by adding code to your class like this: >>> class Foo(object): ... __repr__ = split_repr ... def _repr_words(self): ... return ["words", "here"] ... def _repr_attrs(self): ... return {'attrs': "appear too"} >>> Foo() <Foo words here attrs='appear too'> >>> class Bar(Foo): ... def _repr_words(self): ... return Foo._repr_words(self) + ["too"] ... def _repr_attrs(self): ... return dict(Foo._repr_attrs(self), barttr=42) >>> Bar() <Bar words here too attrs='appear too' barttr=42> """ alist = [(str(k), normalize_repr(v)) for k, v in self._repr_attrs().items()] alist.sort() words = self._repr_words() if not words and not alist: # if we're just going to print the classname fall back # to the previous __repr__ implementation instead return super(self.__class__, self).__repr__() if words and alist: words.append("") return "<%s %s>" % (self.__class__.__name__, " ".join(words) + " ".join(["%s=%s" % itm for itm in alist])) def normalize_repr(v): """ Return dictionary repr sorted by keys, leave others unchanged >>> normalize_repr({1:2,3:4,5:6,7:8}) '{1: 2, 3: 4, 5: 6, 7: 8}' >>> normalize_repr('foo') "'foo'" """ if isinstance(v, dict): items = [(repr(k), repr(v)) for k, v in v.items()] items.sort() return "{" + ", ".join([ "%s: %s" % itm for itm in items]) + "}" return repr(v) def python3_repr(v): """ Return strings and byte strings as they appear in Python 3 >>> python3_repr(u"text") "'text'" >>> python3_repr(bytes()) "b''" """ r = repr(v) if not PYTHON3: if isinstance(v, bytes): return 'b' + r if r.startswith('u'): return r[1:] return r def remove_defaults(d, fn): """ Remove keys in d that are set to the default values from fn. This method is used to unclutter the _repr_attrs() return value. d will be modified by this function. Returns d. >>> class Foo(object): ... def __init__(self, a=1, b=2): ... self.values = a, b ... __repr__ = split_repr ... def _repr_words(self): ... return ["object"] ... def _repr_attrs(self): ... d = dict(a=self.values[0], b=self.values[1]) ... return remove_defaults(d, Foo.__init__) >>> Foo(42, 100) <Foo object a=42 b=100> >>> Foo(10, 2) <Foo object a=10> >>> Foo() <Foo object> """ if not PYTHON3: args, varargs, varkw, defaults = getargspec(fn) else: args, varargs, varkw, defaults, _, _, _ = getfullargspec(fn) # ignore *varargs and **kwargs if varkw: del args[-1] if varargs: del args[-1] # create a dictionary of args with default values ddict = dict(list(zip(args[len(args) - len(defaults):], defaults))) for k in list(d.keys()): if k in ddict: # remove values that match their defaults if ddict[k] == d[k]: del d[k] return d def _test(): import doctest doctest.testmod() if __name__=='__main__': _test()
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
tests | Folder | 0755 |
|
|
__init__.py | File | 4.02 KB | 0644 |
|
_async_kw_event_loop.py | File | 9.17 KB | 0644 |
|
canvas.py | File | 40.75 KB | 0644 |
|
command_map.py | File | 3.26 KB | 0644 |
|
compat.py | File | 3.32 KB | 0644 |
|
container.py | File | 84.35 KB | 0644 |
|
curses_display.py | File | 19.85 KB | 0644 |
|
decoration.py | File | 40.63 KB | 0644 |
|
display_common.py | File | 32.9 KB | 0644 |
|
escape.py | File | 13.73 KB | 0644 |
|
font.py | File | 24.05 KB | 0644 |
|
graphics.py | File | 32.23 KB | 0644 |
|
html_fragment.py | File | 8 KB | 0644 |
|
lcd_display.py | File | 15.91 KB | 0644 |
|
listbox.py | File | 58.26 KB | 0644 |
|
main_loop.py | File | 49.43 KB | 0644 |
|
monitored_list.py | File | 16.45 KB | 0644 |
|
numedit.py | File | 10.65 KB | 0644 |
|
old_str_util.py | File | 9.83 KB | 0644 |
|
raw_display.py | File | 36.24 KB | 0644 |
|
signals.py | File | 12.42 KB | 0644 |
|
split_repr.py | File | 4.46 KB | 0644 |
|
str_util.cpython-310-x86_64-linux-gnu.so | File | 20.96 KB | 0644 |
|
text_layout.py | File | 17.79 KB | 0644 |
|
treetools.py | File | 15.3 KB | 0644 |
|
util.py | File | 13.07 KB | 0644 |
|
version.py | File | 143 B | 0644 |
|
vterm.py | File | 50.59 KB | 0644 |
|
web_display.py | File | 30.8 KB | 0644 |
|
widget.py | File | 60.21 KB | 0644 |
|
wimp.py | File | 21.46 KB | 0644 |
|