Re: Help with changes in traceback stack from Python 2.7 to Python 3.x

2014-04-28 Thread Steven D'Aprano
On Mon, 28 Apr 2014 12:19:02 +1000, Chris Angelico wrote:

> On Mon, Apr 28, 2014 at 12:11 PM, Steven D'Aprano
>  wrote:
>> Suppose we could pass variables directly to the constructor, like this:
>>
>> a = b = 2
>> L = [1, 2, 3]
>> dctA = dict(a, b, L[1], 2, 1+1)
>>
>> Obviously all five values are 2, but what are the keys?
>>
>> The dict construct
>> receives five arguments, all bound to the object 2. (That might be the
>> same object five times, or five distinct objects, all with the same
>> value of 2.)
> 
> Just to nit-pick: A conforming Python implementation might pass up to
> four distinct objects, but the first two MUST be the same object. :)

So it does, thanks for the correction.

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


Re: Unicode in Python

2014-04-28 Thread wxjmfauth
Le samedi 26 avril 2014 15:38:29 UTC+2, Ian a écrit :
> On Apr 26, 2014 3:46 AM, "Frank Millman"  wrote:
> 
> >
> 
> >
> 
> >  wrote in message
> 
> > news:03bb12d8-93be-4ef6-94ae-4a02789ae...@googlegroups.com...
> 
> > > ==
> 
> > >
> 
> > > I wrote once 90 % of Python 2 apps (a generic term) supposed to
> 
> > > process text, strings are not working.
> 
> > >
> 
> > > In Python 3, that's 100 %. It is somehow only by chance, apps may
> 
> > > give the illusion they are properly working.
> 
> > >
> 
> >
> 
> > It is quite frustrating when you make these statements without explaining
> 
> > what you mean by 'not working'.
> 
> As far as anybody has been able to determine, what jmf means by "not working" 
> is  that strings containing the EURO character are handled less efficiently 
> than strings that do not contain it in certain contrived test cases.



Python 2.7 + cp1252:
- Solid and coherent system (nothing to do with the Euro).

Python 3:
- It missed the unicode shift.
- Covering the whole unicode range will not make
Python a unicode compliant product.
- Flexible String Representation (a problem per se),
a mathematical absurditiy which does the opposite of
the coding schemes endorsed by Unicord.org (sheet of
paper and pencil!)
- Very deeply buggy (quadrature of the circle problem).

Positive side:
- A very nice tool to teach the coding of characters
and unicode.

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


Re: possible bug in re expression?

2014-04-28 Thread Robin Becker

On 25/04/2014 19:32, Terry Reedy wrote:
..

I suppose that one could argue that '{' alone should be treated as special
immediately, and not just when a matching '}' is found, and should disable other
special meanings. I wonder what JS does if there is no matching '}'?


well in fact I suspect this is my mistranslation of the original

new RegExp('.{1,' + (+size) + '}', 'g')

my hacked up translator doesn't know what that means. I suspect that (+size) is 
an attempt to force size to an integer prior to it being forced to a string. I 
used to believe that conversion was always written 0-x, but experimentally 
(+"3") ends up as 3 not "3".


Naively, I imagined that re would complain about ambiguous regular expressions, 
but in the regexp world n problems --> n+1 problems almost surely so I should 
have anticipated it :)


Does this in fact that almost any broken regexp specification will silently fail 
because re will reset and consider any metacharacter as literal?

--
Robin Becker

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


Re: Inconsistent viewkeys behaviour

2014-04-28 Thread Terry Reedy

On 4/28/2014 2:22 AM, Joshua Landau wrote:

Is there any reference for this strange behaviour on Python 2:

 >>> set() < dict().viewkeys()
 Traceback (most recent call last):
   File "", line 1, in 
 TypeError: can only compare to a set
 >>> dict().viewkeys() > set()
 False


The left operand determines the result. The manual specifies that < and 
> do not have to be consistent. But I suspect that when 3.x dict.keys() 
was backported to 2.7.0, no one thought to update set, whereas the 
backported key view code already had the comparison.


--
Terry Jan Reedy

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


Re: Proper deletion of selected items during map iteration in for loop: Thanks to all

2014-04-28 Thread Duncan Booth
Chris Angelico  wrote:

> # Snapshot of keys:
> for k in list(d):
> if f(k): del d[k]
> 
> No extra loop at the end, no switching out and in of contents, just
> one little change in the loop header. Obviously you don't want to do
> this when you're deleting two out of three billion, but for smallish
> dicts, that won't make a visible change.

Even if you have three billion keys, the extra memory needed to create a 
list that references those keys is going to be a lot less than the memory 
used by the keys themselves. For example if the keys are 6 character 
strings then each string needs I think at least 45 bytes (64 bit Python 
2.x, up to double that in Python 3.x) but the list only needs one 8 byte 
pointer per key.

I would always choose this simple solution until such time as it is proved 
to be a problem.

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


Re: possible bug in re expression?

2014-04-28 Thread Steven D'Aprano
On Mon, 28 Apr 2014 10:47:54 +0100, Robin Becker wrote:

> Does this in fact that almost any broken regexp specification will
> silently fail because re will reset and consider any metacharacter as
> literal?

Well, I don't know about "almost any", but at least some broken regexes 
will explicitly fail:



py> import re
py> re.search('*', "123*4")
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python3.3/re.py", line 161, in search
return _compile(pattern, flags).search(string)
  [...]
  File "/usr/local/lib/python3.3/sre_parse.py", line 552, in _parse
raise error("nothing to repeat")
sre_constants.error: nothing to repeat

(For brevity I have abbreviated the traceback.)

-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


problem with regex

