Archive

Archive for the ‘python’ Category

Simplicity is the final achievement.

Simplicity is the final achievement. After one has played a vast quantity of notes and more notes, it is simplicity that emerges as the crowning reward of art.

Frederic Chopin

Categories: python Tags: , ,

how to become a data scientist in Python

In this post I found a detailed learning path on how to become proficient in data science.

They also made an infographic (see this post).

data_scienceClick to see the full version.

(Here is a backup of the image if the original site went down.)

creating a Python 3 virt. env. on Ubuntu

Problem
Python 3 on Ubuntu is sometimes a total mess. I wanted to create a Python 3 virt. env., but I got this error:

$ virtualenv -p python3 venv
Running virtualenv with interpreter /usr/bin/python3
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 8, in 
    import base64
  File "/usr/lib/python3.4/base64.py", line 9, in 
    import re
  File "/usr/lib/python3.4/re.py", line 324, in 
    import copyreg
  File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in 
    from copy_reg import *
ImportError: No module named 'copy_reg'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 53, in apport_excepthook
    if not enabled():
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 24, in enabled
    import re
  File "/usr/lib/python3.4/re.py", line 324, in 
    import copyreg
  File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in 
    from copy_reg import *
ImportError: No module named 'copy_reg'

Original exception was:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 8, in 
    import base64
  File "/usr/lib/python3.4/base64.py", line 9, in 
    import re
  File "/usr/lib/python3.4/re.py", line 324, in 
    import copyreg
  File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in 
    from copy_reg import *
ImportError: No module named 'copy_reg'

Awesome! :(

Solution
I found a working solution here. The following command let me create a Python 3 virt. env.:

python3 -c 'import sys; del sys.argv[0]; s = sys.argv[0]; exec(open(s).read(), {"__file__": s, "__name__": "__main__"})' `which virtualenv` -p python3 venv 

Life on Arch / Manjaro is easier. There Python 3 has been the default for years.

PyCon US 2015 videos

April 19, 2015 Leave a comment

PyCon US 2015 took place in Montréal, Canada (just like last year). The videos are being uploaded to pyvideo.org. I think we need some more weeks till all the videos get uploaded but at the moment of writing this post there are already 134 videos available.

Here is a nice script of mine (pyvideo_popularity.py) that orders presentations by popularity.

Categories: python Tags: , ,

Talk Python To Me: a Python podcast

April 10, 2015 Leave a comment

I just found this Python podcast: http://www.talkpythontome.com/. In the latest episode you can find an interview with “Jesse Davis from MongoDB. Jesse is the maintainer for a number of popular open-source projects including the Python MongoDB driver known as PyMongo and Mongo C (for C/C++ developers, yes you read right! C developers). Jesse discusses how interesting it is to write both Python and C code and how it reawakens part of the brain.” (source)

Categories: podcast, python Tags: , ,

UTC now → timestamp → UTC time

January 25, 2015 Leave a comment

Problem
Say you have an application and you want to store the date/time of an event. Later you want to see this date/time.

Solution
Take the current UTC date/time and convert it to a timestamp, which is an integer. Store this value. Then read it and convert it back to UTC date/time.

>>> import datetime
>>>
>>> utcnow = datetime.datetime.utcnow()
>>> utcnow
datetime.datetime(2015, 1, 25, 18, 10, 41, 803198)
>>> ts = int(utcnow.timestamp())
>>> ts
1422205841
>>> datetime.datetime.fromtimestamp(ts)
datetime.datetime(2015, 1, 25, 18, 10, 41)
>>>
Categories: python Tags: , , ,

[mongodb] get a random document from a collection

January 24, 2015 Leave a comment

Problem
From a MongoDB collection, you want to get a random document.

Solution

import random

def get_random_doc():
    # coll refers to your collection
    count = coll.count()
    return coll.find()[random.randrange(count)]

Pymongo documentation on cursors: here.

Categories: python Tags:
Follow

Get every new post delivered to your Inbox.

Join 90 other followers