Re: [Tutor] rationale for nested classes?
Bob, I could see where you could use a class inside of a class. Is it possible for you give a simple example of it? Thanks, Tino On Fri, Aug 14, 2009 at 2:05 PM, Serdar Tumgoren wrote: > Okay, those explanations definitely help. I thought I had run into a > situation where nested classes might be called for, but I think > plain-old inheritance is really what I'm after. > > Many thanks! > ___ > 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] rationale for nested classes?
On Fri, 2009-08-14 at 11:26 -0400, Serdar Tumgoren wrote: > Hi everyone, > > I was wondering if there's anyone who can offer a use case/rationale > for nested class? ... > Are there specific situations when nested classes come in handy > (perhaps for grouping conceptually related classes that don't share > attributes?). Disclaimer: I am not a python-ninja and do not have much python code under my belt, but since I thought to what I am about to write when I first read your question, and nobody else gave feedback in this direction, I though to share my idea, if not for anything else, at least to be told on why this might be a non pythonic one :) . I would use a nested class to create a particular data structure which represent a "sub-unit" of the mother class. For example if I had a class representing a table (with methods like add, delete, sort, etc...) I might wish to create a subclass that represent a record and use instantiation of that sub-class as my main way to handle data. In this way I could enforce behaviours like "not null" or "default" from within the datastructure itself (for example in the __init__ method) rather than enforcing a logic from outside (i.e. the "mother class"). I believe classes/objects are also the most elegant way to implement singletons, so maybe if you need a singleton for each object generated with the "mother class", maybe that is also an occasion in which a subclass comes handy. Finally, I somewhere read that embedded declarations are much faster than external ones in being referenced. So, if performance is an issue, maybe embedding one class within another one might bring you some benefit rather than having an object instantiated from the first class having to reference a class which is external to its generating one. [Hope that is understandable... English is not my first language... :)] Anyhow let me reiterate my disclaimer: I am not a python guru, so I would be very happy if any of the tutors would comment on what above, even if it is for disagree with it! :) Mac. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rationale for nested classes?
On Mon, Aug 17, 2009 at 1:12 PM, Mac Ryan wrote: > Finally, I somewhere read that embedded declarations are much faster > than external ones in being referenced. So, if performance is an issue, > maybe embedding one class within another one might bring you some > benefit rather than having an object instantiated from the first class > having to reference a class which is external to its generating one. > [Hope that is understandable... English is not my first language... :)] *Local* variables are faster than variables that must be looked up. Nested classes would not qualify for this. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rationale for nested classes?
> I would use a nested class to create a particular data structure which > represent a "sub-unit" of the mother class. ... In this > way I could enforce behaviours like "not null" or "default" from within > the datastructure itself (for example in the __init__ method) rather > than enforcing a logic from outside (i.e. the "mother class"). This is an interesting idea that I'd like to hear more about and see examples. As part of a current project, this might in fact come in handy. Specifically, would the above approach be a recommended way of performing some additional data integrity checks on an object after having created it? Say that I've created a series of Campaign Committee objects from an initial data set. class Committee(object): def __init__(self, data): self.id = data[0] self.name = data[1] self.candidate = data[2] self.candidateID = data[3] In the above example, I know for certain that the returned data will always have a committee ID. But a number of other data such as the name, candidate, and candidateID are often missing or incorrect. So after the initialiization of an object, I'm using "self.id" in a series of subsequent database queries to correct and/or fill in this data. So my question, in light of Mac's suggestion, is this a case where I should add a subclass containing so-called DataIntegrity checks? For instance, something that resembles this: class Committee(object): <> class DataFixer(self): def fix_name(self): pass def fix_candidate(self):pass def fix_candidateID(self):pass Or would it be better to make this a separate class entirely, outside of the Committee object? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] python and java report writers
Hi, I have been searching for a report writer to work with my python programs. I did find reportlab. But most of the other report writers are java based. I am confused by all the jargon associated with Java and have very little working knowledge of the environment. So I'm hoping someone will help out. I'd like to understand how python can integrate with Java in general and then how it would work with the Java report writers. I have read a little about jPython but do not understand how to make that work with my python programs. It would appear to me it is a way to replace python. I don't want to replace my python code. I want to just call a java report writer. Passing either the data or parameters to retrieve the data and ask it to print a report. I would think that would be straight forward. But I see nothing about python and jasper for example. Therefore, something is unusual in this case. Johnf ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rationale for nested classes?
On Mon, Aug 17, 2009 at 1:36 PM, Serdar Tumgoren wrote: > Say that I've created a series of Campaign Committee objects from an > initial data set. > > class Committee(object): > def __init__(self, data): > self.id = data[0] > self.name = data[1] > self.candidate = data[2] > self.candidateID = data[3] > > In the above example, I know for certain that the returned data will > always have a committee ID. But a number of other data such as the > name, candidate, and candidateID are often missing or incorrect. So > after the initialiization of an object, I'm using "self.id" in a > series of subsequent database queries to correct and/or fill in this > data. > > So my question, in light of Mac's suggestion, is this a case where I > should add a subclass containing so-called DataIntegrity checks? I would just create an ordinary fix() method of Committee, e.g. def fix(self): self.fix_name() self.fix_candidate() self.fix_candidateID() etc. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python and java report writers
On Mon, Aug 17, 2009 at 2:31 PM, John wrote: > Hi, > > I have been searching for a report writer to work with my python programs. I > did find reportlab. But most of the other report writers are java based. I > am confused by all the jargon associated with Java and have very little > working knowledge of the environment. So I'm hoping someone will help out. > > I'd like to understand how python can integrate with Java in general and then > how it would work with the Java report writers. I have read a little about > jPython but do not understand how to make that work with my python programs. > It would appear to me it is a way to replace python. I don't want to replace > my python code. I want to just call a java report writer. Passing either > the data or parameters to retrieve the data and ask it to print a report. I > would think that would be straight forward. But I see nothing about python > and jasper for example. Therefore, something is unusual in this case. Jython (it has not been called jPython for a very long time) is an implementation of Python that is written in Java and interoperates well with Java libraries, both standard libraries and third-party ones. When you use jython, you do write Python code. The main differences are in the library modules available; not all of the Python std lib is available in Jython, and third-party modules that have compiled extensions won't work. Depending on what else your program does, and how big it is already, jython may be a reasonable approach. Another possibility might be calling a report writer from the command line, if that is possible with any of the writers you are interested in. You could also look at JPype though I don't know much about its status. http://jpype.sourceforge.net/index.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python and java report writers
(Sent to John only by mistake, now sending to list. Sorry John!) On Tue, Aug 18, 2009 at 5:48 AM, Oxymoron wrote: > Hi John, > > On Tue, Aug 18, 2009 at 4:31 AM, John wrote: >> >> I'd like to understand how python can integrate with Java in general and then >> how it would work with the Java report writers. I have read a little about >> jPython but do not understand how to make that work with my python programs. >> It would appear to me it is a way to replace python. I don't want to replace >> my python code. I want to just call a java report writer. Passing either > > I can't comment on Python reporting libs - best to google around - > since I have not worked with them. Hopefully someone more experienced > can give alternatives for this. It's definitely worth the effort if > you can use (C)Python natively. > > Since you mentioned Java and Jython however, a few things to consider > on the Java side of the equation (may be a little off topic, I > apologise): > > 1. Jasper is a java library for reporting as you have discovered. It > is meant to be fairly declarative - there should be minimal > boilerplate code or config or wrapper objects for basic cases, the > bulk of the work would be in designing your report -- an XML report > definition file. > > One complication I can think of with Jasper (best to check doco and > ask on the Jasper list for more!), is that it has the idea of > datasources - essentially classes representing data to display on the > report, Jasper provides some default ones - if you need something > specialised, then you will need to write your own (implement an > interface or extend a Jasper base class), you can do this using > Jython. My use case at the time was that I had to much data (from an > RDBMS) to load up into memory at one go - and Jython's default source > implementations did not help, so we had to write our own, again not > too nasty, just not out of the box. Things may have changed. > > 2. Jython is a Python _language_ implementation in Java, and as such > allows calling Java code seamlessly in both directions. To be more > precise, both Java and Jython code are compiled to JVM bytecode, and > share JVM semantics, it is no longer about the language at that point > - just what the runtime understands - needless to say Jython will have > various 'tricks' so that the more dynamic side of Python can fit into > the statically typed JVM model. > > Another way of looking at it is that Jython is an alternative Python > syntax on top of Java libraries (and of course, to write Jython code). > In other words, you can rely on Jython being Pythonic as far as the > language is concerned, but it may not support the standard library (or > only partially support it or is work in progress!) as you see on > CPython docs. It is meant to leverage off the existing Java libraries. > Native python types such as lists are automatically converted to the > JDK standard collection libraries (such java.util.List/Map impls, > etc.) while retaining (obviously) Python syntax and behaviour (as far > as possible) for the core language. > > Essentially if you wish to use a Java library (and I have only ever > used Jasper, so caveat emptor): > > 1. See if there already is a Java wrapper someone has written that you > can call as an external process - reports can be very simple or crazy, > so not sure how feasible this is, especially considering the diversity > of data sources and presentation, or maybe a GUI Jasper (or whatever > else) report designer of sorts that does this, generates config, etc. > - sourceforge may be a good place to search. > > 2. You could call Java libraries using Jython (and thus not have a > syntax barrier at least), and create your reporting process that you > can use as an external process, passing in params, and getting out a > value etc. It will take some effort for sure, because most examples > for using Jasper will be Java-based, and you would need to translate > this to Jython (or copy and paste). Having said that, once you get a > basic test harness up and running, Jasper's complexity will be in > defining reports in XML, nothing very Java specific. > > You _may_ need to be aware of things like Javabean conventions (also > called properties, where tooling considers x.getFoo() and > x.setFoo(bar) to have the shorthand notation to x.foo -- these may > impact the Jython classes you write - method names will need to follow > conventions to be referred to from the XML as properties (it's been a > while since I used Jasper, so do read up again if you go down this > path - can't remember if it was just something we did). Java to Python > translation is straightforward for most cases, just ignore the type > prefix on Java variable declarations (except of course to refer to the > Javadoc!) :-). > > So yes, you cannot call Jython/JVM bytecode from Python or vice versa > as easily as you would your own Python modules, since they are in > different formats, interpreted by different p
Re: [Tutor] python and java report writers
"John" wrote I'd like to understand how python can integrate with Java in general and then how it would work with the Java report writers. I have read a little about jPython but do not understand how to make that work with my python programs. I assume you mean jython? jython allows you to take Python code(with af few restrictions) and compile it into Java so that it can be included in a regukar java program. Jython also allows you to import java classes into your python code as if they were python classes - this is how you would use it. The only snag are those restrictions I mentioned. You need to make sure your python code does not use any features./modules that are not supported in Jython. It would appear to me it is a way to replace python. It doesn't replace Python the language but it lets you write Python code in a Java environment. But you cannot import your Jython code into CPython and you cannot use any C compiled modules in Jython. my python code. I want to just call a java report writer. Passing either the data or parameters to retrieve the data and ask it to print a report. I would think that would be straight forward. But I see nothing about python and jasper for example. Therefore, something is unusual in this case. Provided you can separate the production of the data from the reporting then you can use your C Python code to produce the data then write a Jython module to do the reporting using the Java report writer object. You just have to run the data producer under CPython and the report writer under Jython. (Or compile it to run under Java) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rationale for nested classes?
"Mac Ryan" wrote in message news:1250529165.18338.24.ca...@jabbar... On Fri, 2009-08-14 at 11:26 -0400, Serdar Tumgoren wrote: Hi everyone, I was wondering if there's anyone who can I would use a nested class to create a particular data structure which represent a "sub-unit" of the mother class. For example if I had a class representing a table (with methods like add, delete, sort, etc...) I might wish to create a subclass that represent a record and use instantiation of that sub-class as my main way to handle data. Thats a fair enough idea except there is no advantage in defining the class as a nested class. It would IMHO be better at the module level where it would be easier for a client to subclass it for different record types. It would be very useful to be able to subclass a record in this way so it would not be a candidate for hiding inside another class. OTOH the coupling between table and record makes an equally strong case for having both record and table classes defined in the same module. way I could enforce behaviours like "not null" or "default" from within the datastructure itself (for example in the __init__ method) rather than enforcing a logic from outside (i.e. the "mother class"). But those features are features of the record not of the table. A table is simply a collection of records. Most of the functionality should be in the record, the table should only need to insert,delete, search, sort etc. And those methods should be written to use polymorphism to be record independent I believe classes/objects are also the most elegant way to implement singletons, so maybe if you need a singleton for each object generated with the "mother class", maybe that is also an occasion in which a subclass comes handy. I have no idea what you mean by that bit. Classes can be used for singletons but I don;t see where nested classes come into it. Nested classes should be used where you don't expect anyone outside the outer class to ever want to do anything with the inner class. (In Python this is just a statement of intent since the access is available to anyone who wants it) Finally, I somewhere read that embedded declarations are much faster than external ones in being referenced. Doesn't apply here. would be very happy if any of the tutors would comment on what above, even if it is for disagree with it! :) Be very happy! :-) More seriously, I suspect from your comments that you too are getting confused about the difference between inheritance and nesting. nesting is about hiding the class name and definition. Inheritance is about subclassing. They are very different concepts. Nesting tends to make subclassing more difficult (albeit in Python only trivially so) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python and java report writers
(Posting to list!) On Tue, Aug 18, 2009 at 6:01 AM, John wrote: > > First I love your handle. And second, thanks for taking the time to explain :-) > jython world. But I was really looking for a simple way of calling a report > writer (like using a com object) to print a report. At this point in the > project changing to jython is not going to be considered. We are using to > many modules and I'm sure we will run into something. I wasn't suggesting changing everything to Jython - I actually meant something similar, i.e. write the reporting component in Jython (this really depends on where you get your data from I suppose, if it's all code generated may be more tricky, but for a DB-based thing, not so). You could then treat the jython/java mini-reporting-app as a blackbox. It's not COM perhaps, but plain IPC should do. > It looks like we are stuck with either reportlab or using something like > openoffice's uno. They may be a simpler solution if it amounts to the same thing. -- Kamal -- There is more to life than increasing its speed. -- Mahatma Gandhi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rationale for nested classes?
On Mon, 2009-08-17 at 13:33 -0400, Kent Johnson wrote: > On Mon, Aug 17, 2009 at 1:12 PM, Mac Ryan wrote: > > > Finally, I somewhere read that embedded declarations are much faster > > than external ones in being referenced. So, if performance is an issue, > > maybe embedding one class within another one might bring you some > > benefit rather than having an object instantiated from the first class > > having to reference a class which is external to its generating one. > > [Hope that is understandable... English is not my first language... :)] > > *Local* variables are faster than variables that must be looked up. > Nested classes would not qualify for this. I could not find the article I was referring to, so I ran a test myself, and - surprisingly - I found out the exact contrary of what I read. Nesting the class lower the performance of nearly 10%. FLAT: [4.0505850315093994, 4.0618939399719238, 4.0537300109863281] import timeit class Printer(object): def __init__(self): a = 3 ** 3 class Embedder(object): def __init__(self): for i in range(1): a = Printer() if __name__ == '__main__': timer = timeit.Timer("new = triedout.Embedder()", "import triedout") print timer.repeat(3, 1000) NESTED: [4.3873238563537598, 4.3799960613250732, 4.3729050159454346] import timeit class Embedder(object): class Printer(object): def __init__(self): a = 3 ** 3 def __init__(self): for i in range(1): a = self.Printer() if __name__ == '__main__': timer = timeit.Timer("new = triedout.Embedder()", "import triedout") print timer.repeat(3, 1000) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rationale for nested classes?
On Mon, 2009-08-17 at 20:27 +0100, Alan Gauld wrote: > > way I could enforce behaviours like "not null" or "default" from within > > the datastructure itself (for example in the __init__ method) rather > > than enforcing a logic from outside (i.e. the "mother class"). > > But those features are features of the record not of the table. > A table is simply a collection of records. Most of the functionality > should be in the record, the table should only need to insert,delete, > search, sort etc. And those methods should be written to use > polymorphism to be record independent I suppose one of us is missing the point of the other one, as it looks to me that we are saying the same. :) I would indeed enforce those checks and defaults in the child class (i.e. nested) so that the mother class can have "generic methods" that would work with various records. Indeed - if I truly wished to use nested classes - I would probably define certain module-wide "generic" classes for both "tables" and "records" and then I would subclass them in hierarchic form, so that the class TableA(Table) has a nested class RecordA(Record). I would think of that as a legitimate design with strong encapsulation. Again though... I am not advocating for this method as "the best one", I am just try to imagine a "rationale for nested classes" as per request of the OP. > > I believe classes/objects are also the most elegant way to implement > > singletons, so maybe if you need a singleton for each object generated > > with the "mother class", maybe that is also an occasion in which a > > subclass comes handy. > > I have no idea what you mean by that bit. Classes can be used for > singletons but I don;t see where nested classes come into it. Mmmm... giving a second thought to it, what I had in mind can be achieved with a single class. So apologies, disregard this bit! :) > > would be very happy if any of the tutors would comment on what above, > > even if it is for disagree with it! :) > > Be very happy! :-) > More seriously, I suspect from your comments that you too are > getting confused about the difference between inheritance and nesting. > nesting is about hiding the class name and definition. Inheritance is > about subclassing. They are very different concepts. Nesting tends > to make subclassing more difficult (albeit in Python only trivially so) Well, thank you for the feedback indeed. I am one of those people who get their mind clear only when they interact with others. As for the confusion between nesting and inheritance... no, I think I have that one pretty well under my belt. :) Thank you again, Mac. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python and java report writers
On Monday 17 August 2009 01:08:15 pm Oxymoron wrote: > (Posting to list!) > > On Tue, Aug 18, 2009 at 6:01 AM, John wrote: > > First I love your handle. And second, thanks for taking the time to > > explain > > > :-) > : > > jython world. But I was really looking for a simple way of calling a > > report writer (like using a com object) to print a report. At this point > > in the project changing to jython is not going to be considered. We are > > using to many modules and I'm sure we will run into something. > > I wasn't suggesting changing everything to Jython - I actually meant > something similar, i.e. write the reporting component in Jython (this > really depends on where you get your data from I suppose, if it's all > code generated may be more tricky, but for a DB-based thing, not so). > You could then treat the jython/java mini-reporting-app as a blackbox. > It's not COM perhaps, but plain IPC should do. > > > It looks like we are stuck with either reportlab or using something like > > openoffice's uno. > > They may be a simpler solution if it amounts to the same thing. > > -- Kamal I don't know enough about jython to understand what has to be done. Let's say I can write the code to retrieve the data in cPython. And I can write a module in jython that will accept the data passing it on to the report writer. How can I call the jython program? It can't be called from the cPython program - right? So the jython runs stand alone - right? IPC = Inter-process communication - right? Does that mean I need to write some sort of server? Johnf ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problems with encoding in BeautifulSoup
Hello, I have this sample script from beautiful soup, but I keep getting an error because of encoding. I have google for solutions but I don't seem to understand. Even this is dealt in Beautiful Soup's doc but I am not able to understant/apply the solution successfully. from BeautifulSoup import BeautifulSoup import urllib2 page = urllib2.urlopen('http://www.yellowpages.ca/search/si/1/Signs/QC') # if I change the url to http://www.yellowpages.ca/search/si/3/Signs/ON, it works because there's no french words... soup = BeautifulSoup(page) companies = soup('h2') print soup.originalEncoding print companies[:4] However, if I do this, I don't get errors even when there are accents: for title in companies: print title Here is the Error output: utf-8 Traceback (most recent call last): File "C:\myscripts\encondingproblem.py", line 13, in print companies[:4] UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 373: ordinal not in range(128) === Thanks in advance. Eduardo www.expresssignproducts.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python and java report writers
On Tue, Aug 18, 2009 at 8:50 AM, John wrote: > I don't know enough about jython to understand what has to be done. Let's say > I can write the code to retrieve the data in cPython. And I can write a > module in jython that will accept the data passing it on to the report > writer. How can I call the jython program? It can't be called from the > cPython program - right? So the jython runs stand alone - right? Yes Jython runs standalone (via the Java interpreter) in this scenario. If you supply the data, chances are you need to either look if there's a Datasource implementation readily available for the data format you intend to supply - CSV, XML, etc. - or you would need to create one (on the Jython side), essentially it reads in your datafile/stream and passes it to the reporting engine appropriately. > IPC = Inter-process communication - right? Does that mean I need to write > some sort of server? Yep I meant interprocess communication. Using a server is one way of doing it, but probably a bit much for simple requirements. Look at the subprocess module and exec family (there have been a few posts regarding these - I personally haven't used them as such so will only refer you to docs and more able posts!), essentially you just need to invoke the jython process, hook up the stdin and stdout between the processes as necessary, e.g. let the jython program take command line params for any operation modifiers/options if you need it to, and read in your data via stdin. This way there's no server, just a simple protocol that the reader process reads from stdin, and an end of file indicates that there's no more data to read, and it can go and do it's stuff. The jython process should also eventually do a System.exit(0) for success, or System.exit(1) for failure (java.lang.System) for easy error handling by CPython; you need to decide how to handle the report output: have jython write it to a file in some predefined location, or send it back your CPython invoker (which makes the IPC a bit more involved). Just depends on your needs, and the time you have available for the task! Regards Kamal -- There is more to life than increasing its speed. -- Mahatma Gandhi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python and java report writers
Forwarding to the list with my reply. On Mon, Aug 17, 2009 at 4:04 PM, John wrote: > Thanks for taking the time to write. But I was really looking for a simple > way of calling a report writer (like using a com object) to print a report. > At this point in the project changing to jython is not going to be > considered. We are using to many modules and I'm sure we will run into > something. >From a quick look at the docs for Jasper Reports I see that it can take data from a CSV file. Perhaps you could make a Java / Jython app that generates your report from a file, then your Python program could write the CSV file and call the external program. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python and java report writers
On Monday 17 August 2009 05:43:05 pm Kent Johnson wrote: > Forwarding to the list with my reply. > > On Mon, Aug 17, 2009 at 4:04 PM, John wrote: > > Thanks for taking the time to write. But I was really looking for a > > simple way of calling a report writer (like using a com object) to print > > a report. At this point in the project changing to jython is not going to > > be considered. We are using to many modules and I'm sure we will run > > into something. > > > > > >From a quick look at the docs for Jasper Reports I see that it can take > > data > > from a CSV file. Perhaps you could make a Java / Jython app that generates > your report from a file, then your Python program could write the CSV file > and call the external program. > > Kent Thanks folks I think I'll have to start playing with jython and see if I can get it to first create a report/ run a report. Then I'll see if I can get it working with python. Again thanks to all. Johnf ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to format JSON output within script
I see you could pipe your output to 'python -mjson.tool', but how do I achieve the same within my script? TIA. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to format JSON output within script
"Vincent Gulinao" wrote I see you could pipe your output to 'python -mjson.tool', but how do I achieve the same within my script? We don't charge you by the word. A wee bit more explanation and background would be useful please? What are you trying to do exactly? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to format JSON output within script
On Tue, Aug 18, 2009 at 1:02 PM, Vincent Gulinao wrote: > I see you could pipe your output to 'python -mjson.tool', but how do I > achieve the same within my script? > > TIA. > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > There is a JSON module in Python...the examples in the docs are pretty self explanatory: http://docs.python.org/library/json.html Can you give a bit of an example of what you're trying to do? -Mal W ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to format JSON output within script
On Mon, Aug 17, 2009 at 11:02 PM, Vincent Gulinao wrote: > I see you could pipe your output to 'python -mjson.tool', but how do I > achieve the same within my script? json.tool is a pretty simple wrapper around json.load() and json.dump(). You can look at the source code in your Python lib directory. (Lib/json/tool.py) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to format JSON output within script
Got it, what I need was json.dumps. Sorry I posted too early. Thanks. On Tue, Aug 18, 2009 at 11:50 AM, Kent Johnson wrote: > On Mon, Aug 17, 2009 at 11:02 PM, Vincent > Gulinao wrote: >> I see you could pipe your output to 'python -mjson.tool', but how do I >> achieve the same within my script? > > json.tool is a pretty simple wrapper around json.load() and > json.dump(). You can look at the source code in your Python lib > directory. (Lib/json/tool.py) > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems with encoding in BeautifulSoup
On Tue, Aug 18, 2009 at 9:00 AM, Eduardo Vieira wrote: > Hello, I have this sample script from beautiful soup, but I keep > getting an error because of encoding. I have google for solutions but > I don't seem to understand. Even this is dealt in Beautiful Soup's doc > but I am not able to understant/apply the solution successfully. > > from BeautifulSoup import BeautifulSoup > import urllib2 > page = urllib2.urlopen('http://www.yellowpages.ca/search/si/1/Signs/QC') > > # if I change the url to > http://www.yellowpages.ca/search/si/3/Signs/ON, it works because > there's no french words... > > soup = BeautifulSoup(page) > > companies = soup('h2') > > print soup.originalEncoding > > print companies[:4] > > However, if I do this, I don't get errors even when there are accents: > for title in companies: > print title > > Here is the Error output: > utf-8 > Traceback (most recent call last): > File "C:\myscripts\encondingproblem.py", line 13, in > print companies[:4] > UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in > position 373: ordinal not in range(128) > > === > Thanks in advance. > > Eduardo > www.expresssignproducts.com > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > It's caused by Python not wanting to send non-ASCII characters to your terminal. To override this you need to create a sitecustomize.py file in your /usr/lib/python/ folder and put the following in it: import sys sys.setdefaultencoding("utf-8") This will set the default encoding in Python to UTF8 and you should stop getting these parsing errors. I dealt with this recently when I was playing around with some international data. -Mal W ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Web framework: looking for python-tutor's angle.
On Fri, Aug 14, 2009 at 4:09 AM, Mac Ryan wrote: > A couple of months ago I took the time to read a few articles on python > web application frameworks and I got the impression that the two most > mature and active projects are Zope and Django. > > Zope vs. Django hits 879.000 pages on google but much of the debate - or > at least this is my impression - falls into the class "vi vs. emacs" or > "gtk vs. qt" with many people singling out a single characteristics that > for them is THE characteristic making one framework better than the > other. > > This [1] graph seems to corroborate my final impression (i.e. that > django is the way to go). Yet, I would be very interested in hearing > what the members of this list think, as I particularly enjoy the > "learner centered" approach that most of the people seems to have here. > > I believe my needs are quite ordinary: my customers are typically small > businesses needing to process their data on a single server, sometime > exposing part of the application as front-end to the customers (hence > easy and flexible theming is important). I would definitively be happy > to sacrifice some functionality in exchange for a leaner and cleaner > design (i.e. more modular, elegant and intuitive), though. > > Thank you in advance for your time, > Mac. > > [1] http://www.google.com/trends?q=python+zope%2C+python+django > > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > Your needs definitely favour Django. The learning curve is so much shallower for Django and it's very simple to theme not to mention a much more beginner-tolerant community IMHO. -Mal W ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor