Re: [Tutor] file?
> How can we know that one specific file is already exist in filesystem? > for instance, I want to read zipfile by issuing code: Take a look in the os.path module. There is explanation of how to check for various aspects of files (including existence) in my web tutor Operating System topic. > if the inputfilename doesn't exist then an error would be occurred. > I want to catch up this special case first. The other approach, implied in your question is simply to *try* to *open* for read: try: open(inputfilename) except IOError: # it presumably didn't exist, so deal with it 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] Hi
On 4/4/06, Kaushal Shriyan <[EMAIL PROTECTED]> wrote: > Hi ALL > > A simple query is that the python mailing List is python powered > > What does "python powered" means The list, and many like it, use a piece of software called Mailman, which is written in Python. A few years back, the tool of choice was Majordomo, but Mailman is now very popular, partly because of its easy-to-use web interface. > Kaushal S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to get data from xml
Dear all Pythoners, I am quiet new to python and now working with xml. I have an xml like this: 0x6B 0x78 0x4B 0x58 0x67 0x63 0x71 0x43 0x51 0x6A and I want to get data for KA, KHA, KO and so on. Can you guide me a way. Btw, I'm using dom for xml. Thanks, Titvirak ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Bigrams and nested dictionaries
Michael Broe wrote: > dict = {} dict is the name of the builtin dictionary class, so you shouldn't use it as the name of your dict - you shadow the built-in name. file is also a built-in name. > > L = file[0] > for R in file[1:]:# move right edge of window across the file > if not L in dict: > dict[L] = {} > > if not R in dict[L]: > dict[L][R] = 1 > else: > dict[L][R] += 1 I might write it this way: dictL = dict.setdefault(L, {}) dictL[R] = dictL.setdefault(R, 0) + 1 though I'm not sure which way will be faster. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to get data from xml
ទិត្យវិរៈ wrote: > Dear all Pythoners, > > I am quiet new to python and now working with xml. I have an xml like this: > > > > > > 0x6B > 0x78 > 0x4B > 0x58 > 0x67 > 0x63 > 0x71 > 0x43 > 0x51 > 0x6A > > > and I want to get data for KA, KHA, KO and so on. Can you guide me a > way. Btw, I'm using dom for xml. I don't know much about xml.dom but it is easy with ElementTree: data = ''' 0x6B 0x78 0x4B 0x58 0x67 0x63 0x71 0x43 0x51 0x6A ''' from elementtree import ElementTree as ET doc = ET.fromstring(data) consonants = doc.find('.//CONSONANTS') for consonant in consonants: print consonant.tag, consonant.text Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Bits
Hello. Is it possible to read the bits (the 0's and 1's) of a string or a file with Python? What module would I use? Thanks in advance, Øyvind -- This email has been scanned for viruses & spam by Decna as - www.decna.no Denne e-posten er sjekket for virus & spam av Decna as - www.decna.no ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Bits
Øyvind wrote: > Hello. > > Is it possible to read the bits (the 0's and 1's) of a string or a file > with Python? What module would I use? I don't know exactly what you mean by "read the bits" but you can use data = open('afile', 'b').read() to get the data into a string byte1=ord(data[1]) to get a character as binary data byte1 & 1 to extract a single bit You might also be interested in the struct module which lets you extract larger data (e.g. ints and floats) from a string. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Protected methods/variables
Hello everybody I have been programming in object oriented languages for several years and I'm learning python now. I have missed protected method/variables in Python. How do you declare methods/variables used only by a class and their derived classes? Thanks in advance -- Miquel Oliete (a.k.a. Ktalà) http://apetite4destruction.org/blog Powered by Debian GNU/Linux Sid __ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] defined()
>> Which language(s) do you know that has such a feature? > > I should came from Marco Cantu's Delphi 2005 book, that I have read just > recently. > But I am unable to find it again. I'd be very surprised if it came from Delphi for two reasons: a) I used Delphi a lot for several years and never came across it! :-) b) Delphi (or Object Pascal) is strictly statically typed and wouldn't even compile with any undefined values in the code. BUT... I just checked and it *is* in Delphi - a new feature in Delphi 6. BUT it's not a language feature rather it is a compiler directive like the C #ifdef. (And starts with uppercase D BTW). His example: // const debugControl = 2 {$IF Defined(DEBUG) and DebugControl > 3} // do stuff here {$IFEND} //- There is also a Declared directive too. These are not primarily intended for determining whether a variable is defined or declared but to determine whether a particular language feature has been defined or constant declared in the current version of Delphi (post version 6 of course!) Now that's an entirely different question in terms of how we do that in Python! Which I'll lreave as an excercise for the readers ;-) -- 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] Bits
"Øyvind" <[EMAIL PROTECTED]> wrote in message > Is it possible to read the bits (the 0's and 1's) of a string or a file > with Python? What module would I use? Yes, you can read the bits by reading bytes and applying bitmasks and bitwise operations. You can get the bytes using the struct module (explained in the File Handling topic of my tutor) and using bitmasks with bitwise operators to extract bits is covered in the Operating System topic of same. -- 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] Protected methods/variables
"Miquel Oliete" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I have been programming in object oriented languages for several years > and I'm learning python now. Congratulations :-) > I have missed protected method/variables in Python. In what sense have you missed them? Have you been hitting a lot of bugs because you didn't have them? OIr do you just mean you are having withdrawal symptoms after working in more conservative OOP languages like C++/Java etc? > How do you declare methods/variables used only by a class > and their derived classes? You don't. Protected was a scheme introduced by Bjarne Stroustrup at the behest of Mark Linton to support Mark's Interviews GUI library in C++ (version 1.2) and subsequently copied in Java. They are part of the statically and strictly typed idioms of OOP used in those languages. However Stroustrup writes in his book "The Design & Evolution of C++" : "In retrospect, I think that protected is a case where "good arguments" and fashion overcame my better judgement..." And Linton himself withdrew support for protected members from Interviews around the same time - they were causing bugs and maintenance problems! Most OOP languages and especially dynamic OOP languages prefer the freedom of expression and flexibility that dynamic typing (or duck typing as its sometimes called) affords. Objective C tries to combine boith with mixed results. But in practice I can honestly say I have never missed having the protected keyword (I do occasionally like to use private - for which there is a convention in Python) but never have I missed protected. What do you find you need it for? -- 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] Protected methods/variables
> I have missed protected method/variables in Python. How do you declare > methods/variables used only by a class and their derived classes? hi Ktalà, welcome to Python! you missed "protection" in OOP with Python bceause there are no such declarations in Python! 1) there is a privacy *hint*, which is using "__" to start a variable or method name with, e.g., def __myMaybePrivateMethod(self,...). this does name-mangle the attribute at run-time, however the algorithm is fairly well-known and easy to crack: self._CLASS__myMaybePrivateMethod(). for more on this see,: http://docs.python.org/tut/node11.html#SECTION001160 2) however, with Python's new-style classes introduced back in 2.2, there are much more powerful security mechanisms you can employ so that you can customize any or all of your attributes (data attributes [variables] or methods) to give it the most fine-grained security you can imagine. - you can use property() for your attributes to assign a getter, setter, and deleter method that get executed every time someone tries to access, set, and delete an instance attribute: | class C(object): | def getx(self): return self.__x | def setx(self, value): self.__x = value | def delx(self): del self.__x | x = property(getx, setx, delx, "I'm the 'x' property.") - you can use descriptors (__get__, __set__, __delete__) that get executed every time someone tries to access, set, and remove your object... this allows you to really customize instances and how attribute access occurs. using descriptors, you can bypass the regular way of attribute search, i.e., x.foo normally looks at object 'x' for 'foo', and if not found, searches type(x) for 'foo', and then it progresses this search to ancestor classes. with descriptors, you can override this default behavior to make instances differ from one another. in fact, properties (as seen above) is an example of a descriptor! - you can use __slots__ to restrict arbirtrary creation of (dynamic) instrance attributes - you can even customize how classes are created (by using metaclasses and __metaclass__) so as you can see, you can do a lot more with Python classes than what you can do with 'private', 'protected', 'friend', or 'protected friend'. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Protected methods/variables
w chun wrote: >> I have missed protected method/variables in Python. How do you declare >> methods/variables used only by a class and their derived classes? > > hi Ktalà, > > welcome to Python! you missed "protection" in OOP with Python bceause > there are no such declarations in Python! > > 1) there is a privacy *hint*, which is using "__" to start a variable > or method name with, e.g., def __myMaybePrivateMethod(self,...). > this does name-mangle the attribute at run-time, however the algorithm > is fairly well-known and easy to crack: > self._CLASS__myMaybePrivateMethod(). for more on this see,: > http://docs.python.org/tut/node11.html#SECTION001160 1a) start a variable name with _ which is a hint to users of the class that the variable is for internal use only. > - you can use __slots__ to restrict arbirtrary creation of (dynamic) > instrance attributes You can do this, but it is generally considered a misuse of __slots__ and potentially problematic. Python takes the attitude that "we're all adults here" and doesn't try to hide anything. The _ naming convention is part of this culture - you tell the users to keep hands off, but if they don't, on their head be it! Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Protected methods/variables
> > - you can use __slots__ to restrict arbirtrary creation of > (dynamic) > > instrance attributes > > You can do this, but it is generally considered a misuse of > __slots__ and potentially problematic. > I'll bite. What is the proper/intended use of __slots__? Does it have something to do with memory? Mike ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Protected methods/variables
Mike Hansen wrote: >>> - you can use __slots__ to restrict arbirtrary creation of >> (dynamic) >>> instrance attributes >> You can do this, but it is generally considered a misuse of >> __slots__ and potentially problematic. >> > > I'll bite. What is the proper/intended use of __slots__? Does it have > something to do with memory? Yes, it is intended specifically to reduce memory consumption of objects that are going to be instantiated a lot. I'm not sure how many counts as a lot, but in the thousands at least. Using __slots__ saves the cost of the attribute dict for each instance. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Space the final frontier!
Dear All, I am having difficulty removing white spaces from my file. The file is 999 lines long and looks like the sample below: 001, new field,dial= 028 90 79 0154, dial= 002, borfiled, dial= 02890 618521, dial= 003, newcomp, dial=02890419689, dial= The program, I am using to import the file does not like the spaces around the numbers. The number should look like the "dial=02890419689" in the third line. Thus the sample above should look like: 001,newfield,dial=02890790154,dial= 002,borfiled,dial=02890618521,dial= 003,newcomp,dial=02890419689,dial= I have searched the tutor mailbag already and have picked up some good tips on join, split and re but I can't seem to get it to work. I am using the following code. filename = "c:/test.txt" import string import os import re listy = [] input = open( filename, 'r')#read access for line in input.readlines(): y = line listy.append(y) print listy x = listy.pop() re.sub(r'\s', '', x) print y,x del input It produces the output: ['001, new field,dial= 028 90 79 0154, dial=\n'] 001, new field,dial= 028 90 79 0154, dial= 001, new field,dial= 028 90 79 0154, dial= ['002, borfiled, dial= 02890 618521, dial=\n'] 002, borfiled, dial= 02890 618521, dial= 002, borfiled, dial= 02890 618521, dial= ['003, newcomp, dial=02890419689, dial='] 003, newcomp, dial=02890419689, dial= 003, newcomp, dial=02890419689, dial= Any help would be greatly appreciated. Regards, John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Question about large numbers of arguments
Hello, Suppose you have a situation where you have a large number of command-line options that you will parse with getopt. You want to keep track of these as you move around in the code and do various things. Is it more Pythonic to: Have the functions take large numbers of parameters. or Create an options class to pass the options around. I personally think the latter would look a lot cleaner once the number of options got up to around a half dozen, but I usually see the "large number of parameters" style in other people's code. I suppose I can lessen some of the noise by using Python's rules for argument defaults, but I think that just adds to the confusion. Thanks, Dana Robinson ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Space the final frontier!
Hi John, It would be easier to do all of your whitespace elimination before you append the string to your list(s). Something like this I should get you started: for line in input.readlines(): line = re.sub('[\s]+', '', line) listy.append(line) print listy bonus points for appending the return from re.sub to your list. :) -mtw On Tue, Apr 04, 2006 at 10:33:18PM +0100, John Corry ([EMAIL PROTECTED]) wrote: > Dear All, > > I am having difficulty removing white spaces from my file. The file is 999 > lines long and looks like the sample below: > > 001, new field,dial= 028 90 79 0154, dial= > 002, borfiled, dial= 02890 618521, dial= > 003, newcomp, dial=02890419689, dial= > > The program, I am using to import the file does not like the spaces around > the numbers. The number should look like the "dial=02890419689" in the > third line. Thus the sample above should look like: > > 001,newfield,dial=02890790154,dial= > 002,borfiled,dial=02890618521,dial= > 003,newcomp,dial=02890419689,dial= > > I have searched the tutor mailbag already and have picked up some good tips > on join, split and re but I can't seem to get it to work. > > I am using the following code. > > filename = "c:/test.txt" > import string > import os > import re > listy = [] > input = open( filename, 'r') #read access > for line in input.readlines(): > y = line > listy.append(y) > print listy > x = listy.pop() > > re.sub(r'\s', '', x) > print y,x > > del input > > It produces the output: > > ['001, new field,dial= 028 90 79 0154, dial=\n'] > 001, new field,dial= 028 90 79 0154, dial= > 001, new field,dial= 028 90 79 0154, dial= > > ['002, borfiled, dial= 02890 618521, dial=\n'] > 002, borfiled, dial= 02890 618521, dial= > 002, borfiled, dial= 02890 618521, dial= > > ['003, newcomp, dial=02890419689, dial='] > 003, newcomp, dial=02890419689, dial= 003, newcomp, dial=02890419689, dial= > > Any help would be greatly appreciated. > > Regards, > > John. > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Matthew White - District Systems Administrator Tigard/Tualatin School District 503.431.4128 "The greatest thing in this world is not so much where we are, but in what direction we are moving." -Oliver Wendell Holmes ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about large numbers of arguments
Dana Robinson wrote: > Hello, > > Suppose you have a situation where you have a large number of command-line > options that you will parse with getopt. You want to keep track of these > as you move around in the code and do various things. > > Is it more Pythonic to: > > Have the functions take large numbers of parameters. > > or > > Create an options class to pass the options around. > IMHO the above or a dictionary. > > I personally think the latter would look a lot cleaner once the number of > options got up to around a half dozen, but I usually see the "large number > of parameters" style in other people's code. I suppose I can lessen some > of the noise by using Python's rules for argument defaults, but I think > that just adds to the confusion. > > Thanks, > > Dana Robinson > ___ > 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] Space the final frontier!
[Sorry for the initial misfire, John] > [mailto:[EMAIL PROTECTED] On Behalf Of John Corry > 001,newfield,dial=02890790154,dial= > 002,borfiled,dial=02890618521,dial= > 003,newcomp,dial=02890419689,dial= Hi John: I believe the common idiom in this case is ''.join( theString.split( ' ' ) ) >>> theLines = [ '001,newfield,dial=02890790154,dial=', '002,borfiled,dial=02890618521,dial=', '003,newcomp,dial=02890419689,dial='] >>> for line in theLines: print ''.join( line.split( ' ' ) ) 001,newfield,dial=02890790154,dial= 002,borfiled,dial=02890618521,dial= 003,newcomp,dial=02890419689,dial= Regards, Ryan --- Ryan Ginstrom http://ginstrom.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor