I have the following regex to detect start and end script tags in the html file: meaning in short it will catch: <script “NOT THIS</s” > “NOT THIS</s” </script> it works but needs really long time to detect <script>, even minutes or hours for long strings The li…
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
Create a Pandas Dataframe by appending one row at a time
How do I create an empty DataFrame, then add rows, one by one? I created an empty DataFrame: Then I can add a new row at the end and fill a single field with: It works for only one field at a time. What is a better way to add new row to df? Answer You can use df.loc[i], where
Looking for a better python Google Calendar API example
I am trying to write an application that can add events to a google calendar. There is this really neat site that lets you play with the API. http://code.google.com/apis/explorer/#_s=calendar&_v=v3&_m=events.insert Using that site, I was able to build a calendar event using the following stuff. I am h…
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…
Is it safe to use the python word “type” in my code?
Can I use the word type in my own code or is it reserved? My function header: Answer Using type as a keyword argument to a function will mask the built-in function “type” within the scope of the function. So while doing so does not raise a SyntaxError, it is not considered good practice, and I wou…
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 …
“except” statement with same exception class as parameter twice
In Python, how can I use except block with same exception name twice in try/except statements without need to wrap code into one more try/except block? Simple example (here each call of pages.get may raise the exception): For now, in my Django app I do handling like this (but I don’t want “extra&#…
How to get all sub-elements of an element tree with Python ElementTree?
I want to find a way to get all the sub-elements of an element tree like the way ElementTree.getchildren() does, since getchildren() is deprecated since Python version 2.7. I don’t want to use it anymore, though I can still use it currently. Answer All sub-elements (descendants) of elem: A more complete…