Re-use result of last line Jupyter - python

In Jupyter notebook, is there any way to re-use the output the line above, within a cell?
Coming from Mathematica, I often find it useful to write things commands which work on the output of the last line using %, here's a stupid example:
Integrate[f[x],x]
Limit[%,x->1] - Limit[%,x->0]
In general one can write %%% for 3rd-last output etc. https://reference.wolfram.com/language/ref/Out.html
#nostradamus reminds me that underscore _ is the output of the last cell, at least in Python. (Get last answer .) I didn't initially ask this, but would particularly like to be able to do this within a cell, so as to be able to execute multiple steps with one shift-enter.
I would also like to know if there's a way of doing either of these in Julia instead of Python.

In julia, ans stores the result of evaluating the last statement.
4*2
ans/2
You may also be interested in checking out the piping syntax
4*2 |>
sqrt

You can use "_" and works generally in python environment.

In case someone else finds this with google, I just discovered a package that does roughly what I wanted, in Julia: ChainRecursive.jl uses it as the magic word, like so:
julia> using ChainRecursive
julia> #chain for k=1:4
k^2 + k^3
print(" $k -> $it ")
end
1 -> 2 2 -> 12 3 -> 36 4 -> 80
There appears to be no performance lost by using this, as it is un-wrapped before compilation.

Related

Python : Fast search a valid substring for text from list of substrings

I need a fast and efficient method for searching pattern string from a list of many pattern strings which are valid substring of a string.
Conditions -
I have a list of 100 pattern strings added in a particular sequence (known).
The test case file is of size 35 GB and contains long strings in subsequent
lines
Ask -
I have to traverse the file and for each line, I have to search for a matched pattern string that is a valid substring of the line (whichever comes first from the list of 100 pattern strings).
Example -
pattern_strings = ["earth is round and huge","earth is round", "mars is small"]
Testcase file contents -
Among all the planets, the earth is round and mars is small.
..
..
Hence for the first line, the string at index 1 should qualify the condition.
Currently, I am trying to do a linear search -
def search(line,list_of_patterns):
for pat in list_of_patterns:
if pat in line:
return pat
else:
continue
return -1
The current run time is 21 minutes. The intent is to reduce it further. Need suggestions!
One trick I know of, though it has nothing to do with changing your existing code, is to try to run your code with PyPy rather than the standard CPython interpreter. That could be one trick that does significantly speed up execution.
https://www.pypy.org/features.html
As I have installed and used it myself, I can tell you know that installation is fairly simple.
This is one option if you do not want to change your code.
Another suggestion would be to time your code or use profilers to see where the bottleneck is and what is taking a relatively long amount of time.
Code-wise, you could avoid for loop and try these methods: https://betterprogramming.pub/how-to-replace-your-python-for-loops-with-map-filter-and-reduce-c1b5fa96f43a
A final option would be to write that piece of code in a faster more performant language such as C++ and call that .exe (if on Windows) from Python.

Python file - change from CamelCase to underscore naming convention - any way to do it faster?

Few weeks back, in my 4th week of learning python, I have written my first real-life program. Now about a month later, with a little bit more experience in hand, I am trying to refactor the code. First thing I want to do is to change my naming convention to follow the PEP8 guidelines.
Originally I have used a Jupyter Notebook to interactively get the program ready. Since I have come from a Java programming background, using the notebook interface was quite a positive and pleasant surprise as I could inspect the values and code in an interactive manner. Then, once done, I copied and pasted the entire code in a python file and used the Mu Code Editor to run it as a whole.
Now that I am trying to refactor by doing a simple renaming of all the items from a camelCase to underscore convention, both Jupyter and Mu Code Editor did not have any support for refactoring. Hence, after doing a bit of googling, I installed PyCharm and started trying it. The ability to refactor-rename is quite nice, however, I find that it has a number of nuances that I don't fully understand - are there any tips for using renaming so that I could convert all the variables, functions using camelCase to use the underscore convention quickly - for example, all the variables names to be changed in one go, rather than doing it one by one?
Here is the code I am trying to refactor - https://github.com/ssamsudeen/learning-python/blob/master/quranSRS.py
Note: After posting this, I did a bit of experiment to see how Find & Replace works in Jupyter works - Find & Replace dialog had 3 toggle buttons for case sensitivity, regular expressions, and the ability to only replace within selected cells. I found the output it provides to be meaningful - so, this is what I am doing now, till I find a better way to do this. Any suggestion on how I can do Find & Replace better would also be welcome (Ideally, I want the replace to exclude my comments or certain cells, but could not figure out how to do them)
Unfortunately this has to be done with a script, because regular expression string substitution on its own can't change letter case. Here's a Python script to do it:
import sys
import re
with open(sys.argv[1]) as prgm:
text = prgm.read()
def subfunc(m):
result = ''
result += m.group(1)
# first word
result += '_'
result += m.group(2).lower()
# second word, if present
if m.group(3):
result += '_'
result += m.group(3).lower()
result += m.group(4)
return result
text = re.sub(r"(\W?[a-z]+)([A-Z][a-z]+)([A-Z][a-z]+)?(\W|$)", subfunc, text)
with open(sys.argv[1], 'w') as prgm:
prgm.write(text)
Save as change_caps.py, run python change_caps.py quranSRS.py

