eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am following the Python tutorial from W3Schools. I just started the MongoDB chapter. I installed MongoDB and checked it with: import pymongo  without getting an error. But as soon as I enter the following code: import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mydict = { "name": "John", "address": "Highway 37" } x = mycol.insert_one(mydict) print(x.inserted_id)  I get these messages and an error message at the bottom in cmd: cd C:\Users\xxx\myname python index.py  Output: Traceback (most recent call last): File "index.py", line 8, in <module> x = mycol.insert_one(mydict) File "C:\Users\path...\pymongo\collection.py", line 695, in insert_one self._insert(document, File "C:\Users\path...\pymongo\collection.py", line 610, in _insert return self._insert_one( File "C:\Users\path...\pymongo\collection.py", line 599, in _insert_one self.__database.client._retryable_write( File "C:\Users\path...\pymongo\mongo_client.py", line 1490, in _retryable_write with self._tmp_session(session) as s: File "C:\Program Files\WindowsApps\path...\lib\contextlib.py", line 113, in __enter__ return next(self.gen) File "C:\Users\path...\pymongo\mongo_client.py", line 1823, in _tmp_session s = self._ensure_session(session) File "C:\Users\path...\pymongo\mongo_client.py", line 1810, in _ensure_session return self.__start_session(True, causal_consistency=False) File "C:\Users\path...\pymongo\mongo_client.py", line 1763, in __start_session server_session = self._get_server_session() File "C:\Users\path...\pymongo\mongo_client.py", line 1796, in _get_server_session return self._topology.get_server_session() File "C:\Users\path...\pymongo\topology.py", line 482, in get_server_session self._select_servers_loop( File "C:\Users\path...\pymongo\topology.py", line 208, in _select_servers_loop raise ServerSelectionTimeoutError( pymongo.errors.ServerSelectionTimeoutError: localhost: 27017: [WinError 10061] Could not connect because target computer actively refused connection  I also tried too disabling firewall temporarily, but the error kept coming up. I used: "python 3.8.2 , mongoDB 4.2.5.0 , pymongo 3.10.1 , windows 10 home"  What is going wrong?

Answer: There is nothing wrong with your code. If you have disabled your firewall, the most likely reason is that the MongoDB service is not installed or running. On Windows, press the Windows key and type services to open the services application. Check the service MongoDB Server is listed and has a Running status. You can test local connectivity by opening your favourite Windows terminal or PowerShell and typing mongo. If it is working you should see: PS> mongo MongoDB shell version v4.2.3 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("1b5499b8-166a-4de6-a8c9-643499f04e66") } MongoDB server version: 4.2.3
Question: I am performing a voice activity detection on the recorded audio file to detect speech vs non-speech portions in the waveform. The output of the classifier looks like (highlighted green regions indicate speech):  The only issue I face here is making it work for a stream of audio input (for eg: from a microphone) and do real-time analysis for a stipulated time-frame. I know PyAudio can be used to record speech from the microphone dynamically and there a couple of real-time visualization examples of a waveform, spectrum, spectrogram, etc, but could not find anything relevant to carrying out feature extraction in a near real-time manner. 

Answer: You should try using Python bindings to webRTC VAD from Google. It's lightweight, fast and provides very reasonable results, based on GMM modelling. As the decision is provided per frame, the latency is minimal. # Run the VAD on 10 ms of silence. The result should be False. import webrtcvad vad = webrtcvad.Vad(2) sample_rate = 16000 frame_duration = 10 # ms frame = b'\x00\x00' * int(sample_rate * frame_duration / 1000) print('Contains speech: %s' % (vad.is_speech(frame, sample_rate))  Also, this article might be useful for you. UPDATE December 2022 As the topic still draws attention, I'd like to update my answer. SileroVAD is very fast and very accurate VAD that was released recently under MIT license.
Question: I always use pip install (which I think is equivalent to pip3 install since I only have python3 in my env) to install packages. But I recently heard python3 -m pip install is better. Why?

Answer: I would advise against ever calling any pip somecommand (or pip3) script directly. Instead it's much safer to call pip's executable module for a specific Python interpreter explicitly, something of the form path/to/pythonX.Y -m pip somecommand. There are many advantages to this, for example:  It is explicit for which Python interpreter the projects will be pip-installed (Python 2 or 3, inside the virtual environment or not, etc.) For a virtual environment, one can pip-install (or do other things) without activating it: path/to/venv/bin/python -m pip install SomeProject Under Windows this is the only way to safely upgrade pip itself path\to\venv\Scripts\python.exe -m pip install --upgrade pip  But yes, if all is perfectly setup, then python3 -m pip install SomeProject and pip3 install SomeProject should do the exact same thing, but there are way too many cases where there is an issue with the setup and things don't work as expected and users get confused (as shown by the many questions about this topic on this platform).  References  Brett Cannon's article "Why you should use python -m pip" pip's documentation section on "Upgrading pip" venv's documentation section on "Creating virtual environments": "You don’t specifically need to activate an environment [...]"
Question: For example, int(x) float(x) str(x) What is time complexity of them?

Answer: There is no definite answer to this because it depends not just what type you're converting to, but also what type you're converting from. Let's consider just numbers and strings. To avoid writing "log" everywhere, we'll measure the size of an int by saying n is how many bits or digits it takes to represent it. (Asymptotically it doesn't matter if you count bits or digits.) For strings, obviously we should let n be the length of the string. There is no meaningful way to measure the "input size" of a float object, since floating-point numbers all take the same amount of space.  Converting an int, float or str to its own type ought to take Θ(1) time because they are immutable objects, so it's not even necessary to make a copy. Converting an int to a float ought to take Θ(1) time because you only need to read at most a fixed constant number of bits from the int object to find the mantissa, and the bit length to find the exponent. Converting an int to a str ought to take Θ(n2) time, because you have to do Θ(n) division and remainder operations to find n digits, and each arithmetic operation takes Θ(n) time because of the size of the integers involved. Converting a str to an int ought to take Θ(n2) time because you need to do Θ(n) multiplications and additions on integers of size Θ(n). Converting a str to a float ought to take Θ(n) time. The algorithm only needs to read a fixed number of characters from the string to do the conversion, and floating-point arithmetic operations (or operations on bounded int values to avoid intermediate rounding errors) for each character take Θ(1) time; but the algorithm needs to look at the rest of the characters anyway in order to raise a ValueError if the format is wrong. Converting a float to any type takes Θ(1) time because there are only finitely many distinct float values.  I've said "ought to" because I haven't checked the actual source code; this is based on what the conversion algorithms need to do, and the assumption that the algorithms actually used aren't asymptotically worse than they need to be. There could be special cases to optimise the str-to-int conversion when the base is a power of 2, like int('11001010', 2) or int('AC5F', 16), since this can be done without arithmetic. If those cases are optimised then they should take Θ(n) time instead of Θ(n2). Likewise, converting an int to a str in a base which is a power of 2 (e.g. using the bin or hex functions) should take Θ(n) time.
Question: FastAPI automatically generates a schema in the OpenAPI spec for UploadFile parameters. For example, this code: from fastapi import FastAPI, File, UploadFile app = FastAPI() @app.post("/uploadfile/") async def create_upload_file(file: UploadFile = File(..., description="The file")): return {"filename": file.filename}  will generate this schema under components:schemas in the OpenAPI spec: { "Body_create_upload_file_uploadfile__post": { "title": "Body_create_upload_file_uploadfile__post", "required":["file"], "type":"object", "properties":{ "file": {"title": "File", "type": "string", "description": "The file","format":"binary"} } } }  How can I explicitly specify the schema for UploadFiles (or at least its name)? I have read FastAPIs docs and searched the issue tracker but found nothing.

Answer: You can edit the OpenAPI schema itself. I prefer to just move these schemas to the path (since they are unique to each path anyway): from fastapi import FastAPI, File, UploadFile from fastapi.openapi.utils import get_openapi app = FastAPI() @app.post("/uploadfile/") async def create_upload_file(file1: UploadFile = File(...), file2: UploadFile = File(...)): pass def custom_openapi(): if app.openapi_schema: return app.openapi_schema openapi_schema = get_openapi( title="Custom title", version="2.5.0", description="This is a very custom OpenAPI schema", routes=app.routes, ) # Move autogenerated Body_ schemas, see https://github.com/tiangolo/fastapi/issues/1442 for path in openapi_schema["paths"].values(): for method_data in path.values(): if "requestBody" in method_data: for content_type, content in method_data["requestBody"]["content"].items(): if content_type == "multipart/form-data": schema_name = content["schema"]["$ref"].lstrip("#/components/schemas/") schema_data = openapi_schema["components"]["schemas"].pop(schema_name) content["schema"] = schema_data app.openapi_schema = openapi_schema return app.openapi_schema app.openapi = custom_openapi
Question: I am running Arduion IDE 1.8.12 on Ubuntu 18.04.4 LTS. I am trying to compile Example code for ESP32 Camera module (standard camera module with default example on Arduino IDE) and I got this error (which I think is not Arduino issue, but Python): "exec: "python": executable file not found in $PATH Error compiling for board ESP32 Wrover Module"  Same message with all ESP32. I also did sudo apt install python. Got back this: Reading package lists... Done Building dependency tree Reading state information... Done python is already the newest version (2.7.15~rc1-1). 0 modernizētas, 0 instalētas no jauna, 0 tiks noņemtas un 6 netiks modernizētas.  When I type Python in Terminal, got this back: Python 2.7.17 (default, Nov 7 2019, 10:07:09) [GCC 7.4.0] on linux2 Type "help", "copyright", "credits" or "license" for more information.  Thank you for help! BR, Valters

Answer: To solve & Fixed the following upload error from Arduino To ESP32-CAM (And for ESP32 too): environment: ubuntu 20.04 64bit, Arduino 1.8.13 ESP32-CAM And yp-05 (for ESP's serial connection)  exec: "python": executable file not found in $PATH Error compiling for board AI Thinker ESP32-CAM.  The solution is:  Installing the package (for example python-is-python3_3.8.2-4_all):  sudo dpkg -i python-is-python3_3.8.2-4_all.deb 2) Wiring WIRES colors: | black | NO | WHITE | GRAY | BROWN | EMPTY yp-05 legs ordere: | GRD | EMPTY | VCC | TX | RX | DIR esp32s CAM: | GRD | EMPTY | 3.3V | GPIO 3 UOR | GPIO 1 UOT | EMPTY *** just For Upload: SHORT ESP32-CAM 100 & GRD I hope it will save time to start using ESP32-CAM (And ESP32 too). That's it - Solved & Run!
Question: I am not able to download file using gdown package.It is giving permission error. But when i am opening it manually.It is giving no such error and opening up fine. Here is the code i am using and link import gdown url='https://drive.google.com/uc?id=0B1lRQVLFjBRNR3Jqam1menVtZnc' output='letter.pdf' gdown.download(url, output, quiet=False)  Error is Permission denied: https://drive.google.com/uc?id=0B1lRQVLFjBRNR3Jqam1menVtZnc Maybe you need to change permission over 'Anyone with the link'?

Answer: If you're working with big files (in my case was a >1gb file), you can solve by copying the url from 'Download anyway' button in Google Drive.
Question: I tried to run FastAPI using uvicorn webserver but it throws an error. I run this command, uvicorn api:app --reload --host 0.0.0.0  but there is an error in the terminal. Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) Started reloader process [23445] Error loading ASGI app. Could not import module "api". Stopping reloader process [23445] 

Answer: TL;DR Add the directory name in front of your filename uvicorn src.main:app  or cd into that directory cd src uvicorn main:app  Long Answer It happens because you are not in the same folder with your FastAPI app instance more specifically: Let's say i have an app-tree like this; my_fastapi_app/ ├── app.yaml ├── docker-compose.yml ├── src │ └── main.py └── tests ├── test_xx.py └── test_yy.py $ pwd # Present Working Directory /home/yagiz/Desktop/my_fastapi_app  I'm not inside the same folder with my app instance, so if I try to run my app with uvicorn I'll get an error like yours $ uvicorn main:app --reload INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [40645] using statreload ERROR: Error loading ASGI app. Could not import module "main".  The answer is so simple, add the folder name in front of your filename uvicorn src.main:app --reload  or you can change your working directory cd src  Now i'm inside of the folder with my app instance src └── main.py  Run your uvicorn again $ uvicorn main:app --reload INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [40726] using statreload INFO: Started server process [40728] INFO: Waiting for application startup. INFO: Application startup complete.
Question: I am using pytest to test a FastAPI endpoint that gets in input an image in binary format as in @app.post("/analyse") async def analyse(file: bytes = File(...)): image = Image.open(io.BytesIO(file)).convert("RGB") stats = process_image(image) return stats  After starting the server, I can manually test the endpoint successfully by running a call with requests import requests from requests_toolbelt.multipart.encoder import MultipartEncoder url = "http://127.0.0.1:8000/analyse" filename = "./example.jpg" m = MultipartEncoder( fields={'file': ('filename', open(filename, 'rb'), 'image/jpeg')} ) r = requests.post(url, data=m, headers={'Content-Type': m.content_type}, timeout = 8000) assert r.status_code == 200  However, setting up tests in a function of the form: from fastapi.testclient import TestClient from requests_toolbelt.multipart.encoder import MultipartEncoder from app.server import app client = TestClient(app) def test_image_analysis(): filename = "example.jpg" m = MultipartEncoder( fields={'file': ('filename', open(filename, 'rb'), 'image/jpeg')} ) response = client.post("/analyse", data=m, headers={"Content-Type": "multipart/form-data"} ) assert response.status_code == 200  when running tests with python -m pytest, that gives me back a  > assert response.status_code == 200 E assert 400 == 200 E + where 400 = <Response [400]>.status_code tests\test_server.py:22: AssertionError -------------------------------------------------------- Captured log call --------------------------------------------------------- ERROR fastapi:routing.py:133 Error getting request body: can't concat NoneType to bytes ===================================================== short test summary info ====================================================== FAILED tests/test_server.py::test_image_analysis - assert 400 == 200  what am I doing wrong? What's the right way to write a test function test_image_analysis() using an image file? 

Answer: You see a different behavior because requests and TestClient are not exactly same in every aspect as TestClient wraps requests. To dig deeper, refer to the source code: (FastAPI is using TestClient from starlette library, FYI) https://github.com/encode/starlette/blob/master/starlette/testclient.py To solve, you can get rid of MultipartEncoder because requests can accept file bytes and encode it by form-data format, with something like # change it r = requests.post(url, data=m, headers={'Content-Type': m.content_type}, timeout = 8000) # to r = requests.post(url, files={"file": ("filename", open(filename, "rb"), "image/jpeg")})  and modifying the FastAPI test code: # change response = client.post("/analyse", data=m, headers={"Content-Type": "multipart/form-data"} ) # to response = client.post( "/analyse", files={"file": ("filename", open(filename, "rb"), "image/jpeg")} )
Question: I am using Tensorflow 2.0 and facing the following situation: @tf.function def my_fn(items): .... #do stuff return  If items is a dict of Tensors like for example: item1 = tf.zeros([1, 1]) item2 = tf.zeros(1) items = {"item1": item1, "item2": item2}  Is there a way of using input_signature argument of tf.function so I can force tf2 to avoid creating multiple graphs when item1 is for example tf.zeros([2,1]) ?

Answer: The input signature has to be a list, but elements in the list can be dictionaries or lists of Tensor Specs. In your case I would try: (the name attributes are optional) signature_dict = { "item1": tf.TensorSpec(shape=[2], dtype=tf.int32, name="item1"), "item2": tf.TensorSpec(shape=[], dtype=tf.int32, name="item2") } # don't forget the brackets around the 'signature_dict' @tf.function(input_signature = [signature_dict]) def my_fn(items): .... # do stuff return # calling the TensorFlow function my_fun(items)  However, if you want to call a particular concrete function created by my_fn, you have to unpack the dictionary. You also have to provide the name attribute in tf.TensorSpec. # creating a concrete function with an input signature as before but without # brackets and with mandatory 'name' attributes in the TensorSpecs my_concrete_fn = my_fn.get_concrete_function(signature_dict) # calling the concrete function with the unpacking operator my_concrete_fn(**items)  This is annoying but should be resolved in TensorFlow 2.3. (see the end of the TF Guide to 'Concrete functions')
Question: When I try to Activate "conda activate tensorflow_cpu" conda activate tensorflow_cpu  Error : Failed to create temp directory "C:\Users\user\AppData\Local\Temp\conda-\"

Answer: It is due to a bug from conda developers. The bug is the temp path is having names with spaces, so to overcome please reassign the Env Variables TEMP, TMP. (for windows)  go to environment variables In "User Variables for " section look for TEMP, TMP double click on TMP and in "variable value", type "C:\conda_tmp" similarly do it for TEMP close env variables section Restart the anaconda prompt, the error should vanish
Question: I'm trying to run a Profile Report for EDA in conda Jupyter NB, but keep getting errors. Here is my code thus far:  import pandas_profiling from pandas_profiling import ProfileReport profile = ProfileReport(data)  and   profile = pandas_profiling.ProfileReport(data)  both of which produce:   TypeError: concat() got an unexpected keyword argument 'join_axes'   Research recommended upgrading to Pandas 1.0, which I'm using.  Also tried  data.profile_report()   AttributeError: 'DataFrame' object has no attribute 'profile_report'   Any tips on where I am going wrong?  Addendum...So I finally figured it out. Needed to install latest version of pandas-profiling in conda, which was 202003 version. Too easy.

Answer: Installed most recent version (March 2020) of pandas-profiling in conda.  conda install -c conda-forge/label/cf202003 pandas-profiling  Was then able to import pandas_profiling in jupyter notebook
Question: I have a pyspark dataframe with following schema: root |-- src_ip: integer (nullable = true) |-- dst_ip: integer (nullable = true)  When converting this dataframe to pandas via toPandas(), the column type changes from integer in spark to float in pandas: <class 'pandas.core.frame.DataFrame'> RangeIndex: 9847 entries, 0 to 9846 Data columns (total 2 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 src_ip 9607 non-null float64 1 dst_ip 9789 non-null float64 dtypes: float64(2) memory usage: 154.0 KB  Is there any way to keep integer value with toPandas() or I can only cast column type in resulting pandas dataframe? 

Answer: SPARK-21766 (https://issues.apache.org/jira/browse/SPARK-21766) explains the behavior your observed. As a workaround, you can call fillna(0) before toPandas(): df1 = sc.createDataFrame([(0, None), (None, 8)], ["src_ip", "dest_ip"]) print(df1.dtypes) # Reproduce the issue pdf1 = df1.toPandas() print(pdf1.dtypes) # A workaround pdf2 = df1.fillna(0).toPandas() print(pdf2.dtypes)
Question: I followed this guide https://firebase.google.com/docs/hosting/cloud-run to setup cloud run docker. Then I tried to follow this guide https://cloud.google.com/run/docs/logging to perform a simple log. Trying to write a structured log to stdout This is my code:  trace_header = request.headers.get('X-Cloud-Trace-Context') if trace_header: trace = trace_header.split('/') global_log_fields['logging.googleapis.com/trace'] = "projects/sp-64d90/traces/" + trace[0] # Complete a structured log entry. entry = dict(severity='NOTICE', message='This is the default display field.', # Log viewer accesses 'component' as jsonPayload.component'. component='arbitrary-property', **global_log_fields) print(json.dumps(entry))  I cannot see this log in the Cloud Logs Viewer. I do see the http Get logs each time I call the docker. Am I missing anything? I am new to this and wondered what is the simples way to be able to log information and view it assuming the docker I created was exactly with the steps from the guide (https://firebase.google.com/docs/hosting/cloud-run) Thanks

Answer: I am running into the exact same issue. I did find that flushing stdout causes the logging to appear when it otherwise would not. Looks like a bug in Cloud Run to me. print(json.dumps(entry)) import sys sys.stdout.flush()  Output with flushing