I've got a variable that could either be a string or a tuple (I don't know ahead of time) and I need to work with it as a list.
Essentially, I want to transform the following into a list comprehension.
variable = 'id'
final = []
if isinstance(variable, str):
final.append(variable)
elif isinstance(variable, tuple):
final = list(...
Hello guys, I'm trying to do switch statement (with dictionary) and the answer needs to be a formatted string, so for example:
descriptions = {
'player_joined_clan': "%(player)s joined clan %(clan)s." % {"player": token1, "clan": token2},
#etc...
}
Now, this would work if those both tokens were always defined, which is not the...
I'm using duck typing in Python.
def flagItem(object_to_flag, account_flagging, flag_type, is_flagged):
if flag_type == Flags.OFFENSIVE:
object_to_flag.is_offensive=is_flagged
elif flag_type == Flags.SPAM:
object_to_flag.is_spam=is_flagged
object_to_flag.is_active=(not is_flagged)
object_to_flag.cleanup(...
Edit: I'm really just curious as to how I can get this regex to work. Please don't tell me there are easier ways to do it. That's obvious! :P
I'm writing a regular expression (using Python) to parse lines in a configuration file. Lines could look like this:
someoption1 = some value # some comment
# this line is only a comment
someoptio...
Is there an easy way to read these integers in? I'd prefer a built in method, but I assume it is possible to do with some bit operations.
Cheers
edit
I thought of another way to do it that is different to the ways below and in my opinion is clearer. It pads with zeros at the other end, then shifts the result. No if required because shif...
This is why I'm asking this question:
Last year I made some C++ code to compute posterior probabilities for a particular type of model (described by a Bayesian network). The model worked pretty well and some other people started to use my software. Now I want to improve my model. Since I'm already coding slightly different inference algo...
I'm new to Python, and I've been playing around with it for simple tasks. I have a bunch of CSVs which I need to manipulate in complex ways, but I'm breaking this up into smaller tasks for the sake of learning Python.
For now, given a list of strings, I want to remove user-defined title prefixes of any names in the strings. Any string ...
I have exception handling in my app engine app. The code work perfectly fine on the dev server. But when I upload the file on the app engine server, I get a syntax error.
Here is the traceback:
Exception in request:
Traceback (most recent call last):
File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/core/h...
I am still learning to do javascript and django and yesterday I tried to do a simple hello world ajax exercise.
Server logs show that python code is being called but somehow django/python does not return anything when I check the xmlhttp.responseText and responseXML in firebug.
UPDATE: I removed the checking of the http status return...
I have no idea what could be the problem here:
I have some modules from Biopython which I can import easily when using the interactive prompt or executing python scripts via the command-line.
The problem is, when I try and import the same biopython modules in a web-executable cgi script, I get a "Import Error"
: No
module named B...
I'm new to python programming, and I am trying to read a password protected file using python, the code is shown below:
import sys
import win32com.client
xlApp = win32com.client.Dispatch("Excel.Application")
print "Excel library version:", xlApp.Version
filename,password = 'C:\myfiles\foo.xls', 'qwerty12'
xlwb = xlApp.Workbooks.Open(fi...
It used to be in Python (2.6) that one could ask:
isinstance(f, file)
but now "file" is undefined (using Python 3.0)
What is the proper method for checking to see if a variable is a file now? The What'sNew docs didn't mention this...
...
I have a view that gets data from a form and executes a subprocess:
def sync_job(request, job_id, src, dest):
form = SyncJobForm()
check = SyncJob.objects.get(id=job_id)
check.status = True
check.save()
pre_sync = SyncJobCMD.objects.get(id=1)
p = Popen([str(pre_sync), '-avu', str(src), str(dest)], stdout=PIPE)
syncoutput,s...
Is it possible to run a PHP script using python?
Thanks in advance.
...
I have written a code for detecting the EOF of an excel file using python:
row_no = 1
while True:
x = xlws.Cells(row_no,1).value
if type(x) is None:
break
else:
print(len(x))
print(x)
row_no = row_no + 1
i expect the while loop will stop then x becomes a "blank cell", which I support to be None,...
What exactly are views in Python3.1? They seem to behave in a similar manner as that of iterators and they can be materialized into lists too. How are iterators and views different?
...
I just read through a PEP concerning the unification of ints and longs in Python3k in PEP 237. The approach used in this seems very interesting. The approach is to create a new type "integer" which is the abstract base class of int and long. Also, performing operations on ints which result in very large numbers will no longer result in a...
I have Rhythmbox running on my desktop, and I want to be able to control it from remotely via a web interface. I'm having problems accessing it, however, because rhythmbox-client is complaining that the user (www-data) that is trying to access it doesn't a) have as X session running, and b) doesn't have access to my rhythmbox dbus inform...
Is there a way to have Python print the names and values of all bound variables?
(without redesigning the program to store them all in a single list)
...
Mann i have a problem with this question from my professor. Here is the question:
Write the definition of a function typing_speed , that receives two parameters. The first is the number of words that a person has typed (an int greater than or equal to zero) in a particular time interval. The second is the length of the time interval i...