eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: Is it possible to get the URL that a request came from in FastAPI? For example, if I have an endpoint that is requested at api.mysite.com/endpoint and a request is made to this endpoint from www.othersite.com, is there a way that I can retrieve the string "www.othersite.com" in my endpoint function?

Answer: The premise of the question, which could be formulated as  a server can identify the URL from where a request came  is misguided. True, some HTTP requests (especially some of the requests issued by browsers) carry an Origin header and/or a Referer [sic] header. Also, the Forwarded header, if present, contains information about where the request was issued. However, nothing in the HTTP specification requires that requests in general advertise where they came from. Therefore, whether with FastAPI or some other server technology, there's no definite way of knowing where a request came from.
Question: Here is my code, it was working properly before I was not getting an error while using it. I don't understand how it happened even though I didn't change with it. :  results = [] for query in my_list: results.append(search(query, tld="com", num=1, stop=1, pause=2))  Error: results.append(search(query, tld="com", num=1, stop=1, pause=2)) TypeError: search() got an unexpected keyword argument 'tld' 

Answer: It is from the google python package. it is still working of all the versions. version parameters :  query : query string that we want to search for. tld : tld stands for top level domain which means we want to search our result on google.com or google.in or some other domain. lang : lang stands for language. num : Number of results we want. start : First result to retrieve. stop : Last result to retrieve. Use None to keep searching forever. pause: Lapse to wait between HTTP requests. Lapse too short may cause Google to block your IP. Keeping significant lapse will make your program slow but its safe and better option. Return : Generator (iterator) that yields found URLs. If the stop parameter is None the iterator will loop forever.  Here is your real problem: There is one more python package with the module name as googlesearch Link here Since it might be installed on your environment, this might be calling this module which does not have these parameters included. The BlockBuster Solution is: (tested these both packages on local)  Delete your Python Environment Create a new one install pip install beautifulsoup4 and pip install google Now use your code which will work like charm. Never install the pip install googlesearch-python python package
Question: I have the following df: df = pd.DataFrame({'key': {0: 'EFG_DS_321', 1: 'EFG_DS_900', 2: 'EFG_DS_900', 3: 'EFG_Q_900', 4: 'EFG_DS_1000', 5: 'EFG_DS_1000', 6: 'EFG_DS_1000', 7: 'ABC_DS_444', 8: 'EFG_DS_900', 9: 'EFG_DS_900', 10: 'EFG_DS_321', 11: 'EFG_DS_900', 12: 'EFG_DS_1000', 13: 'EFG_DS_900', 14: 'EFG_DS_321', 15: 'EFG_DS_321', 16: 'EFG_DS_1000', 17: 'EFG_DS_1000', 18: 'EFG_DS_1000', 19: 'EFG_DS_1000', 20: 'ABC_DS_444', 21: 'EFG_DS_900', 22: 'EFG_DAS_12345', 23: 'EFG_DAS_12345', 24: 'EFG_DAS_321', 25: 'EFG_DS_321', 26: 'EFG_DS_12345', 27: 'EFG_Q_1000', 28: 'EFG_DS_900', 29: 'EFG_DS_321'}})  and I have the following dict: d = {'ABC_AS_1000': 123, 'ABC_AS_444': 321, 'ABC_AS_231341': 421, 'ABC_AS_888': 412, 'ABC_AS_087': 4215, 'ABC_DAS_1000': 3415, 'ABC_DAS_444': 4215, 'ABC_DAS_231341': 3214, 'ABC_DAS_888': 321, 'ABC_DAS_087': 111, 'ABC_Q_1000': 222, 'ABC_Q_444': 3214, 'ABC_Q_231341': 421, 'ABC_Q_888': 321, 'ABC_Q_087': 41, 'ABC_DS_1000': 421, 'ABC_DS_444': 421, 'ABC_DS_231341': 321, 'ABC_DS_888': 41, 'ABC_DS_087': 41, 'EFG_AS_1000': 213, 'EFG_AS_900': 32, 'EFG_AS_12345': 1, 'EFG_AS_321': 3, 'EFG_DAS_1000': 421, 'EFG_DAS_900': 321, 'EFG_DAS_12345': 123, 'EFG_DAS_321': 31, 'EFG_Q_1000': 41, 'EFG_Q_900': 51, 'EFG_Q_12345': 321, 'EFG_Q_321': 321, 'EFG_DS_1000': 41, 'EFG_DS_900': 51, 'EFG_DS_12345': 321, 'EFG_DS_321': 1}  I want to map d into df, but given that the real data is very large and complicated, i'm trying to understand if map or merge is better in terms of efficiency (running time). first option: a simple map res = df['key'].map(d)  second option: convert d into a dataframe and preform a merge d1 = pd.DataFrame.from_dict(d,orient='index',columns=['res']) res = df.merge(d1,left_on='key',right_index=True)['res']  Any help will be much appreciated (or any better solutions of course:))

Answer: map will be faster than a merge If your goal is simply to assign a numerical category to each unique value in df['AB'], you could use pandas.factorize that should be a bit faster than map: res = df['AB'].factorize()[0]+1  output: array([1, 1, 1, 2, 2, 3, 3, 3]) test on 800k rows: factorize 28.6 ms ± 153 µs map 32.1 ms ± 110 µs merge 68.6 ms ± 1.33 ms
Question: I have a string that I'm trying to validate against a few regex patterns and I was hoping since Pattern matching is available in 3.10, I might be able to use that instead of creating an if-else block. Consider a string 'validateString' with possible values 1021102,1.25.32, string021. The code I tried would be something like the following. match validateString: case regex1: print('Matched regex1') case regex2: print('Matched regex2') case regex3: print('Matched regex3')  For regex 1, 2 and 3, I've tried string regex patterns and also re.compile objects but it doesn't seem to work. I have been trying to find examples of this over the internet but can't seem to find any that cover regex pattern matching with the new python pattern matching. Any ideas for how I can make it work? Thanks!

Answer: It is not possible to use regex-patterns to match via structural pattern matching (at this point in time). From: PEP0643: structural-pattern-matching  PEP 634: Structural Pattern Matching Structural pattern matching has been added in the form of a match statement and case statements of patterns with associated actions. Patterns consist of sequences, mappings, primitive data types as well as class instances. Pattern matching enables programs to extract information from complex data types, branch on the structure of data, and apply specific actions based on different forms of data. (emphasis mine)  Nothing in this gives any hint that evoking match / search functions of the re module on the provided pattern is intended to be used for matching.  You can find out more about the reasoning behind strucutral pattern matching by reading the actuals PEPs:  PEP 634 -- Structural Pattern Matching: Specification PEP 635 -- Structural Pattern Matching: Motivation and Rationale PEP 636 -- Structural Pattern Matching: Tutorial  they also include ample examples on how to use it.
Question: I want to remove all signs from my dataframe to leave it in either one of the two formats: 100-200 or 200 So the salaries should either have a single hyphen between them if a range of salaries if given, otherwise a clean single number. I have the following data: import pandas as pd import re df = {'salary':['£26,768 - £30,136/annum Attractive benefits package', '£26,000 - £28,000/annum plus bonus', '£21,000/annum', '£26,768 - £30,136/annum Attractive benefits package', '£33/hour', '£18,500 - £20,500/annum Inc Bonus - Study Support + Bens', '£27,500 - £30,000/annum £27,500 to £30,000 + Study', '£35,000 - £40,000/annum', '£24,000 - £27,000/annum Study Support (ACCA / CIMA)', '£19,000 - £24,000/annum Study Support', '£30,000 - £35,000/annum', '£44,000 - £66,000/annum + 15% Bonus + Excellent Benefits. L', '£75 - £90/day £75-£90 Per Day']} data = pd.DataFrame(df)  Here's what I have tried to remove some of the signs: salary = [] for i in data.salary: space = re.sub(" ",'',i) lower = re.sub("[a-z]",'',space) upper = re.sub("[A-Z]",'',lower) bracket = re.sub("/",'',upper) comma = re.sub(",", '', bracket) plus = re.sub("\+",'',comma) percentage = re.sub("\%",'', plus) dot = re.sub("\.",'', percentage) bracket1 = re.sub("\(",'',dot) bracket2 = re.sub("\)",'',bracket1) salary.append(bracket2)  Which gives me: '£26768-£30136', '£26000-£28000', '£21000', '£26768-£30136', '£33', '£18500-£20500-', '£27500-£30000£27500£30000', '£35000-£40000', '£24000-£27000', '£19000-£24000', '£30000-£35000', '£44000-£6600015', '£75-£90£75-£90'  However, I have some repeating numbers, essentially I want anything after the first range of values removed, and any sign besides the hyphen between the two numbers. Expected output:  '26768-30136', '26000-28000', '21000', '26768-30136', '33', '18500-20500', '27500-30000', '35000-40000', '24000-27000', '19000-24000', '30000-35000', '44000-66000', '75-90 

Answer: Another way using pandas.Series.str.partition with replace: data["salary"].str.partition("/")[0].str.replace("[^\d-]+", "", regex=True)  Output: 0 26768-30136 1 26000-28000 2 21000 3 26768-30136 4 33 5 18500-20500 6 27500-30000 7 35000-40000 8 24000-27000 9 19000-24000 10 30000-35000 11 44000-66000 12 75-90 Name: 0, dtype: object  Explain: It assumes that you are only interested in the parts upto /; it extracts everything until /, than removes anything but digits and hypen
Question: My app runs fine at heroku local but after deployed to Heroku, every time I try to login/register/login as admin, it returns this error shown below. I have tried to put @csrf_exempt on profile views, but that didn't fix the issue. What can I do? 

Answer: The error message is fairly self-explanatory (please excuse typos as I can't copy from an image): Origin checking failed - https://pacific-coast-78888.herokuapp.com does not match any trusted origins  The domain you are using is not a trusted origin for CSRF. There is then a link to the documentation, which I suspect goes to the Django CSRF documentation, though the documentation for the CSRF_TRUSTED_ORIGINS setting might be more useful:  A list of trusted origins for unsafe requests (e.g. POST). For requests that include the Origin header, Django’s CSRF protection requires that header match the origin present in the Host header.  Look in your settings.py for CSRF_TRUSTED_ORIGINS and add https://pacific-coast-78888.herokuapp.com to the list. If that setting doesn't already exist, simply add it: CSRF_TRUSTED_ORIGINS = ["https://pacific-coast-78888.herokuapp.com"]
Question: Is it possible to create a Pydantic field that does not have a default value and this value must be set on object instance creation and is immutable from then on? e.g. from pydantic import BaseModel class User(BaseModel): user_id: int name: str user = User(user_id=1, name='John') user.user_id = 2 # raises immutable error 

Answer: Pydantic already has the option you want. You can customize specific field by specifying allow_mutation to false. This raises a TypeError if the field is assigned on an instance. from pydantic import BaseModel, Field class User(BaseModel): user_id: int = Field(..., allow_mutation=False) name: str class Config: validate_assignment = True user = User(user_id=1, name='John') user.user_id = 2 # TypeError: "user_id" has allow_mutation set to False and cannot be assigned
Question: If a user has the privacy setting "Allow direct messages from server members" turned off and a discord bot calls await user.dm_channel.send("Hello there")  You'll get this error: discord.errors.Forbidden: 403 Forbidden (error code: 50007): Cannot send messages to this user  I would like to check whether I can message a user without sending them a message. Trying to send a message and catching this error does not work for me, because I don't want a message to get sent in the event that the bot is allowed to message. I have tried this: print(user.dm_channel.permissions_for(bot).send_messages)  but it always returns True, even if the message is not permitted. I have also tried this: channel = await user.create_dm() if channel is None: ...  but unfortunately, it seems that "has permission to message user" and "has permission to create a dm channel" are considered different. EDIT To clarify the exact usage since there seems to be a bit of confusion, take this example. There is a server, and 3 users in question: Me, My Bot, and Steve. Steve has "Allow direct messages from server members" checked off. The bot has a command called !newgame which accepts a list of users and starts a game amongst them, which involves DMing some of the members of the game. Because of Steve's privacy settings, he cannot play the game (since the bot will need to message him). If I do !newgame @DJMcMayhem @Steve  I'd like to provide a response like: > I can't start a game with that list of users because @Steve has the wrong privacy settings.  But as far as I know right now, the only way to find out if Steve can play is by first attempting to message every user, which I'd like to avoid.

Answer: Explanation You can send an invalid message, which would raise a 400 Bad Request exception, to the dm_channel. This can be accomplished by setting content to None, for example. If it raises 400 Bad Request, you can DM them. If it raises 403 Forbidden, you can't. Code async def can_dm_user(user: discord.User) -> bool: try: await user.send() except discord.Forbidden: return False except discord.HTTPException: return True
Question: My understanding is that "brew install python" installs the latest version of python. Why isn't it pulling 3.10? 3.10 is marked as a stable release. I can install 3.10 with "brew install python@3.10 just fine and can update my PATH so that python and pip point to the right versions. But I am curious why "brew install python" its not installing 3.10. My other understanding is that 3.10 is directly compatible with the M1 chips so that is why I want 3.10. Please let me know if I am mistaken.

Answer: As Henry Schreiner have specified now Python 3.10 is the new default in Brew. Thx for pointing it --- Obsolete --- The "python3" formula is still 3.9 in the brew system check the doc here: https://formulae.brew.sh/formula/python@3.9#default The latest version of the formula for 3.9 also support apple silicon. If you want to use python3.10 you need to run as you described brew install python@3.10 The reason why 3.9 is still the official python3 formula is that generally user using the vanilla python3 are not looking for the latest revision but the more stable. in some months the transition will done.
Question: When running pip3.8 i get the following warning appearing in my terminal WARNING: The script pip3.8 is installed in '/usr/local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed pip-21.1.1 setuptools-56.0.0 WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv  How to solve this problem on centos 7?

Answer: This question has been answered on the serverfaults forum: Here is a link to the question. You need to add the following line to your ~/.bash_profile or ~/.bashrc file.  export PATH="/usr/local/bin:$PATH"  You will then need to profile, do this by either running the command: source ~/.bash_profile  Or by simply closing your terminal and opening a new session. You should continue to check your PATH to make sure it includes the path. echo $PATH
Question: As the title says, I am trying to generate a refresh token, and then I would like to use the refresh token to get short lived Access tokens. There is a problem though, in that I'm not smart enough to understand the docs on the dropbox site, and all the other information I've found hasn't worked for me (A, B, C) or is in a language I don't understand. I have tried out all three examples from the github page, as well as user code from other questions on this site. I haven't got anything to work. The most I got was  Error: 400 Client Error: Bad Request for url: api.dropboxapi.com/oauth2/token  and  dropbox.rest.RESTSocketError: Error connecting to "api.dropbox.com": [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)  :(

Answer: Here is how I did it. I'll try to keep it simple and precise Replace <APP_KEY> with your dropbox app key in the below Authorization URL https://www.dropbox.com/oauth2/authorize?client_id=<APP_KEY>&token_access_type=offline&response_type=code Complete the code flow on the Authorization URL. You will receive an AUTHORIZATION_CODE at the end. Go to Postman and create a new POST request with below configuration  Request URL- https://api.dropboxapi.com/oauth2/token Authorization -> Type = Basic Auth -> Username = <APP_KEY> , Password = <APP_SECRET> (Refer this answer for cURL -u option) Body -> Select "x-www-form-urlencoded"      Key Value     code <AUTHORIZATION_CODE>   grant_type authorization_code     After you send the request, you will receive JSON payload containing refresh_token. { "access_token": "sl.****************", "token_type": "bearer", "expires_in": 14400, "refresh_token": "*********************", "scope": <SCOPES>, "uid": "**********", "account_id": "***********************" }  In your python application, import dropbox dbx = dropbox.Dropbox( app_key = <APP_KEY>, app_secret = <APP_SECRET>, oauth2_refresh_token = <REFRESH_TOKEN> )  Hope this works for you too!
Question: Recently I have started to use hydra to manage the configs in my application. I use Structured Configs to create schema for .yaml config files. Structured Configs in Hyda uses dataclasses for type checking. However, I also want to use some kind of validators for some of the parameter I specify in my Structured Configs (something like this). Do you know if it is somehow possible to use Pydantic for this purpose? When I try to use Pydantic, OmegaConf complains about it: omegaconf.errors.ValidationError: Input class 'SomeClass' is not a structured config. did you forget to decorate it as a dataclass? 

Answer: For those of you wondering how this works exactly, here is an example of it: import hydra from hydra.core.config_store import ConfigStore from omegaconf import OmegaConf from pydantic.dataclasses import dataclass from pydantic import validator @dataclass class MyConfigSchema: some_var: float @validator("some_var") def validate_some_var(cls, some_var: float) -> float: if some_var < 0: raise ValueError(f"'some_var' can't be less than 0, got: {some_var}") return some_var cs = ConfigStore.instance() cs.store(name="config_schema", node=MyConfigSchema) @hydra.main(config_path="/path/to/configs", config_name="config") def my_app(config: MyConfigSchema) -> None: # The 'validator' methods will be called when you run the line below OmegaConf.to_object(config) if __name__ == "__main__": my_app()  And config.yaml : defaults: - config_schema some_var: -1 # this will raise a ValueError
Question: I'm trying to npm install in a Vue project, and even if I just ran vue create (name) it gives me this err: npm ERR! gyp verb check python checking for Python executable "c:\Python310\python.exe" in the PATH npm ERR! gyp verb `which` succeeded c:\Python310\python.exe c:\Python310\python.exe npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: Command failed: c:\Python310\python.exe -c import sys; print "%s.%s.%s" % sys.version_info[:3]; npm ERR! gyp ERR! stack File "<string>", line 1 npm ERR! gyp ERR! stack import sys; print "%s.%s.%s" % sys.version_info[:3]; npm ERR! gyp ERR! stack ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ npm ERR! gyp ERR! stack SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? npm ERR! gyp ERR! stack npm ERR! gyp ERR! stack at ChildProcess.exithandler (node:child_process:397:12) npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:390:28) npm ERR! gyp ERR! stack at maybeClose (node:internal/child_process:1064:16) npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5) npm ERR! gyp ERR! System Windows_NT 10.0.19044 npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Upwork\\contact_book\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" npm ERR! gyp ERR! cwd C:\Upwork\contact_book\node_modules\node-sass npm ERR! gyp ERR! node -v v16.13.1 npm ERR! gyp ERR! node-gyp -v v3.8.0 npm ERR! gyp ERR! not ok npm ERR! Build failed with error code: 1  I tried it in another PC but it is working fine, I think it is because I need to install something (since the PC is new)

Answer: As @MehdiMamas pointed out in the comments, downgrading Node to v14 should solve the problem nvm install 14 nvm use 14
Question: I am a beginner and have a problem after installing pycaw for the audio control using python, on putting the basic initialization code for pycaw, i get the following error:- Traceback (most recent call last): File "c:\Users\...\volumeControl.py", line 7, in <module> from comtypes import CLSCTX_ALL File "C:\...\env\lib\site-packages\comtypes\__init__.py", line 375 except COMError, err: ^^^^^^^^^^^^^ SyntaxError: multiple exception types must be parenthesized  Basic initialization:- from ctypes import cast, POINTER from comtypes import CLSCTX_ALL from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume devices = AudioUtilities.GetSpeakers() interface = devices.Activate( IAudioEndpointVolume._iid_, CLSCTX_ALL, None) volume = cast(interface, POINTER(IAudioEndpointVolume))  I tried searching for this all over the web but could not find a fix I also trying going into the module file inside the virtual env and parenthize by putting brackets around COMError, err But same error with other lines in code came, Also tried reinstalling pycaw and trying to install different versions of pycaw several times but nothing fixed How to fix this error?

Answer: After a time searching I found that comtypes uses a tool to be compatible with both python 2 and 3 and that is no longer works in new versions. I had to downgrade two packages and reinstall comtypes: pip install setuptools==57.0.0 --force-reinstall pip install wheel==0.36.2 --force-reinstall pip uninstall comtypes pip install --no-cache-dir comtypes