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:

Python

Post by Kamineko »

I decided to learn Python last week. That's some real nifty stuff right there. Every programmer worth their salt should have a go of Python at some point. I wanted to make a GUI application to learn how the language is used, with Qt for the interface and zipped JSON for files and Python replied 'Good idea, DONE.', and I said 'But... I just started. I can't be DONE', and Python said 'Nuh-uh. It's all done. You finished it. Go you!'. My tea hadn't even gotten cold.

What's going on?

I don't think it should be used as a teaching language though; it's too easy. It's the Super Nashwan Power of programming, except it doesn't wear off. It's cheating, and kids should be taught that programming is boring, brittle, longwinded and mostly not doing the algorithm you're trying to express, damn it.
User avatar
gilesgoat
Sheepie Whore
Sheepie Whore
Posts: 27259
Joined: Wed Sep 03, 2003 7:54 am 0
Location: Monfalcone, Italy
Contact:

Re: Python

Post by gilesgoat »

I'd rather get one of those :wink:

Image
Giles the billy goat
"Baa"
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 »

Dynamic languages with some sort of online library repository can be ace if you're up for trying new things. And hugely productive too.

See also Ruby, Javascript / Node and a bunch of others.
User avatar
sack
Baaaaaaa!
Baaaaaaa!
Posts: 7077
Joined: Fri May 10, 2002 4:13 pm 0
XBL Gamertag: evilgopher
PSN: sixFiveOhTwo
Location: Rijswijk, The Netherlands
Contact:

Re: Python

Post by sack »

never liked python for GUI stuff (never found out what the standard way of doing it was), but I played with pygame a little bit and that seemed fun.

