Python markdown footnote backlink text change - python

I am facing a very simple problem on changing the backref text in python footnote. I read the doc of footnote at here . The site clearly says BACKLINK_TEXT could be overwritten to allow other character.
So I tried following code:
import markdown.extensions.footnotes
import markdown
markdown.markdown(some_text,extensions=['footnotes'], BACKLINK_TEXT ="Δ")
The above code doesn't produce the Backref text specified as ← but gave the default value as ↩ .
Is above code the correct way of initializing the config for footnote?

After reading documentation about extensions I think you need
extensions=['footnotes(BACKLINK_TEXT=&#916)']
because BACKLINK_TEXT is option for footnotes extension.
See: Markdown#extensions

I figured out the problem, there is error in footnotes.py code of python-markdown. They have used
for key,val in configs:
instead of:
for key,val in configs.iteritems().
This was the problem. Hope they fix this in another version.

Related

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

Finding documentation on names returned by dir()

If you run the following code:
from flask import Flask
import unittest
dir(Flask(__name__).test_client())
The following is output to terminal:
There are a number of names returned that I cannot find documentation on (all of the names that are not surrounded by double underscores).
I have found indirect reference to post here (if you search for 'self.app.post' you'll see it referenced). Note: this link describes using .post with the following keywords: data and follow_redirects. It does not mention that you can also use the keywords content_type and headers. Perhaps the only reason that these keyword options are not intuitively obvious to me is because I'm new to this...
Does anyone know where documentation on these names resides? (I can't find it in flask/python/unittest documentation anywhere - perhaps I am looking in the wrong place?)
edit: with the help of the answers, I found this documentation.
For any Python Module, Class, Method (all of these in Python are object indeed), you can view the doc by:
>>> a_module.__doc__
>>> a_class.__doc__
>>> a_method.__doc__
To see more detailed documents, you can use help command:
>>> help(a_method)
You can always check the docstring of the method - comments that developers left when they wrote the code. You can check any object or method you need. For example:
Flask.__doc__
unittest.__doc__
dir.__doc__
dir.__doc__.__doc__
You can also query
Flask(__name__).test_client().post.__doc__
Flask(__name__).test_client().preserve_context.__doc__
But you'll notice that not all methods would be documented. For example:
Flask(__name__).test_client().open.__doc__
For more about this you can also see http://legacy.python.org/dev/peps/pep-0257/
Using help() gives you the same information but formatted, e.g.:
help(Flask)
help(unittest)
help(dir)
help(dir.__doc__)

Using isbntools with web2py

I'm using web2py to create a page where I search for books based on title/author/keyword/etc. and ISBN, and I can't seem to figure out how to use isbntools in the webapp. I'm sure it's something basic that I'm missing out on, but this is the first webapp that I've ever created, and it's for a class project. This is the related portion of my controller:
from isbntools import *
def index():
form=SQLFORM.factory(
Field('title',label='Try entering a title:'),
Field('author',label='Or an author:'),
Field('ISBN',label='Even better if you have the ISBN'),
Field('fromDate',label='When is the earliest the book might have come out?'),
Field('toDate',label='...and the latest?'))
if form.process().accepted:
titledata = isbn_goom form.vars.title bibtex
authordata = isbn_goom form.vars.author bibtex
isbndata = isbn_meta merge form.vars.ISBN bibtex
print(titledata)
print(authordata)
print(isbndata)
return dict(form=form)
This is a portion of the ticket information I'm getting back:
Error ticket for "Bibbly"
Ticket ID
96.255.27.81.2014-05-01.21-50-27.f66e0b53-b5bd-4621-8dbd-b6f30e8a6af1
invalid syntax (default.py, line 21)
Version
web2py™ Version 2.8.2-stable+timestamp.2013.11.28.13.54.07
Python Python 2.7.5+: /usr/local/bin/uwsgi (prefix: /usr)
Traceback
line 21
titledata = isbn_goom "form.vars.title" bibtex
^
SyntaxError: invalid syntax
isbm_goom is a command line script. Is that what you want?! (you cannot use it in your code like that!)
I suggests you use the most recent version of isbntools and adapt this snipet
from isbntools.contrib.modules.goom import goom
from isbntools.dev.fmt import fmtbib
...
titledata = goom.query(form.vars.title)
for r in titledata:
print((fmtbib('bibtex', r)))
Since python is complaining about a syntax error, it means it's about the literal code you've written. The Python interpreter can't grasp what you mean because you've specified something "impossible" in the language. In this case it's about the white-space after isbn_goom and form.vars.title. Since i don't know and don't see any declaration concerning isbn_goom i assume it's from the isbntools library. To try the library it may be best to learn about it in a separate console session on your own machine. See the pypi page for some examples.
On resolving syntax errors: You can try editing the code in any decent code editor and it will give you hints on these kinds of errors. The default python editor Idle that comes with any default installation would be great.

How can I get a list of all the strings that Babel knows about?

I have an application in which the main strings are in English and then various translations are made in various .po/.mo files, as usual (using Flask and Flask-Babel). Is it possible to get a list of all the English strings somewhere within my Python code? Specifically, I'd like to have an admin interface on the website which lets someone log in and choose an arbitrary phrase to be used in a certain place without having to poke at actual Python code or .po/.mo files. This phrase might change over time but needs to be translated, so it needs to be something Babel knows about.
I do have access to the actual .pot file, so I could just parse that, but I was hoping for a cleaner method if possible.
You can use polib for this.
This section of the documentation shows examples of how to iterate over the contents of a .po file. Here is one taken from that page:
import polib
po = polib.pofile('path/to/catalog.po')
for entry in po:
print entry.msgid, entry.msgstr
If you alredy use babel you can get all items from po file:
from babel.messages.pofile import read_po
catalog = read_po(open(full_file_name))
for message in catalog:
print message.id, message.string
See http://babel.edgewall.org/browser/trunk/babel/messages/pofile.py.
You alredy can try get items from mo file:
from babel.messages.mofile import read_mo
catalog = read_po(open(full_file_name))
for message in catalog:
print message.id, message.string
But when I try use it last time it's not was availible. See http://babel.edgewall.org/browser/trunk/babel/messages/mofile.py.
You can use polib as #Miguel wrote.

Trouble using Vim's syn-include and syn-region to embed syntax highlighting

I am trying to get Vim to syntax highlight any file that ends with extension .Rtex in the following way:
All top level text is highlighted as TeX
Exception: any text enclosed in \begin{python}...\end{python} is highlighted as Python
I am able to achieve each of these criteria individually, but unable to achieve both simultaneously. I think that somehow the TeX highlighting overrides my Python-highlighted regions, or prevents them from taking effect, and I am stuck trying to figure out how.
First step: edit .vimrc to give files with extension .Rtex the filetype rtex:
au BufRead *.Rtex setf rtex
Second step: create ~/.vim/syntax/rtex.vim. It is the contents of this file that will determine how to highlight .Rtex files.
Third step: enable general top-level TeX highlighting, by making rtex.vim look like this:
runtime! syntax/tex.vim
If I now open a .Rtex file, the entire file is highlighted as TeX, including any text within \begin{python}...\end{python}, as expected.
Fourth step: follow the instructions in Vim's :help syn-include to include python highlighting and apply it to all regions delimited by \begin{python} and \end{python}. My rtex.vim file now looks like this:
runtime! syntax/tex.vim
unlet! b:current_syntax
syntax include #Python syntax/python.vim
syntax region pythonCode start="\\begin{python}" end="\\end{python}" contains=#Python
The unlet! b:current_syntax command is meant to force the python.vim syntax file to execute even though an existing syntax (TeX) is already active.
Problem: If I now open a .Rtex file, the entire file is still highlighted only as TeX. The \begin{python}...\end{python} region seems to have no effect.
Experiment: If I remove or comment out the runtime! command, I do get python highlighting, within the \begin{python}...\end{python} regions, exactly as desired, but of course no TeX highlighting in the remainder of the document. I therefore conclude that the TeX highlighting is somehow responsible for preventing the python regions from taking effect.
Can a Master of Vim offer me any suggestions? I am currently stumped. I have looked at several pages and stackoverflow questions that seem relevant, but none of them have so far led to a solution:
http://vim.wikia.com/wiki/Different_syntax_highlighting_within_regions_of_a_file
Embedded syntax highligting in Vim
VIM syntax highlighting of html nested in yaml
After some more study of the manual, and much more trial and error, I have finally answered my own question (a simultaneously embarrassing and sublime accomplishment), which I now preserve here for posterity.
Basically, I think the problem is that the python highlighting wouldn't take effect because the pythonCode region was itself contained in a region or highlight group defined by tex.vim, so it wasn't top-level. The solution is to also include (rather than just runtime) tex.vim, giving it a name like #TeX, and then add containedin=#TeX to my python region definition. So syntax/rtex.vim now looks like this:
let b:current_syntax = ''
unlet b:current_syntax
runtime! syntax/tex.vim
let b:current_syntax = ''
unlet b:current_syntax
syntax include #TeX syntax/tex.vim
let b:current_syntax = ''
unlet b:current_syntax
syntax include #Python syntax/python.vim
syntax region pythonCode matchgroup=Snip start="\\begin{python}" end="\\end{python}" containedin=#TeX contains=#Python
hi link Snip SpecialComment
let b:current_syntax = 'rtex'
And this works! I'm not sure if all of those unlet b:current_syntax commands are strictly necessary. I also gave the python region delimiters a matchgroup (Snip) so that they can be highlighted themselves (with the SpecialComment color), rather than left just plain, which is apparently what happens by default, since they are no longer considered part of the TeX.
Now it is a trivial thing to add more highlight regions for different languages (e.g., \begin{Scode}...\end{Scode}), which is great if you're getting into literate programming -- the original motivation for my question.
Edit: here is a screenshot showing how it works with Python and R code embedded in a TeX document:
I don't know if it helps, but a hack I use with my Rnw files that use both tex and rnoweb features is as follows:
au BufEnter *.Rnw set filetype=tex | set filetype=rnoweb
Would an adapted version work in your case?

Categories