Skip to content
Advertisement

Tag: python

Equivalent of Python’s KeyError exception in JavaScript?

I am trying to access a certain member in a JavaScript object. In order to do this, I need to try out a couple of key values. For example, Object[‘text/html’] which will give me an export link for a HTML document. However, not every object of this type will have a text/html key pair value. In Python I would solve

How to read and write JSON offline on local machine?

Problem I need a way to store and collect JSON data in an entirely offline(!) web application, hosted on a local (shared) machine. Several people will access the app but it will never actually be online. I’d like the app to: Read and write JSON data continuously and programmatically (i.e. not using a file-upload type schema) Preferably not require any

Flask url_for URLs in Javascript

What is the recommended way to create dynamic URLs in Javascript files when using flask? In the jinja2 templates and within the python views url_for is used, what is the recommended way to do this in .js files? Since they are not interpreted by the template engine. What basically want to do is: Which is not possible. But naturally, I

Convert Python None to JavaScript null

In a Django view I am generating a data set something like this: I am passing this data to a JavaScript variable using: For use in a High Charts script. Unfortunately JavaScript is stumbling when it encounters the None value because actually what I need is null. How can I best replace None with null, and where should I handle

Equivalent JavaScript functions for Python’s urllib.parse.quote() and urllib.parse.unquote()

Are there any equivalent JavaScript functions for Python’s urllib.parse.quote() and urllib.parse.unquote()? The closest I’ve come across are encodeURI()/encodeURIComponent() and escape() (and their corresponding un-encoding functions), but they don’t encode/decode the same set of special characters as far as I can tell. Answer OK, I think I’m going to go with a hybrid custom set of functions: Encode: Use encodeURIComponent(), then

Is there a benefit to defining a class inside another class in Python?

What I’m talking about here are nested classes. Essentially, I have two classes that I’m modeling. A DownloadManager class and a DownloadThread class. The obvious OOP concept here is composition. However, composition doesn’t necessarily mean nesting, right? I have code that looks something like this: But now I’m wondering if there’s a situation where nesting would be better. Something like:

Advertisement