Creating a table in python

I want to build a table in python with three columns, and then get the values ​​if necessary. I think dictionaries are the best way to do this, which has a key mapping with two meanings.

|column1 | column 2 | column 3 | | MAC | PORT NUMBER | DPID | | Key | Value 1 | Value 2 | 

the proposed method:

// define the global learning table globe_learning_table = defaultdict(set)

// add the port number and dpid of the switch based on its MAC address as the key //packet.src will give you the MAC address in this case globe_learning_table[packet.src].add(event.port) globe_learning_table[packet.src].add(dpid_to_str(connection.dpid))

// get the DPID value based on its MAC address globe_learning_table[packket.src][????]

I am not sure if one key points to two values, how can I get the specific value associated with this key.

I am open to using any other data structure if it can build this dynamic table and give me specific values ​​when necessary.

+4
source share
5 answers

Why a dictionary? Why not a list of named tuples or a collection (list, dictionary) of objects from a class you define (with attributes for each column)?

What happened with:

 class myRowObj(object): def __init__(self, mac, port, dpid): self.mac = mac self.port = port self.dpid = dpid myTable = list() for each in some_inputs: myTable.append(myRowObj(*each.split()) 

... or something like that?

(Note: myTable can be a list or a dictionary, or in any way that suits you. Obviously, if this is a dictionary, you should ask which key you will use to access these "lines").

The advantage of this approach is that your "string objects" (which you would name in some way, which would make more sense for your application domain) could implement any semantics that you choose. These objects can check and convert any values ​​provided when creating the instance, calculate any derived values, etc. You can also define the representation of the lines and codes of your object (implicit conversions, when one of your lines is used as a string or in certain types of development and debugging or serialization (for example, _str_ and _repr_ special methods).

Named tuples (added in Python 2.6) are a kind of lightweight object class that can offer some performance benefits and less memory space than regular user classes (for situations where you want named fields to not bind native methods to these objects, eg).

+3
source

Is something like this possible?

 >>> global_learning_table = collections.defaultdict(PortDpidPair) >>> PortDpidPair = collections.namedtuple("PortDpidPair", ["port", "dpid"]) >>> global_learning_table = collections.defaultdict(collections.namedtuple('PortDpidPair', ['port', 'dpid'])) >>> global_learning_table["ff:" * 7 + "ff"] = PortDpidPair(80, 1234) >>> global_learning_table defaultdict(<class '__main__.PortDpidPair'>, {'ff:ff:ff:ff:ff:ff:ff:ff': PortDpidPair(port=80, dpid=1234)}) >>> 

Named tuples may be appropriate for each row, but depending on how big this table is, you might be better off with db sqlite or something like that.

+3
source

If it is small enough to be stored in memory and you want it to be a data structure, you could create a class that contains values ​​1 and 2, and use this as a value to display the dictionary.

However, as Mr. E. noted, it is probably best to use a database to store information and retrieve from there as needed. This probably will not result in significant performance losses.

+1
source

Another possibility to keep in mind the SQLite table. See Python SQLite Docs for a basic example:

11.13. sqlite3 - DB-API 2.0 for SQLite Databases - Python Documentation v2.7.5 http://docs.python.org/2/library/sqlite3.html

+1
source

I think you are mixing two different goals. You need a representative data structure, and (as I read it) you want to print it in readable form. What is printed as a table is not stored in the computer in two dimensions; table view is a visual metaphor.

Assuming I'm right about what you want to achieve, the way I did it would be: a) keep it simple, and b) use the right modules to save effort.

The simplest data structure that correctly represents your information is, in my opinion, a dictionary in a dictionary. Like this:

 foo = {'00:00:00:00:00:00': {'port':22, 'dpid':42}, '00:00:00:00:00:01': {'port':23, 'dpid':43}} 

The best module I've found for quick and dirty table printing is prettytable . Your code will look something like this:

 foo = {'00:00:00:00:00:00': {'port':22, 'dpid':42}, '00:00:00:00:00:01': {'port':23, 'dpid':43}} t = PrettyTable(['MAC', 'Port', 'dpid']) for row in foo: t.add_row([row, foo[row]['port'], foo[row]['dpid']]) print t 
0
source

Source: https://habr.com/ru/post/1488123/


All Articles