Create a Basic API With Flask

What is Flask? Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. Flask is a microframework for Python that you can use to quickly create API’s and websites. It’s a great and easy to use platform, let’s create a simple API, but first we will go through the basics. Flask Installation We’re going to install Flask in our virtual environment to later import it into our code....

<span title='2018-04-07 22:20:50 +0000 UTC'>April 7, 2018</span>&nbsp;·&nbsp;4 min&nbsp;·&nbsp;Franccesco Orozco

Create and Object With Namedtuple

Namedtuples are a subclass of typename which allow us to create objects (or classes) with tuple-like attributes, it’s easy to create a class with immutable attributes with this library. Creating an object To create a namedtuple we only need the name of the class first and then the attribute list. >>> from collections import namedtuple >>> Person = namedtuple('Person', 'name job_position sex married') >>> john = Person('john', 'developer', 'male', False) >>> john Person(name='john', job_position='developer', sex='male', married=False) Be aware that you can pass an argument list as a string ('att1 att2'), as an actual list (['att1', 'att2']) or a string with comma separated values ('att1, att2')....

<span title='2018-04-07 13:21:03 +0000 UTC'>April 7, 2018</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Franccesco Orozco

Return a List of Files and Folders With Glob in Python

There’s a great library added in Python 3.5 that lets you return a list of filenames and folders called glob, here’s how to use it. Return files and folders in current folder. >>> glob('**') ['scaffolds', 'node_modules', 'yarn.lock', '_config.yml', 'source', 'db.json', 'themes', 'package.json', 'package-lock.json'] Return files and folders recursively >>> glob('**', recursive=True) ['scaffolds', 'scaffolds/post.md', 'scaffolds/page.md', 'scaffolds/draft.md', 'node_modules', '...'] Return only specific type of files recursively >>> glob('*.json', recursive=True) ['db.json', 'package.json', 'package-lock.json'] non-recursively >>> glob('**/*....

<span title='2018-03-29 19:20:39 +0000 UTC'>March 29, 2018</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;Franccesco Orozco

Unit Testing Basics With Python

I encourage developers to see the value of unit testing; I urge them to get into the habit of writing structured tests alongside their code. — CodingHorror I was reading about Unit Testing and found a blog entry in CodingHorror called I Pity The Fool Who Doesn’t Write Unit Tests, and guess what, he’s right. Sadly, I’ve encountered a lot of people who doesn’t write tests for their code, and have a CI System (Travis, Jenkins, Gitlab, etc....

<span title='2018-03-27 15:25:01 +0000 UTC'>March 27, 2018</span>&nbsp;·&nbsp;6 min&nbsp;·&nbsp;Franccesco Orozco

Classes and Special Methods in Python

These are my study notes on Classes and special methods. As always, if something is wrong then you can always correct me, it would help me and everybody else. Class ‘Employee’ example This is the example class that we’re going to use, a class Employee which we will inherit from to create more classes like Manager and Supervisor: class Employee(object): """Class simulating an Employee with basic attributes.""" total_employees = 0 def __init__(self, name, rate, position): self....

<span title='2018-03-18 11:24:18 +0000 UTC'>March 18, 2018</span>&nbsp;·&nbsp;6 min&nbsp;·&nbsp;Franccesco Orozco

Figure Out a Download File-Size With Requests

To get the filesize of a download is really easy, servers usually provide a Content-Length in its header response that let us know how heavy is the content we are requesting. We can find out this content length opening our shell and requesting a HEAD response in linux: As you can see, our content length is display in bytes. Let’s try to get this response with Requests Display Content-Length with Requests Let’s use an image from httpbin....

<span title='2018-03-09 14:49:48 +0000 UTC'>March 9, 2018</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Franccesco Orozco

How to Securely Store Sensitive Configuration With Dotenv

TL;DR: Environment Variables API keys are one example of sensitive information that should remain secret, the problem is that we need to use them in our code to access third-party services like Twitter, Github, DigitalOcean and so on, so how do we manage to use those API keys without hard-coding them into the source code? The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard....

<span title='2018-02-28 18:17:17 +0000 UTC'>February 28, 2018</span>&nbsp;·&nbsp;4 min&nbsp;·&nbsp;Franccesco Orozco

Manage Python Versions With Pyenv

Pyenv is an excellent tool to have in your tool-set, it manages Python versions much like rbenv for Ruby, in fact it was forked from it. pyenv lets you easily switch between multiple versions of Python. It’s simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well. Installation The automatic installer provided in GitHub will take care of everything so you don’t have to worry about configuring anything....

<span title='2018-02-27 18:21:18 +0000 UTC'>February 27, 2018</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Franccesco Orozco

HTTP Requests in Python

Before starting lets try HTTP requests with httpbin.org to test multiple HTTP methods with requests and JSON data. We’ll see how to extract data from a JSON-encoded response (e.g. {‘key’: ‘value’}) Install requests library With pipenv (recommended): pipenv install requests With pip: pip install requests GET request Check our IP address: import requests # get request, response is JSON-encoded myIP = requests.get('https://httpbin.org/ip') # extract value from JSON key 'origin' # {'origin': '38....

<span title='2018-02-20 19:52:45 +0000 UTC'>February 20, 2018</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Franccesco Orozco

How to Get Started With Pipenv

What is pipenv Essentially Pipenv is pip + virtualenv and it’s a match made in heaven. It manages dependencies, required python versions (if pyenv is available), generates pipfiles which is more reliable than a requirements.txt file and it generates a virtual environment so you don’t screw other environments and its requirements. It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages....

<span title='2018-02-20 11:36:12 +0000 UTC'>February 20, 2018</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Franccesco Orozco