EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: mendip_discovery on July 07, 2023, 05:24:32 pm

Title: Python used for calculators/converters.
Post by: mendip_discovery on July 07, 2023, 05:24:32 pm
<coat="flameproof">

I have never been a coder as such. More of a tinkerer, as I can work out some code and mod it for my use. I just don't have a coding brain, some argue I am brain free. I also like maths though I never really learnt much of it at school and didn't do any at college my understanding is limited at times.

Anyway, I am messing around with Python and Jupyter. I would like to use it for doing some maths. I know you can get a lot of these sites that will do the calculation for you but I always have an edge case that means I end up using Excel for it. Before I go faffing about making these up are there any published gits or sites for it?

For example PT100 and K-type maths. But I also need to do stuff for sine bars, water ml in g etc.
Title: Re: Python used for calculators/converters.
Post by: RAPo on July 07, 2023, 05:33:25 pm
It is even the standard Python distrib (https://pypi.org/project/pt100/).
For k-type, look here (https://pypi.org/project/thermocouples_reference/).
Title: Re: Python used for calculators/converters.
Post by: mendip_discovery on July 07, 2023, 05:43:24 pm
It is even the standard Python distrib (https://pypi.org/project/pt100/).
For k-type, look here (https://pypi.org/project/thermocouples_reference/).

The PT100 one looks to only be a table lookup but that was just a cursory scan of the code.

The thermocouple one looks nice.
Title: Re: Python used for calculators/converters.
Post by: radiogeek381 on July 16, 2023, 05:57:48 pm
Python has become my general-purpose desktop calculator. If I know I'll be in a session for a while, or if the calculations involve vectors or data files, I'll fire up a Jupyter session.

And mendip_discovery's comment about the availability of libraries illustrates an important point: no matter how weird your requirement is, there is a good chance someone has written a python library for it. Some are pretty good, others passable, and some are awful. But it is still better than starting with a flat rock.
Title: Re: Python used for calculators/converters.
Post by: mendip_discovery on July 16, 2023, 07:36:04 pm
I use online calculators and I also do a fair bit in excel. But it's hard to show how I worked it out and the formula it came from.

I am teaching myself LaTEX for the formulas.

For work I have had to do maths for measuring thread pitch diameters using cyclinders, and also taking into account for rake and compression. That put my in maths meltdown for a while. In excel it's ok but would be nice to use it as a function.

Last week I had to convert a pressure gauge that measures in tons to psi knowing the piston size is 75mm. All doable online but I always fear the online stuff will disappear the next time I need it. Plus many of them fail to explain how they get there so I cant be sure the numbers are correct.

I will go ahead with my plans. I just will put them on github for the next person who has to do the same maths. Hopefully it will help the next idiot who has to workout stuff.
Title: Re: Python used for calculators/converters.
Post by: eugene on July 17, 2023, 02:02:03 pm
There are lots of ways to do what it seems like you want to do. I tend to use Octave for a lot of things, because no matter how hard I try, I just can't grow fond of Python. But that's just me. I know several PhD'ed researchers that use Python for almost everything, and others that accomplish heroic feats in Excel. It all depends on where you feel comfortable.
Title: Re: Python used for calculators/converters.
Post by: RoGeorge on July 17, 2023, 02:51:39 pm
LaTEX for the formulas

My understanding is TeX is the typesetting language, while LaTeX is the language together with some predefined things, like it would be C plus the standard libraries.  For formulas only, it is not needed to learn TeX.  There are online helpers that can tell you the syntax, help you to find a certain symbol, and so on.

This post has a link with more examples than needed for formulas:  https://www.eevblog.com/forum/beginners/how-does-posting-math-notation-work/msg3994178/#msg3994178 (https://www.eevblog.com/forum/beginners/how-does-posting-math-notation-work/msg3994178/#msg3994178)

EEVblog forum supports MathJax, a LaTeX like syntax but not the full TeX language.



About Python vs a spreadsheet like Excel or Calc (Calc is the spreadsheet application from the free office package LibreOffice, compatible with Excel), or spreadsheet vs Matlab (Octave is the free Matlab, also compatible) use whatever comes natural to you.

A spreadsheet would be easier to draft some calculations for a one go, Python or Octave will be more powerful but will require more effort.

The spreadsheet wins for draft calculations, because you can see the result "live".  You type the input number in a cell, hit enter, and in another cell you see the result instantly, all under your eyes and at known positions on the screen, easy to comment about them in a nearby cell, to color them, plot a chart with a couple of mouse clicks, to save all for later, and so on.

When it comes to more elaborated data analysis, then Python (or Octave) is the right tool.
Title: Re: Python used for calculators/converters.
Post by: abeyer on July 17, 2023, 09:07:10 pm
If you're using python for the calculations, you can also generate the LaTeX for an annotated function w/ google's latexify: https://github.com/google/latexify_py
Title: Re: Python used for calculators/converters.
Post by: bpiphany on July 18, 2023, 08:24:47 am
For those with an affinity for python and spreadsheets, try python and spreadsheets. I haven't tried it myself, but I have a friend who keeps raving about it.

https://en.wikipedia.org/wiki/Pyspread
Title: Re: Python used for calculators/converters.
Post by: RAPo on July 18, 2023, 09:23:49 am
Don't tell them a disaster waiting to happen.

For those with an affinity for python and spreadsheets, try python and spreadsheets. I haven't tried it myself, but I have a friend who keeps raving about it.

https://en.wikipedia.org/wiki/Pyspread
Title: Re: Python used for calculators/converters.
Post by: RoGeorge on July 18, 2023, 09:50:53 am
Behind any office software package, there is a backend programming language already, usually accessible to the end user (without installing anything extra, and for free even in Excel) with all the classes that were used by developers, which means it can easily handle object from a spreadsheet, like cells or values, etc.

Usually it's as easy as hitting ALT+F11 in a spreadsheet, to open a code editor and start writing your own programs.

For Microsoft Office products (including Excel) the programming language is VBA (Visual Basic for Applications).  It is not unusual to see a spreadsheet as a GUI for one's own programs.  That's an unexpectedly powerful technique (for those fluent in VBA), just look at this 3 minutes demo:

Excel Magic
EngineeringFun
https://www.youtube.com/watch?v=mlJYhXNky3g (https://www.youtube.com/watch?v=mlJYhXNky3g)

 8)

Though, would be nice to have a spreadsheet with Python as the backend language.  ::)
Or at least to evaluate Python code in each cell, instead of just a spreadsheet formula.
Title: Re: Python used for calculators/converters.
Post by: abeyer on July 18, 2023, 07:49:54 pm
Though, would be nice to have a spreadsheet with Python as the backend language.  ::)
Or at least to evaluate Python code in each cell, instead of just a spreadsheet formula.

https://www.xlwings.org/ (https://www.xlwings.org/)
Title: Re: Python used for calculators/converters.
Post by: RoGeorge on July 19, 2023, 05:40:27 am
Something like that, from their demo videos xlwings looks very promising.
Would have give it a try, unfortunately I don't use Excel, nor Google Sheets.
Title: Re: Python used for calculators/converters.
Post by: bpiphany on July 19, 2023, 12:05:15 pm
Though, would be nice to have a spreadsheet with Python as the backend language.  ::)
Or at least to evaluate Python code in each cell, instead of just a spreadsheet formula.

Quote from: https://pyspread.gitlab.io/manual/basic_concepts.html
Everything is accessible
All parts of pyspread are written in Python, therefore all objects can be accessed from within each cell. This is also the case for external modules.


Don't tell them a disaster waiting to happen.

Quote from: https://pyspread.gitlab.io/manual/basic_concepts.html
Security
Since Python expressions are evaluated in pyspread, a pyspread spreadsheet is as powerful as any program. It could harm the system or even send confidential data to third persons over the Internet.
The risk is the the same that all office applications poese, which is why many provide precautions. The concept in pyspread is that you - the user - are trustworthy and no-one else.


Basic Concepts | pyspread (https://pyspread.gitlab.io/manual/basic_concepts.html)
Title: Re: Python used for calculators/converters.
Post by: eugene on July 19, 2023, 08:33:36 pm
Everyone should know enough not to run an Excel sheet with macros unless written by someone you know and trust. The same would be true of sheets written for pyspread.