Giter VIP home page Giter VIP logo

Comments (4)

alexmojaki avatar alexmojaki commented on May 23, 2024

raise_exc is (at least originally, I think) intended for suppressing a different kind of error, e.g. not being able to retrieve source code. In this case the problem is improper usage, as it's not obvious what the name of x[0] (or subscripts in general) should be.

Similarly, you will get an error with x, y = my_func() saying Expect a single variable on left-hand side, got 2.

On the other hand, my_func() without any assignment will respect raise_exc, and that's also invalid usage, so the library is not being consistent.

Seems like there should be a way to distinguish between "Something is wrong and retrieving the name is not working, you will have to find a workaround" and "This function is being used incorrectly, tell your user to assign it to a variable".

from python-varname.

GeorgeScrivener avatar GeorgeScrivener commented on May 23, 2024

Okay that makes sense thanks, though it would be good to update the docs to make that clearer as I'd expect it to be functionally equivalently to wrapping the call in try/except, which is my current workaround.

The example I gave was improper use but I think it still represents a valid use of the library.
My use case is for writing a library that has an API somewhat similar to tensorflow and I'd like to automatically name variables in the same way e.g.

# The tensor automatically gets the name 'conv1' which can be used later by external tooling
conv1 = Conv2D(32, 3, activation='relu') 

where the call to varname is somewhere inside Conv2D.
It would be perfectly valid for the library user to store Conv2D(...) in a list in which case the tensor would be unnamed.

from python-varname.

pwwang avatar pwwang commented on May 23, 2024

@alexmojaki You guess is correct. VarnameRetrievingError initially does not distinguish those two situations: improper use and inability to retrieve node.

We may need two different Exception classes to do so.

@GeorgeScrivener Another easy solution you could try is:

from varname.helpers import register
Conv2d = register(Conv2d)

conv1 = Conv2D(32, 3, activation='relu') 

# isinstance(conv1, Conv2d)
# conv1.__varname__ == 'conv1'

Just pay attention that this solution is not thread-safe.

from python-varname.

pwwang avatar pwwang commented on May 23, 2024

An ImproperUseError is added to distinguish improper use of varname() from ast node retrieving failure, at v0.6.4.

For now, ImproperUseError is a subclass of VarnameRetrievingError, so that you don't have to modify your code when you try to use VarnameRetrievingError to catch the error:

try:
  x[0] = my_func()
except VarnameRetrievingError:
  ...

But in the future, ImproperUseError will become independent of VarnameRetrievingError.

Keeping this issue open until ImproperUseError becomes independent.

from python-varname.

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.