Wednesday, October 16, 2019

Re: Import a float field using a script

Hi

I agree with what you are saying. And you say there are issues with my solution. However, now it is working I will make the necessary changes to improve the solution.

Many thanks for all the help.

Best
-David

> On 16 Oct 2019, at 14:04, Kasper Laudrup <laudrup@stacktrace.dk> wrote:
>
> Hi again David
>
> On 16/10/2019 14.22, 'David Turner' via Django users wrote:
>> Thanks for pointing me in the right direction.
>
> No problem. Happy to help.
>
> Just a pedantic note on your solution:
>
>> I added a simple
>> try:
>> y = float(row[6])
>> except:
>> y = None
>> And changed the area_hectares=y, in my script.
>> Everything works fine now.
>
> While this definitely solves the issue, there are some things you might consider.
>
> Catch *all* exceptions as you do with the "except:" line can be quite dangerous, since you will not only catch the TypeError that caused the original problem, but also other exceptions like IndexError or whatever else might raise an exception in the try block now or in the future when you update the code.
>
> This might be what you want, but it can cause very hard to debug errors much later, so it's good to be aware of.
>
> Also, while "asking for forgiveness" as this kind of exception handling is doing is a perfectly valid pattern in Python code, it might not be what you want here. What if the field wasn't empty but instead contained an actual but invalid value?
>
> Again that might be what you want, but you could consider doing something like:
>
> y = float(row[6]) if row[6] else None
>
> That will explicitly set the variable if row 6 is falsy as an empty string is.
>
> Kind regards,
>
> Kasper Laudrup
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/vlHtz3JtsC0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9e34a9cd-b47c-b26b-8dba-cd27f338d753%40stacktrace.dk.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7C31C8AB-F76F-43BF-B439-F850BBE5844E%40googlemail.com.

No comments:

Post a Comment