I want a unit test to assert that a variable action within a function is getting set to its expected value, the only time this variable is used is when it is passed in a call to a library. My thought was that I could mock lib.event.Event, and get its input arguments and assert they are of specific value. >…
Tag: python
Show the SQL generated by Flask-SQLAlchemy
I want to get the SQL issued by Flask-SQLAlchemy queries. In Django, I can print the query attribute to get SQL. How can I get something similar in Flask? Answer Flask-SQLAlchemy records debugging information about all queries during a request. You can get the information with get_debug_queries(). Flask-Debug…
Remove duplicates from a dataframe in PySpark
I’m messing around with dataframes in pyspark 1.4 locally and am having issues getting the dropDuplicates method to work. It keeps returning the error: “AttributeError: ‘list’ object has no attribute ‘dropDuplicates’” Not quite sure why as I seem to be following the syntax …
Finding prime numbers using list comprehention
I was trying to generate all prime numbers in range x to y. I tried simple example first: range(10,11) which means to check if 10 is a prime number: Here is my code: I know that the thing is missing the option to tell the expression that x%y != 0 should be checked for all y in range (2,x) and
Why is PyMongo 3 giving ServerSelectionTimeoutError?
I’m using: Python 3.4.2 PyMongo 3.0.2 mongolab running mongod 2.6.9 uWSGI 2.0.10 CherryPy 3.7.0 nginx 1.6.2 uWSGI start params: I setup my MongoClient ONE time: I try and save a JSON dict to MongoDB: It works via a unit test that executes the same code path to mongodb. However when I execute via CherryP…
Python if not == vs if !=
What is the difference between these two lines of code: and Is one more efficient than the other? Would it be better to use Answer Using dis to look at the bytecode generated for the two versions: not == != The latter has fewer operations, and is therefore likely to be slightly more efficient. It was pointed …
Using ffmpeg to obtain video durations in python
I’ve installed ffprobe using the pip ffprobe command on my PC, and installed ffmpeg from here. However, I’m still having trouble running the code listed here. I try to use the following code unsuccessfully. Does anyone know what’s wrong? Am I not referencing the directories correctly? Do I n…
add content to existing docx with python-docx
I’d like to open an existing word document where I already added page numbers and just add some text and headline to it. Here’s a basic example of how I tried to accomplish my goal When I do the above mentioned with a complete fresh document everything works fine – when doing this with the a…
JSON get key path in nested dictionary
Here is my json string, which keeps on changing frquently so the keys position within the dictionary is not same everytime, i need to search for a key and print it corresponding value, Since the json string changes everytime I have written an recursive function(See below) to search for key in the new json str…
How to use JDBC source to write and read data in (Py)Spark?
The goal of this question is to document: steps required to read and write data using JDBC connections in PySpark possible issues with JDBC sources and know solutions With small changes these methods should work with other supported languages including Scala and R. Answer Writing data Include applicable JDBC …