eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to restrict one field in a class to an enum. However, when I try to get a dictionary out of class, it doesn't get converted to string. Instead it retains the enum. I checked pydantic documentation, but couldn't find anything relevant to my problem. This code is representative of what I actually need. from enum import Enum from pydantic import BaseModel class S(str, Enum): am = 'am' pm = 'pm' class K(BaseModel): k: S z: str a = K(k='am', z='rrrr') print(a.dict()) # {'k': <S.am: 'am'>, 'z': 'rrrr'}  I'm trying to get the .dict() method to return {'k': 'am', 'z': 'rrrr'}

Answer: You need to use use_enum_values option of model config:  use_enum_values whether to populate models with the value property of enums, rather than the raw enum. This may be useful if you want to serialise model.dict() later (default: False)  from enum import Enum from pydantic import BaseModel class S(str, Enum): am='am' pm='pm' class K(BaseModel): k:S z:str class Config: use_enum_values = True # <-- a = K(k='am', z='rrrr') print(a.dict())
Question: I am trying to understand if the typing package is still needed? If in Python 3.8 I do: from typing import Any, Dict my_dict = Dict[str, Any]  Now in Python 3.9 via PEP 585 it's now preferred to use the built in types for collections hence: from typing import Any my_dict = dict[str, Any]  Do I still need to use the typing.Any or is there a built in type to replace it which I can not find?

Answer: The use of the Any remains the same. PEP 585 applies only to standard collections.  This PEP proposes to enable support for the generics syntax in all standard collections currently available in the typing module.  Starting with Python 3.9, the following collections become generic and importing those from typing is deprecated:  tuple (typing.Tuple) list (typing.List) dict (typing.Dict) set (typing.Set) frozenset (typing.FrozenSet) type (typing.Type) collections.deque collections.defaultdict collections.OrderedDict collections.Counter collections.ChainMap collections.abc.Awaitable collections.abc.Coroutine collections.abc.AsyncIterable collections.abc.AsyncIterator collections.abc.AsyncGenerator collections.abc.Iterable collections.abc.Iterator collections.abc.Generator collections.abc.Reversible collections.abc.Container collections.abc.Collection collections.abc.Callable collections.abc.Set (typing.AbstractSet) collections.abc.MutableSet collections.abc.Mapping collections.abc.MutableMapping collections.abc.Sequence collections.abc.MutableSequence collections.abc.ByteString collections.abc.MappingView collections.abc.KeysView collections.abc.ItemsView collections.abc.ValuesView contextlib.AbstractContextManager (typing.ContextManager) contextlib.AbstractAsyncContextManager (typing.AsyncContextManager) re.Pattern (typing.Pattern, typing.re.Pattern) re.Match (typing.Match, typing.re.Match)
Question: I did this plot and it worked. The day after I ran it and I got this error: TypeError: 'str' object is not callable. plt.plot(A2, A15, color="black", label="TRC - P1", marker="o") plt.xlabel("Amostras") plt.ylabel("TRC (%)") plt.title("TRC da P1") plt.yticks([0, 10, 20, 30, 40, 50, 60, 70]) plt.xticks(rotation=60)  Following the error I have got 

Answer: I would guess that you defined somewhere in your notebook something like plt.xlabel = "something". This could also happened before you run this code shown. Try to close the Notebook and restart your Kernel. After restarting run your code shown and everything should be fine. If using jupyter notebook then click on Run tab and select restart kernel and run all cells
Question: I'm trying to find out what fonts are available to be used in PIL with the font = ImageFont.load() and/or ImageFont.truetype() function. I want to create a list from which I can sample a random font to be used. I haven't found anything in the documentation so far, unfortunately.

