top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Python based user interface for an Fortran application

+2 votes
108 views

I want to build a Python based user interface for an existing Fortran application (as everyone wants to do ;)). Lets assume the application is parametrised via cmdline parameters and it calculates tons of numbers
that have to find their way into the Python UI. There are several ways to achieve this: using stdin/stdout to exchange data, using files, converting the application to a library and load that from Python, etc.

I thought about equipping the Fortran application with sockets, so that I can send input data and commands (which is now done via cmd line) and reading output data back.

Any thought on this or any best practices?

posted Jun 9, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Is the application a complete black box? Sounds to me like you have the power to edit it, so I'm guessing you have the source code and some knowledge of how it works. If you can, as you suggest, convert it into a library that can be called from a C program, you can use Cython to call on it from Python. That'd be my first recommendation.

(One-and-a-halfth recommendation: If the actual application is very simple, and most of its work is done in library functions, access the library via Cython, and port the main application logic entirely into Python. No need to wrap the application into a library, that way.)

Second option would be some kind of coroutine system, interfacing via a socket. That's quite a good option; all you have to do is settle, between the two, a set of protocol rules. Some examples:

  • Everything is encoded in ASCII. (That gives you the option of expanding to UTF-8 later, if you need full Unicode, but keeps it really easy for now.)
  • Commands and responses are terminated with end-of-line, 0x0A.
  • Commands follow the basic shell style of command, then a space (0x20), then parameters.
  • If you don't need to overlay responses: One command's responses end with a dot (0x2E) on a blank line. (See SMTP for an example of this.)
  • If you do need to have multiple commands in flight simultaneously: Every command is prefixed with an arbitrary token, followed by a space, and every line of response is prefixed with the same token. (See IMAP for an example of this.)

Nut out your protocol first, before you write a single line of code. Keep your protocol document up-to-date if you change anything. Then, if you want to write a different program for one end or the other, you can guarantee that they'll be able to communicate. And if you want to change from Unix sockets to TCP/IP sockets, or to stdin/stdout, or to any other system, the translation will be easier for having that document.

Third option: Keep the application as it is, and use Python's subprocess module to send it parameters and maybe stdin, and retrieve its stdout.

answer Jun 9, 2014 by anonymous
Similar Questions
+1 vote

If I compile c++ code, I am supposed to use g++. This automatically does some magic that calling just Gcc would not do. The same with Fortran code and gfortran. So my question is which frontend should I use to link if I have object files from both c++ as well as Fortran, c, and maybe another language still?

+3 votes

I need python code snippet for LTE/UMTS for automation testing.Example of scenario looking for LTE attach ,detach, MO/MT call .
I am looking for UE side scripts.

0 votes

I have a list, songs, which I want to divide into two groups.
Essentially, I want:

new_songs = [s for s in songs if s.is_new()]
old_songs = [s for s in songs if not s.is_new()]

but I don't want to make two passes over the list. I could do:

new_songs = []
old_songs = []
for s in songs:
 if s.is_new():
 new_songs.append(s)
 else:
 old_songs.append(s)

Which works, but is klunky compared to the two-liner above. This seems like a common enough thing that I was expecting to find something in itertools which did this. I'm thinking something along the lines of:

matches, non_matches = isplit(lambda s: s.is_new, songs)

Does such a thing exist?

0 votes

I am trying to use mitmproxy behind a company proxy that requires a user/password login.

The setup is: Local PC's browser -> mitmproxy (on local PC) -> company proxy -> internet.

Based on this SO thread, this is how you use mitmproxy within a Python program. This example works fine when there's no proxy.

from mitmproxy.options import Options
from mitmproxy.proxy.config import ProxyConfig
from mitmproxy.proxy.server import ProxyServer
from mitmproxy.tools.dump import DumpMaster

class Addon(object):
    def __init__(self):
        pass

    def request(self, flow):
        # examine request here 
        pass

    def response(self, flow):
        # examine response here
        pass


if __name__ == "__main__":

    options = Options(listen_host='0.0.0.0', listen_port=8080, http2=True)
    m = DumpMaster(options, with_termlog=False, with_dumper=False)
    config = ProxyConfig(options)

    m.server = ProxyServer(config)
    m.addons.add(Addon())

    try:
        print('starting mitmproxy')
        m.run()
    except KeyboardInterrupt:
        m.shutdown()

Assuming the company proxy is at IP "1.2.3.4" port 3128 and requires a login USER and PASSWORD, how can I change this script to have mitproxy use that proxy instead of going to the internet directly?

Addition info: I am not using mitmdump with the script-parameter to run this script. The goal is to run this from Python 3.8 with a pip-installed mitmproxy

0 votes

Inputs would be:
- Name
- age
- gender
- married/unmarried

User feeds this data once but he/she can check later on also. So in this case program should have option to feed and read data from file.

...