Re: [Rdkit-discuss] TIL: Mol objects having varying attributes depending on rdkit imports
On 9/23/2020 1:55 PM, Michal Krompiec wrote: Uhm, you’re right. This is not unique to python, python just adds the name-binding twists to the confusion. E.g. C++ methods aren't any different: they exist on code pages regardless of whether their classes have been instantiated or not, and are implicitly passed *this*, just like python's bound methods get __self__. The compiler and/or runtime enforces scope and visibility rules and will throw an error on a symbol that isn't defined and/or visible in a given scope. Python's trickier than C++ because the latter constructs a symbol table at compile time so at runtime all names "exist" from the opening curly. In python they're only visible from the moment they're "bound". Unless they're method names, IIRC. Confused yet? ;) Dima ___ Rdkit-discuss mailing list Rdkit-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
Re: [Rdkit-discuss] TIL: Mol objects having varying attributes depending on rdkit imports
Uhm, you’re right. On Wed, Sep 23, 2020 at 7:46 PM Rocco Moretti wrote: > Python translates object.method() to method(object). > > > Well, yes and no. "Yes" in the sense that instance methods are internally > implemented equivalently to a free method which takes an instance as the > first parameter. "No" in the sense that from a namespace and user > perspective there typically isn't a crossover: > > >>> class TestClass: > def __init__(self, name): > self.name = name > def say(self): > print("I'm TestClass",self.name) > > >>> def recite(test_class): > test_class.say() > > >>> t = TestClass("Bob") > >>> t.say() > I'm TestClass Bob > >>> recite(t) > I'm TestClass Bob > >>> t.recite() > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'TestClass' object has no attribute 'recite' > > (As the `recite` function is in the local namespace, not in the TestClass > namespace, `t.recite()` can't find it.) > > While, due to the equivalence between free functions and member functions, > you can certainly inject such a free function into the class: > > >>> TestClass.recite = recite > >>> t.recite() > I'm TestClass Bob > > such an injection isn't universally what one sees in typical Python > programs, as anyone who's tried to do a `mylist.len()` can attest. Doing > such an injection dependent on module imports is much rarer, and certainly > not the expected "standard" behavior in Python. (It's certainly not > automatic in Python, if that was what you were trying to imply.) > > Regards, > -Rocco > > > ___ > > Rdkit-discuss mailing list > > Rdkit-discuss@lists.sourceforge.net > > https://lists.sourceforge.net/lists/listinfo/rdkit-discuss > > ___ Rdkit-discuss mailing list Rdkit-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
Re: [Rdkit-discuss] TIL: Mol objects having varying attributes depending on rdkit imports
> > Python translates object.method() to method(object). Well, yes and no. "Yes" in the sense that instance methods are internally implemented equivalently to a free method which takes an instance as the first parameter. "No" in the sense that from a namespace and user perspective there typically isn't a crossover: >>> class TestClass: def __init__(self, name): self.name = name def say(self): print("I'm TestClass",self.name) >>> def recite(test_class): test_class.say() >>> t = TestClass("Bob") >>> t.say() I'm TestClass Bob >>> recite(t) I'm TestClass Bob >>> t.recite() Traceback (most recent call last): File "", line 1, in AttributeError: 'TestClass' object has no attribute 'recite' (As the `recite` function is in the local namespace, not in the TestClass namespace, `t.recite()` can't find it.) While, due to the equivalence between free functions and member functions, you can certainly inject such a free function into the class: >>> TestClass.recite = recite >>> t.recite() I'm TestClass Bob such an injection isn't universally what one sees in typical Python programs, as anyone who's tried to do a `mylist.len()` can attest. Doing such an injection dependent on module imports is much rarer, and certainly not the expected "standard" behavior in Python. (It's certainly not automatic in Python, if that was what you were trying to imply.) Regards, -Rocco ___ Rdkit-discuss mailing list Rdkit-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
Re: [Rdkit-discuss] TIL: Mol objects having varying attributes depending on rdkit imports
Hi Rocco & Norwid, Actually, this is expected given the fact that Python translates object.method() to method(object). Hence, m.Compute2DCoords(), although "incorrect" (because Compute2DCoords() is not a method of the Mol class), is valid Python code and is understood as Compute2DCoords(m). And this won't work unless AllChem is loaded. Best, Michal Krompiec Merck KGaA On Wed, 23 Sep 2020 at 16:56, Rocco Moretti wrote: > Hi Norwid, > > There's a subtle but significant difference between the two examples: > > >>> AllChem.Compute2DCoords(m) > versus > >>> m.Compute2DCoords() > > For the former, it's pretty standard Python behavior not to be able to see > a function from a module if you haven't loaded the module yet. That's > expected behavior, and something you'll learn early on when working with > Python modules. > > For the latter, it's not standard Python behavior to have methods which > aren't visible until some other module is loaded. Generally, if you have an > object of a class, you have access to all the methods of that class. Just > having part of the class and then needing to import a separate module to > get the rest of the methods is certainly not something you typically expect > in Python. > > Regards, > -Rocco > > > On Wed, Sep 23, 2020 at 5:35 AM Norwid Behrnd via Rdkit-discuss < > rdkit-discuss@lists.sourceforge.net> wrote: > >> Hi Thomas, >> >> could your report be already backed by the section titled «Working with >> 2D molecules: Generating Depictions» of the upper half of page >> >> https://www.rdkit.org/docs/GettingStartedInPython.htm >> >> about the 2020.03.1 documentation with the following example? >> >> 8>< begin snippet --- >> >>> m = Chem.MolFromSmiles('c1nccc2n1ccc2') >> >>> AllChem.Compute2DCoords(m) >> 0 >> 8>< end snippet --- >> >> Because this snippet is part of a show case, a minimal working example >> (at least for a bit old RDKit 2019.9.1) translates into >> >> 8>< begin snippet --- >> from rdkit import Chem >> from rdkit.Chem import AllChem >> m = Chem.MolFromSmiles('c1c1') >> AllChem.Compute2DCoords(m) >> 8>< end snippet --- >> >> to yield "0" (zero). >> >> However, possibly contributing to your struggle, note an entry on page >> >> https://www.rdkit.org/docs/GettingStartedInPython.html#chem-vs-allchem >> >> with the snippet >> >> 8>< begin snippet --- >> >>> from rdkit.Chem import AllChem as Chem >> >>> m = Chem.MolFromSmiles('CCC') >> 8>< end snippet --- >> >> equivalent to a MWE of >> >> 8>< begin snippet --- >> import rdkit >> from rdkit.Chem import AllChem as Chem >> m = Chem.MolFromSmiles('CCC') >> Chem.Compute2DCoords(m) >> 8>< end snippet --- >> >> to equally yield "0" (zero). >> >> I only recall this part of the manual because one of my yesterday's >> problems caused me to revisit the beginner's page again. >> >> Norwid >> >> >> ___ >> Rdkit-discuss mailing list >> Rdkit-discuss@lists.sourceforge.net >> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss >> > ___ > Rdkit-discuss mailing list > Rdkit-discuss@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/rdkit-discuss > ___ Rdkit-discuss mailing list Rdkit-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
Re: [Rdkit-discuss] TIL: Mol objects having varying attributes depending on rdkit imports
Hi Norwid, There's a subtle but significant difference between the two examples: >>> AllChem.Compute2DCoords(m) versus >>> m.Compute2DCoords() For the former, it's pretty standard Python behavior not to be able to see a function from a module if you haven't loaded the module yet. That's expected behavior, and something you'll learn early on when working with Python modules. For the latter, it's not standard Python behavior to have methods which aren't visible until some other module is loaded. Generally, if you have an object of a class, you have access to all the methods of that class. Just having part of the class and then needing to import a separate module to get the rest of the methods is certainly not something you typically expect in Python. Regards, -Rocco On Wed, Sep 23, 2020 at 5:35 AM Norwid Behrnd via Rdkit-discuss < rdkit-discuss@lists.sourceforge.net> wrote: > Hi Thomas, > > could your report be already backed by the section titled «Working with > 2D molecules: Generating Depictions» of the upper half of page > > https://www.rdkit.org/docs/GettingStartedInPython.htm > > about the 2020.03.1 documentation with the following example? > > 8>< begin snippet --- > >>> m = Chem.MolFromSmiles('c1nccc2n1ccc2') > >>> AllChem.Compute2DCoords(m) > 0 > 8>< end snippet --- > > Because this snippet is part of a show case, a minimal working example > (at least for a bit old RDKit 2019.9.1) translates into > > 8>< begin snippet --- > from rdkit import Chem > from rdkit.Chem import AllChem > m = Chem.MolFromSmiles('c1c1') > AllChem.Compute2DCoords(m) > 8>< end snippet --- > > to yield "0" (zero). > > However, possibly contributing to your struggle, note an entry on page > > https://www.rdkit.org/docs/GettingStartedInPython.html#chem-vs-allchem > > with the snippet > > 8>< begin snippet --- > >>> from rdkit.Chem import AllChem as Chem > >>> m = Chem.MolFromSmiles('CCC') > 8>< end snippet --- > > equivalent to a MWE of > > 8>< begin snippet --- > import rdkit > from rdkit.Chem import AllChem as Chem > m = Chem.MolFromSmiles('CCC') > Chem.Compute2DCoords(m) > 8>< end snippet --- > > to equally yield "0" (zero). > > I only recall this part of the manual because one of my yesterday's > problems caused me to revisit the beginner's page again. > > Norwid > > > ___ > Rdkit-discuss mailing list > Rdkit-discuss@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/rdkit-discuss > ___ Rdkit-discuss mailing list Rdkit-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
Re: [Rdkit-discuss] TIL: Mol objects having varying attributes depending on rdkit imports
Hi Thomas, could your report be already backed by the section titled «Working with 2D molecules: Generating Depictions» of the upper half of page https://www.rdkit.org/docs/GettingStartedInPython.htm about the 2020.03.1 documentation with the following example? 8>< begin snippet --- >>> m = Chem.MolFromSmiles('c1nccc2n1ccc2') >>> AllChem.Compute2DCoords(m) 0 8>< end snippet --- Because this snippet is part of a show case, a minimal working example (at least for a bit old RDKit 2019.9.1) translates into 8>< begin snippet --- from rdkit import Chem from rdkit.Chem import AllChem m = Chem.MolFromSmiles('c1c1') AllChem.Compute2DCoords(m) 8>< end snippet --- to yield "0" (zero). However, possibly contributing to your struggle, note an entry on page https://www.rdkit.org/docs/GettingStartedInPython.html#chem-vs-allchem with the snippet 8>< begin snippet --- >>> from rdkit.Chem import AllChem as Chem >>> m = Chem.MolFromSmiles('CCC') 8>< end snippet --- equivalent to a MWE of 8>< begin snippet --- import rdkit from rdkit.Chem import AllChem as Chem m = Chem.MolFromSmiles('CCC') Chem.Compute2DCoords(m) 8>< end snippet --- to equally yield "0" (zero). I only recall this part of the manual because one of my yesterday's problems caused me to revisit the beginner's page again. Norwid ___ Rdkit-discuss mailing list Rdkit-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
[Rdkit-discuss] TIL: Mol objects having varying attributes depending on rdkit imports
Dear readers, just wasted an amazing amount of time and reporting this in case someone else happens to run into this problem. if you simply do: from rdkit import Chem m = Chem.MolFromSmiles('c1c1') m.Compute2DCoords() you will get an error Mol object has no attribute Compute2DCoords. Since I have previously done this many times and it worked, it was very confusing. It seems that to have this attribute available one needs to also do from rdkit import AllChem and then it works. This also applies to ComputeGasteigerCharges and maybe even more methods. So if you have this problem, you will now hopefully find this information via search and be able to solve it much faster than I was. best regards, Thomas ___ Rdkit-discuss mailing list Rdkit-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rdkit-discuss