Answer: I have so far not found a solution with PIL but matplotlib has a function to get all the available fonts from the system: system_fonts = matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf') The font can then be loaded using fnt = ImageFont.truetype(font, 60)
Question: I am trying to start my uwsgi server in my virtual environment, but after I added plugin python3 option I get this error every time: !!! Python Home is not a directory: /home/env3/educ !!! Set PythonHome to /home/env3/educ Python path configuration: PYTHONHOME = '/home/env3/educ' PYTHONPATH = (not set) program name = '/home/env3/educ/bin/python' isolated = 0 environment = 1 user site = 1 import site = 1 sys._base_executable = '/home/env3/educ/bin/python' sys.base_prefix = '/home/env3/educ' sys.base_exec_prefix = '/home/env3/educ' sys.executable = '/home/env3/educ/bin/python' sys.prefix = '/home/env3/educ' sys.exec_prefix = '/home/env3/educ' sys.path = [ '/home/env3/educ/lib/python38.zip', '/home/env3/educ/lib/python3.8', '/home/env3/educ/lib/python3.8/lib-dynload', ] Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding Python runtime state: core initialized ModuleNotFoundError: No module named 'encodings' Current thread 0x00007efe89db8780 (most recent call first): <no Python frame>  Also I tried to create new virtual environment using python3 -m venv env and moved project files to it, but still the same error. Here is my uwsgi.ini file: [uwsgi] base = /home/env3/educ projectname = educ plugins = python3 master = true virtualenv = /home/env3/%(projectname) pythonpath = %(base) env = DJANGO_SETTINGS_MODULE=%(projectname).settings.pro module = %(projectname).wsgi:application socket = /tmp/%(projectname).sock chmod-socket = 666  I use Python 3.8.5 I am trying to use Django + uWSGI + nginx + Postgresql.

Answer: I see your PYTHONHOME is set to PYTHONHOME = '/home/env3/educ'. Try to check if it is really there. The solution for me was to remove the PYTHONHOME environment variable. For you, it can be just that, or setting that variable to another value. This worked on Windows, and would work on Linux for sure. If someone tries this on Linux, please post a comment here ! A CPython developer confirmed the solution here. :  This is not a Python bug, this is a symptom of setting PYTHONHOME and/or PYTHONPATH when they’re not needed. In nearly all cases you don’t need to set either of them; In the case of PYTHONHOME it’s almost always a mistake to set.
Question: I saw a tweet from Raymond Hettinger yesterday. He used __set_name__. When I define __set_name__ method for my Class, the name becomes the instance's name. The owner became Foo, which is also expected but I couldn't figure out when and how this is useful. class Bar: def __set_name__(self, owner, name): print(f'{self} was named {name} by {owner}') class Foo: x = Bar() y = Bar()  That prints <__main__.Bar object at 0x7f48f2968820> was named x by <class '__main__.Foo'> <__main__.Bar object at 0x7f48f2968c70> was named y by <class '__main__.Foo'> 

Answer: As @juanpa provided a brief explanation, it is used to know the variable's name and class. One of its use cases is for logging. When you want to log the variable's name. This example was in descriptor's HowTo. import logging logging.basicConfig(level=logging.INFO) class LoggedAccess: def __set_name__(self, owner, name): self.public_name = name self.private_name = '_' + name def __get__(self, obj, objtype=None): value = getattr(obj, self.private_name) logging.info('Accessing %r giving %r', self.public_name, value) return value def __set__(self, obj, value): logging.info('Updating %r to %r', self.public_name, value) setattr(obj, self.private_name, value) class Person: name = LoggedAccess() # First descriptor instance age = LoggedAccess() # Second descriptor instance def __init__(self, name, age): self.name = name # Calls the first descriptor self.age = age # Calls the second descriptor def birthday(self): self.age += 1
Question: Let's consider data : import numpy as np from sklearn.linear_model import LogisticRegression x=np.linspace(0,2*np.pi,80) x = x.reshape(-1,1) y = np.sin(x)+np.random.normal(0,0.4,80) y[y<1/2] = 0 y[y>1/2] = 1 clf=LogisticRegression(solver="saga", max_iter = 1000)  I want to fit logistic regression where y is dependent variable, and x is independent variable. But while I'm using : clf.fit(x,y)  I see error 'y should be a 1d array, got an array of shape (80, 80) instead'.  I tried to reshape data by using y=y.reshape(-1,1)  But I end up with array of length 6400! (How come?) Could you please give me a hand with performing this regression ?

Answer: Change the order of your operations: First geneate x and y as 1-D arrays: x = np.linspace(0, 2*np.pi, 8) y = np.sin(x) + np.random.normal(0, 0.4, 8)  Then (after y was generated) reshape x: x = x.reshape(-1, 1)  Edit following a comment as of 2022-02-20 The source of the problem in the original code is that;  x = np.linspace(0,2*np.pi,80) - generates a 1-D array. x = x.reshape(-1,1) - reshapes it into a 2-D array, with one column and as many rows as needed. y = np.sin(x) + np.random.normal(0,0.4,80) - operates on a columnar array and a 1-D array (treated here as a single row array). the effect is that y is a 2-D array (80 * 80). then the attempt to reshape y gives a single column array with 6400 rows.  The proper solution is that both x and y should be initially 1-D (single row) arrays and my code does just this. Then both arrays can be reshaped.
Question: I have some named tuples: JOIN = NamedTuple("JOIN", []) EXIT = NamedTuple("EXIT", [])  I also have functions to handle each type of tuple with: def handleJoin(t: JOIN) -> bool: pass def handleExit(t: EXIT) -> bool: pass  What I want to do is create a dictionary handleTuple so I can call it like so: t: Union[JOIN, EXIT] = #an instance of JOIN or EXIT result: bool result = handleTuple[type(t)](t)  What I cannot figure out is how to define said dictionary. I tried defining a generic T: T = TypeVar("T", JOIN, EXIT) handleTuple: Dict[T, Callable[[T], bool]  However I get an error saying "Type variable T is unbound" which I do not understand. The closest I have got so far is: handleTuple: Dict[Type[Union[JOIN, EXIT]], bool] handleTuple = { JOIN: True EXIT: False }  This works fine for calling it the way I want, however I cannot figure out the Callable part in the definition so I can include my functions. How can I do this

Answer: TypeVars are only meaningful in aliases, classes and functions. One can define a Protocol for the lookup: T = TypeVar("T", JOIN, EXIT, contravariant=True) class Handler(Protocol): def __getitem__(self, item: Type[T]) -> Callable[[T], bool]: ... handleTuple = cast(Handler, {JOIN: handleJoin, EXIT: handleExit})  The special method self.__getitem__(item) corresponds to self[item]. Thus, the protocol defines that accessing handleTuple[item] with item: Type[T] evaluates to some Callable[[T], bool]. The cast is currently needed for type checkers such as MyPy to understand that the dict is a valid implementation of this protocol.  Since the code effectively implements a single dispatch, defining a functools.singledispatch function provides the behaviour out of the box: @singledispatch def handle(t) -> bool: raise NotImplementedError @handle.register def handleJoin(t: JOIN) -> bool: pass @handle.register def handleExit(t: EXIT) -> bool: pass t: Union[JOIN, EXIT] result: bool result = handle(t)
Question: i have started a new project in python using pygame and for the background i want the bottom half filled with gray and the top black. i have used rect drawing in projects before but for some reason it seems to be broken? i don't know what i am doing wrong. the weirdest thing is that the result is different every time i run the program. sometimes there is only a black screen and sometimes a gray rectangle covers part of the screen, but never half of the screen. import pygame, sys from pygame.locals import * pygame.init() DISPLAY=pygame.display.set_mode((800,800)) pygame.display.set_caption("thing") pygame.draw.rect(DISPLAY, (200,200,200), pygame.Rect(0,400,800,400)) while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() 

Answer: You need to update the display. You are actually drawing on a Surface object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visibel, when the display is updated with either pygame.display.update() or pygame.display.flip(). See pygame.display.flip():  This will update the contents of the entire display.  While pygame.display.flip() will update the contents of the entire display, pygame.display.update() allows updating only a portion of the screen to updated, instead of the entire area. pygame.display.update() is an optimized version of pygame.display.flip() for software displays, but doesn't work for hardware accelerated displays. The typical PyGame application loop has to:  handle the events by calling either pygame.event.pump() or pygame.event.get(). update the game states and positions of objects dependent on the input events and time (respectively frames) clear the entire display or draw the background draw the entire scene (draw all the objects) update the display by calling either pygame.display.update() or pygame.display.flip() limit frames per second to limit CPU usage with pygame.time.Clock.tick  import pygame from pygame.locals import * pygame.init() DISPLAY = pygame.display.set_mode((800,800)) pygame.display.set_caption("thing") clock = pygame.time.Clock() run = True while run: # handle events for event in pygame.event.get(): if event.type == QUIT: run = False # clear display DISPLAY.fill(0) # draw scene pygame.draw.rect(DISPLAY, (200,200,200), pygame.Rect(0,400,800,400)) # update display pygame.display.flip() # limit frames per second clock.tick(60) pygame.quit() exit()   repl.it/@Rabbid76/PyGame-MinimalApplicationLoop See also Event and application loop
Question: Plotly Express has an intuitive way to provide pre-formatted plotly plots with minimal lines of code; sort of how Seaborn does it for matplotlib. It is possible to add traces of plots on Plotly to get a scatter plot on an existing line plot. However, I couldn't find such a functionality in Plotly Express. Is it possible to combine a scatter and line graph in Plotly Express?

Answer: You can use: fig3 = go.Figure(data=fig1.data + fig2.data)  Where fig1 and fig2 are built using px.line() and px.scatter(), respectively. And fig3 is, as you can see, built using plotly.graph_objects. Some details: One approach that I use alot is building two figures fig1 and fig2 using plotly.express and then combine them using their data attributes together with a go.Figure / plotly.graph_objects object like this: import plotly.express as px import plotly.graph_objects as go df = px.data.iris() fig1 = px.line(df, x="sepal_width", y="sepal_length") fig1.update_traces(line=dict(color = 'rgba(50,50,50,0.2)')) fig2 = px.scatter(df, x="sepal_width", y="sepal_length", color="species") fig3 = go.Figure(data=fig1.data + fig2.data) fig3.show()  Plot:
Question: I'm using alembic to manage database migrations as per user defined sqlalchemy models. My challenge is that I'd like for alembic to ignore any creation, deletion, or changes to a specific set of tables. Note: My Q is similar to this question Ignoring a model when using alembic autogenerate but is different in that I want to control alembic from outside the model definition. Here's a sample table I want to ignore: from sqlalchemy import MetaData from sqlalchemy.ext.declarative import declarative_base Base = declarative_base(metadata=MetaData()) class Ignore1(Base): """ Signed in to the account... """ __tablename__ = 'ignore_1' __table_args__ = { 'info':{'skip_autogenerate':True} } id = Column(Integer, primary_key=True) foo = Column(String(20), nullable=True)  Example code (which does not solve my issue):  In alembic/env.py # Ideally this is stored in my actual database, but for now, let's assume we have a list... IGNORE_TABLES = ['ignore_1', 'ignore_2'] def include_object(object, name, type_, reflected, compare_to): """ Should you include this table or not? """ if type_ == 'table' and (name in IGNORE_TABLES or object.info.get("skip_autogenerate", False)): return False elif type_ == "column" and object.info.get("skip_autogenerate", False): return False return True # Then add to config context.configure( ... include_object=include_object, ... ) 

Answer: I found a solution to my problem! My error was in the instantiation of my context object in env.py def run_migrations_offline(): ... context.configure( url=url, target_metadata=target_metadata, include_object=include_object, literal_binds=True, dialect_opts={"paramstyle": "named"}, ) with context.begin_transaction(): context.run_migrations()  I wasn't applying this change to context for online migrations: def run_migrations_online(): ... connectable = engine_from_config( config.get_section(config.config_ini_section), prefix="sqlalchemy.", poolclass=pool.NullPool, ) with connectable.connect() as connection: context.configure( connection=connection, target_metadata=target_metadata, # THE FOLLOWING LINE WAS MISSING FROM MY ORIGINAL CODE include_object=include_object, # <----------------------- THIS! ) ...  Hopefully anyone else encountering this issue and experiencing similar turmoil can read through my question & following solution and recognize that despair is but a small dumb tweak from salvation.
Question: I have an async function that calls a streaming api. What is the best way to write unit test for this function? The api response has to be mocked. I tried with aiounittest and used mock from unittest. But this calls the actual api instead of getting the mocked response. Also tried with pytest.mark.asyncio annotation, but this kept giving me the error - coroutine was never awaited. I have verified that pytest-asyncio has been installed. I am using VS Code and Python 3.6.6 Here is the relevant code snippet: async def method1(): response = requests.get(url=url, params=params, stream=True) for data in response.iter_lines(): # processing logic here yield data  Pasting some of the tests I tried. def mocked_get(*args, **kwargs): #implementation of mock class TestClass (unittest.TestCase): @patch("requests.get", side_effect=mocked_get) async def test_method (self, mock_requests): resp = [] async for data in method1: resp.append (data) #Also tried await method1 assert resp  Also tried with class TestClass (aiounittest.AsyncTestCase):

Answer: Use asynctest instead of aiounittest.  Replace unittest.TestCase with asynctest.TestCase. Replace from unittest.mock import patch with from asynctest.mock import patch. async for data in method1: should be async for data in method1():.  import asynctest from asynctest.mock import patch class TestClass(asynctest.TestCase): @patch("requests.get", side_effect=mocked_get) async def test_method(self, mock_requests): resp = [] async for data in method1(): resp.append(data) assert resp
Question: I have installed python 3.8.6 with homebrew on macOS. But when I check with which -a python3 I only get paths of 3.9 and 3.8.2. Is there a way to find the paths of all versions installed by homebrew? Or maybe more general question, how can I find the path of 3.8.6? 

Answer: Use brew info <packagename> it's probably in one of (and referenced in both) /usr/local/Cellar/python@3.8/3.8.6_2 /usr/local/opt/python@3.8/libexec/bin  See also Apple SE Where can I find the installed package path via brew If it's not there, it's plausible the version detection is wrong and only searches for the first two version digits (as conflicting versions may clobber each other). In this case, follow the warning and reinstall the specific version you want.
Question: I have a list patterns: patterns=['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn']  and I have big dataframe with strings, for example: str0='Mg0.97Fe0.03B2' str1='Tl0.5Hg0.5Ba2Ca2Cu3O8'  I am trying this: keyss=list(filter(None,regex.split("[^a-zA-Z]*",somestring))) values=list(filter(None,regex.split("[^0-9.0-9]*",somestring)))  Sometimes, this works: str3='Hg0.75SrBa2Ca2Cu3O8' keyss=list(filter(None,regex.split("[^a-zA-Z]*",str3))) values=list(filter(None,regex.split("[^0-9.0-9]*",str3)) ['Ba', 'Fe', 'Co', 'Mn', 'As'] ['1', '1.832', '0.15', '0.018', '2']  However, if I have a string like this: str3='Hg0.75SrBa2Ca2Cu3O8' keyss=list(filter(None,regex.split("[^a-zA-Z]*",str3))) values=list(filter(None,regex.split("[^0-9.0-9]*",str3))) ['Hg', 'SrBa', 'Ca', 'Cu', 'O']!=['Hg', 'Sr','Ba', 'Ca', 'Cu', 'O'] ['0.75', '2', '2', '3', '8']!=['0.75', '1','2', '2', '3', '8']  or this str4='NbSn3' keyss=list(filter(None,regex.split("[^a-zA-Z]*",str4))) values=list(filter(None,regex.split("[^0-9.0-9]*",str4))) ['NbSn']!=['Nb','Sn'] ['3']!=['1','3'] str4='Pb1.4Sr4Y1.2Ca0.8Cu4.6O' ...  My code is not working correctly. How I can fix it?

Answer: Use import pandas as pd patterns=['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn'] rx = fr'({"|".join(sorted(patterns, key=len,reverse=True))})(\d+(?:\.\d+)?)?' df = pd.DataFrame({'formulas' : ['Mg0.97Fe0.03B2', 'Tl0.5Hg0.5Ba2Ca2Cu3O8', 'Hg0.75SrBa2Ca2Cu3O8', 'NbSn3']}) df['result'] = df['formulas'].str.findall(rx) df['result'] = df['result'].apply(lambda m: [(x,y) if y else (x,1) for x,y in m])  Results >>> df formulas result 0 Mg0.97Fe0.03B2 [(Mg, 0.97), (Fe, 0.03), (B, 2)] 1 Tl0.5Hg0.5Ba2Ca2Cu3O8 [(Tl, 0.5), (Hg, 0.5), (Ba, 2), (Ca, 2), (Cu, 3), (O, 8)] 2 Hg0.75SrBa2Ca2Cu3O8 [(Hg, 0.75), (Sr, 1), (Ba, 2), (Ca, 2), (Cu, 3), (O, 8)] 3 NbSn3 [(Nb, 1), (Sn, 3)]
Question: I have tried to repo init the source code Ubuntu build machine and it is successfully able to clone the code. repo init -u git@github.com:xxx/xx_manifest.git -b xxx Now I am trying repo init the source code in VM Ubuntu machine. In between getting the error like below: Traceback (most recent call last): File "/xxx/.repo/repo/main.py", line 56, in <module> from subcmds.version import Version File "/xxx/.repo/repo/subcmds/__init__.py", line 38, in <module> ['%s' % name]) File "/xxx/.repo/repo/subcmds/upload.py", line 27, in <module> from hooks import RepoHook File "/xxx/.repo/repo/hooks.py", line 472 file=sys.stderr) ^ SyntaxError: invalid syntax  python version is same in build machine and vm machine 2.7.17.

Answer: try these commands curl https://storage.googleapis.com/git-repo-downloads/repo-1 > ~/bin/repo chmod a+x ~/bin/repo python3 ~/bin/repo init -u git@....