def test_iterator(): s = u"1234567890\n" sio = StringIO(s * 10) assert iter(sio) is sio assert hasattr(sio, "__iter__") assert hasattr(sio, "next") i = 0 for line in sio: assert line == s i += 1 assert i == 10 sio.seek(0) i = 0 for line in sio: assert line == s i += 1 assert i == 10 sio.seek(len(s) * 10 + 1) assert list(sio) == [] sio = StringIO(s * 2) sio.close() raises(ValueError, next, sio)
def test_newline_none(): sio = StringIO(u"a\nb\r\nc\rd", newline=None) res = list(sio) assert res == [u"a\n", u"b\n", u"c\n", u"d"] sio.seek(0) res = sio.read(1) assert res == u"a" res = sio.read(2) assert res == u"\nb" res = sio.read(2) assert res == u"\nc" res = sio.read(1) assert res == u"\n" sio = StringIO(newline=None) res = sio.write(u"a\n") assert res == 2 res = sio.write(u"b\r\n") assert res == 3 res = sio.write(u"c\rd") assert res == 3 sio.seek(0) res = sio.read() assert res == u"a\nb\nc\nd" sio = StringIO(u"a\r\nb", newline=None) res = sio.read(3) assert res == u"a\nb"
def test_newline_empty(): sio = StringIO(u"a\nb\r\nc\rd", newline="") res = list(sio) assert res == [u"a\n", u"b\r\n", u"c\r", u"d"] sio.seek(0) res = sio.read(4) assert res == u"a\nb\r" res = sio.read(2) assert res == u"\nc" res = sio.read(1) assert res == u"\r" sio = StringIO(newline="") res = sio.write(u"a\n") assert res == 2 res = sio.write(u"b\r") assert res == 2 res = sio.write(u"\nc") assert res == 2 res = sio.write(u"\rd") assert res == 2 sio.seek(0) res = list(sio) assert res == [u"a\n", u"b\r\n", u"c\r", u"d"]
def LoadPlugin(self, plugin): '''LoadPlugin(self, plugin) --> None Loads a data handler note that there is no UnLoad function since only one DataHandler can be plugged in at a time. ''' # Unload the plugins names = self.plugin_handler.loaded_plugins.copy() try: [self.plugin_handler.unload_plugin(pl) for pl in names] self.parent.SetStatusText('Unloaded data loader %s' % list(names.keys())[0]) except: #outp = StringIO.StringIO() outp = StringIO() traceback.print_exc(200, outp) tbtext = outp.getvalue() outp.close() ShowErrorDialog(self.parent, 'Can NOT unload plugin object'+ \ list(names.keys())[0] + '\nPython traceback below:\n\n' + tbtext) try: self.plugin_handler.load_plugin(plugin) self.parent.SetStatusText('Loaded data loader: %s' % plugin) except: outp = StringIO() traceback.print_exc(200, outp) tbtext = outp.getvalue() outp.close() ShowErrorDialog(self.parent, 'Can NOT load plugin ' + plugin\ + '\nPython traceback below:\n\n' + tbtext)
def envfrom(self, f, *str): try: if self.CUSTOMFROM != "": #If anything is left over from the last email, re -initialize. self.__init__() self.startTime = time.time() self.startTimeDT = datetime.datetime.now() if self.milterConfig.mode == "shutdown": log = self.uuid + " milter in Maint mode, returning [Sender:" + ''.join( f) + "]" self.logger.writeLog(syslog.LOG_DEBUG, "%s" % log) return self.milterConfig.dispositionModes[ "InMaintMode".lower()] log = self.uuid + " envFrom: " log += ''.join(f) self.logger.writeLog(syslog.LOG_DEBUG, "%s" % log) self.CUSTOMFROM = f self.CUSTOMFROM = self.CUSTOMFROM.replace("<", "") self.CUSTOMFROM = self.CUSTOMFROM.replace(">", "") self.sender = self.CUSTOMFROM self.fph = StringIO.StringIO() self.fpb = StringIO.StringIO() except: log = self.uuid + " Uncaught Exception in EnvFrom" self.logger.writeLog(syslog.LOG_ERR, "%s" % (str(log))) return milter.CONTINUE #ALWAYS continue to gather the entire email
def test_stringio(): sio = StringIO() sio.write(u'Hello ') sio.write(u'world') assert sio.getvalue() == u'Hello world' assert StringIO(u"hello").read() == u'hello'
def captured_output(): new_out, new_err = StringIO(), StringIO() old_out, old_err = sys.stdout, sys.stderr try: sys.stdout, sys.stderr = new_out, new_err yield sys.stdout, sys.stderr finally: sys.stdout, sys.stderr = old_out, old_err
def test_simplest_create(self): print("-- test_simplest_create") ucisdb = "file.ucis" db = MemFactory.create() testnode = db.createHistoryNode(None, "logicalName", ucisdb, UCIS_HISTORYNODE_TEST) td = TestData(teststatus=UCIS_TESTSTATUS_OK, toolcategory="UCIS:simulator", date="20200202020") testnode.setTestData(td) file = db.createFileHandle("dummy", os.getcwd()) srcinfo = SourceInfo(file, 0, 0) du = db.createScope( "foo.bar", srcinfo, 1, # weight UCIS_OTHER, UCIS_DU_MODULE, UCIS_ENABLED_STMT | UCIS_ENABLED_BRANCH | UCIS_ENABLED_COND | UCIS_ENABLED_EXPR | UCIS_ENABLED_FSM | UCIS_ENABLED_TOGGLE | UCIS_INST_ONCE | UCIS_SCOPE_UNDER_DU) instance = db.createInstance( "dummy", None, # sourceinfo 1, # weight UCIS_OTHER, UCIS_INSTANCE, du, UCIS_INST_ONCE) cg = instance.createCovergroup( "cg", SourceInfo(file, 3, 0), 1, # weight UCIS_OTHER) cp = cg.createCoverpoint( "t", SourceInfo(file, 4, 0), 1, # weight UCIS_VLOG) cp.setComment("Hello There") cp.createBin("auto[a]", SourceInfo(file, 4, 0), 1, 4, "a") out = StringIO() writer = XmlWriter() writer.write(out, db) print("XML Output:\n" + out.getvalue()) input = StringIO(out.getvalue()) validate_ucis_xml(input)
def test_roundtrip_translation(): sio1 = StringIO(u'a\nb', newline='\r\n') pos = sio1.seek(1) assert sio1.getvalue() == u'a\r\nb' state = sio1.__getstate__() sio2 = StringIO() sio2.__setstate__(state) assert sio2.getvalue() == u'a\r\nb' assert sio2.tell() == pos
def disabled_test_lib_dump(self): LibFactory.load_ucis_library("libucis.so") db = LibFactory.create("file.ucis") out = StringIO() writer = XmlWriter() writer.write(out, db) input = StringIO(out.getvalue())
def test_pprintInterface(self): u = ExampleWithArrayAxi3Lite() o = StringIO() self.prepareUnit(u) pprintInterface(u.clk, file=o) self.assertEqual(o.getvalue(), "'clk'\n") o = StringIO() pprintInterface(u.axi, file=o) self.assertEqual(o.getvalue(), axi_str)
def test_roundtrip_state(): s = u'12345678' sio1 = StringIO(s) sio1.foo = 42 sio1.seek(2) assert sio1.getvalue() == s state = sio1.__getstate__() sio2 = StringIO() sio2.__setstate__(state) assert sio2.getvalue() == s assert sio2.foo == 42 assert sio2.tell() == 2
def test_simple_dump(self): @vsc.covergroup class my_covergroup(object): def __init__(self): self.with_sample(dict( a=vsc.uint8_t(), b=vsc.uint8_t() )) self.a_cp = vsc.coverpoint(self.a, bins=dict( a_bins = vsc.bin_array([], [1,8]) )) self.b_cp = vsc.coverpoint(self.b, bins=dict( b_bins = vsc.bin_array([], [1,8]) )) cg_1 = my_covergroup() cg_1.sample(1, 2) cg_1.sample(2, 1) cg_1.sample(4, 2) cg_2 = my_covergroup() cg_2.sample(5, 4) cg_2.sample(6, 2) cg_2.sample(7, 8) db = MemFactory.create() v = CoverageSaveVisitor(db) td = TestData( teststatus=UCIS_TESTSTATUS_OK, toolcategory="UCIS:simulator", date="20200101132000") v.save(td, CoverageRegistry.inst().covergroup_types()) db.close() out = StringIO() writer = XmlWriter() writer.write(out, db) print("Output:\n" + out.getvalue()) xmlin = StringIO(out.getvalue()) XmlReader.validate(xmlin)
def loadFile(self): # path = os.path.join('/docs/github/Opal/src/ui/view/opalview', 'bookInfo.html') out = StringIO() htmlhandler = rt.RichTextHTMLHandler() buffer = self.rtc.GetBuffer() # htmlhandler.SetFlags(rt.RICHTEXT_HANDLER_SAVE_IMAGES_TO_MEMORY) htmlhandler.SetFontSizeMapping([7, 9, 11, 12, 14, 22, 100]) logger.debug('canload: %s', htmlhandler.CanLoad()) logger.debug('cansave: %s', htmlhandler.CanSave()) logger.debug('CanHandle: %s', htmlhandler.CanHandle('bookInfo.html')) rt.RichTextBuffer.AddHandler(htmlhandler) # buffer.AddHandler(htmlhandler) try: if self.book != None: out.write(self.book.bookDescription) out.seek(0) except Exception as e: logger.error(e) # htmlhandler.LoadStream(buffer, out) # htmlhandler.LoadFile(path,'text') if self.book != None and self.book.bookDescription != None: self.rtc.AppendText(self.book.bookDescription) # htmlhandler.LoadStream(buffer, out.getvalue()) self.rtc.Refresh()
def mails(self): self.mail_content = StringIO() self.mail_content.write("*This is an automatically generated email. Do not reply.*\n\n\n") self.is_need_mail = False if not self.is_all_7zfiles_exist: self.is_need_mail = True self.mail_content.write("**************************Missing 7z files**************************\n") self.mail_content.writelines(self.mail_content_7zfiles.getvalue()) self.mail_content.write("\n\n") if not self.is_all_scs_configed: self.is_need_mail = True self.mail_content.write("**************************Scs not configed**************************\n") self.mail_content.write(self.mail_content_scs) self.mail_content.write("\n\n") if not self.is_recovered_bitbake: self.is_need_mail = True self.mail_content.write("**************************Recover bitbake**************************\n") self.mail_content.writelines(self.mail_content_recover.getvalue()) self.mail_content.write("\n\n") elif not self.is_bitbake_content_fine: self.is_need_mail = True self.mail_content.write("**************************Recover bitbake**************************\n") self.mail_content.writelines(self.mail_content_recover.getvalue()) self.mail_content.write("\n\n") if self.is_need_mail: mail = Mail(self.config.get("mail_from"), self.config.get("mail_to")) mail.create(self.config.get("mail_subject"), self.mail_content.getvalue()) mail.send()
def test_smoke(self): import pssparser marker_l = pssparser.core.BaseMarkerListener() parser = pssparser.core.AstBuilder(marker_l) glbl = pssparser.core.mkGlobalScope(0) print("glbl=" + str(glbl)) input = StringIO(""" /** * Just a comment */ component pss_top { } """) print("--> parse") parser.parse(glbl, input) print("<-- parse") class MyVisitor(pssparser.core.BaseVisitor): def __init__(self): super().__init__() def visitComponent(self, c): print("visitComponent") v = MyVisitor() print("glbl=" + str(glbl)) glbl.accept(v) # pssparser.core.doit(2)
def test1event_generation_match_action(self): """This test case checks if events are generated and pushed to all event handlers.""" description = "Test1Rules" output_stream2 = StringIO() message = 'This message was generated, when the unit were successful.' match_context = MatchContext(b'25537') decimal_integer_value_me = DecimalIntegerValueModelElement( 'd1', DecimalIntegerValueModelElement.SIGN_TYPE_NONE, DecimalIntegerValueModelElement.PAD_TYPE_NONE) match_element = decimal_integer_value_me.get_match_element( 'match', match_context) stream_printer_event_handler2 = StreamPrinterEventHandler( self.analysis_context, output_stream2) t = time() event_generation_match_action = EventGenerationMatchAction( 'Test.%s' % self.__class__.__name__, message, [self.stream_printer_event_handler, stream_printer_event_handler2]) self.analysis_context.register_component(event_generation_match_action, description) log_atom = LogAtom(match_context.match_data, ParserMatch(match_element), t, event_generation_match_action) event_generation_match_action.match_action(log_atom) self.assertEqual(self.output_stream.getvalue(), output_stream2.getvalue()) self.assertEqual( self.output_stream.getvalue(), self.__expected_string % (datetime.fromtimestamp(t).strftime("%Y-%m-%d %H:%M:%S"), event_generation_match_action.__class__.__name__, description, 1, log_atom.parser_match.match_element.annotate_match('')))
def output_func(col): ''' This function outputs the result of the reducer. Namely, the tf-idf vector for every abstract. ''' s = StringIO() col.to_json(s) print('%s\t%s' % (col.name, s.getvalue()))
def _runTest(self, text): input_stream = InputStream(text) parser = CUParser(input_stream, unittest.TestCase.id) cu = parser.parse() if len(cu.markers) > 0: print("Test Failed:") in_reader = StringIO(text) i = 1 while True: line = in_reader.readline() if line == "": break line = line[:-1] print("%3d: %s" % (i, line)) i += 1 self.assertEqual(len(cu.markers), 0, "Syntax Errors") # Now, run the linker... v = LinkVisitor([cu]) v.link() # Run the link checker to ensure we didn't miss resolving any v = TestLinker.LinkCheckVisitor() cu.accept(v)
def test_truncate(): s = u"1234567890" sio = StringIO(s) raises(ValueError, sio.truncate, -1) sio.seek(6) res = sio.truncate() assert res == 6 assert sio.getvalue() == s[:6] res = sio.truncate(4) assert res == 4 assert sio.getvalue() == s[:4] # truncate() accepts long objects res = sio.truncate(4L) assert res == 4 assert sio.getvalue() == s[:4] assert sio.tell() == 6 sio.seek(0, 2) sio.write(s) assert sio.getvalue() == s[:4] + s pos = sio.tell() res = sio.truncate(None) assert res == pos assert sio.tell() == pos raises(TypeError, sio.truncate, '0') sio.close() raises(ValueError, sio.truncate, 0)
def test3get_match_element_valid_match(self): """Parse data and check if the MatchContext was not changed.""" old_stderr = sys.stderr output = StringIO() sys.stderr = output debug_model_element = DebugModelElement(self.id_) self.assertEqual(output.getvalue(), "DebugModelElement %s added\n" % self.id_) output.seek(0) output.truncate(0) data = b"some data" match_context = DummyMatchContext(data) match_element = debug_model_element.get_match_element(self.path, match_context) self.assertEqual( output.getvalue(), 'DebugModelElement path = "%s", unmatched = "%s"\n' % (match_element.get_path(), repr( match_context.match_data))) self.compare_match_results(data, match_element, match_context, self.id_, self.path, b"", b"", None) output.seek(0) output.truncate(0) data = b"123 0x2a. [\"abc\"]:" match_context = DummyMatchContext(data) match_element = debug_model_element.get_match_element(self.path, match_context) self.assertEqual( output.getvalue(), 'DebugModelElement path = "%s", unmatched = "%s"\n' % (match_element.get_path(), repr( match_context.match_data))) self.compare_match_results(data, match_element, match_context, self.id_, self.path, b"", b"", None) sys.stderr = old_stderr
def _read_csv(self): if self.quotechar == '': current_quoting = QUOTE_NONE else: current_quoting = QUOTE_ALL if 'csvcontent' in self.request.FILES.keys(): csvfile = TextIOWrapper( self.request.FILES['csvcontent'].file, encoding=self.encoding, errors='replace') csvcontent = "".join(csvfile.readlines()) for param_idx in range(0, int(len(csvcontent) / 2048) + 2): self.params['csvcontent%d' % param_idx] = csvcontent[ 2048 * param_idx:2048 * (param_idx + 1)] csvfile.seek(0) else: csvcontent = "" for param_idx in range(0, 1000): curent_content = self.getparam('csvcontent%d' % param_idx) if curent_content is None: break else: csvcontent += "" + curent_content csvfile = StringIO(csvcontent) self.spamreader = DictReader(csvfile, delimiter=self.delimiter, quotechar=self.quotechar, quoting=current_quoting) try: if (self.spamreader.fieldnames is None) or (len(self.spamreader.fieldnames) == 0): raise Exception("") except Exception: raise LucteriosException(IMPORTANT, _('CSV file unvalid!'))
def parse_csv_string(s): f = StringIO(s) reader = csv.reader(f, delimiter=',', dialect=csv.excel_tab) ls = [] for row in reader: ls.append(row) return ls[0]
def test_multi_class7( self): #Test based on small piece of Severstal dataset TESTDATA = StringIO("""ImageId,ClassId ff6e35e0a.jpg,1 ff6e35e0a.jpg,2 ff933e271.jpg,3 ff96dfa95.jpg,3 ff9923932.jpg,3 ff9d46e95.jpg,4 ffb48ee43.jpg,3 ffbd081d5.jpg,3 ffc9fdf70.jpg,3 ffcf72ecf.jpg,3 fff02e9c5.jpg,3 fffe98443.jpg,3 ffff4eaa8.jpg,3 ffffd67df.jpg,3 00031f466.jpg, 000418bfc.jpg, 000789191.jpg, """) df = pd.read_csv(TESTDATA, sep=",") ds = image_datasets.MultiClassClassificationDataSet([], df, "ImageId", "ClassId") ds.get_value = dummyImage self.assertEqual(len(ds.classes), 4) encoded = ds.encode(ds, True) df['ClassId'] = df['ClassId'] #df['ClassId'] = df['ClassId'].str.strip() rs = encoded['ClassId'].values == df['ClassId'].values pass
def get_tokens(self, text, unfiltered=False): """ Return an iterable of (tokentype, value) pairs generated from `text`. If `unfiltered` is set to `True`, the filtering mechanism is bypassed even if filters are defined. Also preprocess the text, i.e. expand tabs and strip it if wanted and applies registered filters. """ if isinstance(text, str): if self.stripall: text = text.strip() elif self.stripnl: text = text.strip('\n') if sys.version_info[0] < 3 and isinstance(text, str): # for jython import cStringIO text = cStringIO.StringIO(text) self.encoding = 'utf-8' else: # for jython move import section from _io import StringIO text = StringIO(text) def streamer(): for i, t, v in self.get_tokens_unprocessed(text): # for jython encode if sys.version_info[0] < 3 and isinstance(v, unicode): v = v.encode('utf-8') yield t, v stream = streamer() if not unfiltered: stream = apply_filters(stream, self.filters, self) return stream
def useStrIO(): s = StringIO() s.write('hello,world!') s.write('\n') s.write('hello,BeiJing!') #print(s.getvalue()) #获取所有信息 printStrIO(s)
def testValidInput(self): ''' test if output match expected format ''' expectedOutput = 'A\nB\nC\nX' with mock.patch('sys.stdout', new=StringIO()) as mockedOut: sys.argv = ['shortestpath', self.goodDataPath, 'A', 'X'] main() self.assertEqual(expectedOutput, mockedOut.getvalue().strip())
def test_valid_schema(self): unit_dir = os.path.dirname(os.path.abspath(__file__)) mkdv_dir = os.path.dirname(os.path.dirname(unit_dir)) schema_dir = os.path.join(mkdv_dir, "src", "mkdv", "share", "schema") with open(os.path.join(schema_dir, "mkdv_schema.json"), "r") as fp: schema = json.load(fp) print("schema: %s" % str(schema)) text = """ jobs: - job: name: abc run-vars: - SW_IMAGE: foo.elf - var: A val: B - job: name: def - job-group: name: abc job: - job: name: foo """ text_ds = yaml.load(StringIO(text), Loader=Loader) print("text_ds: %s" % str(text_ds)) jsonschema.validate(text_ds, schema)
def build(cls, unit: Unit, unique_name: str, build_dir: Optional[str], target_platform=DummyPlatform(), do_compile=True) -> "BasicRtlSimulatorVcd": """ Create a pycocotb.basic_hdl_simulator based simulation model for specified unit and load it to python :param unit: interface level unit which you wont prepare for simulation :param unique_name: unique name for build directory and python module with simulator :param target_platform: target platform for this synthesis :param build_dir: directory to store sim model build files, if None sim model will be constructed only in memory """ if unique_name is None: unique_name = unit._getDefaultName() _filter = SerializerFilterDoNotExclude() if build_dir is None or not do_compile: buff = StringIO() store_man = SaveToStream(SimModelSerializer, buff, _filter=_filter) else: if not os.path.isabs(build_dir): build_dir = os.path.join(os.getcwd(), build_dir) build_private_dir = os.path.join(build_dir, unique_name) store_man = SaveToFilesFlat(SimModelSerializer, build_private_dir, _filter=_filter) store_man.module_path_prefix = unique_name to_rtl(unit, name=unique_name, target_platform=target_platform, store_manager=store_man) if build_dir is not None: d = build_dir dInPath = d in sys.path if not dInPath: sys.path.insert(0, d) if unique_name in sys.modules: del sys.modules[unique_name] simModule = importlib.import_module( unique_name + "." + unique_name, package='simModule_' + unique_name) if not dInPath: sys.path.pop(0) else: simModule = ModuleType('simModule_' + unique_name) # python supports only ~100 opened brackets # if exceeded it throws MemoryError: s_push: parser stack overflow exec(buff.getvalue(), simModule.__dict__) model_cls = simModule.__dict__[unit._name] # can not use just function as it would get bounded to class return cls(model_cls, unit)
def hasilCSV(request): if request.method == 'POST': name = request.FILES['fileInput'].name typeFile = name.split('.')[1] if(typeFile == 'txt'): reader = TextIOWrapper(request.FILES['fileInput'].file, encoding='utf-8') elif(typeFile == 'csv'): try: text = TextIOWrapper(request.FILES['fileInput'].file, encoding='utf-8') reader = csv.reader(text) except: text = StringIO(request.FILES['fileInput'].file.read().decode()) reader = csv.reader(text) arrData = [] for line in reader: line = ''.join(line) arrData.append(line) myfile = StringIO() metode = request.POST['metode'] statusFormalisasi = request.POST.get('formalisasi', False) if(metode == 'EDR'): for line in arrData: hasil = F_EDR(line) myfile.write(hasil + os.linesep) elif(metode == 'ED'): for line in arrData: hasil = F_ED(line) myfile.write(hasil + os.linesep) elif(metode == 'BG'): for line in arrData: hasil = F_BG(line) myfile.write(hasil + os.linesep) myfile.flush() myfile.seek(0) response = HttpResponse(FileWrapper(myfile), content_type='text/csv') response['Content-Disposition'] = 'attachment; filename=hasil.csv' return response else: return render(request, 'index_preprocess.html', {})