Gideros textwrap5/3/2023 ![]() This will be triggered by Gideros whenever an ui element will be drawn on the screen.(Stage) like an image. There is one more important event lies there in the code which is Event.ADDED_TO_STAGE. (You can change the name of the called function any rename it, although as a good coding practice try to name them logically like unlying did In the above example, wheneverĮvent.MOUSE_DOWN occurs self.onMouseDown function will be calledĮvent.MOUSE_UP occurs self.onMouseUp function will be calledĮvent.MOUSE_MOVE occurs self.onMouseMove function will be called The second paremeter is the function that Gideros will run whenever those input types occur. (You can define your own events also but that's another topic ) those are already defined inside Gideros. Inside those eventlistener there are some built in input types likeĮvent.MOUSE_DOWN, Event.MOUSE_UP,Event.MOUSE_Move. The file gets parsed in the encoding specified (or sys.defaultencoding, if you didn't specify) and then when the string is a python variable it is already decoded.This is because unlying created a class called howto, and define those eventlisteners to the object itself. the correct coding declaration is on the module), then there is no "convert to unicode" step here. But this is only viable if the byte string conforms to some Unicode encoding.īut you're misunderstanding something about encodings here - if you can write the string literal in your test like that in the first place, and have the file successfully parsed by python (i.e. I could convert to unicode, use dent, and convert back to bytes. This is similar to your bullet point suggestion in the question However, if you want cross-compatible code, I recommend you take advantage of the six library: import sys, unittest It seems like dedent does not support bytestrings, sadly. Text = re.sub(rb'(?m)^' margin, b'', text)ī'\nLorem ipsum dolor sit amet\n consectetuer adipiscing elit\n' "line = %r, margin = %r" % (line, margin) # Find the largest common whitespace between current lineįor i, (x, y) in enumerate(zip(margin, indent)):Īssert not line or line.startswith(margin), \ # Current line consistent with and no deeper than previous winner: # no change (previous winner is still on top). # Current line more deeply indented than previous winner: Indents = _leading_whitespace_re.findall(text) Text = _whitespace_only_re.sub(b'', text) # Look for the longest leading string of spaces and tabs common to New in Python 2.5 older versions of this module incorrectlyĮxpanded tabs before searching for common leading whitespace.) Note that tabs and spaces are both treated as whitespace, but theyĪre not equal: the lines " hello" and "\\thello" areĬonsidered to have no common leading whitespace. This can be used to make triple-quoted strings line up with the leftĮdge of the display, while still presenting them in the source code """Remove any common leading whitespace from every line in `text`. ![]() _leading_whitespace_re = re.compile(b'(^*)(?:)', re.MULTILINE) _whitespace_only_re = re.compile(b'^ $', re.MULTILINE) I prefixed all 6 with b and voila! (I did not edit anything else, but the function docstring should be adjusted.) import re dedent is listed under # - Loosely related functionality -Īs near as I can tell, the only things that makes it text (unicode str) specific are the re literals. But this is only viable if the byte string conforms to some Unicode encoding.Īnswer 2: textwrap is primarily about the Textwrap class and functions. I could convert to unicode, use dent, and convert back to bytes.I could write such a function myself, but if there is an existing function, I'd prefer to use it.So: Is there an alternative to dent that works with byte strings? TypeError: can't use a string pattern on a bytes-like object In Python 2.7.10 the above code works fine, but in Python 3.4.3 it fails: EĮRROR: test_some_function (_main_.SomeTest)įile "test.py", line 16, in test_some_functionįile "/usr/lib64/python3.4/textwrap.py", line 416, in dedent Self.assertEqual(some_function(), dent(b""" Return b'Lorem ipsum dolor sit amet\n consectetuer adipiscing elit' I encountered this while writing a unit test for a method that returned a long multiline byte string, for example: # The function to be tested However, in Python 3.x, dent doesn't seem to work with byte strings. When I use a triple-quoted multiline string in Python, I tend to use dent to keep the code readable, with good indentation: some_string = dent("""
0 Comments
Leave a Reply. |