Re: Joining Big Files
On 26 Aug, 15:45, vasudevram <[EMAIL PROTECTED]> wrote: > On Aug 26, 6:48 am, Paul McGuire <[EMAIL PROTECTED]> wrote: > > > > > On Aug 25, 8:15 pm, Paul McGuire <[EMAIL PROTECTED]> wrote: > > > > On Aug 25, 4:57 am,mosscliffe<[EMAIL PROTECTED]> wrote: > > > > > I have 4 text files each approx 50mb. > > > > 50mb? Really? Did you actually try this and find out it was a > > > problem? > > > > Try this: > > > import time > > > > start = time.clock() > > > outname = "temp.dat" > > > outfile = file(outname,"w") > > > for inname in ['file1.dat', 'file2.dat', 'file3.dat', 'file4.dat']: > > > infile = file(inname) > > > outfile.write( infile.read() ) > > > infile.close() > > > outfile.close() > > > end = time.clock() > > > > print end-start,"seconds" > > > > For 4 30Mb files, this takes just over 1.3 seconds on my system. (You > > > may need to open files in binary mode, depending on the contents, but > > > I was in a hurry.) > > > > -- Paul > > > My bad, my test file was not a text file, but a binary file. > > Retesting with a 50Mb text file took 24.6 seconds on my machine. > > > Still in your working range? If not, then you will need to pursue > > more exotic approaches. But 25 seconds on an infrequent basis does > > not sound too bad, especially since I don't think you will really get > > any substantial boost from them (to benchmark this, I timed a raw > > "copy" command at the OS level of the resulting 200Mb file, and this > > took about 20 seconds). > > > Keep it simple. > > > -- Paul > > There are (at least) another couple of approaches possible, each with > some possible tradeoffs or requirements: > > Approach 1. (Least amount of code to write - not that the others are > large :) > > Just use os.system() and the UNIX cat command - the requirement here > is that: > a) your web site is hosted on *nix (ok, you can do it if on Windows > too - use copy instead of cat, you might have to add a "cmd /c " > prefix in front of the copy command, and you have to use the right > copy command syntax for concatenating multiple input files into one > output file). > > b) your hosting plan allows you to execute OS level commands like cat, > and cat is in your OS PATH (not PYTHONPATH). (Similar comments apply > for Windows hosts). > > import os > os.system("cat file1.txt file2.txt file3.txt file4.txt > > file_out.txt") > > cat will take care of buffering, etc. transparently to you. > > Approach 2: Read (in a loop, as you originally thought of doing) each > line of each of the 4 input files and write it to the output file: > > ("Reusing" Paul McGuire's code above:) > > outname = "temp.dat" > outfile = file(outname,"w") > for inname in ['file1.dat', 'file2.dat', 'file3.dat', 'file4.dat']: > infile = file(inname) > for lin in infile: > outfile.write(lin) > infile.close() > outfile.close() > end = time.clock() > > print end-start,"seconds" > > # You may need to check that newlines are not removed in the above > code, in the output file. Can't remember right now. If they are, just > add one back with: > > outfile.write(lin + "\n") instead of outfile.write(lin) . > > ( Code not tested, test it locally first, though looks ok to me. ) > > The reason why this _may_ not be much slower than manually coded > buffering approaches, is that: > > a) Python's standard library is written in C (which is fast), > including use of stdio (the C Standard IO library, which already does > intelligent buffering) > b) OS's do I/O buffering anyway, so do hard disk controllers > c) from some recent Python version, I think it was 2.2, that idiom > "for lin in infile" has been (based on somethng I read in the Python > Cookbook) stated to be pretty efficient anyway (and yet (slightly) > more readable that earlier followed approaches of reading a text > file). > > Given all the above facts, it probably isn't worth your while to try > and optimize the code unless and until you find (by measurements) that > it's too slow - which is a good practice anyway: > > http://en.wikipedia.org/wiki/Optimization_(computer_science) > > Excerpt from the above page (its long but worth reading, IMO): > > "Donald Knuth said, paraphrasing Hoare[1], > > "We should forget about small efficiencies, say about 97% of the time: > premature optimization is the root of all evil." (Knuth, Donald. > Structured Programming with go to Statements, ACM Journal Computing > Surveys, Vol 6, No. 4, Dec. 1974. p.268.) > > Charles Cook commented, > > "I agree with this. It's usually not worth spending a lot of time > micro-optimizing code before it's obvious where the performance > bottlenecks are. But, conversely, when designing software at a system > level, performance issues should always be considered from the > beginning. A good software developer will do this automatically, > having developed a feel for where performance issues will cause > problems. An inexperienced developer will not bother, misguidedly > believing that a bit of fine tuning at a late
Unzip: Memory Error
I am trying to unzip an 18mb zip containing just a single 200mb file and I get a Memory Error. When I run the code on a smaller file 1mb zip, 11mb file, it works fine. I am running on a hosted Apache web server I am using some code I found on the web somewhere. def unzip_file_into_dir(file, dir): #os.mkdir(dir, 0777) zfobj = zipfile.ZipFile(file) for name in zfobj.namelist(): if name.endswith('/'): os.mkdir(os.path.join(dir, name)) else: outfile = open(os.path.join(dir, name), 'wb') outfile.write(zfobj.read(name)) outfile.close() Error Traceback: Line 357 gives the Memory Error I have removed paths from file references == MemoryError Python 2.3.4: /usr/bin/python Wed Aug 29 19:38:22 2007 A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred. /qlunzip.py 58 59 60 if __name__ == "__main__" : 61 unzipMain() 62 unzipMain = /qlunzip.py in unzipMain() 53 destdir = getDestDir() 54 print destdir, gv.nl 55 unzip_file_into_dir(zips, destdir) 56 57 global unzip_file_into_dir = , zips = '/ pcodes.zip', destdir = '/pcodes/' /qlunzip.py in unzip_file_into_dir(file='pcodes.zip', dir='pcodes/') 34 else: 35 outfile = open(os.path.join(dir, name), 'wb') 36 outfile.write(zfobj.read(name)) 37 outfile.close() 38 outfile = , outfile.write = , zfobj = , zfobj.read = >, name = 'pcodes.lst' /usr/lib/python2.3/zipfile.py in read(self=, name='pcodes.lst') 355 # zlib compress/decompress code by Jeremy Hylton of CNRI 356 dc = zlib.decompressobj(-15) 357 bytes = dc.decompress(bytes) 358 # need to feed in unused pad byte so that zlib won't choke 359 ex = dc.decompress('Z') + dc.flush() bytes = '\xc4\x9d]\x93\xab8\x92\x86\xef7b\xff\x83\xa3/\xf6f\xba \xa7\xe7\xa2g#vwf6\x8a\x02\xc3\xd04\x8d\r\x1e\x7f\xdclP\xb6\xcav\x1c \xca\xd4`\xfbx...; \xb7jp\x06V{\xaf\xc3\xa5\xa7;\xdd\xd2\xaaD\x7f)c \xc6\x9d\x0f\xf2\xff-\xc9\x92\xc3\x1d\xa4`\xe0\xb8\x06)\x188\x9cA\n \x06\x8e\x1bPc\xf8\xf0\x1f', dc = , dc.decompress = MemoryError: args = () Any help much appreciated Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: Unzip: Memory Error
On 29 Aug, 21:18, David Bolen <[EMAIL PROTECTED]> wrote: > mcl <[EMAIL PROTECTED]> writes: > > I am trying to unzip an 18mb zip containing just a single 200mb file > > and I get a Memory Error. When I run the code on a smaller file 1mb > > zip, 11mb file, it works fine. > (...) > > def unzip_file_into_dir(file, dir): > >#os.mkdir(dir, 0777) > >zfobj = zipfile.ZipFile(file) > >for name in zfobj.namelist(): > >if name.endswith('/'): > >os.mkdir(os.path.join(dir, name)) > >else: > >outfile = open(os.path.join(dir, name), 'wb') > >outfile.write(zfobj.read(name)) > >outfile.close() > > The "zfobj.read(name)" call is reading the entire file out of the zip > into a string in memory. It sounds like it's exceeding the resources > you have available (whether overall or because the Apache runtime > environment has stricter limits). > > You may want to peek at a recent message from me in the "Unable to > read large files from zip" thread, as the suggestion there may also be > suitable for your purposes. > > http://groups.google.com/group/comp.lang.python/msg/de04105c170fc805?... > -- David David, Thank you. I read your post and I basically understood the concept, butI could not get my head around the code, I need to write for my solution. (Newbie and a bit long in the tooth) To solve my problem, I think my best approach would be to read my zipped file / files from the zip archive when I need them. Max three users, occasional use. So no big overloading of host's server. pseudo code zfhdl = zopen(zip,filename) # Open File in Zip Archive for Reading while True: ln = zfhdl.readline()# Get nextline of file if not ln: # if EOF file break dealwithline(ln) # do whatever is necessary with file zfhdl.close That is probably over simplified, and probably wrong but you may get the idea of what I am trying to achieve. Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQLdb: ValueError Something Stupid
On 7 Sep, 14:11, Carsten Haese <[EMAIL PROTECTED]> wrote: > On Fri, 2007-09-07 at 05:52 -0700, mcl wrote: > > > ValueError: invalid literal for int(): 0- > > > args = ('invalid literal for int(): 0-',) > > > > = > > > Thanks Richard > > > Sort of solved it. > > > On that particular table it did not like * for all fields. > > > Any reason why that would be the case ? > > None that we can divine without more information. What's the schema for > the table in question, which column(s) are you excluding to make the > query work, and what kind of data is in the column(s) you're excluding? > > -- > Carsten Haesehttp://informixdb.sourceforge.net Thanks for replying. I did not exclude any columns and the Schema is: CREATE TABLE lstData ( qlCat varchar(20) NOT NULL default '', qlTitle varchar(255) NOT NULL default '', qlSubTitle varchar(255) default NULL, qlDetails text, qlPic varchar(20) default NULL, qlPostCode varchar(16) default NULL, qlUpd timestamp NOT NULL default '-00-00 00:00:00' on update CURRENT_TIMESTAMP, KEY `idx-qlCat` (qlCat) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; Thanks again Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQLdb: ValueError Something Stupid
On 7 Sep, 12:08, mcl <[EMAIL PROTECTED]> wrote: > I have just started with python and MySQL. > > I have a simple test for each of my tables. > > The first two work as expected, but the third 'qlooks', gives a > ValueError. > > If I copy and paste the SQL in the traceback to phpMyAdmin, it works > as expected. > > Can anyone give a clue as to what I am doing wrong > > Python 2.4 > MySQL 4.1.22-standard > > The tablename qlooks is LOWERCASE > > Code === > cursor = gv.conn.cursor() > queries = ["%septi%"] > fields = ("qlTitle", "qlSubTitle", "qlPostCode", "qlMap", > "qlDetails") > tblFields = "select * from qlooks " > whereTests = 'Where qlTitle like "%septi%"' > sql = tblFields + whereTests > #cursor.execute(sql) > cursor.execute('select * from PERMS') > row = cursor.fetchone() > print "Row=", row, gv.nl > cursor.execute('select * from LISTS') > row = cursor.fetchone() > print "Row=", row, gv.nl > cursor.execute('select * from qlooks') > row = cursor.fetchone() > print "Row=", row, gv.nl > > cursor.close() > > ERROR TRACEBACK > >79 print "Row=", row, gv.nl >80 cursor.execute('select * from qlooks') >81 row = cursor.fetchone() >82 print "Row=", row, gv.nl > cursor = , cursor.execute = method Cursor.execute of > > /usr/lib/python2.4/site-packages/MySQLdb/cursors.py in > execute(self=, query='select * from > qlooks', args=None) >93 """ >94 del self.messages[:] >95 return self._execute(query, args) >96 >97 def _execute(self, query, args): > self = , self._execute = method Cursor._execute of >, query = > 'select * from qlooks', args = None > /usr/lib/python2.4/site-packages/MySQLdb/cursors.py in > _execute(self=, query='select * from > qlooks', args=None) > 112 exc, value, tb = exc_info() > 113 del tb > 114 self.errorhandler(self, exc, value) > 115 self._executed = query > 116 return r > self = , self.errorhandler = method Connection.defaulterrorhandler of ...l.connection open to > 'db.sabren.com' at 936cac4>>, exc = , > value = > /usr/lib/python2.4/site-packages/MySQLdb/connections.py in > defaulterrorhandler(connection=<_mysql.connection open to > 'db.sabren.com' at 936cac4>, cursor=, > errorclass=, > errorvalue=) >31 else: >32 connection.messages.append(error) >33 raise errorclass, errorvalue >34 >35 > errorclass = , errorvalue = > > > ValueError: invalid literal for int(): 0- > args = ('invalid literal for int(): 0-',) > > = > Thanks Richard Sort of solved it. On that particular table it did not like * for all fields. Any reason why that would be the case ? Richard -- http://mail.python.org/mailman/listinfo/python-list
MySQL: Global Connection
It is tough when the grey matter is getting past it. I am starting to use MySQL and I would like to make my connection to my database a variable in my Global Variable Class, but I just can not see how to do it. Class GlobalVars : -- http://mail.python.org/mailman/listinfo/python-list
MySQLdb: ValueError Something Stupid
I have just started with python and MySQL. I have a simple test for each of my tables. The first two work as expected, but the third 'qlooks', gives a ValueError. If I copy and paste the SQL in the traceback to phpMyAdmin, it works as expected. Can anyone give a clue as to what I am doing wrong Python 2.4 MySQL 4.1.22-standard The tablename qlooks is LOWERCASE Code === cursor = gv.conn.cursor() queries = ["%septi%"] fields = ("qlTitle", "qlSubTitle", "qlPostCode", "qlMap", "qlDetails") tblFields = "select * from qlooks " whereTests = 'Where qlTitle like "%septi%"' sql = tblFields + whereTests #cursor.execute(sql) cursor.execute('select * from PERMS') row = cursor.fetchone() print "Row=", row, gv.nl cursor.execute('select * from LISTS') row = cursor.fetchone() print "Row=", row, gv.nl cursor.execute('select * from qlooks') row = cursor.fetchone() print "Row=", row, gv.nl cursor.close() ERROR TRACEBACK 79 print "Row=", row, gv.nl 80 cursor.execute('select * from qlooks') 81 row = cursor.fetchone() 82 print "Row=", row, gv.nl cursor = , cursor.execute = > /usr/lib/python2.4/site-packages/MySQLdb/cursors.py in execute(self=, query='select * from qlooks', args=None) 93 """ 94 del self.messages[:] 95 return self._execute(query, args) 96 97 def _execute(self, query, args): self = , self._execute = >, query = 'select * from qlooks', args = None /usr/lib/python2.4/site-packages/MySQLdb/cursors.py in _execute(self=, query='select * from qlooks', args=None) 112 exc, value, tb = exc_info() 113 del tb 114 self.errorhandler(self, exc, value) 115 self._executed = query 116 return r self = , self.errorhandler = >, exc = , value = /usr/lib/python2.4/site-packages/MySQLdb/connections.py in defaulterrorhandler(connection=<_mysql.connection open to 'db.sabren.com' at 936cac4>, cursor=, errorclass=, errorvalue=) 31 else: 32 connection.messages.append(error) 33 raise errorclass, errorvalue 34 35 errorclass = , errorvalue = ValueError: invalid literal for int(): 0- args = ('invalid literal for int(): 0-',) = Thanks Richard -- http://mail.python.org/mailman/listinfo/python-list
Accessing a URL file Remotely
I have been given a url of CSV file (http://hostname/dir/file.csv), which when I put the full URL in a web browser shows the contents of the file. I want to be able to use the CSV module to read that file, which I have successfully used with a local CSV file. Any examples anywhere would be appreciated. My basic research suggests URLLIB, but I can not find a suitable example. I do not understand what additional information, needs to be given to the remote site in order for it to expose the contents of the file as though I was reading it locally. Do I need to copy it to my local machine or can I read it directly from the remote site. As you can probably tell, I have never done anything like this, so any help will be gratefully received. Thanks Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: Accessing a URL file Remotely
On 29 Nov, 22:32, TheSeeker <[EMAIL PROTECTED]> wrote: > On Nov 29, 3:13 pm, mcl <[EMAIL PROTECTED]> wrote: > > > > > I have been given a url ofCSVfile (http://hostname/dir/file.csv), > > which when I put the full URL in a web browser shows the contents of > > the file. > > > I want to be able to use theCSVmodule to read that file, which I > > have successfully used with a localCSVfile. > > > Any examples anywhere would be appreciated. > > > My basic research suggests URLLIB, but I can not find a suitable > > example. > > > I do not understand what additional information, needs to be given to > > the remote site in order for it to expose the contents of the file as > > though I was reading it locally. > > > Do I need to copy it to my local machine or can I read it directly > > from the remote site. > > > As you can probably tell, I have never done anything like this, so any > > help will be gratefully received. > > > Thanks > > > Richard > > -untested-- > > import urllib,csv > > fp_page = urllib.urlopen("http://URL_of_file";) > reader =csv.reader(fp_page) > for row in reader: > print row > > Duane Duane, Brilliant - I had fears of much more complication. Thank you very much Richard -- http://mail.python.org/mailman/listinfo/python-list
Standalone USB Web Server and Python Interpeter
I would like to have a USB pen drive, which can execute python scripts including CGI versions, which I can take to any Windows PC and run without having to install anything on the PC. My days of hacking are past so I am looking for something very simple. I would envisage a batch file which would ask if the USB web server needed to be started or just python scripts and it would return the IP address of the server or even some local host name such as 'bobserver' which I could give it. I need v2.3 python. Is this possible Thanks Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: Standalone USB Web Server and Python Interpeter
On 5 Dec, 08:31, Shane Geiger <[EMAIL PROTECTED]> wrote: > mcl wrote: > > I would like to have a USB pen drive, which can execute python scripts > > including CGI versions, which I can take to any Windows PC and run > > without having to install anything on the PC. > > > My days of hacking are past so I am looking for something very simple. > > > I would envisage a batch file which would ask if the USB web server > > needed to be started or just python scripts and it would return the IP > > address of the server or even some local host name such as 'bobserver' > > which I could give it. > > > I need v2.3 python. > > > Is this possible > > > Thanks > > > Richard > > To the best of my knowledge you should be squared away if you have > Movable Python and the script below. Let me know whether it works for > you. (You might have to adjust the path somewhat.) > > Movable Python: http://www.voidspace.org.uk/python/movpy/ > > #!/usr/bin/python > > """ > The CGIHTTPServer module > This is a simple HTTP server that can call external scripts through the > common gateway interface > (CGI). > Example: Using the CGIHTTPServer module > # File:cgihttpserver-example-1.py > > If you are reading this, you have all the source code necessary to run > your own Web server. > Run this with Python. > > Note: This script allows symlinks to places outside the document root. > > """ > try: > from mywebserver_config import * > except: > print "No mywebserver_config module found. Using defaults." > pass > > import CGIHTTPServer > import BaseHTTPServer > class Handler(CGIHTTPServer.CGIHTTPRequestHandler): > cgi_directories = ["/cgi"] > > def writefile(f, data, perms=750): open(f, 'w').write(data) and > os.chmod(f, perms) > def readfile(f): return open(f, 'r').read() > > html_template = """ Transitional//EN"> > > > temp > > > > Temporary Server > This is a temporary server. Do not expect to have access to this > indefinitely. This is likely running on my laptop. > > > """ > > index2_html = """ Transitional//EN"> > > > > > > > CGI scripts > cgi/test.py > > > """ > > example_cgi = """#!/usr/bin/python > > # Required header that tells the browser how to render the text. > print "Content-Type: text/html\\n\\n" > > # Print a simple message to the display window. > print "Hello, World!\\n" > > print "What follows are some files in the CGI directory:" > import commands > output = commands.getoutput('ls -1 cgi/').splitlines() > for item in output: > print "" > print item, > print "" > print "" > > """ > > def readip(): > """returns your external IP address by querying dyndns.org > """ > import re, urllib > f = urllib.urlopen('http://checkip.dyndns.org') > s = f.read() > m = re.search('([\d]*\.[\d]*\.[\d]*\.[\d]*)', s) > return m.group(0) > > print "http://"+readip()+":8000/" > > import urllib > import os > > originaldir = os.getcwd() > > # For safety, changing the document root > import tempfile, os > DOCUMENT_ROOT = tempfile.mkdtemp() > print "DOCUMENT_ROOT: " + DOCUMENT_ROOT > os.chdir(DOCUMENT_ROOT) > writefile(DOCUMENT_ROOT+"/README.html",html_template) > > #print readfile(originaldir+'/'+__file__) > writefile(DOCUMENT_ROOT+"/"+os.path.basename(os.getcwd()+'/'+__file__),readfile(originaldir > +'/'+__file__)) # write a copy of this file > > # create a cgi directory > os.mkdir(DOCUMENT_ROOT+'/cgi') > writefile(DOCUMENT_ROOT+"/cgi/test.py",example_cgi) > os.chmod(DOCUMENT_ROOT+"/cgi/test.py",755) > ### not sure why the previous line doesn't work, but this next > (os-dependent) line does work (on some OSes): > os.system('chmod 755 ' + DOCUMENT_ROOT+"/cgi/test.py") > > ### INDEX2.HTML > writefile(DOCUMENT_ROOT+"/index2.html",index2_html) > os.system('chmod 755 ' + DOCUMENT_ROOT+"/index2.html") > > try: > os.remove('/tmp/web') # this path is OS-dependent > os.symlink(DOCUMENT_ROOT, '/tmp/web') > print "Created symlink /tmp/web" > except: > pass > > import os > os.symlink( "/Users/shanegeiger/Desktop", DOCUMENT_ROOT+'/Desktop' ) > os.symlink( "cgi/test.py", DOCUMENT_ROOT+'/test.py' ) > > PORT = 8000 > #httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler) > #httpd = BaseHTTPServer.HTTPServer(("10.37.129.2", PORT), Handler) > httpd = BaseHTTPServer.HTTPServer(("0.0.0.0", PORT), Handler) > > print "serving at port", PORT > httpd.serve_forever() > > -- > Shane Geiger > IT Director > National Council on Economic Education > [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net > > Leading the Campaign for Economic and Financial Literacy > > signature.asc > 1KDownload Shane, WOW Thank you for the very detailed reply. I will try to get it working and let you know how I get on. Richard -- http://mail.python.org/mailman/listinfo/python-list
Problem with Image: Opening a file
I am obviously doing something stupid or not understanding the difference between HTML file references and python script file references. I am trying to create a thumbnail of an existing .jpg file. It is in the directory 'temp', which is below my script I can display the file with , but Image.open does not see it. I think it is probably a path problem, but I have tried '/temp/ fred.jpg' and './temp/fred.jpg' Can anyone help, please PS I am using mod_python on a Linux Apache 2 and python 2.5 CODE = import os import StringIO def index(req): main(req) def writeHTMLHeader(req): req.content_type = "text/html" req.write('http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>') req.write('http://www.w3.org/1999/xhtml"; lang="en" xml:lang="en">') req.write('My Tests: Python') req.write('') def writeHTMLTrailer(req): req.write('') req.write('') def writelineHTML(req, htmlMSG): req.write(htmlMSG + '\n') def createThumb(req, infile, maxwidth, maxheight): import Image import os infile = "temp/fred.jpg" outfile = "temp/fred_th.jpg" if infile != outfile: writelineHTML(req, "Opening %s" % str(infile)) writelineHTML(req, '' % infile) # Finds File OK *** im = Image.open(infile) # ERRORS on the same file * im.thumbnail((maxwidth,maxheight),Image.ANTIALIAS) im.save(outfile, "JPEG") writelineHTML(req, "Created: " + str(outfile)) writelineHTML(req, '' % outfile) return outfile return "" def showThumbnail(req, picture): myThumb = createThumb(req, picture, 60, 60) if myThumb: writelineHTML(req, 'http://mail.python.org/mailman/listinfo/python-list
Re: Problem with Image: Opening a file
On Feb 4, 10:43 pm, Graham Dumpleton <[EMAIL PROTECTED]> wrote: > On Feb 4, 6:51 pm,mcl<[EMAIL PROTECTED]> wrote: > > > > > I am obviously doing something stupid or not understanding the > > difference between HTML file references and python script file > > references. > > > I am trying to create a thumbnail of an existing .jpg file. It is in > > the directory 'temp', which is below my script > > > I can display the file with , but Image.open does not see it. > > > I think it is probably a path problem, but I have tried '/temp/ > > fred.jpg' and './temp/fred.jpg' > > > Can anyone help, please > > > PS I am usingmod_pythonon a Linux Apache 2 and python 2.5 > > > CODE = > > > import os > > import StringIO > > > def index(req): > > main(req) > > > def writeHTMLHeader(req): > > > req.content_type = "text/html" > > req.write(' > EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>') > > req.write('http://www.w3.org/1999/xhtml"; lang="en" > > xml:lang="en">') > > req.write('My Tests: Python') > > req.write('') > > > def writeHTMLTrailer(req): > > req.write('') > > req.write('') > > > def writelineHTML(req, htmlMSG): > > req.write(htmlMSG + '\n') > > > def createThumb(req, infile, maxwidth, maxheight): > > import Image > > import os > > infile = "temp/fred.jpg" > > outfile = "temp/fred_th.jpg" > > if infile != outfile: > > > writelineHTML(req, "Opening %s" % str(infile)) > > writelineHTML(req, '' % infile) # Finds > > File OK *** > > im = > > Image.open(infile) # > > ERRORS on the same file * > > im.thumbnail((maxwidth,maxheight),Image.ANTIALIAS) > > im.save(outfile, "JPEG") > > writelineHTML(req, "Created: " + str(outfile)) > > writelineHTML(req, '' % outfile) > > > return outfile > > > return "" > > > def showThumbnail(req, picture): > > myThumb = createThumb(req, picture, 60, 60) > > if myThumb: > > writelineHTML(req, ' > > def main(req): > > writeHTMLHeader(req) > > picture = "temp/fred.jpg" > > showThumbnail(req, picture) > > writeHTMLTrailer(req) > > > ERROR == > > > File "/home/mcl/htdocs/timslists/thumbs2.py", line 33, in > > createThumb > > im = Image.open(infile) > > > File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1888, in > > open > > fp = __builtin__.open(fp, "rb") > > > IOError: [Errno 2] No such file or directory: 'temp/fred.jpg' > > > Richard > > Code under mod_python will typically run as Apache user. This means > that when writing files the location you use must be writable to the > Apache user. Do note though that the working directory of Apache is > not guaranteed and you must always use an absolute path to the > location you are saving files, you cannot reliably use relative paths > like you are. > > BTW, do you perhaps mean '/tmp/fred.jpg'? There is no '/temp' > directory on Linux boxes. > > Graham Graham, Thanks for the explanation. The answer is/was Python script os.path.dirname(__file__) + '/mydir/temp/fred.jpg' HTML 'temp/fred.jpg' Thanks again Richard -- http://mail.python.org/mailman/listinfo/python-list
mysqldb: Rows READ or Processed
I have looked through Python Database API Specification v2.0, but can not find any reference to the number of records processed in a select query. I know I can get the number of records returned with cursor.rowcount, but I want to know the number of records processed. I suppose the info is in one of the internal tables, but I can not find any info on that, because phpMyAdmin shows the number of rows in a table. I suppose I could use count(*), but would that process all the records, which would seem a bit silly. What is the best method ? Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: mysqldb: Rows READ or Processed
On Feb 7, 12:19 am, Carsten Haese <[EMAIL PROTECTED]> wrote: > On Wed, 2008-02-06 at 14:51 -0800, mcl wrote: > > I have looked through Python Database API Specification v2.0, but can > > not find any reference to the number of records processed in a select > > query. > > > I know I can get the number of records returned with cursor.rowcount, > > but I want to know the number of records processed. > > > I suppose the info is in one of the internal tables, but I can not > > find any info on that, because phpMyAdmin shows the number of rows in > > a table. > > > I suppose I could use count(*), but would that process all the > > records, which would seem a bit silly. > > > What is the best method ? > > Please define what you mean by "processed". If you simply need to know > how many rows are in a table, "select count(*) from that_table" is the > obvious solution. If the database engine is sufficiently intelligent, it > won't have to read the data in every single row to count the rows. > > If that's not what you're looking for, please feel free to be more > specific about what you need to achieve. > > -- > Carsten Haesehttp://informixdb.sourceforge.net Thanks for all the helpful replies. If you think Count(*) is intelligent, then that is the easy answer. I will attempt to do some timings. I did mean number of rows in a table - by processed. -- http://mail.python.org/mailman/listinfo/python-list
Confused yet again: Very Newbie Question
Why can I not the change the value of a variable in another class, when I have passed it via a parameter list. I am sure I am being stupid, but I thought passed objects were Read/ Write eg #!/usr/bin/python class one(): #my Global Vars fred = 'fred' class three(): def createJoe(self, myName): c1 = one() myName = 'Joe' #* Question why does this not change variable fred in 'class one' print 'Three(Local): ' + myName + 'Three(Global): ' + c1.fred def main(): c1 = one() c3 =three() c3.createJoe(c1.fred) if __name__ == '__main__' : main() Results: Three(Local): JoeThree(Global): fred 'fred' in 'class one' does not get changed to 'joe' in 'class three' 'createJoe', even though I have passed 'class one' 'fred' to 'createJoe' I hope this makes sense. I did not think you had to make the distinction between 'byvar' and 'byref' as in Basic. Thanks Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused yet again: Very Newbie Question
On 7 Jul, 13:09, Jeff <[EMAIL PROTECTED]> wrote: > When you call c3.createJoe(c1.fred), you are passing a copy of the > value stored in c1.fred to your function. Python passes function > parameters by value. The function will not destructively modify its > arguments; you must expliticly state your intention to modify an > object: > > class one(): > fred = 'fred' > > class three(): > def createJoe(self, myName): > return "Joe" > > def main(): > c1 = one() > c3 = three() > c1.fred = c3.createJoe() # Modify c1's attribute, fred, with the > return value from c3.createJoe Thank you very much for your helpful replies. Two questions: One: My use of classes is because I want two classes one for global variables and one for global functions. A function may set multiple global variable values, so what is the best way to do this as 'return' only appears to be able to set one value. Two: I am sorry but I do not understand the significance defining a Class as: >>> class MyClass(object): what is object ? I am using python with Google App Engine, and I only have Alex Martelli's book up to Python 2.2, if they has any relevance ? Thanks again for your help. I now understand what my mistakes were and why I was not getting the results I had expected. Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused yet again: Very Newbie Question
On Jul 7, 5:07 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Mon, 7 Jul 2008 05:41:22 -0700 (PDT), mcl <[EMAIL PROTECTED]> > declaimed the following in comp.lang.python: > > > My use of classes is because I want two classes one for global > > variables and one for global functions. > > Which seems to violate the entire reason for using "classes" (and > sounds like something that Java's syntax forces upon one). Classes (and > instances of them) are typically defined to /combine/ the class/instance > specific data and the functions that operate upon that data into a > single object. > > If you just need a container for so-called "global" data, create a > module. Same for utility functions. > > -=-=-=-=-=- myGlobalData.py > > something = 1 > else = 3 > > -=-=-=-=-=- myUtilityFuncs.py > > def weird(a, b): > return (a-b, b-a, a*b, a/b, float(a)/b) > > -=-=-=-=-=- main.py > > import myGlobalData > import myUtilityFuncs > > results = myUtilityFuncs.weird(myGlobalData.something, > > myGlobalData.else) > print results > myGlobalData.something = results[2] > > > A function may set multiple global variable values, so what is the > > best way to do this as 'return' only appears to be able to set one > > value. > > -- > Wulfraed Dennis Lee Bieber KD6MOG > [EMAIL PROTECTED] [EMAIL PROTECTED] > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: [EMAIL PROTECTED]) > HTTP://www.bestiaria.com/ Dennis, Thank you for this reply. You are correct in what I was trying to do and thank you for your very good example. I will now rewrite my code changing classes to modules, which make more sense to me, even if it is not proper Python. I am much wiser about classes, but still a long way from fully comprehending when and where to best use them. Thank you all for your replies and links, as it solved my problems enough, to get some code working. Richard -- http://mail.python.org/mailman/listinfo/python-list
iptcinfo: Can not import: Newbie not really knowing what he is doing
My Code `import os from PIL import Image from iptcinfo import IPTCInfo info = IPTCInfo('test.jpg') print info.keywords, info.supplementalCategories, info.contacts caption = info.data['caption/abstract'] print caption` running Win XP SP3 I get the message No module: iptcinfo I have downloaded iptcinfo and placed it in python27\Lib\site-packages \iptcinfo I guessed that was the right place, because that is where PIL ended up, but that had a fancy installer with it. As you will appreciate, I am very much an amateur in all this. I can code a bit of python, but where everything goes and links is a complete mystery. All I want is to write a quick script which extracts the Caption field from a directory of JPEGS. I get the filenames and the dimensions with PIL, but I need IPTCinfo to get at the Caption field. If anyone can throw any light on what I need to do or which stupid mistake I have made, I would be most grateful. Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: iptcinfo: Can not import: Newbie not really knowing what he is doing
On Jul 12, 4:11 pm, Nick Raptis wrote: > Hi Richard!> I have downloaded iptcinfo and placed it in > python27\Lib\site-packages > > \iptcinfo > > > I guessed that was the right place, because that is where PIL ended > > up, but that had a fancy installer with it. > > You did place it in the right path, but the "fancy installer" does one > more thing, it registers the package into the python-path, so python can > get to it. > I've long switched from windows unfortunately and can't remember the way > it does this though, so you can do it manually. > > Have some good news though: I looked in the IPTCinfo package and it does > indeed include a setup.py, which is a standard way of installing python > packages. > All you need to do is unpack the package to any folder and run > setup.py install > from that folder, which should take care of everything for you. > > Some documentation on installing packages for you to > read:http://docs.python.org/install/ > > Nick What a star. It is all now working. I will try to look at the reference you mentioned, but at the moment I can not even figure out why 'python' on its own does not work. I would have thought the installation would have set the appropriate environment variables, but it does not seem to have. I have to give the full path name to python.exe. Just remembered SET at DOS prompt, but no mention of PYTHON anywhere. Thank you very much again - I now have a list of all my JPEGS and their CAPTIONS. Richard -- http://mail.python.org/mailman/listinfo/python-list