Python six.moves.cStringIO() Examples
The following are 30
code examples of six.moves.cStringIO().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
six.moves
, or try the search function
.
Example #1
Source File: __init__.py From claripy with BSD 2-Clause "Simplified" License | 6 votes |
def _get_model(self, solver=None, extra_constraints=(), extra_variables=()): vars, csts = self._get_all_vars_and_constraints(solver=solver, e_c=extra_constraints, e_v=extra_variables) smt_script = self._get_full_model_smt_script(constraints=csts, variables=vars) if self.smt_script_log_dir is not None: fname = 'get-model_{}.smt2'.format(hashlib.md5(smt_script.encode()).hexdigest()) with open(os.path.join(self.smt_script_log_dir, fname), 'wb') as f: f.write(smt_script.encode()) solver.reset() solver.write(smt_script) sat = solver.read_sat() if sat == 'sat': model_string = solver.read_model() tokens = Tokenizer(cStringIO(model_string), interactive=True) ass_list = SMTParser(tokens).consume_assignment_list() return sat, {s: val for s, val in ass_list}, ass_list else: error = solver.readline() return sat, error, None
Example #2
Source File: test_printing.py From pysmt with Apache License 2.0 | 6 votes |
def test_daggify(self): x = Symbol("x") f = And(x,x) for _ in xrange(10): f = And(f,f) tree_buf = cStringIO() dag_buf = cStringIO() tree_printer = SmtPrinter(tree_buf) dag_printer = SmtDagPrinter(dag_buf) dag_printer.printer(f) tree_printer.printer(f) short_f_str = dag_buf.getvalue() long_f_str = tree_buf.getvalue() self.assertTrue(len(short_f_str) < len(long_f_str))
Example #3
Source File: test_smtlibscript.py From pysmt with Apache License 2.0 | 6 votes |
def test_declare_sort(self): class SmtLibIgnore(SmtLibIgnoreMixin): declare_sort_history = [] def declare_sort(self, name, arity): self.declare_sort_history.append((name, arity)) mock = SmtLibIgnore() parser = SmtLibParser() smtlib_script = '\n'.join(['(declare-sort s0 0)', \ '(declare-sort s1 1)', \ '(declare-const c0 s0)', \ '(declare-const c1 (s1 Int))']) outstream = cStringIO(smtlib_script) script = parser.get_script(outstream) script.evaluate(solver=mock) self.assertEqual(len(mock.declare_sort_history), 2) s0_name, s0_arity = mock.declare_sort_history[0] s1_name, s1_arity = mock.declare_sort_history[1] self.assertEqual(s0_name, "s0") self.assertEqual(s0_arity, 0) self.assertEqual(s1_name, "s1") self.assertEqual(s1_arity, 1)
Example #4
Source File: test_parser_examples.py From pysmt with Apache License 2.0 | 6 votes |
def test_parse_examples(self): fs = get_example_formulae() for (f_out, _, _, logic) in fs: if logic == logics.QF_BV: # See test_parse_examples_bv continue buf = cStringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf) #print(buf) buf.seek(0) parser = SmtLibParser() script_in = parser.get_script(buf) f_in = script_in.get_last_formula() self.assertEqual(f_in.simplify(), f_out.simplify())
Example #5
Source File: test_regressions.py From pysmt with Apache License 2.0 | 6 votes |
def test_parse_bvx_var(self): """bvX is a valid identifier.""" smtlib_input = """ (declare-fun bv1 () (_ BitVec 8)) (assert (bvult (_ bv0 8) (bvmul (bvadd bv1 (_ bv1 8)) (_ bv5 8)))) (check-sat)""" parser = SmtLibParser() buffer_ = cStringIO(smtlib_input) script = parser.get_script(buffer_) # Check Parsed result iscript = iter(script) cmd = next(iscript) self.assertEqual(cmd.name, DECLARE_FUN) bv1 = cmd.args[0] self.assertEqual(bv1.symbol_type().width, 8) cmd = next(iscript) parsed_f = cmd.args[0] target_f = BVULT(BV(0, 8), BVMul(BVAdd(bv1, BV(1, 8)), BV(5, 8))) self.assertEqual(parsed_f, target_f)
Example #6
Source File: book.py From PyLimitOrderBook with MIT License | 6 votes |
def __str__(self): # Efficient string concat file_str = StringIO() file_str.write("------ Bids -------\n") if self.bids != None and len(self.bids) > 0: for k, v in self.bids.price_tree.items(reverse=True): file_str.write('%s' % v) file_str.write("\n------ Asks -------\n") if self.asks != None and len(self.asks) > 0: for k, v in self.asks.price_tree.items(): file_str.write('%s' % v) file_str.write("\n------ Trades ------\n") if self.trades != None and len(self.trades) > 0: num = 0 for entry in self.trades: if num < 5: file_str.write(str(entry.qty) + " @ " \ + str(entry.price / 100) \ + " (" + str(entry.timestamp) + ")\n") num += 1 else: break file_str.write("\n") return file_str.getvalue()
Example #7
Source File: printers.py From pysmt with Apache License 2.0 | 6 votes |
def to_smtlib(formula, daggify=True): """Returns a Smt-Lib string representation of the formula. The daggify parameter can be used to switch from a linear-size representation that uses 'let' operators to represent the formula as a dag or a simpler (but possibly exponential) representation that expands the formula as a tree. See :py:class:`SmtPrinter` """ buf = cStringIO() p = None if daggify: p = SmtDagPrinter(buf) else: p = SmtPrinter(buf) p.printer(formula) res = buf.getvalue() buf.close() return res
Example #8
Source File: test_parser_examples.py From pysmt with Apache License 2.0 | 6 votes |
def test_parse_examples_bv(self): """For BV we represent a superset of the operators defined in SMT-LIB. We verify the correctness of the serialization process by checking the equivalence of the original and serialized expression. """ fs = get_example_formulae() for (f_out, _, _, logic) in fs: if logic != logics.QF_BV: continue buf_out = cStringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf_out) buf_in = cStringIO(buf_out.getvalue()) parser = SmtLibParser() script_in = parser.get_script(buf_in) f_in = script_in.get_last_formula() self.assertValid(Iff(f_in, f_out))
Example #9
Source File: printers.py From pysmt with Apache License 2.0 | 6 votes |
def serialize(self, formula, printer=None, threshold=None): """Returns a string with the human-readable version of the formula. 'printer' is the printer to call to perform the serialization. 'threshold' is the thresholding value for the printing function. """ buf = cStringIO() if printer is None: p = self.PrinterClass(buf) else: p = printer(buf) p.printer(formula, threshold) res = buf.getvalue() buf.close() return res
Example #10
Source File: test_progiter.py From ubelt with Apache License 2.0 | 6 votes |
def test_rate_format(): # Define a function that takes some time import ubelt as ub file = cStringIO() prog = ub.ProgIter(file=file) prog.begin() prog._iters_per_second = .000001 msg = prog.format_message() rate_part = msg.split('rate=')[1].split(' Hz')[0] assert rate_part == '1e-06' prog._iters_per_second = .1 msg = prog.format_message() rate_part = msg.split('rate=')[1].split(' Hz')[0] assert rate_part == '0.10' prog._iters_per_second = 10000 msg = prog.format_message() rate_part = msg.split('rate=')[1].split(' Hz')[0] assert rate_part == '10000.00'
Example #11
Source File: test_progiter.py From ubelt with Apache License 2.0 | 6 votes |
def test_progiter_offset_10(): """ pytest -s ~/code/ubelt/ubelt/tests/test_progiter.py::test_progiter_offset_10 """ # Define a function that takes some time file = cStringIO() list(ProgIter(range(10), total=20, verbose=3, start=10, file=file, freq=5, show_times=False)) file.seek(0) want = ['10/20...', '15/20...', '20/20...'] got = [line.strip() for line in file.readlines()] if sys.platform.startswith('win32'): # nocover # on windows \r seems to be mixed up with ansi sequences from xdoctest.utils import strip_ansi got = [strip_ansi(line).strip() for line in got] assert got == want
Example #12
Source File: appfuncs.py From ibeis with Apache License 2.0 | 6 votes |
def embed_image_html(imgBGR, target_width=TARGET_WIDTH, target_height=TARGET_HEIGHT): """ Creates an image embedded in HTML base64 format. """ import cv2 from PIL import Image if target_width is not None: imgBGR = _resize(imgBGR, t_width=target_width) elif target_height is not None: imgBGR = _resize(imgBGR, t_height=target_height) imgRGB = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2RGB) pil_img = Image.fromarray(imgRGB) if six.PY2: from six.moves import cStringIO as StringIO string_buf = StringIO() pil_img.save(string_buf, format='jpeg') data = string_buf.getvalue().encode('base64').replace('\n', '') else: import io byte_buf = io.BytesIO() pil_img.save(byte_buf, format='jpeg') byte_buf.seek(0) img_bytes = base64.b64encode(byte_buf.read()) data = img_bytes.decode('ascii') return 'data:image/jpeg;base64,' + data
Example #13
Source File: test_progiter.py From ubelt with Apache License 2.0 | 6 votes |
def test_progiter_offset_0(): """ pytest -s ~/code/ubelt/ubelt/tests/test_progiter.py::test_progiter_offset_0 """ # Define a function that takes some time file = cStringIO() for _ in ProgIter(range(10), total=20, verbose=3, start=0, file=file, freq=5, show_times=False): pass file.seek(0) want = ['0/20...', '5/20...', '10/20...'] got = [line.strip() for line in file.readlines()] if sys.platform.startswith('win32'): # nocover # on windows \r seems to be mixed up with ansi sequences from xdoctest.utils import strip_ansi got = [strip_ansi(line).strip() for line in got] assert got == want
Example #14
Source File: book.py From PyLimitOrderBook with MIT License | 6 votes |
def __str__(self): # Efficient string concat file_str = StringIO() file_str.write("------ Bids -------\n") if self.bids != None and len(self.bids) > 0: for k, v in self.bids.price_tree.items(reverse=True): file_str.write('%s' % v) file_str.write("\n------ Asks -------\n") if self.asks != None and len(self.asks) > 0: for k, v in self.asks.price_tree.items(): file_str.write('%s' % v) file_str.write("\n------ Trades ------\n") if self.trades != None and len(self.trades) > 0: num = 0 for entry in self.trades: if num < 5: file_str.write(str(entry.qty) + " @ " \ + str(entry.price / 100) \ + " (" + str(entry.timestamp) + ")\n") num += 1 else: break file_str.write("\n") return file_str.getvalue()
Example #15
Source File: test_scaffolds.py From pecan with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_copy_dir(self): from pecan.scaffolds import PecanScaffold class SimpleScaffold(PecanScaffold): _scaffold_dir = ('pecan', os.path.join( 'tests', 'scaffold_fixtures', 'simple' )) SimpleScaffold().copy_to(os.path.join( self.scaffold_destination, 'someapp' ), out_=StringIO()) assert os.path.isfile(os.path.join( self.scaffold_destination, 'someapp', 'foo' )) assert os.path.isfile(os.path.join( self.scaffold_destination, 'someapp', 'bar', 'spam.txt' )) with open(os.path.join( self.scaffold_destination, 'someapp', 'foo' ), 'r') as f: assert f.read().strip() == 'YAR'
Example #16
Source File: test_parser_examples.py From pysmt with Apache License 2.0 | 6 votes |
def test_dumped_logic(self): # Dumped logic matches the logic in the example fs = get_example_formulae() for (f_out, _, _, logic) in fs: buf_out = cStringIO() script_out = smtlibscript_from_formula(f_out) script_out.serialize(outstream=buf_out) buf_in = cStringIO(buf_out.getvalue()) parser = SmtLibParser() script_in = parser.get_script(buf_in) for cmd in script_in: if cmd.name == "set-logic": logic_in = cmd.args[0] if logic == logics.QF_BOOL: self.assertEqual(logic_in, logics.QF_UF) elif logic == logics.BOOL: self.assertEqual(logic_in, logics.LRA) else: self.assertEqual(logic_in, logic, script_in) break else: # Loops exited normally print("-"*40) print(script_in)
Example #17
Source File: orderList.py From PyLimitOrderBook with MIT License | 5 votes |
def __str__(self): from six.moves import cStringIO as StringIO file_str = StringIO() for order in self: file_str.write("%s\n" % str(order)) return file_str.getvalue()
Example #18
Source File: test_scaffolds.py From pecan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setUp(self): super(TestScaffoldUtils, self).setUp() self.scaffold_destination = tempfile.mkdtemp() self.out = sys.stdout sys.stdout = StringIO()
Example #19
Source File: bookViewerBook.py From PyLimitOrderBook with MIT License | 5 votes |
def ask_book_str(self): # Efficient string concat file_str = StringIO() file_str.write("------- Asks --------\n") if self.asks != None and len(self.asks) > 0: for k, v in self.asks.price_tree.items(): file_str.write('%s' % v) return file_str.getvalue()
Example #20
Source File: test_scaffolds.py From pecan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_copy_dir_with_file_content_substitution(self): from pecan.scaffolds import copy_dir copy_dir( ( 'pecan', os.path.join('tests', 'scaffold_fixtures', 'content_sub'), ), os.path.join( self.scaffold_destination, 'someapp' ), {'package': 'thingy'}, out_=StringIO() ) assert os.path.isfile(os.path.join( self.scaffold_destination, 'someapp', 'foo') ) assert os.path.isfile(os.path.join( self.scaffold_destination, 'someapp', 'bar', 'spam.txt') ) with open(os.path.join( self.scaffold_destination, 'someapp', 'foo' ), 'r') as f: assert f.read().strip() == 'YAR thingy' with open(os.path.join( self.scaffold_destination, 'someapp', 'bar', 'spam.txt' ), 'r') as f: assert f.read().strip() == 'Pecan thingy'
Example #21
Source File: test_hooks.py From pecan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_item_not_in_defaults(self): _stdout = StringIO() class RootController(object): @expose() def index(self): return 'Hello, World!' app = TestApp( make_app( RootController(), hooks=lambda: [ RequestViewerHook( config={'items': ['date']}, writer=_stdout ) ] ) ) response = app.get('/') out = _stdout.getvalue() assert response.status_int == 200 assert response.body == b_('Hello, World!') assert 'date' in out assert 'method' not in out assert 'status' not in out assert 'method' not in out assert 'params' not in out assert 'hooks' not in out assert '200 OK' not in out assert "['RequestViewerHook']" not in out assert '/' not in out
Example #22
Source File: test_hooks.py From pecan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_single_blacklist_item(self): _stdout = StringIO() class RootController(object): @expose() def index(self): return 'Hello, World!' app = TestApp( make_app( RootController(), hooks=lambda: [ RequestViewerHook( config={'blacklist': ['/']}, writer=_stdout ) ] ) ) response = app.get('/') out = _stdout.getvalue() assert response.status_int == 200 assert response.body == b_('Hello, World!') assert out == ''
Example #23
Source File: test_hooks.py From pecan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_bad_response_from_app(self): """When exceptions are raised the hook deals with them properly""" _stdout = StringIO() class RootController(object): @expose() def index(self): return 'Hello, World!' app = TestApp( make_app( RootController(), hooks=lambda: [ RequestViewerHook(writer=_stdout) ] ) ) response = app.get('/404', expect_errors=True) out = _stdout.getvalue() assert response.status_int == 404 assert 'path' in out assert 'method' in out assert 'status' in out assert 'method' in out assert 'params' in out assert 'hooks' in out assert '404 Not Found' in out assert "['RequestViewerHook']" in out assert '/' in out
Example #24
Source File: test_hooks.py From pecan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_basic_single_default_hook(self): _stdout = StringIO() class RootController(object): @expose() def index(self): return 'Hello, World!' app = TestApp( make_app( RootController(), hooks=lambda: [ RequestViewerHook(writer=_stdout) ] ) ) response = app.get('/') out = _stdout.getvalue() assert response.status_int == 200 assert response.body == b_('Hello, World!') assert 'path' in out assert 'method' in out assert 'status' in out assert 'method' in out assert 'params' in out assert 'hooks' in out assert '200 OK' in out assert "['RequestViewerHook']" in out assert '/' in out
Example #25
Source File: test_base.py From pecan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_logging_setup_with_config_obj(self): class RootController(object): @expose() def index(self): import logging logging.getLogger('pecantesting').info('HELLO WORLD') return "HELLO WORLD" f = StringIO() from pecan.configuration import conf_from_dict app = TestApp(make_app(RootController(), logging=conf_from_dict({ 'loggers': { 'pecantesting': { 'level': 'INFO', 'handlers': ['memory'] } }, 'handlers': { 'memory': { 'level': 'INFO', 'class': 'logging.StreamHandler', 'stream': f } } }))) app.get('/') assert f.getvalue() == 'HELLO WORLD\n'
Example #26
Source File: test_base.py From pecan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_logging_setup(self): class RootController(object): @expose() def index(self): import logging logging.getLogger('pecantesting').info('HELLO WORLD') return "HELLO WORLD" f = StringIO() app = TestApp(make_app(RootController(), logging={ 'loggers': { 'pecantesting': { 'level': 'INFO', 'handlers': ['memory'] } }, 'handlers': { 'memory': { 'level': 'INFO', 'class': 'logging.StreamHandler', 'stream': f } } })) app.get('/') assert f.getvalue() == 'HELLO WORLD\n'
Example #27
Source File: debug.py From pecan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __call__(self, environ, start_response): try: return self.app(environ, start_response) except Exception as exc: # get a formatted exception out = StringIO() print_exc(file=out) LOG.exception(exc) # get formatted WSGI environment formatted_environ = pformat(environ) # render our template result = debug_template.render( traceback=out.getvalue(), environment=formatted_environ ) # construct and return our response response = Response() if isinstance(exc, HTTPException): response.status_int = exc.status else: response.status_int = 500 response.unicode_body = result return response(environ, start_response)
Example #28
Source File: mcsv_coder.py From code-snippets with Apache License 2.0 | 5 votes |
def __init__(self, delimiter): """Initializes the writer wrapper. Args: delimiter: A one-character string used to separate fields. """ self._state = (delimiter) self._buffer = moves.cStringIO() # Since we use self._writer to encode individual rows, we set # lineterminator='' so that self._writer doesn't add a newline. self._writer = csv.writer( self._buffer, lineterminator='', delimiter=delimiter)
Example #29
Source File: tokens.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def normalize_token_spacing(code): tokens = [(t[0], t[1]) for t in tokenize.generate_tokens(StringIO(code).readline)] return pretty_untokenize(tokens)
Example #30
Source File: tokens.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def python_tokenize(code): # Since formulas can only contain Python expressions, and Python # expressions cannot meaningfully contain newlines, we'll just remove all # the newlines up front to avoid any complications: code = code.replace("\n", " ").strip() it = tokenize.generate_tokens(StringIO(code).readline) try: for (pytype, string, (_, start), (_, end), code) in it: if pytype == tokenize.ENDMARKER: break origin = Origin(code, start, end) assert pytype not in (tokenize.NL, tokenize.NEWLINE) if pytype == tokenize.ERRORTOKEN: raise PatsyError("error tokenizing input " "(maybe an unclosed string?)", origin) if pytype == tokenize.COMMENT: raise PatsyError("comments are not allowed", origin) yield (pytype, string, origin) else: # pragma: no cover raise ValueError("stream ended without ENDMARKER?!?") except tokenize.TokenError as e: # TokenError is raised iff the tokenizer thinks that there is # some sort of multi-line construct in progress (e.g., an # unclosed parentheses, which in Python lets a virtual line # continue past the end of the physical line), and it hits the # end of the source text. We have our own error handling for # such cases, so just treat this as an end-of-stream. # # Just in case someone adds some other error case: assert e.args[0].startswith("EOF in multi-line") return