Showing posts with label Python3. Show all posts
Showing posts with label Python3. Show all posts

Sunday, June 26, 2016

A Pythonic ratio for pi (or, Py for pi :)

By Vasudev Ram

Py


Pi image attribution

A Python built-in method can be used to find a ratio (of two integers) that equals the mathematical constant pi. [1]

This is how:
from __future__ import print_function
Doing:
dir(0.0) # or dir(float)
gives (some lines truncated):
'__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 
'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']
>>>
from which we see that as_integer_ratio is a method of float objects. (Floats are objects, so they can have methods.) So:
>>> import math

>>> tup = math.pi.as_integer_ratio()
>>> tup
(884279719003555, 281474976710656)

>>> tup[0] / tup[1]
3.141592653589793

>>> print(sys.version)
3.6.0a2 (v3.6.0a2:378893423552, Jun 14 2016, 01:21:40) [MSC v.1900 64 bit (AMD64
)]
>>>
I was using Python 3.6 above. If you do this in Python 2.7, the "/" causes integer division (when used with integers). So you have to multiply by a float to cause float division to happen:
>>> print(sys.version)
2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)]

>>> tup[0] / tup[1]
3L
>>> 1.0 * tup[0] / tup[1]
3.141592653589793
>>>
[1] There are many Wikipedia topics related to pi.
Also check out a few of my earlier math-related posts (including the one titled "Bhaskaracharya and the man who found zero" :)

The second post in the series on the uses of randomness will be posted in a couple of days - sorry for the delay.

- Vasudev Ram - Online Python training and consulting

Signup to hear about my new courses and products.

My Python posts     Subscribe to my blog by email

My ActiveState recipes



Sunday, November 10, 2013

Guido on Python 2 vs. Python 3


By Vasudev Ram

Guido van Rossum, our BDFL, just gave a presentation on Python 2 vs. Python 3.

Here it is:

Python 2 vs. Python 3 - A retrospective

I scanned most of it. Some excerpts that I personally found interesting:

- Too many ways to do it :-)
- e.g. old- and new-style classes

- People don't like change
-People positively hate incompatible changes
- So we did it anyway

- Drop "raise E, value" —> raise E(value)

- Drop backticks: `x` -> repr(x)

- No default ordering: 1 < 'x' raises TypeError
(I did think this was an issue when I came across it)

- Why reduce() must die

- Vasudev Ram - Python consulting and training



O'Reilly 50% Ebook Deal of the Day


Wednesday, April 10, 2013

Using sys._current_frames() and the Python traceback module for debugging

By Vasudev Ram

Python's sys._current_frames() function/method and the traceback module of Python can be useful for debugging your Python code.

In the example below, I've used py, the Python Launcher for Windows. It comes with Python 3.3. If you're on Python 2, you can download the py launcher for Python 2 here. Use either of the versions (32-bit or 64-bit, as appropriate) called launcher*, not launchwin*, for the commands below.

The example below works on both Python 2 and Python 3.

#--------------------------------------------------------
# test_current_frames.py 

import sys, traceback

def foo():

    for thread, frame in sys._current_frames().items():
        print('Thread 0x%x' % thread)
        traceback.print_stack(frame)

def bar():
    foo()

def baz():
    bar()

baz()
#--------------------------------------------------------

Run the program with any of the following 3 commands:
py test_current_frames.py

or

py -2 test_current_frames.py

or

py -3 test_current_frames.py
You should get output similar to this:
Thread 0x17dc
  File "test_current_frames.py", line 17, in 
    baz()
  File "test_current_frames.py", line 15, in baz
    bar()
  File "test_current_frames.py", line 12, in bar
    foo()
  File "test_current_frames.py", line 9, in foo
    traceback.print_stack(frame)

Also read more about the traceback module on Doug Hellmann's Python Module Of The Week (PyMOTW) site, a.k.a. PyMOTW.

- Vasudev Ram - Dancing Bison Enterprises

Monday, April 8, 2013

py, the Python Launcher for Windows

By Vasudev Ram

py, or py.exe, is the name of a new(ish) Python Launcher for Windows, which was released as part of Python 3.x.

This is the PEP for py.

I tried out Py and it worked okay, for some basic uses.

It lets you launch a specific version of Python, either 2, or 3, or 2.x or 3.x. I tried using it to launch both Python 2.7.3 and Python 3.3.1.

Examples:
C:\> py -V
Python 2.7.3
C:\> py -3 -V
Python 3.3.1
c:\Python33\Tools\Scripts> py -2 which.py py.exe
C:\Windows\py.exe
c:\Python33\Tools\Scripts>py -2 which.py cmd.exe
C:\Windows\system32\cmd.exe

Here is the partial output of running the py --help command:
c:\Python33\Tools\Scripts>py --help
Python Launcher for Windows Version 3.3.1150.1013

usage: py [ launcher-arguments ] script [ script-arguments ]

Launcher arguments:

-2     : Launch the latest Python 2.x version
-3     : Launch the latest Python 3.x version
-X.Y   : Launch the specified Python version
-X.Y-32: Launch the specified 32bit Python version

- Vasudev Ram - Dancing Bison Enterprises

Monday, December 3, 2012

Python 3.3 for iOS released

https://mobile.twitter.com/Phaseit/status/275346465294012416?p=v

https://itunes.apple.com/us/app/python-3.3-for-ios/id577911279?ls=1&mt=8

Sunday, November 18, 2012

The Python Data model (for v2 and v3)

3. Data model — Python v2.7.3 documentation

Useful long page about some Python internals.

There is a similar page for Python 3:

http://docs.python.org/3.2/reference/datamodel.html

- Vasudev Ram
www.dancingbison.com

Saturday, April 23, 2011

Six 1.0.0, Python 2 and 3 compatibility library, released

By Vasudev Ram - http://www.dancingbison.com

Six 1.0.0 has been released. Six is a Python 2 and 3 compatibility library. It "provides utility functions for smoothing over the differences between the Python versions with the goal of writing Python code that is compatible on both Python versions."

Six is available here on PyPI (the Python Package Index):

http://pypi.python.org/pypi/six

The creator of six is Benjamin Peterson who is a member of the core Python developers' team.

Posted via email
- Vasudev Ram
Dancing Bison Enterprises