Showing posts with label Portable-Python. Show all posts
Showing posts with label Portable-Python. Show all posts
Friday, March 15, 2013
Wednesday, March 13, 2013
Python one-liner to get the filename and line number of the caller of the current function
By Vasudev Ram
Just saw this:
An ActiveState Python recipe to get the filename and line number of the calling function.
I modified it and tried it out, while I happened to be at an Internet cafe, using Portable Python, which I had blogged about earlier.
Here is my modified code:
And here is the output of "running" (*) that file, caller_info.py, using Portable Python:
Just for fun, I also tried the same code (not as a Python file, just typed in), in the
repl.it online pastebin that I blogged about earlier. It worked, sort of, the line number shown was 1 and the filename shown was stdin:
Just saw this:
An ActiveState Python recipe to get the filename and line number of the calling function.
I modified it and tried it out, while I happened to be at an Internet cafe, using Portable Python, which I had blogged about earlier.
Here is my modified code:
# caller_info.py import sys def foo(): print "in foo" f1 = sys._getframe(1) f2 = sys._getframe(2) print "f1: filename, lineno:", f1.f_code.co_filename, f1.f_lineno print "f2: filename, lineno:", f2.f_code.co_filename, f2.f_lineno def bar(): print "in bar" print "calling foo" foo() bar()
And here is the output of "running" (*) that file, caller_info.py, using Portable Python:
>>> import caller_info in bar calling foo in foo f1: filename, lineno: caller_info.py 16 f2: filename, lineno: caller_info.py 18 >>>(*) I said "running" because, as you can see from the code, I had to use a roundabout method of running the caller_info.py file with PortablePython. The standard method, like "python caller_info.py", that normal Python supports, does not seem to work with PortablePython (at least on a quick check or two; there may be some other way that works, such as a command-line switch or whatever). So I resorted to importing the file and letting the code in it run as a side-effect. Anyway, it seems to show that the one-liner works, because in the output shown, the filename is right, and so are the line numbers of the calls to foo and bar - I checked in the editor that the numbers are 16 and 18.
Just for fun, I also tried the same code (not as a Python file, just typed in), in the
repl.it online pastebin that I blogged about earlier. It worked, sort of, the line number shown was 1 and the filename shown was stdin:
def foo(): .. print "in foo" .. def bar(): .. print "in bar, calling foo" .. foo() .. f = sys._getframe(1) .. print f.f_code.co_filename .. print f.f_lineno .. bar() in bar, calling foo in foo <stdin> 1- Vasudev Ram - Dancing Bison Enterprises
Monday, March 4, 2013
Portable Python, Python on a USB stick
Portable Python is a Python installation preconfigured to run from any USB storage device, like a USB stick.
It is available for some versions of Python 2 and Python 3. It also comes with many other Python packages / tools. It is optional to install those during the Portable Python installation. Some of those packages are:
PyScripter v2.5.3
NymPy 1.6.2 (*)
SciPy 0.11.0
Matplotlib 1.1.1
PyWin32 218
Django 1.4.3
PIL 1.1.7
Py2Exe 0.6.9
wxPython 2.9.4.0
NetworkX 1.7
Lxml 2.3
PySerial 2.5
PyODBC 3.0.6
PyGame 1.9.1
PyGTK 2.24.2
PyQt 4.9.6-1
(*) Nympy is probably a typo, it must be Numpy.
The fact that it includes Django, wxPython, PyGTK, PyQt, SciPy, Matplotlib, etc., makes it more useful out of the box.
I am trying out Portable Python and will report on my experience with it after a while. If it works well I think it could be a useful tool for people who sometimes need to program with Python on the go, at any place where they may not have a regular installation of Python available, e.g. if you are somewhere without a desktop PC or a laptop, maybe at an Internet cafe, it could be useful there.
The Portable Python About page gives some info about its background and goals.
Vasudev Ram - Dancing Bison Enterprises
Subscribe to:
Comments (Atom)
