Forum: Python Oct 24th, 2009 |
| Replies: 6 Views: 328 Hi, actually you *don't* check. You assume and use data as if it is of the correct type. Pythons run-time checks would throw an exception if they are not, and the programmers necessary knowledge of... |
Forum: Python Oct 24th, 2009 |
| Replies: 4 Views: 624 The cure seems worse than the disease.
have you actually tried returning a tuple of all results and the caller just picking the ones it needs?
- Paddy. |
Forum: Python Oct 24th, 2009 |
| Replies: 12 Views: 370 Even competent programmers will never know all of C++. The bit of C++ you need to be proficient in is the C language.
You might point out that "if Python is good enough for MIT ..."
:-)
-... |
Forum: Python Oct 24th, 2009 |
| Replies: 6 Views: 328 If data is truly coming from an untrusted source, then validate it to death as soon as the dataappears in your program, but don't sprinkle checks thoughout your source. Assume fellow programmers know... |
Forum: Python Mar 23rd, 2009 |
| Replies: 11 Views: 681 When you run a CPython program you won't see the size of the process shrink, from the OS POV. unused objects of all types are garbage collected, and the memory made available for later use by that... |
Forum: Python Mar 23rd, 2009 |
| Replies: 11 Views: 681 Use whatever is easiest to write the code in.
Get it right. Profile; then see if you need to worry about optimisation.
Once you have a working program, you often find that optimisations are... |
Forum: Python Feb 19th, 2009 |
| Replies: 8 Views: 515 If you are just keeping a table of fixed fields that are to be accessed by name, then you might use a dictionary of tuples or namedtuples.
Here is an example using named tuples (Python 2.6),... |
Forum: Python Feb 19th, 2009 |
| Replies: 8 Views: 515 Earlier I said:
The following shows this, simpler method in action, I would use this in preference if it applies:
>>> class Class2(object):
pass
>>> inst = {}
>>> inst['the_name'] =... |
Forum: Python Feb 19th, 2009 |
| Replies: 8 Views: 515 following on from my earlier post:
>>> x.value = 10
>>> x.weight = 1000
>>> y.value = 20
>>> y.weight = 2000
>>> x.name2inst('the_other_name').value
20
>>>... |
Forum: Python Feb 18th, 2009 |
| Replies: 8 Views: 515 You might try keeping a class variable that maps all names to named instances, intercepting any setting of .name via properties:
>>> class Class(object):
_name2inst = dict()
@property
def... |
Forum: Python Jan 16th, 2009 |
| Replies: 13 Views: 755 '''
>>>
Year 0 Cash = principal = 100
Year 1 Cash = (principal) * (1.0 + rate) = 101.0
Year 2 Cash = ((principal) * (1.0 + rate)) * (1.0 + rate) = 102.01
Year 3 Cash = (((principal) * (1.0 +... |
Forum: Python Jan 16th, 2009 |
| Replies: 13 Views: 755 for i in range(year):
principal += principal * interest_rate
- Have fun! |
Forum: Python Dec 18th, 2008 |
| Replies: 4 Views: 383 Listen to the guy who told you to use a list.
If you don't know about exec, your probably going about solving your problem in the wrong way by using it. Try thinking of how you might solve things... |
Forum: Python Nov 25th, 2008 |
| Replies: 3 Views: 407 You seem to be wanting to store a collection of information. Have a look at lists, sets and dictionaries in the python documentation.
- Paddy. |
Forum: Python Jul 2nd, 2008 |
| Replies: 7 Views: 1,039 Try re.finditer as in:
def patternMatching(pattern, string):
print '\n'.join(string[matchobj.start():matchobj.end()]
for matchobj in re.finditer(pattern, string))
-... |
Forum: Python Jun 23rd, 2008 |
| Replies: 2 Views: 1,277 Hi,
I am not sure how you access the P_LastName field in a member of q.
If it is something like q[0].P_LastName then use the key argument to sort like this:
q.sort(key = lambda x: x.P_LastName)... |
Forum: Python Jun 23rd, 2008 |
| Replies: 7 Views: 666 Here is a Python 2.5 version that uses groupby and generator comprehensions.
I guess its not much use if you haven't got to them in your Python studies as yet but here goes:
>>> from StringIO... |
Forum: Python Jun 11th, 2008 |
| Replies: 3 Views: 831 Try here (http://www.python.org/doc/2.4.3/whatsnew/whatsnew24.html).
As someone pointed out, we are now at 2.5 (http://docs.python.org/whatsnew/whatsnew25.html)
- Paddy. |
Forum: Python Apr 24th, 2008 |
| Replies: 6 Views: 581 what functions in other languages did you use? |
Forum: Python Oct 20th, 2007 |
| Replies: 5 Views: 2,570 Not really. For your large project you factor and re-factor the code, to improve maintainability which also improves readability.
People have add-hoc rules like "a function should fit on a... |
Forum: Python Oct 19th, 2007 |
| Replies: 1 Views: 5,588 This might help:
http://effbot.org/librarybook/csv.htm |
Forum: Python Oct 19th, 2007 |
| Replies: 8 Views: 1,962 What have you got so far?
How do you have the data represented? |
Forum: Python Oct 15th, 2007 |
| Replies: 3 Views: 684 Try http://wiki.python.org/moin/BeginnersGuide
- Paddy. |
Forum: Python Oct 15th, 2007 |
| Replies: 3 Views: 1,617 One of the reasons for encompassing most of your code in a function, it does not have to be main, but many choose to use main, is that it allows you to use this idiom of having your program also act... |
Forum: Python Oct 8th, 2007 |
| Replies: 11 Views: 1,612 This should help:
>>> s = 'CAnADA'
>>> s.capitalize()
'Canada'
>>>
- Paddy. |
Forum: Python Oct 7th, 2007 |
| Replies: 11 Views: 1,612 You might also try:
list_in = ["CANADA", "HELLO", "I TOLD YOU SO"]
list_out = []
for element in list_in:
list_out.append(element.lower()]
You should find it easier to find out what the... |
Forum: Python Sep 15th, 2007 |
| Replies: 2 Views: 1,690 If you are returning longs you can selectively turn them into integers using int() e.g:
>>> a = ((11L,), (12L,), (13L,), (14L,), (15L,), (166666666666L,))
>>> b = tuple((int(i[0]),) for i in a)... |
Forum: Python Sep 10th, 2007 |
| Replies: 7 Views: 4,671 And it helps when validating input data. Someone can't insert text to remove all files into the middle of your input file and have it blindly executed by eval :)
- Paddy. |
Forum: IT Professionals' Lounge Sep 9th, 2007 |
| Replies: 13 Views: 2,736 When given a codebase to understand, automatically generated overviews such as UML diagrams and maybe Doxygen views might help and seldom hinder.
When given a task and a clean slate, then you... |
Forum: Python Sep 9th, 2007 |
| Replies: 3 Views: 6,129 If you install wxpython and run the demo there are examples of grid displays given.
As other posters have said, use the CSV module to work with CSV as it is very flexible as well as being easy to... |
Forum: Python Sep 9th, 2007 |
| Replies: 7 Views: 4,671 Instead of:
my_list = [eval(n) for n in my_line.split(None)]
of answer #3 of vegaseat, it is always better to reduce use of eval to an absolute minimum, so if you know it's a file of floats... |
Forum: Python Sep 9th, 2007 |
| Replies: 6 Views: 963 Check out the mutable string class past the middle of this page (http://docs.python.org/lib/module-UserString.html) |
Forum: Python Sep 8th, 2007 |
| Replies: 6 Views: 963 Strings are known as immutable. You cannot take a string object and change its value without creating another string, so although you can have two names refer to the same string object, when you... |