Re: Crackly sound in pygame
Before calling pygame.init(), you can call pygame.mixer.pre_init. Make sure the sample rate and sample size matches your audio file. But most likely the issue is that the default audio buffer size of 1024 doesn't cut it for some sound cards; try increasing to the next power of two. Ex. pygame.mixer.pre_init(44100, -16, 2, 2048) On Nov 29, 2007 7:33 PM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hey guys I am running Windows XP and am having an issue with a game > that my team has created. Whenever an audio file is played it creates > a very distorted, crackly sound. Any ideas what could be the issue? > > Thanks > -- > http://mail.python.org/mailman/listinfo/python-list > -- -David -- http://mail.python.org/mailman/listinfo/python-list
Re: importing a user file in Python
You have options: 1) Have the file in your current working directory, in which case it's just "import odbchelper". 2) Change your PYTHONPATH in your shell, adding a line like this to your bashrc perhaps: export PYTHONPATH=$PYTHONPATH:/home/jw/diveintopython-5.4/py ... and do the same import line in your code once you've sourced your bashrc again. On Nov 30, 2007 11:42 PM, waltbrad <[EMAIL PROTECTED]> wrote: > Hello. I'm brand new to Python. I'm running a Kubuntu system and > working through the "diveintopython" tutorial. The author of that > gives these instructions: > > >>> import odbchelper > >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", > >>> "pwd":"secret"} > >>> print odbchelper.buildConnectionString(params) > server=mpilgrim;uid=sa;database=master;pwd=secret > >>> print odbchelper.buildConnectionString.__doc__ > Build a connection string from a dictionary > Returns string. > > > I've tried to enter this into the interpreter but the very first > instruction: > > >>>import odbchelper > > gives me this: > > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named odbche > > I've tried to enter the pathname: > > >>> import /home/jw/diveintopython-5.4/py/odbchelper.py > > and I get this error: > > >>> import /home/jw/diveintopython-5.4/py/odbchelper.py > File "", line 1 > import /home/jw/diveintopython-5.4/py/odbchelper.py >^ > SyntaxError: invalid syntax > >>> > > > Where on my system do I have to place these files before the > interpreter will import them? > > Thanks. > -- > http://mail.python.org/mailman/listinfo/python-list > -- -David -- http://mail.python.org/mailman/listinfo/python-list
Re: os.access() under windows
To answer indirectly, usually the EAFP (easier to ask forgiveness than permission) approach works better for this kind of thing. try: f = open('e:\\test\\test', 'a') f.write('abc') f.close() except IOError: print "couldn't write test file, continuing..." On Dec 1, 2007 1:48 AM, Yann Leboulanger <[EMAIL PROTECTED]> wrote: > Hi, > > Under Windows XP os.access has a strange behaviour: > > I create a folder test under e: > > then os.access('e:\\test', os.W_OK) returns True. Everything's ok. > > Now I move My Documents to this e:\test folder > > Then os.access('e:\\test', os.W_OK) returns False !! > > but this works: > f = open('e:\\test\\test', 'a') > f.write('abc') > f.close() > > So why os.access returns False ? > > -- > Yann > -- > http://mail.python.org/mailman/listinfo/python-list > -- -David -- http://mail.python.org/mailman/listinfo/python-list
Re: Pickling dictionaries containing dictionaries: failing, recursion-style!
Are you opening the file in binary mode ("rb") before doing pickle.load on it? On 01 Dec 2007 14:13:33 -0800, Paul Rubin <"http://phr.cx"@nospam.invalid> wrote: > lysdexia <[EMAIL PROTECTED]> writes: > > self.wordDB[word] = [self.words.count(word), wordsWeights] > > what is self.words.count? Could it be an iterator? I don't think you > can pickle those. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- -David -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] minimalist web server
Running this in Python should create a server running on localhost port 80 that only serves blank pages: import SimpleHTTPServer import SocketServer class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): print >> self.wfile, "" server = SocketServer.TCPServer(("", 80), MyHandler) server.serve_forever() (also see http://effbot.org/librarybook/simplehttpserver.htm) On Dec 1, 2007 7:02 PM, Daniel Fetchinson <[EMAIL PROTECTED]> wrote: > > > I'm looking for the most minimalist web server ever that does nothing > > > else than return a fixed static page for every request. Regardless of > > > what the request is, it just needs to be an HTTP request to port 80, > > > the web server should return always the same html document. What would > > > be the best choice for this? The goal is of course to minimize system > > > resources in terms of memory, cpu, etc, etc. > > > > If you're running linux, maybe you want tux. > > > > publicfile isn't exactly what you describe, but its description might > > be of some interest: > > > > http://cr.yp.to/publicfile.html > > > Thanks, tux looks good, the only problem is that one needs to > recompile the kernel which I really don't want to do (so yes, I'm on > linux). Publicfile seems to "know" already too much. > > The reason I need this is that my current best strategy to avoid ads > in web pages is putting all ad server names into /etc/hosts and stick > my local ip number next to them (127.0.0.1) so every ad request goes > to my machine. I run apache which has an empty page for 404 errors so > I'll just see that blank page for every ad. Now I guess apache is a > pretty heavy weight guy so I'm looking for a lightweight alternative. > Lighttpd, nginx and company are all too complex and "know" too much. I > even considered just putting netcat into an infinite loop but I'm > afraid if there is a security hole in netcat I might be screwed. > > Maybe now that I outlined a little more why I need this others can > come up with more suggestions. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- -David -- http://mail.python.org/mailman/listinfo/python-list
Re: how to pass parameter to a python script when running it in the interactive shell?
This should work: python -i myscript.py --cl --cs 5 --ce 6 --bw 7 --set 1 On Dec 5, 2007 6:31 PM, wang frank <[EMAIL PROTECTED]> wrote: > > > Hi, > > I am debugging a python script which takes a set of paramters. In the > regular shell, I type: > > myscript.py --cl --cs 5 --ce 6 --bw 7 --set 1 > > However I want to debug the code in the interactive python shell, I do not > know how to run it. I know that > execfile("myscript.py") > > will run the script. But I could not figure out how to pass those > parameters. > > Could some one help me to find out the solution? > > Thanks > > Frank > > > ほら、変わったでしょ? マイクロソフトといっしょに、次のデジタルライフへ http://go.windowslive.jp/ > -- > http://mail.python.org/mailman/listinfo/python-list > -- -David -- http://mail.python.org/mailman/listinfo/python-list
Re: Better way to searching.
Hello, You might try using os.path.walk, I think it ends up being much more flexible than glob for this. #!/usr/bin/python2.4 import os def Finder(topdir, file_extension=None, levels=None): """Return all filenames in topdir. Args: topdir: Top level directory to search file_extension: file extension to match on levels: number of directory levels to search Returns: list of file names. """ def Levels(pathname): return pathname.count(os.path.sep) if levels is not None: pathlevels_allowed = Levels(topdir) + levels def WalkFunction(accumulator, dir, filenames): """Called from os.path.walk; accumulate matching filenames. Args: accumulator: list we are adding files to dir: directory being traversed filenames: list of files in dir Returns: None Modifies: filenames, if we want to stop descending. """ if levels is not None: if Levels(dir) > pathlevels_allowed: del filenames[:] return if file_extension is not None: accumulator.extend(os.path.join(dir, f) for f in filenames if os.path.splitext(f)[1] == file_extension) else: accumulator.extend(os.path.join(dir, f) for f in filenames) acc = [] os.path.walk(topdir, WalkFunction, acc) return acc if __name__ == '__main__': files = Finder("/your/starting/directory", file_extension=".mpg", levels=2) print files On Dec 13, 2007 11:13 PM, farsheed <[EMAIL PROTECTED]> wrote: > my code is here: > > _ > > def Globing(self, dir, extension, nop, inputDepth): > 'It creates a basic glob function that needed in other > classes' > self.exop = '*.' > self.opr = '*/' > self.counter = '' > self.files = [] > self.path = '' > for i in range (inputDepth): > self.path = dir + self.opr * i + self.exop * nop + > extension > #like this:*/*/*.*.mpg > self.counter = glob.glob(self.path) > self.files += self.counter > return self.files > > > Is there any better and faster way to do this? > -- > http://mail.python.org/mailman/listinfo/python-list > -- -David -- http://mail.python.org/mailman/listinfo/python-list
Re: making all letters Caps/Small Letters
>>> "THIS IS A STRING".lower() 'this is a string' >>> "THIS IS A STRING".title() 'This Is A String' >>> "this is a string".upper() 'THIS IS A STRING' You can browse all the string methods by doing >>> dir(str) On Dec 14, 2007 1:30 AM, Merrigan <[EMAIL PROTECTED]> wrote: > Hi There, > > I'm sure I have done this before, but cannot remember how, or find out > how to do it quickly - but is there a way/function/something in python > to make all the letters of a raw_input() string small/capital letters? > > Thanks! > > -- Merrigan > -- > http://mail.python.org/mailman/listinfo/python-list > -- -David -- http://mail.python.org/mailman/listinfo/python-list
Re: Default attribute values pattern
Hello, Seems to me that setattrs sort of assumes that you want to have all your initialization arguments set as attributes of the same name. I would think you'd sometimes want to be able to process the extra arguments inside of each __init__, assign them to attributes with different names, etc. My approach would be to treat each __init__ as a wrapping function, grabbing the items it needs out of the keyword dictionary and then calling the next __init__. Curious to hear other approaches though: def Grab(argdict, key, default): """Like argdict.get(key, default), but also deletes key from argdict.""" if key in argdict: retval = argdict["key"] del(argdict[key]) else: retval = default return retval class Base(object): def __init__(self, x=0, y=None): print "in Base init" self.x = x self.y = y class Derived1(Base): def __init__(self, **kwargs): print "in Derived1 init" self.z = Grab(kwargs, "z", None) super(Derived1, self).__init__(**kwargs) class Derived2(Derived1): def __init__(self, **kwargs): print "in Derived2 init" self.a = Grab(kwargs, "a", 0) self.b = Grab(kwargs, "b", False) super(Derived2, self).__init__(**kwargs) print self.__dict__ newthing = Derived2(x=234, y="blah", a=5) On Jan 19, 2008 10:14 AM, George Sakkis <[EMAIL PROTECTED]> wrote: > A situation that often comes up is having to initialize several > instance attributes that accept a default value. For a single class, > passing the default values in __init__ is fine: > > class Base(object): > def __init__(self, x=0, y=None): > self.x = x > self.y = y > > For inherited classes that need to override __init__ while keeping a > compatible interface though, the default values have to be repeated: > > class Derived(Base): > def __init__(self, x=0, y=None, z=''): > super(Derived,self).__init__(self,x,y) > self.z = '' > > For just two attributes and two classes that's maybe not too bad but > for many attributes and/or derived classes that may span multiple > modules, that doesn't seem to scale from a maintenance point of view, > especially if the defaults change over time. > > A pattern I've been using lately instead is store the defaults in > class attributes and let __init__ accept keyword arguments: > > class Base(object): > > x = 0 > y = None > > def __init__(self, **kwds): > setattrs(self, kwds) > > where setattrs is: > > def setattrs(self, attrvals, strict=True): > if strict: > # raise AttributeError if some attr doesn't exist already > for attr in attrvals.iterkeys(): > getattr(self,attr) > for attr,val in attrvals.iteritems(): > setattr(self, attr, val) > > This way, only the new and overriden default attributes have to > repeated in derived classes: > > class Derived(Base): > > x = 1 > z = '' > > def __init__(self, **kwds): > super(Derived,self).__init__(**kwds) > print 'In Derived.__init__' > > > Is this a good way of doing it ? Is there a better pattern ? > > George > -- > http://mail.python.org/mailman/listinfo/python-list > -- -David -- http://mail.python.org/mailman/listinfo/python-list
Re: Default attribute values pattern
Ah! nice, thanks, knew I was probably missing something. On Jan 19, 2008 5:01 PM, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > On Jan 19, 11:02pm, "David Tweet" <[EMAIL PROTECTED]> wrote: > > > def Grab(argdict, key, default): > > """Like argdict.get(key, default), but also deletes key from argdict.""" > > if key in argdict: > > retval = argdict["key"] > > del(argdict[key]) > > else: > > retval = default > > return retval > > > > Dictionaries already have a method for this. It's called pop. It's a > good idea to have a look at methods of builtin types before > reimplementing the wheel! > > Grab(argdict, key, default) is argdict.pop(key, default) > > -- > Arnaud > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- -David -- http://mail.python.org/mailman/listinfo/python-list