• Welcome to the new Internet Infidels Discussion Board, formerly Talk Freethought.

Python

One problem is Python does not appear to have a formatted file read similar to C fscanf()
fscanf is a library function in C. You can easily write it in python if you want.
.

Actually the Python array read and write functions I posted are essentially fprintf() and fscanf().

Examples of passing variable numbers of arguments to a function with a format string.
 
Sadly, I see a window open, which immediately closes.
I lost currency with Windows a way back.

The two common post install problems for me have been
path variable problems
file/directory permissions

Are you running as administrator? Check the security settings in the install directoriess. Sometimes explicitly running an app as admin fixed problems.

You might want to check the file associations and make sure .py is associated with Python.
 
One problem is Python does not appear to have a formatted file read similar to C fscanf()
fscanf is a library function in C. You can easily write it in python if you want.
.

Actually the Python array read and write functions I posted are essentially fprintf() and fscanf().

Examples of passing variable numbers of arguments to a function with a format string.
Yes, python is wastely more powerful when it comes to text processing.
 
The basic Python IDE IDLE is fine for general math but does not have huih level functions for adding packages/modules and project management.

There are two Python graphics packages matplotlib and pyqtgraph I am working on adding.

With graphics it is a near complete math package. There are math packages for added functionality like matrix operations.

Interesting Python fact, there is no limit on the size of an integer other than memory. It increases size of a variable as it grows.
 
Python does not have an include file function. It appears you have to import each function you need.


file test1.py

Diff:
def adf(a,b):
    return a + b





Accessing adf() in another script file

Code:
import math
import os
os.chdir('c:\\python')
from test1 import adf
x = adf(6,3)
print(x)
 
You can use "import <file>" on your files. You then address its contents with <file>.<something>
 
Python does not have constants that can not be changed. That ca be a problem on lather projects.

math.pi is not a constant, the value of pi can be changed. For me that is a problem. Inadvertent changing of a constant.

The Python class object is not as sophisticated as the C++ class. There is no absolute way to protect data and limit access to class functions.

From the net Python and C/C++ are at the top. Python i s good but for complicated code I'd yse C++. I found a C++ interpreter oriented to science and math called CH. There is a small cost for a single license.

Code:
In C++


class x{

private:
int b = 345
public:
int a = 0
void f1(void){
return a*b

}


void f2(int x){
b = x
}
};

x.f2(123)
x.a = 123
cout<<x.f1()

b can not be accessed or changed from outside the c;lss


In Python


class x:
   a = 0
   def f():
      return x.a


x.a = 123
print(x.f())

in Python internal class variables and functions have to be referenced to the class.


 
Last edited:
Parsing a math function in a string without Python parse functions.

Code:
import math
func = 'log(10)'
f = ''
arg = ''
n = len(func)

for i in range(n):
    if func[i] == '(': break
    f += func[i]
print(f)

for j in range(i+1,n):   
    if func[j] == ')': break
    arg += func[j]
print(arg)
num = 0

match f:
    case 'log': num = math.log(float(arg))
    case _:print('No Match')

print(num)
 
I have made a whole bunch of stuff in Python. It is the absolute best "getting shit done" programming language.

Whenever I speak to someone who's interested in dabbling in some code, I tell them to try Python if they haven't already done that themselves. They can always go and learn C later if they really catch the coding bug, but Python offers two huge benefits to beginners and non-IT people:
  1. It's pretty easy to learn.
  2. It's very useful to people who aren't IT professionals but who use computers at work.
I've primarily used Python to solve three problems:
  1. I need to work on some data.
  2. I need to automate some robotic process.
  3. I need to make a simple app quickly.
 
On the net it is said Python and C/C++ are the top two languages.
 
Well Javascript is the top language by usage, but that's because it has a monopoly in the browser, not because it's good.
 
If you are using Python for math an easy way to do xy plots is create a .csv file and import to a spread sheet

Code:
import math
import os
os.system('cls')


def make_sin(n,tmax,x,y):
        dt = tmax/(n-1.)
        t = 0
        for i in range(n):
                x[i] = t
                y[i] = math.sin(t)
                t += dt


def save_nums(fname,delim,*args):
        nrows = len(args[0])
        # check number of rows the same
        for i in range(len(args)):
            if len(args[i]) != nrows : return 'Rows Mismatch'
        f = open(fname,'w')
        for i in range(nrows):
                s = ' ' # row string
                j = 0
                for x in args:
                    s += repr(x[i])
                    if j < len(args)-1:s += delim
                    j += 1
                s += '\n'
                f.write(s)
        f.close()
        return 'Save Complete'
      
ns = 100
ys = ns*[0]
xs = ns*[0]
tmax = math.pi*2
make_sin(ns,tmax,xs,ys)
fn = 'c:\\python\\datas.csv'
delm = ','
w = save_nums(fn,delm,xs,ys)
print(w)
 
Well Javascript is the top language by usage, but that's because it has a monopoly in the browser, not because it's good.
I used c/c++ and a scripted language in a math tool Scilab. Other than that I was ignorant of anything else.

If I had been aware of it I might have used Python. It has similar math libs to Scilab, but lacks the native graphics and data representation. For math I coded in C and used Scilab for graphics.
 
When saving numbers you may want to truncate or round.

Code:
import math
import random

    def set_dec_places(x,y,dp,t):
        #maintains original numbers
        #shift dec point right
        #truncate or round up
        #restore dec point
        n = len(x)
        shift = pow(10,dp) #dec point shift
        for i in range(n):
                match t:
                        case 'i':y[i] = int(x[i]*shift)  #truncate
                        case 'r':y[i] = round(x[i]*shift)  #round up
                y[i] =  y[i]/shift #restore dec point

#test with random numbers
n = 100
z = n*[0]
zr = n*[0]
dp = 2
for i in range(n):
        z[i] = random.uniform(0,100)

set_dec_places(z,zr,dp,'r')
for i in range(n):
        print('%f   %f' %(z[i],zr[i]))
 
Well Javascript is the top language by usage, but that's because it has a monopoly in the browser, not because it's good.
I used c/c++ and a scripted language in a math tool Scilab. Other than that I was ignorant of anything else.

If I had been aware of it I might have used Python. It has similar math libs to Scilab, but lacks the native graphics and data representation. For math I coded in C and used Scilab for graphics.
Check out matplotlib.
 
I can't find a simple download without a package with Python which I have.
 
I use a general-purpose open-source archive for all my Python add-ons. Be careful when using one of these that you use the ones for whichever version of Python that you have.

MacOS ones: Homebrew — The Missing Package Manager for macOS (or Linux) and The MacPorts Project -- Home and Fink - Home

Linux ones are usually included in whichever distribution that one is using.

Free/Open-Source package managers for Windows? please share your experience. : windows - listing several of them
That's OK I guess, but pip works better:
  1. Install packages without admin privileges.
  2. Install a specific set of dependencies from a requirements.txt file for a project.
  3. Install packages in a virtualenv.
  4. If it's on PyPI (https://pypi.org/), you can install it with pip. With your OS package manager you're relying on another maintainer to repackage your dependency.
  5. Works the same on every OS.
 
Back
Top Bottom