Tuesday, March 30, 2010

comp.lang.python - 25 new messages in 6 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* Binary Decimals in Python - 9 messages, 8 authors
http://groups.google.com/group/comp.lang.python/t/816a3e4b24300797?hl=en
* PyDispatcher on sourceforge doesn't play nice with easy_install - 1 messages,
1 author
http://groups.google.com/group/comp.lang.python/t/122bcc79cac930fe?hl=en
* (a==b) ? 'Yes' : 'No' - 9 messages, 7 authors
http://groups.google.com/group/comp.lang.python/t/b985d5727945d2b4?hl=en
* Python Script Creator/Generator (function factory) - 2 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/4dca843b5238b333?hl=en
* CPAN for python? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/c1c9e54bf7e0a086?hl=en
* How many packages there are in PyPI - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/fd92ecd515d8e0b8?hl=en

==============================================================================
TOPIC: Binary Decimals in Python
http://groups.google.com/group/comp.lang.python/t/816a3e4b24300797?hl=en
==============================================================================

== 1 of 9 ==
Date: Tues, Mar 30 2010 8:49 am
From: Raymond Hettinger


On Mar 30, 8:13 am, aditya <bluemangrou...@gmail.com> wrote:
> To get the decimal representation of a binary number, I can just do
> this:
>
> int('11',2) # returns 3
>
> But decimal binary numbers throw a ValueError:
>
> int('1.1',2) # should return 1.5, throws error instead.
>
> Is this by design? It seems to me that this is not the correct
> behavior.

The int() constructor returns integers.
So, look to float() for non-integral values.
Binary representation isn't supported yet,
but we do have hex:

>>> float.fromhex('1.8')
1.5


Raymond

== 2 of 9 ==
Date: Tues, Mar 30 2010 8:51 am
From: Grant Olson


Doh!

Well the problem is that int's are integers. So yeah, you can't even do
that with normal value "int ('2.1')" will also throw an error. And
floats don't support radix conversion, because no-one really writes
numbers that way. (At least computer programmers...)


On 3/30/2010 11:43 AM, Shashwat Anand wrote:
> The conversion is not supported for decimal integers AFAIK, however
> '0b123.456' is always valid. I guess you can always get a decimal number
> convertor onto Python-recipes
>
>
>
> On Tue, Mar 30, 2010 at 9:05 PM, Grant Olson <kgo@grant-olson.net
> <mailto:kgo@grant-olson.net>> wrote:
>
> On 3/30/2010 11:13 AM, aditya wrote:
> > To get the decimal representation of a binary number, I can just do
> > this:
> >
> > int('11',2) # returns 3
> >
> > But decimal binary numbers throw a ValueError:
> >
> > int('1.1',2) # should return 1.5, throws error instead.
> >
> > Is this by design? It seems to me that this is not the correct
> > behavior.
> >
>
> Well technically that would be a 'radix point', not a decimal point.
>
> But I think the problem is that computers don't store fractional values
> that way internally. They either use floating or fixed point math. You
> would never look at raw binary data on a computer and see something like
> '1010.1010', and no one would write it that way, and no language (that I
> know of) would accept that as a valid value if you did something like "x
> = 0b1010.1010"
>
> So in that sense, it might not be an intentional oversight, but it's not
> a very practical or useful feature.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

== 3 of 9 ==
Date: Tues, Mar 30 2010 8:54 am
From: MRAB


aditya wrote:
> To get the decimal representation of a binary number, I can just do
> this:
>
> int('11',2) # returns 3
>
> But decimal binary numbers throw a ValueError:
>
> int('1.1',2) # should return 1.5, throws error instead.
>
> Is this by design? It seems to me that this is not the correct
> behavior.
>
int() returns an integer (hence the name!), so it should never return a
float anyway.

What you want is for float() to accept a base, but that is rarely
needed.


== 4 of 9 ==
Date: Tues, Mar 30 2010 8:54 am
From: aditya


On Mar 30, 10:37 am, Benjamin Kaplan <benjamin.kap...@case.edu> wrote:
> On Tue, Mar 30, 2010 at 11:13 AM, aditya <bluemangrou...@gmail.com> wrote:
> > To get the decimal representation of a binary number, I can just do
> > this:
>
> > int('11',2) # returns 3
>
> > But decimal binary numbers throw a ValueError:
>
> > int('1.1',2) # should return 1.5, throws error instead.
>
> > Is this by design? It seems to me that this is not the correct
> > behavior.
>
> > - Aditya
> > --
>
> Because int stands for integer and 1.1 is not an integer. You get the
> same error if you try doing int('1.1')
>
> >http://mail.python.org/mailman/listinfo/python-list
>
>

That makes sense. The closest thing I've found is this question on
StackOverflow: http://stackoverflow.com/questions/1592158/python-convert-hex-to-float

It seems to me that adding a conversion feature to floats would be a
lot more intuitive.


== 5 of 9 ==
Date: Tues, Mar 30 2010 8:56 am
From: aditya


On Mar 30, 10:49 am, Raymond Hettinger <pyt...@rcn.com> wrote:
> On Mar 30, 8:13 am, aditya <bluemangrou...@gmail.com> wrote:
>
> > To get the decimal representation of a binary number, I can just do
> > this:
>
> > int('11',2) # returns 3
>
> > But decimal binary numbers throw a ValueError:
>
> > int('1.1',2) # should return 1.5, throws error instead.
>
> > Is this by design? It seems to me that this is not the correct
> > behavior.
>
> The int() constructor returns integers.
> So, look to float() for non-integral values.
> Binary representation isn't supported yet,
> but we do have hex:
>
>     >>> float.fromhex('1.8')
>     1.5
>
> Raymond

That looks very elegant, thanks!


== 6 of 9 ==
Date: Tues, Mar 30 2010 10:05 am
From: Steven D'Aprano


On Tue, 30 Mar 2010 08:28:50 -0700, Patrick Maupin wrote:

> On Mar 30, 10:13 am, aditya <bluemangrou...@gmail.com> wrote:
>> To get the decimal representation of a binary number, I can just do
>> this:
>>
>> int('11',2) # returns 3
>>
>> But decimal binary numbers throw a ValueError:
>>
>> int('1.1',2) # should return 1.5, throws error instead.
>>
>> Is this by design? It seems to me that this is not the correct
>> behavior.
>>
>> - Aditya
>
> So, why should int('1.1', 2) throw an error when int('1.1') doesn't?


>>> int('1.1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.1'


int('1.1', 2) shouldn't return 1.5 because 1.5 isn't an integer.


The obvious question is, why doesn't float('1.1', 2) work? The answer is
that Python doesn't support floats in any base except 10. It's not
something needed very often, and it's harder to get right than it might
seem.

--
Steven


== 7 of 9 ==
Date: Tues, Mar 30 2010 11:14 am
From: John Nagle


aditya wrote:
> On Mar 30, 10:49 am, Raymond Hettinger <pyt...@rcn.com> wrote:
>> On Mar 30, 8:13 am, aditya <bluemangrou...@gmail.com> wrote:
>>
>>> To get the decimal representation of a binary number, I can just do
>>> this:
>>> int('11',2) # returns 3
>>> But decimal binary numbers throw a ValueError:
>>> int('1.1',2) # should return 1.5, throws error instead.
>>> Is this by design? It seems to me that this is not the correct
>>> behavior.
>> The int() constructor returns integers.
>> So, look to float() for non-integral values.
>> Binary representation isn't supported yet,
>> but we do have hex:
>>
>> >>> float.fromhex('1.8')
>> 1.5
>>
>> Raymond
>
> That looks very elegant, thanks!

Hex floats are useful because you can get a string representation
of the exact value of a binary floating point number. It should
always be the case that

float.fromhex(float.hex(x)) == x

That's not always true of decimal representations, due to rounding problems.

Long discussion of this here: "http://bugs.python.org/issue1580"


John Nagle



== 8 of 9 ==
Date: Tues, Mar 30 2010 11:03 am
From: Mensanator


On Mar 30, 10:49 am, Raymond Hettinger <pyt...@rcn.com> wrote:
> On Mar 30, 8:13 am, aditya <bluemangrou...@gmail.com> wrote:
>
> > To get the decimal representation of a binary number, I can just do
> > this:
>
> > int('11',2) # returns 3
>
> > But decimal binary numbers throw a ValueError:
>
> > int('1.1',2) # should return 1.5, throws error instead.
>
> > Is this by design? It seems to me that this is not the correct
> > behavior.
>
> The int() constructor returns integers.
> So, look to float() for non-integral values.
> Binary representation isn't supported yet,

It is supported in the gmpy module.

>>> import gmpy
>>> help(gmpy.mpf)
Help on built-in function mpf in module gmpy:

mpf(...)
mpf(n): builds an mpf object with a numeric value n (n may be any
Python number, or an mpz, mpq, or mpf object) and a
default
precision (in bits) depending on the nature of n
mpf(n,bits=0): as above, but with the specified number of bits (0
means to use default precision, as above)
mpf(s,bits=0,base=10): builds an mpf object from a string s made
up of
digits in the given base, possibly with fraction-part
(with
period as a separator) and/or exponent-part (with exponent
marker 'e' for base<=10, else '@'). If base=256, s must be
a gmpy.mpf portable binary representation as built by the
function gmpy.fbinary (and the .binary method of mpf
objects).
The resulting mpf object is built with a default precision
(in
bits) if bits is 0 or absent, else with the specified
number
of bits.

>>> gmpy.mpf('1.1',0,2)
mpf('1.5e0')


> but we do have hex:
>
>     >>> float.fromhex('1.8')
>     1.5
>
> Raymond

== 9 of 9 ==
Date: Tues, Mar 30 2010 11:08 am
From: Grant Edwards


On 2010-03-30, John Nagle <nagle@animats.com> wrote:

> Hex floats are useful because you can get a string representation of
> the exact value of a binary floating point number. It should always
> be the case that
>
> float.fromhex(float.hex(x)) == x

Until you try running your program on a machine that represents floats
using a radix other than 2,4, or 16.

;)

