Archive
Python performance tips
Relative import in Python
The following post is based on nosklo’s answer who explained relative imports very nicely in this thread.
Imagine the following project structure:
main.py
setup.py
app/ ->
__init__.py
package_a/ ->
__init__.py
module_a.py
package_b/ ->
__init__.py
module_b.py
Problem: how to use module_b.py from module_a.py?
Solution:
”
- you run:
python main.py main.pydoes:import app.package_a.module_amodule_a.pydoes:import app.package_b.module_b
Alternatively, 2 or 3 could use: from app.package_a import module_a
That will work as long as you have app in your PYTHONPATH. main.py could be anywhere then.”
jabbapylib, a lightweight Python library
Update (20120125): jabbapylib is pushed to PyPI, thus it can be installed via pip: “sudo pip install jabbapylib“. For more info see the README file.
———-
I am proud to announce my first Python library called “jabbapylib” (available here).
In this library I collect functions that I use often in my projects. Instead of repeating them in each project, I’ve decided to group them in a library. Thus, this library falls in the “miscellaneous” category, i.e. it’s not specific to a given problem.
It’s in an early stage, so over time it’ll change a lot, I think. I plan to extend it in the near future.
For installation instructions, see the README file (at the bottom of the page). The source files are documented and at the bottom of each source you can find some examples. Feedbacks are welcome.
Only tested on Linux!
Update (20110907)
I’ve made another project called JabbaPyLib-in-Action. In this I will group smaller projects that use jabbapylib. If you want to see how to use jabbapylib in practice, check out JabbaPyLib-in-Action.
Figure out the HOME directory
Problem
You want to figure out the location of the HOME folder of the current user.
Solution
>>> import os
>>> os.path.expanduser('~')
'/home/jabba'
Install Python 3.2 and/or Python 2.5 on Ubuntu 11.04
Update (20121120): Google App Engine supports Python 2.7 so there is no need any more to install Python 2.5. I consider this post outdated.
Chad Lung has a nice post on installing Python 3.2 from source on Ubuntu 11.04. The future versions of Ubuntu will come with Python 3, so maybe it’s a good time to start to discover Python 3 a bit.
Notes
The package “python3.2” exists in the Ubuntu repositories but it’s not up-to-date. If you want the latest version of Python, you’ll have to install it from source.
If you install Python 3.2 from source, make sure to install it with “./configure; make; sudo make altinstall“, where the emphasis is on altinstall. This way Python 3.2 will be installed next to your existing 2.x version(s), so your system won’t be messed up.
To figure out your exact Python version, you can do this:
>>> import sys >>> print(sys.version_info[:]) (3, 2, 0, 'final', 0)
Install Python 2.5 (update, 20110926)
Edit (20111112): Google App Engine now supports Python 2.7! At the moment it’s experimental but it works. So it’s very likely you don’t need Python 2.5 at all.
I wanted to try Google App Engline but it runs on Python 2.5 on the production servers at Google. So it’s better to test your applications locally with Python 2.5 too, otherwise there is a good chance that you develop something that runs fine on your machine but breaks at Google.
So, how to install Python 2.5 keeping the newer Python versions too? I tried the method that I described above with version 3.2 but “make” failed. But in this post I found a PPA from where you can install Python 2.5 easily. Steps to follow:
sudo add-apt-repository ppa:fkrull/deadsnakes sudo apt-get update sudo apt-get install python2.5
Now if you want to use Python 2.5, just modify the first line of your scripts:
#!/usr/bin/env python2.5
Update: I figured out how to compile 2.5 from source. “make” produced the following error message: “/usr/include/sqlite3.h: version 3.7.4”. After “configure”, open Makefile and edit this line:
# before: # LDFLAGS= # after: LDFLAGS= -L/usr/lib/i386-linux-gnu
After this compilation was done successfully with “make”. This tip is from here.
Linux: Python text-to-speech
This is the Linux version of this post.
#!/usr/bin/env python
import os
from time import sleep
text = "text to speak"
cmd = 'espeak "{0}" 2>/dev/null'.format(text)
os.system(cmd)
sleep(1)
os.system(cmd)
Decimal expansion of the digits of a fraction
Problem
If you want to expand the digits of a fraction to arbitrary precision (for instance “find 100 digits of the fraction 1/7“), floating point division won’t work since its precision is limited:
>>> 1/7.0 0.14285714285714285
This problem is related to Problem 26 in Project Euler.
Solution
Let’s see the fraction 1/7. 1 divided by 7 is 0, the remainder is 1. Now repeat the following steps: multiply the remainder (here 1) by 10 and divide it by 7 (result: 1), the remainder is 3. Multiply 3 by 10 and divide by 7, etc. See the process in the following table:
(1 * 10) / 7 = 1 * 7 + 3 (3 * 10) / 7 = 4 * 7 + 2 (2 * 10) / 7 = 2 * 7 + 6 (6 * 10) / 7 = 8 * 7 + 4 (4 * 10) / 7 = 5 * 7 + 5 (5 * 10) / 7 = 7 * 7 + 1 (1 * 10) / 7 = 1 * 7 + 3 (3 * 10) / 7 = 4 * 7 + 2
The numbers in bold are exactly the first few digits of 1/7 = 0.14285714…
Python code #1
Here is the implementation of this method:
#!/usr/bin/env python
PRECISION = 100
SHOW_ZEROS = False
def decimal_expansion(a, b):
q = a/b
r = a%b
s1 = str(q)
l2 = []
for i in range(PRECISION):
a = r * 10
q = a/b
r = a%b
l2.append(q)
if r == 0 and not SHOW_ZEROS:
break
s2 = ''.join([str(x) for x in l2])
return s1, s2
def main():
a = 1
b = 7
s1, s2 = decimal_expansion(a, b)
print "{0}.{1}".format(s1, s2)
if __name__ == "__main__":
main()
Python code #2
There is an easier way: the standard library contains a module called “decimal” that provides support for decimal floating point arithmetic.
#!/usr/bin/env python
from decimal import getcontext, Decimal
PRECISION = 100
def main():
a = 1
b = 7
getcontext().prec = PRECISION
result = Decimal(a) / Decimal(b)
s = str(result)
print s
if __name__ == "__main__":
main()
Credits
I’ve read about this decimal expansion method in this post.