Python index like MATLAB's end/2

MATLAB has a very convenient syntax for getting half of a list:
x(1:end/2)
The syntax I know for python to do this is
x[:len(x)/2]
This is fine in this case, because len(x) is easy to write. But this syntax becomes more than a pain when the name of the list is long (as they sometimes need to be), and even more so when there is a list of similar long names.
I know this is a real shot in the dark, but does python have any syntax option like MATLAB's?
There is no specialized syntax. If you need to do it a lot, write a function:
def half_list(l):
return l[:len(l)/2]
No, lists in python don't have the concept end( a somewhat similar concept is the index -1).
An easy (but not recommended) solution to your problem is:
l = longnamelist
l[:len(l)/2]
or to copy/paste the long name... (some editors have a shortcut for copying a word, this makes copy/paste of a long name very easy.)

Reading user input in python 2.4, putting it into a queue

So I'm writing a differential calculator program in python 2.4 (I know it's out of date, it's a school assignment and our sysadmin doesn't believe in updating anything) that accepts a user input in prefix notation (i.e. input = [+ - * x^2 2x 3x^2 x], equivalent to x^2 + 2x - 3x^2 * x) and calculates the differential.
I'm trying to find a way to read the command line user input and put the mathematical operators into a queue, but I can't figure it out! apparently, the X=input() and x=raw_input() commands aren't working, and I can find literally 0 documentation on how to read user input in python 2.4. My question is: How do I read in user input in python 2.4, and how do I put that input into a queue? Here is what I am trying:
1 formula = input("Enter Formula:")
2
3 operatorQueue=[]
4
5 int i = len(formula)
6
7 for x in formula:
8 if formula[x] == '*', '+', '-', '/':
9 operatorQueue.append(formula[x])
0
11 print "operator A:", operatorQueue.pop(0)
12
Which is not working (I keep getting errors like "print: command not found" and "formula:command not found")
Any help would be appreciated
#miku already answered with this being your initial problem, but I thought I would add some more.
The "sh-bang" line is required by command line scripts so that the proper process is used to interpret the language, whether it be bash, perl, python,etc. So in your case you would need: /usr/bin/env python
That being said, once you get it running you are going to hit a few other issues. raw_input should be used instead of input, because it will give you back a raw string. input is going to try and eval your string which will mostly likely give you problems.
You may need to review python syntax a bit more. Assignments in python don't require that you declare the variable type: int a = 1. It is dynamic and the compiler will handle it for you.
Also, you will need to review how to do your if elif else tests to properly handle the cases of your formula. That too wont work doing it all on one line with multiple params.
If you're on a unix-ish platform, put a
#!/usr/bin/env python
on top of your program. The shell does not seem to recognize that you are running a python script.

Is it possible to access the source code of a python script passed to python on standard in?

This is a bit of a random question that is more out of curiosity than any specific need.
Is it possible to write some python code that will print some stuff out, including the source code itself, without having the python code stored in a file? For example, doing something like this at the Bash prompt:
$ echo '
> print "The Code:"
> PrintScript() # What would this function look like?
> for i in range(5):
> print i,
> print "!"
> ' | python
and get an output like this:
The Code:
print "The Code:"
PrintScript() # What would this function look like?
for i in range(5):
print i,
print "!"
0 1 2 3 4 5 !
I suspect that this probably can't be done, but given python's introspection capabilities, I was curious to know whether it extended to this level.
That's the closest I'm getting:
echo 'import __main__,inspect;print inspect.getsource(__main__)' | python
which fails... In any case, the original code is eaten up (read from stdin) by the interpreter at startup. At most you may be able to get to the compiled code, again through the __main__ module.
Update:
The dis module is supposed to give you a disassembly of all functions in a module, but even that one isn't seeing any code:
$ echo -e 'import __main__,dis;print dis.dis(__main__)' | python
None
And even when I throw in a function:
$ echo -e "import __main__,dis;print dis.dis(__main__)\ndef x():\n pass" | python
None
Yes, it is indeed possible to write a program which outputs it's own source. You don't need even introspection for this tasks, you just need to be able to print computed strings (works with every language).
The technique is called Quine and here is a rather short example in Python:
quine = 'quine = %r\r\nprint quine %% quine'
print quine % quine
But quines aren't limited to such simple programs. They can do much more, for example printing their own source backwards and so on... :)
print open(__file__).read(),
This will work on UNIX systems I think, but I'm not sure about Windows. The trailing comma makes sure that the source code is printed exactly, without an extra trailing newline.
Just realized (based on the comments below) that this does not work if your source code comes from sys.stdin, which is exactly what you were asking for. In that case, you might take advantage of some of the ideas outlined on this page about quines (programs printing their own source codes) in Python, but none of the solutions would be a single function that just works. A language-independent discussion is here.
So, in short, no, I don't think this is possible with a single function if your source code comes from the standard input. There might be a possibility to access the interpreted form of your program as a Python code object and translate that back into source form, but the translated form will almost surely not match the original file contents exactly. (For instance, the comments and the shebang line would definitely be stripped away).
closest you can get is using readline to interrogate the command history if available from what i can see e.g. but i suspect this may not contain stuff piped into the session and would only work for interactive sessions anyway

Categories