Skip to content
Advertisement

How can I make a deepcopy of a function in Python?

I would like to make a deepcopy of a function in Python. The copy module is not helpful, according to the documentation, which says: This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged;

Regular expression to find any number in a string

What’s the notation for any number in re? Like if I’m searching a string for any number, positive or negative. I’ve been using d+ but that can’t find 0 or -1 Answer Searching for positive, negative, and/or decimals, you could use [+-]?d+(?:.d+)? This isn’t very smart about leading/trailing zeros, of course: Edit: Corrected the above regex to find single-digit numbers.

How to create a new database using SQLAlchemy?

Using SQLAlchemy, an Engine object is created like this: Accessing engine fails if the database specified in the argument to create_engine (in this case, mydb) does not exist. Is it possible to tell SQLAlchemy to create a new database if the specified database doesn’t exist? Answer On postgres, three databases are normally present by default. If you are able to

Dictionary keys match on list; get key/value pair

In python… I have a list of elements ‘my_list’, and a dictionary ‘my_dict’ where some keys match in ‘my_list’. I would like to search the dictionary and retrieve key/value pairs for the keys matching the ‘my_list’ elements. I tried this… But it doesn’t do the job. Answer (I renamed list to my_list and dict to my_dict to avoid the conflict

Terminal text becomes invisible after terminating subprocess

After terminating an ffmpeg subprocess, the terminal gets messed up – typed characters are invisible! The input still works in that commands can be executed, but keyboard input is not echoed to the terminal. Issuing shell command reset puts everything back to normal (or !reset from within ipython), so a workaround the issue is calling os.system(‘reset’) inside the script. Other

How to have Calendar thing in Django date field

I have this Datetime field in Django But I want the Calendar to pop up. In docs, they say to write this Now I have a few problems: Where to declare this class and how to use it I don’t have pretty.css , animation.css , how can get it Do I need to do programming for it?? Answer The files

How to get a list of all the Python standard library modules?

I want something like sys.builtin_module_names except for the standard library. Other things that didn’t work: sys.modules – only shows modules that have already been loaded sys.prefix – a path that would include non-standard library modules and doesn’t seem to work inside a virtualenv. The reason I want this list is so that I can pass it to the –ignore-module or

How to add the current query string to an URL in a Django template?

When I load a page, there is a link “sameLink” that I want to append to it the query string of its containing page. I have following URL: How can I do that? Answer To capture the QUERY_PARAMS that were part of the request, you reference the dict that contains those parameters (request.GET) and urlencode them so they are acceptable

How to convert an H:MM:SS time string to seconds in Python?

Basically I have the inverse of this problem: Python Time Seconds to h:m:s I have a string in the format H:MM:SS (always 2 digits for minutes and seconds), and I need the integer number of seconds that it represents. How can I do this in python? For example: “1:23:45” would produce an output of 5025 “0:04:15” would produce an output

HTTP requests and JSON parsing in Python [duplicate]

This question already has answers here: How can I parse (read) and use JSON? (5 answers) What are the differences between the urllib, urllib2, urllib3 and requests module? (11 answers) Closed last month. I want to dynamically query Google Maps through the Google Directions API. As an example, this request calculates the route from Chicago, IL to Los Angeles, CA

Advertisement