without you posting your code it's hard to know that <hint, hint>
vegaseat commented: clever indeed, thanks +12
without you posting your code it's hard to know that <hint, hint>
I don't think you can just dump a python array into the dll's function. Try initializing graydata like this:
graydata_type = ctypes.c_ubyte * (w * h)
graydata = graydata_type()
A class variable is shared across instances of the class, while an instance variable is unique to that instance of the class. Consider the following example:
class Foo:
name = "FOO"
def __init__(self, value):
self.value = value
mine = Foo("bar")
mine_2 = Foo("taz")
print mine.value, mine.name
print mine_2.value, mine.name
Foo.name = "BAR"
print mine.value, mine.name
print mine_2.value, mine.name
#output is:
# bar FOO
# taz FOO
# bar BAR
# taz BAR
Use the "in" keyword.
**--- will return a positive for words like "strunk" found = False for word in ["trunk", "branches"] : if word in words[1]: found = True if not found: print "processing this"
Wouldn't it make sense to do this the opposite way? i.e.
if words[1] in ["trunk", "branches"]:
found = True
else:
found = False
The script seems to be doing exactly what you describe it is. What's the problem?
it's failing because you're trying to subtract an int (Bet) from a string (money). You need to convert the string you read in from the money file into an int. You can use the built-in function int(string) to do that.
Short answer, you don't do this. Usually when you want to get values from a thread you pass it a queue, and have a your main thread get values from the same queue.
A couple other things: you're calling join(), which will block until the thread is finished making the while loop directly after it unnecessary, and don't use "str" as a variable name since it's already the name of a builtin function.
import threading
import Queue
def stringFunction(value, out_queue):
my_str = "This is string no. " + value
out_queue.put(my_str)
my_queue = Queue.Queue()
thread1 = threading.Thread(stringFunction("one", my_queue))
thread1.start()
thread1.join()
func_value = my_queue.get()
print func_value
Always use absolute path+file names.
filePath = "Dataset/parameter feature vectors" for fname in os.listdir(filePath): complete_name = os.path.join(filePath, fname) data_str = open(complete_name).read() index = data_str.find("female") if index != -1: females.append(index) print fname else: print "append the ones that aren't female to a males"
I don't think this is doing what bol0gna wants actually. In the original post bol0gna wants to sort on the filenames, while your code is actually searching the content of each file. Here's a version that works on the filename:
filePath = "Dataset/parameter feature vectors"
for fname in os.listdir(filePath):
if fname.count('female'):
females.append(fname)
elif fname.count('male'):
males.append(fname)
There's a couple problems in your play_choice function it looks like:
def play_choice():
play_ch = raw_input #You're setting "play_ch" to the actual raw_input function. To get the return value from the function it should be "play_ch = raw_input()"
while play_ch != 1 and play_ch != 2 and play_ch != 3:
print 'Invalid choice'
play_ch = ( input( 'Enter a valid choice:' ) ) # Should be using "raw_input" function here.
return play_ch
Here's how I would approach this:
(1) for each entry in L2 find all the matching sub-lists from L1. You can use a list comprehension or a for loop for this.
(2) Add all the sub-lists that matched into a single list
(3) Reduce the list containing all the sub-lists to just the unique entries.
To get you started here's the code for the first step using a for loop:
L1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
L2 = [1,2,3,4,11]
for item in L2:
# entry will contain all the sub lists from L1 that contain the item
entry = []
for sub_list in L1:
if item in sub_list:
entry.append(sub_list)
You can use the builtin function setattr to do this. See http://docs.python.org/library/functions.html#setattr.
The problem is that you're creating a raw string, which isn't doing what you think it will. Instead of allowing you to create a hex value attached to the '\x' escape, it's instead creating a string that contains the characters exactly as '\' + 'x' + 'f' + '4'.
Here's a version that will do what you want (I hope :) ):
import re
def convert_to_unicode(match_obj):
raw_chr_val = match_obj.group(1)
return unichr(int(raw_chr_val, 16))
def func(mylist):
for e in mylist:
e = re.sub(' ?&# x([0-9a-f]*);', convert_to_unicode, e)
print e
mylist = ['12 angry men', 'Rash &# xf4;mon']
func(mylist)
I think your problem is that in the call to re.sub you're replacing the match with a raw string. Try removing the 'r' prefacing the replacement string.
Won't that the process of opening and closing the file cause it to get overwritten, instead of the line being appended?
Not if you open it in append mode. That means that anything previously in the file will stay there, and this might not be desirable on the first write (since you want the file to contain only data from the current run presumably). To fix this just keep track of whether you've written to the file before; if you have open it in append mode, otherwise open it in write mode.
Ok, here is what I am doing:
First my code scans a large data file. In column 10, the genetic data is listed by "name", and there are about 110 different names total. Whenever it gets to a new name, it stores it in a list, so I have something like:
Name_List = ['AluSp', 'AluGp', 'AluSx' ... 'ZZcta' ]
For each name in the name list, I want to store a file of the same name, eg:
AluSp.bed, AluGp.bed, AluSx.bed...ZZcta.bed
The program will rescan the data file, and for each name, that line gets written into the appropriate file.
Because the raw data file is so large, I can't just open up one file for a name in the list, scan the whole data file, append entries for only the first name (AluSp.bed) and then close. Doing so would require a 110 scans of a 50 million line file. What I am doing is scanning the raw data file once, while all the 110 name files remain open, and for each line in the data file, that line gets written to the appropriate name file.
Is that clear?
The code is in place, so if you'd prefer to look at it, I can post it.
There's no need to keep all the files open at the same time though. Instead of having a list of 100 file handles replace it with a list of the filenames, then each time you want to write to one of them just open the file, write …
Seems like a good case for a regular expression
import re
test_input = "AND Category 07|Spec 01|ABC 01 AND Category 07|Spec 02|XYZ 02 AND Category 07|Spec 03|PQR 03 "
test_output = re.sub('\|[A-Z]{3} [0-9]{2}', '', test_input)
There's always a more clever way :-)
start = 10
end = 20
open(outfile, 'w').writelines(open(infile).readlines()[start:end])