Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Quick solution to fix the error: attributeerror: __exit__.
Next
Answered
Cecil Evans
  • 29
Cecil Evans
Asked: May 18, 20222022-05-18T18:35:29+00:00 2022-05-18T18:35:29+00:00In: python

Quick solution to fix the error: attributeerror: __exit__.

  • 29

. Advertisement .

..3..

. Advertisement .

..4..

I get the error: attributeerror: __exit__ when I try to run the program below:

import csv
 import time
 import datetime
 import re
 from operator import itemgetter
 from multiprocessing import Pool
 import itertools
 
 def chunks(l,n):
  """Divide a list of nodes `l` in `n` chunks"""
  l_c = iter(l)
  while 1:
  x = tuple(itertools.islice(l_c,n))
  if not x:
  return
  yield x
 
 def csv2nodes(r):
  strptime = time.strptime
  mktime = time.mktime
  l = []
  ppl = set()
  pattern = re.compile(r"""[A-Za-z0-9"/]+?(?=[,\n])""")
  for row in r:
  with pattern.findall(row) as f:
  cell = int(f[3])
  id = int(f[2])
  st = mktime(strptime(f[0],'%d/%m/%Y'))
  ed = mktime(strptime(f[1],'%d/%m/%Y'))
  # collect list
  l.append([(id,cell,{1:st,2: ed})])
  # collect separate sets
  ppl.add(id)
  return (l,ppl)
 
 def csv2graph(source):
  MG=nx.MultiGraph()
  # Remember that I use integers for edge attributes, to save space! Dic above.
  # start: 1
  # end: 2
  p = Pool()
  node_divisor = len(p._pool)
  node_chunks = list(chunks(source,int(len(source)/int(node_divisor))))
  num_chunks = len(node_chunks)
  pedgelists = p.map(csv2nodes,
  node_chunks)
  ll = []
  ppl = set()
  for l in pedgelists:
  ll.append(l[0])
  ppl.update(l[1])
  MG.add_edges_from(ll)
  return (MG,ppl)
 
 with open('/Users/laszlosandor/Dropbox/peers_prisons/python/codetenus_test.txt','r') as source:
  r = source.readlines()
  MG,ppl = csv2graph(r)

The error appears the system notifies as follows:

Traceback (most recent call last):
  File "parser5_nodots_parallel.py", line 256, in <module>
  MG,ppl = csv2graph(r)
  File "parser5_nodots_parallel.py", line 245, in csv2graph
  node_chunks)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/multiprocessing/pool.py", line 251, in map
  return self.map_async(func, iterable, chunksize).get()
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/multiprocessing/pool.py", line 552, in get
  raise self._value
 AttributeError: __exit__

I tried to solve it with another sample. I got the reference in the community forum, but it still returned an invalid result. If someone knows the solution, please give me the support. Thanks!

python-3.x
  • 2 2 Answers
  • 59 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-15T16:03:49+00:00Added an answer on June 15, 2022 at 4:03 pm

    The cause:

    This is the reason of the error:

    with pattern.findall(row) as f:

    The with statement is being used. It requires an object which uses __enter__ or __exit__ methods. pattern.findall returns list. with attempts to store __exit__ methods, but it fails and causes an error.

    Solution:

    You can resolve your problem by using:

    f = pattern.findall(row)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Aaron de Sousa
    2022-05-25T20:47:11+00:00Added an answer on May 25, 2022 at 8:47 pm

    This is not the problem of the asker in this instance, but . The first troubleshooting step to a generic “AttributeError” should be checking that the brackets are present, e.g.

    with SomeContextManager() as foo:
     #works because a new object is referenced...


    but

    with SomeContextManager as foo:
     #AttributeError because the class is referenced

    I am a bit tempted by Cat and end up here. -__-

    • 5
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.