It's also great for those data wrangling jobs you can't be arsed to do manually and works well across mac & PC (almost certainly Linux too, but I haven't done any of that myself). Between that and the fact you can play with it live from the commandline it feels like BASIC for the modern age.

The indentation put me off for a while (you can break it just by shifting between editors if you aren't careful), but once I got over it and stopped doing that I started to get into it.
User avatar
Darkflame
Sheepie Whore
Sheepie Whore
Posts: 18236
Joined: Mon Mar 31, 2003 11:06 am 0
Location: Not the UK

Re: Python

Post by Darkflame »

gilesgoat wrote:I'd rather get one of those :wink:

Image
Raise you;
Image
User avatar
randomdude
Baaaaaa!
Baaaaaa!
Posts: 4609
Joined: Sat Aug 02, 2003 3:54 am 0

Re: Python

Post by randomdude »

I am finally starting to love Python, after quite some time of hating the very idea of it.

I've started to use it as a replacement for overly-complex bash scripts, or very-simple C programs - I find it is best used as a more-powerful scripting language.

I wrote a script using a curses-based interface with some library a few weeks ago - it was bordering on painful but not really that bad. Easier than doing the same thing in C. I haven't ventured in to proper GUI yet.
I am both the king and the queen of cheese.
<Wife_of_Snake> Random the sugar rush fairy!
<}stinky_beast{> I'd do a zebra if it'd stand still
User avatar
gilesgoat
Sheepie Whore
Sheepie Whore
Posts: 27259
Joined: Wed Sep 03, 2003 7:54 am 0
Location: Monfalcone, Italy
Contact:

Re: Python

Post by gilesgoat »

If I am not wrong there's even a language called Anaconda ?
Giles the billy goat
"Baa"
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Python

Post by Kamineko »

gilesgoat wrote:I'd rather get one of those :wink:
Agh snake, agh snake! Oooooooooooooh... it's a snaaaaake!

My Qt Python stuff is going super great. I keep changing the language between Python 2 and 3 to deliberately hit all the odd cases that distinguish the two (mostly iteritems on dicts, xrange, strs and unicodes). If I can train myself to typically write in a forwards compatible Python 2/backwards compatible Python 3 style I'll be happy.

I'm using PySide (the LGPL Nokia Python binding for Qt), which is super neato. It's as if Qt and Python were specifically designed for one another. I switched over to PyQt (the other GPL-only binding) and had a go of that too. They interpret the C++ Qt API as seen in the docs slightly differently, and PyQt doesn't hide away the Qt types, so you'll get QStrings coming out of GUI elements instead of unicodes or strs. Apart from that, if you use the same import statement, all the Qt widget types have the same name so the code mostly doesn't change. Which was nice.

I'm now porting my program from Qt to tkinter for the hell of it! Clearly what's needed is some kind of abstraction layer to combine all of these cross platform GUI toolkits into one.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Python

Post by Kamineko »

In Python 2, a file seems to be a like a stringy kind of object, when loaded as 'r', but in Python 3, a file seems to be a bytes-y kind of object, even when loaded as 'r'.

The effect of this is that you can't JSON load from a file object directly any more? Kind of strange.

My Python 2 code:

Reading:

Code: Select all

        with zipfile.ZipFile(unicode(filename), 'r') as zf:
            with zf.open(self.json_saved_data_filename, 'r') as df:
                jsonread = json.load(df)
Writing:

Code: Select all

        with zipfile.ZipFile(unicode(filename), "w", compression=zipfile.ZIP_DEFLATED) as zf:
            dumpobject = {}
            
            dumpobject['curr_q_no'] = self.quizstate.curr_q_no
            dumpobject['items'] = self.itemslist
            dumpobject['questionorder'] = self.quizstate.questionorder
            dumpobject['answers'] = self.quizstate.prevanswers
            dumpobject['quizpoints'] = self.quizstate.quizpoints 
           
            zf.writestr(self.json_saved_data_filename, json.dumps(dumpobject, separators=(',', ':')))
Becomes in Python 3:
Reading:

Code: Select all

        with zipfile.ZipFile(filename, 'r') as zf:
            with zf.open(self.json_saved_data_filename, 'r') as df:                
                jsonread = json.loads(df.read().decode())
Writing:

Code: Select all

        with zipfile.ZipFile(filename, "w", compression=zipfile.ZIP_DEFLATED) as zf:
            dumpobject = {}
            
            dumpobject['curr_q_no'] = self.quizstate.curr_q_no
            dumpobject['items'] = self.itemslist
            dumpobject['questionorder'] = self.quizstate.questionorder
            dumpobject['answers'] = self.quizstate.prevanswers
            dumpobject['quizpoints'] = self.quizstate.quizpoints 
           
            zf.writestr(self.json_saved_data_filename, json.dumps(dumpobject, separators=(',', ':'), ensure_ascii=True))
I'd have thought that Python 3 would be the one that would smartly detect the encoding of the opened text-mode file and shuffle into its str-which-is-always-unicode type form. :s
User avatar
sack
Baaaaaaa!
Baaaaaaa!
Posts: 7077
Joined: Fri May 10, 2002 4:13 pm 0
XBL Gamertag: evilgopher
PSN: sixFiveOhTwo
Location: Rijswijk, The Netherlands
Contact:

Re: Python

Post by sack »

I used to drive directX succesfully via python and some native bindings - worked quite nicely for what I was doing, but I don't know if it'd scale to an entire game
User avatar
evilpaul
Baaaa!
Baaaa!
Posts: 811
Joined: Wed Sep 29, 2004 2:01 pm 0
Location: #ukscene, helsinki branch
Contact:

Re: Python

Post by evilpaul »

Python is the new Perl. We all love it now but in a couple of years time we'll all look back at our code, wonder how the fuck it works and then vow never to use Python again (but we still will)
User avatar
Kelloggs
Baaaaa!
Baaaaa!
Posts: 1859
Joined: Wed Jun 22, 2005 11:38 pm 0
XBL Gamertag: TheAntiKelloggs
Location: Widnes or Altrincham. Take your pick.

Re: Python

Post by Kelloggs »

I actually find Python quite easy to read (once I'd realized that loops just... end, without having an "End" command, or bracket, or anything) so I'm not too worried about it going The Perl Way. The bigger problem I find is that (I suppose a bit like Perl one-liners) because of all the stuff you get for free, it sometimes becomes too tempting to chain iterator after iterator and Behold, It Works! (and, of course, when it Stops Working, you're Stuffed)

I had an issue with Python a few years ago (I program in C for applications, Perl for data wrangling, and shell scripting for getting everything to work together - and none of this professionally) whereby one of our programmers had written a monstrosity of a Python program, that ran perfectly well on his laptop in 45 minutes, but blew up badly when run on our new servers, on Friday afternoon, with said servers (14) needing this program running correctly on them on Monday morning. So, I spent the weekend with my head up the southbound end of a northbound Python (error was EAGAIN on a non-blocking file read, and the program's exception handling wasn't quite up to it - but that took a hell of a lot of digging to find) and when I got in on Monday morning, and was asked how the weekend went, I pointed out that I'd fixed the problem, and it now worked fine (runtime now 7 minutes, by the way - hence, the EAGAIN errors, because the new servers ran it much faster) but it was just a shame I'd needed to learn a new language to do so!
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Python

Post by Kamineko »

Is that a Python problem though? If you were a super C++ STL buff, you could have a lot of iterators on containers (and a lot of colons and brackets) and have the same problem.
User avatar
Kelloggs
Baaaaa!
Baaaaa!
Posts: 1859
Joined: Wed Jun 22, 2005 11:38 pm 0
XBL Gamertag: TheAntiKelloggs
Location: Widnes or Altrincham. Take your pick.

Re: Python

Post by Kelloggs »

Not specifically a Python problem, but I find in any situation where things are trying to be "helpful", it's occasionally a little to easy to let them do it - whereas a language needing more handholding might have code containing more hand holds.

I think it strikes me especially in Python because of the prevalence of what I think of as "stealth loops" (iterators) with nowhere to stick the equivalent of "echo $i" - and no indication that you could, even if you wanted to - so you could see how far things got before failing. The EAGAIN problem was an example of this - an inwards pipe from another command, that happened to take a while to get going, but with nothing around it (that I could see) to show where the particular problem actually was... just "churn churn churn churn The Magic Happens churn churn bomb" (and don't forget, I'd not really had cause to look at Python prior to that).

I think I'm just used to languages, like C, where if it's not explicitly coded, it doesn't happen. Mostly the reason I don't do C++ if I can avoid it, even if I do think classes are a good idea - I think I might be happier writing assembly, if x86 hadn't taken over the world (give me a 68K any day).
User avatar
Dio
Sheepie Whore
Sheepie Whore
Posts: 18887
Joined: Mon May 31, 2004 1:08 pm 0
Location: Newcastle UK

Re: Python

Post by Dio »

Odd because those semi-orthogonal CISC asms are exactly the same in the end. I loved 68k and railed against x86 for a long time but now I can't quite understand why because there's actually no difference of note between the two.
Post Reply