Re: Append/Replace a row in a pandas DataFrame [SOLVED]

2016-04-14 Thread Peter Otten
Paulo da Silva wrote:

> Às 21:10 de 13-04-2016, Paulo da Silva escreveu:
>> Hi all.
> ...
> 
>> [6 rows x 4 columns]
>> 
>>> dft=pd.DataFrame([[1,2,3,4]],
>> index=[datetime.date(2016,1,12)],columns=df.columns)
>> 
>>> dft
>> A  B  C  D
>> 2016-01-12  1  2  3  4
>> 
>> [1 rows x 4 columns]
>> 
>>> pd.concat([df,dft])
>> Out[71]:
>> A B C D
>> 2013-01-01 00:00:00 -0.111621  1.126761 -2.420517  0.660948
>> 2013-01-02 00:00:00 -0.243397 -0.975684 -0.679209 -0.656913
>> 2013-01-03 00:00:00  0.405816  0.478353  0.621906 -0.262615
>> 2013-01-04 00:00:00 -0.380249  0.416711 -0.906286  1.828339
>> 2013-01-05 00:00:00  0.772747  0.993784  0.452746  1.665306
>> 2013-01-06 00:00:00  0.535011 -0.662874  1.504281  0.543537
>> 2016-01-12   1.00  2.00  3.00  4.00
>> 
>> [7 rows x 4 columns]
>> 
>> Why am I getting the second column?!
> I need to use for example pd.datetime instead of datetime.date. In fact
> there is no extra col but the inclusion of hour in the index.
> Still don't understand why!

It looks like handling of datetime objects is specialised

>>> pd.DataFrame([[1,2]], index=[datetime.datetime.now()], columns=["foo", 
"bar"]).index

[2016-04-14 08:51:09.192283]
Length: 1, Freq: None, Timezone: None

and assumes the "all-midnight" case to mean that no time of day was provided 
whereas date is handled like a generic object

>>> pd.DataFrame([[1,2]], index=[datetime.date.today()], columns=["foo", 
"bar"]).index
Index([2016-04-14], dtype='object')

and triggers usage of the string conversion provided by the entries.
For datetime this includes the time that you mistook for an extra column.

>>> str(datetime.datetime(2010, 12, 21))
'2010-12-21 00:00:00'
>>> str(datetime.date(2010, 12, 21))
'2010-12-21'

>> How do I do to have a row replaced instead of added if its date (index)
>> is an existent one?
> df.loc[]=
> 
> Paulo
> 


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


Query regarding python 2.7.11 release

2016-04-14 Thread Gaurav Rastogi -X (garastog - ARICENT TECHNOLOGIES MAURIITIUS LIMITED at Cisco)
Hi,

We are currently using Python 2.6.7 in our product.
We have received below vulnerabilities from field:

CVE-2014-7185

Integer overflow in bufferobject.c in Python before 2.7.8 allows 
context-dependent attackers to
obtain sensitive information from process memory via a large size and offset in 
a "buffer" function.

CVE-2013-1752

python: multiple unbound readline() DoS flaws in python stdlib

CVE-2014-1912

python: buffer overflow in socket.recvfrom_into()

CVE-2014-4650

It was discovered that the CGIHTTPServer module incorrectly handled URL encoded 
paths.
A remote attacker could use this flaw to execute scripts outside of the cgi-bin 
directory, or disclose source of scripts in the cgi-bin directory


Currently I can see the 2.7.11 is the latest release as per the below link:
https://www.python.org/downloads/

Could you please suggest if the above mentioned vulnerabilities are resolved in 
the latest release?

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


Re: Serious error in int() function?

2016-04-14 Thread Christian Gollwitzer

Am 14.04.16 um 08:52 schrieb ast:

Is it sure that the square root of a square number is always
an integer ?


Mathematically, yes.


I would not like to get a result as 345.

from math import sqrt


sqrt(16)

4.0

sqrt(16).is_integer()

True


for n in range(100):

... if not sqrt(n**2).is_integer():
... print(sqrt(n**2))

it seems to work ... but does it work for all integers ?



No. sqrt uses floating point arithmetic, which in Python uses IEEE 64 
bit floating point math (the C double type). This has 53 bits of 
mantissa, that means if your square is larger than 2^53, and does not 
contain only powers of two in addition, the square root will be off.


But the good news is that there are integer square root algorithms. 
Since Python supports arbitrary large integers, you can use one of those.


An isqrt implementation wa discussed here recently:

https://groups.google.com/forum/#!topic/comp.lang.python/70D5yVj7mIY

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


Accuracy of math.sqrt(), was Re: Serious error in int() function?

2016-04-14 Thread Peter Otten
ast wrote:

> 
>  a écrit dans le message de
> news:[email protected]...
>> Hi,
>>
>> there may be a serious error in python's int() function:
>>
>> print int(float(2.8/0.1))
>>
>> yields
>>
>> 27
>>
>> instead of 28!!
>>
>> I am using Python Python 2.7.6, GCC 4.8.2 on Linux Ubuntu.
>>
>> Is that known?
>> Best,
>> Martin
> 
> 
> I have a similar question, so I post my message here.
> Hope this will not annoy the OP

Well, you "annoy" me ;)

Would you please always take the time to come up with a subject line that 
matches your question. You should also start a new thread.

> Is it sure that the square root of a square number is always
> an integer ?
> 
> I would not like to get a result as 345.
> 
> from math import sqrt
> 
sqrt(16)
> 4.0
 sqrt(16).is_integer()
> True
> 
 for n in range(100):
> ... if not sqrt(n**2).is_integer():
> ... print(sqrt(n**2))
> 
> it seems to work ... but does it work for all integers ?

Even if you get an integer it may not be the one you are expecting:

>>> sqrt((10**22)**2) == 10**22
True
>>> sqrt((10**23)**2) == 10**23
False
>>> sqrt((10**23)**2).is_integer()
True
>>> int(sqrt((10**23)**2))
1611392


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


Re: Convert input to upper case on screen as it is typed

2016-04-14 Thread Ben Finney
Dan Sommers  writes:

> I don't know which OS you're using, but if I run "stty olcuc" in my
> Linux shell, then the input driver does that for me.


Gregory Ewing  writes:

> You might be able to do something with the termios module
> to put the tty driver into the appropriate mode.
>
> If you do that, you'll have to be careful to set it back
> again before the program exits for any reason, otherwise
> your shell session will be messed up.


Thank you both.

Okay, ‘termios.tcgetattr’ will let me preserve the attributes, and with
Dan Sommers's suggestion of which attribute to use, I may have a shot at
setting the terminal attributes.

Then with a top-level exception handler I can clean up by restoring the
saved attributes with ‘termios.tcsetattr’.

I will investigate along those lines.

-- 
 \   “Some people have a problem, and they think “I know, I'll use |
  `\ Perl!”. Now they have some number of problems but they're not |
_o__) sure whether it's a string or an integer.” —Benno Rice, 2011 |
Ben Finney

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


Re: Serious error in int() function?

2016-04-14 Thread blindanagram
On 14/04/2016 07:52, ast wrote:
> 
>  a écrit dans le message de
> news:[email protected]...
>> Hi,
>>
>> there may be a serious error in python's int() function:
>>
>> print int(float(2.8/0.1))
>>
>> yields
>>
>> 27
>>
>> instead of 28!!
>>
>> I am using Python Python 2.7.6, GCC 4.8.2 on Linux Ubuntu.
>>
>> Is that known?
>> Best,
>> Martin
> 
> 
> I have a similar question, so I post my message here.
> Hope this will not annoy the OP
> 
> 
> Is it sure that the square root of a square number is always
> an integer ?
> 
> I would not like to get a result as 345.
> 
> from math import sqrt
> 
 sqrt(16)
> 4.0
 sqrt(16).is_integer()
> True
> 
 for n in range(100):
> ... if not sqrt(n**2).is_integer():
> ... print(sqrt(n**2))
> 
> it seems to work ... but does it work for all integers ?

Assuming that IEEE 754 floating point arithmetic is being used, the
sqrt() function will return an exact integer square root for square
integers provided that the square root is exactly representable.

This means that the result will be correct provided it has 53 or less
bits - just short of 16 decimal digits (i.e for square numbers with less
than 32 digits).

With an integer square root function (isqrt), this program:

for exp in count(20):
  v = 2 ** exp - 1
  if isqrt(v) != sqrt(v):
print(exp, isqrt(v), sqrt(v))
break

terminates with:

  54 18014398509481983 1.8014398509481982e+16

showing a first error for a 54 bit square root

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


Re: Serious error in int() function?

2016-04-14 Thread blindanagram
On 14/04/2016 08:59, [email protected] wrote:
> On 14/04/2016 07:52, ast wrote:

> This means that the result will be correct provided it has 53 or less
> bits - just short of 16 decimal digits (i.e for square numbers with less
> than 32 digits).
> 
> With an integer square root function (isqrt), this program:
> 
> for exp in count(20):
>   v = 2 ** exp - 1
>   if isqrt(v) != sqrt(v):
> print(exp, isqrt(v), sqrt(v))
> break
> 
> terminates with:
> 
>   54 18014398509481983 1.8014398509481982e+16
> 
> showing a first error for a 54 bit square root
> 
I should also have said that the square root of integer squares with
between 15 and 30 decimal digits will only be correct if the square
numbers themselves are exactly representable in 53 bits.  So we can
expect failures for squares with 16 or more digits.



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


Re: How to XOR a byte output?

2016-04-14 Thread durgadevi1
On Wednesday, April 13, 2016 at 9:18:37 PM UTC+8, durgadevi1 wrote:
> Hi all, 
> 
> I have a doubt regarding a problem.
> 
> First, I am required to read a given file.
> 
> 
> The output from the file is given below:
> 
> b'$//W?\xc0\x829\xa2\xb9\x13\x8c\xd5{\'
> 
> 
> I used the type() to identify the class and its a byte class.
> 
> I saw many \x and thought it might be hex.
> 
> 
> So, I used binascii.hexlify() and got the following output:
> b'242f2f573fc08239a2b9138cd57b'
> 
> Now, this output is being encrypted and I need to perform an XOR operation on 
> it in order to retrieve a secret message.
> 
> But, I'm not sure how to code the XOR operation. How do I code that?
> 
> Thank you.
> 
> PS: I'm not sure if I have done any mistake along the way. That's why I have 
> mentioned all the steps that I've done. Let me know if I have done any step 
> wrongly. :)

Ok thank you very much for your replies :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Convert input to upper case on screen as it is typed

2016-04-14 Thread Ben Finney
Ben Finney  writes:

> Okay, ‘termios.tcgetattr’ will let me preserve the attributes, and
> with Dan Sommers's suggestion of which attribute to use, I may have a
> shot at setting the terminal attributes.

This works!

I can get the current attributes, and preserve them; then, later, force
uppercase of all terminal output regardless what the user types; then,
later, request the flag be restored to its prior setting.

This only addresses how the terminal shows its output. The input is
still received by Python as it was typed. However, converting text
behind the scenes to uppercase is a simple problem.

I had been hoping that I could simply wrap some stream in a simple
“convert what they actually type so it's upper case” text codec, without
fiddling at such a low operating-system specific level. This is rather
more esoteric than I had hoped.

But it is a working solution, and easy enough to hide in a library.

Thanks again.

-- 
 \ “I have the simplest tastes. I am always satisfied with the |
  `\best.” —Oscar Wilde, quoted in _Chicago Brothers of the Book_, |
_o__) 1917 |
Ben Finney

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


Re: Convert input to upper case on screen as it is typed

2016-04-14 Thread Chris Angelico
On Thu, Apr 14, 2016 at 6:37 PM, Ben Finney  wrote:
> I had been hoping that I could simply wrap some stream in a simple
> “convert what they actually type so it's upper case” text codec, without
> fiddling at such a low operating-system specific level. This is rather
> more esoteric than I had hoped.

