Antonio Alvarado Hernández avatar Antonio Alvarado Hernández committed 9540ec8

Added my legacy Python's stuff

These codes include legacy scripts for test various aspects of the
Python programming language. This includes:

* my learning examples
* a simple `ceODBC` example
* the `Ruffus` tutorial script
* a `Tornado` basic example
* my incompleted utilities
* some `win32`-specific tests

Comments (0)

Files changed (7)

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

Python/Ruffus/tutorial.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from __future__ import print_function
+
+from ruffus import *
+
+def first_task():
+    print("Hello,")
+
+@follows(first_task)
+def second_task():
+    print("world!")
+
+if __name__ == '__main__':
+    pipeline_run([second_task], verbose = 1)
+
+# EOF

Python/Tornado/hellosrv.py

+#!/usr/bin/env python
+
+from __future__ import print_function
+
+import tornado.ioloop
+import tornado.web
+
+class MainHandler(tornado.web.RequestHandler):
+    def get(self):
+        self.write("Hello, world!")
+
+server = tornado.web.Application([
+    (r'/', MainHandler),
+])
+
+if __name__ == '__main__':
+    print("Starting server at http://localhost:8888/ ...")
+    server.listen(8888)
+    tornado.ioloop.IOLoop.instance().start()
+
+# EOF

Python/Utilities/Economics/umacromy.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from __future__ import print_function
+
+import numpy as np
+
+
+class BaseModel(object):
+    """TODO"""
+
+    def __init__(self, nplots=1):
+        print("Hello, from Base!")
+        self._nplots = nplots
+
+    @property
+    def nplots(self):
+        return self._nplots
+
+class ISMPModel(BaseModel):
+    """TODO"""
+
+    def __init__(self):
+        super(ISMPModel, self).__init__(nplots=2)
+        print("Hello, from ISMP!")
+
+def main():
+    """This is the main entry-point while running stand-alone."""
+    print("Hello, world!")
+    model = ISMPModel()
+    print("Model has", model.nplots, "plots")
+
+
+if __name__ == '__main__':
+    main()
+
+# EOF

Python/Win32/pynetcon.py

+#!/usr/bin/env python
+
+# http://pymotw.com/2/contextlib/
+
+from __future__ import print_function
+
+import win32wnet as wn
+import win32netcon as nc
+
+class ResourceEnumerator(object):
+    """Blah, blah, blah, ..."""
+    
+    def __init__(self, scope, type_, usage):
+        """Blah, blah, blah, ..."""
+        self._scope = scope
+        self._type = type_
+        self._usage = usage
+
+    def __iter__(self):
+        """Blah, blah, blah, ..."""
+        enum = wn.WNetOpenEnum(self._scope, self._type, self._usage, None)
+        try:
+            for rsrc in wn.WNetEnumResource(enum):
+                yield rsrc
+        finally:
+            wn.WNetCloseEnum(enum)
+
+def main():
+    print("Begining...")
+    scope = nc.RESOURCE_CONNECTED
+    type_ = nc.RESOURCETYPE_DISK
+    usage = nc.RESOURCEUSAGE_ALL
+    for rsrc in ResourceEnumerator(scope, type_, usage):
+        print("Resource:", rsrc.lpLocalName, rsrc.lpRemoteName)
+    print("Finished!")
+
+if __name__ == '__main__':
+    main()
+
+# EOF

Python/ceODBC/test-ceodbc.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from __future__ import print_function
+
+from ceODBC import connect
+
+print("Beginning...")
+
+print("Connecting...")
+conn = connect(r'dsn=WAPanel')
+
+print("Creating a new cursor object...")
+cursor = conn.cursor()
+
+print("Executing a query...")
+cursor.execute("SELECT * FROM DAILY_MARKETING_PROGRAMS")
+for row in cursor.fetchall():
+    print(">", row)
+
+print("Closing the cursor...")
+cursor.close()
+
+print("Closing connection...")
+conn.close()
+
+print("Finished!")
+
+# EOF
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.