eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am new to Langchain and followed this Retrival QA - Langchain. I have a custom prompt but when I try to pass Prompt with chain_type_kwargs its throws error in pydantic StufDocumentsChain. and on removing chain_type_kwargs itt just works. how can pass to the prompt? error File /usr/local/lib/python3.11/site-packages/pydantic/main.py:341, in pydantic.main.BaseModel.__init__() ValidationError: 1 validation error for StuffDocumentsChain __root__ document_variable_name context was not found in llm_chain input_variables: ['question'] (type=value_error)  Code import json, os from langchain.chains import RetrievalQA from langchain.llms import OpenAI from langchain.document_loaders import JSONLoader from langchain.text_splitter import CharacterTextSplitter from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import Chroma from langchain.chat_models import ChatOpenAI from langchain import PromptTemplate from pathlib import Path from pprint import pprint os.environ["OPENAI_API_KEY"] = "my-key" def metadata_func(record: dict, metadata: dict) -> dict: metadata["drug_name"] = record["drug_name"] return metadata loader = JSONLoader( file_path='./drugs_data_v2.json', jq_schema='.drugs[]', content_key="data", metadata_func=metadata_func) docs = loader.load() text_splitter = CharacterTextSplitter(chunk_size=5000, chunk_overlap=200) texts = text_splitter.split_documents(docs) embeddings = OpenAIEmbeddings() docsearch = Chroma.from_documents(texts, embeddings) template = """/ example custom prommpt Question: {question} Answer: """ PROMPT = PromptTemplate(template=template, input_variables=['question']) qa = RetrievalQA.from_chain_type( llm=ChatOpenAI( model_name='gpt-3.5-turbo-16k' ), chain_type="stuff", chain_type_kwargs={"prompt": PROMPT}, retriever=docsearch.as_retriever(), ) query = "What did the president say about Ketanji Brown Jackson" qa.run(query) 

Answer: {context} is missing in template.
Question: I'm encountering a problem when building a Docker image using a Python-based Dockerfile. I'm trying to use the mysqlclient library (version 2.2.0) and Django (version 4.2.2). Here is my Dockerfile: FROM python:3.11-alpine WORKDIR /usr/src/app COPY requirements.txt . RUN apk add --no-cache gcc musl-dev mariadb-connector-c-dev && \ pip install --no-cache-dir -r requirements.txt COPY . . CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]  The problem arises when the Docker build process reaches the point of installing the mysqlclient package. I get the following error: Exception: Can not find valid pkg-config name To address this issue, I tried adding pkgconfig to the apk add command, Unfortunately, this didn't help and the same error persists. I would appreciate any guidance on how to resolve this issue. Thank you in advance.

Answer: I've managed to solve the issue and here's how I did it: Here is the new Dockerfile: FROM python:3.11-alpine WORKDIR /usr/src/app COPY requirements.txt . RUN apk add --no-cache --virtual build-deps gcc musl-dev libffi-dev2 pkgconf mariadb-dev && \ apk add --no-cache mariadb-connector-c-dev && \ pip install --no-cache-dir -r requirements.txt && \ apk del build-deps COPY . . CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]  requirements.txt: mysqlclient==2.2.0 Django~=4.2.0  I hope this will help someone who visits this post in the future.
Question: I need to automate a test that uses Windows Authentication. I know that the the prompt that opens up is not part of the HTML page, yet why my code is not working: login_page.click_iwa() sleep(5) self.page.keyboard.type('UserName') sleep(5) self.page.keyboard.press('Tab') self.page.keyboard.type('Password') 

Answer: I solved the issue by using a virtual keyboard: from pyautogui import press, typewrite, hotkey def press_key(key): press(key) def type_text(text): typewrite(text) def special_keys(special, normal): hotkey(special, normal)  Then, after I implemented the virtual keyboard, I did this: def login_iwa(self, user_name='User321', password='123456', need_to_fail=False): pyautogui.FAILSAFE = False self.click_iwa() sleep(7) type_text(user_name) sleep(1) press_key('Tab') sleep(1) type_text(password) sleep(1) press_key('Enter') sleep(2) if need_to_fail: press_key('Tab') sleep(1) press_key('Tab') sleep(1) press_key('Tab') sleep(1) press_key('Enter') sleep(1)  I used pyautogui.FAILSAFE = False because sometime the popup was hiding behind the screen or was openning up on the 2nd screen.
Question: Please see the below snippet, run with Python 3.10: from collections.abc import Generator DUMP_DATA = 5, 6, 7 class DumpData(Exception): """Exception used to indicate to yield from DUMP_DATA.""" def sample_gen() -> Generator[int | None, int, None]: out_value: int | None = None while True: try: in_value = yield out_value except DumpData: yield len(DUMP_DATA) yield from DUMP_DATA out_value = None continue out_value = in_value  My question pertains to the DumpData path where there is a yield from. After that yield from, there needs to be a next(g) call, to bring the generator back to the main yield statement so we can send: def main() -> None: g = sample_gen() next(g) # Initialize assert g.send(1) == 1 assert g.send(2) == 2 # Okay let's dump the data num_data = g.throw(DumpData) data = tuple(next(g) for _ in range(num_data)) assert data == DUMP_DATA # How can one avoid this `next` call, before it works again? next(g) assert g.send(3) == 3  How can this extra next call be avoided?

Answer: When you yield from a tuple directly, the built-in tuple_iterator (which sample_gen delegates to) handles an additional "final value" yield before it terminates. It does not have a send method (unlike generators in general) and returns a final value None to sample_gen. The behavior: yield from DUMP_DATA # is equivalent to: yield from tuple_iterator(DUMP_DATA)  def tuple_iterator(t): for item in t: yield item return None  You can implement tuple_iterator_generator, with usage: try: in_value = yield out_value except DumpData: yield len(DUMP_DATA) in_value = yield from tuple_iterator_generator(DUMP_DATA) out_value = in_value  def tuple_iterator_generator(t): in_value = None for item in t: in_value = yield item return in_value  Or just not use yield from if you don't want that behavior: try: in_value = yield out_value except DumpData: yield len(DUMP_DATA) for out_value in DUMP_DATA: in_value = yield out_value out_value = in_value  See https://docs.python.org/3/whatsnew/3.3.html#pep-380-syntax-for-delegating-to-a-subgenerator for a use case of that behavior.
Question: I was toying around with the Singleton pattern and derivation. Specifically, I had this code: class Singleton: _instance = None init_attempts = 0 def __init__(self): self.init_attempts += 1 def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super().__new__(cls, *args, **kwargs) return cls._instance class Derived(Singleton): def __init__(self): super().__init__() self.attribute = "this is derived" def main(): instance_1 = Singleton() instance_2 = Singleton() print("instance_1 and instance_2 are ", end="") if id(instance_1) == id(instance_2): print("the ame") else: print("different") derived_instance = Derived() print("derived_instance and instance_2 are the same:", derived_instance is instance_2) try: print(derived_instance.attribute) except AttributeError: print("Initialization of Derived has been prevented") print("Number of initializations:", derived_instance.init_attempts) if __name__ == '__main__': main()  To my surprise, the __init__ of Derived is never called, i.e. derived_instance.attribute does not exist and derived_instance.init_attempts remains 2. As I understand it, SomeClass() is resolved to SomeClass.__call__(), which in turn should call the __new__ and __init__ methods. Yet, in the above example, Derived.__init__ is never called. In detail, my output is: instance_1 and instance_2 are the ame derived_instance and instance_2 are the same: True Initialization of Derived has been prevented Number of initializations: 2  Can anybody explain to me, why that is? In particular, the similar example here: How to initialize Singleton-derived object once works as expected, i.e. Tracer triggers printing init twice. I am aware that deriving from Singletons cannot be done in a meaningful way, and that both, a Singleton decorator and metaclass exist and how to use them, so I'm specifically interested in how the inheritance process goes awry. I presume it has something to do with the MRO, but I cannot think of anything that would not also compromise the Tracer example from the link. I tried finding similar links, but could not find any additional information. This here asks a very similar question: does __init__ get called multiple times with this implementation of Singleton? (Python) but doesn't answer it, or rather only confirms that a derived class' ´__init__´ should get called as well. Thanks in advance! Hoping to pay back the favour soon!

Answer: The Python documentation has the answer:  If __new__() is invoked during object construction and it returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to the object constructor. If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.  In the case at hand, when Derived() (which has no __new__ of its own) is called to create a class instance, Singleton.__new__ is invoked, and because that does not return an instance of Derived (but of Singleton), the __init__() method will not be invoked.
Question: Server side: from flask import Flask, request, Response, stream_with_context import time app = Flask(__name__) # Define a route /stream that handles POST requests @app.route('/stream', methods=['POST']) # GET also been tried, no difference def stream(): @stream_with_context def generate(): print('1') yield "Hello\n" time.sleep(1) # Simulate some delay print('2') yield "World\n" time.sleep(1) print('3') yield "This is\n" time.sleep(1) print('4') yield "Streaming data\n" time.sleep(1) print('5') return Response(generate(), content_type='text/event-stream') if __name__ == '__main__': app.run(debug=True)  Client side: import requests import sseclient reqUrl = 'https://api.my-domain-name.com/stream' headers={'Accept': 'text/event-stream'} # response = requests.get(url=reqUrl, headers=headers, stream=True) response = requests.post(url=reqUrl, headers=headers, stream=True) if response.status_code == 200: for chunk in response.iter_content(chunk_size=3): if chunk: print(chunk) else: print('fail:', response.status_code) # sse tried, no difference # client = sseclient.SSEClient(response) # for event in client.events(): # print(event.data)  When client connected, there was 1 2 3 4 5 printed one by one at the server side. But client printed nothing, until server finished, client printed all data. Help! Have tried the code above, and GET / POST tried, with or without sse. When I tried GET method, I also tried curl -v command, the same result, data comes together after about 5 seconds, not one by one. I expect stream data, that is, each yield data can be handled in time separately, not together.

