Re: Access an object to which being bound

2020-05-27 Thread Abdur-Rahmaan Janhangeer
Greetings,

.append(self) is a hit idea

Updated the project with the button demo
https://github.com/Abdur-rahmaanJ/hooman/

Next i will be attempting to add a grid system.
I feel myself developing tkinter from scratch

Thanks for the help everybody ^^_

Kind Regards,

Abdur-Rahmaan Janhangeer
compileralchemy  | blog

github 
Mauritius


On Thu, May 28, 2020 at 2:45 AM DL Neil via Python-list <
python-list@python.org> wrote:

> @AR,
>
>
> On 28/05/20 8:41 AM, Chris Angelico wrote:
> > On Thu, May 28, 2020 at 6:27 AM Abdur-Rahmaan Janhangeer
> >  wrote:
> >>
> >> Thanks,
> >>
> >> Actually i want to keep a reference from B to all A
> >> instantiated like in the case of z
> >>
> >> I have class A and i want to call class B via A
> >>
> >> You can have
> >>
> >> def x(self, *args, **kwargs):
> >>  return A(*args, **kwargs)
> >>
> >> but was wondering if we could keep track while
> >> doing it via z = ...
> >>
> >
> > Okay, now I think I get what you're after. Let's simplify and clarify
> > things. Let me know if I'm misinterpreting.
> >
> > 1) You have a container class that can instantiate objects of a thing
> class
> > 2a) You wish for the container to be aware of all things it has created
> - OR -
> > 2b) You wish for the Thing to be aware of all containers that have
> created them
> > 3) You want this to happen automatically.
> >
> > I'm not sure which way round you're trying to do this, so I'll try to
> > answer both. Your Container (Hooman) can construct multiple Things
> > (Button), and other classes could also construct Things. So you need
> > some way to automatically register the Thing as you create it.
> >
> > The easiest way to do this would, I think, be to have your Container
> > subclass a utility class, and then use self.Button() instead of
> > Button(). That can take care of registering the button in some sort of
> > list, and then it can still return the button in case you need it for
> > something else.
> >
> > Would that work? Or am I completely off on my analysis?
>
> Here's some sample code, which may give you some ideas (intriguingly it
> comes from my playing-about with a project @Chris described (some time
> back), which includes the need to 'register' sub-components and to keep
> a count of them):
>
>
> # PSL
> import collections
> from itertools import count
>
>
> class Link():
> '''Contain original link, manipulate for presentation,
> and retain for later action.
> '''
>
> ID = count( 0 )
> instances = []
>
>
> def __init__( self, token:Token, )->None:
> '''Instantiate.'''
> self.token = token
>
> self.linkNR:int = next( self.ID )   #linkNR
> Link.instances.append( self )   #form a register
> of instances/links
>
> self.URL:str = ""
>
> ...more methods here - likely irrelevant to your needs...
>
>
> class LinksRegister( collections.UserList ):
> '''Links available for use within application.'''
>
> def __iter__( self ):
> '''Generator.'''
> for link in self.data:
> yield link
>
> def register_links( self, cls ):
> '''Register all links.'''
> self.data = cls.instances
>
>
> Once all of the Link() objects have been ascertained, we can make use of
> the class-attributes:
>
> links_register.register_links( Link )
>
> NB in this scenario it is only necessary to register once - all of the
> links at-once, cf registering each link as it is itself instantiated.
> Also, that each Link() object is not aware that it is/will be 'registered'!
>
>
> Later, when it is necessary to carry-out the same action on each of the
> objects, we can use the register's iterator/generator (as above).
>
>
> Critique/other ideas welcome...
> --
> Regards =dn
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Access an object to which being bound

2020-05-27 Thread DL Neil via Python-list

@AR,


On 28/05/20 8:41 AM, Chris Angelico wrote:

On Thu, May 28, 2020 at 6:27 AM Abdur-Rahmaan Janhangeer
 wrote:


Thanks,

Actually i want to keep a reference from B to all A
instantiated like in the case of z

I have class A and i want to call class B via A

You can have

def x(self, *args, **kwargs):
 return A(*args, **kwargs)

but was wondering if we could keep track while
doing it via z = ...



Okay, now I think I get what you're after. Let's simplify and clarify
things. Let me know if I'm misinterpreting.

