eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I would like to compare two global dataclasses in terms of equality. I changed the field in one of the dataclasses and python still insists on telling me, that those objects are equal. I don't know how internally dataclasses work, but when I print asdict I get an empty dictionary... What am I doing wrong and how can I compare dataclasses by checking for equality of its members? I'm using Python 3.9.4 from dataclasses import dataclass, asdict @dataclass class TestClass: field1 = None field2 = False test1 = TestClass() test2 = TestClass() def main(): global test1 global test2 test2.field2 = True print('Test1: ', id(test1), asdict(test1), test1.field1, test1.field2) print('Test2: ', id(test2), asdict(test2), test2.field1, test2.field2) print('Are equal? ', test1 == test2) print('Are not equal?', test1 != test2) if __name__ == '__main__': main()  Output: Test1: 2017289121504 {} None False Test2: 2017289119296 {} None True Are equal? True Are not equal? False 

Answer: For Python to recognize fields of a dataclass, those fields should have PEP 526 type annotations. For example: from typing import Optional @dataclass class TestClass: field1: Optional[str] = None field2: bool = False  With that definition comparisons and asdict work as expected: In [2]: TestClass(field2=True) == TestClass() Out[2]: False In [3]: asdict(TestClass(field2=True)) Out[3]: {'field1': None, 'field2': True}
Question: I am trying to use the testing extension in VSCode with the Python extension. I am using pytest as my testing library. My folder structure looks like this: PACKAGENAME/ ├─ PACKAGENAME/ │ ├─ __init__.py │ ├─ main.py ├─ tests/ │ ├─ test_main.py ├─ requirements.txt  In the test_main.py file I am trying to import the package code, in order to test it: from PACKAGENAME import *  From the command line, in the root directory, PACKAGENAME, I can use the command python -m pytest which runs the tests fine. There are no issues with modules not being found. However, when I try to use the VSCode testing tab, the tests are discovered, but this errors: =================================== ERRORS ==================================== _____________________ ERROR collecting tests/test_main.py _____________________ ImportError while importing test module 'd:\PATH\TO\PACKAGENAME\tests\test_main.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: C:\Users\USER\anaconda3\envs\uni\lib\importlib\__init__.py:127: in import_module return _bootstrap._gcd_import(name[level:], package, level) tests\test_main.py:1: in <module> from PACKAGENAME import * E ModuleNotFoundError: No module named 'PACKAGENAME' =========================== short test summary info ===========================  Is there any way to get this working without having to use the command line?

Answer: I suggest that you try like this:  make sure that your VS Code workspace is set to the parent directory of (the root directory) PACKAGENAME add an empty __init__.py file in testsdirectory in test_main.py, replace from PACKAGENAME import * with from PACKAGENAME.main import *
Question: I know this is one of the popular questions, but none of the solutions worked for me, so far. I'm running a legacy code that is written in tensorflow v1.13.1 and keras v2.2.4. I cannot modify the code to run latest tensorflow version. Since keras has now been merged into tensorflow, I'm facing problems installing the specific versions of tensorflow and keras via pip. I found that anaconda has option to install keras and tensorflow with the above version. So, I installed it with conda install -c conda-forge keras-gpu=2.2.4 tensorflow-gpu=1.13.1  It installed the version and all works too. But it doesn't use GPU, and instead runs on CPU. I noticed that anaconda installed both CPU and GPU versions of tensorflow and I guess this is why it is defaulting to CPU version. So, my question is, how can I force it to use GPU version? PS: There are many answers out there that suggest to remove CPU version of tensorflow. But when I try to remove CPU version, conda uninstalls everything including keras. So, I assume there should be a way to use tensorflow-gpu when both of them are installed. Any help in this regard is appreciated!

Answer: Installing tensorflow first and then keras worked! conda install tensorflow-gpu=1.13.1 conda install keras-gpu=2.2.4
Question: I'm trying to build FastAPI application fully covered with test using python 3.9 For this purpose I've chosen stack: FastAPI, uvicorn, SQLAlchemy, asyncpg, pytest (+ async, cov plugins), coverage and httpx AsyncClient Here is my minimal requirements.txt All tests run smoothly and I get the expected results. But I've faced the problem, coverage doesn't properly collected. It breaks after a first await keyword, when coroutine returns control back to the event loop Here is a minimal set on how to reproduce this behavior (it's also available on a GitHub). Appliaction code main.py: import sqlalchemy as sa from fastapi import FastAPI from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from starlette.requests import Request app = FastAPI() DATABASE_URL = 'sqlite+aiosqlite://?cache=shared' @app.on_event('startup') async def startup_event(): engine = create_async_engine(DATABASE_URL, future=True) app.state.session = AsyncSession(engine, expire_on_commit=False) app.state.engine = engine @app.on_event('shutdown') async def shutdown_event(): await app.state.session.close() @app.get('/', name="home") async def get_home(request: Request): res = await request.app.state.session.execute(sa.text('SELECT 1')) # after this line coverage breaks row = res.first() assert str(row[0]) == '1' return {"message": "OK"}  test setup conftest.py looks like this: import asyncio import pytest from asgi_lifespan import LifespanManager from httpx import AsyncClient @pytest.fixture(scope='session') async def get_app(): from main import app async with LifespanManager(app): yield app @pytest.fixture(scope='session') async def get_client(get_app): async with AsyncClient(app=get_app, base_url="http://testserver") as client: yield client @pytest.fixture(scope="session") def event_loop(): loop = asyncio.new_event_loop() yield loop loop.close()  test is simple as it is (just check status code is 200) test_main.py: import pytest from starlette import status @pytest.mark.asyncio async def test_view_health_check_200_ok(get_client): res = await get_client.get('/') assert res.status_code == status.HTTP_200_OK  pytest -vv --cov=. --cov-report term-missing --cov-report html  As a result coverage I get: Name Stmts Miss Cover Missing -------------------------------------------- conftest.py 18 0 100% main.py 20 3 85% 26-28 test_main.py 6 0 100% -------------------------------------------- TOTAL 44 3 93%    Example code above uses aiosqlite instead of asyncpg but coverage failure also reproduces persistently I've concluded this problem is with SQLAlchemy, because this example with asyncpg without using the SQLAlchemy works like charm 

Answer: it's an issue with SQLAlchemy 1.4 in coveragepy: https://github.com/nedbat/coveragepy/issues/1082, https://github.com/nedbat/coveragepy/issues/1012 you can try with --concurrency==greenlet option
Question: Hi all I am building a simple web app with streamlit in python. I need to add 3 buttons but they must be on the same line. Obviously the following code puts them on three different lines st.button('Button 1') st.button('Button 2') st.button('Button 3')  Do you have any tips?

Answer: Apparently this should do it col1, col2, col3 = st.columns([1,1,1]) with col1: st.button('1') with col2: st.button('2') with col3: st.button('3')
Question: I'm wondering if there's any way in python or perl to build a regex where you can define a set of options can appear at most once in any order. So for example I would like a derivative of foo(?: [abc])*, where a, b, c could only appear once. So: foo a b c foo b c a foo a b foo b  would all be valid, but foo b b  would not be

Answer: You may use this regex with a capture group and a negative lookahead: For Perl, you can use this variant with forward referencing: ^foo((?!.*\1) [abc])+$  RegEx Demo RegEx Details:  ^: Start foo: Match foo (: Start a capture group #1  (?!.*\1): Negative lookahead to assert that we don't match what we have in capture group #1 anywhere in input  [abc]: Match a space followed by a or b or c   )+: End capture group #1. Repeat this group 1+ times $: End  As mentioned earlier, this regex is using a feature called Forward Referencing which is a back-reference to a group that appears later in the regex pattern. JGsoft, .NET, Java, Perl, PCRE, PHP, Delphi, and Ruby allow forward references but Python doesn't.  Here is a work-around of same regex for Python that doesn't use forward referencing: ^foo(?!.* ([abc]).*\1)(?: [abc])+$  Here we use a negative lookahead before repeated group to check and fail the match if there is any repeat of allowed substrings i.e. [abc]. RegEx Demo 2
Question: Example setup: from typing import Optional class A(object): def __init__(self): self.a: Optional[int] = None def check_a(self) -> bool: return self.a is not None a = A() if a.check_a(): print(a.a + 1) # error: Unsupported operand types for + ("None" and "int")  The check_a method checks what type of variable a is, but mypy does not see this and writes an error. TypeGuard will not help, because it can create a function to check the type, and not a function to check the type of an object variable Is it possible to somehow make mypy notice this in order to use the function to check the type of the variable self.a without explicitly referring to it in the check? (Use if a.a_check instead of if a.a is not None)?

Answer: There are two ways in which this can be done with the new TypeGuard feature, which can be imported from typing in Python 3.10, and is available from the PyPI typing_extensions package in earlier Python versions. Note that typing_extensions is already a dependency of Mypy, so if you're using Mypy, you likely already have it. The first option is to change your check_a method to a staticmethod that takes in a variable that might be int or None as an input, and verifies whether or not it is an int. (Apologies, I have changed the names of some of your variables, as I found it quite confusing to have a class A that also had an a attribute.) from typing import TypeGuard, Optional class Foo1: def __init__(self, bar: Optional[int] = None) -> None: self.bar = bar @staticmethod def check_bar(bar: Optional[int]) -> TypeGuard[int]: return bar is not None f1 = Foo1() if f1.check_bar(f1.bar): print(f1.bar + 1)  The second option is to use structural subtyping to assert that an instance of class Foo (or class A in your original question) has certain properties at a certain point in time. This requires altering the test method so that it becomes a classmethod, and is a little more complex to set up, but leads to a nicer check once you have it set up. from typing import TypeGuard, Optional, Protocol, TypeVar class HasIntBar(Protocol): bar: int F = TypeVar('F', bound='Foo2') class Foo2: def __init__(self, bar: Optional[int] = None) -> None: self.bar = bar @classmethod def check_bar(cls: type[F], instance: F) -> TypeGuard[HasIntBar]: return instance.bar is not None f2 = Foo2() if Foo2.check_bar(f2): # could also write this as `if f2.check_bar(f2)` print(f2.bar + 1)  You can try both of these options out on Mypy playground here.
Question: I want to log script hashes to implement content security policy. I have been able to generate the hash in python with the following code: import hashlib import base64 string=''' //<![CDATA[ var theForm = document.forms['ctl00']; if (!theForm) { theForm = document.ctl00; } function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } } //]]> ''' # encode as UTF-8 string_UTF8 = string.encode('utf-8') # hash the message hash_string = hashlib.sha256(string_UTF8).digest() # base64 encode result = base64.b64encode(hash_string) print('sha256-' + result.decode('utf-8'))  How can I do this with Javascript?

Answer:   const string = ` //<![CDATA[ var theForm = document.forms['ctl00']; if (!theForm) { theForm = document.ctl00; } function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } } //]]> ` async function hashFromString(string) { const hash = await crypto.subtle.digest("SHA-256", (new TextEncoder()).encode(string)) return "sha256-" + btoa(String.fromCharCode(...new Uint8Array(hash))) } hashFromString(string).then(console.log)    Edit: I realize now that while not stated in your question it's likely that you are using Node.js therefor this answer that uses browser APIs may be of less use.
Question: I would like to infer in Python if a date is the actual day of the year in which the hour is changed due to DST (Daylight Saving Time). With the library pytz you can localize a datetime and the actual DST change is correctly done. Furthermore, there is the method dst() of the library datetime that allows you to infer if an actual date is in summer or winter time (example). However, I would like to infer the actual day in which a DST change is made. Concretely, I would need a function (for example is_dst_change(date, timezone)) that receives a date and returns True only for those days of the year that do have an hour change. For example: import pytz import datetime def is_dst_change(day, timezone): # Localize loc_day = pytz.timezone(timezone).localize(day) # Pseudocode: Infer if a date is the actual day in which the dst hour change is made if loc_day is dst_change_day: return True else: return False # In the timezone 'Europe/Madrid', the days in which the DST change is made in 2021 are 28/03/2021 and 31/10/2021 is_dst_change(day=datetime.datetime(year=2021, month=3, day=28), timezone = 'Europe/Madrid') # This should return True is_dst_change(day=datetime.datetime(year=2021, month=10, day=31), timezone = 'Europe/Madrid') # This should return True is_dst_change(day=datetime.datetime(year=2021, month=2, day=1), timezone = 'Europe/Madrid') # This should return False is_dst_change(day=datetime.datetime(year=2021, month=7, day=1), timezone = 'Europe/Madrid') # This should return False  Thus, in the above example the only days of 2021 for which the function is_dst_change(day, timezone='Europe/Madrid') will return True are 28/03/2021 and 31/10/2021. For the rest of the days of the year 2021, it must return False. Is there a way to infer this with Python?

Answer: You can make use of datetime.dst() (a change in UTC offset is not necessarily a DST transition): from datetime import datetime, time, timedelta from zoneinfo import ZoneInfo # Python 3.9+ def is_date_of_DSTtransition(dt: datetime, zone: str) -> bool: """ check if the date part of a datetime object falls on the date of a DST transition. """ _d = datetime.combine(dt.date(), time.min).replace(tzinfo=ZoneInfo(zone)) return _d.dst() != (_d+timedelta(1)).dst()  e.g. for tz Europe/Berlin: for d in range(366): if is_date_of_DSTtransition(datetime(2021, 1, 1) + timedelta(d), "Europe/Berlin"): print((datetime(2021, 1, 1) + timedelta(d)).date()) # 2021-03-28 # 2021-10-31   Note: I'm using zoneinfo here instead of pytz; for legacy code, there is a pytz deprecation shim. Here's a pytz version anyway (needs an additional normalize): import pytz def is_date_of_DSTtransition(dt: datetime, zone: str) -> bool: _d = pytz.timezone(zone).localize(datetime.combine(dt.date(), time.min)) return _d.dst() != pytz.timezone(zone).normalize(_d+timedelta(1)).dst()
Question: Example 1: from typing import List, Optional def myfunc() -> List[Optional[str]]: some_list = [x for x in "abc"] return some_list  Mypy complains on example 1:  Incompatible return value type (got "List[str]", expected "List[Optional[str]]")  However, this example gets no complaint: Example 2: def myfunc() -> List[Optional[str]]: some_list = [x for x in "abc"] return list(some_list)  What is the explanation for the inconsistent behavior?

Answer: Since in Python lists are invariant (see the examples here and here). If we pass List[str] to someone that expects List[Optional[str]], that someone may add a None to our list and break our assumptions. The second example is valid however as the output of list() in the return statement is not saved anywhere and no one can depend on the the returned value being mutated illegally .
Question: I'm training a KNN classifier using scikit-learn's KNeighborsClassifier with cross validation: k=1 param_space = {'n_neighbors': [k]} model = KNeighborsClassifier(n_neighbors=k, metric='euclidean') search = GridSearchCV(model, param_space, cv=cv, verbose=10, n_jobs=8) search.fit(X_df, y_df) preds = search.best_estimator_.predict(X_df)  when k=1 and with any cv value (lets say cv=4), I'm getting perfect score from sklearn.metrics import confusion_matrix tn, fp, fn, tp = confusion_matrix(y_df, preds).ravel() f1 = tp / (tp + 0.5 * (fp + fn)) # f1 is perfect 1  It is important to say that I use this method on multiple datasets, and every time k=1 the score is a perfect 1. I tried doing it on random data and still got f1=1. Is there any known bug with KNeighborsClassifier when k=1? Maybe I'm missing something else? Thanks in advance.

Answer: Your predictions are from the best_estimator_, which is a copy of the estimator with the optimal hyperparameters (according to the cross-validation scores) refitted to the entire training set. So the confusion matrix you generate is really a training score, and for 1-neighbors that's trivially perfect (the nearest neighbor of a point is itself).
Question: So I'm trying to make a login prompt and I want it to print 'Success' only if there are no errors. This is the code I'm using: if not is_email(email) or not is_name(name) or password != confirmPassword or not is_secure(password): if not is_email(email): print('Not a valid email') if not is_name(name): print('Not a valid name') if password != confirmPassword: print('Passwords don\'t match') if not is_secure(password): print('Password is not secure') else: print('Success')  Would there be any way to make this code shorter? I want to make it show all the errors at once so I'm not using elif.

Answer: How about this? flags = [is_email(email), is_name(name), password != confirmPassword, is_secure(password)] prints = ['Not a valid email', 'Not a valid name', 'Passwords don\'t match', 'Password is not secure'] for index in range(len(flags)): if flags[index] == False: print(prints[index])
Question: How to mark pydantic model filed as secret so it will not shown in the repr str and will be excluded from dict and etc... from pydantic import BaseModel class User(BaseModel): name: str password_hash: str # I do not want this field to leak out.  I write my code with security in mind and I afraid that in the future someone else will write non secure code that will leak the 'password_hash' field outside to the logs etc... Is there a way to mark the field as secret to be sure it not leak out?

Answer: Pydantic provides convenience Secret* classes for this exact purpose: from pydantic import BaseModel, SecretStr class User(BaseModel): name: str password_hash: SecretStr
Question: i have an import problem when executing my code: from keras.models import Sequential from keras.layers.normalization import BatchNormalization  2021-10-06 22:27:14.064885: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found 2021-10-06 22:27:14.064974: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. Traceback (most recent call last): File "C:\Data\breast-cancer-classification\train_model.py", line 10, in <module> from cancernet.cancernet import CancerNet File "C:\Data\breast-cancer-classification\cancernet\cancernet.py", line 2, in <module> from keras.layers.normalization import BatchNormalization ImportError: cannot import name 'BatchNormalization' from 'keras.layers.normalization' (C:\Users\Catalin\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\layers\normalization\__init__.py)   Keras version: 2.6.0 Tensorflow: 2.6.0 Python version: 3.9.7  The library it is installed also with pip install numpy opencv-python pillow tensorflow keras imutils scikit-learn matplotlib  Do you have any ideas? library path

Answer: You're using outdated imports for tf.keras. Layers can now be imported directly from tensorflow.keras.layers: from tensorflow.keras.models import Sequential from tensorflow.keras.layers import ( BatchNormalization, SeparableConv2D, MaxPooling2D, Activation, Flatten, Dropout, Dense ) from tensorflow.keras import backend as K class CancerNet: @staticmethod def build(width, height, depth, classes): model = Sequential() shape = (height, width, depth) channelDim = -1 if K.image_data_format() == "channels_first": shape = (depth, height, width) channelDim = 1 model.add(SeparableConv2D(32, (3, 3), padding="same", input_shape=shape)) model.add(Activation("relu")) model.add(BatchNormalization(axis=channelDim)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(SeparableConv2D(64, (3, 3), padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(axis=channelDim)) model.add(SeparableConv2D(64, (3, 3), padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(axis=channelDim)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(SeparableConv2D(128, (3, 3), padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(axis=channelDim)) model.add(SeparableConv2D(128, (3, 3), padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(axis=channelDim)) model.add(SeparableConv2D(128, (3, 3), padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(axis=channelDim)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(256)) model.add(Activation("relu")) model.add(BatchNormalization()) model.add(Dropout(0.5)) model.add(Dense(classes)) model.add(Activation("softmax")) return model model = CancerNet()
Question: Reading the documentation of the python standard library copy module I stumbled across the following sentence:  The memo dictionary should be treated as an opaque object.  I understand that an opaque object usually is an object whose internals are unknown and which is only accessed via member functions. What does being an opaque object mean for a simple data structure like a dictionary? And what do I have to pay attention to in the case of implementing __deepcopy__() for custom classes?

Answer: Read the sentence that precedes your quoted sentence.  If the __deepcopy__() implementation needs to make a deep copy of a component, it should call the deepcopy() function with the component as first argument and the memo dictionary as second argument.  The idea is that your __deepcopy__ method should do nothing with a received memo dictionary except pass it to another call to deepcopy. Specifially, you should not  Add any keys Remove any keys Modify the value of an existing key.  As far as you are concerned, the memo dictionary is an object whose internals are unknown (someday, it may not even be a dict!). Your only job is to pass it to any "recursive" calls to deepcopy.