Python six.StringIO() Examples
The following are 30
code examples of six.StringIO().
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
, or try the search function
.
Example #1
Source File: test_debug.py From asynq with Apache License 2.0 | 6 votes |
def test_asynq_stack_trace_formatter(mock_format_error): mock_format_error.return_value = u"This is some traceback." stderr_string_io = StringIO() handler = logging.StreamHandler(stream=stderr_string_io) handler.setFormatter(asynq.debug.AsynqStackTracebackFormatter()) logger = logging.getLogger("test_asynq") logger.addHandler(handler) exc_info = None try: async_function_whose_child_async_task_will_throw_an_error() except ValueError: exc_info = sys.exc_info() logger.exception("Test") ty, val, tb = exc_info mock_format_error.assert_called_once_with(val, tb=tb) assert_eq(u"Test\nThis is some traceback.\n", stderr_string_io.getvalue())
Example #2
Source File: __init__.py From vbuild with MIT License | 6 votes |
def preProcessCSS(cnt, partial=""): """ Apply css-preprocessing on css rules (according css.type) using a partial or not return the css pre-processed """ if cnt.type in ["scss", "sass"]: if hasSass: from scss.compiler import compile_string # lang="scss" return compile_string(partial + "\n" + cnt.value) else: print("***WARNING*** : miss 'sass' preprocessor : sudo pip install pyscss") return cnt.value elif cnt.type in ["less"]: if hasLess: import lesscpy, six return lesscpy.compile( six.StringIO(partial + "\n" + cnt.value), minify=True ) else: print("***WARNING*** : miss 'less' preprocessor : sudo pip install lesscpy") return cnt.value else: return cnt.value
Example #3
Source File: controllers.py From devicehive-audio-analysis with Apache License 2.0 | 6 votes |
def get(self, handler, *args, **kwargs): f = StringIO() for timestamp, predictions in handler.server.server.events_queue: data = { 'timestamp': '{:%Y-%m-%d %H:%M:%S}'.format(timestamp), 'predictions': predictions } f.writelines(self.render_template('event.html', **data)) response = f.getvalue() f.close() handler.send_response(http_client.OK) handler.send_header('Content-type', 'text/html') handler.end_headers() handler.wfile.write(response.encode())
Example #4
Source File: score.py From python-esppy with Apache License 2.0 | 6 votes |
def import_schema_from_astore_output(self, output_variables_input): ''' Import a schema from the astore CAS action output format Parameters ---------- output_variables_input : DataFrame or list or string The schema definition ''' if isinstance(output_variables_input, six.string_types): if os.path.isfile(output_variables_input): output_variables_input = pd.read_csv(output_variables_input) else: output_variables_input = pd.read_csv(six.StringIO(output_variables_input)) if isinstance(output_variables_input, pd.DataFrame): self.schema = self._create_schema_list(output_variables_input) elif isinstance(output_variables_input, (tuple, list)): self.schema = list(output_variables_input)
Example #5
Source File: __init__.py From linter-pylama with MIT License | 6 votes |
def compute_content(self, layout): """trick to compute the formatting of children layout before actually writing it return an iterator on strings (one for each child element) """ # Patch the underlying output stream with a fresh-generated stream, # which is used to store a temporary representation of a child # node. out = self.out try: for child in layout.children: stream = six.StringIO() self.out = stream child.accept(self) yield stream.getvalue() finally: self.out = out
Example #6
Source File: test_util.py From pygreynoise with MIT License | 6 votes |
def test_values_from_configuration_file(self, os, open): """Values retrieved from configuration file.""" expected = { "api_key": "<api_key>", "api_server": "<api_server", "timeout": 123456, } os.environ = {} os.path.isfile.return_value = True file_content = textwrap.dedent( """\ [greynoise] api_key = {} api_server = {} timeout = {} """.format( expected["api_key"], expected["api_server"], expected["timeout"], ) ) open().__enter__.return_value = StringIO(file_content) config = load_config() assert config == expected open().__enter__.assert_called()
Example #7
Source File: test_util.py From pygreynoise with MIT License | 6 votes |
def test_api_key_from_environment_variable(self, os, open): """API key value retrieved from environment variable.""" expected = { "api_key": "<api_key>", "api_server": "<api_server>", "timeout": 123456, } os.environ = {"GREYNOISE_API_KEY": expected["api_key"]} os.path.isfile.return_value = True file_content = textwrap.dedent( """\ [greynoise] api_key = unexpected api_server = {} timeout = {} """.format( expected["api_server"], expected["timeout"], ) ) open().__enter__.return_value = StringIO(file_content) config = load_config() assert config == expected open().__enter__.assert_called()
Example #8
Source File: test_util.py From pygreynoise with MIT License | 6 votes |
def test_api_server_from_environment_variable(self, os, open): """API server value retrieved from environment variable.""" expected = { "api_key": "<api_key>", "api_server": "<api_server>", "timeout": 123456, } os.environ = {"GREYNOISE_API_SERVER": expected["api_server"]} os.path.isfile.return_value = True file_content = textwrap.dedent( """\ [greynoise] api_key = {} api_server = unexpected timeout = {} """.format( expected["api_key"], expected["timeout"], ) ) open().__enter__.return_value = StringIO(file_content) config = load_config() assert config == expected open().__enter__.assert_called()
Example #9
Source File: test_util.py From pygreynoise with MIT License | 6 votes |
def test_timeout_from_environment_variable(self, os, open): """Timeout value retrieved from environment variable.""" expected = { "api_key": "<api_key>", "api_server": "<api_server>", "timeout": 123456, } os.environ = {"GREYNOISE_TIMEOUT": str(expected["timeout"])} os.path.isfile.return_value = True file_content = textwrap.dedent( """\ [greynoise] api_key = {} api_server = {} timeout = unexpected """.format( expected["api_key"], expected["api_server"], ) ) open().__enter__.return_value = StringIO(file_content) config = load_config() assert config == expected open().__enter__.assert_called()
Example #10
Source File: test_subcommand.py From pygreynoise with MIT License | 6 votes |
def test_input_file_invalid_ip_addresses_passsed(self, api_client): """Error returned if only invalid IP addresses are passed in input file.""" runner = CliRunner() expected = ( "Error: at least one valid IP address must be passed either as an " "argument (IP_ADDRESS) or through the -i/--input_file option." ) result = runner.invoke( subcommand.interesting, ["-i", StringIO("not-an-ip")], parent=Context(main, info_name="greynoise"), ) assert result.exit_code == -1 assert "Usage: greynoise interesting" in result.output assert expected in result.output api_client.interesting.assert_not_called()
Example #11
Source File: test_subcommand.py From pygreynoise with MIT License | 6 votes |
def test_empty_input_file(self, api_client): """Error is returned if empty input fle is passed.""" runner = CliRunner() expected = ( "Error: at least one query must be passed either as an argument " "(QUERY) or through the -i/--input_file option." ) result = runner.invoke( subcommand.query, ["-i", StringIO()], parent=Context(main, info_name="greynoise"), ) assert result.exit_code == -1 assert "Usage: greynoise query" in result.output assert expected in result.output api_client.query.assert_not_called()
Example #12
Source File: test_util.py From pygreynoise with MIT License | 6 votes |
def test_timeout_from_environment_variable_with_invalid_value(self, os, open): """Invalid timeout value in environment variable is ignored.""" expected = { "api_key": "<api_key>", "api_server": "<api_server>", "timeout": 123456, } os.environ = {"GREYNOISE_TIMEOUT": "invalid"} os.path.isfile.return_value = True file_content = textwrap.dedent( """\ [greynoise] api_key = {} api_server = {} timeout = {} """.format( expected["api_key"], expected["api_server"], expected["timeout"], ) ) open().__enter__.return_value = StringIO(file_content) config = load_config() assert config == expected open().__enter__.assert_called()
Example #13
Source File: dsub_util.py From dsub with Apache License 2.0 | 6 votes |
def load_file(file_path, credentials=None): """Load a file from either local or gcs. Args: file_path: The target file path, which should have the prefix 'gs://' if to be loaded from gcs. credentials: Optional credential to be used to load the file from gcs. Returns: A python File object if loading file from local or a StringIO object if loading from gcs. """ if file_path.startswith('gs://'): return _load_file_from_gcs(file_path, credentials) else: return open(file_path, 'r') # Exponential backoff retrying downloads of GCS object chunks. # Maximum 23 retries. # Wait 1, 2, 4 ... 64, 64, 64... seconds.
Example #14
Source File: util.py From mqttwarn with Eclipse Public License 2.0 | 6 votes |
def exception_traceback(exc_info=None): """ Return a string containing a traceback message for the given exc_info tuple (as returned by sys.exc_info()). from setuptools.tests.doctest """ if not exc_info: exc_info = sys.exc_info() # Get a traceback message. excout = StringIO() exc_type, exc_val, exc_tb = exc_info traceback.print_exception(exc_type, exc_val, exc_tb, file=excout) return excout.getvalue()
Example #15
Source File: util.py From knack with MIT License | 6 votes |
def redirect_io(func): original_stderr = sys.stderr original_stdout = sys.stdout def wrapper(self): sys.stdout = sys.stderr = self.io = StringIO() func(self) self.io.close() sys.stdout = original_stderr sys.stderr = original_stderr # Remove the handlers added by CLI, so that the next invoke call init them again with the new stderr # Otherwise, the handlers will write to a closed StringIO from a preview test root_logger = logging.getLogger() cli_logger = logging.getLogger(CLI_LOGGER_NAME) root_logger.handlers = root_logger.handlers[:-1] cli_logger.handlers = cli_logger.handlers[:-1] return wrapper
Example #16
Source File: base.py From knack with MIT License | 6 votes |
def _in_process_execute(self, command): cli_name_prefixed = '{} '.format(self.cli.name) if command.startswith(cli_name_prefixed): command = command[len(cli_name_prefixed):] out_buffer = six.StringIO() try: # issue: stderr cannot be redirect in this form, as a result some failure information # is lost when command fails. self.exit_code = self.cli.invoke(shlex.split(command), out_file=out_buffer) or 0 self.output = out_buffer.getvalue() except vcr.errors.CannotOverwriteExistingCassetteException as ex: raise AssertionError(ex) except CliExecutionError as ex: if ex.exception: raise ex.exception raise ex except Exception as ex: # pylint: disable=broad-except self.exit_code = 1 self.output = out_buffer.getvalue() self.process_error = ex finally: out_buffer.close()
Example #17
Source File: test_subcommand.py From pygreynoise with MIT License | 6 votes |
def test_empty_input_file(self, api_client): """Error is returned if empty input fle is passed.""" runner = CliRunner() expected = ( "Error: at least one query must be passed either as an argument " "(QUERY) or through the -i/--input_file option." ) result = runner.invoke( subcommand.stats, ["-i", StringIO()], parent=Context(main, info_name="greynoise"), ) assert result.exit_code == -1 assert "Usage: greynoise stats" in result.output assert expected in result.output api_client.query.assert_not_called()
Example #18
Source File: test_subcommand.py From pygreynoise with MIT License | 6 votes |
def test_input_file_invalid_ip_addresses_passsed(self, api_client): """Error returned if only invalid IP addresses are passed in input file.""" runner = CliRunner() expected = ( "Error: at least one valid IP address must be passed either as an " "argument (IP_ADDRESS) or through the -i/--input_file option." ) result = runner.invoke( subcommand.quick, ["-i", StringIO("not-an-ip")], parent=Context(main, info_name="greynoise"), ) assert result.exit_code == -1 assert "Usage: greynoise quick" in result.output assert expected in result.output api_client.quick.assert_not_called()
Example #19
Source File: test_subcommand.py From pygreynoise with MIT License | 6 votes |
def test_input_file_invalid_ip_addresses_passsed(self, api_client): """Error returned if only invalid IP addresses are passed in input file.""" runner = CliRunner() expected = ( "Error: at least one valid IP address must be passed either as an " "argument (IP_ADDRESS) or through the -i/--input_file option." ) result = runner.invoke( subcommand.ip, ["-i", StringIO("not-an-ip")], parent=Context(main, info_name="greynoise"), ) assert result.exit_code == -1 assert "Usage: greynoise ip" in result.output assert expected in result.output api_client.ip.assert_not_called()
Example #20
Source File: test_help.py From knack with MIT License | 5 votes |
def redirect_io(func): def wrapper(self): global io old_stdout = sys.stdout old_stderr = sys.stderr sys.stdout = sys.stderr = io = StringIO() func(self) io.close() sys.stdout = old_stdout sys.stderr = old_stderr return wrapper
Example #21
Source File: test_prompting.py From knack with MIT License | 5 votes |
def test_prompt_choice_list_question_with_help_string(self, _): a_list = ['red', 'blue', 'yellow', 'green'] with mock.patch('knack.prompting._input', side_effect=['?', '1']): with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout: actual_result = prompt_choice_list('What is your favourite color?', a_list, help_string='Your real favourite.') self.assertEqual(0, actual_result) self.assertIn('Your real favourite.', mock_stdout.getvalue())
Example #22
Source File: test_prompting.py From knack with MIT License | 5 votes |
def test_prompt_t_f_question_with_help_string(self, _): with mock.patch('knack.prompting._input', side_effect=['?', 't']): with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout: actual_result = prompt_t_f('Do you accept?', help_string='t to accept conditions; no otherwise') self.assertTrue(actual_result) self.assertIn('t to accept conditions; no otherwise', mock_stdout.getvalue())
Example #23
Source File: test_prompting.py From knack with MIT License | 5 votes |
def test_prompt_y_n_question_with_help_string(self, _): with mock.patch('knack.prompting._input', side_effect=['?', 'y']): with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout: actual_result = prompt_y_n('Do you accept?', help_string='y to accept conditions; no otherwise') self.assertTrue(actual_result) self.assertIn('y to accept conditions; no otherwise', mock_stdout.getvalue())
Example #24
Source File: test_prompting.py From knack with MIT License | 5 votes |
def test_prompt_pass_question_with_help_string(self, _): my_password = '7ndBkS3zKQazD5N3zzstubZq' with mock.patch('getpass.getpass', side_effect=['?', my_password]): with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout: actual_result = prompt_pass(help_string='Anything you want!') self.assertEqual(my_password, actual_result) self.assertIn('Anything you want!', mock_stdout.getvalue())
Example #25
Source File: test_prompting.py From knack with MIT License | 5 votes |
def test_prompt_msg_question_with_help_string(self, _): expected_result = 'My response' with mock.patch('knack.prompting._input', side_effect=['?', expected_result]): with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout: actual_result = prompt('Please enter some text: ', help_string='Anything you want!') self.assertEqual(expected_result, actual_result) self.assertIn('Anything you want!', mock_stdout.getvalue())
Example #26
Source File: output.py From knack with MIT License | 5 votes |
def dump(data): io = StringIO() for item in data: _TsvOutput._dump_row(item, io) result = io.getvalue() io.close() return result
Example #27
Source File: test_pandas_store.py From arctic with GNU Lesser General Public License v2.1 | 5 votes |
def test_save_read_pandas_dataframe_empty_multiindex_and_no_columns(library): expected = read_csv(StringIO(u'''STRATEGY MAC INSTRUMENT CONTRACT'''), delimiter=' ').set_index(['STRATEGY', 'MAC', 'INSTRUMENT', 'CONTRACT']) library.write('pandas', expected) saved_df = library.read('pandas').data assert np.all(expected.values == saved_df.values) assert np.all(expected.index.names == saved_df.index.names)
Example #28
Source File: test_subcommand.py From pygreynoise with MIT License | 5 votes |
def test_input_file(self, api_client, ip_addresses, mock_response, expected): """Quickly check IP address from input file.""" runner = CliRunner() api_client.quick.return_value = mock_response result = runner.invoke( subcommand.quick, ["-f", "json", "-i", StringIO("\n".join(ip_addresses))] ) assert result.exit_code == 0 assert result.output.strip("\n") == expected api_client.quick.assert_called_with(ip_addresses=ip_addresses)
Example #29
Source File: error_messages_test.py From threat_intel with MIT License | 5 votes |
def setupStringIO(self): self._stringio = StringIO() with patch('sys.stderr', self._stringio): yield
Example #30
Source File: test_output.py From knack with MIT License | 5 votes |
def setUp(self): self.mock_ctx = MockContext() self.io = StringIO()