News for hackers. Stuff that matters.
Re:Python Number Conversion Chart by obiterdictum on Saturday April 20, 2013 @11:04AM (#5581600)
It should be noted that data.encode('hex') and data.decode('hex') would not work in Python 3.x:

http://stackoverflow.com/q/12917278/23643

Re:Python Number Conversion Chart by naftaliharris on Saturday April 20, 2013 @02:12PM (#5582278)
And for people who want to convert user entered numbers, like "3.2 million", into strings, I wrote a package especially for that: https://github.com/naftaliharris/numutil

    >>> from numutil import str2num, num2str
    >>> str2num('1.3 million')
    1300000
    >>> str2num('three and a half')
    Fraction(7, 2)
    >>> str2num('123,456.789')
    123456.789
    >>> num2str(1234567, style='newspaper')
    '1.23 million'
    >>> num2str(1234567, style='words')
    'one million, two hundred thirty four thousand, five hundred sixty seven'
Re:Python Number Conversion Chart by robbles on Saturday April 20, 2013 @05:42PM (#5582919)
Really useful, thanks for making this!

How is the "three and a half" part implemented? Is that just a set of regexes looking for a couple of common patterns? For example, can it parse something like "three and three thirds"?

Re:Python Number Conversion Chart by naftaliharris on Saturday April 20, 2013 @07:38PM (#5583250)
Sure, I'm glad you like it! :)

str2num parses words by iterating through them once and keeping state about the result so far and what I call the "magnitude". So, for example, when you feed it "three and three thirds", it initializes magnitude and result to zero, chomps the first "three", and then sets magnitude = 3.

If the next word were "million", then it would know that magnitude refers to million, and so it would multiply 3 by 1 million. But the next word is "and", which flushes the magnitude to result, (ie, result += magnitude), and resets the magnitude to zero.

The next word is "three", which, as before, sets magnitude to three. Then the next word is "thirds", so you know that magnitude refers to "thirds", and so you multiply the magnitude by Fraction(1, 3) and add it to result.

  >>> str2num("three and three thirds")
  Fraction(4, 1)
The source for this is here: https://github.com/naftaliharris/numutil/blob/master/numutil...
Re:Python Number Conversion Chart by Demiurge on Saturday April 20, 2013 @11:25AM (#5581655)
of all the languages, python probably needs this the least. it's been my main language for 8 years, and once I initially learned the functions, I never needed to be reminded.
Re:Python Number Conversion Chart by robbles on Saturday April 20, 2013 @05:37PM (#5582900)
I think this kind of reference is useful just to remember what can be done "out of the box" with the language, especially for polyglot developers.

I've definitely written my own Python hex string -> binary data conversion function, before I knew some of these tricks. If you spend part of your time working in JavaScript or PHP, it's easy to forget that you don't have to reinvent the wheel with Python.

Re:Python Number Conversion Chart by niggler on Saturday April 20, 2013 @11:23AM (#5581653)
Direct link https://gist.github.com/Nurdok/4096182

'oct' should also be added:

    >>> oct(45)
    '055'
One caveat: Python doesn't assume the leading '0' indicates that the number is octal, so you must specify radix 8:

    >>> int('055', 8)
    45
Re:Python Number Conversion Chart by Dylan16807 on Sunday April 21, 2013 @12:51PM (#5585399)
I would rather call that cause to celebrate than a caveat. The design of octal literals in C is ridiculous.

In fact you almost can't write a program without using them inadvertently. :)

Re:Python Number Conversion Chart by dandellion on Saturday April 20, 2013 @11:30AM (#5581682)
Wouldn't it be better to comment on the direct link, where the author is more likely to see it?
Re:Python Number Conversion Chart by niggler on Saturday April 20, 2013 @11:36AM (#5581703)
Valid point, comment submitted. I assumed the author submitted this to HN in the first place
Re:Python Number Conversion Chart by haven on Saturday April 20, 2013 @12:22PM (#5581843)
Re:Python Number Conversion Chart by haven on Saturday April 20, 2013 @12:56PM (#5581993)