Skip to content
Advertisement

How can I check if a string represents an int, without using try/except?

Is there any way to tell whether a string represents an integer (e.g., ‘3’, ‘-17’ but not ‘3.14’ or ‘asfasfas’) Without using a try/except mechanism? Answer If you’re really just annoyed at using try/excepts all over the place, please just write a helper function: It’s going to be WAY more code to exactly cover all the strings that Python considers

Is it safe to replace a self object by another object of the same type in a method?

I would like to replace an object instance by another instance inside a method like this: The object is retrieved from a database. Answer It is unlikely that replacing the ‘self’ variable will accomplish whatever you’re trying to do, that couldn’t just be accomplished by storing the result of func(self) in a different variable. ‘self’ is effectively a local variable

can’t see records inserted by django test case

I’m trying to provide integration to my django application from subversion through the post commit hook. I have a django test case (a subclass of unittest.TestCase) that (a) inserts a couple of records into a table, (b) spawns an svn commit, (c) svn commit runs a hook that uses my django model to look up info. I’m using an sqlite3

How to find the longest word with python?

How can I use python to find the longest word from a set of words? I can find the first word like this: Answer If I understand your question correctly: split() splits the string into words (seperated by whitespace); max() finds the largest element using the builtin len() function, i.e. the string length, as the key to find out what

Using module ‘subprocess’ with timeout

Here’s the Python code to run an arbitrary command returning its stdout data, or raise an exception on non-zero exit codes: communicate is used to wait for the process to exit: The subprocess module does not support timeout–ability to kill a process running for more than X number of seconds–therefore, communicate may take forever to run. What is the simplest

What is the best way to call a script from another script? [closed]

Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 10 months ago. The community reviewed whether to reopen this question 10 months ago and left it closed: Original close reason(s) were not resolved Improve this question

Controlling mouse with Python

How does one control the mouse cursor in Python, i.e. move it to certain position and click, under Windows? Answer Tested on WinXP, Python 2.6 (3.x also tested) after installing pywin32 (pywin32-214.win32-py2.6.exe in my case):

How to efficiently calculate a running standard deviation

I have an array of lists of numbers, e.g.: I would like to efficiently calculate the mean and standard deviation at each index of a list, across all array elements. To do the mean, I have been looping through the array and summing the value at a given index of a list. At the end, I divide each value in

Print current call stack from a method in code

In Python, how can I print the current call stack from within a method (for debugging purposes). Answer Here’s an example of getting the stack via the traceback module, and printing it: If you really only want to print the stack to stderr, you can use: Or to print to stdout (useful if want to keep redirected output together), use:

Equivalent of NotImplementedError for fields in Python

In Python 2.x when you want to mark a method as abstract, you can define it like so: Then if you forget to override it, you get a nice reminder exception. Is there an equivalent way to mark a field as abstract? Or is stating it in the class docstring all you can do? At first I thought I could

Advertisement