How can I disable markdown on a string in a embed? - python

I am trying to make a bot that shows in an embed some files from a directory. The code works but in case of files with symbols in it, it modifies the text:
EX:
hello_world_-_1
BECOMES:
How can I solve this?
I don't even know what markdown this would be...

You can't explicitly disable markdown, as the rendering happens client-side. The best you can do is wrap in triple backticks.
```hello_word_-_```
This will work unless your string contains triple backticks. And if you have a filename which contains triple backticks, then you have a different issue.
You could (and probably should) still check, of course. Just a simple "if filename contains ```", and you can report an error if it does.

Related

string formatting appears differently on different environment

I'm trying to send text from python to slack by using slackbotAPI.
I processed text like this: (looks neat)
I formatted string by this code line:
But after sending this text by slackbot API, text appeared on slack like this:
I guess the width of each character(include space)is different on slack, but not on python prompt.
How can I see the text neatly on slack like on python?
Thank you.
I think the best way to achieve what you're looking for isn't with text characters like tabs etc, but to use fields inside a section block.
That will make sure that regardless of interface, they're arranged correctly.
Here's an example

How to set up filter for isort as external tool in PyCharm

I'm trying to set up isort as external tool in PyCharm. I'm unable to set up filter so that file paths are links.
Output from isort is:
ERROR: C:\dev\path\to\a\project\file.py Imports are incorrectly sorted.
According to docs putting $FILE_PATH$ should be sufficient yet it does not work for me. I've tried several regex styles without any success.
tl;dr use $FILE_PATH$(?<=\.py)( |$) as filter.
So (^|[\W])(?<file>(?:\p{Alpha}\:|/)[0-9 a-z_A-Z\-\\./]+)(?<=\.py) is regexp used for $FILE_PATH. Source: https://github.com/JetBrains/intellij-community/blob/d29c4fa1a73e03b852353186d792540150336b9f/platform/lang-api/src/com/intellij/execution/filters/RegexpFilter.java#L39
See how it allows spaces in there?
Meaning it will grab C:\dev\path\to\a\project\file.py Imports are incorrectly sorted. and as it does not point to a real file it won't be converted to a link.
So you can either modify isort output format to something with clear filepath boundaries, or use something more fancy in regexp like positive look behind, which would make your filter look like this:
$FILE_PATH$(?<=\.py)( |$)
For testing java regexps you can use https://www.freeformatter.com/java-regex-tester.html if the provided filter does not meet your particular needs.

Search text for valid Python code

I have chunks of text that may or may not contain Python code. I need a way to search the text for code and if it is there, do something. I could easily search for specific strings that match a regex, but I need something general.
One thought I had would be to run the text through ast, but that would require parsing out all possible substrings and submitting each of them to ast.
To be clear, the text comes from a Q&A forum for Python. Users frequently post code in their questions, but the code is all smushed into one long, incoherent line when it should be formatted to be displayed properly. I need to check if there is code included in the text and if there is and it isn't formatted properly, complain to the user. Checking formatting is something I can handle, I just need to check the text for any Python code.
Any help would be appreciated.

How to correctly indent Django template code automatically?

I am taking care of an old Django project and discovered that most of the templates are not indented correctly (random spaces in each line, sometimes tabs instead of spaces, etc). I feel comfortable with two-space indentation.
I never had this kind of problem and would like a solution (a script, library, doesn't matter) which would take care of this. Note that I do not want something that formats the HTML before sending it to the client but I want to format the local files that are used for templating.
I tried to use BeautifulSoup and its prettify function - however, it does not let you change the indentation level (although you can always modify it) and the indentation made for some template tags ({% example %}) was incorrect.

Formatted output in OpenOffice/Microsoft Word with Python

I am working on a project (in Python) that needs formatted, editable output. Since the end-user isn't going to be technically proficient, the output needs to be in a word processor editable format. The formatting is complex (bullet points, paragraphs, bold face, etc).
Is there a way to generate such a report using Python? I feel like there should be a way to do this using Microsoft Word/OpenOffice templates and Python, but I can't find anything advanced enough to get good formatting. Any suggestions?
A little known, and slightly evil fact: If you create an HTML file, and stick a .doc extension on it, Word will open it as a Word document, and most users will be none the wiser.
Except maybe a very technical person will say, my this is a small Word file! :)
Use the Python Docx module for this - 100% Python, tables, images, document properties, headings, paragraphs, and more.
" The formatting is complex(bullet points, paragraphs, bold face, etc), "
Use RST.
It's trivial to produce, since it's plain text.
It's trivial to edit, since it's plain text with a few extra characters to provide structural information.
It formats nicely using a bunch of tools.
I know there is an odtwriter for docutils. You could generate your output as reStructuredText and feed it to odtwriter or look into what odtwriter is using on the backend to generate the ODT and use that.
(I'd probably go with generating rst output and then hacking odtwriter to output the stuff I want (and contribute the fixes back to the project), because that's probably a whole lot easier that trying to render your stuff to ODT directly.)
I've used xlwt to create Excel documents using python, but I haven't needed to write word files yet. I've found this package, OOoPy, but I haven't used it.
Also you might want to try outputting html files and having the users open them in Word.
You can use QTextDocument, QTextCursor and QTextDocumentWriter in PyQt4. A simple example to show how to write to an odt file:
>>>from pyqt4 import QtGui
# Create a document object
>>>doc = QtGui.QTextDocument()
# Create a cursor pointing to the beginning of the document
>>>cursor = QtGui.QTextCursor(doc)
# Insert some text
>>>cursor.insertText('Hello world')
# Create a writer to save the document
>>>writer = QtGui.QTextDocumentWriter()
>>>writer.supportedDocumentFormats()
[PyQt4.QtCore.QByteArray(b'HTML'), PyQt4.QtCore.QByteArray(b'ODF'), PyQt4.QtCore.QByteArray(b'plaintext')]
>>>odf_format = writer.supportedDocumentFormats()[1]
>>>writer.setFormat(odf_format)
>>>writer.setFileName('hello_world.odt')
>>>writer.write(doc) # Return True if successful
True
QTextCursor also can insert tables, frames, blocks, images. More information at:
http://qt-project.org/doc/qt-4.8/qtextcursor.html
As a bonus, you also can print to a pdf file by using QPrinter.
I think OpenOffice has some Python bindings - you should be able to write OO macros in Python.
But I would use HTML instead - Word and OO.org are rather good at editing it and you can write it from Python easily (although Word saves a lot of mess which could complicate parsing it by your Python app).

Categories

Resources