Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
In my attempt to create a class dots with the fields n and xy as shown below:
from dataclasses import dataclass
#dataclass
class dots:
n: int = 200
xy: List[int] = field(default_factory=list)
I am constantly getting the error :
NameError: name 'field' is not defined
Any ideas on how to fix it?
My operating system is Ubuntu 18.04.3 LTS, and the kernel version 4.15.0-58-generic. I am using Python 3.6.4
You need to import field() to use it in your code:
from dataclasses import dataclass, field
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am making a podcast application where I want to display channel icons. That's where I asked myself : How do I properly display a picture in PyQt5 ? I read about this:
label = QLabel()
pixmap = QPixmap('image.png')
label.setPixmap(pixmap)
It's often done that way, but I feel a bit, well, uneasy, about it. It feels hackish to me even though there is an extra method to do it like this. Is there any docs passage where the above way is defined as the official standard one ? If not, is there a better way ? Something like
lay = QVBoxLayout()
image = SomeWidget("image.png")
lay.addWidget(image)
All Qt subclasses of QObject (which includes all subclasses of QWidget, since it inherits from QObject) allow setting their Qt properties in the constructor as keyword arguments.
Since pixmap is a property of QLabel, you can create one directly with an image set with the following line:
image = QLabel(pixmap=QPixmap('image.png'))
If you are so bothered about that, then just create a convenience function or class:
def ImageWidget(path):
return QLabel(pixmap=QPixmap(path))
# or, alternatively
class ImageWidget(QLabel):
def __init__(self, path):
super().__init__(pixmap=QPixmap(path))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am new to python and I am using idle 3.7.4. I want to automate command line to input() in python.
For example:
a = input("Please enter your name : ")
I want to add my name without manually typing it. I read about run() method under subprocess module, but I am not sure how to implement it.
Any helps are highly appreciated. Thanks.
you can use the mock library to do this with the builtins. For example, like this:
import mock
import builtins
def input_test():
ans = input("Please input your name :")
return f"your name is {ans}"
def test_input():
with mock.patch.object(builtins, 'input', lambda _: "okay"):
assert input_test() == 'your name is okay'
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a folder that contains zip files in subfolders. I want to unzip all the files using this python code. code shows no error but the files are not extracted can't figure out the problem. Thanks in Advance.
from zipfile import ZipFile
from pathlib import Path
entries = Path('E:\\Bootcamp')
for entry in entries.rglob('*.zip'):
with ZipFile(entry, 'r') as zip:
print('Check1')
zip.extractall()
print('check2')
The extracted files will be located in the folder where your python file has been saved
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
AttributeError: 'list' object has no attribute 'shape'
I am trying to make the code for row and column for given array.
and the top line is my error, how can it be fixed?
def comuteFinalGrades(grades):
a=[]
N, M = grades.shape
You have to convert the list into numpy array and then You can use the shape attribute.
import numpy as np
def comuteFinalGrades(grades):
grades=np.array(grades)
a=[]
N, M = grades.shape
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Could someone please help me with the below requirement. I am using below version of linux os :-
Red Hat Enterprise Linux Server release 6.6 (Santiago)
Python version :2.6.6
I need to send a multiple log files to an user everyday as an attachment.
In my log directory i have multiple files with *.fix extension. I need to send all these files to user as attachment. Could you please let me know the code for it ?
FYI .. its a linux server and i am not gonna use gmail.
Appreciate your earliest help. Thanks !!
There is a python package called email that helps you in sending mails.
Getting the list of *.fix files could be done using glob.
Something like this should do it:
from glob import glob
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart()
# Fill out the needed properties of msg, like from, to, etc.
for filename in glob("*.fix"):
fp = open(filename)
msg.attach(MIMEText(fp.read()))
fp.close()
...
The msgcan then be sent using smtplib as shown here