match point
Hi, Reading the docs about regular expressions, I am under the impression that calling re.match(pattern, string) is exactly the same as re.search(r'\A'+pattern, string) Same for fullmatch, that amounts to re.search(r'\A'+pattern+r'\Z', string) The docs devote a chapter to "6.2.5.3. search() vs. match()", but they only discuss how match() is different from search() with '^', completely eluding the case of search() with r'\A'. At first I thought those functions could have been introduced at a time when r'\A' and r'\Z' did not exist, but then I noticed that re.fullmatch is a recent addition (python 3.4) Surely the python devs are not cluttering the interface of the re module with useless functions for no reason, so what am I missing? Maybe re.match has an implementation that makes it more efficient? But then why would I ever use r'\A', since that anchor makes a pattern match in only a single position, and is therefore useless in functions like re.findall, re.finditer or re.split? Thanks, Thierry -- https://mail.python.org/mailman/listinfo/python-list
Yet Another Software Challenge
For those interested in programming riddles, I would like to announce a new programming challenge I'm just launching at http://software.challenge.googlepages.com This challenge is in its early stage and thus set to be continuously improved. I would be especially interested in your comments and feedbacks about this initiative and its relevance. Enjoy! Thierry -- http://mail.python.org/mailman/listinfo/python-list
Re: Yet Another Software Challenge
On May 14, 3:40 pm, Paul McGuire <[EMAIL PROTECTED]> wrote: > > In Riddle 2, the "global" declarations are unnecessary, as you are > only referencing the globally-defined vars for read. > > Also in Riddle 2, I would replace > for s in alphabet: indices[s] = alphabet.index(s) > with > indices = dict( (s,i) for i,s in enumerate(alphabet) ) > > (I see part of your Python Challenge as giving new Pythoners something > to cut their teeth on, and so this is an opportunity for giving > examples of good style.) > > I do enjoy these challenges, they are quite addicting. :) > > -- Paul Hi Paul, Many thanks for your feedback and your example of good Python style. Even though the challenge is opened to any language practitioner, I must confess that I personally prefer Python and that languages like Python and Ruby are likely to be the most effective to solve the riddles. Don't hesitate to report any issue you could find. Enjoy. Cheers. Thierry -- http://mail.python.org/mailman/listinfo/python-list
sys.stdout, urllib and unicode... I don't understand.
Hello fellow pythonists, I'm a relatively new python developer, and I try to adjust my understanding about "how things works" to python, but I have hit a block, that I cannot understand. I needed to output unicode datas back from a web service, and could not get back unicode/multibyte text before applying an hack that I don't understand (thank you google) I have realized an wxPython simple application, that takes the input of a user, send it to a web service, and get back translations in several languages. The service itself is fully UTF-8. The "source" string is first encoded to "latin1" after a passage into unicode.normalize(), as urllib.quote() cannot work on unicode >>srcText=unicodedata.normalize('NFKD',srcText).encode('latin1','ignore') After that, an urllib request is sent with this encoded string to the web service >>con=urllib2.Request(self.url, headers={'User-Agent':'Mozilla/5.0 (X11; U; >>Linux i686) Gecko/20071127 Firefox/2.0.0.11'}, >>origin_req_host='http://translate.google.com') >>req=urllib2.urlopen(con) First problem, how to determine the encoding of the return ? If I inspect a request from firefox, I see that the server return header specify UTF-8 But if I use this code: >>ret=U'' >>for line in req: >> ret=ret+string.replace(line.strip(),'\n',chr(10)) I end up with an UnicodeDecodeError. I tried various line.decode(), line.normalize and such, but could not make this error disapear. I, until now, avoided that problem as the service always seems to return 1 line, but I am wondering. Second problem, if I try an >>print line into the loop, I too get the same error. I though that unicode() would force python to consider the given text as unicode, not to try to convert it to unicode. Here again, trying several normalize/decode combination did not helped at all. Then, looking for help through google, I have found this post: http://mail.python.org/pipermail/python-list/2007-October/462977.html and I gave it a try. What I did, though, was not to override sys.stdout, but to declare a new writer stream as a property of my main class: >>self.out=OutStreamEncoder(sys.stdout, 'utf-8') But what is strange, is that since I did that, even without using this self.out writer, the unicode translation are working as I was expecting them to. Except on the for loop, where a concatenation still triggers the UnicodeDecodeErro exception. I know the "explicit is better than implicit" python motto, and I really like it. But here, I don't understand what is going on. Does the fact that defining that writer object does a initialization of the standard sys.stdout object ? Does it is related to an internal usage of it, maybe in urllib ? I tried to find more on the subject, but felt short. Can someone explain to me what is happening ? The full script source can be found at http://www.webalis.com/translator/translator.pyw -- http://mail.python.org/mailman/listinfo/python-list
Re: sys.stdout, urllib and unicode... I don't understand.
Thank you to both of you (Marc and Tino). I feel a bit stupid right now, because as both of you said, encoding my source string to utf-8 do not produce an exception when I pass it to urllib.quote() and is what it should be. I was certain that this created an error sooner, and id not tried it again. The result of 2 days making random changes and hoping it works. I know, reflection should have primed. My bad... The same goes for my treatment in the iteration over the request result. I now have an >> line=line.encode('utf-8') and no errors (as long as I don't try to print this to stdout, which I understand). So, I'm now really getting back an unicode string that I can handle as such. I really am confused about what I was trying to do... I cannot understand what I did that caused those errors, because the state the script is now correspond to what I have in mind originally. >>BTW: ``line.strip()`` removes all whitespace at both ends *including >>newlines*, so there are no '\n' to replace anymore. Not exactly... It's that I receive a string, with 2 literal characters in it: "\" and "n". What I (want to) do here is that I replace those 2 characters with 1 chr(10). >>And functions in the >>`string` module that are also implemented as method on `str` or `unicode` >>are deprecated. I actually had read that, but not modified my code. Thank to point it out Anyway, thanks again to both of you. I'm quite happy to see it working the way I intended. -- http://mail.python.org/mailman/listinfo/python-list
Re: sys.stdout, urllib and unicode... I don't understand.
> Are you sure that Python wasn't just printing out "\n" because you'd > asked it to show you the repr() of a string containing newlines? Yes, I am sure. Because I dumped the ord() values to check them. But again, I'm stumped on how complicated I have made this. I should not try to code anymore at 2am. -- http://mail.python.org/mailman/listinfo/python-list
new to python, looking for streams clues
Hello peoples, As I said, I'm new to python, and particularly to XML generation in python. Using the 4suite XML package, I have been able to produce XML, but only directly to STDOUT. Refering to the 4suite markupWriter refrence, the class needs a stream to output the generated XML, and if none is specified, it's the STDOUT stream that is used. What I would like, would be to store the generated XML into a python object which implement the stream interface to be able to transform it via XSLT if needed (it's in a web based project). But, I've read the python doc for the last 12 hours without finding anything about an existing object that implements that interface. Am I missing something, or should I really create that object myself ? I mean, I just need something that I can write into and read thereafter. It should already exists, no ? -- http://mail.python.org/mailman/listinfo/python-list
Re: new to python, looking for streams clues
On Jun 4, 1:50 pm, Bruno Desthuilliers wrote: > Thierry a écrit : > > > > > Hello peoples, > > > As I said, I'm new to python, and particularly to XML generation in > > python. > > Using the 4suite XML package, I have been able to produce XML, but > > only directly to STDOUT. > > > Refering to the 4suite markupWriter refrence, the class needs a stream > > to output the generated XML, and if none is specified, it's the STDOUT > > stream that is used. > > > What I would like, would be to store the generated XML into a python > > object which implement the stream interface to be able to transform it > > via XSLT if needed (it's in a web based project). > > > But, I've read the python doc for the last 12 hours without finding > > anything about an existing object that implements that interface. > > Am I missing something, or should I really create that object myself ? > > > I mean, I just need something that I can write into and read > > thereafter. > > It should already exists, no ? > > It does, it's named StringIO (or cStringIO for the faster C > implementation), and it's part of the standard lib. AFAICT, it should > fit your needs. Thanks everyone. I had seen it, but understood that you had to give it an already existing string to operate on that one. I feel a bit stupid. Time to get back to those lost sleep hours, I presume. But again, thanks everyone. -- http://mail.python.org/mailman/listinfo/python-list
Re: what is meaning of "@" in pyhon program.
> ie: > @if os.exists(foo): > etc > etc > > and > > @for blah: > etc > etc > This sounds more like PHP code, where a @ prefixing a function means that even if there are errors or warnings, you don't want to see them. -- http://mail.python.org/mailman/listinfo/python-list
Help in File selector window in pygtk
Let's say I have a main window which has a File menu. When I click on the File menu and the open button, I have a File selector window which comes in front of my main window. How do I make the main window unselectable? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Help with modal in Gtk::FileChooserDialog
Does anyone know how to set modal to True for Gtk::FileChooserDialog? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Mapping a drive to a network path
In DOS, if I want to map a network path to a drive, I do the following: net use z: \\computer\folder How do we do something similar in python? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Re: Mapping a drive to a network path
Thanks, that helps. Thierry -- http://mail.python.org/mailman/listinfo/python-list
Mapping network drive on Linux
On windows, if I want to map a network drive to a local drive on my computer, I do the following: data = { 'remote' : '\\blah\data', 'local' : 'Z:' } win32net.NetUseAdd(None, 1, data) How am I supposed to do similar thing on Linux? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Calling ftp commands from python
Is it possible to run an ftp command to connect to some remote computer on the network. For example, if I want to retrieve some data from \\remcomputer\datafiles on the network and copy it to my local computer, how do I do it in python on the Unix side? I don't want to use mount since I don't have permission. Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Python xml.dom, help reading attribute data
Let's say I have the following xml tag: 1 I can't figure out what kind of python xml.dom codes I should invoke to read the data 1? Any help please? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Ignoring ampersand(&) as a special character in xml
Let's say I have the following xml tag: a & b Currently, an xml parser will treat & as a special character. Does anyone know the special characters to use around the ampersand so that the xml parser can treat "a & b" as a whole value? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Writing at the beginning of a file
Let's say I already wrote a file and have the following: testing testing testing testing testing testing Is there an easy way to write something of variable length at the top of the file? For example, 6 testing written testing testing testing testing testing testing I tried to write some garbage on top right after opening the file and then use seek to overwrite the garbage, but since the string to be written can be of variable length, I'm not sure how much garbage I have to write initially. The other way to do what I want is to write the whole thing to a new file, but I want to skip that method if there's an alternative way. Another way of doing it is to buffer the whole file writing into some variable, but that means I have to change 2000+ lines of codes and change fp.write() to something else. Any suggestions please? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Creating Pie Chart from Python
Let's say I have the following data: 500 objects: -100 are red -300 are blue -the rest are green Is there some python package which can represent the above information in a pie chart? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Creating Pie Chart from Python
Let's say I have the following data: 500 objects: -100 are red -300 are blue -the rest are green Is there some python package which can represen the above information in a pie chart? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating Pie Chart from Python
In a web browser, having a pie chart in some image format will be great. -- http://mail.python.org/mailman/listinfo/python-list
Walking through directories and files
I'm trying to use the os.walk() method to search all the directory from a root directory and display their contents. For example, I want my output to be like the following: directoryA stuffs.c stuffs2.cpp directoryB asd.c asdf.cpp Any ideas how to do it? Currently, I can only print all the filenames first and then the directory names. Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating Pie Chart from Python
Those python pie chart add ons are not very useful. For my specific pie chart, I have some 6-8 items to show up and some of them occupy only 2-5% of the pie. This cause the names and percentages to overlap each other. -- http://mail.python.org/mailman/listinfo/python-list
Re: match point
I found the story behind the creation of re.fullmatch(). I had no luck before because I was searching under "www.python.org/dev", while in reality it sprang out of a bug report: https://bugs.python.org/issue16203 In summary, there were repeated bugs where during maintenance of code the $ symbol disappeared from patterns, hence the decision to create a function that anchors the pattern to the end of the string independently of the presence of that symbol. I am perplexed by what I discovered, as I would never have thought that such prominent functions can be created to scratch such a minor itch: The creation of fullmatch() might address this very specific issue, but I would tend to think that if really certain symbols disappear from patterns inside a code base, this should be seen as the sign of more profound problems in the code maintenance processes. Anyway, the discussion around that bug inspired me another argument that is more satisfying: When I was saying that re.fullmatch(pattern, string) is exactly the same as re.search(r'\A'+pattern+r'\Z', string) I was wrong. For example if pattern starts with an inline flag like (?i), we cannot simply stick \A in front of it. Other example, consider pattern is 'a|b'. We end up with: re.search(r'\Aa|b\Z', string) which is not what we want. To avoid that problem we need to add parentheses: re.search(r'\A('+pattern+r')\Z', string) But now we created a group, and if the pattern already contained groups and backreferences we may just have broken it. So we need to use a non-capturing group: re.search(r'\A(?:'+pattern+r')\Z', string) ...and now I think we can say we are at a level of complexity where we cannot reasonably expect the average user to always remember to write exactly this, so it makes sense to add an easy-to-use fullmatch function to the re namespace. It may not be the real historical reason behind re.fullmatch, but personally I will stick with that one :) Cheers, Thierry -- https://mail.python.org/mailman/listinfo/python-list
problem while using os.walk with utf-8 characters
Hi, I'm using the os.walk function for parsing files from an external mass-storage such as usbkey. When i try the following code, i have an error: from __future__ import unicode_literals import sys reload(sys) sys.setdefaultencoding('utf_8') for dirname, dirnames, filenames in os.walk(os.path.join(massStorage, path)): or for dirname, dirnames, filenames in os.walk(unicode(path, 'utf-8') And my error is: 'ascii' codec can't encode character u'\xe9' in position 58: ordinal not in range(128) The wrong character is an accent (french one). It doesn't work when i start it but after a reboot i need to restart the code to make it work properly without any error. Any idea about the first fact related to the usf-8 support with external files that can be encoded with any charset (latin-1, utf-8, ... ) Secondly, why it can works after a restart of the python script ? BR Thierry GAYET -- https://mail.python.org/mailman/listinfo/python-list
Re: Creating .exe file in Python
Hi, The question is why to you want to create an exe from your python project? Setuptools is capable to create small .exe launchers in the Scripts dir of your python install. These launchers start a python script and use the python interpreter registered on your platform. That's pretty light and that's my prefered solution. If installing the Python interpreter is an issue for the end user, we can make the installer do it for him. Installer programs like Inno Setup let you do it quite easily. Kind regards Thierry On lun., juin 15, 2015 at 4:10 PM, < subhabrata.bane...@gmail.com [subhabrata.bane...@gmail.com] > wrote: On Monday, June 15, 2015 at 5:12:24 PM UTC+5:30, subhabrat...@gmail.com wrote: > Dear Group, > > I am trying to learn how to create .exe file for Python. I tried to work around > http://www.py2exe.org/index.cgi/Tutorial of Py2exe. The sample program went nice. > But if I try to make exe for larger programs with methods and classes I am getting error. > > If any one of the esteemed members may kindly suggest how to work out. > I am using Python2.7+ on Windows 7 Professional. > > Regards, > Subhabrata Banerjee. I am also experimenting around Pyinstaller, cx_Freeze and Inno Studio. But not finding ample examples and manuals. Regards, Subhabrata. -- https://mail.python.org/mailman/listinfo/python-list-- https://mail.python.org/mailman/listinfo/python-list
Re: Evaluation order
Hi, No, the value of t is a reference to tje list. So first, (1) the value of the reference t is recovered, (2) the parenthesis is evaluated, (...) the whole expression is evaluated. To evaluate (2), the .sort() call is executed in place with the side effect of sorting the content of t. t.sort() returns None. When the whole expression is evaluated, the sorted list will be displayed, as expected. Kind regards 2015-07-10 14:04 GMT+02:00 candide : > Le vendredi 10 juillet 2015 04:02:56 UTC+2, Chris Angelico a écrit : > > > > > I'm not sure what contradiction you're referring to, here. The > > evaluation that you're pointing out says, as Terry showed via the > > disassembly, that Python's first action is to look up the name 't' and > > grab a reference to whatever object it points to. > > > But in order to perform an operation, the interpreter has to evaluate the > operands and "evaluating" is not "grabbing a reference to". > > > The execution of > > t.sort() has to happen before the multiplication, because of the > > parentheses. > > > > > > Official docs explains what evaluation is : > > When the name is bound to an object, evaluation of the atom yields that > object. > > So, since the Python interpreter is performing evaluation from left to > right, the first operand of the expression : > > t*(1+int(bool(t.sort( > > evaluates to [2020, 42, 2015]. Next, the second operatand evaluates to the > integer 1. So I was expecting the result to be a shallow copy of the first > list [2020, 42, 2015] (the value of t before side effect produced by the > sort method). On the contrary, the final result takes into in account the > side effect and it is as if the first operand has been evaluated twice > before execution of the multiplication operation. > > > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Evaluation order
Hi, No, the value of t is a reference to tje list. So first, (1) the value of the reference t is recovered, (2) the parenthesis is evaluated, (...) the whole expression is evaluated. To evaluate (2), the .sort() call is executed in place with the side effect of sorting the content of t. t.sort() returns None. When the whole expression is evaluated, the sorted list will be displayed, as expected. Kind regards 2015-07-10 14:04 GMT+02:00 candide : > Le vendredi 10 juillet 2015 04:02:56 UTC+2, Chris Angelico a écrit : > > > > > I'm not sure what contradiction you're referring to, here. The > > evaluation that you're pointing out says, as Terry showed via the > > disassembly, that Python's first action is to look up the name 't' and > > grab a reference to whatever object it points to. > > > But in order to perform an operation, the interpreter has to evaluate the > operands and "evaluating" is not "grabbing a reference to". > > > The execution of > > t.sort() has to happen before the multiplication, because of the > > parentheses. > > > > > > Official docs explains what evaluation is : > > When the name is bound to an object, evaluation of the atom yields that > object. > > So, since the Python interpreter is performing evaluation from left to > right, the first operand of the expression : > > t*(1+int(bool(t.sort( > > evaluates to [2020, 42, 2015]. Next, the second operatand evaluates to the > integer 1. So I was expecting the result to be a shallow copy of the first > list [2020, 42, 2015] (the value of t before side effect produced by the > sort method). On the contrary, the final result takes into in account the > side effect and it is as if the first operand has been evaluated twice > before execution of the multiplication operation. > > > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Evaluation order
Anyway, if we enter this kind of discussion, it is a reliable indication that the code smells. There is a pythonic way to express the same task: >>> t.sort() >>> t kind regards Thierry On ven., juil. 10, 2015 at 2:28 PM, Terry Reedy < tjre...@udel.edu [tjre...@udel.edu] > wrote: On 7/10/2015 8:04 AM, candide wrote: > Le vendredi 10 juillet 2015 04:02:56 UTC+2, Chris Angelico a écrit : >> I'm not sure what contradiction you're referring to, here. The >> evaluation that you're pointing out says, as Terry showed via the >> disassembly, that Python's first action is to look up the name 't' and >> grab a reference to whatever object it points to. > > But in order to perform an operation, the interpreter has to evaluate > the operands and "evaluating" is not "grabbing a reference to". In the CPython, evaluating a name is implemented as getting the reference corresponding to the name. >> The execution of >> t.sort() has to happen before the multiplication, because of the >> parentheses. > Official docs explains what evaluation is : > > When the name is bound to an object, evaluation of the atom yields that object. Conceptually, that is exactly right. How that is implemented on a computer in CPython is to load the address on the top of the virtual machine stack. > So, since the Python interpreter is performing evaluation from left to right, > the first operand of the expression : > > t*(1+int(bool(t.sort( > > evaluates to [2020, 42, 2015]. 't' evaluates to the ***mutable*** list that 't' is bound to. > Next, the second operatand evaluates to the integer 1. And in the process of that evaluation, the list is sorted. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list-- https://mail.python.org/mailman/listinfo/python-list
Renaming files in ftplib
Let's say I have a file called 'test.c' on my local machine and I'm ftping a file with similar name from a remote computer. I want to prefix the file ftped over with a T_, how do I do that through ftplib in python? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Copying files between different linux machines
Let's say I have two linux machines with the following names: -linone -lintwo If I'm currently on linone and if I want to copy a bunch of files from lintwo into linone, how can that be done in a python script without using ftp? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Timeout at command prompt
I can use the python function raw_input() to read any input from the user but how can I add a timeout so that my program exits after x period of time when no input has been entered. Thierry -- http://mail.python.org/mailman/listinfo/python-list
Re: Timeout at command prompt
Which Python version are you using? I'm getting the following error with Python 2.3.4: Traceback (most recent call last): File "C:\home\pciroot\vcur\sdk\tools\inter.py", line 32, in ? signal.signal(signal.SIGALRM, input) AttributeError: 'module' object has no attribute 'SIGALRM' Thierry -- http://mail.python.org/mailman/listinfo/python-list
Re: Timeout at command prompt
Is there a windows equivalent for that solution? -- http://mail.python.org/mailman/listinfo/python-list
Re: Timeout at command prompt
I got the signal to work on linux with sys.stdin.readline() but the process timeout after x seconds even when I input something. Is there a way to close the signal after getting a correct input from the console window? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Re: HTTPS Login
Instead of using the following: > req = urllib2.Request("https://web.site.com/default.aspx";, params) > data = urllib2.urlopen(req) Try: data = urllib.urlopen("https://web.site.com/default.aspx";, param) Thierry Tom Grove wrote: > I am trying to login to a secure website and I am having some difficulty > understanding the process. Here is what I have with my limited > knowledge of the subject: > > ##Start Code## > #!/usr/bin/env > python > > > > import > urllib > > import > urllib2 > > > > # > Main > > params = > urllib.urlencode({ > > "user" : > "username", > > "pass" : > "password" > > }) > > > > req = urllib2.Request("https://web.site.com/default.aspx";, > params) > data = > urllib2.urlopen(req) > > > > for line in > data.readlines(): > > print line > ##End Code## > > This just doesn't seem to work. It just brings me back to a login screen. > > If you can lend a hand it would be much appreciated. > > -Tom -- http://mail.python.org/mailman/listinfo/python-list
Reading output from standard out
Let’s say I have a python program which prints output to standard out, let’s call it HelloApp. How do I capture these outputs from the python GUI tkinter? For example, I want to call HelloApp from my GUI program and I want to send the output live to a tkinter text area. Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Reading output from standard out
Let’s say I have a python program which prints output to standard out, let’s call it HelloApp. How do I capture these outputs from the python GUI tkinter? For example, I want to call HelloApp from my GUI program and I want to send the output live to a tkinter text area. Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Extending Python with C API
Hello, I'm trying to use the C API to extend Python. I've looked at various books and web sites (including the docs at python.org) and I can't get any of the samples to work. Below is a very minimalist example that I'm trying to get working and I was wondering if someone could tell me where I'm going wrong. It's probably something very obvious to those familiar with this technique. gtestmodule.c: #include "Python.h" int GetInt(int iVal) { return iVal; } char* GetString(void) { return "This is the message"; } PyObject* gtestmodule_GetInt(PyObject* pSelf, PyObject* pArgs) { int x = 0; if (!PyArg_ParseTuple(pArgs, "i", &x)) return NULL; GetInt(x); return PyBuildValue("i", x); } PyObject* gtestmodule_GetString(PyObject* pSelf, PyObject* pArgs) { char* szMsg = GetString(); return PyBuildValue("s", szMsg); } static PyMethodDef gtestmoduleMethods[] = { {"GetInt", gtestmodule_GetInt, METH_VARARGS, "Description goes here"}, {"GetString", gtestmodule_GetString, METH_VARARGS, "Description goes here"}, {NULL, NULL} }; setup.py: ---- from distutils.core import setup, Extension setup(name = "gtestmodule", version = "1.0", maintainer = "thierry masson", maintainer_email = "[EMAIL PROTECTED]", description = "test module", ext_modules = [Extension("gtestmodule",["gtestmodule.c"])], ) test.py: -- import gtestmodule print gtestmodule.GetInt(36); print gtestmodule.GetString(); The setup script builds the library OK, and I've verified that gtestmodule.so is getting created. But when I try to run a test script ( test.py, above) that uses the library, I get this error: Traceback (most recent call last): File "test.py", line 2, in ? import gtestmodule ImportError: ./gtestmodule.so: undefined symbol: PyBuildValue The OS is Red Hat Enterprise Linux ES release 3 (Taroon Update 8), and the Python version is 2.2.3 (rather old, I know, but I don't control the server environment and my sysadmin tells me this is the most recent version of Python officially supported by Red Hat. Can anyone see where I'm going wrong? Thanks! Thierry -- http://mail.python.org/mailman/listinfo/python-list
Re: Extending Python with C API
On 9/13/07, Carsten Haese wrote: >Your module C code uses an unknown function by the name of PyBuildValue. >The actual name of the function you mean is Py_BuildValue. Thank you so much, Carsten. I can't believe I missed that underscore! I'm posting the working code below in case it ever turns out to be useful for someone else who is new to the Python C API. (I also added the init function, which I forgot to include in my original post). I've verified that this code works on Red Hat Enterprise Linux ES release 3 (Taroon Update 8). Code for writing a C API Python Extension (cobbled together from so many online and printed sources that it's hard to know where to start with the acknowledgements): File gtestmodule.c: == #include "Python.h" /* First, a couple of C functions that do the real work */ /* In this test example, "real work" is perhaps exaggerating a little */ int GetInt(int iVal) { // Double the supplied value and pass it back return iVal * 2; } char* GetString(void) { return "This is the message"; } /* Now a couple of corresponding wrapper functions so that Python can get access to them */ /* Python requires this return data type and these arguments */ PyObject* gtestmodule_GetInt(PyObject* pSelf, PyObject* pArgs) { int x = 0, iRet = 0; /* Validate and assign the single integer argument (that's what GetInt() expects) */ /* The "i" means just one integer */ /* Two integers would be "ii"; an integer, a string, a float and a long would be "isdl" */ if (!PyArg_ParseTuple(pArgs, "i", &x)) return NULL; iRet = GetInt(x); return Py_BuildValue("i", iRet); } PyObject* gtestmodule_GetString(PyObject* pSelf, PyObject* pArgs) { char* szMsg = GetString(); return Py_BuildValue("s", szMsg); } /* Method table for mapping function names to wrapper functions */ static PyMethodDef gtestmoduleMethods[] = { {"GetInt", gtestmodule_GetInt, METH_VARARGS, "Description goes here"}, {"GetString", gtestmodule_GetString, METH_VARARGS, "Description goes here"}, {NULL, NULL} }; /* Module initialization function */ initgtestmodule(void) { Py_InitModule("gtestmodule", gtestmoduleMethods); } File setup.py: = # To build and install the module, use: python setup.py install # Running this will generate a library file (gtestmodule.so on Linux) # from distutils.core import setup, Extension setup(name = "gtestmodule", version = "1.0", maintainer = "your name here", maintainer_email = "[EMAIL PROTECTED]", description = "test module", ext_modules = [Extension("gtestmodule",[" gtestmodule.c"])], ) File test.py: # Simple test routine to run after building the library file import gtestmodule print gtestmodule.GetInt(36); print gtestmodule.GetString(); -- http://mail.python.org/mailman/listinfo/python-list
WMI Python, writing remotely and retrieving env variables values
I'm using the WMI library for python and I was able to connect to another computer on the network with the following line: c = wmi.WMI(computer="the-network-computer", user="hello", password="hello") Is there a way to write information to a file on that computer? How do I read environment variables, for example SystemDrive? Thierry -- http://mail.python.org/mailman/listinfo/python-list
Finding yesterday's date with datetime
Is there an easy way to determine the yesterday's date(year-month-day) from the python datetime library if I know today's date? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list
Re: writing Python in Emacs
I add just a note about ipython: if you use a version > 0.6.15 may be you will have a bad output on error like: == " ": instead of: if __name__ == "__main__": all the characters are missing. To avoid that, run in ipython: %upgrade -nolegacy uncomment in ~/.ipython/ipy_user_config.py: import ipy_defaults restart emacs and try a .py with some syntax errors. It should be ok now. Terry Jones <[EMAIL PROTECTED]> writes: >>>>>> "Richard" == Richard Szopa <[EMAIL PROTECTED]> writes: > > Richard> I am a devoted Emacs user and I write a lot in Python. > > Me too. > > Richard> I need the following features: > > Richard> 1) Tab completion, ideally Slime like. That is, when there's not > Richard> enough letters to unambiguously complete a symbol, I want it to > Richard> show a buffer (w/o taking the focus) w/ the possible > Richard> completions. In an ideal world, it would be able to complete > Richard> fo.ba to foo.bar. I imagine this would require quite tight > Richard> Emacs-Python integration. > > I know this is not what you want, but I use hippie expand (M-/) to cycle > through possible completions. It's not Python aware, but it is of some use. > > Richard> 2) Sending the toplevel definition (class or function) to the Python > Richard> buffer. > > I switched to IPython to have better interaction with a spawned Python. > > To use IPython you need to use the Python mode that is NOT the one from > (endorsed?) by the FSF. It gives you some completion (at least in the > *Python* buffer) and you can send pieces of the buffer to the python > process, via py-send-region (C-c |), py-execute-def-or-class (M-C-x), etc. > > Richard> 3) Hints on function/method arguments. IDLE has this done nearly > Richard> right, but the hints are a bit too intrusive for me. I would like to > Richard> see them in the minibuffer. > > I don't have this. > > Richard> 4) (optional) I would like to see the definition of a function > Richard> function or class by hitting M-. on its name. (I understand that > Richard> this may be impossible for methods, as Emacs would have to > Richard> automagically infer the type of the object). > > This is just an emacs tag file need. Have you googled for something like > emacs tags python? The issue of methods might be overcome by just moving > through tags with the same name. Yes, that requires _you_ to know when > you've hit the right thing. That's not optimal, but it's better than > nothing. Ideally you could send the definition to IPython, ask it for the > class info, and use that to jump to the right tag. > > Richard> I have tried a couple of times both python-modes (the one shipped w/ > Richard> Python and the one shipped w/ Emacs), pymacs and stuff like that... > Richard> And, as I said, never got it right. But, maybe I just cannot find the > Richard> way to configure it, and some configuration hints will be enough... > > If you have the time, please summarize your findings. The emacs/python > world has always seemed quite amorphous to me too. > > Terry > ___ > help-gnu-emacs mailing list > [EMAIL PROTECTED] > http://lists.gnu.org/mailman/listinfo/help-gnu-emacs > -- A + Thierry Pub key: http://pgp.mit.edu -- http://mail.python.org/mailman/listinfo/python-list
Converting a time string to a more readable date, time
I have a python time string which has the following value: 1225137896 The above corresponds to 2008/10/27 16:04:56 What can I use to convert 1225137896 to a more readable date, time format? -- http://mail.python.org/mailman/listinfo/python-list
Python poly obsolete?
I have a piece of python code which goes like the following: import poly list = [1, 2, 3] result = poly.scan(list) I'm using Python 2.3.4 and I don't think that poly library is working properly, I read somewhere that's it's obsolete now. What's the alternative? Thanks Thierry -- http://mail.python.org/mailman/listinfo/python-list