1) You have a container class that can instantiate objects of a thing class
2a) You wish for the container to be aware of all things it has created - OR -
2b) You wish for the Thing to be aware of all containers that have created them
3) You want this to happen automatically.

I'm not sure which way round you're trying to do this, so I'll try to
answer both. Your Container (Hooman) can construct multiple Things
(Button), and other classes could also construct Things. So you need
some way to automatically register the Thing as you create it.

The easiest way to do this would, I think, be to have your Container
subclass a utility class, and then use self.Button() instead of
Button(). That can take care of registering the button in some sort of
list, and then it can still return the button in case you need it for
something else.

Would that work? Or am I completely off on my analysis?


Here's some sample code, which may give you some ideas (intriguingly it 
comes from my playing-about with a project @Chris described (some time 
back), which includes the need to 'register' sub-components and to keep 
a count of them):



# PSL
import collections
from itertools import count


class Link():
'''Contain original link, manipulate for presentation,
and retain for later action.
'''

ID = count( 0 )
instances = []


def __init__( self, token:Token, )->None:
'''Instantiate.'''
self.token = token

self.linkNR:int = next( self.ID )   #linkNR
Link.instances.append( self )   #form a register of 
instances/links

self.URL:str = ""

...more methods here - likely irrelevant to your needs...


class LinksRegister( collections.UserList ):
'''Links available for use within application.'''

def __iter__( self ):
'''Generator.'''
for link in self.data:
yield link

def register_links( self, cls ):
'''Register all links.'''
self.data = cls.instances


Once all of the Link() objects have been ascertained, we can make use of 
the class-attributes:


links_register.register_links( Link )

NB in this scenario it is only necessary to register once - all of the 
links at-once, cf registering each link as it is itself instantiated. 
Also, that each Link() object is not aware that it is/will be 'registered'!



Later, when it is necessary to carry-out the same action on each of the 
objects, we can use the register's iterator/generator (as above).



Critique/other ideas welcome...
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Access an object to which being bound

2020-05-27 Thread Abdur-Rahmaan Janhangeer
Hum yes a third class is a nice option!

btw managed to do it with those in Hooman

self._all_widgets = []

def button(self, *args, **kwargs):
b = Button(*args, **kwargs)
self._all_widgets.append(b)
return b

def update_all(self):
for widget in self._all_widgets:
widget.update()

Just in the drawing loop i have (hapi is a Hooman object)

for i in range(5):
x = hapi.button(
# params
)
hapi.update_all()

the update all adds a button in the array each frame, that's beyond the
question
but for creating buttons on the fly each frame was never a good option
(better define
before then update x and y in loop)

that's the only caveat to watch out if people create objects on the fly

Kind Regards,

Abdur-Rahmaan Janhangeer
compileralchemy  | blog

github 
Mauritius


On Thu, May 28, 2020 at 12:41 AM Chris Angelico  wrote:

> On Thu, May 28, 2020 at 6:27 AM Abdur-Rahmaan Janhangeer
>  wrote:
> >
> > Thanks,
> >
> > Actually i want to keep a reference from B to all A
> > instantiated like in the case of z
> >
> > I have class A and i want to call class B via A
> >
> > You can have
> >
> > def x(self, *args, **kwargs):
> > return A(*args, **kwargs)
> >
> > but was wondering if we could keep track while
> > doing it via z = ...
> >
>
> Okay, now I think I get what you're after. Let's simplify and clarify
> things. Let me know if I'm misinterpreting.
>
> 1) You have a container class that can instantiate objects of a thing class
> 2a) You wish for the container to be aware of all things it has created -
> OR -
> 2b) You wish for the Thing to be aware of all containers that have created
> them
> 3) You want this to happen automatically.
>
> I'm not sure which way round you're trying to do this, so I'll try to
> answer both. Your Container (Hooman) can construct multiple Things
> (Button), and other classes could also construct Things. So you need
> some way to automatically register the Thing as you create it.
>
> The easiest way to do this would, I think, be to have your Container
> subclass a utility class, and then use self.Button() instead of
> Button(). That can take care of registering the button in some sort of
> list, and then it can still return the button in case you need it for
> something else.
>
> Would that work? Or am I completely off on my analysis?
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Access an object to which being bound

