Skip to content

matplotlibrc reader cannot handle rcparams with a hash #7089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
has2k1 opened this issue Sep 11, 2016 · 14 comments
Closed

matplotlibrc reader cannot handle rcparams with a hash #7089

has2k1 opened this issue Sep 11, 2016 · 14 comments

Comments

@has2k1
Copy link
Contributor

has2k1 commented Sep 11, 2016

The parser for the .mplstyle files is eager when identifying comments. A # always demarcates the beginning of a comment, therefore it cannot be part of an rcparam.

Reference: #6907 (comment)
The reader function: https://github.com/matplotlib/matplotlib/blob/af16ac8/lib/matplotlib/__init__.py#L1055
Example style file: https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/mpl-data/stylelib/classic.mplstyle#L183

@tacaswell
Copy link
Member

Maybe we should just use json or yaml for the style files?

@WeatherGod
Copy link
Member

not json... and yaml brings in another dependency (but it isn't that
onoerous...). ConfigParser is now in the standard library, and it could
bring for us string interpolation features (which I have been wanting for a
while).

On Sun, Sep 11, 2016 at 12:02 PM, Thomas A Caswell <notifications@github.com

wrote:

Maybe we should just use json or yaml for the style files?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#7089 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AARy-OJk5Y42-PwB0UT6oMQeDlYXQc57ks5qpCYigaJpZM4J57mm
.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Sep 11, 2016
@has2k1 has2k1 changed the title matplotlibrc reader cannot rcparams with a hash matplotlibrc reader cannot handle rcparams with a hash Sep 11, 2016
@has2k1
Copy link
Contributor Author

has2k1 commented Sep 11, 2016

Comparison of potential options.

YAML JSON ConfigParser
No Extra Dependency No Yes Yes
Interpolation Yes No Yes
Inline comments Yes No Yes[1]

Caveats

  1. Cannot be used for entries that have only keys(and no values).

@efiring
Copy link
Member

efiring commented Sep 11, 2016

It doesn't look to me like ConfigParser provides facilities for gracefully handling the problem of an inline hash that starts a comment unless it is inside a string. YAML looks like a pretty big hammer. I think I would rather leave the existing restriction against having the hash in a value than introduce a new dependency and a new set of rules for matplotlibrc and style files.

@WeatherGod
Copy link
Member

JSON can not support inline comments

On Sun, Sep 11, 2016 at 2:07 PM, Eric Firing notifications@github.com
wrote:

It doesn't look to me like ConfigParser provides facilities for gracefully
handling the problem of an inline hash that starts a comment unless it is
inside a string. YAML looks like a pretty big hammer. I think I would
rather leave the existing restriction against having the hash in a value
than introduce a new dependency and a new set of rules for matplotlibrc and
style files.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#7089 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AARy-Jww4rIys61x5noWIlfmpVh-C0PNks5qpENbgaJpZM4J57mm
.

@anntzer
Copy link
Contributor

anntzer commented Sep 12, 2016

I'm going to make a possibly not so popular but actually serious proposal: if we do decide to change the config format, simply switch to using python source files as style and config formats.

Pros:
No extra dependency, interpolation, inline comments (duh).
Basically anything you will ever need.

Cons:
AFAICT the main con I have heard about is the security issue. But the current parser is already unsafe (See link from OP in #6274 (Yes, I know that PR also proposes a custom PyParsing-based parser -- I guess that would be a pretty big hammer...)). Additionally, a number of prominent projects (e.g. IPython, Sphinx, Django) already use python for config files without this turning out to be a real problem.

@jenshnielsen
Copy link
Member

This should also be coordinated with the traitlets work #4762 Afaik the IPython config files are traitlets based

@davenquinn
Copy link

davenquinn commented Apr 15, 2017

Why not just make yaml an optional parser for the matplotlibrc reader, if installed? The amount of code to read into the rc system is minimal, and we could integrate them more closely over time if needed.

I've already made a basic parser for this type of data for use in a project, it seems to work fine for basic datasets:

with open(fn) as f:
    style_object = yaml.load(f)
for k,v in style_object.items():
    matplotlib.rc(k,**v)

...which reads YAML configuration files as below into matplotlibrc.

font:
  size: 8
  family: [sans-serif]
  sans-serif:
    - Helvetica Neue
    - Helvetica
    - Arial
    - sans-serif
  weight: 200
mathtext:
  default: regular
axes:
  labelsize: 8
  titlesize: 12
  facecolor: white
  grid: false
  edgecolor: black

A more bulletproof parser would probably take some effort, but I imagine not much. Happy to submit a pr if it seems like a good plan.

@anntzer
Copy link
Contributor

anntzer commented Apr 15, 2017

My personal opinion is that yaml is a far too complex language for any purpose (see e.g. http://stackoverflow.com/questions/3790454/in-yaml-how-do-i-break-a-string-over-multiple-lines).

@WeatherGod
Copy link
Member

WeatherGod commented Apr 16, 2017 via email

@anntzer
Copy link
Contributor

anntzer commented Apr 16, 2017

This is just an example amongst others of the extreme complexity of the language. For example, would you know whether

- % foo

is valid yaml? if so, what does it evaluate to? what about

- foo % foo

?

@davenquinn
Copy link

YAML is a pretty standard config language for many projects, and trying to reinvent the wheel when a standard tool can be adapted to the system is unnecessary.

@anntzer: we don't have to support the entire feature-set of YAML or handle edge cases for it to be useful nonetheless

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@github-actions
Copy link

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Mar 29, 2023
@oscargus
Copy link
Member

oscargus commented Apr 7, 2023

In #22589 support for quoted strings was introduced, which then allows including a hash. Will close this for now.

@oscargus oscargus closed this as completed Apr 7, 2023
@oscargus oscargus modified the milestones: future releases, v3.6.0 Apr 7, 2023
@QuLogic QuLogic removed the status: inactive Marked by the “Stale” Github Action label Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants