You are not logged in.

#1 2017-09-11 10:00:42

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

py3 interactive calc

script -q -a "$HOME/.pcalc.txt" -c "python3 -ic \"import sys,math; sys.ps1=''\""

Will start python3 interactive console (with empty prompt) and everything will be logged to ~/.pcalc.txt.

example session:

1+2
3
_*5
15
_+2
17
exit()

Expanded slightly to a script (this should also hopefully drop latest valid numeral to ~/.pans.txt).

Edit: latest version

pycalc -n 10*10 # noninteractive
100

pycalc # interactive, reads ~/.pans.txt to py ans variable
ans
100
ans*10
1000
_+1
1001
_+ans
1101
exit()
storing ans 1101 to ~/.pans.txt

pycalc # 2nd session
ans
1101
exit()
storing ans 1101 to ~/.pans.txt

Last edited by brontosaurusrex (2017-09-11 22:11:14)

Offline

#2 2017-09-11 11:27:58

iMBeCil
WAAAT?
From: Edrychwch o'ch cwmpas
Registered: 2015-09-29
Posts: 767

Re: py3 interactive calc

For those who want sqrt, trigonometric functions and friends, add 'math' to import:

$ script -q -a "$HOME/.pcalc.txt" -c "python3 -ic \"import sys,math; sys.ps1=''\""
math.sqrt(42)
6.48074069840786
math.sin(42)
-0.9165215479156338
exit()

Postpone all your duties; if you die, you won't have to do them ..

Offline

#3 2017-09-11 11:55:40

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

Re: py3 interactive calc

@iMBeCil, added math.

Offline

#4 2017-09-11 12:58:53

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

Re: py3 interactive calc

@earlybird, thanks, will check.

Offline

#5 2017-09-11 14:04:31

iMBeCil
WAAAT?
From: Edrychwch o'ch cwmpas
Registered: 2015-09-29
Posts: 767

Re: py3 interactive calc

brontosaurusrex wrote:

@iMBeCil, added math.

Huh, thanks wink I just made a quick comment, not the request.

However, now I have suggestion for you script (in case you continue with it; cf. ipython; but I like your approach, IPython might be an overkill/bloat):
- if script is called without arguments, assume only 'import sys'
- if script is called with arguments, assume they are for the 'import'
Rationale: perhaps, certain user doesn't need 'math', but needs some other functionalities (network functions?).

Example usage:

$ yourscript  # would import 'sys' only
$ yourscript math,cmath,random,statistics  # would import 'math,cmath,random,statistics' alongside 'sys'

Postpone all your duties; if you die, you won't have to do them ..

Offline

#6 2017-09-11 14:13:42

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

Re: py3 interactive calc

I did a quick ipyhton and it makes my head hurt.

Just thinkering with how would variables travel from py > bash (or rather child > parent), at the moment one could do

source bin/pycalc
# do the interactive math
# 1+2+3
exit()

# and back in bash
echo $ans

Which isn't that bad i guess.

As for math functions, let's add what's useful, if the python interpreter still starts fast. And added non-interactive mode -n. Py modules to load are now in bash variable (configure in script).

Last edited by brontosaurusrex (2017-09-11 18:56:55)

Offline

#7 2017-09-11 16:12:24

tknomanzr
BL Die Hard
From: Around the Bend
Registered: 2015-09-29
Posts: 1,057

Re: py3 interactive calc

There is also numpy.

Offline

#8 2017-09-11 16:25:43

Martin
Member
From: Stockholm, Sweden
Registered: 2015-10-01
Posts: 800
Website

Re: py3 interactive calc

This little program is neat. I use it a lot both at home and at work.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  calculate.pyw
#
#  This is the second PyQt example from Rapid GUI Programming with
#  Python and Qt by Mark Summerfield. pp 116--121.


from __future__ import division  # To make sure we use floating point.
import sys  # Needed to read command line arguments
from math import *  # We are going to make calculations!
from PyQt4.QtCore import *
from PyQt4.QtGui import *  # The GUI stuff!

class Form(QDialog):
    def __init__ (self, parent=None):
        super(Form, self).__init__(parent)
        self.browser = QTextBrowser()  # Where results will appear.
        self.lineedit = QLineEdit("Type an expression and press Enter")
        self.lineedit.selectAll()  # Ready for overwriting
        layout = QVBoxLayout()  # Vertical box layout using:
        layout.addWidget(self.browser)  # Results box on top of
        layout.addWidget(self.lineedit)  # input line.
        self.setLayout(layout)
        self.lineedit.setFocus()  #.In focus ready to take input!
        # Run updateUi on hitting Return while cursor is in lineedit:
        self.connect(self.lineedit, SIGNAL("returnPressed()"), self.updateUi)
        self.setWindowTitle("Calculate")

    def updateUi(self):
        try:
            text = unicode(self.lineedit.text())  # Read from lineedit
            # Python's eval is cool, it does both parsing and calculation:
            self.browser.append("%s = <b>%s</b>" % (text, eval(text)))
        except:
            self.browser.append("<font color=red>%s is invalid!</font>" % text)

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

/Martin


"Problems worthy of attack
prove their worth by hitting back."
Piet Hein

Offline

#9 2017-09-11 17:24:01

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,746

Re: py3 interactive calc

@Martin, nice, any trick to use previous result, like '_ + 3' ? (Should read the book I guess and learn python and ...)

Last edited by brontosaurusrex (2017-09-11 19:05:22)

Offline

Board footer

Powered by FluxBB