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

Understanding Accessors in Ruby

One thing that puzzled me as a newbie (disclaimer: I still am) are accessors in Ruby, more commonly known as setters and getters or explicitly described as attr_reader, attr_writer and attr_accessor. Now let’s dive into the code first and describe the concepts of accessors after we’re done with coding. Initializing a Class Let’s say we want to create a class to resemble a Person with a name, and finally let’s try to access that name outside the class:...

<span title='2018-02-25 10:04:26 +0000 UTC'>February 25, 2018</span>&nbsp;·&nbsp;5 min&nbsp;·&nbsp;Franccesco Orozco

Initialize Rails and Deploy to Heroku

Install Rails Install the gem: gem install rails Create New Rails Project Create a new project and cd into it: rails new ProjectTest cd ProjectTest Change Gemfile to add PostgreSQL Heroku works with PostgreSQL as backend database as it doesn’t support SQLite3, so you’ll have to add the pg gem in the Gemfile in a production group: group :production do gem 'pg' end IMPORTANT: After adding PostgreSQL to the production group in the Gemfile you’ll have to move the SQLite3 gem to a development group or delete it, if you work with PostgreSQL just delete it entirely but if you would like to keep SQLite3 for local development then move the gem to a dev group like this:...

<span title='2018-02-23 18:16:43 +0000 UTC'>February 23, 2018</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;Franccesco Orozco

Calculate Filename SHA1 with Ruby

Basic usage Where FILENAME is the filename that you want to calculate require 'digest/sha1' Digest::SHA1.hexdigest(FILENAME) More advanced usage Save this code as checkhash.rb, usage: checkhash.rb <filename>. require 'digest/sha1' # Usage: checkhash.rb <filename> filename = ARGV.pop if filename.nil? # if no filename specified then prints help puts 'Please specify the filename to calculate the hash' puts "Usage: #{File.basename($PROGRAM_NAME)} FILENAME" exit end # calculating SHA1 hash def calculate_hash(file) Digest::SHA1.hexdigest(file) end file_hash = calculate_hash(filename) puts "#{filename}: #{file_hash}"

<span title='2018-02-22 17:05:20 +0000 UTC'>February 22, 2018</span>&nbsp;·&nbsp;1 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

Sort a Dictionary in Python

Dicionary Example: dictionary = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5} Sorting methods Sort keys sorted(dictionary) >> ['five', 'four', 'one', 'three', 'two'] Sort keys by value sorted(dictionary, key=dictionary.__getitem__) >> ['one', 'two', 'three', 'four', 'five'] Sort values sorted(dictionary.values()) >> [1, 2, 3, 4, 5] Reverse sorting with reverse=True sorted(dictionary, key=dictionary.__getitem__, reverse=True) >> ['one', 'two', 'three', 'four', 'five']

<span title='2018-02-18 19:31:07 +0000 UTC'>February 18, 2018</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;Franccesco Orozco