2,646 Posted Topics
Re: I got closest_pair1(mylist) = 0.000008 seconds closest_pair2(mylist) = 0.000038 seconds closest_pair3(mylist) = 0.000040 seconds closest_pair4(mylist) = 0.000010 seconds closest_pair5(mylist) = 0.000041 seconds with an AMD Athlon(tm) X2 245 Processor | |
Re: You can also try to read the file by chunks import io MB = 1 << 20 # read the file by chunks of 64 megabytes with io.open('input.txt', mode="r+b", buffering = 64 * MB) as infile: # etc | |
Re: > dear list i am new to python and write few basic programs in python now i want learn multithrading in pyhton , can anyone please provide me some link or guidance , how to go through it. Start by reading [python module of the week: threading](http://blog.doughellmann.com/2008/01/pymotw-threading_13.html). | |
Re: In your code, `num` is a string, and `numin` is an integer. It means that the arithmetic expressions involving `num` won't work as expected. The best thing to do is to replace line 2 with `num = int(num)` and forget about `numin`. You can even write `num = int(num) % … | |
Re: Your input window is idle's shell window. There is an option to change the title of the shell window when you start idle. Start idle with idle -t "Python input" from a terminal. | |
Re: Because this call invokes an underlying C++ function which uses argv and doesn't know about python. Qt understand a set of command line arguments, see [here](http://doc.qt.digia.com/qt/qapplication.html#QApplication) for example. | |
Re: > How does python know that card must be of class Card? Python does not know. You could add anything with this method. There is no type checking at run time. What you could do is add an assert statement def add_card(self, card): assert isinstance(card, Card) return self.hand.append(card) | |
Re: If you don't want to generate the page dynamically, you can generate it once a week and store an html file when you run your script. You could use a template engine like Mako for variable substitution in the html file. It is very easy to do. | |
Re: I would try a single regular expression pat = r"(?:\d{1,2}(?:(?:(?: \w{3})? - \d{1,2})? )?(?:\w{3} \d{4}))" I don't know about the mongodb part. Use kodos to debug python regexes. | |
Re: Hint: if there is a vertex at a point `(x0, y0)`, the other vertices are at points x, y = (x0+ a * i + b * j, y0 + c * j) where `a, b, c` are 3 given numbers and `i, j` are varying integers. | |
Re: A reason could be that the programmer wants the Asset to be a dictionary, and at the same time he/she wants to add asset-specific behavior to this dictionary. To do so, he/she subclasses the built-in `dict` class. I don't think adding the properties name, scene, show, image is a very … | |
Re: Start with some code for the > ask the user-for year month and date part, you need the `input()` function (in python 3) or `raw_input()` (in python 2). | |
Re: I'm using Ulipad 4.1, a nice tool written in wxPython. | |
Re: Add e:\pythonpacks to the PYTHONPATH environment variable (or create the variable). | |
Re: Use the fractions module >>> from fractions import Fraction >>> day = 23 + Fraction(56, 60) >>> day Fraction(359, 15) >>> 60 * day * 7 Fraction(10052, 1) | |
This python 2.7 snippet adds a thin layer of sugar on the itertools module's api, allowing many of its functions to be used as decorators and adding some new functions. Enjoy ! | |
Re: Here is how to display the base64 image using tkinter and PIL in python 2 if __name__ == "__main__": from StringIO import StringIO import Tkinter from PIL import ImageTk, Image root = Tkinter.Tk() #root.bind("<Button>", lambda event: event.widget.quit()) root.geometry('+%d+%d' % (100,100)) f = StringIO(base64.decodestring(str(rainbow_jpg_b64))) image = Image.open(f) zoom = 5 image … | |
Re: I suggest using a [cached property](http://www.daniweb.com/software-development/python/code/217241/a-cached-property-decorator) class HoldAttr(object): @cached_property def foundat(self): return [] @property def was_found(self): return "foundat" in self.__dict__ if __name__ == "__main__": h = HoldAttr() for i in range(3): h.foundat.append(i) print(h.foundat) print(h.was_found) h = HoldAttr() print(h.was_found) """ my output --> [0, 1, 2] True False """ However, a … | |
Re: Here is one import nametest nametest.__name__ = "foobar" nametest.showName() | |
Re: Read the csv module's documentation! Here, you need at least to pass `delimiter=';'` to `csv.reader()`. If necessary, play with dialects and formatting parameters until you get the correct result. | |
Re: A good way to do this is to have a folder for your python modules and to set the environment variable PYTHONPATH to contain this folder. If you're on windows, search for environment variables in windows help and set the correct value. In this folder, you can create files like … | |
Re: Hello, I don't use 3D myself, but I have a few links: [mayavi](http://docs.enthought.com/mayavi/mayavi/) [collection of links](http://www.vrplumber.com/py3d.py) [freecad](http://sourceforge.net/projects/free-cad/) [vpython](http://www.vpython.org/) I hope they will help you find your solution. | |
Re: [This post](http://stackoverflow.com/questions/405058/line-reading-chokes-on-0x1a) says to open the file in mode 'rb' or 'rU'. | |
Re: There is a way to ask python what are start, stop and step: >>> s = slice(None, None, -2) # a slice object corresponding to [::-2] >>> s.indices(8) (7, -1, -2) # start, stop, step if the slice is applied to a tuple of length 8 | |
Re: It means that a function tried to call an attribute getitem on a float object, like this code >>> x = 3.14 >>> x.getitem Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'float' object has no attribute 'getitem' Here are similar errors >>> x = 3.14 … | |
Re: > Can you explain me shortly how that if statement works? When a python file `foo.py` is executed, a variable `__name__` is inserted in its namespace. If `foo.py` is ran as the main script, the value of this variable is the string `"__main__"`. If instead `foo.py` is imported from another … | |
Re: Start by making a list containing the lengths of the 1000 sequences. Tony is right, we're dying to see the code. ![]() | |
Re: Add `return p_num` at the end of the function. Your function currently returns `None`. The `p_num` inside the function is not the same as the p_num outside the function (because it appears in the left part of assignment statements). It is a local variable. You need a return statement to … | |
Re: So the function calculates the barycentre of a set of points in a multidimensional space. | |
Re: [See the light](http://docs.python.org/using/windows.html) with python guru Mark Hammond. | |
Re: This should work def funcdist(myvar): return [[ sum(1 for (a, b) in zip(line_a, line_b) if a != b) for line_b in myarr] for line_a in myarr ] The problem in your snippet, is that a new testarr needs to be created in the hx loop. Replace line 6 by line … | |
Re: Yes it is possible: a, b = zip(*(((item*10),(item*20)) for item in items)) but it gives tuples instead of lists. If you want lists, you can write a, b = (list(x) for x in zip(*(((item*10),(item*20)) for item in items))) another way is a, b = (list(item * x for item in … | |
Re: The other solution is to add `shell = True` to `subprocess.call()`'s arguments. | |
Re: You can go one step further and write for i in range(3): for w,x,y,z in zip(*(v.values for v in (ar[i+1], br[i+1], ar[i], br[i]))): val = (sqrt((w.data**2)+(x.data**2)))-(sqrt((y.data**2)+(z.data**2))) files[i].write(('%6.20f\n') % (val)) provided you define lists `ar` and `br` containing `[ar0, ar1, ar2, ar3]` etc instead of variables whith numbered names. | |
Re: Compute `isomorphic(nestA, nestB)` by hand without a computer and note carefully every single step of your reasonning. This will give you pseudo code. Then write the pseudo code, then implement. | |
Re: Your class should read class Environment(object): def __init__(self,rowCount,columnCount): self.env = [[ None for i in range(columnCount)] for j in range(rowCount) ] def setElement(self, row, column, value = 0): self.env[row][column] = value def getElement(self, row, column): return self.env[row][column] myenv = Environment(6, 6) # create an instance myenv.setElement(2, 2) myenv.setElement(3, 4, 1000) … | |
Re: Did you run the examples in the csv module's documentation ? | |
Re: Well, print('animal' if word in animals else 'drinks') for example. | |
This snippet defines a `dir()` function which extends python's builtin `dir()` function by adding options to filter the returned sequence of strings by the means of regular expressions. The main use case is live inspection of python objects and modules during an interactive session with the python interpreter. The idea … | |
Re: Use [this snippet](http://www.daniweb.com/software-development/python/code/257449/a-command-class-to-run-shell-commands) and write com = Command("cat /tmp/file.txt | grep ListenPort | awk -F").run() if com.failed: print com.error else: print com.output (correct the end of th awk command if necessary) | |
Re: I would try the root solvers in [scipy.optimize](http://docs.scipy.org/doc/scipy/reference/optimize.html) first (like scipy.optimize.fsolve or scipy.optimize.bisect, etc...). Theoretically speaking, this problem depends mainly on the monotonicity properties of the values in your array. Are they increasing or decreasing values, or randomly chosen values (or something else?). These properties should govern your strategy. For … | |
Re: Usually, I'm using [this cached_property decorator](http://www.daniweb.com/software-development/python/code/217241/a-cached-property-decorator). Perhaps you could me more specific about your problem. A property usually means a computed attribute. I don't see an instance or an attribute in your post above, so you may be looking for something else. | |
Re: Two nested with statements work in python 2.6; If your code fails, post the code and the traceback. | |
Re: The third item in os.walk is a list of filenames. Otherwise, the md5 of a file refers to the content of the file, not to the filename. Run your code before posting issues! | |
Re: It's impossible. How can you tell if 101110 means 10 11 10 or 101 110 ? What you can do is format every char with 3 digits >>> c = "\x0a" >>> ord(c) 10 >>> str(ord(c)) '10' >>> "{0:0>3}".format(ord(c)) '010' | |
Re: Line 7 should go before the for loop. When it is executed, scores becomes a new empty list. Also line 10 should be `scores[:] = []`, otherwise it does nothing. It is better to write all the function definitions out of the for loop (before the loop). A function definition … | |
Re: Well, you have >>> import os >>> filename = "foo/bar/baz/qux.naps" >>> name = os.path.basename(filename) >>> name 'qux.naps' >>> base, extension = os.path.splitext(name) >>> base 'qux' >>> extension '.naps' and also >>> os.path.exists(filename) # check if file exists False # or >>> os.path.isfile(filename) # check if a regular file with that … | |
Re: You need a `return` statement. Also, I deduce from your print statement that you're using python 2 where raw_input should be used instead of input, so it will be def comein(string): return raw_input(string) | |
Re: I don't know what this variable `infile` is, but it's not `rfile` nor `rfile1`. In the same way, why do you read the first file with `read()` and the last one with `readlines()` ? The 2 functions return different datatypes. Files are better opened with the `with` construct: with open('original.txt', … | |
Re: One way to use class 1's methods in class 2 is to have a class 2 object *aggregate* a class 1 instance. It is a standard pattern in OOP, called composition, which is often used as an alternative to inheritance class Class1(object): def method(self): print(self) class Class2(object): def __init__(self): self.obj1 … |
The End.