Git Product home page Git Product logo

Comments (7)

odashi avatar odashi commented on April 29, 2024

Maybe it could be a choice to provide a global function latexify.set_config() to modify the default behavior:

import latexify
latexify.set_config(...)  # Calls it before any @with_latex decorators.

@with_latex
def foobar(...):
  ...

from latexify_py.

1MLightyears avatar 1MLightyears commented on April 29, 2024
class LatexifyVisitor(ast.NodeVisitor):
# define static variables here like
  math_symbol = True
  equal_form = r"\triangleq"

# change __init__ into
  def __init__(self, **kwargs):
    # config part
    for k in kwargs.keys():
      if k in dir(LatexifyVisitor):
        setattr(LatexifyVisitor,k,kwargs[k])
      else:
        # deal wrong config setting here
    ...

Will allow all instances of LatexifyVisitor keep the same config.

from latexify_py.

odashi avatar odashi commented on April 29, 2024

For this particular implementation, it looks somewhat dangerous because...

  • object method (__init__) globally changes the class attributes.
  • dir() returns every visible symbol of all super classes.
  • kwargs drops much stuff for self-documenting -- using it brings difficulty for further maintenance.

I am thinking of separating configs from the visitor completely rather than directly implementing them into the class.

from latexify_py.

1MLightyears avatar 1MLightyears commented on April 29, 2024

Well...it isn't a strict implementation, I just want to show the logic. Of course there are many ways to strengthen its security, like

class LatexifyVisitor(ast.NodeVisitor):
  math_symbol = True
  equal_form = r"\triangleq"

  config_list=['math_symbol',
               'equal_form'
               # more could be added here in future
               ]

and change dir(LatexifyVisitor) into config_list, or
simply change

if k in dir(LatexifyVisitor):

into

if not callable(getattr(LatexifyVisitor,k,lambda:1))# or any callable dummy function

will guarantee k is a variable defined in LatexifyVisitor.
I'm just voting for implementing config inside LatexifyVisitor, but if you finally decide to have it outside, I'm also glad to see it. A config working is better than none.
As for documentations, it is true documenting things like kwargs takes a lot of time and energy. But since latexify is so welcomed and has earned 800+ stars so far, making it more easy to use would make latexify a better tool. All we need to do at present is to add an example at the Jupyter notebook page-I think that is a tutorial good and simple enough for new users.

from latexify_py.

odashi avatar odashi commented on April 29, 2024

Well, the essential point I am thinking of is these flags should not be class attributes since it would be changed every time we invoked __init__. And introspection is basically the final choice of implementation which is chosen only when there are no easier solutions.

The first intent of this issue is how to avoid specifying a bunch of flags to @with_latex for user-default flags. Your solution can resolve it by specifying a particular flag to only the first decoration, but it will also introduce any kind of confusion due to a stateful behavior of the decorator.

it isn't a strict implementation, I just want to show the logic.

I could think so, but couldn't guess which aspect is actually pointed. That's why I said "for this particular implementation..."

not callable(getattr(LatexifyVisitor,k,lambda:1)) will guarantee k is a variable defined in LatexifyVisitor.

Unfortunately no, it still returns many ruins from super classes. Of course we can also take any hacks to avoid them, but this is unneeded burden we have to take here.

from latexify_py.

1MLightyears avatar 1MLightyears commented on April 29, 2024

It seems I have misunderstood your requirements of the config. I'm not experienced in designing configs and thus haven't thought that far.
I would not try to implement the config part in #15 and would return the discussion after another config logic be come up with.

from latexify_py.

odashi avatar odashi commented on April 29, 2024

This issue is taken by #66 , close for now.

from latexify_py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.