Re: Challenge: optimizing isqrt

2014-11-01 Thread Christian Gollwitzer

Hi Steven,

let me start by answering from reverse:
> Q3: What is the largest value of n beyond which you can never use the 
float

> optimization?
>

A3: There is no such value, besides the upper limit of floats (DBL_MAX~ 
10^308)


P3: If you feed a perfect square into the floating point square root 
algorithm, with a mantissa of the root of length smaller than the 
bitwidth of your float, it will always come out perfectly. I.e., 
computing sqrt(25) in FP math is no different from sqrt(25*2**200):


>>> 25*2**200
40173451106474756888549052308529065063055074844569820882534400L
>>> x=int(math.sqrt(25*2**200))
>>> x
6338253001141147007483516026880L
>>> x*x
40173451106474756888549052308529065063055074844569820882534400L
>>>



Am 01.11.14 02:29, schrieb Steven D'Aprano:

There is an algorithm for calculating the integer square root of any
positive integer using only integer operations:

def isqrt(n):
 if n < 0: raise ValueError
 if n == 0:
 return 0
 bits = n.bit_length()
 a, b = divmod(bits, 2)
 x = 2**(a+b)
 while True:
 y = (x + n//x)//2
 if y >= x:
 return x
 x = y



Q2: For values above M, is there a way of identifying which values of n are
okay to use the optimized version?


A2: Do it in a different way.

Your above algorithm is obviously doing Heron- or Newton-Raphson 
iterations, so the same as with floating point math. The first line 
before the while loop computes some approximation to sqrt(n). Instead of 
doing bit shuffling, you could compute this by FP math and get closer to 
the desired result, unless the integer is too large to be represented by 
FP. Now, the terminating condition seems to rely on the fact that the 
initial estimate x>=sqrt(n), but I don't really understand it. My guess 
is that if you do x=int(sqrt(n)), then do the first iteration, then swap 
x and y such that x>y, then enter the loop, you would simply start with 
a better estimate in case that the significant bits can be represented 
by the float.


So this is my try, but not thoroughly tested:

def isqrt(n):
if n < 0: raise ValueError
if n == 0:
return 0
bits = n.bit_length()
# the highest exponent in 64bit IEEE is 1023
if n>2**1022:
a, b = divmod(bits, 2)
x = 2**(a+b)
else:
x=int(math.sqrt(n))
y=n//x
if x= x:
return x
x = y


Christian




--
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: optimizing isqrt

2014-11-01 Thread Christian Gollwitzer
Addendum: If my method below works, you can also use it to speed up 
computations for n>2*1022, by splitting off an even power of two from 
the integer and computing the FP sqrt of the mantissa for the seed, i.e. 
doing the FP manually.


Am 01.11.14 09:02, schrieb Christian Gollwitzer:

Hi Steven,

let me start by answering from reverse:
 > Q3: What is the largest value of n beyond which you can never use the
float
 > optimization?
 >

A3: There is no such value, besides the upper limit of floats (DBL_MAX~
10^308)

P3: If you feed a perfect square into the floating point square root
algorithm, with a mantissa of the root of length smaller than the
bitwidth of your float, it will always come out perfectly. I.e.,
computing sqrt(25) in FP math is no different from sqrt(25*2**200):

 >>> 25*2**200
40173451106474756888549052308529065063055074844569820882534400L
 >>> x=int(math.sqrt(25*2**200))
 >>> x
6338253001141147007483516026880L
 >>> x*x
40173451106474756888549052308529065063055074844569820882534400L
 >>>



Am 01.11.14 02:29, schrieb Steven D'Aprano:

There is an algorithm for calculating the integer square root of any
positive integer using only integer operations:

def isqrt(n):
 if n < 0: raise ValueError
 if n == 0:
 return 0
 bits = n.bit_length()
 a, b = divmod(bits, 2)
 x = 2**(a+b)
 while True:
 y = (x + n//x)//2
 if y >= x:
 return x
 x = y



Q2: For values above M, is there a way of identifying which values of
n are
okay to use the optimized version?


A2: Do it in a different way.

Your above algorithm is obviously doing Heron- or Newton-Raphson
iterations, so the same as with floating point math. The first line
before the while loop computes some approximation to sqrt(n). Instead of
doing bit shuffling, you could compute this by FP math and get closer to
the desired result, unless the integer is too large to be represented by
FP. Now, the terminating condition seems to rely on the fact that the
initial estimate x>=sqrt(n), but I don't really understand it. My guess
is that if you do x=int(sqrt(n)), then do the first iteration, then swap
x and y such that x>y, then enter the loop, you would simply start with
a better estimate in case that the significant bits can be represented
by the float.

So this is my try, but not thoroughly tested:

def isqrt(n):
 if n < 0: raise ValueError
 if n == 0:
 return 0
 bits = n.bit_length()
 # the highest exponent in 64bit IEEE is 1023
 if n>2**1022:
 a, b = divmod(bits, 2)
 x = 2**(a+b)
 else:
 x=int(math.sqrt(n))
 y=n//x
 if x= x:
 return x
 x = y


Christian






--
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: optimizing isqrt

2014-11-01 Thread Chris Angelico
On Sat, Nov 1, 2014 at 7:02 PM, Christian Gollwitzer  wrote:
> Your above algorithm is obviously doing Heron- or Newton-Raphson iterations,
> so the same as with floating point math. The first line before the while
> loop computes some approximation to sqrt(n). Instead of doing bit shuffling,
> you could compute this by FP math and get closer to the desired result,
> unless the integer is too large to be represented by FP. Now, the
> terminating condition seems to rely on the fact that the initial estimate
> x>=sqrt(n), but I don't really understand it. My guess is that if you do
> x=int(sqrt(n)), then do the first iteration, then swap x and y such that
> x>y, then enter the loop, you would simply start with a better estimate in
> case that the significant bits can be represented by the float.

Part of the point of that algorithm is that it never uses FP, and is
therefore not limited by FP restrictions. As to the assumption that
x>=sqrt(n), that would be safe: if the bit length is even (that is,
it's between an odd power of 2 and an even one - for example, 2**13 <
16000 <= 2**14), the initial estimate is the exact square root of the
power of two at the top of that range (bit length of 14 means x is
2**7, 128 == sqrt(16384)); if the bit length is odd (eg 2**8 < 300 <=
2**9), the initial estimate rounds the halving upward (bit length of 9
means x is 2**(9//2+1), 32 > sqrt(512)). So there's a guarantee that
the initial estimate is no lower than the target number.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: optimizing isqrt

2014-11-01 Thread Christian Gollwitzer

Am 01.11.14 09:13, schrieb Chris Angelico:

On Sat, Nov 1, 2014 at 7:02 PM, Christian Gollwitzer  wrote:

Your above algorithm is obviously doing Heron- or Newton-Raphson iterations,
so the same as with floating point math. The first line before the while
loop computes some approximation to sqrt(n). Instead of doing bit shuffling,
you could compute this by FP math and get closer to the desired result,
unless the integer is too large to be represented by FP. Now, the
terminating condition seems to rely on the fact that the initial estimate
x>=sqrt(n), but I don't really understand it. My guess is that if you do
x=int(sqrt(n)), then do the first iteration, then swap x and y such that
x>y, then enter the loop, you would simply start with a better estimate in
case that the significant bits can be represented by the float.


Part of the point of that algorithm is that it never uses FP, and is
therefore not limited by FP restrictions.


which are???


--
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: optimizing isqrt

2014-11-01 Thread Chris Angelico
On Sat, Nov 1, 2014 at 7:25 PM, Christian Gollwitzer  wrote:
>> Part of the point of that algorithm is that it never uses FP, and is
>> therefore not limited by FP restrictions.
>
>
> which are???

Most notably, the inability to represent every integer beyond 2**53,
and the inability to represent any integer beyond 2**1024. This
algorithm should work fine with any positive integer.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: optimizing isqrt

2014-11-01 Thread Christian Gollwitzer

Am 01.11.14 09:33, schrieb Chris Angelico:

On Sat, Nov 1, 2014 at 7:25 PM, Christian Gollwitzer  wrote:

Part of the point of that algorithm is that it never uses FP, and is
therefore not limited by FP restrictions.



which are???


Most notably, the inability to represent every integer beyond 2**53,
and the inability to represent any integer beyond 2**1024. This
algorithm should work fine with any positive integer.

 OK so you did not bother to look at my proposed alternative 
implementation. If I understood Steven correctly, he wanted to speed up 
the original isqrt algorithm by using FP when this is possible. I have 
shown how to do it for n<2**1022 (maybe 2**1024, I'm to lean to check 
it). I admit that there is some microoptimizatio left, e.g. the first 
division is done twice, the comparison should be bits>1022, not n>2*1022 
etc.


Christian

--
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: optimizing isqrt

2014-11-01 Thread Chris Angelico
On Sat, Nov 1, 2014 at 7:38 PM, Christian Gollwitzer  wrote:
> Am 01.11.14 09:33, schrieb Chris Angelico:
>>
>> On Sat, Nov 1, 2014 at 7:25 PM, Christian Gollwitzer 
>> wrote:

 Part of the point of that algorithm is that it never uses FP, and is
 therefore not limited by FP restrictions.
>>>
>>>
>>>
>>> which are???
>>
>>
>> Most notably, the inability to represent every integer beyond 2**53,
>> and the inability to represent any integer beyond 2**1024. This
>> algorithm should work fine with any positive integer.
>>
>  OK so you did not bother to look at my proposed alternative implementation.
> If I understood Steven correctly, he wanted to speed up the original isqrt
> algorithm by using FP when this is possible. I have shown how to do it for
> n<2**1022 (maybe 2**1024, I'm to lean to check it). I admit that there is
> some microoptimizatio left, e.g. the first division is done twice, the
> comparison should be bits>1022, not n>2*1022 etc.

I did look at it. Trouble is, I don't know floating point's details
well enough to prove that there are no *other* limitations.

FWIW, I've proven the algorithm as far as 2**38. That's still a long
way short of 2**53, though, and getting as far as 2**39 would, with my
brute-force checker, require between 2.5 and 5 hours. I've made some
improvements over the original, but it's still slow.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Challenge: optimizing isqrt

2014-11-01 Thread Akira Li
Steven D'Aprano  writes:

> There is an algorithm for calculating the integer square root of any
> positive integer using only integer operations:
>
> def isqrt(n):
> if n < 0: raise ValueError
> if n == 0:
> return 0
> bits = n.bit_length()
> a, b = divmod(bits, 2)
> x = 2**(a+b)
> while True:
> y = (x + n//x)//2
> if y >= x:
> return x
> x = y
>
> This returns the integer part of the square root of n, that is, the greatest
> whole number less than or equal to the square root of n:
>
> py> isqrt(35)
> 5
> py> isqrt(36)
> 6
>
>
> That makes it equivalent to int(math.sqrt(n)), which also happens to be
> much, much faster, at least for small values of n. However, for large
> values of n, using floating point intermediate calculations fail:
>
> py> import math
> py> int(math.sqrt(2**3000))
> Traceback (most recent call last):
>   File "", line 1, in 
> OverflowError: long int too large to convert to float
>
> Another problem is that, above a certain size, the resolution of floats is
> larger than 1, so you can't convert every int into a float without loss:
>
> py> float(2**90-1) == 2**90-1
> False
>
> which means that using math.sqrt is not correct:
>
> py> isqrt(2**90-1)
> 35184372088831
> py> int(math.sqrt(2**90-1))  # Off by one.
> 35184372088832
>
>
> So, the challenge is to identify when it is safe to optimise isqrt(n) as
> int(math.sqrt(n)):
>
> Q1: What is the largest value of M, such that 
>
> all(isqrt(i) == int(math.sqrt(n)) for n in range(M))
>
> returns True?
>
> I have done a brute force test, and in nine hours confirmed that M is at
> least 7627926244. That took nine hours, and I expect that a brute force
> test of every int representable as a float would take *months* of
> processing time.
>
> Q2: For values above M, is there a way of identifying which values of n are
> okay to use the optimized version?
>
> Q3: What is the largest value of n beyond which you can never use the float
> optimization?
>
>
> You can assume that Python floats are IEEE-754 C doubles, and that
> math.sqrt() is correctly rounded.

Where do you want to use your optimized isqrt(i)? 

There could be specialized algorithms that work only in narrow specific
circumstances e.g., the inverse square root (1/sqrt(x)) implementation
from Quake III Arena that has 0x5f3759df constant in it (only of
historical interest now).

If you want to work with very large (thousands, millions of digits)
integers then gmp library might be faster then the default Python
integer implementation.


--
Akira

-- 
https://mail.python.org/mailman/listinfo/python-list


Python tarfile module

2014-11-01 Thread eploughe
I am looking see if anyone knows if/how I can enable the selinux option in the tarfile module.  I have looked around and I don't specifically see how this is done.  Any insight would be extremely helpful as I have pretty much come up empty handed everywhere I look.ThanksEric
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Build Question: How to Add -Wl, --option Before Objects In Setup.py?

2014-11-01 Thread Ned Deily
In article 
,
 Cyd Haselton  wrote:
> So, after trying various ways to add that flag before lpython in
> setup.py, I stripped all --allow-shlib-undefined
> --no-allow-shlib-undefined feom the Makefile, ran make clean, and
> make.
> 
> I still get the following error:
> Modules/python.o \
> -lc -ldl -lm  -L. -lpython2.7   -lm
> ./libpython2.7.so: undefined reference to `sincos'
> collect2: error: ld returned 1 exit status
> make: *** [python] Error 1

I don't understand why you are seeing that problem *unless" you might be 
running into this issue that I found by searching for "sincos Android":

https://code.google.com/p/android/issues/detail?id=38423

That looks awfully suspicious.  If it isn't that, I don't know what to 
tell you.  You could open an issue on the Python bug tracker 
(bugs.python.org) but we don't officially support Android so I doubt 
that will help much.  If you haven't already, you might try asking on 
some Android forums; I know other oddities of building things on Android 
have been reported.

Good luck!

-- 
 Ned Deily,
 n...@acm.org

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.4.2 + PyQt4 + PyCharm 3.4.1

2014-11-01 Thread Juan Christian
No one here uses PyCharm and Qt? =/

On Wed, Oct 29, 2014 at 8:45 PM, Juan Christian 
wrote:

> It only occurs whule using PyCharm I tried it via pure terminal and
> everything works... =/
>
> On Tue, Oct 28, 2014 at 7:45 PM, Juan Christian 
> wrote:
>
>> Python 3.4.2 Windows x64
>> PyQt4 4.11.2 Py3.4 Qt4.8.6 (x64)
>> PyCharm 3.4.1 Pro Edition
>>
>>
>> So, PyCharm works 100% with everything here but PyQt.
>>
>> I have this folder structure:
>>
>> Disk C:
>> > PyQt4
>> >> Lib/site-packages/PyQt4/(tons of files here)
>>
>> > Python34 (normal/default installation)
>>
>> ---
>>
>> I tried copying the 'PyQt4' folder to my 'Python34/Lib/site-packages'
>> folder but when I try to code something Qt related on PyCharm I get this
>> issue:
>>
>> Some skeletons failed to generate: 19 modules failed in 1 interpreter.
>> Details...
>>
>> Failed modules
>>
>> Python 3.4.2
>> PyQt4.QAxContainer
>> PyQt4.Qsci
>> PyQt4.QtCore
>> PyQt4.QtDeclarative
>> PyQt4.QtDesigner
>> PyQt4.QtGui
>> PyQt4.QtHelp
>> PyQt4.QtMultimedia
>> PyQt4.QtNetwork
>> PyQt4.QtOpenGL
>> PyQt4.QtScript
>> PyQt4.QtScriptTools
>> PyQt4.QtSql
>> PyQt4.QtSvg
>> PyQt4.QtTest
>> PyQt4.QtWebKit
>> PyQt4.QtXml
>> PyQt4.QtXmlPatterns
>> PyQt4.phonon
>>
>> Generation of skeletons for the modules above will be tried again when
>> the modules are updated or a new version of generator is available.
>>
>> And PyCharm tells me that my 'import PyQt4.ANYTHING_HERE import *' has
>> 'Unresolved references'.
>>
>> ---
>>
>> When I try to install the PyQt4 via the installer, in the default
>> location (C:/Python34) I get an even 'worse' error, whenever PyCharm try to
>> update the skeletons or something like that (that is, whenever I open a
>> file there...), I get tons and tons of the same error, 'Error while
>> accessing memory at address ', and 'python.exe' stops working.
>>
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Fabric on Windows :

2014-11-01 Thread Ganesh Pal
On Tue, Oct 28, 2014 at 5:00 PM, Robin Becker  wrote:
>
> I found fabric on windows quite hard, but I have managed to use it. For
> ssh I think I had to use the putty tools eg plink to do remote work.
>
> On the other hand I find plumbum much easier
>
> http://tomerfiliba.com/blog/Plumbum/
>
>
Thanks for pointing me to Plumbum , it looks nice but I will stick around
with fabric for a while.  It works beautifully on linux  .  I dont want to
rewrite my linux scripts .
-- 
https://mail.python.org/mailman/listinfo/python-list


PYQT4 referer in javascript still blank

2014-11-01 Thread Peter Irbizon
Hello,
I am trying to set referrer for my script this way in PYQT4:

class NetworkManager(QNetworkAccessManager):
def createRequest(self, op, req, outgoing_data):
req.setRawHeader('Referer', 'http://www.my-university.com/')
req.setRawHeader('Accept-Language', 'en')
return super(NetworkManager, self).createRequest(op, req,
outgoing_data)

but when I use javascript document.write('js ref: ' + document.referrer
+''); on my website it still returns empty line.
When I use php $_SERVER["HTTP_REFERER"] it returns correct referer.

How should I make it working for javascript as well?
-- 
https://mail.python.org/mailman/listinfo/python-list


Installing Parsers/Tree Builders to, and accessing these packages from Python2.7

2014-11-01 Thread Simon Evans
Hi Programmers, 
I have downloaded, installed, and can access the LXMLTreeBuilder/lxml, from 
Python2.7. 
however I have also downloaded HTMLTreeBuilder/html5lib but cannot get console 
to recognize the download, even using the code the download site suggests. I 
did put it in the Python2.7 directory, but unlike the HTML one, it doesn't 
recognize it, so the import statement returns an error. 
Can anyone tell me how I might proceed so's these TreeBuilders/ Parsers will 
work on my Python console ? 
I also will have to install HTMLParserTreeBuilder/html.parser and 
LXMLTreeBuilderForXML/lxml but best to cross that bridge when gotten to, as 
they say. 
Thank you for reading.I look forward to hearing from you.
Yours 
Simon Evans
-- 
https://mail.python.org/mailman/listinfo/python-list


what can i do to improve my skill after finished python course on codecademy

2014-11-01 Thread fanhuhuai
i will finish the python course on codecademy soon,i dont konw how to improve 
my skill and what can i do to use it ,some projects ? should i learn others 
course ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pySerial works in miniterm but not in my app

2014-11-01 Thread Dario
Il giorno venerdì 31 ottobre 2014 19:00:26 UTC+1, Dennis Lee Bieber ha scritto:

> Didn't quite answer my question. If the comm line is using remote 

I understand your point, I didn't mention but I also tried sending one char at 
a time and listening at the same time, nothing changed.

BUT.. plot twist: in Windows XP, the very same python code and usb adapter are 
working just right (python 2.7 and pySerial 2.7). Also with c#, no issues.

So one could blame the usb adapter or its drivers, but the fact is that minicom 
(not miniterm) always works, while miniterm only works if used after minicom, 
and only the first time.

I mean: I start minicom, it works. Close it (quit without reset), start 
miniterm, it works. Close miniterm, open it again, just garbage...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pySerial works in miniterm but not in my app

2014-11-01 Thread Dario
Il giorno sabato 1 novembre 2014 16:04:06 UTC+1, Dario ha scritto:

> BUT.. plot twist: in Windows XP, the very same python code and usb adapter 
> are working just right (python 2.7 and pySerial 2.7). Also with c#, no issues.

I compared the behaviour of mono and python (2.7 and 3.3) on the same hw and 
os, I guess something is wrong with pySerial implementation...

Mono code on Mint:


SerialPort s = new SerialPort("/dev/ttyUSB0", 19200, Parity.None, 8, 
StopBits.One);
s.Open();

s.Write("sw o01 +\r");

while (true)
Console.Write(Convert.ToChar(s.ReadByte()));


Device reacts correctly and I get back what I expect (the first line is because 
I sent the command via com port, the second is because I pushed a button on the 
device):

dario@ivymint ~ $ sudo ./Test1.exe 
sw o01 + Command OK
Button 1 pushed


Now equivalent Python 3.3 code on Mint:

---
import serial

s = serial.serial_for_url('/dev/ttyUSB0', 19200, bytesize = 8, parity = 'N', 
stopbits = 1)

s.close()
s.open()
s.write(bytearray('sw o01 +\r','ascii'))

while True:
print(s.read())
---

In this case, I don't receive anything for my command, and when I press I 
receive garbage instead of "Button 1 pushed"

dario@ivymint ~ $ sudo python3 ./test2.py 
b'\xfc'
b'\x8f'
b'\r'
b'\x85'
b'1'
b'+'
b'\xfe'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pySerial works in miniterm but not in my app

2014-11-01 Thread Dario
Ehm sorry for the neverending spam, anyway I tried from my raspberry pi and it 
works there:

root@pi:/home/pi# python3 ./test.py 
b's'
b'w'
b' '
b'o'
b'0'
b'1'
b' '
b'+'
b' '
b'C'
b'o'
b'm'
b'm'
b'a'
b'n'
b'd'
b' '
b'O'
b'K'
b'\r'
b'\n'

Since I need it to work on the rpi and I was using Mint only for easiness, I'm 
ok with it :)

Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-11-01 Thread Alexander Blinne
Am 31.10.2014 um 02:22 schrieb Artur Bercik:
> I have to set environmental variable in my windows PC as follows:
> 
> variable name: GISBASE
> 
> value: C:\GRASS-64
> 
> Is it possible to set it from python?
> 
> import sys
> 
> sys.path.append("C:\\GRASS-64")
> 
> But how to give variable name? I have to set both the variable name and
> value.

http://lmgtfy.com/?q=how+to+set+environment+variable+with+python

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-11-01 Thread Seymore4Head
On Sat, 01 Nov 2014 12:00:46 -0400, Dennis Lee Bieber
 wrote:

>On Fri, 31 Oct 2014 19:32:13 -0400, Seymore4Head
> declaimed the following:
>
>>
>>class Rectangle(object):
>>def __init__(self, length, width=None):
>>self.length = length
>>if width is None:
>>self.width = length
>>else:
>>self.width = width
>>def area(self):
>>return self.length * self.width
>>def perimeter(self):
>>return 2 * (self.length + self.width)
>>
>>class Square(Rectangle):
>>def area(self):
>>return self.length * self.width
>>def perimeter(self):
>>return 2 * (self.length + self.width)
>>
>   Why did you redefine area and perimeter? Those are supposed to be
>inherited operations from Rectangle. You've duplicated what should not have
>been changed, and removed everything that makes a Square a Square.
>
>   The "assignment" was to create a Square class that enforces the
>property of "squareness" (length = width, or a simple "side").
>
>   What you created does not do that.
>
>>a=Rectangle(3,5)
>>print (a.area())
>>print (a.perimeter())
>>b=Rectangle(5,7)
>>print (b.area())
>>print (b.perimeter())
>>c=Square(4)
>>print (c.area())
>>print (c.perimeter())
>
>   You're tests ignore the case of /changing/ side/length/width...
>Consider what happens if you execute
>
>c.length = 5
>c.width = 9
>
OK  Maybe I misunderstood the question.

My answer to you then is ..I don't know.  I will have to think
about it some more.
-- 
https://mail.python.org/mailman/listinfo/python-list


__index__

2014-11-01 Thread duncan smith
Hello,
  I have a Bloom filter class and want to (partially) serialize
instances using hex() or oct(). Instances are mutable, so I can't
inherit from long. I thought I'd found the answer when I came across
__index__,
https://docs.python.org/2/reference/datamodel.html#object.__index__. But
it doesn't seem to work as I expected it to.


>>> class MyClass(object):
def __init__(self):
self.val = 7
def __index__(self):
return self.val


>>> x = MyClass()
>>> oct(x)

Traceback (most recent call last):
  File "", line 1, in 
oct(x)
TypeError: oct() argument can't be converted to oct
>>> oct(x.__index__())
'07'
>>>


Can someone please explain why my thinking is wrong on this? TIA.

Duncan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __index__

2014-11-01 Thread Ned Batchelder

On 11/1/14 12:56 PM, duncan smith wrote:

Hello,
   I have a Bloom filter class and want to (partially) serialize
instances using hex() or oct(). Instances are mutable, so I can't
inherit from long. I thought I'd found the answer when I came across
__index__,
https://docs.python.org/2/reference/datamodel.html#object.__index__. But
it doesn't seem to work as I expected it to.



class MyClass(object):

def __init__(self):
self.val = 7
def __index__(self):
return self.val



x = MyClass()
oct(x)


Traceback (most recent call last):
   File "", line 1, in 
 oct(x)
TypeError: oct() argument can't be converted to oct

oct(x.__index__())

'07'





Can someone please explain why my thinking is wrong on this? TIA.


Just above your link in the docs is __oct__ and __hex__, which are used 
to implement oct() and hex(): 
https://docs.python.org/2/reference/datamodel.html#object.__oct__


That said, I would simply add a .hex() method on the class.  I would 
never expect a complex thing like a Bloom filter to be turned into 
useful hex with the hex() builtin.


For example, when making an MD5 hash, you use the md5.hexdigest() 
method, not hex(md5).


--
Ned Batchelder, http://nedbatchelder.com

--
https://mail.python.org/mailman/listinfo/python-list


Re: __index__

2014-11-01 Thread duncan smith
On 01/11/14 16:56, duncan smith wrote:

[snip]

Sorry, forgot to add that I'm using Python 2.7.6 on Ubuntu 14.04. Cheers.

Duncan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __index__

2014-11-01 Thread Ethan Furman

On 11/01/2014 10:11 AM, Ned Batchelder wrote:

On 11/1/14 12:56 PM, duncan smith wrote:


   I have a Bloom filter class and want to (partially) serialize
instances using hex() or oct(). Instances are mutable, so I can't
inherit from long. I thought I'd found the answer when I came across
__index__, it doesn't seem to work as I expected it to.


Just above your link in the docs is __oct__ and __hex__, which are used to 
implement oct() and hex():
https://docs.python.org/2/reference/datamodel.html#object.__oct__


In Python 2 __oct__ and __hex__ are used for oct() and hex(), but in Python 3 
__index__ is used.

But I agree with Net that using a separate method is probably better.

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: Build Question: How to Add -Wl, --option Before Objects In Setup.py?

2014-11-01 Thread Cyd Haselton
On Sat, Nov 1, 2014 at 5:52 AM, Ned Deily  wrote:
> In article
> ,
>  Cyd Haselton  wrote:
>> So, after trying various ways to add that flag before lpython in
>> setup.py, I stripped all --allow-shlib-undefined
>> --no-allow-shlib-undefined feom the Makefile, ran make clean, and
>> make.
>>
>> I still get the following error:
>> Modules/python.o \
>> -lc -ldl -lm  -L. -lpython2.7   -lm
>> ./libpython2.7.so: undefined reference to `sincos'
>> collect2: error: ld returned 1 exit status
>> make: *** [python] Error 1
>
> I don't understand why you are seeing that problem *unless" you might be
> running into this issue that I found by searching for "sincos Android":
>
> https://code.google.com/p/android/issues/detail?id=38423
>
> That looks awfully suspicious.  If it isn't that, I don't know what to
> tell you.  You could open an issue on the Python bug tracker
> (bugs.python.org) but we don't officially support Android so I doubt
> that will help much.  If you haven't already, you might try asking on
> some Android forums; I know other oddities of building things on Android
> have been reported.
>
> Good luck!
>
> --
>  Ned Deily,
>  n...@acm.org
>
> --
> https://mail.python.org/mailman/listinfo/python-list

Hammer -> Head -> Nail.

Sure enough, nm -D libm.so shows that sincos is NOT available in that
library on my Android device. Now to figure out what to do about it.

Nice find!

Cyd
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __index__

2014-11-01 Thread duncan smith
On 01/11/14 18:29, Ethan Furman wrote:
> On 11/01/2014 10:11 AM, Ned Batchelder wrote:
>> On 11/1/14 12:56 PM, duncan smith wrote:
>>>
>>>I have a Bloom filter class and want to (partially) serialize
>>> instances using hex() or oct(). Instances are mutable, so I can't
>>> inherit from long. I thought I'd found the answer when I came across
>>> __index__, it doesn't seem to work as I expected it to.
>>
>> Just above your link in the docs is __oct__ and __hex__, which are
>> used to implement oct() and hex():
>> https://docs.python.org/2/reference/datamodel.html#object.__oct__
> 
> In Python 2 __oct__ and __hex__ are used for oct() and hex(), but in
> Python 3 __index__ is used.
> 

It was the doc for hex at
https://docs.python.org/2/library/functions.html that led me to think I
needed to implement the _index__ method. The doc for bin and oct seems
to be right (I need __index__ for bin, but it doesn't work for oct).


> But I agree with Net that using a separate method is probably better.
> 
> -- 
> ~Ethan~

Possibly, I'm still tinkering with it. I use the Bloom filters to
generate, and act as pseudonyms for token sets. I have another class
that represents pseudonyms (of a different kind, but still bit strings)
that is derived from long - so hex etc. just work. I should probably (at
least) subclass my Bloom filter class before adding the relevant
methods. Cheers.

Duncan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: __index__

2014-11-01 Thread Ethan Furman

On 11/01/2014 11:29 AM, Ethan Furman wrote:


But I agree with Net ...


Oops, should have ben 'Ned' -- apologies!

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: Build Question: How to Add -Wl, --option Before Objects In Setup.py?

2014-11-01 Thread Cyd Haselton
On Sat, Nov 1, 2014 at 1:47 PM, Cyd Haselton  wrote:
> On Sat, Nov 1, 2014 at 5:52 AM, Ned Deily  wrote:
>> In article
>> ,
>>  Cyd Haselton  wrote:
>>> So, after trying various ways to add that flag before lpython in
>>> setup.py, I stripped all --allow-shlib-undefined
>>> --no-allow-shlib-undefined feom the Makefile, ran make clean, and
>>> make.
>>>
>>> I still get the following error:
>>> Modules/python.o \
>>> -lc -ldl -lm  -L. -lpython2.7   -lm
>>> ./libpython2.7.so: undefined reference to `sincos'
>>> collect2: error: ld returned 1 exit status
>>> make: *** [python] Error 1
>>
>> I don't understand why you are seeing that problem *unless" you might be
>> running into this issue that I found by searching for "sincos Android":
>>
>> https://code.google.com/p/android/issues/detail?id=38423
>>
>> That looks awfully suspicious.  If it isn't that, I don't know what to
>> tell you.  You could open an issue on the Python bug tracker
>> (bugs.python.org) but we don't officially support Android so I doubt
>> that will help much.  If you haven't already, you might try asking on
>> some Android forums; I know other oddities of building things on Android
>> have been reported.
>>
>> Good luck!
>>
>> --
>>  Ned Deily,
>>  n...@acm.org
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
> Hammer -> Head -> Nail.
>
> Sure enough, nm -D libm.so shows that sincos is NOT available in that
> library on my Android device. Now to figure out what to do about it.
>
> Nice find!
>
> Cyd

UPDATE:  After doing a bit of research it looks like it would be
easier to build Python without sincos...is that possible?
If not, I'll need to figure out how to get bionic libm sources with a
proper Makefile (instead of Android's build system)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installing Parsers/Tree Builders to, and accessing these packages from Python2.7

2014-11-01 Thread Terry Reedy

On 11/1/2014 10:17 AM, Simon Evans wrote:

Hi Programmers, I have downloaded, installed, and can access the
LXMLTreeBuilder/lxml, from Python2.7. however I have also downloaded
HTMLTreeBuilder/html5lib but cannot get console to recognize the
download, even using the code the download site suggests. I did put
it in the Python2.7 directory,


What OS are you using.  Exactly where dod you put each lib?  Are each of 
the two installs a single module or a package directory?  If the latter, 
does the directory contain __init__.py



but unlike the HTML one, it doesn't
recognize it, so the import statement returns an error.


There must be some difference.  Look carefully. And please post the 
traceback.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Python Namibia 2015

2014-11-01 Thread D.M. Procida
Python Namibia 2015 will be Namibia's first-ever Python conference.

http://python-namibia.org

We're building an interesting programme of talks and other activities,
and we're seeking more. 

You can find more information about the kind of thing we're looking for,
with a link to the proposal submission form, at:

http://python-namibia.org/programme/

We'd like to have a wide range of perspectives represented - talks and
activities aimed at an audience with a variety of different levels of
expertise, and covering many different applications of Python.

If you're not sure about how to frame your proposal, just drop me a
line.

Thanks!

Daniele
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: what can i do to improve my skill after finished python course on codecademy

2014-11-01 Thread alister
On Sat, 01 Nov 2014 08:01:12 -0700, fanhuhuai wrote:

> i will finish the python course on codecademy soon,i dont konw how to
> improve my skill and what can i do to use it ,some projects ? should i
> learn others course ?

find a task you need a solution too.
are there any regular admin task you perform that could be automated?
do you have any hobbies that could benefit from computerisation?

genuine tasks are always better that those created purely to teach

"Practicality beats purity"



-- 
"The IETF motto is 'rouch consesus and running code'"
  
  -- Scott Bradner (Open Sources, 1999 O'Reilly and Associates)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: what can i do to improve my skill after finished python course on codecademy

2014-11-01 Thread Chris Angelico
On Sun, Nov 2, 2014 at 9:01 AM, alister
 wrote:
> find a task you need a solution too.
> are there any regular admin task you perform that could be automated?
> do you have any hobbies that could benefit from computerisation?
>
> genuine tasks are always better that those created purely to teach

I agree, but also, pick something that's small and self-contained. Try
to find a project that you can complete (to the point of usability)
within a day, or at most a week, preferably within the space of a page
or two of code. As an added advantage, you'll be able to ask for code
review, here or somewhere else; you can learn a lot by writing your
code first, and then having other programmers offer comments. (Note
that I didn't say "better programmers" or even "more senior
programmers". It's called "peer review" in some circles, because the
point is that anyone can learn from anyone else.)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Build Question: How to Add -Wl, --option Before Objects In Setup.py?

2014-11-01 Thread Ned Deily
In article 
,
 Cyd Haselton  wrote:
> On Sat, Nov 1, 2014 at 1:47 PM, Cyd Haselton  wrote:
[...]
> > Sure enough, nm -D libm.so shows that sincos is NOT available in that
> > library on my Android device. Now to figure out what to do about it.
[...]
> UPDATE:  After doing a bit of research it looks like it would be
> easier to build Python without sincos...is that possible?
> If not, I'll need to figure out how to get bionic libm sources with a
> proper Makefile (instead of Android's build system)

While sin() and/or cos() are primarily used in the expected places in 
the Python standard library (like the math module), sin() is also used 
in the implementation of type "complex" objects, code that is part of 
the core interpreter.  I see that there is an old, undocumented, and 
unsupported macro to disable building of complex number support.  But 
more hacking is needed to even get a somewhat working build with it 
these days and then, without complex support, many tests in the standard 
library fail and what you end up isn't really Python.  So, I think 
rather than hacking up Python, you should try to fix the broken 
platform.  There seem to be a number of project that claim to support 
Python on Android.  Perhaps they could be of help.

FWIW, the macro is "WITHOUT_COMPLEX":

./configure [...] CPPFLAGS='-DWITHOUT_COMPLEX'

and Lib/optparse.py would need to be patched to comment out its use of 
"complex".

Again, good luck!

-- 
 Ned Deily,
 n...@acm.org

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: When using a decorator exceptions raised reference the decorator not the function

2014-11-01 Thread Néstor Boscán
So actually what I ended up doing was a function that will return the last
tb_next and this will always give me the reference to the py and line
number where the exception was raised.

Regards,

Néstor

On Thu, Oct 30, 2014 at 4:42 PM, Néstor Boscán 
wrote:

> Thanks Terry
>
> Yes both lines where in the traceback using tb_next I got what I needed.
>
> Regards,
>
> Néstor
>
> On Thu, Oct 30, 2014 at 1:36 PM, Terry Reedy  wrote:
>
>> On 10/30/2014 8:33 AM, Néstor Boscán wrote:
>>
>>  I'm using Python 2.7 and I'm creating a class decorator that extract
>>> information from exceptions for logging purposes.
>>>
>>> Everytime an exception is raised from the original function and I
>>> extract the origin of the exception with sys.exc_info() I get a
>>> reference to the line in the decorator where the function is called, not
>>> the line of the original function where the exception was raised.
>>>
>>
>> I expect that both lines should be in the traceback.  Post an example
>> where you do not intercept the exception.
>>
>> --
>> Terry Jan Reedy
>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: what can i do to improve my skill after finished python course on codecademy

2014-11-01 Thread Grant Edwards
On 2014-11-01, alister  wrote:

"The IETF motto is 'rouch consesus and running code'"

  -- Scott Bradner (Open Sources, 1999 O'Reilly and Associates)

I don't get it, and googling didn't help.  What is "rouch" consensus?

-- 
Grant
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: what can i do to improve my skill after finished python course on codecademy

2014-11-01 Thread MRAB

On 2014-11-02 00:31, Grant Edwards wrote:

On 2014-11-01, alister  wrote:

"The IETF motto is 'rouch consesus and running code'"

   -- Scott Bradner (Open Sources, 1999 O'Reilly and Associates)

I don't get it, and googling didn't help.  What is "rouch" consensus?


I googled for:

"Scott Bradner" "IETF motto"

It gave me:

The IETF motto is "rough consensus and running code."

--
https://mail.python.org/mailman/listinfo/python-list


Re: what can i do to improve my skill after finished python course on codecademy

2014-11-01 Thread Chris Angelico
On Sun, Nov 2, 2014 at 11:31 AM, Grant Edwards  wrote:
> On 2014-11-01, alister  wrote:
>
> "The IETF motto is 'rouch consesus and running code'"
>
>   -- Scott Bradner (Open Sources, 1999 O'Reilly and Associates)
>
> I don't get it, and googling didn't help.  What is "rouch" consensus?

Presumably "rough consensus". As long as most people agree (as opposed
to total consensus, where *everyone* agrees), and as long as the code
can be made to run, perfection is a luxury hat the IETF doesn't strive
for. It's a form of "practicality beats purity" as it applies to
standards development.

The joke may be that this is only approximately correct spelling, as
well as only approximate consensus and approximately working code. Or
that might just be a transcription error.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: President Bush Meets Pope Before Heading to Paris

2014-11-01 Thread michel88
Out of the  list of US presidents
  ,
George Bush remains my least favorite. Even though people are saying that
Obama is losing popularity over the war on terrorism and his approach to it,
I still support him.




--
View this message in context: 
http://python.6.x6.nabble.com/President-Bush-Meets-Pope-Before-Heading-to-Paris-tp1390014p5076374.html
Sent from the Python - python-list mailing list archive at Nabble.com.
-- 
https://mail.python.org/mailman/listinfo/python-list