Skip to content
Advertisement

JavaScript function similar to Python range()

Is there a function in JavaScript similar to Python’s range()? I think there should be a better way than to write the following lines every time: Answer No, there is none, but you can make one. JavaScript’s implementation of Python’s range() Trying to emulate how it works in Python, I would create function similar to this: See this jsfiddle for

Remove all whitespace in a string

I want to eliminate all the whitespace from a string, on both ends, and in between words. I have this Python code: But that only eliminates the whitespace on both sides of the string. How do I remove all whitespace? Answer If you want to remove leading and ending spaces, use str.strip(): If you want to remove all space characters,

Dividing Python module into multiple regions

In C# we can create regions by using Is there any way to format python code in similar fashion so that I can keep all my relevant methods in one block? Answer I recommend you have a look at PyDev. If you structure your Python code well it will be very useful to have a document outline and code folding.

How to JSON serialize sets?

I have a Python set that contains objects with __hash__ and __eq__ methods in order to make certain no duplicates are included in the collection. I need to json encode this result set, but passing even an empty set to the json.dumps method raises a TypeError. I know I can create an extension to the json.JSONEncoder class that has a

How can I change the cursor shape with PyQt?

I have a simple application that runs a process that can last for several minutes before completing. I am trying to provide an indication to the user that it is processing the request – such as changing the cursor to an hourglass. But I cannot quite get it to work right. All of my attempts have resulted in either an

How to check if a value exists in a dictionary?

I have the following dictionary in python: I need a way to find if a value such as “one” or “two” exists in this dictionary. For example, if I wanted to know if the index “1” existed I would simply have to type: And then python would tell me if that is true or false, however I need to do

When to use cla(), clf() or close() for clearing a plot in matplotlib?

Matplotlib offers these functions: When should I use each function and what exactly does it do? Answer They all do different things, since matplotlib uses a hierarchical order in which a figure window contains a figure which may consist of many axes. Additionally, there are functions from the pyplot interface and there are methods on the Figure class. I will

Data from a MATLAB .fig file using Python?

Does anyone know of any methods of extracting the data from a MATLAB fig file using Python? I know these are binary files but the methods in the Python Cookbook for .mat files http://www.scipy.org/Cookbook/Reading_mat_files don’t seem to work for .fig files… Thanks in advance for any help, Dan Answer .fig files are .mat files (containing a struct), see http://undocumentedmatlab.com/blog/fig-files-format/ As

How to create a SSH tunnel using Python and Paramiko?

I need to create tunneling to read information from a database. I use Paramiko, but I have not worked with tunneling yet. Please provide an example of a simple code that creates and closes a tunnel. Answer At work we usually create ssh tunnels forwarding ports. The way we do that is, by using the standard command ssh -L port:addr:port

Advertisement