In Python 3, how would I print a random word from a list of words? Answer Use the random.choice() function:
Tag: python
Sum array by number in numpy
Assuming I have a numpy array like: [1,2,3,4,5,6] and another array: [0,0,1,2,2,1] I want to sum the items in the first array by group (the second array) and obtain n-groups results in group number order (in this case the result would be [3, 9, 9]). How do I do this in numpy? Answer There’s more than on…
What is exactly a file-like object in Python?
From the documentation for the standard library json module: json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw) Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-lik…
How do I disable a Pylint warning?
I’m trying to disable warning C0321 (“more than one statement on a single line” — I often put if statements with short single-line results on the same line), in Pylint 0.21.1 (if it matters: astng 0.20.1, common 0.50.3, and Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)). I’ve …
Extract ZipFile using Python, display Progress Percentage?
I know how to extract a zip archive using Python, but how exactly do I display the progress of that extraction in a percentage? Answer the extract method doesn’t provide a call back for this so one would have to use getinfo to get the e uncompressed size and then open the file read from it in blocks and…
Python unittest – opposite of assertRaises?
I want to write a test to establish that an Exception is not raised in a given circumstance. It’s straightforward to test if an Exception is raised … … but how can you do the opposite. Something like this i what I’m after … Answer
How can I store an array of strings in a Django model?
I am building a Django data model and I want to be able to store an array of strings in one of the variables; how can I do that? e.g. Thanks for the help. Answer Make another model that holds a string with an optional order, give it a ForeignKey back to myClass, and store your array in there.
Python trace module – Trace lines as they are executed, but save to file, rather than stdout
I want to trace the lines of a python script as they are executed. However the programme I use needs to print things to stdout. The trace option to the python trace module prints them to stdout. Is there anwyay to tell it to not print them to stdout, but instead save them to a file? I tried setting the
Reversible hash function?
I need a reversible hash function (obviously the input will be much smaller in size than the output) that maps the input to the output in a random-looking way. Basically, I want a way to transform a number like “123” to a larger number like “9874362483910978”, but not in a way that wil…
Python: sort function breaks in the presence of nan
sorted([2, float(‘nan’), 1]) returns [2, nan, 1] (At least on Activestate Python 3.1 implementation.) I understand nan is a weird object, so I wouldn’t be surprised if it shows up in random places in the sort result. But it also messes up the sort for the non-nan numbers in the container, wh…