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: inconsistence in the way you define the function __setattr__?
Consider this code: I would expect the same output. However, with CPython 2.5, 2.6 (similarly in 3.2) I get: With PyPy 1.5.0, I get the expected output: Which is the “right” output? (Or what should be the output according to the Python documentation?) Here is the bug report for CPython. Answer I suspect it has to do with a lookup
How can I pass a Python StringIO() object to a ZipFile(), or is it not supported?
I have a StringIO() file-like object, and I am trying to write it to a ZipFile(), but I get this TypeError: Here is a sample of the code I am using: The docs say that StringIO() is a file-like class and that ZipFile() can accept a file-like object. Is there something I am missing? Answer To add a string to
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
Python Button Bind two functions and send arguments with the binding
I am somewhat new to python still, and am working on a project including a GUI. I using with Tkinter and buttons a lot, and am curious if there was a way to run a function with a bind because I want one thing to happen when it is presses and something else when it is released. I don’t understand
How can I make ipdb show more lines of context while debugging?
By default, during debugging in IPython, ipdb shows one line above and one line below the current position in code. Is there an easy way to make the area shown a bit bigger? I’d think it would be configurable, but haven’t been able to find it. Answer easy way to do this – 2022 figure out where you’re loading ipdb
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