new machine : Python calculator
120919 Marc Joliet wrote:
> 120918 Philip Webb <purslow@ca.inter.net> wrote: >> With Python running as interpreter, I would get much more capability, >> but I would need to enter the special line to load the math functions : >> is it possible to do it with some capitalised variable in .bashrc , >> which might list parameters telling Python3 what to load when it starts ? >> one of the 'man' files seems to refer to something like that, but briefly. > 3.) Put the "import" line in its own file and put it in the variable > PYTHONSTARTUP, e.g. "export PYTHONSTARTUP=/path/to/my/script.py". > Python executes it's contents before presenting the prompt, > so you can put whatever imports you want in that script. Thanks, that's what I saw in my brief glance at the 'man'. It works out of the box: the only problem is precision, which at 16 decimal places is a bit more than I usually need (smile). I can search out how to limit it to something more useful to me, but might you have a quick answer ? Thanks for the above. -- ========================,,======================== ==================== SUPPORT ___________//___, Philip Webb ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto TRANSIT `-O----------O---' purslowatchassdotutorontodotca |
new machine : Python calculator
Am Thu, 20 Sep 2012 05:05:11 -0400
schrieb Philip Webb <purslow@ca.inter.net>: > 120919 Marc Joliet wrote: > > 120918 Philip Webb <purslow@ca.inter.net> wrote: > >> With Python running as interpreter, I would get much more capability, > >> but I would need to enter the special line to load the math functions : > >> is it possible to do it with some capitalised variable in .bashrc , > >> which might list parameters telling Python3 what to load when it starts ? > >> one of the 'man' files seems to refer to something like that, but briefly. > > 3.) Put the "import" line in its own file and put it in the variable > > PYTHONSTARTUP, e.g. "export PYTHONSTARTUP=/path/to/my/script.py". > > Python executes it's contents before presenting the prompt, > > so you can put whatever imports you want in that script. > > Thanks, that's what I saw in my brief glance at the 'man'. > It works out of the box: the only problem is precision, > which at 16 decimal places is a bit more than I usually need (smile). > I can search out how to limit it to something more useful to me, > but might you have a quick answer ? Thanks for the above. Reading up the "format specification mini language" (http://docs.python.org/library/string.html#formatspec, and the format syntax explained above it), you could do as follows, to print as float rounded to four decimal places: print('{0:.4f}'.format(2.4)) Or, leaving out the zero (you only need the indexes if you print things out of order or multiple times): print('{:.4f}'.format(2.4)) Also, I re-remembered that there is an alternative formatting method (I don't print formatted output that often in python, I guess): print("%.4f" % 2.4) will do the same as the above two examples. Either way, to make things easy, you could define your own print function to do that for you, e.g.: def myprint(num, places=4, *args, **kargs): fmt_str = "{:." + str(places) + "f}" print(fmt_str.format(num), *args, **kargs) Using it would look like (in IPython): In [13]: myprint(2.4) 2.4000 In [14]: myprint(2.4, 5) 2.40000 You would put this in the startup script after the import line. Note that it passes extra positional and keyword arguments to print(), so you can specify a file to print to, for example. Also note that because of this, it won't work in Python 2. HTH -- Marc Joliet -- "People who think they know everything really annoy those of us who know we don't" - Bjarne Stroustrup |
new machine : Python calculator
[...]
> > def myprint(num, places=4, *args, **kargs): > fmt_str = "{:." + str(places) + "f}" > print(fmt_str.format(num), *args, **kargs) OK, quick update because I just realised how weird it is to have positional arguments after a (potential) keyword argument (I really should go to bed). Either of these is better: # "places" is exclusively a keyword argument now def myprint(num, *args, places=4, **kargs): fmt_str = "{:." + str(places) + "f}" print(fmt_str.format(num), *args, **kargs) # doesn't support extra arguments to print(), but is simpler def myprint(num, places=4): fmt_str = "{:." + str(places) + "f}" print(fmt_str.format(num)) -- Marc Joliet -- "People who think they know everything really annoy those of us who know we don't" - Bjarne Stroustrup |
| All times are GMT. The time now is 04:06 AM. |
VBulletin, Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.