hi can any one please help me..
Hi, when i'm installing the pytc(python wrapper for tokyo cabinet.) i'm getting the fallowing error.. i'm getting this error for python2.6 only.. for python 2.4 its working fine.. - running install running build running build_ext building 'pytc' extension gcc -pthread -shared build/temp.linux-x86_64-2.6/pytc.o -L/usr/local/ lib -L. -ltokyocabinet -lpython2.6 -o build/lib.linux-x86_64-2.6/ pytc.so /usr/bin/ld: cannot find -lpython2.6 collect2: ld returned 1 exit status error: command 'gcc' failed with exit status 1 - What is that error. if any body know please help me.. thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: hi can any one please help me..
* chiranjeevi muttoju: Hi, when i'm installing the pytc(python wrapper for tokyo cabinet.) i'm getting the fallowing error.. i'm getting this error for python2.6 only.. for python 2.4 its working fine.. - running install running build running build_ext building 'pytc' extension gcc -pthread -shared build/temp.linux-x86_64-2.6/pytc.o -L/usr/local/ lib -L. -ltokyocabinet -lpython2.6 -o build/lib.linux-x86_64-2.6/ pytc.so /usr/bin/ld: cannot find -lpython2.6 collect2: ld returned 1 exit status error: command 'gcc' failed with exit status 1 - What is that error. if any body know please help me.. thank you. It means the compiler can't find the python2.6 library; it's not in any of the directories where gcc searches for libraries. You can specify (additional) directories where gcc should search for libraries via the LIBRARY_PATH environment variable. At least according to my MinGW documentation for Windows, but I assume that it's the same in Linux. Cheers hth., - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: hi can any one please help me..
Hello, On 02/15/2010 02:20 PM, chiranjeevi muttoju wrote: Hi, when i'm installing the pytc(python wrapper for tokyo cabinet.) i'm getting the fallowing error.. i'm getting this error for python2.6 only.. for python 2.4 its working fine.. - running install running build running build_ext building 'pytc' extension gcc -pthread -shared build/temp.linux-x86_64-2.6/pytc.o -L/usr/local/ lib -L. -ltokyocabinet -lpython2.6 -o build/lib.linux-x86_64-2.6/ pytc.so /usr/bin/ld: cannot find -lpython2.6 ^^ You need to install the python development libraries. For example if you are doing this on a Fedora or Red Hat Enterprise Linux system, execute the command: $ yum install python-devel ...to install the necessary libraries. cheers, - steve -- http://mail.python.org/mailman/listinfo/python-list
Printing with raw_input
Does `raw_input` uses internally `sys.stdout.write`? -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing with raw_input
Joan Miller wrote: Does `raw_input` uses internally `sys.stdout.write`? You can test this yourself without reading the C source: Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55) [GCC 4.4.1] on linux2 Type help, copyright, credits or license for more information. import sys class A: ... def write(self, s): ... sys.__stdout__.write(s.upper()) ... sys.stdout = A() print yadda YADDA raw_input(foo) FOObar 'BAR' Peter -- http://mail.python.org/mailman/listinfo/python-list
trouble installing matplotlib - syslibroot: multiply specified
Hi, Hope you guys don't mind a question about building matplotlib from a biologist who wants to learn how to use python. I am trying to install matplotlib on my mac with OS X 10.4.11, using python 2.6.4 and Xcode 2.2.1. I have had a few fights with freetype and Tkinter, and I think I've got those worked out. I have configured and installed numpy, freetype, zlib, and libpng. But I am still getting an error about multiply specified syslibroot - does this make sense to anyone? Maybe I need to change the PATH somehow? Or maybe Xcode is not installed properly? (I am using the make.osx provided with matplotlib, and I am including the top of the file as I am running it here: # build mpl into a local install dir with PREFIX=/usr/local MPLVERSION=0.99.1.1 PYVERSION=2.6 PYTHON=python${PYVERSION} ZLIBVERSION=1.2.3 PNGVERSION=1.2.33 FREETYPEVERSION=2.3.7 MACOSX_DEPLOYMENT_TARGET=10.4 ## You shouldn't need to configure past this point CFLAGS=-arch i386 -arch ppc -I${PREFIX}/include -isysroot /Developer/ SDKs/MacOSX10.4u.sdk LDFLAGS=-arch i386 -arch ppc -L${PREFIX}/lib -syslibroot,/Developer/ SDKs/MacOSX10.4u.sdk -- ** -- Now here is what happens when I try to make the make.osx: katrine-whitesons-computer:/Applications/Q/matplotlib-0.99.1.1 katrinewhiteson$ make -f make.osx mpl_build export MACOSX_DEPLOYMENT_TARGET=10.4 \ export CFLAGS=-arch i386 -arch ppc -I/usr/local/include -I/usr/local/ include/freetype2 -isysroot /Developer/SDKs/MacOSX10.4u.sdk \ export LDFLAGS=-arch i386 -arch ppc -L/usr/local/lib -syslibroot,/ Developer/SDKs/MacOSX10.4u.sdk \ python2.6 setup.py build BUILDING MATPLOTLIB matplotlib: 0.99.1.1 python: 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32) [GCC 4.0.1 (Apple Inc. build 5493)] platform: darwin REQUIRED DEPENDENCIES numpy: 1.3.0 freetype2: found, but unknown version (no pkg-config) * WARNING: Could not find 'freetype2' headers in any * of '.', './freetype2'. OPTIONAL BACKEND DEPENDENCIES libpng: found, but unknown version (no pkg-config) * Could not find 'libpng' headers in any of '.' Tkinter: Tkinter: 73770, Tk: 8.4, Tcl: 8.4 wxPython: no * wxPython not found Gtk+: no * Building for Gtk+ requires pygtk; you must be able * to import gtk in your build/install environment Mac OS X native: yes Qt: no Qt4: no Cairo: no OPTIONAL DATE/TIMEZONE DEPENDENCIES datetime: present, version unknown dateutil: matplotlib will provide pytz: matplotlib will provide adding pytz OPTIONAL USETEX DEPENDENCIES dvipng: no ghostscript: /bin/sh: line 1: gs: command not found latex: no [Edit setup.cfg to suppress the above messages] pymods ['pylab'] packages ['matplotlib', 'matplotlib.backends', 'matplotlib.projections', 'mpl_toolkits', 'mpl_toolkits.mplot3d', 'mpl_toolkits.axes_grid', 'matplotlib.sphinxext', 'matplotlib.numerix', 'matplotlib.numerix.mlab', 'matplotlib.numerix.ma', 'matplotlib.numerix.linear_algebra', 'matplotlib.numerix.random_array', 'matplotlib.numerix.fft', 'matplotlib.delaunay', 'pytz', 'dateutil', 'dateutil/zoneinfo'] running build running build_py copying lib/matplotlib/mpl-data/matplotlibrc - build/lib.macosx-10.4- fat-2.6/matplotlib/mpl-data copying lib/matplotlib/mpl-data/matplotlib.conf - build/ lib.macosx-10.4-fat-2.6/matplotlib/mpl-data running build_ext building 'matplotlib.ft2font' extension c++ -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -g - bundle -undefined dynamic_lookup -arch i386 -arch ppc -L/usr/local/lib -syslibroot,/Developer/SDKs/MacOSX10.4u.sdk -arch i386 -arch ppc -I/ usr/local/include -I/usr/local/include/freetype2 -isysroot /Developer/ SDKs/MacOSX10.4u.sdk build/temp.macosx-10.4-fat-2.6/src/ft2font.o build/temp.macosx-10.4-fat-2.6/src/mplutils.o build/temp.macosx-10.4- fat-2.6/CXX/cxx_extensions.o build/temp.macosx-10.4-fat-2.6/CXX/ cxxsupport.o build/temp.macosx-10.4-fat-2.6/CXX/ IndirectPythonInterface.o build/temp.macosx-10.4-fat-2.6/CXX/ cxxextensions.o -lfreetype -lz -lstdc++ -lm -o build/lib.macosx-10.4- fat-2.6/matplotlib/ft2font.so powerpc-apple-darwin8-g++-4.0.1: unrecognized option '-syslibroot,/ Developer/SDKs/MacOSX10.4u.sdk' i686-apple-darwin8-g++-4.0.1: unrecognized option '-syslibroot,/ Developer/SDKs/MacOSX10.4u.sdk' //usr/bin/ld: -usr/syslibroot: multiply specified bincollect2: /ld: -syslibroot: multiply specified ld returned 1 exit statuscollect2: ld returned 1 exit
Re: trouble installing matplotlib - syslibroot: multiply specified
* katrine: Hope you guys don't mind a question about building matplotlib from a biologist who wants to learn how to use python. I am trying to install matplotlib on my mac with OS X 10.4.11, using python 2.6.4 and Xcode 2.2.1. I have had a few fights with freetype and Tkinter, and I think I've got those worked out. I have configured and installed numpy, freetype, zlib, and libpng. But I am still getting an error about multiply specified syslibroot - does this make sense to anyone? Don't know if this will help, but I just googled syslibroot, and it coughed up the following discussion: url: http://lists.apple.com/archives/unix-porting/2005/Oct/msg3.html Quoting from that thread: Try not setting LDFLAGS. Passing -isysroot to gcc might cause it to pass -isyslibroot to the linker if you're using gcc to link. Cheers, - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: python crash on windows but not on linux
On 13 feb, 13:48, Dave Angel da...@ieee.org wrote: hjebbers wrote: On Feb 13, 10:25 am, Dennis Lee Bieber wlfr...@ix.netcom.com wrote: On Fri, 12 Feb 2010 09:21:07 -0800 (PST), hjebbers hjebb...@gmail.com declaimed the following in gmane.comp.python.general: What strikes me is: 1. the crash on windows, but linux works OK (same test sets) 2. the linux box has 750Mb RAM, the windows box has 1.5Gb (twice as much). Which on its own does not mean much. Windows in a normal installation only grants 2GB address space to user code, reserving the other 2GB space for the OS shared libraries. If your program attempts to allocate over that, it will fail. That the Windows box has twice the physical memory only means it doesn't resort to page swapping as soon. There is a boot parameter switch that toggles Windows into a 3GB user/1GB OS mode -- hey, that would be great!! on my 1,5G mahcine ;-) it is mainly meant for server machines where there won't be many disjoint OS libraries loaded, but the server applications need lots of data space. What split does the Linux OS use? If it give 3GB to user space, while you'd start to page swap much soon, you'd also have 50% more virtual memory that can be allocated than under Windows. -- Wulfraed Dennis Lee Bieber KD6MOG wlfr...@ix.netcom.com HTTP://wlfraed.home.netcom.com/ I will check this.any advice on how to check this? henk-jan As I posted in recent thread on Tutor, But the one you might want is a boot.ini option that tells the OS to only reserve 1gb for itself, and leave 3gb for user space. But there are tradeoffs, including the need to modify an application's executable to take advantage of it. And the whole system may run substantially slower, even when your're extended app isn't running. See links: http://www.microsoft.com/whdc/system/platform/server/PAE/PAEmem.mspx http://blogs.technet.com/askperf/archive/2007/03/23/memory-management... DaveA Yes, you are right, i also would need to modify the executable. the reason why I posted on this list was the hard crash of python - which python really should not do I think. AFAICS there is no 'bug' in my edi translator (runs OK on linux) - but it uses far to much memory. For me, I am going to bring back the memory footprint of my edi translator. Which should be fairly easy to do. kind regards, henk-jan -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing with raw_input
On 15 feb, 10:11, Peter Otten __pete...@web.de wrote: Joan Miller wrote: Does `raw_input` uses internally `sys.stdout.write`? You can test this yourself without reading the C source: Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55) [GCC 4.4.1] on linux2 Type help, copyright, credits or license for more information. import sys class A: ... def write(self, s): ... sys.__stdout__.write(s.upper()) ... sys.stdout = A() print yadda YADDA raw_input(foo) FOObar 'BAR' Peter It was to display the output inside a GUI app. overriding `sys.stdout`. And as `print` also uses internally `sys.stdout.write` then can be used `print` the shell script and get the output too in the GUI, cann't it? -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing with raw_input
Joan Miller wrote: Does `raw_input` uses internally `sys.stdout.write`? It was to display the output inside a GUI app. overriding `sys.stdout`. And as `print` also uses internally `sys.stdout.write` then can be used `print` the shell script and get the output too in the GUI, cann't it? It should be easy to collect data written with print and show it in a gui, but I can't see how you would integrate raw_input() into a gui app. As to shell scripts, you can invoke them via subprocess, or, if the script needs user interaction, via pexpect. Peter -- http://mail.python.org/mailman/listinfo/python-list
Executing a command from within python using the subprocess module
Dear Folks, I want to execute a command from within python using the subprocess module. Coming from a Perl background, I thought I could use variable interpolation in strings, but found that this is neither supported nor the Python way. Accordingly, I am a little at sea about how to accomplish it. I have stated what I am trying to do in the minimal example below: --- import subprocess width = 5 height = 30 colors = ['#abcdef]', '#456789'] filename = /tmp/image.png # I want to get the equivalent of variable interpolation in Perl # so that the command # # convert -size 5x30 gradient:#abcdef-#456789 /tmp/image.png # # is derived from the variables above # and executed by subprocess.call() or subprocess.Popen() # from within Python # # Note that the command convert is from the ImageMagick suite # It exists and is executable by the shell; # the remaining values are arguments. # The command has been confirmed to execute correctly. --- Thanks in advance. Chandra -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing a command from within python using the subprocess module
* R (Chandra) Chandrasekhar: width = 5 height = 30 colors = ['#abcdef]', '#456789'] filename = /tmp/image.png # I want to get the equivalent of variable interpolation in Perl # so that the command # # convert -size 5x30 gradient:#abcdef-#456789 /tmp/image.png # # is derived from the variables above Assuming that the extra right square bracket in 'colors' is a typo, here's one way: s = convert -size {w}x{h} gradient:{g1}-{g2} {f}.format( w = width, h = height, g1 = colors[0], g2 = colors[1], f = filename ) Cheers hth., - ALf -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing a command from within python using the subprocess module
R (Chandra) Chandrasekhar wrote: I want to execute a command from within python using the subprocess module. Coming from a Perl background, I thought I could use variable interpolation in strings, but found that this is neither supported nor the Python way. Accordingly, I am a little at sea about how to accomplish it. import subprocess def convert(width=5, height=30, colors=['#abcdef', '#456789'], filename=tmp/image with space in its name.png): lookup = locals() assert all(\n not in str(s) for s in lookup.values()) subprocess.call(\ convert -size {width}x{height} gradient:{colors[0]}-{colors[1]} {filename}.format(**lookup).splitlines()) convert() Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing with raw_input
raw_input uses sys.stderr I guess ? On Mon, Feb 15, 2010 at 5:35 PM, Peter Otten __pete...@web.de wrote: Joan Miller wrote: Does `raw_input` uses internally `sys.stdout.write`? It was to display the output inside a GUI app. overriding `sys.stdout`. And as `print` also uses internally `sys.stdout.write` then can be used `print` the shell script and get the output too in the GUI, cann't it? It should be easy to collect data written with print and show it in a gui, but I can't see how you would integrate raw_input() into a gui app. As to shell scripts, you can invoke them via subprocess, or, if the script needs user interaction, via pexpect. Peter -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: trouble installing matplotlib - syslibroot: multiply specified
On Feb 15, 12:20 pm, Alf P. Steinbach al...@start.no wrote: * katrine: Hope you guys don't mind a question about building matplotlib from a biologist who wants to learn how to use python. I am trying to install matplotlib on my mac with OS X 10.4.11, using python 2.6.4 and Xcode 2.2.1. I have had a few fights with freetype and Tkinter, and I think I've got those worked out. I have configured and installed numpy, freetype, zlib, and libpng. But I am still getting an error about multiply specified syslibroot - does this make sense to anyone? Don't know if this will help, but I just googled syslibroot, and it coughed up the following discussion: url:http://lists.apple.com/archives/unix-porting/2005/Oct/msg3.html Quoting from that thread: Try not setting LDFLAGS. Passing -isysroot to gcc might cause it to pass -isyslibroot to the linker if you're using gcc to link. Cheers, - Alf thanks! I managed to install an egg with easy_install, seems to work, FINALLY! thanks for your help, Katrine -- http://mail.python.org/mailman/listinfo/python-list
Re: Replace various regex
Martin wrote: Hi, I am trying to come up with a more generic scheme to match and replace a series of regex, which look something like this... 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) 5.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) Ideally match the pattern to the right of the ! sign (e.g. lai), I would then like to be able to replace one or all of the corresponding numbers on the line. So far I have a rather unsatisfactory solution, any suggestions would be appreciated... The file read in is an ascii file. f = open(fname, 'r') s = f.read() if CANHT: s = re.sub(r\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ ! canht_ft, CANHT, s) where CANHT might be CANHT = '115.01,16.38,0.79,1.26,1.00 ! canht_ft' But this involves me passing the entire string. Thanks. Martin I remove all lines containing things like 9*0.0 in your file, cause I don't know what they mean and how to handle them. These are not numbers. import re replace = { 'snow_grnd' : (1, '99.99,'), # replace the 1st number by 99.99 't_soil' : (2, '88.8,'), # replace the 2nd number by 88.88 } testBuffer = 0.749, 0.743, 0.754, 0.759 ! stheta(1:sm_levels)(top to bottom) 0.46 ! snow_grnd 276.78,277.46,278.99,282.48 ! t_soil(1:sm_levels)(top to bottom) 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) 200.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) outputBuffer = '' for line in testBuffer.split('\n'): for key, (index, repl) in replace.items(): if key in line: parameters = { 'n' : '[\d\.]+', # given you example you have to change this one, I don't know what means 9*0.0 in your file 'index' : index - 1, } # the following pattern will silently match any digit before the indexth digit is found, and use a capturing parenthesis for the last pattern = '(\s*(?:(?:%(n)s)[,\s]+){0,%(index)s})(?:(%(n)s)[,\s]+)(.*!.*)' % parameters # regexp are sometimes a nightmare to read line = re.sub(pattern, r'\1 '+repl+r'\3' , line) break outputBuffer += line +'\n' print outputBuffer -- http://mail.python.org/mailman/listinfo/python-list
Talking with ebay using Python
Hi, I need a way of talking with the eBay API: Made search, Ask for item status. What can it be done, and where can I get the info. Thank you in advance for your help. -- http://mail.python.org/mailman/listinfo/python-list
CFP - SLE'2010
CALL FOR PAPERS 3rd International Conference on Software Language Engineering SLE 2010 12-13 Oct 2010 -- Eindhoven, The Netherlands http://planet-sl.org/sle2010/ Co-located with the International Conference on Generative Programming and Component Engineering (GPCE'10). DATES Jun 28: Abstract submission (required) Jul 05: Paper submission (midnight Apia Samoa time) Aug 27: Author notification Sep 17: Paper submission for online proceedings Oct 12-13: SLE 2010 Dec 03: Camera-ready copy submission for post-proceedings Software language engineering is devoted to topics related to artificial languages in software engineering. The foremost mission of the International Conference on Software Language Engineering (SLE) is to encourage and organize communication between communities that traditionally have looked at soft- ware languages from different, more specialized, and yet complementary perspectives. Thus, technologies, methods, experiments and case studies from modelware, grammarware, and ontologyware serving the objectives of software languages are of particular relevance to SLE. We invite high-quality submissions to all conference tracks. Submissions must be PDF files following the Springer LNCS style and will be managed using the EasyChair submission system. Please check the conference web site for further information. New at SLE 2010 is a Doctoral Symposium that will provide a supportive yet questioning setting in which PhD students can present their work, including goals, methods, and preliminary results. The Symposium aims to provide students with useful guidance and feedback on various aspects of their research from established researchers and the other student attendees. Please forward this call to anyone who might be interested. http://planet-sl.org/sle2010/ PAPER SUBMISSION Submitted papers must be original work and must not be previously published in, currently submitted to, or currently in consideration for any journal, book, conference, or workshop. Each submitted paper will be reviewed closely by at least three members of the program committee. Accepted papers will be distributed at the conference via the online proceedings as well as published in the post-proceedings, which will appear in the Springer Lecture Notes in Computer Science (LNCS) series. Authors will have the opportunity to revise their accepted paper(s) for the pre- and post- proceedings. For an accepted paper to appear in the proceedings, at least one author must attend the event and present the work. RESEARCH PAPERS Research papers should report a substantial research contribution to SLE and/or a successful application of SLE techniques. We solicit high-quality contributions in the area of SLE ranging from theoretical and conceptual contributions to tools, techniques, and frameworks that support the aforementioned lifecycle activities. We list examples of tools, techniques, applications, and problems of interest to clarify the types of contributes that we seek: * Formalisms used in designing and specifying languages and tools that analyze such language descriptions * Language implementation techniques * Program and model transformation tools * Composition, integration, and mapping tools for managing different aspects of software languages or different manifestations of a given language * Transformations and transformation languages between languages and models * Language evolution * Approaches to elicitation, specification, or verification of requirements for software languages * Language development frameworks, methodologies, techniques, best practices, and tools for the broader language lifecycle covering phases such as analysis, testing , and documentation. * Design challenges in SLE * Applications of languages including innovative domain-specific languages or little languages The preceding list is not exclusive or exhaustive. Visit the conference web site for more information about the scope and topics of interest of SLE, or contact the program co-chairs with questions. Page limit: 20 SHORT PAPERS Short paper may describe interesting or thought-provoking concepts that are not yet fully developed or evaluated, make an initial contribution to challenging research issues in SLE, or discuss and analyze controversial issues in the field. Page limit: 10 TOOL DEMONSTRATION PAPERS Because of SLE's ample interest in tools, we seek papers that present software tools related to the field of SLE. These papers will accompany a tool demonstration to be given at the conference. The selection criteria include the originality of the tool, its innovative aspects,
Re: Replace various regex
On Feb 15, 2:03 pm, Jean-Michel Pichavant jeanmic...@sequans.com wrote: Martin wrote: Hi, I am trying to come up with a more generic scheme to match and replace a series of regex, which look something like this... 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) 5.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) Ideally match the pattern to the right of the ! sign (e.g. lai), I would then like to be able to replace one or all of the corresponding numbers on the line. So far I have a rather unsatisfactory solution, any suggestions would be appreciated... The file read in is an ascii file. f = open(fname, 'r') s = f.read() if CANHT: s = re.sub(r\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ ! canht_ft, CANHT, s) where CANHT might be CANHT = '115.01,16.38,0.79,1.26,1.00 ! canht_ft' But this involves me passing the entire string. Thanks. Martin I remove all lines containing things like 9*0.0 in your file, cause I don't know what they mean and how to handle them. These are not numbers. import re replace = { 'snow_grnd' : (1, '99.99,'), # replace the 1st number by 99.99 't_soil' : (2, '88.8,'), # replace the 2nd number by 88.88 } testBuffer = 0.749, 0.743, 0.754, 0.759 ! stheta(1:sm_levels)(top to bottom) 0.46 ! snow_grnd 276.78,277.46,278.99,282.48 ! t_soil(1:sm_levels)(top to bottom) 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) 200.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) outputBuffer = '' for line in testBuffer.split('\n'): for key, (index, repl) in replace.items(): if key in line: parameters = { 'n' : '[\d\.]+', # given you example you have to change this one, I don't know what means 9*0.0 in your file 'index' : index - 1, } # the following pattern will silently match any digit before the indexth digit is found, and use a capturing parenthesis for the last pattern = '(\s*(?:(?:%(n)s)[,\s]+){0,%(index)s})(?:(%(n)s)[,\s]+)(.*!.*)' % parameters # regexp are sometimes a nightmare to read line = re.sub(pattern, r'\1 '+repl+r'\3' , line) break outputBuffer += line +'\n' print outputBuffer Thanks I will take a look. I think perhaps I was having a very slow day when I posted and realised I could solve the original problem more efficiently and the problem wasn't perhaps as I first perceived. It is enough to match the tag to the right of the ! sign and use this to adjust what lies on the left of the ! sign. Currently I have this...if anyone thinks there is a neater solution I am happy to hear it. Many thanks. variable_tag = 'lai' variable = [200.0, 60.030, 0.060, 0.030, 0.030] # generate adjustment string variable = ,.join([%s % i for i in variable]) + ' ! ' + variable_tag # call func to adjust input file adjustStandardPftParams(variable, variable_tag, in_param_fname, out_param_fname) and the inside of this func looks like this def adjustStandardPftParams(self, variable, variable_tag, in_fname, out_fname): f = open(in_fname, 'r') of = open(out_fname, 'w') pattern_found = False while True: line = f.readline() if not line: break pattern = re.findall(r!\s++variable_tag, line) if pattern: print 'yes' print of, %s % variable pattern_found = True if pattern_found: pattern_found = False else: of.write(line) f.close() of.close() return -- http://mail.python.org/mailman/listinfo/python-list
Re: Replace various regex
Martin wrote: On Feb 15, 2:03 pm, Jean-Michel Pichavant jeanmic...@sequans.com wrote: Martin wrote: Hi, I am trying to come up with a more generic scheme to match and replace a series of regex, which look something like this... 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) 5.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) Ideally match the pattern to the right of the ! sign (e.g. lai), I would then like to be able to replace one or all of the corresponding numbers on the line. So far I have a rather unsatisfactory solution, any suggestions would be appreciated... The file read in is an ascii file. f = open(fname, 'r') s = f.read() if CANHT: s = re.sub(r\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ ! canht_ft, CANHT, s) where CANHT might be CANHT = '115.01,16.38,0.79,1.26,1.00 ! canht_ft' But this involves me passing the entire string. Thanks. Martin I remove all lines containing things like 9*0.0 in your file, cause I don't know what they mean and how to handle them. These are not numbers. import re replace = { 'snow_grnd' : (1, '99.99,'), # replace the 1st number by 99.99 't_soil' : (2, '88.8,'), # replace the 2nd number by 88.88 } testBuffer = 0.749, 0.743, 0.754, 0.759 ! stheta(1:sm_levels)(top to bottom) 0.46 ! snow_grnd 276.78,277.46,278.99,282.48 ! t_soil(1:sm_levels)(top to bottom) 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) 200.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) outputBuffer = '' for line in testBuffer.split('\n'): for key, (index, repl) in replace.items(): if key in line: parameters = { 'n' : '[\d\.]+', # given you example you have to change this one, I don't know what means 9*0.0 in your file 'index' : index - 1, } # the following pattern will silently match any digit before the indexth digit is found, and use a capturing parenthesis for the last pattern = '(\s*(?:(?:%(n)s)[,\s]+){0,%(index)s})(?:(%(n)s)[,\s]+)(.*!.*)' % parameters # regexp are sometimes a nightmare to read line = re.sub(pattern, r'\1 '+repl+r'\3' , line) break outputBuffer += line +'\n' print outputBuffer Thanks I will take a look. I think perhaps I was having a very slow day when I posted and realised I could solve the original problem more efficiently and the problem wasn't perhaps as I first perceived. It is enough to match the tag to the right of the ! sign and use this to adjust what lies on the left of the ! sign. Currently I have this...if anyone thinks there is a neater solution I am happy to hear it. Many thanks. variable_tag = 'lai' variable = [200.0, 60.030, 0.060, 0.030, 0.030] # generate adjustment string variable = ,.join([%s % i for i in variable]) + ' ! ' + variable_tag # call func to adjust input file adjustStandardPftParams(variable, variable_tag, in_param_fname, out_param_fname) and the inside of this func looks like this def adjustStandardPftParams(self, variable, variable_tag, in_fname, out_fname): f = open(in_fname, 'r') of = open(out_fname, 'w') pattern_found = False while True: line = f.readline() if not line: break pattern = re.findall(r!\s++variable_tag, line) if pattern: print 'yes' print of, %s % variable pattern_found = True if pattern_found: pattern_found = False else: of.write(line) f.close() of.close() return Are you sure a simple if variable_tag in line: # do some stuff is not enough ? People will usually prefer to write for line in open(in_fname, 'r') : instead of your ugly while loop ;-) JM -- http://mail.python.org/mailman/listinfo/python-list
constructor overwrite
hi ,i had a problem on constructor overwrite: i have something like: class obj: def __init__(self, x=100, y=None): if y is None: self.x=x else: self.y=y so i can call : objet = obj() # x=100 y=None or objet = obj(40) # x= 40 y=None but if i do : objet = obj('not cool') #x='not cool' y=None since x is not typed . i am waiting for a result: objet = obj('not cool') #x=100 y='not cool' as they do in C++ or java. is there a way to do it? thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: constructor overwrite
Mug exallion.l...@gmail.com writes: hi ,i had a problem on constructor overwrite: i have something like: class obj: def __init__(self, x=100, y=None): if y is None: self.x=x else: self.y=y so i can call : objet = obj() # x=100 y=None or objet = obj(40) # x= 40 y=None but if i do : objet = obj('not cool') #x='not cool' y=None since x is not typed . i am waiting for a result: objet = obj('not cool') #x=100 y='not cool' as they do in C++ or java. is there a way to do it? thanks Your problem doesn't seem very well defined (e.g. do you ever call obj with two arguments?), but as I understand it you can do this: def __init__(self, x=100): if isinstance(x, int): self.x, self.y = x, None else: self.x, self.y = None, x HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: constructor overwrite
Mug wrote: hi ,i had a problem on constructor overwrite: i have something like: class obj: def __init__(self, x=100, y=None): if y is None: self.x=x else: self.y=y so i can call : objet = obj() # x=100 y=None or objet = obj(40) # x= 40 y=None but if i do : objet = obj('not cool') #x='not cool' y=None since x is not typed . i am waiting for a result: objet = obj('not cool') #x=100 y='not cool' as they do in C++ or java. is there a way to do it? thanks You could check the type(s) of the argument(s) in your code, if you want, but Python does not support signature analysis, dynamic method dispatch or static typing. You can do object = obj(y='not cool') but I doubt this is what you want. Your post raises a couple of thier points. First, __init__ is *not* the constructor. By the time it is called creation of the new object is already complete, and __init__() (as its name suggests) merely initializes it. In Python 2's new-style classes, and in Python 3, construction is performed by the class's __new__() method. Secondly, it seems a little strange that you are happy to create different instances, in some of which self.y is not initialized and in others self.x is not initialized. You may have a good reason for doing this, I merely point it out as a potential cause of AttributeError exceptions. regards Steve regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: constructor overwrite
Mug a écrit : hi ,i had a problem on constructor overwrite: i have something like: class obj: def __init__(self, x=100, y=None): if y is None: self.x=x else: self.y=y With such an initializer, you'll have instances with an attribute 'y' and no attribute 'x', and instances with an attribute 'x' and no attribute 'y' : class Obj(object): ... def __init__(self, x=100, y=None): ... if y is None: self.x = x ... else: self.y = y ... objx = Obj() objx.x 100 objx.y Traceback (most recent call last): File console, line 1, in module AttributeError: 'Obj' object has no attribute 'y' objy = Obj(y='foo') objy.x Traceback (most recent call last): File console, line 1, in module AttributeError: 'Obj' object has no attribute 'x' objy.y 'foo' Are you *sure* this is what you want ? so i can call : objet = obj() # x=100 y=None or objet = obj(40) # x= 40 y=None but if i do : objet = obj('not cool') #x='not cool' y=None What else would you expect ??? since x is not typed . 'x' is a name, and names are indeed untyped. Now the object bound to name 'x' is actually typed. i am waiting for a result: objet = obj('not cool') #x=100 y='not cool' as they do in C++ or java. Python is neither C++ nor Java (nor Pascal nor Lisp nor yourfavoritelanguagehere FWIW), so trying to forcefit C++/Java idioms will at best lead you to pain and frustation. Just like trying to forcefit Python idioms in C++ or Java (or Pascal or Lisp etc). is there a way to do it? objet = obj(y='not cool') Now if you could explain the problem you're trying to solve instead of the solution you thought would solve it, we might eventually provide more help. -- http://mail.python.org/mailman/listinfo/python-list
how to structure a directory with many scripts and shared code
Hi, i wanted to ask how you guys structure your code. I mainly have scripts that automate small tasks. These scripts use a common set of utility code. My code is structured like this, for example: script_1.py script_2.py script_3.py tools\ |-__init__.py |-logutils.py |-mailutils.py I was thinking of putting code in seperate directories because the number of scripts grows fast and i want to keep it clean. Something like this: script_1.py tools\ |-__init__.py |-logutils.py |-mailutils.py database\ |-__init__.py |-script_2.py |-script_3.py However, when i make a subdirectory, for example database and put a script in there, how would i import logutils or mailtutils from within the database subdirectory? This fails: from tools.logutils import logger Thanks, Benedict -- http://mail.python.org/mailman/listinfo/python-list
Time out a regular expression in Python 2.6.4?
Is there any way to time out a regular expression in Python 2.6.4? Motiviation: Our application allows users to enter regular expressions as validation criteria. If a user enters a pathological regular expression, we would like to timeout the evaluation of this expression after a short period of time. Thank you, Malcolm -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing a command from within python using the subprocess module
Peter Otten wrote: import subprocess def convert(width=5, height=30, colors=['#abcdef', '#456789'], filename=tmp/image with space in its name.png): lookup = locals() assert all(\n not in str(s) for s in lookup.values()) subprocess.call(\ convert -size {width}x{height} gradient:{colors[0]}-{colors[1]} {filename}.format(**lookup).splitlines()) convert() Peter Thank you. It works. Now I need to understand why and am able to follow what you are doing part of the way: 1. Assign default values in the function definition. 2. Store the variables existing in the local namespace in the list lookup. 3. Assert that there are no newlines in the values in lookup converted to strings. (Why? Is this because of splitlines() later on?) 4. Assemble a string (array?) for the subprocess.call argument using the format string syntax (section 8.1.3 et seq. of the Python documentation for 2.6.4). Your example works with default option of shell=False for subprocess.call(). 5. I am unable to decipher how you got to format(**lookup).splitlines()) especially the **prefix part, although I guess that splitlines() is dishing out the arguments one by one from each line in the subprocess.call argument. Any correction of (1) to (4) and an explanation of (5) would be most appreciated. All in all, your code is a magnificent example of wrapping the function within python. This is the first time I have seen something like this. Thank you very much. Chandra -- http://mail.python.org/mailman/listinfo/python-list
Re: Time out a regular expression in Python 2.6.4?
pyt...@bdurham.com wrote: Is there any way to time out a regular expression in Python 2.6.4? Motiviation: Our application allows users to enter regular expressions as validation criteria. If a user enters a pathological regular expression, we would like to timeout the evaluation of this expression after a short period of time. Python itself does not contain any mechanism to terminate an operation if it takes too much time. One approach would be to run the regex in a subprocess, and apply process limits to terminate that subprocess if it ran too long. This group being what it is you are likely to receive other, better suggestions too. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing a command from within python using the subprocess module
On 2/15/2010 7:35 AM, R (Chandra) Chandrasekhar wrote: Dear Folks, I want to execute a command from within python using the subprocess module. Coming from a Perl background, I thought I could use variable interpolation in strings, but found that this is neither supported Yes, it is: see the use of string.Template below. ... nor the Python way. That right -- it isn't the Python way. Python offers two basic alternatives. Alf already presented the use of the new format() method of string objects. I think traditional Python string formatting might be what you want in this case: colors = ['#abcdef', '#456789'] format_string = convert -size 5x30 gradient:%s-%s /tmp/image.png cmd_string = format_string % tuple(colors) ... or better, by making *colors* a tuple instead of a list ... colors = ('#abcdef', '#456789') format_string = convert -size 5x30 gradient:%s-%s /tmp/image.png cmd_string = format_string % colors As Peter demonstrated, you need to use split() on *cmd_string* when you send the command to subprocess.call(). Now if you *really* miss using Perl, try this: c1, c2 = ('#abcdef', '#456789') template = string.Template( convert -size 5x30 gradient:$c1-$c2 /tmp/image.png) cmd_string = template.substitute(locals()) Not worth the trouble, IMHO. Refs: http://www.python.org/doc/2.5.2/lib/typesseq-strings.html http://www.python.org/doc/2.5.2/lib/node40.html HTH, John -- http://mail.python.org/mailman/listinfo/python-list
Loop problem while generating a new value with random.randint()
Greetings, I'm having problems with a little script that I'm trying to finish, I don't know if I'm in the right track but I know somebody is going to help me. The script: # Import modules random for function randint import random # Generating a constant. var = 65 # Generating a random number. ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum ## End of Script ### What I'm trying to do is to print the new value that ranum generates if the condition is not met. So far if you run the script it prints the same value over and over again, making in an infinite loop. What can I do in order to print out the new value generated every time the condition is not met? Thanks! Paulo Repreza -- http://mail.python.org/mailman/listinfo/python-list
Re: how to structure a directory with many scripts and shared code
Benedict Verheyen wrote: Hi, i wanted to ask how you guys structure your code. I mainly have scripts that automate small tasks. These scripts use a common set of utility code. My code is structured like this, for example: script_1.py script_2.py script_3.py tools\ |-__init__.py |-logutils.py |-mailutils.py I was thinking of putting code in seperate directories because the number of scripts grows fast and i want to keep it clean. Something like this: script_1.py tools\ |-__init__.py |-logutils.py |-mailutils.py database\ |-__init__.py |-script_2.py |-script_3.py However, when i make a subdirectory, for example database and put a script in there, how would i import logutils or mailtutils from within the database subdirectory? This fails: from tools.logutils import logger Thanks, Benedict Hi, 1/ Provide the output of the error so we can provide some useful help. 2/ Embed your structure into a package. Names like 'tools', 'lib', 'mail', 'log' are too generic and will clash with other badly designed module/package names. Choose a name for you package, and use it with absolute imports e.g : from myBeautifulPackage.tools.logutil import logger 3/ make sure your working copy of myBeautifulPackage is in your PYTHONPATH. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing a command from within python using the subprocess module
Peter Otten wrote: import subprocess def convert(width=5, height=30, colors=['#abcdef', '#456789'], filename=tmp/image with space in its name.png): lookup = locals() assert all(\n not in str(s) for s in lookup.values()) subprocess.call(\ convert -size {width}x{height} gradient:{colors[0]}-{colors[1]} {filename}.format(**lookup).splitlines()) convert() Peter One other question I forgot to ask is this why is there a terminal backslash in subprocess.call(\ Removing the backslash makes the function fail. I wonder why, because is supposed to allow multi-line strings. I am sorry if this sounds obtuse but that backslash baffles me. -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop problem while generating a new value with random.randint()
Paulo Repreza wrote: Greetings, I'm having problems with a little script that I'm trying to finish, I don't know if I'm in the right track but I know somebody is going to help me. The script: # Import modules random for function randint import random # Generating a constant. var = 65 # Generating a random number. ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum ## End of Script ### What I'm trying to do is to print the new value that ranum generates if the condition is not met. So far if you run the script it prints the same value over and over again, making in an infinite loop. What can I do in order to print out the new value generated every time the condition is not met? Thanks! Paulo Repreza in your script you generate the random number only once, no wonder it keep being the same value over over. # Initialize ranum with a random number ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum ranum = random.randint(1,100) # generate a new number, that's the missing line in your script JM -- http://mail.python.org/mailman/listinfo/python-list
get a field
Hi, I have this: pe= http://www.rtve.es/mediateca/videos/20100211/saber-comer---patatas-castellanas-costillas-11-02-10/691046.shtml I would like to extract this: 691046.shtml But is dynamically. Not always have the same lenght the string. Could you help me how could I do extract this in python? Many thanks and sorry for my English! -- http://mail.python.org/mailman/listinfo/python-list
Which mock library do you prefer?
Hello! I am newbie mastering test driven development. I can't clarify myself which mock library to use. There are number of them and which one do you prefer? Two libraries that attracted my attention are: * minimock * dingus As for me the latest one, dingus, is the easiest (see this screencast: http://vimeo.com/3949077 ), but it has very few downloads from pypi, so it scares me a little. Minimock has wider usage and community, but I have some troubles using it. Maybe I am wrong, but with minimock you always have to keep track the order of imports in your test modules. Well, may be I just don't understand fully how minimock works. What are your suggestions? -- http://mail.python.org/mailman/listinfo/python-list
Re: how to structure a directory with many scripts and shared code
Jean-Michel Pichavant wrote: snip Hi, 1/ Provide the output of the error so we can provide some useful help. 2/ Embed your structure into a package. Names like 'tools', 'lib', 'mail', 'log' are too generic and will clash with other badly designed module/package names. Choose a name for you package, and use it with absolute imports e.g : from myBeautifulPackage.tools.logutil import logger 3/ make sure your working copy of myBeautifulPackage is in your PYTHONPATH. JM Hi, the error i get is this: Traceback (most recent call last): File update_tax_responsible.py, line 18, in module from tools.logutils import logger ImportError: No module named tools.logutils As to your comments on putting everything in a package, that's the whole problem, it are all scripts where only a few of them are related and they all use one or more modules that are located in the tools directory. In that tools subdirectory i have a py file for typical file functions, time functions, and so on. For instance, the tools\fileutils.py file contains amongst others a function that calculates the size of a directory. If i understood correctly, your suggestion is to put the utility files in the tools subdirectory in a seperate package and put that package on the python path so it can be shared by all scripts. I should then further structure the scripts by putting scripts together. But since the functions in tools are really utility functions for common tasks, what would a meaningfule package name be? The scripts are really diverse for instance, one of the scripts will check the names of subdirectories made by employees, another will send mail if people forget to fill in certain fields in a database and so on. That's one of the reasons why i'm having a hard time structering it. Regards, Benedict -- http://mail.python.org/mailman/listinfo/python-list
Re: how to structure a directory with many scripts and shared code
On Feb 15, 2010, at 11:06 AM, Jean-Michel Pichavant wrote: 3/ make sure your working copy of myBeautifulPackage is in your PYTHONPATH. Or, make a simple setup.py which imports distribute or setuptools and use: # python setup.py develop which will put your code into the PYTHONPATH by virtue of a link inside your site-packages. First one I found googling for setup.py develop : https://www.siafoo.net/article/77 There's a typo in the code of section 1.2 (parenthesis at the end of import, wrong), but the basic idea is there. S -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing a command from within python using the subprocess module
R (Chandra) Chandrasekhar wrote: Peter Otten wrote: import subprocess def convert(width=5, height=30, colors=['#abcdef', '#456789'], filename=tmp/image with space in its name.png): lookup = locals() assert all(\n not in str(s) for s in lookup.values()) subprocess.call(\ convert -size {width}x{height} gradient:{colors[0]}-{colors[1]} {filename}.format(**lookup).splitlines()) convert() Peter One other question I forgot to ask is this why is there a terminal backslash in subprocess.call(\ Removing the backslash makes the function fail. I wonder why, because is supposed to allow multi-line strings. I am sorry if this sounds obtuse but that backslash baffles me. It does, but a leading newline would cause the splitlines() result to start with an empty word - the backslash just causes the interpreter to ignore the newline immediately following, rather than including it in the string literal's value. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop problem while generating a new value with random.randint()
Jean-Michel Pichavant a écrit : Paulo Repreza wrote: Greetings, I'm having problems with a little script that I'm trying to finish, I don't know if I'm in the right track but I know somebody is going to help me. (snip - problem already addressed by Jean-Michel...) while var != ranum: if var == ranum: Note that this branch will never be executed - the condition in the while statement make sure var != ranum in the while block. print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing a command from within python using the subprocess module
Alf P. Steinbach wrote: * R (Chandra) Chandrasekhar: width = 5 height = 30 colors = ['#abcdef]', '#456789'] filename = /tmp/image.png # I want to get the equivalent of variable interpolation in Perl # so that the command # # convert -size 5x30 gradient:#abcdef-#456789 /tmp/image.png # # is derived from the variables above Assuming that the extra right square bracket in 'colors' is a typo, here's one way: s = convert -size {w}x{h} gradient:{g1}-{g2} {f}.format( w = width, h = height, g1 = colors[0], g2 = colors[1], f = filename ) Cheers hth., - ALf Thanks, Alf. It works if I use it so: subprocess.call(s, shell=True) and I think that is because s is a single string assembled in almost the variable interpolation fashion of Perl. It does not work with the default shell=False argument, presumably because the arguments are not split into separate strings (something that was horrible to do by hand using the %s and %() syntax that I had tried out previously). I think that you and Peter have, between you, shown me two ways of using subprocess.call(): one with shell=True and the other with shell = False. Thanks. Chandra -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop problem while generating a new value with random.randint()
Jean-Michel Pichavant jeanmic...@sequans.com writes: Paulo Repreza wrote: Greetings, I'm having problems with a little script that I'm trying to finish, I don't know if I'm in the right track but I know somebody is going to help me. The script: # Import modules random for function randint import random # Generating a constant. var = 65 # Generating a random number. ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum ## End of Script ### What I'm trying to do is to print the new value that ranum generates if the condition is not met. So far if you run the script it prints the same value over and over again, making in an infinite loop. What can I do in order to print out the new value generated every time the condition is not met? Thanks! Paulo Repreza in your script you generate the random number only once, no wonder it keep being the same value over over. # Initialize ranum with a random number ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum ranum = random.randint(1,100) # generate a new number, that's the missing line in your script JM And a more idiomatic way of writing this in Python would be, I guess: import random var = 65 while True: ranum = random.randint(1, 100) if var == ranum: print var, 'equals to:', ranum break else: print var, 'does not equal to:', ranum (following up from a FU as I can't see the OP) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop problem while generating a new value with random.randint()
Paulo Repreza wrote: Greetings, I'm having problems with a little script that I'm trying to finish, I don't know if I'm in the right track but I know somebody is going to help me. The script: # Import modules random for function randint import random # Generating a constant. var = 65 # Generating a random number. ranum = random.randint(1,100) #Creating while loop. Stops until var == ranum while var != ranum: if var == ranum: The if condition can never be true, since if it were then the loop would have just terminated before starting this iteration! print var, 'equals to:', ranum else: print var, 'does not equal to:', ranum Shouldn't there be something here to set ranum to a new value? Otherwise the same value will be there the next time around ... ## End of Script ### What I'm trying to do is to print the new value that ranum generates if the condition is not met. So far if you run the script it prints the same value over and over again, making in an infinite loop. What can I do in order to print out the new value generated every time the condition is not met? Thanks! regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ d -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing a command from within python using the subprocess module
R (Chandra) Chandrasekhar wrote: Peter Otten wrote: import subprocess def convert(width=5, height=30, colors=['#abcdef', '#456789'], filename=tmp/image with space in its name.png): lookup = locals() assert all(\n not in str(s) for s in lookup.values()) subprocess.call(\ convert -size {width}x{height} gradient:{colors[0]}-{colors[1]} {filename}.format(**lookup).splitlines()) convert() Peter Thank you. It works. Now I need to understand why and am able to follow what you are doing part of the way: 1. Assign default values in the function definition. 2. Store the variables existing in the local namespace in the list lookup. 3. Assert that there are no newlines in the values in lookup converted to strings. (Why? Is this because of splitlines() later on?) Yes. 4. Assemble a string (array?) for the subprocess.call argument using the format string syntax (section 8.1.3 et seq. of the Python documentation for 2.6.4). Your example works with default option of shell=False for subprocess.call(). I wanted to pass the executable and its arguments as a list (a python list is called array in other languages) to avoid bothering with shell escapes. 5. I am unable to decipher how you got to format(**lookup).splitlines()) especially the **prefix part, although I guess that splitlines() is dishing out the arguments one by one from each line in the subprocess.call argument. Given some_dict = {key1: value1, key2: value2,...} f(**some_dict) is a shortcut for f(key1=value1, key2=value2, ...) with the additional twist that you do not have to know the key/value pairs in advance. I introduced splitlines to avoid calling the format method on every argument to convert, i. e. def convert2(width=5, height=30, colors=['#abcdef', '#456789'], filename=tmp/image with space in its name.png): subprocess.call([ convert, -size, {width}x{height}.format(width=width, height=height), gradient:{0}-{1}.format(*colors), filename]) I supected that it would look cleaner than the above, but now that I tried it I think convert2() is the better alternative. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: get a field
mierdatutis mi wrote: Hi, I have this: pe=http://www.rtve.es/mediateca/videos/20100211/saber-comer---patatas-castellanas-costillas-11-02-10/691046.shtml; I would like to extract this: 691046.shtml But is dynamically. Not always have the same lenght the string. Could you help me how could I do extract this in python? Many thanks and sorry for my English! In [2]: import os In [3]: os.path.basename(pe) Out[3]: '691046.shtml' JM -- http://mail.python.org/mailman/listinfo/python-list
Re: get a field
mierdatutis mi wrote: Hi, I have this: pe=http://www.rtve.es/mediateca/videos/20100211/saber-comer---patatas-castellanas-costillas-11-02-10/691046.shtml; I would like to extract this: 691046.shtml But is dynamically. Not always have the same lenght the string. Could you help me how could I do extract this in python? Many thanks and sorry for my English! s = http://server/path/to/file/file.shtml; s.rfind(/) # finds rightmost / 26 s[s.rfind(/)+1:] # substring starting after / 'file.shtml' regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Fighting with subprocess.Popen
On Sun, 14 Feb 2010 21:43:22 +0100, Christian Heimes wrote: Below is a test case that demonstrates this. Tests 1 2 concatenate the command and the argument, Tests 3 4 mimic the python docs and use the form Popen([mycmd, myarg], ...), which never seems to work. It doesn't work with shell=True because the shell is not able to interpret the list form. It's recommended that you don't use shell=True unless you need a shell. A more general rule is to use shell=True on Windows and shell=False on Unix (including MacOSX). On Windows, whether the command is a list or a string is entirely orthogonal to whether a shell is used. The underlying execution function (CreateProcess) always uses a string, so a list is always converted to a string, and the conversion is independent of the shell= setting. The difference between shell=False and shell=True is that the former passes the string directly to CreateProcess while the latter passes cmd.exe /c +string (cmd.exe is actually the value of %comspec% if it is set). The reason for using shell=True on Windows is that it allows the program to be a batch file (or other script), while shell=False only works if the program is a binary executable (.exe, .com). On Unix, passing the arguments as a list in conjunction with shell=True rarely makes sense. It results in the execution of [/bin/sh, -c] + args i.e. args[0] is the argument to -c while the remaining arguments are assigned to the shell variables $1, $2, etc. If you're using the shell, you usually you want to pass the entire command as a string, resulting in the execution of [/bin/sh, -c, command] -- http://mail.python.org/mailman/listinfo/python-list
Re: Which mock library do you prefer?
Lacrima wrote: I am newbie mastering test driven development. I can't clarify myself which mock library to use. There are number of them and which one do you prefer? Two libraries that attracted my attention are: * minimock * dingus As for me the latest one, dingus, is the easiest (see this screencast:http://vimeo.com/3949077 ), but it has very few downloads from pypi, so it scares me a little. Minimock has wider usage and community, but I have some troubles using it. Maybe I am wrong, but with minimock you always have to keep track the order of imports in your test modules. Well, may be I just don't understand fully how minimock works. What are your suggestions? I have used http://pypi.python.org/pypi/mock/0.6.0 . It mocks, and it has a mode that works one method at a time, and another mode that mocks a method before its owning object gets constructed. However, TDD is not about mocking, and on greenfield code you should only mock to recover from some external problem, such as: - a random number generator - the system clock - anything over the wire - over a TCP/IP socket - hardware, such as your graphics or sound Never mock to avoid hitting the database. Some TDD verbiage advises never hit the database. That is a mind-game to force you to decouple your code. Your objects should always have the option (via construction encapsulation) to run as stubs, with some of their behaviors turned off. And if you TDD low-level code that hits a database, a mock would only tell the test what it wants to hear. And if you TDD high-level code that manages business rules, database records make perfectly good behavioral fixtures to support those rules. -- Phlip http://c2.com/cgi/wiki?ZeekLand -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing a command from within python using the subprocess module
On Tue, 16 Feb 2010 00:11:36 +0800, R (Chandra) Chandrasekhar wrote: One other question I forgot to ask is this why is there a terminal backslash in subprocess.call(\ Removing the backslash makes the function fail. I wonder why, because is supposed to allow multi-line strings. I am sorry if this sounds obtuse but that backslash baffles me. The backslash causes the following newline to be skipped, so the c at the beginning of convert is the first character in the string. Without it, the newline would be the first character, and the .splitlines() would result in an empty string as the first element in the list. Example: = hello = world = .splitlines() ['', 'hello', 'world'] \ = hello = world = .splitlines() ['hello', 'world'] -- http://mail.python.org/mailman/listinfo/python-list
Re: Which mock library do you prefer?
On Feb 15, 8:15 am, Lacrima lacrima.ma...@gmail.com wrote: Hello! I am newbie mastering test driven development. I can't clarify myself which mock library to use. There are number of them and which one do you prefer? Two libraries that attracted my attention are: * minimock * dingus As for me the latest one, dingus, is the easiest (see this screencast:http://vimeo.com/3949077 ), but it has very few downloads from pypi, so it scares me a little. I've used dingus with success. I wouldn't let the lack of downloads be a concern; the funny name is probably scaring some people away, and of course there are other alternatives too. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Optimization
On Feb 15, 1:07 am, Steve Howell showel...@yahoo.com wrote: On Feb 14, 11:52 am, Mark Dickinson dicki...@gmail.com wrote: On Feb 14, 4:53 pm, mukesh tiwari mukeshtiwari.ii...@gmail.com wrote: Hello everyone. I am new to python and previously i did programming in c/c++.Could some one please help me to improve the run time for this python program as i don't have idea how to optimized this code.This code also seems to be more unpythonic so how to make it look like more pythonic . I am trying for this problem(https://www.spoj.pl/problems/ FACT1/). Thank you One other thing: in the 'brent' function, you're setting m to randrange(1, n). What's the purpose of this? It looks to me as though m controls the number of Pollard-Rho iterations that are clumped together at one time (before doing a gcd operation), and using a random number for this doesn't make a lot of sense to me. The randomness also makes the algorithm a little buggy. I tried this input: 37^5 41^5 Usually I get the right answer. Other times I get this: 37^5 41^3 1681^1 And occasionally it appears to get into an infinite loop, which definitely could be a cause of slowness. :) Thank you all for help. I will try to improve these short comings. -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop problem while generating a new value with random.randint()
Thanks for the help! Using while True helps alot! Paulo Repreza -- http://mail.python.org/mailman/listinfo/python-list
saving a TIFF
I'm trying to save an image created from two arrays (I'm using numpy and PIL): n=(b4a-b3a)/(b4a+b3a); ndvi = Image.fromarray(n) ndvi.save(F:\Fire_scar_mapping\ILS3\ndvi_test,TIFF) ...but I get the following error message: IOError: [Errno 22] invalid mode ('wb') or filename: 'F:\\Fire_scar_mapping\\ILS3\ndt' Could anybody tell me what I'm doing wrong? -- http://mail.python.org/mailman/listinfo/python-list
Re: saving a TIFF
On Mon, Feb 15, 2010 at 12:55 PM, fernando sedano fer...@gmail.com wrote: I'm trying to save an image created from two arrays (I'm using numpy and PIL): n=(b4a-b3a)/(b4a+b3a); ndvi = Image.fromarray(n) ndvi.save(F:\Fire_scar_mapping\ILS3\ndvi_test,TIFF) ...but I get the following error message: IOError: [Errno 22] invalid mode ('wb') or filename: 'F:\\Fire_scar_mapping\\ILS3\ndt' Could anybody tell me what I'm doing wrong? -- http://mail.python.org/mailman/listinfo/python-list -- print F:\Fire_scar_mapping\ILS3\ndvi_test,TIFF F:\Fire_scar_mapping\ILS3 dvi_test TIFF -- Notice anything wrong with that? Unlike the Windows command line, Python neither knows nor cares that the string you're entering is a file name. It sees the \n and converts it to a newline. Either double up your backslashes ( F:\\Fire_scar_mapping\\ILS3\\ndvi_test), use a raw string (rF:\Fire_scar_mapping\ILS3\ndvi_test) or use forward slashes (F:/Fire_scar_mapping/ILS3/ndvi_test) -- http://mail.python.org/mailman/listinfo/python-list
Re: get a field
Holden wrote: mierdatutis mi wrote: I have this: pe=http://www.rtve.es/mediateca/videos/20100211/saber-comer---patatas-castellanas-costillas-11-02-10/691046.shtml; I would like to extract this: 691046.shtml But is dynamically. Not always have the same lenght the string. s = http://server/path/to/file/file.shtml; s.rfind(/) # finds rightmost / 26 s[s.rfind(/)+1:] # substring starting after / 'file.shtml' If I didn't use os.path.basename(s) then I'd write this as s.rsplit('/', 1)[-1] http://server/path/to/file/file.shtml.rsplit('/', 1)[-1] 'file.shtml' .rsplit('/', 1)[-1] '' file.html.rsplit('/', 1)[-1] 'file.html' I don't know how much of a difference it makes, but I always appreciate seeing how various people solve the same problem. I tend to lean away from the find()/index() methods on strings because I have to stop and think which one raises the exception and which one returns -1 (and that it's -1 instead of 0) usually dropping to a python shell and doing help(.find) help(.index) to refresh my memory. FWIW, Steve's solution and the os.path.basename() both produce the same results with all 3 input values, so it's more a matter of personal style preference. The basename() version has slightly different results if you have Windows paths with backslashes: s = r'c:\path\to\file.txt' but since you (OP) know that they should be URLs with forward-slashes, it should be a non-issue. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Using class attributes
I have a base class Foo with a number of derived classes FooA, FooB, FooC, etc. Each of these derived classes needs to read (upon initialisation) text from an associated template file FooA.tmpl, FooB.tmpl, FooC.tmpl, etc. I can derive the template filename string for each instance by doing something like this in the base class (and then not forgetting to call super() in the __init__() of each derived class): class Foo(object): def __init__(self): self.template_filename = %s.tmpl % self.__class__.__name__ self.template_body = read_body_from(self.template_filename) But, since this information is the same for every instance of each derived class, I was wondering if there was a way to achieve the same thing outside of the __init__ function, and just have these assignments be done as a class attribute (i.e. so that I can refer to FooA.template_body, etc.) I can of course always just hardcode the template filenames in each derived class, but I am just curious if it can be automated through some form of introspection. -- Leo Breebaart l...@lspace.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Which mock library do you prefer?
On Feb 15, 6:57 pm, Phlip phlip2...@gmail.com wrote: Lacrima wrote: I am newbie mastering test driven development. I can't clarify myself which mock library to use. There are number of them and which one do you prefer? Two libraries that attracted my attention are: * minimock * dingus As for me the latest one, dingus, is the easiest (see this screencast:http://vimeo.com/3949077 ), but it has very few downloads from pypi, so it scares me a little. Minimock has wider usage and community, but I have some troubles using it. Maybe I am wrong, but with minimock you always have to keep track the order of imports in your test modules. Well, may be I just don't understand fully how minimock works. What are your suggestions? I have usedhttp://pypi.python.org/pypi/mock/0.6.0. It mocks, and it has a mode that works one method at a time, and another mode that mocks a method before its owning object gets constructed. However, TDD is not about mocking, and on greenfield code you should only mock to recover from some external problem, such as: - a random number generator - the system clock - anything over the wire - over a TCP/IP socket - hardware, such as your graphics or sound Never mock to avoid hitting the database. Some TDD verbiage advises never hit the database. That is a mind-game to force you to decouple your code. Your objects should always have the option (via construction encapsulation) to run as stubs, with some of their behaviors turned off. And if you TDD low-level code that hits a database, a mock would only tell the test what it wants to hear. And if you TDD high-level code that manages business rules, database records make perfectly good behavioral fixtures to support those rules. -- Phlip http://c2.com/cgi/wiki?ZeekLand Hi, Phlip! Thanks for your reply! Isn't what you are talking about integration tests? And unit tests should be fully isolated? So even for method 'some_method()' of class A I should mock instance of class A (i.e. to mock 'self') to test 'some_method()'. Please, could you explain in more detail your thoughts: Your objects should always have the option (via construction encapsulation) to run as stubs, with some of their behaviors turned off. And if you TDD low-level code that hits a database, a mock would only tell the test what it wants to hear. And if you TDD high-level code that manages business rules, database records make perfectly good behavioral fixtures to support those rules. And could you give an example. For me it's really hard to develop test first. Often I don't know what tests to write to replace hardcoded return values by objects that perform actual work. I have read several books on TDD and explored http://c2.com/cgi/wiki?TestDrivenDevelopment and related wikis, but often it seems I don't have enough understanding to write even simple application. And sorry for my English. with regards, Max. -- http://mail.python.org/mailman/listinfo/python-list
Plugin architecture
Hello everyone, I'm thinking about writing plugin-style architecture for (a new) web-based app (with SQLAlchemy as backend). Say, there's core web app and someone decides to write plugin Accounting for this web app that would work with SQA objects of the core app or other plugins. I found this: http://effbot.org/zone/metaclass-plugins.htm (Although I'm not entirely sure this is the best approach for the web app) How would you approach designing such architecture using features available in Python (and some major web framework, like Pylons or Django)? Regards, mk -- http://mail.python.org/mailman/listinfo/python-list
Re: Plugin architecture
I'm thinking about writing plugin-style architecture for (a new) web-based app (with SQLAlchemy as backend). Say, there's core web app and someone decides to write plugin Accounting for this web app that would work with SQA objects of the core app or other plugins. I found this: http://effbot.org/zone/metaclass-plugins.htm (Although I'm not entirely sure this is the best approach for the web app) How would you approach designing such architecture using features available in Python (and some major web framework, like Pylons or Django)? Major web frameworks like django, turbogears (thicker ones), pylons, cherrypy (thinner ones) include all the functionality for doing this sort of thing out of the box. If I were you I would go ahead and use one of these frameworks and only start thinking about designing something new if I hit a wall. Chances are you won't. Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
TypeError Exception in email lib
File /usr/lib/python2.5/email/_parseaddr.py, line 142, in mktime_tz if data[9] is None: TypeError: 'NoneType' object is unsubscriptable I'm parsing a bunch of spam and using the date field from the spams for a date-time stamp. I've fixed the lib on my box to place the call inside a try/except clause to catch the exception now, but it seems the module has a bug in it. How would I go about recommending a change the module? Original code: def mktime_tz(data): Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp. if data[9] is None: # No zone info, so localtime is better assumption than GMT return time.mktime(data[:8] + (-1,)) else: t = time.mktime(data[:8] + (0,)) return t - data[9] - time.timezone Patched code: def mktime_tz(data): Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp. try: if data[9] is None: # No zone info, so localtime is better assumption than GMT return time.mktime(data[:8] + (-1,)) else: t = time.mktime(data[:8] + (0,)) return t - data[9] - time.timezone except TypeError: return time.mktime(data[:8] + (-1,)) Auburn fans are like slinkys... not really good for anything but they still bring a smile to your face when you push them down a flight of stairs. To argue that honorable conduct is only required against an honorable enemy degrades the Americans who must carry out the orders. -- Charles Krulak, Former Commandant of the Marine Corps We are all slave to our own paradigm. -- Joshua Williams If the letters PhD appear after a person's name, that person will remain outdoors even after it's started raining. -- Jeff Kay -- http://mail.python.org/mailman/listinfo/python-list
Re: get a field
Tim Chase wrote: Holden wrote: mierdatutis mi wrote: I have this: pe=http://www.rtve.es/mediateca/videos/20100211/saber-comer---patatas-castellanas-costillas-11-02-10/691046.shtml; I would like to extract this: 691046.shtml But is dynamically. Not always have the same lenght the string. s = http://server/path/to/file/file.shtml; s.rfind(/) # finds rightmost / 26 s[s.rfind(/)+1:] # substring starting after / 'file.shtml' If I didn't use os.path.basename(s) then I'd write this as s.rsplit('/', 1)[-1] http://server/path/to/file/file.shtml.rsplit('/', 1)[-1] 'file.shtml' .rsplit('/', 1)[-1] '' file.html.rsplit('/', 1)[-1] 'file.html' I don't know how much of a difference it makes, but I always appreciate seeing how various people solve the same problem. I tend to lean away from the find()/index() methods on strings because I have to stop and think which one raises the exception and which one returns -1 (and that it's -1 instead of 0) usually dropping to a python shell and doing help(.find) help(.index) to refresh my memory. FWIW, Steve's solution and the os.path.basename() both produce the same results with all 3 input values, so it's more a matter of personal style preference. The basename() version has slightly different results if you have Windows paths with backslashes: s = r'c:\path\to\file.txt' but since you (OP) know that they should be URLs with forward-slashes, it should be a non-issue. The MacOS separator was (is?) :. Since MacOS X it has been based on Unix, so I'm not sure what Python sees nowadays, / or still :. Another platform, RISC OS, uses .. For that reason I wouldn't use os.path.basename(). An alternative to .rsplit() is .rpartition(). -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
Alf said (2/13/2010 8:34 PM): Names in Python refer to objects. Those references can be copied via assignment. That's (almost) all. And it provides a very short and neat way to describe pass by sharing. Alf also said (2/13/2010 8:43 PM): * Steve Howell: This thread is interesting on many levels. What is the core question that is being examined here? I think that regarding the technical it is whether a Python name refers to an object or not. I maintain that it does, and that the reference can be copied, and that the semantics of the language requires this and is defined in terms of this. Steve Holden, D'Aprano and many others maintain that there are no references, or that if there are then they're only an implementation aspect, i.e. that conceiveable one could have an implementation without them. Steve D'Aprano said (2/14/2010 12:13 AM): That's not to say that the general concept of references (as in to refer to) isn't valuable when discussing Python. If you want to say that (e.g.) following x = 1 the name x refers to (or even points to!) the object 1, my objections will be mild or non-existent. In that sense, it's probably impossible to program without some sort of references: the computer manipulates variables or objects directly, while we manipulate characters in source code. The only way to write a program is to use some abstract thing (a name, an offset, whatever) that refers, in some fashion, to a collection of bits in the computer's memory. But to go from that to the idea that (say) x is a pointer does so much violence to the concept of pointer and has so much room for confusion that it is actively harmful. I think most of the technical argument in this thread comes down to the various forms of the word *refer*. After execution of this Python statement: x = 5 ... we can use this English-language statement: the name *x* refers to the value *5* (If anyone has more than Steve's mild objection, please speak up!) The English-language statement uses the VERB refers. It is not the same as using the NOUN reference: *x* is a reference to the value *5* Why not? Because using the NOUN form suggests that you're talking about a particular kind of object in the world Python, as in these statements: *x* is a string *x* is a function *x* is a class But Python does not define *reference* objects -- by which I mean that executing *import types; dir(types)* produces a long list of object-type names, and there's nothing like a reference on that list. (I suspect there's a better way to make this there are no reference objects argument. Can anyone help?) Amending the statement to: the name *x* is a reference to the value *5* ... doesn't really help matters. Is *x* a NAME or is it a REFERENCE? Looking again at Alf's assertion: [the core technical question is] whether a Python name refers to an object or not. I maintain that it does, and that the reference can be copied, ... and Alf's example (message dated 2/10/2010 5:02 PM): For example, x = s[0] accesses the object that s points (refers) to. I believe Alf would characterize this assignment statement as a situation in which a reference is copied [using the NOUN form of refer]. But no object is copied during execution of this statement. Moreover, saying a reference is copied might mislead a Python newbie into thinking that some kind of reference object exists that can be copied by Python statements. So it's better to describe the situation using the VERB form of refer: assigns the name *x* to the object that *s[0]* refers to -John -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError Exception in email lib
On Mon, Feb 15, 2010 at 10:53 AM, BJ Swope bigbluesw...@gmail.com wrote: File /usr/lib/python2.5/email/_parseaddr.py, line 142, in mktime_tz if data[9] is None: TypeError: 'NoneType' object is unsubscriptable I'm parsing a bunch of spam and using the date field from the spams for a date-time stamp. I've fixed the lib on my box to place the call inside a try/except clause to catch the exception now, but it seems the module has a bug in it. While there may or may not be a bug in the library, I don't think its where you're fixing. Just because an exception occurs in a function doesn't mean that function is broken: its documented as accepting a 10 item tuple, only. Functions in the stdlib generally -should- throw exceptions on invalid input. Someone's passing None into it, which its not allowed to do. So -that's- where the bug probably is, I think. (Although it may not be the immediate of mktime_tz; it could be happening higher up on the stack) Advice: Always post complete tracebacks to c.p.l/python-list :) --S -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError Exception in email lib
On Mon, Feb 15, 2010 at 2:31 PM, Stephen Hansen apt.shan...@gmail.com wrote: On Mon, Feb 15, 2010 at 10:53 AM, BJ Swope bigbluesw...@gmail.com wrote: File /usr/lib/python2.5/email/_parseaddr.py, line 142, in mktime_tz if data[9] is None: TypeError: 'NoneType' object is unsubscriptable I'm parsing a bunch of spam and using the date field from the spams for a date-time stamp. I've fixed the lib on my box to place the call inside a try/except clause to catch the exception now, but it seems the module has a bug in it. While there may or may not be a bug in the library, I don't think its where you're fixing. Just because an exception occurs in a function doesn't mean that function is broken: its documented as accepting a 10 item tuple, only. Functions in the stdlib generally -should- throw exceptions on invalid input. Someone's passing None into it, which its not allowed to do. So -that's- where the bug probably is, I think. (Although it may not be the immediate of mktime_tz; it could be happening higher up on the stack) Advice: Always post complete tracebacks to c.p.l/python-list :) --S From the module: def mktime_tz(data): Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp. if data[9] is None: # No zone info, so localtime is better assumption than GMT return time.mktime(data[:8] + (-1,)) else: t = time.mktime(data[:8] + (0,)) return t - data[9] - time.timezone It appears that the module is trying to accommodate the potential missing TZ data because poorly written emails are missing the TZ data. I discarded all the crontab emails that had the full traceback in them. I took out the try/except clause in the hopes that I'll get another exception soon. If I do I'll post the entire exception traceback. -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError Exception in email lib
BJ Swope wrote: File /usr/lib/python2.5/email/_parseaddr.py, line 142, in mktime_tz if data[9] is None: TypeError: 'NoneType' object is unsubscriptable I'm parsing a bunch of spam and using the date field from the spams for a date-time stamp. I've fixed the lib on my box to place the call inside a try/except clause to catch the exception now, but it seems the module has a bug in it. How would I go about recommending a change the module? Original code: def mktime_tz(data): Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp. if data[9] is None: # No zone info, so localtime is better assumption than GMT return time.mktime(data[:8] + (-1,)) else: t = time.mktime(data[:8] + (0,)) return t - data[9] - time.timezone Patched code: def mktime_tz(data): Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp. try: if data[9] is None: # No zone info, so localtime is better assumption than GMT return time.mktime(data[:8] + (-1,)) else: t = time.mktime(data[:8] + (0,)) return t - data[9] - time.timezone except TypeError: return time.mktime(data[:8] + (-1,)) Auburn fans are like slinkys... not really good for anything but they still bring a smile to your face when you push them down a flight of stairs. To argue that honorable conduct is only required against an honorable enemy degrades the Americans who must carry out the orders. -- Charles Krulak, Former Commandant of the Marine Corps We are all slave to our own paradigm. -- Joshua Williams If the letters PhD appear after a person's name, that person will remain outdoors even after it's started raining. -- Jeff Kay Is the code that calls mktime_tz() your own? If so, maybe you'd be better off fixing that. The error message indicates that the 'data' parameter is None, which is invalid, according to the 2.6 documentation anyway (the parameter to the function is supposed to be a 10-tuple). So it's a bug in the caller, not in that function. Besides, your fix will still raise another exception inside the except class, since if data[9] gives that error, so will data[:8} DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Using class attributes
On Mon, Feb 15, 2010 at 10:29 AM, Leo Breebaart l...@lspace.org wrote: I have a base class Foo with a number of derived classes FooA, FooB, FooC, etc. Each of these derived classes needs to read (upon initialisation) text from an associated template file FooA.tmpl, FooB.tmpl, FooC.tmpl, etc. I can derive the template filename string for each instance by doing something like this in the base class (and then not forgetting to call super() in the __init__() of each derived class): class Foo(object): def __init__(self): self.template_filename = %s.tmpl % self.__class__.__name__ self.template_body = read_body_from(self.template_filename) But, since this information is the same for every instance of each derived class, I was wondering if there was a way to achieve the same thing outside of the __init__ function, and just have these assignments be done as a class attribute (i.e. so that I can refer to FooA.template_body, etc.) I can of course always just hardcode the template filenames in each derived class, but I am just curious if it can be automated through some form of introspection. Metaclasses to the rescue!: class WithTemplateAttrs(type): def __new__(cls, name, bases, dct): klass = type.__new__(cls, name, bases, dct) klass.template_filename = %s.tmpl % name klass.template_body = read_body_from(klass.template_filename) return klass class Foo(object): __metaclass__ = WithTemplateAttrs #rest of class body here Now just have FooA, FooB, etc. subclass Foo as before. They'll automatically get the attributes generated. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError Exception in email lib
On Mon, Feb 15, 2010 at 2:47 PM, BJ Swope bigbluesw...@gmail.com wrote: On Mon, Feb 15, 2010 at 2:31 PM, Stephen Hansen apt.shan...@gmail.com wrote: On Mon, Feb 15, 2010 at 10:53 AM, BJ Swope bigbluesw...@gmail.com wrote: File /usr/lib/python2.5/email/_parseaddr.py, line 142, in mktime_tz if data[9] is None: TypeError: 'NoneType' object is unsubscriptable I'm parsing a bunch of spam and using the date field from the spams for a date-time stamp. I've fixed the lib on my box to place the call inside a try/except clause to catch the exception now, but it seems the module has a bug in it. While there may or may not be a bug in the library, I don't think its where you're fixing. Just because an exception occurs in a function doesn't mean that function is broken: its documented as accepting a 10 item tuple, only. Functions in the stdlib generally -should- throw exceptions on invalid input. Someone's passing None into it, which its not allowed to do. So -that's- where the bug probably is, I think. (Although it may not be the immediate of mktime_tz; it could be happening higher up on the stack) Advice: Always post complete tracebacks to c.p.l/python-list :) --S From the module: def mktime_tz(data): Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp. if data[9] is None: # No zone info, so localtime is better assumption than GMT return time.mktime(data[:8] + (-1,)) else: t = time.mktime(data[:8] + (0,)) return t - data[9] - time.timezone It appears that the module is trying to accommodate the potential missing TZ data because poorly written emails are missing the TZ data. I discarded all the crontab emails that had the full traceback in them. I took out the try/except clause in the hopes that I'll get another exception soon. If I do I'll post the entire exception traceback. Speak of the devil and demons appear... /logs/python/imap_fetcher/spam_serv1.bigbluenetworks.com.py Parsing of emails for spam at serv1.bigbluenetworks.com failed. Traceback (most recent call last): File /logs/python/imap_fetcher/spam_serv1.bigbluenetworks.com.py, line 81, in module clean_stale_mail() File /logs/python/imap_fetcher/spam_serv1.bigbluenetworks.com.py, line 24, in clean_stale_mail utc_msg_date = email.utils.mktime_tz(msg_date2) File /usr/lib/python2.5/email/_parseaddr.py, line 142, in mktime_tz if data[9] is None: TypeError: 'NoneType' object is unsubscriptable def clean_stale_mail(): msg_date1= the_email.get('Date') msg_date2 = email.utils.parsedate_tz(msg_date1) try: utc_msg_date = email.utils.mktime_tz(msg_date2) except OverflowError: M.store(msg_id, '+FLAGS.SILENT', '\\Deleted') return utc_stale_date = time.time() - (86000*stale_days) if utc_msg_date = utc_stale_date: M.store(msg_id, '+FLAGS.SILENT', '\\Deleted') try: #M = imaplib.IMAP4(HOST) M = imaplib.IMAP4_SSL(HOST) M.login(USER, PASSWD) M.select(MAILBOX) response, msg_list = M.search(None, 'ALL') # response is IMAP response, msg_list is list of Message IDs for msg_id in msg_list[0].split(): #msg_list[0] is a space separated string of message ids response, message = M.fetch(msg_id, '(RFC822)') # response is the IMAP response, message is an RFC222 message msg = email.FeedParser.FeedParser( ) msg.feed(message[0][1]) the_email = msg.close() clean_stale_mail() if the_email.is_multipart(): for part in the_email.walk(): if part.get_content_type() == 'text/html': decoded_part = part.get_payload(decode=1) soup_parse(decoded_part) elif the_email.get_content_type() == 'text/html': decoded_part = the_email.get_payload(decode=1) soup_parse(decoded_part) elif the_email.get_content_type() == 'text/plain': msg_payload = the_email.get_payload() manual_parse(msg_payload) else: continue #print msg_id, Did't match any defined content types... #print the_email M.expunge() M.close() M.logout() -- http://mail.python.org/mailman/listinfo/python-list
Re: Which mock library do you prefer?
Lacrima wrote: Thanks for your reply! Isn't what you are talking about integration tests? And unit tests should be fully isolated? So even for method 'some_method()' of class A I should mock instance of class A (i.e. to mock 'self') to test 'some_method()'. Unit test is a high-end QA concept. Developers can get the best return on developer tests. They don't bother with aerospace-quality isolation between units. If a TDD test needs to pull in a bunch of modules to pass, that's generally a good thing, because they all get indirect testing. If they catch a bug, their local tests might not catch it, but the higher level tests still have a chance. (And if your product still needs unit tests, TDD will make them very easy for a formal QA team to add.) However, expensive setup is a design smell. That means if a test case requires too many lines of code for its Assemble phase (before its Activate and Assert phases), then maybe those lines of code support objects that are too coupled, and they need a better design. Throwing mocks at these objects, instead of decoupling them, will perfume the design smell, instead of curing it. construction encapsulation) And could you give an example. def test_frob(self): frob = Frob() frob.knob = Mock() frob.knob.value = Mock(return_value = 42) assert 42 == frob.method_using_knob() We need the mock because we can't control how Frob's constructor built its knob. So instead, give Frob the option to construct with a Knob: def test_frob(self): knob = Knob(42) frob = Frob(knob) assert frob.method_using_knob() Note that in production the Knob constructor never takes a Knob. Maybe we should upgrade the production code too (!), or maybe Knob's constructor should only create a knob if it didn't get passed one. Either technique is acceptable, because the resulting code decouples Frobs and Knobs just a little bit more. For me it's really hard to develop test first. Often I don't know what tests to write to replace hardcoded return values by objects that perform actual work. You have read too many books on TDD. C-: Alternate between writing lines of test and lines of code. Run the tests after the fewest possible edits, and always correctly predict if the tests will pass, or will fail, and with what diagnostic. (And configure your editor to run the stankin tests, no matter how hard it fights you!) The high-end tricks will get easier after you get the basic cycle down. -- Phlip http://c2.com/cgi/wiki?ZeekLand -- http://mail.python.org/mailman/listinfo/python-list
Re: Talking with ebay using Python
On Mon, Feb 15, 2010 at 6:10 AM, starglider develop starglider@gmail.com wrote: Hi, I need a way of talking with the eBay API: Made search, Ask for item status. What can it be done, and where can I get the info. In the future, search Google and PyPI (http://pypi.python.org/pypi). (PyPI is like CPAN for Python.) Here's what I came up with: pyeBay: http://ebaydeveloper.typepad.com/pyebay.html easyBay: http://pypi.python.org/pypi/easyBay/0.1dev They both haven't been updated in a while are are probably unmaintained. I have never used either of these; I have no idea whether they still work. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Talking with ebay using Python
On 15 February 2010 20:04, Chris Rebert c...@rebertia.com wrote: On Mon, Feb 15, 2010 at 6:10 AM, starglider develop starglider@gmail.com wrote: Hi, I need a way of talking with the eBay API: Made search, Ask for item status. What can it be done, and where can I get the info. In the future, search Google and PyPI (http://pypi.python.org/pypi). (PyPI is like CPAN for Python.) Here's what I came up with: pyeBay: http://ebaydeveloper.typepad.com/pyebay.html easyBay: http://pypi.python.org/pypi/easyBay/0.1dev They both haven't been updated in a while are are probably unmaintained. I have never used either of these; I have no idea whether they still work. On Mon, Feb 15, 2010 at 12:12 PM, starglider develop starglider@gmail.com wrote: Thanks for your replay Chris, but easyBay is no longer available, I had contact the mantainer without result. pyebay is new to me but lacks documentations and the output is in XML unlike easybay the creats a class. Er, easyBay is available. Click one of the file links on its PyPI entry, they work just fine. And in the future, mention the results of any search (i.e. the fact you tried contacting the easyBay dev) in your first message. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
error: Setup script exited with error: command 'gcc' failed with exit status 1
Hi, When i'm trying to install some modules for python i'm getting the fallowing error.. error: Setup script exited with error: command 'gcc' failed with exit status 1 can any one know what it exactly means.. if any body know please reply. i have gcc version 4.1.2 20080704 (Red Hat 4.1.2-46). i'm using python python2.6, some of the modules getting this type of errors are.. mysqldb, and pytc., etc.. if any one know please help me.. Thanks and Regards.. chiranjeevi.muttoju -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError Exception in email lib
BJ Swope wrote: [snip] Speak of the devil and demons appear... /logs/python/imap_fetcher/spam_serv1.bigbluenetworks.com.py Parsing of emails for spam at serv1.bigbluenetworks.com failed. Traceback (most recent call last): File /logs/python/imap_fetcher/spam_serv1.bigbluenetworks.com.py, line 81, in module clean_stale_mail() File /logs/python/imap_fetcher/spam_serv1.bigbluenetworks.com.py, line 24, in clean_stale_mail utc_msg_date = email.utils.mktime_tz(msg_date2) File /usr/lib/python2.5/email/_parseaddr.py, line 142, in mktime_tz if data[9] is None: TypeError: 'NoneType' object is unsubscriptable def clean_stale_mail(): msg_date1= the_email.get('Date') What is the value of 'msg_date1' at this point? msg_date2 = email.utils.parsedate_tz(msg_date1) What is the value of 'msg_date2' at this point? The docs say that parsedate_tz() can return a 10-tuple or None. Presumably, if it can't parse the date then it returns None. try: utc_msg_date = email.utils.mktime_tz(msg_date2) except OverflowError: M.store(msg_id, '+FLAGS.SILENT', '\\Deleted') return utc_stale_date = time.time() - (86000*stale_days) if utc_msg_date = utc_stale_date: M.store(msg_id, '+FLAGS.SILENT', '\\Deleted') [snip] -- http://mail.python.org/mailman/listinfo/python-list
Re: error: Setup script exited with error: command 'gcc' failed with exit status 1
On Feb 15, 2010, at 3:33 PM, chiranjeevi muttoju wrote: Hi, When i'm trying to install some modules for python i'm getting the fallowing error.. error: Setup script exited with error: command 'gcc' failed with exit status 1 can any one know what it exactly means.. if any body know please reply. i have gcc version 4.1.2 20080704 (Red Hat 4.1.2-46). i'm using python python2.6, some of the modules getting this type of errors are.. mysqldb, and pytc., etc.. Hi Chiranjeevi, Show us all of the output, please -- everything from where you typed 'setup.py' until the script died. -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError Exception in email lib
On Feb 15, 2:54 pm, BJ Swope bigbluesw...@gmail.com wrote: def clean_stale_mail(): msg_date1= the_email.get('Date') msg_date2 = email.utils.parsedate_tz(msg_date1) try: utc_msg_date = email.utils.mktime_tz(msg_date2) except OverflowError: M.store(msg_id, '+FLAGS.SILENT', '\\Deleted') return utc_stale_date = time.time() - (86000*stale_days) if utc_msg_date = utc_stale_date: M.store(msg_id, '+FLAGS.SILENT', '\\Deleted') It looks like msg_date1 is None. That is, 'Date' is not found in the_email. As Dave Angel said, your patch shouldn't fix this problem anyway. Use your patch and put the same problematic input in it to see if it really does fix the problem. (Just to satisfy our morbid curiosity, not really to help you get a true solution.) It looks to me like you have to do something to make clean_stale_mail more robust, rather than focusing on anything in the standard library. John -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
John Posner jjpos...@optimum.net writes: [...] x = s[0] [...] assigns the name *x* to the object that *s[0]* refers to s[0] does not refer to an object, it *is* an object (once evaluated of course, otherwise it's just a Python expression). -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Few Small Questions Regarding CGI
Dear Group, I am trying to learn CGI. I was checking Python Docs. There are multiple modules. Which one to start with? Is there any other material or URL for step by step learning of CGI. My next question is: I want to test it. I have a small personal computer. I have internet but is connected by a service provider. I do not have personal web page. Can I use my personal computer as a client as well as a server? If your kind time permits to answer me these questions, I would be hugely benefited. Wishing you a happy day ahead, Best Regards, Subhabrata. -- http://mail.python.org/mailman/listinfo/python-list
Re: plugin / intra process communication system
On Sun, 2010-02-14 at 18:47 +0100, Diez B. Roggisch wrote: Here there problem with the trac (and other plugin systems I've seen) approach: You need to define something like: | | class IAuthPlugin(Interface): [...] | in your blog software. Why? Any reason you can't define it in a separate package the blog-software depends on, as well as your wiki? That's actually my point - most plugin systems I've seen, like the one trac uses, are not encouraging you to do so. Having a module that just defines an Interface is kind of weird - and in the real world no one is doing it. And then of course, this is not really needed. In Python, behavior counts, not type-information. So you can get away without any explicit declared interface. You might chose to not do that, for aestetic reasons, or better documentation. But you aren't forced. Actually some plugin-systems in python do force you and they check if your implementation comply with the interface. Here is one solution I came up with. Based on the earlier example: Lets say you program a wiki and like to allow different kind of authentications. So you create two plugins (for example one for OpenID and one for Shibboleth). Some time later you feel like reinventing the wheel again and you program a blog. Again you like to allow different ways of authentication and you already wrote plugins for exactly the same for your wiki, right? auth_openid.py - providing the authentication service http://dpaste.com/hold/159619/ wiki.py - providing the wiki http://dpaste.com/hold/159634/ Now putting both together: import pbus import wiki import auth_openid # or: import auth_shibboleth pbus.get(wiki).run() No interface definitions. What do you think? Any obvious pitfalls (besides reinventing something)? Please keep in mind that syntax/api is not done or anything its just an concept presentation. Thanks, Florian -- Florian Ludwig d...@phidev.org signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError Exception in email lib
On Mon, Feb 15, 2010 at 3:42 PM, MRAB pyt...@mrabarnett.plus.com wrote: BJ Swope wrote: [snip] def clean_stale_mail(): msg_date1= the_email.get('Date') What is the value of 'msg_date1' at this point? msg_date2 = email.utils.parsedate_tz(msg_date1) What is the value of 'msg_date2' at this point? The docs say that parsedate_tz() can return a 10-tuple or None. Presumably, if it can't parse the date then it returns None. [snip] msg_date1: @@DATE msg_date2: None Thanks MRAB, Dave and Stephen! The spam bot didn't populate the date field with anything remotely resembling the date. The @@DATE var was left in the Date header. In reading the module I misunderstood what the if date[9] is None: was doing. I'll wrap my calls in a try/except. -- http://mail.python.org/mailman/listinfo/python-list
Re: Plugin architecture
Hi mk, On Mon, 2010-02-15 at 19:43 +0100, mk wrote: Hello everyone, I'm thinking about writing plugin-style architecture for (a new) web-based app (with SQLAlchemy as backend). [...] How would you approach designing such architecture using features available in Python (and some major web framework, like Pylons or Django)? You should really look into the web framework you're using, there probably already is some plugin architecture. But being not content myself with the architectures I've seen or worked with I was looking for some input as well and posted some days back about a similar topic. Maybe some of the thoughts expressed there might help you as well: http://groups.google.com/group/comp.lang.python/browse_thread/thread/0a210267753919b0/5add7bc93789b418 Florian -- Florian Ludwig d...@phidev.org signature.asc Description: This is a digitally signed message part -- http://mail.python.org/mailman/listinfo/python-list
Re: Replace various regex
On Feb 15, 2:27 pm, Jean-Michel Pichavant jeanmic...@sequans.com wrote: Martin wrote: On Feb 15, 2:03 pm, Jean-Michel Pichavant jeanmic...@sequans.com wrote: Martin wrote: Hi, I am trying to come up with a more generic scheme to match and replace a series ofregex, which look something like this... 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) 5.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) Ideally match the pattern to the right of the ! sign (e.g. lai), I would then like to be able to replace one or all of the corresponding numbers on the line. So far I have a rather unsatisfactory solution, any suggestions would be appreciated... The file read in is an ascii file. f = open(fname, 'r') s = f.read() if CANHT: s = re.sub(r\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ ! canht_ft, CANHT, s) where CANHT might be CANHT = '115.01,16.38,0.79,1.26,1.00 ! canht_ft' But this involves me passing the entire string. Thanks. Martin I remove all lines containing things like 9*0.0 in your file, cause I don't know what they mean and how to handle them. These are not numbers. import re replace = { 'snow_grnd' : (1, '99.99,'), # replace the 1st number by 99.99 't_soil' : (2, '88.8,'), # replace the 2nd number by 88.88 } testBuffer = 0.749, 0.743, 0.754, 0.759 ! stheta(1:sm_levels)(top to bottom) 0.46 ! snow_grnd 276.78,277.46,278.99,282.48 ! t_soil(1:sm_levels)(top to bottom) 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) 200.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) outputBuffer = '' for line in testBuffer.split('\n'): for key, (index, repl) in replace.items(): if key in line: parameters = { 'n' : '[\d\.]+', # given you example you have to change this one, I don't know what means 9*0.0 in your file 'index' : index - 1, } # the following pattern will silently match any digit before the indexth digit is found, and use a capturing parenthesis for the last pattern = '(\s*(?:(?:%(n)s)[,\s]+){0,%(index)s})(?:(%(n)s)[,\s]+)(.*!.*)' % parameters # regexp are sometimes a nightmare to read line = re.sub(pattern, r'\1 '+repl+r'\3' , line) break outputBuffer += line +'\n' print outputBuffer Thanks I will take a look. I think perhaps I was having a very slow day when I posted and realised I could solve the original problem more efficiently and the problem wasn't perhaps as I first perceived. It is enough to match the tag to the right of the ! sign and use this to adjust what lies on the left of the ! sign. Currently I have this...if anyone thinks there is a neater solution I am happy to hear it. Many thanks. variable_tag = 'lai' variable = [200.0, 60.030, 0.060, 0.030, 0.030] # generate adjustment string variable = ,.join([%s % i for i in variable]) + ' ! ' + variable_tag # call func to adjust input file adjustStandardPftParams(variable, variable_tag, in_param_fname, out_param_fname) and the inside of this func looks like this def adjustStandardPftParams(self, variable, variable_tag, in_fname, out_fname): f = open(in_fname, 'r') of = open(out_fname, 'w') pattern_found = False while True: line = f.readline() if not line: break pattern = re.findall(r!\s++variable_tag, line) if pattern: print 'yes' print of, %s % variable pattern_found = True if pattern_found: pattern_found = False else: of.write(line) f.close() of.close() return Are you sure a simple if variable_tag in line: # do some stuff is not enough ? People will usually prefer to write for line in open(in_fname, 'r') : instead of your ugly while loop ;-) JM My while loop is suitably offended. I have changed it as you suggested...though if I do: if pattern (variable_tag) in line as you suggested i would in my example correctly pick the tag lai, but also one called dcatch_lai, which I wouldn't want. No doubt there is an obvious solution I am again missing! of = open(out_fname, 'w') pattern_found = False for line in open(in_fname, 'r'): pattern = re.findall(r!\s++variable_tag, line) if pattern: print of, %s % variable pattern_found = True if pattern_found: pattern_found = False else: of.write(line) of.close() Many Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: MODULE FOR I, P FRAME
On Sun, 14 Feb 2010 10:07:35 -, DANNY danijel.gv...@gmail.com wrote: Hy, first thanks for your response! Well I am thinkin on coding in MPEG4/10, but I would just encode the video in that encoding, then stream it with VLC and save the video file on my disc. Then I would play it with my player I think you're misunderstanding what VLC does here. Saving the video file should preserve format by default; you may be able save it out in YUV format (I don't have a copy on this machine to check), but that will take up ludicrous amounts of disc space and you'd still have to write a byte reader for it. If you do do that, you have lost any chance of knowing whether a frame was an I or P frame in the original format, not that it matters anyway by that point. MPEG-4/10 is hard to write efficient decoders for, and I have to admit I wouldn't do it in Python. You'd be better off writing a wrapper for one of the existing MP4 libraries. -- Rhodri James *-* Wildebeeste Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: how to structure a directory with many scripts and shared code
On Mon, 15 Feb 2010 16:29:05 +0100, Benedict Verheyen wrote: However, when i make a subdirectory, for example database and put a script in there, how would i import logutils or mailtutils from within the database subdirectory? This fails: from tools.logutils import logger Sounds like you need to ensure that the top level directory needs to be added to your PYTHONPATH. Do you need instructions to do this? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
On Mon, 15 Feb 2010 21:25:23 +, Arnaud Delobelle wrote: John Posner jjpos...@optimum.net writes: [...] x = s[0] [...] assigns the name *x* to the object that *s[0]* refers to s[0] does not refer to an object, it *is* an object (once evaluated of course, otherwise it's just a Python expression). Precisely. Treated as an expression, that is, as a string being evaluated by the compiler, we would say that it *refers to* an object (unless evaluation fails, in which case it refers to nothing at all). But treated as whatever you get after the compiler is done with it, that is, post- evaluation, we would say that it *is* an object. This subtle distinction is essentially the difference between a label and the thing that is labeled. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Parsing for email addresses
Hey all, I'm trying to write python code that will open a textfile and find the email addresses inside it. I then want the code to take just the characters to the left of the @ symbol, and place them in a list. (So if galileo...@gmail.com was in the file, 'galileo228' would be added to the list.) Any suggestions would be much appeciated! Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
* Steven D'Aprano: On Mon, 15 Feb 2010 21:25:23 +, Arnaud Delobelle wrote: John Posner jjpos...@optimum.net writes: [...] x = s[0] [...] assigns the name *x* to the object that *s[0]* refers to s[0] does not refer to an object, it *is* an object (once evaluated of course, otherwise it's just a Python expression). Precisely. Treated as an expression, that is, as a string being evaluated by the compiler, we would say that it *refers to* an object (unless evaluation fails, in which case it refers to nothing at all). But treated as whatever you get after the compiler is done with it, that is, post- evaluation, we would say that it *is* an object. This subtle distinction is essentially the difference between a label and the thing that is labeled. The main differences between a pure functional language where that view can hold and be reasonable, and a language like Python, are that * Python assignments change which object a name refers to, /at runtime/. - Name binding is run time action, not a compile time action. Without run time binding names couldn't be reassigned. s = 1; s = 2 * Some Python objects are modifiable (a.k.a. mutable). - This is particularly important when two or more names refer to the same object and that object is modified. That is, a simple-minded transfer of concepts from pure functional programming to Python breaks down in these cases[1]. I hope this explanation of exactly where the functional programming enthusiasts here go wrong can be of help to readers of the thread, although I've given up hope on those holding the functional programming view (since I'm only human, and even the Gods contend in vain against that sort of thing). Cheers hth., - Alf Notes: [1] Steven D'Aprano's focus on compilation rather than execution mainly ignores the first point, that a name in given a statement in a loop, say, can refer to different objects in different loop iterations. Happily the Python language specification explains binding as a run-time action. Binding is not a compile time action in Python, it is a run-time action, and can bind a given name in a given statement within the same routine execution, to different objects, and the language specification of course uses the phrase refers to to explain the situation after a run time binding. -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing for email addresses
On Feb 15, 3:34 pm, galileo228 mattbar...@gmail.com wrote: I'm trying to write python code that will open a textfile and find the email addresses inside it. I then want the code to take just the characters to the left of the @ symbol, and place them in a list. (So if galileo...@gmail.com was in the file, 'galileo228' would be added to the list.) Any suggestions would be much appeciated! You may want to use regexes for this. For every match, split on '@' and take the first bit. Note that the actual specification for email addresses is far more than a single regex can handle. However, for almost every single case out there nowadays, a regex will get what you need. -- http://mail.python.org/mailman/listinfo/python-list
Re: Few Small Questions Regarding CGI
On Feb 15, 2:04 pm, joy99 subhakolkata1...@gmail.com wrote: I am trying to learn CGI. I was checking Python Docs. There are multiple modules. Which one to start with? Is there any other material or URL for step by step learning of CGI. I would suggest skipping 15 years of internet progress and going with a more modern framework. I recommend Pylons, although Django and web.py are not bad. My next question is: I want to test it. I have a small personal computer. I have internet but is connected by a service provider. I do not have personal web page. Can I use my personal computer as a client as well as a server? Yes. The nature of the internet means anyone that connects can be a client or a server or both at the same time. Heck, you don't even need to have a connection to the internet. Just play with 127.0.0.1, which points right back to the same machine you're running on. I do this all the time for developing and testing out websites or change to websites I work on. -- http://mail.python.org/mailman/listinfo/python-list
Re: Time out a regular expression in Python 2.6.4?
On Feb 15, 7:59 am, Steve Holden st...@holdenweb.com wrote: pyt...@bdurham.com wrote: Is there any way to time out a regular expression in Python 2.6.4? Motiviation: Our application allows users to enter regular expressions as validation criteria. If a user enters a pathological regular expression, we would like to timeout the evaluation of this expression after a short period of time. Python itself does not contain any mechanism to terminate an operation if it takes too much time. One approach would be to run the regex in a subprocess, and apply process limits to terminate that subprocess if it ran too long. This group being what it is you are likely to receive other, better suggestions too. I'm not sure how exactly the re module is implemented, but since I assume a great chunk is in C code, you may get away with a single process and multiple threads. One thread will watch the process, or have a timer event set to go off at a certain point. The other will actually run the regex and get killed by the timer process if it doesn't finish in time. -- http://mail.python.org/mailman/listinfo/python-list
listing existing windows services with python
Hi, What is the best way with python to get a list of all windows services. As a start I would be glad to receive only the service names. However it would be nicer if I could get all the properties of a service as well. Thanks for any info and bye N -- http://mail.python.org/mailman/listinfo/python-list
How to use python to register a service (an existing .exe file)
Hi, Is there a python way to register new windows services. I am aware of the instsrv.exe program, which can be used to install services. I could use subprocess.Popen to call instsrv.exe service_name program.exe but wondered, whether there's already an existing function. Thans in advance and bye N -- http://mail.python.org/mailman/listinfo/python-list
Re: Which mock library do you prefer?
Lacrima lacrima.ma...@gmail.com writes: Minimock has wider usage and community, but I have some troubles using it. Maybe I am wrong, but with minimock you always have to keep track the order of imports in your test modules. Well, may be I just don't understand fully how minimock works. I'm not sure why you think you need to keep track of the order of imports. Simply set up the mocks as you want them, in your fixtures; then, when tearing down your fixtures, use ‘minimock.restore()’ to restore the affected namespaces to their initial state. -- \ “… one of the main causes of the fall of the Roman Empire was | `\that, lacking zero, they had no way to indicate successful | _o__) termination of their C programs.” —Robert Firth | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: listing existing windows services with python
* News123: Hi, What is the best way with python to get a list of all windows services. As a start I would be glad to receive only the service names. However it would be nicer if I could get all the properties of a service as well. Thanks for any info and bye * Library: If there is some existing library for that (just search the web) then that would probably be the easiest. * Registry functions: Otherwise, I'd use the general registry access functions. Info about the registry access functions is available in your Python docs. All the information about services is in the Windows registry (documented in the MSDN Library, which is available in a browsable on-line version at Microsoft). * Text-oriented commands: An alternative to using the registry access functions is to capture the output of e.g. the 'sc' command. The 'net' command also has some functionality related to services, e.g. 'net start'. Cheers hth., - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: Modifying Class Object
On 2/15/2010 6:09 PM, Steven D'Aprano wrote: On Mon, 15 Feb 2010 21:25:23 +, Arnaud Delobelle wrote: John Posnerjjpos...@optimum.net writes: [...] x = s[0] [...] assigns the name *x* to the object that *s[0]* refers to s[0] does not refer to an object, it *is* an object (once evaluated of course, otherwise it's just a Python expression). Precisely. Treated as an expression, that is, as a string being evaluated by the compiler, we would say that it *refers to* an object (unless evaluation fails, in which case it refers to nothing at all). But treated as whatever you get after the compiler is done with it, that is, post- evaluation, we would say that it *is* an object. This subtle distinction is essentially the difference between a label and the thing that is labeled. Is this your only quibble with my writeup? If, so, I'm gratified. And your objections make perfect sense. Still, I'll attempt to justify my phrasing. I was originally going to write: assigns the name *x* to the object that THE NAME *s[0]* refers to ... but I didn't want to start a distracting argument on the use of the phrase *the name* to describe the 4-char string *s[0]*. So now I'll try to (briefly) make my case. Yes, it might be more correct to say that *s[0]* is an expression (equivalent to the more obvious expression *s.__getitem__(0)*). But in common usage, the 4-char string *s[0]* _behaves_ like a name. If you accept this viewpoint, the story on Python assignment statements becomes quite simple ... Syntactic sugar aside, there are only two kinds of assignment statements: 1. NAME = EXPRESSION The EXPRESSION creates a new object, and the NAME is assigned to that object. Examples: x = x + 1 obj = MyClass(1, 2, red) mywordlist = mysentence.split() 2. NAME2 = NAME1 No new object is created. NAME2 becomes another name (an alias) for the existing object that currently has NAME1 assigned to it. Examples: y = x s[0] = s[42] mydict[spamwich] == this_sandwich obj.color = MYCOLORS.LTGREEN This viewpoint might fail in advanced areas of Python programming: properties/descriptors, double-underscore methods, etc. But in my own day-to-day usage (admittedly, I'm a hobbyist Python programmer, not a professional), it's never failed me to think this way: * A dict is a collection of user-devised names, each of which is assigned to an object. * A list/tuple is an interpreter-maintained collection of integer names (0, 1, 2, ...), each of which is assigned to an object. * A class instance is very much like a dict. Tx, John -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing for email addresses
Jonathan Gardner wrote: On Feb 15, 3:34 pm, galileo228 mattbar...@gmail.com wrote: I'm trying to write python code that will open a textfile and find the email addresses inside it. I then want the code to take just the characters to the left of the @ symbol, and place them in a list. (So if galileo...@gmail.com was in the file, 'galileo228' would be added to the list.) Any suggestions would be much appeciated! You may want to use regexes for this. For every match, split on '@' and take the first bit. Note that the actual specification for email addresses is far more than a single regex can handle. However, for almost every single case out there nowadays, a regex will get what you need. You can even capture the part as you find the regexps. As Jonathan mentions, finding RFC-compliant email addresses can be a hairy/intractable problem. But you can get a pretty close approximation: import re r = re.compile(r'([-\w._+]+)@(?:[-\w]+\.)+(?:\w{2,5})', re.I) #^ # if you want to allow local domains like # u...@localhost # then change the + marked with the ^ # to a * and the {2,5} to + to unlimit # the TLD. This will change the outcome # of the last test j...@com to True for test, expected in ( ('j...@example.com', True), ('j...@sub.example.com', True), ('@example.com', False), ('@sub.example.com', False), ('@com', False), ('j...@com', False), ): m = r.match(test) if bool(m) ^ expected: print Failed: %r should be %s % (test, expected) emails = set() for line in file('test.txt'): for match in r.finditer(line): emails.add(match.group(1)) print All the emails:, print ', '.join(emails) -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Few Small Questions Regarding CGI
joy99 wrote: Dear Group, I am trying to learn CGI. I was checking Python Docs. There are multiple modules. Which one to start with? Is there any other material or URL for step by step learning of CGI. My next question is: I want to test it. I have a small personal computer. I have internet but is connected by a service provider. I do not have personal web page. Can I use my personal computer as a client as well as a server? A server can be as simple as: #!/usr/bin/env python # -*- coding: ASCII -*- '''Simple CGI server in Python $Id$''' import getopt, os, sys from BaseHTTPServer import HTTPServer from CGIHTTPServer import CGIHTTPRequestHandler port = 8000 opts, args = getopt.getopt (sys.argv[1:], 'd:p:', ['www=', 'port=']) for o, v in opts: if o in ['-d', '--www']: os.chdir (v) elif o in ('-p', '--port'): port = int (v) handler = HTTPServer (('', port), CGIHTTPRequestHandler) handler.serve_forever() If you run this like mycgiserver.py --www=my_www_dir and put your web pages under my_www_dir, and your CGI programs under my_www_dir/cgi-bin then pointing your browser to http://localhost:8000/ will show you your site. Mel. -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing for email addresses
galileo228 mattbar...@gmail.com writes: I'm trying to write python code that will open a textfile and find the email addresses inside it. I then want the code to take just the characters to the left of the @ symbol, and place them in a list. Email addresses can have more than one �...@’ character. In fact, the quoting rules allow the local-part to contain *any ASCII character* and remain valid. Any suggestions would be much appeciated! For a brief but thorough treatment of parsing email addresses, see RFC 3696, “Application Techniques for Checking and Transformation of Names” URL:http://www.ietf.org/rfc/rfc3696.txt, specifically section 3. -- \ “What I have to do is see, at any rate, that I do not lend | `\ myself to the wrong which I condemn.” —Henry Thoreau, _Civil | _o__)Disobedience_ | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Time out a regular expression in Python 2.6.4?
Jonathan Gardner wrote: On Feb 15, 7:59 am, Steve Holden st...@holdenweb.com wrote: pyt...@bdurham.com wrote: Is there any way to time out a regular expression in Python 2.6.4? Motiviation: Our application allows users to enter regular expressions as validation criteria. If a user enters a pathological regular expression, we would like to timeout the evaluation of this expression after a short period of time. Python itself does not contain any mechanism to terminate an operation if it takes too much time. One approach would be to run the regex in a subprocess, and apply process limits to terminate that subprocess if it ran too long. This group being what it is you are likely to receive other, better suggestions too. I'm not sure how exactly the re module is implemented, but since I assume a great chunk is in C code, you may get away with a single process and multiple threads. One thread will watch the process, or have a timer event set to go off at a certain point. The other will actually run the regex and get killed by the timer process if it doesn't finish in time. That would be a great idea if it were possible to kill a thread form outside. Unfortunately it's not, so the best you can do is set a flag and have it queried periodically. This is not practical during re matching. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Time out a regular expression in Python 2.6.4?
Steve Holden wrote: Jonathan Gardner wrote: On Feb 15, 7:59 am, Steve Holden st...@holdenweb.com wrote: pyt...@bdurham.com wrote: Is there any way to time out a regular expression in Python 2.6.4? Motiviation: Our application allows users to enter regular expressions as validation criteria. If a user enters a pathological regular expression, we would like to timeout the evaluation of this expression after a short period of time. Python itself does not contain any mechanism to terminate an operation if it takes too much time. One approach would be to run the regex in a subprocess, and apply process limits to terminate that subprocess if it ran too long. This group being what it is you are likely to receive other, better suggestions too. I'm not sure how exactly the re module is implemented, but since I assume a great chunk is in C code, you may get away with a single process and multiple threads. One thread will watch the process, or have a timer event set to go off at a certain point. The other will actually run the regex and get killed by the timer process if it doesn't finish in time. That would be a great idea if it were possible to kill a thread form outside. Unfortunately it's not, so the best you can do is set a flag and have it queried periodically. This is not practical during re matching. The code for matching in the re module is written in C, and it doesn't release the GIL because it calls the Python API, and you need to have the GIL when doing that (unless you can guarantee that the specific call is safe, that is!). This means that other threads can't run during matching. In order to be able to cancel the matching, the re module would have to release the GIL when possible and have some kind of cancel() method (belonging to which class?). A simpler option would be to add a timeout argument. It already periodically checks for ctrl-C, so perhaps the time check could be done then. -- http://mail.python.org/mailman/listinfo/python-list
Web Service Using XML for input and output
I'm attempting to use a web service that takes XML as an input and returns an XML output. I can connect to the service and attempt to pass in the XML but do not get any results. I believe my problem is how to package up the XML and send it in the request. The XML consists of about 20 tag/value pairs. Any code snippets or a place to look for some examples would be appreciated. I'm currently trying to use an HTTPConnection with the XML as a parameter in the request.-- http://mail.python.org/mailman/listinfo/python-list
Re: listing existing windows services with python
News123 news...@free.fr wrote: What is the best way with python to get a list of all windows services. As a start I would be glad to receive only the service names. However it would be nicer if I could get all the properties of a service as well. I highly recommend Tim Golden's fantastic WMI module[1]. import wmi c = wmi.WMI() services = c.Win32_Service() s = services[0] s _wmi_object: \\LIB-D5NYF1S\root \cimv2:Win32_Service.Name=Alerter s.properties {u'DisplayName': None, u'ServiceSpecificExitCode': None, u'State': None, u'Syste mName': None, u'ErrorControl': None, u'Status': None, u'ProcessId': None, u'Desc ription': None, u'Started': None, u'AcceptStop': None, u'CheckPoint': None, u'Pa thName': None, u'WaitHint': None, u'Name': None, u'InstallDate': None, u'Caption ': None, u'StartMode': None, u'DesktopInteract': None, u'ServiceType': None, u'T agId': None, u'StartName': None, u'AcceptPause': None, u'CreationClassName': Non e, u'SystemCreationClassName': None, u'ExitCode': None} s.Name u'Alerter' s.Started False s.ServiceType u'Share Process' 1: http://timgolden.me.uk/python/wmi/index.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Web Service Using XML for input and output
Shane McIntyre wrote: I'm attempting to use a web service that takes XML as an input and returns an XML output. I can connect to the service and attempt to pass in the XML but do not get any results. I believe my problem is how to package up the XML and send it in the request. The XML consists of about 20 tag/value pairs. Any code snippets or a place to look for some examples would be appreciated. I'm currently trying to use an HTTPConnection with the XML as a parameter in the request. Well, one could fatuously point out that you must be doing it wrong, but without seeing your code it's difficult to know exactly *how* you are going wrong. For all I know your current attempt at solution might require only minimal changes to make it work. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list