Posts

Showing posts with the label snippet

Google spreadsheet data to dictionary using Google Apps Script

Another great snippet I wrote this morning:

Sorting select options in alphabet order

Pretty handyyyy: Reference: http://stackoverflow.com/questions/12073270/sorting-options-elements-alphabetically-using-jquery

Getting max length of list of lists in Python

Here is the handy snippet:

Delete all pending/spam/trash comments from WordPress (Single or Multisite) using WP-CLI

I just wrote this bash shell script to delete all pending/spam/trash comments from WordPress (Single or Multisite) using WP-CLI: Put the script inside your WordPress root and run: $ ./clean_comments.sh These are available statuses of a comment: approve : approved comments hold : pending comments spam : spams trash : comments in the trash bin This is pretty neat huh?!

How to remove leading and trailing spaces in bash

To remove both leading and trailing spaces of a string in bash shell, you can do like following: mytext=' This is a test ' trimed_text="$(echo -e "${mytext}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"

Excel to list of dictionaries in Python

This is a pretty neat python snippet I wrote to read an Excel (xlsx) file and convert its data into a list of dictionaries: Note: you need to install the openpyxl module before you can use the snippet $ sudo pip install openpyxl So, you can use the snippet as following: >> from excel_utils import excel_to_dict >> data = excel_to_dict('/path/to/excel/file.xlsx', ['col1', 'col2', 'col3']) >> data[0] {'col1': u'Genius',  'col2': u'Super',  'col3': u'Awesome'} Cool huh? Reference:   https://automatetheboringstuff.com/chapter12/

CSV file to PHP array

Here is a pretty cool snippet to read csv file and turn it into a php array: function read_csv($csv_path){ $file = fopen($csv_path, 'r'); while (!feof($file) ) { $lines[] = fgetcsv($file); } fclose($file); return $lines; }

Django + Heroku - To avoid error when deploying

As the previous blog post, I posted a setting snippet to let Django know where the db is (local and in Heroku):  http://iambusychangingtheworld.blogspot.com/2013/10/django-heroku-local-and-heroku-database.html Today, I found someone has create another snippet which is much more clever and useful: Deploying Django to Heroku (Psycopg2 Error) heroku run python manage.py syncdb psycopg2.OperationalError: could not connect to server: Connection refused Is the server running on host "localhost" and accepting TCP/IP connections on port 5432? heroku addons:add shared-database:5mb import sys import urlparse import os # Register database schemes in URLs. urlparse.uses_netloc.append('postgres') urlparse.uses_netloc.append('mysql') try: # Check to make sure DATABASES is set in settings.py file. # If not default to {} if 'DATABASES' not in locals(): DATABASES = {} if 'DATABASE_URL' in os.environ: url = urlpar...