And it works for NaN and Inf too!

It would have been nice to have had that 5-6 years ago when I had to
write my own pickle/unpickle methods for floating point values so that
inf and nan were portable between Windows and Linux.

--
Grant Edwards grant.b.edwards Yow! But they went to MARS
at around 1953!!
gmail.com

==============================================================================
TOPIC: PyDispatcher on sourceforge doesn't play nice with easy_install
http://groups.google.com/group/comp.lang.python/t/122bcc79cac930fe?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Mar 30 2010 8:56 am
From: "Mike C. Fletcher"


Chris Withers wrote:
> Hi All,
>
> Using easy_install to get PyDispatcher results in:
>
...
> Who's the maintainer of PyDispatcher nowadays? Would be handy if they
> removed the sourceforge link from pypi.
...

Thanks for the report. I've released a 2.0.2 version on PyPI. That
should now work properly with easy_install.

HTH,
Mike

--
________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com


==============================================================================
TOPIC: (a==b) ? 'Yes' : 'No'
http://groups.google.com/group/comp.lang.python/t/b985d5727945d2b4?hl=en
==============================================================================

== 1 of 9 ==
Date: Tues, Mar 30 2010 9:01 am
From: Chris Rebert


On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.beck@hotmail.com> wrote:
> Hi, how can I write the popular C/JAVA syntax in Python?
>
> Java example:
>    return (a==b) ? 'Yes' : 'No'
>
> My first idea is:
>    return ('No','Yes')[bool(a==b)]
>
> Is there a more elegant/common python expression for this?

Yes, Python has ternary operator-like syntax:
return ('Yes' if a==b else 'No')

Note that this requires a recent version of Python.

Cheers,
Chris
--
http://blog.rebertia.com


== 2 of 9 ==
Date: Tues, Mar 30 2010 9:17 am
From: Daniel Fetchinson


>> Hi, how can I write the popular C/JAVA syntax in Python?
>>
>> Java example:
>> return (a==b) ? 'Yes' : 'No'
>>
>> My first idea is:
>> return ('No','Yes')[bool(a==b)]
>>
>> Is there a more elegant/common python expression for this?
>
> return ('Yes' if a == b else 'No')

And for less clutter you can even leave the parenthesis:

return 'Yes' if a == b else 'No'


--
Psss, psss, put it down! - http://www.cafepress.com/putitdown


== 3 of 9 ==
Date: Tues, Mar 30 2010 10:08 am
From: John Nagle


Chris Rebert wrote:
> On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.beck@hotmail.com> wrote:
>> Hi, how can I write the popular C/JAVA syntax in Python?
>>
>> Java example:
>> return (a==b) ? 'Yes' : 'No'
>>
>> My first idea is:
>> return ('No','Yes')[bool(a==b)]
>>
>> Is there a more elegant/common python expression for this?
>
> Yes, Python has ternary operator-like syntax:
> return ('Yes' if a==b else 'No')
>
> Note that this requires a recent version of Python.

Who let the dogs in? That's awful syntax.

John Nagle


== 4 of 9 ==
Date: Tues, Mar 30 2010 9:57 am
From: Joaquin Abian


On Mar 30, 5:40 pm, gentlestone <tibor.b...@hotmail.com> wrote:
> Hi, how can I write the popular C/JAVA syntax in Python?
>
> Java example:
>     return (a==b) ? 'Yes' : 'No'
>
> My first idea is:
>     return ('No','Yes')[bool(a==b)]
>
> Is there a more elegant/common python expression for this?

(a==b) and 'YES' or 'NO'

Yes, ugly

