GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • About
  • Learn Python
  • Archives
  • Contact
  • Google
  • Youdao
  • Xian Guo
  • Zhua Xia
  • My Yahoo!
  • newsgator
  • Bloglines
  • iNezha
  • Get the value of PI without a constant

This view

  • Original page
  • New address

GeezerVison is showing pythonadventures.wordpress.com — April 2012 – Python Adventures

Python Adventures

Welcome to the Jungle!
  • Home
  • About
  • Learn Python
  • Archives
  • Contact

Archive

Archive for April, 2012

Python-related videos indexed so you can find it

April 18, 2012 Jabba Laci Leave a comment

http://pyvideo.org/

Example: PyCon US 2012

Categories: python Tags: python conferences, python videos, pyvideo

Product of the elements in a list

April 14, 2012 Jabba Laci 1 comment

Design goal: should be just one line, without using explicit iteration over the list.

Sum of the elements:

>>> li = [2,3,5]
>>> sum(li)
10

Product of the elements:

>>> li = [2,3,5]
>>> from operator import mul
>>> reduce(mul, li)
30
Categories: python Tags: product, reduce, sum

Digits of PI (Part 2)

April 13, 2012 Jabba Laci Leave a comment

On the Python mailing list I got some great answers on how to generate the digits of PI. Here I sum them up.

Solution 1
Tichodroma forwarded me to http://mail.python.org/pipermail/edu-sig/2006-July/006810.html.

Quote:

Here's a generator I coded up based on a paper by Gibbons:
   

Click to access spigot.pdf

It's simple to code, but I think you have to read the paper to figure out what it's doing. (I just translated some code, so I really can't tell you :-) In the paper, this was done in a lazy functional language. I was mostly interested to see how it would translate to a Python generator. # pi.py -- imlementation of Gibbons' spigot algorithm for pi # John Zelle 4-5-06 def pi_digits(): """generator for digits of pi""" q,r,t,k,n,l = 1,0,1,1,3,3 while True: if 4*q+r-t < n*t: yield n q,r,t,k,n,l = (10*q,10*(r-n*t),t,k,(10*(3*q+r))/t-10*n,l) else: q,r,t,k,n,l = (q*k,(2*q+r)*l,t*l,k+1,(q*(7*k+2)+r*l)/(t*l),l+2) Here it is in action: >>> import pi >>> digits = pi.pidigits() >>> for i in range(30): print digits.next(), ... 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 >>> Since this uses long ints, it slows down considerably after a few thousand digits. You might want to use psyco when generating really "deep" digits. --John

It generates the digits of PI one after the other. It works well bit if you want lots of digits, it gets really slow.

Solution 2
E. Woiski suggested using the library SymPy.

sudo apt-get install python-sympy
>>> from sympy.mpmath import mp
>>> mp.dps = 1000   # number of digits
>>> +mp.pi    # str(mp.pi)

Very fast and simple. The only problem might be that you need to install sympy.

Solution 3 (update, 20121128)
One of my students called G. Szegedi came up with this solution:

from bigfloat import precision
import bigfloat

str_pi = str(bigfloat.atan2(+0.0,-0.0,precision(1000)))

With the bigfloat package you can do high precision floating-point arithmetic.

Categories: python Tags: bigfloat, digits of pi, Gibbons' spigot algorithm for pi, pi, sympy

Digits of PI (Part 1)

April 13, 2012 Jabba Laci Leave a comment

Problem
You want to work with the digits of PI. Why? For instance you want a new job (screenshot here if it got removed since then).

Solution
I like simple solutions. So instead of generating the digits, I simply fetched the data from the web. This is a fast, efficient, and painless approach of the problem :) Visit http://newton.ex.ac.uk/research/qsystems/collabs/pi/, where you can download several data files.

For the lazy pigs
I made a script that downloads the data, parses them, and returns the digits as a string. Here it is.

Usage (get the first 30 digits of PI after the dot):

#!/usr/bin/env python

from jabbapylib.math import pi

def main():
    digits = pi.get_digits_of(pi.PI3)    # get 10^3 = 1000 digits
    print digits[:30]

if __name__ == "__main__":
    main()

Output:

141592653589793238462643383279

jabbapylib is here

