I’m trying to find the best way to format an sql query string. When I’m debugging my application I’d like to log to file all the sql query strings, and it is important that the string is properly formated. Option 1 This is good for printing the sql string. It is not a good solution if the st…
Tag: python
python: union keys from multiple dictionary?
I have 5 dictionaries and I want a union of their keys. I tried but it gave me an error Am I doing it wrong ? I using normal forloop but I wonder why the above code didn’t work. Answer Your solution works for the first two elements in the list, but then dict1 and dict2 got reduced into a
generate python regex at runtime to match numbers from ‘n’ to infinite
I am using scrapy to crawl a website and extract data from it, scrapy uses regex-based rules to check if a page has to be parsed, or a link has to be followed. I am implementing a resume feature for my spider, so it could continue crawling from the last visited page. For this, I get the last followed link
How do I get an empty list of any size in Python?
I basically want a Python equivalent of this Array in C: but in python I declare an array like: and the problem is I want to assign random slots with values like: but I can’t do that with Python, since the Python list is empty (of length 0). Answer If by “array” you actually mean a Python li…
How to get the current running module path/name
I’ve searched and this seems to be a simple question without a simple answer. I have the file a/b/c.py which would be called with python -m a.b.c. I would like to obtain the value a.b.c in the module level. So when I receive the -h option, I display the USAGE without the need to actually write down the …
Multiple assignment semantics
In Python one can do: I checked the generated bytecode using dis and they are identical. So why allow this at all? Would I ever need one of these instead of the others? Answer One case when you need to include more structure on the left hand side of the assignment is when you’re asking Python unpack a s…
Django: override the save() function of an abstract model
I have tried to override the save() function on a model that is abstract, and am getting an error So if possible, how do you override the save function on an abstract model. The model that extends from this is Entry Here is my model Code: (For those who are familiar, I am extending the Entry model on the zinn…
What is the use of “assert” in Python?
What does assert mean? How is it used? Answer The assert statement exists in almost every programming language. It has two main uses: It helps detect problems early in your program, where the cause is clear, rather than later when some other operation fails. A type error in Python, for example, can go through…
Slicing a list in Python without generating a copy
I have the following problem. Given a list of integers L, I need to generate all of the sublists L[k:] for k in [0, len(L) – 1], without generating copies. How do I accomplish this in Python? With a buffer object somehow? Answer The short answer Slicing lists does not generate copies of the objects in t…
Why doesn’t list have safe “get” method like dictionary?
Why doesn’t list have a safe “get” method like dictionary? Answer Ultimately it probably doesn’t have a safe .get method because a dict is an associative collection (values are associated with names) where it is inefficient to check if a key is present (and return its value) without th…