Joaquin


== 5 of 9 ==
Date: Tues, Mar 30 2010 9:58 am
From: Robert Kern


On 2010-03-30 12:08 PM, John Nagle wrote:
> Chris Rebert wrote:
>> On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.beck@hotmail.com>
>> wrote:
>>> Hi, how can I write the popular C/JAVA syntax in Python?
>>>
>>> Java example:
>>> return (a==b) ? 'Yes' : 'No'
>>>
>>> My first idea is:
>>> return ('No','Yes')[bool(a==b)]
>>>
>>> Is there a more elegant/common python expression for this?
>>
>> Yes, Python has ternary operator-like syntax:
>> return ('Yes' if a==b else 'No')
>>
>> Note that this requires a recent version of Python.
>
> Who let the dogs in? That's awful syntax.

http://www.python.org/dev/peps/pep-0308/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

== 6 of 9 ==
Date: Tues, Mar 30 2010 10:09 am
From: Steven D'Aprano


On Tue, 30 Mar 2010 08:40:56 -0700, gentlestone wrote:

> Hi, how can I write the popular C/JAVA syntax in Python?
>
> Java example:
> return (a==b) ? 'Yes' : 'No'
>
> My first idea is:
> return ('No','Yes')[bool(a==b)]

You don't need the call to bool.

('No','Yes')[a==b]


> Is there a more elegant/common python expression for this?

The above is pretty elegant to my eyes, but you can also do:

return 'Yes' if a==b else 'No'

--
Steven


== 7 of 9 ==
Date: Tues, Mar 30 2010 10:11 am
From: Steven D'Aprano


On Tue, 30 Mar 2010 10:08:31 -0700, John Nagle wrote:

>> Yes, Python has ternary operator-like syntax: return ('Yes' if a==b
>> else 'No')
>>
>> Note that this requires a recent version of Python.
>
> Who let the dogs in? That's awful syntax.

I used to think so to, but now I like it. It matches common English
syntax like:

"I'm going to the movies tonight, if I leave the office early, otherwise
I'll stay home and nitpick on Usenet."

--
Steven


== 8 of 9 ==
Date: Tues, Mar 30 2010 10:56 am
From: Steve Holden


John Nagle wrote:
> Chris Rebert wrote:
>> On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.beck@hotmail.com>
>> wrote:
>>> Hi, how can I write the popular C/JAVA syntax in Python?
>>>
>>> Java example:
>>> return (a==b) ? 'Yes' : 'No'
>>>
>>> My first idea is:
>>> return ('No','Yes')[bool(a==b)]
>>>
>>> Is there a more elegant/common python expression for this?
>>
>> Yes, Python has ternary operator-like syntax:
>> return ('Yes' if a==b else 'No')
>>
>> Note that this requires a recent version of Python.
>
> Who let the dogs in? That's awful syntax.
>
Yes, that's deliberately awful syntax. Guido designed it that way to
ensure that people didn't aver-use it, thereby reducing the readability
of Python applications. Speaking purely personally I hardly ever use it,
but don't dislike it.


regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

== 9 of 9 ==
Date: Tues, Mar 30 2010 11:05 am
From: Robert Kern


On 2010-03-30 12:11 PM, Steven D'Aprano wrote:
> On Tue, 30 Mar 2010 10:08:31 -0700, John Nagle wrote:
>
>>> Yes, Python has ternary operator-like syntax: return ('Yes' if a==b
>>> else 'No')
>>>
>>> Note that this requires a recent version of Python.
>>
>> Who let the dogs in? That's awful syntax.
>
> I used to think so to, but now I like it. It matches common English
> syntax like:
>
> "I'm going to the movies tonight, if I leave the office early, otherwise
> I'll stay home and nitpick on Usenet."

I would suggest that this is much more common and less awkward English usage:
"If I leave the office early, I'm going to the movies tonight; otherwise, I'll
stay home and nitpick on Usenet."

I don't have a problem with the current syntax, and while its English analogue
is grammatical, I don't think you can rightly call it idiomatic.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco


==============================================================================
TOPIC: Python Script Creator/Generator (function factory)
http://groups.google.com/group/comp.lang.python/t/4dca843b5238b333?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Mar 30 2010 9:34 am
From: Andrej Mitrovic


Hello,

I have the following situation: I've got a 3rd party application that
has a Python API. The applicaiton has integrated support for MIDI
Hardware Controllers, which can be used to manipulate various
parameters of the application. The app can also load Python User
Scripts, which can define the mapping between the Controllers and
Parameters, but they can also be used to define the behavior of those
mappings as well.

