2,646 Posted Topics
Re: [url=http://diveintopython.org/]Dive into python[/url] has a good reputation. A good part of learning python is learning the standard library. Note that the library has been reorganized in python 3, so that some module names may differ, and some code may run with python 2.x and not python 3.x. | |
Re: You don't even need the readlines(). You can iterate on the file object directly. On the other hand, after the file has been read, the file position is at the end of the file, so you must use seek to go to the beginning of the file [CODE]initFile = open( … | |
Re: I think the point is that directories have many other reasons to exist than being python packages. So if you write a python package named 'books' for example. You can create other 'books' directories here and there, python will still find the package 'books' because it has an __init__.py file. | |
Re: You can get a drive letter from a path like this [code=python] import os drive = os.path.splitdrive(mypath)[0] print(drive) [/code] Now for mypath, you could use either the current working dir [icode]os.getcwd()[/icode] or your module's file path [icode]__file__[/icode]. Then you can call the dos command using [icode]subprocess.Popen[/icode] (I normally work on … | |
Re: Don't you think you could save some CPU usage if you replaced the "while 1" by a timer which would run the loop at a certain frequency ? | |
Re: A similar problem has already been posted in this forum. For a multiplatform solution, I suggest that you try to use the class Popen in [url=http://code.activestate.com/recipes/440554/]this activestate recipe[/url]. It contains a very sophisticated solution. See if it applies to your problem. | |
Re: I suggest the following replacement for Starter_AA: [code=python] from pprint import pprint mapping = """ Phe: UUU, UUC; Leu: UUA, UUG, CUU, CUC, CUA, CUG; Iso: AUU, AUC, AUA; Met: AUG; Val: GUU, GUC, GUA, GUG; Ser: UCU, UCC, UCA, UCG, AGU, AGC; Pro: CCU, CCC, CCA, CCG; Thr: ACU, … | |
Re: You must overwrite the __setitem__ method [code=python] class mydict(dict): def __setitem__(self, key, value): doStuff() return dict.__setitem__(self, key, value) [/code] | |
Re: 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 initialize the structure's content. These are 2 different kinds of operations. For example you can decide to change your … | |
Re: There is nothing very clever here. You must learn to think about algorithms. You want to output a series of blocks of lines; each block is indexed by the the number k in file<k>.tcl. So the structure of your algorithm is (this is pseudo code, not python) [code=text] for k … | |
Re: You should also read the documentation of the standard lib module [b]atexit[/b] which gives example of how you can register functions to be executed at program termination. | |
Re: [QUOTE=thesinik;1060404]I ended up figuring it out by breaking down sin into a function of series rather than using Taylor expansions. It was much simpler. This was my code for sin: [CODE] def sine(x): sum = 0.0 n = 0.0 term = 1.0 while (term > .0000000001): #loops until the iterations … | |
Re: I have a small collection of receipes found on the web a long time ago. See if one of them can help you [code=python] # linux, mac os.kill(pid, signal.SIGKILL) killedpid, stat = os.waitpid(pid, os.WNOHANG) if killedpid == 0: print >> sys.stderr, "ACK! PROCESS NOT KILLED?" # windows handle = subprocess.Popen("someprocess … | |
Re: 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 appears as a facade behind which you can implement arbitrary complex and flexible algorithms. Also … | |
Re: The indentation is bad. The code should read [code=python] # Classy Critter # Demonstrates class attributes and static methods class Critter(object): """A virtual pet""" total = 0 def status(): print "\nThe total number of critters is", Critter.total status = staticmethod(status) def __init__(self, name): print "A critter has been born!" self.name … | |
Re: [QUOTE=Kruptein;1058422]I found out the launcher is actually called: localhost.desktop but if I do: [icode]os.system("~/localhost.desktop")[/icode] it just starts my standard editor and shows the script...[/QUOTE] I don't know ubuntu very well, but if your launcher is related to a bash script, the script must be somewhere in the filesystem, so locate … | |
Re: [QUOTE=vegaseat;1058172] Note (so you don't waste your time): bpython uses Linux's curses and does not work with Windows.[/QUOTE] bpython doesn't even work on my linux box. It could be the 64 bits chipset... | |
Re: You can use the modules HTMLParser and collections.deque to implement the queue [code=python] from HTMLParser import HTMLParser from collections import deque # deque is a linked list which can be used as fifo or filo class MyHTMLParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.tag_deque = deque() def handle_starttag(self, tag, attrs): self.tag_deque.append("<{t}>".format(t=tag)) def handle_endtag(self, … | |
Re: You can write [code=python] webbrowser.get('firefox').open("http://www.google.com/") [/code] Sometimes it's better to open firefox with subprocess like this [code=python] import subprocess as sp child = sp.Popen(['firefox', '-p', 'myprofile', '-no-remote', "http://www.google.com/"]) [/code] this allows you to open firefox with a different profile, or to close firefox later from your program (with child.terminate()). | |
Re: 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. [code=python] #!/usr/local/bin/python import re, os from os.path import join as pjoin, isdir while True: … | |
Re: theIndex[124] is a list, so you can just write [code=python] theIndex[124].append("m") [/code] | |
Re: Try to run the loop 1 000 000 times instead of 1000. On my machine, it takes 0.0818829536438 seconds for 1 million times. See also the module timeit. | |
Re: Steal means that the item will be referenced in the tuple, but without increasing it's reference count. When the tuple is deleted, the object will be decrefed. For example if you create a python integer with PyInt_FromLong, you normally need to decref this integer before leaving the current function, otherwise … | |
Re: [url=http://en.wikipedia.org/wiki/Primitive_type]See also this[/url] | |
Re: If your compile can't find Python.h, you must add an include directory. On my system, I get [code=python] >>> from distutils import sysconfig >>> sysconfig.get_python_inc() '/usr/include/python2.6' [/code] So for Python.h, I need the compiler option [icode]-I/usr/include/python2.6[/icode]. | |
Re: You could write something like [code=python] from time import sleep import os def wait_file(filename): cnt = 3 * 60 while cnt > 0: if os.path.isfile(filename): return cnt -= 1 sleep(1) raise SystemExit [/code] | |
Re: You can write [code=python] 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, newname) ... [/code] If you don't want to nuke your jpegs, make a zip file with the whole folder before doing anything. | |
Re: [QUOTE=lukerobi;1053226]Sorry for the very slow reply, but for some reason this method is crashing my program... I guess i will have to try to figure out another solution :([/QUOTE] First, if it's crashing your program, there must be an exception traceback. Also why don't you simply create a normal class … | |
This snippet defines a context to execute a block of statements in a different working directory (with the help of the with statement). After the block is exited, the initial working directory is restored, even if an exception has occurred in the with block. | |
Re: The str(d) in the argument doesn't work. I suggest that you use the subprocess module [code=python] import subprocess as SP SP.Popen(["c:\\program files\\webteh\\bsplayerpro\\bsplayer.exe", "-stime="+str(d), "d:\\rodin\\rodin.mp4"]) [/code] | |
Re: 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 [code=python] Python 2.6.4 (r264:75706, Oct 27 2009, 15:50:27) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" … | |
Re: For a crash reference, there is [url=http://www.limsi.fr/Individu/pointal/python/pqrc/versions/PQRC-2.4-A4-latest.pdf]PQRC[/url]. There is also [url=http://home.uchicago.edu/~gan/file/python.pdf]another one[/url] here, probably less complete. | |
Re: Google found a patch for scapy.all..sniff, wich allows to stop the sniff function programmatically. You should try this [url=http://trac.secdev.org/scapy/wiki/PatchSelectStopperTimeout]http://trac.secdev.org/scapy/wiki/PatchSelectStopperTimeout[/url] | |
Re: I modified your code to give you a first running version [code=python] from graphics import * def drawCircle(win, centre, radius, colour): circle = Circle(centre, radius) circle.setFill(colour) circle.setWidth(2) circle.draw(win) def eyePicker(): colour = raw_input("Please enter the colour: ") return colour # <- return the picked colour def drawEye( #win, centre, radius, … | |
Re: You could go this way, assuming that you have a folder named 'mypackage' wich contains a file '__init__.py' [code=python] # tested with py 2.6 import os import mypackage def module_names(package): # This only finds python modules which name doesn't start with '__' folder = os.path.split(package.__file__)[0] for name in os.listdir(folder): if … | |
Re: [QUOTE=jbennet;1045803]By using encapsulation you ensure fidelity of data, and provide a known interface to others who may extend or use your class, making refactoring much less painful.[/QUOTE] The author of the article meant that the "property" function replaces getters and setters. In fact python's "property" encapsulates the attribute, and also … | |
Re: You can make a list with your functions [code=python] import random funcs = [func0, func1,func2,func3,func4,func5,func6,func7,func8,func9,] funcs[random.randrange(10)]() # <- call a random function random.choice(funcs)() # <- another way [/code] edit: jlm699 was faster :) | |
![]() | Re: [url=http://blog.doughellmann.com/2007/07/pymotw-subprocess.html]This one[/url] perhaps ? ![]() |
Re: Perhaps you could use the cProfile module to find out where time is spent ? | |
Re: What they mean is that you must run these commands [code] patch-p1-i clammwin/patches/wxPython-2424.maskededit.patch patch-p1-i clamwin/patches/wxPython-2424.throbber.patch patch-p1-i clamwin/patches/wxPython-2424.timectrl.patch [/code] in a cygwin terminal. Perhaps you need to put a space before -p1 and before -i. | |
Re: Notice [icode]getpass.getuser()[/icode] which finds your login name (does it work on mac ?). | |
Re: [QUOTE=vegaseat;1044320]We wouldn't have this problem if Windows machines would ship with Python installed. The real problem is that MS wants to make money with their old VB interpreter.[/QUOTE] I think that Van Rossum prefers OSX, and he works for Google, which makes 2 other obstacles. | |
Re: [QUOTE=persianprez;1021594]Thanks a bunch! worked as usual. You can see my complete source code or download it here: [url]http://pedrumgolriz.com/index/?p=52[/url][/QUOTE] You should wrap you code in tags at your web site to preserve indentation. | |
Re: [QUOTE=pythopian;1043157]I see that this is about "scraping a website". Do we not care if that would infringe someone's copyright? Or at least if it be ethically questionable? I don't want to sound straitlaced, but wouldn't it be better we knew more about it before offering help with this?[/QUOTE] When you … | |
Re: You can use the 'key' argument to 'sort' or 'sorted'. Here, Data is a list. You can write [code=python] def my_key(item): return item[1][1] # this returns 3.1 if item is ('1023', ['Name', '3.1', 'SomeData']) Data.sort(key = my_key) [/code] Data is easily turned into a dictionary: [code] Data = dict(Data) L … | |
Re: You can try [code=python] while rounds > max(player_point, cpu_point) [/code] | |
Re: You can use [url=http://www.daniweb.com/code/snippet217237.html]this code snippet[/url]. | |
Re: In idle you can restart the shell in the shell menu. Normally, this kills your execution because idle starts a new process. | |
Re: According to your previous post, I think that you are using the "graphics.py" module [url=http://mcsp.wartburg.edu/zelle/python/]like this one[/url]. In that case, since p1 and p2 are not numbers but Points, you must use first p1.getX() and p1.getY() to get the coordinates of the points. | |
Re: [QUOTE=woooee;1038091] It takes a large amount of self control not to respond with Can Python do this? (Who are you, Steve Urkel) I need help (We all do but most of us can't afford a psychiatrist) Help (Have you fallen and can't get up?)[/QUOTE] The answer to "Can Python do … |
The End.