|
Doing stuff like that is generally not cool. You're using exec, polluting your global namespace, etc. I don't know what you ultimately achieve here since you only gave us a part of the code, but why not use a dict? A dict is used to map keys to values and it seems like that's what you're doing here. So I'd prefer code:
code:
code:
|
# ? May 1, 2008 12:23 |
|
|
# ? May 13, 2024 10:21 |
|
Sorry, should have provided more info. The vars are, for example, for use with wxPython, giving unique IDs to buttons and whatnot, and the wxPython functions require numbers. I guess there was some misguided thought that it would be a bit more efficient using variables instead of a dict, though thinking about it now, aren't all variables actually just strings in the dict of a Python script? So that doesn't matter. It does make the code fractionally more concise later on; you just have to type ID_NEWPROJECT instead of enums['ID_NEWPROJECT'], though if that's at the cost of using exec and polluting the namespace I guess it's not such a good tradeoff. The other use I thought of was having an external script to your main one, which just stores the constants which you can then import individually. Possibly could do this by, I don't know, replacing the script's dict with a new one? That's probably a terrible idea. I think you misunderstood the str(i) bit, that's just for use with exec, as it operates on a string; the vars end up being numbers once the exec statement is actually executed. Anyway, you didn't answer my question.
|
# ? May 1, 2008 13:00 |
|
Ah, yeah, right, the str is for the exec. Anyway, if all the data that goes into exec is from within your system, you're technically safe. In your case, it's right there in the same script, hardcoded by you, so it comes from within your system. If data you put in exec comes from some untrusted source (like the user), then it's a vulnerability. The thing why it's bad to use exec generally is because you may one day decide that something that you hardcoded previously should come from the user and you may not notice that it later gets fed into exec. Putting stuff like that into the global namespace is not considered good practice, since that can easily lead to name mix-ups and it's best to have everything in the smallest scope that you need. True, the namespace is represented as a dict, but it's important what's in which dict and it's nice to have stuff nicely scoped. But if you really want to put those names in the global namespace, you could achieve the same functionality without using exec like so: code:
|
# ? May 1, 2008 13:13 |
|
Lancer383 posted:Not sure why you're having that issue with the print command -- have you tried using sys.stdout.write(string)? In your example it would be: What would you gain from that? Anyway. Try this;- code:
|
# ? May 1, 2008 15:39 |
|
Moof Strydar posted:The other use I thought of was having an external script to your main one, which just stores the constants which you can then import individually. Possibly could do this by, I don't know, replacing the script's dict with a new one? That's probably a terrible idea. This should do what I think you're asking for: events.py code:
code:
oRenj9 fucked around with this message at 16:11 on May 1, 2008 |
# ? May 1, 2008 15:51 |
|
Can someone fix that fugging faq. Python was not designed as a teaching language. It was designed as a scripting language for Andrew Tannenbaum's Amoeba Operating system, which is where I first heard of it many many moons ago.
|
# ? May 1, 2008 16:53 |
|
I need to read a string containing ASCII control characters, but Python won't let me. Even if I read it as a raw string it says "SyntaxError: EOF while scanning triple-quoted string". An example of a string is: ×J^@¯^@^@ ^AQU TEXT .TEXT TIMESTAMP ^BTEXT Where ^@ is an ASCII NULL, ^A is start of heading and ^B is end of heading. I'm sure there's an easy way around this but I can't find it. Any help appreciated.
|
# ? May 2, 2008 15:30 |
|
Zedlic posted:I need to read a string containing ASCII control characters, but Python won't let me. Even if I read it as a raw string it says "SyntaxError: EOF while scanning triple-quoted string". Post the code. The error you've got sounds like not an error with your logic, but with your actual code (syntax ).
|
# ? May 2, 2008 16:54 |
|
duck monster posted:Python was not designed as a teaching language. It was designed as a scripting language for Andrew Tannenbaum's Amoeba Operating system, which is where I first heard of it many many moons ago. It was 'heavily influenced' by ABC, a language designed for teaching. quote:In 1986 I moved to a different project at CWI, the Amoeba project. Amoeba was a distributed operating system. By the late 1980s we found we needed a scripting language. I had a large degree of freedom on that project to start my own mini project within the scope of what we were doing. I remembered all my experience and some of my frustration with ABC. I decided to try to design a simple scripting language that possessed some of ABC's better properties, but without its problems. Although it was written as part of the ameoba project, it seems he was considering more general goals very early on.
|
# ? May 2, 2008 17:44 |
|
Edit: Nevermind. Instead, if someone wants to show me how to rewrite this in a more Python way, that would be cool. I'm sure it can be done in a single somehow. code:
leinad fucked around with this message at 23:05 on May 6, 2008 |
# ? May 6, 2008 04:49 |
|
Hey, how do you pass a variable within a URL? I am using urllib2 code:
Any help would be great, thanks Also i'm having another problem.. I'm trying to generate a xml file. I can generate an xml file, However the content is hardcoded in the python script. like so code:
|
# ? May 6, 2008 22:59 |
|
karuna posted:Hey, how do you pass a variable within a URL? Are you trying to pass in paramters? Or just trying to interpolate the variable name into the string? The latter is super-simple: code:
quote:Also i'm having another problem..
|
# ? May 7, 2008 04:37 |
|
leinad posted:
There's not too much you can do, other than making your variable names more descriptive.
|
# ? May 7, 2008 13:28 |
|
I am trying to teach myself python as my first language, using a tutorial I found on their site called "A Byte of Python". I just reached the section on Lists, and I've hit a bit of a speed bump with a bit of example code. The code is for writing a shopping list, and manipulating it.code:
|
# ? May 9, 2008 18:29 |
|
The thing with for loops is that you specify which variable you want each element of the list you're iterating over bound too. So you could just as well docode:
|
# ? May 9, 2008 18:32 |
|
Hughmoris posted:Can someone explain what is going on with the for loop, mainly where "item" is coming from? I don't see it defined anywhere in the program. code:
* Technically, I think Python for blocks create an iterator then apply the value returned by iter.next() to the desired value. Such as: code:
oRenj9 fucked around with this message at 19:15 on May 9, 2008 |
# ? May 9, 2008 19:10 |
|
I was talking to a friend last night and he suggested I begin learning python. I just wanted to double check that his advice was correct and maybe see if there are any specific things you guys could add. Basically I'm going to be creating some SQL databases and I'd like to be able to access them from the web. I was originally thinking of using/learning PHP but I was told Python can accomplish the same tasks and in the end I'll be better off for learning Python versus PHP. I've been going through the basic tutorials this morning, but I figured I'd double check before I invest any significant time into it.
|
# ? May 9, 2008 20:14 |
|
Yeah, he was on spot. Python has drivers for mostly any kind of SQL database and can interact with them easily. Also Django is a web framework for Python, which makes it really easy to make web sites and web applications that use databases and all the Python knowledge you have is of course of great use with Django. Python is so widespread and generally useful that you pretty much can't miss with Python for any kind of use, but especially for web and databases. Also Google's App Engine is Python. It's a hosting environment for web Python program's by Google, also very cool and hip right now.
|
# ? May 9, 2008 20:27 |
|
Well I'm going through this: http://docs.python.org/tut right now, and I'm going to poke around with the stuff in the OP. Anything you can recommend that'd be more specific to the project I described would be awesome, but one step at a time. And yeah, the guy who mentioned python mentioned the google API engine, but they're full at the moment, so I just downloaded the SDK to toy with once I get there. Thanks! Edit: Also, retarded question I'm sure, but the tutorial just has me using the command prompt - is there a preferred editor or environment for python for beginners? Mulloy fucked around with this message at 20:50 on May 9, 2008 |
# ? May 9, 2008 20:48 |
|
Mulloy posted:Well I'm going through this: http://docs.python.org/tut right now, and I'm going to poke around with the stuff in the OP. Anything you can recommend that'd be more specific to the project I described would be awesome, but one step at a time. And yeah, the guy who mentioned python mentioned the google API engine, but they're full at the moment, so I just downloaded the SDK to toy with once I get there. IDLE, the bundled editor with the Python distribution isn't bad for beginners, but you should try out all the popular coding editors and pick one I won't name my personal preference here for fear of starting up another thread of those, but there are probably some recommendations in the thread.
|
# ? May 9, 2008 20:53 |
|
No Safe Word posted:IDLE, the bundled editor with the Python distribution isn't bad for beginners, but you should try out all the popular coding editors and pick one I won't name my personal preference here for fear of starting up another thread of those, but there are probably some recommendations in the thread. As an addendum; download wxPython and the associated demos. The demo pack comes with PyCrust (The Flakiest Python Shell), which is like the standard shell, but with more features like being able to see the elements inside of objects in real time. Plus wxPython kicks tk in the balls when it comes to features and usability.
|
# ? May 9, 2008 21:00 |
|
Is there a good mp3 playback module? Pymedia seems to be only for 2.4 and pygame seems to playback choppy in windows vista. EDIT: Also while I am at it how about midi playback? I am still learning, but I am jumping head first into python. I have tried to pick up C/C++ in the past and failed. With python I am really catching on to it. I am working on a crappy rpg game and its up to 230 lines of code and growing. Thats a huge program for me. Capnbigboobies fucked around with this message at 06:08 on May 10, 2008 |
# ? May 10, 2008 05:56 |
|
Capnbigboobies posted:Is there a good mp3 playback module? Pymedia seems to be only for 2.4 and pygame seems to playback choppy in windows vista. It sounds like you looking for playback in games so maybe trying to diagnose/cure pygame's issues would be best. Other then that if you are looking to make a media player you can play around with gstreamer and its amazing python bindings(win32 packages exist). I made a video player in less then 60 lines of code. tehk fucked around with this message at 06:39 on May 10, 2008 |
# ? May 10, 2008 06:33 |
|
tehk posted:Check out this, and maybe the package index too. I do plan on adding sound to my game at some point, but really its just text and if anything I would want midi music (for old school effect.) Well my plan is to make a simple player that grabs files from RSS feeds and plays them. The only problem is I have no idea how to process the RSS file to get the links and then download all the links. The gui (wxpython) + figuring out how to play the music sounds like it will be a lot easier.
|
# ? May 10, 2008 08:41 |
|
Capnbigboobies posted:I do plan on adding sound to my game at some point, but really its just text and if anything I would want midi music (for old school effect.) Ah! Something I know alot about! Python FeedParser is amazing and will allow you to do all the fun RSS stuff. To download all the links check out urllib2, you can write something like wget in 5 lines. edit: I did some rss stuff for AWN code:
tehk fucked around with this message at 09:50 on May 10, 2008 |
# ? May 10, 2008 09:21 |
|
tehk posted:Ah! Something I know alot about! Python FeedParser is amazing and will allow you to do all the fun RSS stuff. To download all the links check out urllib2, you can write something like wget in 5 lines. Thanks I was able to get something similar working to download the rss, but it used just urllib and didn't work all that great. Could I perhaps bug you on AIM to bombard you with beginner questions? I would really like to have somebody more experience to yak with. EDIT: Sweet that sample code you gave me work great at just finding the links! EDIT2: I noticed for the wget function not to trash mp3 files local_file needs to be local_file = open(desination, 'wb') Capnbigboobies fucked around with this message at 10:34 on May 10, 2008 |
# ? May 10, 2008 10:21 |
|
Capnbigboobies posted:Thanks I was able to get something similar working to download the rss, but it used just urllib and didn't work all that great. Could I perhaps bug you on AIM to bombard you with beginner questions? I would really like to have somebody more experience to yak with. Yea. The reason 'wb' is needed on your platform is covered here, basically its how windows handles binary files(compared to other platforms python works on). tehk fucked around with this message at 03:43 on May 15, 2008 |
# ? May 10, 2008 11:23 |
|
karuna posted:Hey, how do you pass a variable within a URL? Check out the official documentation on urllib and urllib2 (http://docs.python.org/lib/module-urllib2.html) specifically the part about urllib2.urlopen() and urllib.urlencode(). That should answer most of your questions. I've worked with these libraries extensively and have encountered pretty much every conceivable problem that could come up with them. deedee megadoodoo fucked around with this message at 18:53 on May 10, 2008 |
# ? May 10, 2008 18:35 |
|
I'm teaching myself a little bit about wxPython, so I'm implementing the calculator from this tutorial except I'll make it evaluate postfix expressions. Anyway, do I have to create a new event handler function for each number and operator button in order to append the symbol to the end of the text box, or is there a way to do it in a single function? Could I set the id of each number button to its value and get the id in the handler? Then use 11-14 for the operators?
|
# ? May 12, 2008 04:58 |
|
leinad posted:Anyway, do I have to create a new event handler function for each number and operator button in order to append the symbol to the end of the text box, or is there a way to do it in a single function? Could I set the id of each number button to its value and get the id in the handler? code:
code:
|
# ? May 12, 2008 06:37 |
|
As I mentioned above, I am still extremely new to python and writing code in general. With that said, I've encountered another hiccup. I am working on a project Euler problem where I need to calculate a huge number, then add all the digits in that number. I have succeeded in calculating the number, but I'm a tad unsure how to add up the digits that compose that number. *disclaimer: I'm a little shaky on my terminology, so forgive me. I am thinking I should break my huge integer into a list, so that each digit in my huge number takes up an element. I can then do a loop on my list that will give me a summation of every element in the list, which in turn is my final answer. Is my idea doable, or am I approaching it in the wrong way? If it is possible, how do I turn an integer into a list?
|
# ? May 14, 2008 20:28 |
|
code:
Strings in Python are actually sequences, meaning they're very list-like and can be iterated over just like a list, with for loops and etc. So str(some_integer) is how you get all the digits of a given integer, as a sequence. Of course, you then have to call int() on the individual list items to get them back into integers, otherwise it's rather difficult to sum them Python will not automagically let you do things like 1 + "1" like PHP might do. This is a good thing! PHP's way of doing things leads to horribly hard to find bugs, and promotes sloppiness. Finally, you can make a one-line equivalent to a for loop with a list comprehension, namely [expression for item in sequence] where expression is just something that returns a value, item is your loop variable name, and sequence is some sort of sequence - a list, a string, etc, or some expression evaluating to one. Putting it all together does exactly what you seem to be asking for...hope it makes sense
|
# ? May 14, 2008 20:42 |
|
Hm, I read about list comprehensions last night and thought I'd give them a go. What I'm trying to do is create a dictionary from a list, using a comprehension:code:
edit: I suppose this is better suited to my specific purpose, since I am not fussed about the actual return value of the comprehension: code:
Mashi fucked around with this message at 22:20 on May 14, 2008 |
# ? May 14, 2008 22:06 |
|
trycode:
Also, it's not cool to use dict as a variable name, since the aforementioned function is named that way. hey mom its 420 fucked around with this message at 23:08 on May 14, 2008 |
# ? May 14, 2008 23:05 |
|
Yea, I found that out! `dict` object not callable, what!? And thanks that looks like exactly what I wanted! I'm really liking Python. I think I'm going to have fun creating some disgustingly long one liners before I eventually opt for readability like I did with PHP... Mashi fucked around with this message at 23:21 on May 14, 2008 |
# ? May 14, 2008 23:17 |
|
Yeah it's really easy to make really powerful one liners with Python, especially with generator expressions and the itertools module. I admit I submit to the urge a bit too often. But when you're just out to quickly make some code to one-tiem calculate something you need, it's great. For instance, I had a file that had 1 line for every hour in year 2007 and each line had the level of the tide on the Adriatic sea coast at that hour. It took me just 5 lines to read the file and get a list that has 24 values, each representing the average tide level for the hour over a year.
|
# ? May 14, 2008 23:30 |
|
That's great, your program was wider than it was long.
|
# ? May 14, 2008 23:34 |
|
bitprophet posted:
code:
Many functions actually use iterators, even though they are most commonly called with lists. Scaevolus fucked around with this message at 00:04 on May 15, 2008 |
# ? May 14, 2008 23:59 |
|
Yeah it's good practice to maximize generator use and minimize lists. Of course it's cool to use lists for stuff you know will be small, but it doesn't cost you anything to use generators and generator expressions, but you will save on memory space when the data sets get big.
|
# ? May 15, 2008 00:04 |
|
|
# ? May 13, 2024 10:21 |
|
I'm quite aware of generator expressions, but I've been doing Python since before they existed, and it's very hard to change that particular habit I guess vv You're right that they are generally superior to list comps.
|
# ? May 15, 2008 00:11 |