2014-04-28 Thread dimmaim
i want to find a specific urls from a txt file but i have some issus. First 
when i take just two lines from the file with copy paste and assign it to a 
variable like this and it works only with triple quotes
 
test='''_*_n.jpg","timelineCoverPhoto":"{\"focus\":{\"x\":0.5,\"y\":0.386925795053},\"photo\":{\"__type__\":{\"name\":\"Photo\"},\"image_lowres\":{\"uri\":\"https://fbcdn-photos-f-a.akamaihd.net/*-*-*/*_*_*_a.jpg\",\"width\":180,\"height\":179}}}","subscribeStatus":"IS_SUBSCRIBED","smallPictureUrl":"https://fbcdn-profile-a.akamaihd.net/*-*-*/s100x100/*_*_*_s.jpg","contactId":"*==","contactType":"USER","friendshipStatus":"ARE_FRIENDS","graphApiWriteId":"contact_*:*:*","hugePictureUrl":"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-frc3/*_*_*_n.jpg","profileFbid":"1284503586","isMobilePushable":"NO","lookupKey":null,"name":{"displayName":"*
 
*","firstName":"*","lastName":"*"},"nameSearchTokens":["*","*"],"phones":[],"phoneticName":{"displayName":null,"firstName":null,"lastName":null},"isMemorialized":false,"communicationRank":1.1144714,"canViewerSendGift":false,"canMessage":true}
*=={"bigPictureUrl":"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/*.*.*.*/s200x200/*_*_*_n.jpg","timelineCoverPhoto":"{\"focus\":{\"x\":0.5,\"y\":0.49137931034483},\"photo\":{\"__type__\":{\"name\":\"Photo\"},\"image_lowres\":{\"uri\":\"https://fbcdn-photos-h-a.akamaihd.net/*-*-*/*_*_*_a.jpg\",\"width\":180,\"height\":135}}}","subscribeStatus":"IS_SUBSCRIBED","smallPictureUrl":"https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/s100x100/*_*_*_a.jpg","contactId":"*==","contactType":"USER","friendshipStatus":"ARE_FRIENDS","graphApiWriteId":"contact_*:*:*","hugePictureUrl":"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/c0.0.540.540/*_*_*_n.jpg","profileFbid":"*","isMobilePushable":"YES","lookupKey":null,"name":{"displayName":"*
 
*","firstName":"*","lastName":"*"},"nameSearchTokens":["*","*"],"phones":[],"phoneticName":{"displayName":null,"firstName":null,"lastName":null},"isMemorialized":false,"communicationRank":1.2158813,"canViewerSendGift":false,"canMessage":true}'''

uri = re.findall(r'''uri\":\"https://fbcdn-(a-z|photos)?([^\'" >]+)''',test)
print uri

it works fine and i have my result [('photos', 
'-f-a.akamaihd.net/*-*-*/*_*_*_a.jpg'), ('photos', 
'-h-a.akamaihd.net/*-*-*/*_*_*_a.jpg')]

but if a take those lines and save it into a txt file like the original is 
without the quotes and do the following 

datafile=open('a.txt','r')
data_array=''
for line in datafile:
data_array=data_array+line

uri = re.findall(r'''uri\":\"https://fbcdn-(a-z|photos)?([^\'" 
>]+)''',data_array)

after printing uri it gives an empty list,. what to do to make it work for the 
lines of a txt file
-- 
https://mail.python.org/mailman/listinfo/python-list


problem with regex

2014-04-28 Thread dimmaim
i want to find a specific urls from a txt file but i have some issus. First 
when i take just two lines from the file with copy paste and assign it to a 
variable like this and it works only with triple quotes
 
test='''_*_n.jpg","timelineCoverPhoto":"{\"focus\":{\"x\":0.5,\"y\":0.386925795053},\"photo\":{\"__type__\":{\"name\":\"Photo\"},\"image_lowres\":{\"uri\":\"https://fbcdn-photos-f-a.akamaihd.net/*-*-*/*_*_*_a.jpg\",\"width\":180,\"height\":179}}}","subscribeStatus":"IS_SUBSCRIBED","smallPictureUrl":"https://fbcdn-profile-a.akamaihd.net/*-*-*/s100x100/*_*_*_s.jpg","contactId":"*==","contactType":"USER","friendshipStatus":"ARE_FRIENDS","graphApiWriteId":"contact_*:*:*","hugePictureUrl":"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-frc3/*_*_*_n.jpg","profileFbid":"*","isMobilePushable":"NO","lookupKey":null,"name":{"displayName":"*
 
*","firstName":"*","lastName":"*"},"nameSearchTokens":["*","*"],"phones":[],"phoneticName":{"displayName":null,"firstName":null,"lastName":null},"isMemorialized":false,"communicationRank":1.1144714,"canViewerSendGift":false,"canMessage":true}
*=={"bigPictureUrl":"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/*.*.*.*/s200x200/*_*_*_n.jpg","timelineCoverPhoto":"{\"focus\":{\"x\":0.5,\"y\":0.49137931034483},\"photo\":{\"__type__\":{\"name\":\"Photo\"},\"image_lowres\":{\"uri\":\"https://fbcdn-photos-h-a.akamaihd.net/*-*-*/*_*_*_a.jpg\",\"width\":180,\"height\":135}}}","subscribeStatus":"IS_SUBSCRIBED","smallPictureUrl":"https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/s100x100/*_*_*_a.jpg","contactId":"*==","contactType":"USER","friendshipStatus":"ARE_FRIENDS","graphApiWriteId":"contact_*:*:*","hugePictureUrl":"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash3/c0.0.540.540/*_*_*_n.jpg","profileFbid":"*","isMobilePushable":"YES","lookupKey":null,"name":{"displayName":"*
 
*","firstName":"*","lastName":"*"},"nameSearchTokens":["*","*"],"phones":[],"phoneticName":{"displayName":null,"firstName":null,"lastName":null},"isMemorialized":false,"communicationRank":1.2158813,"canViewerSendGift":false,"canMessage":true}'''

uri = re.findall(r'''uri\":\"https://fbcdn-(a-z|photos)?([^\'" >]+)''',test)
print uri

it works fine and i have my result [('photos', 
'-f-a.akamaihd.net/*-*-*/*_*_*_a.jpg'), ('photos', 
'-h-a.akamaihd.net/*-*-*/*_*_*_a.jpg')]

but if a take those lines and save it into a txt file like the original is 
without the quotes and do the following

datafile=open('a.txt','r')
data_array=''
for line in datafile:
data_array=data_array+line

uri = re.findall(r'''uri\":\"https://fbcdn-(a-z|photos)?([^\'" 
>]+)''',data_array)

after printing uri it gives an empty list,. what to do to make it work for the 
lines of a txt file
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: problem with regex

2014-04-28 Thread Roy Smith
In article ,
 dimm...@gmail.com wrote:

> i want to find a specific urls from a txt file but i have some issus. First 
> when i take just two lines from the file with copy paste and assign it to a 
> variable like this and it works only with triple quotes
>  
> test=''
[...]
> but if a take those lines and save it into a txt file like the original is 
> without the quotes [it doesn't work]

I suspect this has nothing to do with regular expressions, but it's just 
about string management.

The first thing you want to do is verify that the text you are reading 
in from the file is the same as the text you have in triple quotes.  So, 
write a program like this:

test=''

datafile=open('a.txt','r')
data_array=''
for line in datafile:
data_array=data_array+line

print test == data_array

If that prints True, then you've got the same text in both cases (and 
you can go on to looking for other problems).  I suspect it will print 
False, though.  So, now your task is to figure out where those two 
strings differ.  Maybe something like:

for c1, c2 in zip(test, data_array):
print c1 == c2, repr(c1), repr(c2)

and look for the first place they're not the same.  Hopefully that will 
give you a clue what's going wrong.
-- 
https://mail.python.org/mailman/listinfo/python-list


Convert numpy array to single number

2014-04-28 Thread mboyd02255
I have a numpy array consisting of 1s and zeros for representing binary numbers:

e.g.

 >>> binary
 array([ 1.,  0.,  1.,  0.])

I wish the array to be in the form 1010, so it can be manipulated.

I do not want to use built in binary converters as I am trying to build my own.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: possible bug in re expression?

2014-04-28 Thread Robin Becker

On 28/04/2014 12:49, Steven D'Aprano wrote:
..


Well, I don't know about "almost any", but at least some broken regexes
will explicitly fail:



py> import re



sre_constants.error: nothing to repeat

(For brevity I have abbreviated the traceback.)


so there is intent to catch some specification errors.

I've abandoned this translation anyhow as all that was intended was to split the 
string into non-overlapping strings of size at most k. I find this works faster 
than the regexp even if the regexp is pre-compiled.


[p[i:i+k] for i in xrange(0,len(p),k)]
--
Robin Becker

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


Re: Convert numpy array to single number

2014-04-28 Thread Steven D'Aprano
On Mon, 28 Apr 2014 06:04:02 -0700, mboyd02255 wrote:

> I have a numpy array consisting of 1s and zeros for representing binary
> numbers:
> 
> e.g.
> 
>  >>> binary
>  array([ 1.,  0.,  1.,  0.])
> 
> I wish the array to be in the form 1010, so it can be manipulated.
> 
> I do not want to use built in binary converters as I am trying to build
> my own.

Did you have a question, or are you just sharing?



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Significant digits in a float?

2014-04-28 Thread Roy Smith
I'm using Python 2.7

I have a bunch of floating point values.  For example, here's a few (printed as 
reprs):

38.0
41.2586
40.752801
49.25
33.7951994
36.8371996
34.1489
45.5

Fundamentally, these numbers have between 0 and 4 decimal digits of precision, 
and I want to be able to intuit how many each has, ignoring the obvious 
floating point roundoff problems.  Thus, I want to map:

38.0  ==> 0
41.2586 ==> 4
40.752801 ==> 4
49.25 ==> 2
33.7951994 ==> 4
36.8371996 ==> 4
34.1489 ==> 4
45.5 ==> 1

Is there any clean way to do that?  The best I've come up with so far is to 
str() them and parse the remaining string to see how many digits it put after 
the decimal point.

The numbers are given to me as Python floats; I have no control over that.  I'm 
willing to accept that fact that I won't be able to differentiate between 
float("38.0") and float("38.").  Both of those map to 1, which is OK for my 
purposes.

---
Roy Smith
r...@panix.com



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


Re: Significant digits in a float?

2014-04-28 Thread Ned Batchelder

On 4/28/14 12:00 PM, Roy Smith wrote:

Fundamentally, these numbers have between 0 and 4 decimal digits of precision, 
and I want to be able to intuit how many each has, ignoring the obvious 
floating point roundoff problems.  Thus, I want to map:

38.0  ==> 0
41.2586 ==> 4
40.752801 ==> 4
49.25 ==> 2
33.7951994 ==> 4
36.8371996 ==> 4
34.1489 ==> 4
45.5 ==> 1

Is there any clean way to do that?  The best I've come up with so far is to 
str() them and parse the remaining string to see how many digits it put after 
the decimal point.


That sounds like a pretty clean way:  len(str(num).partition(".")[2]), 
though it also sounds like you understand all of the inaccuracies in 
that technique.


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

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


Re: Significant digits in a float?

2014-04-28 Thread Chris Angelico
On Tue, Apr 29, 2014 at 2:00 AM, Roy Smith  wrote:
> I have a bunch of floating point values.  For example, here's a few (printed 
> as reprs):
>
> 38.0
> 41.2586
> 40.752801
> 49.25
> 33.7951994
> 36.8371996
> 34.1489
> 45.5
>
> Fundamentally, these numbers have between 0 and 4 decimal digits of 
> precision...

Terminology question: Why do you count only what's after the decimal
point? I would describe these as having between 2 and 6 significant
figures. Will they always have two digits before the decimal, or does
your precision really care only about what's after it?

Of course, there would still remain the problem of describing 49.25
with 6 sig figs, in the same way that it's hard to explain to someone
that you really do need to leave in five minutes (but if you said you
had to leave in six minutes, they'd believe you to be accurate to the
minute). But I assume that's not part of the question.

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


Heartbleed and the windows distributions on python.org

2014-04-28 Thread Timothy McDonald
I am building a cherrypy app that is testing as vulnerable to the heartbleed 
exploit. The app is running on the 64 bit 3.3.5 Windows distribution of python. 
An updated version of 64 bit Python 3.3.x for Windows or an updated pyopenssl? 
I am kind of surprised the distribution on python.org hasen't been updated.

Here is the thread on the issue from the cherrypy-users group.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Significant digits in a float?

2014-04-28 Thread Roy Smith
On Monday, April 28, 2014 12:28:59 PM UTC-4, Chris Angelico wrote:

> Terminology question: Why do you count only what's after the decimal
> point? I would describe these as having between 2 and 6 significant
> figures. Will they always have two digits before the decimal, or does
> your precision really care only about what's after it?

Hmmm, yeah, I was being sloppy in my description.  Definitely would have gotten 
points off in physics class for being so sloppy :-)

No, there's not always two digits before the decimal.  Could be anywhere from 0 
to 3 digits before. What I'm really after is how many digits are after the 
decimal point.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Significant digits in a float?

2014-04-28 Thread Roy Smith
On Monday, April 28, 2014 12:07:14 PM UTC-4, Ned Batchelder wrote:

> On 4/28/14 12:00 PM, Roy Smith wrote:
>> 38.0  ==> 0
>> [...]
>> Is there any clean way to do that?  The best I've come up with so far is to 
>> str() them and parse the
>> remaining string to see how many digits it put after the decimal point.
> 
> That sounds like a pretty clean way:  len(str(num).partition(".")[2]), 
> though it also sounds like you understand all of the inaccuracies in 

Well, it's actually, a little uglier, because I want to map 38.0 ==>0, so I 
need to special case that.

The other annoying thing about using str() is its behavior isn't well defined.  
It looks like it does the right thing, but I imagine the details could change 
in a different implementation.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Significant digits in a float?

2014-04-28 Thread Ned Batchelder

On 4/28/14 2:39 PM, Roy Smith wrote:

On Monday, April 28, 2014 12:07:14 PM UTC-4, Ned Batchelder wrote:


On 4/28/14 12:00 PM, Roy Smith wrote:

38.0  ==> 0
[...]
Is there any clean way to do that?  The best I've come up with so far is to 
str() them and parse the
remaining string to see how many digits it put after the decimal point.


That sounds like a pretty clean way:  len(str(num).partition(".")[2]),
though it also sounds like you understand all of the inaccuracies in


Well, it's actually, a little uglier, because I want to map 38.0 ==>0, so I 
need to special case that.


Ah, right.



The other annoying thing about using str() is its behavior isn't well defined.  
It looks like it does the right thing, but I imagine the details could change 
in a different implementation.



I don't have a reference, but in recent Pythons, str() was specifically 
changed to guarantee that it produces the shortest string that when 
re-interpreted as a float, produces the same float.


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

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


Re:Significant digits in a float?

2014-04-28 Thread Dave Angel
Roy Smith  Wrote in message:
> I'm using Python 2.7
> 
> I have a bunch of floating point values.  For example, here's a few (printed 
> as reprs):
> 
> 38.0
> 41.2586
> 40.752801
> 49.25
> 33.7951994
> 36.8371996
> 34.1489
> 45.5
> 
> Fundamentally, these numbers have between 0 and 4 decimal digits of 
> precision, and I want to be able to intuit how many each has, ignoring the 
> obvious floating point roundoff problems.  Thus, I want to map:
> 
> 38.0  ==> 0
> 41.2586 ==> 4
> 40.752801 ==> 4
> 49.25 ==> 2
> 33.7951994 ==> 4
> 36.8371996 ==> 4
> 34.1489 ==> 4
> 45.5 ==> 1
> 
> Is there any clean way to do that?  The best I've come up with so far is to 
> str() them and parse the remaining string to see how many digits it put after 
> the decimal point.
> 
> The numbers are given to me as Python floats; I have no control over that.  
> I'm willing to accept that fact that I won't be able to differentiate between 
> float("38.0") and float("38.").  Both of those map to 1, which is OK for 
> my purposes.
> 

Ignoring the unexpected terminology,  you seem to be looking for
 the number of decimal places, and you're not interested in 2100
 ==> -2

If you know something about the possible range of the numbers, 
 and/or you know the valid range of decimal places, then you
 should convert to string in a way that will round the 3rd,  5th,
 and 6th values. Then if the string has no decimal,  the answer is
 0. If there are any trailing zeroes,  strip them. Then just count
 digits after the decimal point. 

Without such limits,  there can be no unique algorithm,  and thus
 no correct code.


