How to update text of the document file in the queryset? - python

def corrections_fn(request, file, selected_words, corrections):
parser = GingerIt()
user = request.user
document = Document(file.file)
text = ''
for line in document.paragraphs:
matches = parser.parse(line.text)
for obj in matches['corrections']:
misspelled_word = obj['text']
if misspelled_word in selected_words:
correction = selected_words[misspelled_word]
line.text = line.text.replace(str(misspelled_word), str(correction))
else:
if corrections == 'auto':
line.text = line.text.replace(str(obj['text']), str(obj['correct']))
document.save(file.file)
return file
In the file variable, i'm sending the Queryset which contains file(.docx).
resumes = Resume.objects.filter(user=user)
for resume in resumes:
corrections_fn(request=request, file=resume, selected_words=selected_words, corrections="show")
When i try to rewrite the file. It is showing "Unsupported Operation write" error. I searched on internet and i'm not able to find the right document to deal with this type of issue. can any help me on how to solve the issue?
Thank you in advance

Related

Downloading emails with UTF-8 B encoded header

I have a problem with a code which is supposed to download your emails in eml files.
Its supposed to go through the INBOX email listing, retrieve the email content and attachments(if any) and create an .eml file which contains all that.
What it does is that it works with content type of text and the majority multiparts. If an email in the listing contains utf-8B in its header, it simply acts like its the end of the email listing, without displaying any error.
The code in question is:
result, data = p.uid('search',None, search_criteria) # search_criteria is defined earlier in code
if result == 'OK':
data = get_newer_emails_first(data) # get_newer_emails_first() is a function defined to return the list of UIDs in reverse order (newer first)
context['emailsum'] = len(data) # total amount of emails based on the search_criteria parameter.
for num in data:
mymail2 = {}
result,data1 = p.iud('fetch', num, '(RFC822)')
email_message = email.message_from_bytes(data[0][1])
fullemail = email_message.as_bytes()
default_charset = 'ASCII'
if email_message.is_multipart():
m_subject = make_header(decode_header(email_message['Subject']))
else:
m_subject = r''.join([ six.text_type(t[0], t[1] or default_charset) for t in email.header.decode_header(email_message['Subject']) ])
m_from = string(make_header(decode_header(email_message['From'])))
m_date = email_message['Date']
I have done my tests and discovered that while the fullemail variable contains the email properly (thus it reads the data from the actual email successfully), the problem should be in the if else immediately after, but I cannot find what the problem is exactly.
Any ideas?
PS: I accidentally posted this question as a guest, but I opted to delete it and repost it from my account.
Apparently the error lay in my code in the silliest of ways.
Instead of:
m_from = string(make_header(decode_header(email_message['From'])))
m_date = email_message['Date']
It should be:
m_from = str(make_header(decode_header(email_message['From'])))
m_date = str(make_header(decode_header(email_message['Date'])))

Either saving to or reading from an XML file is not working

