Check if python version is 64 or 32 bit

Just do this following in python interpreter or python source code file:

import struct;print struct.calcsize("P") * 8

You will get 64 or 32 to indicate which python version do you have installed.
That’s it, now go and rock python!
Or comment below if you know a better way of doing that 🙂

How to check the version of a python module?

You only need to have pip installed. Once it’s done, you simply need to run the following command:

$ pip freeze |grep module_name

Where the output would be in format:

scikit-learn==0.14.1

For instance, if you want to get the version of scikit-learn installed on your system, run this command:

$ pip freeze |grep scikit-learn

For windows users, you can substitute grep with findstr

If you know another way to do this, please share it in the comments below.

How to find files owned by specific user in Linux

Find file owned by user

The syntax is:

find directory-location -user {username} -name {file-name}

Where,

  • directory-location : Locate files or directories in this directory location.
  • -user { user-name } : Find the file belongs to user.
  • -name {file-name} : File name or pattern.

example :

find  / -user1

find all files owned by user1

cheers,

How to convert big int timestamp to datetime in mysql

Using bigint timestamp in application databases is widespread probably because it’s handier to handle an int datatype that to use a system date format. Also because storing an int is more economical than storing a bare system date format.

But, for the purposes of reporting and dashboarding it’s more meaningful to use date formats than to use timestamps, therefore we require a mechanism to convert timestamps to datetime format.

In this tutorial we will see one of several ways, in which we can get date time out of a timestamp.

In fact, mysql comes with a handy function, FROM_UNIXTIME that can convert timestamps into date time.

For instance, say that you have the timestamp 1376750476349 and you want to convert it into date time. You could simply do the following if your timestamp is in seconds.

select FROM_UNIXTIME(timestamp_value);

however, and this is a recurrent problem for many users of the above function, if your timestamp is say in milliseconds, you will have to perform the required conversions to seconds before using the function. So, in the case where the timestamp is in milliseconds you could do the following to get the datetime:

select FROM_UNIXTIME(timestamp_value/1000);

Hope this helps. Enjoy!

How to convert SQL data into a csv file in mysql

CSV is a standard in data analysis and almost certainly any tool that has anything to do with data supports importing data from CSV files.

When working with large databases it might be, sometimes, handy to export a subset of the data for preliminary examination, therefore you might need to export data from a sql database into a CSV file.

In this tutorial we will see, how exporting data from a mysql database into a CSV file can be done.

In fact, this is can be done quite easily. For instance, say that we have a table called students. Without having to precisely know the table description, you can export the content of the table into a CSV file with the following command:

SELECT *
INTO OUTFILE '/tmp/students.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\' LINES TERMINATED BY '\n' FROM students
  • FIELDS TERMINATED BY

CSV stands for Comma Separated Values, but comma is not an exclusive separator, in fact values in a CSV file can be separated with a semi-colon (;), with a tabular (”    “) or any other character that is supported by the tools to which you want to import the data from the CSV file later. You can use the command FIELDS TERMINATED BY In order to define which separator you want to use.

  • ENCLOSED BY , ESCAPED BY
  1. you can use ENCLOSED BY in order to say that you want the value to be enclosed with the “” specifier.
  2. you can use ESCAPED BY in order to define which character to use in order to escape special character within CSV file values.

Enjoy!

 

Hash-tables in Javascript

Coming from a java background, you might be tempted to apply the same concept and data structures in javascript. It turns out that, the conversion of java concepts/datastructure into javascript is possible, however it might be somewhat in some cases and fairly simple in other.

Fortunately, it is fairly simple to use hash-tables in javascript. In fact, everything in javascript is an object, but is there a difference between an object and a hashtable, let’s recall their definition and see if there is any:

  • a hashtable:  is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.
  • a javascript object: is an associative array that holds properties and methods of a particular entity.

Briefly, javascript objects are simply associative arrays as much as the hashtable in java are. So, if you can use an object and hashtable terms to describe the same entity in javascript.

For further notes on the subjects, check these discussions:

Cheers,

get and post in python with the requests module: quick example

This post provides an easier solution than the one based on urllib module here.

Story:

you want to handle post and get requests quickly without the need to go into the low level of socket handling or managing the details of the HTTP request.

Steps: 

  • Handle Get: Example of grabbing data with a get request and sending it with another get
import requests
url = "http://google.com"
r = requests.get(url)
result = r.text

print result
  • Handle Post: Example of grabbing data with a get request and sending it with post request
import requests

url = "url to contact"
cookies1 = {'cookie1_name': 'cookie_value', 'cookie1_name': 'cookie_value'}
data1= {'username': username, 'password': current_pass}
r = requests.post(url, data=data1, cookies=cookies1)
result = r.text
print result

For more information check the official documentation for the library:

Enjoy!

UNIX wildcards

Unix provides a subset of handy characters that allow users to go beyond handling a given file or directory to handling a subset of files or directories where the name matches a certain criterion. Wildcards are the primary assets for this kind of file handling. Let’s discuss them with some examples.

  • The * wildcard 

The * character will match against none or more character(s) in a file (or directory) name. For example:

 ls list* 

will match any file/directory name that starts with list including but not restricted to: list, lists, listed…

 
ls *list 

The second example however will match all the file/directory names that ends with list. Including but not restricted to: list, The_list…

  • The ? wildcard

The ? wildcard will match exactly one character. For instance, the following example will match all the keywords: house, mouse but not grouse.

 
grep ?ouse myFile.txt  
  • The [] wildcard:

The [] wildcard is used to match a range of characters. With the [] wildcard comes two more characters to denote a variation to the original behavior:

  1. – to denote a range
  2. ! to denote an exclusion

For example

[abc]?? 3 character filename beginning with “a”, “b”, or “c”.
[1-9][A-Z] 2 character filename starting with a number, and ending with an uppercase letter.
[!A-Z]?? 3 character filename that does not begin with an uppercase letter.
*e[0-9]f any file ending with “e”, a single number, and “f”.

Further readings:

Enjoy!