Re: So apparently I've been banned from this list
On 30/09/2018 18:30, Steven D'Aprano wrote: I've been unexpectedly in hospital for the past two weeks, without internet or email. Just before my unexpected hospital stay, I was apparently banned (without warning) by Ethan Furman in what seems to me to be an act of retaliation for my protest against his overzealous and hostile tone-policing against a newcomer to the list, Reto Brunner: https://mail.python.org/pipermail/python-list/2018-September/737020.html (I did make one mistake in that post: I claimed that I hadn't said anything at the time on Ethan's last round of bans. That was incorrect, I actually did make an objection at the time.) Since I'm still catching up on emails, I have just come across Ethan's notice to me (copied below). Notwithstanding Ethan's comment about having posted the suspension notice on the list, I see no sign that he actually did so. At the risk of further retaliation from the moderators, I am ignoring the ban in this instance for the purposes of transparency and openness. (I don't know if this will show up on the mailing list or the newsgroup.) Since I believe this ban is illegitimate, I intend to challenge it if possible. In the meantime, I may not reply on-list to any responses. Subject: Fwd: Temporary Suspension To: From: Ethan Furman Date: Tue, 11 Sep 2018 11:22:40 -0700 In-Reply-To: Steven, you've probably already seen this on Python List, but I forgot to email it directly to you. My apologies. -- ~Ethan~ Python List Moderator Forwarded Message Subject: Temporary Suspension Date: Mon, 10 Sep 2018 07:09:04 -0700 From: Ethan Furman To: Python List Moderators As a list moderator, my goal for this list is to keep the list a useful resource -- but what does "useful" mean? To me it means a place that python users can go to ask questions, get answers, offer advice, and all without sarcasm, name-calling, and deliberate mis-understandings. Conversations should stay mostly on-topic. Due to hostile and inappropriate posts*, Steven D'Aprano is temporarily suspended from Python List for a period of two months. This suspension, along with past suspensions, is being taken only after careful consideration and consultation with other Python moderators. -- ~Ethan~ Python List Moderator * posts in question: [1] https://mail.python.org/pipermail/python-list/2018-July/735735.html [2] https://mail.python.org/pipermail/python-list/2018-September/737020.html Hi, I hope you are getting better. Cheers Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: AH01215: ImportError: No module named PIL: - python script unable to recognize PIL when running from apache2 config
On 28/03/2017 18:35, Chris Angelico wrote: On Wed, Mar 29, 2017 at 3:32 AM, Karim <kliat...@gmail.com> wrote: On 28/03/2017 18:05, Chris Angelico wrote: On Wed, Mar 29, 2017 at 2:59 AM, <vishnupras...@gmail.com> wrote: Please help if this wont resolve, I have to completely move away from python. How about, instead, moving away from CGI? Switch to Apache's modpython. I've deployed several Python-based web sites without difficulties. ChrisA Ah ok. What do you think of Django? (Bouncing back to the list because I think the private message was unintentional. Apologies if I'm misreading your intentions.) Not hugely experienced with Django; I mainly use Flask. But they're fairly similar in style and structure. Either way, you make yourself an importable package/module that has all your code in it, and then you set up WSGI so that Apache can call on you. ChrisA Thank you Chris! -- https://mail.python.org/mailman/listinfo/python-list
Re: calling a program from Python batch file
On Wednesday, December 7, 2016 at 12:18:51 PM UTC-5, Michael Torrie wrote: > On 12/07/2016 09:38 AM, Karim Farokhnia wrote: > > I am writing a batch file in Python. The batch file, in part, calls a > > program named "oq-console.bat" to run. Then once the program comes up > > (it looks like windows CMD), I need the batch file to type some > > commands to make it run (just like I would do it by myself). > > > > I need the batch file does the whole things automatically, I mean > > calls the program, types in commands and press enter! > > Is the program that oq-console.bat runs interactive? After it launches > into its own console window it asks for more input? The program doesn't ask for more input parameters as all parameters are in input files and the program will automatically read the files, but I need to type in some required commands and press enter. > > > So far, I have used the below Python codes in my batch file to do > > that; > > > > > > import subprocess > > > > subprocess.call(["C:\Program Files (x86)\OpenQuake > > Engine\oq-console.bat", "oq --run", "job.ini"]) > > > > > > Note that the first string is the program, and the last two are the > > commands which needs to be typed in. > > > > It pulls up the program well, but doesn't type in the commands to > > make it to run. > > You'll have to explain what you mean by "[it] doesn't type ni the > commands to make it run." By [it] I meant the program. -- https://mail.python.org/mailman/listinfo/python-list
calling a program from Python batch file
Hi there, I am writing a batch file in Python. The batch file, in part, calls a program named "oq-console.bat" to run. Then once the program comes up (it looks like windows CMD), I need the batch file to type some commands to make it run (just like I would do it by myself). I need the batch file does the whole things automatically, I mean calls the program, types in commands and press enter! So far, I have used the below Python codes in my batch file to do that; import subprocess subprocess.call(["C:\Program Files (x86)\OpenQuake Engine\oq-console.bat", "oq --run", "job.ini"]) Note that the first string is the program, and the last two are the commands which needs to be typed in. It pulls up the program well, but doesn't type in the commands to make it to run. Any ideas on how I can get the program to run the commands as well? Thank you! -- https://mail.python.org/mailman/listinfo/python-list
Re: Farewell to Rob Collins
On 16/11/2016 11:21, M.-A. Lemburg wrote: s a true Pythonista from the heart. He will always be remember RIP Rob. Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: How can I copy one excel file data to another excel file by excluding hidden rows content using Python
On 19/10/2016 11:30, Kishore JK wrote: I need to copy one excel file data into another excel file by excluding rows which were hidden in source excel file. https://i.stack.imgur.com/NPUK6.png As shown in the image, from the source excel file, I need to copy the data of row numbers 116,135 and 139 and exclude all the remaining rows which were hidden because of not matching the criteria. I have tried below code, but this is copying entire data into new excel sheet. wb = openpyxl.load_workbook('sourcefile.xlsx') sheet = wb.active sheet.title = 'Sheet1' wb.save('destinationfile.xlsx') Hello, I use xlrd external module. This is only samples codes from one of my class. You can filter the row you want with an conditional statement. You must have equivalent attribute in openpyxl module. Import xlrd Import csv self._book= xlrd.open_workbook(xlfilename) self._selected_sheets = [self._book.sheet_by_name(sheetname) for sheetname in sheets] writer = csv.writer(open(xlfilename, 'wb'), delimiter=self.file_format.delimiter) rows = izip(*(sheet.col_values(col) for col in selected_columns)) for row in rows: # NOTE!!!: could have UnicodeEncodeError exception raised here! try: writer.writerow(row) except UnicodeEncodeError, e: print("\nWarning: {0}. Row '{1}' will not be extracted!".format(e, row)) Regards Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: How to add data in xlm file ?
On 13/08/2016 11:05, Asad ur Rehman wrote: def Test(request): save_path = '/usr/share/newfies/' name_of_file = ("Avatar") completeName = os.path.join(save_path, name_of_file+".xlm") file1 = open(completeName, "w") toFile = raw_input("Write what you want into the field") file1.write(toFile) file1.close() This is code for save file in specific directory.Output of this file is empty i want to get data from database i write database query in it but it did't work.Example\ def Test(request): save_path = '/usr/share/newfies/' name_of_file = ("Avatar") completeName = os.path.join(save_path, name_of_file+".xlm") file1 = open(completeName, "w") toFile = Subscriber.objects.all().count() file1.write(toFile) file1.close() Subscriber is table name in my database. How can i get data ? Thanks in advance ! Hello, Use the standard module (I use python2.7), code not tested If my memory is good this is the general way to do it: from xml.dom import minidom from xml.etree import ElementTree as et from xml.etree.ElementTree import ElementTree, Element fname = "my_input_file" xml_data = et.parse(fname) root_element = xml_data.getroot() new_element = et.Element("new_tag") root_element.append(new_element) new_element.set('new_attribute', 'attribute value') fout = "my_ouput_file" fout.write(prettify(root_element)) Where prettify() is defined by: def prettify(element): """ Pretty print to an indented XML output. @param element a Element object. @return a pretty-printed XML string for the element. """ # The idea is to print your Element in a string, parse it using # minidom and convert it again in XML using toprettyxml function. raw_string = et.tostring(element, encoding='UTF-8', method='xml') reparsed = minidom.parseString(raw_string) return reparsed.toprettyxml(indent='\t', encoding='UTF-8') Cheers Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Find a file
On 01/07/2016 02:51, tdspe...@gmail.com wrote: Hi All I have a web app that allows me to choose a file - the file name is returned but I need to find the path to the file so I can add it as an attachment to a email. The files I am returning could be in different folders/directories. Thanks for any help. Cheers Colin Hello, I did this almost equivalent unix find command in python it is documented, it is a generator. If it could help you: import os from os.path import join as jp, normpath as np def find(dirs, filenames=None, exts=None, exclude_dirs=None, exclude_filenames=None, followlinks=False): """ Equivalent to the unix/linux find shell command. @param dirsthe top directories where to search. Could be a simple string. Default to current directory '.'. @param filenames the searched file names. Could be a simple string. @param extsan additional parameter to search files by its extension. Could be cumulative with the filenames @param exclude_dirsa filter to exclude given top directories from the search process. Coud be a simple string. @param exclude_filenames a filter to bypass certain files by its name. Useful when using a search by file extension. Could be a simple string. @param followlinks a boolean to specify whether to follow symbolic links to subdirectories. Defaulted to False. @return a generator data which provides the found file one after another. """ if not filenames and not exts: raise ValueError('The list of searched files and the list of searched file extensions could not be both empty!') if filenames is None: filenames = '' if exts is None: exts = '' if exclude_dirs is None: exclude_dirs = '' if exclude_filenames is None: exclude_filenames = '' # Default value if not dirs: dirs = tuple(('.',)) if isinstance(dirs, str): dirs = tuple((dirs,)) if isinstance(filenames, str): filenames = tuple((filenames,)) if isinstance(exts, str): exts = tuple((exts,)) if isinstance(exclude_dirs, str): exclude_dirs = tuple((exclude_dirs,)) if isinstance(exclude_filenames, str): exclude_filenames = tuple((exclude_filenames,)) # The search. for d in dirs: for dirpath, dirnames, fnames in os.walk(d, followlinks=followlinks): # Filtering unwanted top directories. for d in exclude_dirs: if d in dirnames: dirnames.remove(d) # Search for file names or extensions for f in fnames: file_extension = os.path.splitext(f)[-1][1:] # extension without the dot! if f in filenames or (file_extension in exts and f not in exclude_filenames): yield jp(dirpath, f) if __name__ == '__main__': #print(np('\n'.join(find(dirs='.', exts='li', exclude_filenames='cds.li' # Test l = tuple(find(dirs='.', filenames='cds.lib')) print('\n'.join(l)) Cheers Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: [RELEASE] Python 2.7.12
On 28/06/2016 06:36, Benjamin Peterson wrote: It is my privilege to present you with another release in the Python 2.7 series, Python 2.7.12. Since the release candidate, there were two changes: - The Windows binaries have been changed to use OpenSSL 1.0.2h. - The "about" dialog in IDLE was fixed. Downloads, as always, are on python.org: https://www.python.org/downloads/release/python-2712/ The complete 2.7.12 changelog is available at https://hg.python.org/cpython/raw-file/v2.7.12/Misc/NEWS Yet another Python 2.7.x release is anticipated near the end of the year. Numerologists may wish to upgrade to Python 3 before we hit the unlucky 2.7.13. Servus, Benjamin 2.7 release manager Thank you! Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: movie from pictures
On 10/06/2016 23:31, alex wright wrote: I find shlex.split to be most useful to make my arguments a list in these cases. On Jun 9, 2016 3:28 PM, "MRAB" <pyt...@mrabarnett.plus.com> wrote: On 2016-06-09 19:58, Peter Otten wrote: Nev wrote: Thank you for your reply. I tried something like this in python code: from subprocess import call call(["ffmpeg -framerate 4/1 -start_number 1 -i C:\\Projects\\data2\\img_%05d.png -c:v libx264 -r 30 -pix_fmt yuv420p C:\\Projects\\data2\\movie.mp4"]) But it did not work. I get FileNotFoundError: [WinError 2] The system cannot find the file specified.. You have to pass the command-line arguments as separate items in the list: call(["ffmpeg", "-framerate", "4/1", "-start_number", "1", "-i", "C:\\Projects\\data2\\img_%05d.png", ... # and so on ]) You should also give it the full path of ffmpeg. On the other hand, in-loop solution would be more preferable since it lets me to use variable names of the images and paths.. -- https://mail.python.org/mailman/listinfo/python-list Why not use the split() method on the string of the command line. Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Django Girls workshop at EuroPython 2016
On 26/05/2016 15:31, alex wright wrote: From the link: "If you are a woman, know English and have a laptop you can apply for a pass!" So it would appear so. On Thu, May 26, 2016 at 5:39 AM, Karim <kliat...@gmail.com> wrote: On 26/05/2016 10:58, M.-A. Lemburg wrote: We’re pleased to announce a Django Girls workshop on Sunday, July 17, the first day of the EuroPython 2016 conference. If you want participate or know someone who’d like to join, please fill in our application form on the Django Girls’ website: *** Django Girls EuroPython 2016 *** https://djangogirls.org/europython2016/ Remember: you don’t need any prior programming knowledge to participate ! About the workshop -- After the workshop you will have learned about: * HTML / CSS * Python / Django and in a single day, you’ll have created a blog. If that sounds challenging, no worries: our friendly coaches will help you. Paola Katherine, DG organizer: "If you participate, we’ll raffle one full ticket for EuroPython 2016, the largest and best Python event in Europe." More infos -- The Django Girls EuroPython 2016 networks: * More infos and details on the website: djangogirls.org/europython2016 * Facebook: fb.me/djangogirlseuropython * Twitter: @DjangoGirlsEP16 If you have questions, please write to bil...@djangogirls.org With gravitational regards, -- EuroPython 2016 Team http://ep2016.europython.eu/ http://www.europython-society.org/ PS: Please forward or retweet to help us reach all interested parties: https://twitter.com/europython/status/735755247046426624 Thanks. Hello, This is only for girls? Regards KL -- https://mail.python.org/mailman/listinfo/python-list Thank you Alex I missed that. Regards KL -- https://mail.python.org/mailman/listinfo/python-list
Re: Django Girls workshop at EuroPython 2016
On 26/05/2016 10:58, M.-A. Lemburg wrote: We’re pleased to announce a Django Girls workshop on Sunday, July 17, the first day of the EuroPython 2016 conference. If you want participate or know someone who’d like to join, please fill in our application form on the Django Girls’ website: *** Django Girls EuroPython 2016 *** https://djangogirls.org/europython2016/ Remember: you don’t need any prior programming knowledge to participate ! About the workshop -- After the workshop you will have learned about: * HTML / CSS * Python / Django and in a single day, you’ll have created a blog. If that sounds challenging, no worries: our friendly coaches will help you. Paola Katherine, DG organizer: "If you participate, we’ll raffle one full ticket for EuroPython 2016, the largest and best Python event in Europe." More infos -- The Django Girls EuroPython 2016 networks: * More infos and details on the website: djangogirls.org/europython2016 * Facebook: fb.me/djangogirlseuropython * Twitter: @DjangoGirlsEP16 If you have questions, please write to bil...@djangogirls.org With gravitational regards, -- EuroPython 2016 Team http://ep2016.europython.eu/ http://www.europython-society.org/ PS: Please forward or retweet to help us reach all interested parties: https://twitter.com/europython/status/735755247046426624 Thanks. Hello, This is only for girls? Regards KL -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing different sections into a file
On 25/04/2016 17:00, justin walters wrote: On Mon, Apr 25, 2016 at 3:04 AM, Karim <kliat...@gmail.com> wrote: On 25/04/2016 09:30, Palpandi wrote: Hi, I need to write different sections into a file. At any point of time, content can be added to any section. I don't want keep each section into a temporary file. What is the better way to store the contents of each section and write them into a file at the end? What is the better datatype to achieve this? Thanks and Regards, Palpandi Use Stringio: - from cStringIO import StringIO content = StringIO() # Header content.write('; Header\n') content.write('; Body'\n) content.write('; Footer\n') open('my_file', 'wb').write(content.getvalue()) - Karim -- https://mail.python.org/mailman/listinfo/python-list All of the other answers are great too. I was thinking that you could format the text file to have dividers for each section. For instance it may look something like this: Header Lorem ipsum <---> Body Lorem ipsum... <---> Footer Lorem ipsum.. Then, you could create a list of sections like so: file = open('file.txt', 'r').read() section_list = file.split('<--->') print(section_list[0]) Header Lorem ipsum... Say you wanted to get really fancy, you could even format the file to have named sections like so: Lorem ipsum... Lorem ipsum... Lorem ipsum... Then you can use the xmlparser library to parse the file into an xml object. Alternatively, you could also use JSON formatting: { sections: { header: { title: "header", content: "Lorem ipsum..." }, body: { title: "Body", content: "Lorem ipsum..." }, footer: { title: "Footer", content: "Lorem ipsum..." } } } I hope this helps. Great ideas! -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing different sections into a file
On 25/04/2016 09:30, Palpandi wrote: Hi, I need to write different sections into a file. At any point of time, content can be added to any section. I don't want keep each section into a temporary file. What is the better way to store the contents of each section and write them into a file at the end? What is the better datatype to achieve this? Thanks and Regards, Palpandi Use Stringio: - from cStringIO import StringIO content = StringIO() # Header content.write('; Header\n') content.write('; Body'\n) content.write('; Footer\n') open('my_file', 'wb').write(content.getvalue()) - Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Fraud
On 17/04/2016 03:27, Steven D'Aprano wrote: On Sun, 17 Apr 2016 03:12 am, Mel Drosis wrote: My phone my accounts my home network have all been affected because of someone using coding from Python and Linux and GitHub and json. I don't even know what this stuff is but how do I get rid of it all. It's ruined my life. Sent from my iPhone Go to Russia. Find a corrupt Russian general. Purchase the biggest nuclear warhead you can afford, at least 20 megatons. Launch it into space and detonate it in near-Earth orbit. When the radiation hits the atmosphere, it will generate the mother of all electromagnetic pulses. Which will permanently fry all computer chips and most electronics all over the world, sending the entire planet back to pre-Industrial Revolution technology. (You may need to do it twice, once for each hemisphere.) Which is the only way to get rid of the scourge of "Python and Linux and GitHub and json" once and for all. Steven you make me laugh very loud. hahaha. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python, Linux, default search places.
On 08/04/2016 12:01, frantisek.fridr...@rubena.cgs.cz wrote: Hello. Thank you to Karim, thank you to Wildman for response. I will describe my problem in more detail. Python on my computer is installed in /usr directory. My Python contains additional modules such as numpy, scipy, matplotlib, h5py, … . Python runs correctly. Third party program opens Python but I can’t import numpy and so on. Python can’t find numpy. After modifying PYTHONPATH, Python can find numpy but shows the following error message: /usr/lib64/python2.6/lib-dynload/math.so: undefined symbol: PyFPE_jbuf I think Python can’t find some libraries. I don’t know what the third party program does with environment variables but I think I should correctly define environment either in shell before I start third party program or in Python in third party program. That is the reason why I would like to know default: - search path for libraries, - search path for Python modules, in case LD_LIBRARARY_PATH and PYTHONPATH is empty not defined. I think that default - search path for executables is easier task. I also would like to know the correct default definition. OS: SUSE Linux Enterprise Server 11. HW: HP DL160 Gen8 SFF CTO. Python 2.6. Frantisek Disclaimer: This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. Distribution only by express authority of a Rubena company. Hi Frantisek, If it could help you whatever the platform you are on the site python pkg gives you information of all 3rd parties modules and dynamic loads: bash: python /usr/lib/python2.7/site.py sys.path = [ '/usr/lib/python2.7', '/home/karim/project/pyparsing-2.0.3', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', ] USER_BASE: '/home/karim/.local' (exists) USER_SITE: '/home/karim/.local/lib/python2.7/site-packages' (doesn't exist) ENABLE_USER_SITE: True Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Python, Linux, default search places.
On 07/04/2016 13:02, frantisek.fridr...@rubena.cgs.cz wrote: Hello. I run a third party program that can use a system installation of Python. I have to modify environment variables: PYTHONPATH, PATH, LD_LIBRARY_PATH. All these environment variables are empty at the beginning but Python uses a default or initial places to search for modules, libraries or executables. Q1. Could anybody tell me where I could find default search places for environment variables PYTHONPATH, PATH, LD_LIBRARY_PATH? OS: SUSE Linux Enterprise Server 11. HW: HP DL160 Gen8 SFF CTO. Python 2.6. Frantisek export PYTHONPATH=${PYTHONPATH}// export PATH=${PATH}:/bin export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/lib Mine for pyparsing: export PYTHONPATH=~/project/pyparsing-2.0.3 Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Re-using TCL code from python over network
On 29/03/2016 10:29, Sharad Singla wrote: Thanks. This is more of invoking python code from TCL. I am looking for the other way round. Curious, did you rewrite all your TCL code in python? Regards Sharad On Mar 29, 2016 1:10 PM, "Karim" <kliat...@gmail.com <mailto:kliat...@gmail.com>> wrote: On 29/03/2016 07:20, sharad1...@gmail.com <mailto:sharad1...@gmail.com> wrote: Hi We've a test automation framework written in TCL (including the automated test cases). We are evaluating shifting to Python and have a test framework in Python (including the automated test cases). Python provides a lot more 3rd party libraries that we'd like to make use of. We use a pretty old version of TCL (8.4.5, 32 bit). It's on FreeBSD and we've compiled it in-house. Compiling it to 64 bit or moving to a newer version is a massive task (since we've a lot of libraries - written in C and compiled as well as pure tcl). Also, we are evaluating having this Python infrastructure on Linux (CentOS). I've explored Python's Tkinter but it won't suit our case as it points to system installed TCL. I've also explored Python's subprocess (launch an interactive TCL shell remotely) and pexpect but none of them worked well for me to allow me to use TCL code interactively from Python. I'd like to gather any ideas/experience around this. If anyone has tried a similar stuff before and can share his/her experience, I'd appreciate it. Regards Sharad You can find below a partial example where I launch a python process from a tcl program to get data from python which reads a database. You just have to get and compile tclpython (google is your best friend) which is a C interface bridging python and tcl and allow to launch at most 5 python interpreter processes if I remember correctly. I used it during 4 years but I now I migrated all the TCL code to python one indeed I don't need it anymore. But it is useful to do the transition. #!/usr/bin/env tclsh8.4 lappend auto_path $env(TCLPYTHON_PKG_PATH) package require tclpython 4.1 namespace eval ops { namespace export initPython namespace export exitPython ... namespace export getDeviceDescription } proc ops::initPython {} { # # @goal: Create the interpreter process and import python needed modules. # @arg: # @return: # variable interpreter set interpreter [python::interp new] $interpreter exec {from ops.tcl.pythontcl import to_string, to_list, to_dict, to_bool} $interpreter exec "opsdb = None" $interpreter exec "input_structure = dict()" } proc ops::exitPython {} { # # @goal: Close the interpreter process. # @arg: # @return: # variable interpreter python::interp delete $interpreter } proc ops::getDeviceDescription { libName deviceName } { # # @goal: get # @arg: # @return: # variable interpreter $interpreter exec "d_s = to_string(getDeviceDescription(opsdb, '$libName', '$deviceName'))" eval "set value [$interpreter eval {d_s}]" return $value } Karim Yes, part by part. In fact it was faster than expected thanks to python syntax. TCL coding is heavier than python equivalent. I say that by experience. My project involved 2 developers one pro TCL and myself pro Python. We found the best deal for both. Now I am alone. This is the reason everything is now python. But it was a good experience to learn TCL (weaknesses). Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Re-using TCL code from python over network
On 29/03/2016 07:20, sharad1...@gmail.com wrote: Hi We've a test automation framework written in TCL (including the automated test cases). We are evaluating shifting to Python and have a test framework in Python (including the automated test cases). Python provides a lot more 3rd party libraries that we'd like to make use of. We use a pretty old version of TCL (8.4.5, 32 bit). It's on FreeBSD and we've compiled it in-house. Compiling it to 64 bit or moving to a newer version is a massive task (since we've a lot of libraries - written in C and compiled as well as pure tcl). Also, we are evaluating having this Python infrastructure on Linux (CentOS). I've explored Python's Tkinter but it won't suit our case as it points to system installed TCL. I've also explored Python's subprocess (launch an interactive TCL shell remotely) and pexpect but none of them worked well for me to allow me to use TCL code interactively from Python. I'd like to gather any ideas/experience around this. If anyone has tried a similar stuff before and can share his/her experience, I'd appreciate it. Regards Sharad You can find below a partial example where I launch a python process from a tcl program to get data from python which reads a database. You just have to get and compile tclpython (google is your best friend) which is a C interface bridging python and tcl and allow to launch at most 5 python interpreter processes if I remember correctly. I used it during 4 years but I now I migrated all the TCL code to python one indeed I don't need it anymore. But it is useful to do the transition. #!/usr/bin/env tclsh8.4 lappend auto_path $env(TCLPYTHON_PKG_PATH) package require tclpython 4.1 namespace eval ops { namespace export initPython namespace export exitPython ... namespace export getDeviceDescription } proc ops::initPython {} { # # @goal: Create the interpreter process and import python needed modules. # @arg: # @return: # variable interpreter set interpreter [python::interp new] $interpreter exec {from ops.tcl.pythontcl import to_string, to_list, to_dict, to_bool} $interpreter exec "opsdb = None" $interpreter exec "input_structure = dict()" } proc ops::exitPython {} { # # @goal: Close the interpreter process. # @arg: # @return: # variable interpreter python::interp delete $interpreter } proc ops::getDeviceDescription { libName deviceName } { # # @goal: get # @arg: # @return: # variable interpreter $interpreter exec "d_s = to_string(getDeviceDescription(opsdb, '$libName', '$deviceName'))" eval "set value [$interpreter eval {d_s}]" return $value } Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: libre office
On 20/01/2016 00:01, jim-pc wrote: How do I get data from libre office using python? Hello, The function below search and get text between 2 delimiters inside a libre office swriter document. It is given as an example. You still have to open a document object in Swriter server.(In the Libre office UNO website you will find simple code to do that step.) def get_text_inside_delimiters(component=None, delimiter1=None, delimiter2=None): """ Get text between 2 string delimiters.""" # Creation of 2 descriptors from a document w/ search capabilities. open_search = component.createSearchDescriptor() close_search = component.createSearchDescriptor() # Specify the delimiter texts to find. open_search.SearchString = delimiter1 close_search.SearchString = delimiter2 # Find and open the first delimiter object. open_found = component.findFirst(open_search) # Unselect the 1rst delimiter from the range selection # done by findFirst() to not have in the search result. open_found.goRight(0, False) # Search for the 2nd delimiter the closest from first delimiter. close_found = component.findNext(open_found.End, close_search) # Filter the 2nd delimiter from the range selection # done by findNext() to not have in the search result. close_found.goLeft(len(close_found.getString()), False) # Cursor selection of the target string. open_found.gotoRange(close_found, True) # Return target string whithin the cursor selection. return open_found.getString().strip() Hope this help. Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding how to quote XML string in order to serialize using Python's ElementTree
On 09/01/2016 21:54, kbtyo wrote: My specs: Python 3.4.3 Windows 7 IDE is Jupyter Notebooks What I have referenced: 1) http://stackoverflow.com/questions/1546717/python-escaping-strings-for-use-in-xml 2) http://stackoverflow.com/questions/7802418/how-to-properly-escape-single-and-double-quotes 3)http://stackoverflow.com/questions/4972210/escaping-characters-in-a-xml-file-with-python Here is the data (in CSV format) and script, respectively, (I have tried variations on serializing Column 'E' using both Sax and ElementTree): i) A,B,C,D,E,F,G,H,I,J "3","8","1","2312285SChecking10","',0001,0070,","1967-12-25 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0" ii) #!/usr/bin/python # -*- coding: utf-8 -*- import os.path import sys import csv from io import StringIO import xml.etree.cElementTree as ElementTree from xml.etree.ElementTree import XMLParser import xml import xml.sax from xml.sax import ContentHandler class MyHandler(xml.sax.handler.ContentHandler): def __init__(self): self._charBuffer = [] self._result = [] def _getCharacterData(self): data = ''.join(self._charBuffer).strip() self._charBuffer = [] return data.strip() #remove strip() if whitespace is important def parse(self, f): xml.sax.parse(f, self) return self._result def characters(self, data): self._charBuffer.append(data) def startElement(self, name, attrs): if name == 'Response': self._result.append({}) def endElement(self, name): if not name == 'Response': self._result[-1][name] = self._getCharacterData() def read_data(path): with open(path, 'rU', encoding='utf-8') as data: reader = csv.DictReader(data, delimiter =',', quotechar="'", skipinitialspace=True) for row in reader: yield row if __name__ == "__main__": empty = '' Response = 'sample.csv' for idx, row in enumerate(read_data(Response)): if idx > 10: break data = row['E'] print(data) # The before data = data[1:-1] data = ""'{}'"".format(data) print(data) # Sanity check # data = '',0001,0070,' try: root = ElementTree.XML(data) # print(root) except StopIteration: raise pass # xmlstring = StringIO(data) # print(xmlstring) # Handler = MyHandler().parse(xmlstring) Specifically, due to the quoting in the CSV file (which is beyond my control), I have had to resort to slicing the string (line 51) and then formatting it (line 52). However the print out from the above attempt is as follows: "' File "", line unknown ParseError: no element found: line 1, column 69 Interestingly - if I assign the variable "data" (as in line 54) I receive this: File "", line 56 data = '',0001,0070,' ^ SyntaxError: invalid token I seek feedback and information on how to address utilizing the most Pythonic means to do so. Ideally, is there a method that can leverage ElementTree. Thank you, in advance, for your feedback and guidance. In fact to get rid of double quote simply create your csv reader like that: reader = csv.DictReader(data, dialect='excel', skipinitialspace=True) You should then don't need to slice data variable and reformat it. Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding how to quote XML string in order to serialize using Python's ElementTree
On 09/01/2016 21:54, kbtyo wrote: My specs: Python 3.4.3 Windows 7 IDE is Jupyter Notebooks What I have referenced: 1) http://stackoverflow.com/questions/1546717/python-escaping-strings-for-use-in-xml 2) http://stackoverflow.com/questions/7802418/how-to-properly-escape-single-and-double-quotes 3)http://stackoverflow.com/questions/4972210/escaping-characters-in-a-xml-file-with-python Here is the data (in CSV format) and script, respectively, (I have tried variations on serializing Column 'E' using both Sax and ElementTree): i) A,B,C,D,E,F,G,H,I,J "3","8","1","2312285SChecking10","',0001,0070,","1967-12-25 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0" ii) #!/usr/bin/python # -*- coding: utf-8 -*- import os.path import sys import csv from io import StringIO import xml.etree.cElementTree as ElementTree from xml.etree.ElementTree import XMLParser import xml import xml.sax from xml.sax import ContentHandler class MyHandler(xml.sax.handler.ContentHandler): def __init__(self): self._charBuffer = [] self._result = [] def _getCharacterData(self): data = ''.join(self._charBuffer).strip() self._charBuffer = [] return data.strip() #remove strip() if whitespace is important def parse(self, f): xml.sax.parse(f, self) return self._result def characters(self, data): self._charBuffer.append(data) def startElement(self, name, attrs): if name == 'Response': self._result.append({}) def endElement(self, name): if not name == 'Response': self._result[-1][name] = self._getCharacterData() def read_data(path): with open(path, 'rU', encoding='utf-8') as data: reader = csv.DictReader(data, delimiter =',', quotechar="'", skipinitialspace=True) for row in reader: yield row if __name__ == "__main__": empty = '' Response = 'sample.csv' for idx, row in enumerate(read_data(Response)): if idx > 10: break data = row['E'] print(data) # The before data = data[1:-1] data = ""'{}'"".format(data) print(data) # Sanity check # data = '',0001,0070,' try: root = ElementTree.XML(data) # print(root) except StopIteration: raise pass # xmlstring = StringIO(data) # print(xmlstring) # Handler = MyHandler().parse(xmlstring) Specifically, due to the quoting in the CSV file (which is beyond my control), I have had to resort to slicing the string (line 51) and then formatting it (line 52). However the print out from the above attempt is as follows: "' File "", line unknown ParseError: no element found: line 1, column 69 Interestingly - if I assign the variable "data" (as in line 54) I receive this: File "", line 56 data = '',0001,0070,' ^ SyntaxError: invalid token I seek feedback and information on how to address utilizing the most Pythonic means to do so. Ideally, is there a method that can leverage ElementTree. Thank you, in advance, for your feedback and guidance. I don't understand because this line 54 gives: >>> import xml.etree.cElementTree as ElementTree >>> data = 'RequestType="HoldInquiry">',0001,0070,' File "", line 1 data = 'RequestType="HoldInquiry">',0001,0070,' ^ SyntaxError: invalid syntax BUT IF you correct the string and remove the inner quote after everything's fine: >>> data = 'RequestType="HoldInquiry">,0001,0070, ' >>> root = ElementTree.XML(data) >>> root Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Understanding how to quote XML string in order to serialize using Python's ElementTree
Yes it changes your quotechar = "'" into quotechar = '"' You should no more get the double quoting of the data string and no more slicing step. Karim On 10/01/2016 00:15, Saran Ahluwalia wrote: Thank you for the feedback on this. I believe that the excel dialect includes just that: class excel(Dialect): delimiter = ',' quotechar = '"' doublequote = True skipinitialspace = False lineterminator = '\r\n' quoting = QUOTE_MINIMAL On Sat, Jan 9, 2016 at 5:23 PM, Karim <kliat...@gmail.com <mailto:kliat...@gmail.com>> wrote: On 09/01/2016 21:54, kbtyo wrote: My specs: Python 3.4.3 Windows 7 IDE is Jupyter Notebooks What I have referenced: 1) http://stackoverflow.com/questions/1546717/python-escaping-strings-for-use-in-xml 2) http://stackoverflow.com/questions/7802418/how-to-properly-escape-single-and-double-quotes 3)http://stackoverflow.com/questions/4972210/escaping-characters-in-a-xml-file-with-python Here is the data (in CSV format) and script, respectively, (I have tried variations on serializing Column 'E' using both Sax and ElementTree): i) A,B,C,D,E,F,G,H,I,J "3","8","1","2312285SChecking10","',0001,0070,","1967-12-25 22:18:13.471000","2005-12-25 22:18:13.768000","2","70","0" ii) #!/usr/bin/python # -*- coding: utf-8 -*- import os.path import sys import csv from io import StringIO import xml.etree.cElementTree as ElementTree from xml.etree.ElementTree import XMLParser import xml import xml.sax from xml.sax import ContentHandler class MyHandler(xml.sax.handler.ContentHandler): def __init__(self): self._charBuffer = [] self._result = [] def _getCharacterData(self): data = ''.join(self._charBuffer).strip() self._charBuffer = [] return data.strip() #remove strip() if whitespace is important def parse(self, f): xml.sax.parse(f, self) return self._result def characters(self, data): self._charBuffer.append(data) def startElement(self, name, attrs): if name == 'Response': self._result.append({}) def endElement(self, name): if not name == 'Response': self._result[-1][name] = self._getCharacterData() def read_data(path): with open(path, 'rU', encoding='utf-8') as data: reader = csv.DictReader(data, delimiter =',', quotechar="'", skipinitialspace=True) for row in reader: yield row if __name__ == "__main__": empty = '' Response = 'sample.csv' for idx, row in enumerate(read_data(Response)): if idx > 10: break data = row['E'] print(data) # The before data = data[1:-1] data = ""'{}'"".format(data) print(data) # Sanity check # data = '',0001,0070,' try: root = ElementTree.XML(data) # print(root) except StopIteration: raise pass # xmlstring = StringIO(data) # print(xmlstring) # Handler = MyHandler().parse(xmlstring) Specifically, due to the quoting in the CSV file (which is beyond my control), I have had to resort to slicing the string (line 51) and then formatting it (line 52). However the print out from the above attempt is as follows: "' File "", line unknown ParseError: no element found: line 1, column 69 Interestingly - if I assign the variable "data" (as in line 54) I receive this: File "", line 56 data = '',0001,0070,' ^ SyntaxError: invalid token I seek feedback and information on how to address utilizing the most Pythonic means to do so. Ideally, is there a method that can leverage ElementTree. Thank you, in advance, for your feedback and guidance. In fact to get rid of double quote simply create your csv reader like that: reader = csv.DictReader(data, dialect='excel', skipinitialspace=True) You should then don't need to slice data variable and reformat it. Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie: Check first two non-whitespace characters
On 01/01/2016 00:25, Mark Lawrence wrote: On 31/12/2015 18:54, Karim wrote: On 31/12/2015 19:18, otaksoftspamt...@gmail.com wrote: I need to check a string over which I have no control for the first 2 non-white space characters (which should be '[{'). The string would ideally be: '[{...' but could also be something like ' [ { '. Best to use re and how? Something else? Use pyparsing it is straight forward: >>> from pyparsing import Suppress, restOfLine >>> mystring = Suppress('[') + Suppress('{') + restOfLine >>> result = mystring.parse(' [ { I am learning pyparsing' ) >>> print result.asList() [' I am learning pyparsing'] You'll get your string inside the list. Hope this help see pyparsing doc for in depth study. Karim Congratulations for writing up one of the most overengineered pile of cobblers I've ever seen. You welcome ! The intent was to make a simple introduction to pyparsing which is a powerful tool for more complex parser build. Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie: Check first two non-whitespace characters
On 31/12/2015 19:54, Karim wrote: On 31/12/2015 19:18, otaksoftspamt...@gmail.com wrote: I need to check a string over which I have no control for the first 2 non-white space characters (which should be '[{'). The string would ideally be: '[{...' but could also be something like ' [ { '. Best to use re and how? Something else? Use pyparsing it is straight forward: >>> from pyparsing import Suppress, restOfLine >>> mystring = Suppress('[') + Suppress('{') + restOfLine >>> result = mystring.parse(' [ { I am learning pyparsing' ) >>> print result.asList() [' I am learning pyparsing'] You'll get your string inside the list. Hope this help see pyparsing doc for in depth study. Karim Sorry the method to parse a string is parseString not parse, please replace by this line: >>> result = mystring.parseString(' [ { I am learning pyparsing' ) Regards -- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie: Check first two non-whitespace characters
On 31/12/2015 19:18, otaksoftspamt...@gmail.com wrote: I need to check a string over which I have no control for the first 2 non-white space characters (which should be '[{'). The string would ideally be: '[{...' but could also be something like ' [ { '. Best to use re and how? Something else? Use pyparsing it is straight forward: >>> from pyparsing import Suppress, restOfLine >>> mystring = Suppress('[') + Suppress('{') + restOfLine >>> result = mystring.parse(' [ { I am learning pyparsing' ) >>> print result.asList() [' I am learning pyparsing'] You'll get your string inside the list. Hope this help see pyparsing doc for in depth study. Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Path problems when I am in bash
On 30/12/2015 00:21, xeon Mailinglist wrote: I have my source code inside the directory `medusa`, and my unit tests inside `tests` dir. Both dirs are inside `medusa-2.0` dir. Here is my file structure [1]. When I run my tests inside pycharm, everything works fine, but when I try to run my unit tests inside in the prompt [2], the `scheduler/predictionranking.py` can't find the `hdfs` import [3]. I have set my environment inside the `medusa-2.0` dir with the virtualenv, I even have set `medusa-2.0` in the PYTHON_PATH [4]. The error that I have is in [5]. The question is, why the import is not being done correctly? [1] My file structure medusa-2.0$ medusa (source code) hdfs.py scheduler (dir with my schedulers) predictionranking.py tests (my unit tests) [2] I run the unit test like this. medusa-2.0$ python -v tests/testSimpleRun.py [3] The header in `predictionranking.py` import hdfs def get_prediction_metrics(clusters, pinput): """ :param pinput (list) list of input paths """ input_size = hdfs.get_total_size(pinput) [4] My python path export MEDUSA_HOME=$HOME/repositories/git/medusa-2.0 export PYTHONPATH=${PYTHONPATH}:${MEDUSA_HOME}/medusa [5] error that I have medusa-2.0$ python -v tests/testSimpleRun.py # /home/xeon/repositories/git/medusa-2.0/medusa/local.pyc matches /home/xeon/repositories/git/medusa-2.0/medusa/local.py import medusa.local # precompiled from /home/xeon/repositories/git/medusa-2.0/medusa/local.pyc # /home/xeon/repositories/git/medusa-2.0/medusa/ranking.pyc matches /home/xeon/repositories/git/medusa-2.0/medusa/ranking.py import medusa.ranking # precompiled from /home/xeon/repositories/git/medusa-2.0/medusa/ranking.pyc # /home/xeon/repositories/git/medusa-2.0/medusa/decors.pyc matches /home/xeon/repositories/git/medusa-2.0/medusa/decors.py import medusa.decors # precompiled from /home/xeon/repositories/git/medusa-2.0/medusa/decors.pyc # /home/xeon/repositories/git/medusa-2.0/medusa/settings.pyc matches /home/xeon/repositories/git/medusa-2.0/medusa/settings.py import medusa.settings # precompiled from /home/xeon/repositories/git/medusa-2.0/medusa/settings.pyc import medusa.scheduler # directory /home/xeon/repositories/git/medusa-2.0/medusa/scheduler # /home/xeon/repositories/git/medusa-2.0/medusa/scheduler/__init__.pyc matches /home/xeon/repositories/git/medusa-2.0/medusa/scheduler/__init__.py import medusa.scheduler # precompiled from /home/xeon/repositories/git/medusa-2.0/medusa/scheduler/__init__.pyc # /home/xeon/repositories/git/medusa-2.0/medusa/scheduler/predictionranking.pyc matches /home/xeon/repositories/git/medusa-2.0/medusa/scheduler/predictionranking.py import medusa.scheduler.predictionranking # precompiled from /home/xeon/repositories/git/medusa-2.0/medusa/scheduler/predictionranking.pyc Traceback (most recent call last): File "tests/simpleRun.py", line 4, in from medusa.simplealgorithm import run_simple_execution File "/home/xeon/repositories/git/medusa-2.0/medusa/simplealgorithm.py", line 8, in import accidentalfaults File "/home/xeon/repositories/git/medusa-2.0/medusa/accidentalfaults.py", line 5, in import hdfs File "/home/xeon/repositories/git/medusa-2.0/medusa/hdfs.py", line 6, in from system import execute_command File "/home/xeon/repositories/git/medusa-2.0/medusa/system.py", line 10, in from ranking import rank_clusters File "/home/xeon/repositories/git/medusa-2.0/medusa/ranking.py", line 9, in from scheduler.predictionranking import get_prediction_metrics File "/home/xeon/repositories/git/medusa-2.0/medusa/scheduler/predictionranking.py", line 6, in import hdfs Hello, Can you try to set your PYTHONPATH like that: export PYTHONPATH=${PYTHONPATH}:${MEDUSA_HOME} Regards Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Olivier Adret
On 06/11/2015 11:34, Chris Angelico wrote: On Fri, Nov 6, 2015 at 2:24 AM, olivier <olivier.adre...@gmail.com> wrote: Je vais essayer de programmer en python. On verra bien ! Hello Olivier! This is a mostly English language mailing list/newsgroup, and there aren't many people who are fluent in French. There is, however, a corresponding French newsgroup, fr.comp.lang.python; you may find that you're more comfortable communicating there. My apologies for not responding in French, but I'm not very familiar with it, and all I would do is put my text through an online translation tool. Chris Angelico Comme dit Chris, cette mailing liste est en anglais et tu peux aller sur le news group fr.comp.lang.python pour du support en français. Mais je suppose que tu avais compris mais au cas où. Cordialement Karim -- https://mail.python.org/mailman/listinfo/python-list
Re: Optimal solution for coloring logging output
On 03/08/2015 14:47, Jean-Michel Pichavant wrote: te a logging Formatter that will re Thank you Jean-Michel useful example K -- https://mail.python.org/mailman/listinfo/python-list
Re: xlrd 0.9.4 released!
On 15/07/2015 08:27, Chris Withers wrote: Hi All, I'm pleased to announce the release of xlrd 0.9.4: http://pypi.python.org/pypi/xlrd/0.9.4 This release includes the following changes: - Automated tests are now run on Python 3.4 - Use ElementTree.iter() if available, not deprecated getiterator() when parsing xlsx files. - Fix #106 : Exception Value: unorderable types: Name() Name() - Create row generator expression with Sheet.get_rows() - Fix for forward slash file separator and lowercase names within xlsx internals. Thanks to the following for their contributions to this release: - Corey Farwell - Jonathan Kamens - Deepak N - Brandon R. Stoner - John McNamara If you find any problems, please ask about them on the python-ex...@googlegroups.com list, or submit an issue on GitHub: https://github.com/python-excel/xlrd/issues Full details of all things Python and Excel related can be found here: http://www.python-excel.org/ cheers, Chris Thank You! -- https://mail.python.org/mailman/listinfo/python-list
Re: Why do Perl programmers make more money than Python programmers
Le 06/05/2013 09:49, Fábio Santos a écrit : have asked for a raise back in December, and was told that it wouldn't happen before this year. I have done well. I think I deserve better pay than a supermarket employee now. I am sure that my efforts were appreciated and I will be rewarded. I am being sarc Austerity for python programmers in Portugal !? ;o) Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: xlrd 0.9.2 released!
Thx ! I will update my 0.6 version! Cheers Karim On 09/04/2013 21:38, Chris Withers wrote: Hi All, I'm pleased to announce the release of xlrd 0.9.2: http://pypi.python.org/pypi/xlrd/0.9.2 This release includes the following changes: - Fix some packaging issues that meant docs and examples were missing from the tarball. - Fixed a small but serious regression that caused problems opening .xlsx files. If you find any problems, please ask about them on the python-ex...@googlegroups.com list, or submit an issue on GitHub: https://github.com/python-excel/xlrd/issues Full details of all things Python and Excel related can be found here: http://www.python-excel.org/ cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Free book, Hacking Secret Ciphers with Python
On 20/04/2013 08:37, asweig...@gmail.com wrote: I've released my third book, Hacking Secret Ciphers with Python for free under a Creative Commons license. This book is aimed at people who have no experience programming or with cryptography. The book goes through writing Python programs that not only implement several ciphers but also can hack these ciphers. Each chapter presents a new program and explains how the source code works. You can download the book from http://inventwithpython.com/hacking 100% of the proceeds from the book sales will be donated to the Electronic Frontier Foundation, Creative Commons, and The Tor Project. Very good book! 1000 Thanks for your unvaluable work. By the way who is Aaron? Karim France -- http://mail.python.org/mailman/listinfo/python-list
Re: equivalent to C pointer
Hello, There is no such notion in python. But the closest are iterators and generator functions. Cheers Karim On 18/04/2013 19:06, abdelkader belahcene wrote: Hi everybody, I am new to python and I am discovering it. I know C well, and want to know if python knows how to manage Pointers like pointer to function here is a C example how to write it in python Intergration with trapeze method When we write Trapeze ( at the compilation level) we don't know which functions Fonc to handle. Here for example we use sin and a user defined F1 The program is attached too #include stdio.h #include math.h double F1 (double x){ return x*x; } double Trapeze(double Fonc(double ), double left, double right, double step){ double X1, X0, Y0, Y1, Z = 0; for(X0=left; X0 right ; X0 = X0 + step) { X1 = X0 + step; Y1 = Fonc(X1);Y0 = Fonc(X0); Z += (Y1 + Y0) * step * 0.5; } return Z; } int main(){ double y; y=Trapeze(sin, -2.5, 3.2, 0.1); printf(\n\tValue for sin is : \t %8.3lf , y); y=Trapeze(F1, 0, 3, 0.1); printf(\n\tValue for F1 is : \t %8.3lf , y); return 0; } /** Value for sin is : 0.197 Value for F1 is : 9.005 */ thanks a lot -- http://mail.python.org/mailman/listinfo/python-list
Re: Screencast: Creating and deploying an advanced python web application in under 15 minutes!
On 29/03/2013 14:53, timothy crosley wrote: I apologize for the audio from the original screen cast, it was really sub-par. I bought a new microphone and re-recorded it: http://www.youtube.com/watch?v=0L8TsmrZPLgfeature=youtu.be Thanks! Timothy On Tuesday, March 26, 2013 4:54:15 AM UTC-4, timothy crosley wrote: Hi, I've created a screen cast showing how a message board with live-validation and Ajax calls written in python can be built and deployed in under 15 minutes. You can view it here: http://www.youtube.com/watch?v=ucougrZK9wI I hope some of you find it useful, Thanks! Timothy Hi Timothy, Very interesting! Thx a lot! Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: tracer une cercle dans python tkinter?
On 01/03/2013 19:10, Vincent Vande Vyvre wrote: Le 01/03/13 18:53, olsr.ka...@gmail.com a écrit : comment tracer une cercle contient un numéro au un symbole dans le center dans python tkinter? Posez votre question ici: http://www.developpez.net/forums/f96/autres-langages/python-zope/ Heuu en anglais vous toucherez plus de monde. K -- http://mail.python.org/mailman/listinfo/python-list
Re: please i need explanation
On 11/01/2013 16:35, kwakukwat...@gmail.com wrote: def factorial(n): if n2: return 1 f = 1 while n= 2: f *= n f -= 1 return f What explanation this a function representing the math factorial. You provide a parameter n: if n est lower than 2 the factorial is 1 (return by the function). in other case you multiply previous factoriel value by n (f *= n = f = f *n). And you decrement n by 1 (f -=1 = f = f - 1). This gives n*(n-)*(n-2) general formula for factorial. Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: please i need explanation
On 11/01/2013 17:33, kwakukwat...@gmail.com wrote: -Original Message- From: K. Elo Sent: Friday, January 11, 2013 3:56 AM To: python-list@python.org Subject: Re: please i need explanation Hi! Since there is no stated question, I need to guess: n -= 1 (instead of f -= 1) should work. Or maybe the question was a totally different one... -Kimmo 11.01.2013 17:35, kwakukwat...@gmail.com wrote: def factorial(n): if n2: return 1 f = 1 while n= 2: f *= n f -= 1 return f please it works.but don’t get why the return 1 and the code below. factorial or n! = n*(n-1)*(n-2) for n 1. For n = 0, 1 the factorial is 1 the reason for the 'return 1'. f *= n = f = f* n f -= 1 = f = f - 1 but sure this is not correct as Kimmo said n = n - 1 or n -= 1 Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: [Tutor] How to run multiline shell command within python
On 10/01/2013 09:31, Hugo Arts wrote: On Thu, Jan 10, 2013 at 7:01 AM, Karim kliat...@gmail.com mailto:kliat...@gmail.com wrote: Hello all, I want to run multiline shell command within python without using a command file but directly execute several lines of shell. I already use *subprocess.checkoutput(csh -f my_file.csh.split())* but I want to know if it is posssible to avoid making file and execute shell lines of code directly. Yes, this is very possible. Specify shell=True as an argument and you can do anything you can do in a shell: commands = echo hello ... echo hello | wc -l ... ps aux | grep python b = subprocess.check_output(commands, shell=True) print(b.decode('ascii')) hello 1 hugo 1255 1.0 0.6 777316 49924 ?Sl 09:14 0:08 /usr/bin/python2 /usr/bi hugo 6529 0.0 0.0 42408 7196 pts/0S+ 09:23 0:00 python hugo 6559 0.0 0.0 10656 1128 pts/0S+ 09:28 0:00 grep python watch out though, accepting user input into the commands variable will lead to shell injection, which can be a dangerous security vulnerability. HTH, Hugo Many thanks Hugo. It makes my day! In my case there are no possibilities for shell injection. It is internal to a class. Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: [Tutor] How to run multiline shell command within python
On 10/01/2013 16:21, Matty Sarro wrote: Have you looked a the pexpect class? It works like gangbusters, especially if you're trying to run something with an interactive shell. http://www.noah.org/wiki/pexpect On Thu, Jan 10, 2013 at 9:25 AM, Karim kliat...@gmail.com mailto:kliat...@gmail.com wrote: On 10/01/2013 09:31, Hugo Arts wrote: On Thu, Jan 10, 2013 at 7:01 AM, Karim kliat...@gmail.com mailto:kliat...@gmail.com wrote: Hello all, I want to run multiline shell command within python without using a command file but directly execute several lines of shell. I already use *subprocess.checkoutput(csh -f my_file.csh.split())* but I want to know if it is posssible to avoid making file and execute shell lines of code directly. Yes, this is very possible. Specify shell=True as an argument and you can do anything you can do in a shell: commands = echo hello ... echo hello | wc -l ... ps aux | grep python b = subprocess.check_output(commands, shell=True) print(b.decode('ascii')) hello 1 hugo 1255 1.0 0.6 777316 49924 ? Sl 09:14 0:08 /usr/bin/python2 /usr/bi hugo 6529 0.0 0.0 42408 7196 pts/0S+ 09:23 0:00 python hugo 6559 0.0 0.0 10656 1128 pts/0S+ 09:28 0:00 grep python watch out though, accepting user input into the commands variable will lead to shell injection, which can be a dangerous security vulnerability. HTH, Hugo Many thanks Hugo. It makes my day! In my case there are no possibilities for shell injection. It is internal to a class. Regards Karim -- http://mail.python.org/mailman/listinfo/python-list Thanks Matty! I will have a look specially for interactive session. Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
How to run multiline shell command within python
Hello all, I want to run multiline shell command within python without using a command file but directly execute several lines of shell. I already use *subprocess.checkoutput(csh -f my_file.csh.split())* but I want to know if it is posssible to avoid making file and execute shell lines of code directly. Example: catEOF myfile echo foo echo bar ... EOF Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: my email
Le 18/07/2012 04:34, Simon Cropper a écrit : On 18/07/12 11:44, Maria Hanna Carmela Dionisio wrote: mmdionisio1...@yahoo.com.ph Just a newbhie here : [snip] You must know your password to change your options (including changing the password, itself) or to unsubscribe. It is: sweet103093 I suggest you change you password now that you have told the world what it is. ?? -- http://mail.python.org/mailman/listinfo/python-list
Re: Which way is best to execute a Python script in Excel?
Le 05/07/2012 09:22, Maurizio Spadaccino a écrit : Hi all I'm new to Python but soon after a few days of studying its features I find it my favourite mean of programming scripts to allow for data storing and mining. My idea would be to inplement python scripts from inside an excel sheet that would store and fetch data from a Mysql database. So i need the script to be launched, say, by pressing a button in excel and, for instance, retrieve immediately data from the mysql table. For what I found in the net, it seems there are three ways to control python from excel: 1) run my python script via shell using the Run command in VBA, which seems to me the easiest (but maybe slower?) way; 2) creating a COM server, for which I need more training since it doesnt appear that easy; 3) via Microsoft Script Control, for which I saw some example around where It looks like I can 'simulate' a python shell via the 'executeStatement' command. What would you suggest would be the more performing way to achieve my goals as described above? Thanks for you help Maurizio -- Maurizio www.mauriziospadaccino.com http://www.mauriziospadaccino.com --- If you can avoid printing this e-mail, you are only doing good for our planet. Look at PyUNO from OpenOffice very large API: http://www.openoffice.org/api/docs I use to create all my documention (Excell, Writer, etc...) on this API. Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Which way is best to execute a Python script in Excel?
Le 05/07/2012 23:20, Terry Reedy a écrit : On 7/5/2012 5:12 AM, Thomas Jollans wrote: On 07/05/2012 09:26 AM, Karim wrote: Look at PyUNO from OpenOffice very large API: http://www.openoffice.org/api/docs I use to create all my documention (Excell, Writer, etc...) on this API. Note that this API is for OpenOffice, not Microsoft Excel. However, as you probably know, you can open most Excel files in OpenOffice's Calc. I urge you to consider LibreOffice, a fork of OpenOffice that is now broadly considered its successor. Its API can also be used in Python http://api.libreoffice.org/ Can you explain or point to a document that explains how to actually do that? (use the LibreOffice api from Python?) The only mention of Python on that page is under examples. (and there is no mention of python in the installation guide, even as an option, nor in the development tools doc, ). On the example page, there is only one example and unlike all the other sections, no 'Additional information' linking to a 'Python Language binding'. In toolpanel.py of the example, there is import uno import unohelper from com.sun.star.ui import XUIElementFactory from com.sun.star.ui import XUIElement from com.sun.star.ui.UIElementType import TOOLPANEL as unoTOOLPANEL from com.sun.star.ui import XToolPanel but where are the python-importable modules and com package and their docs and how does one get them? There are not in the example directory. The IDL reference only covers the com.sun.star stuff. --- Looking with Google, I see that some linux distros include LibreOffice-PyUno in thier package managers. Not helpful for Windows. The LibreOffice installation includes a python2.6.1 installation under LO.../program/ with pyuno pre-installed. No doc that I could see. However, when running it import com.sun.star.ui Traceback (most recent call last): File stdin, line 1, in module ImportError: No module named com.sun.star.ui Oh, there is a trick to it import uno import unohelper import com.sun.star.ui Traceback (most recent call last): File stdin, line 1, in module File uno.py, line 263, in _uno_import return _g_delegatee( name, *optargs, **kwargs ) ImportError: No module named com.sun.star.ui from com.sun.star.ui import XToolPanel So import uno and unohelper and one can import objects from the non-existent com.sun.star.ui module. Still, a lot more is needed to understand even the example. Hi Terry, You are right it was the warrior path at the beginning to be able to produce some code but it is working pretty well. I add to gather many pieces and examples. In fact, you should take the OOo Basic langage example (API) and convert it (sometimes for python some cases have been changed but mainly you have to use ones from original API namely OOo Basic language). They only did python wrapper around the native API. An excellent link to derived all code example to python: http://www.pitonyak.org/AndrewMacro.sxw. On latest ubuntu you get no error if you are importing: import uno import unohelper BUT IN OTHER ENVIRONMENT if the version of python is different from the one embedded inside OpenOffice (LibreOffice) namely 2.6.X You are obliged to recompile a OOo version with you python version. BUT there is a way to hack this. I did it, just do the following: LD_LIBRARY_PATH=:/usr/lib/openoffice.org/ure/lib:/usr/lib/openoffice.org/basis3.3/program PYTHONPATH=:/usr/lib/openoffice.org3/basis-link/program/ UNO_PATH=/usr/lib/openoffice.org/basis3.3/program URE_BOOTSTRAP=vnd.sun.star.pathname:/usr/lib/openoffice.org/basis3.3/program/fundamentalbasisrc Jut set these env variables according to your OOo installation and you will get no error by importing uno and unohelper. One article to begin with: http://www.linuxjournal.com/content/starting-stopping-and-connecting-openoffice-python And to finish basic code to get started and some functions I adapted from Andrew 's Macro book in python to search string and update a table inside writer document (work or MSW ord or SW format): Start up code --- import uno import unohelper # 1. Get the uno component context from the PyUNO runtime localContext = uno.getComponentContext() # 2. Create the UnoUrlResolver resolver = localContext.ServiceManager.createInstanceWithContext(com.sun.star.bridge.UnoUrlResolver, localContext ) # 3. Get the central desktop object smgr = resolver.resolve( uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager ) # 4. Declare the ServiceManager remoteContext = smgr.getPropertyValue( DefaultContext ) # 5. Get the central desktop object desktop = smgr.createInstanceWithContext( com.sun.star.frame.Desktop,remoteContext) !!! Document text creation !!! # open a writer document #doc = desktop.loadComponentFromURL('private:factory/swriter','_blank', 0, ()) #document
Re: Which way is best to execute a Python script in Excel?
Le 06/07/2012 07:09, Terry Reedy a écrit : On 7/5/2012 10:30 PM, Karim wrote: An excellent link to derived all code example to python: http://www.pitonyak.org/AndrewMacro.sxw. Even though he only writes in OOBasic, you are right that he explains the basic concepts needed for accessing the api from any language. He is also honest. Writing non-api code is relatively easy; accessing the OO/LO api is harder. I made a start. When I get further, I will look at the examples that are close to some things I want to do. I will also study your Python examples. Thanks for the help. Terry NP you are welcome to ask me any question. Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Py330b1, un café crème sucré
Euhhh, j'ai pas tout suivi. C'est quoi la feinte? Cheers Karim Le 27/06/2012 21:49, wxjmfa...@gmail.com a écrit : # -*- coding: cp1252 -*- # café.py import sys print(sys.version) sys.path.append('d:\\crème') import crème import sucré s = ' '.join(['un', 'café', crème.tag, sucré.tag]) print(s) input(':') #-- # .\sucré.py: # -*- coding: cp1252 -*- #tag = 'sucré' #-- # d:\crème\crème.py # -*- coding: cp1252 -*- #tag = 'crème' # output # 3.3.0b1 (v3.3.0b1:e15c554cd43e+, Jun 26 2012, 20:30:00) [MSC v.1600 32 bit (Intel)] # un café crème sucré # : # :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: python with xl
Le 11/06/2012 16:12, chebrian a écrit : Hi, How to append the list of data in individual column of XL file, every time from python script . In standard lib = module csv (ascii comma separated values) In non standard = binary xl = module xlrd for reading and module xlwt for writing (http://www.python-excel.org/) Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: ANNOUNCE: byteformat0.2a
Cool steven, very helpful I have a db which holds units data it will avoids me to do such format conversion. Thnx Cheers Karim Le 15/05/2012 10:30, Steven D'Aprano a écrit : I am pleased to announce a new release of byteformat. http://pypi.python.org/pypi/byteformat byteformat is a Python module for intelligently formatting numbers of bytes using common human-readable strings: from byteformat import format format(12000) '12 KB' format(510, style='ABBREV') '5.1 Mbytes' format(485000, style='LONG') '48.5 terabytes' byteformat understands SI decimal units, IEC binary units, and mixed units: format(12000, scheme='IEC') '11.7 KiB' format(12000, scheme='MIXED') '11.7 KB' You can also specify which prefix to use: format(485000, style='LONG', prefix='M') '4850 megabytes' byteformat can be used as a command-line tool: [steve@ando ~]$ python -m byteformat --prefix=K 1000 12300 145000 1 KB 12.3 KB 145 KB byteformat understands all the relevant SI and IEC unit prefixes, and is released under the MIT licence. -- http://mail.python.org/mailman/listinfo/python-list
Re: xlrd 0.7.6 released!
Le 07/04/2012 04:07, Keith Medcalf a écrit : Karimkliat...@gmail.com wrote in news:mailman.1309.1333529851.3037.python-l...@python.org: This release manage the '.xlsx' format? http://packages.python.org/openpyxl/ Ah Keith, I just check this is only for xlsx format so I will make 2 code branches so I don't have to modify the xlrd one for xls binary file. I will use both in the same program. Many thanks Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: xlrd 0.7.6 released!
Le 04/04/2012 09:24, Chris Withers a écrit : And the goat sacrifice continues... On 03/04/2012 08:34, Chris Withers wrote: On 03/04/2012 08:04, Chris Withers wrote: I'm pleased to announce the release of xlrd 0.7.4. I've just release a 0.7.5 that fixes this. Except it didn't, I've just released 0.7.6, which will hopefully bring an end to the brown-bagging. Any more problems, please let the python-ex...@googlegroups.com mailing list know... cheers, Chris Hello, This release manage the '.xlsx' format? Thanks Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a ConfigParser which keeps comments
Le 15/03/2012 03:48, Steven W. Orr a écrit : On 3/14/2012 6:07 AM, Gelonida N wrote: Hi, At the moment I use ConfigParser http://docs.python.org/library/configparser.html for one of my applications. Now I'm looking for a library, which behaves like config parser, but with one minor difference. The write() mehtod should keep existing comments. Does anybody know or implement something like this or is there as switrch, that I overlooked in hte documentaiton. I use ConfigObj. Sure configObj is a must...I use it too. http://www.voidspace.org.uk/python/configobj.html Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: decompilation
for i in range(m): guess, availableLetters = getNewLetterGuess(availableLetters) wordFamilyCounter, familyIndexList = getWordFamilyCounter(L, n, guess) bestFamily = wordFamilyCounter.index(max(wordFamilyCounter)) if bestFamily == 0: print('Letter not in word.') else: print('Letter is in word!!!') L = extractLargestFamily(L, familyIndexList, wordFamilyCounter) patternList = updatePatternList(patternList, guess, bestFamily) print(''.join(patternList)) if '_ ' not in patternList: break if '_ ' not in patternList: print('SURELY you must be CHEATING, but you guessed my word in ' + str(i + 1) + ' tries!!!') else: bogusWord = pickWordFrom(L) print('You lose. The word was: ' + bogusWord) I haven't actually checked if this code runs :) Great code (unpy3) Arnaud! pretty well built. Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem using s.strip() to remove leading whitespace in .csv file
Le 29/02/2012 09:25, Guillaume Chorn a écrit : Hello All, I have a .csv file that I created by copying and pasting a list of all the players in the NBA with their respective teams and positions (http://sports.yahoo.com/nba/players?type=lastnamefirst=1query=go=GO! http://sports.yahoo.com/nba/players?type=lastnamefirst=1query=go=GO%21). Unfortunately, when I do this I have no choice but to include a single leading whitespace character for each name. I'd like to compare this list of names to another list (also in .csv format but in a different file) using a simple Python script, but in order to do that I need to strip the leading whitespace from all of the names or none of them will match the names in the second list. However, when I try to do this as follows: positions = open('/home/guillaume/Documents/playerpos.csv') for line in positions: info = line.split(',') name = info[0] name = name.strip() print name #to examine the effect of name.strip() I keep getting all of the names with the leading whitespace character NOT removed (for example: Jeff Adrien. Why is this happening? The following is a sample of the .csv file (the one I'm trying to remove the whitespace from) opened in gedit, the built-in Ubuntu text editor: Jeff Adrien,SF,Houston Rockets Arron Afflalo,SG,Denver Nuggets Maurice Ager,GF,Minnesota Timberwolves Blake Ahearn,PG,Los Angeles Clippers Alexis Ajinca,FC,Toronto Raptors Solomon Alabi,C,Toronto Raptors Cole Aldrich,C,Oklahoma City Thunder LaMarcus Aldridge,FC,Portland Trail Blazers Joe Alexander,SF,New Orleans Hornets Lavoy Allen,FC,Philadelphia 76ers Malik Allen,FC,Orlando Magic Ray Allen,SG,Boston Celtics Tony Allen,GF,Memphis Grizzlies Lance Allred,C,Indiana Pacers Rafer Alston,PG,Miami Heat Any help with this seemingly simple but maddening issue would be much appreciated. thanks, Guillaume Use csv module instead of parsing yourself the csv file: import csv reader = csv.Reader(open(file.csv)) for row in reader: for key in row: print(key=, key, value=, row[key]) Code above not tested but should work. Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem using s.strip() to remove leading whitespace in .csv file
Le 29/02/2012 10:01, Karim a écrit : Le 29/02/2012 09:25, Guillaume Chorn a écrit : Hello All, I have a .csv file that I created by copying and pasting a list of all the players in the NBA with their respective teams and positions (http://sports.yahoo.com/nba/players?type=lastnamefirst=1query=go=GO! http://sports.yahoo.com/nba/players?type=lastnamefirst=1query=go=GO%21). Unfortunately, when I do this I have no choice but to include a single leading whitespace character for each name. I'd like to compare this list of names to another list (also in .csv format but in a different file) using a simple Python script, but in order to do that I need to strip the leading whitespace from all of the names or none of them will match the names in the second list. However, when I try to do this as follows: positions = open('/home/guillaume/Documents/playerpos.csv') for line in positions: info = line.split(',') name = info[0] name = name.strip() print name #to examine the effect of name.strip() I keep getting all of the names with the leading whitespace character NOT removed (for example: Jeff Adrien. Why is this happening? The following is a sample of the .csv file (the one I'm trying to remove the whitespace from) opened in gedit, the built-in Ubuntu text editor: Jeff Adrien,SF,Houston Rockets Arron Afflalo,SG,Denver Nuggets Maurice Ager,GF,Minnesota Timberwolves Blake Ahearn,PG,Los Angeles Clippers Alexis Ajinca,FC,Toronto Raptors Solomon Alabi,C,Toronto Raptors Cole Aldrich,C,Oklahoma City Thunder LaMarcus Aldridge,FC,Portland Trail Blazers Joe Alexander,SF,New Orleans Hornets Lavoy Allen,FC,Philadelphia 76ers Malik Allen,FC,Orlando Magic Ray Allen,SG,Boston Celtics Tony Allen,GF,Memphis Grizzlies Lance Allred,C,Indiana Pacers Rafer Alston,PG,Miami Heat Any help with this seemingly simple but maddening issue would be much appreciated. thanks, Guillaume Use csv module instead of parsing yourself the csv file: import csv reader = csv.Reader(open(file.csv)) for row in reader: for key in row: print(key=, key, value=, row[key]) Code above not tested but should work. Cheers Karim Just a correction use a dictionary like csv reader: *reader = csv.DictReader(open(file.csv))* Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: xlwt 0.7.3 released!
Hello chris, Thanks for the greet news! Is there any chance that xlrd read .xlsx format and arrive to decode special unicode instead of firing some unicode Exception? Cheers Karim Le 21/02/2012 10:18, Chris Withers a écrit : Hi All, I'm pleased to announce the release of xlwt 0.7.3. This release is long overdue and has been over 2.5 years in the making! The highlights: - Added user_set and best_fit attributes to Column class. - Fixed an [Errno 0] Error raised when Worksheet.flush_row_data() was called after Workbook.save() - Fixed an error on Windows that occurred when writing large blocks to files. - Added the ability to write rich text cells - Fixed a bug when writing MULBLANK records on big-endian platforms. - allow the active_pane on worksheets to be specified - added support for zoom (magn) factors and improved possibilities when generating split panes For a full list of the changes, please see the svn log: https://secure.simplistix.co.uk/svn/xlwt/trunk I've currently only put up .tar.gz sdists, if anyone requires anything else, please explain why and I'll be happy to add! cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: How to build 64-bit Python on Solaris with GCC?
Le 07/12/2011 12:30, Skip Montanaro a écrit : Does anyone have a recipe for the subject build? I know Solaris is a minority platform these days, but surely someone has tackled this problem, haven't they? Thx, Skip ./configure make make install Karim -- http://mail.python.org/mailman/listinfo/python-list
Template class and class design on concrete example xl2csv writer
Hello All, I have the following code and I am quite satisfied with its design BUT I have the feeling I can do better. Basically the, main() execute the program (I did not put the parsing of arguments function). I am opening an Excel document and writing content in a CSV one w/ different format. The design is an abstract template class XLWriter, and derived 'Xl2Csv', 'Xl2Scsv', 'Xl2text' classes to write the correct ASCII DOCUMENT to correct format. The property hook method file_format is implement in these classes and return an object of type 'XlCsvFileFormat' or 'XlTabFileFormat'. It allows to put the right file extension and delimiter. These class are derived from standard csv.excel and csv.excel_tab. At last a Factory class MakeXlWriter has the job to create the correct writer. For now I did not add a strategy pattern which usually goes with the Template pattern. Except from that all better design or others critics will be welcome. Regards karim ___ from __future__ import print_function import sys, os, argparse, csv, xlrd __all__ = ['main', 'Xl2CsvError', 'XLWriter', 'XlCsvFileFormat', 'XlTabFileFormat', 'Xl2Csv', 'Xl2Scsv', 'Xl2text', 'MakeXlWriter'] class Xl2CsvError(Exception): The exception class to manage the internal program errors. pass class XlWriter(object): Abstract template class. def __init__(self, xlfilename=None, sheets=None): Initializer. if self.__class__.__name__ == 'XlWriter': raise TypeError('Abstract template Class XlWriter could not be instanciated directly!') if not xlfilename: raise Xl2CsvError('Please provide a non empty file name!') else: self._source_name = xlfilename self._book = xlrd.open_workbook(xlfilename) if sheets is not None: if isinstance(sheets[0], int): self._selected_sheets = [self._book.sheet_by_index(sheetnum-1) for sheetnum in sheets] elif isinstance(sheets[0], str): try: self._selected_sheets = [self._book.sheet_by_name(sheetname) for sheetname in sheets] except xlrd.biffh.XLRDError, e: print('{0} in file document {1}'.format(e, xlfilename)) sys.exit(1) else: raise Xl2CsvError('Sheets element type not recognized!') else: self._selected_sheets = self._book.sheets() def write(self): The file extraction public method. for sheet in self._selected_sheets: xlfilename = '{sheet}{ext}'.format(sheet=sheet.name, ext='.'+self.file_format.extension.lower()) try: writer = csv.writer(open(xlfilename, 'wb'), delimiter=self.file_format.delimiter) print(Creating csv file '{file}' for sheet '{sheet}' contained in document {src} format( sheet=sheet.name, file=xlfilename, src=self._source_name), end=' ') for row in xrange(sheet.nrows): writer.writerow(sheet.row_values(row)) print('Done.') except csv.Error, e: print(e) return 1 return 0 @property def file_format(self): Hook method. Need to implement in derived classes. Should return an XLAsciiFileFormat object to get file extension and inner delimiter. pass class XlCsvFileFormat(csv.excel): Add file extension to the usual properties of Excel-generated CSV files. extension = 'CSV' class XlTabFileFormat(csv.excel_tab): Add file extension to the usual properties of Excel-generated TAB delimited files. extension = 'TXT' class Xl2Csv(XlWriter): @property def file_format(self): Hook factory method return XlCsvFileFormat() class Xl2Scsv(XlWriter): @property def file_format(self): Hook factory method _format = XlCsvFileFormat() _format.extension = 'SCSV' _format.delimiter = ';' return _format class Xl2Text(XlWriter): @property def file_format(self): Hook factory method return XlTabFileFormat() class MakeXlWriter(object): Factory class for XLWriter objects. @staticmethod def make(xlfilename=None, sheets=None, extension='CSV'): if extension == TXT: return Xl2Text(xlfilename=xlfilename, sheets=sheets) elif extension == SCSV: return Xl2Scsv(xlfilename=xlfilename, sheets=sheets) elif extension == CSV: return Xl2Csv(xlfilename=xlfilename, sheets=sheets) def main(): Main of this application args = _process_command_line() args.xl.close() writer = MakeXlWriter.make(xlfilename=args.xl.name, sheets=args.sheets, extension=args.ext) return
Please code review.
Hello, I need a generator to create the cellname in a excell (using pyuno) document to assign value to the correct cell. The following code does this but do you have some optimizations on it, for instance to get the alphabetic chars instead of hard-coding it. Cheers karim Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type help, copyright, credits or license for more information. def _xrange_cellnames(rows, cols): ... Internal iterator function to compute excell table cellnames. ... cellnames = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ... for row in xrange(1, rows+1): ... for char in cellnames.replace('', ' ').split()[:cols]: ... yield char + str(row) ... list( _xrange_cellnames(rows=3,cols=4)) ['A1', 'B1', 'C1', 'D1', 'A2', 'B2', 'C2', 'D2', 'A3', 'B3', 'C3', 'D3'] -- http://mail.python.org/mailman/listinfo/python-list
Re: Please code review.
Thanks Paul, I never used string module. In fact, cellnames.split('') gives a syntax error w/ empty string. That's why I use the intermediate replace() which accept that. Cheers Karim On 08/02/2011 02:04 PM, Paul Kölle wrote: Am 02.08.2011 13:45, schrieb Karim: Hello, I need a generator to create the cellname in a excell (using pyuno) document to assign value to the correct cell. The following code does this but do you have some optimizations on it, for instance to get the alphabetic chars instead of hard-coding it. you can use: import string cellnames = string.ascii_uppercase not sure why you need the .replace().split() stuff... def _xrange_cellnames(rows, cols): cellnames = string.ascii_uppercase for row in xrange(1, rows+1): for char in cellnames[:rows]: yield char + str(row) cheers Paul Cheers karim Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type help, copyright, credits or license for more information. def _xrange_cellnames(rows, cols): ... Internal iterator function to compute excell table cellnames. ... cellnames = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ... for row in xrange(1, rows+1): ... for char in cellnames.replace('', ' ').split()[:cols]: ... yield char + str(row) ... list( _xrange_cellnames(rows=3,cols=4)) ['A1', 'B1', 'C1', 'D1', 'A2', 'B2', 'C2', 'D2', 'A3', 'B3', 'C3', 'D3'] -- http://mail.python.org/mailman/listinfo/python-list
Re: Please code review.
Thanks Chris! It seems I am blind I should have seen it... In fact I started with the need (imaginary) to use enumerate() to get some indices but ended in a more simple code. Indeed, your's is simpler. For the double chars extension I will see if I need it in the future. Cheers Karim On 08/02/2011 02:07 PM, Chris Angelico wrote: On Tue, Aug 2, 2011 at 12:45 PM, Karimkliat...@gmail.com wrote: ... for char in cellnames.replace('', ' ').split()[:cols]: for char in cellnames[:cols]: Strings are iterable over their characters. Alternatively, you could use chr and ord, but it's probably cleaner and simpler to have the string there. It also automatically and implicitly caps your columns at 26. On the other hand, if you want to support more than 26 columns, you may want to make your own generator function to yield 'A', 'B',... 'Z', 'AA', 'AB', etc. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Please code review.
On 08/02/2011 02:27 PM, Peter Otten wrote: Karim wrote: I need a generator to create the cellname in a excell (using pyuno) document to assign value to the correct cell. Isn't there a way to use a (row, column) tuple directly? If so I'd prefer that. Also, there used to be an alternative format to address a spreadsheet cell with something like R1C2. In fact, in pyuno I get the following code: values = ( (22.5,21.5,121.5), (5615.3,615.3,-615.3), (-2315.7,315.7,415.7) ) table.getCellByName(A2).setValue(22.5) table.getCellByName(B2).setValue(5615.3) table.getCellByName(C2).setValue(-2315.7) Indeed the values tuple is formated like (row, column). I want to write simply and get cellname ondemand via an iterator function like that: values = ( (22.5,21.5,121.5), (5615.3,615.3,-615.3), (-2315.7,315.7,415.7) ) it = _xrange_cellnames(rows=len(value), cols=len(values[0])) table.getCellByName(it.next()).setValue(22.5) table.getCellByName(it.next()).setValue(5615.3) table.getCellByName(it.next()).setValue(-2315.7) The following code does this but do you have some optimizations on it, for instance to get the alphabetic chars instead of hard-coding it. Cheers karim Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type help, copyright, credits or license for more information. def _xrange_cellnames(rows, cols): ... Internal iterator function to compute excell table cellnames. ... cellnames = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ... for row in xrange(1, rows+1): ... for char in cellnames.replace('', ' ').split()[:cols]: That is interesting ;) But for maximum clarity use for char in cellnames[:cols]: instead. Yes I am blind ;o) I did not see simplification. Simple is better than complicate... ... yield char + str(row) ... list( _xrange_cellnames(rows=3,cols=4)) ['A1', 'B1', 'C1', 'D1', 'A2', 'B2', 'C2', 'D2', 'A3', 'B3', 'C3', 'D3'] Here's my (untested) attempt to handle columns beyond Z: from itertools import chain, count, imap, islice, product from string import ascii_uppercase def columnnames(): alpha = (ascii_uppercase,) return imap(.join, chain.from_iterable(product(*alpha*i) for i in count(1))) def cellnames(columns, rows): for row in xrange(1, rows+1): for column in islice(columnnames(), columns): yield column + str(row) if __name__ == __main__: import sys print list(cellnames(*map(int, sys.argv[1:]))) I think the subject has come up before; goo^h^h^h the search engine of your choice is your friend. I will study this one and itertools modules, many thanks. Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Please code review.
Thanks Martin, This is the generator expression version. I can use both function generator or generator expression version correction. Cheers Karim On 08/02/2011 02:47 PM, Martin Gracik wrote: def get_cellnames2(rows, cols): rows = range(1, rows + 1) cols = string.ascii_uppercase[:cols] return ('%s%s' % (col, row) for row in rows for col in cols) -- http://mail.python.org/mailman/listinfo/python-list
Re: Please code review.
On 08/02/2011 03:59 PM, Peter Otten wrote: Karim wrote: values = ( (22.5,21.5,121.5), (5615.3,615.3,-615.3), (-2315.7,315.7,415.7) ) it = _xrange_cellnames(rows=len(value), cols=len(values[0])) table.getCellByName(it.next()).setValue(22.5) table.getCellByName(it.next()).setValue(5615.3) table.getCellByName(it.next()).setValue(-2315.7) Some googling suggests that there exists a getCellByPosition() method. With that the above would become (untested): for x, column in enumerate(values): for y, value in enumerate(column): table.getCellByPosition(x, y).setValue(value) Thanks for the tip I will check com.sun.star.text.TextTable API. Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: shlex parsing
On 07/29/2011 09:24 AM, Web Dreamer wrote: Nobody a écrit ce jeudi 28 juillet 2011 18:37 dans pan.2011.07.28.16.37.35.287...@nowhere.com : On Thu, 28 Jul 2011 17:48:34 +0200, Web Dreamer wrote: I would like to parse this TCL command line with shlex: '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG' s = s.replace('[','[') s = s.replace(']',']') Note that this approach won't work if you have nested brackets or braces. That would require a real parser. True, I tried with the shlex class, but adding '[]' to a shlexobject.quotes doesn't work. Indeed, shlex expects an opening and closing quote to be the same, and [ is different from ] so shlex therefore expects an opening [ to be closed with [ and not ] My solution indeed only works as long as there are not any nested brackets. Yeah I saw that shlex object behavior is slighty different from shlex.split() fonction. the char minus by default is taken as a individual word and in split it is not. get_token() method return '-' alone and you have to set object attributes wordchars += '-' to have the same behavior as shlex.split(). Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Is it bad practise to write __all__ like that
On 07/29/2011 08:37 AM, Thomas Rachel wrote: Am 28.07.2011 20:01 schrieb Ian Kelly: The advantage of Thomas's decorator here is that it lets you place the denotation of whether a function is exported alongside its definition, whereas simply declaring the __all__ list forces you to separate them. It also avoids the problem of possibly mistyping the function's name in the list. Thank you. For the ones who do not like @__all__ because it might look ugly, here is an alternative: __all__ = [] def export(obj): __all__.append(obj.__name__) return obj It is nearly the same, but without an extra class and with the more readable @export def func(): pass Thomas I did not plan that this topic introduce so many great ideas. It started from a simple question and ended up with new and beautiful solution. Very pleased of the collective spirit of this mailing list. It changed from non-sense verbal fightings I saw earlier in some discussions like perl versus python related topic. Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: shlex parsing
On 07/29/2011 03:42 PM, Web Dreamer wrote: whitespace_split = True Thanks for the tip! Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Is it bad practise to write __all__ like that
Hello, __all__ = 'api db input output tcl'.split() or __all__ = api db input output tcl .split() for lazy boy ;o). It is readable as well. What do you think? Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Is it bad practise to write __all__ like that
On 07/28/2011 02:29 PM, Thomas Rachel wrote: __all__ = AllList() Hello Thomas, Very beautiful and elegant code. Having both at the same time an instance and a method... With this 'small' topic, you taught me something today on property application! Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: shlex parsing
Hello You have you feet on earth Web Dreamer! Very clever! Beautiful hack! Many Thanks Karim On 07/28/2011 05:48 PM, Web Dreamer wrote: Karim a écrit ce mercredi 27 juillet 2011 21:30 dansmailman.1538.1311795072.1164.python-l...@python.org : Hello All, I would like to parse this TCL command line with shlex: '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG' And I want to get the splitted list: ['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG'] Then I will gather in tuple 2 by 2 the arguments. Do this: s = '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG' Now if you don't enclose [get_rule A1 B2] in cotes, you will pull your hair off! So: s = s.replace('[','[') s = s.replace(']',']') Now: s '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG' Lets continue: import shlex optionlist = shlex.split(s) optionlist ['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG'] Now to get your tupple of two by two arguments: argtuple = tuple([(option, value) for option,value in zip(optionlist[0::2],optionlist[1::2])]) argtuple (('-option1', '[get_rule A1 B2]'), ('-option2', '$VAR'), ('-option3', 'TAG')) whole code: import shlex s = '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG' s = s.replace('[','[') s = s.replace(']',']') optionlist = shlex.split(s) argtuple = tuple([(option, value) for option,value in zip(optionlist[0::2],optionlist[1::2])) -- http://mail.python.org/mailman/listinfo/python-list
Re: shlex parsing
Just a little modification: tuple([(option, value) for option,value in zip(optionlist[0::2],optionlist[1::2])]) == tuple(zip(optionlist[0::2],optionlist[1::2])) True Indeed: tuple(zip(optionlist[0::2],optionlist[1::2])) shorter than: tuple([(option, value) for option,value in zip(optionlist[0::2],optionlist[1::2])]) Karim PS: I am from Grenoble, which place in France are from? On 07/28/2011 06:37 PM, Nobody wrote: On Thu, 28 Jul 2011 17:48:34 +0200, Web Dreamer wrote: I would like to parse this TCL command line with shlex: '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG' s = s.replace('[','[') s = s.replace(']',']') Note that this approach won't work if you have nested brackets or braces. That would require a real parser. -- http://mail.python.org/mailman/listinfo/python-list
shlex parsing
Hello All, I would like to parse this TCL command line with shlex: '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG' And I want to get the splitted list: ['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG'] Then I will gather in tuple 2 by 2 the arguments. I tried to the shlec properties attributes 'quotes', 'whitespace', etc... But I make 'choux blanc'. If somebody has complex experiences with this module I am in. Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: shlex parsing
Thank you Dan for answering. I ended with this and gave up with shlex: split = ['-option1', '[get_rule', 'A1', 'B2]', '-option2', '$VAR', '-option3', 'TAG'] procedure_found = False result = [] for token in split: if not token.startswith('[') and not token.endswith(']') and not procedure_found: result.append(token) elif token.startswith('['): procedure_found = True _token = token elif token.endswith(']'): procedure_found = False _token += ' ' + token result.append(_token) else: _token += ' ' + token print split print result which gives the desired values: ['-option1', '[get_rule', 'A1', 'B2]', '-option2', '$VAR', '-option3', 'TAG'] ['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG'] Sure pyParsing seems to be pretty simple but my constraint is to use standard lib (at maximum). To bad it is not part of python standard libs. On the other hand, I will have to regroup expression like '-option1 $VAL == $CONSTRAINT' in ['-option1', '$VAL == $CONSTRAINT']. So it seems that I have no others choicse and have to use a parser like pyParsing. Regards Karim On 07/27/2011 10:44 PM, Dan Stromberg wrote: I've not used the shlex module, but this feels more like an issue to address with a parser than for a lexical analyzer - or perhaps even both, since you're splitting on whitespace sometimes, and matching square brackets sometimes. I've used pyparsing for stuff a bit similar to this. Or here's a list: http://wiki.python.org/moin/LanguageParsing On Wed, Jul 27, 2011 at 12:30 PM, Karim karim.liat...@free.fr mailto:karim.liat...@free.fr wrote: Hello All, I would like to parse this TCL command line with shlex: '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG' And I want to get the splitted list: ['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG'] Then I will gather in tuple 2 by 2 the arguments. I tried to the shlec properties attributes 'quotes', 'whitespace', etc... But I make 'choux blanc'. If somebody has complex experiences with this module I am in. Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Fall in love with bpython
Hello, I use bpython interpreter. This is a very good interactive CLI. I want to create a CLI with the same features than bpython. But the cmd std module seems no to be used in this project... Is there a tool where I can plug all my grammary commands line a sort of generic box with completion, highlights, etc... Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: shlex parsing
On 07/28/2011 12:11 AM, Dan Stromberg wrote: You could probably use a recursive descent parser with the standard library. But if your management is OK with pyparsing, that might be easier, and a bit more clear as well. Yes, I thought to use str method partition in a recursive way but using pyParsing still be easer. Thanks Cheers On Wed, Jul 27, 2011 at 2:08 PM, Karim karim.liat...@free.fr mailto:karim.liat...@free.fr wrote: Thank you Dan for answering. I ended with this and gave up with shlex: split = ['-option1', '[get_rule', 'A1', 'B2]', '-option2', '$VAR', '-option3', 'TAG'] procedure_found = False result = [] for token in split: if not token.startswith('[') and not token.endswith(']') and not procedure_found: result.append(token) elif token.startswith('['): procedure_found = True _token = token elif token.endswith(']'): procedure_found = False _token += ' ' + token result.append(_token) else: _token += ' ' + token print split print result which gives the desired values: ['-option1', '[get_rule', 'A1', 'B2]', '-option2', '$VAR', '-option3', 'TAG'] ['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG'] Sure pyParsing seems to be pretty simple but my constraint is to use standard lib (at maximum). To bad it is not part of python standard libs. On the other hand, I will have to regroup expression like '-option1 $VAL == $CONSTRAINT' in ['-option1', '$VAL == $CONSTRAINT']. So it seems that I have no others choicse and have to use a parser like pyParsing. Regards Karim On 07/27/2011 10:44 PM, Dan Stromberg wrote: I've not used the shlex module, but this feels more like an issue to address with a parser than for a lexical analyzer - or perhaps even both, since you're splitting on whitespace sometimes, and matching square brackets sometimes. I've used pyparsing for stuff a bit similar to this. Or here's a list: http://wiki.python.org/moin/LanguageParsing On Wed, Jul 27, 2011 at 12:30 PM, Karim karim.liat...@free.fr mailto:karim.liat...@free.fr wrote: Hello All, I would like to parse this TCL command line with shlex: '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG' And I want to get the splitted list: ['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG'] Then I will gather in tuple 2 by 2 the arguments. I tried to the shlec properties attributes 'quotes', 'whitespace', etc... But I make 'choux blanc'. If somebody has complex experiences with this module I am in. Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Fall in love with bpython
On 07/27/2011 12:34 AM, Alan Gauld wrote: Karim wrote: I use bpython interpreter. This is a very good interactive CLI. I had never heard of it and had to google for it. It appears to be a curses based CLI for *nix and MacOS Ah Windows user. I want to create a CLI with the same features than bpython. But the cmd std module seems no to be used in this project... Why not read the bpython source to see what they used? Thats the big advantage of open source - its open! (Although they don't make the source explicitly available, I'm assuming the tar file contains the source- it might even be in Python!) They use pygments to parse the source as you type. That would be worth investigating too... Yes I saw mainly pygments which is an OpenSource project... But, I must admit. I will spend 6 months before understanding this. Rhahaaa... This level is high! I saw too cmd2 which is fully compatible and inherit from cmd. But not quite as fun! The main module cli.py is 1700 lignes and the architecture is not very well documented. We will see If I can make any progress during the next month. Thanks Alan PS: Are you still making some hiking in the higlands. Beautiful photos I have seen on your web site! Cheers Alan HTH, Alan G. -- http://mail.python.org/mailman/listinfo/python-list
Re: Fall in love with bpython
On 07/28/2011 12:31 AM, Michael Poeltl wrote: hi, have you heard abut ipython? maybe that's helpful for you? http://ipython.scipy.org/moin/python Hello Michael, Yes I saw some article where Ipython and Bpython CLI integrations was made in Django. Thanks for the link I will evaluate it as well. Cheers Karim Michael * Karimkarim.liat...@free.fr [2011-07-27 23:46]: Hello, I use bpython interpreter. This is a very good interactive CLI. I want to create a CLI with the same features than bpython. But the cmd std module seems no to be used in this project... Is there a tool where I can plug all my grammary commands line a sort of generic box with completion, highlights, etc... Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: [Tutor] Fall in love with bpython
On 07/28/2011 01:32 AM, Walter Prins wrote: Hi Karim On 28 July 2011 00:04, Karim karim.liat...@free.fr mailto:karim.liat...@free.fr wrote: On 07/27/2011 12:34 AM, Alan Gauld wrote: Karim wrote: I use bpython interpreter. This is a very good interactive CLI. I had never heard of it and had to google for it. It appears to be a curses based CLI for *nix and MacOS Ah Windows user. Thanks for mentioning bpython -- I had also not heard of it but it duly installed on my ubuntu box from the repositories! I've browsed the code -- it uses ncurses to do the screen IO. You can always read the code, but, as you intimated, unless you're pretty familiar with somewhat large codebases and ncurses it might not be that much use to you just yet. ... Still, it might be worth having a look at the source... :) http://hg.bpython-interpreter.org/bpython/src/bcd836c859aa/bpython/ Walter Hi, I am using ubuntu too. I used to write large API but in fact I did not know curses and I wanted to see in the code where the keywords are 'injected' to try with my own grammar. I still have to study the details. But it seems that the keywords are imported dynamically from __builtins__ module. Don't know yet how to configure or modify it. Thanks for your advice Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Trying to learn about metaclasses
Very good. Karim On 07/25/2011 05:46 PM, jfine wrote: Hi I gave a tutorial at this year's EuroPython that covered metaclasses. You can get the tutorial materials from a link on: http://ep2011.europython.eu/conference/talks/objects-and-classes-in-python-and-javascript Jonathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Use self.vars in class.method(parameters, self.vars)
I think you did a typo it is def foo2(self, len = self._myvar): while i len: dosomething You forget '.' dot between self and _myvar By the way in the function header you have only one '_' and in the init you have 2 '_'. Be careful that's not the same variable and behavior in case you want to access it. Regards Karim On 07/22/2011 01:12 PM, caccolangrifata wrote: I'm very very new with python, and I have some experience with java programming, so probably you guys will notice. Anyway this is my question: I'd like to use class scope vars in method parameter, something like that class foo(object): __init__(self, len = 9): self.__myvar = len def foo2(self, len = self_myvar): while i len: dosomething I want to use optional parameter, so i can use myfoo = foo() or myfoo = foo(20) and also foo.foo2(20) or foo.foo2() but in def foo2(self, len = self_myvar): ^ self is undefined, so: How can I do this stuff? -- http://mail.python.org/mailman/listinfo/python-list
Re: Use self.vars in class.method(parameters, self.vars)
You're right. Sure the method header is evaluated first I usually not fall in this trap when default is a list but a singleton one with the same id. I answered too fast, I did not understand if he forget the dot or what. And the double '_' in init was strange because he uses only one '_' after. Thanks to take time to point that. Regards Karim On 07/22/2011 02:06 PM, Thomas Jollans wrote: On 22/07/11 13:32, Karim wrote: I think you did a typo it is def foo2(self, len = self._myvar): while i len: dosomething That, of course, won't work: the default argument (in this case: self._myvar) is looked up when the function is created, and stored with the function. self does not exist at that point. (or, if it does, it's the wrong self) -- http://mail.python.org/mailman/listinfo/python-list
Re: Use self.vars in class.method(parameters, self.vars)
Be careful when using double underscore prefix the variable is said to be private but in fact you can modify it. It is a convention to say don't change it. And to discourage to use it python change its name to '_class name__myvar' appending the prefix '_class name' to it. See with your example: karim@Requiem4Dream:~$ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type help, copyright, credits or license for more information. a = foo() dir(a) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_foo__myvar', 'foo2'] In the instance namespace your attribute was transformed in '_foo__myvar'. This is a veritable mess when you want to access from outside your class this attribute. For maintenance when you inherited you have huge coupling NEVER DO THAT (advice): In case you change the name of your class and reference this attribute in external class you will end up with huge trouble to change the name in all referenced code. With one '_' it says to others well this is my non public variable don't use it (this is a convention because you can still modify it in python. Cheers Karim On 07/22/2011 06:59 PM, caccolangrifata wrote: while i len: -- http://mail.python.org/mailman/listinfo/python-list
Re: Argparse, and linking to methods in Subclasses
with global: SERVER = None A the end of Argparse declarations: parser_check.set_defaults(action=do_the_check) parser_build.set_defaults(action=do_the_build) Then declare the action functions: def do_the_check(namespace_args): if not SERVER: SERVER = Server(namespace_arg.type_of_server) SERVER.check() def do_the_build(namespace_args): if not SERVER: SERVER = Server(namespace_arg.type_of_server) SERVER.build() If I correctly understood your issue. Regards Karim On 07/18/2011 03:48 AM, Victor Hooi wrote: Hi, I have a simple Python script to perform operations on various types on in-house servers: manage_servers.pyoperation type_of_server Operations are things like check, build, deploy, configure, verify etc. Types of server are just different types of inhouse servers we use. We have a generic server class, then specific types that inherit from that: class Server def configure_logging(self, loggin_file): ... def check(self): ... def deploy(self): ... def configure(self): ... def __init__(self, hostname): self.hostname = hostname logging = self.configure_logging(LOG_FILENAME) class SpamServer(Server): def check(self): ... class HamServer(Server): def deploy(self): ... My question is how to link that all up to argparse? Originally, I was using argparse subparses for the operations (check, build, deploy) and another argument for the type. subparsers = parser.add_subparsers(help='The operation that you want to run on the server.') parser_check = subparsers.add_parser('check', help='Check that the server has been setup correctly.') parser_build = subparsers.add_parser('build', help='Download and build a copy of the execution stack.') parser_build.add_argument('-r', '--revision', help='SVN revision to build from.') ... parser.add_argument('type_of_server', action='store', choices=types_of_servers, help='The type of server you wish to create.') Normally, you'd link each subparse to a method - and then pass in the type_of_server as an argument. However, that's slightly backwards due to the classes- I need to create an instance of the appropriate Server class, then call the operation method inside of that. Any ideas of how I could achieve the above? Perhaps a different design pattern for Servers? Or a way to use argparse in this situation? Thanks, Victor -- http://mail.python.org/mailman/listinfo/python-list
Re: Argparse, and linking to methods in Subclasses
Hello Michele, Your solution is great! You can combine it perhaps with the use of set_defaults() method of the argparse parser, I gave. Cheers karim On 07/18/2011 01:56 PM, Michele Simionato wrote: Here is an example by using my own library plac (http://pypi.python.org/pypi/plac): class Server(): def configure_logging(self, logging_file): pass def check(self): pass def deploy(self): pass def configure(self): pass def __init__(self, hostname): self.hostname = hostname class SpamServer(Server): def check(self): pass class HamServer(Server): def deploy(self): pass def main(classname, hostname, methname): # add error checking at will instance = globals()[classname](hostname) getattr(instance, methname)() if __name__ == '__main__': import plac; plac.call(main) -- http://mail.python.org/mailman/listinfo/python-list
Re: What is the Most Efficient Way of Printing A Dict's Contents Out In Columns?
On 06/14/2011 05:29 PM, Zachary Dziura wrote: I have a dict that I would like to print out in a series of columns, rather than as a bunch of lines. Normally when you do print(dict), the output will look something like this: {'Header2': ['2', '5', '8'], 'Header3': ['3', '6', '9'], 'Header1': ['1', '4', '7'], 'Header4': ['10', '11', '12']} I can then iterate through (in this case) a list of the headers in order to produce something similar to this: Header1 = ['1', '4', '7'] Header2 = ['2', '5', '8'] Header3 = ['3', '6', '9'] Header4 = ['10', '11', '12'] What I want to know is how I can print out that information in a column, where the header is the first line of the column, with the data following underneath, like so: Header1Header2Header3Header4 1 2 3 4 5 6 7 8 9 1011 12 Over alternative that only costs 2 lines of code, use pretty print (not in columns but crystal clear): import pprint pprint.pprint(my_dict) or in a file: pprint.pprint(my_dict, open(output.dat, wb)) Cheers karim -- http://mail.python.org/mailman/listinfo/python-list
Re: how to avoid leading white spaces
On 06/01/2011 09:39 PM, ru...@yahoo.com wrote: On Jun 1, 11:11 am, Chris Rebertc...@rebertia.com wrote: On Wed, Jun 1, 2011 at 12:31 AM, rakesh kumar Hi i have a file which contains data //ACCDJ EXEC DB2UNLDC,DFLID=DFLID,PARMLIB=PARMLIB, // UNLDSYST=UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCDJ ' //ACCT EXEC DB2UNLDC,DFLID=DFLID,PARMLIB=PARMLIB, // UNLDSYST=UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCT' //ACCUM EXEC DB2UNLDC,DFLID=DFLID,PARMLIB=PARMLIB, // UNLDSYST=UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM ' //ACCUM1EXEC DB2UNLDC,DFLID=DFLID,PARMLIB=PARMLIB, // UNLDSYST=UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM1 ' i want to cut the white spaces which are in between single quotes after TABLE=. for example : 'ACCT[spaces] ' 'ACCUM ' 'ACCUM1 ' the above is the output of another python script but its having a leading spaces. Er, you mean trailing spaces. Since this is easy enough to be homework, I will only give an outline: 1. Use str.index() and str.rindex() to find the positions of the starting and ending single-quotes in the line. 2. Use slicing to extract the inside of the quoted string. 3. Use str.rstrip() to remove the trailing spaces from the extracted string. 4. Use slicing and concatenation to join together the rest of the line with the now-stripped inner string. Relevant docs:http://docs.python.org/library/stdtypes.html#string-methods For some odd reason (perhaps because they are used a lot in Perl), this groups seems to have a great aversion to regular expressions. Too bad because this is a typical problem where their use is the best solution. import re f = open (your file) for line in f: fixed = re.sub (r(TABLE='\S+)\s+'$, r\1', line) print fixed, (The above is for Python-2, adjust as needed for Python-3) Rurpy, Your solution is neat. Simple is better than complicated... (at list for this simple issue) -- http://mail.python.org/mailman/listinfo/python-list
Re: Why did Quora choose Python for its development?
On 05/27/2011 03:47 PM, Roy Smith wrote: In article948l8nf33...@mid.individual.net, Gregory Ewinggreg.ew...@canterbury.ac.nz wrote: John Bokma wrote: A Perl programmer will call this line noise: double_word_re = re.compile(r\b(?Pword\w+)\s+(?P=word)(?!\w), re.IGNORECASE) One of the truly awesome things about the Python re library is that it lets you write complex regexes like this: pattern = r\b # beginning of line (?Pword\w+) # a word \s+# some whitespace (?P=word)(?!\w)# the same word again double_word_re = re.compile(pattern, re.I | re.X) Sometimes regex really is the best tool. It's often the most compact, or fastest, or clearest way to express something complicated. Fortunately, re.X mode gives you a way to write truly monster regexes and still having them not be total line noise. It's a shame that the Python community has evolved to be so anti-regex that most people never consider using them. While Perl's attitude to regex may be when the only tool you have is a hammer, everything looks like a nail, Python's seems to be, I've got a great collection of all kinds of neat tools, so I'm going to pretend the hammer that's in there doesn't exist because I once smashed my thumb with it and it hurt a lot. HAHAHAHAHAHA Very funny! This thread is awsome. Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Why did Quora choose Python for its development?
On 05/26/2011 10:03 PM, Terry Reedy wrote: On 5/26/2011 11:36 AM, John Bokma wrote: Ben Finneyb...@benfinney.id.au writes: [impolite comment not quoted] Get a life. Or better, just fuck off and die. It will improve both the world and the Python community, of which you are nothing but a little, smelly shitstain. That abuse is entirely unwelcome in this community, against any person. Please desist. You should have spoken up sooner, especially as the spokes person of this community. But every bully has is fan club. I agree that the original impolite comment was just that -- impolite -- and perhaps enough so that it should have been spoken out against. But I also agree that the quoted response is at least three times as bad, enough so to understandably push someone to respond. Both comments are atypical here. Original one impolite perhaps but only truth could cause such hatred. Cheers -- http://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand html.parser.HTMLParser
On 05/19/2011 11:35 PM, Andrew Berg wrote: On 2011.05.16 02:26 AM, Karim wrote: Use regular expression for bad HTLM or beautifulSoup (google it), below a exemple to extract all html links: Actually, using regex wasn't so bad: import re import urllib.request url = 'http://x264.nl/x264/?dir=./64bit/8bit_depth' page = str(urllib.request.urlopen(url).read(), encoding='utf-8') # urlopen() returns a bytes object, need to get a normal string rev_re = re.compile('revision[0-9][0-9][0-9][0-9]') num_re = re.compile('[0-9][0-9][0-9][0-9]') rev = rev_re.findall(str(page))[0] # only need the first item since the first listing is the latest revision num = num_re.findall(rev)[0] # findall() always returns a list print(num) prints out the revision number - 1995. 'revision1995' might be useful, so I saved that to rev. This actually works pretty well for consistently formatted lists. I suppose I went about this the wrong way - I thought I needed to parse the HTML to get the links and do simple regexes on those, but I can just do simple regexes on the entire HTML document. Great for you! Use what works well and easy to code, always the simpler is the better. For complicate search link to avoid using too complex and bugs prone regex you can derived the code I gave on HTMLParser with max comparison. Anyway you get the choice which is cool, not be stuck on only one solution. Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand html.parser.HTMLParser
On 05/17/2011 03:05 AM, Andrew Berg wrote: On 2011.05.16 02:26 AM, Karim wrote: Use regular expression for bad HTLM or beautifulSoup (google it), below a exemple to extract all html links: linksList = re.findall('a href=(.*?).*?/a',htmlSource) for link in linksList: print link I was afraid I might have to use regexes (mostly because I could never understand them). Even the BeautifulSoup website itself admits it's awful with Python 3 - only the admittedly broken 3.1.0 will work with Python 3 at all. ElementTree doesn't seem to have been updated in a long time, so I'll assume it won't work with Python 3. lxml looks promising, but it doesn't say anywhere whether it'll work on Python 3 or not, which is puzzling since the latest release was only a couple months ago. Actually, if I'm going to use regex, I might as well try to implement Versions* in Python. Thanks for the answers! *http://en.totalcmd.pl/download/wfx/net/Versions (original, made for Total Commander) and https://addons.mozilla.org/en-US/firefox/addon/versions-wfx_versions/ (clone implemented as a Firefox add-on; it's so wonderful, I even wrote the docs for it!) Andrew, I wrote a class with HMLTParser to get only one link for a given project, cf below: 73 class ResultsLinkParser(HTMLParser.HTMLParser): 74 Class ResultsLinkParser inherits form HTMLParser to extract 75 the original 'Submission date' of the a bug. 76 This customized parser will deals with the 'View Defect' HTML 77 page from Clear DDTS. 78 79 def __init__(self): 80 HTMLParser.HTMLParser.__init__(self) 81 self._link = None 82 83 def handle_starttag(self, tag, attrs): 84 Implement standard class HTMLParser customizing method. 85 if tag == 'frame': 86 try: 87 attributes = dict(attrs) 88 if attributes['name'] == 'indexframe': 89 self._link = attributes['src'] 90 except KeyError, e: 91 print(WARNING: Attribute '{keyname}' from frame tag 92 in QueryResult page does not exist!.format(keyname=e)) 93 94 def link(self): 95 Return the html link of the query results page. 96 return self._link You can use it and just modified it to get the latest just add some code (and change the tag 'name' of my example) to compare revision number with max and keep the max to compare it to the next value. I let you add this little code just create self._revision = None in the __init__(self) which hold the current max revision. After parser.feed() you can get the value by parser._revision or a public parser.revision() method to get the value. Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand html.parser.HTMLParser
On 05/16/2011 03:06 AM, David Robinow wrote: On Sun, May 15, 2011 at 4:45 PM, Andrew Bergbahamutzero8...@gmail.com wrote: I'm trying to understand why HMTLParser.feed() isn't returning the whole page. My test script is this: import urllib.request import html.parser class MyHTMLParser(html.parser.HTMLParser): def handle_starttag(self, tag, attrs): if tag == 'a' and attrs: print(tag,'-',attrs) url = 'http://x264.nl/x264/?dir=./64bit/8bit_depth' page = urllib.request.urlopen(url).read() parser = MyHTMLParser() parser.feed(str(page)) I can do print(page) and get the entire HTML source, but parser.feed(str(page)) only spits out the information for the top links and none of the revision links. Ultimately, I just want to find the name of the first revision link (right now it's revision1995, when a new build is uploaded it will be revision2000 or whatever). I figure this is a relatively simple page; once I understand all of this, I can move on to more complicated pages. You've got bad HTML. Look closely and you'll see the there's no space between the revision strings and the style tag following. The parser doesn't like this. I don't know a solution other than fixing the html. (I created a local copy, edited it and it worked.) Hello, Use regular expression for bad HTLM or beautifulSoup (google it), below a exemple to extract all html links: linksList = re.findall('a href=(.*?).*?/a',htmlSource) for link in linksList: print link Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Customize help output from optparse (or argparse)
On 05/12/2011 10:22 PM, Thomas 'PointedEars' Lahn wrote: Thorsten Kampe wrote: I'm using optparse for a little Python script. 1. The output from --help is: Usage: script.pyarg script.py does something Options: -h, --help show this help message and exit I would prefer to have the description before the usage, like... script.py does something Usage: script.pyarg Options: -h, --help show this help message and exit 2. The output from --doesnotexit is: Usage: script.pyarg script.py: error: no such option: --doesnotexist I would prefer to have the error first, then the usage and additionally the options, like... script.py: error: no such option: --doesnotexist Usage: script.pyarg Options: -h, --help show this help message and exit Is that possible with either optparse or the new kid on the block argparse. If so how? You can easily have #1 with optparse.OptionParser(usage=…)¹, but optparse is deprecated in favor of argparse.ArgumentParser. I do not think you can have #2 with either optparse or argparse: OptionParser() would print the error message last, and ArgumentParser() would not print the description on error. Subclassing ArgumentParser might be feasible, though. __ ¹http://PointedEars.de/devel/tools/text/odfinfo/ Please find documentation to configure help in ArgumentParser BUT for argparse module: - HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter, 50 ArgumentDefaultsHelpFormatter -- Formatter classes which 51 may be passed as the formatter_class= argument to the 52 ArgumentParser constructor. HelpFormatter is the default, 53 RawDescriptionHelpFormatter and RawTextHelpFormatter tell the parser 54 not to change the formatting for help text, and 55 ArgumentDefaultsHelpFormatter adds information about argument defaults 56 to the help. So It seems easy to a different pass formatter_class to ArgumentParser. You can inherite from HelpFormater class but you have to know the implementation details: File is located at python install/lib/python2.7/argparse.py Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: Custom string joining
On 05/07/11 16:25, Chris Rebert wrote: On Sat, May 7, 2011 at 5:31 AM, Claudiu Popacp...@bitdefender.com wrote: Hello Python-list, I have an object which defines some methods. I want to join a list or an iterable of those objects like this: new_string = |.join(iterable_of_custom_objects) What is the __magic__ function that needs to be implemented for this case to work? I though that __str__ is sufficient but it doesn't seems to work. Thanks in advance. You need to do the string conversion yourself; .join() doesn't do it for you, due to strong typing. It only accepts iterables of strings: new_string = |.join(str(x) for x in iterable_of_custom_objects) You just have to implement __str__() python special method for your custom_objects. Regards Karim Cheers, Chris -- http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to use optparse without the command line?
On 04/07/2011 10:37 AM, markolopa wrote: Hello, Is there support/idioms/suggestions for using optparse without a command line? I have a code which used to be called through subprocess. The whole flow of the code is based on what 'options' object from optparse contains. Now I want to call this code without subprocessing. What I did first was to build a fake command-line and use options, args = parser.parse_args(fake_cmdline) But I find it dumb to encode and decode a dictionary... So I would like to know how I if there is a good way of passing a dictionary to optparse and benefiting from its option management (check, error detection, etc). Thanks a lot! Marko Move to the best module on args parsing: argparse it is way, way, way better no equivalent in any others language. No tuple when parsing but a simple Namespace objects and very easy to port. Go have a look in std libs (=v2.7). Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: using python to post data to a form
On 04/04/2011 01:01 PM, Corey Richardson wrote: On 04/04/2011 01:36 AM, Littlefield, Tyler wrote: Hello: I have some data that needs to be fed through a html form to get validated and processed and the like. How can I use python to send data through that form, given a specific url? the form says it uses post, but Im not really sure what the difference is. would it just be: http://mysite.com/bla.php?foo=barbar=foo? If so, how do I do that with python? import urllib import urllib2 url = http://www.foo.com/; data = {name: Guido, status: BDFL} data = urllib.urlencode(data) request = urllib2.Request(url, data) response = urllib2.urlopen(request) page = response.read() So yeah, passing in a Request object to urlopen that has some urlencode'ed data in it. Real life example: I query for bugs: def make_form_data(query=None): Factory function to create a post form query to submit on a html webpage. @param query - the query string from the existing query list of the query webpage. return { 'init' : EMPTY, 'NextForm' : EMPTY, 'LastForm' : FORM_QUERY, 'ACTION' : ACTION, 'class' : CLASS, 'personalQuery' : query, 'sharedQuery': EMPTY, '.cgifields' : CONFIG_QUERY, '.cgifields' : SHARED_QUERY } def authentication_setup(username=None, password=None, url=None): Setup an authentication for a super-url (root url) on a given securised web server. @param username - String @param password - String @param url - String # Password Manager creation pwd_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() # As we set the first parameter to None # the Password Manager will always use # the same combination username/password # for the urls for which 'url' is a super-url. pwd_manager.add_password(None, url, username, password) # Authentication Handler creation from the Password Manager. auth_handler = urllib2.HTTPBasicAuthHandler(pwd_manager) # Opener creation from the Authentication Handler. opener = urllib2.build_opener(auth_handler) # Tous les appels a urllib2.urlopen vont maintenant utiliser le handler # Ne pas mettre le protocole l'URL, ou # HTTPPasswordMgrWithDefaultRealm sera perturbe. # Vous devez (bien sur) l'utiliser quand vous recuperez la page. urllib2.install_opener(opener) # l'authentification est maintenant geree automatiquement pour nous def post_request(url=None, data=None, headers=None): Post a request form on a given url web server. @param url - String @param data - Dictionnary @param headers - Dictionnary @return response The web page (file) object. if headers is None: headers = { 'User-Agent' : __file__, 'Content-Type' : 'application/x-www-form-urlencoded', 'Accept' : 'text/html' } query = urllib.urlencode(data) if data is not None else '' request = urllib2.Request(url, query, headers) response = urllib2.urlopen(request) #print('INFO', response.info()) return response AND MAIN CODE: try: webpage = post_request(url=MAIN_URL, data=make_form_data(query)) html= webpage.read() print('Authentication Information: access granted.') except URLError, e: print('Authentication Information: {msg}.'.format(msg=e)) sys.exit(1) That's all folks Authentication+Posting a request. make_form_data() is the most important. To find cgi data dict you can use ClientForm.py (google it!) it is a good helper to find form data. Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: return an object of a different class
On 02/16/2011 06:05 AM, Richard Thomas wrote: On Feb 16, 2:23 am, s...@uce.gov wrote: How can I do something like this in python: #!/usr/bin/python3.1 class MyNumbers: def __init__(self, n): self.original_value = n if n= 100: self = SmallNumers(self) else: self = BigNumbers(self) class SmallNumbers: def __init__(self, n): self.size = 'small' class BigNumbers: def __init__(self, n): self.size = 'big' t = MyNumbers(200) When I do type(t) it says MyNumbers, while I'd want it to be BigNumbers, because BigNumbers and SmallNumbers will have different methods etc... Do I need to use metaclasses? Thanks. -- Yves. http://www.SollerS.ca/ http://blog.zioup.org/ If you don't want to use a factory function I believe you can do this: class MyNumber(object): def __new__(cls, n): if n= 100: cls = SmallNumbers else: cls = BigNumbers return object.__new__(cls, n) ... Chard. Very beautiful code great alternative to factory method! To memorize this pythonic way. Regards Karim -- http://mail.python.org/mailman/listinfo/python-list
Re: [Tutor] Accessing query results html frame :The solution
On 02/14/2011 01:41 PM, Karim wrote: Hello, As I get no response from the tutor python list, I am continuing to investigate my problem. In fact the issue is that there are 2 forms in the interactive page and my request does nothing instead I get the interactive page not the submission I asked (query results). The 2 forms are: 1. FORM METHOD=POST ACTION=/ddts/ddts_main ENCTYPE=application/x-www-form-urlencoded NAME=form1 2. FORM METHOD=POST ACTION=/ddts/ddts_main ENCTYPE=application/x-www-form-urlencoded NAME=form9 And the parameters for each are: 1) INPUT TYPE=hidden NAME=init VALUE= INPUT TYPE=hidden NAME=LastForm VALUE=SavedQuery INPUT TYPE=hidden NAME=NextForm VALUE= INPUT TYPE=hidden NAME=REMOTE_USER VALUE=karim.liateni INPUT TYPE=submit NAME=ACTION VALUE=Querynbsp; INPUT TYPE=submit NAME=ACTION VALUE=Reportnbsp; INPUT TYPE=submit NAME=ACTION VALUE=Editnbsp; INPUT TYPE=submit NAME=ACTION VALUE=Deletenbsp; INPUT TYPE=submit NAME=ACTION VALUE=Create ONCLICK=oncreate()nbsp; INPUT TYPE=submit NAME=ACTION VALUE=Create String Query INPUT TYPE=hidden NAME=.cgifields VALUE=personalQuery INPUT TYPE=hidden NAME=.cgifields VALUE=sharedQuery 2) INPUT TYPE=hidden NAME=LastForm VALUE=DumpBug INPUT TYPE=hidden NAME=REMOTE_USER VALUE=karim.liateni INPUT TYPE=text NAME=bug_id VALUE= SIZE=10 MAXLENGTH=10 INPUT TYPE=submit NAME=.submit VALUE=View And I recall my data of the progam below: data = { 'init' : , 'LastForm': SavedQuery, 'prompted': yes, 'class': Development, 'personalQuery': DKPV, 'REMOTE_USER': username, 'QS': -p DKPVALIDATION_PLUGIN \(Class 'isequal' quot;Developmentquot; \), 'use_field_defs':false, 'QueryName': DKPV, 'QueryType': personal, 'ACTION': Query } So the question is how could I specify the correct FORM submission and how could I chose the correct action because there are several TYPE='submit' and I am only interested by this one VALUE=Query? Regards Karim On 02/11/2011 08:51 AM, Karim wrote: Hello, In fact as found in the net: The concept of browser frames is completely outside the scope of HTTP. However, browser frames are defined in HTML, and so is the target property on form elements: lt;form action=/somescript?x=y method=POST target=_topgt; This will make the form submit to the _top frame, which means use the full browser window That means that my post form: FORM METHOD=POST ACTION=/ddts/ddts_main ENCTYPE=application/x-www-form-urlencoded TARGET=rightframe has a target property to make the submit to the 'rightframe'. Any ideas how I can modified the code (I think the request data or whatever) below to access without knowing the temporary html file name generically. Regards Karim On 02/10/2011 07:12 PM, Karim wrote: Hello All, I get from Steven an very useful link (void space) for http authentication. I added some codes to be able to POST FORM a query as I do it by clicking a query button to get a list of bug Id on a server. The problem is I get a html page which refers 2 frames. And I am interesting in one particular frame namely for example, http://{server}:{port}/wt/tmp/results:karim.liateni.31_3917.html'.format(server=server, port=port). But this pages is created every times in a tmp directory each time with a different name. 1) How can I get the name of this page because with python the page resulting of my query is not mentionned (hidden like)? Interactively there are 3 frames but only this one is of interest for me. But no name of this page is visible in the main html page. Is there a method to get all the nested frames locations? 2) I can see this page interactively when I click on a submit query button. Do I need to add 'ACTION': Query input form in the query dictionnary to simulate a click for submission (type=submit button) ? 3) Interactively I see that cgi arg NextForm is empty so I let it like that in my query and LastForm was set to SavedQuery. I put the same value in my python code. Is this ok? import urllib import urllib2 server='dummy.com' port='8081' username = 'karim.liateni' password = 'dummy_pass' theurl = 'http://{server}:{port}/ddts/ddts_main'.format(server=server, port=port) #theurl = 'http://{server}:{port}:8081/wt/tmp/results:karim.liateni.31_3917.html'.format(server=server, port=port) #MEMO: #FORM METHOD=POST ACTION=/ddts/ddts_main ENCTYPE=application/x-www-form-urlencoded TARGET=rightframe data = { 'NextForm': , 'LastForm': SavedQuery, 'prompted': yes, 'class': Development, 'personalQuery': DKPV, 'REMOTE_USER': username, 'QS': -p DKPVALIDATION_PLUGIN \(Class 'isequal' quot;Developmentquot; \), 'use_field_defs':false, 'QueryName': DKPV, 'QueryType': personal, 'ACTION': Query } query = urllib.urlencode(data) request = urllib2.Request(theurl, query) passman = urllib2
Re: [Tutor] Accessing query results html frame : PLEASE READ ME :o)
Hello, As I get no response from the tutor python list, I am continuing to investigate my problem. In fact the issue is that there are 2 forms in the interactive page and my request does nothing instead I get the interactive page not the submission I asked (query results). The 2 forms are: 1. FORM METHOD=POST ACTION=/ddts/ddts_main ENCTYPE=application/x-www-form-urlencoded NAME=form1 2. FORM METHOD=POST ACTION=/ddts/ddts_main ENCTYPE=application/x-www-form-urlencoded NAME=form9 And the parameters for each are: 1) INPUT TYPE=hidden NAME=init VALUE= INPUT TYPE=hidden NAME=LastForm VALUE=SavedQuery INPUT TYPE=hidden NAME=NextForm VALUE= INPUT TYPE=hidden NAME=REMOTE_USER VALUE=karim.liateni INPUT TYPE=submit NAME=ACTION VALUE=Querynbsp; INPUT TYPE=submit NAME=ACTION VALUE=Reportnbsp; INPUT TYPE=submit NAME=ACTION VALUE=Editnbsp; INPUT TYPE=submit NAME=ACTION VALUE=Deletenbsp; INPUT TYPE=submit NAME=ACTION VALUE=Create ONCLICK=oncreate()nbsp; INPUT TYPE=submit NAME=ACTION VALUE=Create String Query INPUT TYPE=hidden NAME=.cgifields VALUE=personalQuery INPUT TYPE=hidden NAME=.cgifields VALUE=sharedQuery 2) INPUT TYPE=hidden NAME=LastForm VALUE=DumpBug INPUT TYPE=hidden NAME=REMOTE_USER VALUE=karim.liateni INPUT TYPE=text NAME=bug_id VALUE= SIZE=10 MAXLENGTH=10 INPUT TYPE=submit NAME=.submit VALUE=View And I recall my data of the progam below: data = { 'init' : , 'LastForm': SavedQuery, 'prompted': yes, 'class': Development, 'personalQuery': DKPV, 'REMOTE_USER': username, 'QS': -p DKPVALIDATION_PLUGIN \(Class 'isequal' quot;Developmentquot; \), 'use_field_defs':false, 'QueryName': DKPV, 'QueryType': personal, 'ACTION': Query } So the question is how could I specify the correct FORM submission and how could I chose the correct action because there are several TYPE='submit' and I am only interested by this one VALUE=Query? Regards Karim On 02/11/2011 08:51 AM, Karim wrote: Hello, In fact as found in the net: The concept of browser frames is completely outside the scope of HTTP. However, browser frames are defined in HTML, and so is the target property on form elements: lt;form action=/somescript?x=y method=POST target=_topgt; This will make the form submit to the _top frame, which means use the full browser window That means that my post form: FORM METHOD=POST ACTION=/ddts/ddts_main ENCTYPE=application/x-www-form-urlencoded TARGET=rightframe has a target property to make the submit to the 'rightframe'. Any ideas how I can modified the code (I think the request data or whatever) below to access without knowing the temporary html file name generically. Regards Karim On 02/10/2011 07:12 PM, Karim wrote: Hello All, I get from Steven an very useful link (void space) for http authentication. I added some codes to be able to POST FORM a query as I do it by clicking a query button to get a list of bug Id on a server. The problem is I get a html page which refers 2 frames. And I am interesting in one particular frame namely for example, http://{server}:{port}/wt/tmp/results:karim.liateni.31_3917.html'.format(server=server, port=port). But this pages is created every times in a tmp directory each time with a different name. 1) How can I get the name of this page because with python the page resulting of my query is not mentionned (hidden like)? Interactively there are 3 frames but only this one is of interest for me. But no name of this page is visible in the main html page. Is there a method to get all the nested frames locations? 2) I can see this page interactively when I click on a submit query button. Do I need to add 'ACTION': Query input form in the query dictionnary to simulate a click for submission (type=submit button) ? 3) Interactively I see that cgi arg NextForm is empty so I let it like that in my query and LastForm was set to SavedQuery. I put the same value in my python code. Is this ok? import urllib import urllib2 server='dummy.com' port='8081' username = 'karim.liateni' password = 'dummy_pass' theurl = 'http://{server}:{port}/ddts/ddts_main'.format(server=server, port=port) #theurl = 'http://{server}:{port}:8081/wt/tmp/results:karim.liateni.31_3917.html'.format(server=server, port=port) #MEMO: #FORM METHOD=POST ACTION=/ddts/ddts_main ENCTYPE=application/x-www-form-urlencoded TARGET=rightframe data = { 'NextForm': , 'LastForm': SavedQuery, 'prompted': yes, 'class': Development, 'personalQuery': DKPV, 'REMOTE_USER': username, 'QS': -p DKPVALIDATION_PLUGIN \(Class 'isequal' quot;Developmentquot; \), 'use_field_defs':false, 'QueryName': DKPV, 'QueryType': personal, 'ACTION': Query } query = urllib.urlencode(data) request = urllib2.Request(theurl, query) passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, theurl
Re: get the pid of a process with pexpect
Many thanks ! This is what I did for mytunnel.py : I use pexpect like this tunnel_command = '''bash -c ssh -N -R 60011:localhost:60002 [EMAIL PROTECTED] ''' com=echo \+str(ssh_tunnel.pid)+\ pids ... time.sleep(172800) in the bash script which calls mytunnel.py : ./mytunnel.py ${USERN} ${HMM} ${P2USE} ${p2use2} echo $! pids this way I have all the pids Cheers ! Noah wrote: On May 5, 7:18 am, Karim Bernardet [EMAIL PROTECTED] wrote: ssh_tunnel = pexpect.spawn (tunnel_command % globals()) ... print ssh_tunnel.pid but ssh_tunnel is not the pid of the ssh tunnel Is there a way to get it using pexpect ? You will notice that you can't get this information even from the shell. This does not work of course: ssh -f -N -L 81:localhost:80 [EMAIL PROTECTED] echo $! However, this seems to work, but I don't trust it. Certainly it isn't a real daemon, but this work OK for you if you only need to manage the tunnel for the duration of your script. Notice that now Bash had the PID in $! variable: ssh -N -L 81:localhost:80 [EMAIL PROTECTED] TUNNEL_PID=$! echo $TUNNEL_PID What command-line are you using for 'tunnel_command'? This is hard because SSH does not provide a way to get the PID of the tunnel if you request ssh to go to the background (see the -f option). I always considered this a bug because it makes scripting hard. Even if you start the tunnel from a shell you can't use $! to get the PID because the daemonizing is initiated by ssh. This is not the same use using the shell to put a command into the background, so the shell won't know anything about the PID. I'm not sure if you can put ssh into the background using the shell and still have the tunnel work. So you might start a tunnel something like this: ssh -f -N -L 80:localhost:80 [EMAIL PROTECTED] But can you also do something like this? ssh -N -L 80:localhost:80 [EMAIL PROTECTED] echo $! And for that to even work you will have to use Pexpect to start bash. Remember, Python doesn't start your command in a subshell, so you have to specify it if you want. So your tunnel command would have to be something like this: tunnel_command = '''bash -c ssh -N -L ...foo... ''' ssh_tunnel = pexpect.spawn (tunnel_command % globals()) -- Noah -- http://mail.python.org/mailman/listinfo/python-list
get the pid of a process with pexpect
Hi I am using pexpect to do ssh tunneling and to open a vnc server (jobs on a grid cluster). When the job is canceled, these 2 processes remain on the worker node (they are detached), so I have to kill them (using a trap command in the bash script of the job) but I need the pid of each process. I have tried to get it like this ssh_tunnel = pexpect.spawn (tunnel_command % globals()) ... print ssh_tunnel.pid but ssh_tunnel is not the pid of the ssh tunnel Is there a way to get it using pexpect ? Cheers Karim -- http://mail.python.org/mailman/listinfo/python-list
Appending a log file and see progress as program executes
Hi, I am writing a program that will take several days to execute :) and would like to append to a log file but be able to open that file at any time and see the errors that have occured. So this is what I am doing: -- flog = open('out.log', 'a') when needed: sys.stdout=flog print error message This will print directly to log. I use sys.stdout so i can quickly (in code) change back and forth between errors displayed on screen and errors logged.. This works great. The only problem is that I cant see anything in the log file when I try to open it say with notepad while the program is running...and this is not good at all! Any suggestions are appreciated. Karim _ See Fireworks On Live Image Search http://search.live.com/images/results.aspx?q=Fireworksmkt=en-caFORM=SERNEP -- http://mail.python.org/mailman/listinfo/python-list