Node iterator in Python

233 views
Skip to first unread message

Andrew Hallagan

unread,
Jul 31, 2009, 7:44:47 PM7/31/09
to ns-3-...@googlegroups.com
Hi all,

I have some C++ code that prints out the initial locations of all of my Nodes.  It looks like this:

void
PrintLocations (NodeContainer nodes, std::string heading)
{
  std::cout << heading << std::endl;
  for(NodeContainer::Iterator j = nodes.Begin (); j !=
      nodes.End (); ++j)
  {
    Ptr<Node> object = *j;
    Ptr<MobilityModel> position = object->GetObject<MobilityModel> ();
    NS_ASSERT (position != 0);
    Vector pos = position->GetPosition ();
    std::cout << "xpos = " << pos.x << " ypos = " << pos.y << " zpos = "
      << pos.z << std::endl;
  }
  /*
   * Resulting output:
   * AP Node Locations:
   * xpos = 0 ypos = 0 zpos = 0
   * STA Node Locations:
   * xpos = 4.4 ypos = 0 zpos = 0
   *
   */
}



I'd like to do the same thing in Python.  Here's what I've got so far, but it doesn't work.


def PrintLocations(nodes, heading):
  print heading
  for j in range(0, nodes.GetN()):
    object = nodes.Get(j)
    # just a de-bugging print statement:
    print str(object)    #  "<ns3.Node object at 0xb6e4426c>"
    position = object.GetObject<ns3.MobilityModel>()

    pos = position.GetPosition()
    print "xpos = " + str(pos.x) + " ypos = " + str(pos.y) + " zpos = " + str(pos.z)



Here's what I get in return:

AP Node Locations:
<ns3.Node object at 0xb6c9924c>
True
Traceback (most recent call last):
  File "scratch/fifth.py", line 165, in <module>
    main(sys.argv)
  File "scratch/fifth.py", line 130, in main
    PrintLocations(wifiApNodes, "AP Node Locations:")
  File "scratch/fifth.py", line 15, in PrintLocations
    pos = position.GetPosition()
AttributeError: 'bool' object has no attribute 'GetPosition'
Command ['/usr/bin/python', 'scratch/fifth.py'] exited with code 1


Thanks,

AWH

Gustavo Carneiro

unread,
Jul 31, 2009, 8:05:41 PM7/31/09
to ns-3-...@googlegroups.com


2009/8/1 Andrew Hallagan <andrewh...@gmail.com>

^^^^^^^^^^^
This is not valid Python code.

It should be:

   position = object.GetObject (ns3.MobilityModel.GetTypeId())
 



    pos = position.GetPosition()
    print "xpos = " + str(pos.x) + " ypos = " + str(pos.y) + " zpos = " + str(pos.z)



Here's what I get in return:

AP Node Locations:
<ns3.Node object at 0xb6c9924c>
True
Traceback (most recent call last):
  File "scratch/fifth.py", line 165, in <module>
    main(sys.argv)
  File "scratch/fifth.py", line 130, in main
    PrintLocations(wifiApNodes, "AP Node Locations:")
  File "scratch/fifth.py", line 15, in PrintLocations
    pos = position.GetPosition()
AttributeError: 'bool' object has no attribute 'GetPosition'
Command ['/usr/bin/python', 'scratch/fifth.py'] exited with code 1


Thanks,

AWH






--
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert

Andrew Hallagan

unread,
Aug 2, 2009, 8:43:52 PM8/2/09
to ns-3-...@googlegroups.com
Ahh, yes, that works perfectly.  Thank you.  I have two follow up questions:

1.  How did you know this in the first place?  Maybe this sounds dumb, but I can't find any kind of API that would specify how my Python syntax should be arranged.  So far I've just been going on "hunches" by looking at equivalent C++ code until it does what I want it to do.

2. I have another C++ to Python question:

Here's my C++ code:

  std::ostringstream oss;
  oss <<
    "/NodeList/" << wifiStaNodes.Get (nSta - 1)->GetId() <<
    "/$ns3::MobilityModel/CourseChange";

  Config::Connect (oss.str (), MakeCallback (&CourseChange));



And this is my attempt at the corresponding Python code which of course, doesn't work :-)

  context = "/NodeList/" + str(wifiStaNodes.Get(0).GetId()) + "/$ns3::MobilityModel/CourseChange"
  model = wifiStaNodes.Get(0).GetObject(ns3.MobilityModel.GetTypeId())
  ns3.Config.Connect(context, ns3.CallbackBase(CourseChange))



