Open Side Menu Go to the Top
Register
** Python Support Thread ** ** Python Support Thread **

04-05-2011 , 10:49 AM
Quote:
Originally Posted by FreeBird!
Starting to pick up programming again; my only experience was in QBASIC as a pre-teen back in the 90s. Decided on a whim yesterday to try to learn Python.

Anyway, Is there an easy (I'll settle for hard) way to display a variable using tkinter and have the display update as the variable changes?

I started off using a label, but it didn't update dynamically, and the solutions I've found online so far seem to go about solving it in a very roundabout way.

TIA for any help!
That would be easy to do with perl + Tk (using tie), but I can't think of a way to do it automatically in python. One way would be to set up a timeout that checks the variable and updates the label. (I'm not sure its called timeout in Tk, but it is a Tk function that wakes up every few msec and runs a function. after() or after_idle() look like a way to do this.)
** Python Support Thread ** Quote
04-07-2011 , 03:16 AM
Thanks for the help.

Would it be worth it to start with perl (or a different programming language) instead of learning python if one eventual goal (fully realizing I have a steep learning curve ahead) is to create an application that interacts with a SQL database and also connects to a flash client and a 'downloadable' client?
** Python Support Thread ** Quote
04-07-2011 , 10:54 AM
Quote:
Originally Posted by FreeBird!
Thanks for the help.

Would it be worth it to start with perl (or a different programming language) instead of learning python if one eventual goal (fully realizing I have a steep learning curve ahead) is to create an application that interacts with a SQL database and also connects to a flash client and a 'downloadable' client?
(grunch)

I would say no. Perl is a lot more cryptic, python is a pretty easy language to pick up and learn, and there's at least as many (probably more) libraries out there for Python that do exactly what you'll need to.
** Python Support Thread ** Quote
04-07-2011 , 03:02 PM
Quote:
Originally Posted by Johnny Douglas
(grunch)

I would say no. Perl is a lot more cryptic, python is a pretty easy language to pick up and learn, and there's at least as many (probably more) libraries out there for Python that do exactly what you'll need to.
Python is much easier to learn than perl. I think that using python (+ python std library) is better than using perl (+ CPAN) because the python std lib works anywhere python does and you don't need to worry about whether some users will have it. With perl +CPAN, some modules won't work on some platforms and few (or no) CPAN modules can be assumed to be on your customers' systems.
** Python Support Thread ** Quote
04-07-2011 , 06:16 PM
+PyPi is basically a CPAN for Python. I like Perl but it's rather chaotic (which is also the strong suit of the language)

http://pypi.python.org/pypi
** Python Support Thread ** Quote
04-30-2011 , 05:33 PM
OK, I'm firmly against using ****ing tabs as meaningful characters. Maybe it's fine once all the editors you are going to use are all set up just so but moving text files between OS's and computers is just too much of a pain in the ass over using line and block endings.
** Python Support Thread ** Quote
04-30-2011 , 08:52 PM
Quote:
Originally Posted by FreeBird!
Anyway, Is there an easy (I'll settle for hard) way to display a variable using tkinter and have the display update as the variable changes?

I started off using a label, but it didn't update dynamically, and the solutions I've found online so far seem to go about solving it in a very roundabout way.
TIA for any help!
Binding your View to your Control/Model and keeping them in sync is one of the biggest pains of GUI programming. I recommend moving away from Tk and using something like QT's QML bindings from PySide:

http://developer.qt.nokia.com/wiki/PySideDocumentation/
http://doc.qt.nokia.com/4.7/qdeclara...roduction.html
** Python Support Thread ** Quote
04-30-2011 , 08:57 PM
Quote:
Originally Posted by kerowo
OK, I'm firmly against using ****ing tabs as meaningful characters. Maybe it's fine once all the editors you are going to use are all set up just so but moving text files between OS's and computers is just too much of a pain in the ass over using line and block endings.
I've had this discussion with many many people over the years. Last time I couldn't quite articulate why I prefer spaces over tabs. I think it's just some deep-seated hatred for characters that I can't see

If you're not setting the standard for the code you're sharing then I recommend setting the 'show invisible characters' option in whatever editor you're using... it will prevent a lot of headaches, especially in a language with significant whitespace!
** Python Support Thread ** Quote
04-30-2011 , 09:05 PM
are you really meant to use exceptions for program flow? I really like Python except that it seems to encourage constructs like

Code:
try:
    v = dic[keyVal]
except(KeyError):
    v = -1
I know there's a .get() function for this specific situation, but I mean in general, is this encouraged, in a situation where an exception is quite likely to be thrown? e.g. Dive Into Python suggests using exceptions for figuring out if a given external module is available. Is this really OK? Is it "unpythonic" to say stuff like "you shouldn't use exceptions here because the compiler optimizes for the situation where no exception is thrown"? Or "you shouldn't use exceptions here because when I read your code, I expect that an exception only occurs in an unusual situation"? Or "exceptions are basically syntactic sugar for GOTO, which is ugly, so you shouldn't use them if at all possible"?

I don't think I would even ask this question if I was sober, but even tomorrow morning I will be interested in your feedback.
** Python Support Thread ** Quote
05-01-2011 , 10:04 AM
Quote:
Originally Posted by kerowo
OK, I'm firmly against using ****ing tabs as meaningful characters. Maybe it's fine once all the editors you are going to use are all set up just so but moving text files between OS's and computers is just too much of a pain in the ass over using line and block endings.
Just set your edior to convert tabs to spaces. Who uses tabs anyways, I use space-space for identation.

Why should I write starting/end blocks when I indent either way. I think Python code is very readable and the indentation as delimiter may seem odd but it's one of the neatest things.

I guess I'm with you that only spaces should be used though, no clue why tabs were allowed.
** Python Support Thread ** Quote
05-01-2011 , 10:21 AM
Smacks like a step backwards to COBOL and it's zones and having to start lines in particular columns. I never found braces or semi colons so hard to use. Especially when you have nested conditions and try to line up statements on both sides of the block by eye, was that 20 spaces or 24? I like Python but this feature I could do without.
** Python Support Thread ** Quote
05-01-2011 , 11:41 AM
Quote:
Originally Posted by kerowo
Smacks like a step backwards to COBOL and it's zones and having to start lines in particular columns. I never found braces or semi colons so hard to use. Especially when you have nested conditions and try to line up statements on both sides of the block by eye, was that 20 spaces or 24? I like Python but this feature I could do without.
For what it's worth, I've done a decent amount of Python programming and I've never had a problem with this. Use a decent editor that will autoindent and indent n spaces when you hit tab, and this is almost never an issue. (E.g. in vim: set expandtab tabstop=4 shiftwidth=4)
** Python Support Thread ** Quote
05-01-2011 , 01:38 PM
You also don't need to use a set number of spaces, you just need to be consistent for the block. If you like two spaces, that will work just fine.

Technically, this is valid. It's just a bad idea because your code should be consistent:
Code:
x = 0
if (x == 0):
 for i in xrange(10):
         with open("file.txt") as f:
           for line in f:
                                   print line
As far as exceptions, I save them for the more complicated things. This is fine.
Code:
dic = {}
if keYval in dic:
    v = dic[keyVal]
else:
    v = -1
Some people use the exception way because it doesn't require 2 hash lookups. But I am in favor of readability until you know that extra operation is slowing you down.
** Python Support Thread ** Quote
05-01-2011 , 08:28 PM
Quote:
Originally Posted by kerowo
Smacks like a step backwards to COBOL and it's zones and having to start lines in particular columns. I never found braces or semi colons so hard to use. Especially when you have nested conditions and try to line up statements on both sides of the block by eye, was that 20 spaces or 24? I like Python but this feature I could do without.
If it was in my power I'd make Java throw compile errors on hitting a tab character. There's nothing worse than having a code's format be dependent on the developer's editor that is looking at it.

I've now written a significant amount of Production Python code and I can think of only one method where I had a 6 level indent in my method (requiring 24 spaces). I might have written a few others that I can't think of - but its certainly very rare.

I've found a number of things I don't like about Python - but the spaces as syntax isn't one of them. It's also never been a complaint I've heard from regular Python programmers but is something I frequently hear from people that haven't programmed in Python very muhc.
** Python Support Thread ** Quote
05-01-2011 , 11:17 PM
Quote:
Originally Posted by jjshabado
It's also never been a complaint I've heard from regular Python programmers but is something I frequently hear from people that haven't programmed in Python very muhc.
Got it in one...
** Python Support Thread ** Quote
05-01-2011 , 11:19 PM
I don't see the real problem with tabs vs spaces. Every single editor/IDE worth using has an ability to set the tab size. By using spaces specifically you're imposing your rules on everyone else unless their editor/IDE also has an ability to convert leading spaces to tabs in an intelligent way.

There's absolutely no down side in using tabs.
** Python Support Thread ** Quote
05-02-2011 , 12:13 AM
Quote:
Originally Posted by Shoe Lace
I don't see the real problem with tabs vs spaces. Every single editor/IDE worth using has an ability to set the tab size. By using spaces specifically you're imposing your rules on everyone else unless their editor/IDE also has an ability to convert leading spaces to tabs in an intelligent way.

There's absolutely no down side in using tabs.
Writing code is about communication - and formatting is part of that communication. Since I work with sensible developers I want to see the code how they wrote it.

In a perfect world with perfect developers - it might actually be possible that tabs could be better than spaces. But people aren't perfect and using exclusively tabs is hard to do (and impossible to enforce or automatically correct) and soon you have a mess of spaces and tabs that just doesn't work with multiple developers.
** Python Support Thread ** Quote
05-02-2011 , 01:18 PM
Quote:
Originally Posted by jjshabado
Writing code is about communication - and formatting is part of that communication. Since I work with sensible developers I want to see the code how they wrote it.

In a perfect world with perfect developers - it might actually be possible that tabs could be better than spaces. But people aren't perfect and using exclusively tabs is hard to do (and impossible to enforce or automatically correct) and soon you have a mess of spaces and tabs that just doesn't work with multiple developers.
I don't see a sensible developer ever using both spaces and tabs. You need to almost be on drugs to accidentally use both unless you're copying code off a web site (in which case your editor should be able to handle that, 99% of the time it's all spaces).

You can almost say writing print or writing cursive is the same concept. You don't just randomly switch to cursive as you're writing print right? How could you write 50 lines of code using tab and then just out of no where you hit space 2 times + hit tab once + hit space 2 times on line 51? That just shouldn't happen ever.
** Python Support Thread ** Quote
05-02-2011 , 02:03 PM
It happened a lot at my old company before we had a nazi-Programmer (good kind) go through our repository and find people committing tabs and make them switch editor settings.

Here's a common example where we'd see it. You write a long line like this:

Quote:
if something > somethingElse and somethingB < somethingElseB
you realize that its too long and so you want to make it take two lines. If you're using spaces you can easily do this:

Quote:
if something > somethingElse and
__somethingB < somethingElseB
and use a three spaces to nicely line up the conditions (works better with mono-spaced fonts ldo). If you're using tabs you either have to have slightly uglier code that doesn't line up nicely or start mixing up tabs and spaces.

Edit: Copying and pasting is also a great example of how spaces start getting introduced. Anyway, I'll let it go now and just be happy that I've always worked with people that use spaces and set up tabs to automatically be converted to spaces.
** Python Support Thread ** Quote
05-03-2011 , 03:29 AM
Quote:
Originally Posted by RoundTower
are you really meant to use exceptions for program flow? I really like Python except that it seems to encourage constructs like

Code:
try:
    v = dic[keyVal]
except(KeyError):
    v = -1
I know there's a .get() function for this specific situation, but I mean in general, is this encouraged, in a situation where an exception is quite likely to be thrown? e.g. Dive Into Python suggests using exceptions for figuring out if a given external module is available. Is this really OK? Is it "unpythonic" to say stuff like "you shouldn't use exceptions here because the compiler optimizes for the situation where no exception is thrown"? Or "you shouldn't use exceptions here because when I read your code, I expect that an exception only occurs in an unusual situation"? Or "exceptions are basically syntactic sugar for GOTO, which is ugly, so you shouldn't use them if at all possible"?

I don't think I would even ask this question if I was sober, but even tomorrow morning I will be interested in your feedback.
I have a fair amount of code that looks like:
Code:
v = -1
if keyVal in dic:
    v = dic[keyVal]
Rather than using exceptions.
** Python Support Thread ** Quote
05-03-2011 , 08:12 AM
Like RoundTower mentioned, in that specific case you should probably just do:

Quote:
v = dic.get(keyVal, -1)
In the more general case, I kind of agree that I've never felt that easy with the really common use of exceptions. On one hand I've had the "only use exceptions for truly exceptional situations" mantra drilled into my head for awhile - and generally agree with it. On the other hand, its handy to not have a lot of special in-band return values that need to be checked for/handled.
** Python Support Thread ** Quote
06-01-2011 , 06:32 PM
I'm coming from using C++. The last formal instruction I had was in college. I was using Dev-C++ as my programming kit/compiler. So I'm a little lost as to how exactly Python works as a scripting language. Is there a front-end that I'll be working with?
** Python Support Thread ** Quote
06-01-2011 , 06:47 PM
Since Python is not statically compiled, you don't really need an IDE. Typing
Code:
python myScript.py
on the command line is enough to run your program.

That being said, there are Python IDEs available.
** Python Support Thread ** Quote
06-02-2011 , 02:16 AM
You do not need IDE. In case you need to debug you can use print function. This by far best and superior way for writing programs.

For simple editor you can use IDLE (it comes whit python distribution), but if you really think that you can not live without VisualStudio look like software, debugger and code completion try using Eclipse whit Pydev plugin.
** Python Support Thread ** Quote
06-02-2011 , 03:15 AM
Quote:
Originally Posted by ralu
You do not need IDE. In case you need to debug you can use print function. This by far best and superior way for writing programs.


Bash IDEs all you want but please, please learn there's more to debugging than print statements. Sometimes a full debugger is overkill... but sometimes a debugger is the only way.
** Python Support Thread ** Quote

      
m