GithubHelp home page GithubHelp logo

Comments (2)

Tirasz avatar Tirasz commented on September 2, 2024

First, i tried to just modify the _get_subject() method, to return a node.
(instead of just recognizing a Name node, and returning its id)
Then, i modified the get_const_node method, since this also depended on the subject being a Name node.

Then came the hard part:
Since I used the id-s of Name nodes before, (strings), it was easy for me to put them into sets and manipulate them.
But now, I'm using actual Node-s (objects). This sadly meant, that two, completely identical subjects were treated as if they were different, since technically, they were different objects.
The reason for that is, is that python is using the default, eq() method for comparing two objects, which just compares their addresses in memory.
Maybe I have a way of overwriting this method for all the different types of nodes, but i dont know how good of an idea that is.

I decided that the easiest way to check if two subjects are equal, is to compare their ast.dump()-s.
(First of all, im not sure if this is actually correct for every possibility, and Im also not sure if this is the most efficient way of doing this)
But since there is no method for going from a dump to an actual node object, i couldnt just use this "representation" of the nodes alone.
To solve this, i made a dictionary (ofc) that maps dumps to actual node objects.
I made sure, that if a dump is already a part of its keys, i dont overwrite its value, since my point with this, is that i want identical subjects to have an identical node object representing them.

With this, i just had to make sure that the analyzers were returning this "representative" object, so that in the main analyzer i could manipulete them using sets, just like with the Name node id-s.

from transpy.

Tirasz avatar Tirasz commented on September 2, 2024

By "monkey-patching" the ast.AST.__hash__ method, to return a hash of the node's ast.dump(), and the ast.AST.__eq__ method, to compare the hashes of two nodes, I managed to make the process a bit simpler.
Doing this allows me to just put the subjects into sets, without having the problem of two identical subjects being treated as two different objects.
Now I just have to wait for the day when this 'patch' is going to bite me in the ass.
Until then, it works fine.

from transpy.

Related Issues (7)

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.