eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'd like to load a keras model that i've trained and saved it as .pb. Here's the code,   Am using a jupyter notebook.  The model is successfully saved as saved_model.pb under the same directory. But the code is unable to access it.  Can anybody see to it, how can i access this keras model that's saved in .pb extension. I checked at several other places for solution but no luck.   Model is saved at model/saved_model.pb.  I've taken out the .pb file and placed it in the same directory where my code file exists.

Answer: The function tf.keras.models.load_model load a SavedModel into a tf.keras -model. The argument of the function is path to a saved model. So try model = tf.keras.models.load_model('model')
Question: What is the proper way to add type hints for the return of a function with the @asynccontextmanager decorator? Here are two attempts I made that both fail. from contextlib import asynccontextmanager from typing import AsyncContextManager async def caller(): async with return_str() as i: print(i) async with return_AsyncContextManager() as j: print(j) @asynccontextmanager async def return_str() -> str: yield "hello" @asynccontextmanager async def return_AsyncContextManager() -> AsyncContextManager[str]: yield "world"  For both i and j Pylance in vscode shows type Any. Other ideas I've considered:  I thought maybe I could pass in the type info to the decorator itself (like @asynccontextmanager(cls=str), but I can't find any examples of that, or any description of the args I could pass in. async with return_str() as i: # type: str doesn't work either. Even if it did, I'd rather hint at the function definition, not at every invocation. Type comments are not very DRY. I tried to create an AsyncContextManager class with __aenter()__ and __aexit()__ functions, but was not successful. I would fall back to that if it worked, but I'd prefer to make the decorator work because it's much more concise.  Here's a screencap of me hovering the return_AsyncContextManager() function, and showing the Pylance popup saying it returns AsyncContextManager[_T] 

Answer: You have to hint AsyncIterator as the return type, like this: @asynccontextmanager async def my_acm() -> AsyncIterator[str]: yield "this works" async def caller(): async with my_acm() as val: print(val)  This is because the yield keyword is used to create generators. Consider: def a_number_generator(): for x in range(10): # type: int yield x g = a_number_generator() # g is type Generator[int]  This makes sense given the type hints for @asynccontextmanager: asynccontextmanager(func: Callable[_P, AsyncIterator[_T_co]]) -> Callable[_P, _AsyncGeneratorContextManager[_T_co]] That's a lot to parse but it says that the asynccontextmanager takes a function which returns AsyncIterator and transforms it into a new function that returns AsyncContextManager. The generic types _P and _T_co are preserved as well. Here is a screenshot showing the type hint transferring into the caller function.
Question: I have multiple dictionaries inside the list. I want to sort the dictionary with the custom key. In my case, I want to sort it using Date key. By that, I mean to move the Date key to the first position. What is the efficient way to sort the dictionary using Date key? PS: I don't want to sort by the value of the Date. [ { "AmazonS3":6.54, "AmazonEC2":27.55, "AmazonCloudWatch":0.51, "Date":"2020-07-01" }, { "AmazonEC2":27.8, "Date":"2020-07-02" }, { "AmazonElastiCache":0.01, "AmazonEC2":35.34, "Date":"2020-07-03" } ]  Expected output: ... { "Date":"2020-07-03", "AmazonElastiCache":0.01, "AmazonEC2":35.34 } ... 

Answer: If you are using a Python version that preserves key insertion order (i.e. 3.7 or newer) you can do this: print([{"Date": di["Date"], **di} for di in my_list])   [ { 'Date': '2020-07-01', 'AmazonS3': 6.54, 'AmazonEC2': 27.55, 'AmazonCloudWatch': 0.51 }, { 'Date': '2020-07-02', 'AmazonEC2': 27.8 }, { 'Date': '2020-07-03', 'AmazonElastiCache': 0.01, 'AmazonEC2': 35.34 } ]
Question: I was coding sorter for downloads folder. I'm getting this error, I tried to change permissions:  chmod: Unable to change file mode on Users: Operation not permitted import os from_dir = os.path.dirname('/Users/user/Downloads/') working_dir = os.walk(from_dir) to_dir = os.path.dirname('/User/user/Downloads/New Folder/') def move(folder): for roots, dirs, files in folder: for file in files: src_folder = from_dir + '/' + file to_folder = to_dir + '/' + file if not os.path.exists(to_dir): os.makedirs(to_dir) os.rename(src_folder, to_folder) move(working_dir)  Maybe there is another way to write this code just without touching root folders? Full error: Traceback (most recent call last): File "/Users/beknazarnurbek/Documents/PycharmProjects/Move Files/move.py", line 19, in <module> move(working_dir) File "/Users/beknazarnurbek/Documents/PycharmProjects/Move Files/move.py", line 14, in move os.makedirs(to_dir) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/os.py", line 211, in makedirs makedirs(head, exist_ok=exist_ok) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/os.py", line 211, in makedirs makedirs(head, exist_ok=exist_ok) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/os.py", line 211, in makedirs makedirs(head, exist_ok=exist_ok) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/os.py", line 221, in makedirs mkdir(name, mode) OSError: [Errno 30] Read-only file system: '/User' 

Answer: That error message is bit misleading. In this case, the problem is that there is no /User directory on macOS. The directory is named /Users. In the following line to_dir = os.path.dirname('/User/user/Downloads/New Folder/') User should be Users to_dir = os.path.dirname('/Users/user/Downloads/New Folder/') What's happening is that os.mkdirs() is attempting to create a directory User in /. Which is not writable. Which is causing the error message.
Question: I am trying to install Tensorflow 1.14 for a package that I am trying to use. I tried: pip3 uninstall tensorflow Then I tried to install Tensorflow 1.14 using: pip3 install tensorflow==1.14 and I get the following error ERROR: Could not find a version that satisfies the requirement tensorflow==1.14 (from versions: 2.2.0rc3, 2.2.0rc4, 2.2.0, 2.3.0rc0, 2.3.0rc1, 2.3.0rc2) ERROR: No matching distribution found for tensorflow==1.14 I also tried making a new virtual env and tried the following commands but it didn't work. Is there any way to install Tensorflow 1?

Answer: What I've found on discourse: You just need to make sure you’re using Python 3.5, 3.6 or 3.7. TensorFlow 1.15 does not support Python 3.8
Question: I have to sum 2 arrays with broadcasting. This is the first: a = [0 1 2 3]  And this is the second: A = [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]]  This is the code I had tried until now: a = np.array(a) A = np.array(A) G = a + A print(G)  But when I run, it throws this error: ValueError: operands could not be broadcast together with shapes (4,) (4,6) How to solve it?

Answer: Arrays need to have compatible shapes and same number of dimensions when performing a mathematical operation. That is, you can't add two arrays of shape (4,) and (4, 6), but you can add arrays of shape (4, 1) and (4, 6). You can add that extra dimension as follows: a = np.array(a) a = np.expand_dims(a, axis=-1) # Add an extra dimension in the last axis. A = np.array(A) G = a + A  Upon doing this and broadcasting, a will practically become [[0 0 0 0 0 0] [1 1 1 1 1 1] [2 2 2 2 2 2] [3 3 3 3 3 3]]  for the purpose of addition (the actual value of a won't change, a will still be [[0] [1] [2] [3]]; the above is the array A will be added to).
Question: I'm using python 3.8.2 on ubuntu on windows 10. I'm reading an OOP pdf and I'm at the third-party libraries section. It says that pip doesn't come with python, but python 3.4 contains a useful tool called ensurepip, which will install it: python -m ensurepip. But when I press enter, it says no module named ensurepip /usr/bin/python3: No module named ensurepip  So I thought that I already have pip so I tried to install pygame with pip but it says there's no module named pip. What am I doing wrong? Thanks.

Answer: The module ensurepip is part of Python's standard library. It should be there. You say you're on Windows, but then you show /usr/bin/python3 in your question, which is obviously not a Windows path (rather Linux). My assumption is that you might be using WSL (or WSL2), which is actually Linux running on Windows (without going into details). By default WSL runs a Ubuntu distribution. This distribution (and other Debian-related distributions) typically breaks up Python and its standard library in multiple pieces. So you might need to install an additional system package, I believe it could be the python3-venv system package that contains the Python ensurepip module in your case: sudo apt-get install python3-venv
Question: I understand (as explained in this question and the docs) that a type hint of X or Y can be expressed as: Union[X,Y]  But how does one express a type hint of X and Y? This would be useful when expressing that the object in question must be a subclass of both X and Y. The following example works as long as all classes that inherit both X and Y are known in advance: class X: pass class Y: pass class A(X,Y): pass class B(X,Y): pass def some_function(arg: Union[A,B]): pass # do stuff with arg that only depends on inherited members from X and Y  But what if another package which depends on the code above defines: class C(X,Y): pass  C also will work in some_function by design. I am looking for a better type hint to use instead of Union[X,Y] that includes any possible class that sub-classes both X and Y. I understand a workaround could be to define: class XY(X,Y): pass  And then use it as a base class and type hint: class A(XY): pass class B(XY): pass class C(XY): pass def some_function(arg: XY): pass  But I am not sure if it would be worthwhile to define a new class only for type hint, which doesn't effect runtime anyway. How do we create a type hint for any class that is a subclass of both X and Y?

Answer: Python type hints does not support explicit intersection annotation. But you have at least two workarounds: You could introduce a mix class, e.g: class A: def foo_a(self): pass class B: def foo_b(self): pass class Mix(A, B): pass def foo(p: Mix) -> None: p.foo_a() p.foo_b()  Or use structural subtyping, Protocol, e.g.: from typing import Protocol class Supports_ab(Protocol): def a(self): pass def b(self): pass class A: def a(self): pass class B: def b(self): pass class Derived(A, B): pass class SomeClassAB: # no need superclass def a(self): pass def b(self): pass def foo(p: Supports_ab) -> None: p.a() p.b() foo(SomeClassAB()) foo(Derived())
Question: I am using django-ckeditor for my project to upload image along with text content. I used body = RichTextUploadingField(blank=True, null=True) in model. Now I want to restrict the user to upload large size images in content or larger than predefined maximum height/width. I want the uploaded images in content of particular height/weight and size like less then 1mb. How can I predefine maximum image height and width as well as maximum image size limit? Is there any way to define it from django-ckeditor configuration? Or How to resize the uploaded images from content at backend after user submitted the form? Here is my models.py: class Post(models.Model): STATUS_CHOICES = { ('draft', 'Draft'), ('published', 'Published'), } title = models.CharField(max_length=250) slug = models.SlugField(max_length=250, unique=True) author = models.ForeignKey(User, on_delete=models.CASCADE) body = RichTextUploadingField(blank=True, null=True) status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')  I tried a lot to solve it but failed.Any suggetion to solve the issue? Thanks in advance.

Answer: You can set CKEDITOR_THUMBNAIL_SIZE in setting.py CKEDITOR_THUMBNAIL_SIZE = (500, 500)  With the pillow backend, you can change the thumbnail size with the CKEDITOR_THUMBNAIL_SIZE setting (formerly THUMBNAIL_SIZE). Default value: (75, 75) With the pillow backend, you can convert and compress the uploaded images to jpeg, to save disk space. Set the CKEDITOR_FORCE_JPEG_COMPRESSION setting to True (default False) You can change the CKEDITOR_IMAGE_QUALITY setting (formerly IMAGE_QUALITY), which is passed to Pillow:  The image quality, on a scale from 1 (worst) to 95 (best). The default is 75. Values above 95 should be avoided; 100 disables portions of the JPEG compression algorithm and results in large files with hardly any gain in image quality.  This feature is disabled for animated images. check official doc. https://github.com/django-ckeditor/django-ckeditor/blob/master/README.rst
Question: My saved state_dict does not contain all the layers that are in my model. How can I ignore the Missing key(s) in state_dict error and initialize the remaining weights?

Answer: This can be achieved by passing strict=False to load_state_dict. load_state_dict(state_dict, strict=False)  Documentation
Question: I'm trying to take certain roles when bot starting. First i need to take guild but i couldn't achieve it. takenGuild = client.get_guild(myServerID)  takenGuild returning None When i try to loop guilds for guild in client.guilds: print(guild.id)  it's not printing anything. My whole code : import discord from discord.ext import commands, tasks from discord.utils import get client = commands.Bot(command_prefix = '.') takenGuild = client.get_guild(123123123123123123) print(takenGuild.id) for guild in client.guilds: print(guild) print(guild.id) client.run('Token') 

Answer: You have to wait for the bot to be ready you can use this. FYI bot is more common now after the updates than client import discord from discord.ext import commands, tasks from discord.utils import get bot = commands.Bot(command_prefix = '.') @bot.event async def on_ready(): print("I am running on " + bot.user.name) print("With the ID: " + bot.user.id) print('Bot is ready to be used') # after it is ready do it takenGuild = bot.get_guild(123123123123123123) print(takenGuild.id) for guild in bot.guilds: print(guild) print(guild.id) bot.run('Token')
Question: I'm serving React app from FastAPI by mounting app.mount("/static", StaticFiles(directory="static"), name="static") @app.route('/session') async def renderReactApp(request: Request): return templates.TemplateResponse("index.html", {"request": request})  by this React app get served and React routing also works fine at client side but as soon as client reloads on a route which is not defined on server but used in React app FastAPI return not found to fix this I did something as below.  @app.route('/network') @app.route('/gat') @app.route('/session')   async def renderReactApp(request: Request): return templates.TemplateResponse("index.html", {"request": request})  but it seems weird and wrong to me as I need to add every route at the back-end as well as at frontend. I'm sure there must be something like Flask @flask_app.add_url_rule('/<path:path>', 'index', index) in FastAPI which will server all arbitrary path

Answer: Since FastAPI is based on Starlette, you can use what they call "converters" with your route parameters, using type path in this case, which "returns the rest of the path, including any additional / characers." See https://www.starlette.io/routing/#path-parameters for reference. If your react (or vue or ...) app is using a base path, you can do something like this, which assigns anything after /my-app/ to the rest_of_path variable: @app.get("/my-app/{rest_of_path:path}") async def serve_my_app(request: Request, rest_of_path: str): print("rest_of_path: "+rest_of_path) return templates.TemplateResponse("index.html", {"request": request})  If you are not using a unique base path like /my-app/ (which seems to be your use case), you can still accomplish this with a catch-all route, which should go after any other routes so that it doesn't overwrite them: @app.route("/{full_path:path}") async def catch_all(request: Request, full_path: str): print("full_path: "+full_path) return templates.TemplateResponse("index.html", {"request": request})  (In fact you would want to use this catch-all regardless in order to catch the difference between requests for /my-app/ and /my-app)
Question: I'm trying to work with the openai gym module but I get this error: >>> import atari_py Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\ssit5\AppData\Local\Programs\Python\Python38\lib\site-packages\atari_py\__init__.py", line 1, in <module> from .ale_python_interface import * File "C:\Users\ssit5\AppData\Local\Programs\Python\Python38\lib\site-packages\atari_py\ale_python_interface.py", line 17, in <module> ale_lib = cdll.LoadLibrary(os.path.join(os.path.dirname(__file__), File "C:\Users\ssit5\AppData\Local\Programs\Python\Python38\lib\ctypes\__init__.py", line 451, in LoadLibrary return self._dlltype(name) File "C:\Users\ssit5\AppData\Local\Programs\Python\Python38\lib\ctypes\__init__.py", line 373, in __init__ self._handle = _dlopen(self._name, mode) FileNotFoundError: Could not find module 'C:\Users\ssit5\AppData\Local\Programs\Python\Python38\lib\site-packages\atari_py\ale_interface\ale_c.dll' (or one of its dependencies). Try using the full path with constructor syntax.  I don't have an ale_c.dll and tried finding solutions but nothing worked. I followed the solution here https://github.com/openai/gym/issues/1726 but when trying to import atari_py it comes up with the same error. I don't see why the __init__ would search for something that didn't come with the module either. There were other StackOverflow questions that I looked at but they also yielded no results. The only solution I can think of is to get a copy of ale_c.dll but I don't know how I would get it.

Answer: I was facing the same error. Fortunately, I was able to find one workaround. Follow this steps and you should be good to go.  Download ale_c.dll from here. Copy it in C:\Users\Deep Raval\AppData\Local\Programs\Python\Python38\Lib\site-packages\atari_py\ale_interface (Your path can be different).
Question: I wanted to work on a small project to challenge my computer vision and image processing skills. I came across a project where I want to remove the hidden marks from the image. Hidden here refers to the watermarks that are not easily visible in rgb space but when you convert into hsv or some other space the marks become visible. Here's one example: BGR SPACE:  HSV SPACE:  I've tried different ways but was able to implement a solution that would remove those watermarks from the image. I am posting this question here to get different ideas to tackle this problem. What I have tried: I have tried various approaches but none of them worked, sharing the code might not help. It is not necessary to provide code for it, a pseudo code, idea or any lead would be appreciated.  I noticed that the hidden marks are all the colors similar to RGB(90,94,105). And when I showed R, G, and B separately I noticed that the watermarks were only visible in B channel. I thought that if adjust/remove the marks in B channel and merge the image again, may be I could get better results.  Code: b,g,r = cv2.split(img) b = b//2; r = cv2.merge((r,g,b)) cv2.imshow("image",r)  Problems: This doesn't does solve the problem, it did make the colors little dimmer but the image colors were also disturbed.  I tried playing around with B channel to see if could accomplish something.  I also noticed that if we convert the image to LUV space then the marks are visible in V space.  

Answer: I didn't find any answer that completely solved the question. I appreciate everyone's effort though (Thank you). I did something on my own and would like to share. It results in little quality loss (a little bluish blurriness) but successfully removes the watermarks. The solution is very simple but took time to analyze the image. I WOULD BE VERY GLAD IF SOMEONE CAN EXTEND THIS APPROACH AND COME UP WITH SOMETHING EVEN BETTER I observed that the watermarks were only visible in B space (out of RGB) and there were no traces of watermarks in R and G space. B space:  I also red somewhere that blue light contributes little to the overall image compared to R and G channel so here's what I decided to do. Blur the B channel by a large enough amount to remove traces of those patterns. Here's how the B channel would appear afterwards:  Finally, merge the image with the new B channel, previous R and previous G channel. Here's how the RGB channel would appear afterwards:  The advantage of using approach is that the traces are gone. The only disadvantage is that the bluish and purplish colors appear at the black edges and the image is a little bluish in general. My Code: import cv2 from matplotlib import pyplot as plt import numpy as np img = cv2.imread("img.png") b, g, r = cv2.split(img) # split into B,G,R spaces b = cv2.GaussianBlur(b, None, 8) plt.imshow(cv2.merge((r,g,b)), cmap='gray')
Question: Im new in this Pandas and Matplotlib, I follow an example from a book and apparently it give me a warning "MatplotlibDeprecationWarning: The epoch2num function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. base = dates.epoch2num(dt.asi8 / 1.0e9)" and the X value of axis change from years to some random numbers import matplotlib.pyplot as plt from pandas_datareader import data AMZ = data.DataReader('AMZN', start='2011', end='2018', data_source='yahoo') AMZ = AMZ['Close'] AMZ.plot() AMZ.resample('BA').mean().plot(style=':') AMZ.asfreq('BA').plot(style='--') plt.show()  

Answer: This was caused by a temporary bad interaction between Matplotlib and Pandas and is fixed in both projects. To work around until the new versions are available: plt.rcParams['date.epoch'] = '0000-12-31'
Question: I have 2 projects structured as below: /abc-lib / abc / __init__.py / main.py / pyproject.toml /abc-web-api / src / __init__.py / main.py / pyproject.toml  I attempted to include abc-lib as a dependency in abc-web-api, thus having a abc-web-api/pyproject.toml as below: [tool.poetry] name = "abc-web-api" version = "0.0.1" description = "Some description." authors = ["Someone <someone@example.com>"] repository = "https://github.com/someone/abc-web-api" readme = "README.md" [tool.poetry.scripts] serve = "src.main:app" [tool.poetry.dependencies] python = "~3.6.8" abc-lib = { path="../abc-lib" } [tool.poetry.dev-dependencies] pytest = "^3.10.1" yapf = "^0.30.0" flake8 = "^3.8.3" [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api"  When I execute poetry install, I receive the following message: Package operations: 1 installs, 0 updates, 0 removals - Installing abc-lib (1.0.0 ../abc-lib) [ModuleOrPackageNotFound] No file/folder found for package abc-lib  The version number shown in the "Installing" statement is correct, so I am quite confused about the meaning of [ModuleOrPackageNotFound]. Does anyone know how can I resolve it? Thanks

Answer: Your folder structure looks a bit weird. It looks like your prefer the "src" variant. So I would suggest the following: ./ ├── abc-lib │ ├── pyproject.toml │ └── src │ └── abc_lib │ ├── __init__.py │ └── main.py └── abc-web-api ├── pyproject.toml └── src └── abc_web_api ├── __init__.py └── main.py  With this pyproject.toml in abc-lib: [tool.poetry] name = "abc-lib" version = "0.1.0" description = "" authors = ["Someone <someone@example.com>"] [tool.poetry.dependencies] python = "^3.6" [tool.poetry.dev-dependencies] [build-system] requires = ["poetry>=1.0"] build-backend = "poetry.masonry.api"  And this in abc-web-api: [tool.poetry] name = "abc-web-api" version = "0.1.0" description = "" authors = ["Someone <someone@example.com>"] [tool.poetry.dependencies] python = "^3.6" abc-lib = {path = "../abc-lib"} [tool.poetry.dev-dependencies] [build-system] requires = ["poetry>=1.0"] build-backend = "poetry.masonry.api"