python on davidchua https://dchua.com/tags/python/ Recent content in python on davidchua Hugo -- gohugo.io en-us Sat, 23 Jul 2016 00:00:00 +0000 How to set current_user in Tornado asynchronously https://dchua.com/posts/2016-07-23-how-to-set-current_user-in-tornado-asynchronously/ Sat, 23 Jul 2016 00:00:00 +0000 https://dchua.com/posts/2016-07-23-how-to-set-current_user-in-tornado-asynchronously/ Something that I’ve missed when working on setting the current_user upon @tornado.web.authenticated. Normally, you’d set get_current_user(self) in the RequestHandler which is used by @tornado.web.authenticated to pull/query the current_user that is logged on. It is fine until you start to utilize asynchronous functions within the get_current_user(self). Quick notes: get_current_user(self) cannot be a coroutine. So no @tornado.gen.coroutine decorator before it. This also means that it cannot perform yield functions from elsewhere. when required to use a async function to pull user information, use prepare(self) instead. Logging Tornado Logs To Graylog (with JSON) https://dchua.com/posts/2016-07-19-logging-tornado-logs-to-graylog/ Tue, 19 Jul 2016 00:00:00 +0000 https://dchua.com/posts/2016-07-19-logging-tornado-logs-to-graylog/ If you’re building your microservices on Tornado and your chosen deployment strategy is to treat it like cattle instead of pets, logging is going to have to play a very important part of your architecture. To ensure that all your information is logged and stored in a centralized location, I built a full HA cluster of graylog nodes and elasticsearch nodes which I will use to keep track of my application. A Python RethinkDB walkthrough https://dchua.com/posts/2016-04-13-a-rethinkdb-python-walkthrough/ Wed, 13 Apr 2016 00:00:00 +0000 https://dchua.com/posts/2016-04-13-a-rethinkdb-python-walkthrough/ RethinkDB is a modern NoSQL document-based database that baked in push/pull features into its database core which allows developers to build realtime applications without having to add a websocket/messaging layer ontop of their existing stack. If you’re familar with MongoDB and its schemaless document structure, RethinkDB works in a similar manner with flexible schemas. With RethinkDB, you can write a client in any language (Python, JS, Ruby etc…) to listen to specific database queries and perform operations when there’s a change in the database. Event Loop Tasks https://dchua.com/posts/2016-04-11-event-loop-tasks/ Mon, 11 Apr 2016 00:00:00 +0000 https://dchua.com/posts/2016-04-11-event-loop-tasks/ A pictorial to understanding async loopings Source: Asyncio Task - Python Documentation Packaging your Python Packages into PyPI (pip) https://dchua.com/posts/2016-04-09-packaging-your-python-packages-into-pypi-pip/ Sat, 09 Apr 2016 00:00:00 +0000 https://dchua.com/posts/2016-04-09-packaging-your-python-packages-into-pypi-pip/ This is a quick walkthrough on submiting your module for a package. Most of it has been distiled from Peter Down’s article so any credits please send it his way. I’ve modified the walkthrough to use setuptools instead of disutils For a more verbose account, please read his article. First-time PyPI packagers Make sure you register an account at both https://pypi.python.org and https://testpypi.python.org Create a .pypirc file in your ~/. Installing GRPC, Protobuf and its dependencies for Python Development https://dchua.com/posts/2016-04-08-installing-grpc-protobuf-and-its-dependencies-for-python-development/ Fri, 08 Apr 2016 00:00:00 +0000 https://dchua.com/posts/2016-04-08-installing-grpc-protobuf-and-its-dependencies-for-python-development/ Been having some trouble getting my local machine setup for GRPC development lately. Hopefully this quick walkthrough will help someone out. Prerequisites You’re on the Python 2.X branch. Python 3.X is currently not supported for the official grpc library Setup Install Protobuf $ pip install "protobuf>=3.0.0a2" Note: In order to support protobuf syntax 3, you’ll need to ensure your version of protobuf is at least 3. Debugging Websockets with wscat (Websocket CLI Terminal) https://dchua.com/posts/2016-04-01-debugging-websockets-with-wscat/ Fri, 01 Apr 2016 22:32:09 +0800 https://dchua.com/posts/2016-04-01-debugging-websockets-with-wscat/ So I’m currently working on a Websocket implementation from almost-scratch (Tornado/Websockets) and I’m working on implementing some form of rooms/channel functionality. This is mostly just for my learning experience to dive deeper into Python. A cool tool that I’ve just discovered is helping me shave off the time needed to do debugging to a bare minimum. Introducing wscat, a nodejs cli application that allows you to connect directly to a websocket server and receive/send data in a terminal like environment. Converting KML into GeoJSON for Mongoimport & Querying https://dchua.com/posts/2016-03-31-converting-kml-into-geojson-for-mongoimport/ Thu, 31 Mar 2016 00:00:00 +0000 https://dchua.com/posts/2016-03-31-converting-kml-into-geojson-for-mongoimport/ First, you need to install kml2geojson. Luckily, there’s pip. $ pip install kml2geojson Once that’s done, you can take a .kml file and convert it into a GeoJSON format. Why GeoJSON? Because its gaining in support (MongoDB, RethinkDB) and has lots of smart people working on its specification. $ kml2geojson <.kml> <output_path> Now if you’re looking to import the geoJSON data into MongoDB so that you can do geospatial queries, you’ll need to prepare the GeoJSON file for import so that MongoDB will create multiple documents rather than a single document of your geo-objects. Debugging in Python https://dchua.com/posts/2016-03-30-debugging-in-python/ Wed, 30 Mar 2016 00:00:00 +0000 https://dchua.com/posts/2016-03-30-debugging-in-python/ Miss being able to inspect, debug and run expressions in between your code? I’ve heard so much about iPython’s debugging functionality and it turns out there’s a way to make use of it without having to use iPython itself. Meet ipdb. With just a couple of simple steps: import ipdb print("before break") ipdb.set_trace() print("after break") You’ll drop into a nice interactive shell for your debugging need. Most syntaxes are similar to ruby-debug, so if you’re coming from ruby, you shouldn’t have any issue. Understanding Python Decorators https://dchua.com/posts/2016-03-29-understanding-python-decorators/ Tue, 29 Mar 2016 00:00:00 +0000 https://dchua.com/posts/2016-03-29-understanding-python-decorators/ Decorators are functions that replaces an existing function with another function with the original function as a parameter. A decorator function would have the following structure: def decorator_name(func): def func_wrapper(*args) return "any content" + str(*args) return func_wrapper Sometimes, you might see functions that are defined like: @some_function def do_stuff(*args): return str(*args) @some_function in this case is a decorator. What the above actually translate to in pseudo-code is: Whenever do_stuff() is called, return the output of some_function() which takes in the output of do_stuff(). Running virtualenvwrapper with pyenv https://dchua.com/posts/2016-03-27-running-virtualenvwrapper-with-pyenv/ Sun, 27 Mar 2016 13:10:46 +0800 https://dchua.com/posts/2016-03-27-running-virtualenvwrapper-with-pyenv/ Virtualenvwrapper is a great way to manage your different python package environment. How would you then manage the different package environment ontop of your different python environments? Pyenv-virtualenvwrapper attempts to answer that question by allowing your virtualenvwrapper to be used in conjunction with your pyenv. If you don’t have virtualenvwrapper or virtualenv installed, don’t fret, you can still follow the tutorial, pyenv-virtualenvwrapper would install all the necessary dependencies. Prerequisite: Have Pyenv installed Installation Installation is pretty quick, just do a simple: Installing pyenv - RVM for Python https://dchua.com/posts/2016-03-25-installing-pyenv-rvm-for-python/ Fri, 25 Mar 2016 11:02:06 +0800 https://dchua.com/posts/2016-03-25-installing-pyenv-rvm-for-python/ One of the things I’ve missed while learning python, is the ability to manage and switch between python versions. In Ruby, we often use tools like RVM or rbenv to handle the different ruby versions. This is particularly useful when you have projects that have to rely on an older version of ruby and you don’t want to mess up your local environment. In python, there’s pyenv. To set it up, its really quite simple. Writing a Serverless Python Microservice with AWS Lambda and AWS API Gateway https://dchua.com/posts/2016-03-22-writing-a-serverless-python-microservice-with-aws-lambda-and-aws-api-gateway/ Tue, 22 Mar 2016 00:00:00 +0000 https://dchua.com/posts/2016-03-22-writing-a-serverless-python-microservice-with-aws-lambda-and-aws-api-gateway/ Serverless Architectures are the rage nowadays. In this article, I’ll attempt to walkthrough the process of writing a Serverless Microservice with AWS Lambda and Amazon API Gateway. For people who know me, know that I’m not much of a Pythonista, but doing this blog post helped me to better appreciate Python and its awesome standard libraries and while I could have done this in NodeJS, this was a perfect example to pick up Python while doing something new.