Using Tkinter I have programmed a window that displays information, and links to files, based on an xml file.
When saving to the xml file it saves as:
<Files>C:\Users\-\Documents\_Programming\Example.txt<File /></Files>
Instead of:
<Files><File>C:\Users\-\Documents\Programming\Example1.txt</File></Files>
which works.
As a result, other parts of the code see the text as a 'NoneType' and thus can't use it.
How can I change the reading of the xml file or the saving to the xml file that will allow the program to function?
Entire code: https://pastebin.com/0BwkaJhz
I apologise for a lack of commenting
This section of code should take user-input information and write to an existing .xml file
def done(self):
sessions_not_exist = True
for element in self.student:
if element.tag == 'Sessions':
sessions_not_exist = False
if sessions_not_exist == True:
sessions = et.SubElement(self.student, "Sessions")
tree.write(data_file)
for element in self.student:
if element.tag == 'Sessions':
new_session = et.SubElement(element, "Session")
new_session_date = et.SubElement(new_session, 'Date')
new_session_date.text = self.date_entry.get()
new_session_description = et.SubElement(new_session, 'Description')
new_session_description.text = self.description_entry.get()
new_session_files = et.SubElement(new_session, "Files")
tree.write(data_file)
for file in self.files:
new_session_file = et.SubElement(new_session_files, "File")
new_session_files.text = file
tree.write(data_file)
This section of code looks at the xml file and returns the data
def find_data(self):
for student in tree.findall('Student'):
for element in student:
if element.tag == "Name" and element.text == self.studentX:
student_id = student.attrib['id']
self.student_exact = student
else:
break
session_data_list = []
for element in self.student_exact:
if element.tag == "Sessions":
for session in element:
single_session = []
sessions_files = []
for child in session:
print(f"{child.tag}: {child}")
if child.tag == 'Files':
for file in child:
sessions_files.append(file.text)
self.allFiles.append(file.text)
single_session.append(sessions_files)
else:
single_session.append(child.text)
session_data_list.append(single_session)
return session_data_list
The xml file/code (sorry for poor layout):
<root><Student id="1"><Name>Ruby</Name><Sessions><Session><Date>12/03/18</Date><Description>Measurement Problems and Long Division</Description><Files><File>C:\Users\Hazzb.HARRY-PC\Desktop\testing\test.py</File></Files></Session><Session><Date>26/03/18</Date<Description>Symmetry</Description></Session><Session><Date>xx.xx.xx</Date><Description>XXXXXX</Description><Files><File>C:\Users\Hazzb.HARRY-PC\PycharmProjects\Mother's Tutoring\oioi.txt</File><File>C:\Users\Hazzb.HARRY-PC\Desktop\testing\This File.txt</File></Files></Session><Session><Date>23/11/2019</Date><Description>Example</Description><Files>C:\Users\Hazzb.HARRY-PC\Documents\_Programming\ArtExample.txt<File /></Files></Session></Sessions></Student></root>
This line in your done function is the culprit
new_session_files.text = file
Change that to
new_session_file.text = file
Secondly, in your done function, there are multiple tree.write(data_file), you need only have to do that once. At the end when you are done with updating the tree completely.

Inserting rows in workbook using win32com in python not working

I have a very specific question. The line:
expform_ws.Range("Total").Offset(-1, 0).EntireRow.Insert
in the code below is not working:
# Write data in expenses form
expform_wb = xl.Workbooks.Open(expform_path, Editable=True)
expform_ws = expform_wb.Worksheets('Expense Form')
last_row_ef = expense_items + 15
expform_ws.Range("Total").Offset(-1, 0).EntireRow.Insert
expform_ws.Range('Casecode').Value = case_code
expform_ws.Range('D6').Value = name
expform_ws.Range('D7').Value = last_name
expform_ws.Range('D8').Value = datetime.date.today().strftime("%d/%m/%Y")
expform_ws.Range('B16:B' + str(last_row_ef)).Value = date
expform_ws.Range('D16:D' + str(last_row_ef)).Value = descr
In case this helps: the line gets highlighted in PyCharm as "Statement seems to have no effect".
Anyone can help to spot what I am doing wrong?
Thanks!
In this line
expform_ws.Range("Total").Offset(-1, 0).EntireRow.Insert
You aren't actually CALLING the function, you are just getting "reference" to it, add () to call it
expform_ws.Range("Total").Offset(-1, 0).EntireRow.Insert()

Python Praw ways to store data for calling later?

Is a dictionary the correct way to be doing this? Ideally this will be more then 5+ deep. Sorry my only language experience is powershell there I would just make an array of object. Im not looking for someone to write the code I just wanna know if there is a better way?
Thanks
Cody
My Powershell way:
[$title1,$title2,$title3]
$titleX.comment = "comment here"
$titleX.comment.author = "bob"
$titleX.comment.author.karma = "200"
$titleX.comment.reply = "Hey Bob love your comment."
$titleX.comment.reply.author = "Alex"
$titleX.comment.reply.reply = "I disagree"
#
Python code Borken:
import praw
d = {}
reddit = praw.Reddit(client_id='XXXX',
client_secret='XXXX',
user_agent='android:com.example.myredditapp:'
'v1.2.3 (by /u/XXX)')
for submission in reddit.subreddit('redditdev').hot(limit=2):
d[submission.id] = {}
d[submission.id]['comment'] = {}
d[submission.id]['title']= {}
d[submission.id]['comment']['author']={}
d[submission.id]['title'] = submission.title
mySubmission = reddit.submission(id=submission.id)
mySubmission.comments.replace_more(limit=0)
for comment in mySubmission.comments.list():
d[submission.id]['comment'] = comment.body
d[submission.id]['comment']['author'] = comment.author.name
print(submission.title)
print(comment.body)
print(comment.author.name)
print(d)
File "C:/git/tensorflow/Reddit/pull.py", line 23, in <module>
d[submission.id]['comment']['author'] = comment.author.name
TypeError: 'str' object does not support item assignment
#
{'6xg24v': {'comment': 'Locking this version. Please comment on the [original post](https://www.reddit.com/r/changelog/comments/6xfyfg/an_update_on_the_state_of_the_redditreddit_and/)!', 'title': 'An update on the state of the reddit/reddit and reddit/reddit-mobile repositories'}}
I think your approach using a dictionary is okay, but you might also solve this by using a data structure for your posts: Instead of writing
d[submission.id] = {}
d[submission.id]['comment'] = {}
d[submission.id]['title']= {}
d[submission.id]['comment']['author']={}
d[submission.id]['title'] = submission.title
you could create a class Submission like this:
class Submission(object):
def __init__(self, id, author, title, content):
self.id = id
self.author = author
self.title = title
self.content = content
self.subSubmissions = {}
def addSubSubmission(self,submission):
self.subSubmission[submission,id] = submission
def getSubSubmission(self,id):
return self.subSubmission[id]
by using you could change your code to this
submissions = {}
for sm in reddit.subreddit('redditdev').hot(limit=2):
submissions[sm.id] = Submission(sm.id, sm.author, sm.title, sm.content)
# I am not quite sure what these lines are supposed to do, so you might be able to improve these, too
mySubmission = reddit.submission(id=sm.id)
mySubmission.comments.replace_more(limit=0)
for cmt in mySubmission.comments.list():
submissions[sm.id].addSubSubmission(Submission(cmt.id, cmt.title, cmt.author, cmt.body))
By using this apporach you are also able to export the code to readout the comments/subSubmissions into an extra function which can call itself recursively, so that you can read infitive depths of the comments.

Why can't I pickle this list?

The purpose of this form is to let users enter a lot of places (comma separated) and it'll retrieve the phone, name, website. Have it working in a python IDE, no problem, but having issues putting it into my webapp.
I'm getting the error Exception Value: Can't pickle local object 'GetNums.<locals>.get_data' at the line where a is assigned. I checked the type of inputText and verified that it is indeed a list. So, I'm not sure why it won't pickle.
def GetNums(request):
form = GetNumsForm(request.POST or None)
if form.is_valid():
inputText = form.cleaned_data.get('getnums')
# all experimental
inputText = inputText.split(',')
def get_data(i):
#DON'T FORGET TO MOVE THE PRIMARY KEY LATER TO SETTINGS
r1 = requests.get('https://maps.googleapis.com/maps/api/place/textsearch/json?query=' + i + '&key=GET_YOUR_OWN')
a = r1.json()
pid = a['results'][0]['place_id']
r2 = requests.get('https://maps.googleapis.com/maps/api/place/details/json?placeid=' + pid + '&key=GET_YOUR_OWN')
b = r2.json()
phone = b['result']['formatted_phone_number']
name = b['result']['name']
try:
website = b['result']['website']
except:
website ='No website found'
return ' '.join((phone, name, website))
v = str(type(inputText))
with Pool(5) as p:
a = (p.map(get_data, inputText))
# for line in p.map(get_data, inputText):
# print(line)
#code assist by http://stackoverflow.com/a/34512870/5037442
#end experimental
return render(request, 'about.html', {'v': a})
It's actually barfing when trying to pickle get_data, which is a nested function/closure.
Move get_data out of GetNums (and agh rename it to snake_case please) and it should work.

Categories