Answer: Turned out to be the server setting problem, this post 10 years ago does help. nginx setting is important. Now server side is ok, tested by postman on my local machine, the strange is, the client side code does not go streaming, it prints all data at the same time. Have tried copy postman test header but not help. headers={ 'Accept': 'text/event-stream', # "Content-Type": "application/json", # in postman is this, tested no difference "Content-Type": "text/event-stream", "Transfer-Encoding": "chunked" #tried, no help # below are copied from postman "Cache-Control":"no-cache", "Connection":"keep-alive", 'Accept':'*/*', 'User-Agent':'PostmanRuntime/7.32.3' }  BTW: client code can call some third party streaming data api correctly. So I am confusing, where is the problem. Update: Finally solved the problem. sseclient and sseclient-py are different, I uninstall them and install only one, then ok. for me, I use sseclient, and modified some code, to make GET and POST all work well. sseclient should also work but I have not tried yet. (simply trying still have problem, need investigation)
Question: I have following Python code: check_files(original_file = original_file_name, used_file = used_file_name, unused_file = unused_file_name)  I want to make it instead to look like: check_files(original_file = original_file_name, used_file = used_file_name, unused_file = unused_file_name)  Also I want to correct formatting not only for function calls but also that way dictionary key/value pairs and etc. For Example, in RStudio, if I select the code and press CTRL + I RStudio will correct formating as I have described above. Is there any similar way to correct formating in VSCode?

Answer: Based on the comments by @starball, @jarmod and additional googling I found that you need to follow those steps:  Step 1. Install Python extension from marketplace: https://marketplace.visualstudio.com/items?itemName=ms-python.python  Step 2. Install one of the formatter packages for Python.  The Python extension supports source code formatting using either autopep8 (the default), black, or yapf.  from and More about it here: https://code.visualstudio.com/docs/python/editing#_formatting  Step 3. Select which code formatter you want to use in python.formatting.provider which is in settings>Extensions>Python (this maybe automatically set after step 1 and step 2). Also, in settings>Extensions>Python there are more options to select.  How to use formatting: The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations: On Windows Shift + Alt + F On macOS Shift + Option + F On Linux Ctrl + Shift + I Format Selection (Ctrl+K Ctrl+F) - Format the selected text. Or you can use right click menu:  from: https://mkyong.com/vscode/how-to-format-source-code-in-visual-studio-code-vscode/ and from: https://code.visualstudio.com/docs/editor/codebasics#_formatting
Question: I wanted to ask whether there is a method to expand brackets in Python non-commutatively. For example, INPUT (x+y)**2, OUTPUT x**2 + x*y + y*x + y**2, instead of the usual output x**2 + 2*x*y + y**2. SymPy gives this commutative output, but I have seen that a non-commutative output is possible in Mathematica (NonCommutativeMultiply). Could anyone suggest some Python code which will expand brackets non-commutatively? It would be a big help.

Answer: You have to create non-commutative symbols: from sympy import * x, y = symbols("x, y", commutative=False) expr = (x+y)**2 expr = expr.expand() print(expr) # out: x*y + x**2 + y*x + y**2
Question: I have a very simple Python script: import hydra from omegaconf import DictConfig, OmegaConf @hydra.main(version_base="1.3", config_path=".", config_name="config") def main(cfg: DictConfig) -> None: if cfg.benchmarking.seed_number is None: raise ValueError() if __name__ == "__main__": main()  And here the config file: benchmarking: seed_number: None  Unfortunately, the Python script does not raise an error. Instead, when I print the type of cfg.benchmarking.seed_number, it is str. How can I pass None instead?

Answer: Try null: benchmarking: seed_number: null
Question: I am trying to match the string in this error: for warning in args: if not isinstance(warning, str): with StaticvarExceptionHandler(): raise TypeError(f"Configure.suppress() only takes string arguments. Current type: {type(warning)}")  with the one in this pytest test: with pytest.raises( TypeError, match = "Configure.suppress() only takes string arguments. Current type: .*" ): Configure.suppress('ComplicatedTypeWarning', 1)   It should match, but I am getting this error: E AssertionError: Regex pattern did not match. E Regex: 'Configure.suppress() only takes string arguments. Current type: .*' E Input: "Configure.suppress() only takes string arguments. Current type: <class 'int'>"  Keep in mind that for all other tests that I use .*, everything works fine. I am very new to Regex, so I apologise if this is actually a really stupid question. Also, I've seen many many questions like these, but they were all in Java and not the same case as mine, so I couldn't find a solution. Feel free to flag this as a duplicate and link a matching question, though.

Answer: Parentheses have a special meaning in Regex, for marking capture groups and a few other things, see https://regex101.com (select "Python" on the left menu to get Python-specific regex explanations, since they vary from language to language). You need to escape them match = "Configure.suppress\\(\\) only takes string arguments. Current type: .*"  or (note the r before the ", for a "raw string") match = r"Configure.suppress\(\) only takes string arguments. Current type: .*"  Technically you don't need to escape those periods, since a period will match any non-newline character, and it's unlikely that there will be another error with a similar name that has a different character in place of those periods, but if you want to fix that use either of these: match = "Configure\\.suppress\\(\\) only takes string arguments\\. Current type: .*"  or (note the r before the ", for a "raw string") match = r"Configure\.suppress\(\) only takes string arguments\. Current type: .*"
Question: I need to specifically install Python 3.10.8 for school, but I can't seem to get a version higher than 3.10.6 sudo apt-get install python3.10 just result in telling me that python3.10 is already the newest version (3.10.6-1~22.04.2ubuntu1.1) Reading package lists... Done Building dependency tree... Done Reading state information... Done python3.10 is already the newest version (3.10.6-1~22.04.2ubuntu1.1). 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.  I already did sudo apt update && sudo apt upgrade and this is what I got: Failed to start apt-news.service: Unit apt-news.service not found. Failed to start esm-cache.service: Unit esm-cache.service not found. Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease Hit:2 http://security.ubuntu.com/ubuntu jammy-security InRelease Hit:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease \[108 kB\] Fetched 108 kB in 1s (110 kB/s) Reading package lists... Done Building dependency tree... Done Reading state information... Done All packages are up to date. Reading package lists... Done Building dependency tree... Done Reading state information... Done Calculating upgrade... Done 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. 

Answer: Step 1: sudo apt update Step 2: sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget Step 3: wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tgz Step 4: tar -xf Python-3.10.8.tgz Step 5: cd Python-3.10.8 Step 6: ./configure --enable-optimizations Step 7: make -j $(nproc) Step 9: sudo make altinstall Verify if python 3.10.8 was installed: python3.10 --version
Question: Been bashing my head on this since 2 days. I'm trying to match a packet content with regex API: packet_re = (r'.*RADIUS.*\s*Accounting(\s|-)Request.*(Framed(\s|-)IP(\s|-)Address.*Attribute.*Value: (?P<client_ip>\d+\.\d+\.\d+\.\d+))?.*(Username|User-Name)(\s|-)Attribute.*Value:\s*(?P<username>\S+).*') packet1 = """ IP (tos 0x0, ttl 64, id 35592, offset 0, flags [DF], proto UDP (17), length 213) 10.10.10.1.41860 > 10.10.10.3.1813: [udp sum ok] RADIUS, length: 185 Accounting-Request (4), id: 0x0a, Authenticator: 41b3b548c4b7f65fe810544995620308 Framed-IP-Address Attribute (8), length: 6, Value: 10.10.10.11 0x0000: 0a0a 0a0b User-Name Attribute (1), length: 14, Value: 005056969256 0x0000: 3030 3530 3536 3936 3932 3536 """ result = search(packet_re, packet1, DOTALL)  The regex matches, but it fails to capture Framed-IP-Address Attribute, client_ip=10.10.10.11. The thing is Framed-IP-Address Attribute can or cannot come in the packet. Hence the pattern is enclosed in another capture group ending with ? meaning 0 or 1 occurrence. I should be able to ignore it when it doesn't come. Hence packet content can also be: packet2 = """ IP (tos 0x0, ttl 64, id 60162, offset 0, flags [DF], proto UDP (17), length 163) 20.20.20.1.54035 > 20.20.20.2.1813: [udp sum ok] RADIUS, length: 135 Accounting-Request (4), id: 0x01, Authenticator: 219b694bcff639221fa29940e8d2a4b2 User-Name Attribute (1), length: 14, Value: 005056962f54 0x0000: 3030 3530 3536 3936 3266 3534 """  The regex should ignore Framed-IP-Address in this case. It does ignore but it doesn't capture when it does come.

Answer: I suggest using RADIUS.*?Accounting[\s-]Request(?:.*?(Framed[\s-]IP[\s-]Address.*?Attribute(?:.*?Value: (?P<client_ip>\d+\.\d+\.\d+\.\d+))?))?.*User-?[nN]ame[\s-]Attribute.*?Value:\s*(?P<username>\S+)  See the regex demo. Note I removed .* on both ends of the pattern as you are using re.search that does not require matching at the start of string like re.match, and the MatchData object contains .string property that you can access to obtain the whole input string. Details  RADIUS - a word .*? - any zero or more chars, as few as possible Accounting - a word [\s-] - a whitespace or hyphen Request - a word (?:.*? - start of an optional non-capturing group: any zero or more chars as few as possible, then...  (Framed[\s-]IP[\s-]Address.*?Attribute - Group 1: Framed + a whitespace or a hyphen + IP + whitespace/hyphen + Address + any zero or more chars as few as possible + Attribute  (?:.*?Value: (?P<client_ip>\d+\.\d+\.\d+\.\d+))? - an optional non-capturing group matching any zero or more chars as few as possible + Value:  + Group "client_ip": four one or more digit matching patterns separated with a literal dot   ) - end of the Group 1   )? - end of the outer non-capturing group .* - any zero or more chars as many as possible User-?[nN]ame - Username, UserName or User-name/User-Name [\s-] - whitespace or hyphen Attribute - a word .*? - any zero or more chars as few as possible Value: - a literal string \s* - zero or more whitespaces (?P<username>\S+) - Group "username": one or more non-whitespace chars
Question: When I try to debug a Python file in VS Code, the debugger opens in a new terminal instead of using the existing integrated terminal. I have tried the following: Making sure that I have saved my launch.json configuration file. Restarting VS Code. Trying debugging a different Python file. If I am using a virtual environment, making sure that the virtual environment is activated. launch.json { "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": true }  Environment:  VS code Version: 1.79.2 (user setup) OS: WSL Ubuntu   As you can see in the image it launches a new terminal rather than using the existing terminal.

Answer: From googling "github vscode python issues debug reuse terminal", I found this issue ticket: Run Python Debug Console in Existing Terminal #13040, where a maintainer of the VS Code Python extension, @int19h stated this:  This would be a great feature, but, unfortunately, we're constrained by VSCode itself here. The debugger can only request to run something in a terminal with a given title (and you can specify that title in launch.json by setting "consoleTitle", if you don't like the default "Python Debug Console"). However, whether and how to reuse an existing terminal is entirely down to VSCode. I was hoping that it'd reuse an existing terminal with matching title - but this doesn't seem to be the case. microsoft/vscode#68123 might be relevant here, although it's not quite the same, since it's about grouping newly spawned terminals.  Note for historical purposes, there was also an older bug where debugging with a Python-type launch config would cause a new terminal to be created every time instead of reusing an existing debug terminal, but that one seems to have been fixed (see also Debug mode spawns a new "Python Debug Console" every time with console set to integratedTerminal #132 and In VSCode, Python debugger launches a new Terminal every time I debug).