Repository: smok-serwis/coolamqp

Issue:
Add some kind of .get() for attributes
Because doing

```python
        try:
            mode = message.properties.content_type.tobytes()
        except AttributeError:
            mode = b'application/x-pickle'
```

all over sucks

Discussion:

Patch:
diff --git a/coolamqp/framing/base.py b/coolamqp/framing/base.py
index 5f7ccf2..31753c9 100644
--- a/coolamqp/framing/base.py
+++ b/coolamqp/framing/base.py
@@ -92,6 +92,15 @@ class AMQPContentPropertyList(object):
 
     # todo they are immutable, so they could just serialize themselves...
 
+    def get(self, property_name, default=None):
+        """
+        Return a particular property, or default if not defined
+        :param property_name: property name, unicode
+        :param default: default value
+        :return: memoryview or bytes
+        """
+        return getattr(self, property_name, default)
+
     @staticmethod
     def zero_property_flags(property_flags):
         """
diff --git a/coolamqp/framing/compilation/compile_definitions.py b/coolamqp/framing/compilation/compile_definitions.py
index 0109147..62a957d 100644
--- a/coolamqp/framing/compilation/compile_definitions.py
+++ b/coolamqp/framing/compilation/compile_definitions.py
@@ -55,7 +55,7 @@ binary string? It's a memoryview all right.
 Only thing that isn't are field names in tables.
 """
 
-import struct, collections, warnings, logging, six
+import struct, collections, logging, six
 
 from coolamqp.framing.base import AMQPClass, AMQPMethodPayload, AMQPContentPropertyList
 from coolamqp.framing.field_table import enframe_table, deframe_table, frame_table_size
@@ -230,7 +230,7 @@ Field = collections.namedtuple('Field', ('name', 'type', 'basic_type', 'reserved
 #
 #       If you do not know in advance what properties you will be using, it is correct to use
 #       this constructor.
-
+        if zpf in BasicContentPropertyList.PARTICULAR_CLASSES:
             return %s.PARTICULAR_CLASSES[zpf](**kwargs)
         else:
             logger.debug('Property field (%s:%d) not seen yet, compiling', repr(zpf))
diff --git a/coolamqp/framing/definitions.py b/coolamqp/framing/definitions.py
index cab1e07..e472c3d 100644
--- a/coolamqp/framing/definitions.py
+++ b/coolamqp/framing/definitions.py
@@ -23,7 +23,7 @@ binary string? It's a memoryview all right.
 Only thing that isn't are field names in tables.
 """
 
-import struct, collections, warnings, logging, six
+import struct, collections, logging, six
 
 from coolamqp.framing.base import AMQPClass, AMQPMethodPayload, AMQPContentPropertyList
 from coolamqp.framing.field_table import enframe_table, deframe_table, frame_table_size
@@ -2359,6 +2359,23 @@ class BasicContentPropertyList(AMQPContentPropertyList):
         ])
         zpf = six.binary_type(zpf)
 
+#        If you know in advance what properties you will be using, use typized constructors like
+#
+#          runs once
+#            my_type = BasicContentPropertyList.typize('content_type', 'content_encoding')
+#
+#           runs many times
+#            props = my_type('text/plain', 'utf8')
+#
+#       instead of
+#
+#           # runs many times
+#           props = BasicContentPropertyList(content_type='text/plain', content_encoding='utf8')
+#
+#       This way you will be faster.
+#
+#       If you do not know in advance what properties you will be using, it is correct to use
+#       this constructor.
         if zpf in BasicContentPropertyList.PARTICULAR_CLASSES:
             return BasicContentPropertyList.PARTICULAR_CLASSES[zpf](**kwargs)
         else:


Test patch:
diff --git a/tests/test_objects.py b/tests/test_objects.py
index 6a4c0c6..e3a109d 100644
--- a/tests/test_objects.py
+++ b/tests/test_objects.py
@@ -5,9 +5,7 @@ It sounds like a melody
 from __future__ import print_function, absolute_import, division
 import six
 import unittest
-
-
-from coolamqp.objects import NodeDefinition
+from coolamqp.objects import NodeDefinition, MessageProperties
 
 
 class TestObjects(unittest.TestCase):
@@ -23,3 +21,10 @@ class TestObjects(unittest.TestCase):
         n1 = NodeDefinition(u'amqp://ala:ma@kota/')
 
         self.assertEquals(n1.virtual_host, u'/')
+
+    def test_get_message_properties(self):
+        empty_p_msg = MessageProperties()
+        ce_p_msg = MessageProperties(content_encoding=b'wtf')
+
+        self.assertIsNone(empty_p_msg.get('content_encoding'), None)
+        self.assertEquals(ce_p_msg.get('content_encoding', b'wtf'), b'wtf')