Hi,
what would be the most efficient way to do following?
I have a list of dictionaries taken from DB e.g.
dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
{'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
{'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
and list of object instances in memory(it's just for example)
which are looping within itself and testing particular hosts
memlist = [<instance 1>,<instance 2>]
memlist[0].id is 1 and memlist[0].host is google.com etc.
memlist[1].id is 9 and memlist[1].host is msn.com etc.
Now I want to add a new instance to memlist since id=3(in dblist) is not
in memlist.
How would you iterate through it and insert a new instance?
The result should be:
memlist = [<instance 1>,<instance 2>, <instance 3>]
memlist[0].id is 1 and memlist[0].host is google.com etc.
memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
memlist[2].id is 9 and memlist[2].host is msn.com etc.
Furthermore, I can have the opposite situation.
This time I need to remove from memlist a host which is not in dblist.
How would you do this?
The way how it works is that DBlist is loaded every 10 minutes and
compares with memlist.
The memlist should be the same as dblist.
Could you help me, please?
I'm working on my version of this but somebody might be quicker than me.
In case I have it done I will post it.
Thanks,
Lada
There is
and wt 6 1236
On Aug 23, 11:27 am, Ladislav Andel <lad...@iptel.orgwrote:
Hi,
what would be the most efficient way to do following?
I have a list of dictionaries taken from DB e.g.
dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
{'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
{'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
and list of object instances in memory(it's just for example)
which are looping within itself and testing particular hosts
memlist = [<instance 1>,<instance 2>]
memlist[0].id is 1 and memlist[0].host is google.com etc.
memlist[1].id is 9 and memlist[1].host is msn.com etc.
Now I want to add a new instance to memlist since id=3(in dblist) is not
in memlist.
How would you iterate through it and insert a new instance?
The result should be:
memlist = [<instance 1>,<instance 2>, <instance 3>]
memlist[0].id is 1 and memlist[0].host is google.com etc.
memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
memlist[2].id is 9 and memlist[2].host is msn.com etc.
Furthermore, I can have the opposite situation.
This time I need to remove from memlist a host which is not in dblist.
How would you do this?
The way how it works is that DBlist is loaded every 10 minutes and
compares with memlist.
The memlist should be the same as dblist.
Could you help me, please?
I'm working on my version of this but somebody might be quicker than me.
In case I have it done I will post it.
Thanks,
Lada
There is
and wt
On lists that change in memory, I use the following looping structure:
for i in range(len(in_memory_list)-1,-1,-1)
This loops over the list backwards. I found this method here: http://mail.python.org/pipermail/pyt...er/012451.html
Hopefully this will fulfill your needs. I've used it to great effect!
Mike ky******@gmail.com wrote:
On Aug 23, 11:27 am, Ladislav Andel <lad...@iptel.orgwrote:
>Hi, what would be the most efficient way to do following?
I have a list of dictionaries taken from DB e.g. dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'}, {'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'}, {'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
and list of object instances in memory(it's just for example) which are looping within itself and testing particular hosts
memlist = [<instance 1>,<instance 2>] memlist[0].id is 1 and memlist[0].host is google.com etc. memlist[1].id is 9 and memlist[1].host is msn.com etc.
Now I want to add a new instance to memlist since id=3(in dblist) is not in memlist. How would you iterate through it and insert a new instance?
The result should be: memlist = [<instance 1>,<instance 2>, <instance 3>] memlist[0].id is 1 and memlist[0].host is google.com etc. memlist[1].id is 3 and memlist[1].host is yahoo.com etc. memlist[2].id is 9 and memlist[2].host is msn.com etc.
Furthermore, I can have the opposite situation. This time I need to remove from memlist a host which is not in dblist. How would you do this?
The way how it works is that DBlist is loaded every 10 minutes and compares with memlist. The memlist should be the same as dblist.
Could you help me, please? I'm working on my version of this but somebody might be quicker than me. In case I have it done I will post it.
Thanks, Lada
There is
and wt
On lists that change in memory, I use the following looping structure:
for i in range(len(in_memory_list)-1,-1,-1)
well, actually I will need to iterate over 2 sequences which are not the
same length
and synchronize it against the dblist i have.
But I will look at your post. Thanks a lot.
Lada
This loops over the list backwards. I found this method here: http://mail.python.org/pipermail/pyt...er/012451.html
Hopefully this will fulfill your needs. I've used it to great effect!
Mike
Ladislav Andel wrote:
what would be the most efficient way to do following?
I have a list of dictionaries taken from DB e.g.
dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
{'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
{'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
and list of object instances in memory(it's just for example)
which are looping within itself and testing particular hosts
memlist = [<instance 1>,<instance 2>]
memlist[0].id is 1 and memlist[0].host is google.com etc.
memlist[1].id is 9 and memlist[1].host is msn.com etc.
Now I want to add a new instance to memlist since id=3(in dblist) is not
in memlist.
How would you iterate through it and insert a new instance?
The result should be:
memlist = [<instance 1>,<instance 2>, <instance 3>]
memlist[0].id is 1 and memlist[0].host is google.com etc.
memlist[1].id is 3 and memlist[1].host is yahoo.com etc.
memlist[2].id is 9 and memlist[2].host is msn.com etc.
You should replace the memlist with a dictionary using (host, id) tuples as
the keys. Here's an example that uses a set but requires you to modify the
<instance Nclass:
dblist = [{'id':1, 'host':'google.com','ip_address':'1.2.3.4'},
{'id':3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
{'id':9, 'host':'msn.com','ip_address':'11.3.2.3'}]
class Item(object):
def __init__(self, id, host, **discarded):
self._tuple = (id, host)
def __hash__(self):
return hash(self._tuple)
def __eq__(self, other):
return self._tuple == other._tuple
def __repr__(self):
return "Item(id=%r, host=%r)" % self._tuple
items = set([Item(1, "google.com")])
for d in dblist:
item = Item(**d)
if item not in items:
print "adding", item
items.add(item)
else:
print item, "already there"
Peter
Peter Otten wrote:
Ladislav Andel wrote:
>what would be the most efficient way to do following?
I have a list of dictionaries taken from DB e.g. dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'}, {'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'}, {'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
and list of object instances in memory(it's just for example) which are looping within itself and testing particular hosts
memlist = [<instance 1>,<instance 2>] memlist[0].id is 1 and memlist[0].host is google.com etc. memlist[1].id is 9 and memlist[1].host is msn.com etc.
Now I want to add a new instance to memlist since id=3(in dblist) is not in memlist. How would you iterate through it and insert a new instance?
The result should be: memlist = [<instance 1>,<instance 2>, <instance 3>] memlist[0].id is 1 and memlist[0].host is google.com etc. memlist[1].id is 3 and memlist[1].host is yahoo.com etc. memlist[2].id is 9 and memlist[2].host is msn.com etc.
You should replace the memlist with a dictionary using (host, id) tuples as
the keys. Here's an example that uses a set but requires you to modify the
<instance Nclass:
dblist = [{'id':1, 'host':'google.com','ip_address':'1.2.3.4'},
{'id':3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
{'id':9, 'host':'msn.com','ip_address':'11.3.2.3'}]
class Item(object):
def __init__(self, id, host, **discarded):
self._tuple = (id, host)
def __hash__(self):
return hash(self._tuple)
def __eq__(self, other):
return self._tuple == other._tuple
def __repr__(self):
return "Item(id=%r, host=%r)" % self._tuple
items = set([Item(1, "google.com")])
for d in dblist:
item = Item(**d)
if item not in items:
print "adding", item
items.add(item)
else:
print item, "already there"
Thank you for this nice solution. I wouldn't be able to write it this
way at all
but what about removing from memlist if there is less items in dblist
than in items (instances)?
I will have to iterate over items(instances) and remove that one which
is not in dblist I guess.
Lada
Peter
Ladislav Andel wrote:
need to be stopped before deleting any instance from items.
So I need to call stopLoop method in the given item in items before it
gets removed.
If there is any addition to items it's quite easy to call
item.startLoop() method.
Unless you want to rely on the __del__ method or introduce another
complication (weak references) you have to be explicit:
# untested
# removing items
db_items = set(Item(**d) for d in dblist)
delenda = items - db_items
for item in delenda:
item.stopLoop()
items.remove(item)
(I use twisted but it should not make any difference):
I've not worked with twisted, so I can't confirm that.
I can imagine that they provide their own way to spell a finalizer...
Peter
I have learnt a lot from your example and used it for my purpose.
Thank you, it very helped me.
Lada
Peter Otten wrote:
Ladislav Andel wrote:
>need to be stopped before deleting any instance from items. So I need to call stopLoop method in the given item in items before it gets removed. If there is any addition to items it's quite easy to call item.startLoop() method.
Unless you want to rely on the __del__ method or introduce another
complication (weak references) you have to be explicit:
# untested
# removing items
db_items = set(Item(**d) for d in dblist)
delenda = items - db_items
for item in delenda:
item.stopLoop()
items.remove(item)
>(I use twisted but it should not make any difference):
I've not worked with twisted, so I can't confirm that.
I can imagine that they provide their own way to spell a finalizer...
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Odd-R. |
last post by:
I have to lists, A and B, that may, or may not be equal. If they are not
identical, I want the output to be three new lists, X,Y and Z where X has
all the elements that are in A, but not in B, and...
|
by: Sheldon |
last post by:
Hi,
I have two arrays that are identical and contain 1s and zeros. Only the
ones are valid and I need to know where both arrays have ones in the
same position. I thought logical_and would work...
|
by: Loic |
last post by:
I would like to know if it is possible, and how to do this with Python:
I want to design a function to compare lists and return True only if
both lists are equal considering memory location of...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |