Re: [Tutor] Importing serial module
>ImportError: No module named win32file >>> > Is there something else that I need to specify? Do you have the winall package installed? Its a separate download to the official Python package or comes prebundled in the ActiveState version of Python. > 2) I want to send data (ascii text) to a serial port, but at the > same > time want to keep monitoring incoming data in that same serial port. > Whats the best approach to do this? Multithreading? Multithreading will work but for a serial port you might be easier just to create a loop that reads/writes alternately. Just read and write faitrly small blocks of data and it should seem to function concurrently. If you can't do that then threads are your next best option. Alternatively write two programs and join them via a common UI program using popen or subprocess... Alan g ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Writing to text files
Hi all, I want to write to a text file with a timestamp, but I want to newest entry at the top. So I want to insert the next entry to the file at the beginning. I can create and append to a file and then the latest entry is at the bottom. Any ideas how this is done please? Thanks, Johan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to text files
Am Montag, den 22.08.2005, 09:30 +0200 schrieb Johan Geldenhuys: > Hi all, > I want to write to a text file with a timestamp, but I want to newest > entry at the top. So I want to insert the next entry to the file at > the beginning. > I can create and append to a file and then the latest entry is at the > bottom. > Any ideas how this is done please? Well, difficult. Depending upon your needs, you can either copy the whole file, adding the timestamp at the the top, and rename it afterwards. Expensive to do. Or if you have to add more timestamps, you need to store it in some other "data structure", and export a log file on demand. Other "data structure" might be a SQL database, a directory full of small entry files, or one file with a reversed flow. Basically files (in most OS environments) can only be appended to. Andreas signature.asc Description: Dies ist ein digital signierter Nachrichtenteil ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] no rsplit
>v = "64x43x12" -> '64x43', '12' > > How split it by the las 'x'? [snip] >>>v = "64x43x12" >>>temp = v.split("x")[-1:] >>>print temp ['12'] that's the best I can do for now. HTH, -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Writing to XML file with minidom
Hi all, I use minidom to parse xml data from a file. I know how to get the data: """ """ Parse the xml file """ xmlDocument = minidom.parse(self.configFile) """ Parse xml main section """ mainSection = xmlDocument.getElementsByTagName('Config') """ Parse xml Global section """ configSection = mainSection[0] """ Parse Ports section """ socketList = configSection.getElementsByTagName('Sockets') """ Now I want to change a string that a retrieved from the file and write it back to where it was. So, I get something, change it and write it back. How do I put the new string in the place of the old? How do I overwrite the first value with the new value? Thanks, Johan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] nested loops
Is there any way more efficient for run a nested loop? -- for a in list_a: for b in list_b: if a == b: break ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Network Programming Information and terminology
> Hello. It's me again. Thanks for all the help with > the Python Networking Resources, but does anyone know > what I'll need to know to write a paper on Network > Programming and Python. Like terminology and all > that. Maybe I'll have a section on socketets, TCP, > Clients (half of the stuff I don't even know what it > means). So, does anyone know any good websites with > Network Programming information. Thanks! http://www.itprc.com/tcpipfaq/default.htm ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested loops
Jonas Melian wrote: > Is there any way more efficient for run a nested loop? > > -- > for a in list_a: > for b in list_b: > if a == b: break efficient in running time? lines of code? What you have is pretty simple, what don't you like about it? In Python 2.4 you could use a generator expression: for (a for a in list_a for b in list_b if a==b): break If you want to know which is faster you have to time them... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Thread deamon
Hi, Anyone knows how to setDaemon(True) or pass it as an argument to start_new_thread() with the code snippet below? server.listen(1) thread.start_new_thread(run_server,(server,)) Otherwise the thread stops running when I close the telnet client (even using Unix's & operator for background running) chrs j. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Thread deamon
Ok, better question. Are these two equivalent: def run_server(socket): ... 1) thread = Thread(target=run_server, args=(server,)) #thread.setDaemon(True) thread.start() 2) server.listen(1) thread.start_new_thread(run_server,(server,)) So that I can uncomment the setDaemon() method? chrs j. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to text files
> I want to write to a text file with a timestamp, but I want to > newest > entry at the top. So I want to insert the next entry to the file at > the > beginning. You will need to create a new file. Basically you need to: Read the current file into a list or string. Rename the old file (foo.bak seems a common type!) Prepend your new data Write the new string out to a new file with the old name You may prefer to read the existing file in line by line and write it back out again to save memory usage. You can see some examples of this type of file copying in my tutorial where I print out a restaurant menu with a daily updated header. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Thread deamon
Well, I don't know how it works on Windows, but on UNIX, if you want to create a deamon able to stay alive when you deconnect you "just" have to catch the HUP signal and ... do nothing of it :) By default this signal exit the application. You have two ways of doing so : 1 - use the standard signal handling to intercept the SIGHUP signal and ignore it 2 - launch your program using "nohup" : $ nohup my_prg In both cases it should stay alive after the death of the terminal. Pierre Jorge Louis de Castro a écrit : > Hi, > > > Anyone knows how to setDaemon(True) or pass it as an argument to > start_new_thread() with the code snippet below? > > server.listen(1) > thread.start_new_thread(run_server,(server,)) > > Otherwise the thread stops running when I close the telnet client (even > using Unix's & operator for background running) > > chrs > j. > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77fax : (33) 4 67 61 56 68 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Thread deamon
Jorge Louis de Castro wrote: > Are these two equivalent: > > def run_server(socket): > ... > > 1) > thread = Thread(target=run_server, args=(server,)) > #thread.setDaemon(True) > thread.start() > > 2) > server.listen(1) > thread.start_new_thread(run_server,(server,)) They both start threads but the threading module also modifies the behavior of sys.exit() so the program will not exit until all *non* daemon threads exit. So you may get the behaviour you want with 1) as written (without the setDaemon() call). Calling setDaemon(True) *allows* the program to exit before the daemon thread terminates which sounds like the opposite of what you want. Kent > > So that I can uncomment the setDaemon() method? > > chrs > j. > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Network Tutorials
John Walton wrote: > Hello, everyone. I just began school, and they > already assigned us science fair. Since I'm in 8th > grade, I get to do demonstrations for our projects. > > I'm probably going to demonstrate Python's networking> > capabilities by writing a simple instant messenger > program. I only have a few problems: > > 1. I know squat about Python network Programming > > 2. I know nothing about networks You might want to get a copy of Foundations of Python Network Programming by John Goerzen. It has a good introduction to networking concepts and a few examples of chat servers. Note to shawn - this book also has an introduction to threading. http://apress.com/book/bookDisplay.html?bID=363 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to text files
Hi Johan, It's actually fairly simply and straight forward... Here's how to do it: (I haven't officially tested this code for bugs, but I believe it is correct.) file = open("datafile.txt", "r") filedata = file.read() file.close() newLine = "Your new line of data with the time stamp goes here.\n" + filedata file = open("datafile.txt", "w") file.write(newLine) file.close() --- Hope this helps, Byron :-) --- Johan Geldenhuys wrote: > Hi all, > I want to write to a text file with a timestamp, but I want to newest > entry at the top. So I want to insert the next entry to the file at > the beginning. > I can create and append to a file and then the latest entry is at the > bottom. > Any ideas how this is done please? > > Thanks, > > Johan > > > >___ >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] reading excel and access files
hello, can python read excel and access files? If so where do I go to read about how this would work? thanks. Jeff___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Declaring encoding question
mailing list wrote: > Hi all, > > I've got some source code which will be handling non-ASCII chars like > umlauts and what not, and I've got a testing portion stored in the > code. > > I get this deprecation warning when I run my code - > __main__:1: DeprecationWarning: Non-ASCII character '\xfc' in file > C:\Python24\testit.py on line 733, but no encoding declared; see > http://www.python.org/peps/pep-0263.html for details > > I'm reading this - http://www.python.org/peps/pep-0263.html > > Now, the non-ASCII character is in the test data, so it's not actually > part of my code. > Will Python be able to handle \xfc and company in data without my > telling it to use a different form of encoding? You should tell Python what the encoding is. The non-ASCII character is part of the source file. Just include the line # -*- coding: cp1252 -*- at the start of the code. > When I run the code, and get my returned data, it looks like this in > Pythonwin - > > print j["landunits"].keys() > > ['"J\xe4ger"', '"Deutschmeister"', '"Army of Bohemia"', > '"Gardegrenadiere"', '"K.u.K Armee"', '"Erzherzog"', '"Army of > Italy"', '"Army of Silesia"', '"Army of Hungary"'] > > So J\xe4ger is actually Jäger. When I run it slightly differently - > for item in j["landunits"].keys(): > > ... print item > ... > "Jäger" > "Deutschmeister" > "Army of Bohemia" > "Gardegrenadiere" > "K.u.K Armee" > "Erzherzog" > "Army of Italy" > "Army of Silesia" > "Army of Hungary" > > It prints the umlauted 'a' fine and dandy. You are seeing the difference between printing a string and printing it's repr(). When you print a list (which is what j["landunits"].keys() is), Python prints the repr() of each element of the list. repr() of a string shows non-ascii characters as \x escapes; that's why you get J\xe4ger. When you print the string directly, the non-ascii chars are sent to the terminal directly. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to text files
Byron wrote: >Hi Johan, > >It's actually fairly simply and straight forward... Here's how to do >it: (I haven't officially tested this code for bugs, but I believe it >is correct.) > >file = open("datafile.txt", "r") >filedata = file.read() >file.close() > >newLine = "Your new line of data with the time stamp goes here.\n" + >filedata >file = open("datafile.txt", "w") >file.write(newLine) >file.close() > >--- > >Hope this helps, > >Byron :-) >--- > > > > >Johan Geldenhuys wrote: > > > >>Hi all, >>I want to write to a text file with a timestamp, but I want to newest >>entry at the top. So I want to insert the next entry to the file at >>the beginning. >>I can create and append to a file and then the latest entry is at the >>bottom. >>Any ideas how this is done please? >> >>Thanks, >> >>Johan >> >> >> >>___ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor >> >> >> >> > > >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > > No . . . when you open a file as "w" it erases whatever was there before: >>> f = open("ban.txt", "w") >>> f.write("ban") >>> f.close() >>> f = open("ban.txt", "r") >>> f.read() 'ban' >>> f.close() >>> f = open("ban.txt", "w") >>> f.close() >>> f = open("ban.txt", "r") >>> f.read() '' -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to text files
At 08:09 AM 8/22/2005, Byron wrote: >Hi Johan, > >It's actually fairly simply and straight forward... Here's how to do >it: (I haven't officially tested this code for bugs, but I believe it >is correct.) > >file = open("datafile.txt", "r") This is an example of rebinding to a name originally bound to a builtin function. In other words, file is a builtin function; assigning to file makes the builtin inaccessible. So it is a good idea to avoid such assignments. >filedata = file.read() >file.close() > >newLine = "Your new line of data with the time stamp goes here.\n" + >filedata >file = open("datafile.txt", "w") >file.write(newLine) >file.close() or if you like terseness: newText = newLine + open("datafile.txt", "r").read() open("datafile.txt", "w").write(newText ) Bob Gailer 303 442 2625 home 720 938 2625 cell ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to text files
Bob Gailer wrote: >At 08:09 AM 8/22/2005, Byron wrote: > > >>Hi Johan, >> >>It's actually fairly simply and straight forward... Here's how to do >>it: (I haven't officially tested this code for bugs, but I believe it >>is correct.) >> >>file = open("datafile.txt", "r") >> >> > >This is an example of rebinding to a name originally bound to a builtin >function. >In other words, file is a builtin function; assigning to file makes the >builtin inaccessible. >So it is a good idea to avoid such assignments. > > > >>filedata = file.read() >>file.close() >> >>newLine = "Your new line of data with the time stamp goes here.\n" + >>filedata >>file = open("datafile.txt", "w") >>file.write(newLine) >>file.close() >> >> > >or if you like terseness: >newText = newLine + open("datafile.txt", "r").read() >open("datafile.txt", "w").write(newText ) > >Bob Gailer >303 442 2625 home >720 938 2625 cell > >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > > Oops. Please ignore previous email. I didn't notice the statement where you added filedata to newLine. -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] FW: How do you turn something into a number?
Hi Gang, Thanks to Danny Yoo for a great answer. The answer given is a little advanced so I have to ask the following follow-up. What does it mean when you write the [0] after the return statement? e.g. return struct.unpack("!h", bytes)[0] I'm really green here but can tell I'm going to love python! :-) Thanks, Frank -Original Message- From: Danny Yoo [mailto:[EMAIL PROTECTED] Sent: Friday, August 19, 2005 5:54 PM To: Lane, Frank L Cc: Tutor Subject: Re: [Tutor] How do you turn something into a number? > I have what I think is a string from socket.recvfrom(...). I want to > turn it into numbers Hi Frank, If you know how those bytes should be interpreted, you may want to look at the 'struct' module to destructure them back into integers: http://www.python.org/doc/lib/module-struct.html > from socket import * > from array import * Side note: you may want to avoid doing the 'from import *' form in Python, just because there's a high chance that one module will munge the names of another. If you want to avoid typing, you can always abbreviate module names by doing something like this: ## import socket as S import array as A ## For more information on this, see: http://www.python.org/doc/tut/node8.html#SECTION00841 Ok, let's continue looking at some code: [some code cut] > number =int(s.join(data[10:13],16)) I think you meant to write: number = int(data[10:13], 16) But even with the correction, this will probably not work: int() expects to see string literals, not arbitrary byte patterns that come off the socket.recv_from. I think you want to use 'struct' instead. For example: ## >>> import struct >>> struct.calcsize("h") 2 ## On my platform, a "short" is two bytes. ## >>> def parse_short(bytes): ... """Given two bytes, interprets those bytes as a short.""" ... return struct.unpack("h", bytes)[0] ... >>> parse_short('\x01\x00') 1 >>> parse_short('\x00\x01') 256 ## And from this example, we can see that I'm on a "little-endian" system. http://catb.org/~esr/jargon/html/L/little-endian.html So we probably do need to take care to tell 'struct' to interpret the bytes in "network" order, bu using the '!' prefix during the byte unpacking: ## >>> def parse_short(bytes): ... """Given two bytes, interprets those bytes as a short.""" ... return struct.unpack("!h", bytes)[0] ... >>> parse_short('\x01\x00') 256 >>> parse_short('\x00\x01') 1 ## Please feel free to ask questions on this. Hope this helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested loops
> Is there any way more efficient for run a nested loop? > > -- > for a in list_a: if a in list_b: break Should help a bit, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading excel and access files
> hello, can python read excel and access files? Yes > If so where do I go to read about how this would work? thanks. You need to use COM to do it, Mark Hammonds book "Python Programming on Win32" gives several examples. But basically COM programming is a pain in the neck and involves lots of trial and error. OTOH If you have ever done it from VB you will know that already1 Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to XML file with minidom
> """ Parse the xml file """ > xmlDocument = minidom.parse(self.configFile) [code cut] > Now I want to change a string that a retrieved from the file and write > it back to where it was. So, I get something, change it and write it > back. > > How do I put the new string in the place of the old? How do I overwrite > the first value with the new value? Hi Johan, The documentation in: http://www.python.org/doc/lib/module-xml.dom.minidom.html has a small example where they insert text into an element: ## (From the documentation) from xml.dom.minidom import getDOMImplementation impl = getDOMImplementation() newdoc = impl.createDocument(None, "some_tag", None) top_element = newdoc.documentElement text = newdoc.createTextNode('Some textual content.') top_element.appendChild(text) ## Elements have methods like appendChild(), replaceChild() and removeChild(). So it should be fairly straightforward to replace the existing text node with a new one. That being said, the DOM model is a bit verbose and feels very low-level. Have you looked at the third-party "ElementTree" module yet? http://effbot.org/zone/element-index.htm It's a bit more convenient to work with; its model maps better to Python. Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to make a script do two things at once.
On Sun, 21 Aug 2005 16:23:20 -0500 nephish <[EMAIL PROTECTED]> wrote: > Hey there, > i have a simple question about getting a script to do > two things at once. > like this. > > > for i in range(100): > print i > time.sleep(.2) > if i == 15: > os.system('python /home/me/ipupdate.py') > > print 'done' > > when i run this, it stops at 15 and runs the script called out in the > os.system line. i know it is supposed to do that. But, how could i get a > script to do this without stopping the count (or delaying it unill the > script called exits) I don' t have to run it this way, i can import it > if necessary as a module. or whatever will work so i can execute two > things at once. > If you just need to call a unix system command you can simply add "&" to the command string to make it run in the background. Regards Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested loops
On Mon, 22 Aug 2005, Kent Johnson wrote: > > Is there any way more efficient for run a nested loop? > > > > -- > > for a in list_a: > > for b in list_b: > > if a == b: break Hi Jonas, Depends on what we're trying to do. Is it necessary to have a nested loop here? What kind of problem is this trying to solve? If the question is: "are any elements in list_a shared in list_b?", then yes, we can avoid nested loops altogether. If we're concerned about efficiency, we can take advanatage of dictionaries, or use something like the 'set' data structure. http://www.python.org/doc/lib/module-sets.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to make a script do two things at once.
Michael Lange wrote: >On Sun, 21 Aug 2005 16:23:20 -0500 >nephish <[EMAIL PROTECTED]> wrote: > > > >>Hey there, >>i have a simple question about getting a script to do >>two things at once. >>like this. >> >> >>for i in range(100): >>print i >>time.sleep(.2) >>if i == 15: >>os.system('python /home/me/ipupdate.py') >> >>print 'done' >> >>when i run this, it stops at 15 and runs the script called out in the >>os.system line. i know it is supposed to do that. But, how could i get a >>script to do this without stopping the count (or delaying it unill the >>script called exits) I don' t have to run it this way, i can import it >>if necessary as a module. or whatever will work so i can execute two >>things at once. >> >> >> > >If you just need to call a unix system command you can simply add "&" to the >command string to >make it run in the background. > >Regards > >Michael > >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > > Well Cool , yeah, i run linux. this would work out great. thanks! shawn ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Sort a list following the order of other list
best = [ [1024, 768], [800, 600], [640, 480] ] (it's larger) modes = [ (24, [1280, 1024]), (24, [1024, 768]), (24, [640, 480]), (16, [1600, 1200]), (16, [1280, 1024]), (15, [320, 200]), ] I want to create a list with ALL elements of 'modes', but following the order of list 'best' (if exist the number). At the end the rest of numbers in modes, are added too. And all this for each modes[0] (24, 16, 15, ...) For a simple list is easy: a = [123, 45, 98, 2, 12]; b=[12, 35, 45, 2] [y for y in b if y in a] + [x for x in a if x not in b] Help please, that i'm going crazy v 2.3.5 Thanks in advance ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Thread deamon
On Mon, 22 Aug 2005, Jorge Louis de Castro wrote: > Anyone knows how to setDaemon(True) or pass it as an argument to > start_new_thread() with the code snippet below? > > server.listen(1) > thread.start_new_thread(run_server,(server,)) > > Otherwise the thread stops running when I close the telnet client (even > using Unix's & operator for background running) Hi Jorge, It appears that you're using the low-level thread library: http://www.python.org/doc/lib/module-thread.html But it looks like you're looking at threading.setDaemon(), which comes as part of the high-level 'threading' library. http://www.python.org/doc/lib/thread-objects.html Those are two separate modules: 'threading' builds on top of 'thread', so I'd recommend using 'threading'. You can replace: thread.start_new_thread(run_server,(server,)) with: child = threading.Thread(target=run_server, args=(server,)) child.setDaemon(True) child.start() Hope this helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to text files
> It's actually fairly simply and straight forward... Here's how to do > it: (I haven't officially tested this code for bugs, but I believe it > is correct.) > > file = open("datafile.txt", "r") > filedata = file.read() > file.close() > > newLine = "Your new line of data with the time stamp goes here.\n" + > filedata > file = open("datafile.txt", "w") > file.write(newLine) > file.close() Hi Byron, The approach here is fine, but it's slightly fragile, because if anything exceptional happens in-between writing the lines back to the file, we can lose the data in the file. It might be safe to first backup the original file by renaming it to something else. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Will someone please tell me how to read this?
Hi List, I cut and pasted a dump from recvfrom below. I can’t read it and don’t where to look in the documentation to figure out how to read this. Is there a name for this type of number? I’m assuming the \x means it’s hex, but then you have things like \x00h, and \x007p^. Any help here is greatly appreciated. Code snippet: text = server.recvfrom(1024) print repr(text) '\x05\x01\x03\x02\x05\xaf\xce\x04\x00h\x00\x00\x00\x01\x007\x00\x01\x00\x01\x007p^\x00\x00\x00\x00\x00\x00\x00\x01\x007\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00AN\x94\n\x923\xcaXA$s\xdc(\x1e\xcf\xbaAR\xb6\xc9\x1c\x1e%#\x02\x02\x00\xe1\x00\x00\x00\x00\x00\x00\x03\xe8\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00' Thanks, Frank ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Network Tutorials
> 1. I know squat about Python network Programming I'll let others answer this bit. > 2. I know nothing about networks There's a really neat intro to networks on my company's web site albeit rather well hidden. It's not techniccal at all but aimed at the general public, however it's short and accurate and quite good fun. Of course I may be a tad biased... http://www.btplc.com/thegroup/Networkstory/index.html Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading excel and access files
On Mon, 2005-08-22 at 18:57 +0100, Alan G wrote: > > hello, can python read excel and access files? > > Yes > > > If so where do I go to read about how this would work? thanks. > > You need to use COM to do it, Mark Hammonds book "Python Programming > on Win32" gives several examples. But basically COM programming is a > pain in the neck and involves lots of trial and error. OTOH If you > have ever done it from VB you will know that already1 > > Alan G. > Someone posted this link on this list a while ago (sorry, I can't remember who). I found it very useful under Win32: http://www.microsoft.com/technet/scriptcenter/scripts/python/pyindex.mspx Ziyad. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Will someone please tell me how to read this?
On Mon, 2005-08-22 at 13:18 -0500, Lane, Frank L wrote: > Hi List, > > > > I cut and pasted a dump from recvfrom below. I can’t read it and > don’t where to look in the documentation to figure out how to read > this. > > > > Is there a name for this type of number? I’m assuming the \x means > it’s hex, Correct. > but then you have things like \x00h, and \x007p^. Coincidence! '\x00h' means: there's a byte with a value 0 followed by a byte with a value 104. Since 104 is 'h' in ASCII table and can be represented in a human form, Python displayed it as 'h'. On the other hand, 0 doesn't have a human form to represent it, and thus, Python displays it as '\x00'. Likewise with the '\x00p^'. > > > > Any help here is greatly appreciated. > > > > Code snippet: > > > > text = server.recvfrom(1024) > > print repr(text) > > > > '\x05\x01\x03\x02\x05\xaf\xce\x04\x00h\x00\x00\x00\x01\x007\x00\x01 > \x00\x01\x007p^\x00\x00\x00\x00\x00\x00\x00\x01\x007\x00\x04\x00\x00 > \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00AN\x94\n\x923\xcaXA$s\xdc(\x1e > \xcf\xbaAR\xb6\xc9\x1c\x1e%#\x02\x02\x00\xe1\x00\x00\x00\x00\x00\x00 > \x03\xe8\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 > \x00\x01\x00\x00\x00' > This is binary. In other words, it's no use for human as is. You need to know how to interpret this sequence of bytes to something useful. It might be compressed or need to be translated. Your solution is must likely in the other end of the connection. Consult the SERVER and see what it really sends to you! > > > Thanks, > > Frank > Ziyad. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] FW: How do you turn something into a number?
> What does it mean when you write the [0] after the return statement? > e.g. return struct.unpack("!h", bytes)[0] Its just indexing the list returned by struct.unpack(), specifically extracting tghe first element. For more on using struct() see the bottom section of my file handling topic... (now corrected to fix a bug reported by a recent reader...) Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to text files
Danny Yoo wrote: > > >>It's actually fairly simply and straight forward... Here's how to do >>it: (I haven't officially tested this code for bugs, but I believe it >>is correct.) >> >>file = open("datafile.txt", "r") >>filedata = file.read() >>file.close() >> >>newLine = "Your new line of data with the time stamp goes here.\n" + >>filedata >>file = open("datafile.txt", "w") >>file.write(newLine) >>file.close() >> >> > >Hi Byron, > >The approach here is fine, but it's slightly fragile, because if anything >exceptional happens in-between writing the lines back to the file, we can >lose the data in the file. It might be safe to first backup the original >file by renaming it to something else. > Hi Danny, I agree 100% with your statement. The reason why I left it in its "fragile" state was to help keep the example provided simple and straight forward. Since this is a "beginners" group, I wanted to confuse by adding extra protection to it. ;-) Byron --- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to text files
Byron wrote: > Hi Danny, > > I agree 100% with your statement. The reason why I left it in its > "fragile" state was to help keep the example provided simple and > straight forward. Since this is a "beginners" group, I wanted to > confuse by adding extra protection to it. ;-) > > Byron > --- Opps, a quick correction: "I wanted to *avoid* confusion by..." Byron --- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Will someone please tell me how to read this?
> Is there a name for this type of number? I'm assuming the \x means > it's hex, but then you have things like \x00h, and \x007p^. It's a sequence of bytes and you've asked Python to print its string representation. So where the bytes are unprintable ascii codes it shows you the hex, thus: '\x05\x01\x03\x02\x05\xaf\xce\x04\x00 Those are all single bytes in hex h that byte has the ascii value for the letter 'h' (ie. its 104 in decimal) \x00\x00\x00\x01\x007\x00\x01\x00\x01\x00 More bytes as hex 7 p ^ The three ascii characters '7','p','^' (or 55,112,94 in decimal) \x00\x00\x00\x00\x00\x00\x00\x01\x00 More hex bytes 7 '7' again \x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 more bytes as hex... you get the idea? However that doesn't mean that those are really what the data represents. The first 4 bytes could be a 32 bit integer, or a short floating point value. Similary the 7p^ sequence could be three bytes from within an integer too. You really need to know whats being thrown at you down the socket otherwise you have very little hope of accurately decoding it. And to decode it you need to use the struct module. HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading excel and access files
>> on Win32" gives several examples. But basically COM programming is >> a >> pain in the neck and involves lots of trial and error. OTOH If you >> have ever done it from VB you will know that already1 >> > Someone posted this link on this list a while ago (sorry, I can't > remember who). I found it very useful under Win32: > > http://www.microsoft.com/technet/scriptcenter/scripts/python/pyindex.mspx > Excellent! thanks for posting this, I hadn't seen it before. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading excel and access files
On Mon, 22 Aug 2005, ZIYAD A. M. AL-BATLY wrote: > Someone posted this link on this list a while ago (sorry, I can't > remember who). I found it very useful under Win32: > > http://www.microsoft.com/technet/scriptcenter/scripts/python/pyindex.mspx What a great resource. Thanks for posting the URL. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sort a list following the order of other list
Quoting Jonas Melian <[EMAIL PROTECTED]>: > best = [ [1024, 768], [800, 600], [640, 480] ] (it's larger) > > modes = [ > (24, [1280, 1024]), > (24, [1024, 768]), > (24, [640, 480]), > (16, [1600, 1200]), > (16, [1280, 1024]), > (15, [320, 200]), > ] > > I want to create a list with ALL elements of 'modes', but following the > order of list 'best' (if exist the number). At the end the rest of > numbers in modes, are added too. And all this for each modes[0] (24, 16, > 15, ...) I well understand the desire to do everything in a single beautiful list comprehension, but sometimes we have to go back to old-style programming and actually write a function :-) in 2.3, sort() takes an optional comparison function as a parameter. cmp(x,y) should return <0 if x0 if x>y. >>> best = [ [1024, 768], [800, 600], [640, 480] ] >>> modes = [ (24, [1280, 1024]), (24, [1024, 768]), (24, [640, 480]), (16, [1600, 1200]), (16, [1280, 1024]), (15, [320, 200]) ] >>> def modeCompare(m1, m2): ... r1 = m1[1]; r2 = m2[1] ... if r1 in best: ... if r2 in best: ...return cmp(best.index(r1), best.index(r2)) ... else: ...return -1 ... else: ... if r2 in best: ...return 1 ... else: ...return 0 ... >>> sorted(modes, cmp=modeCompare) [(24, [1024, 768]), (24, [640, 480]), (24, [1280, 1024]), (16, [1600, 1200]), (16, [1280, 1024]), (15, [320, 200])] ...mind you, having said that, here is a list comprehension solution anyway: >>> [(b, r) for r in best for b, r2 in modes if r == r2] + [m for m in modes if m[1] not in best] [(24, [1024, 768]), (24, [640, 480]), (24, [1280, 1024]), (16, [1600, 1200]), (16, [1280, 1024]), (15, [320, 200])] HTH! -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Split a string into characters
Hi, A quick one... How do I split a string like "Hans" into a list of characters ['H','a','n','s']? Note that there are no spaces in the original string. Str.split() without any arguments looks for whitespace as splitting character. So, this doesn't serve the purpose. And str.split("") appears to be illegal. Cheers Hans ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Split a string into characters
> How do I split a string like "Hans" into a list of characters > ['H','a','n','s']? [snip] well you could write your own function... def splititems(itemlist): templist = [] for item in itemlist: templist.append(item) return templist print splititems("Hello") or even def splititems(itemlist): return [item for item in itemlist] print splititems("Hello") well I have to go, I hope that helps you. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Split a string into characters
On Tue, Aug 23, 2005 at 01:18:43PM +1200, Hans Dushanthakumar wrote: > Hi, >A quick one... >How do I split a string like "Hans" into a list of characters > ['H','a','n','s']? > Note that there are no spaces in the original string. >Str.split() without any arguments looks for whitespace as splitting > character. So, this doesn't serve the purpose. > And str.split("") appears to be illegal. list('Hans') -- "History will be kind to me, for I intend to write it." -- Winston Churchill Rick Pasotto[EMAIL PROTECTED]http://www.niof.net ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading excel and access files
ZIYAD A. M. AL-BATLY wrote: > On Mon, 2005-08-22 at 18:57 +0100, Alan G wrote: > >>>hello, can python read excel and access files? >> You might also have a look at http://sourceforge.net/projects/pyexcelerator -- Poor Yorick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sort a list following the order of other list
Jonas Melian wrote: > best = [ [1024, 768], [800, 600], [640, 480] ] (it's larger) > > modes = [ > (24, [1280, 1024]), > (24, [1024, 768]), > (24, [640, 480]), > (16, [1600, 1200]), > (16, [1280, 1024]), > (15, [320, 200]), > ] > > I want to create a list with ALL elements of 'modes', but following the > order of list 'best' (if exist the number). At the end the rest of > numbers in modes, are added too. And all this for each modes[0] (24, 16, > 15, ...) In Python 2.4 you can get clever with the key function to sort() or sorted(): def keyFunc(mode): depth, dims = mode try: ix = best.index(dims) except ValueError: ix = len(modes) return (ix, -depth, -dims[0]) for mode in sorted(modes, key=keyFunc): print mode For Python 2.3 you can to use the decorate-sort-undecorate idiom. To do this you build an intermediate list with the that will sort the way you want, sort it, an remove the extra stuff you added at the beginning. The same keyFunc will work: deco = [(keyFunc(mode), mode) for mode in modes] deco.sort() modes = [mode for (key, mode) in deco] for mode in modes: print mode ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading excel and access files
ZIYAD A. M. AL-BATLY wrote: > On Mon, 2005-08-22 at 18:57 +0100, Alan G wrote: > >>>hello, can python read excel and access files? >> You might also have a look at http://sourceforge.net/projects/pyexcelerator -- Poor Yorick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading excel and access files
Jeff Peery wrote: > hello, can python read excel and access files? If so where do I go to > read about how this would work? thanks. There are some resources here that might be helpful: http://www.python.org/pypi?%3Aaction=search&name=&version=&summary=&description=&keywords=excel&_pypi_hidden=0 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tk canvas question
I am writing a resource manager like program ! the files of directory is display on canvas and taged. the tag and file name is keep in a dictionary! The tag is a increaseing number from 1,so I build the dictionary like (1:file1,2:file2,..). While geting into another directory,I try to remove all object and tags on the canvas so that the tags would be count form 1 again.But I found out the tag number is increasing, not recount form 1. My dictionary can be organize that simple. I want to know is there a way to get the tag of canvas recount form 1 again? or even more simple ,get the filename I had wrote in canvas directly? [EMAIL PROTECTED] 2005-08-23 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sort a list following the order of other list
Quoting Jonas Melian <[EMAIL PROTECTED]>: > Thank you very much!. > > I didn't want to make it using the old-style programming because it's > more large and I'm supposed that slower that using a list comprehension > solution. Well, we can investigate, to find out the truth :-) Hopefully you can read the attached file. Here are my results: $ python sorttest.py John 1:[5.324422796752101, 6.9189729420918029, 6.771758421810592] John 2:[3.4790142322557749, 3.4373198269612502, 3.392254322825945] Kent:[29.745739472474856, 42.376246143015386, 38.031272689685423] (each time represents 100,000 reps) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Split a string into characters
Bingo :) Thanks Rick -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Rick Pasotto Sent: Tuesday, 23 August 2005 1:55 p.m. To: tutor@python.org Subject: Re: [Tutor] Split a string into characters On Tue, Aug 23, 2005 at 01:18:43PM +1200, Hans Dushanthakumar wrote: > Hi, >A quick one... >How do I split a string like "Hans" into a list of characters > ['H','a','n','s']? > Note that there are no spaces in the original string. >Str.split() without any arguments looks for whitespace as splitting > character. So, this doesn't serve the purpose. > And str.split("") appears to be illegal. list('Hans') -- "History will be kind to me, for I intend to write it." -- Winston Churchill Rick Pasotto[EMAIL PROTECTED]http://www.niof.net ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Split a string into characters
>How do I split a string like "Hans" into a list of characters > ['H','a','n','s']? >>> list('fred') ['f', 'r', 'e', 'd'] >>> HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor