Skip to content
Advertisement

Using @functools.lru_cache with dictionary arguments

I have a method that takes (among others) a dictionary as an argument. The method is parsing strings and the dictionary provides replacements for some substrings, so it doesn’t have to be mutable. This function is called quite often, and on redundant elements so I figured that caching it would improve its efficiency. But, as you may have guessed, since

python win32 COM closing excel workbook

I open several different workbooks (excel xlsx format) in COM, and mess with them. As the program progresses I wish to close one specific workbook but keep the rest open. How do I close ONE workbook? (instead of the entire excel application) Answer The the Workbook COM object has a Close() method. Basically, it should be something like: The above

Sibling package imports

I’ve tried reading through questions about sibling imports and even the package documentation, but I’ve yet to find an answer. With the following structure: How can the scripts in the examples and tests directories import from the api module and be run from the commandline? Also, I’d like to avoid the ugly sys.path.insert hack for every file. Surely this can

How to convert Python decimal to SQLite numeric?

I have a program that reads financial data in JSON and inserts it into an SQLite database. The problem is when I’m inserting it into SQLite numeric column and it doesn’t seem to like the decimal object. I’ve found this question answered before, but the answer is outdated and from what I understand SQLite now has a currency data type

Python logging: use milliseconds in time format

By default logging.Formatter(‘%(asctime)s’) prints with the following format: where 638 is the millisecond. I need to change the comma to a dot: To format the time I can use: however the documentation doesn’t specify how to format milliseconds. I’ve found this SO question which talks about microseconds, but a) I would prefer milliseconds and b) the following doesn’t work on

Logging uncaught exceptions in Python

How do you cause uncaught exceptions to output via the logging module rather than to stderr? I realize the best way to do this would be: But my situation is such that it would be really nice if logging.exception(…) were invoked automatically whenever an exception isn’t caught. Answer As Ned pointed out, sys.excepthook is invoked every time an exception is

Advertisement