2020-05-27 Thread Chris Angelico
On Thu, May 28, 2020 at 6:27 AM Abdur-Rahmaan Janhangeer
 wrote:
>
> Thanks,
>
> Actually i want to keep a reference from B to all A
> instantiated like in the case of z
>
> I have class A and i want to call class B via A
>
> You can have
>
> def x(self, *args, **kwargs):
> return A(*args, **kwargs)
>
> but was wondering if we could keep track while
> doing it via z = ...
>

Okay, now I think I get what you're after. Let's simplify and clarify
things. Let me know if I'm misinterpreting.

1) You have a container class that can instantiate objects of a thing class
2a) You wish for the container to be aware of all things it has created - OR -
2b) You wish for the Thing to be aware of all containers that have created them
3) You want this to happen automatically.

I'm not sure which way round you're trying to do this, so I'll try to
answer both. Your Container (Hooman) can construct multiple Things
(Button), and other classes could also construct Things. So you need
some way to automatically register the Thing as you create it.

The easiest way to do this would, I think, be to have your Container
subclass a utility class, and then use self.Button() instead of
Button(). That can take care of registering the button in some sort of
list, and then it can still return the button in case you need it for
something else.

Would that work? Or am I completely off on my analysis?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Access an object to which being bound

2020-05-27 Thread Abdur-Rahmaan Janhangeer
i have this:
https://github.com/Abdur-rahmaanJ/hooman/blob/master/hooman/hooman.py

someone PRed a Button class

i want the class button to be available to the class Hooman via
Human.button

self.button = Button

but button has update() which must be called individually

one way to update all buttons is to keep track of buttons instantiated
and calling update on each one. Wrap in a update_all () method

And please close your eyes to the wonderful mess

Kind Regards,

Abdur-Rahmaan Janhangeer
compileralchemy  | blog

github 
Mauritius


On Thu, May 28, 2020 at 12:15 AM Chris Angelico  wrote:

> On Thu, May 28, 2020 at 5:48 AM Abdur-Rahmaan Janhangeer
>  wrote:
> >
> > Greetings,
> >
> > Lets say i have
> >
> > class A:
> >   ...
> >
> > class B:
> >self.x = A
> >
> > then used
> >
> > b = B()
> > z = b.x()
> >
> > now how in A i get a reference to B when z is assigned to b.x?
> >
>
> Things are very tangled here. What exactly are you doing? Your code is
> incomplete at the moment, and I'm not sure what you're trying to
> achieve. As it currently stands, class A is completely stand-alone and
> will not have any way of knowing about B.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Access an object to which being bound

2020-05-27 Thread Abdur-Rahmaan Janhangeer
Thanks,

Actually i want to keep a reference from B to all A
instantiated like in the case of z

I have class A and i want to call class B via A

You can have

def x(self, *args, **kwargs):
return A(*args, **kwargs)

but was wondering if we could keep track while
doing it via z = ...

Kind Regards,

Abdur-Rahmaan Janhangeer
compileralchemy  | blog

github 
Mauritius


On Thu, May 28, 2020 at 12:15 AM Chris Angelico  wrote:

> On Thu, May 28, 2020 at 5:48 AM Abdur-Rahmaan Janhangeer
>  wrote:
> >
> > Greetings,
> >
> > Lets say i have
> >
> > class A:
> >   ...
> >
> > class B:
> >self.x = A
> >
> > then used
> >
> > b = B()
> > z = b.x()
> >
> > now how in A i get a reference to B when z is assigned to b.x?
> >
>
> Things are very tangled here. What exactly are you doing? Your code is
> incomplete at the moment, and I'm not sure what you're trying to
> achieve. As it currently stands, class A is completely stand-alone and
> will not have any way of knowing about B.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Access an object to which being bound

2020-05-27 Thread Chris Angelico
On Thu, May 28, 2020 at 5:48 AM Abdur-Rahmaan Janhangeer
 wrote:
>
> Greetings,
>
> Lets say i have
>
> class A:
>   ...
>
> class B:
>self.x = A
>
> then used
>
> b = B()
> z = b.x()
>
> now how in A i get a reference to B when z is assigned to b.x?
>

Things are very tangled here. What exactly are you doing? Your code is
incomplete at the moment, and I'm not sure what you're trying to
achieve. As it currently stands, class A is completely stand-alone and
will not have any way of knowing about B.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list