Change 88 characters limit for Black plugin in PyCharm - formatting

I am using Black inside PyCharm to format my Python code.
I am using the Black-Pycharm plugin, unfortunately, selecting code and applying Black on it (Code > Reformat Code (BLACK)) cuts all my lines at 88 characters (the default limit for Black).
I want to change this limit to cut the lines at 80 characters. I tried two different ways:
Changing the Black exe path in the "Black plugin settings" from ~/.local/bin/black to ~/.local/bin/black -l80, but applying Black with PyCharm outputs this error: BlackPycharm: Cannot run program "/home/BCT/.local/bin/black -l80": error=2, File or folder not found
Using Black as an 'External Tool' in Pycharm (as described here), and specifying the line length in the arguments text box. This successfully applies Black on my file with the desired character limit, but:
It automatically saves/replaces my file with the new formatted file, I can't undo the change.
I can't apply Black on a portion of code only.
Do you know ways to use Black with:
The ability to specify a desired line length
The ability to reformat only a portion of code
at the same time ?
EDIT: Apparently PyCharm cannot use Black for only a portion of code...

I also had the same problem with adjusting the linelength for the black 'external tool'.
1- follow this link to install and set black as an external tool: https://black.readthedocs.io/en/stable/editor_integration.html#pycharm-intellij-idea
2- within the "PyCharm/IntelliJ IDEA" section, in "Arguments":
replace "$FilePath$" with "$FilePath$" -l 120
Note: the '-l 120' should be outside the quotes, and replace 120 by whatever line length you need.
Cheers!
Maher.

I use Black as an external tool within PyCharm, but I am able to specify line length by adding a pyproject.toml (The PEP/ more info) file to my root project directory. I don't have to pass anything as an argument. Maybe it's able to solve your problems. It looks the following way:
# NOTE: you have to use single-quoted strings in TOML for regular expressions.
# It's the equivalent of r-strings in Python. Multiline strings are treated as
# verbose regular expressions by Black. Use [ ] to denote a significant space
# character.
[tool.black]
line-length = 79
target-version = ['py37', 'py38']
include = '\.pyi?$'
exclude = '''
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
# The following are specific to Black, you probably don't want those.
| blib2to3
| tests/data
| profiling
)/
'''

#Nadros solution is correct but it should be noted that -l 120 has to be added to the FileWatcher that was created as well if you are using it to format files on save.

Related

Can man pass an option to the roff formatter?

SYNOPSIS
From man(1):
-l
Format and display local manual files instead of
searching through the system's manual collection.
-t
Use groff -mandoc to format the manual page to stdout.
From groff_tmac(5):
papersize
This macro file is already loaded at start-up by troff so it
isn't necessary to call it explicitly. It provides an interface
to set the paper size on the command line with the option
-dpaper=size. Possible values for size are the same as
the predefined papersize values in the DESC file (only
lowercase; see groff_font(5) for more) except a7–d7.
An appended l (ell) character denotes landscape orientation.
Examples: a4, c3l, letterl.
Most output drivers need additional command-line switches -p
and -l to override the default paper length and orientation
as set in the driver-specific DESC file. For example, use the
following for PS output on A4 paper in landscape orientation:
sh# groff -Tps -dpaper=a4l -P-pa4 -P-l -ms foo.ms > foo.ps
THE PROBLEM
I would like to use these to format local and system man pages to print out, but want to switch the paper size from letter to A4. Unfortunately I couldn't find anything in man(1) about passing options to the underlying roff formatter.
Right now I can use
zcat `man -w man` | groff -tman -dpaper=a4 -P-pa4
to format man(1) on stdout, but that's kind of long and I'd rather have man build the pipeline for me if I can. In addition the above pipeline might need changing for more complicated man pages, and while I could use grog, even it doesn't detect things like accented characters (for groff's -k option), while man does (perhaps using locale settings).
The man command is typically intended only for searching for and displaying manual pages on a TTY device, not for producing typeset and paper printed output.
Depending on the host system, and/or the programs of interest, the a fully typeset printable form of a manual page can sometimes be generated when a program (or the whole system) is compiled. This is more common for system documents and less common for manual pages though.
Note that depending on which manual pages you are trying to print there may be additional steps required. Traditionally the following pipeline would be used to cover all the bases:
grap $MANFILE | pic | tbl | eqn /usr/pub/eqnchar | troff -tman -Tps | lpr -Pps
Your best solution for simplifying your command line would probably be to write a little tiny script which encapsulates what you're doing. Note that man -w might find several different filenames, so you would probably want to print each separately (or maybe only print the first one).

