eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to install a toolkit, I'm on WSL using ubuntu - I downloaded ubuntu yesterday. Here is what the installation process looks like for this toolkit. On windows cmd it says I have python 3.7.9 but on ubuntu its saying I have python 3.8.2 git clone https://github.com... cd program pip install -e .  or: pip install program  pip install -e . is not working for me, I get this error: user@DESKTOP-REA10BN:~/gym$ pip install -e . Command 'pip' not found, but there are 18 similar ones.  however, I checked and I have pip installed, here's what I checked for before running: user@DESKTOP-REA10BN:~$ cd\ > sudo apt-get install python-pip cdsudo: command not found user@DESKTOP-REA10BN:~$ python3 --version Python 3.8.2 user@DESKTOP-REA10BN:~$ python3-pip --version python3-pip: command not found user@DESKTOP-REA10BN:~$ which pip3 /usr/bin/pip3 user@DESKTOP-REA10BN:~$ pip3 -V pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)  my PATHS: /home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files/WindowsApps/CanonicalGroupLimited.UbuntuonWindows_2004.2020.812.0_x64__79rhkp1fndgsc:/mnt/c/windows/system32:/mnt/c/windows:/mnt/c/windows/System32/Wbem:/mnt/c/windows/System32/WindowsPowerShell/v1.0/:/mnt/c/windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Users/user/AppData/Local/Programs/Python/Python37-32/Scripts/:/mnt/c/Users/user/AppData/Local/Programs/Python/Python37-32/:/mnt/c/Users/user/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/user/AppData/Local/Programs/Microsoft VS Code/bin:/snap/bin 

Answer: Short answer: Try running python3 -m pip install -e .  Some explanations: The different versions of Python are not surprising. WSL is, effectively, an ultra-lightweight virtual machine. Your Windows python installation is entirely independent of the WSL python installation. Python has two widely used major versions, Python 2 and Python 3. The command python runs some minor version of Python 2, while the command python3 runs some minor version of Python 3. Below is my console output. lawruble@Balrog:~/scratch$ python --version Python 2.7.18 lawruble@Balrog:~/scratch$ python3 --version Python 3.8.5  Pip is the python installation manager, and has the same major versions as Python. The command pip runs the Python 2 version of pip, while pip3 runs the Python 3 version of pip. It's better practice to use python3 -m pip over pip3, it helps ensure that you're using the version of pip associated with the version of python you expect to run.
Question: Below is my fastAPI code from typing import Optional, Set from fastapi import FastAPI from pydantic import BaseModel, HttpUrl, Field from enum import Enum app = FastAPI() class Status(Enum): RECEIVED = 'RECEIVED' CREATED = 'CREATED' CREATE_ERROR = 'CREATE_ERROR' class Item(BaseModel): name: str description: Optional[str] = None price: float tax: Optional[float] = None tags: Set[str] = [] status: Status = None @app.put("/items/{item_id}") async def update_item(item_id: int, item: Item): results = {"item_id": item_id, "item": item} return results  Below is the swagger doc generated. The Status is not shown. I am new to pydantic and i am not sure on how to show status in the docs 

Answer: create the Status class by inheriting from both str and Enum class Status(str, Enum): RECEIVED = 'RECEIVED' CREATED = 'CREATED' CREATE_ERROR = 'CREATE_ERROR' References  Working with Python enumerations--(FastAPI doc) [BUG] docs don't show nested enum attribute for body--(Issue #329)
Question: I've been trying to simulate some Monte Carlos simulations lately and came across numpy.random. Checking the documentation of the exponential generator I've noticed that that's a warning in the page, which tells that  Generator.exponential should be used for new code.  Althought that, numpy.random.exponential still works, but I couldn't run the Generator counterpart. I've been getting the following error: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-14-c4cc7e61aa98> in <module> ----> 1 np.random.Generator.exponential(2, 1000) TypeError: descriptor 'exponential' for 'numpy.random._generator.Generator' objects doesn't apply to a 'int' object  My questions are:  What's the difference between these 2?  How to generate a sample with Generator?  

