Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Programming Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...
# 1  
Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all...

As you know I like making code backwards compatible for as many platforms as possible.

This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam on a stock A1200 only to expansions to 68060, FPU, MMU and serious memory for the day.
This is designed purely for my stock A1200 with HDD and 4MB of extra FastRam and Python 1.4.0, the only version that does not require MMU and FPU.
It requires ADE the *NIX emulator and gcc 2.95.3 to work and the result gives a simple calculator for ADE as bc, dc and others do not exist.
AND before anyone mentions the 'r' prefix on the 'docstring' variable = r""" Some raw text... """ , Pythons 1.4.0 to 1.6.x do NOT have that facility. So a basic docstring is required. Therefore ALL escaped text MUST have the escqpe escaped, for example, \n becomes \\n .

I uploaded it on 30-04-2019 and it reached 179 dls in 14 days, it is now at 197 dls as of 20-05-2019.
Aminet - dev/gcc/C_Source_In_Python.py.txt

( @ Corona688, your awk DFT is now at 734 dls, Aminet - dev/gcc/DFT-FFT.awk.txt and I expect it to be over 1000 by the end of the year. )

It is not easy working out all the permutations and combinations of a language where each major version is near entirely different from its predecessor but this is the result, note my MBP booting into Linux Mint 19:
Code:
import sys
import os

EMBED_C="""
/* Simple Calculator in C. */
/* Compiled througth ADE, the *NIX emulator, using:- */
/* gcc -Wall -pedantic -ansi -o calc mycalc.c -lm */

/* This version is purely for any AMIGA Python from 1.4.0 upwards. */
/* Also tested on Python 2.7.10 and 3.5.2, on OSX 10.14.3 and Linux Mint 19. */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double SOLUTION;
double FLOAT_1;
double FLOAT_2;
char OPERATOR;

double calc(double FLOAT_1, double FLOAT_2, char OPERATOR)
{
    double SOLUTION = 0.0;
    switch(OPERATOR)
    {
        case '+':
            SOLUTION = FLOAT_1 + FLOAT_2;
            break;
        case '-':
            SOLUTION = FLOAT_1 - FLOAT_2;
            break;
        case 'x':
            SOLUTION = FLOAT_1 * FLOAT_2;
            break;
        case '/':
            SOLUTION = FLOAT_1 / FLOAT_2;
            break;
        case '^':
            SOLUTION = pow(FLOAT_1, FLOAT_2);
            break;
        case 'r':
            FLOAT_2 = 1.0 / FLOAT_2;
            SOLUTION = pow(FLOAT_1, FLOAT_2);
            break;
    }
    return SOLUTION;
}

int main(int argc, char *argv[])
{
    char OPERATOR;
    double FLOAT_1;
    double FLOAT_2;
    double SOLUTION;

    if (argc <= 3)
    {
        printf("\\nERROR!\\n\\n");
        printf("Usage: calc FLOAT_1 <|+|-|x|/|^|r|> FLOAT_2<CR>\\n");
        printf("Where: '+'=ADD; '-'=SUBTRACT; 'x'=MULTIPLY; '/'=DIVIDE;\\n");
        printf("       '^'=POWER; and 'r'=NTHROOT.\\n\\n");
        printf("Example: calc 781.37 r 3.3<CR>, (NOTE: spaces), where <CR> is the ENTER key!\\n\\n");
        /* Google result: 7.52702316103. */
        exit(1);
    }

    SOLUTION = 0.0;
    OPERATOR = *argv[2];
    FLOAT_1 = strtod(argv[1], NULL);
    FLOAT_2 = strtod(argv[3], NULL);

    SOLUTION = calc(FLOAT_1, FLOAT_2, OPERATOR);
    printf("%.11f\\n", SOLUTION);
    return(0);
}
"""

# Create the C file.
myfile = open("mycalc.c", "w")
myfile.write(EMBED_C)
myfile.close()

# Print the whole C file to stdout.
print(EMBED_C)

# Compile the C file.
print("Compile mycalc.c to calc using:-")
print("os.system(\"gcc -Wall -pedantic -ansi -o calc mycalc.c -lm\")\n")
os.system("gcc -Wall -pedantic -ansi -o calc mycalc.c -lm")

# Now run it...
print("Run calc to calculate 781.37 to root 3.3 and save in a _variable_:-")
# The next few lines takes the calculation and places it into 'result'.
if sys.platform == 'amiga':
    calc_result = os.popen("calc 781.37 r 3.3")
else:
    calc_result = os.popen("./calc 781.37 r 3.3")
result = calc_result.read()
calc_result.close()

# This is the result.
print(result)
print("Google value using 781.37**(1.0/3.3) is:-\n7.52702316103")
sys.exit()

Results on Linux Mint 19, default bash terminal, Python 2.7.x, gcc 7.3.0.
Code:
bazza@amiga-MacBookPro:~$ cd Desktop/Code/Python
bazza@amiga-MacBookPro:~/Desktop/Code/Python$ python embed_C.py

/* Simple Calculator in C. */
/* Compiled througth ADE, the *NIX emulator, using:- */
/* gcc -Wall -pedantic -ansi -o calc mycalc.c -lm */

/* This version is purely for any AMIGA Python from 1.4.0 upwards. */
/* Also tested on Python 2.7.10 and 3.5.2, on OSX 10.14.3 and Linux Mint 19. */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double SOLUTION;
double FLOAT_1;
double FLOAT_2;
char OPERATOR;

double calc(double FLOAT_1, double FLOAT_2, char OPERATOR)
{
    double SOLUTION = 0.0;
    switch(OPERATOR)
    {
        case '+':
            SOLUTION = FLOAT_1 + FLOAT_2;
            break;
        case '-':
            SOLUTION = FLOAT_1 - FLOAT_2;
            break;
        case 'x':
            SOLUTION = FLOAT_1 * FLOAT_2;
            break;
        case '/':
            SOLUTION = FLOAT_1 / FLOAT_2;
            break;
        case '^':
            SOLUTION = pow(FLOAT_1, FLOAT_2);
            break;
        case 'r':
            FLOAT_2 = 1.0 / FLOAT_2;
            SOLUTION = pow(FLOAT_1, FLOAT_2);
            break;
    }
    return SOLUTION;
}

int main(int argc, char *argv[])
{
    char OPERATOR;
    double FLOAT_1;
    double FLOAT_2;
    double SOLUTION;

    if (argc <= 3)
    {
        printf("\nERROR!\n\n");
        printf("Usage: calc FLOAT_1 <|+|-|x|/|^|r|> FLOAT_2<CR>\n");
        printf("Where: '+'=ADD; '-'=SUBTRACT; 'x'=MULTIPLY; '/'=DIVIDE;\n");
        printf("       '^'=POWER; and 'r'=NTHROOT.\n\n");
        printf("Example: calc 781.37 r 3.3<CR>, (NOTE: spaces), where <CR> is the ENTER key!\n\n");
        /* Google result: 7.52702316103. */
        exit(1);
    }

    SOLUTION = 0.0;
    OPERATOR = *argv[2];
    FLOAT_1 = strtod(argv[1], NULL);
    FLOAT_2 = strtod(argv[3], NULL);

    SOLUTION = calc(FLOAT_1, FLOAT_2, OPERATOR);
    printf("%.11f\n", SOLUTION);
    return(0);
}

Compile mycalc.c to calc using:-
os.system("gcc -Wall -pedantic -ansi -o calc mycalc.c -lm")

Run calc to calculate 781.37 to root 3.3 and save in a _variable_:-
7.52702316103

Google value using 781.37**(1.0/3.3) is:-
7.52702316103
bazza@amiga-MacBookPro:~/Desktop/Code/Python$ _

So even though these Python versions are worlds apart, if the minimum is found between them all then everyone can share fully usable code for, now esoteric, platforms.
( Long live the AMIGA... <wink> )

EDIT:
Now tested on Python 3.6.7 and 3.7.1 on Linux Mint 19...

Last edited by wisecracker; 05-20-2019 at 01:38 PM.. Reason: See EDIT.
This User Gave Thanks to wisecracker For This Post:
# 2  
Note that your four global variables (SOLUTION, FLOAT_1, FLOAT_2, and OPERATOR) are never referenced in your code because both of your subroutines (main() and calc()) have local variables with the same names.

If you get rid of the global variables, you should get exactly the same results from a binary that might be a few bytes smaller.
This User Gave Thanks to Don Cragun For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #899
Difficulty: Easy
Programs included in BusyBox can be run simply by adding their name as an argument to the BusyBox executable.
True or False?

9 More Discussions You Might Find Interesting

1. Programming

Search or find a element inside list python

I have a list as follows: From this i need to grep the element using keyword as "primary" and return output as 12:13-internet-wifi-primary i used as follows if (i <= (len(system_info))): ss = system_info print... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

4. Shell Programming and Scripting

sed command inside paramiko python

Hi I am trying to execute a sed command inside paramiko which finds and deletes the particular string from a file But sed command doesnt work inside paramiko python machine=qwe dssh = paramiko.SSHClient() dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ... (4 Replies)
Discussion started by: Priya Amaresh
4 Replies

5. Programming

A Function To Create A 1 Second Sinewave WAVE Beep File In Python.

sinebeep.py Creating an audio WAVE file called... beep.wav ...that can be played using almost ANY audio player available. This simple DEMO snippet of code generates a 1 second sinewave WAVE file. It IS saved inside the CURRENT drawer so that you can find it... ;o) Note that the... (1 Reply)
Discussion started by: wisecracker
1 Replies

6. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

7. SuSE

"ssh suse-server 'python -V' > python-version.out" not redirecting

Okay, so I have had this problem on openSUSE, and Debian systems now and I am hoping for a little help. I think it has something to do with Python but I couldn't find a proper Python area here. I am trying to redirect the output of "ssh suse-server 'python -V'" to a file. It seems that no matter... (3 Replies)
Discussion started by: Druonysus
3 Replies

8. Programming

Python: bash-shell-like less functionality in the python shell

Hello, Is there some type of functional way to read things in the Python shell interpreter similar to less or more in the bash (and other) command line shells? Example: >>> import subprocess >>> help(subprocess) ... ... I'm hoping so as I hate scrolling and love how less works with... (0 Replies)
Discussion started by: Narnie
0 Replies

9. UNIX for Advanced & Expert Users

Help needed to test CorneliOS open source software on different platforms

Hello everyone, I' m currently working on an open source project and I'm looking for people willing to test the software on various platforms so that we'll be able to publish a compatibility list. The project is called CorneliOS, it's a webOS and web application framework, and it has been... (0 Replies)
Discussion started by: joskirps
0 Replies

Featured Tech Videos