Python

Coders forum. Discuss techniques, swap code snippets, ask for help etc. relating to all forms of software development.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Python

Post by Kamineko »

I'm writing a simple comic book data thing using the Comic Vine API! It doesn't have much in the way of documentation or examples, just a big splat of structures that the API will return if you can figure out how to invoke it.

The Python side is easy as pants though. urllib.request and xml.etree.ElementTree are the cheatmodes as usual.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Python

Post by Kamineko »

Woo! I now have a nice Qt program that can search the Comic Vine database for all comic volumes matching a query, download all the issue data for all comics within the volume, with release date and story arc data, and provide homogeneous filename suggestions based on its findings.

One Python idiom I'm getting a lot of use out of is the following:

Code: Select all

def stuff(parameters):
    ...

    result = None

    try:
        result = function_may_fail(a, b, c, d)
    except IOError etc:
        pass

    if result:
        process result object somehow
        return some success object
    else:
        error stuff

(alternatively)
    if not result:
        abort returning appropriate failure number/object

    good path
It seems to be the nicest way to detect problems where a function might return a useful value on success, return None on success but nothing was found, or throw an exception on misuse/file error/network error. Stuff like network connecting, file writing, dict inspection (especially with long paths on internal structure you know to there if the state is good).

I'm not particularly interested in what went wrong (or passing it up to the user interface), but I do want to handle problems to the extent where I can maintain good state. I'm unsure because it resembles amateurish Java 'I don't know how to deal with this exception so I'll just suppress it' coding.

Does anybody else use try-attempt-except-pass much?

I kind of miss Java's checked exceptions and javadoc. I can't find what kind of exception xml.etree.ElementTree.fromstring() might throw if I chuck it malformed XML.

Edit - Oh wait Python 3's docs mention it, Python 2's docs didn't.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Python

Post by Kamineko »

If anybody is into collecting digital comics, my comic renamer program is up at https://gitorious.org/comicrenamer-py

Probably more interesting as a tool to help folks learn Qt than do renaming though. :)
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Python

Post by Kamineko »

I wanted to know more about networky stuff, so I wrote myself a cheesy IRC client in Python.

Well, to be more specific, I just opened a socket to an ircd running on my laptop, and wired up some input and output text widgets to shove utf-8 chars to and from it. As long as you don't mind manually typing every part of the IRC protocol yourself, it's a fully functional (and compliant ;)) IRC client.
adam
Baaaaaa!
Baaaaaa!
Posts: 4418
Joined: Sat Jun 24, 2006 9:15 pm 0

Re: Python

Post by adam »

there are probably many python irc clients already, and libraries and that
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Python

Post by Kamineko »

No good, I doubt any of them are as bad as mine.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Python

Post by Kamineko »

Woohoo! My python IRC thing is a go.

I got stuck for a few hours trying to make a dummy invisible Tkinter widget which would act as an intermediary between my IRC networking thread and the GUI mainloop thread. I wanted the IRC thread to be able to add items to a fifo queue of sanitized messages to handle and then send a 'notify' event that the GUI mainloop would respond to, eat up the messages and put 'em on screen/change GUI state.

You can't generate or respond to events that haven't been assigned geometry it seems. If I made a new Frame subclass with the main form as the master and tried to generate <<IRC>> events on it, nothing would happen. If I put the widget onto the form then it would work but that wasn't the model I wanted. Instead, I decided to put that responsibility onto the window itself so there are no extra invisible widgets sticking out.
User avatar
gravy
Sheepie Whore
Sheepie Whore
Posts: 19572
Joined: Wed May 01, 2002 1:28 pm 0
Location: The Other Place

Re: Python

Post by gravy »

Just started looking at iPython and iPython Notebook

Early days but it looks kind of amazing
User avatar
sdetoni
Baaaaa!
Baaaaa!
Posts: 1368
Joined: Tue Jun 18, 2002 4:14 am 0
Location: New Sheepland

Re: Python

Post by sdetoni »

I've pretty much finishing up a web based reporting service I wrote in Python and JQuery for work. It isn't large, but has couple of dynamic report selection controls using ajax which was easy enough to implement, and then styled using CSS, though my styles tend to not to be the best, but it looks mostly the part and uses the following features:

* Easy config file, check, 40 lines of code!
* Full rollover Logging, check, 10 lines of code!
* Custom built web server with external/dynamic page loading, check, 300 lines of code!
* SQLite database build, select, update queries, check, 200 lines of code!
* Email HTML generation, check, 30 lines of code!
* Custom authentication, check, 50 lines of code!
* Other web pages + css + jquery 1000 lines of code.

A lot of the above is with the help of built in libraries, but its also down to the concise syntax and dynamic data structures that just get the job done. Dynamic lists, and dictionary lists make indexing data a breeze.

I think I'll be making Python my language of choice.
It use to be Java, but that has kind of gone the way of C++. Its just become bloatware with additional libs and syntax for Africa that can make it an unweilding mess. Also, the extra compile/link step and strong typing is starting to get in the way of 'just let me code!'.
User avatar
lazerwalker
Baaaaa!
Baaaaa!
Posts: 1319
Joined: Wed Sep 03, 2003 10:22 pm 0
Location: (Very) sarf of the river

Re: Python

Post by lazerwalker »

gravy wrote:Just started looking at iPython and iPython Notebook

Early days but it looks kind of amazing
iPython & the notebook are really good for maths & simulation. I'm in the process of slowly switching from Matlab/Octave for doing my calculations to Python & Numpy.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Python

Post by Kamineko »

Realised that my IRC client in its current state is in effect a Python scriptable IRC chanbot. I went mad and wrote up a quick draughts AI routine and some REST-like interface overnight and now have a bot you can play draughts with over IRC.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Python

Post by Kamineko »

Here's my client, fwiw. Not much to it!

It plays a mean game of checkers. Well, a game of checkers at least.

https://gitorious.org/python_irc/python_irc/
Post Reply