Commits

Antonio Alvarado Hernández committed 5163a7c

group miscellaneous Python learning samples

* moved legacy PoC scripts

Comments (0)

Files changed (4)

Python/Learning/Samples/call-class-method-by-name.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+class CallMethodByName(object):
+    def __init__(self, data):
+        self._data = data
+
+    def run(self):
+        for key, value in self._data.items():
+            if key in self._field_map:
+                func = getattr(self, self._field_map[key])
+                print key, value, func(value)
+
+    def _from_string(self, value):
+        print "Calling _from_string(" + str(value) + ")..."
+        return str(value)
+
+    def _from_integer(self, value):
+        print "Calling _from_integer(" + str(value) + ")..."
+        return str(value)
+
+    _field_map = {
+        'name': '_from_string',
+        'age':  '_from_integer',
+    }
+
+if __name__ == '__main__':
+    o = CallMethodByName({'name': 'joe', 'age': 33})
+    o.run()
+
+# EOF

Python/Learning/Samples/str-vs-repr.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+class Test(object):
+    def __init__(self, a, b):
+        self._a = a
+        self._b = b
+
+    def __str__(self):
+        return "An instance of class Test with state: a=%s b=%s" % (self._a, self._b)
+
+    def __repr__(self):
+        return 'Test("%s","%s")' % (self._a, self._b)
+
+if __name__ == '__main__':
+    x = Test('hello', 'world')
+    print 'Human readable: ', str(x)
+    print 'Object representation: ', repr(x)
+    print
+
+    y = eval(repr(x))
+    print 'Human readable: ', str(y)
+    print 'Object representation: ', repr(y)
+    print
+
+# EOF

Python/Learning/call-class-method-by-name.py

-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-class CallMethodByName(object):
-    def __init__(self, data):
-        self._data = data
-
-    def run(self):
-        for key, value in self._data.items():
-            if key in self._field_map:
-                func = getattr(self, self._field_map[key])
-                print key, value, func(value)
-
-    def _from_string(self, value):
-        print "Calling _from_string(" + str(value) + ")..."
-        return str(value)
-
-    def _from_integer(self, value):
-        print "Calling _from_integer(" + str(value) + ")..."
-        return str(value)
-
-    _field_map = {
-        'name': '_from_string',
-        'age':  '_from_integer',
-    }
-
-if __name__ == '__main__':
-    o = CallMethodByName({'name': 'joe', 'age': 33})
-    o.run()
-
-# EOF

Python/Learning/str-vs-repr.py

-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-class Test(object):
-    def __init__(self, a, b):
-        self._a = a
-        self._b = b
-
-    def __str__(self):
-        return "An instance of class Test with state: a=%s b=%s" % (self._a, self._b)
-
-    def __repr__(self):
-        return 'Test("%s","%s")' % (self._a, self._b)
-
-if __name__ == '__main__':
-    x = Test('hello', 'world')
-    print 'Human readable: ', str(x)
-    print 'Object representation: ', repr(x)
-    print
-
-    y = eval(repr(x))
-    print 'Human readable: ', str(y)
-    print 'Object representation: ', repr(y)
-    print
-
-# EOF