eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I want to test a python file with pytest which contains a if __name__ == '__main__': it also has arguments parsed in it. the code is something like this: if __name__ == '__main__': parser = argparse.ArgumentParser(description='Execute job.') parser.add_argument('--env', required=True, choices=['qa', 'staging', 'prod']) args = parser.parse_args() # some logic  The limitation here is I cannot add a main() method and wrap the logic in if __name__ == '__main__': inside it and run it from there! The code is a legacy code and cannot be changed! I want to test this with pytest, I wonder how can I run this python file with some arguments inside my test?

Answer: Command line arguments are an input/output mechanism like any other. The key is to isolate it in a "boundary layer", and have your main program not depend on them directly. In this case, rather than making your program access sys.argv directly (which is essentially a global variable), make it so your program is wrapped in an "entry point" function (e.g. "main") that takes args as an explicit parameter. If you don't pass any arguments to ArgumentParser.parse_args, defaults to accessing sys.argv for you. Instead, just pass along your args param. It might look something like: def main(args): # some logic pass if __name__ == '__main__': main(sys.arv)  Your unit tests can call this entry point function and pass in any args they want: def test_main_does_foo_when_bar(): result = main(["bar"]) assert "foo" == result
Question: I'm doing this on esp8266 with micro python and there is a way to clear OLED display in Arduino but I don't know how to clear display in micropython i used ssd1306 library to control my OLED and this is my error I've written a code that prints on OLED from a list loop, but OLED prints it on the text that was printed before it (print one on top of the other not clear and printing) 7 display = [ip, request_uri, country, region, city] for real_time in display: oled.text(real_time, 0, 20) oled.show() time.sleep(2) print(real_time) 

Answer: The fill() method is used to clean the OLED screen: oled.fill(0) oled.show()
Question: Im trying to use Poetry and the scripts option to run a script. Like so: pyproject.toml [tool.poetry.scripts] xyz = "src.cli:main"  Folder layout  . ├── poetry.lock ├── pyproject.toml ├── run-book.txt └── src ├── __init__.py └── cli.py  I then perform an install like so: ❯ poetry install Installing dependencies from lock file No dependencies to install or update  If I then try and run the command its not found (?) ❯ xyz zsh: command not found: xyz  Am i missing something here! Thanks,

Answer: Poetry is likely installing the script in your user local directory. On Ubuntu, for example, this is $HOME/.local/bin. If that directory isn't in your path, your shell will not find the script. A side note: It is generally a good idea to put a subdirectory with your package name in the src directory. It's generally better to not have an __init__.py in your src directory. Also consider renaming cli.py to __main__.py. This will allow your package to be run as a script using python -m package_name.
Question: I have a lambda function runs on Python 3.7 with architecture x86_64 before. Now I would like to migrate it to arm64 to use the Graviton processor and upgrade to Python 3.9 as well. While I success to create the Python 3.9 virtual environment layer with the dependencies that I need, which is aws-encryption-sdk, when I change the architecture of my lambda function to arm64 and runtime to Python 3.9, below error shows after I test my code: Unable to import module 'encryptor-lambda': /opt/python/cryptography/hazmat/bindings/_rust.abi3.so: cannot open shared object file: No such file or directory",  I went and check my virtual env layer and pretty sure the file /opt/python/cryptography/hazmat/bindings/_rust.abi3.so is existed there. Then I tried to keep my runtime at Python 3.9 and switched back the architecture to x86, it works! Only if I try to change it to arm64, it has that error above. I look up online and can't seems to have a solution or as of why is that. Is it not possible to migrate for the lambda functions that requires dependencies? Or am I missing anything?

Answer: Libraries like aws-encryption-sdk-python sometimes contain code/dependencies that are not pure Python and need to be compiled. When code needs to be "compiled" it is usually compiled for a target architecture (like ARM or x86) to run properly.  You can not run code compiled for one architecture on different architecture. So I suspect that is the reason for your error.  Looking at the error message I suspect it is the cryptography library causing this issue. The library uses Rust. If you check your error, you will see that the shared library for the Rust binding is the causing your error (_rust.abi3.so). According to the documentation of the library, the ARM architecture is supported. Therefore, I suspect that the way you are packaging your Lambda deployment package and it's dependencies is the issue. You are probably doing that on a computer with x86 architecture. Package manager like pip usually detect the OS and architecture they are run on and download dependencies for those OS's and architectures. So I guess you have two options:  Run your build/deployment on an ARM machine Somehow manage to "cross compile" with tools like crossenv  Both options are not really great. Unfortunately, this is one of those areas where Python Lambdas can become very cumbersome to develop/deploy. Every time a depdency uses a non-Python extension (like a C extension), packaging/deployment becomes a problem. Maybe someone else has a great tool to recommend.
Question: I have a faiss index and want to use some of the embeddings in my python script. Selection of Embeddings should be done by id. As faiss is written in C++, swig is used as an API. I guess the function I need is reconstruct : /** Reconstruct a stored vector (or an approximation if lossy coding) * * this function may not be defined for some indexes * @param key id of the vector to reconstruct * @param recons reconstucted vector (size d) */ virtual void reconstruct(idx_t key, float* recons) const;  Therefore, I call this method in python, for example: vector = index.reconstruct(0)  But this results in the following error:  vector = index.reconstruct(0) File "lib/python3.8/site-packages/faiss/init.py", line 406, in replacement_reconstruct self.reconstruct_c(key, swig_ptr(x)) File "lib/python3.8/site-packages/faiss/swigfaiss.py", line 1897, in reconstruct return _swigfaiss.IndexFlat_reconstruct(self, key, recons) TypeError: in method 'IndexFlat_reconstruct', argument 2 of type 'faiss::Index::idx_t' python-BaseException  Has someone an idea what is wrong with my approach?

Answer: This is the only way I found manually. import faiss import numpy as np a = np.random.uniform(size=30) a = a.reshape(-1,10).astype(np.float32) d = 10 index = faiss.index_factory(d,'Flat', faiss.METRIC_L2) index.add(a) xb = index.xb print(xb.at(0) == a[0][0])  Output: True  You can get any vector with a loop required_vector_id = 1 vector = np.array([xb.at(required_vector_id*index.d + i) for i in range(index.d)]) print(np.all(vector== a[1]))  Output: True
Question: Consider the following code: def foo(a: dict[str | tuple[str, str], str]) -> None: pass def bar(b: dict[str, str]) -> None: foo(b) def baz(b: dict[tuple[str, str], str]) -> None: foo(b) foo({"foo": "bar"}) foo({("foo", "bar"): "bar"})  When checked with mypy in strict mode it produces the following errors: file.py:6: error: Argument 1 to "foo" has incompatible type "Dict[str, str]"; expected "Dict[Union[str, Tuple[str, str]], str]" file.py:9: error: Argument 1 to "foo" has incompatible type "Dict[Tuple[str, str], str]"; expected "Dict[Union[str, Tuple[str, str]], str]"  Which doesn't seem to make sense to me. The parameter is defined to accept a dict with either a string or a tuple as keys and strings as values. However, both variants are not accepted when explicitly annotated as such. They do however work when passing a dict like this directly to the function. It seems to me that mypy expects a dict that has to be able to have both options of the union as keys. I fail to understand why? If the constraints for the key are to be either a string or a tuple of to strings, passing either should be fine. Right? Am I missing something here?

Answer: A dict[str | tuple[str, str], str] isn't just a dict with either str or tuple[str, str] keys. It's a dict you can add more str or tuple[str, str] keys to. You can't add str keys to a dict[tuple[str, str], str], and you can't add tuple[str, str] keys to a dict[str, str], so those types aren't compatible. If you pass a literal dict directly to foo (or to bar or baz), that literal has no static type. mypy infers a type for the dict based on the context. Many different types may be inferred for a literal based on its context. When you pass b to foo inside bar or baz, b already has a static type, and that type is incompatible with foo's signature.
Question: Say I have this array: array = np.array([[1,2,3],[4,5,6],[7,8,9]])  Returns: 123 456 789  How should I go about getting it to return something like this? 111222333 111222333 111222333 444555666 444555666 444555666 777888999 777888999 777888999 

Answer: You'd have to use np.repeat twice here. np.repeat(np.repeat(array, 3, axis=1), 3, axis=0) # [[1 1 1 2 2 2 3 3 3] # [1 1 1 2 2 2 3 3 3] # [1 1 1 2 2 2 3 3 3] # [4 4 4 5 5 5 6 6 6] # [4 4 4 5 5 5 6 6 6] # [4 4 4 5 5 5 6 6 6] # [7 7 7 8 8 8 9 9 9] # [7 7 7 8 8 8 9 9 9] # [7 7 7 8 8 8 9 9 9]]
Question: Why do i keep getting this error? I tried different versions of anaconda 3 but did not manage to get it done. What should i install to work it properly? I used sklearn versions from 0.20 - 0.23. Error message:  Code: import pandas as pd import matplotlib.pyplot as plt import plotly.express as px from sklearn.feature_extraction.text import CountVectorizer from collections import Counter from wordcloud import WordCloud vectorizer = CountVectorizer(ngram_range=(2,2), analyzer='word') sparse_matrix = vectorizer.fit_transform(df['content'][:2000]) frequencies = sum(sparse_matrix).toarray()[0] ngrams = pd.DataFrame(frequencies, index=vectorizer.get_feature_names_out(), columns=['frequency']) ngrams = ngrams.sort_values(by='frequency', ascending=False) ngrams 

Answer: You are using an old version of scikit-learn. If I'm not mistaken, get_feature_names_out() was only introduced in version 1.0. Upgrade to a newer version, or, to get similar functionality in an earlier version, you can use get_feature_names().
Question: I have an iterable of bytes, such as bytes_iter = ( b'col_1,', b'c', b'ol_2\n1', b',"val', b'ue"\n', )  (but typically this would not be hard coded or available all at once, but supplied from a generator say) and I want to convert this to an iterable of str lines, where line breaks are unknown up front, but could be any of \r, \n or \r\n. So in this case would be: lines_iter = ( 'col_1,col_2', '1,"value"', )  (but again, just as an iterable, not so it's all in memory at once). How can I do this? Context: my aim is to then pass the iterable of str lines to csv.reader (that I think needs whole lines?), but I'm interested in this answer just in general.

Answer: Use the io module to do most of the work for you: class ReadableIterator(io.IOBase): def __init__(self, it): self.it = iter(it) def read(self, n): # ignore argument, nobody actually cares # note that it is *critical* that we suppress the `StopIteration` here return next(self.it, b'') def readable(self): return True  then just call io.TextIOWrapper(ReadableIterator(some_iterable_of_bytes)).
Question: I have a dataframe with 2 categorical columns (col1, col2).  col1 col2 0 A DE 1 A B 2 B BA 3 A A 4 C C  I want to map the unique string values to integers, for example (A:0, B:1, BA:2, C:3, DE:4)  col1 col2 ideal1 ideal2 0 A DE 0 4 1 A B 0 1 2 B BA 1 2 3 A A 0 0 4 C C 3 3  I am have tried to use factorize or category, but I am not getting the same unique value for both columns, as can be seen from ROW C: Here is my code: df = pd.DataFrame({'col1': ["A", "A", "B", "A" , "C"], 'col2': ["DE", "B", "BA", "A", "C"]}) #ideal map alphabetical: A:0, B:1, BA:2, C:3, DE:4 #ideal result df["ideal1"] = [0, 0, 1,0, 3] df["ideal2"] = [4,1,2,0,3] #trial #1 --> C value 2 & 3 : not matching df["cat1"] = df['col1'].astype("category").cat.codes df["cat2"] = df['col2'].astype("category").cat.codes #trial #2 --> C value 2 & 4 : not matching df["fac1"] = pd.factorize(df["col1"])[0] df["fac2"] = pd.factorize(df["col2"])[0] print (df) OUT: col1 col2 ideal1 ideal2 cat1 cat2 fac1 fac2 0 A DE 0 4 0 4 0 0 1 A B 0 1 0 1 0 1 2 B BA 1 2 1 2 1 2 3 A A 0 0 0 0 0 3 4 C C 3 3 2 3 2 4 

Answer: To get the same categories across columns you need to reshape to a single dimension first. Then use factorize and restore the original shape. Here is an example using stack/unstack: x = df.stack() x[:] = x.factorize()[0] df2 = x.unstack()  Output:  col1 col2 0 0 1 1 0 2 2 2 3 3 0 0 4 4 4  Joining to the original data: x = df.stack() x[:] = x.factorize()[0] df2 = df.join(x.unstack().add_suffix('_cat'))  Output:  col1 col2 col1_cat col2_cat 0 A DE 0 1 1 A B 0 2 2 B BA 2 3 3 A A 0 0 4 C C 4 4  alphabetical order If you really want alphabetical order, you could create you own custom mapping dictionary: import numpy as np cats = {k:v for v,k in enumerate(np.unique(df.values))} df.replace(cats)  Output:  col1 col2 0 0 4 1 0 1 2 1 2 3 0 0 4 3 3
Question: Hi I've made a simple Binary Tree and added a pre-order traversal method. After throwing around some ideas I got stuck on finding a way to return each value from the traverse_pre() method in an array. class BST: def __init__(self, val): self.value = val self.left = None self.right = None def add_child(self, val): if self.value: if val < self.value: if self.left == None: self.left = BST(val) else: self.left.add_child(val) else: if val > self.value: if self.right == None: self.right = BST(val) else: self.right.add_child(val) else: self.value = val def traverse_pre(self): if self.left: self.left.traverse_pre() print(self.value) if self.right: self.right.traverse_pre() Tree = BST(5) Tree.add_child(10) Tree.add_child(8) Tree.add_child(2) Tree.add_child(4) Tree.add_child(7) Tree.traverse_pre()  How would I modify the traverse_pre() function to return an array consisting of the node values. Is there a good example of this process for me to understand this further, I'm a bit stuck on how values can be appended to an array within recursion.

Answer: I would not recommend copying the entire tree to an intermediate list using .append or .extend. Instead use yield which makes your tree iterable and capable of working directly with many built-in Python functions - class BST: # ... def preorder(self): # value yield self.value # left if self.left: yield from self.left.preorder() # right if self.right: yield from self.right.preorder()  We can simply reorder the lines this to offer different traversals like inorder - class BST: # ... def inorder(self): # left if self.left: yield from self.left.inorder() # value yield self.value # right if self.right: yield from self.right.inorder()  And postorder - class BST: # ... def postorder(self): # left if self.left: yield from self.left.postorder() # right if self.right: yield from self.right.postorder() # value yield self.value  Usage of generators provides inversion of control. Rather than the traversal function deciding what happens to each node, the the caller is left with the decision on what to do. If a list is indeed the desired target, simply use list - list(mytree.preorder())  # => [ ... ]  That said, there's room for improvement with the rest of your code. There's no need to mutate nodes and tangle self context and recursive methods within your BST class directly. A functional approach with a thin class wrapper will make it easier for you to grow the functionality of your tree. For more information on this technique, see this related Q&A. If you need to facilitate trees of significant size, a different traversal technique may be required. Just ask in the comments and someone can help you find what you are looking for.
Question: So, I'm swapping my project from node.js to python FastAPI. Everything has been working fine with node, but here it says that my static files are not present, so here's the code: from fastapi import FastAPI, Request, WebSocket from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates app = FastAPI() app.mount("/static", StaticFiles(directory="../static"), name="static") templates = Jinja2Templates(directory='../templates') @app.get('/') async def index_loader(request: Request): return templates.TemplateResponse('index.html', {"request": request})  The project's structure looks like this:   Files are clearly where they should be, but when I connect to the website, the following error occurs: ←[32mINFO←[0m: connection closed ←[32mINFO←[0m: 127.0.0.1:54295 - "←[1mGET /img/separator.png HTTP/1.1←[0m" ←[31m404 Not Found←[0m ←[32mINFO←[0m: 127.0.0.1:54296 - "←[1mGET /css/rajdhani.css HTTP/1.1←[0m" ←[31m404 Not Found←[0m ←[32mINFO←[0m: 127.0.0.1:54295 - "←[1mGET /js/pixi.min.js HTTP/1.1←[0m" ←[31m404 Not Found←[0m ←[32mINFO←[0m: 127.0.0.1:54296 - "←[1mGET /js/ease.js HTTP/1.1←[0m" ←[31m404 Not Found←[0m ←[32mINFO←[0m: 127.0.0.1:54298 - "←[1mGET / HTTP/1.1←[0m" ←[32m200 OK←[0m ←[32mINFO←[0m: 127.0.0.1:54298 - "←[1mGET /img/separator.png HTTP/1.1←[0m" ←[31m404 Not Found←[0m ←[32mINFO←[0m: 127.0.0.1:54299 - "←[1mGET /css/rajdhani.css HTTP/1.1←[0m" ←[31m404 Not Found←[0m ←[32mINFO←[0m: 127.0.0.1:54298 - "←[1mGET /js/pixi.min.js HTTP/1.1←[0m" ←[31m404 Not Found←[0m ←[32mINFO←[0m: 127.0.0.1:54299 - "←[1mGET /js/ease.js HTTP/1.1←[0m" ←[31m404 Not Found←[0m  So, basically, any static file that I'm using is missing, and I have no idea what I am doing wrong. How to fix it?

Answer: Here: app.mount("/static", StaticFiles(directory="../static"), name="static")  You mount your static directory under /static path. That means, if you want access static files in your html you need to use static prefix, e.g. <img src="static/img/separator.png"/>
Question: I have a Panda dataframe that contains two columns, as well as a default index. The first columns is the intended 'Column Name' and the second column the required value for that column.  name returnattribute 0 Customer Name Customer One Name 1 Customer Code CGLOSPA 2 Customer Name Customer Two Name 3 Customer Code COTHABA 4 Customer Name Customer Three Name 5 Customer Code CGLOADS 6 Customer Name Customer Four Name 7 Customer Code CAPRCANBRA 8 Customer Name Customer Five Name 9 Customer Code COTHAMO  I would like to povit this so that instead of 10 rows, I have 5 rows with two columns ('Customer Name' and 'Customer Code'). The hoped for result is as below:  Customer Code Customer Name 0 CGLOSPA Customer One Name 1 COTHABA Customer Two Name 2 CGLOADS Customer Three Name 3 CAPRCANBRA Customer Four Name 4 COTHAMO Customer Five Name  I have tried to use the pandas pivot function: df.pivot(columns='name', values='returnattribute')  But this results in ten rows still with alternate blanks:  Customer Code Customer Name 0 NaN Customer One Name 1 CGLOSPA NaN 2 NaN Customer Two Name 3 COTHABA NaN 4 NaN Customer Three Name 5 CGLOADS NaN 6 NaN Customer Four Name 7 CAPRCANBRA NaN 8 NaN Customer Five Name 9 COTHAMO NaN  How to I pivot the dataframe to get just 5 rows of two columns?

Answer: You can also pass directly the new index to pivot_table, use aggfunc='first' as you have non numeric data: df.pivot_table(index=df.index//2, columns='name', values='returnattribute', aggfunc='first')  output: name Customer Code Customer Name 0 CGLOSPA Customer One Name 1 COTHABA Customer Two Name 2 CGLOADS Customer Three Name 3 CAPRCANBRA Customer Four Name 4 COTHAMO Customer Five Name
Question: I'm trying to replicate this snippet from geeksforgeeks, except that it uses the oauth for Twitter API v1.1 while I the API v2. # the screen name of the user screen_name = "PracticeGfG" # fetching the user user = api.get_user(screen_name) # fetching the ID ID = user.id_str print("The ID of the user is : " + ID) OUTPUT: The ID of the user is: 4802800777.  And here's mine: import os import tweepy API_KEY = os.getenv('API_KEY') API_KEY_SECRET = os.getenv('API_KEY_SECRET') BEARER_TOKEN = os.getenv('BEARER_TOKEN') ACCESS_TOKEN = os.getenv('ACCESS_TOKEN') ACCESS_TOKEN_SECRET = os.getenv('ACCESS_TOKEN_SECRET') screen_name = 'nytimes' client = tweepy.Client(consumer_key=API_KEY, consumer_secret=API_KEY_SECRET, access_token=ACCESS_TOKEN, access_token_secret=ACCESS_TOKEN_SECRET) user = client.get_user(screen_name) id = user.id_str print(id)  When I run mine, it returns this error: TypeError: get_user() takes 1 positional argument but 2 were given. Can you give me hints in what did I miss? Thanks ahead.

Answer: get_user have the following signature. Client.get_user(*, id, username, user_auth=False, expansions, tweet_fields, user_fields)  Notice the *. * is used to force the caller to use named arguments. For example, This won't work. >>> def add(first, *, second): ... print(first, second) ... >>> add(1, 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: add() takes 1 positional argument but 2 were given  But this will >>> add(1, second=2) 1 2  So to answer your question you should call the get_user method like this. client.get_user(username=screen_name)
Question: I am trying to use session to pass variables across view functions in fastapi. However, I do not find any doc which specifically says of about session object. Everywhere I see, cookies are used. Is there any way to convert the below flask code in fastapi? I want to keep session implementation as simple as possible. from flask import Flask, session, render_template, request, redirect, url_for app=Flask(__name__) app.secret_key='asdsdfsdfs13sdf_df%&' @app.route('/a') def a(): session['my_var'] = '1234' return redirect(url_for('b')) @app.route('/b') def b(): my_var = session.get('my_var', None) return my_var if __name__=='__main__': app.run(host='0.0.0.0', port=5000, debug = True) 

Answer: Take a look at Starlette's SessionMiddleware. FastAPI uses Starlette under the hood so it is compatible. After you register SessionMiddleware, you can access Request.session, which is a dictionary. Documentation: SessionMiddleware An implementation in FastAPI may look like: @app.route("/a") async def a(request: Request) -> RedirectResponse: request.session["my_var"] = "1234" return RedirectResponse("/b") @app.route("/b") async def b(request: Request) -> PlainTextResponse: my_var = request.session.get("my_var", None) return PlainTextResponse(my_var)
Question: I want find all images and trying to use pathlib, but my reg expression don't work. where I went wrong? from pathlib import Path FILE_PATHS=list(Path('./photos/test').rglob('*.(jpe?g|png)')) print(len(FILE_PATHS)) FILE_PATHS=list(Path('./photos/test').rglob('*.jpg'))#11104 print(len(FILE_PATHS)) 0 11104 

Answer: Get list of files using Regex import re p = Path('C:/Users/user/Pictures') files = [] for x in p.iterdir(): a = re.search('.*(jpe?g|png)',str(x)) if a is not None: files.append(a.group())