top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Integer division debugging in Python

+1 vote
454 views

The change in integer division seems to be the most insidious source of silent errors in porting code from python2 - since it changes the behavior or valid code silently.

I wish the interpreter had an instrumented mode to detect and report such problems.

posted Aug 28, 2013 by Satish Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Run the code under Python 2.6/2.7 with the -3 flag:

$ cat test.py 
print(10 / 7)

$ python -3 test.py
test.py:2: DeprecationWarning: classic int division
 print(10 / 7)
1
answer Aug 28, 2013 by Bob Wise
Similar Questions
+2 votes

I wanna simulate C style integer division in Python3.

So far what I've got is:

# a, b = 3, 4

import math
result = float(a) / b
if result > 0:
 result = math.floor(result)
else:
 result = math.ceil(result)

I found it's too laborious. Any quick way?

0 votes

I've written a program using Twisted that uses SqlAlchemy to access a database using threads.deferToThread(...) and SqlAlchemy's scoped_session(...). This program runs for a long time, but leaks memory slowly to the point of needing to be restarted. I don't know that the SqlAlchemy/threads thing is the problem, but thought I'd make you aware of it.

Anyway, my real question is how to go about debugging memory leak problems in Python, particularly for a long running server process written with Twisted. I'm not sure how to use heapy or guppy, and objgraph doesn't tell me enough to locate the problem. If anyone as any suggestions or pointers it would be very much appreciated!

+2 votes

I am using the Linux system with python, i am running the following script

#!/usr/bin/python

import threading

import time

import sys
import subprocess
import datetime
import os
import time
import logging

proc1=subprocess.Popen("/root/Desktop/abc.py","64","abc",shell=True,stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

In this script i am calling the other script named abc.py which is located on the desktop with 64 and abc as its arguments I am getting the following error

File "/usr/lib64/python2.6/subprocess.py", line 589, in __init__
 raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

Can someone help me?

0 votes

I was following an exercise in a book when I edited the code and came across something I did not get. Here is the relevant part of the code that works:

start=None #initialise 
while start !="": 
 start=input("nStart: ")

 if start:
 start=int(start)
 finish=int(input("Finish: ")) 

 print("word[",start,":",finish,"] is", word[start:finish])

I then changed the code to this:

start=None #initialise 
while start !="": 
 start=int(input("nStart: "))

 if start:

 finish=int(input("Finish: "))

 print("word[",start,":",finish,"] is", word[start:finish])

I combined the int conversion and the input on the same line, rather than to have two different statements. But got an error message:

Traceback (most recent call last):
 File "C:UsersFaisalDocumentspythonpizza_slicer.py", line 23, in 
 start=int(input("nStart: "))
ValueError: invalid literal for int() with base 10: ''

Could someone tell me why I got this error message?

...