Get current line in Sublime Text 3 plugin - python

I'm writing a simple plugin for Sublime Text 3 and need to get the contents of the currently selected line.
I can use
selection = sublime.Region(self.view.sel()[0].begin(), self.view.sel()[0].end())
to get the currently highlighted section, but is there a way to select or return the contents of the entire current line without manually selecting all of it?
The 'duplicate' example on this page does what I want, but seems to be broken in ST3. When I run it I get the error TypeError: run() missing 1 required positional argument: 'args'.

I believe what you're after is view.substr(view.line(view.sel()[0]))
This will return the text of the current line.

Related

VSCode: show entire variable in debug console

when debugging python code in VSCode, how do i show the entire variable. As you can see from the image below, i am printing out the variable 'x'..but not all of it's values shows in the debugging console. it is cut off and followed by an elipse indicating there is more:
While I know I can use the '>' to show a list of each item in the set in this case, is there a way to print the entire variable as a string in the debug pane/console - much like we would be able to if we were using the command line for our venv?
for item in set(x):
print(item)
You should be able to click the > sign to the left of the variable result to expand it.
I made a simple replay. You can find the variable on the left side after using debug:
You can copy it like the following picture:
you can paste the copied content:

How to solve "ECitMatch() got multiple values for argument 'bdata'"?

I am new to use bioservices Python package. Now I am going to use that to retrieve PMIDs for two citations, given the specified information and this is the code I have tried:
from bioservices import EUtils
s = EUtils()
print(s.ECitMatch("pubmed",retmode="xml", bdata="proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|%0Dscience|1987|235|182|palmenberg+ac|Art2|"))
But it occurs an error:
"TypeError: ECitMatch() got multiple values for argument 'bdata'".
Could anyone help me to solve that problem?
I think the issue is that you have an unnamed argument (pubmed); if you look at the source code, you can see that the first argument should be bdata; if you provide the arguments like you do, it is, however, unclear whether bdata is "pubmed" or the named argument bdata, therefore the error you obtain.
You can reproduce it with this minimal example:
def dummy(a, b):
return a, b
dummy(10, a=3)
will return
TypeError: dummy() got multiple values for argument 'a'
If you remove "pubmed", the error disappears, however, the output is still incomplete:
from bioservices import EUtils
s = EUtils()
print(s.ECitMatch("proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|%0Dscience|1987|235|182|palmenberg+ac|Art2|"))
returns
'proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|2014248\n'
so only the first publication is taken into account. You can get the results for both by using the correct carriage return character \r:
print(s.ECitMatch(bdata="proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|\rscience|1987|235|182|palmenberg+ac|Art2|"))
will return
proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|2014248
science|1987|235|182|palmenberg+ac|Art2|3026048
I think you neither have to specify retmod nor the database (pubmed); if you look at the source code I linked above you can see:
query = "ecitmatch.cgi?db=pubmed&retmode=xml"
so seems it always uses pubmed and xml.
Two issues here: syntaxic and a bug.
The correct syntax is:
from bioservices import EUtils
s = EUtils()
query = "proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|%0Dscience|1987|235|182|palmenberg+ac|Art2|"
print(s.ECitMatch(query))
Indeed, the underlying service related to ICitMatch has only one database (pubmed) and one format (xml) hence, those 2 parameters are not available : there are hard-coded. Therefore, only one argument is required: your query.
As for the second issue, as pointed above and reported on the bioservices issues page, your query would return only one publication. This was an issue with the special character %0D (in place of a return carriage) not being interpreted corectly by the URL request. This carriage character (either \n, \r or %0d) is now taken into account in the latest version on github or from pypi website if you use version 1.7.5
Thanks to willigot for filling the issue on bioservices page and bringing it to my attention.
disclaimer: i'm the main author of bioservices

Word .docx created using python-docx + python-docx-template results in eror

I'm using python-docx and python-docx-template to generate a multi-page document. Word on both MacOS and Windows complains about an error in the .docx file produced, yet Word is able to open the file if allowed to continue and the document looks fine when opened. (On MacOS, the error dialog reads "HRESULT 0x80004005 Location: Part: /word/document.xml, line 0, column 0").
The .docx template is a pretty simple one-page document. The loop to construct the compound document is based on an answer to another question and is this simple Python code:
overall_doc = Document()
num_pages = len(records_list)
for index, record in enumerate(records_list):
page = DocxTemplate(template)
values = vars(records_list[index])
page.render(values)
if index < (num_pages - 1):
page.add_page_break()
for element in page.element.body:
overall_doc.element.body.append(element)
overall_doc.save('outputfile.docx')
The values substituted into the template are UTF-8 strings with no special characters (and in particular, no ampersands or greater than/less than characters). I've verified the problem is not due to the string values being substituted into the template.
If you break the loop after the first page is created, no error results. If the loop is allowed to create even just 2 pages, the error in Word occurs. If I remove the page break code altogether, the error still occurs. If I add an extra page break at the end, the error still occurs.
I've tried to find a docx validation tool. The only thing I have been able to run is docx4j's OpenMainDocumentAndTraverse function, which as far as I can tell, should report errors. But docx4j does not report any error with the output document.
What could cause this error? If my mistake is not obvious, how can I diagnose the reason that Word is complaining?

In SikuliX, type(variable) always types the variable then presses enter automatically. How do I avoid pressing enter?

Sorry if this is really basic, I cannot find a workaround. I have a variable called doc that stores the number 510 that was copied from an excel cell.
I need to type it in a field, but I need to continue typing in another field on the same page afterwards.
My code has:
type(doc)
The log shows:
[log] TYPE "510#ENTER."
The full code looks like this:
type(doc)
wait(1)
type(Key.DOWN)
type(Key.BACKSPACE+Key.BACKSPACE+Key.BACKSPACE+Key.BACKSPACE)
wait(1)
type(code)
However, I can't get to the type(code) because it switches page before I get there...
Using paste() maybe solved your issue here but this is not the right way to do that as Sikuli does not automatically presses any buttons.
Your problem is probably with the doc variable itself. In your case, you probably just copied the new line character with your variable from excel and that's why Sikuli is hitting Enter. To avoid that, try stripping the new line from your variable prior to typing it, like this:
doc.rstrip()
Then do your usual type(doc) and it should be fine.
Another thing that works is: doc.strip()
It turns out sikuli writes /n after strings, so strip removes that /n.

Highlight specific line of output

I'm building a piece of educational software and I have pseudocode on the output where I would like to highlight a specific line of code depending on which piece of code is running.
First round()
.....
--> highlight this line and the next after it moves
Output: First round has just started
The furthest I got was doing some bash highlighting but that required me to print out the line twice. Once in black and second in a different colour. Any suggestions on how to highlight a specific line? Any help is appreciated.
EDIT: I'm using Pyqt as my GUI toolkit so my output will be displayed in a textbox
You have to get the QTextBlock object that correspond to the line (*) you want to highlight or unhighlight and use a QTextCursor to change the format of that line:
def setLineFormat(self, lineNumber, format):
cursor = QTextCursor(self.textEdit.document().findBlockByNumber(lineNumber))
cursor.setBlockFormat(format)
# with
format = QTextBlockFormat()
format.setBackground(Qt.yellow)
# or
format.clearBackground()
If you are using QSyntaxHighlighter, you could also store the state of the line in the QTextBlock with QTextBlock.setUserState() or setUserData, handle that state in the QSyntaxHighlighter.highlightBlock() method as part of the syntax highlighting, and force the previous and the current lines to be repainted with QSyntaxHighlighter.rehighlightBlock().
*: lines==blocks unless you use a custom document layout

Categories