Re: [Tutor] xls file
"Kirk Bailey" <[EMAIL PROTECTED]> wrote > Ii want to read a xls file and use the data in part of it. What > module > would help make sense of one? If the data is straighforward you might find it easier to save it as a csv file first. But otherwise there is a module called pyexcelerator (I think?) which can work with Excel 97 and 95 formats. http://sourceforge.net/projects/pyexcelerator HTH, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting a source package into a dll/shared library?
I asked: > Is it possible to convert a Python package, with __init__.py and > related python modules, into a single DLL or shared library that can > be imported in the same way? > > We have used py2exe and cx_freeze to create a complete executable, > but we are curious whether there is a middle way between this single > executable and distributing all of the source files. Kent suggested: K> You can get a modest degree of obscurity by distributing the .pyc K> bytecode files instead of the .py source. These can still be K> decompiled and reverse engineered but it is more effort. Yes we really just want to protect ourselves from giving code relating to various companies' products to their competitors, even though we wrote the code ourselves based on the behaviour of our own test input files for their tools. We don't want any company to feel that we have exposed any internal details about how their tools might work. We had already found the py_compile and compileall modules, and had even gone one further than .pyc files to create .pyo files because these have no docstrings either. We also found the dis module, but it is unlikely that anyone will to go to all the effort to disassemble and reverse engineer the code. K> I suppose you could rewrite some or all of the code into the Python K> dialect supported by Pyrex and compile it that way. That's something to remember for the future, but we already have 100K lines of code for the 6 different tool formats that we currently handle, so it would be non-trivial to change now. Alan also suggested: A> Since you refer to DLLs I'll assume a Windoze platform. A> If so the answer is yes you can create an ActiveX/COM object. A> A> So if its accessibility to non Python code you are interested A> in grab a copy of Mark Hammonds Win32 book for details A> and examples. You can even go DCOM if thats significant. It's a demonstrator project, so we've written the whole thing in Python for the rapid development side, and so far we have not had to worry about accessing non-Python code. Each company only needs to see how we've mapped their own tool formats into our neutral format. Actual integration in their non-Python code will be a completely different story involving an alternate code generator spitting out C/C++... Thanks for confirming our ideas and the extra feedback. Duncan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] xls file
Alan Gauld <[EMAIL PROTECTED]> wrote: "Kirk Bailey" wrote > Ii want to read a xls file and use the data in part of it. What > module > would help make sense of one? If the data is straighforward you might find it easier to save it as a csv file first. But otherwise there is a module called pyexcelerator (I think?) which can work with Excel 97 and 95 formats. http://sourceforge.net/projects/pyexcelerator HTH, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Try this. I found it on the web. (xlrd package) I think you don't even need windows to use these. I tried it and it works well. http://www.lexicon.net/sjmachin/xlrd.htm http://cheeseshop.python.org/pypi/xlrd - Ready for the edge of your seat? Check out tonight's top picks on Yahoo! TV. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Class Attribute "Overloading?"
Sorry, I just started experimenting on Python classes... Is there any way a reference to a class attribute ([class].[attribute]) be treated like a method ([class].[method]())? I have a class with DB component. Some of its attributes are derived from DB and I find it impractical to derive all at once upon __init__. Of course we can use dictionary-like syntax ([class]["[attribute]"]) and use __getitem__, checking for existence and None of the attribute before deriving the value from the DB. But I think [class].[attribute] is more conventional (Is it?). Or someone suggest a more Pythonic way. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] KeyError list?
Is there a comprehensive list of dictionary KeyError meanings? I could sure use one these days and haven't had much luck tracking one down yet. -Rob ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Book Recommendations
At 04:38 PM 8/14/2007, Fiyawerx wrote: My company has a subscription with the books24x7.com site, and I'm sure they offer individual accounts, but so far I'm ashamed that I've paid close to 200$ worth of computer books that I could have been accessing online for free. Including 'dummies' books, Teach yourself whatever, and just a multitude of other books. Just did a quick search for titles with 'python' and returned about 20. My public library system in the Seattle area (< http://www.kcls.org/>) has access to Books 24x7 for cardholders. It seems there may be different classes of access, because I just searched on 'python' in titles and got only 15 hits. The latest published book is Python for Dummies (2006). And that's the ONLY Python book for 2006. Dick Moores ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Attribute "Overloading?"
> Is there any way a reference to a class attribute ([class].[attribute]) be > treated like a method ([class].[method]())? > : > Of course we can use dictionary-like syntax ([class]["[attribute]"]) and use > __getitem__, checking for existence and None of the attribute before > deriving the value from the DB. But I think [class].[attribute] is more > conventional (Is it?). Or someone suggest a more Pythonic way. i'm not sure exactly what you're asking, but let me say the following: - you can use the Boolean hasattr() to determine if any object (not just classes) have an attribute, e.g., hasattr(MyClass, 'anAttr') - if myInstance is an instance of MyClass, e.g., if the Boolean isinstance(myInstance, MyClass) returns true, then you can also do hasattr(myInstance, 'anAttr'). (or 'self' if this code is defined within a method) - you can use the Boolean callable() to determine if it can be "called," or executed like a function, e.g., is hasattr(self, 'anAttr') and callable(self.anAttr): self.anAttr(...) hasattr(), isinstance(), callable()... 3 useful built-in functions... hopefully 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] KeyError list?
> Is there a comprehensive list of dictionary KeyError meanings? > > I could sure use one these days and haven't had much luck tracking one down > yet. can you give us an example of what you're looking for? generally: exception KeyError: Raised when a mapping (dictionary) key is not found in the set of existing keys from: http://www.python.org/doc/2.5/lib/module-exceptions.html in other words, you tried to access an element of a dictionary with a key that is not in that dict. using the get method [e.g., myDict.get(key, 'N/A')] is a better way to avoid these exceptions than just trying to get something with myDict[key]. -- 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] Livewires - stuck on a class
Alan Gauld wrote: > "Tonu Mikk" <[EMAIL PROTECTED]> wrote > > >> I create more robots in this way which seems to work: >> class Robot: >>pass >> > > By using an empty class you are losing m,uch of the power of classes. > > I would need to learn more to handle the classes better. In this case, I am following the Livewires tutorial pretty closely which uses an empty class. >> def move_robot(): >>for x in robots: >>while 1: >>if robot.x + 0.5< player.x and robot.y +0.5< player.y: >> > > You are doing *for x in robots* but then moving *robot* not x. > Yes, that was my mistake. I changed the line to "for robot in robots:" and now it works! > > Also it would be better IMHO to use if/elif rather than all those > if/breaks. > I followed your advice here. It does make the code a bit more clear. Thanks a lot, Tonu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Attribute "Overloading?"
Vincent Gulinao wrote: > Sorry, I just started experimenting on Python classes... > > Is there any way a reference to a class attribute ([class].[attribute]) > be treated like a method ([class].[method]())? > > I have a class with DB component. Some of its attributes are derived > from DB and I find it impractical to derive all at once upon __init__. > > Of course we can use dictionary-like syntax ([class]["[attribute]"]) and > use __getitem__, checking for existence and None of the attribute before > deriving the value from the DB. But I think [class].[attribute] is more > conventional (Is it?). Or someone suggest a more Pythonic way. I think you are looking for either __getattr__() or properties. __getattr__() lets you intercept access to atttributes that are not found via the usual attribute lookup. It is useful for delegation which I think is what you are trying to do. e.g. class Proxy(object): def __init__(self, delegate): self.delegate = delegate def __getattr__(self, attr): return getattr(self.delegate, attr) then with p = Proxy(realObject) p.x becomes p.delegate.x i.e. realObject.x http://docs.python.org/ref/attribute-access.html Properties OTOH let you customize access to a particular attribute so p.x becomes a method call. http://www.python.org/download/releases/2.2/descrintro/#property Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] KeyError list?
Rob Andrews wrote: > Is there a comprehensive list of dictionary KeyError meanings? ?? AFAIK there is only one meaning for KeyError - the key was not found in the dict. From the docs: "Raised when a mapping (dictionary) key is not found in the set of existing keys." Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Book Recommendations [Was:[Re: Security]]
Hello list, thanks to everybody who shared their experience with books and their usefulness. I think with regard to the other thread that passed lately: Books with exercises and problems to solve - http://article.gmane.org/gmane.comp.python.tutor/42394 we must admit there are two type of learners: * real programming beginners/novices who take Python to enter the world of programmin or need to program only to solve some specific problems that are arising in other areas of work like scientific data analysis, modelling or engineering * computer professionals who are very familiar with programming and already know some lingos. They need a different learning approach than the first group. I for instance belong to the first group. I need Python for effective data manipulation & analysis or program customatization/scripting within other programs, etc. I bought "Python Scripting for Comuptional Science" by H. Langtangen. While this book adresses exctly my needs as far as the content and spcialization is concerned I want to start off learning with a book that helps me to pick up easier. Learning -- which is a auto-learning and free choice in my case -- should be fun! Therefore I am consindering to buy "Python Programming for the Absolute Beginner". Did anyone here use this book for leaning? Is it easy enough for a non-programmer while not being too light? Kind regards, Tim ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting a source package into a dll/shared library?
"Duncan Gibson" <[EMAIL PROTECTED]> wrote > Alan also suggested: > A> So if its accessibility to non Python code you are interested > A> in grab a copy of Mark Hammonds Win32 book for details Actually I meant to say So if its accessibiity *from* non python code But since that doesn't appear to be an issue either... As to access to the internal data would it be difficult to parameterise it so that each companies data is in a local config file? That way even if the dissasemble the code it won't help? Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] KeyError list?
"Rob Andrews" <[EMAIL PROTECTED]> wrote > Is there a comprehensive list of dictionary KeyError meanings? I don't understand the question Rob. A KeyError means the key used wasn't found in the dictionary What other meanings are you thinking of? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Attribute "Overloading?"
Vincent Gulinao wrote: > > > Sorry, I just started experimenting on Python classes... > > Is there any way a reference to a class attribute ([class].[attribute]) > be treated like a method ([class].[method]())? > > I have a class with DB component. Some of its attributes are derived > from DB and I find it impractical to derive all at once upon __init__. > > Of course we can use dictionary-like syntax ([class]["[attribute]"]) and > use __getitem__, checking for existence and None of the attribute before > deriving the value from the DB. But I think [class].[attribute] is more > conventional (Is it?). Or someone suggest a more Pythonic way. > Do you simply want to dynamically create class attributes? If so, you can use setattr. Something like this class foo(object): def __init__(self, *args): for i in args: setattr(self,i,"Hello=%s"%i) x=foo("a","b","c") print x.a,x.b,x.c will print Hello=a Hello=b Hello=c -- ~noufal ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Attribute "Overloading?"
Sorry about that. I want something like: class foo: def __init__(self): self.attr1 = None def get_attr1(self): if not self.attr1: attr1 = self.attr1 = attr1 return self.attr1 such that: foo_instance = foo() then: foo_instance.get_attr1() and foo_instance.attr1 gets the same value. Such that you get to derive attr1 only as needed and just once, both outside and within foo class. Or is it a bad idea or just plain ugly to do something like that? If it is, kindly suggest better approach. Thanks. On 8/16/07, bob gailer <[EMAIL PROTECTED]> wrote: > > Vincent Gulinao wrote: > > > > Sorry, I just started experimenting on Python classes... > > > > Is there any way a reference to a class attribute > > ([class].[attribute]) be treated like a method ([class].[method]())? > > > Attributes are objects. A method is an object. If you want to know > something more specific, please rephrase the question. > > I for one don't really know what you want. > > > > I have a class with DB component. Some of its attributes are derived > > from DB and I find it impractical to derive all at once upon __init__. > > > > Of course we can use dictionary-like syntax ([class]["[attribute]"]) > > and use __getitem__, checking for existence and None of the attribute > > before deriving the value from the DB. But I think [class].[attribute] > > is more conventional (Is it?). Or someone suggest a more Pythonic way. > > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Attribute "Overloading?"
"Vincent Gulinao" <[EMAIL PROTECTED]> wrote > Is there any way a reference to a class attribute > ([class].[attribute]) be > treated like a method ([class].[method]())? Yes, classes are just objects and their attributes are too. class Test: pass def f(self): print 'howdy!' Test.foo = f t = Test() # creae an instance t.foo() # access the class method Creating actual class methods at runtime and calling them via the class is something I haven't tried but it might be possible... Yep it works: class Test: pass Test.foo = staticmethod(lambda: 'hello') Test.foo() 'hello' HTH, -- 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] Class Attribute "Overloading?"
Vincent Gulinao wrote: > > Sorry, I just started experimenting on Python classes... > > Is there any way a reference to a class attribute > ([class].[attribute]) be treated like a method ([class].[method]())? > Attributes are objects. A method is an object. If you want to know something more specific, please rephrase the question. I for one don't really know what you want. > > I have a class with DB component. Some of its attributes are derived > from DB and I find it impractical to derive all at once upon __init__. > > Of course we can use dictionary-like syntax ([class]["[attribute]"]) > and use __getitem__, checking for existence and None of the attribute > before deriving the value from the DB. But I think [class].[attribute] > is more conventional (Is it?). Or someone suggest a more Pythonic way. > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] KeyError list?
Rob Andrews wrote: > Is there a comprehensive list of dictionary KeyError meanings? > >>> d = {1:2} >>> d[1] 2 >>> d[2] KeyError AFAIK that's it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Need help on "How to Think Like a Computer Scientist: Learning with Python".
Hello All, I am new to computer programming and chose to learn Python. Now I am stuck with certain codes that I need help. Please let me know if there is an answer list to the exercises in "How to Think Like a Computer Scientist: Learning with Python." I followed and did good on most of the exercises but I have been stuck on a few of them which burned me up after spending days and nights trying to make them works. I hope you understand the feeling when you are stuck and need a drop of water to keep alive. I really appreciate any assistance you can give. Thanks. Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user panel and lay it on us. http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] open multiple files
Hi everyone - I'm beginning to learn how to program in python. I need to process several text files simultaneously. The program needs to open several files (like a corpus) and output the total number of words. I can do that with just one file but not the whole directory. I tried glob but it didn't work. Your ideas are greatly appreciated. Thanks, Paulo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] open multiple files
Paulo Quaglio wrote: > Hi everyone - > I'm beginning to learn how to program in python. I need to process > several text files simultaneously. The program needs to open several > files (like a corpus) and output the total number of words. I can do > that with just one file but not the whole directory. I tried glob but it > didn't work. Your ideas are greatly appreciated. Can you show us what you have so far? Is this a homework problem? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] xls file
Really? What are you having trouble with? I have used pyexcelerator under Windows without problems. -- John. On 16/08/07, Kirk Bailey <[EMAIL PROTECTED]> wrote: > looks good. works bad; this is a windows workplace. ouch. Advice please > (other than change operating systems)? > > John Fouhy wrote: > > On 15/08/07, Kirk Bailey <[EMAIL PROTECTED]> wrote: > >> Ii want to read a xls file and use the data in part of it. What module > >> would help make sense of one? > > > > I have used pyExcelerator in the past. You can find it with google. > > > > Sample usage: > > > > import pyExcelerator > > workbook = pyExcelerator.parse_xls('filename.xls') > > worksheet = workbook['Sheet 1'] > > # print cells A1 and B1 > > print worksheet[(0,0)], worksheet[(0,1)] > > > > -- > Salute! > -Kirk Bailey >Think > +-+ > | BOX | > +-+ >knihT > > Fnord. > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Attribute "Overloading?"
Vincent Gulinao wrote: > Sorry about that. I want something like: > > class foo: > > def __init__(self): > > self.attr1 = None > > > def get_attr1(self): > > if not self.attr1: > > attr1 = > > self.attr1 = attr1 > > return self.attr1 > > > such that: > > foo_instance = foo() > > then: > > foo_instance.get_attr1() > > and > > foo_instance.attr1 > > gets the same value. > > > Such that you get to derive attr1 only as needed and just once, both > outside and within foo class. I would do it like this: class Foo(object): def __getattr__(self, attr): if not : raise AttributeError( "'%s' object has no attribute '%s'" % (self.__class__.__name__, attr)) value = setattr(self, attr, value) return value You don't need to init attr1 and you don't need get_attr1(). The first time you try to read foo.attr1, there will be no attr1 attribute so __getattr(self, 'attr1') will be called. You read the value from the database and save it as an ordinary attribute; future accesses will read the ordinary attribute and skip the __getattr__() call. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help on "How to Think Like a Computer Scientist: Learning with Python".
Greetings, I think the accepted way to get help on this list is to post the code you're having problems with, showing what you've done, the error messages you're getting, and an explanation of what you're trying to do, the input, output expected, and so forth. It is helpful for the Tutors to also know the platform/version you're working on (MS-XP, Linux kernel 2.6, Mac OSX), as well as the version of Python you're using 2.2, 2.3, 2.4, 2.5 -- bhaaluu at gmail dot com On 8/15/07, Vanneth Tea <[EMAIL PROTECTED]> wrote: > Hello All, > > I am new to computer programming and chose to learn > Python. Now I am stuck with certain codes that I need > help. Please let me know if there is an answer list > to the exercises in "How to Think Like a Computer > Scientist: Learning with Python." > > I followed and did good on most of the exercises but I > have been stuck on a few of them which burned me up > after spending days and nights trying to make them > works. > > I hope you understand the feeling when you are stuck > and need a drop of water to keep alive. I really > appreciate any assistance you can give. > > Thanks. > > > > > > > > Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user panel > and lay it on us. http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7 > > ___ > 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] Need help on "How to Think Like a Computer Scientist: Learning with Python".
I don't know of any answer list but one you can ask for help here or what I do when I have a question i stop working on it for anywhere from an hour to a week and go back to it you will solve it if you solve it in your sleep well thats a whole other issue but that does happen. -Amadeo On 8/15/07, Vanneth Tea <[EMAIL PROTECTED]> wrote: > > Hello All, > > I am new to computer programming and chose to learn > Python. Now I am stuck with certain codes that I need > help. Please let me know if there is an answer list > to the exercises in "How to Think Like a Computer > Scientist: Learning with Python." > > I followed and did good on most of the exercises but I > have been stuck on a few of them which burned me up > after spending days and nights trying to make them > works. > > I hope you understand the feeling when you are stuck > and need a drop of water to keep alive. I really > appreciate any assistance you can give. > > Thanks. > > > > > > > > > Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user > panel and lay it on us. > http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7 > > ___ > 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] Class Attribute "Overloading?"
bob gailer wrote: > Vincent Gulinao wrote: >> Sorry, I just started experimenting on Python classes... >> >> Is there any way a reference to a class attribute >> ([class].[attribute]) be treated like a method ([class].[method]())? >> > Attributes are objects. A method is an object. If you want to know > something more specific, please rephrase the question. > I for one don't really know what you want. And since wesley, Alan and I have given completely different answers, I guess none of us know. A clearer question is clearly needed. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help on "How to Think Like a Computer Scientist: Learning with Python".
I don't know of an answer key, but feel free to post questions about what you're having trouble with and we'd be happy to discuss various approaches among the list members. Vanneth Tea wrote: > Hello All, > > I am new to computer programming and chose to learn > Python. Now I am stuck with certain codes that I need > help. Please let me know if there is an answer list > to the exercises in "How to Think Like a Computer > Scientist: Learning with Python." > > I followed and did good on most of the exercises but I > have been stuck on a few of them which burned me up > after spending days and nights trying to make them > works. > > I hope you understand the feeling when you are stuck > and need a drop of water to keep alive. I really > appreciate any assistance you can give. > > Thanks. > > > > > > > > Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user panel > and lay it on us. http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7 > > ___ > 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] Need help on "How to Think Like a Computer Scientist: Learning with Python".
Vanneth Tea wrote: > Hello All, > > I am new to computer programming and chose to learn > Python. Now I am stuck with certain codes that I need > help. Where are you stuck? We should be able to help you here if you can be more specific. It helps us to help you if you can show us - the code you have tried - what happens when you run the code - the specific error message and traceback, if that is what you get - what you want to happen Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help on "How to Think Like a Computer Scientist:Learning with Python".
We've all been there. But what exactly is the problem you're having? Its really hard to know what sort of help you require if you didn't tell us what the problem is. Please help us to help you! Ciao - Original Message - From: "Vanneth Tea" <[EMAIL PROTECTED]> To: Sent: Wednesday, August 15, 2007 6:08 PM Subject: [Tutor] Need help on "How to Think Like a Computer Scientist:Learning with Python". > Hello All, > > I am new to computer programming and chose to learn > Python. Now I am stuck with certain codes that I need > help. Please let me know if there is an answer list > to the exercises in "How to Think Like a Computer > Scientist: Learning with Python." > > I followed and did good on most of the exercises but I > have been stuck on a few of them which burned me up > after spending days and nights trying to make them > works. > > I hope you understand the feeling when you are stuck > and need a drop of water to keep alive. I really > appreciate any assistance you can give. > > Thanks. > > > > > > > > Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user > panel and lay it on us. > http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7 > > ___ > 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] Need to convert 1,987,087,234,456 to an int
If a line in a script has n = "1,987,087,234,456" It's simple to convert the string, "1,987,087,234,456" to an int, but I can't figure out how to do it for n = 1,987,087,234,456 # no quotes (I want to be able to type in big ints using commas--much easier to see that I've typed correctly) Python sees 1,987,077,234,456 as a tuple: >>>type(1,987,077,234,456) But: >>>type(1,987,087,234,456) File "", line 1 type(1,987,087,234,456) ^ SyntaxError: invalid token (My wild guess is that 087 is octal.) Anyway, I need to convert things such as 1,987,077,234,456 to ints. This will do it: (m can also be an int such as 1987077234456) if type(m) == tuple: mAsStr = "" for part in m: part = str(part) mAsStr += part m = int(mAsStr) So this is fine as long as none of the parts of the tuple is of the form 08X (where X is from 0-9). What to do? Thanks, Dick Moores ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] open multiple files
On Wed, Aug 15, 2007 at 08:26:45AM -0700, Paulo Quaglio wrote: > Hi everyone - > I'm beginning to learn how to program in python. I need to > process several text files simultaneously. The program needs to > open several files (like a corpus) and output the total number > of words. I can do that with just one file but not the whole > directory. I tried glob but it didn't work. Your ideas are > greatly appreciated. Thanks, Here is a pattern that you might use (if I understand your problem correctly): import glob names = glob.glob('*.txt') for name in names: infile = open(name, 'r') etc. etc infile.close() Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Attribute "Overloading?"
> Sorry about that. I want something like: > > class foo: > >def __init__(self): > > self.attr1 = None > > >def get_attr1(self): > > if not self.attr1: > > attr1 = > > self.attr1 = attr1 > > return self.attr1 > > > such that: > > foo_instance = foo() > > then: > > foo_instance.get_attr1() > > and > > foo_instance.attr1 > > gets the same value. > > Such that you get to derive attr1 only as needed and just once, both > outside > and within foo class. > > Or is it a bad idea or just plain ugly to do something like that? If it > is, > kindly suggest better approach. > > Thanks. It's a little ugly but not too bad. What you are describing are properties. class Foo: def _get_attr1(self): if not self.attr1: attr1 = TheValue return attr1 def _set_attr1(self, value): self.attr1 = value ChangeDBFunction(value) attr1 = property(self._get_attr1,self._setattr1,None,None) I think that will work. The docs seem to suggest that you should subclass `object`. JS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Attribute "Overloading?"
"Vincent Gulinao" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Sorry about that. I want something like: > > class foo: > >def __init__(self): > self.attr1 = None >def get_attr1(self): > if not self.attr1: > attr1 = > self.attr1 = attr1 > return self.attr1 > > such that: > foo_instance = foo() > foo_instance.get_attr1() > foo_instance.attr1 > > gets the same value. That looks like an ideal candidate for a "property". Using a property you can treat it as a data item but behind the scenes call a getter and setter method. In your case the getter would be the method you have above and the setter could write the new value to the database, if needed. Thus after defining the property attr1 you access like: foo_instance = Foo() print foo.attr1 # executes the getter foo.attr1 = 42 # executes the setter HTH, -- 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] Need help on "How to Think Like a Computer Scientist:Learning with Python".
"Vanneth Tea" <[EMAIL PROTECTED]> wrote > have been stuck on a few of them which burned me up > after spending days and nights trying ... > I hope you understand the feeling when you are stuck Yep we all understand and sympathise. But before we can help you we need some more information. Like: what are you trying to do and what is not working. Post short code examples and full error messages. Don't assume we know what the tutorial exercises are, provide the context of the problem too. If you do that there is a very good chance we can help you. HTH, -- 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] Need help on "How to Think Like a Computer Scientist: Learning with Python".
Vanneth Tea wrote: > Hello All, > > I am new to computer programming and chose to learn > Python. Now I am stuck with certain codes that I need > help. Please let me know if there is an answer list > to the exercises in "How to Think Like a Computer > Scientist: Learning with Python." > > I followed and did good on most of the exercises but I > have been stuck on a few of them which burned me up > after spending days and nights trying to make them > works. > > I hope you understand the feeling when you are stuck > and need a drop of water to keep alive. I really > appreciate any assistance you can give. > I don't know if there's a site specifically for that book - check google maybe? As far as specific questions go, though, if you let us know what problem you're having we'd love to try to help you fix it. -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Fw: KeyError list?
Forwarding to list for completeness - Forwarded Message From: Rob Andrews <[EMAIL PROTECTED]> To: Alan Gauld <[EMAIL PROTECTED]> Sent: Wednesday, 15 August, 2007 10:32:30 PM Subject: Re: [Tutor] KeyError list? I guess I couldn't believe it was that simple. ;-) Sorry for the delayed, brief reply. I'm on my Treo while my workstation appends data to a sizable collection of demographic data. -Rob -- Libertarian Party of Mississippi: http://mslp.org I guess I couldn't believe it was that simple. ;-) Sorry for the delayed, brief reply. I'm on my Treo while my workstation appends data to a sizable collection of demographic data. -Rob -- Libertarian Party of Mississippi: http://mslp.org ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] open multiple files
> Hi everyone - > I'm beginning to learn how to program in python. I need to process > several text files simultaneously. The program needs to open several files > (like a corpus) and output the total number of words. I can do that with > just one file but not the whole directory. I tried glob but it didn't > work. Your ideas are greatly appreciated. Thanks, > Paulo What do you have so far? I don't just want to give you an answer. You say that glob didn't work... Probably because you are not realizing that glob returns a list of filenames, and you can't open a whole list at a time. You'll have to use a loop or something similar. JS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] open multiple files
"Paulo Quaglio" <[EMAIL PROTECTED]> wrote > I'm beginning to learn how to program in python. > I need to process several text files simultaneously. Do you really mean simultaneously or just all in the same execution of the program? ie The one program run opens each file in sequence and reports the total?. > The program needs to open several files (like a corpus) > and output the total number of words. OK, That sounds straightforward. > I can do that with just one file but not the whole directory. > I tried glob but it didn't work. glob usually works so I assume you mean that you didn't get it to do what you wanted? Recall that glob returns a list of filenames. You need to iterate over that list processing each file in turn. Can you show us a nbbit more of how you tried it? The code should be short enough that you can post it here on the list. Also post any error messages (the whole message not a summary). HTH, -- 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
[Tutor] Getting Date/Time from a file and using it in calculations
Hello there Messing around with certain time and datetime objects, I have managed to subtract a date/time from the present time thusly: from time import * import datetime one = datetime.datetime.now() two = datetime.datetime(2007, 8, 29, 11, 15, 00) difference = one - two print difference However, I have to take a date from a file and then insert it to where two should be, however to no success. I have managed to get a string containing the above date/time, but that is as far as I've gotten without it not working. Does anyone have any ideas on how to do this properly? Also I wish to display the result only in seconds left, rather than the current result, which just displays days/hours/minutes/seconds/milliseconds left, but am again struggling to progress. Any help would be amazingly appreciated :) Thanks again -Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Attribute "Overloading?"
Alan Gauld wrote: > "Vincent Gulinao" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> Sorry about that. I want something like: >> >> class foo: >> >>def __init__(self): >> self.attr1 = None >>def get_attr1(self): >> if not self.attr1: >> attr1 = >> self.attr1 = attr1 >> return self.attr1 >> >> such that: >> foo_instance = foo() >> foo_instance.get_attr1() >> foo_instance.attr1 >> >> gets the same value. > > > That looks like an ideal candidate for a "property". I guess the requirements are still not clear. If there is just one attribute to be read from the database, a property will work well. If the OP wants to delegate many attributes, the __getattr__ approach might be simpler. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Attribute "Overloading?"
Tiger12506 wrote: > It's a little ugly but not too bad. What you are describing are properties. > > class Foo: should be class Foo(object): > def _get_attr1(self): > if not self.attr1: > attr1 = TheValue > return attr1 You have to use a different name for the actual data attribute and the property (stack overflow, anyone?), and you didn't save the value. if not self._attr1: self._attr1 = TheValue return self._attr1 > def _set_attr1(self, value): > self.attr1 = value > ChangeDBFunction(value) > attr1 = property(self._get_attr1,self._setattr1,None,None) A read-only property might be more appropriate (no _set_attr1). > > I think that will work. The docs seem to suggest that you should subclass > `object`. That is required. Properties do not work correctly with old-style classes (not derived from object). Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need to convert 1,987,087,234,456 to an int
On 16/08/07, Dick Moores <[EMAIL PROTECTED]> wrote: > Python sees 1,987,077,234,456 as a tuple: > >>>type(1,987,077,234,456) > Hmm, not for me: >>> type(1,987,077,234,456) Traceback (most recent call last): File "", line 1, in TypeError: type() takes 1 or 3 arguments What python are you using? (2.5.1 for me) > I need to convert things such as 1,987,077,234,456 to ints. You could do this: >>> def decomma(*t): ... return int(''.join(str(i) for i in t)) ... Thus: >>> decomma(1,234,567) 1234567 Of course, if you have: >>> n = 1,234,567 Then you can't do this: >>> decomma(n) Instead, do this: >>> decomma(*n) 1234567 -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need to convert 1,987,087,234,456 to an int
At 06:58 PM 8/15/2007, John Fouhy wrote: >On 16/08/07, Dick Moores <[EMAIL PROTECTED]> wrote: > > Python sees 1,987,077,234,456 as a tuple: > > >>>type(1,987,077,234,456) > > > >Hmm, not for me: > > >>> type(1,987,077,234,456) >Traceback (most recent call last): > File "", line 1, in >TypeError: type() takes 1 or 3 arguments Tried again just now, and I got what you got. However: >>>m = 1,987,077,234,456 >>>type(m) == tuple True >What python are you using? (2.5.1 for me) XP, Python 2.5, editor is Ulipad > > I need to convert things such as 1,987,077,234,456 to ints. > >You could do this: > > >>> def decomma(*t): >... return int(''.join(str(i) for i in t)) >... > >Thus: > > >>> decomma(1,234,567) >1234567 That's cool! However, it doesn't solve the problem in my original post. >>> t = 1,987,087,234,456 File "", line 1 t = 1,987,087,234,456 ^ SyntaxError: invalid token >>> def decomma(*t): return int(''.join(str(i) for i in t)) >>> decomma(1,987,087,234,456) File "", line 1 decomma(1,987,087,234,456) ^ SyntaxError: invalid token Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fw: KeyError list?
> I guess I couldn't believe it was that simple. ;-) now if you had asked about the meanings of *all* exceptions (including warnings), or perhaps all the SMTP exceptions in smtplib, that would another matter. ;-) cheers, -- 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] Getting Date/Time from a file and using it in calculations
[EMAIL PROTECTED] wrote: > Hello there > > Messing around with certain time and datetime objects, I have managed to > subtract a date/time from the present time thusly: > datetime is definitely a module that takes a little getting used to. > from time import * > (Bad form) > import datetime > > one = datetime.datetime.now() > two = datetime.datetime(2007, 8, 29, 11, 15, 00) > > difference = one - two > > print difference > > However, I have to take a date from a file and then insert it to where two > should be, however to no success. I have managed to get a string containing > the above date/time, but that is as far as I've gotten without it not > working. > You don't show the format of the date string, but I'll show an example: >>> from datetime import datetime >>> start = datetime.strptime( '10:24:03 2006-11-01', '%H:%M:%S %Y-%m-%d' ) >>> end = datetime.strptime( '11:10:33 2006-11-01', '%H:%M:%S %Y-%m-%d' ) >>> delta = end - start >>> print delta.seconds 2790 Is that what you need? Take a skim through the library reference for the entire module. The result of the subtraction is a timedelta object, you kind of need to know what's available overall to be able to pick out the right functionality. Hope that helps, e. > Does anyone have any ideas on how to do this properly? > > Also I wish to display the result only in seconds left, rather than the > current result, which just displays days/hours/minutes/seconds/milliseconds > left, but am again struggling to progress. > > Any help would be amazingly appreciated :) > > Thanks again > > -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
Re: [Tutor] Need to convert 1,987,087,234,456 to an int
On 16/08/07, Dick Moores <[EMAIL PROTECTED]> wrote: > That's cool! However, it doesn't solve the problem in my original post. > > >>> t = 1,987,087,234,456 >File "", line 1 > t = 1,987,087,234,456 > ^ > SyntaxError: invalid token Hmm, yes. Any integer starting with a 0 is octal, which means that using '8' or '9' is a syntax error. Also, even if you don't get the syntax error, the numbers will come out differently. >>> decomma(1,024) 120 It'll be fixed in Python 3000 :-) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fw: KeyError list?
At my current day job, I'm handed a wild variety of data from customers, and most of it hasn't been normalized in any reasonable way. File formats are inconsistent, numbers of fields are randomly inconsistent with the headers, etc. Attempting to massage these into files I can process has involved a lot of "throw-away" scripting, and I've caught myself doing some really questionable things with basic data structures, leading to my original question. Dictionary key errors have been the most common here lately, popping up in a fairly wide range of circumstances due to seemingly random data. On 8/15/07, wesley chun <[EMAIL PROTECTED]> wrote: > > I guess I couldn't believe it was that simple. ;-) > > now if you had asked about the meanings of *all* exceptions (including > warnings), or perhaps all the SMTP exceptions in smtplib, that would > another matter. ;-) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting Date/Time from a file and using it in calculations
[EMAIL PROTECTED] wrote: > Hello there > > Messing around with certain time and datetime objects, I have managed to > subtract a date/time from the present time thusly: > > from time import * > import datetime > > one = datetime.datetime.now() > two = datetime.datetime(2007, 8, 29, 11, 15, 00) > > difference = one - two > > print difference > > However, I have to take a date from a file and then insert it to where two > should be, however to no success. I have managed to get a string containing > the above date/time, but that is as far as I've gotten without it not > working. > > Does anyone have any ideas on how to do this properly? > > Also I wish to display the result only in seconds left, rather than the > current result, which just displays days/hours/minutes/seconds/milliseconds > left, but am again struggling to progress. Have a look at time.strptime (and time.mktime) -- you may be able to drop datetime entirely. Here is my attempt (not tested): import time time_string = '2007/08/15 22:10:21' one = time.time() # now two = time.mktime(time.strptime(time_string, '%Y/%m/%d %H:%M:%S')) # difference in seconds (float) difference = one - two HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need to convert 1,987,087,234,456 to an int
At 06:58 PM 8/15/2007, John Fouhy wrote: >You could do this: > > >>> def decomma(*t): >... return int(''.join(str(i) for i in t)) What's that asterisk doing in decomma(*t)? Where do I go in the docs to look it up? Thanks, Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need to convert 1,987,087,234,456 to an int
I am a newbie but think its for variable input. Allows you to enter any number with commas in it and still work properly. Since it thinks the number is a tuple, then depending on the number your tuple could vary in length. Eric Walker Dick Moores <[EMAIL PROTECTED]> wrote: At 06:58 PM 8/15/2007, John Fouhy wrote: >You could do this: > > >>> def decomma(*t): >... return int(''.join(str(i) for i in t)) What's that asterisk doing in decomma(*t)? Where do I go in the docs to look it up? Thanks, Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor - Ready for the edge of your seat? Check out tonight's top picks on Yahoo! TV. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need to convert 1,987,087,234,456 to an int
At 06:58 PM 8/15/2007, John Fouhy wrote: >What python are you using? (2.5.1 for me) 2.5. If I install 2.51 will it install itself "over" my 2.5? Or will it set itself up in a separate Python 251 folder/directory? Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need to convert 1,987,087,234,456 to an int
On 16/08/07, Dick Moores <[EMAIL PROTECTED]> wrote: > What's that asterisk doing in decomma(*t)? Where do I go in the docs > to look it up? You can find it in the tutorial: http://docs.python.org/tut/node6.html#SECTION00674 Or see my post here: http://mail.python.org/pipermail/tutor/2007-April/053725.html -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need to convert 1,987,087,234,456 to an int
Dick Moores wrote: > At 06:58 PM 8/15/2007, John Fouhy wrote: > >> You could do this: >> >> > def decomma(*t): > >> ... return int(''.join(str(i) for i in t)) >> > > What's that asterisk doing in decomma(*t)? Where do I go in the docs > to look it up? > > It's the opposite of "def f( *args )" It's easiest to show by example: >>> def f( *args ): ... print args ... >>> f( 1 ) (1,) >>> f( 1, 2, 3 ) (1, 2, 3) >>> f( (1, 2, 3) ) ((1, 2, 3),) >>> f( *(1, 2, 3) ) (1, 2, 3) This is your brain on python. Any questions? :-) > Thanks, > > Dick > > > ___ > 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] Need to convert 1,987,087,234,456 to an int
To Eric Walker, John Fouhy, and Eric Brunson: Thanks very much. I think I'm beginning to get it. At least I was able to write a couple of functions that do what I wanted: = def f(n,m,*args): x = n*m for arg in args: product = arg*x x = product print product numbers = 10, 6, 7 f(3,4,*numbers) # -- def g(*args): n = 0 for arg in args: sum = n + arg n = sum print sum numbers = 10, 6, 7, 5, 100 g(*numbers) They give me 5040 and 128, respectively. Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Book Recommendations [Was:[Re: Security]]
At 05:48 PM 8/14/2007, Kent Johnson wrote: >I also have a shortcut set up so if I type >py modulename >in the Firefox address bar it takes me directly to the docs for that >module. To do this, create a bookmark with this URL: >file://localhost/Users/kent/Library/Documentation/Python-Docs-2.5/lib/module-%s.html > >and give it the keyword 'py'. Great tips, Kent! I especially appreciate this one. Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor