Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Dennis Lee Bieber Newsgroups: comp.lang.python Subject: Re: Considering migrating to Python from Visual Basic 6 for engineering applications Date: Sun, 21 Feb 2016 13:39:51 -0500 Organization: IISS Elusive Unicorn Lines: 113 Message-ID: References: <9e57761f-26e1-41c5-8e71-23800de1fdd3@googlegroups.com> <7f9c473e-b0c2-4d77-91d1-d0733c93b12d@googlegroups.com> <23d8156f-1808-4395-9c04-27d2984fe67c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 8Gg1K87L8oyY120YMEWSYAQ6612P2YfFz24xY0PtailQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'modify': 0.04; 'subject:Python': 0.05; 'lines,': 0.05; 'chunk': 0.07; 'granted,': 0.07; '"r")': 0.09; 'chunks': 0.09; 'ignoring': 0.09; 'integers': 0.09; 'message-id:@4ax.com': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'runtime': 0.09; 'separator.': 0.09; 'subject:Visual': 0.09; "they've": 0.09; 'def': 0.13; 'ignore': 0.14; 'file,': 0.15; 'skip:f 30': 0.15; '"word"': 0.16; '2016': 0.16; '>to': 0.16; 'assignments': 0.16; 'buffers,': 0.16; 'concatenate': 0.16; 'contents:': 0.16; 'fld': 0.16; 'generator.': 0.16; 'ignores': 0.16; 'integers,': 0.16; 'make,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'splits': 0.16; 'later': 0.16; 'string': 0.17; 'integer': 0.18; 'say,': 0.18; 'skip': 0.18; 'string,': 0.18; 'url:home': 0.18; 'input': 0.18; 'skip:" 30': 0.20; 'class,': 0.22; 'converted': 0.22; 'explicit': 0.22; 'level,': 0.22; 'parsing': 0.22; 'leave': 0.23; 'feb': 0.23; 'nearly': 0.23; 'sat,': 0.23; 'split': 0.23; 'written': 0.24; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'rest': 0.26; 'skip:" 20': 0.26; 'expanding': 0.27; 'format,': 0.27; 'yield': 0.27; 'function': 0.28; 'values': 0.28; 'looks': 0.29; 'declared': 0.29; 'i/o': 0.29; 'if,': 0.29; 'referencing': 0.29; 'code:': 0.29; 'convert': 0.29; 'objects': 0.29; 'maybe': 0.33; 'point': 0.33; 'impression': 0.33; 'stream': 0.33; 'suddenly': 0.33; 'open': 0.33; 'file': 0.34; 'handle': 0.34; 'gets': 0.35; 'next': 0.35; 'could': 0.35; 'text': 0.35; 'done': 0.35; 'feed': 0.35; 'text.': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'lines': 0.36; '(and': 0.36; 'depends': 0.36; 'framework': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; 'received:org': 0.37; 'spread': 0.37; 'charset:us-ascii': 0.37; 'seem': 0.37; 'things': 0.38; 'doing': 0.38; 'names': 0.38; 'several': 0.38; 'files': 0.38; 'mean': 0.38; 'data': 0.39; 'format': 0.39; 'does': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'space': 0.40; 'some': 0.40; 'entire': 0.61; 'email addr:gmail.com': 0.62; 'back': 0.62; 'different': 0.63; 'request.': 0.66; 'here': 0.66; 'cognizance': 0.84; 'fin': 0.84; 'dennis': 0.91; 'indicator': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: adsl-108-68-179-144.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103314 On Sat, 20 Feb 2016 10:38:40 -0800 (PST), wrong.address.1@gmail.com declaimed the following: > >To give an example of the kind of things I have to read (and I have to read numbers from several files usually), here is some VB6 code: > > Open "N020S.TXT" For Input As #8 > > Input #8, ND, NIN, NT ' all integers > Text9.Text = ND > Text1 = CStr(NIN) > Text4 = NT > Are characters rationed on this job? Names that mean something? I note that you read a text file -- letting the runtime convert "words" into integers (presume they've been declared as such somewhere), and then immediately convert one of them back into text. Granted, it IS used as an integer later but one gets the impression above that all three "integers" just get converted back to text. > Text12 = "" > For i = 1 To NIN > Input #8, DINCOL(i) ' single precision floating point vector > Text12 = Text12.Text & DINCOL(i) & " " > If Dvarname(1) <> "" Then varname(i) = Dvarname(NLCOL(i)) ' strings > Next i > Ignoring that "Dvarname" is undefined, it looks like you are reading -- one at a time (ie; by "words") values that are being converted to floating point format, only to immediately have the runtime convert them back to text format so you can string concatenate them with a blank separator. You also seem to be relying upon BASIC's tendency to ignore end-of-line marks (after reading the three integers, the next read ignores the line feed and reads the first float) AND its tendency to leave partial lines in the stream for the next read request. -=-=-=-=- f8 = open("N020S.TXT", "r") (ND, NIN, NT) = [int(x) for x in f8.readline().strip().split()] #only works if there are three valid integers on the line #skipping the three assignments as i suspect you are referencing #GUI form objects and that depends then on the framework in use DINCOL = [float(x) for x in f8.readline().strip().split()] if len(DINCOL) != NIN: #expected NIN values, but got a different number of values Text12 = " ".join(str(x) for x in DINCOL) #assumption: all NIN-count floats are on one line; if they can spread #across multiple lines, the I/O will need to get fancier #upto, yes, reading large buffers, stripping new-lines, and doing #your own "word" parsing -- though .split() does take a #parameter on how many splits to make, so # rest = " ".join(f8.read().split("\n")) # read entire file, split on lines, rejoin with space # while rest: # fld, rest = rest.split(None, 1) # do something with fld -=-=-=-=- For "ignore new lines" type I/O, you could maybe make a generator function to handle the word fetching... Something like (I've not written a generator before)... def words(fname): fin = open(fname, "r") contents = " ".join(fin.read().split("\n")) fin.close while contents: aword, contents = contents.split(None, 1) yield aword reader = words("N020S.TXT") ND = int(reader.next()) NIN = int(reader.next()) NT = int(reader.next()) DINCOL = [] for _ in xrange(NIN): DINCOL.append(float(reader.next())) text12 = " ".join(str(x) for x in DINCOL) If the input files are really huge, you would have to modify the generator to read in chunks, and when one chunk is nearly consumed, read the next chunk and concatenate it... Could use lines for the chunks def words(fname): fin = open(fname, "r") for line in fin: contents = line.strip() while contents: #warning: will skip over blank lines aword, contents = contents.split(None, 1) yield aword fin.close() Of course, since the file read is all done inside the generator, if you suddenly need a multiple word string, you will have problems, since no indicator of end-of-line is seen outside this generator. Expanding up to the next level, you could create a class, with methods to handle explicit data types, cognizance of the EOL, allowing blank lines if, say, the input request is for a string, etc. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/