0

I have a script written in python 2.x that checks the data type for a number of different unseen variables. I try to move the code to python 3.x and one of the variables is crushing my program its value is date:2018-04-21 04:00:10. I catch the error with this piece of code:

raise RuntimeError("Value type not handled {}:{}".format(name,value))

Returning this: RuntimeError: Value type not handled date:2018-04-21 04:00:10

The sensitive code can be found below.

Code in python 2.x

if isinstance(value, basestring):
    return ("text", "'{}'".format(escape_single_quote(value.encode("utf-8"))))

Code in python 3.x

if isinstance(value, str):
    return ("text", "'{}'".format(escape_single_quote(value)))

I have also tried using string_types from six and future library.

Stidgeon
  • 2,673
  • 8
  • 20
  • 28
Zisis F
  • 322
  • 5
  • 11
  • What exactly is the problem — what's wrong with your attempts? – martineau Mar 18 '20 at 18:24
  • In the past basestring would catch date:2018-04-21 04:00:10 as basestring data type and now str won't, I need an appropriate data type for this format in Python 3 – Zisis F Mar 19 '20 at 07:39
  • In Python 2. `isinstance(value, basestring)` was equivalent to `isinstance(value, (str, unicode))` which means an instance of either type (because they were different in that version). Have your tried using the long form in Python 3? Also note you can have two different pieces of code if you explicitly check what version of Python is running. – martineau Mar 19 '20 at 07:47
  • 1
    str in python 3 covers unicode as well as I have read, as mentiend above I have tried str and also using string_type from future and six libraries but date:2018-04-21 04:00:10 doesn't get caught – Zisis F Mar 19 '20 at 07:55
  • It's unclear what exactly the type of `date:2018-04-21 04:00:10` is. Can you change to `raise RuntimeError("Value type not handled {}:{}".format(name,type(value)))` and show what the error message becomes? – martineau Mar 19 '20 at 07:59
  • RuntimeError: Value type not handled date: so I created a datetime.datetime case and I caught it – Zisis F Mar 19 '20 at 08:07
  • OK that helps. I'm confused by you saying "I _catch_ the error ..." because the code following that: `raise RuntimeError...`, is **raising** an error, not catching one — so don't understand how that relates to using `if isinstance...` (which also isn't about catching a error). – martineau Mar 19 '20 at 08:14
  • You are right I don't really mean catching errors I terminate the program when I get an unexpected data type, what I'm trying to do is parse some variables find their types and write a .sql file that will do a create statement in python2 basestring could capture more data types than str does now so I try to figure out what I need, the type function does the job for me. Thank you a lot – Zisis F Mar 19 '20 at 10:10
  • 1
    That's good to hear. Note that you really shouldn't use the `type()` function to do type-checking. If you must do it, it's better to use something like `isinstance(value, datetime.datetine)` rather than comparing with `type(value) == datetime.datetine` (because the latter is polymorphic). – martineau Mar 19 '20 at 11:30
  • @ZisisF Any luck yet? – jtlz2 Nov 18 '21 at 07:12

0 Answers0