AWH

Mathieu Lacage

unread,
Aug 3, 2009, 2:08:33 AM8/3/09
to ns-3-...@googlegroups.com

On Sun, 2009-08-02 at 20:43 -0400, Andrew Hallagan wrote:

> 1. How did you know this in the first place? Maybe this sounds dumb,
> but I can't find any kind of API that would specify how my Python
> syntax should be arranged. So far I've just been going on "hunches"
> by looking at equivalent C++ code until it does what I want it to do.

I don't think we have documented anywhere the python API. For the most
part, it's just like the c++ API, except where c++ templates are
present.

>
> 2. I have another C++ to Python question:
>
> Here's my C++ code:
>
> std::ostringstream oss;
> oss <<
> "/NodeList/" << wifiStaNodes.Get (nSta - 1)->GetId() <<
> "/$ns3::MobilityModel/CourseChange";
>
> Config::Connect (oss.str (), MakeCallback (&CourseChange));
>
>
> And this is my attempt at the corresponding Python code which of
> course, doesn't work :-)
>
> context = "/NodeList/" + str(wifiStaNodes.Get(0).GetId()) +
> "/$ns3::MobilityModel/CourseChange"
> model = wifiStaNodes.Get(0).GetObject(ns3.MobilityModel.GetTypeId())
> ns3.Config.Connect(context, ns3.CallbackBase(CourseChange))
>

Another case of c++ templates in the python api. tracing + callbacks
does not work yet in python.

Mathieu

Andrew Hallagan

unread,
Aug 3, 2009, 8:48:12 AM8/3/09
to ns-3-...@googlegroups.com
So without tracing and callbacks, is there any way to confirm that a particular Python script is doing the exact same thing as its C++ counterpart?  If not, then maybe it's better just to stick with using C++?

AWH

Gustavo Carneiro

unread,
Aug 3, 2009, 9:50:47 AM8/3/09
to ns-3-...@googlegroups.com


2009/8/3 Andrew Hallagan <andrewh...@gmail.com>

So without tracing and callbacks, is there any way to confirm that a particular Python script is doing the exact same thing as its C++ counterpart?  If not, then maybe it's better just to stick with using C++?

In any case, callback based tracing would be slow in Python.  The alternatives in Python are:

 1. ascii tracing;

 2. pcap tracing;

 3. in the future, FlowMonitor (once it's ported to ns-3-dev, for ns-3.2 see http://code.nsnam.org/gjc/ns-3-flowmon)

 4. make your own C++ class that does the tracing and returns the results to python as data structures; you'll need to master the art of python scanning (./waf --python-scan).

I am a heavy user of python simulations, and what I end up doing is a combination of 3 and 4.

Mathieu Lacage

unread,
Aug 3, 2009, 11:17:23 AM8/3/09
to ns-3-...@googlegroups.com
On Mon, 2009-08-03 at 14:50 +0100, Gustavo Carneiro wrote:
>
>
> 2009/8/3 Andrew Hallagan <andrewh...@gmail.com>
> So without tracing and callbacks, is there any way to confirm
> that a particular Python script is doing the exact same thing
> as its C++ counterpart? If not, then maybe it's better just
> to stick with using C++?
>
> In any case, callback based tracing would be slow in Python. The

Yes, it would be slow but it's a bug that we don't support it, right ?

Mathieu

Gustavo Carneiro

unread,
Aug 3, 2009, 11:32:05 AM8/3/09
to ns-3-...@googlegroups.com


2009/8/3 Mathieu Lacage <mathieu...@sophia.inria.fr>


On Mon, 2009-08-03 at 14:50 +0100, Gustavo Carneiro wrote:
>
>
> 2009/8/3 Andrew Hallagan <andrewh...@gmail.com>
>         So without tracing and callbacks, is there any way to confirm
>         that a particular Python script is doing the exact same thing
>         as its C++ counterpart?  If not, then maybe it's better just
>         to stick with using C++?
>
> In any case, callback based tracing would be slow in Python.  The

Yes, it would be slow but it's a bug that we don't support it, right ?

I don't know what "support" means in this context.  It is something that I would like to fix some day, but it has limited utility and is difficult to fix, so the interest/effort ratio is low.  Or at least that's my personal opinion...
 
Reply all
Reply to author
Forward
0 new messages