Answer: The Generator referred to in the documentation is a class, introduced in NumPy 1.17: it's the core class responsible for adapting values from an underlying bit generator to generate samples from various distributions. numpy.random.exponential is part of the (now) legacy Mersenne-Twister-based random framework. You probably shouldn't worry about the legacy functions being removed any time soon - doing so would break a huge amount of code, but the NumPy developers recommend that for new code, you should use the new system, not the legacy system. Your best source for the rationale for the change to the system is probably NEP 19: https://numpy.org/neps/nep-0019-rng-policy.html To use Generator.exponential as recommended by the documentation, you first need to create an instance of the Generator class. The easiest way to create such an instance is to use the numpy.random.default_rng() function. So you want to start with something like: >>> import numpy >>> my_generator = numpy.random.default_rng()  At this point, my_generator is an instance of numpy.random.Generator: >>> type(my_generator) <class 'numpy.random._generator.Generator'>  and you can use my_generator.exponential to get variates from an exponential distribution. Here we take 10 samples from an exponential distribution with scale parameter 3.2 (or equivalently, rate 0.3125): >>> my_generator.exponential(3.2, size=10) array([6.26251663, 1.59879107, 1.69010179, 4.17572623, 5.94945358, 1.19466134, 3.93386506, 3.10576934, 1.26095418, 1.18096234])  Your Generator instance can of course also be used to get any other random variates you need: >>> my_generator.integers(0, 100, size=3) array([56, 57, 10])
Question: Given this piece of code: from tensorflow.keras.preprocessing.text import Tokenizer sentences = [ 'i love my dog', 'I, love my cat', 'You love my dog!' ] tokenizer = Tokenizer(num_words = 1) tokenizer.fit_on_texts(sentences) word_index = tokenizer.word_index print(word_index)  whether num_words=1 or num_words=100, I get the same output when I run this cell on my jupyter notebook, and I can't seem to understand what difference it makes in tokenization.  {'love': 1, 'my': 2, 'i': 3, 'dog': 4, 'cat': 5, 'you': 6} 

Answer: word_index it's simply a mapping of words to ids for the entire text corpus passed whatever the num_words is the difference is evident in the usage. for example, if we call texts_to_sequences sentences = [ 'i love my dog', 'I, love my cat', 'You love my dog!' ] tokenizer = Tokenizer(num_words = 1+1) tokenizer.fit_on_texts(sentences) tokenizer.texts_to_sequences(sentences) # [[1], [1], [1]]  only the love id is returned because the most frequent word instead sentences = [ 'i love my dog', 'I, love my cat', 'You love my dog!' ] tokenizer = Tokenizer(num_words = 100+1) tokenizer.fit_on_texts(sentences) tokenizer.texts_to_sequences(sentences) # [[3, 1, 2, 4], [3, 1, 2, 5], [6, 1, 2, 4]]  the ids of the most 100 frequent words is returned
Question: I'm a Python developer, making my first steps in JavaScript. I started using Map and Set. They seem to have the same API as dict and set in Python, so I assumed they're a hashtable and I can count on O(1) lookup time. But then, out of curiosity, I tried to see what would happen if I were to do this in Chrome's console: new Set([new Set([1, 2, 3])])  What happens is this: Set(1) {Set(3)}  JavaScript happily creates the set. How can this be? In Python you would have gotten an error since you can't put a mutable item in a set or a dict. Why does JavaScript allow it?

Answer: Consider the following JS code: > m1 = new Map([['a', 1]]) Map { 'a' => 1 } > m2 = new Map() Map {} > m2.set(m1, 3) Map { Map { 'a' => 1 } => 3 } > m2.get(m1) 3  But note, it is hashing based on identity, i.e. ===, so... > m2.get(new Map([['a',1]])) undefined  So really, how useful is this map? Note, this isn't different than Python's default behavior. The default status of user-defined type is being hashable: >>> class Foo: pass ... >>> f0 = Foo() >>> s = {f0} >>> Foo() in s False >>> f0 in s True  In Python, by default, object.__eq__ will compare based on identity, so the above is fine. However, if you override __eq__, by default, __hash__ is set to None and trying to use a hashing-based container will fail: >>> class Bar: ... def __init__(self, value): ... self.value = value ... def __eq__(self, other): ... return self.value == other.value ... >>> b0 = Bar(0) >>> b1 = Bar(2) >>> {b0, b1} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'Bar'  At this point, you must implement __hash__ to be consistent with __eq__, and note, though, that your user-defined object is never really very "immutable"
Question: I have installed google-cloud-vision library following the documentation. It is for some reason unable to import types from google.cloud.vision. It worked fine on my pc, now when I shared with my client, he had a problem with imports though he has the library installed via pip. Here's the line that throws error: from google.cloud import vision from google.cloud.vision import types # this line throws error  Any idea how to resolve this issue?

