2015-12-22
2015-12-10
Meteor - MongoDB collection inspection from browsers console
COLLECTION_NAME.find({}).collection._docs._map;
2015-12-03
MongoDB - Unique compound index creation
db.COLL_NAME.createIndex(
{
FIELD_NAME : 1
, FIELD_NAME_2: 1
}
, { unique: true }
);
2015-12-02
MongoDB - Execute a custom script file and export the results to a flat file using the command line
mongo localhost:3001/DATABASE_NAME SCRIPT_FILE_NAME.js >> TARGET_FILE_NAME.EXTENSION
MongoDB - Export query to flat file from the command-line
mongo SERVER_NAME_OR_IP:PORT_NUMBER/DATABASE_NAME --eval "var c =
db.COLLECTION_NAME.find(); while(c.hasNext()) {printjson(c.next())}"
>> TARGET_FILE_NAME.TARGET_FILE_NAME_EXTENSION
Actual example:
mongo localhost:3001/meteor --eval "var c = db.mycoll.find(); while(c.hasNext()) {printjson(c.next())}" >> test.txt
Another example/thread at:
http://stackoverflow.com/questions/12823990/how-to-get-mongo-command-results-in-to-a-flat-file
Actual example:
mongo localhost:3001/meteor --eval "var c = db.mycoll.find(); while(c.hasNext()) {printjson(c.next())}" >> test.txt
Another example/thread at:
http://stackoverflow.com/questions/12823990/how-to-get-mongo-command-results-in-to-a-flat-file
MongoDB - Meteor command-line import
mongoimport --jsonArray -h localhost:3001 -d meteor -c COLLECTION_NAME --file "/PATH_TO_FILE/FILE.EXTENSION"
PS. Specify the address (-h) of your Meteor's MongoDB instance.
PS. Specify the address (-h) of your Meteor's MongoDB instance.
2015-12-01
MongoDB - How to insert integer numbers
db.COLLECTION_NAME.insert({ FIELD_NAME : NumberInt(NUMERIC_VALUE) });
2015-11-24
MacOS - How to prevent process "gamed" from running
On El Capitan MacOS:
launchctl unload -w /System/Library/LaunchAgents/com.apple.gamed.plist
This undoes it:
launchctl load -w /System/Library/LaunchAgents/com.apple.gamed.plist
Before El Capital versions:
sudo defaults write /System/Library/LaunchAgents/com.apple.gamed Disabled -bool true
To undo/reverse the change:
sudo defaults delete /System/Library/LaunchAgents/com.apple.gamed Disabled
launchctl unload -w /System/Library/LaunchAgents/com.apple.gamed.plist
This undoes it:
launchctl load -w /System/Library/LaunchAgents/com.apple.gamed.plist
Before El Capital versions:
sudo defaults write /System/Library/LaunchAgents/com.apple.gamed Disabled -bool true
To undo/reverse the change:
sudo defaults delete /System/Library/LaunchAgents/com.apple.gamed Disabled
2015-11-15
MongoDB - Filter on a couple of fields (simple and using $and:[])
db.COLLECTION_NAME.find(
FIELD_NAME_1 : EXACT_VALUE_TO_COMPARE_TO
, FIELD_NAME_2 : EXACT_VALUE_TO_COMPARE_TO
);
db.COLLECTION_NAME.find(
$and: [
{ FIELD_NAME_1 : EXACT_VALUE_TO_COMPARE_TO }
, { FIELD_NAME_2 : EXACT_VALUE_TO_COMPARE_TO }
]
);
FIELD_NAME_1 : EXACT_VALUE_TO_COMPARE_TO
, FIELD_NAME_2 : EXACT_VALUE_TO_COMPARE_TO
);
db.COLLECTION_NAME.find(
$and: [
{ FIELD_NAME_1 : EXACT_VALUE_TO_COMPARE_TO }
, { FIELD_NAME_2 : EXACT_VALUE_TO_COMPARE_TO }
]
);
MongoDB - Filter on a given field, if it exists
db.COLLECTION_NAME.find(
$or: [
{ FIELD_NAME : { $exists : false}}
, { FIELD_NAME : EXACT_VALUE_TO_COMPARE_TO }
]
);
db.COLLECTION_NAME.find(
FIELD_NAME : { $ne : EXACT_VALUE_TO_COMPARE_TO }
);
2015-11-09
Ubuntu/Linux - Restore your home folder permissions
sudo chown -R "$USER:$USER" "$HOME"
This helps when, for example, Firefox won't start on Ubuntu 15.10... it seems to be due to permissions issues. It worked for me afterwards.
This helps when, for example, Firefox won't start on Ubuntu 15.10... it seems to be due to permissions issues. It worked for me afterwards.
2015-09-08
EXCEL - Count unique values in range
={ SUM( 1 / COUNTIF( A1:A100, A1:A100 ) ) }
PS. For this to work, the range (e.g. A1:A100) can not contain blank cells.
PSS. This is an array function... press CTRL+ENTER once written for it to work.
PS. For this to work, the range (e.g. A1:A100) can not contain blank cells.
PSS. This is an array function... press CTRL+ENTER once written for it to work.
MongoDB - Group stages example with match, group and sort
db.COLLECTION.aggregate([
// 1. Filter (optional)
{$match:
{
FIELD_1 : { $in: [ 'VALUE_1', 'VALUE_2' ] }
}
}
// 2. Group number one
, {$group :
{
_id: {
"FIELD_TO_GROUP_BY_1_ALIAS" : "$FIELD_TO_GROUP_BY_1"
, "FIELD_TO_GROUP_BY_2_ALIAS" : "$FIELD_TO_GROUP_BY_2"
}
, AGGREGATION_ALIAS : {$sum : "$FIELD_TO_AGGREGATE"}
}
}// group 1
// 3. Match (filter on the results of the previous aggregation)
, {$match :
{
population: {$gt : NUMERIC_VALUE }
}
}// match
// 4. Group by
, {$group :
{
_id: {"FIELD_TO_GROUP_BY_ALIAS" : "$FIELD_TO_GROUP_BY" }
, AGGREGATION_ALIAS : { $avg : "$FIELD_TO_AGGREGATE" }
}
}// group 2
// 5. Sort descending
, {$sort :
{
FIELD_OR_ALIAS_TO_SORT_BY : -1
}
}//sort
]);
// 1. Filter (optional)
{$match:
{
FIELD_1 : { $in: [ 'VALUE_1', 'VALUE_2' ] }
}
}
// 2. Group number one
, {$group :
{
_id: {
"FIELD_TO_GROUP_BY_1_ALIAS" : "$FIELD_TO_GROUP_BY_1"
, "FIELD_TO_GROUP_BY_2_ALIAS" : "$FIELD_TO_GROUP_BY_2"
}
, AGGREGATION_ALIAS : {$sum : "$FIELD_TO_AGGREGATE"}
}
}// group 1
// 3. Match (filter on the results of the previous aggregation)
, {$match :
{
population: {$gt : NUMERIC_VALUE }
}
}// match
// 4. Group by
, {$group :
{
_id: {"FIELD_TO_GROUP_BY_ALIAS" : "$FIELD_TO_GROUP_BY" }
, AGGREGATION_ALIAS : { $avg : "$FIELD_TO_AGGREGATE" }
}
}// group 2
// 5. Sort descending
, {$sort :
{
FIELD_OR_ALIAS_TO_SORT_BY : -1
}
}//sort
]);
2015-09-04
MongoDB - How to aggregate distinct children field into a single field array ($addToSet)
db.COLLECTION_NAME_HERE.aggregate([
{$group:
{
_id: "$FIELD_TO_GROUP_BY_NAME_HERE"
, AGGREGATED_FIELD_ALIAS : { $addToSet : "$FIELD_TO_AGGREGATE" }
}
}
]);
{$group:
{
_id: "$FIELD_TO_GROUP_BY_NAME_HERE"
, AGGREGATED_FIELD_ALIAS : { $addToSet : "$FIELD_TO_AGGREGATE" }
}
}
]);
2015-09-03
MongoDB - Aggregate (average) grouping by multiple fields
db.COLLECTION_NAME.aggregate([
{$group :
{
_id : {
"FIELD_1_ALIAS" : "$FIELD_1_NAME"
, "FIELD_2_ALIAS" : "$FIELD_2_NAME"
}
, AGGREGATION_RESULT_FIELD_ALIAS : {$avg : "$FIELD_NAME"}
}
}
]);
{$group :
{
_id : {
"FIELD_1_ALIAS" : "$FIELD_1_NAME"
, "FIELD_2_ALIAS" : "$FIELD_2_NAME"
}
, AGGREGATION_RESULT_FIELD_ALIAS : {$avg : "$FIELD_NAME"}
}
}
]);
2015-09-02
MacOS - Executing/sending the contents of a file to an executable program
cat /path/filename.txt | executable_program_here
In this example, sending the contents of a .js file, to mongodb.
cat script.js | mongo
In this example, sending the contents of a .js file, to mongodb.
cat script.js | mongo
MongoDB - Basic group by with a sum
db.COLLECTION_NAME.aggregate([
{$group:
{
_id : "$FIELD_TO_GROUP_BY",
RESULTING_SUM_FIELD_ALIAS : { $sum : "$FIELD_TO_SUM"}
}
}
]);
{$group:
{
_id : "$FIELD_TO_GROUP_BY",
RESULTING_SUM_FIELD_ALIAS : { $sum : "$FIELD_TO_SUM"}
}
}
]);
2015-08-26
2015-08-25
MongoDB - Index management commands
db.COLLECTION_NAME.getIndexes();
db.COLLECTION_NAME.createIndex( { FIELD_NAME:1 } );
db.COLLECTION_NAME.dropIndex( { FIELD_NAME:1 } ); // use same creation signature
// Background index creation (no read locks, no write locks, a bit slower)
db.COLLECTION_NAME.createIndex( { FIELD_NAME:1 }, {background:true} );
// Compound index creation
db.COLLECTION_NAME.createIndex( { FIELD_NAME:1, FIELD_NAME_2:-1 } );
// Sub-document index creation
db.COLLECTION_NAME.createIndex( { 'FIELD_NAME.SUB_FIELD_NAME':1} );
PS. Number 1 represents an ascending-order index, while -1 a descending-order one.
MongoDB - Explaining queries (plan and actual execution explanation)
db.COLLECTION_NAME.explain().find({FILTER_HERE});
// This will explain the actual execution (execute and explain)
db.COLLECTION_NAME.explain(true).find({FILTER_HERE});
// Including execution statistics (more verbose than regular explain)
db.COLLECTION_NAME.explain("executionStats").find({FILTER_HERE});
PS. The explain method does not only apply to .find() but also to .aggregate(), .count(), .group(), .remove(), .update()
// This will explain the actual execution (execute and explain)
db.COLLECTION_NAME.explain(true).find({FILTER_HERE});
// Including execution statistics (more verbose than regular explain)
db.COLLECTION_NAME.explain("executionStats").find({FILTER_HERE});
PS. The explain method does not only apply to .find() but also to .aggregate(), .count(), .group(), .remove(), .update()
MySQL - How to load a data text file into a table
LOAD DATA LOCAL INFILE "file_name_here.csv" INTO TABLE table_name_here FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES STARTING BY 'xxx' LINES TERMINATED BY '\r\n' IGNORE 0 LINES
PS. Most arguments are optional (e.g. lines starting by... to be used only if each line starts with a given string).
PS. Most arguments are optional (e.g. lines starting by... to be used only if each line starts with a given string).
MySQL - Run a .sql script directly in the command line
mysql -h localhost -u USER_HERE -p PASSWORD_HERE DATABASE_NAME_HERE < SCRIPT_FILE_NAME_HERE.sql
MySQL - How to optimize for large SQL files upload/import
(1) Change the value of the mysql configuration file variable "" from 1 to 0.
(2) If using InnoDB, read the configuration file and do use up to 80% of the machine's available RAM.
(3) Start your .sql script with "SET autocommit = 0;" and end it with a manual "commit;".
(4) If possible, make sure your script inserts rows in PK-order.
(5) Avoid using IDE's... mysql command-line is usually faster and more reliable. Loading using MySQL Workbench or Sequel Pro (mac), often produces random disconnections, or interruptions due to strange errors such as the database detecting a different encoding than the one specified or automatically detected.
2015-08-23
VIM - How to save the file as, with a particular encoding
:write ++enc=utf-8 TARGET_FILE_NAME.EXTENSION
VIM - How to save each line of a file into a separate file, numbered
:g/./execute '.w '.line('.').'.FILE_EXTENSION'
MacOS - How to split a file for a given row number range
cat LARGE_FILE_NAME.TXT | awk 'NR >= 999 && NR <= 1500 { print }' > SMALLER_FILE_NAME.TXT
PS. Where 999 is the line to start with, and 1500 the last line you want.
PS. Where 999 is the line to start with, and 1500 the last line you want.
2015-08-22
VIM - How to go to the end of the file (last line of the file)
(1) Press ESC to get out of INSERT mode.
(2) Type G
(3) Press ENTER
PS. To go back to the beginning issue this command G1
(2) Type G
(3) Press ENTER
PS. To go back to the beginning issue this command G1
MacOS - How to replace all lines of a text file matching a string (in terminal)
grep -vwE "(string to replace)" sourcefile.txt > destinationfile.txt
2015-08-19
MongoDB - Performance optimization tips
- Issue updates to specific fields, avoid retrieving the entire document.
- Avoid negation in queries... MongoDB does not index the absence of values.
- Test/explain every query in your application with explain().
- Eliminate unnecessary indexes.
- Use covered queries when possible (queries that exclusively retrieve values that are indexed)
- Make sure to use the latest available MongoDB drivers.
- Store all data for a record in a single document.
- Avoid large documents (use GridFS if you must).
- Manually pad your documents with fields that may later on exist. This helps the Mongo engine from having to move/re-write the document in a separate disk location.
- Avoid large indexed arrays.
- Avoid long field names (repeated field names add-up, and take space!).
- Avoid creating separate indexes for fields already found in compounded indexes.
- Store indexes in a separate disk/volume.
- Use EXT4 or XFS files systems (avoid EXT3).
- Use RAID10 (not RAID0, nor RAID5)
Taken from "Performance Best Practicers for MongoDB", MongoDB 3.0, August 2015.
MongoDB - Project (select) only a specific field from a sub-document
db.COLLECTION_HERE.find(
{FILTER_HERE}
,{
"SUB_DOC_FIELD_NAME.FIELD_NAME" : 1
}
)
{FILTER_HERE}
,{
"SUB_DOC_FIELD_NAME.FIELD_NAME" : 1
}
)
2015-08-18
MongoDB - Drop column, remove a field from all documents (records)
db.COLLECTION.update(
{
FIELD_NAME : { $exists : true }
}
, {
$unset : { 'FIELD_NAME' : 1 }
}
, {
multi : true
}
);
{
FIELD_NAME : { $exists : true }
}
, {
$unset : { 'FIELD_NAME' : 1 }
}
, {
multi : true
}
);
MongoDB - Array add to set (insert a value), unless it already exists, to an array type of field
db.COLLECTION_NAME.update(
{FILTER_HERE}
, {
$addToSet : {
FIELD_NAME_WITH_ARRAY : VALUE_HERE
}
}
);
{FILTER_HERE}
, {
$addToSet : {
FIELD_NAME_WITH_ARRAY : VALUE_HERE
}
}
);
MongoDB - Array pull all (multiple element deletion), based on value, from an array type of field
db.COLLECTION_NAME.update(
{FILTER_HERE}
, {
$pullAll:{
FIELD_NAME : [ VALUE_1, VALUE_2 ]
}
}
);
{FILTER_HERE}
, {
$pullAll:{
FIELD_NAME : [ VALUE_1, VALUE_2 ]
}
}
);
MongoDB - Array pull (remove) all elements with a given value, of an array type of field
db.COLLECTION_NAME.update( {FILTER_HERE}, { $pull:{FIELD_NAME_WITH_ARRAY : VALUE_HERE } } );
MongoDB - Array pop the first element of an array type of field
db.COLLECTION_NAME.update( {FILTER_HERE}, { $pop:{FIELD_NAME_WITH_ARRAY: -1 } } );
MongoDB - Array pop (remove, delete) last item of an array type of field
db.COLLECTION_NAME.update( {FILTER_HERE}, { $pop:{FIELD_NAME_WITH_ARRAY:1} } );
MongoDB - Array push all (insert multiple values) into an array type of field
db.COLLECTION_NAME.update( {FILTER_HERE}, { $pushAll:{FIELD_NAME_WITH_ARRAY:[ VALUE_1, VALUE_2, VALUE_N ]} } );
MongoDB - Array push (insert, add) a value into an array type of field
db.COLLECTION_NAME.update( {FILTER_HERE}, { $push:{FIELD_NAME_WITH_ARRAY: VALUE_HERE } } );
MongoDB - Set the value of a specific item (item number 2) within an array-type of field
db.COLLECTION_NAME.update( {FILTER_HERE}, { $set:{"FIELD_NAME_WITH_ARRAY.ARRAY_INDEX_HERE": VALUE_HERE } } );
2015-08-17
MacOS - Inspect a json file using python and 'more' in terminal
cat FILE_NAME.FILE_EXTENSION | python -m json.tool | more
2015-08-14
PHP - Catch all query-string values and add them into variables automatically
parse_str($_SERVER['QUERY_STRING']);
2015-08-13
MongoDB - How to drop a collection (completely remove a collection/table)
db.COLLECTION_NAME.drop();
// Indexes are lost.
// Returns false if the collection is not found.
// Indexes are lost.
// Returns false if the collection is not found.
MongoDB - How to remove all documents (row) from a collection (table)
db.COLLECTION_NAME.remove({});
// This works only on version 2 and below (no document being passed).
db.COLLECTION_NAME.remove();
// This works only on version 2 and below (no document being passed).
db.COLLECTION_NAME.remove();
MongoDB - Count records matching a given filter criteria
db.COLLECTION_NAME.find({FIELD_NAME:'VALUE_TO_MATCH'}).count();
OR
db.COLLECTION_NAME.count({FIELD_NAME:'VALUE_TO_MATCH'});
OR
db.COLLECTION_NAME.count({FIELD_NAME:'VALUE_TO_MATCH'});
MongoDB - How to list all collections
db.getCollectionNames();
// This gets only the first found collection
db.getCollectionNames()[1];
// This ignores the system collection called "system.indexes"
db.getCollectionNames().filter(function(x){return x!=='system.indexes'});
// This gets only the first found collection
db.getCollectionNames()[1];
// This ignores the system collection called "system.indexes"
db.getCollectionNames().filter(function(x){return x!=='system.indexes'});
2015-08-12
Sublime Text - Increased readability custom user settings
{
"font_size": 12
, "caret_style": "phase"
, "highlight_line": true
, "fade_fold_buttons": false
, "bold_folder_labels": true
}
"font_size": 12
, "caret_style": "phase"
, "highlight_line": true
, "fade_fold_buttons": false
, "bold_folder_labels": true
}
MongoDB - Retrieve docs where a given field ends with a certain letter (regex)
db.COLLECTION_NAME.find( { FIELD_NAME:{ $regex:"e$" } } );
MongoDB - Find documents where a field does not exist
db.COLLECTION_NAME.find( { FIELD_NAME:{$exists:false} } );
MongoDB - Find (select) documents where a field value is a string
db.COLLECTION_NAME.find( { FIELD_NAME:{$type:2} } );
2015-08-11
CSS - How to use Prettify
Automatic code highlighting for your web pages.
Download the zip/tar file and unzip and place within your website.
http://code.google.com/p/google-code-prettify/
Then add the CSS and the main JavaScript file to your page, along with an onLoad event trigger in the BODY tag.
Download the zip/tar file and unzip and place within your website.
http://code.google.com/p/google-code-prettify/
Then add the CSS and the main JavaScript file to your page, along with an onLoad event trigger in the BODY tag.
TSQL - User defined function to return the greater of two given integer numbers
/*
--
Execution
select
dbo.getGreaterInt( '201201', '201112' );
select
dbo.getGreaterInt( '201201', null );
select
dbo.getGreaterInt( null, '201201' );
select
dbo.getGreaterInt( null, null );
*/
ALTER function [dbo].[getGreaterInt](
@int1 int
, @int2 int
)
returns int
as
begin
return(
case
when isnull(@int1,0) >= isnull(@int2,0)
then @int1
else @int2
end
);
end
;
GO
PYTHON - How to install a .whl file (offline package installation)
(1) Download the wheel file (.whl... from, for example https://pypi.python.org).
(2) Run this command:
pip install PACKAGE_FILE_NAME.whl
(2) Run this command:
pip install PACKAGE_FILE_NAME.whl
2015-08-03
MySQL - Filter for rows with values starting with a lowercase letter
select *
from TABLE_NAME
where CONVERT( FIELD_NAME USING latin1) COLLATE latin1_general_cs regexp '^[a-z]'
;
from TABLE_NAME
where CONVERT( FIELD_NAME USING latin1) COLLATE latin1_general_cs regexp '^[a-z]'
;
MySQL - How to filter in a 'case sensitive' way
select *
from TABLE_NAME
where CONVERT( FIELD_NAME USING latin1) COLLATE latin1_general_cs like 'a%'
;
(1) You convert your field to a latin collation (if not already... from UTF8 for example)
(2) You collate it using a case sensitive collation (..._cs)
from TABLE_NAME
where CONVERT( FIELD_NAME USING latin1) COLLATE latin1_general_cs like 'a%'
;
(1) You convert your field to a latin collation (if not already... from UTF8 for example)
(2) You collate it using a case sensitive collation (..._cs)
2015-07-28
PYTHON - List all first-level subdirectories
import os
os.chdir( '/YOUR/PATH/' )
for folder in sorted( os.listdir( os.getcwd() ) ):
if not os.path.isfile( folder ):
print folder
os.chdir( '/YOUR/PATH/' )
for folder in sorted( os.listdir( os.getcwd() ) ):
if not os.path.isfile( folder ):
print folder
PYTHON - Extract string content between parenthesis (without regex)
print YOUR_STRING[ YOUR_STRING.find( '(' ) + 1 : YOUR_STRING.find( ')' ) ]
MacOS - How to toggle between windows of the same app
COMMAND-`
PS. To toggle between apps: COMMAND-TAB
PS. To toggle between apps: COMMAND-TAB
2015-07-27
PYTHON - How to determine the python version your code is running on
import sys
print sys.version
print sys.version_info
print sys.version
print sys.version_info
2015-07-26
PYTHON - How to curl into a file?
(1) Install the pycurl package:
easy_install pycurl
(2) Sample script:
import pycurl
c = pycurl.Curl()
c.setopt(c.USERAGENT, 'Mozilla/5.0 (compatible; pycurl)' )
c.setopt(c.URL, 'http://website.com/page1.html')
with open('target_file_name.html', 'w') as f:
c.setopt( c.WRITEFUNCTION, f.write )
c.perform()
f.close()
easy_install pycurl
(2) Sample script:
import pycurl
c = pycurl.Curl()
c.setopt(c.USERAGENT, 'Mozilla/5.0 (compatible; pycurl)' )
c.setopt(c.URL, 'http://website.com/page1.html')
with open('target_file_name.html', 'w') as f:
c.setopt( c.WRITEFUNCTION, f.write )
c.perform()
f.close()
2015-07-22
MacOS - How to customize your terminal prompt, adding a line after each command
(1) Open or create ~\.bash_profile (a text flat file at the root of your MacOS profile).
(2) Enter this line (cusomize... the dashes can be anything you want... the \n is a carriage return+line feed):
export PS1="----------------\n$"
(2) Enter this line (cusomize... the dashes can be anything you want... the \n is a carriage return+line feed):
export PS1="----------------\n$"
2015-07-17
MongoDB - Import CSV with field values containing double quotes
You have to escape the double quote.
Do not escape it with a backslash (\").
Instead, escape it with another double quote ("").
Do not escape it with a backslash (\").
Instead, escape it with another double quote ("").
MongoDB - How to rename a field
db.COLLECTION_NAME.update( {}, { $rename: {"OLD_FIELD_NAME": "NEW_FIELD_NAME"} }, false, true);
MongoDB - How to import a CSV file
mongoimport --db DATABASE_NAME --collection COLLECTION_NAME --type csv --headerline --file /path/to/file/FILE_NAME.csv
MacOS - Show hidden files and folders
defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder
2015-07-15
PYTHON - Randomly choose an list/array element (numeric or text)
import random
print( random.choice( [1,2,3] ) )
print( random.choice( ['a','b','c'] ) )
print( random.choice( [1,2,3] ) )
print( random.choice( ['a','b','c'] ) )
2015-07-13
PYTHON - Twitter search and results parsing
# 1. Twitter authentication credentials (get yours at dev.twitter.com)
consumerKey = ''
consumerSecret = ''
# 2. Create a twython object (installation command: "sudo easy_install twython")
from twython import Twython
twitter = Twython( consumerKey, consumerSecret )
# 3. Display tweets' text matching a given search term
term = 'chapo'
for status in twitter.search(q=term)["statuses"]:
print status["text"]
consumerKey = ''
consumerSecret = ''
# 2. Create a twython object (installation command: "sudo easy_install twython")
from twython import Twython
twitter = Twython( consumerKey, consumerSecret )
# 3. Display tweets' text matching a given search term
term = 'chapo'
for status in twitter.search(q=term)["statuses"]:
print status["text"]
PYTHON - How to most efficiently concatenate strings
from cStringIO import StringIO
string = StringIO()
string.write( 'First sentence.' )
string.write( 'Second string.' )
string.write( 'Thrid' )
print( string.getvalue() )
string = StringIO()
string.write( 'First sentence.' )
string.write( 'Second string.' )
string.write( 'Thrid' )
print( string.getvalue() )
PYTHON - How to parse a JSON string
# 1. Original JSON String
text = """{ "count": 2, "record": [ {"lemma":"God"}, {"lemma":"Men"} ] }"""
# 2. Text to JSON object
import json
j = json.loads(text)
# 3. Parse a top-level property
print 'count: ' + str( j['count'] )
# 4. Traverse multiple records
for item in j['record']:
print( item['lemma'] )
text = """{ "count": 2, "record": [ {"lemma":"God"}, {"lemma":"Men"} ] }"""
# 2. Text to JSON object
import json
j = json.loads(text)
# 3. Parse a top-level property
print 'count: ' + str( j['count'] )
# 4. Traverse multiple records
for item in j['record']:
print( item['lemma'] )
PYTHON - Break a paragraph into a sentences list
import re
text = 'This is. A paragraph with. Three sentences.'
sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
for item in sentences:
print(item)
text = 'This is. A paragraph with. Three sentences.'
sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
for item in sentences:
print(item)
PYTHON - Getting text from a website
import requests
txt = requests.get("http://lipsum.com/robots.txt").text
print( txt )
txt = requests.get("http://lipsum.com/robots.txt").text
print( txt )
2015-07-11
MySQL - Convert table's charset and collation to utf8/utf8_general_ci (for English, Spanish and French characters)
ALTER TABLE table_name_here CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
MySQL - Charset and collation that works for English, Spanish and French characters
CHARACTER SET: utf8
COLLATION: utf8_general_ci
COLLATION: utf8_general_ci
2015-07-09
EXCEL - Figure out if a cell contains a number (any digit)
=COUNT( FIND( {0,1,2,3,4,5,6,7,8,9}, A2 ) ) > 0
2015-07-08
PYTHON - Display and change current working directory
import os
print( os.getcwd() )
os.chdir( os.path.dirname("C:/folder/subfolder") )
print( os.getcwd() )
print( os.getcwd() )
os.chdir( os.path.dirname("C:/folder/subfolder") )
print( os.getcwd() )
2015-05-20
PHP - How to run PHP pages in your local Ubuntu
These commands will install Apache and its PHP library.
sudo apt-get update
sudo apt-get install apache2
sudo apt-get install php5 libapache2-mod-php5
sudo apt-get update
sudo apt-get install apache2
sudo apt-get install php5 libapache2-mod-php5
2015-05-18
PYTHON - Determine all possible factor combinations for a given number, from a given numeric array
Check this out at GitHub.com
https://github.com/falconerandloillc/factors
# 1. Sample call
#print( factors( 240, [2,3,4,5,6,7,8,9,10,11,12,24]
) );
# 2. The function...
|
import
time
|
def
factors(target,
arr):
|
# Parameters test/fix
|
if target == None or target == 0 or arr == None or len(arr) < 2:
|
return
None
|
# Declarer
|
start = int(round(time.time() * 1000))
|
global iterations
|
global combinations
|
iterations=0
|
combinations=[]
|
# Keep only non-zero numbers
|
arr = [x for x in arr if isinstance(x, float) or isinstance(x, int)]
|
if len(arr) <= 1:
|
return
None
|
# Sort
|
arr.sort()
|
# Recurse each array element
|
for j in range(len(arr)):
|
i =
j
|
r =
[]
|
def
recurse(v,
i):
|
global
iterations, combinations
|
while
i <
len(arr)
and
(v*arr[i-1]) < abs(target):
|
iterations += 1
|
r.append(arr[i])
|
recurse(v*arr[i], i+1)
|
i += 1
|
r.pop()
|
if
v ==
target and
len(r)
>
1:
|
combinations.append("".join(str(r)))
|
return
1
|
r.append(arr[j])
|
recurse(arr[j], j+1)
|
return {
|
"combinations": combinations
|
, "count": len(combinations)
|
, "time": str(int(round(time.time()*1000))-start) + " ms"
|
, "array": arr
|
, "target": target
|
, "iterations": iterations
|
}
|
TSQL - Company name cleanup, reducing a company's name to its essence
This can be very helpful when comparing two different lists of companies, matching them when no unique ID is available, and when the company names may be slightly different on both lists.
/*
--
Execution
select
dbo.getCompanyNameCleanForMatching( 'ABC Co. Ltd. L.L.C.');
*/
ALTER function [dbo].[getCompanyNameCleanForMatching]
(
@strCompanyName varchar(max)
)
returns varchar(max)
as
begin
return(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
lower(@strCompanyName)
, '.'
, ''
)
, ','
, ''
)
, '*'
, ''
)
, '('
, ''
)
,
')'
,
''
)
,
' llc'
,
''
)
, ' incorporated'
, ''
)
, ' inc'
, ''
)
, ' limited partnership'
, ''
)
, ' lp'
, ''
)
, ' limited'
, ''
)
, ' ltd'
, ''
)
, 'corporation'
, ''
)
, ' corporate'
, ''
)
, ' corp'
, ''
)
,
'ultimate parent'
,
''
)
,
' co '
,
''
)
,
' par '
, ''
)
, 'group'
, ''
)
, 'company'
, ''
)
, 'international'
, ''
)
, 'holdings'
, ''
)
, 'communications'
, ''
)
, 'industries'
, ''
)
, 'broadcasting'
, ''
)
, 'financial'
, ''
)
, 'products'
, ''
)
, 'technologies'
, ''
)
, 'services'
, ''
)
, 'acquisition'
, ''
)
, 'systems'
, ''
)
, 'manufacturing'
, ''
)
, 'enterprises'
, ''
)
, 'entertainment'
, ''
)
, 'manufacturing'
, ''
)
, 'processing'
, ''
)
, '-dip'
, ''
)
, 'bank loan'
, ''
)
, 'cfs'
, ''
)
, 'hfs'
, ''
)
, 'cif'
, ''
)
, 'global sponsor finance'
, ''
)
, ' - '
, ''
)
, '- '
, ''
)
, ' -'
, ''
)
, '-'
, ''
)
);
end
Subscribe to:
Posts (Atom)