Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, 14 June 2018

I got the following error when installing matplotlib:

sudo python -mpip install -U matplotlib

Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-t_g0M1/subprocess32/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-YcuLqR/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-t_g0M1/subprocess32/

I fixed it by installing python-dev:
sudo apt-get install python-dev

ref:
https://github.com/google/python-subprocess32/issues/38

Thursday, 23 November 2017

Producer Consumer Model in Python

All credits go to Akshar Raaj

Note that if Queue is used, queue itself is threading safe to use because Queue encapsulates the behaviour of Condition, wait(), notify(), acquire() etc.

ref:
http://agiliq.com/blog/2013/10/producer-consumer-problem-in-python/

Wednesday, 22 November 2017

Install Python 3 Packets

By default if you install something by
      pip install some_packet
I got the packet in Python 2.7 installed.

But if a packet, say pyusb, is a dependent to another packet that has be on Python 3, say pystlink, I will get:

Traceback (most recent call last):
  File "pystlink.py", line 4, in <module>
    import lib.stlinkusb
  File "/home/boris/Softwares/EmbeddedSystem/pystlink/pystlink-master/lib/stlinkusb.py", line 1, in <module>
    import usb.core
ImportError: No module named 'usb'

If I try to go to Python 3 and import usb, I would get the same output. Therefore, I need to install pyusb in Python 3 and here is how.

sudo apt-get install python3-pip
sudo pip3 install MODULE_NAME

ref:
https://stackoverflow.com/questions/10763440/how-to-install-python3-version-of-package-via-pip-on-ubuntu

Tuesday, 5 September 2017

The Django Book Study -- Installing Django

I stucked a bit at this part:

To use this new Python virtual environment, we have to activate it, so let’s go back to the command prompt and type the following:
env_mysite\scripts\activate
This will run the activate script inside your virtual environment’s \scripts folder. You will notice your command prompt has now changed:
(env_mysite) C:\Users\Nigel\OneDrive\Documents\mysite_project>
The (env_mysite) at the beginning of the command prompt lets you know that you are running in the virtual environment. Our next step is to install Django.
Apparently, this is how it goes in Windows. In Linux, we would have to do the following:
source bin/activate
And then we can see:
boris@boris-D630:~/workspace/mysite_project/env_mysite$ source bin/activate
(env_mysite)boris@boris-D630:~/workspace/mysite_project/env_mysite$


ref:
https://pypi.python.org/pypi/virtualenv/1.8.2

Sunday, 11 October 2015

Python Data Structure -- defaultdict()

When using the original Python data structure -- dict(), there will be exception for KeyError if the key doesn't exist when accessing by d[key]. But with difaultdict(), only with a default factory function, it will be used as the default value of the key if the key does not exist. 
# -*- coding: utf-8 -*-
from collections import defaultdict

members = [
    # Age, name
    ['male', 'John'],
    ['male', 'Jack'],
    ['female', 'Lily'],
    ['male', 'Pony'],
    ['female', 'Lucy'],
]

result = defaultdict(list)
for sex, name in members:
    result[sex].append(name)

print result

# Result:
defaultdict(<type 'list'>, {'male': ['John', 'Jack', 'Pony'], 'female': ['Lily', 'Lucy']})

Wednesday, 9 September 2015

Visualize Data Density on 1 Dimension Axis


This source code clip is for visulizing data density on 1 axis like the following:



# reference:
# http://stackoverflow.com/questions/7352220/how-to-plot-1-d-data-at-given-y-value-with-pylab

import numpy as np
import matplotlib.pyplot as pp
import random

val = 0. # this is the value where you want the data to appear on the y-axis.
ar = np.random.random(size=40)
pp.plot(ar, np.zeros_like(ar) + val, '+')
pp.show()