Answer: It's probably because there's some version mismatch (or less likely there's other library(s) with the same name). Have your client use a virtual environment. This should resolve the issue. P.S. You'll have to provide him with a requirements.txt file (obtained from pip3 freeze) so that he can do a pip3 install -r requirements.txt on his virtual environment to have the exact same packages as yours.
Question: I have recently been following this tutorial to get myself started with Discord's API. Unfortunately, when I got the part about printing all the users in the guild I hit a wall. When I try to print all users' names it only prints the name of the bot and nothing else. For reference, there are six total users in the guild. The bot has Administrator privileges. import os import discord TOKEN = os.environ.get('TOKEN') client = discord.Client() @client.event async def on_ready(): for guild in client.guilds: print(guild, [member.name for member in guild.members]) client.run(TOKEN) 

Answer: As of discord.py v1.5.0, you are required to use Intents for your bot, you can read more about them by clicking here In other words, you need to do the following changes in your code - import discord from dotenv import load_dotenv load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') GUILD = os.getenv('DISCORD_GUILD') intents = discord.Intents.all() client = discord.Client(intents=intents) @client.event async def on_ready(): for guild in client.guilds: if guild.name == GUILD: break print( f'{client.user} is connected to the following guild: \n' f'{guild.name} (id: {guild.id})' ) # just trying to debug here for guild in client.guilds: for member in guild.members: print(member.name, ' ') members = '\n - '.join([member.name for member in guild.members]) print(f'Guild Members:\n - {members}') client.run(TOKEN)
Question: I've learned how to write a python server, and figured out that I have a hole in my knowledge. Therefore, I would glad to know more about the differences between the commands bind(), listen() of the module called socket. In addition, when I use bind() with a specific port as a parameter, Is the particular port being in use already, before using the listen() method?!

Answer: I found a tutorial which explains in detail:  ... bind() is used to associate the socket with the server address. Calling listen() puts the socket into server mode, and accept() waits for an incoming connection.  listen() is what differentiates a server socket from a client.  Once bind() is called, the port is now reserved and cannot be used again until either the program ends or the close() method is called on the socket. A test program that demonstrates this is as follows: import socket import time HOST = '127.0.0.1' PORT = 65432 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) while 1: time.sleep(1)  when running two instances of this program at once, you can see that the one started last has the error:  Which proves that the port is reserved before listen() is ever called.
Question: I am trying to import my own module but I am getting error: Unable to import module 'lambda_function': attempted relative import with no known parent package  lambda_function.py  Own modulename.py 

Answer: You import it as if you were to import any other Python module. In other words don't do this: from .name import * but do this: from name import show_name For example:  The contents of name.py: def my_name(): print("Your name goes here.")  Don't forget to Deploy your function after making changes.
Question: I want to replace the value of a dataframe cell using pandas. I'm using this line: submission.iloc[i, coli] = train2.iloc[i2, coli-1]  I get the following error line: IndexError: iloc cannot enlarge its target object  What is the reason for this?

Answer: I think this happens because either 'i' or 'coli' is out of bounds in submission. According to the documentation, you can Enlarge the dataframe with loc, meaning it would add the required row and column (in either axis) if you assign a value to a row/column that currently does not exist, but apparently iloc will not do the same.
Question: I have just installed pyenv following the installation guide pyenv-win, things goes smoothly, but i could not make the pyenv global python as the global interpreter I have rehashed after installation using pyenv rehash PS D:\> pyenv versions 3.5.1 3.6.2 3.7.7 * 3.8.2 (set by C:\Users\xxx\.pyenv\pyenv-win\version)  results > python --version > 3.8.4 # expected > 3.8.2  therefore, I am not able to use virtualenv with the pyenv installed python interpreter virtualenv py382-djangodev --python=3.8.2 The path 3.8.2 (from --python=3.8.2) does not exist 

Answer: In windows NT, the PATH variable is a combined result of the system and user variables:  The Path is constructed from the system path, which can be viewed in the System Environment Variables field in the System dialog box. The User path is appended to the system path  Shims PATH are defined in the user variables, so make sure your host python interpreter path is not defined in your system path
Question: Using the following input, [1, 2, 3, 4]  I'm trying to get the following output [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [2], [2, 3], [2, 3, 4], [3], [3, 4], [4]]  As far I have made such an algorithm, but time complexity is not good. def find(height): num1 = 0 out = [] for i in range(len(height)): num2 = 1 for j in range(len(height)): temp = [] for x in range(num1, num2): temp.append(height[x]) num2 += 1 if temp: out.append(temp) num1 += 1 return out  Is there any way to speed up that algorithm?

Answer: Contiguous sub-sequences The OP specified in comments that they were interested in contiguous sub-sequences. All that is needed to select a contiguous sub-sequence is to select a starting index i and an ending index j. Then we can simply return the slice l[i:j]. def contiguous_subsequences(l): return [l[i:j] for i in range(0, len(l)) for j in range(i+1, len(l)+1)] print(contiguous_subsequences([1,2,3,4])) # [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [2], [2, 3], [2, 3, 4], [3], [3, 4], [4]]  This function is already implemented in package more_itertools, where it is called substrings: import more_itertools print(list(more_itertools.substrings([0, 1, 2]))) # [(0,), (1,), (2,), (0, 1), (1, 2), (0, 1, 2)]  Non-contiguous sub-sequences For completeness. Finding the "powerset" of an iterable is an itertool recipe: import itertools def powerset(iterable): "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" s = list(iterable) return itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(len(s)+1))  It is also in package more_itertools: import more_itertools print(list(more_itertools.powerset([1,2,3,4]))) # [(), (1,), (2,), (3,), (4,), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (1, 2, 3, 4)]
Question: I want to replace the None in the list with the previous variables (for all the consecutive None). I did it with if and for (multiple lines). Is there any way to do this in a single line? i.e., List comprehension, Lambda and or map And my idea was using the list comprehension but I was not able to assign variables in a list comprehension to set a previous value. I have got a similar scenario in my project to handle None in such a way, the thing is I don't want to write 10 lines of code for the small functionality. def none_replace(ls): ret = [] prev_val = None for i in ls: if i: prev_val = i ret.append(i) else: ret.append(prev_val) return ret print('Replaced None List:', none_replace([None, None, 1, 2, None, None, 3, 4, None, 5, None, None]))  Output: Replaced None List: [None, None, 1, 2, 2, 2, 3, 4, 4, 5, 5, 5]

Answer: In Python 3.8 or higher you can do this using the assignment operator: def none_replace(ls): p = None return [p:=e if e is not None else p for e in ls]
Question: So I have some text data that's been messily parsed, and due to that I get names mixed in with the actual data. Is there any kind of package/library that helps identify whether a word is a name or not? (In this case, I would be assuming US/western/euro-centric names) Otherwise, what would be a good way to flag this? Maybe train a model on a corpus of names and assign each word in the dataset a classification? Just not sure the best way to approach this problem/what kind of model would be suited, or if a solution already exists

Answer: import nltk from nltk.tag.stanford import NERTagger st = NERTagger('stanford-ner/all.3class.distsim.crf.ser.gz', 'stanford-ner/stanford-ner.jar') text = """YOUR TEXT GOES HERE""" for sent in nltk.sent_tokenize(text): tokens = nltk.tokenize.word_tokenize(sent) tags = st.tag(tokens) for tag in tags: if tag[1]=='PERSON': print(tag)  via Improving the extraction of human names with nltk
Question: Python's any and all built-in functions are supposed to short-circuit, like the logical operators or and and do. However, suppose we have a function definition like so: def func(s): print(s) return True  and use it to build a list of values passed to any or all: >>> any([func('s'), func('t')]) 's' 't' True  Since the list must be constructed before any is called, the function is also evaluated ahead of time, effectively defeating the short-circuiting. If the function calls are expensive, evaluating all the functions up front is a big loss and is a waste of this ability of any. Knowing that any accepts any kind of iterable, how can we defer the evaluation of func, so that the short-circuiting of any prevents calling func(t)?

Answer: We can use a generator expression, passing the functions and their arguments separately and evaluating only in the generator like so: >>> any(func(arg) for arg in ('s', 't')) 's' True  For different functions with different signatures, this could look like the following: any( f(*args) for f, args in [(func1, ('s',)), (func2, (1, 't'))] )  That way, any will stop iterating over the generator as soon as one function call evaluates to True, and that means that the function evaluation is fully lazy. Another neat way to postpone the function evaluation is to use lambda expressions, like so: >>> any( ... f() ... for f in [lambda: func('s'), lambda: func('t')] ... ) 's' True
Question: How do I find the constants A,B,C,D,K,S such that 1/(x**6+1) = (A*x+B)/(x**2+1) + (C*x+D)/(x**2-sqrt(3)*x+1) + (K*x+S)/(x**2+sqrt(3)*x+1) is true for every real x. I need some sympy code maybe, not sure. Or any other Python lib which could help here. I tried by hand but it's not easy at all: after 1 hour of calculating, I found that I have probably made some mistake. I tried partial fraction decomposition in SymPy but it does not go that far. I tried Wolfram Alpha too, but it also does not decompose to that level of detail, it seems. WA attempt See the alternate forms which WA gives below. Edit I did a second try entirely by hand and I got these: A = 0 B = 1/3 C = -1/(2*sqrt(3)) D = 1/3 K = 1/(2*sqrt(3)) S = 1/3 How can I verify if these are correct? Edit 2 The main point of my question is: how to do this with some nice/reusable Python code?

Answer: You can do this using apart in sympy but apart will look for a rational factorisation by default so you have to tell it to work in Q(sqrt(3)): In [37]: apart(1/(x**6+1)) Out[37]: 2 x - 2 1 - ─────────────── + ────────── ⎛ 4 2 ⎞ ⎛ 2 ⎞ 3⋅⎝x - x + 1⎠ 3⋅⎝x + 1⎠ In [36]: apart(1/(x**6+1), extension=sqrt(3)) Out[36]: √3⋅x - 2 √3⋅x + 2 1 - ───────────────── + ───────────────── + ────────── ⎛ 2 ⎞ ⎛ 2 ⎞ ⎛ 2 ⎞ 6⋅⎝x - √3⋅x + 1⎠ 6⋅⎝x + √3⋅x + 1⎠ 3⋅⎝x + 1⎠  EDIT: The comments are asking for a way to find this in more general cases without needing to know that sqrt(3) generates the extension. We can use .apart(full=True) to compute the full PFE over linear factors: In [29]: e = apart(1/(x**6+1), full=True).doit() In [30]: e Out[30]: √3 ⅈ √3 ⅈ √3 ⅈ √3 ⅈ - ── - ─ - ── + ─ ── - ─ ── + ─ 2 2 2 2 2 2 2 2 ⅈ ⅈ - ────────────── - ────────────── - ────────────── - ────────────── + ───────── - ───────── ⎛ √3 ⅈ⎞ ⎛ √3 ⅈ⎞ ⎛ √3 ⅈ⎞ ⎛ √3 ⅈ⎞ 6⋅(x + ⅈ) 6⋅(x - ⅈ) 6⋅⎜x + ── + ─⎟ 6⋅⎜x + ── - ─⎟ 6⋅⎜x - ── + ─⎟ 6⋅⎜x - ── - ─⎟ ⎝ 2 2⎠ ⎝ 2 2⎠ ⎝ 2 2⎠ ⎝ 2 2⎠  This is not what is wanted because you want to have real quadratic denominators rather than introducing complex numbers. The terms here come in complex conjugate pairs though so we can combine the pairs: In [46]: terms1 = [] In [47]: for a in terms: ...: if conjugate(a) not in terms1: ...: terms1.append(a) ...: In [49]: terms_real = [(t+t.conjugate()) for t in terms1] In [51]: Add(*(factor(cancel(t)) for t in terms_real)) Out[51]: √3⋅x - 2 √3⋅x + 2 1 - ───────────────── + ───────────────── + ────────── ⎛ 2 ⎞ ⎛ 2 ⎞ ⎛ 2 ⎞ 6⋅⎝x - √3⋅x + 1⎠ 6⋅⎝x + √3⋅x + 1⎠ 3⋅⎝x + 1⎠  Note that in general there might not be any simple expressions for the roots (Abel-Ruffini) so this kind of expression for the partial fraction expansion will not succeed for all possible denominator polynomials. This is why .apart by default computes an expansion over irreducible denominators (something that can always succeed).
Question: How do I get a list of all the font names that are on my computer's system?

Answer: This is just a matter of listing the files in Windows\fonts: import os print(os.listdir(r'C:\Windows\fonts'))  The output is a list that starts with something that looks like this: ['arial.ttf', 'arialbd.ttf', 'arialbi.ttf', 'cambria.ttc', 'cambriab.ttf'
Question: I know about the existance of the %load markdown_file.md magic command but this will load the content of the file on the first run of the cell. If the file changes, the cell won't be updated. Does anyone know if it is possible to avoid this problem and load the content of the file each time the cell runs?

Answer: If you want to load a markdown every time a cell is run, you can do: from IPython.display import Markdown, display display(Markdown("markdown_file.md"))