Forum: Python 16 Hours Ago |
| Replies: 2 Views: 80 I think it maintains the traditional distinction between allocation and initialization. When you use structures in C for example, you must first use malloc to allocate a structure and then you... |
Forum: Python 1 Day Ago |
| Replies: 18 Views: 224 You can get a drive letter from a path like this
import os
drive = os.path.splitdrive(mypath)[0]
print(drive)
Now for mypath, you could use either the current working dir os.getcwd() or your... |
Forum: Python 2 Days Ago |
| Replies: 5 Views: 185 You can speed this up:
def sine(x):
term = float(x)
result = term # avoid 'sum' which is the name of a builtin function
u = - term * term # this is minus (x squared)
n = 0 #... |
Forum: Python 3 Days Ago |
| Replies: 4 Views: 155 You probably still have the wrong indentation for the line
status = staticmethod(status). |
Forum: Python 3 Days Ago |
| Replies: 13 Views: 304 I see. Note that handling errors by the means of a return status is not very pythonic. It's a C like way of thinking. A more pythonic approach would be to define a
class LibraryError(Exception):... |
Forum: Python 3 Days Ago |
| Replies: 4 Views: 155 The indentation is bad. The code should read
# Classy Critter
# Demonstrates class attributes and static methods
class Critter(object):
"""A virtual pet"""
total = 0 |
Forum: Python 3 Days Ago |
| Replies: 13 Views: 304 Nice. Just one criticism, I don't like your
except:
return False
because you're just hiding errors, so that if something goes wrong, your program will never know. You should think about... |
Forum: Python 4 Days Ago |
| Replies: 13 Views: 304 I prefer design 1. We don't know anything about the algorithms that you have in mind, so there is no obvious benefit for introducing the classes Search and Edit. With design 1, the Library object... |
Forum: Python 7 Days Ago |
| Replies: 5 Views: 209 I modified your code so that it prints the file name and the destination name instead of renaming the file. Run it and modifiy it until it gives the correct values, then uncomment the 'rename' line.... |
Forum: Python 10 Days Ago |
| Replies: 7 Views: 7,004 You can write
from os.path import join as pjoin
targetfolder = raw_input('enter a target folder:\n').strip()
newname = raw_input('enter a new base name:\n')
newname = pjoin(targetfolder,... |
Forum: Python 13 Days Ago |
| Replies: 5 Views: 277 Google found a patch for scapy.all..sniff, wich allows to stop the sniff function programmatically. You should try this http://trac.secdev.org/scapy/wiki/PatchSelectStopperTimeout |
Forum: Python 14 Days Ago |
| Replies: 6 Views: 253 You can write
if colour in ("Green", "Blue", "Red"):
... # do something
else:
... # do something else |
Forum: Python 15 Days Ago |
| Replies: 9 Views: 295 Note that in python 3, there are no 'old style class', so that your class is implicitely a subclass of object. Here is a small experiment with python 2.6 and python 3.1
Python 2.6.4 (r264:75706,... |
Forum: Python 16 Days Ago |
| Replies: 14 Views: 539 Is this what you mean ?
class Agent:
def __init__(self):
exec "import random" in self.__dict__
ag = Agent()
print ag.random.randrange(100) # this works |
Forum: Python 17 Days Ago |
| Replies: 7 Views: 216 It's true, suppose that mypackage contains __init__.py, foo.py, bar.py and baz.py. You could have this in __init__.py
import foo
import bar
import baz
then, if you simply write import... |
Forum: Python 23 Days Ago |
| Replies: 8 Views: 250 I ran pylint (http://www.logilab.org/project/pylint) on this code. Here is the result (note that this depends on my personal configuration of pylint, it may give a different result on your system)
... |
Forum: Python 24 Days Ago |
| Replies: 4 Views: 215 You can also use format
message = "z's value is {z:0{digits}n}".format(z=2, digits=3)
print(message) |
Forum: Python 25 Days Ago |
| Replies: 6 Views: 336 Here is a script which should work for 1 .pdb file and many .pd files. It creates the new versions of the .pd files (with less lines) in a separate output directory. See the usage at the end of the... |
Forum: Python 26 Days Ago |
| Replies: 3 Views: 190 When you open a file, the cursor is at the beginning of the file. However
myfile.seek(0)
goes to the beginning of the file. Finally, read this.... |
Forum: Python 28 Days Ago |
| Replies: 4 Views: 223 Did you read this in your assingment ?
Although it can be helpful to you to discuss your program with other people, and that is a reasonable thing to do
and a good way to learn, the work you hand... |
Forum: Python 31 Days Ago |
| Replies: 18 Views: 484 In the applications menu of your desktop panel, you select install software (you need the root password for this). This opens the software manager, which has a search form. Put libtcl in the search... |
Forum: Python 31 Days Ago |
| Replies: 9 Views: 280 There are small differences, for example in Pickle, there are Pickler and Unpickler classes which don't exist in cPickle. Often such double implementations exist because someone first wrote a python... |
Forum: Python 31 Days Ago |
| Replies: 9 Views: 280 The standard library has examples of modules wich offer 2 implementations, a C and a pure python implementation. For example Pickle and cPickle and StringIO and cStringIO. For most problems, you can... |
Forum: Python 31 Days Ago |
| Replies: 9 Views: 280 I think it's only the terminology of the python documentation. Usually the documentation opposes 'user defined types' implemented in python to 'built in types' implemented in C. |
Forum: Python 31 Days Ago |
| Replies: 9 Views: 280 There are many examples of extensions written in C or C++. For example the numpy package implements classes written in C.
One of the main reasons to do this is that C or C++ run much faster than... |
Forum: Python 32 Days Ago |
| Replies: 3 Views: 173 try
string2 = "Random Number is\n{value:^16}".format(value = string1)
Also, you can read this (http://www.daniweb.com/code/snippet232375.html) :) |
Forum: Python 33 Days Ago |
| Replies: 5 Views: 345 I discovered a new python book today http://www.linuxtopia.org/online_books/programming_books/python_programming/index.html. |
Forum: Python Oct 25th, 2009 |
| Replies: 9 Views: 333 |
Forum: Python Oct 25th, 2009 |
| Replies: 2 Views: 304 This should work
value = min(x for x in temperature_sequence if x > 0)
Note: this raises ValueError if all temperatures are below 0. |
Forum: Python Oct 22nd, 2009 |
| Replies: 12 Views: 1,189 The floating point precision can be passed as argument too
rabbits = {
"flopsy" : 1.0/3, "mopsy" : 576.0/7, "cotton tail": .76/5, "peter": 300000.0/37,
}
nwidth = 1 + max(len(name) for... |
Forum: Python Oct 22nd, 2009 |
| Replies: 12 Views: 1,189 Same example with computed field's width:
rabbits = {
"flopsy" : 1.0/3, "mopsy" : 576.0/7, "cotton tail": .76/5, "peter": 300000.0/37,
}
nwidth = 1 + max(len(name) for name in rabbits)
... |
Forum: Python Oct 22nd, 2009 |
| Replies: 12 Views: 1,189 The syntax of the str.format() method described in the python 2.6 documentation (http://docs.python.org/library/string.html#formatstrings) looks both powerful and complex. The idea of this thread is... |
Forum: Python Oct 21st, 2009 |
| Replies: 2 Views: 225 s='1234444432187667890000568984'
print("\n".join(("I found %d: %d" % (s.count(str(i)), i)) for i in range(10))) |
Forum: Python Oct 20th, 2009 |
| Replies: 4 Views: 208 I wrote a code snippet (http://www.daniweb.com/code/snippet217111.html) for this last year :) |
Forum: Python Oct 20th, 2009 |
| Replies: 4 Views: 302 Ok, you can change the text with
l.setText(str(300)) # the text now shows 300
note that the letter 'l' is a bad variable name. Good variable names have at least 3 characters (except for loop... |
Forum: Python Oct 19th, 2009 |
| Replies: 13 Views: 488 I think I understand your point now. There must be a limitation somewhere when n is large. However, note that the builtin shuffle function takes an optional argument 'random' which allows you to pass... |
Forum: Python Oct 19th, 2009 |
| Replies: 8 Views: 213 A shorter method
L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
L2=[1,2,3,4,11]
set2 = set(L2)
L3 = [x for x in L1 if set2.intersection(x)]
print(L3) |
Forum: Python Oct 18th, 2009 |
| Replies: 6 Views: 295 you should use getattr(random, name) |
Forum: Python Oct 16th, 2009 |
| Replies: 6 Views: 3,270 @@pythonuser18 For reference, I got it working, with python 2.6 and this last line:
print DecToRom(int(raw_input("Decimal number: "))) |
Forum: Python Oct 15th, 2009 |
| Replies: 5 Views: 251 Objects are the result of years of programming practice by thousands of programmers. You need some time to see the benefit. The idea is that an object holds a certain quantity of data. The object's... |