Displaying better error message than “No JSON object could be decoded”

I’ve found that the simplejson module gives more descriptive errors in many cases where the built-in json module is vague. For instance, for the case of having a comma after the last item in a list: which is not very descriptive. The same operation with simplejson: Much better! Likewise for other common errors like capitalizing True.

Flask ImportError: No Module Named Flask

Try deleting the virtualenv you created. Then create a new virtualenv with: Then: Now let’s activate the virtualenv Now you should see (flask) on the left of the command line. Edit: In windows there is no “source” that’s a linux thing, instead execute the activate.bat file, here I do it using Powershell: PS C:\DEV\aProject> & .\Flask\Scripts\activate) Let’s install … Read more

IndexError: list index out of range and python

If you have a list with 53 items, the last one is thelist[52] because indexing starts at 0. From Real Python: Understanding the Python Traceback – IndexError: IndexError The IndexError is raised when you attempt to retrieve an index from a sequence, like a list or a tuple, and the index isn’t found in the sequence. The Python documentation defines when this exception is … Read more

Python re.split() vs split()

re.split is expected to be slower, as the usage of regular expressions incurs some overhead. Of course if you are splitting on a constant string, there is no point in using re.split().

Issues using Keras np_utils.to_categorical

I’m trying to make an array of one-hot vector of integers into an array of one-hot vector that keras will be able to use to fit my model. Here’s the relevant part of the code: Below is an image showing what Y_train and dummy_y actually are. I couldn’t find any documentation for to_categorical that could help me. Thanks in advance.

How do I append one string to another in Python?

If you only have one reference to a string and you concatenate another string to the end, CPython now special cases this and tries to extend the string in place. The end result is that the operation is amortized O(n). e.g. used to be O(n^2), but now it is O(n). From the source (bytesobject.c): It’s … Read more

How to measure elapsed time in Python?

What I want is to start counting time somewhere in my code and then get the passed time, to measure the time it took to execute few function. I think I’m using the timeit module wrong, but the docs are just confusing for me.

List files ONLY in the current directory

Just use os.listdir and os.path.isfile instead of os.walk. Example: But be careful while applying this to other directory, like which would not work because f is not a full path but relative to the current dir. Therefore, for filtering on another directory, do os.path.isfile(os.path.join(somedir, f)) (Thanks Causality for the hint)

Error handling in Python-MySQL

First point: you have too much code in your try/except block. Better to use distinct try/except blocks when you have two statements (or two groups of statements) that may raise different errors: Now do you really have to catch a TypeError here ? If you read at the traceback, you’ll notice that your error comes … Read more