Re: [Tutor] Best solution to modifying code within a distributed library

2018-09-29 Thread Steven D'Aprano
On Wed, Sep 19, 2018 at 05:47:07PM -0400, Chip Wachob wrote:

[...]
> I've looked through the code in the FT232H.py file and found what I
> believe to be the culprit.
> 
> I would like to comment out line 340 (self.mpsse_write_gpio()) to
> prove that this is what is causing glitches that I do not want.

Since you don't have the source file, it might work to monkey patch the 
library. Beware: monkey patching is an advanced technique, hard to get 
right. But when it works, it can work very well.

https://en.wikipedia.org/wiki/Monkey_patch

Not to be confused with this:
https://en.wikipedia.org/wiki/Scratch_monkey


Let me assume that self.mpsse_write_gpio is *only* called from that one 
line 340, and nowhere else. If that's the case, you take your code which 
might look something like this:

import FT232H
obj = FT232H.SomeClass()  # initialise an instance
obj.run()

(let's say), and add a monkey patch that replaces the suspect 
mpsse_write_gpio method with a Do Nothing function:


import FT232H
obj = FT232H.SomeClass()  # initialise an instance
obj.mpsse_write_gpio = lambda self: None
obj.run()


The beauty of this is you don't need the source code! It all happens 
with the live code in the interpreter.

On the other hand, it might be that the suspect method is called from 
all over the place, and you only want to patch that *one* call on a 
single line. That's much harder and will require much ingenuity to 
solve. At that point, you might be better of just getting the source 
code and editing it.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best solution to modifying code within a distributed library

2018-09-23 Thread Chip Wachob
Looks like I'm golden in this regard.  My first path element is ''
which is what I'd want if I'm including a modified library.

Thank you,



On Thu, Sep 20, 2018 at 6:14 PM, Mats Wichmann  wrote:
> On 09/19/2018 09:59 PM, Chip Wachob wrote:
>> Mats,
>>
>> Silly question here..
>>
>> But after using the git clone command, I've got a directory of the
>> Adafruit project in the same directory as my project.
>>
>> When I import the library, will I get the 'installed' library, or do I
>> get the library that is in the project directory?
>>
>> If I have to specify which library to use, how is that done?
>
> you look at, and possibly modify, sys.path
>
>
>   >>> import sys
>   >>> sys.path
>   ['', '/usr/lib64/python36.zip', '/usr/lib64/python3.6',
> '/usr/lib64/python3.6/lib-dynload',
> '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages']
>
>
> the first element '' is the local directory, so with my sys.path, it
> would pick the local one first.
>
>
> if you wanted the opposite, that is be _sure_ you get the installed one,
> you could use a stanza something like this:
>
>
>savepath = sys.path
>sys.path = [path for path in sys.path if path.strip('.')]
>import foo
>sys.path = savepath
>
>
> but this is actually kind of tricky stuff, trying to deal with possibly
> two modules of the same name, so tread carefully.
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best solution to modifying code within a distributed library

2018-09-20 Thread Mats Wichmann
On 09/19/2018 09:59 PM, Chip Wachob wrote:
> Mats,
> 
> Silly question here..
> 
> But after using the git clone command, I've got a directory of the
> Adafruit project in the same directory as my project.
> 
> When I import the library, will I get the 'installed' library, or do I
> get the library that is in the project directory?
> 
> If I have to specify which library to use, how is that done?

you look at, and possibly modify, sys.path


  >>> import sys
  >>> sys.path
  ['', '/usr/lib64/python36.zip', '/usr/lib64/python3.6',
'/usr/lib64/python3.6/lib-dynload',
'/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages']


the first element '' is the local directory, so with my sys.path, it
would pick the local one first.


if you wanted the opposite, that is be _sure_ you get the installed one,
you could use a stanza something like this:


   savepath = sys.path
   sys.path = [path for path in sys.path if path.strip('.')]
   import foo
   sys.path = savepath


but this is actually kind of tricky stuff, trying to deal with possibly
two modules of the same name, so tread carefully.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best solution to modifying code within a distributed library

2018-09-19 Thread Chip Wachob
Mats,

Silly question here..

But after using the git clone command, I've got a directory of the
Adafruit project in the same directory as my project.

When I import the library, will I get the 'installed' library, or do I
get the library that is in the project directory?

If I have to specify which library to use, how is that done?

Thanks,


On Wed, Sep 19, 2018 at 7:51 PM, Mats Wichmann  wrote:
> On 09/19/2018 03:47 PM, Chip Wachob wrote:
>> Hello once again,
>>
>> I'm sure this is probably way outside my 'pay grade' but I would like
>> to try an experiment and I'm not sure how to go about it.
>>
>> I'm using the Adafruit FT232 libraries found here:
>>
>> https://github.com/adafruit/Adafruit_Python_GPIO/blob/master/Adafruit_GPIO/SPI.py
>>
>> I'm experiencing some wiggling of the IO lines when I configure the IO
>> pin direction.
>>
>> I've looked through the code in the FT232H.py file and found what I
>> believe to be the culprit.
>>
>> I would like to comment out line 340 (self.mpsse_write_gpio()) to
>> prove that this is what is causing glitches that I do not want.
>>
>> Using the .__file__ inside the interpreter I learned that the file is
>> located here on my machine:
>>
>> /usr/local/lib/python2.7/dist-packages/Adafruit_GPIO-1.0.3-py2.7.egg/Adafruit_GPIO/FT232H.pyc
>>
>> But obviously, this is a binary file.
>>
>> If I understand enough about Python, I believe that I need to edit the
>> FT232H.py file in the .egg file to implement the change.  There is
>> also likely some sort of compilation that needs to be done once the
>> change is made...
>>
>> BUT
>>
>> As I've also learned from our friend Google, one should not be editing
>> .egg files, etc.
>
> You'll want to get the original and work from there. You already know
> where it is - you've included the github link.
>
> It's hard to know how much needs to be explained here... roughly, in
> your project you want to clone the git tree, and make sure that's what
> your experiment is picking up.  That would start as:
>
> git clone https://github.com/adafruit/Adafruit_Python_GPIO.git
>
> or of you want to start with something you might want to create a github
> pull request to the maintainer, make sure you have a github account,
> click the fork button on the github page, then in your own account find
> the URL to give to "git clone" for your fork, and start from there.
>
> to do an experiment, the former ought to be enough, but "there are more
> details", depending on what you're familiar with as far as these tools.
>
> Do write back with more questions if you go down this path...
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best solution to modifying code within a distributed library

2018-09-19 Thread Chip Wachob
David,

I should have pointed out that I've already posted to the forums there
and there is only crickets.

So, I've taken it upon myself to attempt to solve it for myself.  But,
as I noted, I'm very very new to Python and the whole .egg, pip, git
thing, and that's what lead to my query here.

Mats,

Thanks for the pointer on the clone.  I think I'll take that approach
for now.  Since this is an experiment, this seems like the right path
to take.

Thanks to all for the information.


On Wed, Sep 19, 2018 at 10:35 PM, David Rock  wrote:
>
>> On Sep 19, 2018, at 18:51, Mats Wichmann  wrote:
>>
>> On 09/19/2018 03:47 PM, Chip Wachob wrote:
>>> Hello once again,
>>>
>>> I would like to comment out line 340 (self.mpsse_write_gpio()) to
>>> prove that this is what is causing glitches that I do not want.
>>>
>>
>> You'll want to get the original and work from there. You already know
>> where it is - you've included the github link.
>>
>
> There’s another option… Ask Adafruit directly.
>
> They are amazingly helpful and would be more than happy to hear if there’s a 
> potential issue with their code.
>
>
> —
> David Rock
> da...@graniteweb.com
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best solution to modifying code within a distributed library

2018-09-19 Thread David Rock

> On Sep 19, 2018, at 18:51, Mats Wichmann  wrote:
> 
> On 09/19/2018 03:47 PM, Chip Wachob wrote:
>> Hello once again,
>> 
>> I would like to comment out line 340 (self.mpsse_write_gpio()) to
>> prove that this is what is causing glitches that I do not want.
>> 
> 
> You'll want to get the original and work from there. You already know
> where it is - you've included the github link.
> 

There’s another option… Ask Adafruit directly.

They are amazingly helpful and would be more than happy to hear if there’s a 
potential issue with their code.


— 
David Rock
da...@graniteweb.com




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best solution to modifying code within a distributed library

2018-09-19 Thread Mats Wichmann
On 09/19/2018 03:47 PM, Chip Wachob wrote:
> Hello once again,
> 
> I'm sure this is probably way outside my 'pay grade' but I would like
> to try an experiment and I'm not sure how to go about it.
> 
> I'm using the Adafruit FT232 libraries found here:
> 
> https://github.com/adafruit/Adafruit_Python_GPIO/blob/master/Adafruit_GPIO/SPI.py
> 
> I'm experiencing some wiggling of the IO lines when I configure the IO
> pin direction.
> 
> I've looked through the code in the FT232H.py file and found what I
> believe to be the culprit.
> 
> I would like to comment out line 340 (self.mpsse_write_gpio()) to
> prove that this is what is causing glitches that I do not want.
> 
> Using the .__file__ inside the interpreter I learned that the file is
> located here on my machine:
> 
> /usr/local/lib/python2.7/dist-packages/Adafruit_GPIO-1.0.3-py2.7.egg/Adafruit_GPIO/FT232H.pyc
> 
> But obviously, this is a binary file.
> 
> If I understand enough about Python, I believe that I need to edit the
> FT232H.py file in the .egg file to implement the change.  There is
> also likely some sort of compilation that needs to be done once the
> change is made...
> 
> BUT
> 
> As I've also learned from our friend Google, one should not be editing
> .egg files, etc.

You'll want to get the original and work from there. You already know
where it is - you've included the github link.

It's hard to know how much needs to be explained here... roughly, in
your project you want to clone the git tree, and make sure that's what
your experiment is picking up.  That would start as:

git clone https://github.com/adafruit/Adafruit_Python_GPIO.git

or of you want to start with something you might want to create a github
pull request to the maintainer, make sure you have a github account,
click the fork button on the github page, then in your own account find
the URL to give to "git clone" for your fork, and start from there.

to do an experiment, the former ought to be enough, but "there are more
details", depending on what you're familiar with as far as these tools.

Do write back with more questions if you go down this path...



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Best solution to modifying code within a distributed library

2018-09-19 Thread Chip Wachob
Hello once again,

I'm sure this is probably way outside my 'pay grade' but I would like
to try an experiment and I'm not sure how to go about it.

I'm using the Adafruit FT232 libraries found here:

https://github.com/adafruit/Adafruit_Python_GPIO/blob/master/Adafruit_GPIO/SPI.py

I'm experiencing some wiggling of the IO lines when I configure the IO
pin direction.

I've looked through the code in the FT232H.py file and found what I
believe to be the culprit.

I would like to comment out line 340 (self.mpsse_write_gpio()) to
prove that this is what is causing glitches that I do not want.

Using the .__file__ inside the interpreter I learned that the file is
located here on my machine:

/usr/local/lib/python2.7/dist-packages/Adafruit_GPIO-1.0.3-py2.7.egg/Adafruit_GPIO/FT232H.pyc

But obviously, this is a binary file.

If I understand enough about Python, I believe that I need to edit the
FT232H.py file in the .egg file to implement the change.  There is
also likely some sort of compilation that needs to be done once the
change is made...

BUT

As I've also learned from our friend Google, one should not be editing
.egg files, etc.

So, can the experts out there point me to the proper method of
implementing the change?

IF, this ends up resolving my issues, then I presume that I will need
to create my own .egg file.  Is this accurate?

As always thank you in advance for your time,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor