Re: Argh!! Can't wrap my head around this Python stuff!
On 26/11/2017 09:09, Greg Tibbet wrote: > > I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit > of Java and trying to learn this new-fangled Python language! > > I've got a small program that uses PIL to create an image, draw some > primitives (rectanges, ellipses, etc...) and save it. Works fine... > no issues. > > I've found in the past, the best way to "really learn" the language > was to "dig into the guts" and understand it,.. I thought I was making > progress, but when looking into the PIL library to see what's going on > behind the scenes, I find the following code in ImageDraw.py > > def ellipse(self, xy, fill=None, outline=None): > """Draw an ellipse.""" > ink, fill = self._getink(outline, fill) > if fill is not None: > self.draw.draw_ellipse(xy, fill, 1) > <...snipped...> > > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? Python isn't a very pure language in that much of the functionality that looks like it should be written in Python (because you imported a module just like you import any Python module), actually is written in something else, as has been pointed out. It's reasonable that some things need to be implemented using some foreign functions. But the boundary between Python and non-Python is blurred. Take this program: import sys and try and find sys.py in your installation. (This is an obstacle if, for example, you're thinking of implementing a Python interpreter. In theory, once you have it working, it should run any .py program. But the critical modules it needs don't have .py source code. And the interface to those non-Python functions isn't defined with special byte-code instructions. (It will be done /via/ those instructions, but the magic needed is on the other side of them. Calling into sys.fn() uses the same CALL_FUNCTION byte-code as calling into a regular Python function.) As I said, it's not pure. More of a jungle as you've found out.) -- bartc -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
On Sunday, November 26, 2017 at 4:10:12 AM UTC-5, Greg Tibbet wrote: > I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit > of Java and trying to learn this new-fangled Python language! > > I've got a small program that uses PIL to create an image, draw some > primitives (rectanges, ellipses, etc...) and save it. Works fine... > no issues. > > I've found in the past, the best way to "really learn" the language > was to "dig into the guts" and understand it,.. I thought I was making > progress, but when looking into the PIL library to see what's going on > behind the scenes, I find the following code in ImageDraw.py > > def ellipse(self, xy, fill=None, outline=None): > """Draw an ellipse.""" > ink, fill = self._getink(outline, fill) > if fill is not None: > self.draw.draw_ellipse(xy, fill, 1) > <...snipped...> > > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? > > Thanks! > -Stumpy (aka Greg) I'm googlesmart when it comes to Python. I used to know C. I practically knew what the assembly language would look like when it compiled. There was no internet and searching through books can be really laborious so you almost had to really know it to use it. But with Python, I don't start from first principles. I google what I want to do, download the appropriate packages and read through the examples. It can be really frustrating when you get bugs, because you don't know what's going on deep down in the code. -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
Ram) (Stefan Ram) Greg Tibbetwrites: >I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit >of Java and trying to learn this new-fangled Python language! Which actually is older than Java. >def ellipse(self, xy, fill=None, outline=None): >"""Draw an ellipse.""" >ink, fill = self._getink(outline, fill) >if fill is not None: >self.draw.draw_ellipse(xy, fill, 1) ><...snipped...> >ellipse() uses the method self.draw.draw_ellipse() Okay, fine... >but WHERE is draw_ellipse defined?? What magic is happening there? Depends on the nature of â»selfâ«. Usually, the answer would be that it's defined in a superclass. But with Python, one could also decrypt a string and then feed the result to â»execâ« to dynamically add methods to an object whose source code is well hidden. Looking into the matter, it turns out, however, ... â»_draw_ellipseâ« is defined in the language C in the file â»_imaging.câ« and then mapped to â»draw_ellipseâ« via PyMethodDef which is part of Python's C API. -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
On 26/11/2017 09:09, Greg Tibbet wrote: > > I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit > of Java and trying to learn this new-fangled Python language! > > I've got a small program that uses PIL to create an image, draw some > primitives (rectanges, ellipses, etc...) and save it. Works fine... > no issues. > > I've found in the past, the best way to "really learn" the language > was to "dig into the guts" and understand it,.. I thought I was making > progress, but when looking into the PIL library to see what's going on > behind the scenes, I find the following code in ImageDraw.py > > def ellipse(self, xy, fill=None, outline=None): > """Draw an ellipse.""" > ink, fill = self._getink(outline, fill) > if fill is not None: > self.draw.draw_ellipse(xy, fill, 1) > <...snipped...> > > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? Python isn't a very pure language in that much of the functionality that looks like it should be written in Python (because you imported a module just like you import any Python module), actually is written in something else, as has been pointed out. It's reasonable that some things need to be implemented using some foreign functions. But the boundary between Python and non-Python is blurred. Take this program: import sys and try and find sys.py in your installation. (This is an obstacle if, for example, you're thinking of implementing a Python interpreter. In theory, once you have it working, it should run any .py program. But the critical modules it needs don't have .py source code. And the interface to those non-Python functions isn't defined with special byte-code instructions. (It will be done /via/ those instructions, but the magic needed is on the other side of them. Calling into sys.fn() uses the same CALL_FUNCTION byte-code as calling into a regular Python function.) As I said, it's not pure. More of a jungle as you've found out.) -- bartc -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
On Sunday, November 26, 2017 at 4:10:12 AM UTC-5, Greg Tibbet wrote: > I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit > of Java and trying to learn this new-fangled Python language! > > I've got a small program that uses PIL to create an image, draw some > primitives (rectanges, ellipses, etc...) and save it. Works fine... > no issues. > > I've found in the past, the best way to "really learn" the language > was to "dig into the guts" and understand it,.. I thought I was making > progress, but when looking into the PIL library to see what's going on > behind the scenes, I find the following code in ImageDraw.py > > def ellipse(self, xy, fill=None, outline=None): > """Draw an ellipse.""" > ink, fill = self._getink(outline, fill) > if fill is not None: > self.draw.draw_ellipse(xy, fill, 1) > <...snipped...> > > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? > > Thanks! > -Stumpy (aka Greg) I'm googlesmart when it comes to Python. I used to know C. I practically knew what the assembly language would look like when it compiled. There was no internet and searching through books can be really laborious so you almost had to really know it to use it. But with Python, I don't start from first principles. I google what I want to do, download the appropriate packages and read through the examples. It can be really frustrating when you get bugs, because you don't know what's going on deep down in the code. -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
On 26Nov2017 01:09, Greg Tibbetwrote: >I've got a small program that uses PIL to create an image, draw some >primitives (rectanges, ellipses, etc...) and save it. Works fine... >no issues. > >I've found in the past, the best way to "really learn" the language >was to "dig into the guts" and understand it,.. I thought I was making >progress, but when looking into the PIL library to see what's going on >behind the scenes, I find the following code in ImageDraw.py > >def ellipse(self, xy, fill=None, outline=None): >"""Draw an ellipse.""" >ink, fill = self._getink(outline, fill) >if fill is not None: >self.draw.draw_ellipse(xy, fill, 1) ><...snipped...> > >ellipse() uses the method self.draw.draw_ellipse() Okay, fine... >but WHERE is draw_ellipse defined?? What magic is happening there? >I've searched the entire PIL directory tree, and the ONLY two places >draw_ellipse is mentioned are right there in the ellipse() function... >WHAT am I missing?? "ellispse()" is a method in the ImageDraw class. Looking at the __init__ method of that class we see: self.draw = Image.core.draw(self.im, blend) so "self.draw" in your code above is the result of "Image.core.draw(self.im, blend)". "Image" is the Image module imported at the top of ImageDraw.py. So we hop over to Image.py, which has this code: try: # If the _imaging C module is not present, you can still use # the "open" function to identify files, but you cannot load # them. Note that other modules should not refer to _imaging # directly; import Image and use the Image.core variable instead. import _imaging core = _imaging del _imaging except ImportError, v: core = _imaging_not_installed() if str(v)[:20] == "Module use of python" and warnings: # The _imaging C module is present, but not compiled for # the right version (windows only). Print a warning, if # possible. warnings.warn( "The _imaging extension was built for another version " "of Python; most PIL functions will be disabled", RuntimeWarning ) Now the import works (because you'd get exceptions otherwise), so code which matters is that the top of that: import _imaging core = _imaging del _imaging So "core" is a reference to the "_imaging" module (and the name "_imaging" has been discarded). So... The name Image.core is now a reference to that module. So back in ImageDraw, the call to "Image.core.draw()" called the function "draw" from the _imaging module, which presumably returns some kind of drawing object, and _that_ object has a "draw_ellispe" method. Now, dynamic languages like Python don't lend themselves to screamingly fast compute, so expensive stuff like drawing graphics is usually done by hooking into special purpose libraries written in C or something that compiles to efficient machine level code (C++, Go, what have you). You can ship C code with Python to be compiled on the target and presented to Python as a library, and by convention such modules are named with a leading underscore. So we can expect that _imaging is a C code module. And if you go up a level you'll find _imaging.c, with a draw_ellipse function inside it. Cheers, Cameron Simpson (formerly c...@zip.com.au) -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
Greg Tibbet wrote: > > I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit > of Java and trying to learn this new-fangled Python language! > > I've got a small program that uses PIL to create an image, draw some > primitives (rectanges, ellipses, etc...) and save it. Works fine... > no issues. > > I've found in the past, the best way to "really learn" the language > was to "dig into the guts" and understand it,.. I thought I was making > progress, but when looking into the PIL library to see what's going on > behind the scenes, I find the following code in ImageDraw.py > > def ellipse(self, xy, fill=None, outline=None): > """Draw an ellipse.""" > ink, fill = self._getink(outline, fill) > if fill is not None: > self.draw.draw_ellipse(xy, fill, 1) > <...snipped...> > > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? The first argument of ellipse(), self is a strong hint that it is a method. If you look into ImageDraw.py, located at >>> import PIL.ImageDraw >>> PIL.ImageDraw.__file__ '/usr/lib/python3/dist-packages/PIL/ImageDraw.py' on my machine, you'll find the class of the same name, ImageDraw, and in its initializer there's an assignment to the self.draw attribute: self.draw = Image.core.draw(self.im, blend) Image is another module >>> PIL.ImageDraw.Image and if you look for the name 'core' in that module you'll find from PIL import _imaging as core >>> PIL.ImageDraw.Image.core That would be the right time to say "Argh" with any number of exclamation marks for those who don't know C, but since you are familiar with that language there's nothing to stop you from downloading the PIL (or rather the pillow) source and look into the implementation. Once you have the complete code https://pypi.python.org/pypi/Pillow/4.3.0 https://python-pillow.org/ https://github.com/python-pillow/Pillow $ git clone https://github.com/python-pillow/Pillow.git ...you can use traditional tools: $ find Pillow/ -name \*.c -print0 | xargs -0 grep draw_ellipse Pillow/_imaging.c:_draw_ellipse(ImagingDrawObject* self, PyObject* args) Pillow/_imaging.c:{"draw_ellipse", (PyCFunction)_draw_ellipse, 1}, Rinse and repeat ;) -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
Greg Tibbet wrote: > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? The C sources. Not all of PIL is ritten in Python. draw_ellipse() is a thin wrapper for ImagingDrawEllips(). See PIL/_imaging.c: https://github.com/python-pillow/Pillow/_imaging.c Which again is in libimage/Draw.c: https://github.com/python-pillow/Pillow/blob/13d84993717cffd64a2e1d7e3e6edb1 85973d559/libImaging/Draw.c calling ellipse(). -- --gv -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
Greg Tibbetwrites: >I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit >of Java and trying to learn this new-fangled Python language! Which actually is older than Java. >def ellipse(self, xy, fill=None, outline=None): >"""Draw an ellipse.""" >ink, fill = self._getink(outline, fill) >if fill is not None: >self.draw.draw_ellipse(xy, fill, 1) ><...snipped...> >ellipse() uses the method self.draw.draw_ellipse() Okay, fine... >but WHERE is draw_ellipse defined?? What magic is happening there? Depends on the nature of â»selfâ«. Usually, the answer would be that it's defined in a superclass. But with Python, one could also decrypt a string and then feed the result to â»execâ« to dynamically add methods to an object whose source code is well hidden. Looking into the matter, it turns out, however, ... â»_draw_ellipseâ« is defined in the language C in the file â»_imaging.câ« and then mapped to â»draw_ellipseâ« via PyMethodDef which is part of Python's C API. -- https://mail.python.org/mailman/listinfo/python-list
Argh!! Can't wrap my head around this Python stuff!
I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit of Java and trying to learn this new-fangled Python language! I've got a small program that uses PIL to create an image, draw some primitives (rectanges, ellipses, etc...) and save it. Works fine... no issues. I've found in the past, the best way to "really learn" the language was to "dig into the guts" and understand it,.. I thought I was making progress, but when looking into the PIL library to see what's going on behind the scenes, I find the following code in ImageDraw.py def ellipse(self, xy, fill=None, outline=None): """Draw an ellipse.""" ink, fill = self._getink(outline, fill) if fill is not None: self.draw.draw_ellipse(xy, fill, 1) <...snipped...> ellipse() uses the method self.draw.draw_ellipse() Okay, fine... but WHERE is draw_ellipse defined?? What magic is happening there? I've searched the entire PIL directory tree, and the ONLY two places draw_ellipse is mentioned are right there in the ellipse() function... WHAT am I missing?? Thanks! -Stumpy (aka Greg) -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
On 26/11/2017 09:09, Greg Tibbet wrote: > > I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit > of Java and trying to learn this new-fangled Python language! > > I've got a small program that uses PIL to create an image, draw some > primitives (rectanges, ellipses, etc...) and save it. Works fine... > no issues. > > I've found in the past, the best way to "really learn" the language > was to "dig into the guts" and understand it,.. I thought I was making > progress, but when looking into the PIL library to see what's going on > behind the scenes, I find the following code in ImageDraw.py > > def ellipse(self, xy, fill=None, outline=None): > """Draw an ellipse.""" > ink, fill = self._getink(outline, fill) > if fill is not None: > self.draw.draw_ellipse(xy, fill, 1) > <...snipped...> > > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? Python isn't a very pure language in that much of the functionality that looks like it should be written in Python (because you imported a module just like you import any Python module), actually is written in something else, as has been pointed out. It's reasonable that some things need to be implemented using some foreign functions. But the boundary between Python and non-Python is blurred. Take this program: import sys and try and find sys.py in your installation. (This is an obstacle if, for example, you're thinking of implementing a Python interpreter. In theory, once you have it working, it should run any .py program. But the critical modules it needs don't have .py source code. And the interface to those non-Python functions isn't defined with special byte-code instructions. (It will be done /via/ those instructions, but the magic needed is on the other side of them. Calling into sys.fn() uses the same CALL_FUNCTION byte-code as calling into a regular Python function.) As I said, it's not pure. More of a jungle as you've found out.) -- bartc -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
Greg Tibbet wrote: > > I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit > of Java and trying to learn this new-fangled Python language! > > I've got a small program that uses PIL to create an image, draw some > primitives (rectanges, ellipses, etc...) and save it. Works fine... > no issues. > > I've found in the past, the best way to "really learn" the language > was to "dig into the guts" and understand it,.. I thought I was making > progress, but when looking into the PIL library to see what's going on > behind the scenes, I find the following code in ImageDraw.py > > def ellipse(self, xy, fill=None, outline=None): > """Draw an ellipse.""" > ink, fill = self._getink(outline, fill) > if fill is not None: > self.draw.draw_ellipse(xy, fill, 1) > <...snipped...> > > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? The first argument of ellipse(), self is a strong hint that it is a method. If you look into ImageDraw.py, located at >>> import PIL.ImageDraw >>> PIL.ImageDraw.__file__ '/usr/lib/python3/dist-packages/PIL/ImageDraw.py' on my machine, you'll find the class of the same name, ImageDraw, and in its initializer there's an assignment to the self.draw attribute: self.draw = Image.core.draw(self.im, blend) Image is another module >>> PIL.ImageDraw.Image and if you look for the name 'core' in that module you'll find from PIL import _imaging as core >>> PIL.ImageDraw.Image.core That would be the right time to say "Argh" with any number of exclamation marks for those who don't know C, but since you are familiar with that language there's nothing to stop you from downloading the PIL (or rather the pillow) source and look into the implementation. Once you have the complete code https://pypi.python.org/pypi/Pillow/4.3.0 https://python-pillow.org/ https://github.com/python-pillow/Pillow $ git clone https://github.com/python-pillow/Pillow.git ...you can use traditional tools: $ find Pillow/ -name \*.c -print0 | xargs -0 grep draw_ellipse Pillow/_imaging.c:_draw_ellipse(ImagingDrawObject* self, PyObject* args) Pillow/_imaging.c:{"draw_ellipse", (PyCFunction)_draw_ellipse, 1}, Rinse and repeat ;) -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
On 26Nov2017 01:09, Greg Tibbetwrote: >I've got a small program that uses PIL to create an image, draw some >primitives (rectanges, ellipses, etc...) and save it. Works fine... >no issues. > >I've found in the past, the best way to "really learn" the language >was to "dig into the guts" and understand it,.. I thought I was making >progress, but when looking into the PIL library to see what's going on >behind the scenes, I find the following code in ImageDraw.py > >def ellipse(self, xy, fill=None, outline=None): >"""Draw an ellipse.""" >ink, fill = self._getink(outline, fill) >if fill is not None: >self.draw.draw_ellipse(xy, fill, 1) ><...snipped...> > >ellipse() uses the method self.draw.draw_ellipse() Okay, fine... >but WHERE is draw_ellipse defined?? What magic is happening there? >I've searched the entire PIL directory tree, and the ONLY two places >draw_ellipse is mentioned are right there in the ellipse() function... >WHAT am I missing?? "ellispse()" is a method in the ImageDraw class. Looking at the __init__ method of that class we see: self.draw = Image.core.draw(self.im, blend) so "self.draw" in your code above is the result of "Image.core.draw(self.im, blend)". "Image" is the Image module imported at the top of ImageDraw.py. So we hop over to Image.py, which has this code: try: # If the _imaging C module is not present, you can still use # the "open" function to identify files, but you cannot load # them. Note that other modules should not refer to _imaging # directly; import Image and use the Image.core variable instead. import _imaging core = _imaging del _imaging except ImportError, v: core = _imaging_not_installed() if str(v)[:20] == "Module use of python" and warnings: # The _imaging C module is present, but not compiled for # the right version (windows only). Print a warning, if # possible. warnings.warn( "The _imaging extension was built for another version " "of Python; most PIL functions will be disabled", RuntimeWarning ) Now the import works (because you'd get exceptions otherwise), so code which matters is that the top of that: import _imaging core = _imaging del _imaging So "core" is a reference to the "_imaging" module (and the name "_imaging" has been discarded). So... The name Image.core is now a reference to that module. So back in ImageDraw, the call to "Image.core.draw()" called the function "draw" from the _imaging module, which presumably returns some kind of drawing object, and _that_ object has a "draw_ellispe" method. Now, dynamic languages like Python don't lend themselves to screamingly fast compute, so expensive stuff like drawing graphics is usually done by hooking into special purpose libraries written in C or something that compiles to efficient machine level code (C++, Go, what have you). You can ship C code with Python to be compiled on the target and presented to Python as a library, and by convention such modules are named with a leading underscore. So we can expect that _imaging is a C code module. And if you go up a level you'll find _imaging.c, with a draw_ellipse function inside it. Cheers, Cameron Simpson (formerly c...@zip.com.au) -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
On Sunday, November 26, 2017 at 4:10:12 AM UTC-5, Greg Tibbet wrote: > I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit > of Java and trying to learn this new-fangled Python language! > > I've got a small program that uses PIL to create an image, draw some > primitives (rectanges, ellipses, etc...) and save it. Works fine... > no issues. > > I've found in the past, the best way to "really learn" the language > was to "dig into the guts" and understand it,.. I thought I was making > progress, but when looking into the PIL library to see what's going on > behind the scenes, I find the following code in ImageDraw.py > > def ellipse(self, xy, fill=None, outline=None): > """Draw an ellipse.""" > ink, fill = self._getink(outline, fill) > if fill is not None: > self.draw.draw_ellipse(xy, fill, 1) > <...snipped...> > > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? > > Thanks! > -Stumpy (aka Greg) I'm googlesmart when it comes to Python. I used to know C. I practically knew what the assembly language would look like when it compiled. There was no internet and searching through books can be really laborious so you almost had to really know it to use it. But with Python, I don't start from first principles. I google what I want to do, download the appropriate packages and read through the examples. It can be really frustrating when you get bugs, because you don't know what's going on deep down in the code. -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
Greg Tibbet wrote: > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? The C sources. Not all of PIL is ritten in Python. draw_ellipse() is a thin wrapper for ImagingDrawEllips(). See PIL/_imaging.c: https://github.com/python-pillow/Pillow/_imaging.c Which again is in libimage/Draw.c: https://github.com/python-pillow/Pillow/blob/13d84993717cffd64a2e1d7e3e6edb1 85973d559/libImaging/Draw.c calling ellipse(). -- --gv -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
Greg Tibbetwrites: >I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit >of Java and trying to learn this new-fangled Python language! Which actually is older than Java. >def ellipse(self, xy, fill=None, outline=None): >"""Draw an ellipse.""" >ink, fill = self._getink(outline, fill) >if fill is not None: >self.draw.draw_ellipse(xy, fill, 1) ><...snipped...> >ellipse() uses the method self.draw.draw_ellipse() Okay, fine... >but WHERE is draw_ellipse defined?? What magic is happening there? Depends on the nature of â»selfâ«. Usually, the answer would be that it's defined in a superclass. But with Python, one could also decrypt a string and then feed the result to â»execâ« to dynamically add methods to an object whose source code is well hidden. Looking into the matter, it turns out, however, ... â»_draw_ellipseâ« is defined in the language C in the file â»_imaging.câ« and then mapped to â»draw_ellipseâ« via PyMethodDef which is part of Python's C API. -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
Le 26/11/17 Ä 10:09, Greg Tibbet a ÄCcritâ : > I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit > of Java and trying to learn this new-fangled Python language! > > I've got a small program that uses PIL to create an image, draw some > primitives (rectanges, ellipses, etc...) and save it. Works fine... > no issues. > > I've found in the past, the best way to "really learn" the language > was to "dig into the guts" and understand it,.. I thought I was making > progress, but when looking into the PIL library to see what's going on > behind the scenes, I find the following code in ImageDraw.py > > def ellipse(self, xy, fill=None, outline=None): > """Draw an ellipse.""" > ink, fill = self._getink(outline, fill) > if fill is not None: > self.draw.draw_ellipse(xy, fill, 1) > <...snipped...> > > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? > > Thanks! > -Stumpy (aka Greg) Hi, It's not defined in Python module but in Pillow-3.5/Pillow-master/libImaging/Draw.c ... line 748 Vincent -- https://mail.python.org/mailman/listinfo/python-list
Argh!! Can't wrap my head around this Python stuff!
I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit of Java and trying to learn this new-fangled Python language! I've got a small program that uses PIL to create an image, draw some primitives (rectanges, ellipses, etc...) and save it. Works fine... no issues. I've found in the past, the best way to "really learn" the language was to "dig into the guts" and understand it,.. I thought I was making progress, but when looking into the PIL library to see what's going on behind the scenes, I find the following code in ImageDraw.py def ellipse(self, xy, fill=None, outline=None): """Draw an ellipse.""" ink, fill = self._getink(outline, fill) if fill is not None: self.draw.draw_ellipse(xy, fill, 1) <...snipped...> ellipse() uses the method self.draw.draw_ellipse() Okay, fine... but WHERE is draw_ellipse defined?? What magic is happening there? I've searched the entire PIL directory tree, and the ONLY two places draw_ellipse is mentioned are right there in the ellipse() function... WHAT am I missing?? Thanks! -Stumpy (aka Greg) -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
On Sunday, November 26, 2017 at 4:10:12 AM UTC-5, Greg Tibbet wrote: > I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit > of Java and trying to learn this new-fangled Python language! > > I've got a small program that uses PIL to create an image, draw some > primitives (rectanges, ellipses, etc...) and save it. Works fine... > no issues. > > I've found in the past, the best way to "really learn" the language > was to "dig into the guts" and understand it,.. I thought I was making > progress, but when looking into the PIL library to see what's going on > behind the scenes, I find the following code in ImageDraw.py > > def ellipse(self, xy, fill=None, outline=None): > """Draw an ellipse.""" > ink, fill = self._getink(outline, fill) > if fill is not None: > self.draw.draw_ellipse(xy, fill, 1) > <...snipped...> > > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? > > Thanks! > -Stumpy (aka Greg) I'm googlesmart when it comes to Python. I used to know C. I practically knew what the assembly language would look like when it compiled. There was no internet and searching through books can be really laborious so you almost had to really know it to use it. But with Python, I don't start from first principles. I google what I want to do, download the appropriate packages and read through the examples. It can be really frustrating when you get bugs, because you don't know what's going on deep down in the code. -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
Le 26/11/17 à 10:09, Greg Tibbet a écrit : I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit of Java and trying to learn this new-fangled Python language! I've got a small program that uses PIL to create an image, draw some primitives (rectanges, ellipses, etc...) and save it. Works fine... no issues. I've found in the past, the best way to "really learn" the language was to "dig into the guts" and understand it,.. I thought I was making progress, but when looking into the PIL library to see what's going on behind the scenes, I find the following code in ImageDraw.py def ellipse(self, xy, fill=None, outline=None): """Draw an ellipse.""" ink, fill = self._getink(outline, fill) if fill is not None: self.draw.draw_ellipse(xy, fill, 1) <...snipped...> ellipse() uses the method self.draw.draw_ellipse() Okay, fine... but WHERE is draw_ellipse defined?? What magic is happening there? I've searched the entire PIL directory tree, and the ONLY two places draw_ellipse is mentioned are right there in the ellipse() function... WHAT am I missing?? Thanks! -Stumpy (aka Greg) Hi, It's not defined in Python module but in Pillow-3.5/Pillow-master/libImaging/Draw.c ... line 748 Vincent -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
On 26/11/2017 09:09, Greg Tibbet wrote: I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit of Java and trying to learn this new-fangled Python language! I've got a small program that uses PIL to create an image, draw some primitives (rectanges, ellipses, etc...) and save it. Works fine... no issues. I've found in the past, the best way to "really learn" the language was to "dig into the guts" and understand it,.. I thought I was making progress, but when looking into the PIL library to see what's going on behind the scenes, I find the following code in ImageDraw.py def ellipse(self, xy, fill=None, outline=None): """Draw an ellipse.""" ink, fill = self._getink(outline, fill) if fill is not None: self.draw.draw_ellipse(xy, fill, 1) <...snipped...> ellipse() uses the method self.draw.draw_ellipse() Okay, fine... but WHERE is draw_ellipse defined?? What magic is happening there? I've searched the entire PIL directory tree, and the ONLY two places draw_ellipse is mentioned are right there in the ellipse() function... WHAT am I missing?? Python isn't a very pure language in that much of the functionality that looks like it should be written in Python (because you imported a module just like you import any Python module), actually is written in something else, as has been pointed out. It's reasonable that some things need to be implemented using some foreign functions. But the boundary between Python and non-Python is blurred. Take this program: import sys and try and find sys.py in your installation. (This is an obstacle if, for example, you're thinking of implementing a Python interpreter. In theory, once you have it working, it should run any .py program. But the critical modules it needs don't have .py source code. And the interface to those non-Python functions isn't defined with special byte-code instructions. (It will be done /via/ those instructions, but the magic needed is on the other side of them. Calling into sys.fn() uses the same CALL_FUNCTION byte-code as calling into a regular Python function.) As I said, it's not pure. More of a jungle as you've found out.) -- bartc -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
On 26Nov2017 01:09, Greg Tibbetwrote: I've got a small program that uses PIL to create an image, draw some primitives (rectanges, ellipses, etc...) and save it. Works fine... no issues. I've found in the past, the best way to "really learn" the language was to "dig into the guts" and understand it,.. I thought I was making progress, but when looking into the PIL library to see what's going on behind the scenes, I find the following code in ImageDraw.py def ellipse(self, xy, fill=None, outline=None): """Draw an ellipse.""" ink, fill = self._getink(outline, fill) if fill is not None: self.draw.draw_ellipse(xy, fill, 1) <...snipped...> ellipse() uses the method self.draw.draw_ellipse() Okay, fine... but WHERE is draw_ellipse defined?? What magic is happening there? I've searched the entire PIL directory tree, and the ONLY two places draw_ellipse is mentioned are right there in the ellipse() function... WHAT am I missing?? "ellispse()" is a method in the ImageDraw class. Looking at the __init__ method of that class we see: self.draw = Image.core.draw(self.im, blend) so "self.draw" in your code above is the result of "Image.core.draw(self.im, blend)". "Image" is the Image module imported at the top of ImageDraw.py. So we hop over to Image.py, which has this code: try: # If the _imaging C module is not present, you can still use # the "open" function to identify files, but you cannot load # them. Note that other modules should not refer to _imaging # directly; import Image and use the Image.core variable instead. import _imaging core = _imaging del _imaging except ImportError, v: core = _imaging_not_installed() if str(v)[:20] == "Module use of python" and warnings: # The _imaging C module is present, but not compiled for # the right version (windows only). Print a warning, if # possible. warnings.warn( "The _imaging extension was built for another version " "of Python; most PIL functions will be disabled", RuntimeWarning ) Now the import works (because you'd get exceptions otherwise), so code which matters is that the top of that: import _imaging core = _imaging del _imaging So "core" is a reference to the "_imaging" module (and the name "_imaging" has been discarded). So... The name Image.core is now a reference to that module. So back in ImageDraw, the call to "Image.core.draw()" called the function "draw" from the _imaging module, which presumably returns some kind of drawing object, and _that_ object has a "draw_ellispe" method. Now, dynamic languages like Python don't lend themselves to screamingly fast compute, so expensive stuff like drawing graphics is usually done by hooking into special purpose libraries written in C or something that compiles to efficient machine level code (C++, Go, what have you). You can ship C code with Python to be compiled on the target and presented to Python as a library, and by convention such modules are named with a leading underscore. So we can expect that _imaging is a C code module. And if you go up a level you'll find _imaging.c, with a draw_ellipse function inside it. Cheers, Cameron Simpson (formerly c...@zip.com.au) -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
Greg Tibbet wrote: > > I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit > of Java and trying to learn this new-fangled Python language! > > I've got a small program that uses PIL to create an image, draw some > primitives (rectanges, ellipses, etc...) and save it. Works fine... > no issues. > > I've found in the past, the best way to "really learn" the language > was to "dig into the guts" and understand it,.. I thought I was making > progress, but when looking into the PIL library to see what's going on > behind the scenes, I find the following code in ImageDraw.py > > def ellipse(self, xy, fill=None, outline=None): > """Draw an ellipse.""" > ink, fill = self._getink(outline, fill) > if fill is not None: > self.draw.draw_ellipse(xy, fill, 1) > <...snipped...> > > ellipse() uses the method self.draw.draw_ellipse() Okay, fine... > but WHERE is draw_ellipse defined?? What magic is happening there? > I've searched the entire PIL directory tree, and the ONLY two places > draw_ellipse is mentioned are right there in the ellipse() function... > WHAT am I missing?? The first argument of ellipse(), self is a strong hint that it is a method. If you look into ImageDraw.py, located at >>> import PIL.ImageDraw >>> PIL.ImageDraw.__file__ '/usr/lib/python3/dist-packages/PIL/ImageDraw.py' on my machine, you'll find the class of the same name, ImageDraw, and in its initializer there's an assignment to the self.draw attribute: self.draw = Image.core.draw(self.im, blend) Image is another module >>> PIL.ImageDraw.Image and if you look for the name 'core' in that module you'll find from PIL import _imaging as core >>> PIL.ImageDraw.Image.core That would be the right time to say "Argh" with any number of exclamation marks for those who don't know C, but since you are familiar with that language there's nothing to stop you from downloading the PIL (or rather the pillow) source and look into the implementation. Once you have the complete code https://pypi.python.org/pypi/Pillow/4.3.0 https://python-pillow.org/ https://github.com/python-pillow/Pillow $ git clone https://github.com/python-pillow/Pillow.git ...you can use traditional tools: $ find Pillow/ -name \*.c -print0 | xargs -0 grep draw_ellipse Pillow/_imaging.c:_draw_ellipse(ImagingDrawObject* self, PyObject* args) Pillow/_imaging.c:{"draw_ellipse", (PyCFunction)_draw_ellipse, 1}, Rinse and repeat ;) -- https://mail.python.org/mailman/listinfo/python-list
Re: Argh!! Can't wrap my head around this Python stuff!
Greg Tibbet wrote: ellipse() uses the method self.draw.draw_ellipse() Okay, fine... but WHERE is draw_ellipse defined?? What magic is happening there? I've searched the entire PIL directory tree, and the ONLY two places draw_ellipse is mentioned are right there in the ellipse() function... WHAT am I missing?? The C sources. Not all of PIL is ritten in Python. draw_ellipse() is a thin wrapper for ImagingDrawEllips(). See PIL/_imaging.c: https://github.com/python-pillow/Pillow/_imaging.c Which again is in libimage/Draw.c: https://github.com/python-pillow/Pillow/blob/13d84993717cffd64a2e1d7e3e6edb185973d559/libImaging/Draw.c calling ellipse(). -- --gv -- https://mail.python.org/mailman/listinfo/python-list
Argh!! Can't wrap my head around this Python stuff!
I'm an old timer, have programmed in Fortran, C, C++, Perl, and a bit of Java and trying to learn this new-fangled Python language! I've got a small program that uses PIL to create an image, draw some primitives (rectanges, ellipses, etc...) and save it. Works fine... no issues. I've found in the past, the best way to "really learn" the language was to "dig into the guts" and understand it,.. I thought I was making progress, but when looking into the PIL library to see what's going on behind the scenes, I find the following code in ImageDraw.py def ellipse(self, xy, fill=None, outline=None): """Draw an ellipse.""" ink, fill = self._getink(outline, fill) if fill is not None: self.draw.draw_ellipse(xy, fill, 1) <...snipped...> ellipse() uses the method self.draw.draw_ellipse() Okay, fine... but WHERE is draw_ellipse defined?? What magic is happening there? I've searched the entire PIL directory tree, and the ONLY two places draw_ellipse is mentioned are right there in the ellipse() function... WHAT am I missing?? Thanks! -Stumpy (aka Greg) -- https://mail.python.org/mailman/listinfo/python-list