-- 
DaveA

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


Re: Heartbleed and the windows distributions on python.org

2014-04-28 Thread Ned Deily
In article ,
 Timothy McDonald  wrote:
> I am building a cherrypy app that is testing as vulnerable to the heartbleed 
> exploit. The app is running on the 64 bit 3.3.5 Windows distribution of 
> python. An updated version of 64 bit Python 3.3.x for Windows or an updated 
> pyopenssl? I am kind of surprised the distribution on python.org hasen't been 
> updated.

The current release of Python 3 is 3.4.0.  A 3.4.1 maintenance release, 
with OpenSSL updated in the Windows installer, is planned for final 
release in mid-May.  Python 3.3.x is now in security-fix-only mode which 
means only source fixes for security problems are released as needed and 
no further binary installers for Windows or OS X are produced.  (The 
Python 2 Windows installer is not affected since it bundles an older, 
pre-heartbleed version of OpenSSL.)

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

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


Re:Convert numpy array to single number

2014-04-28 Thread Dave Angel
mboyd02...@gmail.com Wrote in message:
> I have a numpy array consisting of 1s and zeros for representing binary 
> numbers:
> 
> e.g.
> 
>  >>> binary
>  array([ 1.,  0.,  1.,  0.])
> 
> I wish the array to be in the form 1010, so it can be manipulated.
> 
> I do not want to use built in binary converters as I am trying to build my 
> own.
> 

One thousand and ten is an int, not an array. 

So is 10, but it seems a more likely value you might be trying for.

As for using the builtin binary converters, you'd better be a lot
 more specific,  as you cannot even print an int in decimal form
 without utilizing them. The number is in binary already.
 

Please copy the exact problem and constraints,  as well as what
 you've written so far and what's wrong with it.

-- 
DaveA

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


Re:problem with regex

