Sometimes I'll be entering a long statement into ipython
In [4]: def foo():
...: a
...: bery # here's the error
...: long
...: function
...: definition
...: that
...: has
...: some
...: error
...: at
...: the
...: top
...:
And I'd like to be able to fix the error and submit the function but instead my workflow looks like this
In [7]: def foo():
...: a
...: very # 2) FIXED - I want to be able to ENTER from this line
...: # 3) but instead I get a newline
...: long
...: function
...: definition
...: that
...: has
...: some
...: error
...: at
...: the
...: top
...: # 1) I have to start here and scroll all the way to the top
...: # 4) so now I have to scroll all the way down here to ENTER the fixed function
...:
Any advice on how to
More efficiently navigate to the top of the code that I want to edit
Be able to submit from a line that is not the end
To submit a cell from anywhere in the block, hit ESC + return.
I am not sure about navigating to the top of the code (I am wondering the same myself, which is how I found your question). I find this is particularly problematic when scrolling back through history and then encountering a long block.
Edit: in case it's helpful, another useful trick is Ctrl + O to enter a new line in the same cell regardless of whether iPython wants to or not. Kind of the opposite of ESC + return.
Related
Let's say, I'm working in a REPL environment. I want to provide an example of a specific code snippet in my README. Something like this:
```python
>>> import math
>>> x = math.sqrt(64)
>>> print(f"The square root of 64 equals {x}")
The square root of 64 equals 8.0
```
Is there any way to escape syntax highlighting while being inside the same code block? Specifically, I want the print output to be unformatted.
```python
>>> import math
>>> x = math.sqrt(64)
>>> print(f"The square root of 64 equals {x}")
[escape highlighting here]The square root of 64 equals 8.0[escape highlighting here]
```
I've seen this question but I'm not completely sure I can achieve what I want with the answer that was provided.
Note that this is just an experience to study Scala. The example that I'll provide you may not make sense.
I want to open my ~/.subversion/servers file and if I spot a line that has the word "proxy" I want comment it (basically I just want to prepend the character "#"). Every other line must be left as is.
So, this file:
Line 1
Line 2
http-proxy-host = defaultproxy.whatever.com
Line 3
would become:
Line 1
Line 2
# http-proxy-host = defaultproxy.whatever.com
Line 3
I was able to read the file, spot the lines I want to change and print them. Here's what I've done so far:
val fileToFilter = new File(filePath)
io.Source.fromFile(fileToFilter)
.getLines
.filter( line => !line.startsWith("#"))
.filter( line => line.toLowerCase().contains("proxy") )
.map( line => "#" + line )
.foreach( line => println( line ) )
I missing two things:
How to save the changes I've done to the file (can I do it directly, or do I need to copy the changes to a temp file and then replace the "servers" file with that temp file?)
How can I apply the "map" conditionally (if I spot the word "proxy", I prepend the "#", otherwise I leave the line as is).
Is this possible? Am I even following the right approach to solve this problem?
Thank you very much.
Save to a different file and rename it back to original one.
Use if-else
This should work:
import java.io.File
import java.io.PrintWriter
import scala.io.Source
val f1 = "svn.txt" // Original File
val f2 = new File("/tmp/abc.txt") // Temporary File
val w = new PrintWriter(f2)
Source.fromFile(f1).getLines
.map { x => if(x.contains("proxy")) s"# $x" else x }
.foreach(x => w.println(x))
w.close()
f2.renameTo(f1)
There is no "replace" file method in stock scala libraries: so you would open the file (as you are showing), make the changes, and then save it back (also various ways to do this) to the same path.
AFA Updating certain lines to # if they have proxy:
.map line { case l if l.contains("proxy") => s"# $l"
case l => l
}
I would like to enter something like the following match instruction, but formatted across multiple lines. Is that possible in the Scala REPL?
myString match { case patt(a) => true case _ => false }
If you're just typing it in as-is, the REPL should detect the opening brace when you return, so that it won't try to parse and execute the code until it finds the closing brace.
You could also use paste mode by typing :pa or :paste. This will allow you to enter as much as you want in any format (two blank lines will automatically quit from it). Then when finished entering in code, you can press Ctrl + D to evaluate.
One way to enter into multi-line mode in the Scala REPL is to hit enter right after the opening curly brace "{", then hit enter after each line until the last closing curly brace has been entered "}". Hitting enter after it will exit the multi-line mode
myScript match { <enter> //enter multi-line mode
| case scriptStart(a) => true <enter>
| case _ => false <enter>
|} <enter> //exit multi-line mode
When cascading transformations, it is as simple as ending each line with a dot. For example:
val wordcount = sc.
textFile("MYFILE.txt").
flatMap( x => x.split(" ") ).
map( w => (w,1) ).
reduceByKey( (a,b) => a+b )
I have some simple code that isn't working as expected. First, the docs say that Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY).set_text() should be able to accept only one argument with the length argument option, but it doesn't work (see below). Finally, pasting a unicode ° symbol breaks setting the text when trying to retrieve it from the clipboard (and won't paste into other programs). It gives this warning:
Gdk-WARNING **: Error converting selection from UTF8_STRING
>>> from gi.repository.Gtk import Clipboard
>>> from gi.repository.Gdk import SELECTION_PRIMARY
>>> d='\u00B0'
>>> print(d)
°
>>> cb=Clipboard
Clipboard
>>> cb=Clipboard.get(SELECTION_PRIMARY)
>>> cb.set_text(d) #this should work
Traceback (most recent call last):
File "<ipython-input-6-b563adc3e800>", line 1, in <module>
cb.set_text(d)
File "/usr/lib/python3/dist-packages/gi/types.py", line 43, in function
return info.invoke(*args, **kwargs)
TypeError: set_text() takes exactly 3 arguments (2 given)
>>> cb.set_text(d, len(d))
>>> cb.wait_for_text()
(.:13153): Gdk-WARNING **: Error converting selection from UTF8_STRING
'\\Uffffffff\\Uffffffff'
From the documentation for Gtk.Clipboard
It looks like the method set_text needs a second argument. The first is the text, the second is the length of the text. Or if you don't want to provide the length, you can use -1 to let it calculate the length itself.
gtk.Clipboard.set_text
def set_text(text, len=-1)
text : a string.
len : the length of text, in bytes, or -1, to calculate the length.
I've tested it on Python 3 and it works with cb.set_text(d, -1).
Since GTK version 3.16 there is a easier way of getting the clipboard. You can get it with the get_default() method:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib, Gio
display = Gdk.Display.get_default()
clipboard = Gtk.Clipboard.get_default(display)
clipboard.set_text(string, -1)
also for me it worked without
clipboard.store()
Reference: https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Clipboard.html#Gtk.Clipboard.get_default
In Python 3.4. this is only needed for GtkEntryBuffers. In case of GtkTextBuffer set_text works without the second parameter.
example1 works as usual:
settinginfo = 'serveradres = ' + server + '\n poortnummer = ' + poort
GtkTextBuffer2.set_text(settinginfo)
example2 needs extra parameter len:
ErrorTextDate = 'choose earlier date'
GtkEntryBuffer1.set_text(ErrorTextDate, -1)
i'm trying to find pattern search with specific constraints :
input pattern to be searched will be entered from std. input
R,W and Y will be anywhere in the pattern which should be replaced with below values before searching and so every combination possible for the pattern are to be searched in a sequence.
R = C or G
W = A or T
Y = A or G
[e.g, pattern may be AWTYCR and sequence may be ATCGATGAT....]
we have to find starting positions
Also one mismatch is allowed.
i.e., one character of the pattern may or maynot match while matching with the sequence.
output:
should be written into a file in a table format:
for example,
finding ATCR (R = C or G as above mentioned) in sequence ATCGAT will result in
S.no position the_matched_pattern
1 1 ATCG
2 1 ATCC
the match and mismatch may come in any position (i.e, in same position or in different position)
The following simple code is a starting point. It can be easily generalized to read the patterns from the file, read the sequence from the Fasta file; unique the results, etc.
sub trans_pat {
my $pat=shift;
$pat=~s/R/\[CG\]/g;
$pat=~s/W/\[AT\]/g;
$pat=~s/Y/\[AG\]/g;
return $pat;
}
sub find_pat {
my ($pat,$seq) = (#_);
print "Looking for pattern $pat\n";
while ($seq=~m/$pat/g) {
print "... match at $-[0]: $&\n";
};
}
my $read_pat="ATCR";
my $seq="ATCGATATCGAT";
# Looking for a perfect match
find_pat (trans_pat($read_pat),$seq);
# Allowing for a single mismatch
foreach $i (1..length $read_pat) {
my $mis_pat = $read_pat;
substr($mis_pat,$i-1,1) = ".";
find_pat (trans_pat($mis_pat),$seq);
}
A sample implementation:
perl -e 'sub trans {$pat=shift;$pat=~s/R/\[CG\]/g;return $pat};$read_pat="ATCR";$seq="ATCGATATCGAT";$pat=trans($read_pat);print "Looking for pattern $pat\n";while ($seq=~m/$pat/g) {print "... match at $-[0]: $&\n"};foreach $i (1..length $read_pat) {$mis_pat = $read_pat;substr($mis_pat,$i-1,1)=".";$pat=trans($mis_pat);print "Looking for pattern $pat\n"; while ($seq=~m/$pat/g) {print "... match at $-[0]: $&\n"}}'
Yields
Looking for pattern ATC[CG]
... match at 0: ATCG
... match at 6: ATCG
Looking for pattern .TC[CG]
... match at 0: ATCG
... match at 6: ATCG
Looking for pattern A.C[CG]
... match at 0: ATCG
... match at 6: ATCG
Looking for pattern AT.[CG]
... match at 0: ATCG
... match at 6: ATCG
Looking for pattern ATC.
... match at 0: ATCG
... match at 6: ATCG