From a6a36368805404547b08b37fca182f818e1e388e Mon Sep 17 00:00:00 2001 From: Griatch Date: Thu, 31 Jan 2019 23:44:16 +0100 Subject: [PATCH] Clean up unicode mentions in docstrings --- evennia/contrib/mapbuilder.py | 2 +- evennia/typeclasses/admin.py | 6 +++--- evennia/utils/ansi.py | 36 +++++++++++++------------------- evennia/utils/batchprocessors.py | 2 +- evennia/utils/evform.py | 3 +-- evennia/utils/evtable.py | 9 -------- evennia/utils/picklefield.py | 14 +++---------- evennia/utils/utils.py | 17 +++++++++------ 8 files changed, 34 insertions(+), 55 deletions(-) diff --git a/evennia/contrib/mapbuilder.py b/evennia/contrib/mapbuilder.py index b4cc70778e..12b23a4a10 100644 --- a/evennia/contrib/mapbuilder.py +++ b/evennia/contrib/mapbuilder.py @@ -323,7 +323,7 @@ def build_map(caller, game_map, legend, iterations=1, build_exits=True): for y in range(len(game_map)): for x in range(len(game_map[y])): for key in legend: - # obs - we must use == for unicode + # obs - we must use == for strings if game_map[y][x] == key: room = legend[key](x, y, iteration=iteration, room_dict=room_dict, diff --git a/evennia/typeclasses/admin.py b/evennia/typeclasses/admin.py index 7a4aba17bd..a23b1967fe 100644 --- a/evennia/typeclasses/admin.py +++ b/evennia/typeclasses/admin.py @@ -213,7 +213,7 @@ class AttributeForm(forms.ModelForm): self.instance.attr_key = attr_key self.instance.attr_category = attr_category self.instance.attr_value = attr_value - # prevent set from being transformed to unicode + # prevent set from being transformed to str if isinstance(attr_value, set) or isinstance(attr_value, _SaverSet): self.fields['attr_value'].disabled = True self.instance.deserialized_value = from_pickle(attr_value) @@ -242,8 +242,8 @@ class AttributeForm(forms.ModelForm): def clean_attr_value(self): """ - Prevent Sets from being cleaned due to literal_eval failing on them. Otherwise they will be turned into - unicode. + Prevent Sets from being cleaned due to literal_eval failing on them. Otherwise they will be turned into str. + """ data = self.cleaned_data['attr_value'] initial = self.instance.attr_value diff --git a/evennia/utils/ansi.py b/evennia/utils/ansi.py index 350145e186..6ba848dfb9 100644 --- a/evennia/utils/ansi.py +++ b/evennia/utils/ansi.py @@ -613,7 +613,7 @@ def _transform(func_name): class ANSIMeta(type): """ - Many functions on ANSIString are just light wrappers around the unicode + Many functions on ANSIString are just light wrappers around the string base class. We apply them here, as part of the classes construction. """ @@ -637,10 +637,9 @@ class ANSIString(with_metaclass(ANSIMeta, str)): """ Unicode-like object that is aware of ANSI codes. - This class can be used nearly identically to unicode, in that it will - report string length, handle slices, etc, much like a unicode or - string object would. The methods should be used identically as unicode - methods are. + This class can be used nearly identically to strings, in that it will + report string length, handle slices, etc, much like a string object + would. The methods should be used identically as string methods are. There is at least one exception to this (and there may be more, though they have not come up yet). When using ''.join() or u''.join() on an @@ -735,18 +734,11 @@ class ANSIString(with_metaclass(ANSIMeta, str)): ANSI parser with one of your own syntax if you wish, so long as it implements the same interface. - The second is the _raw_string. It should be noted that the ANSIStrings - are unicode based. This seemed more reasonable than basing it off of - the string class, because if someone were to use a unicode character, - the benefits of knowing the indexes of the ANSI characters would be - negated by the fact that a character within the string might require - more than one byte to be represented. The raw string is, then, a - unicode object rather than a true encoded string. If you need the - encoded string for sending over the wire, try using the .encode() - method. + The second is the _raw_string. This is the original "dumb" string + with ansi escapes that ANSIString represents. - The third thing to set is the _clean_string. This is a unicode object - that is devoid of all ANSI Escapes. + The third thing to set is the _clean_string. This is a string that is + devoid of all ANSI Escapes. Finally, _code_indexes and _char_indexes are defined. These are lookup tables for which characters in the raw string are related to ANSI @@ -894,20 +886,20 @@ class ANSIString(with_metaclass(ANSIMeta, str)): def clean(self): """ - Return a unicode object without the ANSI escapes. + Return a string object *without* the ANSI escapes. Returns: - clean_string (unicode): A unicode object with no ANSI escapes. + clean_string (str): A unicode object with no ANSI escapes. """ return self._clean_string def raw(self): """ - Return a unicode object with the ANSI escapes. + Return a string object with the ANSI escapes. Returns: - raw (unicode): A unicode object with the raw ANSI escape sequences. + raw (str): A unicode object *with* the raw ANSI escape sequences. """ return self._raw_string @@ -951,7 +943,7 @@ class ANSIString(with_metaclass(ANSIMeta, str)): readable characters, and one which contains the indexes of all ANSI escapes. It's important to remember that ANSI escapes require more that one character at a time, though no readable character needs more - than one character, since the unicode base class abstracts that away + than one character, since the string base class abstracts that away from us. However, several readable characters can be placed in a row. We must use regexes here to figure out where all the escape sequences @@ -1229,7 +1221,7 @@ class ANSIString(with_metaclass(ANSIMeta, str)): NOTE: This should always be used for joining strings when ANSIStrings are involved. Otherwise color information will be discarded by - python, due to details in the C implementation of unicode strings. + python, due to details in the C implementation of strings. Args: iterable (list of strings): A list of strings to join together diff --git a/evennia/utils/batchprocessors.py b/evennia/utils/batchprocessors.py index a35e6d37f8..ac2e27a734 100644 --- a/evennia/utils/batchprocessors.py +++ b/evennia/utils/batchprocessors.py @@ -220,7 +220,7 @@ def read_batchfile(pythonpath, file_ending='.py'): decoderr = [] for abspath in abspaths: # try different paths, until we get a match - # we read the file directly into unicode. + # we read the file directly into string. for file_encoding in _ENCODINGS: # try different encodings, in order try: diff --git a/evennia/utils/evform.py b/evennia/utils/evform.py index f1f00b3f2e..f8affb7eb7 100644 --- a/evennia/utils/evform.py +++ b/evennia/utils/evform.py @@ -86,8 +86,7 @@ Use as follows: form.map(tables={"A": tableA, "B": tableB}) - # unicode is required since the example contains non-ascii characters - print unicode(form) + print(form) ``` This produces the following result: diff --git a/evennia/utils/evtable.py b/evennia/utils/evtable.py index 6d80b60d05..6112cb58b3 100644 --- a/evennia/utils/evtable.py +++ b/evennia/utils/evtable.py @@ -3,10 +3,6 @@ This is an advanced ASCII table creator. It was inspired by [prettytable](https://code.google.com/p/prettytable/) but shares no code. -> Note: to test ANSI colors on the command line you need to call the -printed table in a unicode() call, like print unicode(table). This is -due to a bug in the python interpreter and print. - Example usage: ```python @@ -140,7 +136,6 @@ def _to_ansi(obj): return ANSIString(obj) -_unicode = str _whitespace = '\t\n\x0b\x0c\r ' @@ -167,8 +162,6 @@ class ANSITextWrapper(TextWrapper): # if self.replace_whitespace: # if isinstance(text, str): # text = text.translate(self.whitespace_trans) -# elif isinstance(text, _unicode): -# text = text.translate(self.unicode_whitespace_trans) # return text def _split(self, text): @@ -440,7 +433,6 @@ class EvCell(object): self.align = kwargs.get("align", "l") self.valign = kwargs.get("valign", "c") - # self.data = self._split_lines(unicode(data)) self.data = self._split_lines(_to_ansi(data)) self.raw_width = max(m_len(line) for line in self.data) self.raw_height = len(self.data) @@ -740,7 +732,6 @@ class EvCell(object): `EvCell.__init__`. """ - # self.data = self._split_lines(unicode(data)) self.data = self._split_lines(_to_ansi(data)) self.raw_width = max(m_len(line) for line in self.data) self.raw_height = len(self.data) diff --git a/evennia/utils/picklefield.py b/evennia/utils/picklefield.py index 50eb26f035..0e7f12bba4 100644 --- a/evennia/utils/picklefield.py +++ b/evennia/utils/picklefield.py @@ -35,23 +35,15 @@ from copy import deepcopy from base64 import b64encode, b64decode from zlib import compress, decompress # import six # this is actually a pypy component, not in default syslib -import django from django.core.exceptions import ValidationError from django.db import models -# django 1.5 introduces force_text instead of force_unicode from django.forms import CharField, Textarea from django.forms.utils import flatatt from django.utils.html import format_html -from evennia.utils.dbserialize import from_pickle, to_pickle -from future.utils import with_metaclass from pickle import loads, dumps - -try: - from django.utils.encoding import force_text -except ImportError: - from django.utils.encoding import force_unicode as force_text +from django.utils.encoding import force_text DEFAULT_PROTOCOL = 4 @@ -188,10 +180,10 @@ class PickledObjectField(models.Field): """ Returns the default value for this field. - The default implementation on models.Field calls force_unicode + The default implementation on models.Field calls force_text on the default, which means you can't set arbitrary Python objects as the default. To fix this, we just return the value - without calling force_unicode on it. Note that if you set a + without calling force_text on it. Note that if you set a callable as a default, the field will still call it. It will *not* try to pickle and encode it. diff --git a/evennia/utils/utils.py b/evennia/utils/utils.py index 40391d0bfc..9e0ca7174f 100644 --- a/evennia/utils/utils.py +++ b/evennia/utils/utils.py @@ -738,16 +738,21 @@ _UNICODE_MAP = {"EM DASH": "-", "FIGURE DASH": "-", "EN DASH": "-", "HORIZONTAL "HORIZONTAL ELLIPSIS": "...", "RIGHT SINGLE QUOTATION MARK": "'"} -def latinify(unicode_string, default='?', pure_ascii=False): +def latinify(string, default='?', pure_ascii=False): """ Convert a unicode string to "safe" ascii/latin-1 characters. - This is used as a last resort when normal decoding does not work. + This is used as a last resort when normal encoding does not work. Arguments: - unicode_string (unicode): A string to convert to an ascii - or latin-1 string. + string (str): A string to convert to 'safe characters' convertable + to an latin-1 bytestring later. default (str, optional): Characters resisting mapping will be replaced - with this character or string. + with this character or string. The intent is to apply an encode operation + on the string soon after. + + Returns: + string (str): A 'latinified' string where each unicode character has been + replaced with a 'safe' equivalent available in the ascii/latin-1 charset. Notes: This is inspired by the gist by Ricardo Murri: https://gist.github.com/riccardomurri/3c3ccec30f037be174d3 @@ -757,7 +762,7 @@ def latinify(unicode_string, default='?', pure_ascii=False): from unicodedata import name converted = [] - for unich in iter(unicode_string): + for unich in iter(string): try: ch = unich.decode('ascii') except UnicodeDecodeError: