How to use select_best_encoding method in Behave

Best Python code snippet using behave

egid3.py

Source:egid3.py Github

copy

Full Screen

...13 j = 014 for i in range(len(barray)):15 j = (j << 7) | (barray[i] & 0x7f)16 return j17def select_best_encoding(str, encoding_list):18 best_encoding = (None, None)19 for e in encoding_list:20 estr = None21 try:22 estr = str.encode(e[2])23 if best_encoding[1] is None or len(estr) < len(best_encoding[1]):24 best_encoding = (e, estr)25 except UnicodeEncodeError:26 pass27 return best_encoding28class ID3SyntaxError(SyntaxError):29 pass30class ID3NotImplemented(Exception):31 pass32class ID3Frame:33 CODING_ASCII = 'ascii'34 35 def __init__(self):36 self.parent = None37 self.frameid = None38 self.flags = [0, 0]39 self.info = None40 self.bininfo = b''41 def size(self):42 return len(self.bininfo)43 def fullsize(self):44 if self.parent and self.parent.version[0] <= 2:45 fullsize = self.size() + 646 else:47 fullsize = self.size() + 1048 return fullsize49 def makebin(self):50 self.bininfo = self.info51 @classmethod52 def from_stream(cls, instrm, parent=None):53 if parent and parent.version[0] <= 2:54 frameid_bin = instrm.read(3)55 else:56 frameid_bin = instrm.read(4)57 frameid = frameid_bin.decode(cls.CODING_ASCII)58 if frameid[0] == 'T':59 newframe = ID3FrameText()60 elif frameid[0] == 'W':61 newframe = ID3FrameURL()62 else:63 newframe = ID3Frame()64 newframe.parent = parent65 newframe.frameid = frameid66 67 if parent and parent.version[0] <= 2:68 size_bin = instrm.read(3)69 else:70 size_bin = instrm.read(4)71 size = from_synchsafe(size_bin)72 73 if parent and parent.version[0] <= 2:74 newframe.flags = None75 else:76 flags = instrm.read(2)77 newframe.flags = [flags[0], flags[1]]78 buf = instrm.read(size)79 newframe.set_bininfo(buf)80 return newframe81 def set_bininfo(self, bininfo):82 self.bininfo = bininfo83 self.info = bininfo84 def write(self, outstrm):85 outstrm.write(self.frameid.encode(self.CODING_ASCII))86 87 if self.parent and self.parent.version[0] <= 2:88 sizebin = to_synchsafe(self.size(), 3)89 else:90 sizebin = to_synchsafe(self.size(), 4)91 outstrm.write(sizebin)92 93 if self.parent and self.parent.version[0] >= 3:94 outstrm.write(b'%c%c' % (self.flags[0], self.flags[1]))95 outstrm.write(self.bininfo)96# Text information frames97class ID3FrameText(ID3Frame):98 ENCODING_LIST = ((b'\x00', b'\x00' , 'iso-8859-1'),99 (b'\x01', b'\x00\x00', 'utf-16'),100 (b'\x02', b'\x00\x00', 'utf-16be'),101 (b'\x03', b'\x00' , 'utf-8'),102 )103 def __init__(self):104 super().__init__()105 self.encoding = None106 def set_bininfo(self, buf):107 self.bininfo = buf108 for e in self.ENCODING_LIST:109 if buf[0] == e[0][0]:110 self.encoding = e111 s = buf[1:].decode(self.encoding[2])112 if self.parent and self.parent.version[0] >= 4 and '\0' in s:113 self.info = s.split('\0')114 else:115 self.info = s116 def makebin(self):117 if isinstance(self.info, (list)):118 if self.parent.version[0] <= 3:119 separator = ' / '120 else:121 separator = '\x00'122 info = ''123 for i in self.info:124 if len(info) > 0:125 info += separator126 info += i127 else:128 info = self.info129 if self.parent.version[0] <= 2:130 encoding_list = self.ENCODING_LIST[:2] # ISO-8859-1 or UTF-16131 else:132 encoding_list = self.ENCODING_LIST133 einfo = select_best_encoding(info, encoding_list)134 self.encoding = einfo[0]135 self.bininfo = b'%c%b' % (self.encoding[0], einfo[1])136class ID3FrameURL(ID3Frame):137 def set_bininfo(self, buf):138 self.bininfo = buf139 self.info = buf.decode(self.CODING_ASCII)140 def makebin(self):141 self.bininfo = self.info.encode(self.CODING_ASCII)142class ID3Tag:143 BYTE_ORDER = 'big'144 145 ID_ID3 = b'ID3'146 147 # bit masks for 'flags'...

Full Screen

Full Screen

base.py

Source:base.py Github

copy

Full Screen

...13 * the name (filename/dirname) of the output stream14 * let it decide if directory mode is used instead of file mode15 """16 # FORMER: default_encoding = "UTF-8"17 default_encoding = select_best_encoding()18 def __init__(self, filename=None, stream=None, encoding=None):19 if not encoding:20 encoding = self.default_encoding21 if stream:22 stream = self.ensure_stream_with_encoder(stream, encoding)23 self.name = filename24 self.stream = stream25 self.encoding = encoding26 self.should_close_stream = not stream # Only for not pre-opened ones.27 @staticmethod28 def ensure_dir_exists(directory):29 if directory and not os.path.isdir(directory):30 os.makedirs(directory)31 @classmethod...

Full Screen

Full Screen

textutil.py

Source:textutil.py Github

copy

Full Screen

...48 try:49 return codecs.lookup(encoding).name == "ascii"50 except LookupError:51 return False52def select_best_encoding(outstream=None):53 """Select the *best* encoding for an output stream/file.54 Uses:55 * ``outstream.encoding`` (if available)56 * ``sys.getdefaultencoding()`` (otherwise)57 Note: If encoding=ascii, uses encoding=UTF-858 :param outstream: Output stream to select encoding for (or: stdout)59 :return: Unicode encoding name (as string) to use (for output stream).60 """61 outstream = outstream or sys.stdout62 encoding = getattr(outstream, "encoding", None) or sys.getdefaultencoding()63 if is_ascii_encoding(encoding):64 # -- REQUIRED-FOR: Python265 # MAYBE: locale.getpreferredencoding()66 return "utf-8"67 return encoding68def text(value, encoding=None, errors=None):69 """Convert into a unicode string.70 :param value: Value to convert into a unicode string (bytes, str, object).71 :return: Unicode string72 SYNDROMES:73 * Convert object to unicode: Has only __str__() method (Python2)74 * Windows: exception-traceback and encoding=unicode-escape are BAD75 * exception-traceback w/ weird encoding or bytes76 ALTERNATIVES:77 * Use traceback2 for Python2: Provides unicode tracebacks78 """79 if encoding is None:80 encoding = select_best_encoding()81 if errors is None:82 errors = BEHAVE_UNICODE_ERRORS83 if isinstance(value, six.text_type):84 # -- CASE: ALREADY UNICODE (pass-through, efficiency):85 return value86 elif isinstance(value, six.binary_type):87 # -- CASE: bytes/binary_type (Python2: str)88 try:89 return six.text_type(value, encoding, errors)90 except UnicodeError:91 # -- BEST-EFFORT:92 return six.u(value)93 # elif isinstance(value, bytes):94 # # -- MAYBE: filename, path, etc.95 # try:96 # return value.decode(sys.getfilesystemencoding())97 # except UnicodeError:98 # return value.decode("utf-8", "replace") # MAYBE: "ignore"99 else:100 # -- CASE: CONVERT/CAST OBJECT TO TEXT/STRING101 try:102 if six.PY2:103 try:104 text2 = six.text_type(value)105 except UnicodeError as e:106 # -- NOTE: value has no sane unicode conversion107 # encoding=unicode-escape helps recover from errors.108 data = str(value)109 text2 = six.text_type(data, "unicode-escape", "replace")110 else:111 # PY3: Cast to string/unicode112 text2 = six.text_type(value)113 except UnicodeError as e:114 # Python3: multi-arg call supports only string-like object: str, bytes115 text2 = six.text_type(e)116 return text2117def to_texts(args, encoding=None, errors=None):118 """Process a list of string-like objects into list of unicode values.119 Optionally converts binary text into unicode for each item.120 121 :return: List of text/unicode values.122 """123 if encoding is None:124 encoding = select_best_encoding()125 return [text(arg, encoding, errors) for arg in args]126def ensure_stream_with_encoder(stream, encoding=None):127 if not encoding:128 encoding = select_best_encoding(stream)129 if six.PY3:130 return stream131 elif hasattr(stream, "stream"):132 return stream # Already wrapped with a codecs.StreamWriter133 else:134 assert six.PY2135 # py2 does, however, sometimes declare an encoding on sys.stdout,136 # even if it doesn't use it (or it might be explicitly None)137 stream = codecs.getwriter(encoding)(stream)...

Full Screen

Full Screen

egid3test.py

Source:egid3test.py Github

copy

Full Screen

...14 (b'\x01', b'\x00\x00', 'utf-16'),15 (b'\x02', b'\x00\x00', 'utf-16be'),16 (b'\x03', b'\x00' , 'utf-8'),17 )18 e = egid3.select_best_encoding('¡ASCII STRING!', ENCODING_LIST)19 self.assertEqual(e[0][0], b'\x00')20 self.assertEqual(e[1], b'\xa1ASCII STRING!')21 def test_select_best_encoding_utf16be(self):22 ENCODING_LIST = ((b'\x00', b'\x00' , 'iso-8859-1'),23 (b'\x01', b'\x00\x00', 'utf-16'),24 (b'\x02', b'\x00\x00', 'utf-16be'),25 (b'\x03', b'\x00' , 'utf-8'),26 )27 e = egid3.select_best_encoding('漢字', ENCODING_LIST)28 self.assertEqual(e[0][0], b'\x02')29 self.assertEqual(e[1], b'\x6f\x22\x5b\x57')30 def test_select_best_encoding_utf16(self):31 ENCODING_LIST = ((b'\x00', b'\x00' , 'iso-8859-1'),32 (b'\x01', b'\x00\x00', 'utf-16'),33 )34 e = egid3.select_best_encoding('漢字', ENCODING_LIST)35 self.assertEqual(e[0][0], b'\x01')36 if e[1][0] == 0xfe:37 # Big Endian38 self.assertEqual(e[1], b'\xfe\xff\x6f\x22\x5b\x57')39 else:40 # Little Endian41 self.assertEqual(e[1], b'\xff\xfe\x22\x6f\x57\x5b')42 def test_select_best_encoding_utf8(self):43 ENCODING_LIST = ((b'\x00', b'\x00' , 'iso-8859-1'),44 (b'\x01', b'\x00\x00', 'utf-16'),45 (b'\x02', b'\x00\x00', 'utf-16be'),46 (b'\x03', b'\x00' , 'utf-8'),47 )48 e = egid3.select_best_encoding('ASCII STRING 漢字', ENCODING_LIST)49 self.assertEqual(e[0][0], b'\x03')50 self.assertEqual(e[1], b'ASCII STRING \xe6\xbc\xa2\xe5\xad\x97')51 def test_text_makebin_iso8859(self):52 frame = egid3.ID3FrameText()53 frame.parent = egid3.ID3Tag()54 frame.parent.version = b'\x03\x00' # id3v2.3.055 frame.encoding = frame.ENCODING_LIST[1]56 frame.info = "¡TEST!"57 frame.makebin()58 expectedBin = b'\x00\xa1TEST!'59 self.assertEqual(frame.bininfo, expectedBin)60 def test_text_makebin_utf16be(self):61 frame = egid3.ID3FrameText()62 frame.parent = egid3.ID3Tag()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Behave automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful