eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have two dictionaries One: default = {"val1": 10, "val2": 20, "val3": 30, "val4": 40}  Two: parsed = {"val1": 60, "val2": 50}  Now, I want to combine these two dictionaries in such a way that values for the keys present in both the dictionaries are taken from the parsed dictionary and for rest of the keys in default and their values are put in to the new dictionary. For the above given dictionary the newly created dictionary would be, updated = {"val1": 60, "val2": 50, "val3": 30, "val4": 40}  The obvious way to code up this would be to loop through the keys in default and check if that is present in parsed and put then into a new list updated, and in the else clause of the same check we can use values from default. I am not sure if this is a pythonic way to do it or a much cleaner method. Could someone help on this?

Answer: You can create a new dict with {**dict1,**dict2} where dict2 is the one that should have priority in terms of values >>> updated = {**default, **parsed} >>> updated {'val1': 60, 'val2': 50, 'val3': 30, 'val4': 40}
Question: When trying to create new python 3.9 Virtualenv Environment in Pycharm I got such error AttributeError: 'HTMLParser' object has no attribute 'unescape' Traceback (most recent call last): File "/var/folders/6g/vnvmvlf51gv49m22rzj9zdtw0000gn/T/tmpifdsjw6lpycharm-management/setuptools-40.8.0/setup.py", line 11, in <module> import setuptools File "/private/var/folders/6g/vnvmvlf51gv49m22rzj9zdtw0000gn/T/tmpifdsjw6lpycharm-management/setuptools-40.8.0/setuptools/__init__.py", line 20, in <module> from setuptools.dist import Distribution, Feature File "/private/var/folders/6g/vnvmvlf51gv49m22rzj9zdtw0000gn/T/tmpifdsjw6lpycharm-management/setuptools-40.8.0/setuptools/dist.py", line 35, in <module> from setuptools.depends import Require File "/private/var/folders/6g/vnvmvlf51gv49m22rzj9zdtw0000gn/T/tmpifdsjw6lpycharm-management/setuptools-40.8.0/setuptools/depends.py", line 7, in <module> from .py33compat import Bytecode File "/private/var/folders/6g/vnvmvlf51gv49m22rzj9zdtw0000gn/T/tmpifdsjw6lpycharm-management/setuptools-40.8.0/setuptools/py33compat.py", line 55, in <module> unescape = getattr(html, 'unescape', html_parser.HTMLParser().unescape) AttributeError: 'HTMLParser' object has no attribute 'unescape'  What can be done with it?

Answer: Based on Matthias comment To fix this error I have to update both pycharm (>=2020) and setuptools (>=41). Hope this will help somebody
Question: I add a JSONField for one of my models. I want to create an instance of that model in admin panel but that JSONField returns a validation error (Enter a valid JSON.) how I can fix this?? model: class Product(models.Model): category = models.ManyToManyField(Category, related_name='products') name = models.CharField(max_length=500) slug = models.SlugField(max_length=500, allow_unicode=True, unique=True) image_1 = models.ImageField(upload_to='products_pic/%Y/%m/%d/', null=True, blank=True) description = models.TextField(null=True, blank=True) attribute = models.JSONField(default={}) price = models.PositiveIntegerField() discount = models.PositiveIntegerField(null=True, blank=True) available = models.BooleanField(default=True) popular = models.BooleanField(default=False) created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) comments = GenericRelation(Comment) class Meta: ordering = ('-created',) def __str__(self): return self.name  I add products in admin panel. I want to have multiple attributes for my products like color , size and ... . is it true to use json fiels for that?? this is the error:

Answer: This is not valid JSON. The strings in JSON are wrapped with double quotes. Indeed, you can verify this for example with JSONLint.com. It is a valid Python dictionary, but not valid JSON. You thus should enter: { "key": "value" } For more information, see the JSON specifications.  I add products in admin panel. I want to have multiple attributes for my products like color , size and ... . is it true to use json fiels for that??  If the fields are fixed (are attributes of all Products), then you should use individual fields, since that is more convenient to fielter, make forms, etc. If the data is more dynamic, it is often better to make use of a JSON field.
Question: Running JupyterLab version 3.0.0 and would like to enable code folding (collapse classes, functions etc in Python). I have followed the instructions in this Jupyter Lab github post: Under Settings / Text Editor, I have these User preferences (right pane): { "editorConfig": { "lineNumbers": true, "codeFolding": true } }  And I do not see any triangles appearing on the left side of the cells. What else do I need to do in order to enable code folding? For abundance of clarity, this question pertains to JupyterLab specifically.

Answer: Those instructions are specifically for the text editor, not the notebook interface. Try this instead, in Settings / Notebook: { "codeCellConfig": { "codeFolding": true } }  If you want to enable code folding for Markdown or raw cells, see markdownCellConfig and rawCellConfig respectively.
Question: I apologise if I'm missing anything obvious; is there a way to see if a value is in an enum which returns True if it is, False if not? For example, if I take the following enum from the python documentation, from enum import Enum class Colour(Enum): RED = 1 GREEN = 2 BLUE = 3  is there any way to do the following action, or an equivalent, without an exception being raised: colour_test = "YELLOW" if Colour[colour_test]: print("In enum") else: print("Not in enum") ## Output wanted - "Not in enum" ## Actual output - KeyError: "YELLOW"  I know that I can use try;except statements, but I'd prefer not to in this situation as I'd like to use this conditional with some others.

Answer: Enums have a __members__ dict that you can check: if colour_test in Colour.__members__: print("In enum") else: print("Not in enum")  You can alternatively use a generalized approach with hasattr, but this returns wrong results for some non-members like "__new__": if hasattr(Colour, colour_test): print("In enum") else: print("Not in enum")
Question: How does the following expression work in python? >>> 1 ++++++++++++++++++++ 1 2 >>> 1 ++++++++++++++++++++-+ 1 0  I thought this would raise a SyntaxError but that was not the case.

Answer: You have to use the logic of brackets and arithmetic operations for this kind of calculation. 1--2 becomes, 1-(-(2)) = 1-(-2) = 1+2 = 3  1+++1 becomes, 1+(+(+1)) = 2  1++-1 becomes, 1+(+(-1)) = 0
Question: I'm writing a cross-platform Python application that acts as a frontend for DOSBox. It needs to call the DOSBox executable with a number of command line arguments. I don't want to hardcode a specific path to DOSBox because it might depend on where the user has installed it. On Linux, I can simply do: import subprocess subprocess.run(['dosbox'] + args)  On macOS, however, I currently use the following code: import subprocess subprocess.run(['/Applications/dosbox.app/Contents/MacOS/DOSBox'] + args)  Which seems awfully specific and I'm not even sure whether it works, since I don't have a mac to test on. What is the correct way to open an application by name on macOS? (NB: I have also asked this sibling question for Windows.)

Answer: I don't know DOSBox or want it on my Mac, but in general, when you install an application on macOS it has a "property list" file, or plist or "info.plist" in it. In there, the developer is supposed to put a "bundle identifier" key called CFBundleIdentifier. This must be unique across all applications, so for DOSBox it should be something like: <key>CFBundleIdentifier</key> <string>com.dosboxinc.dosbox</string>  Get one of your users to find that, then you can use the bundle identifier to open it like this regardless of installation location: open -b BUNDLEIDENTIFIER --args arg1 arg2 arg3  where arg1, arg2 and arg3 get passed on to DOSBox.  You may be able to get the bundle identifier by running this in Terminal: osascript -e 'id of app "DOSBox"'  Note, however, that if this command works, it means I have correctly guessed the app name "DOSBox", which means that you could just use the app name with open, rather than the bundle identifier like this: open -a DOSBox --args arg1 arg2 arg3
Question: I have a list of dictionary. I wish to have only one result for each unique api and the result need to show according to priority: 0, 1, 2. May I know how should I work on it? Data: [ {'api':'test1', 'result': 0}, {'api':'test2', 'result': 1}, {'api':'test3', 'result': 2}, {'api':'test3', 'result': 0}, {'api':'test3', 'result': 1}, ]  Expected output: [ {'api':'test1', 'result': 0}, {'api':'test2', 'result': 1}, {'api':'test3', 'result': 0}, ] 

Answer: data = [ {'api': 'test1', 'result': 0}, {'api': 'test3', 'result': 2}, {'api': 'test2', 'result': 1}, {'api': 'test3', 'result': 1}, {'api': 'test3', 'result': 0} ] def find(data): step1 = sorted(data, key=lambda k: k['result']) print('step1', step1) step2 = {} for each in step1: if each['api'] not in step2: step2[each['api']] = each print('step2', step2) step3 = list(step2.values()) print('step3', step3) print('\n') return step3 find(data)  Try this, it will give you step1 [{'api': 'test1', 'result': 0}, {'api': 'test3', 'result': 0}, {'api': 'test2', 'result': 1}, {'api': 'test3', 'result': 1}, {'api': 'test3', 'result': 2}] step2 {'test1': {'api': 'test1', 'result': 0}, 'test3': {'api': 'test3', 'result': 0}, 'test2': {'api': 'test2', 'result': 1}} step3 [{'api': 'test1', 'result': 0}, {'api': 'test3', 'result': 0}, {'api': 'test2', 'result': 1}]  Sort all first, then find first for each "api", and there goes your result.
Question: I'm using the logging module from the python standard library and would like to obtain the current Formatter. The reason is that I'm using multiprocessing module and for each process I'd like to assign its logger another file handler to log to its own log file. When I do this in the following way logger = logging.getLogger('subprocess') log_path = 'log.txt' with open(log_path, 'a') as outfile: handler = logging.StreamHandler(outfile) logger.addHandler(handler)  the messages in log.txt have no formatting at all, but I would like the message to be in the same format as my typical logging format. My typical logging setup is shown below  logging.basicConfig( format='%(asctime)s | %(levelname)s | %(name)s - %(process)d | %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=os.environ.get('LOGLEVEL', 'INFO').upper(), ) logger = logging.getLogger(__name__)  Since it looks like the Formatter object is associated with the Handler object, I tried to obtain handler from the main Logger object which has the correct formatting. So I called logger.handlers  but I got an empty list []. So my question is, where do I get the Formatter object which has the same format that my main logger has? For the record, I'm using python 3.8 on macOS but the code will be deployed to Linux (but still python 3.8).

Answer: Judging by the cpython source code on logging.basicConfig, it appears that the formatter object which contains your formatting string is eventually added to a handler that is passed to the root logger (see: here ). So you can obtain the handler (and therefore the formatter) from the root logger object by doing logging.root.handlers[0].formatter  In particular, to achieve the subprocess logging handler in the question, you can do this instead  handler = logging.StreamHandler(outfile) handler.setFormatter(logging.root.handlers[0].formatter) handler.setLevel(logging.root.level) logger.addHandler(handler)  which will set your handler to the same logging format (and level as well!) as that of your usual logger object.
Question: Is there any difference between add_trace and append_trace in Plotly? Is the latter a legacy of the former? In the Plotly.py GitHub, there are 88 markdown + 21 Python instances of add_trace and 9 markdowbn + 7 Python instances of append_trace. The latter are mainly coming from doc and packages/python/plotly/plotly/figure_factory. In the Plotly subplots documentation, there are 4 instances of append_trace while all other 52 instances are add_trace. Here is an example extracted from there: from plotly.subplots import make_subplots import plotly.graph_objects as go fig = make_subplots(rows=3, cols=1) fig.append_trace(go.Scatter( x=[3, 4, 5], y=[1000, 1100, 1200], ), row=1, col=1) fig.append_trace(go.Scatter( x=[2, 3, 4], y=[100, 110, 120], ), row=2, col=1) fig.append_trace(go.Scatter( x=[0, 1, 2], y=[10, 11, 12] ), row=3, col=1) fig.update_layout(height=600, width=600, title_text="Stacked Subplots") fig.show()  I have tried replacing the append_trace instances in this code snippets to add_trace and did not observe any apparent differences.

Answer: I don't have the technical background to explain it to you, but the official reference has the following explanation  New traces can be added to a graph object figure using the add_trace() method. This method accepts a graph object trace (an instance of go.Scatter, go.Bar, etc.) and adds it to the figure. This allows you to start with an empty figure, and add traces to it sequentially. The append_trace() method does the same thing, although it does not return the figure.
Question: While Django documentation https://docs.djangoproject.com/en/3.1/ref/models/options/#managed mentions the use of managed = False field in meta is used to not create migrations I am still getting migrations when I call makemigrations. This is the meta of the model: class FieldOpsBooking(models.Model): . . class Meta: managed = False db_table = 'field_ops_booking'  And I am getting this after makemigrations python manage.py makemigrations Migrations for 'user_analysis': user_analysis/migrations/0001_initial.py - Create model FieldOpsBooking - Create model RewardManagementLeads Migrations for 'od_engagement': od_engagement/migrations/0001_initial.py - Create model NormalisedTonnage  And it creates 0001_initial.py file with all migrations to apply as well. Any help is appreciated

Answer: I checked my own projetcs with models having managed=False: YES there is an entry in migrations file like:  operations = [ migrations.CreateModel( name='xyz', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ], options={ 'db_table': 'xyz_table', 'managed': False, }, ),  BUT: this will not create a new table on database level when you execute "makemigration". Sorry if a insiste but your original solution was absolutely correct!! this is from django documentation about abtract base class: " ... since it is an abstract base class. .... and cannot be instantiated or saved directly."
Question: I'm new to python programming. I have tried a lot to avoid these nested for loops, but no success. My data input like: [ { "province_id": "1", "name": "HCM", "districts": [ { "district_id": "1", "name": "Thu Duc", "wards": [ { "ward_id": "1", "name": "Linh Trung" }, { "ward_id": "2", "name": "Linh Chieu" } ] }, { "district_id": "2", "name": "Quan 9", "wards": [ { "ward_id": "3", "name": "Hiep Phu" }, { "ward_id": "4", "name": "Long Binh" } ] } ] }, { "province_id": "2", "name": "Binh Duong", "districts": [ { "district_id": "3", "name": "Di An", "wards": [ { "ward_id": "5", "name": "Dong Hoa" }, { "ward_id": "6", "name": "Di An" } ] }, { "district_id": "4", "name": "Thu Dau Mot", "wards": [ { "ward_id": "7", "name": "Hiep Thanh" }, { "ward_id": "8", "name": "Hiep An" } ] } ] } ]  And my code is: for province in data: for district in province['districts']: for ward in district['wards']: # Excute my function print('{}, {}, {}'.format(ward['name'], district['name'], province['name']))  Output Linh Trung, Thu Duc, HCM Linh Chieu, Thu Duc, HCM Hiep Phu, Quan 9, HCM ...  Even though my code is working it looks pretty ugly. How can I avoid these nested for loops?

Answer: Your data structure is naturally nested, but one option you have for neatening your code is to write a generator function for iterating over it: def all_wards(data): for province in data: for district in province['districts']: for ward in district['wards']: yield province, district, ward  This function has the same triply-nested loop in it as you currently have, but everywhere else in your code, you can now iterate over the data structure with a single non-nested loop: for province, district, ward in all_wards(data): print('{}, {}, {}'.format(ward['name'], district['name'], province['name']))  If you prefer to avoid having too much indentation, here's an equivalent way to write the function, similar to @adarian's answer but without creating a temporary list: def all_wards(data): return ( province, district, ward for province in data for district in province['districts'] for ward in district['wards'] )
Question: I've started learning playwright-python and the package playwright has the two submodules async_api and sync_api. However I could not find any deeper description or discussion on their respective benefits and drawbacks. From their names I assume that the synchronous API calls are blocking and the asynchronous ones run in the background? Are they different in their capabilities, i.e. are there scenarios in which the sync_api cannot accomplish something you can do using the async_api (or vice versa)?

Answer: The sync_api is simply a wrapper around the async_api that abstracts asyncio usage away from you. As such, the capabilities are largely the same, but the async_api may afford some more flexibility in complex scenarios. I would suggest using async in case you need the flexibility in the future, or sync for ease of use.
Question: I'm listing registered users on a ListView page and I'm trying to show if user is superuser or not. My main user is created with python manage.py createsuperuser command and I'm sure it is a superuser beacuse I've checked from admin panel too. When I try to print if it is superuser or not my code always shows a False output. Here are my codes: views.py @method_decorator(staff_member_required, name='dispatch') class Uyeler(ListView): model = User paginate_by = 40 ordering = ['-pk'] template_name = "panel/uyeler.html"  and in template file: {% for obj in object_list %} {% if obj.is_superuser %} SuperUser {% else %} Not SuperUser {{ obj.is_superuser }} {% endif %} {% endfor %}  And my html output is "Not SuperUser False" for all users including my superuser account. Any ideas?

Answer: I tried in my code and it's working maybe there's issue in your data this is how my code looks like views.py def user_detail(request): user_detail = CustomUser.objects.filter(id=id) return(request, 'user_datail.html', {'user_detail': user_detail})  user_datail.html {% for i in user_detail %} {% if i.is_superuser %} <td class="text-center"><span class="btn btn-success">You</span> </td> {% else %} <td class="text-center"><span class="btn btn-info">Agent</span> </td> {% endif %} {% endfor %}  output
Question: How to transform vectors of labels to one-hot encoding and back in Pytorch? The solution to the question was copied to here after having to go through the entire forum discussion, instead of just finding an easy one from googling.

Answer: From the Pytorch forums import torch import numpy as np labels = torch.randint(0, 10, (10,)) # labels --> one-hot one_hot = torch.nn.functional.one_hot(labels) # one-hot --> labels labels_again = torch.argmax(one_hot, dim=1) np.testing.assert_equals(labels.numpy(), labels_again.numpy())
Question: I'm getting this error while loading the tensorflow addons library import tensorflow_addons as tfa ImportError: cannot import name 'keras_tensor' from 'tensorflow.python.keras.engine' 

Answer: This error is because you have incompatibility issues between your TensorFlow, Python and tensorflow-addons. Uninstall the tensorflow-addons and install the version based on the table below. Refer the Github repo for more information.
Question: Program to convert a video file into a NumPy array and vice-versa. I had searched for many search engines but was unable to find the answer.

Answer: There are multiple libraries people use for this (i.e. PyAV, decord, opencv); I personally use Python OpenCV for this a lot (mostly with PyTorch, but it's a similar principle), so I'll speak about my experience there. You can use cv2.VideoCapture to load a video file into a numpy array; in theory, you can also use cv2.VideoWriter to write it back, but in practice, I've had a hard time getting that to work in my own projects. Video to Numpy Array tl;dr: Create a cv2.VideoCapture wrapper; iteratively load images (i.e. frames) from the video. frames = [] path = "/path/to/my/video/file.mp4" cap = cv2.VideoCapture(path) ret = True while ret: ret, img = cap.read() # read one frame from the 'capture' object; img is (H, W, C) if ret: frames.append(img) video = np.stack(frames, axis=0) # dimensions (T, H, W, C)  Do note that the images will be returned in BGR channel format rather than the more common RGB; if you need to convert it to the RGB colorspace, img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) will be sufficient. Numpy Array to Video In theory, the examples I've seen for using cv2.VideoWriter go something like # let `video` be an array with dimensionality (T, H, W, C) num_frames, height, width, _ = video.shape filename = "/path/where/video/will/be/saved.mp4" codec_id = "mp4v" # ID for a video codec. fourcc = cv2.VideoWriter_fourcc(*codec_id) out = cv2.VideoWriter(filename, fourcc=fourcc, fps=20, frameSize=(width, height)) for frame in np.split(video, num_frames, axis=0): out.write(frame)  You can also save the frames to temporary images (there exist many np.ndarray -> image pipelines; I personally use Pillow), then use ffmpeg (a command-line utility) to encode the frames into a video file. This takes up significantly more space though, and I use this method when I need to inspect the individual frames of my video array (in that case, I use ffmpeg, but that's a different conversation). And on another note -- you may want to change the codec_id variable depending on how you want to encode the video (if this means nothing to you, don't worry -- it probably won't matter for your application); this is simply a four-byte code used to identify the video codec used to generate the video (see this page; availability may vary by platform(. H.264 is the most common one in use today AFAIK, which is given by code "H264" or "X264", but I've had trouble getting this to work with OpenCV (more details here); however, the array -> images -> video file approach works seamlessly with ffmpeg from the command line.