Skip to content

Tag: python

Arbitrary precision of square roots

I was quite disappointed when decimal.Decimal(math.sqrt(2)) yielded and the digits after the 15th decimal place turned out wrong. (Despite happily giving you much more than 15 digits!) How can I get the first m correct digits in the decimal expansion of sqrt(n) in Python? Answer Use the sqrt method on Decimal

Python spacing and aligning strings

I am trying to add spacing to align text in between two strings vars without using ” ” to do so Trying to get the text to look like this, with the second column being aligned. Currently have it coded like this, just using spaces… I tried working with string.rjust & srting.ljust but to no…

Dynamically creating a class from file

I’ve seen these “Dynamically create a class” questions which are answered saying, “use the type() function”. I’m sure I’ll have to at some point but right know I’m clueless. But from what I’ve seen you have to already know something about the class, such a…

Compare dictionaries ignoring specific keys

How can I test if two dictionaries are equal while taking some keys out of consideration. For example, should return True. UPD: I’m looking for an efficient, fast solution. UPD2. I ended up with this code, which appears to be the fastest: Timings: https://gist.github.com/2651872 Answer EDIT: This might …