Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Considering migrating to Python from Visual Basic 6 for engineering applications Date: Tue, 23 Feb 2016 00:09:08 +1100 Lines: 43 Message-ID: References: <90cc50d2-1ce5-4588-9bfd-a49d439f00dd@googlegroups.com> <14c75a68-0d2e-45cc-8d73-0d71b6a6aea6@googlegroups.com> <9e57761f-26e1-41c5-8e71-23800de1fdd3@googlegroups.com> <7f9c473e-b0c2-4d77-91d1-d0733c93b12d@googlegroups.com> <23d8156f-1808-4395-9c04-27d2984fe67c@googlegroups.com> <56caee18$0$1612$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de YdWTX0Ko6w3efIzVOYa5SwlGkuLE9qpzX82kRqpbKwMA== 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; 'received:209.85.223': 0.03; 'subject:Python': 0.05; '"""': 0.05; 'val': 0.07; 'cc:addr :python-list': 0.09; '22,': 0.09; 'assigning': 0.09; 'logic': 0.09; 'parameter.': 0.09; 'parsed': 0.09; 'str,': 0.09; 'subject:Visual': 0.09; 'type;': 0.09; 'python': 0.10; 'language,': 0.11; 'template': 0.11; 'accordingly.': 0.13; 'read.': 0.13; 'def': 0.13; 'subject: \n ': 0.15; 'variables': 0.15; '"""read': 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'idx': 0.16; 'idx,': 0.16; 'magic': 0.16; 'pythonic': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'string': 0.17; 'example.': 0.18; 'string,': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'int,': 0.22; 'level,': 0.22; 'parse': 0.22; 'techniques,': 0.22; 'bit': 0.23; '(or': 0.23; 'feb': 0.23; 'elements': 0.23; 'insert': 0.23; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'mon,': 0.24; 'point.': 0.27; 'parameters': 0.27; 'message-id:@mail.gmail.com': 0.27; 'specify': 0.27; 'values': 0.28; 'declared': 0.29; 'types.': 0.29; 'read,': 0.29; 'work.': 0.30; "can't": 0.32; 'though,': 0.32; 'instances': 0.33; 'lets': 0.33; 'file': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'text': 0.35; 'returning': 0.35; 'something': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'list.': 0.37; 'received:209': 0.38; 'does': 0.39; 'subject:from': 0.39; 'easily': 0.39; 'more': 0.63; 'series': 0.65; 'capture': 0.66; 'chrisa': 0.84; 'etc,': 0.84; 'float,': 0.84; "op's": 0.84; 'percent,': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=mkJyu/VXoh8Y2x6iE0oIwy2cdiDMlfwxsoetaIV+8h8=; b=SeGLhjFCUkEuRCrpfkasb04G+KVscSQb6Rlduch5mQNgvq+SuiDcDuPjRqe+AycVST 7cxuFaL20lQLExnBWlJN9KgjuXwB7hOG348a217oheqgGxDK0Sz7nkGTtynOv29d4ihJ r46nzor9fXWsEaxmAKQae4KPaPBYvPFTkvF61NwrxFLi3rcgQDdN4N3efHcExaOlXjms +iH0o/8LhESBSe3kyXOSsGbYsFN4MA5aZIDTKAJggQV7ayXA79gzcWrQ8FWUnfUKuZ/w y08rFibO1zwo+UVJQpaqJY9uecMETgLlVgdAT7O0J/Mu6vwNYLLdYb6MOQ+r4ceHjOdQ MetA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=mkJyu/VXoh8Y2x6iE0oIwy2cdiDMlfwxsoetaIV+8h8=; b=F4F8aVXs22NV5eZEKADRtX5YDLJb1u6m2WD51ngqbLoDjmIN/X7nE9laIHG11xRr3K MZWmEs9FIra0fJBhcoI6gmupnAeLD/VIcTYh8DLPksZ6qTZ8ACbUIlxVV8N7he/rXMad T2j9D9/GdRZ78hLZ4VJUAtV/MUiQfTBjbHUivHHgIsPHYiw76LenneHmvuHryOeW0lk2 KEcqFWgwYfFqTeLMNZmOdUS3CZ9wGvkfqxrDg2HBe4ZvdZbftH7tvovdR96m9Ar2y5c5 J0WMWB05MySeBN3EYclB3mWK56+aLYw7sg6POBFvYFaliPqPKvrmpYiU9z5kU+zPn3Ap bggA== X-Gm-Message-State: AG10YOR2EbKfuJiTfVtRd6tWjj0crK4lygXG1AdZChjJroLlf9pSO0jA5jmLk9WJBN9oNSArLlYiV1AUMBEYdQ== X-Received: by 10.107.132.90 with SMTP id g87mr25909508iod.157.1456146549092; Mon, 22 Feb 2016 05:09:09 -0800 (PST) In-Reply-To: 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:103340 On Mon, Feb 22, 2016 at 11:51 PM, BartC wrote: > Yes, I mentioned that point. In the OP's language, the variables can be > declared as a particular type; in Python they could perhaps be 'declared' by > first assigning with 0, 0.0 or "" for example. But that would need reference > parameters to make use of tidily. What you could do is something like this: def read_values(file, values): """Read one line from file and parse it into the given list. Prepopulate values with a series of types or instances of types. The line of text read will be parsed accordingly. Returns the number of elements successfully read. """ line = file.readline() for idx, val in enumerate(values): if not isinstance(val, type): val = type(val) # Now add the logic to read an int, a float, # a str, a Fraction, etc etc etc, from the line, # breaking out of the loop if one can't be # read, and stashing the result into values[idx] # if it can. return idx with open("inputfile") as f: values = [int, int, float, Fraction, str] while read_values(f, values) == 5: [index, level, percent, ratio, message] = values This lets you "declare" the values' types, and use an input/output parameter. It's not exactly the most Pythonic of techniques, but it does kinda work. I suspect, though, that what you'd _really_ want is something more like a sscanf string, or a "typed regex", which would allow you to specify a bit more flexibly what you're looking for. You could easily make a template string that has magic markers for "take an integer", which would insert [0-9]+ (or something more elaborate) and then capture it and call int() on the string before returning it. ChrisA