Mock gives "raise except" error - python

I am trying to unit test my program.
I have a side effect in my mock object
from models import MyObject
mock_obj.objects.get.side_effect = mock.Mock(side_effect=MyObject.DoesNotExist)
The test works and passes when I have this in the function that I am testing:
import models
try:
obj = models.MyObject.objects.get(id=1)
except Exception:
return True
However when I change this to:
import models
try:
obj = models.MyObject.objects.get(id=1)
except models.MyObject.DoesNotExist:
return True
It gives me this instead of returning True:
> Traceback (most recent call last):
> File "/home/test/test_my_function.py", line 40, in test_get_job_not_exist
> response = my_function.my_function(request_mock, 1)
> File "/home/handlers/my_function.py", line 35, in get_job_with_id
> obj = MyObject.objects.get(id=id)
> File "/local/lib/python2.7/site-packages/mock/mock.py", line 1062, in __call__
> return _mock_self._mock_call(*args, **kwargs)
> File "/local/lib/python2.7/site-packages/mock/mock.py", line 1118, in _mock_call
> raise effect
> DoesNotExist
Why is this happening? MyObject is a Django model object

Related

ValueError:Cannot render objects with any missing geometries

I wrote this code in order to have a map with population in other words i wanted polygan layer but I got an error
here is my code:
import folium
import pandas
data = pandas.read_csv("Volcanoes.txt")
lat=list(data["LAT"])
lon=list(data["LON"])
elev=list(data["ELEV"])
def color_producer(elevation):
if elevation < 1000:
return 'green'
elif 1000<=elevation<3000:
return 'orange'
else:
return 'red'
map=folium.Map(location=[42.304386,-83.066254],zoom_start=6,tiles="Stamen Terrain")
fg=folium.FeatureGroup(name="My Map")
for lt, ln ,el in zip(lat,lon,elev) :
fg.add_child(folium.CircleMarker(location=[lt,ln],radius=6,popup=str(el)+"m",
fill_color=color_producer(el),color='grey',fill_opacity=0.7))
fg.add_child(folium.GeoJson(data=open('world.json','r',encoding='utf-8-sig'),style_function=lambda x:{'fillColor':'yellow' }))
map.add_child(fg)
map.save("Map1.html")
this is my error:
Traceback (most recent call last):
File "C:\Users\Vida\Desktop\Webmapping_real\map1.py", line 28, in <module>
fg.add_child(folium.GeoJson(data=open('world.json','r',encoding='utf-8-sig'),style_function=lambda x:{'fillColor':'yellow' }))
File "C:\Users\Vida\AppData\Local\Programs\Python\Python310\lib\site-packages\folium\features.py", line 499, in __init__
self.data = self.process_data(data)
File "C:\Users\Vida\AppData\Local\Programs\Python\Python310\lib\site-packages\folium\features.py", line 544, in process_data
raise ValueError('Cannot render objects with any missing geometries'
ValueError: Cannot render objects with any missing geometries: <_io.TextIOWrapper name='world.json' mode='r' encoding='utf-8-sig'>
PS C:\Users\Vida\Desktop\Webmapping_real>
would you please help me how to fix it?

Filtering python logging module to only log last line of an exception?

I'm utilizing the following code to print exceptions from a python module I'm running inside of my script:
except Exception as e:
logging.info('yt-dlp initated a break', exc_info=True)
Currently it outputs much more information than I need.
Something along the lines of:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/web_ripper.py", line 58, in subscriptions_loop
ydl.download([url])
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 3113, in download
self.__download_wrapper(self.extract_info)(
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 3086, in wrapper
res = func(*args, **kwargs)
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 1344, in extract_info
return self.__extract_info(url, self.get_info_extractor(ie_key), download, extra_info, process)
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 1371, in wrapper
self.report_error(str(e), e.format_traceback())
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 912, in report_error
self.trouble(f'{self._format_err("ERROR:", self.Styles.ERROR)} {message}', *args, **kwargs)
File "/app/venv/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 853, in trouble
raise DownloadError(message, exc_info)
yt_dlp.utils.DownloadError: ERROR: [youtube:tab] Videogamedunkey: Unable to download API page: HTTP Error 404: Not Found
What's the most efficient way to simply print the last line of the exception?
You can use a custom Formatter that will process the exc_text of the record you are trying to log.
The following MyFormatter will check if the record has an exception text. If not, it will create it using self.formatException. Then, since record.exc_text is a string storing the trace as text, with \n as separators between lines, you can keep the last line using .split("\n")[-1].
import logging
class MyFormatter(logging.Formatter):
def format(self, record):
# https://github.com/python/cpython/blob/main/Lib/logging/__init__.py#L711
if not record.exc_text:
record.exc_text = self.formatException(record.exc_info)
record.exc_text = record.exc_text.split("\n")[-1]
return super().format(record)
Then, add the following code to use your new MyFormatter:
def a(x):
return b(x)
def b(x):
return c(x)
def c(x):
return d(x)
def d(x):
return x / 0
logger = logging.getLogger("foobar")
logger.setLevel(logging.INFO)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(MyFormatter(fmt="%(name)s - %(levelname)s - %(message)s"))
logger.addHandler(stream_handler)
try:
a(1)
except Exception as e:
logger.info('yt-dlp initated a break', exc_info=True)
and you get:
foobar - INFO - yt-dlp initated a break
ZeroDivisionError: division by zero
instead of the previous:
foobar - INFO - yt-dlp initated a break
Traceback (most recent call last):
File "70704112.py", line 31, in <module>
a(1)
File "70704112.py", line 12, in a
return b(x)
File "70704112.py", line 15, in b
return c(x)
File "70704112.py", line 18, in c
return d(x)
File "70704112.py", line 21, in d
return x / 0
ZeroDivisionError: division by zero

Lambda causing AttributeError

I have this piece of code that fetches an id of a sale team if the person is in one, else it returns None.
class AccountInvoice(models.Model):
_inherit = 'account.invoice'
xx_section_id = fields.Many2one('crm.case.section', string='Invoice template', required=True,
default=lambda self: self._get_sales_team())
#api.model
def _get_sales_team(self):
ids = self.env['crm.case.section'].search([('member_ids', 'in', self._uid)])
if len(ids) != 1:
# Has no sales team or more than one
return None
return ids[0]
For some reason this works in my local environment but not on the server. The error occurs when I try to install the module. The server gives me the following error:
AttributeError: 'NoneType' object has no attribute 'id'
And in the logging it states:
2016-12-06 19:39:06,662 2005 ERROR None openerp.http: Exception during JSON request handling.
Traceback (most recent call last):
File "/Users/glenn/Documents/Work/odoo80_vzwwebcrm_tst/openerp/http.py", line 537, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/Users/glenn/Documents/Work/odoo80_vzwwebcrm_tst/openerp/http.py", line 1415, in _dispatch_nodb
func, arguments = self.nodb_routing_map.bind_to_environ(request.httprequest.environ).match()
File "/Users/glenn/.virtualenvs/odoo8/lib/python2.7/site-packages/werkzeug/routing.py", line 1430, in match
raise NotFound()
NotFound: 404: Not Found
What am I doing wrong?
For the case len(ids) == 0, the script tries to 'return ids[0]'.
Depending on the result value of 'ids', you condition should be:
if ids:
or
if len(ids) > 0:

Why does this very basic query on this Django model fail?

I have the following Django class:
from caching.base import CachingManager, CachingMixin
from mptt.models import MPTTModel
def make_id():
'''
inspired by http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram
'''
START_TIME = 1876545318034
return (int(time.time()*1000) - START_TIME << 23 ) | random.SystemRandom().getrandbits(23)
class My_Class(CachingMixin, MPTTModel):
id = models.BigIntegerField(default=make_id, primary_key=True)
# Other Attributes Snipped here for brevity
But look what happens when I try to query this class:
>>> My_Class.objects.get(pk=5000)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "my_virtual_env/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "my_virtual_env/lib/python2.7/site-packages/django/db/models/query.py", line 334, in get
self.model._meta.object_name
DoesNotExist: My_Class matching query does not exist.
Why does it fail?? How can I fix this?
It could be you have no My_Class with id = 5000
try:
mc = My_Class.objects.get(pk=id)
except My_Class.DoesNotExist:
mc = None

Handling exceptions in while loop - Python

Here is my code (almost full version for #cdhowie :)):
def getResult(method, argument=None):
result = None
while True:
print('### loop')
try:
print ('### try hard...')
if argument:
result = method(argument)
else:
result = method()
break
except Exception as e:
print('### GithubException')
if 403 == e.status:
print('Warning: ' + str(e.data))
print('I will try again after 10 minutes...')
else:
raise e
return result
def getUsernames(locations, gh):
usernames = set()
for location in locations:
print location
result = getResult(gh.legacy_search_users, location)
for user in result:
usernames.add(user.login)
print user.login,
return usernames
# "main.py"
gh = Github()
locations = ['Washington', 'Berlin']
# "main.py", line 12 is bellow
usernames = getUsernames(locations, gh)
The problem is, that exception is raised, but I can't handle it. Here is an output:
### loop
### try hard...
Traceback (most recent call last):
File "main.py", line 12, in <module>
usernames = getUsernames(locations, gh)
File "/home/ciembor/projekty/github-rank/functions.py", line 39, in getUsernames
for user in result:
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 33, in __iter__
newElements = self.__grow()
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 45, in __grow
newElements = self._fetchNextPage()
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py", line 37, in _fetchNextPage
return self.get_page(page)
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Legacy.py", line 48, in get_page
None
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/Requester.py", line 69, in requestAndCheck
raise GithubException.GithubException(status, output)
github.GithubException.GithubException: 403 {u'message': u'API Rate Limit Exceeded for 11.11.11.11'}
Why it doesn't print ### handling exception?
Take a close look at the stack trace in the exception:
Traceback (most recent call last):
File "main.py", line 12, in <module>
usernames = getUsernames(locations, gh)
File "/home/ciembor/projekty/github-rank/functions.py", line 39, in getUsernames
for user in result:
File "/usr/lib/python2.7/site-packages/PyGithub-1.8.0-py2.7.egg/github/PaginatedList.py", line 33, in __iter__
newElements = self.__grow()
...
The exception is being thrown from code being called by the line for user in result: after getResult finishes executing. This means that the API you're using is using lazy evaluation, so the actual API request doesn't quite happen when you expect it to.
In order to catch and handle this exception, you'll need to wrap the code inside the getUsernames function with a try/except handler.

Categories

Resources