Train Tesseract to label icons

I'm trying to create training data for Tesseract 4.0 to identify icons (like, comment, share, save) in screenshots. This is a sample screenshot:
I would like to fine tune the Tesseract to achieve output as below:
Like 147
Comment 29
Saved 5
Actions
58
Actions
Profile Visits 24
Follows 2
I have followed step-by-step as stated in https://pretius.com/how-to-prepare-training-files-for-tesseract-ocr-and-improve-characters-recognition/
I modified the box file as below:
- Heart : Like
- Speech bubble: Comment
- Bookmark: Saved
- Arrow: Share
But, the final training data failed to read the icon as I wanted. Example of error I've got is 'Like is not in unicharset'. Do I have to do something different when creating the unicharset for icons?
I've figured it out. The box editor expects single letter/number instead of full words. I have used Unicode character to interpret my icons. The steps are as below:
Crop all target icons that you wish for Tesseract to detect and save it in one file named as (in my case) own.std.exp0.png
Create box file using the command 'tesseract own.std.exp0.png own.std.exp0 makebox'
Open jTessBoxEditor and input unicode at the char column. The list of supported unicode can be found under program Character Map (https://sites.psu.edu/symbolcodes/windows/charmap/). Example: For heart symbol I used U+2665. Note that some unicode are not supported. It shows as blank square. So, keep trying till you find one that works. My final edited box file looks like this.
Create the final training file which will be own.trainneddata (can be done as shown here https://medium.com/apegroup-texts/training-tesseract-for-labels-receipts-and-such-690f452e8f79 or train using jTessBoxEditor).
Copy the own.traineddata to the directory Tesseract/tessdata and run Tesseract using lang='own+eng'. I used pytesseract and the output is as below:

Rename ttf/woff/woff2 file to PostScript Font Name with Script

I am a typographer working with many fonts that have incorrect or incomplete filenames. I am on a Mac and have been using Hazel, AppleScript, and Automator workflows, attempting to automate renaming these files*. I require a script to replace the existing filename of ttf, woff, or woff2 files in Finder with the font's postscriptName. I know of tools (fc-scan/fontconfig, TTX, etc) which can retrieve the PostScript name-values I require, but lack the programming knowhow to code a script for my purposes. I've only managed to setup a watched directory that can run a script when any files matching certain parameters are added.
*To clarify, I am talking about changing the filename only, not the actual names stored within the font. Also I am open to a script of any compatible language or workflow of scripts if possible, e.g. this post references embedding AppleScript within Shell scripts via osascript.
StackExchange Posts I've Consulted:
How to get Fontname from OTF or TTF File?
How to get PostScript name of TTF font in OS X?
How to Change Name of Font?
Automate Renaming Files in macOS
Others:
https://github.com/dtinth/JXA-Cookbook/wiki/Using-JavaScript-for-Automation
https://github.com/fonttools/fonttools
https://github.com/devongovett/fontkit
https://www.npmjs.com/package/rename-js
https://opentype.js.org/font-inspector.html
http://www.fontgeek.net/blog/?p=343
https://www.lantean.co/osx-renaming-fonts-for-free
Edit: Added the following by request.
1) Screenshot of a somewhat typical webfont, illustrating how the form fields for font family and style names are often incomplete, blank, or contain illegal characters.
2) The woff file depicted (also, as base64).
Thank you all in advance!
Since you mentioned Automator in your question, I thought I'd try and solve this while using that to rename the file, along with standard Mac bash to get the font name. Hopefully, it beats learning a whole programming language.
I don't know what your workflow is so I'll leave any deviations to you but here is a method to select a font file and from Services, rename the file to the font's postscript name… based on Apple's metadata, specifically "com_apple_ats_name_postscript". This is one of the pieces of data retrieved using 'mdls' from the Terminal on the font file. To focus on the postscript name, grep the output for name_postscript. For simplicity here, I'll exclude the path to the selected file.
Font Name Aquisition
So… running this command…
mdls GenBkBasBI.ttf | grep -A1 name_postscript
… generates this output, which contains FontBook's Postscript name. The 'A1' in grep returns the found line and the first line after, which is the one containing the actual font name.
com_apple_ats_name_postscript = (
"GentiumBookBasic-BoldItalic"
Clean this up with some more bash (tr, tail)…
tr -d \ | tail -n 1 | tr -d \"
In order, these strip spaces, all lines excepting the last, and quotation marks. So for the first 'tr' instance, there is an extra space after the backslash.
In a single line, it looks like this…
mdls GenBkBasBI.ttf | grep -A1 name_postscript | tr -d \ | tail -n 1 | tr -d \"
…and produces this…
GentiumBookBasic-BoldItalic
Now, here is the workflow that includes the above bash command. I got the idea for variable usage from the answer to this question…
Apple Automator “New PDF from Images” maintaining same filename
Automator Workflow
Automator Workflow screenshot
At the top; Service receives selected 'files or folders' in 'Finder'.
Get Selected Finder Items
This (or Get Specified…) is there to allow testing. It is obviated by using this as a Service.
Set Value of Variable (File)
This is to remember which file you want to rename
Run Shell Script
This is where we use the bash stuff. The $f is the selected/specified file. I'm running 'zsh' for whatever reason. You can set it to whatever you're running, presumably 'bash'.
Set Value of Variable (Text)
Assign the bash output to a variable. This will be used by the last action for the new filename.
Get Value of Variable (File)
Recall the specified/selected file to rename.
Rename Finder Items: Name Single Item
I have it set to 'Basename only' so it will leave the extension alone. Enter the 'Text' variable from action 4 in here.

OCLint rule customization

I am using OCLint static code analysis tool for objective-C and want to find out how to customize rules? The rules are represented by set of dylib files.
In lieu of passing configuration as arguments (see Jon Boydell's answer), you can also create a YML file named .oclint in the project directory.
Here's an example file that customizes a few things:
rules:
- LongLine
disable-rules:
rulePaths:
- /etc/rules
rule-configurations:
- key: LONG_LINE
value: 20
output: filename
report-type: xml
max-priority-1: 10
max-priority-2: 20
max-priority-3: 30
enable-clang-static-analyzer: false
The answer, as with so many things, is that it depends.
If you want to write your own custom rule then you'll need to get down and dirty into writing your own rule, in C++ on top of the existing source code. Check out the oclint-rules/rules directory, size/LongLineRule.cpp is a simple rule to get going with. You'll need to recompile, etc.
If you want to change the parameters of an existing rule you need to add the command line parameter -rc=<rulename>=<value> to the call to oclint. For example, if you want the long lines rule to only activate for lines longer than 150 chars you need to add -rc=LONG_LINE=150.
I don't have the patience to list out all the different parameters you can change. The list of rules is here http://docs.oclint.org/en/dev/rules/index.html and a list of threshold based rules here http://docs.oclint.org/en/dev/customizing/rules.html but there's no list of acceptable values and I don't know whether these two URLs cover all the rules or not. You might have to look into the source code for each rule to work out how it works.
If you're using Xcode script you should use oclint_args like this:
oclint-json-compilation-database oclint_args "-rc LONG_LINE=150" | sed
's/(..\m{1,2}:[0-9]:[0-9]*:)/\1 warning:/'
in that sample I'm changing the rule of LONG_LINE to 150 chars

How can I combine two PDF pages show up on the same page? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I am looking for a free tool that allows re-arranging pages of a PDF document and combining multiple pages per sheet. The first part (re-arranging) is easily solved by many tools (I use PyPDF).
The problem is with the second requirement: to combine two (or more) pages into single page. For example, take two pages (A and B), rotate them, scale and combine into a single page like this
------ ------ ------
| | | | | |
| A | | B | | a |
| | | | | |
| | | | ---> ------
| | | | | |
| | | | | b |
| | | | | |
------ ------ ------
The solution needs to work on Linux and preferably on Windows too. I'm looking for either console application or library with Python or Perl bindings.
Edit there is pdfnup library that is supposed to perform exactly this kind of transformation, and is cross-platform, however I cannot use it due to a bug similar to this.
This is a summary of the tools I found for PDF (I wanted to find the equivalent of psup and psbook)
Create booklets: pdfbook, pdf-tools (command: pdfbklt)
Merge PDF files: pdfmerge, pdfjam (command: pdfjoin)
Rotate pages: pdfjam (command: pdf90)
Multiple pages per sheet: pdfjam (command: pdfnup)
Create posters (multiple sheets per page): pdfposter
From my package manager:
pdf-tools: http://search.cpan.org/dist/Text-PDF
pdfbook: http://www.ctan.org/tex-archive/support/pdfbook/
pdfmerge: https://github.com/dmaphy/pdfmerge
pdfjam: http://go.warwick.ac.uk/pdfjam
pdfposter: http://pdfposter.origo.ethz.ch/
Create an A6 booklet:
pdfbook -2 -p a5 infile.pdf outfile.pdf
pdf-tools contains:
pdfbklt: create booklets
pdfrevert: Removes one layer of changes to a PDF file, trying to maximise the size of the output file (to account for linearised PDF).
pdfstamp: Adds the given string to the infile .pdf file at the given location, font and size.
There is also multivalent: http://multivalent.sourceforge.net/Tools/index.html
On Linux, you can convert the PDF files to Postscript and use psnup. The exact way to invoke it depends on exactly how you want the pages to be put together, whether you want them rotated, what paper size(s) you want to use, etc. but it'll be something like this:
pdf2ps infile.pdf infile.ps
psnup -2 infile.ps outfile.ps
ps2pdf outfile.ps outfile.pdf
Depending on what tools you have available, you might have a more efficient way to do this - psnup is certainly not the only way, but it's a relatively well-known program (on Linux anyway).
If you use Linux, you can use BookletImposer for putting multiple PDF pages on one single page.
For Ubuntu users, this tool is available at Ubuntu Software Center.
Check out this answer that uses Multivalent to impose PDF pages
In answer to your question, you'll need a PDF 'Imposition' tool, which is a fancy way of saying a tool that arranges PDF page images onto a particular array to create a NEW single PDF page. Imagine it's something like typesetting a newspaper. You define an array of slots a certain number of columns wide, by a certain number of rows deep, on a page of a certain fixed dimensions (in cm). Then you fill those empty slots top to bottom, left to right with pages from a pdf source-file. In the case of the OP, they want to create a single page, composed of two 8.5x11 pages arranged in a 1x2 array (1 column, 2 rows). Their pages will be dropped into the array in the following order: 1,2. So you are dropping the first page (page 1 of the pdf) into the first slot of your array (Column 1, row 1), and you are dropping the second page (page 2) into the second slot (Column 1, row 2).
How to use the tool to make this happen:
Download the old version of Multivalent. Note the author removed the good tool classes from the latest edition without explanation, so you have to use an older one. Here's a working link as of 02/12:
http://code.google.com/p/pdfsizeopt/downloads/detail?name=Multivalent20060102.jar
For simplicity, I renamed the jar file to m.jar.
It sort of goes without saying that you need to install JRE for this to work, but I'll put it out there.
Add m.jar to your Java Class Path Environment variables (for scripting) or run the command line syntax with the -cp option and the relative path (shown below). Note, I ran it FROM the command-line at the install directory in my example below. Provide an absolute path from root otherwise (like c:\1\bin\m.jar).
Here is an example that will accomplish exactly what OP posted about:
C:\1\bin>java -cp m.jar tool.pdf.Impose -dim 1x2 -verbose -papersize "21.59x55.88cm" -layout "1,2" yourfilename.pdf
Note, the -dim option creates the array in Columns x Rows. The -papersize is specified in centimeters here, but if you need inches, just multiply inches by 2.54 to get cm. The -layout option gives you the order you want to fill the empty slots in your array, filling from top to bottom and left to right. In this case, we want page one of the pdf on top and page two on the bottom, so our argument is "1,2". The final argument is your actual source file. The tool will output a file called yourfilename-up.pdf when you are done.
Hope that helps.
-Matt Zweil
Check the source code of PyPDF, especially the rotateClockwise() method. There must be a place where the content of a page is written. Insert a "q" operator (save state) and "cm" (with the correct parameters for a scaling matrix) before the content and a "Q" operator (restore state) afterwards.
See the PDF documentation for an explanation of operators and the structure of a page (scroll to the bottom for some useful links).
Don't forget to send a patch to PyPDF :)
[EDIT] You might also want to check the pdfjam sources which include a pdfnup command.
This is a perl function I use to grab a directory full of prn files from a 3rd party app and create a single merged pdf.
sub runMerged($)
{
my($path) = #_;
print "Generating merged PDFs for $path\n";
my #files = sort(getFiles($path, ".prn\$"));
if (scalar(#files))
{
open(MERGE, ">$path/merged.prn");
for (my $i = 0; $i < scalar(#files); $i++)
{
print MERGE "^L\n" if ($i > 0);
open(FN, "$path/" . $files[$i]);
while (my $line = <FN>)
{
print MERGE $line;
}
close(FN);
}
chdir("$BASE_PATH/txt2pdf");
print `./txt2pdf.pl $path/merged.prn`;
}
}
I Had a similar need this week.
But I needed to repeat each A4 page (landscape) "twice" in the A3 sheet (portrait), to later cut then in half.
I found a Acrobat Plugin with tons of imposition features that worked great for my needs, and with a fully functional 30 days trial.
Hope It could be helpfull for someone else.
http://www.pdfsnake.com/
I had the same issue as you and this is what I did:
Extracted all the pages in the pdf file as a separate file each
In irfanView (with plugins) I created a Vertical "Panoramic" image
Dragged the pdf files over to the images section
Clicked created
The "image" is created with all the pages following each other as one very long vertical image
You can export to PDF with almost no loss in quality.
Enjoy
Here's a script for repeating pages (like A5, landscape) twice on a sheet double this size (A4, portrait):
#!/bin/bash
INPUTFILE=$*
PAGENUM=`pdftk ${INPUTFILE} dump_data | grep NumberOfPages | cut -d : -f 2 | cut -d " " -f 2`
PAGES=`seq 1 ${PAGENUM}`
DUPAGES=`for i in ${PAGES} ; do echo $i $i | tr "\n" " " ; done`
OUTPUT1=`basename ${INPUTFILE} .pdf`.dup.pdf
OUTPUT2=`basename ${INPUTFILE} .pdf`.double.pdf
pdftk ${INPUTFILE} cat ${DUPAGES} output ${OUTPUT1}
pdfjam --nup 1x2 ${OUTPUT1} --outfile ${OUTPUT2}
It's not really elegant; it could be done without the second pdftk call, and it does not work with files containing spaces. But it works with multi-page pdfs.
Using Adobe Acrobat XI Pro, open the 1st document.
Edit, select "take a snapshot", then click at the top corner of what you want to copy and drag to the opposite corner.
Have an open clean sheet in Paint.
Click over to the clean sheet in paint and control V to paste the 1st document into the clean sheet.
Repeat for the 2nd item that you want to combine on the same page except position the 2nd item UNDER the 1st item temporarily in your Paint sheet.
Then drag the 2nd item to position it where you need it in the Paint Sheet.
Save the paint sheet file and you are done!