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…
Tag: python
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 unde…
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 s…
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 a…
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 whe…
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 pointe…
What is the difference between a string and a byte string?
I am working with a library which returns a “byte string” (bytes) and I need to convert this to a string. Is there actually a difference between those two things? How are they related, and how can I do the conversion? Answer Assuming Python 3 (in Python 2, this difference is a little less well-def…
Python read next()
next() in python does not work. What is an alternative to reading next line in Python? Here is a sample: Running this on a file does not show ‘ne ‘. Answer next() does not work in your case because you first call readlines() which basically sets the file iterator to point to the end of file. Since…