Writelines() a bit confusing
If file.WriteLines( seq ) accepts a list and it says it writes lines, why does it write the whole list in a single line. Be cause of that the reverse of file.writelines(seq) is not file.readlines(). Are the assumptions i made correct? If yes why is this so? I find a function called writelines not actualy writing the list in lines wierd. [code] something = ['re','ri','ro'] f.writelines( something ) something_else = f.readlines() [/code] In this case the list something_else will be diffrent from something -- http://mail.python.org/mailman/listinfo/python-list
Re: Writelines() a bit confusing
On May 19, 2:46 pm, "Gre7g Luterman" <[EMAIL PROTECTED]> wrote: > "aiwarrior" <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > > > If file.WriteLines( seq ) accepts a list and it says it writes lines, > > why does it write the whole list in a single line. Be cause of that > > the reverse of file.writelines(seq) is not file.readlines(). > > Are the assumptions i made correct? If yes why is this so? > > readlines() and writelines() are complimentary. readlines() leaves the line > terminators intact. It does not strip them off. writelines() does not add in > carriage returns for the same reason. For example: > > >>> D=open("temp.txt").readlines() > >>> D > > ['the quick\n', 'brown fox\n', 'jumps over\n', 'the lazy dog.'] > > >>> open("temp2.txt","w").writelines(D) > > will create temp2.txt to be identical to temp.txt. I Haven't seen that way before thanks, both of you :D -- http://mail.python.org/mailman/listinfo/python-list
Unable to strip \n characters
Hi Im writing a personal wrapper to the perl script offered by rapidshare, so that im able to use multiple files and glob pathnames, but im using a file so i can track and resume any uploading data. The problem is the lines come with a \n character that im not bein able to take out, files = f.readlines() for upload in files: upload.strip("\n") final_args = "./rsapiresume.pl %s prem user password" % (upload) print upload #os.system( final_args ) My upload string still comes with the \n making the system call look like this: ./rsapiresume.pl filename_to_upload prem user password I've already tried replace but it doesn't work either -- http://mail.python.org/mailman/listinfo/python-list
Re: Unable to strip \n characters
On May 21, 7:05 am, Asun Friere <[EMAIL PROTECTED]> wrote: > On May 20, 10:49 pm, Michael Bentley <[EMAIL PROTECTED]> > wrote: > > > On May 20, 2007, at 7:41 AM, Michael Bentley wrote: > > > > (upload.strip()) > > > Oops: (upload.strip(),) or upload.strip() > > Superfluous though the braces around your original were, it should > still run ... > ie. (a) == a When you mean superfluous you mean it makes a diffrence in run-time or just code style? -- http://mail.python.org/mailman/listinfo/python-list
SQL problem in python
class db: def __init__(self): #constructor conn = sqlite3.connect(":memory:") conn.isolation_level = None self.cursor = conn.cursor() self.cursor.execute("CREATE TABLE database (album,filepath)") def add_entry(self, eone , etwo): #Add entry to database self.cursor.execute("INSERT INTO database (album,filepath) VALUES (?,?)", ( eone , etwo ) ) return 1 #TODO: exception handler def get_mediadb(self, print_db = False): self.cursor.execute('SELECT * FROM database') if (print_db == True): print self.cursor.fetchall() def get_value( self, column ): self.cursor.execute( "SELECT (?) FROM database", column ) for n in self.cursor: print n def destructor(self): self.cursor.close() if __name__ == "__main__": f = db() f.add_entry( "Pinkk Floyd", "fdgf" ) f.add_entry( "Pink", "fdgf" ) # f.get_mediadb(print_db=True) f.get_value(('filepath',)) f.destructor() When i run it the get_value() returns 'filepath' instead of the columns. But if i dont use any variable and make the expression static all goes on as its supposed to. What am i doing wrong? PS: Dont mind the bad code -- http://mail.python.org/mailman/listinfo/python-list
Re: SQL problem in python
Thanks a lot. In the Python documentation, the sqlite module documentation doesn't mention that special rule. I really thought that every variable to be included in a query had to use that special method. Again thanks a lot -- http://mail.python.org/mailman/listinfo/python-list
Mutagen File Problem
Hi i'm having a IO error saying a file does not exist even though i perform a isFile() check. Can you help me out figuring what is wrong? Thanks in advance from mutagen.easyid3 import EasyID3 from mutagen.mp3 import MP3 for root, dirs, files in os.walk('''C:\\Documents and Settings\\pneves\ \Music\\''', topdown=False): for name in files: joined = os.path.join(root, name) if (name[-3:] == 'mp3' ): audio = MP3(joined, ID3=EasyID3) if not audio.has_key('album') and os.path.isfile(joined): print os.path.join(root, name) I get an error as following: IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\pneves\\Music\\Naked Music - Miguel Migs - Colorful You\ \Miguel Migs - Colorful you - Full album\\Miguel Migs - Colorful you - Cd 2 -Mixed and mastered by J. Mark Andrus\\7 - Miguel Migs feat. Lisa Shaw - You Bring Me Up (Main Vocal Mix).mp3' -- http://mail.python.org/mailman/listinfo/python-list
Rapidshare to Megaupload script
I've made this script and would like to have some input and share it with the community. I also have a page with some code i produce on my spare time. http://pneves.net Thanks # -*- coding: utf-8 -*- ## I Paulo Neves am the owner of this script and i do not allow the copy or distribution ## of this script without my permission to commercial purposes ## If you have any suggestions please mail me and i will reply and post the on the site from __future__ import with_statement from urlparse import urljoin import urllib2, urllib import os, sys import re import ConfigParser urls = [] acc_details = 0 firs_run = 0 def from_rapidshare(url): '''Check if this is a rapidshare link''' return (url.startswith("rapidshare.com") or url.startswith("www.rapidshare.com") or url.startswith("http://rapidshare.com";) or url.startswith("http://www.rapidshare.com";)) def account_details(): if os.environ.has_key("APPDATA") and os.path.exists(os.environ ["APPDATA"]): path = os.environ["APPDATA"] + "\\murs.cfg" else: path = os.path.expanduser("~") + "\.murs.cfg" if not os.path.exists(path): print path m_user = raw_input("Enter your user name for megaupload: ") m_password = raw_input("Enter your password for megaupload: ") r_user = raw_input("Enter your user name for rapidshare: ") r_password = raw_input("Enter your password for rapidshare: ") cfg = ConfigParser.SafeConfigParser() cfg.add_section('Acc_Details') cfg.set('Acc_Details', 'm_user ', m_user) cfg.set('Acc_Details', 'm_password', m_password) cfg.set('Acc_Details', 'r_user', r_user) cfg.set('Acc_Details', 'r_password', r_password) with open(path, 'wb') as configfile: cfg.write(configfile) cfg = ConfigParser.SafeConfigParser() cfg.read(path) try: m_user = cfg.get("Acc_Details", "m_user") m_password = cfg.get("Acc_Details", "m_password") r_user = cfg.get("Acc_Details", "r_user") r_password = cfg.get("Acc_Details", "r_password") except ConfigParser.NoSectionError or ConfigParser.NoOptionError: print "no section or No Option" print os.remove(path) return (m_user, m_password, r_user, r_password) def cookie_processor(cookie): cookie = cookie.split("; ") ##cookie = dict( [cookie[x].split("=") for x in xrange ( len (cookie) ) ] ) ##if cookie['user'] != None: return cookie ##else: ##print "Scheme has changed or authentication failes. Last option most likely" ##sys.exit() ## def rs_auth(login): r_user = login[0] r_password = login[1] opener = urllib2.build_opener(urllib2.HTTPSHandler()) urllib2.install_opener( opener ) cred = urllib.urlencode({"login": r_user, "password": r_password}) try: req = urllib2.urlopen("https://ssl.rapidshare.com/cgi-bin/ premiumzone.cgi", cred) cookie_rs = cookie_processor( req.headers.get("set-cookie", "") ) except urllib2.URLError: print"Some error with the connection ocurred. Try again now or later" sys.exit() #Returns the page if flag is set return req.read(), cookie_rs def mu_auth(login): r_user = login[0] r_password = login[1] cred = urllib.urlencode({"login": r_user, "password": r_password}) try: req = urllib2.urlopen("http://www.megaupload.com";, cred) cookie_mu = cookie_processor( req.headers.get("set-cookie", "") ) except: print "Connection failed" sys.exit() #returns the authenticated header, in case future changes specificaly ask the cookie it would be easy #to change code or even extract it from the header itself as a dictionary key return cookie_mu def set_rscookie_in_musite(cookie_mu, cookie_rs) : #it doesnt need to check for the cookie because it automaticaly resets for the new value cookie_rs = cookie_rs.split("=",1) header = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain", "cookie":cookie_mu} params = urllib.urlencode({'domain': "http://rapidshare.com";, 'cookiename1': cookie_rs[0], 'cookievalue1': cookie[1] }) req = urllib2.Request("http://www.megaupload.com/multifetch/? c=cookies", params, header ) print header print params #r = urllib2.urlopen(req) return True acc_details = account_details() if len(sys.argv)==1: print "Try -help for more information" sys.exit("No arguments") if sys.argv[1] == "-h" or sys.argv[1] == "–help": print " > murs http://megaupload.com/?d=FILE1 … http://megaupload.com/?d=FILEN"; print " Download one or several rapidshare links passed as argument separated by whitespace\n" print " >murs links.txt" print " Download a list of links from a file\n" print " >murs account" print " Automaticaly downloads the entire contents of your account" sys.exit() ## Process the arguments ## AFTER THIS ITS ALL
Re: Rapidshare to Megaupload script
Thanks a lot for your input i really needed because i realized these are minor flaws but even so define whether its good or bad code and i really need to improve that. I already implemented the changes you suggested and this one, > cookie = dict(x.split("=") for x in cookie) for me is just very good and really is elegant. An aspect you didn't mention was the >urls.append("http://rapidshare.com/files/"; + retrieved_data[0] + "/" + > retrieved_data[1]) I think its very hackish and crude but urlparser doesnt seem to accept more than one argument at a time and doing it in a loop seem worse than the current solution. What would you do? Thanks a lot again -- http://mail.python.org/mailman/listinfo/python-list
Re: Rapidshare to Megaupload script
Thanks a lot for your input i really needed because i realized these are minor flaws but even so define whether its good or bad code and i really need to improve that. I already implemented the changes you suggested and this one, > cookie = dict(x.split("=") for x in cookie) for me is just very good and really is elegant. An aspect you didn't mention was the >urls.append("http://rapidshare.com/files/"; + retrieved_data[0] + "/" + > retrieved_data[1]) I think its very hackish and crude but urlparser doesnt seem to accept more than one argument at a time and doing it in a loop seem worse than the current solution. What would you do? Thanks a lot again -- http://mail.python.org/mailman/listinfo/python-list
Threads and temporary files
Hi I recently am meddling with threads and wanted to make a threaded class that instead of processing anything just retrieves data from a file and returns that data to a main thread that takes all the gathered data and concatenates it sequentially. An example is if we want to get various ranges of an http resource in paralell import threading class download(threading.Thread): def __init__(self,queue_in,queue_out): threading.Thread.__init__( self ) self.url = url self.starts = starts self.ends = ends self.content = 0 def getBytesRange(self): request = urllib2.Request(self.url) # New Request object if self.ends is not None: # If the end of the desired range is specified request.add_header("Range", "bytes=%d-%d" % (self.starts, self.ends)) else: # If you want everything from start up to the resource's length request.add_header("Range", "bytes=%d-" % self.starts) response = urllib2.urlopen(request) # Make the request, get the data self.response.read() def run(self): self.getBytesRange() then when we create the threads we wait for them to complete and write in a file. To reduce memory footprint we can write the self.response to a tempifile and then when all threads complete concatenate the temps in a single file in a specified location. The problem here is the same, how to get a reference to the temporary file objects created in the threads sequeatially. I hope i have made myself clear. Thanks you in advance -- http://mail.python.org/mailman/listinfo/python-list
Re: Neatest way to do a case insensitive "in"?
On Mar 13, 9:31 pm, Albert Hopkins wrote: > On Fri, 2009-03-13 at 21:04 +, tinn...@isbd.co.uk wrote: > > What's the neatest way to do the following in case insensitive fashion:- > > > if stringA in stringB: > > bla bla bla > > > I know I can just do:- > > > if stringA.lower() in stringB.lower(): > > bla bla bla > > > But I was wondering if there's a neater/easier way? > > How is "if stringA.lower() in stringB.lower():" complex/messy? I do agree with you but what if your really insist and perhaps subclass the str and override the __contains__(x) method? http://pneves.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Threads and temporary files
On Mar 14, 3:01 am, "Gabriel Genellina" wrote: > En Fri, 13 Mar 2009 19:07:46 -0200, aiwarrior > escribió: > > > I recently am meddling with threads and wanted to make a threaded > > class that instead of processing anything just retrieves data from a > > file and returns that data to a main thread that takes all the > > gathered data and concatenates it sequentially. > > An example is if we want to get various ranges of an http resource in > > paralell > > The usual way to communicate between threads is using a Queue object. > Instead of (create a thread, do some work, exit/destroy thread) you could > create the threads in advance (a "thread pool" of "worker threads") and > make them wait for some work to do from a queue (in a quasi-infinite > loop). When work is done, they put results in another queue. The main > thread just places work units on the first queue; another thread > reassembles the pieces from the result queue. For an I/O bound application > like yours, this should work smoothly. > You should be able to find examples on the web - try the Python Cookbook. > > -- > Gabriel Genellina I already tried a double queue implementation as you suggest with a queue for the threads to get info from and another for the threads to put the info in. My implementation test was using a file with some lines of random data. Here it is class DownloadUrl(threading.Thread): def __init__(self,queue_in,queue_out): threading.Thread.__init__( self ) #self.url = url #self.starts = starts #self.ends = ends self.queue_in = queue_in self.queue_out = queue_out def run(self): (fp,i) = self.queue_in.get() self.queue_in.task_done() #print var #self.queue_out.put("i",False) worknr = 5 queue_in = Queue.Queue(worknr) queue_out = Queue.Queue(worknr) threads = [] fp = open("./xi","r") #print fp.readlines() for i in xrange(10): queue_in.put((fp,i)) DownloadUrl(queue_in,queue_out).start() queue_in.join() while queue_out.qsize(): print queue_out.get() queue_out.task_done() >Any reason you're using threads instead of processes? Perhaps because of more flexible way to share data between threads than processes -- http://mail.python.org/mailman/listinfo/python-list
Problem with sqlite
class db: def __init__(self): #constructor conn = sqlite3.connect('./db.db') conn.isolation_level = None self.cursor = conn.cursor() try: self.cursor.execute("CREATE TABLE database (album,filepath)" ) except: pass def add_entry( self, eone , etwo ): #Add entry to database self.cursor.execute( "INSERT INTO database (album,filepath) VALUES (?,?)", ( eone , etwo ) ) return 1 #TODO: exception handler def get_mediadb( self, print_db = False ): self.cursor.execute( 'SELECT * FROM database' ) if (print_db == True): print self.cursor.fetchall() def get_value( self, column ): self.cursor.execute( "SELECT %s FROM database" % column ) for n in self.cursor: print n def destructor(self): self.cursor.close() def walking_and_filling(db): pass if __name__ == "__main__": db = db() #walking_and_filling( db ) for root, dirs, files in os.walk( '''foo_path/''', topdown=False ): for name in files: joined = os.path.join(root, name) if (name[-3:] == 'mp3' and os.path.isfile( joined ) ): try: audio = MP3 (joined, ID3=EasyID3 ) print (audio['album']) db.add_entry( joined, audio['album'] ) except: pass db.get_mediadb( print_db=True ) When i execute this the database doesn't get filled with anything and the program stays running in memory for ever. The print statement is just a Unicode string that would get to the database if i stated it in the add_entry function as a constant string. It's a really weird problem that i dont seem to understand why it's working out this way. Thanks in advance -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with sqlite
On Mar 29, 6:41 pm, Gerhard Häring <[EMAIL PROTECTED]> wrote: > Ok, I'll review your code. > > aiwarrior wrote: > > class db: > > def __init__(self): #constructor > > conn = sqlite3.connect('./db.db') > > conn.isolation_level = None > > Autocommit mode is mostly for newbies who forget to call commit. > Unfortunately, newbiews find enough other ways to shoot themselves in > the foot. So, in retrospect, maybe I should not have added that feature > to pysqlite ;-) > > > self.cursor = conn.cursor() > > try: > > self.cursor.execute("CREATE TABLE database > > (album,filepath)" ) > > except: > > pass > > try: except: pass without catching *specific* exceptions is generally a > very bad idea. If you're doing something stupid, or another error > happens than the one you want to ignore, you will never know this way. > > > def add_entry( self, eone , etwo ): #Add entry to database > > self.cursor.execute( "INSERT INTO database (album,filepath) > > VALUES (?,?)", ( eone , etwo ) ) > > return 1 #TODO: exception handler > > > def get_mediadb( self, print_db = False ): > > self.cursor.execute( 'SELECT * FROM database' ) > > if (print_db == True): > > print self.cursor.fetchall() > > The if clause can be written as just "if print_db:". > > > def get_value( self, column ): > > self.cursor.execute( "SELECT %s FROM database" % column ) > > for n in self.cursor: > > print n > > > def destructor(self): > > self.cursor.close() > > Just FYI, Python's "destructor" method is called "__del__", not > "destructor". > > > > > def walking_and_filling(db): > > pass > > > if __name__ == "__main__": > > db = db() > > #walking_and_filling( db ) > > for root, dirs, files in os.walk( '''foo_path/''', > > topdown=False ): > > for name in files: > > joined = os.path.join(root, name) > > if (name[-3:] == 'mp3' and os.path.isfile( joined ) ): > > try: > > audio = MP3 (joined, ID3=EasyID3 ) > > print (audio['album']) > > db.add_entry( joined, audio['album'] ) > > except: > > pass > > Now, this try: except: pass is most probably hiding the real error That > leads to the insert failing. Because you just ignored any errors, you > will never now what exactly went wrong here. > > > db.get_mediadb( print_db=True ) > > > When i execute this the database doesn't get filled with anything and > > the program stays running in memory for ever. [...] > > HTH, > > -- Gerhard I'm sorry about not saying showing the libraries. It was not on purpose. import os import sqlite3 from mutagen.easyid3 import EasyID3 from mutagen.mp3 import MP3 ##def tree(path): ##node = () ##for node in os.listdir( path ): ##if( os.path.isdir( path + node )): ##tree(path+node) ##return path class db: def __init__(self): #constructor conn = sqlite3.connect( './db.db' ) conn.isolation_level = None self.cursor = conn.cursor() try: self.cursor.execute( "CREATE TABLE database (album,filepath)" ) except: pass def add_entry( self, eone , etwo ): #Add entry to database self.cursor.execute( "INSERT INTO database (album,filepath) VALUES (?,?)", ( eone , etwo ) ) return 1 #TODO: exception handler def get_mediadb( self, print_db = False ): self.cursor.execute( 'SELECT * FROM database' ) if (print_db == True): print self.cursor.fetchall() def get_value( self, column ): self.cursor.execute( "SELECT %s FROM database" % column ) for n in self.cursor: print n def destructor( self ): self.cursor.close() def walking_and_filling( db ): pass if __name__ == "__main__": db = db() #walking_and_filling( db ) for root, dirs, files in os.walk( '''foo_path/''', topdown=False ): for name in files: joined = os.path.join(root, name) if (name[-3:] == 'mp3' and os.path.isfile( joined ) ): try: audio = MP3 (joined, ID3=EasyID3 ) print (audio['album']) db.add_entry( joined, audio['album'] ) except: pass db.get_mediadb( print_db=True ) This is all the code. Some of that try pass code is just something i glued to create a clean slate database file -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with sqlite
Ok regarding Gerhard's comment of the try, except, pass, i came to understand that it's really bad code. And i should have referred that i put that there be cause i was getting: Traceback (most recent call last): File "C:\Python25\Projects\cp.py", line 48, in db = db() File "C:\Python25\Projects\cp.py", line 19, in __init__ self.cursor.execute( "CREATE TABLE database (album,filepath)" ) OperationalError: table database already exists But when i tried to handle the Operational error with : try: self.cursor.execute( "CREATE TABLE database (album,filepath)" ) except OperationalError: pass I would got: NameError: global name 'OperationalError' is not defined I searched the Internet and found that sqlite3.OperationalError was missing. Dumb me. Now even though i've been reading a bit about exceptions in python tutorial i've come to realize that my approach won't give me the results i want. To my understanding the try statement will still execute the statement within it until it produces the error. So even if i caught this OperationalError i wouldn't know what to do with it. What i'm going to study is whether it's possible to evaluate if a table already exists, and if so act accordingly. Duncan Booth wrote: >Are you absolutely certain of that? If you use print to try to debug the >value of an object you should usually use repr >print repr(audio['album']) As Gerhard correctly guessed i'm a newbie and didn't know of the existence repr. I've been reading about it in python documentation but have yet to analyze it more carefully. I guess the parentheses are just some form of maniac stupidity. Will try to be more clean about them. Thanks for all the patience and hope i've been more forthcoming in this post. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with sqlite
Ok regarding Gerhard's comment of the try, except, pass, i came to understand that it's really bad code. And i should have referred that i put that there be cause i was getting: Traceback (most recent call last): File "C:\Python25\Projects\cp.py", line 48, in db = db() File "C:\Python25\Projects\cp.py", line 19, in __init__ self.cursor.execute( "CREATE TABLE database (album,filepath)" ) OperationalError: table database already exists But when i tried to handle the Operational error with : try: self.cursor.execute( "CREATE TABLE database (album,filepath)" ) except OperationalError: pass I would got: NameError: global name 'OperationalError' is not defined I searched the Internet and found that sqlite3.OperationalError was missing. Dumb me. Now even though i've been reading a bit about exceptions in python tutorial i've come to realize that my approach won't give me the results i want. To my understanding the try statement will still execute the statement within it until it produces the error. So even if i caught this OperationalError i wouldn't know what to do with it. What i'm going to study is whether it's possible to evaluate if a table already exists, and if so act accordingly. Duncan Booth wrote: >Are you absolutely certain of that? If you use print to try to debug the >value of an object you should usually use repr >print repr(audio['album']) As Gerhard correctly guessed i'm a newbie and didn't know of the existence repr. I've been reading about it in python documentation but have yet to analyze it more carefully. I guess the parentheses are just some form of maniac stupidity. Will try to be more clean about them. Thanks for all the patience and hope i've been more forthcoming in this post. -- http://mail.python.org/mailman/listinfo/python-list