2014-04-28 Thread Dave Angel
dimm...@gmail.com Wrote in message:
> i want to find a specific urls from a txt file but i have some issus. First 
> when i take just two lines from the file with copy paste and assign it to a 
> variable like this and it works only with triple quotes
>  
> test='''_*_n.jpg","timelineCoverPhoto":"{\"focus\":{\"x\":0.5,\"y\":0.386925795053},\"photo\":{\"__type__\":{\"name\":\"Photo\"},\"image_lowres\":{\"uri\":\"https://fbcdn-photos-f-a.akamaihd.net/*-*-*

Why did you start a second thread with similar content two minutes
 after the first? Do you expect us to compare the two messages and
 figure out what you changed,  or were you just impatient for a
 response?  I only check in here about 6 times a day, and I
 imagine some might be even less often.

Your test string literal has lots of backslashes in it, which get
 interpreted into escape sequences in a literal,  but not in a
 file.  If that's really what the file looks like,  you're going
 to want to use a raw string.  I agree with Roy, you're probably
 not getting the same string the two ways.

-- 
DaveA

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


Re: Help with changes in traceback stack from Python 2.7 to Python 3.x

2014-04-28 Thread Ned Batchelder

On 4/27/14 5:51 PM, Andrew Konstantaras wrote:

I guess I am missing something big as I am looking for a shorthand way
of doing the following:

dctA = dict(x=x, y=y, ... n=n)



Yes, your makeDict(x, y) is a shorthand for dict(x=x, y=y), but there 
are many things you can do with dict that you can't do with makeDict. 
What is the makeDict equivalent of:


dict(x=12, y=self.y, z=a+b)

The code you have allows you more compact expression, but it brings 
fragility and surprise.



This is, as I understand it a very natural way of using a dictionary.
It seems that this syntax is unnecessarily redundant and hence my goal
of writing something more compact.  Perhaps the way I am doing it is a
little unorthodox, but the ultimate use of a dictionary is, as I
understand it, completely in line with how dictionaries were designed to
be used.  In my other code, I often use these dictionaries to pass
arguments to functions and return results.  It allows me great
flexibility without breaking existing code.  I pack a dictionary before
passing and unpack when retrieving.


Perhaps you want to create a class instead?  If you find yourself 
passing more than a handful of arguments to a function, and especially 
more than a handful of values returned from a function, then a class 
with methods might be a better way to combine state and behavior.


Also, keep in mind that you can return a tuple from a function if you 
want to return two or three values and assign them to names:


x, y, z = compute_xyz()

You mention unpacking your dictionary after the function call.  How do 
you do that? Isn't that a cumbersome and repetitive operation?




I will give the locals approach a try, it seems a little more clumsy
than simply passing the variables to the function.

Thanks again for your input.

---Andrew


BTW, it's a little easier to follow the threads of conversation if you 
put your responses after the text you are responding to.  This is known 
as bottom-posting, and is preferred to top-posting as you did here.



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

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


Re: Help with changes in traceback stack from Python 2.7 to Python 3.x

2014-04-28 Thread Andrew Konstantaras



On Apr 28, 2014, at 12:36 PM, Ned Batchelder  wrote:


On 4/27/14 5:51 PM, Andrew Konstantaras wrote:
   > I guess I am missing something big as I am looking for a shorthand way
   > of doing the following:
   >
   > dctA = dict(x=x, y=y, ... n=n)
   >

Yes, your makeDict(x, y) is a shorthand for dict(x=x, y=y), but there
are many things you can do with dict that you can't do with makeDict.
What is the makeDict equivalent of:

dict(x=12, y=self.y, z=a+b)

The code you have allows you more compact expression, but it brings
fragility and surprise.

   > This is, as I understand it a very natural way of using a dictionary.
   > It seems that this syntax is unnecessarily redundant and hence my goal
   > of writing something more compact. Perhaps the way I am doing it is a
   > little unorthodox, but the ultimate use of a dictionary is, as I
   > understand it, completely in line with how dictionaries were designed 
to
   > be used. In my other code, I often use these dictionaries to pass
   > arguments to functions and return results. It allows me great
   > flexibility without breaking existing code. I pack a dictionary before
   > passing and unpack when retrieving.

Perhaps you want to create a class instead? If you find yourself
passing more than a handful of arguments to a function, and especially
more than a handful of values returned from a function, then a class
with methods might be a better way to combine state and behavior.

Also, keep in mind that you can return a tuple from a function if you
want to return two or three values and assign them to names:

x, y, z = compute_xyz()

You mention unpacking your dictionary after the function call. How do
you do that? Isn't that a cumbersome and repetitive operation?
One of the reasons I like using the dictionary over the tuple is that if I have a function that grows in functionality (i.e., I decide to return more data for new scenarios), I can just add the new object to the dictionary and I don't have to worry about changing the code every where else that used to call the function. 

Actually, that is one of the nice features of using a dictionary, I can check if the key is there and if it is pull it out.  As I was dusting off this old code, I considered trying to implement this functionality through by creating a class.  I never liked going through the stack, it seemed hacky, but I came to love using dictionaries as they have some great built in features. 


Again, thanks for the advice and I will focus on the class implementation.



   >
   > I will give the locals approach a try, it seems a little more clumsy
   > than simply passing the variables to the function.
   >
   > Thanks again for your input.
   >
   > ---Andrew

BTW, it's a little easier to follow the threads of conversation if you
put your responses after the text you are responding to. This is known
as bottom-posting, and is preferred to top-posting as you did here.


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

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


Re: Sorting list alphabetically

2014-04-28 Thread Terry Reedy

On 4/28/2014 2:33 AM, Kev Dwyer wrote:


Hello Terry,

Regarding your second point, my mistake in not checking the link:
I'd seen a similar one elsewhere and assumed they were the same.

This link should work:
http://msdn.microsoft.com/en-us/library/hzz3tw78


As to your first point, you're right, it seems setlocale(locale.LC_ALL,
'en_US.UTF-8') doesn't work on Windows.


From what I read of the link above and
http://msdn.microsoft.com/en-US/goglobal/bb896001.aspx
given therein, I am going to guess that .UTF-8 is not supported for any 
language.



It seems the locale name needs to be one of the aliases provided
at http://msdn.microsoft.com/en-us/library/39cwe7zf, so for example
locale.setlocale(locale.LC_ALL, 'usa') returns
'English_United States.1252'.

Do you know if this is something people programming on Windows
should just be aware of, or is there a case for a hint of some
kind in the locale module documentation?


*Definitely the latter. Perhaps you could open an issue with a specific 
suggestion.


--
Terry Jan Reedy

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


Re: Soap list and soap users on this list

2014-04-28 Thread Burak Arslan

Hi Joseph,

Sorry for the late response, I seem to have missed this post.

On 04/17/14 21:34, Joseph L. Casale wrote:
> I've been looking at Spyne to produce a service that
> can accept a request formatted as follows:
>
> 
> http://..."; xmlns:xsi=http:/..." 
> xmlns:xsd="http://...";>
> 
> 
>   
>   
>   
> 
>   
> 

https://gist.github.com/plq/11384113

Unfortunately, you need the latest Spyne from
https://github.com/arskom/spyne, this doesn't work with 2.10

2.11 is due around end of may, beginning of june.

Ping back if you got any other questions.

Best,
Burak
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Soap list and soap users on this list

2014-04-28 Thread Joseph L. Casale
> https://gist.github.com/plq/11384113
> 
> Unfortunately, you need the latest Spyne from
> https://github.com/arskom/spyne, this doesn't work with 2.10
> 
> 2.11 is due around end of may, beginning of june.
> 
> Ping back if you got any other questions.

Burak,
Thanks a ton! I've just pulled this down and started working through it.

Much appreciated!
jlc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with changes in traceback stack from Python 2.7 to Python 3.x

2014-04-28 Thread Chris Angelico
On Tue, Apr 29, 2014 at 5:50 AM, Andrew Konstantaras  wrote:
> Actually, that is one of the nice features of using a dictionary, I can
> check if the key is there and if it is pull it out.  As I was dusting off
> this old code, I considered trying to implement this functionality through
> by creating a class.  I never liked going through the stack, it seemed
> hacky, but I came to love using dictionaries as they have some great built
> in features.

In that case, consider using function named arguments. You can simply
list a whole lot of args with defaults, and then provide values for
the exact ones you want:

def f(a=1, b=2, c=3, d=4, e=5):
print("My args are",a,b,c,d,e)

f() # all defaults, like an empty dict
f(d=123) # all defaults but one
f(10,20,e=7) # specify the early ones positionally if you always pass those

This is a really REALLY handy feature.

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


Re: MacOS 10.9.2: threading error using python.org 2.7.6 distribution

2014-04-28 Thread Matthew Pounsett
On Sunday, 27 April 2014 10:33:38 UTC-4, Chris Angelico  wrote:
> In most contexts, "thread unsafe" simply means that you can't use the 
> same facilities simultaneously from two threads (eg a lot of database
> connection libraries are thread unsafe with regard to a single
> connection, as they'll simply write to a pipe or socket and then read
> a response from it). But processes and threads are, on many systems,
> linked. Just the act of spinning off a new thread and then forking can
> potentially cause problems. Those are the exact sorts of issues that
> you'll see when you switch OSes, as it's the underlying thread/process
> model that's significant. (Particularly of note is that Windows is
> *very* different from Unix-based systems, in that subprocess
> management is not done by forking. But not applicable here.)
> 

Thanks, I'll keep all that in mind.  I have to wonder how much of a problem it 
is here though, since I was able to demonstrate a functioning fork inside a new 
thread further up in the discussion.

I have a new development that I find interesting, and I'm wondering if you 
still think it's the same problem.

I have taken that threading object and turned it into a normal function 
definition.  It's still forking the external tool, but it's doing so in the 
main thread, and it is finished execution before any other threads are created. 
  And I'm still getting the same error.

Turns out it's not coming from the threading module, but from the subprocess 
module instead.  Specifically, like 709 of 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py
which is this:

try:
self._execute_child(args, executable, preexec_fn, close_fds,
cwd, env, universal_newlines,
startupinfo, creationflags, shell, to_close,
p2cread, p2cwrite,
c2pread, c2pwrite,
errread, errwrite)
except Exception:

I get the "Warning: No stack to get attribute from" twice when that 
self._execute_child() call is made.  I've tried stepping into it to narrow it 
down further, but I'm getting weird behaviour from the debugger that I've never 
seen before once I do that.  It's making it hard to track down exactly where 
the error is occurring.

Interestingly, it's not actually raising an exception there.  The except block 
is not being run.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: MacOS 10.9.2: threading error using python.org 2.7.6 distribution

2014-04-28 Thread Chris Angelico
On Tue, Apr 29, 2014 at 8:50 AM, Matthew Pounsett
 wrote:
> Thanks, I'll keep all that in mind.  I have to wonder how much of a problem 
> it is here though, since I was able to demonstrate a functioning fork inside 
> a new thread further up in the discussion.
>

Yeah, it's really hard to pin down sometimes. I once discovered a
problem whereby I was unable to spin off subprocesses that did certain
things, but I could do a trivial subprocess (I think I fork/exec'd to
the echo command or something) and that worked fine. Turned out to be
a bug in one of my signal handlers, but the error was being reported
at the point of the forking.

> I have a new development that I find interesting, and I'm wondering if you 
> still think it's the same problem.
>
> I have taken that threading object and turned it into a normal function 
> definition.  It's still forking the external tool, but it's doing so in the 
> main thread, and it is finished execution before any other threads are 
> created.   And I'm still getting the same error.
>

Interesting. That ought to eliminate all possibility of
thread-vs-process issues. Can you post the smallest piece of code that
exhibits the same failure?

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


Re: Inconsistent viewkeys behaviour

2014-04-28 Thread Gregory Ewing

Terry Reedy wrote:
The left operand determines the result. The manual specifies that < and 
 > do not have to be consistent. But I suspect that when 3.x dict.keys() 
was backported to 2.7.0, no one thought to update set, whereas the 
backported key view code already had the comparison.


The question is why set() is raising an exception
instead of returning NotImplemented to give the
other operand a chance.

--
Greg

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


Re: Significant digits in a float?

2014-04-28 Thread Steven D'Aprano
On Mon, 28 Apr 2014 12:00:23 -0400, Roy Smith wrote:

[...]
> Fundamentally, these numbers have between 0 and 4 decimal digits of
> precision, 

I'm surprised that you have a source of data with variable precision, 
especially one that varies by a factor of TEN THOUSAND. The difference 
between 0 and 4 decimal digits is equivalent to measuring some lengths to 
the nearest metre, some to the nearest centimetre, and some to the 
nearest 0.1 of a millimetre. That's very unusual and I don't know what 
justification you have for combining such a mix of data sources.

One possible interpretation of your post is that you have a source of 
floats, where all the numbers are actually measured to the same 
precision, and you've simply misinterpreted the fact that some of them 
look like they have less precision. Since you indicate that 4 decimal 
digits is the maximum, I'm going with 4 decimal digits. So if your data 
includes the float 23.5, that's 23.5 measured to a precision of four 
decimal places (that is, it's 23.5000, not 23.5001 or 23.4999).

On the other hand, if you're getting your values as *strings*, that's 
another story. If you can trust the strings, they'll tell you how many 
decimal places: "23.5" is only one decimal place, "23.5000" is four.

But then what to make of your later example?

> 40.752801 ==> 4

Python floats (C doubles) are quite capable of distinguishing between 
40.7528 and 40.752801. They are distinct numbers:

py> 40.752801 - 40.7528
7.105427357601002e-15

so if a number is recorded as 40.752801 presumably it is because 
it was measured as 40.752801. (How that precision can be 
justified, I don't know! Does it come from the Large Hadron Collider?) If 
it were intended to be 40.7528, I expect it would have be recorded as 
40.7528. What reason do you have to think that something recorded to 14 
decimal places was only intended to have been recorded to 4?

Without knowing more about how your data is generated, I can't advise you 
much, but the whole scenario as you have described it makes me think that 
*somebody* is doing something wrong. Perhaps you need to explain why 
you're doing this, as it seems numerically broken.


> Is there any clean way to do that?  The best I've come up with so far is
> to str() them and parse the remaining string to see how many digits it
> put after the decimal point.

I really think you need to go back to the source. Trying to infer the 
precision of the measurements from the accident of the string formatting 
seems pretty dubious to me.

But I suppose if you wanted to infer the number of digits after the 
decimal place, excluding trailing zeroes (why, I do not understand), up 
to a maximum of four digits, then you could do:

s = "%.4f" % number  # rounds to four decimal places
s = s.rstrip("0")  # ignore trailing zeroes, whether significant or not
count = len(s.split(".")[1])


Assuming all the numbers fit in the range where they are shown in non-
exponential format. If you have to handle numbers like 1.23e19 as well, 
you'll have to parse the string more carefully. (Keep in mind that most 
floats above a certain size are all integer-valued.)


> The numbers are given to me as Python floats; I have no control over
> that.

If that's the case, what makes you think that two floats from the same 
data set were measured to different precision? Given that you don't see 
strings, only floats, I would say that your problem is unsolvable. 
Whether I measure something to one decimal place and get 23.5, or four 
decimal places and get 23.5000, the float you see will be the same.

Perhaps you ought to be using Decimal rather than float. Floats have a 
fixed precision, while Decimals can be configured. Then the right way to 
answer your question is to inspect the number:

py> from decimal import Decimal as D
py> x = D("23.5000")
py> x.as_tuple()
DecimalTuple(sign=0, digits=(2, 3, 5, 0, 0, 0), exponent=-4)

The number of decimal digits precision is -exponent.


> I'm willing to accept that fact that I won't be able to differentiate
> between float("38.0") and float("38.").  Both of those map to 1,
> which is OK for my purposes.

That seems... well, "bizarre and wrong" are the only words that come to 
mind. If I were recording data as "38." and you told me I had 
measured it to only one decimal place accuracy, I wouldn't be too 
pleased. Maybe if I understood the context better?

How about 38.12 and 38.1200?

By the way, you contradict yourself here. Earlier, you described 38.0 as 
having zero decimal places (which is wrong). Here you describe it as 
having one, which is correct, and then in a later post you describe it as 
having zero decimal places again.



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Significant digits in a float?

2014-04-28 Thread Ben Finney
Steven D'Aprano  writes:

> By the way, you contradict yourself here. Earlier, you described 38.0 as 
> having zero decimal places (which is wrong). Here you describe it as 
> having one, which is correct, and then in a later post you describe it as 
> having zero decimal places again.

I get the impression that this is at the core of the misunderstanding.
Having a number's representation ending in “….0” does not mean zero
decimal places; it has exactly one. The value's representation contains
the digit “0” after the decimal point, but that digit is significant to
the precision of the representation.

If the problem could be stated such that “38.0” and “38” and “38.000”
are consistently described with the correct number of decimal digits of
precision (in those examples: one, zero, and three), maybe the
discussion would make more sense.

-- 
 \ “Men never do evil so completely and cheerfully as when they do |
  `\it from religious conviction.” —Blaise Pascal (1623–1662), |
_o__)   Pensées, #894. |
Ben Finney

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


Re: Significant digits in a float?

2014-04-28 Thread Steven D'Aprano
On Tue, 29 Apr 2014 13:23:07 +1000, Ben Finney wrote:

> Steven D'Aprano  writes:
> 
>> By the way, you contradict yourself here. Earlier, you described 38.0
>> as having zero decimal places (which is wrong). Here you describe it as
>> having one, which is correct, and then in a later post you describe it
>> as having zero decimal places again.
> 
> I get the impression that this is at the core of the misunderstanding.
> Having a number's representation ending in “….0” does not mean zero
> decimal places; it has exactly one. The value's representation contains
> the digit “0” after the decimal point, but that digit is significant to
> the precision of the representation.
> 
> If the problem could be stated such that “38.0” and “38” and “38.000”
> are consistently described with the correct number of decimal digits of
> precision (in those examples: one, zero, and three), maybe the
> discussion would make more sense.


It's actually trickier than that. Digits of precision can refer to 
measurement error, or to the underlying storage type. Python floats are C 
doubles, so they have 64 bits of precision (approximately 17 decimal 
digits, if I remember correctly) regardless of the precision of the 
measurement. The OP (Roy) is, I think, trying to guess the measurement 
precision after the fact, given a float. If the measurement error really 
does differ from value to value, I don't think he'll have much luck: 
given a float like 23.0, all we can say is that it has *at least* zero 
significant decimal places. 23.1 has at least one, 23. has at least 
four.

If you can put an upper bound on the precision, as Roy  indicates he can, 
then perhaps a reasonable approach is to convert to a string rounded to 
four decimal places, then strip trailing zeroes:

py> x = 1234.1  # actual internal is closer to 1234.009
py> ("%.4f" % x).rstrip('0')
'1234.1'

then count the number of digits after the dot. (This assumes that the 
string formatting routines are correctly rounded, which they should be on 
*most* platforms.) But again, this only gives a lower bound to the number 
of significant digits -- it's at least one, but might be more.


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


Re: Sorting list alphabetically

2014-04-28 Thread Kev Dwyer
Terry Reedy wrote:

> On 4/28/2014 2:33 AM, Kev Dwyer wrote:
> 
>> Hello Terry,
>>
>> Regarding your second point, my mistake in not checking the link:
>> I'd seen a similar one elsewhere and assumed they were the same.
>>
>> This link should work:
>> http://msdn.microsoft.com/en-us/library/hzz3tw78
>>
>>
>> As to your first point, you're right, it seems setlocale(locale.LC_ALL,
>> 'en_US.UTF-8') doesn't work on Windows.
> 
>  From what I read of the link above and
> http://msdn.microsoft.com/en-US/goglobal/bb896001.aspx
> given therein, I am going to guess that .UTF-8 is not supported for any
> language.
> 
>> It seems the locale name needs to be one of the aliases provided
>> at http://msdn.microsoft.com/en-us/library/39cwe7zf, so for example
>> locale.setlocale(locale.LC_ALL, 'usa') returns
>> 'English_United States.1252'.
>>
>> Do you know if this is something people programming on Windows
>> should just be aware of, or is there a case for a hint of some
>> kind in the locale module documentation?
> 
> *Definitely the latter. Perhaps you could open an issue with a specific
> suggestion.
> 

Thanks! I'll try to write something up this weekend.

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