Re: [Tutor] windows and python and shebangs, oh my!
"Kirk Bailey" <[EMAIL PROTECTED]> wrote > OK, in a script, we include a special statement telling the shell > where > to go find the interpeter. This is the first line of the script, and > is > a dpecial sort of comment, called informally the shebang. Yes, but it is not a Python feature it is a Unix thing. When you execute a script (of any kind) in Unix the Unix shell(*) reads the first line and if its a shebang transfers control to the appropriate interpreter. (*) And not all Unix shells adhere to the convention, but thankfully the vast majority do. The SVR4 Bourne shell didn't as I recall. > In windows, this is for the current edition C:\python25\pythonw.exe > so the shebang is > #!C:\python\pythonw.exe This is often done purely as a convention that shows what version of Python the script was created for. Python does nothing with it, it is only a comment. > At a loss, it then occurred to me that the program is a .py name > extension. When the auto installer installed python it may have > created > an association between that name extension and the correct > interpreter > automatically, Correct, or you can do it manually. tHat is the only way that Windows associates files with commands. > So work with me, windows Pythonistas. CAN I rely on windows > definitely > and reliably having .py files associated with the windows python > interpreter, No, the association can be changed by any user or install script. But in practice it rarely is changed so you can habe a good chance of success. If you really want to be sure the associations are stored in the registry. You can look them up and change them (or add a missing one) as you need. > If so, my task of designing the autoinstaller script just got a LOT > simpler. On Windows the answer is usually in the registry somewhere, you just need to figure out where to look! Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie question concerning text covering multiple lines
"Kent Johnson" <[EMAIL PROTECTED]> wrote > Several good beginners tutorials are listed here: > http://wiki.python.org/moin/BeginnersGuide/NonProgrammers But very few of them will have been updated to reflect changes in 2.5. My own tutor is accurate for 2.3. This is precisely because, as Kent said, the changes in Python are nearly always backwards compatible, so you can use old code in a new version. If you came across something broken between 2.2 and 2.5 you are very unlucky. What was it? -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 38, Issue 2
> > > Message: 3 > Date: Sun, 1 Apr 2007 16:42:56 +0100 > From: "Alan Gauld" <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Tutor Digest, Vol 38, Issue 1 > To: tutor@python.org > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; format=flowed; charset="iso-8859-1"; > reply-type=original > > > "Rikard Bosnjakovic" <[EMAIL PROTECTED]> wrote > > s1 = "some line\n" > s2 = "some line" > s1.endswith("line"), s2.endswith("line") >> (False, True) >> >> Just skip the if and simply rstrip the string. > see below > Or add \n to the endswith() test string if you really only > want to strip the newline in those cases > > Alan G. > > > > -- > > Message: 4 > Date: Sun, 1 Apr 2007 16:46:05 +0100 > From: "Alan Gauld" <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Tutor Digest, Vol 38, Issue 1 > To: tutor@python.org > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; format=flowed; charset="iso-8859-1"; > reply-type=original > > "Jay Mutter III" <[EMAIL PROTECTED]> wrote > >> inp = open('test.txt','r') >> s = inp.readlines() >> for line in s: >> if line.endswith('No.'): >> line = line.rstrip() >> print line > > BTW, > You do know that you can shorten that considerably? > With: > > for line in open('test.txt'): >if line.endswith('No.\n'): > line = line.rstrip() >print line > Whether I attempt to just strip the string or attempt to if line.endswith('No.\r'): line = line.rstrip() It doesn't work. Note - I tried \n, \r and \n\r although text wrangler claims that it does have unix line endings When I used tr to do a few things \n or \r worked fine I tried sed and it didn't work but from the command line in sed using ctrl-v and ctrl-j to insert the line feed it worked although i then could not figure out how to do the same in a script. It is as if the python interpreter doesn't recognize the escaped n (or r) as a line feed. This is an imac running python 2.3.5 under OS-X 10.4.9 Thanks again > -- > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > > > > -- > > Message: 5 > Date: 01 Apr 2007 12:17:00 -0400 > From: "Greg Perry" <[EMAIL PROTECTED]> > Subject: [Tutor] Communication between classes > To: > Message-ID: <[EMAIL PROTECTED]> > > Hi again, > > I am still in the process of learning OOP concepts and reasons why > classes should be used instead of functions etc. > > One thing that is not apparent to me is the best way for classes to > communicate with each other. For example, I have created an Args > class that sets a variety of internal variables (__filename, > __outputdir etc) by parsing the argv array from th command line. > What would be the preferred mechanism for returning or passing > along those variables to another class? Maybe by a function method > that returns all of those variables? > > > > > > -- > > Message: 6 > Date: Sun, 01 Apr 2007 20:46:21 +0200 > From: Andrei <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Communication between classes > To: tutor@python.org > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > Hi Greg, > > Greg Perry wrote: >> I am still in the process of learning OOP concepts and >> reasons why classes should be used instead of functions etc. >> >> One thing that is not apparent to me is the best way for >> classes to communicate with each other. For example, > > Good question. Unfortunately there's no general rule that you can > apply > and end up with an undisputably perfect solution. > > Classes should communicate on a need-to-know basis. Take for example a > RSS feed reader application. You may have a class representing a feed > and a class representing a post. The feed will know what posts it > contains, but the post probably won't know what feed it comes from. > The > interface would display a list of feeds (without knowing their > contents), a list of posts within a feed (this needs to know both feed > and feed contents) and the contents of a single post (knows only about > an individual post). > >> I have created an Args class that sets a variety of internal >> variables (__filename, __outputdir etc) by parsing the argv > > Be careful with classes that simply act as a container for what are in > fact global variables. A class should do one thing only (of course > what > you accept as 'one thing' is open for debate) and encapsulate all > that's > necessary for that particular thing. Make sure you're not > overcomplicating your solution by making classes where they're not > really necessary. > >> array from th command line. What would be the preferred >> mechanism for returning or passing along those variables > > In some cases only some parts of the information contained in class A > are relevant to class B - you should pass only that particular > information, e.g. in the constructor or by set
Re: [Tutor] windows and python and shebangs, oh my!
Be aware that by default the Apache web server _WILL_ use the shebang line even when running on Windows to try to find the Python interpreter when python is run as a CGI script. There is a setting in the configuration file that controls whether to use the shebang line or to reference the windows registry. The setting is ScriptInterpreterSource registry -jdc -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Alan Gauld Sent: Thursday, April 05, 2007 4:28 AM To: tutor@python.org Subject: Re: [Tutor] windows and python and shebangs, oh my! "Kirk Bailey" <[EMAIL PROTECTED]> wrote > OK, in a script, we include a special statement telling the shell > where to go find the interpeter. This is the first line of the script, > and is a dpecial sort of comment, called informally the shebang. Yes, but it is not a Python feature it is a Unix thing. When you execute a script (of any kind) in Unix the Unix shell(*) reads the first line and if its a shebang transfers control to the appropriate interpreter. (*) And not all Unix shells adhere to the convention, but thankfully the vast majority do. The SVR4 Bourne shell didn't as I recall. > In windows, this is for the current edition C:\python25\pythonw.exe so > the shebang is #!C:\python\pythonw.exe This is often done purely as a convention that shows what version of Python the script was created for. Python does nothing with it, it is only a comment. > At a loss, it then occurred to me that the program is a .py name > extension. When the auto installer installed python it may have > created an association between that name extension and the correct > interpreter automatically, Correct, or you can do it manually. tHat is the only way that Windows associates files with commands. > So work with me, windows Pythonistas. CAN I rely on windows definitely > and reliably having .py files associated with the windows python > interpreter, No, the association can be changed by any user or install script. But in practice it rarely is changed so you can habe a good chance of success. If you really want to be sure the associations are stored in the registry. You can look them up and change them (or add a missing one) as you need. > If so, my task of designing the autoinstaller script just got a LOT > simpler. On Windows the answer is usually in the registry somewhere, you just need to figure out where to look! Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] where to look for help with modbus TCP/IP
hello, I want to use python to communicate (pluck data out of registers) with a controller (walchem, http://www.walchem.com/nav/CMImage.aspx?CMID=0&Name=180277_Modbus_C.pdf). I found great documentation using python sockets and TCP/IP; however I am very unfamiliar with modbus and how modbus TCP/IP is related to TCP/IP. Does anyone know where I might find sample code or a good book describing how to do this? thanks. Jeff - Sucker-punch spam with award-winning protection. Try the free Yahoo! Mail Beta.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie question concerning text covering multiple lines
Guba Castro wrote: > The links you sent me are helpful, many thanks for that. Maybe there is > another site you might be able to recommend to me: what I want to do > with Python in primarily (Linux) scripting. Any ideas? What do you mean by "Linux scripting"? If you want to work with files and directories, look at the os and shutil modules. If you want to run other programs, look at the subprocess module (though you may find that Python has built-in capabilities that you can use instead of calling external programs). If you want better recommendations you will have to be more specific about your needs. Kent PS Please use Reply All to reply to the list. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] where to look for help with modbus TCP/IP
"Jeff Peery" <[EMAIL PROTECTED]> wrote > sockets and TCP/IP; however I am very unfamiliar with > modbus and how modbus TCP/IP is related to TCP/IP. I've learned never to say never on this list but this isn't really a novice type thing so you might get better results asking on the main comp.lang.python newsgroup. Which means that even as I type somebody is probably posting a reply all about modbus! :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 38, Issue 2
"Jay Mutter III" <[EMAIL PROTECTED]> wrote > Whether I attempt to just strip the string or attempt to > > if line.endswith('No.\r'): > line = line.rstrip() > > It doesn't work. Can you try printing the string repr just before the test. Or even the last 6 characters: print repr(line[-6:]) if line.endswith('No: \n') line = line.strip() See if that helps narrow down the cause... > This is an imac running python 2.3.5 under OS-X 10.4.9 Shouldn't make any odds. Weird, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] where to look for help with modbus TCP/IP
Jeff Peery wrote: > hello, I want to use python to communicate (pluck data out of registers) > with a controller (walchem, > http://www.walchem.com/nav/CMImage.aspx?CMID=0&Name=180277_Modbus_C.pdf). > I found great documentation using python sockets and TCP/IP; however I > am very unfamiliar with modbus and how modbus TCP/IP is related to > TCP/IP. Does anyone know where I might find sample code or a good book > describing how to do this? thanks. From a quick look at the docs you reference, my guess is you just open a socket to the device and start exchanging data. Figuring out the command formats might take some time to get right. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 38, Issue 2
Jay, PLEASE shorten your posts by removing all unnecessary quoting. On 4/5/07, Jay Mutter III <[EMAIL PROTECTED]> wrote: > Whether I attempt to just strip the string or attempt to > > if line.endswith('No.\r'): > line = line.rstrip() > > It doesn't work. That's because you assume the linefeeds to be \r only. If you really want to strip endings on strings ending with "No.", then do this workaround: foo = line.rstrip() if foo.endswith("No."): line = foo Never assume line breaks to be of any type, because there are four of them: \n, \r, \n\r, \r\n. It would be a waste of code to check for all four, kind of reimplementing the wheel again. -- - Rikard - http://bos.hack.org/cv/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Talking between C++ & Python ?
Hi all, I have written a Python app, a company who don't use Python want to integrate its back end with their C++ coded GUI. At the moment they are proposing using CSV files to communicate between the Python & C++, ie C++ GUI generates a CSV, calls Python back end, back end does the work and generates return CSV, Python exits back to C++. This would work but seems a bit messy. Any comments of suggestions on a better solution ? The data to & from the C++, Python code consists of fairly large tables of up to 80,000 items. Many thanks for your help in advance Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Talking between C++ & Python ?
"Dave S" <[EMAIL PROTECTED]> wrote > At the moment they are proposing using CSV files to communicate > between the > Python & C++, ie C++ GUI generates a CSV, calls Python back end, > back end > does the work and generates return CSV, Python exits back to C++. > > This would work but seems a bit messy. Any comments of suggestions > on a better > solution ? Sounds like a job for SOAP or XML/RPC. I'd probably opt for SOAP in this case. Have a look at the various python SOAP modules. pySOAP is one example. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Talking between C++ & Python ?
On Thursday 05 April 2007 18:54, Alan Gauld wrote: > "Dave S" <[EMAIL PROTECTED]> wrote > > > At the moment they are proposing using CSV files to communicate > > between the > > Python & C++, ie C++ GUI generates a CSV, calls Python back end, > > back end > > does the work and generates return CSV, Python exits back to C++. > > > > This would work but seems a bit messy. Any comments of suggestions > > on a better > > solution ? > > Sounds like a job for SOAP or XML/RPC. > I'd probably opt for SOAP in this case. > Have a look at the various python SOAP modules. > pySOAP is one example. > > > Alan G. > Thanks for that - I will go investigate - Once the back end is integrated - its pay day £ :) Cheers Dave > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] question about gui tool-kits
lo there all, i have been working with pygtk2 for a while now, and, though i do like the look and feel of a GTK2 app, i would like to do some stuff with wx. I know, it doesn't look as cool, but , i may have need to work on something that i can port to a windows box, and i think that wx would be a lot easier to do. Also, i think it might prove a tad easier. So my question is to any out there who have developed on both. What is the comparison of how easy one is compared to another ? thanks, sk ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Question about exception
When doing a try/except block, is it possible to return a list as part of the exception? try: check_someting() except CheckSomethingError, e: for each_error in e: # do something Can 'e' be a list of errors? If so, how do you construct your exception class? Is it better to do it like this? (errors) = check_something() if errors: # do something Mike "head zoned out due to allergies" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about exception
On Thu, Apr 05, 2007, Mike Hansen wrote: >When doing a try/except block, is it possible to return a list as part >of the exception? > >try: >check_someting() >except CheckSomethingError, e: >for each_error in e: > # do something > >Can 'e' be a list of errors? If so, how do you construct your exception >class? If the Exception is defined as a class, e will be an instance of that class so you can have pretty much anything available: class MyException(Exception): def __init__(self, msg, mylist) self.msg = msg self.mylist = mylist Exception.__init__(self, msg) try: check_something() except MyException, e: for entry in e.mylist: ... Bill -- INTERNET: [EMAIL PROTECTED] Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way FAX:(206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676 ``I don't make jokes, I just watch the Government and report the facts...'' Will Rogers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about exception
> If the Exception is defined as a class, e will be an instance of > that class so you can have pretty much anything available: > > class MyException(Exception): > def __init__(self, msg, mylist) > self.msg = msg > self.mylist = mylist > Exception.__init__(self, msg) > > try: > check_something() > except MyException, e: > for entry in e.mylist: ... > > Bill > -- > INTERNET: [EMAIL PROTECTED] Bill Campbell; Celestial Software LLC > URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way > FAX:(206) 232-9186 Mercer Island, WA 98040-0820; > (206) 236-1676 > > ``I don't make jokes, I just watch the Government and report > the facts...'' > Will Rogers Doh! It was right in front of me, but I wasn't connecting the dots. Thanks, Mike ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] beautifulSoup and .next iteration
As a complete tyro, I've broken my teeth on this web-page scraping problem. I've several times wanted to scrape pages in which the only identifying elements are positional rather than syntactical, that is, pages in which everything's a sibling and there's no way to predict how many sibs there are in each section headed by an empty named anchor. I've been trying to use beautifulSoup to scrape these. It's not clear to me which is worse: my grasp of python in general or beautifulSoup in particular. Here's a stripped down example of the sort of thing I mean: paragraph 1 paragraph 1.A some line another line paragraph 1.B paragraph 2 paragraph 2.B paragraph 3 somedata I want to end up with some container, say a list, containing something like this: [ [A1, paragraph 1, paragraph 1.A, some line, another line, paragraph 1.B] [A2, paragraph 2, paragraph 2.B] [A3, paragraph 3, some, data] ] I've tried things like this: (just using print for now, I think I'll be able to build the lists or whatever once I get the basic idea.) anchors = soup.findAll('a', { 'name' : re.compile('^A.*$')}) for x in anchors: print x x = x.next while getattr(x, 'name') != 'a': print x And get into endless loops. I can't help thinking there are simple and obvious ways to do this, probably many, but as a rank beginner, they are escaping me. Can someone wise in the ways of screen scraping give me a clue? thanks, Jon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] UnicodeDecodeError in kinterbasdb
Hello, I've installed Python 25 on an XP machine, installed kinterbasdb (and eginix-mx-base). Python works fine. However, when I try to load the firebird module in IDLE I get ... Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import kinterbasdb Traceback (most recent call last): File "", line 1, in File "C:\Python25\Lib\site-packages\kinterbasdb\__init__.py", line 98, in _instPath, 'bin' UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 237: ordinal not in range(128) .. From searching the web I'm led to think this is related to the default encodings on the machine. But I'm not sure what to do about this. For what it's worth, I have another machine and this same library works just fine. Thank You, Andy Koch ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Talking between C++ & Python ?
> I have written a Python app, a company who don't use Python want to integrate > its back end with their C++ coded GUI. > At the moment they are proposing using CSV files to communicate between the > Python & C++, ie C++ GUI generates a CSV, calls Python back end, back end > does the work and generates return CSV, Python exits back to C++. > This would work but seems a bit messy. Any comments of suggestions on a > better > solution ? > The data to & from the C++, Python code consists of fairly large tables of up > to 80,000 items. SQLite? Alan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor