Why I chose Python over Ruby
I discovered Python a few months ago and soon decided to invest time in learning it well. While surfing the net for Python, I also saw the hype over Ruby and tried to find out more about it, before I definitely embarked on studying and practicing Python. I recently found two sufficient answers for choosing Python - which is a personal choice and others may differ, but I'd like to share it anyway : 1) In Ruby there is a risk of "Variable/Method Ambiguity" when calling a method with no parameters without using () : Here is an excerpt from the book "Programming Ruby The Pragmatic Programmer's Guide". http://www.rubycentral.com/book/language.html "When Ruby sees a name such as ``a'' in an expression, it needs to determine if it is a local variable reference or a call to a method with no parameters. To decide which is the case, Ruby uses a heuristic. As Ruby reads a source file, it keeps track of symbols that have been assigned to. It assumes that these symbols are variables. When it subsequently comes across a symbol that might be either a variable or a method call, it checks to see if it has seen a prior assignment to that symbol. If so, it treats the symbol as a variable; otherwise it treats it as a method call. As a somewhat pathological case of this, consider the following code fragment, submitted by Clemens Hintze." def a print "Function 'a' called\n" 99 end for i in 1..2 if i == 2 print "a=", a, "\n" else a = 1 print "a=", a, "\n" end end OUTPUTS >> a=1 Function 'a' called a=99 "During the parse, Ruby sees the use of ``a'' in the first print statement and, as it hasn't yet seen any assignment to ``a,'' assumes that it is a method call. By the time it gets to the second print statement, though, it has seen an assignment, and so treats ``a'' as a variable. Note that the assignment does not have to be executed---Ruby just has to have seen it. This program does not raise an error." I tried the code above at the interactive Ruby 1.8.2 interpreter : http://www.ruby.ch/tutorial/ 2) Ruby does not have true first-class functions living in the same namespace as other variables while Python does : In Python : def sayHello (name) : return "Hello " + name print sayHello("Mr. Bond") m = sayHello print m print m("Miss Moneypenny") OUTPUTS >> Hello Mr. Bond Hello Miss Moneypenny In Ruby you need extra syntax that ruins the "first-class-ness" : def sayHello (name) return "Hello " + name end puts sayHello("Mr. Bond") m = Class.method(:sayHello) puts m puts m.call("Miss Moneypenny") OUTPUTS >> Hello Mr. Bond # Hello Miss Moneypenny 4) Conclusion Since I did a lot of work in Scheme, rigor and consistency are most important to me, and Python certainly meets this requirement. --- Python newbie -- http://mail.python.org/mailman/listinfo/python-list
Re: Why I chose Python over Ruby
Alex Martelli wrote: > > I also share your preference for a single namespace for callable and > non-callable values, as in Python (and Scheme, Lisp, C++, ...), rather > than disjoint namespaces as in Ruby (and Smalltalk), but I do not see it > as a question of rigor and consistency at all -- e.g., I do not perceive > Smalltalk as less rigorous or consistent than C++, on the contrary. > > So, I agree with your choice, and I think I understand your motivations, > but I do not entirely share your motivations, personally speaking. > Thanks Alex for your excellent explanations. I have the Python Cookbook 2nd Ed., and I highly appreciate your knowledge and experience. I guess my choice of words "rigor and consistency" was not very good. In this context "rigor" meant enforcing rules (for example having to use parentheses to call a method) to prevent ambiguity rather than depending on heuristics. Also "consistency" meant doing things as uniformly as possible (for example always call a method with the same syntax, whether the variable referencing it is the original name or an alias). -- Francois -- http://mail.python.org/mailman/listinfo/python-list
Re: Why I chose Python over Ruby
[EMAIL PROTECTED] wrote: > What happened to 3)? > "4)" should have read "3)". I found the typo after I posted. I guess I lack "rigor" myself ! -- http://mail.python.org/mailman/listinfo/python-list
Re: Why I chose Python over Ruby
Alex Martelli wrote: > > I also share your preference for a single namespace for callable and > non-callable values, as in Python (and Scheme, Lisp, C++, ...), rather > than disjoint namespaces as in Ruby (and Smalltalk), but I do not see it > as a question of rigor and consistency at all -- e.g., I do not perceive > Smalltalk as less rigorous or consistent than C++, on the contrary. > > So, I agree with your choice, and I think I understand your motivations, > but I do not entirely share your motivations, personally speaking. > Thanks Alex for your excellent explanations. I have the Python Cookbook 2nd Ed., and I highly appreciate your knowledge and experience. I guess my choice of words "rigor and consistency" was not very good. In this context "rigor" meant enforcing rules (for example having to use parentheses to call a function) to prevent ambiguity rather than depending on heuristics. Also "consistency" meant doing things as uniformly as possible (for example always call a function with the same syntax, whether the variable referencing it is the original name or an alias). -- Francois -- http://mail.python.org/mailman/listinfo/python-list
Re: NOT Python: Taxonomy list reference
This taxonomy is described in the Python Cookbook 2nd Ed,. Chapter 6, Introduction written by Alex Martelli, on page 234. The author explains that "multiple inheritance frees you from these contraints" - of fitting into a single taxonomy. -- http://mail.python.org/mailman/listinfo/python-list
How can I verify if the regex exist in a file without reading ?
Hi, Here is my script : It propose to replace some words in a file with a regular expression. It create a copy to write on it, and if there isn't an error, it delete the original by the copy with "os.rename" at the end. My problem is, if I work on a huge file, I'll try to avoid to read the file because it will be crash my computer :) and I would to verify if the regex enter by the user, exist. I don't know if it's possible, I'm looking for a solution since few hours... so sorry if the question is easy or wtf :) import re import os try: path = raw_input('Please enter the path of your file that you want to correct : \n') print("") print('Which regex ? \n') regex = raw_input('- : ') print('By what ? \n') new_word = raw_input('- : ') # Creating copy file filenames_regex = re.findall(r'[a-zA-Z0-9]+\.', path) filename = filenames_regex[len(filenames_regex)-1] new_filename = filename + 'copy.txt' # Replace regex by new word line by line on copy file with open(path) as rf, open(new_filename, 'w') as wf: for line in rf: wf.write(re.sub(regex, new_word, line)) except OSError: print("Permission denied") except IOError: print("This file doesn't exist") else: os.rename(new_filename, filename + 'txt') python 2.7.10 Thanks ! Best regards, François. -- https://mail.python.org/mailman/listinfo/python-list
Re: How can I verify if the regex exist in a file without reading ?
Le vendredi 15 juin 2018 02:42:12 UTC+2, Cameron Simpson a écrit : > On 15Jun2018 00:24, Steven D'Aprano > wrote: > >On Fri, 15 Jun 2018 10:00:59 +1000, Cameron Simpson wrote: > >> Francois, unless your regex can cross multiple lines it is better to > >> search files like this: > >> > >> with open(the_filename) as f: > >> for line in f: > >> ... search the line for the regexp ... > >> > >> That way you only need to keep one line at a time in memory. > > > >That's what François is doing. > > Urr, so he is. Then like you, I don't know why he's concerned about running > out > of memory. Unless it hasn't been made clear the Python will free up unused > memory on its own. > > Cheers, > Cameron Simpson Thanks you a lot for all your tips ! They helps me a lot :) I'm beginner in python, this is an excercise I'm trying to realise. I gonna read more about exceptions and .splitex() ! I work with a file which contains millions lines, a simply file.read() and I'm running out of memory François -- https://mail.python.org/mailman/listinfo/python-list
Re: How can I verify if the regex exist in a file without reading ?
Le vendredi 15 juin 2018 12:36:40 UTC+2, Steven D'Aprano a écrit : > On Fri, 15 Jun 2018 01:01:03 -0700, francois.rabanel wrote: > > > I work with a file which contains millions lines, a simply file.read() > > and I'm running out of memory > > Assuming each line is on average a hundred characters long, a million > lines is (approximately) 100 MB. Even on a computer with only 2GB of > memory, you should be able to read 100 MB. > > But you shouldn't: it is much better to process the file line by line. > > > # Don't do this: > with open(pathname) as f: > text = f.read() # Slurp the entire file into memory at once. > ... > > # Do this instead > with open(pathname) as f: > for line in f: > # process one line at a time > > > You said you are running out of memory, earlier you said the computer was > crashing... please describe exactly what happens. If you get a Traceback, > copy and paste the entire message. > > (Not just the last line.) > > > > > -- > Steven D'Aprano > "Ever since I learned about confirmation bias, I've been seeing > it everywhere." -- Jon Ronson I resolve my problem and when I look to my solution I don't understand why I didn't do it earlier :) with open(path) as file: result = [] for line in file: find_regex = re.search(regex,line) if find_regex: result.append(find_regex.group()) if len(result) == 0: sys.exit('Regex not found') elif result[0] == '': sys.exit('Whitespace as regex don\'t work') I was looking for a way to check if the regex's user was correct or not -- https://mail.python.org/mailman/listinfo/python-list
Re: Dealing with Excel
Robert Hicks wrote: > I need to pull data out of Oracle and stuff it into an Excel > spreadsheet. What modules have you used to interface with Excel and > would you recommend it? > > Robert > http://sourceforge.net/projects/pyexcelerator/ http://sourceforge.net/projects/pyxlwriter/ We use the latter one in the past. As long as your output is plain enough. It's effective and MS-free. The former should be more powerful. Not tested here. Hope this helps Francois -- http://mail.python.org/mailman/listinfo/python-list
Re: MS SQL Server/ODBC package for Python
Peter, May my I apologize for knocking against your information, as well. > For what it is worth, my experience is as follows: Using a PIII > 550MHz, 256MB RAM, running WinNT 4.0 and Python 2.3.4 and connecting > to a Sybase Adaptive Server Anywhere 8.0 database, mx.ODBC took > approximately 8 wall-clock seconds to connect As a long time user of the ASA range of products, I was surprised by your connection time to ASA 8. I'm not a regular user of mxODBC but i have tested it with success here. With no pending time upon connection whatever the middleware. The script below ran fast on my machine (an oldish pentium 400) with ASA 8.0.2 (the engine is local). When re-cycling ASA connection (a good practice) the script took 0.21 sec. to run and 3.4 sec. when re-building the connection on every hit (which you should avoid). For 100 connections, not one! Which confirms my feeling that something got in the way when you ran your test. mxODBC is fast and safe (on both linux and win32). I won't comment on ado since i'm not a user. But the fact that ASA+mxODBC runs multi-platform may be an additional advantage. Regards Francis from mx import ODBC import time mytime=time.time() print "1 connection and 750 cursors started, used and closed at once..." dbHandle=ODBC.Windows.Connect("descriptive demo fr","dupont","",0) for i in range(100): cHandle=dbHandle.cursor() cHandle.execute("select 1") cHandle.fetchall() cHandle.close() print time.time() - mytime print "750 connection fully started, used and closed at once..." for i in range(100): dbHandle=ODBC.Windows.Connect("descriptive demo fr","dupont","",0) cHandle=dbHandle.cursor() cHandle.execute("select 1") cHandle.fetchall() cHandle.close() dbHandle.close() print time.time() - mytime -- http://mail.python.org/mailman/listinfo/python-list
Re: MS SQL Server/ODBC package for Python
Hi Peter Running your benchmark, I ran into a couple of interesting points. Using mx.ODBC, my times were 0.54 seconds and 6.56 seconds respectively, while using adodbapi my results are 3.55 seconds and 25.9 seconds respectively. mx.ODBC is faster with the simple query you provide. We agree on figures at this stage :) Next I modified the benchmark to reflect my particular circumstances more accurately [...] reduce the number of iterations from 100 to 10. Since there are 128000 records in the main table, the wait for 100 iterations was too long for my patience. Under these circumstances, mx.ODBC's numbers are 188.49 seconds and 377.56 seconds respectively, and adodbapi's times are 111.15 seconds and 223.55 seconds respectively. This is an interesting feedback. It looks like both middleware have their distinct value and distinct set of advantages. I'll definitely review my judgment on ADO! My first wall-clock impressions are obvious exaggerations of reality, for which I duly apologize to all. However, adodbapi did prove to be faster in my admittedly very wacky common use case. Slower to connect, but faster to run a substantial query. Comments? Questions? Suggestions for improvement? Based on your results, my feeling is that mx.ODBC remains a solution of choice for db-support behing web services "à la mod_python" where connection time is essential whilst adodbapi would be the definite winner when it comes to typical db-intensive win32-based applications (such as wxpython-based ones). Regards to you Francois -- http://mail.python.org/mailman/listinfo/python-list
[argparse] mutually exclusive group with 2 sets of options
Hi, Is it possible with argparse to have this syntax for a script? my-script (-a -b VALUE-B | -c -d VALUE-D) I would like to do this with the argparse module. Thanks in advance. -- François Lafont -- http://mail.python.org/mailman/listinfo/python-list
Re: [argparse] mutually exclusive group with 2 sets of options
Hello, Up. ;-) Le 04/08/2013 04:10, Francois Lafont a écrit : > Is it possible with argparse to have this syntax for a script? > > my-script (-a -b VALUE-B | -c -d VALUE-D) > > I would like to do this with the argparse module. > > Thanks in advance. I have found this post: https://groups.google.com/forum/#!searchin/argparse-users/exclusive/argparse-users/-o6GOwhCjbQ/m-PfL4OxLAIJ It was in 2011 and at that time, it was impossible to have the syntax above. I have the impression that it's impossible now yet. Am I wrong? If it's impossible yet, I could try a hack. With some checks, I think I could have the "(-a -b VALUE-B | -c -d VALUE-D)" behavior but I would like this syntax appear in the help output too and I have no idea to do it. -- François Lafont -- http://mail.python.org/mailman/listinfo/python-list
Re: [argparse] mutually exclusive group with 2 sets of options
Le 05/08/2013 16:11, Miki Tebeka a écrit : > You can probably do something similar using sub commands > (http://docs.python.org/2/library/argparse.html#sub-commands). Yes, but this is not the same syntax. I want this syntax : my-script (-a -b VALUE-B | -c -d VALUE-D) I don't want this syntax: my-script (subcommad-a -b VALUE-B | subcommand-c -d VALUE-D) In fact, in this post, I have simplified my question to put the stress just on my problem. In the real life, my script already uses the subcommands (and no problem with that). But I need to have mutually exclusive groups with 2 *sets* of options for compatibility reasons with another programs. -- François Lafont -- http://mail.python.org/mailman/listinfo/python-list
Re: [argparse] mutually exclusive group with 2 sets of options
Le 05/08/2013 22:01, Rafael Durán Castañeda a écrit : > I think you are looking for exclusive groups: > > http://docs.python.org/2.7/library/argparse.html#argparse.add_mutually_exclusive_group Yes... but no. The doc explains you can do this: my-script (-b VALUE-B | -d VALUE-D) ie mutally exclusive group with *just* *one* option incompatible with another one. But, apparently, you can't do that: my-script (-a -b VALUE-B | -c -d VALUE-D) ie mutually exclusive group with one *set* of option*s* incompatible with another *set* of option*s*. This is why I have posted my message. I have read the documentation before to post it. ;-) I know docopt but I prefer argparse. My script have subcommands and some of them have common options, so I appreciate the parser objects and the inheritance between parser objects (with parents parameter of argparse.ArgumentParser class). -- François Lafont -- http://mail.python.org/mailman/listinfo/python-list
Re: [argparse] mutually exclusive group with 2 sets of options
Hi, On relfection, it's clear that: 1. the "(-a -b VALUE-B | -c -d VALUE-D)" syntax is not implemented by the argparse module; 2. and get this syntax with "argparse + hacking" is not very clean. So, finally I'll use the docopt module version 0.6.1. For the inheritance of common options, I'll used something like that (even if I prefer the oriented object side of the argparse module): [the main file] #--- import importlib import sys from docopt import docopt help_message =""" Usage: my-script (-h | --help) my-script [...] Options: -h, --help Show this help message and exit. Available commands: command1 Description1... command2 Description2... See 'my-script -h' for more information on a specific command. """ if __name__ == '__main__': args = docopt(help_message, options_first=True) command = args[''] try: m = importlib.import_module('mymodule.' + command) except ImportError: print("Sorry, the %s command doesn't exist. See 'my-script -h'.") % (command,) sys.exit(1) args = docopt(m.help_message, options_first=False) m.run(args) #--- [a file for each specific command, mymodule/command1.py etc.] #--- import mymodule.common as common help_message = """ Usage: my-script subcommand1 (-h | --help) my-script subcommand1 %s -w -c Options: %s -w , --warning= Some help. -c , --critical= Some help. """ % (common.common_syntax, common.common_help,) def run(args): pass #--- [and a file for the common syntax, mymodule/common.py] #--- common_syntax = """-H -t (--v2c -C | -l -x -X -L )""" common_help = """ -h, --help Show this help message and exit. -H , --host= Some help. -t , --timeout= Some help. --v2c Some help. -C , --community= Set the community password for SNMP V2c. # etc. # etc. """ #--- Thank you all. -- François Lafont -- http://mail.python.org/mailman/listinfo/python-list
Re: [argparse] mutually exclusive group with 2 sets of options
Le 07/08/2013 01:18, Francois Lafont a écrit : > For the inheritance of common options, I'll used something like > that (even if I prefer the oriented object side of the argparse > module): But I admit that this is a very simple and intelligent module. ;-) -- François Lafont -- http://mail.python.org/mailman/listinfo/python-list
Tell me the truth
In the python-ideas mailing list http://mail.python.org/pipermail/python-ideas/2007-March/thread.html there was a discussion about the fact that python has opeartors 'and', 'or' and 'not' (keywords of the language) but 'bool' is a type. Naturally we have (not not x) == bool(x) # always True If I take into account the fact that 'True' and 'False' are singletons (guaranteed ?) : (not not x) is bool(x) # should be always True. >>> def ok(x): ... return (not not x) is bool(x) ... >>> ok(0) True >>> ok(False) True ok(11) is True True Interesting Let's make another function: >>> def foo(x): ... return (not not x) is bool(x) is True ... >>> foo(False) False >>> foo(0.0) False >>> foo(0.1) True To see if the problem comes from using the a is b is True form, let's try >>> def ok3(x): ... return (not not x) is bool(x) == True ... >>> ok3(44) True >>> ok3(0) False After much head scrating and experimenting with dis.dis() I have found that chaining comparisons (with is or ==) a == b == c in Python is never a good idea. It is interpreted as ( a == b ) and ( b == c) Such a magic is fine when we write: if 0.0 <= x < 1.0: but it renders the outcome of if a == b == c: somewhat confusing. In the reference documentation the sentences """Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false). """ should be followed by a warning about chaining '==' or similar operators. Regards PS. Sorry for bad english. NOTICE: This message contains information which is confidential and the copyright of our company or a third party. If you are not the intended recipient of this message please delete it and destroy all copies. If you are the intended recipient of this message you should not disclose or distribute this message to third parties without the consent of our company. Our company does not represent, warrant and/or guarantee that the integrity of this message has been maintained nor that the communication is free of virus, interception or interference. The liability of our company is limited by our General Conditions of Services. Nota : Ce message contient des informations confidentielles propriété de notre société et/ou d'un tiers. Si vous n'êtes pas parmi les destinataires désignés de ce message, merci de l'effacer ainsi que toutes ses copies. Si vous êtes parmi les destinataires désignés de ce message, prière de ne pas le divulguer ni de le transmettre à des tiers sans l'accord de notre société. Notre société ne peut garantir que l'intégrité de ce message a été préservée ni que la présente communication est sans virus, interception ou interférence. La responsabilité de notre société est limitée par nos Conditions Générales de Services. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tell me the truth
Duncan Booth wrote : > francois.petitjean at bureauveritas.com wrote: >> After much head scrating and experimenting with dis.dis() I have found >> that chaining comparisons (with is or ==) a == b == c in Python is >> never a good idea. It is interpreted as >> ( a == b ) and ( b == c) >> Such a magic is fine when we write: >> if 0.0 <= x < 1.0: >> but it renders the outcome of if a == b == c: somewhat confusing. > I don't understand why it is confusing. What other meaning would you > expect? Consider: >>> Python = cool = True >>> Python is cool True >>> Python is cool is True True # heh it seems possible to tackle is True to a boolean expression (not always) >>> it = False >>> Python is cool is not it True # I knew that. slef evident... >>> Python = cool = False >>> Python is cool is not it False # Waht? desillution... # Ough! You must reread the reference documentation on chaining the comparison operators and understand the semantics (the two comparisons are 'anded' and 'and' shortcircuits in Python). >>> Python is cool True # Yeah! Regards NOTICE: This message contains information which is confidential and the copyright of our company or a third party. If you are not the intended recipient of this message please delete it and destroy all copies. If you are the intended recipient of this message you should not disclose or distribute this message to third parties without the consent of our company. Our company does not represent, warrant and/or guarantee that the integrity of this message has been maintained nor that the communication is free of virus, interception or interference. The liability of our company is limited by our General Conditions of Services. Nota : Ce message contient des informations confidentielles propriété de notre société et/ou d'un tiers. Si vous n'êtes pas parmi les destinataires désignés de ce message, merci de l'effacer ainsi que toutes ses copies. Si vous êtes parmi les destinataires désignés de ce message, prière de ne pas le divulguer ni de le transmettre à des tiers sans l'accord de notre société. Notre société ne peut garantir que l'intégrité de ce message a été préservée ni que la présente communication est sans virus, interception ou interférence. La responsabilité de notre société est limitée par nos Conditions Générales de Services. -- http://mail.python.org/mailman/listinfo/python-list
Simple calculation error
Dear all, I observed a strange calculation answer, on both python 2.3.4 and 2.4.4 >>> print 753343.44 - 753361.89 -18.450001 >>> print ( (753361.89*100) - (753343.44*100) ) / 100 18.45 Can somebody help me to play correctly with decimal values? Thanks in advance, Francois Liot -- http://mail.python.org/mailman/listinfo/python-list
RE: Simple calculation error
No the change of sign is due to a fake copy and past, My question was related to decimal calculation. Thanks, Francois Liot -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Paul McGuire Sent: Friday, January 04, 2008 1:46 PM To: python-list@python.org Subject: Re: Simple calculation error On Jan 4, 12:30 pm, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Francois Liot wrote: > > > I observed a strange calculation answer, on both python 2.3.4 and 2.4.4 > > >> >> print 753343.44 - 753361.89 > > > -18.450001 > > >> >> print ( (753361.89*100) - (753343.44*100) ) / 100 > > > 18.45 > > > Can somebody help me to play correctly with decimal values? > If the OP is questioning the change in sign, the operands are reversed in the second statement. -- Paul -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
How to clear a list (3 ways).
Consider the following code : #!/usr/bin/env python # -*- coding: latin_1 -*- """ container.py how to clear a container """ class Container(object): def __init__(self): self.elts = {} self.parts = [] def clear(self): self.elts.clear() # for a dictionary it's clear :-) self.parts = [] # (1) # self.parts[:] = []# (2) # del self.parts[:] # (3) def __len__(self): return len(self.parts) def add_predicate(self, part): """return True if part should be added (to override)""" return True def add(self, part): """return True if part is added""" res = self.add_predicate(part) if res: self.parts.append(part) self.elts[str(part)] = part # or wahtever return res def replace_parts(self, values): """return True if all values are added/replaced""" acceptable = all(map(self.add_predicate, values)) # FIXME itertools.imap ? if not acceptable: return acceptable self.parts[:] = values # TODO elts return acceptable Container1 = Container class Container2(Container): def clear(self): self.elts.clear() # for a dictionary it's clear :-) self.parts[:] = []# (2) class Container3(Container): def clear(self): self.elts.clear() # for a dictionary it's clear :-) del self.parts[:] # (3) Solution (1) is somewhat broken (see the test_container.rst hereafter) but often used. For instance in configobj we have def clear(self): """ A version of clear that also affects scalars/sections Also clears comments and configspec. Leaves other attributes alone : depth/main/parent are not affected """ dict.clear(self) self.scalars = [] self.sections = [] self.comments = {} self.inline_comments = {} self.configspec = {} What idiom do you use (and prefer) ? test_container.rst can be used like this : >>> import doctest >>> doctest.testfile('test_container.rst', encoding='latin_1') (nfails, ntests) is printed. === test_container.rst === >>> from container import Container1, Container2, Container3 >>> cont1 = Container1() >>> cont1.add(1) True >>> cont1.add(2) True >>> cont1.add(3) True >>> parts = cont1.parts >>> parts [1, 2, 3] The client has cached the parts attribute. Solution (1) is not robust, is the parts attribute in the public API ? >>> cont1.clear() >>> parts [1, 2, 3] >>> cont1.parts, parts ([], [1, 2, 3]) We now have two different objects. >>> cont2 = Container2() >>> cont2.add(21) True >>> cont2.add(22) True >>> cont2.add(23) True >>> parts2 = cont2.parts >>> parts2 [21, 22, 23] >>> cont2.clear() >>> parts2 [] >>> cont1.parts, parts ([], [1, 2, 3]) >>> cont3 = Container3() >>> cont3.add(31) True >>> cont3.add(32) True >>> parts3 = cont3.parts >>> cont3.add(33) True >>> parts3 [31, 32, 33] >>> cont3.clear() >>> parts3 [] Test de replace_parts >>> cont3 = Container3() >>> len(cont3) 0 >>> parts3 = cont3.parts >>> cont3.add(30) True >>> parts3 [30] >>> cont3.replace_parts( (31, 32, 33) ) True >>> parts3 [31, 32, 33] >>> Regards. NOTICE: This message contains information which is confidential and the copyright of our company or a third party. If you are not the intended recipient of this message please delete it and destroy all copies. If you are the intended recipient of this message you should not disclose or distribute this message to third parties without the consent of our company. Our company does not represent, warrant and/or guarantee that the integrity of this message has been maintained nor that the communication is free of virus, interception or interference. The liability of our company is limited by our General Conditions of Services. Nota : Ce message contient des informations confidentielles propriété de notre société et/ou d'un tiers. Si vous n’êtes pas parmi les destinataires désignés de ce message, merci de l'effacer ainsi que toutes ses copies. Si vous êtes parmi les destinataires désignés de ce message, prière de ne pas le divulguer ni de le transmettre à des tiers sans l’accord de notre société. Notre société ne peut garantir que l’intégrité de ce message a été préservée ni que la présente communication est sans virus, interception ou interférence. La responsabilité de notre société est limitée par nos Conditions Générales de Services.-- http://mail.python.org/mailman/listinfo/python-list
How to clear a list (3 ways).
(second try with an enhanced version) Executive summary : What idiom do you use for resetting a list ? lst = |] # (1) lst[:] = [] # (2) del lst[:] # (3) Consider the following code : #!/usr/bin/env python # -*- coding: latin_1 -*- """ container.py how to clear a container """ class Container(object): def __init__(self): self.elts = {} self.parts = [] def clear(self): self.elts.clear() # for a dictionary it's clear :-) self.parts = [] # (1) # self.parts[:] = []# (2) # del self.parts[:] # (3) def __len__(self): return len(self.parts) def add_predicate(self, part): """return True if part should be added (to override)""" return True def add(self, part): """return True if part is added""" res = self.add_predicate(part) if res: self.parts.append(part) self.elts[str(part)] = part # or wahtever return res def replace_parts(self, values): """return True if all values are added/replaced""" acceptable = all(map(self.add_predicate, values)) # FIXME itertools.imap ? if not acceptable: return acceptable self.parts[:] = values # TODO elts return acceptable Container1 = Container class Container2(Container): def clear(self): self.elts.clear() # for a dictionary it's clear :-) self.parts[:] = []# (2) class Container3(Container): def clear(self): self.elts.clear() # for a dictionary it's clear :-) del self.parts[:] # (3) Solution (1) is somewhat broken (see the test_container.rst hereafter) but often used. For instance in configobj we have def clear(self): """ A version of clear that also affects scalars/sections Also clears comments and configspec. Leaves other attributes alone : depth/main/parent are not affected """ dict.clear(self) self.scalars = [] self.sections = [] self.comments = {} self.inline_comments = {} self.configspec = {} Solution (2) does not suffer the same problem, and (3) has the advantage of not building an empty list. What idiom do you use (and prefer) ? test_container.rst can be used like this : >>> import doctest >>> doctest.testfile('test_container.rst', encoding='latin_1') (nfails, ntests) is printed. === test_container.rst === >>> from container import Container1, Container2, Container3 >>> cont1 = Container1() >>> cont1.add(1) True >>> cont1.add(2) True >>> cont1.add(3) True >>> parts = cont1.parts >>> parts [1, 2, 3] The client has cached the parts attribute. Solution (1) is not robust, is the parts attribute in the public API ? >>> cont1.clear() >>> parts [1, 2, 3] >>> cont1.parts, parts ([], [1, 2, 3]) We now have two different objects. >>> cont2 = Container2() >>> cont2.add(21) True >>> cont2.add(22) True >>> cont2.add(23) True >>> parts2 = cont2.parts >>> parts2 [21, 22, 23] >>> cont2.clear() >>> parts2 [] >>> cont1.parts, parts ([], [1, 2, 3]) >>> cont3 = Container3() >>> cont3.add(31) True >>> cont3.add(32) True >>> parts3 = cont3.parts >>> cont3.add(33) True >>> parts3 [31, 32, 33] >>> cont3.clear() >>> parts3 [] Test de replace_parts >>> cont3 = Container3() >>> len(cont3) 0 >>> parts3 = cont3.parts >>> cont3.add(30) True >>> parts3 [30] >>> cont3.replace_parts( (31, 32, 33) ) True >>> parts3 [31, 32, 33] >>> Regards. NOTICE: This message contains information which is confidential and the copyright of our company or a third party. If you are not the intended recipient of this message please delete it and destroy all copies. If you are the intended recipient of this message you should not disclose or distribute this message to third parties without the consent of our company. Our company does not represent, warrant and/or guarantee that the integrity of this message has been maintained nor that the communication is free of virus, interception or interference. The liability of our company is limited by our General Conditions of Services. Nota : Ce message contient des informations confidentielles propriété de notre société et/ou d'un tiers. Si vous n’êtes pas parmi les destinataires désignés de ce message, merci de l'effacer ainsi que toutes ses copies. Si vous êtes parmi les destinataires désignés de ce message, prière de ne pas le divulguer ni de le transmettre à des tiers sans l’accord de notre société. Notre société ne peut garantir que l’intégrité de ce message a été préservée ni que la présente communication est sans virus, interception ou interférence. La responsabilité de notre société est limitée par nos Conditions Générales de Services.-- http://mail.python.org/mailman/listinfo/python-list
Regex url
Hi, I try to match the following url with one regex /hello /hello/ /hello/world /hello/world/ world is a variable, I can put toto instead Thanks ! -- http://mail.python.org/mailman/listinfo/python-list
New instance of a class : not reset?
Hi, In the following example, I don't understand why attribute 'data' is not reset when I get the new instance of Bag It works if I make a copy (data[:]) but I want to understand why Thanks class Bag(object): def __init__(self, data = []): self.data = data #self.data = data[:] def add(self, x): self.data.append(x) bag = Bag() print bag.data bag.add('toto') print bag.data bag = Bag() # new instance of Bag, all attributes clear? print bag.data # 'toto' is still there !!! -- http://mail.python.org/mailman/listinfo/python-list
Re: New instance of a class : not reset?
Great thank you -- http://mail.python.org/mailman/listinfo/python-list
Fwd: permission denied using python 3.8
Outlook voor Android downloaden<https://aka.ms/ghei36> Van: Francois van Lieshout Verstuurd: maandag 4 november 2019 18:19 Aan: python-list@python.org Onderwerp: permission denied using python 3.8 Hi, i installed python 3.8 the latest version but it doesn’t work, i get “permission denied” when trying to acces python in the CLI and also i can’t run my code from my python files in the command-line nor in IDLE. I’d like to know why this is so. I’ve tried the 64 bit and the 32 bit executable version of 3.8 but without succes. So i reinstalled version 3.7.2 and now everything works fine again. Can you tell me why i can’t use python 3.8, i’m using windows as OS. Thanks. -- https://mail.python.org/mailman/listinfo/python-list
RE: Python-list Digest, Vol 196, Issue 26
for x in range( 0,10 ): stars = "" count = 0 while count < x: stars += "x" count += 1 print( stars ) x xx xxx x xx xxx x You've got already an "x" placed in your variable stars that's why. -Oorspronkelijk bericht- Van: Python-list Namens python-list-requ...@python.org Verzonden: zondag 26 januari 2020 18:00 Aan: python-list@python.org Onderwerp: Python-list Digest, Vol 196, Issue 26 Send Python-list mailing list submissions to python-list@python.org To subscribe or unsubscribe via the World Wide Web, visit https://mail.python.org/mailman/listinfo/python-list or, via email, send a message with subject or body 'help' to python-list-requ...@python.org You can reach the person managing the list at python-list-ow...@python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Python-list digest..." -- https://mail.python.org/mailman/listinfo/python-list
(Win32 API) callback to Python, threading hiccups
Hiho, could somebody please enlighten me about the mechanics of C callbacks to Python? My domain is more specifically callbacks from the win32 API, but I'm not sure that's where the problem lies. Here's a description... I want a callback-based MIDI input/processing, so PortMidi was not an alternative. I have written a C extension module that links to the mmsys MIDI API. I separated the win32-dependant code from the Python extension code, so a) the module interface is system-neutral, b) the implementation can be tested (re-used) outside of Python. So, that code is tested OK, but it might be useful to sketch it's design: - as you may know, the MIDI input on win32 is already managed thru a callback mechanism; the driver calls back your program with data buffers - yet, I don't call directly into Python from the callback, since some restrictions apply on what system calls you can make from there, and I don't know what Python's interpreter / scripts might call. - so, on callback, I create a new thread, after checking that the previous one has returned already (WaitOnSingleObject(mythread)) so we only have one thread involved. - this is this thread that calls the user callback, yet this callback isn't yet a Python callable, we're still in native C code. - on the side of the extension module now, I merely copied the callback example from the Python/C API doc, and added GIL management around the call: static PyObject * my_callback = NULL; //this gets fixed by a setCallback() func static void external_callback(const MidiData * const data) { if (my_callback && (my_callback != Py_None)) { if (! data) { PyErr_SetString(PyExc_IndexError, getLastErrorMessage()); } else { PyObject * arglist = NULL; PyObject * result = NULL; arglist = Py_BuildValue("(i,i,s#)", data->deviceIndex, data->timestamp, data->buffer, data->size);// 0, 0, "test", 4);// PyGILState_STATE gil = PyGILState_Ensure(); result = PyEval_CallObject(my_callback, arglist); PyGILState_Release(gil); Py_DECREF(arglist); Py_DECREF(result); } } } - this one above is what is actually passed as callback to the 'native' C part. So, here's what (I presume) happens: 1. the driver calls into my C code 2. my C code spawns a thread that calls into the extension 3. the extension calls into Python, after acquiring the GIL Now, here's the hiccup: inside a Python script, anytime a Python object is accessed by both the (Python) callback and the main program, I get a GPF :/ You bet I tried to use locks, Queues and whatnot, but nothing will do, it seems I get protection faults on accessing... thread exclusion objects. Yet, there is a way where it works seamlessly: it's when the __main__ actually spawns a threading.Thread to access the shared object. Hence I am lost. This works: def input_callback(msg): midiio.send(msg) ##MIDI thru, a call into my extension that wraps the implementation call with BEGIN_ALLOW_THREADS and END_... __main__: raw_input() This results in GPF as soon as the callback is fired: tape = Queue.Queue() def input_callback(msg): tape.put(msg) __main__: while True: print tape.get() This works like a charm: tape = Queue.Queue() def input_callback(msg): tape.put(msg) def job(): while True: print tape.get() __main__: t = threading.Thread(target = job) t.start() raw_input() Note that I also get a GPF in the later case if I stop the program with KeyInterrupt, but I guess it's something I'll look into afterwards... While I can think of hiding away the added threading within a wrapper module, I won't sleep well untill I figure out what's really happening. Could someone save my precious and already sparse sleeping time, by pointing me to what I'm missing here? WinXP SP1 Python 2.4.1 MinGW GCC 3.4.2 TIA, Francois De Serres -- http://mail.python.org/mailman/listinfo/python-list
Re: (Win32 API) callback to Python, threading hiccups
Christopher Subich wrote: >Francois De Serres wrote: > > >>- so, on callback, I create a new thread, after checking that the >>previous one has returned already (WaitOnSingleObject(mythread)) so we >>only have one thread involved. >> >> > >Uh... to me, this looks like a frighteningly inefficient way of doing >things. How about using a synchronous queue to post the data to a >processing thread? That way, you don't have to create an entierly new >thread each time you receive data in the callback. > > Thanks for the tip, and sorry for frightening you. Maybe you can help as well with my issue? Francois -- http://mail.python.org/mailman/listinfo/python-list
The GIL, callbacks, and GPFs
Hello, I'm chasing a GPF in the interpreter when running my extension module. It's not very elaborated, but uses a system (threaded) callback, and therefore the GIL. Help would be mucho appreciated. Here's the rough picture: win32_spam.c /* code here is unit-tested OK */ typedef struct Bag { char data[128]; site_t size; } Bag; typedef void (*PCallback)(const Bag * const bag); Bag bag; PCallback user_callback = NULL; /* called by windoz */ static void CALLBACK win32_call_me(void) { memcpy(bag.data, somestuff, 100); bag.size = 100; SPAWN_THREAD(user_callback, & bag);//pseudo-code } spam_module.c - /* most of the code here is pasted from doc */ static PyObject * my_callback = NULL; static PyObject * setCallback(PyObject *dummy, PyObject *args) { PyObject *result = NULL; PyObject *temp; if (PyArg_ParseTuple(args, "O:miSetCallback", &temp)) { if ((temp != Py_None) && (!PyCallable_Check(temp))) { PyErr_SetString(PyExc_TypeError, "parameter must be callable"); return NULL; } Py_XINCREF(temp); Py_XDECREF(my_callback); my_callback = temp; Py_INCREF(Py_None); result = Py_None; /* set the actual callback in win32_spam.c */ user_callback = & external_callback; } return result; } static void external_callback(const Bag * const bag) { if (my_callback && (my_callback != Py_None)) { PyObject * arglist = NULL; PyObject * result = NULL; arglist = Py_BuildValue("(s#)", bag->data, bag->size); /* do it */ PyGILState_STATE gil = PyGILState_Ensure(); result = PyEval_CallObject(my_callback, arglist); PyGILState_Release(gil); Py_DECREF(arglist); Py_DECREF(result); } } blowup_spam1.py - # This throws a GPF on callback. # Why, since the GIL is acquired by the callback? import spam.py def callback(stuff): print stuff if __name__ == '__main__': spam.setCallback(callback) raw_input() blowup_spam2.py - # This throws a GPF as well # Why, since we're using a threadsafe queue? import spam import Queue q = Queue.Queue() def callback(stuff): q.put(stuff) if __name__ == '__main__': spam.setCallback(callback) while True: print q.get() nice_spam.py - # This works # Why, since the rest did not? import spam import Queue import threading q = Queue.Queue() def callback(stuff): q.put(stuff) def job(): while True: print q.get() if __name__ == '__main__': spam.setCallback(callback) t = threading.Thread(job) t.start() raw_input() Please point me to what I'm doing wrong... TIA, Francois PS: I've already submitted my issue under "(Win32 API) callback to Python, threading hiccups", but I guess it was poorly formulated. -- http://mail.python.org/mailman/listinfo/python-list
Building a function call?
Hiho, Having a string: "dothat" and a tuple: (x, y) 1. What's the best way to build a function call like: dothat(x,y)? Assuming dothat is def'd in the same module, 2. is: eval("dothat(x,y)", None, (('x', 100), ('y', 200))) the right way to have it executed? If dothat is def'd in another module: 3. what would be the right way to initialize the globals to pass to eval ? TIA, Francois -- http://mail.python.org/mailman/listinfo/python-list
**kwargs?
All your **kwargs are belong to us. *args is documented in the Tutorial. I reckon **kwargs represents a dictionary of arguments. But I don't quite get the semantics of **x. Undefined length tuple of undefined length tuples? Are there other practical use cases for ** (common enough please, I wish I was, but I'm not a scientist). TIA, Francois -- http://mail.python.org/mailman/listinfo/python-list
Re: Building a function call?
Roland Heiber wrote: >Francois De Serres wrote: > > >>Hiho, >> >>Having a string: "dothat" >>and a tuple: (x, y) >>1. What's the best way to build a function call like: dothat(x,y)? >> >> > >Not the best (not at all) but one way: > > Still pretty interesting, thx. >def dothat(x,y): > print "Called with:", x, y > >c = (1,2) > >locals().get("dothat")(*c) > > >Called with: 1 2 > >HtH, Roland > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Building a function call?
Peter Hansen wrote: >Roland Heiber wrote: > > >>Not the best (not at all) but one way: >> >>def dothat(x,y): >> print "Called with:", x, y >> >>c = (1,2) >> >>locals().get("dothat")(*c) >> >> > >As you say, not the best, but in fact not really advisable under any >circumstances. locals() returns "module level" stuff only when executed >_at_ module level (i.e. not inside a function). Otherwise it will >return only the local variables of the frame its in. > >Better is this, since it works for the OP's requirements in either >situation. > > globals()['dothat'](*c) > >-Peter > > Actually that's part of my dilemma... I guess the function name is moreoften in globals(). But the parameters in the tuple are A) formals (that was not clear in my OP, sorry), B) once bound by another function, I they'll become part of locals(). func = "dothat" args = (x,y) localcontext = () r = None while r is None: try: r = eval("dothat(x,y)", None, localcontext) #how do I construct "dothat(x,y)"? except NameError: ensure_context(x,y) #find/compute the formals, eg: localcontext = (('x', 100), ('y', 200)) F. -- http://mail.python.org/mailman/listinfo/python-list
Re: **kwargs?
Michael Hoffman wrote: >Peter Hansen wrote: > > >>Francois De Serres wrote: >> >> >> > > > >>>*args is documented in the Tutorial. I reckon **kwargs represents a >>>dictionary of arguments. But I don't quite get the semantics of **x. >>>Undefined length tuple of undefined length tuples? Are there other >>>practical use cases for ** (common enough please, I wish I was, but >>>I'm not a scientist). >>> >>> >>Where did you get "tuples of tuples" for **x ? >> >> > >I would guess it is confusion from languages where unary * means >dereference and ** means double dereference. > > That was precisely my mistake. >To the OP: I'm glad you have been reading the tutorial. If you have >further questions the reference manual is a good place to look: > >http://docs.python.org/ref/function.html > > Now I see, there's a '**' token, which is not the same as two adjacents '*' tokens. >Just as you can use a name other than self as the first argument to an >unbound method, you can call your *args and **kwargs *x and **y instead, >but they will still act just like *args and **kwargs. > >The stars are magic, not the names. > > Thanks mucho! F. -- http://mail.python.org/mailman/listinfo/python-list
Re: Building a function call? (update)
Francois De Serres wrote: >Hiho, > >Having a string: "dothat" >and a tuple: (x, y) >1. What's the best way to build a function call like: dothat(x,y)? > >Assuming dothat is def'd in the same module, >2. is: eval("dothat(x,y)", None, (('x', 100), ('y', 200))) >the right way to have it executed? > >If dothat is def'd in another module: >3. what would be the right way to initialize the globals to pass to eval ? > > >TIA, >Francois > > > > Sorry, I was unclear about the fact that the args are formals. I'm trying to do something like: func = "dothat" args = ('x','y') localcontext = () r = None while r is None: try: r = eval("dothat(x,y)", None, localcontext) #how do I construct "dothat(x,y)"? with 'dothat(%s,%s)' % args? except NameError: ensure_context(args) #find/compute the formals, eg: localcontext = (('x', 100), ('y', 200)) F. -- http://mail.python.org/mailman/listinfo/python-list
Re: Building a function call?
Steven D'Aprano wrote: >On Wed, 13 Jul 2005 06:16:54 -0700, Robert Kern wrote: > > > >>Duncan Booth wrote: >> >> >>>Francois De Serres wrote: >>> >>> >>> >>>>Having a string: "dothat" >>>>and a tuple: (x, y) >>>>1. What's the best way to build a function call like: dothat(x,y)? >>>> >>>> >[snip] > > >>>No, none of this is a good place to use eval. >>> >>> >[snip] > > >>> import otherModule >>> vars(otherModule)[aString](*aTuple) >>> >>> >>Ick! Please: >>getattr(otherModule, aString)(*aTuple) >> >> > > >Or, remember that functions are first class objects in Python. Instead of >passing around the function name as a string, pass around a reference to >the function itself. Something like this: > > >def dothis(x,y): >return x-y > >def dothat(x,y): >return x+y > >somefunction = dothis >somefunction(3, 2) >=> returns 1 > >somefunction = dothat >somefunction(3, 2) >=> returns 5 > >allfunctions = [dothis, dothat] >for func in allfunctions: >print func(3, 2) >=> prints 1 then 5 > >If you want to collect user-supplied strings and use them to find a >function, you could use eval (terribly risky and unsafe), or you could do >something like this: > >funcnames = {} >for func in allfunctions: >funcnames[func.__name__] = func >F = raw_input("Enter the name of a function: ") >try: >funcnames[F](3, 2) >except KeyError: >print "Function '%s' not found!" % F > > >In my humble opinion, people muck about with eval, locals and globals far >too often. It is unclear, hard to maintain, and frequently a security >risk. These confusing, unsafe practices can usually be avoided by >remembering that functions are first class objects just like ints, strings >and lists. > > > > > I'm aware of the functions being objects, but I really need to work with strings in that case. Still, I was not aware that eval() was such a 'rogue', it's not said in the manuals ;) Many thanks! F. -- http://mail.python.org/mailman/listinfo/python-list
Re: Building a function call? (update)
Duncan Booth wrote: >Francois De Serres wrote: > > > >>Sorry, I was unclear about the fact that the args are formals. I'm >>trying to do something like: >> >>func = "dothat" >>args = ('x','y') >>localcontext = () >>r = None >>while r is None: >>try: >>r = eval("dothat(x,y)", None, localcontext) #how do I construct >>"dothat(x,y)"? with 'dothat(%s,%s)' % args? >>except NameError: >>ensure_context(args) #find/compute the formals, eg: localcontext >>= (('x', 100), ('y', 200)) >> >> > >I think when your code ends up in as big a mess as this, you need to take a >step back. Forget about asking us how you get at functions or arguments as >strings, instead tell us what problem you are really trying to solve. > >I'm pretty sure that whatever your problem is, the solution won't involve >using 'eval', and it almost certainly won't involve looking up variables >or functions from their names. > > Sure? okay, let me take the time to formulate a thorough blueprint of my context, and I'll come back to you... F. -- http://mail.python.org/mailman/listinfo/python-list
Re: Building a function call? (update)
Scott David Daniels wrote: >Francois De Serres wrote: > > >>Francois De Serres wrote: >> >> >>>Having a string: "dothat" >>>and a tuple: (x, y) >>>1. What's the best way to build a function call like: dothat(x,y)? >>> >>>Assuming dothat is def'd in the same module, >>>2. is: eval("dothat(x,y)", None, (('x', 100), ('y', 200))) >>>the right way to have it executed? >>> >>> > >You do know that you could do something like: > result = eval('dothat')(100, 200) > >That is, eval of the function name gives you a function. > >--Scott David Daniels >[EMAIL PROTECTED] > > No I did not know. And, that, is smart! Many thanks, F. -- http://mail.python.org/mailman/listinfo/python-list
Documenting extension modules?
Hiho, I can't seem to find a proper way to document my extension module. Following the C API doc: static PyMethodDef ioMethods[] = { {"o_count", o_count, METH_VARARGS, "Return the count of available MIDI outputs."}, } lacks: a) module level documentation b) function parameters Also, I'd like to know if there's a typical format for the help string (but in C), compatible with docstring's """short desription long description""" Any pointers please? TIA, Francois -- http://mail.python.org/mailman/listinfo/python-list
Re: Documenting extension modules?
Robert Kern wrote: >Francois De Serres wrote: > > >>Hiho, >> >>I can't seem to find a proper way to document my extension module. >>Following the C API doc: >> >>static PyMethodDef ioMethods[] = { >>{"o_count", o_count, METH_VARARGS, "Return the count of available >>MIDI outputs."}, >> >>} >> >>lacks: >>a) module level documentation >> >> > >In your init function, assign a PyStr object to __doc__. > > > >>b) function parameters >> >> > >Add that information to your docstring. I don't think that there is a >way to get this information via inspect. > > > >>Also, I'd like to know if there's a typical format for the help string >>(but in C), compatible with docstring's >>"""short desription >> >>long description""" >> >> > >char *o_count__doc__; >char *o_count__doc__ = "short description\n"\ >"\n"\ >"long description\n"; > > > Mucho thankees Robert. -- http://mail.python.org/mailman/listinfo/python-list
Re: Documenting extension modules?
Simon Dahlbacka wrote: >Re: assigning a PyStr object to __doc__, take a look at Py_InitModule3, >which does that for you. > > > got it, thx. >Then you have the PyDoc_STRVAR macro in python.h that you might want to >use (see definition below). But as Robert already told you, you'll need >to provide the necessary information about i.e. parameters yourself in >the docstrings. > >/* Define macros for inline documentation. */ >#define PyDoc_VAR(name) static char name[] >#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str) >#ifdef WITH_DOC_STRINGS >#define PyDoc_STR(str) str >#else >#define PyDoc_STR(str) "" >#endif > > > beautiful. PS: The following snip from http://python.fyxm.net/peps/pep-0007.html was helpful to me in understanding usage of these macros: - Use the PyDoc_STR() or PyDoc_STRVAR() macro for docstrings to support building Python without docstrings (./configure --without-doc-strings). For C code that needs to support versions of Python older than 2.3, you can include this after including Python.h: #ifndef PyDoc_STR #define PyDoc_VAR(name) static char name[] #define PyDoc_STR(str) (str) #define PyDoc_STRVAR(name, str) PyDoc_VAR(name) = PyDoc_STR(str) #endif - The first line of each fuction docstring should be a "signature line" that gives a brief synopsis of the arguments and return value. For example: PyDoc_STRVAR(myfunction__doc__, "myfunction(name, value) -> bool\n\n\ Determine whether name and value make a valid pair."); Always include a blank line between the signature line and the text of the description. If the return value for the function is always None (because there is no meaningful return value), do not include the indication of the return type. - When writing multi-line docstrings, be sure to always use backslash continuations, as in the example above, or string literal concatenation: PyDoc_STRVAR(myfunction__doc__, "myfunction(name, value) -> bool\n\n" "Determine whether name and value make a valid pair."); Though some C compilers accept string literals without either: /* BAD -- don't do this! */ PyDoc_STRVAR(myfunction__doc__, "myfunction(name, value) -> bool\n\n Determine whether name and value make a valid pair."); not all do; the MSVC compiler is known to complain about this. -- http://mail.python.org/mailman/listinfo/python-list
Exception in callback => GPF?
Hiho, When there's an unhandled exception in my extension-module's-callback-into-Python-function-object, I get a GPF and Python exits. When the exception is being handled within the callback (hence in Python), I get a very significant hiccup (1 to 5 seconds freeze). Question: is there a specific way to deal with exceptions when they happen inside such a callback? I searched the web with no avail. All callbacks examples I've seen seem not to care about exceptions. Maybe of importance: the callback is initiated within a native thread. My C extension module is calling back into Python like this: /* Python callback function object. */ static PyObject * my_callback = NULL; /* C callback function passed to the implementation. */ static void external_callback(const MidiData * const data) { if (my_callback && (my_callback != Py_None)) { if (! data) { PyErr_SetString(PyExc_IndexError, getLastErrorMessage()); } else { PyObject * arglist = NULL; PyObject * result = NULL; PyGILState_STATE gil = PyGILState_Ensure(); arglist = Py_BuildValue("(i,i,s#)", data->deviceIndex, data->timestamp, data->buffer, data->size);// 0, 0, "test", 4);// result = PyEval_CallObject(my_callback, arglist); Py_DECREF(arglist); Py_DECREF(result); PyGILState_Release(gil); } } } TIA, Francois -- http://mail.python.org/mailman/listinfo/python-list
tuple to string?
hiho, what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to the string 'spam'? TIA, Francois -- http://mail.python.org/mailman/listinfo/python-list
Re: tuple to string?
Francois De Serres wrote: >hiho, > >what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to >the string 'spam'? > >TIA, >Francois > > thanks to all! I'll pick ('%c' * len(t)) % t, for it's readability and the fact that join() is on the deprec'd list. Francois -- http://mail.python.org/mailman/listinfo/python-list
Re: tuple to string?
Robert Kern wrote: >Francois De Serres wrote: > > > >>I'll pick ('%c' * len(t)) % t, for it's readability and the fact that >>join() is on the deprec'd list. >> >> > >''.join() is certainly not deprecated. What made you think that? > > > this: http://www.python.org/doc/2.4.1/lib/node110.html but I now realize it's a different version of join() that was proposed here... thank you, Francois -- http://mail.python.org/mailman/listinfo/python-list
Real time plot
I am reading a large amount of data from a COM port (or from a file) at a rate of 20 records per second. It is about positioning of a vehicle on a race track. In each record we have time, position (lat and long) speed, course all from GPS equipment. I would like to Produce a graph in real time with these records to see the vehicle moving on the track. The goal is to study the vehicle performance and to always have its position, the application would run around 24 hours. I would like to write the application in Python and I am wondering which plot interface would be the more appropriate, I have seen matplotlib, but I do not know how to make real time applications with big amount of data. Is hippodraw more suited to this use or an other interface? Many thanks jfc -- http://mail.python.org/mailman/listinfo/python-list
Re: Real time plot
"Hendrik van Rooyen" <[EMAIL PROTECTED]> a écrit dans le message de news: [EMAIL PROTECTED] > Jean-Francois Canac wrote: > >>I am reading a large amount of data from a COM port (or from a file) at a >>rate > of 20 records per second. It is about >positioning of a vehicle on a race > track. >>In each record we have time, position (lat and long) speed, course all >>from GPS > equipment. I would like to Produce a >graph in real time with these > records to > see the vehicle moving on the track. The goal is to study the vehicle >>performance and to always have its position, the application would run >>around > 24 hours. > > >>I would like to write the application in Python and I am wondering which >>plot > interface would be the more appropriate, I >have seen matplotlib, but I do > not > know how to make real time applications with big amount of data. Is > hippodraw > more >suited to this use or an other interface? >>Many thanks > > I would draw dots on a suitably sized Tkinter canvas, after drawing a > schematic > of the race track (laborious). > > 20 per second will be no problem, provided the machine is half decent. > > What is the speed at which the com port runs? > > - Hendrik > The com port run at 19.2 kb/s. My pb is more the real time aspect: to see the plot changing in real time than the com aspect as the installation is already running with programs in c++. The PC on which it is running is good enought For me the interest is to migrate all taht on python to be able to make fast changes when it is of interest Jean-Francois > -- http://mail.python.org/mailman/listinfo/python-list
Please Help me
I am very new in Python and I would like to write an application which takes records from a COM port (or from a file to replay) and draw a real time Plot. But I am confused I have looked at Matplotlib, and a little at hippodraw, I have seen that there are many plot interfaces . But I really dont know which I should take for producing plot with data coming from a steam Please could you help? Many thanks Jean-François -- http://mail.python.org/mailman/listinfo/python-list
Re: Real time plot
"Lawson Hanson" <[EMAIL PROTECTED]> a écrit dans le message de news: [EMAIL PROTECTED] > Nicholas Bastin wrote: >> On 10/4/07, Jean-Francois Canac <[EMAIL PROTECTED]> wrote: >>> [EMAIL PROTECTED] >>>> I would draw dots on a suitably sized Tkinter canvas, after drawing a >>>> schematic >>>> of the race track (laborious). >>>> >>>> 20 per second will be no problem, provided the machine is half decent. >>>> >>>> What is the speed at which the com port runs? >>>> >>>> - Hendrik >>>> >>> The com port run at 19.2 kb/s. >>> My pb is more the real time aspect: to see the plot changing in real >>> time >>> than the com aspect as the installation is already running with programs >>> in >>> c++. >>> The PC on which it is running is good enought >>> For me the interest is to migrate all taht on python to be able to make >>> fast >>> changes when it is of interest >> >> The success of this will have more to do with design than programming >> language. Any GUI toolkit which allows partial screen updates can be >> made to update at least as fast as your screen refresh rate, even in >> python. >> >> -- >> Nick > > If you are using Tkinter (probably a very good choice), just remember > to make periodic (i.e., once per I/O processing loop) calls to: > > Tkinter.update_idletasks() > > which will update the display window, etc. > > Regards, > > Lawson > Finally I tried this coding and it works #== import time import pylab pylab.ion() lat,long,vit = pylab.load(r"c:\pytst\test1.txt",usecols=(1,2,3), unpack=True) lat_min=pylab.amin(lat) lat_max=pylab.amax(lat) long_min=pylab.amin(long) long_max=pylab.amax(long) print "start" timefig = pylab.figure(1) timesub = pylab.subplot(111) x=[] y=[] lines = pylab.plot(x,y) print lat_min,lat_max,long_min,long_max for i in range(len(lat)): x.append(long[i]) y.append(lat[i]) lines[0].set_data(x,y) timesub.set_xlim(long_min,long_max) timesub.set_ylim(lat_min,lat_max) pylab.draw() # The kind of data I have in the text file are just below Now I have two problems, I think my coding is not very clean as when I try yo close or manipulate the windows it generates an error secondly I think it is a bit slow. I would much prefer something quicker and to be able to adapt the speed dynamically with a sleep instruction or something as that 09:25:08.50 46.863930 3.161866 56.9 Km/h 09:45:07.75 46.863907 3.161786 11.5 Km/h 09:45:08.0 46.863914 3.161794 12.4 Km/h 09:45:08.25 46.863918 3.161804 13.4 Km/h 09:45:08.50 46.863922 3.161814 14.5 Km/h 09:45:08.75 46.863930 3.161825 15.4 Km/h 09:45:09.0 46.863934 3.161837 16.1 Km/h 09:45:09.25 46.863941 3.161848 16.6 Km/h 09:45:09.50 46.863945 3.161861 17.1 Km/h 09:45:09.75 46.863953 3.161874 17.3 Km/h -- http://mail.python.org/mailman/listinfo/python-list