I want to provide my users a GUI App that allows them to map their
MIDI Controllers to the application's Parameters, but with a specific
behavior. Once they've defined the mappings and behavior, my
application would generate a custom Python User Script, which then
gets loaded by the 3rd party app.

I've already got MIDI covered, and the GUI. Deployment via Pyinstaller
has been taken care of as well. My application can currently map
Controllers to Parameters in a 1:1 fashion, and can generate a simple
python User Script with all the mappings. What I'm left with
implementing is the behavior part.

Let me give you a simple example of a possible behavior that the user
could select:

- The user has a button on their MIDI Hardware (a simple type of MIDI
controller)
- The first time the button is hit, a specific parameter is selected
in the 3rd party application.
- The second time the button is hit, a different parameter is
selected.
- And so on, depending on the behavior of the function that's mapped
to that MIDI button

So, in essence, a simple User Script would look something like this
(pseudocode-ish):

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
swap_parameters()
if button1 was_hit_the_first_time:
select parameter1
else:
select parameter2

buttons = (
button1 = swap_parameters()
button3 = parameter3
button4 = parameter4
...
)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

What I had in mind, was to have a collection of behaviors stored in a
file (basically functions such as "swap_parameters") which the user
could select via my application, map them to a specific MIDI
Controller (such as a Button), and generate the User Script.

In essence, I'm trying to build a function factory that's specific to
the 3rd party application's provided API. But the user would have the
ability to change the way the function works. In the above example,
the user might want their MIDI Button to select parameter2 first, and
parameter1 the second time.

Has anyone had any experience with generating functionality this way,
and could you give me some pointers in the right direction? Any tips,
advice, book recommendations are more than welcome.


Kind regards,
Andrej Mitrovic


== 2 of 2 ==
Date: Tues, Mar 30 2010 9:35 am
From: Andrej Mitrovic


I forgot to mention, I'm using Python 2.5.x. I can't use Python 3
unfortunately, the 3rd party application uses Py2.5.x internally, so I
have to limit the functionality to that version.

==============================================================================
TOPIC: CPAN for python?
http://groups.google.com/group/comp.lang.python/t/c1c9e54bf7e0a086?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Mar 30 2010 9:51 am
From: Kushal Kumaran


On Tue, Mar 30, 2010 at 9:53 PM, Someone Something <fordhaivat@gmail.com> wrote:
> Hi,
> I've learned python a few months ago but I still use Perl because of CPAN
> and the tremendous amount of stuff that's already been done for you. is
> there something like CPAN for python?
>

Try PyPI. http://pypi.python.org/pypi

--
regards,
kushal


== 2 of 2 ==
Date: Tues, Mar 30 2010 10:07 am
From: Philip Semanchuk

On Mar 30, 2010, at 12:23 PM, Someone Something wrote:

> Hi,
> I've learned python a few months ago but I still use Perl because of
> CPAN and the tremendous amount of stuff that's already been done for
> you. is there something like CPAN for python?

Yes and no, depending on what CPAN means to you. This question come up
often; have a look in the archives. Here's one thread on the topic
from last month:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/ecd51ced8d24593e/f773e080024862cb?lnk=gst&q=cpan#f773e080024862cb


==============================================================================
TOPIC: How many packages there are in PyPI
http://groups.google.com/group/comp.lang.python/t/fd92ecd515d8e0b8?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Mar 30 2010 10:50 am
From: Joaquin Abian


Hi,
PyPI is reaching the 10000 package figure (In the case of 3.x only
about 140 packages and increasing very very slowly).

Looking at available packages for 3.x I observed that some packages
are listed several times. For example, lxml is listed 5 times.
Are these repetitions included in the package count? If yes, just out
of curiosity, somebody knows what the actual number of different
packages in the PyPI reservoir is?

Joaquin.


== 2 of 2 ==
Date: Tues, Mar 30 2010 11:24 am
From: John Nagle


Joaquin Abian wrote:
> Hi,
> PyPI is reaching the 10000 package figure (In the case of 3.x only
> about 140 packages and increasing very very slowly).
>
> Looking at available packages for 3.x I observed that some packages
> are listed several times. For example, lxml is listed 5 times.
> Are these repetitions included in the package count? If yes, just out
> of curiosity, somebody knows what the actual number of different
> packages in the PyPI reservoir is?
>
> Joaquin.

There aren't that many packages "in" PyPI. It's more of a directory than
a repository. And some of the links are dead.

I just tried "Dejavu", which only has a link to "projects.amor.org". That
subdomain is dead. "amor.org" is some kind of religious organization.

John Nagle


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en

To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate