Cannot post Python code to the website rosalind.info - python

I am trying to post a sample solution, written in Python, to rosalind.info.
I tried following their instructions:
To highlight code, add a shebang styled first line :::lexername to your code. Replace lexername with the lexer keyword for the language that you want to be highlighted as shown in the list of Pygments lexers."
However, I can't get it to work.
I have tried setting the first line to:
:::python
:::PythonLexer
#!:::python
#!:::PythonLexer
but it just appears as ordinary text.

It seems your first attempt was correct, but you did not click the 'Submit' button to view your code with the lexer applied.
In order to see the code with syntax highlighting, you must first submit your response. The WYSIWYG editor provided below the markdown box does not perform syntax highlighting. In order to see your code with proper highlighting you would type something like the following into the box.
:::python
print "Hello World"
which will look something like
print "Hello World"
once you click the 'Submit' button and view your response. You will have the option to edit your submission if you want to change things later.
Joshua's answer has linked you to the place where you can determine which lexer name you want to use. Simply choose type the corresponding 'short name' for the highlighting you would like to apply.

You could try
#!:::python3
Source: http://pygments.org/docs/lexers/

Two ways that worked for me were:
Indent the whole code block with 4 spaces. Make the first indented line either
#! python or
:::python.
The first version also adds line numbers to the formatted code.
Instead, you can also surround the code block with lines containing only triple backticks ```, in which case you don't need to indent the code with spaces. As in the previous case, make the first line after the opening backtick line either
#! python or
:::python.
The first version adds line numbers, as mentioned above.
As mentioned before by others, you need to "Submit" before you see the fully formatted result.

Related

vscode python avoid indent of multiline string

So here is an example of a multiline string in vscode/python:
Cursor is after the p , and then you press enter, and end up like this:
i.e. the string ends up indented, which seems what you almost never want - why have an arbitratly amount of whitespace on the next line of this string ?
Is there any way change this in vscode, i.e. for multiline strings, it should end up with this:
I think this problem is related to different coding styles of different people.
For example,
def example(x):
if x:
a = '''
This is help
'''
def example(x):
if x:
a = '''This is help
'''
The automatic indenting of vscode line breaks is based on code blocks. If you want Vscode can identify multiline string, I think it would be better to submit future request in github. I've submitted this issue for you.
I am not 100% sure if what OP meant is just to refer to the indentation in the editor (namely, VSC) or if, by this:
i.e. the string ends up indented, which seems what you almost never want - why have an arbitrary amount of white space on the next line of this string?
...they also meant to refer to the actual output of the multi-line string,
(or also, just in case anybody else finds this post looking for a way to avoid this affecting the actual output of the multi-line string), I'd like to add as a complementary answer (cannot comment yet) that this was already beautifully answered here.
If that's the case and you're reading this for that reason, in short, all you want is to import the standard lib 'inspect' and post-process your string with it, using the cleandoc method.
Without breaking the indentation in your IDE, this method makes sure to give you the string output you actually expected:
All leading whitespace is removed from the first line. Any leading whitespace that can be uniformly removed from the second line onwards is removed. Empty lines at the beginning and end are subsequently removed. Also, all tabs are expanded to spaces.
(From the docs link above)
Hope that helps anyone.

Syntax highlighted code block in markdown indented list paragraph

Markdown lets me indent a paragraph twice under a list item to make it part of that list.
1. List item
Paragraph will be rendered inside of list item.
2. Other list item
Other paragraph.
This would be code.
3. But what about syntax highlighted code?
```bash
echo "This won't work"
```
I can also indent twice as much to make code blocks.
BUT how do I use syntax highlighted code blocks such as these inside a list paragraph? Is it at all possible?
In my situation I'm using mkdocs, a python static HTML generator using markdown.
Background on why I'm asking is because such lists actually make for a really nice design when you're writing step by step guides.
What about this:
3. But what about syntax highlighted code?
~~~~ bash
echo "This won't work"
~~~~
It renders like this in Firefox (using Stackedit):

How can I get the minted package to render code blocks with copyable indentation?

To the naked eye, minted (Konrad Rudolph's LaTeX package for code highlighting using the Pygments library) faithfully renders code blocks that are passed to it, displaying them with whatever indentation was contained in the source code.
If, however, you attempt to copy and paste code from one of those blocks, you'll notice that their visible indentation is achieved using non-copyable
spaces, such that the pasted code loses each line's leading spaces. This is
particularly problematic with Python code blocks, because in Python
indentation has actual meaning as a part of the code.
So, here's my question: Is there some way to get minted to render code blocks that, when
copy-and-pasted, keep the indentation of the source code they display?
For examples of what I mean, see any of the several indented code blocks in
the minted manual (found
here), or compile
the following minimal-ish reproducible example:
\documentclass{article}
\usepackage{minted}
\newminted[python]{python}{frame=single}
\begin{document}
\begin{python}
def example1():
if verbose:
print 'Running example1'
verbose = True
example1()
\end{python}
\end{document}
This works in Acrobat Reader, at least on my system, but not in SumatraPDF and perhaps some other programs. There may be other, better solutions.
\usepackage{color}
\usepackage{minted}
\newminted[python]{python}{frame=single}
\fvset{showspaces}
\renewcommand\FancyVerbSpace{\textcolor{white}{\char32}}
This sets fancyvrb, which Pygments uses for formatting output, to use visible space characters (␣), and then makes the characters "invisible" by making them white. Ultimately, this does end up being a TeX question, since Pygments is using the fancyvrb package for its output, and the trick is to get fancyvrb to create (leading) spaces that can be copied.

How to find and replace all tabs with spaces in idle

I have an invisible expected an indented block error, which is likely caused by me using tabs instead of spaces.
When I open the "find/replace" window and try to enter TAB in the find field IDLE unsurprisingly just skips to the replace field.
How do I do this?
Final update:
Thank you for all answers, much appreciated. My problem was that python wants the function comments to be indented too, that is
def imdheladumb():
"""
I'm dum as hel
"""
does not work, it needs to be
def imdheladumb():
"""
I'm dum as hel
"""
IDLE doesn't let you search to literal tab characters. You can paste one into the search box (as suggested by will), but it will never match anything.
However, it does let you do regular expression searches, and the regular expression \t will match a literal tab. So, turn on the Regular expression checkbox, and put '\t in the Find: box, and 4 or 8 spaces (as appropriate) in the Replace: box.
But, as will suggested, it's better to use IDLE's features instead of trying to do things manually: Select the block of code with tabbed (or inconsistent) indentation, go to the Format menu, and select Untabify Region. (Or just hit control-6.) If the tabs were inserted with an editor that uses 4-space tabs, you may need to first use New Indent Width and change it to 4, then Untabify Region.
IDLE doesn't have any code to guess what your tab size was when you wrote the inconsistent code. The only editor I know of that does is emacs. If you just open the file in emacs, it will try to guess your settings, and then you can select the whole buffer and untabify-region. If it guessed right, you're golden; if it guessed wrong, don't save the buffer, because now it'll be even harder to fix. (If you're one of the 3 people in the world who knows how to read emacs lisp but doesn't like emacs, you could look through the python-mode.el source and see how it does its magic.)
A generic way to do this is to just copy a tab character from the document (or just do one in any random text editor and copy it) and then put that into the field.
You could try putting \t in there, but that only works in some editors.
Most IDEs have a function to automatically replace tabs with a predefined number of spaces. I suggest turning that on...
EDIT: doing a standard find and replace could be dangerous if you're using tabs somewhere else for any reason.
If you look here, there's an option called "tabify region". That might be more interesting for you.
There should be an app for that. ;)
If you enjoy overkill, what about running your code through a regular expression like:
re.sub('\t', '\s\s\s\s', yourCode)
For those people having the problem, the new version of VSCode can solve this easily.
Click on Tab Sizes at the bottom of the page.
Select Convert Indentation to Spaces from the "Select Action" menu that pops up.

vim highlighting everything in red

I added a print line to a python script while the script was executing, and now all the text is highlighted in red when I open the file. Opening and closing the file doesn't get rid of it. Opening a second python file momentarily fixed the problem, but then closing file and reopening brought the problem back. Now it wont go away at all. Any body know what could cause this?
This happens sometimes in vim when it goes to highlight syntax on multi-line comments. Occasionally everything after the multi-line comment also becomes colored the same as the comment.
I am unsure whether this is a legit bug in vim, or in the actual syntax settings for Python (e.g. python.vim), but I definitely experience this from time-to-time.
For a quick fix, you might try typing:
:hi Error NONE
And then hit Enter.
Old thread, but hope this helps.
By mistake I did a "/." on my vim screen, which highlighted all lines in red. If I open any other file, the red highlighting stays.
Try searching for some other keyword, let's say "/word" - doesn't matter word exists or not. It restores the color.
You probably have an unterminated multiline string. They start and end with either three single or three double quotes.
''' <== this is the start of a multiline string
this is still in the string
this is the last line '''
As per this article http://vim.wikia.com/wiki/Fix_syntax_highlighting, I mapped F12 to resync the syntax highlighting from the start of the file. It has worked better in some instances where <ctrl-l> didn't.
noremap <F12> <Esc>:syntax sync fromstart<CR>
inoremap <F12> <C-o>:syntax sync fromstart<CR>

Categories

Resources