Categories: python Tags: digits of pi, pi

Quick Python Script Explanation for Programmers

April 11, 2012 Jabba Laci 1 comment

At http://coffeeghost.net/pybat/python_cheatsheet.png, I found the following cheat sheet:

Categories: python Tags: cheat sheet

Python Regular Expression Testing Tool

April 11, 2012 Jabba Laci Leave a comment

See http://www.pythonregex.com/. Cool stuff, it also generates source code. Happiness!

For a screenshot, click on the image on the right side.

Categories: python Tags: regex, regexp, regexp online, regular expressions

Python’s strftime directives

April 11, 2012 Jabba Laci Leave a comment

Python’s strftime directives is a thing that I don’t always need, but when I do, I get frustrated finding it on the net. So here is a shortcut: http://strftime.org/.

I also copy/paste it here for future references:

%a 	Locale’s abbreviated weekday name.
%A 	Locale’s full weekday name.
%b 	Locale’s abbreviated month name.
%B 	Locale’s full month name.
%c 	Locale’s appropriate date and time representation.
%d 	Day of the month as a decimal number [01,31].
%f 	Microsecond as a decimal number [0,999999], zero-padded on the left
%H 	Hour (24-hour clock) as a decimal number [00,23].
%I 	Hour (12-hour clock) as a decimal number [01,12].
%j 	Day of the year as a decimal number [001,366].
%m 	Month as a decimal number [01,12].
%M 	Minute as a decimal number [00,59].
%p 	Locale’s equivalent of either AM or PM.
%S 	Second as a decimal number [00,61].
%U 	Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
%w 	Weekday as a decimal number [0(Sunday),6].
%W 	Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
%x 	Locale’s appropriate date representation.
%X 	Locale’s appropriate time representation.
%y 	Year without century as a decimal number [00,99].
%Y 	Year with century as a decimal number.
%Z 	Time zone name (no characters if no time zone exists).
%% 	A literal '%' character.
</pre>%a 	Locale’s abbreviated weekday name.
%A 	Locale’s full weekday name.
%b 	Locale’s abbreviated month name.
%B 	Locale’s full month name.
%c 	Locale’s appropriate date and time representation.
%d 	Day of the month as a decimal number [01,31].
%f 	Microsecond as a decimal number [0,999999], zero-padded on the left
%H 	Hour (24-hour clock) as a decimal number [00,23].
%I 	Hour (12-hour clock) as a decimal number [01,12].
%j 	Day of the year as a decimal number [001,366].
%m 	Month as a decimal number [01,12].
%M 	Minute as a decimal number [00,59].
%p 	Locale’s equivalent of either AM or PM.
%S 	Second as a decimal number [00,61].
%U 	Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
%w 	Weekday as a decimal number [0(Sunday),6].
%W 	Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
%x 	Locale’s appropriate date representation.
%X 	Locale’s appropriate time representation.
%y 	Year without century as a decimal number [00,99].
%Y 	Year with century as a decimal number.
%Z 	Time zone name (no characters if no time zone exists).
%% 	A literal '%' character.
Categories: python Tags: date, shortcut, strftime, time

urllib.quote, urllib.unquote

April 10, 2012 Jabba Laci Leave a comment
>>> import urllib
>>> urllib.quote('hello world')
'hello%20world'
>>> urllib.unquote('hello%20world')
'hello world'
Categories: python Tags: quote, unquote, urllib

produce the output of Unix’s date command

April 6, 2012 Jabba Laci Leave a comment

Problem
You want to get the same output as Unix’s date command but you don’t want to make an external call. How to produce this output in pure Python?

Solution
I posed this question today on the python-list and I got the following answer from Chris R.:

From POSIX (http://pubs.opengroup.org/onlinepubs/009695399/utilities/date.html):

When no formatting operand is specified, the output in the POSIX locale shall be equivalent to specifying:

   date "+%a %b %e %H:%M:%S %Z %Y"

Therefore:
   from time import strftime
   print strftime("%a %b %e %H:%M:%S %Z %Y")

But note that `date` is locale-sensitive; imitating that would be a
bit more complicated.

Thus, the code snippet:

from time import strftime
print strftime("%a %b %e %H:%M:%S %Z %Y")
# sample output:
# Thu Apr  5 23:55:56 CEST 2012
Categories: python Tags: date, unix date
RSS feed
  • Google
  • Youdao
  • Xian Guo
  • Zhua Xia
  • My Yahoo!
  • newsgator
  • Bloglines
  • iNezha

Blog Stats

  • 1,659,960 hits

Random Post

Click here for a random post

Recent Posts

  • Get the value of PI without a constant
  • In a graph, find the largest complete subgraph
  • How to solve a two-variable equation with Sympy
  • Autoformat your Python code with Ruff
  • Play a sound file from memory

ascii bash beautifulsoup book bpython challenge command-line convert cron crontab date datetime distance dropbox email eric exercise extract file firefox github google hacking heroku html ide imdb imgur install ipython java jinja2 json linux module modules MongoDB movies news pdf perl pi pip print pylint pypi python3 python shell quote readline reddit redirect refactoring regexp requests reverse review shell sound stdout string tab text threads time tip tutorial unicode url video virtualenv virtual environment virtualenvwrapper xml zip

Archives

  • December 2025 (1)
  • December 2024 (2)
  • March 2024 (1)
  • February 2024 (3)
  • June 2023 (1)
  • December 2022 (1)
  • May 2022 (1)
  • March 2022 (1)
  • February 2022 (2)
  • October 2020 (3)
  • September 2020 (2)
  • July 2020 (1)
  • December 2019 (1)
  • November 2019 (7)
  • June 2019 (2)
  • May 2019 (1)
  • February 2019 (1)
  • January 2019 (1)
  • August 2018 (1)
  • July 2018 (4)
  • June 2018 (5)
  • May 2018 (1)
  • March 2018 (2)
  • February 2018 (1)
  • January 2018 (2)
  • December 2017 (2)
  • October 2017 (1)
  • August 2017 (1)
  • May 2017 (2)
  • April 2017 (1)
  • March 2017 (1)
  • February 2017 (3)
  • January 2017 (5)
  • December 2016 (3)
  • October 2016 (2)
  • September 2016 (1)
  • August 2016 (4)
  • July 2016 (3)
  • June 2016 (2)
  • May 2016 (2)
  • April 2016 (1)
  • March 2016 (4)
  • February 2016 (1)
  • January 2016 (4)
  • December 2015 (5)
  • November 2015 (8)
  • October 2015 (2)
  • September 2015 (3)
  • August 2015 (6)
  • July 2015 (4)
  • June 2015 (1)
  • May 2015 (3)
  • April 2015 (2)
  • February 2015 (1)
  • January 2015 (6)
  • December 2014 (5)
  • November 2014 (2)
  • October 2014 (1)
  • September 2014 (1)
  • August 2014 (14)
  • July 2014 (3)
  • June 2014 (6)
  • May 2014 (3)
  • April 2014 (2)
  • March 2014 (4)
  • February 2014 (3)
  • January 2014 (19)
  • December 2013 (8)
  • November 2013 (9)
  • October 2013 (4)
  • September 2013 (10)
  • August 2013 (16)
  • July 2013 (4)
  • June 2013 (7)
  • May 2013 (7)
  • April 2013 (3)
  • March 2013 (12)
  • February 2013 (2)
  • January 2013 (10)
  • December 2012 (18)
  • November 2012 (3)
  • October 2012 (4)
  • September 2012 (5)
  • August 2012 (1)
  • July 2012 (1)
  • May 2012 (8)
  • April 2012 (9)
  • March 2012 (17)
  • February 2012 (3)
  • January 2012 (8)
  • November 2011 (11)
  • October 2011 (7)
  • September 2011 (17)
  • August 2011 (5)
  • June 2011 (1)
  • May 2011 (7)
  • April 2011 (21)
  • March 2011 (21)
  • February 2011 (7)
  • December 2010 (4)
  • November 2010 (1)
  • October 2010 (16)
  • September 2010 (15)

Meta

  • Create account
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.com
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Subscribe Subscribed
  • Python Adventures
    View site in Reader
    Manage subscriptionsSign upLog in
    Report this content
    Collapse this bar
Loading Comments...

You must be logged in to post a comment.

Create your own website, free on WordPress.com.Create your own website.
Start free
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.