Python Black Formatter - stop working with "color = true" in pyproject.toml - python-3.x

I have an issue with Black Python formatter in VSCode.
It works perfectly with the VSCode parameters and now I work on a shared repository with a pyproject.toml configuration file.
This file includes a few configs for Black so we all share the same code formatting.
But on this repo, Black does not format anything for me anymore.
This is what the Black config part look like
[tool.black]
# https://github.com/psf/black
target-version = ["py39"]
color = false
line-length = 120
exclude = '''
/(
\.git
| \.mypy_cache
| \.tox
| \.venv
| build
| venv
)/
'''
What is really weird to me is that the single line that seems to be the issue is color = true.
If I remove it or even do color = false, it works fine.
But I don't want to change a parameter for everyone on the repo because it fails on my computer...
Does anyone have any idea?
P.S: I am on Windows, could it be the reason? And if so, is there anything I can do about it?

Related

"-m" not visible when using Git in Windows Terminal

I'm using Windows Terminal and I'm having trouble with Git, when I type -m it's invisible:
I've tried modifying the .gitconfig file but haven't managed to find the parameter to that specific thing. How can I change it?
This looks like a PowerShell theme issue - check the output of Get-PSReadLineOption - if you see any invisible colors:
PowerShell invisible colors
You can fix this by specifying your own colors - e.g: Set-PSReadLineOption -Colors #{ 'Number' = "blue"; 'Member' = "DarkGray"}

Acessing myclass defined in python file in different folder from jupyter notebook

I have following folder structure
|
|---notebook
| |
| --- MyNoteBook.ipnyb
|
|---python
| |
| --- image_rotation_utils.py
I have following content in python.image_rotation_utils.py
class ImageRotationUtils:
""" Utils for rotating images."""
def __init__(self, image_path=None, image_name=None, output_path=None, output_annotations_file=None ):
""" Initializes the class.
Args:
image_path: the relative path to the image
image_name: image file name
output_path: the relative path where rotated output file is stored.
output_annotations_file: annotations file where rotated anotations are stored.
"""
self.image_path = image_path
self.image_name = image_name
self.output_path = output_path
self.output_annotations_file = output_annotations_file
#other functions defined
Now in MyNoteBook.ipnyb I have following cells
Cell 1:
import sys
sys.path.insert(0, '..')
from python.image_rotation_utils import *
Above is executed successfully now in cell 2 I have below
myUtils = ImageRotationUtils()
I have getting below error
----> 1 ImageRotationUtils()
TypeError: __init__() missing 4 required positional arguments: 'image_path', 'image_name', 'output_path', and 'output_annotations_file'
whye I am getting this error when I have default values in constructor has None in class definition.
I am new to python and trying to put code in productive manner. What mistake I am making. Kindly provide inputs
What you pasted looks ok to me, and your code works for me without errors.
Perhaps something before or after that code is what is spoiling the class definition?
Perhaps you are running this in Jupyter or in IPython and you imported ImageRotationUtils previously, but then changed the code, and tried to reimport again -- and something was wrong with the new reimport so your definition did not get over-written? If that's the case, restart the environment (or the kernel in Jupyter) and rerun the code.
I would suggest putting a simple initialization code, like that constructor line, into the same source file and executing it as a separate process to test if that is the code or the environment issue.
As a matter of convenience and to avoid tweaking sys.path in your code, I would suggest adding your python directory to the PYTHONPATH environment variable before you load your environment, so you can just import.

Synchronous pymol

I am trying to load an mrc file, generate the map of it and then save the image, the problem is that it only saves a blank image, the code I run is the following:
from pymol import cmd
cmd.load('./6vof.mrc', ' 6vof')
cmd.volume( '6vof _volume', ' 6vof')
cmd.png('./test.png', 300, 200)
The equivalent code in the interface does work, I wanted to know how I can make the save method wait for the rendering of everything.
Hi Luis José Castillo,
it seems I encountered the same problem as you did.
I wanted to use Pymol from within a Ruby on Rails application and always got his message, when I launched it from within ruby:
CmdLoad: "mypol/4hhb.pdb" loaded as "4hhb".
Ray: render time: 0.02 sec. = 178776.9 frames/hour (0.02 sec. accum.).
The result was a blank transparent png. If I ran it in the command line with the same script I got this message and a perfect png:
CmdLoad: "4hhb.pdb" loaded as "4hhb".
Ray: render time: 4.04 sec. = 890.8 frames/hour (4.04 sec. accum.).
I got this behavoir on Ubuntu 20, Ubuntu 18 Server and in Docker.
I figured out, that the only difference was, I used relative paths from within ruby, but was always running everything in the same folder in the command line.
Well, so I just make this detour for my Rails application and now it does work:
def get_protein_image(pdb_id)
dir_path = get_pdb_file(pdb_id)
`cp lib/png_from_pdb.py #{dir_path} && cd #{dir_path} && pymol protein.pdb png_from_pdb.py -qc`
end
Sorry for the ruby code here, but here the essential solution, try to run everything inside the same folder (I simply delete the tmp subfolder after png generation).
cp lib/png_from_pdb.py #{dir_path} && cd #{dir_path} && pymol protein.pdb png_from_pdb.py -qc
I hope this trick will help you too!
Best regards,
Stephan

Autoformatting failed, buffer not changed : Sublime text

While setting up Python Development Environment in Sublime text 3, I wanted Auto formatting on and hence I made the following settings in Preferences > Package settings > Anaconda > Settings User
{
"auto_formatting": true,
"autoformat_ignore":
[
],
"pep8_ignore":
[
"E501"
],
"anaconda_linter_underlines": false,
"anaconda_linter_mark_style": "none",
"display_signatures": false,
"disable_anaconda_completion": true,
"python_interpreter": "/usr/local/bin/python3"
}
The auto_formatting value is set to true in user settings and it is set to false in default settings .
The auto formatting does not work out and gives me Autoformatting failed, buffer not changed error . Also tried changing auto_formatting_timeout = 5 //seconds , but that didn't work out . It would be of great help if someone could help me out .
I had the exact same issue. The problem is the last line is pointing to the wrong path for your python interpreter
Preferences > Package Settings > Anaconda > Settings - User
Delete the last line
"python_interpreter": "/usr/local/bin/python3"
Replace it with
"python_interpreter": "C:\\Users\\YOUR_NAME\\AppData\\Local\\Programs\\Python\\Python38-32\\python.EXE"
IMPORTANT: Your python interpreter path might be different to mine so find out where it is. Remember you need to use TWO backslashes in the path not ONE
Sublime is looking for python in environmental variable when you build it, and it is not able to find it there. You can test it in cmd by typing python and if it opens Microsoft store then here is the fix you can do, it worked for me. Open setting -> search for manage app execution aliases -> turn off "python.exe" and "python3.exe". Now check if you can get python in cmd by typing python, if you do then sublime problem should be fixed. If you want to know why this works follow this stack overflow

cookie cutter: what's the easiest way to specify variables for the prompts

Is there anything that offers replay-type functionality, by pointing at a predefined prompt-answer file?
What works and what I'd like to achieve.
Let's take an example, using a cookiecutter to prep a Python package for pypi
cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git
You've downloaded /Users/jluc/.cookiecutters/cookiecutter-pypackage before. Is it okay to delete and re-download it? [yes]:
full_name [Audrey Roy Greenfeld]: Spartacus 👈 constant for me/my organization
email [audreyr#example.com]: spartacus#example.com 👈 constant for me/my organization
...
project_name [Python Boilerplate]: GladiatorRevolt 👈 this will vary.
project_slug [q]: gladiator-revolt 👈 this too
...
OK, done.
Now, I can easily redo this, for this project, via:
cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git --replay
This is great!
What I want:
Say I create another project, UnleashHell.
I want to prep a file somehow that has my developer-info and project level info for Unleash. And I want to be able to run it multiple times against this template, without having to deal with prompts. This particular pypi template gets regular updates, for example python 2.7 support has been dropped.
The problem:
A --replay will just inject the last run for this cookiecutter template. If it was run against a different pypi project, too bad.
I'm good with my developer-level info, but I need to vary all the project level info.
I tried copying the replay file via:
cp ~/.cookiecutter_replay/cookiecutter-pypackage.json unleash.json
Edit unleash.json to reflect necessary changes.
Then specify it via --config-file flag
cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git --config-file unleash.json
I get an ugly error, it wants YAML, apparently.
cookiecutter.exceptions.InvalidConfiguration: Unable to parse YAML file .../000.packaging/unleash.json. Error: None of the known patterns match for {
"cookiecutter": {
"full_name": "Spartacus",
No problem, json2yaml to the rescue.
That doesn't work either.
cookiecutter.exceptions.InvalidConfiguration: Unable to parse YAML file ./cookie.yaml. Error: Unable to determine type for "
full_name: "Spartacus"
I also tried a < stdin redirect:
cookiecutter.prompts.txt:
yes
Spartacus
...
It doesn't seem to use it and just aborts.
cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git < ./cookiecutter.prompts.txt
You've downloaded ~/.cookiecutters/cookiecutter-pypackage before. Is it okay to delete and re-download it? [yes]
: full_name [Audrey Roy Greenfeld]
: email [audreyr#example.com]
: Aborted
I suspect I am missing something obvious, not sure what. To start with, what is the intent and format expected for the --config file?
Debrief - how I got it working from accepted answer.
Took accepted answer, but adjusted it for ~/.cookiecutterrc usage. It works but the format is not super clear. Especially not on the rc which has to be yaml, though that's not always/often the case with rc files.
This ended up working:
file ~/.cookiecutterrc:
without nesting under default_context... tons of unhelpful yaml parse errors (on a valid yaml doc).
default_context:
#... cut out for privacy
add_pyup_badge: y
command_line_interface: "Click"
create_author_file: "y"
open_source_license: "MIT license"
# the names to use here are:
# full_name:
# email:
# github_username:
# project_name:
# project_slug:
# project_short_description:
# pypi_username:
# version:
# use_pytest:
# use_pypi_deployment_with_travis:
# add_pyup_badge:
# command_line_interface:
# create_author_file:
# open_source_license:
I still could not get a combination of ~/.cookiecutterrc and a project-specific config.yaml to work. Too bad that expected configuration format is so lightly documented.
So I will use the .rc but enter the project name, slug and description each time. Oh well, good enough for now.
You are near.
Try this cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git --no-input --config-file config.yaml
The --no-input parameter will suppress the terminal user input, it is optional of course.
The config.yaml file could look like this:
default_context:
full_name: "Audrey Roy"
email: "audreyr#example.com"
github_username: "audreyr"
cookiecutters_dir: "/home/audreyr/my-custom-cookiecutters-dir/"
replay_dir: "/home/audreyr/my-custom-replay-dir/"
abbreviations:
pp: https://github.com/audreyr/cookiecutter-pypackage.git
gh: https://github.com/{0}.git
bb: https://bitbucket.org/{0}
Reference to this example file: https://cookiecutter.readthedocs.io/en/1.7.0/advanced/user_config.html
You probably just need the default_context block since that is where the user input goes.

Resources