Forum: Python May 3rd, 2009 |
| Replies: 5 Views: 461 I don't know of one. This code requires a radial coordinate.
class Pt(object):
def __init__(self, x=0.0, y=0.0, z=0.0):
self.x = x
self.y = y
self.z = z
... |
Forum: Python Apr 6th, 2009 |
| Replies: 4 Views: 959 The readlines() file method returns a list. The read() method returns a string. A str object has no insert() method but a list object does. |
Forum: Python Apr 6th, 2009 |
| Replies: 4 Views: 959 Changefilestring = f.read() tofilestring = f.readlines()
-BV |
Forum: Python Dec 21st, 2008 |
| Replies: 2 Views: 350 Here is one way to do it:
>>> class test(object):
... def change_var(self, name, value):
... globals()[name] = value
...
>>> var = 123
>>> obj = test()
>>> obj.change_var('var', 456)... |
Forum: Python Dec 4th, 2008 |
| Replies: 4 Views: 681 If you want to determine if a number is prime or not, you should return True (if prime) or False (if not prime). Use the modulo operator to determine if the entered number is divisible by loop... |
Forum: Python Dec 3rd, 2008 |
| Replies: 5 Views: 2,509 I like using list comprehensions for this kind of task.
>>> cols = 4
>>> rows = 5
>>> array = [[0 for i in range(cols)] for j in range(rows)]
>>> array
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0],... |
Forum: Python Nov 24th, 2008 |
| Replies: 3 Views: 403 It depends on the type of data and how you intend to use it.
>>> adam = "john"
>>> adam += "mike"
>>> adam
'johnmike'
>>> ' '.join([adam, 'fred'])
'johnmike fred'
>>> |
Forum: Python Nov 10th, 2008 |
| Replies: 5 Views: 865 If I understand you correctly, you want to substitute the "VALUE" of a residence feature which would be a "KEY" in a dictionary. For each different value, there will be a different key. A dictionary... |
Forum: Python Nov 5th, 2008 |
| Replies: 5 Views: 727 I guess the rotate method does not return the rotated object. Try this:for n,r in enumerate([90,180,270]):
im1.rotate(r).crop(box) # ? im1 = im1.rotate(r).crop(box)
im1.save("screen%s.jpg"... |
Forum: Python Nov 5th, 2008 |
| Replies: 5 Views: 727 #2 I am not sure, but methods crop() and rotate() should return image objects. If so, you could simply:for n in range(1, 4):
im1 = im1.rotate(90).crop(box)
im1.save("screen%s.jpg" % n) |
Forum: Python Nov 4th, 2008 |
| Replies: 2 Views: 1,249 Sometimes it's easier to show some code:
import time, datetime
data = '2008-10-27 12:05:54'
fmt = '%Y-%m-%d %H:%M:%S'
ts1 = time.strptime(data, fmt)
dt1 =... |
Forum: Python Oct 30th, 2008 |
| Replies: 6 Views: 996 You must close the file object to flush the output buffers. You can manually flush the buffers with file method flush(). |
Forum: Python Oct 24th, 2008 |
| Replies: 9 Views: 1,901 random.sample() may be what you are looking for.
>>> let = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ]
>>> random.sample(let,len(let))
['a', 'd', 'g', 'b', 'c', 'f', 'e']
>>> random.sample(let,3)... |
Forum: Python Oct 8th, 2008 |
| Replies: 16 Views: 1,624 Andy,
No forgiveness necessary. Doing exercises like this is a good way to learn. That's one reason I am doing it. |
Forum: Python Oct 8th, 2008 |
| Replies: 16 Views: 1,624 I posted to a similar thread recently, but I can't remember where. The following will preserve punctuation:from string import punctuation as stuff
def words_between(s, first, second):
#... |
Forum: Python Sep 26th, 2008 |
| Replies: 13 Views: 1,496 I played around with a function that returns a list of factors a few months ago. You only need to check numbers up to sqrt(number).def factors(num):
outList = [i for i in xrange(1,... |
Forum: Python Sep 26th, 2008 |
| Replies: 10 Views: 1,561 Not to be picky, but an iterable can be converted to a list with the built-in function list.
>>> import string
>>> list(string.letters)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',... |
Forum: Python Sep 19th, 2008 |
| Replies: 2 Views: 2,227 Use string method strip() to remove newline characters.
for line in lines:
filemenu.add_cascade(label=line.strip()) |
Forum: Python Aug 30th, 2008 |
| Replies: 6 Views: 534 You can use escape sequence \b to match exact words.
import re
s = "I'm trying to debug a problem but the debugger is not working"
target = 'debug'
repl = 'fix'
patt = re.compile(r'\b%s\b'... |
Forum: Python Aug 28th, 2008 |
| Replies: 3 Views: 1,487 Since I am using Python 2.3, I will define a function to pass to sort().
def comp(a,b):
return cmp(a[5], b[5])
f = open('sample_data.txt')
data_list = [[float(item) for item in... |
Forum: Python Aug 24th, 2008 |
| Replies: 6 Views: 832 File method readline() reads an entire line from an open file at the file's current position. The file position is relocated. File method tell() returns the current position. Example:>>> f =... |
Forum: Python Aug 22nd, 2008 |
| Replies: 8 Views: 674 You don't need regex for this:
>>> array = ['red','blue']
>>> array1 = [' %s ' % item for item in array]
>>> array1
[' red ', ' blue ']
>>> |
Forum: Python Jul 28th, 2008 |
| Replies: 6 Views: 1,362 By adding a __cmp__ method to class Movie, you can sort instance objects in list m1 with list method sort(). Following is an example of a __cmp__ overload in a Vector class that sorts on the x, y and... |
Forum: Python Jul 25th, 2008 |
| Replies: 2 Views: 788 You can use string formatting for your regex pattern. The following iterates on the file object. For the top 10 players, variable data should have 40 items. Match object m.groups() should have 5... |
Forum: Python Jul 3rd, 2008 |
| Replies: 2 Views: 1,026 You are returning a tuple in cun_to_centimeters(). Access the elements of the tuple by slicing.
Example:
>>> def ret_tuple():
... return 1, 2
...
>>> obj = ret_tuple()
>>> obj[0]
1
>>>... |
Forum: Python Jul 1st, 2008 |
| Replies: 4 Views: 1,595 >>> s = 'XXXXX -\n\t XXXX'
>>> ''.join(s.split())
'XXXXX-XXXX'
>>> |
Forum: Python Jun 22nd, 2008 |
| Replies: 7 Views: 659 Here is an example using woooee's suggestion:
s = '''ABCD vvvv 1e-12
ABCD hhhh 1e-6
ABCD ggggg 1e-3
ASDE ffffff 1e-57
ASDE dddd 0.001'''
dd = {}
for item in s.split('\n'):
itemList =... |
Forum: Python May 27th, 2008 |
| Replies: 6 Views: 707 ich1,
A class that inherits from object is called a "new-style" class. "Old-style" or "classic" classes are likely to be deprecated some time in the future. |
Forum: Python May 25th, 2008 |
| Replies: 6 Views: 707 Inheritance! Your new class object fruitcake will inherit the attributes of object. See this snippet: http://www.daniweb.com/code/snippet354.html |
Forum: Python May 21st, 2008 |
| Replies: 1 Views: 493 Close the log file, then open again for reading.log_file.close()
log_file = open('%s' % os.path.join(logfile_path, file))
lineList = log_file.readlines()
log_file.close() |
Forum: Python May 8th, 2008 |
| Replies: 3 Views: 707 Example using the timeit module:
def my_function():
return results
if __name__ == '__main__':
import timeit
t = timeit.Timer("my_function()", "from __main__ import my_function")... |
Forum: Python May 7th, 2008 |
| Replies: 10 Views: 2,467 Try this:
csv.reader(str(self[filename]).split('\n')The argument to csv.reader can be any iterable object that produces a string each time its next() method is called. |
Forum: Python May 7th, 2008 |
| Replies: 10 Views: 2,467 self[filename] does not appear to be a valid file name. Following is an example of a valid file name:
r'H:\Zip_Files\618 Johnston\IFA040308\24747-IFA040308.zip' |
Forum: Python May 6th, 2008 |
| Replies: 10 Views: 2,467 The csv module is ideal for your parsing your data.
import csv
fn = 'data.csv'
f = open(fn)
reader = csv.reader(f)
headerList = reader.next()
outputList = []
for line in reader:
# test... |
Forum: Python Mar 13th, 2007 |
| Replies: 4 Views: 1,516 's.template' contains the original string passed to Template().
s.substitute() returns a string object which can be assigned to a variable.
HTH |
Forum: Python Mar 13th, 2007 |
| Replies: 6 Views: 2,470 If I understand you correctly, there are several ways:
# Update the local namespace
>>> for i in range(25):
... exec "%s = %d" % ('var'+str(i), 0) in None
# Update the global namespace... |
Forum: Python Mar 9th, 2007 |
| Replies: 3 Views: 1,971 >>> def hyp(n):
... if isinstance(n, (float, int)):
... return (2*(n*n))**0.5
... else:
... raise ValueError, 'Invalid argument'
...
>>> hyp(3)
4.2426406871192848... |