If all you were doing was reading from stdin, then it probably would
be that easy. Wanting readline to work correctly requires a bit more
work; and don't forget that there are escape sequences that should
*not* be uppercased (as they'll change in meaning). So yeah, it's a
bit esoteric... but I'm glad it's working.

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


Re: How to XOR a byte output?

2016-04-14 Thread durgadevi1

> 
> This looks clearer:
> 
>>>> code = b'a0\xed\xf0Z\x15]g^\xce3x'
>>>> key = b')U\x81\x9c55*\x08,\xa2WY'
>>>> bytes(c ^ k for c, k in zip(code, key)).decode()
>'Hello world!'
> 
> 
> Marko

Hi, I have gotten another error message when working with the bytes(c ^ k for 
c, k in zip(code, key)).decode().

Here is the error.
 print(bytes(c ^ k for c, k in zip(CODE, key)).decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0: invalid 
start byte

What I did is XOR the CODE with a certain value before using the

bytes(c ^ k for c, k in zip(CODE, key)).decode() code.

However, I get no errors when using values 0 to 127 to XOR with CODE. But I get 
errors when using values (128 to 255). May I know how I can modify the program 
code so that i can XOR with values (128 to 255)?
 

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


Re: How to XOR a byte output?

2016-04-14 Thread Peter Otten
durgadevi1 wrote:

> 
>> 
>> This looks clearer:
>> 
>>>>> code = b'a0\xed\xf0Z\x15]g^\xce3x'
>>>>> key = b')U\x81\x9c55*\x08,\xa2WY'
>>>>> bytes(c ^ k for c, k in zip(code, key)).decode()
>>'Hello world!'
>> 
>> 
>> Marko
> 
> Hi, I have gotten another error message when working with the bytes(c ^ k
> for c, k in zip(code, key)).decode().
> 
> Here is the error.
>  print(bytes(c ^ k for c, k in zip(CODE, key)).decode())
> UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0:
> invalid start byte
> 
> What I did is XOR the CODE with a certain value before using the
> 
> bytes(c ^ k for c, k in zip(CODE, key)).decode() code.
> 
> However, I get no errors when using values 0 to 127 to XOR with CODE. But
> I get errors when using values (128 to 255). May I know how I can modify
> the program code so that i can XOR with values (128 to 255)?

By default bytes.decode() interprets the sequence of bytes as UTF-8. This 
will fail if for example there is a lone 0x85 because no encoded character 
in UTF-8 starts with that byte:

>>> code = b'a0\xed\xf0Z\x15]g^\xce3x'
>>> key = b'\xe4U\x81\x9c55*\x08,\xa2WY'
>>> decrypted = bytes(c^k for c, k in zip(code, key))
>>> decrypted
b'\x85ello world!'
>>> decrypted.decode()
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0: 
invalid start byte

If you explicitly choose an encoding that maps every single byte to exactly 
one character  (e. g. ISO-8859-15), 

>>> decrypted.decode("iso-8859-15")
'\x85ello world!'

the conversion cannot fail -- but the result may still not make sense.


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


Re: How to XOR a byte output?

2016-04-14 Thread Marko Rauhamaa
durgadevi1 :

>>>>> bytes(c ^ k for c, k in zip(code, key)).decode()
>
> [...]
> UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0:
> invalid start byte
>
> [...]
>
> However, I get no errors when using values 0 to 127 to XOR with CODE.
> But I get errors when using values (128 to 255). May I know how I can
> modify the program code so that i can XOR with values (128 to 255)?

Leave out .decode() at the end.


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


Re: Convert input to upper case on screen as it is typed

2016-04-14 Thread Marko Rauhamaa
Ben Finney :

> I had been hoping that I could simply wrap some stream in a simple
> “convert what they actually type so it's upper case” text codec,
> without fiddling at such a low operating-system specific level. This
> is rather more esoteric than I had hoped.

If you run your program in a Linux terminal, you have to abide by the
Linux terminal principles. It is not a simple Python matter. Echoing is
performed by the terminal driver and not by the Python program.

I do commend Python for having such complete coverage of Linux system
facilities.


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


python3 - No module named 'html5lib'

2016-04-14 Thread Sergio Spina
I'm running a python3 program that requires html5lib but I receive the error No 
module named 'html5lib'.

Here are two session of terminal:

sam@pc ~ $ python
Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
[GCC 4.9.2] on linux2
>>> import html5lib
>>> html5lib.__file__
'/usr/local/lib/python2.7/dist-packages/html5lib/__init__.pyc'
>>> quit()

sam@pc ~ $ python3
Python 3.4.2 (default, Oct  8 2014, 10:45:20) 
[GCC 4.9.1] on linux
>>> import html5lib
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named 'html5lib'

Where can be the problem?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Advice on Python build tools

2016-04-14 Thread Sayth Renshaw
Thanks for the tips. Doit does look interesting.

Regarding template plugins with Nikola the plugins would be only for python
template alternatives such as mako.

Mainly i find the whitespace and readability of Jade/pug far more pythonic
than all tge brackets {%  %} yes its a minor thing but so much clearer.

Anyway checked out mako which has some improvement might see if there is
another with support and create a nikola plugin and then give it a try.

Cheers

Sayth

On Thu, 14 Apr 2016 1:19 am Chris Warrick  wrote:

> On 12 April 2016 at 11:48, Sayth Renshaw  wrote:
> > Hi
> >
> > Looking at the wiki list of build tools
> > https://wiki.python.org/moin/ConfigurationAndBuildTools
> >
> > Has anyone much experience in build tools as i have no preference or
> experience to lean on.
> >
> > Off descriptions only i would choose invoke.
> >
> > My requirements, simply i want to learn and build a simple static
> website generator. Many i am not liking design of or are overkill so its a
> good opportunity to learn, logya is a good starting point for what i think
> a good python static generator should be.
> >
> > Second i want to use Jade templates (js) as i think they are more
> pythonic than jinja and mako so being able to have mixed js and python
> support would be needed.
> >
> > Thoughts?
> >
> > Sayth
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
> Here’s a great static site generator (disclaimer, I’m a core dev over
> there):
>
> https://getnikola.com/
>
> We use doit, which is on that list. With doit, we get an existing
> build system, and incremental rebuilds — for free. I recommend you try
> Nikola, and if you don’t like it and still want to build something
> yourself, doit is going to be a great way to do it. That said,
> incremental builds often involve trial-and-error and subtle bugs when
> you start working on it. And if you don’t like doit, you can always
> write your own build micro-system. Because if you want to write
> something simple and minimal, an existing large build system will just
> make things harder.
>
> As for Jade templates, you can’t do that reasonably. You would need to
> produce some hack to spawn a JavaScript subprocess, and it would limit
> what you can use in templates. Instead, look for a template system
> that is written in Python and that has similar syntax.
>
> (also, I wouldn’t consider such weird-thing-into-real-HTML template
> engines pythonic)
>
> --
> Chris Warrick 
> PGP: 5EAAEA16
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python3 - No module named 'html5lib'

2016-04-14 Thread Ben Finney
Sergio Spina  writes:

> I'm running a python3 program that requires html5lib but I receive the
> error No module named 'html5lib'.

Right, the Python 3 standard library does not have any module by that
name.

If it is to be available, it will need to be installed somehow.

What leads you to think it is available on your system?

-- 
 \“Program testing can be a very effective way to show the |
  `\presence of bugs, but is hopelessly inadequate for showing |
_o__)  their absence.” —Edsger W. Dijkstra |
Ben Finney

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


Re: Convert input to upper case on screen as it is typed

2016-04-14 Thread Steven D'Aprano
On Thu, 14 Apr 2016 05:53 pm, Ben Finney wrote:


> Okay, ‘termios.tcgetattr’ will let me preserve the attributes, and with
> Dan Sommers's suggestion of which attribute to use, I may have a shot at
> setting the terminal attributes.
> 
> Then with a top-level exception handler I can clean up by restoring the
> saved attributes with ‘termios.tcsetattr’.
> 
> I will investigate along those lines.

If you get this working, please post your solution here, and consider
publishing it on ActiveState:

http://code.activestate.com/recipes/



Thanks,



-- 
Steven

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


Re: python3 - No module named 'html5lib'

2016-04-14 Thread Steven D'Aprano
On Thu, 14 Apr 2016 07:31 pm, Sergio Spina wrote:

> I'm running a python3 program that requires html5lib but I receive the
> error No module named 'html5lib'.
> 
> Here are two session of terminal:
> 
> sam@pc ~ $ python
> Python 2.7.9 (default, Mar  1 2015, 12:57:24)
> [GCC 4.9.2] on linux2
> >>> import html5lib
> >>> html5lib.__file__
> '/usr/local/lib/python2.7/dist-packages/html5lib/__init__.pyc'
> >>> quit()

You have installed html5lib as a library for Python 2.7.

Or possibly your Linux distribution has installed it.


> sam@pc ~ $ python3
> Python 3.4.2 (default, Oct  8 2014, 10:45:20)
> [GCC 4.9.1] on linux
> >>> import html5lib
> Traceback (most recent call last):
>   File "", line 1, in 
> ImportError: No module named 'html5lib'
> 
> Where can be the problem?

Python 3 has its own set of libraries, and doesn't automatically use the
Python 2 libraries.

My guess is that if you installed html5lib using yum or apt-get, there will
be a similar command that will install the Python 3 version.



-- 
Steven

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


Re: IdentationError; unexpected indent

2016-04-14 Thread Igor Korot
Salma,

On Thu, Apr 14, 2016 at 3:28 AM, salma ammar  wrote:
> Hi Igor,
>
> thank you very much for your response. In fact, I put the code in bold, that
> is why it displays those symbols.
> This is the code:
>
> import sys
> sys.path.append('C:/Users/user/src/sumo-0.22.0/tools')
> import sumolib
> net =
> sumolib.net.readNet("C:/Users/user/src/sumo-0.22.0/tools/sumolib/net/qgislyonvelo.net.xml")
> radius = 0
> f = open("C:/fichierstationsvelos.txt", "r")
> contenu = f.read()
> print(contenu)
> for id, lat, lon in f:
> x, y = net.convertLonLat2XY(lon, lat)
>   print(x, y)
>   edges = net.getNeighboringEdges(x, y, radius)
> print (edges)
> while len(edges) == 0:
> radius = radius + 10
> edges = net.getNeighboringEdges(x, y, radius)
> distancesAndEdges = sorted([(dist, edge) for edge, dist in edges])
> print(distancesAndEdges)
> edgeofstation[id] = distancesAndEdges[0]
> print (edgeofstation[id])
> f.close()

And what is the error message exactly? Can you copy/paste it as well?

Also, I'd advise you to subscribe to the list so that other people can
help you...

Thank you.

>
>
> Cordially.
>
> 2016-04-13 17:02 GMT+01:00 Igor Korot :
>>
>> Hi, Salma,
>>
>> On Wed, Apr 13, 2016 at 11:53 AM, salma ammar 
>> wrote:
>> > Hi,
>> >
>> > I am about to run this code using python 2.7. But when executing this
>> > code,
>> > an error appears (see attachement): IdentationError; unexpected indent
>> >
>> > What should I rectify to correct this error please?
>> >
>> >
>> > *import sys*
>> > *sys.path.append('C:/Users/user/src/sumo-0.22.0/tools')*
>> > *import sumolib*
>> > *net =
>> >
>> > sumolib.net.readNet("C:/Users/user/src/sumo-0.22.0/tools/sumolib/net/qgislyonvelo.net.xml")*
>> > *radius = 0*
>> > *f = open("C:/fichierstations.txt", "r")*
>> >
>> > *contenu = f.read()*
>> >
>> > *print(contenu)*
>> > *for id, lat, lon in f:*
>> > *x, y = net.convertLonLat2XY(lon, lat)*
>> > *print(x, y)*
>> > *edges = net.getNeighboringEdges(x, y, radius)*
>> > *print (edges)*
>> > *while len(edges) == 0:*
>> > *radius = radius + 10*
>> > *  edges = net.getNeighboringEdges(x, y, radius)*
>> > * distancesAndEdges = sorted([(dist, edge) for edge, dist in edges])*
>> > *  print(distancesAndEdges)*
>> > *edgeofstation[id] = distancesAndEdges[0]*
>> > *print (edgeofstation[id])*
>> > *f.close()*
>>
>> What are those '*' symbols in the e-mails?
>> Are they hard tabs or something else?
>>
>> Please copy and paste the script from the editor.
>>
>> Thank you.
>>
>> >
>> >
>> > Thank you in advance.
>> > --
>> > https://mail.python.org/mailman/listinfo/python-list
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Serious error in int() function?

2016-04-14 Thread blindanagram
On 14/04/2016 09:13, [email protected] wrote:
> On 14/04/2016 08:59, [email protected] wrote:
>> On 14/04/2016 07:52, ast wrote:
> 
>> This means that the result will be correct provided it has 53 or less
>> bits - just short of 16 decimal digits (i.e for square numbers with less
>> than 32 digits).
>>
>> With an integer square root function (isqrt), this program:
>>
>> for exp in count(20):
>>   v = 2 ** exp - 1
>>   if isqrt(v) != sqrt(v):
>> print(exp, isqrt(v), sqrt(v))
>> break
>>
>> terminates with:
>>
>>   54 18014398509481983 1.8014398509481982e+16
>>
>> showing a first error for a 54 bit square root
>>
> I should also have said that the square root of integer squares with
> between 15 and 30 decimal digits will only be correct if the square
> numbers themselves are exactly representable in 53 bits.  So we can
> expect failures for squares with 16 or more digits.

However, if a number with 31 or less digits is known to be the square of
an integer, the IEEE754 sqrt function will (I believe) give the correct
result.

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


Re: [OT] A doubt about a doubt

2016-04-14 Thread Peter Otten
Rustom Mody wrote:

> I have a doubt:
> Is it incorrect to correct someone when they are incorrect?

No doubt about it. Everyone's a winner...

> [Sprinkle a "politically" on the above to taste ]

Ah, contradictio in adiecto. That shifts it to definitely maybe.


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


Re: IdentationError; unexpected indent

2016-04-14 Thread Joel Goldstick
On Thu, Apr 14, 2016 at 8:04 AM, Igor Korot  wrote:
> Salma,
>
> On Thu, Apr 14, 2016 at 3:28 AM, salma ammar  wrote:
>> Hi Igor,
>>
>> thank you very much for your response. In fact, I put the code in bold, that
>> is why it displays those symbols.
>> This is the code:
>>
>> import sys
>> sys.path.append('C:/Users/user/src/sumo-0.22.0/tools')
>> import sumolib
>> net =
>> sumolib.net.readNet("C:/Users/user/src/sumo-0.22.0/tools/sumolib/net/qgislyonvelo.net.xml")
>> radius = 0
>> f = open("C:/fichierstationsvelos.txt", "r")
>> contenu = f.read()
>> print(contenu)

look closely at this for loop.  Print and edges don't line up with the
other statements
>> for id, lat, lon in f:
>> x, y = net.convertLonLat2XY(lon, lat)
>>   print(x, y)
>>   edges = net.getNeighboringEdges(x, y, radius)
>> print (edges)
>> while len(edges) == 0:
>> radius = radius + 10
>> edges = net.getNeighboringEdges(x, y, radius)
>> distancesAndEdges = sorted([(dist, edge) for edge, dist in edges])
>> print(distancesAndEdges)
>> edgeofstation[id] = distancesAndEdges[0]
>> print (edgeofstation[id])
>> f.close()
>
> And what is the error message exactly? Can you copy/paste it as well?
>
> Also, I'd advise you to subscribe to the list so that other people can
> help you...
>
> Thank you.
>
>>
>>
>> Cordially.
>>
>> 2016-04-13 17:02 GMT+01:00 Igor Korot :
>>>
>>> Hi, Salma,
>>>
>>> On Wed, Apr 13, 2016 at 11:53 AM, salma ammar 
>>> wrote:
>>> > Hi,
>>> >
>>> > I am about to run this code using python 2.7. But when executing this
>>> > code,
>>> > an error appears (see attachement): IdentationError; unexpected indent
>>> >
>>> > What should I rectify to correct this error please?
>>> >
>>> >
>>> > *import sys*
>>> > *sys.path.append('C:/Users/user/src/sumo-0.22.0/tools')*
>>> > *import sumolib*
>>> > *net =
>>> >
>>> > sumolib.net.readNet("C:/Users/user/src/sumo-0.22.0/tools/sumolib/net/qgislyonvelo.net.xml")*
>>> > *radius = 0*
>>> > *f = open("C:/fichierstations.txt", "r")*
>>> >
>>> > *contenu = f.read()*
>>> >
>>> > *print(contenu)*
>>> > *for id, lat, lon in f:*
>>> > *x, y = net.convertLonLat2XY(lon, lat)*
>>> > *print(x, y)*
>>> > *edges = net.getNeighboringEdges(x, y, radius)*
>>> > *print (edges)*
>>> > *while len(edges) == 0:*
>>> > *radius = radius + 10*
>>> > *  edges = net.getNeighboringEdges(x, y, radius)*
>>> > * distancesAndEdges = sorted([(dist, edge) for edge, dist in edges])*
>>> > *  print(distancesAndEdges)*
>>> > *edgeofstation[id] = distancesAndEdges[0]*
>>> > *print (edgeofstation[id])*
>>> > *f.close()*
>>>
>>> What are those '*' symbols in the e-mails?
>>> Are they hard tabs or something else?
>>>
>>> Please copy and paste the script from the editor.
>>>
>>> Thank you.
>>>
>>> >
>>> >
>>> > Thank you in advance.
>>> > --
>>> > https://mail.python.org/mailman/listinfo/python-list
>>
>>
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


How to parameterize unittests

2016-04-14 Thread Antoon Pardon

I have a unittest for my avltree module.

Now I want this unittest to also run on a subclass of avltree.
How can I organise this, so that I can largely reuse the
original TestCase?

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


Re: How to parameterize unittests

2016-04-14 Thread David Palao
2016-04-14 16:08 GMT+02:00 Antoon Pardon :
>
> I have a unittest for my avltree module.
>
> Now I want this unittest to also run on a subclass of avltree.
> How can I organise this, so that I can largely reuse the
> original TestCase?
>
> --
> Antoon Pardon
> --
> https://mail.python.org/mailman/listinfo/python-list

First, is it necessary to test the functionality twice?

If so then, why don't you simply subclass the original TestCase?

Or am I missing the point of your question?

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


Re: How to parameterize unittests

2016-04-14 Thread Steven D'Aprano
On Fri, 15 Apr 2016 12:08 am, Antoon Pardon wrote:

> 
> I have a unittest for my avltree module.
> 
> Now I want this unittest to also run on a subclass of avltree.
> How can I organise this, so that I can largely reuse the
> original TestCase?


class Test_AVLTree(unittest.TestCase):
tree = avltree

def test_empty_tree_is_false(self):
instance = self.tree()
self.assertFalse(instance)


class Test_MySubclassTree(Test_AVLTree):
tree = My_Subclass_Tree



-- 
Steven

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


Re: python3 - No module named 'html5lib'

2016-04-14 Thread Wildman via Python-list
On Thu, 14 Apr 2016 02:31:59 -0700, Sergio Spina wrote:

> I'm running a python3 program that requires html5lib but I receive the error 
> No module named 'html5lib'.
> 
> Here are two session of terminal:
> 
> sam@pc ~ $ python
> Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
> [GCC 4.9.2] on linux2
> >>> import html5lib
> >>> html5lib.__file__
> '/usr/local/lib/python2.7/dist-packages/html5lib/__init__.pyc'
> >>> quit()
> 
> sam@pc ~ $ python3
> Python 3.4.2 (default, Oct  8 2014, 10:45:20) 
> [GCC 4.9.1] on linux
> >>> import html5lib
> Traceback (most recent call last):
>   File "", line 1, in 
> ImportError: No module named 'html5lib'
> 
> Where can be the problem?

apt-get install python3-html5lib

-- 
 GNU/Linux user #557453
The cow died so I don't need your bull!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for feedback on weighted voting algorithm

2016-04-14 Thread Martin A. Brown

Greetings Justin,

>score = sum_of_votes/num_of_votes

>votes = [(72, 4), (96, 3), (48, 2), (53, 1), (26, 4), (31, 3), (68, 2), (91, 
>1)]

>Specifically, I'm wondering if this is a good algorithm for 
>weighted voting. Essentially a vote is weighted by the number of 
>votes it counts as. I realize that this is an extremely simple 
>algorithm, but I was wondering if anyone had suggestions on how to 
>improve it.

I snipped most of your code.  I don't see anything wrong with your 
overall approach.  I will make one suggestion: watch out for 
DivisionByZero.

try:
score = sum_of_votes / num_of_votes
except ZeroDivisionError:
score = float('nan')

In your example data, all of the weights were integers, which means 
that a simple mean function would work, as well, if you expanded the 
votes to an alternate representation:

  votes = [72, 72, 72, 72, 96, 96, 96, 48, 48, 53, 26, 26, 26, 26,
   31, 31, 31, 68, 68, 91]

But, don't bother!

Your function can handle votes that have a float weight:

  >>> weight([(4, 1.3), (1, 1),])
  2.695652173913044

Have fun!

-Martin

-- 
Martin A. Brown
http://linux-ip.net/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for feedback on weighted voting algorithm

2016-04-14 Thread justin walters
On Apr 14, 2016 9:41 AM, "Martin A. Brown"  wrote:
>
>
> Greetings Justin,
>
> >score = sum_of_votes/num_of_votes
>
> >votes = [(72, 4), (96, 3), (48, 2), (53, 1), (26, 4), (31, 3), (68, 2),
(91, 1)]
>
> >Specifically, I'm wondering if this is a good algorithm for
> >weighted voting. Essentially a vote is weighted by the number of
> >votes it counts as. I realize that this is an extremely simple
> >algorithm, but I was wondering if anyone had suggestions on how to
> >improve it.
>
> I snipped most of your code.  I don't see anything wrong with your
> overall approach.  I will make one suggestion: watch out for
> DivisionByZero.
>
> try:
> score = sum_of_votes / num_of_votes
> except ZeroDivisionError:
> score = float('nan')
>
> In your example data, all of the weights were integers, which means
> that a simple mean function would work, as well, if you expanded the
> votes to an alternate representation:
>
>   votes = [72, 72, 72, 72, 96, 96, 96, 48, 48, 53, 26, 26, 26, 26,
>31, 31, 31, 68, 68, 91]
>
> But, don't bother!
>
> Your function can handle votes that have a float weight:
>
>   >>> weight([(4, 1.3), (1, 1),])
>   2.695652173913044
>
> Have fun!
>
> -Martin
>
> --
> Martin A. Brown
> http://linux-ip.net/

Thanks Martin!

I'll add the check for division by zero. Didn't think about that. I think
I'm going to sanitize input anyways, but always better to be safe than
sorry.
-- 
https://mail.python.org/mailman/listinfo/python-list


how to setup for localhost:8000

2016-04-14 Thread wrh8609
Hi,

I am working on window 7 and Python 3.5 to setup a localhost:8000 but it did 
not get through as shown below:
> python -m http.server
Serving HTTP on 0.0.0.0 port 8000 ...

But it did not show the results.

Can someone help me how to setup the localhost?

Thanks,
Wen-Ruey

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


Re: how to setup for localhost:8000

2016-04-14 Thread Peter Otten
[email protected] wrote:

> I am working on window 7 and Python 3.5 to setup a localhost:8000 but it
> did not get through as shown below:
>> python -m http.server
> Serving HTTP on 0.0.0.0 port 8000 ...

That looks correct so far. Now open a browser and open

http://localhost:8000/


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


Re: how to setup for localhost:8000

2016-04-14 Thread Andrew Farrell
What happens when you type

http://localhost:8000

Into the address bar of your browser as this is running?

On Thu, Apr 14, 2016 at 12:46 PM,  wrote:

> Hi,
>
> I am working on window 7 and Python 3.5 to setup a localhost:8000 but it
> did not get through as shown below:
> > python -m http.server
> Serving HTTP on 0.0.0.0 port 8000 ...
>
> But it did not show the results.
>
> Can someone help me how to setup the localhost?
>
> Thanks,
> Wen-Ruey
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


installing python

2016-04-14 Thread Cheryl Arko
I am having issues installing the python software on my laptop.  I have
tried to install it several times and when I try to get into the program it
keeps giving me options to modify, repair or uninstall.  I have tried all
of those options and even tried to uninstall and reinstall and still keep
getting the same.  Any suggestions?

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


Re: how to setup for localhost:8000

2016-04-14 Thread Random832
Just a note, some browsers will try to resolve this as www.localhost.com
- try http://127.0.0.1:8000 .

On Thu, Apr 14, 2016, at 14:06, Andrew Farrell wrote:
> What happens when you type
> 
> http://localhost:8000
> 
> Into the address bar of your browser as this is running?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: installing python

2016-04-14 Thread Random832
On Thu, Apr 14, 2016, at 13:59, Cheryl Arko wrote:
> I am having issues installing the python software on my laptop.  I have
> tried to install it several times and when I try to get into the program
> it
> keeps giving me options to modify, repair or uninstall.  I have tried all
> of those options and even tried to uninstall and reinstall and still keep
> getting the same.  Any suggestions?

Are you trying to reopen the same file you downloaded? That's the
installer, not python itself. You need to find Python in the start menu
after installing it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to setup for localhost:8000

2016-04-14 Thread Chris Angelico
On Fri, Apr 15, 2016 at 4:36 AM, Random832  wrote:
> Just a note, some browsers will try to resolve this as www.localhost.com
> - try http://127.0.0.1:8000 .

Huh? Why should the name 'localhost' get a dot com added?

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


Re: Looking for feedback on weighted voting algorithm

2016-04-14 Thread Michael Selik
On Thu, Apr 14, 2016, 7:37 PM justin walters 
wrote:

> On Apr 14, 2016 9:41 AM, "Martin A. Brown"  wrote:
> >
> >
> > Greetings Justin,
> >
> > >score = sum_of_votes/num_of_votes
> >
> > >votes = [(72, 4), (96, 3), (48, 2), (53, 1), (26, 4), (31, 3), (68, 2),
> (91, 1)]
> >
> > >Specifically, I'm wondering if this is a good algorithm for
> > >weighted voting. Essentially a vote is weighted by the number of
> > >votes it counts as. I realize that this is an extremely simple
> > >algorithm, but I was wondering if anyone had suggestions on how to
> > >improve it.
> >
> > I snipped most of your code.  I don't see anything wrong with your
> > overall approach.  I will make one suggestion: watch out for
> > DivisionByZero.
> >
> > try:
> > score = sum_of_votes / num_of_votes
> > except ZeroDivisionError:
> > score = float('nan')
> >
> > In your example data, all of the weights were integers, which means
> > that a simple mean function would work, as well, if you expanded the
> > votes to an alternate representation:
> >
> >   votes = [72, 72, 72, 72, 96, 96, 96, 48, 48, 53, 26, 26, 26, 26,
> >31, 31, 31, 68, 68, 91]
> >
> > But, don't bother!
> >
> > Your function can handle votes that have a float weight:
> >
> >   >>> weight([(4, 1.3), (1, 1),])
> >   2.695652173913044
> >
> > Have fun!
> >
> > -Martin
> >
> > --
> > Martin A. Brown
> > http://linux-ip.net/
>
> Thanks Martin!
>
> I'll add the check for division by zero. Didn't think about that. I think
> I'm going to sanitize input anyways, but always better to be safe than
> sorry.
>

I suggest not worrying about sanitizing inputs. If someone provides bad
data, Python will do the right thing: stop the program and print an
explanation of what went wrong, often a more helpful message than one you'd
write. Use error handling mostly for when you want to do something *other*
than stop the program.

I'm not sure I'd use NaN instead of raise division by zero error. NaNs can
be troublesome for downstream code that might not notice until it gets
confusing. A div-by-zero error is clear and easier to track down because of
the traceback.

What do you think of using list comprehensions?

weighted_sum = sum(rating * weight for rating, weight in votes)
total_weights = sum(weight for rating, weight in votes)
score = weighted_sum / total_weights

It's two loops as I wrote it, which is instinctively slower, but it might
actually execute faster because of the built-in sum vs a regular for loop.
Not sure.

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


I have been dealing with Python for a few weeks...

2016-04-14 Thread Fillmore


...and I'm loving it.

Sooo much more elegant than Perl...and so much less going back to the 
manual to lookup the syntax of simple data structures and operations...


REPL is so useful

and you guys rock too

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


Re: how to setup for localhost:8000

2016-04-14 Thread wrh8609
On Thursday, April 14, 2016 at 2:23:36 PM UTC-4, Andrew Farrell wrote:
> What happens when you type
> 
> http://localhost:8000
> 
> Into the address bar of your browser as this is running?
> 
> On Thu, Apr 14, 2016 at 12:46 PM,  wrote:
> 
> > Hi,
> >
> > I am working on window 7 and Python 3.5 to setup a localhost:8000 but it
> > did not get through as shown below:
> > > python -m http.server
> > Serving HTTP on 0.0.0.0 port 8000 ...
> >
> > But it did not show the results.
> >
> > Can someone help me how to setup the localhost?
> >
> > Thanks,
> > Wen-Ruey
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >

hi Andrew,

Yes. after I type http:\\localhost:8000, the browser did not show anything 
except a empty page without any errors.

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


Using SSML in Python script/program

2016-04-14 Thread Samuel M.

I need to use SSML (Synthesized Speech Markup Language) to play an audio file 
with the tag in my Alexa Skill (as per Amazon's instructions).

Problem is, I don't know how to use SSML with Python. I know I can use it with 
Java but I want to build my skills with Python. I've looked all over, but 
haven't found any working examples of SSML in a Python script/program - does 
anyone know?

Any help is much appreciated!
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: how to setup for localhost:8000

2016-04-14 Thread Dan Strohl via Python-list
If you got an empty page with no errors (and no warnings trying to get 
there...) it is likely that your server is working, and you are trying to 
access it correctly, but the server is not serving anything.  Most of the time, 
if the server is not present, you will get a timeout error saying the browser 
could not connect to server xxx. Or the site can't be reached, or something.

I am not however much of an expert in figuring out WHY it isn't showing 
anything  (I used a different lib for http servers), but I am sure someone else 
on this list is.  I can only tell you that you probably at least launched the 
server correctly.

Have you tried the example on the page in the docs? (about 3/4 of the way down).
https://docs.python.org/3/library/http.server.html

If that doesn't work, it's possible you have a firewall or browser rule 
blocking something... and it if does work, you can look back at your code to 
see what might be wrong.  (I suggest playing with the log_request, log_error, 
and log_message parameters to figure out what is happening.)  

OF course, that all assumes that someone else smarter than I on this list 
doesn't come forth and just say... "well, just do this, and poof, all  will be 
good".

Sorry it's not more.

Dan

 

> -Original Message-
> From: Python-list [mailto:[email protected]]
> On Behalf Of [email protected]
> Sent: Thursday, April 14, 2016 1:50 PM
> To: [email protected]
> Subject: Re: how to setup for localhost:8000
> 
> On Thursday, April 14, 2016 at 2:23:36 PM UTC-4, Andrew Farrell wrote:
> > What happens when you type
> >
> > http://localhost:8000
> >
> > Into the address bar of your browser as this is running?
> >
> > On Thu, Apr 14, 2016 at 12:46 PM,  wrote:
> >
> > > Hi,
> > >
> > > I am working on window 7 and Python 3.5 to setup a localhost:8000
> > > but it did not get through as shown below:
> > > > python -m http.server
> > > Serving HTTP on 0.0.0.0 port 8000 ...
> > >
> > > But it did not show the results.
> > >
> > > Can someone help me how to setup the localhost?
> > >
> > > Thanks,
> > > Wen-Ruey
> > >
> > > --
> > > https://mail.python.org/mailman/listinfo/python-list
> > >
> 
> hi Andrew,
> 
> Yes. after I type http:\\localhost:8000, the browser did not show anything
> except a empty page without any errors.
> 
> Thanks,
> Wen-Ruey
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to XOR a byte output?

2016-04-14 Thread Chris Juried via Python-list
Hello list,
I am new to the list and was wondering if anyone is using Python for MCU 
programing? In particular the AVR and ARM based controllers. Is Python a 
plausible language for MCU programming or is C/C++ or Assembly the only way to 
go? Thanks in advance for your insight.
 Sincerely,   
  Chris   
Audio Engineering Society (AES) Member  
InfoComm-Recognized AV Technologist
http://www.JuriedEngineering.com (Juried Engineering, LLC.)
http://www.TubeEquipment.com (Tube Equipment Corporation)
http://www.HistoryOfRecording.com (History of Recording)


This e-mail, and any attachments thereto, is intended only for use by the 
addressee(s) named herein and may contain legally privileged and/or 
confidential information. If you are not the intended recipient of this e-mail, 
you are hereby notified that any distribution or copying of this email, and any 
attachments thereto, is strictly prohibited. If you have received this email in 
error, please immediately notify me at (754) 300-9972 and permanently delete 
the original and any copy of any e-mail and any printout thereof.


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


Re: I have been dealing with Python for a few weeks...

2016-04-14 Thread justin walters
On Thu, Apr 14, 2016 at 1:50 PM, Fillmore 
wrote:

>
> ...and I'm loving it.
>
> Sooo much more elegant than Perl...and so much less going back to the
> manual to lookup the syntax of simple data structures and operations...
>
> REPL is so useful
>
> and you guys rock too
>
> cheers
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Good to hear you're enjoying it. Out of curiosity, what were you using Perl
for?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for feedback on weighted voting algorithm

2016-04-14 Thread justin walters
Thanks for the advice and the code Michael.

That's definitely a clever way to do it.

The algorithm is going to be used for a web application, so the inputs
should be sanitized for security reasons. It will be trivial to make sure
the inputs are integers and are in the correct range. I will raise a
DivisionByZero error for clarity.

I plan to have the result stored in the cache and then transferred to the
db after every new vote, so this algorithm could see some heavy use for
short periods of time. Therefore, performance is important for me. I
believe the algorithm that I provided is O(n) or O(n+1), but I never really
studied Compsci, so I'm not sure. Either way, I think it should perform
well enough.

On Thu, Apr 14, 2016 at 1:48 PM, Michael Selik 
wrote:

>
>
> On Thu, Apr 14, 2016, 7:37 PM justin walters 
> wrote:
>
>> On Apr 14, 2016 9:41 AM, "Martin A. Brown"  wrote:
>> >
>> >
>> > Greetings Justin,
>> >
>> > >score = sum_of_votes/num_of_votes
>> >
>> > >votes = [(72, 4), (96, 3), (48, 2), (53, 1), (26, 4), (31, 3), (68, 2),
>> (91, 1)]
>> >
>> > >Specifically, I'm wondering if this is a good algorithm for
>> > >weighted voting. Essentially a vote is weighted by the number of
>> > >votes it counts as. I realize that this is an extremely simple
>> > >algorithm, but I was wondering if anyone had suggestions on how to
>> > >improve it.
>> >
>> > I snipped most of your code.  I don't see anything wrong with your
>> > overall approach.  I will make one suggestion: watch out for
>> > DivisionByZero.
>> >
>> > try:
>> > score = sum_of_votes / num_of_votes
>> > except ZeroDivisionError:
>> > score = float('nan')
>> >
>> > In your example data, all of the weights were integers, which means
>> > that a simple mean function would work, as well, if you expanded the
>> > votes to an alternate representation:
>> >
>> >   votes = [72, 72, 72, 72, 96, 96, 96, 48, 48, 53, 26, 26, 26, 26,
>> >31, 31, 31, 68, 68, 91]
>> >
>> > But, don't bother!
>> >
>> > Your function can handle votes that have a float weight:
>> >
>> >   >>> weight([(4, 1.3), (1, 1),])
>> >   2.695652173913044
>> >
>> > Have fun!
>> >
>> > -Martin
>> >
>> > --
>> > Martin A. Brown
>> > http://linux-ip.net/
>>
>> Thanks Martin!
>>
>> I'll add the check for division by zero. Didn't think about that. I think
>> I'm going to sanitize input anyways, but always better to be safe than
>> sorry.
>>
>
> I suggest not worrying about sanitizing inputs. If someone provides bad
> data, Python will do the right thing: stop the program and print an
> explanation of what went wrong, often a more helpful message than one you'd
> write. Use error handling mostly for when you want to do something *other*
> than stop the program.
>
> I'm not sure I'd use NaN instead of raise division by zero error. NaNs can
> be troublesome for downstream code that might not notice until it gets
> confusing. A div-by-zero error is clear and easier to track down because of
> the traceback.
>
> What do you think of using list comprehensions?
>
> weighted_sum = sum(rating * weight for rating, weight in votes)
> total_weights = sum(weight for rating, weight in votes)
> score = weighted_sum / total_weights
>
> It's two loops as I wrote it, which is instinctively slower, but it might
> actually execute faster because of the built-in sum vs a regular for loop.
> Not sure.
>
>>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Serious error in int() function?

2016-04-14 Thread Christian Gollwitzer

Am 15.04.16 um 02:36 schrieb Dennis Lee Bieber:

I should also have said that the square root of integer squares with
between 15 and 30 decimal digits will only be correct if the square
numbers themselves are exactly representable in 53 bits.  So we can
expect failures for squares with 16 or more digits.


However, if a number with 31 or less digits is known to be the square of
an integer, the IEEE754 sqrt function will (I believe) give the correct
result.


How could it EXCEPT by having ~15 significant digits and an exponent --
since that is all the data that is provided by a double precision floating
point. That is, for example,


1000.0 * 1000.0

1e+30

import math
math.sqrt(1e30)

1000.0




only has ONE significant digit -- even though it has thirty 0s before the
decimal point.


No, you need to count the significant digits in binary. 1e30 is not an 
even number in binary floating point.


Apfelkiste:AsynCA chris$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
obase=2
10^30
11001001001011001001110011010100011001110100111011001010\
0100
sqrt(10^30)
1110001101011010100100110001101000

Still, the floating point arithmetic should round to the correct answer 
if both are converted to decimal.


Christian


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


Re: I have been dealing with Python for a few weeks...

2016-04-14 Thread Ben Finney
Fillmore  writes:

> ...and I'm loving it.

Hooray!

> and you guys rock too

I'm glad you have had good responses and help here.

If you feel motivated, please spread the love and write an article
somewhere (your weblog?) outside this community, praising what you've
found good about the language and its community.

-- 
 \  “What we usually pray to God is not that His will be done, but |
  `\   that He approve ours.” —Helga Bergold Gross |
_o__)  |
Ben Finney

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


Create a forecast estimate updated with actuals weekly

2016-04-14 Thread Sayth Renshaw
Hi

Wondering if someone has this knowledge, and please forgive my maths 
expressions.
If I want to estimate need results to achieve a goal at the end of a term 
updated weekly with real results how would I structure this?

So as an example to illustrate my thought process(which could be wrong) These 
are Bills results for the first to weeks.

Bills Goal = 60% after 5 weeks.

wk1 wk2 wk3 wk4 wk5
Bill54.5%   57.1%   

So say these are the results that get it to current.

wk1 wk2 
get opp get opp
6   11  4   7

So 6/11 etc

I am thinking to achieve this I then need to estimate an average opportunity 
rate rounded up. (11 + 7)/2 = 9 so if we had a 5 week term

wk3 wk4 wk5 
get avgopp  get avgopp  get avgopp
X   9   X   9   X   9

So doing it manually I would discover Bill needs 6 out of 9 each week, which 
results in:

sumget  28  0.6 result
sumopp  45  


But how do I structure this so that the new results when known for week 3 
update and adjust the following estimates?

Thanks

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


Re: how to setup for localhost:8000

2016-04-14 Thread Vito De Tullio
Chris Angelico wrote:

>> Just a note, some browsers will try to resolve this as www.localhost.com
>> - try http://127.0.0.1:8000 .
> 
> Huh? Why should the name 'localhost' get a dot com added?

ask browser vendors...

-- 
By ZeD

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


Re: How to parameterize unittests

2016-04-14 Thread Antoon Pardon
Op 14-04-16 om 17:05 schreef Steven D'Aprano:
> On Fri, 15 Apr 2016 12:08 am, Antoon Pardon wrote:
>
>> I have a unittest for my avltree module.
>>
>> Now I want this unittest to also run on a subclass of avltree.
>> How can I organise this, so that I can largely reuse the
>> original TestCase?
>
> class Test_AVLTree(unittest.TestCase):
> tree = avltree
>
> def test_empty_tree_is_false(self):
> instance = self.tree()
> self.assertFalse(instance)
>
>
> class Test_MySubclassTree(Test_AVLTree):
> tree = My_Subclass_Tree

I see, that's going to be a lot of cut & pastes.
Thanks.

-- 
Antoon.

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


Re: Create a forecast estimate updated with actuals weekly

2016-04-14 Thread Steven D'Aprano
On Fri, 15 Apr 2016 02:36 pm, Sayth Renshaw wrote:

> Hi
> 
> Wondering if someone has this knowledge, and please forgive my maths
> expressions. If I want to estimate need results to achieve a goal at the
> end of a term updated weekly with real results how would I structure this?

I'm not really sure that this question has anything to do with Python. But
then I don't really understand the question -- you haven't explained what
you are trying to do. Remember, it might be clear to you, because you have
been working on the question and understand the background of where is
comes from. But to us it is a big mystery.

 
> So as an example to illustrate my thought process(which could be wrong)
> These are Bills results for the first to weeks.
> 
> Bills Goal = 60% after 5 weeks.

60% of what?


> wk1   wk2 wk3 wk4 wk5
> Bill  54.5%   57.1%

What do these numbers represent? Where do they come from? How do they relate
to the goal of 60%?


> So say these are the results that get it to current.

You've mentioned two weeks. What is "current"?

 
> wk1   wk2
> get   opp get opp
> 6 11  4   7
> 
> So 6/11 etc

Where do these numbers come from? What do you mean "get" and "opp"?


> I am thinking to achieve this I then need to estimate an average
> opportunity rate rounded up. (11 + 7)/2 = 9 so if we had a 5 week term
>
> wk3   wk4 wk5
> get   avgopp  get avgopp  get avgopp
> X 9   X   9   X   9
> 
> So doing it manually I would discover Bill needs 6 out of 9 each week,
> which results in:
> 
> sumget28  0.6 result
> sumopp45

Finally something I can understand! 28/45 == 0.6... but why are you
dividing those numbers? Where do they come from? 3*6/9 == 2, not 28/45.


> But how do I structure this so that the new results when known for week 3
> update and adjust the following estimates?

I don't understand your question. You were able to perform a calculation to
get the result 28/45, so why don't you do the same calculation again (but
this time with three input numbers instead of two) to get the next result?




-- 
Steven

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