|
Is there a better way to write this?code:
|
# ? Nov 24, 2011 02:27 |
|
|
# ? May 9, 2024 13:12 |
|
Victor Vermis posted:Yes, byext.py and whatever script I'm trying to run, say atest.py, are both located in C:\Python25\Tools\Scripts When you are saying "entering the following code into the shell", do you mean that in your terminal you are typing "from byext import *" before running "python atest.py"? If so, you need to put the "from byext import *" at the top of the "atest.py" file. Then just open your terminal and type "python atest.py" to run it.
|
# ? Nov 24, 2011 02:33 |
|
vikingstrike posted:When you are saying "entering the following code into the shell", do you mean that in your terminal you are typing "from byext import *" before running "python atest.py"? If so, you need to put the "from byext import *" at the top of the "atest.py" file. Then just open your terminal and type "python atest.py" to run it. I open up Idle Python GUI. To the right of >>> I type "from atest.py import *" and hit enter. I expect the contents of atest.py to now take effect. For example, if the contents of atest.py are just "message = poop", then the next time I type "print message" I expect the shell to spit "poop" back out at me. Instead, after typice "from atest.py import *" I get code:
|
# ? Nov 24, 2011 03:03 |
|
From test1.pycode:
code:
code:
|
# ? Nov 24, 2011 04:57 |
|
Victor Vermis posted:
Try just: code:
|
# ? Nov 24, 2011 06:38 |
|
Victor Vermis posted:I open up Idle Python GUI. To the right of >>> I type "from atest.py import *" and hit enter. I expect the contents of atest.py to now take effect. code:
|
# ? Nov 24, 2011 12:03 |
|
FoiledAgain posted:Is there a better way to write this? What are some values of lang_list[j]?
|
# ? Nov 24, 2011 13:58 |
|
spankweasel posted:You don't need the file extension when importing a module. vikingstrike posted:I never use IDLE, so I have no idea if that's causing you trouble. But, from what I am understanding you're trying to do, the above example shows you what's going on. If you do just "import test1", then you would just change the calls to test1.returnPoo() and test1.poo. The Gripper posted:Note that sys.path doesn't include windows/system PATH locations, only your python-install specific locations. You don't need to worry about this if you're making your own .py files and keeping the modules you want to import in the same directory. code:
Thanks!
|
# ? Nov 24, 2011 14:29 |
|
If you want to put it in somewhere that scripts can access it anywhere, put it in the site_packages directory, or create your own special funtime directory and figure out how to stick it in the python path.
|
# ? Nov 24, 2011 14:39 |
|
I do have to say it was quite enjoyable to write a function called returnPoo(). Ha.
|
# ? Nov 24, 2011 14:41 |
|
duck monster posted:If you want to put it in somewhere that scripts can access it anywhere, put it in the site_packages directory, or create your own special funtime directory and figure out how to stick it in the python path.
|
# ? Nov 24, 2011 16:14 |
|
Right, its just worth pointing out that leaving code strewn all over the system python directory isn't really a "good" habit to be in.
|
# ? Nov 24, 2011 19:40 |
|
Scaevolus posted:What are some values of lang_list[j]? Just strings. It looks like this: lang_list = ['Vietnamese', 'Welsh', 'Welsh (south)', 'Arrente', 'Arrente (some dialects), 'Faroese', ...] I'm comparing some information about these languages and I need to avoid dialects, which is why I have that split('(') in there. If I'm already looking at 'Welsh' I don't want to compare it to 'Welsh (south)'. edit: for clarity, my original question was "is there a better way to write the following?" if lang_list[j].split('(')[0].strip() == name: pass FoiledAgain fucked around with this message at 19:50 on Nov 24, 2011 |
# ? Nov 24, 2011 19:48 |
|
FoiledAgain posted:Just strings. It looks like this: If not, you can skip some steps and just do code:
You could potentially do code:
The Gripper fucked around with this message at 20:06 on Nov 24, 2011 |
# ? Nov 24, 2011 19:57 |
|
I'm writing a program with PyQt4 and trying to pickle quite a large data structure that includes lists, dicts, string, and so on. I seem to be running into problems as one of the objects in one of the lists includes a QDomElement, as it is apparently impossible to serialize this. Every time I try, I get this error:code:
Does anyone know how QDomElements are supposed to be serialized? Can the problems have something to do with them being associated with open .xml files or QDomDocuments? I know I could write the QDomElement into .xml but that would be a huge hassle compared to just pickling it along with the rest of the data structure. There also seems to be some sort of function for saving into a QTextStream but I don't know if that can help or how I should do it.
|
# ? Nov 25, 2011 02:08 |
|
I have a school issue I would like to discuss. I'm currently enrolled in university as a writing minor and I would like to pursue a interactive media BA. To get in, though, I need to send a piece of work that I've done specificaly for the program. The piece is rated equally between concept quality, technical ability and the aestethics of the project. I was thinking of making a text based game but that's hardly aestethically nice. I have about a year and a half of time in front of me to make the best piece I can. I have plenty of time on my hands but I want to use it in the best way possible. I have never programmed anything in my life so my question is this: Is python a good language to learn to code an interactive game? I suppose I could watch a few photoshop tutorials and make a web-based RPG with pictures I made. Is this doable from scracth in a little more than a year? Any ideas? Anything helps, as have no knowledge of programming or programming languages.
|
# ? Nov 25, 2011 05:22 |
|
pastorrich posted:I have never programmed anything in my life Walk before running. Try writing a text game first. Don't spend the entire time on the project - spend it on smaller projects and get a whole bunch of stuff done. Learning involves making mistakes. Don't burden a project with the mistakes you will make from the outset.
|
# ? Nov 25, 2011 05:41 |
|
The Gripper posted:I can't think of a better way to do what you're doing already, if the code you've already posted does things exactly the way you need it to. Thanks for the feedback. It does work fine, it just looked ugly. I'm one of those academics you hear about in this thread who writes horrible code because I'm not a programmer by training. I try to make my stuff as readable as possible, so I was really just checking if there was a "nicer" way of writing that line. I have another question while I'm at it: I want to be able to identify everything that's the same "base" letter is a list of letters, ignoring accents/diacritics. For instance I want to treat 'e', 'é', 'ê', etc. as the same. Is there an easy way to do this, or would I need to set up a custom function for this? (There are probably 15-20 different accent marks in my data, some of them quite rare, and some of them also on consonants.)
|
# ? Nov 25, 2011 06:13 |
|
FoiledAgain posted:I have another question while I'm at it: I want to be able to identify everything that's the same "base" letter is a list of letters, ignoring accents/diacritics. For instance I want to treat 'e', 'é', 'ê', etc. as the same. Is there an easy way to do this, or would I need to set up a custom function for this? (There are probably 15-20 different accent marks in my data, some of them quite rare, and some of them also on consonants.) I don't know if this'll work perfectly for you but it's what I have in my helpers library for normalizing accents and diacritics into their base ASCII forms when auto-generating web page names from arbitrary input text, etc: code:
|
# ? Nov 25, 2011 11:28 |
|
I solved my own problem by refactoring code so that the information in the QDomElements is read into container classes that are then serialized normally. A lot more complicated this way but it works. I guess the lesson here is that you can't serialize DOM objects!
|
# ? Nov 25, 2011 20:59 |
|
ynohtna posted:
Thanks for the idea, but this is giving me a UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128) Any idea why? Unicode confuses the hell out of me for some reason, and I always have trouble with funny letters. In case it makes any difference, this is an example of typical list that I would have: [p,pʲ,pʷ,t,tʲ,c,cʷ,k,kʷ,k͡p,k͡pʲ,i,u,b,bʲ,bʷ,d,dʲ,ɟ,ɟʷ,ɡ,ɡʷ,ɡ͡b,ɡ͡bʲ,e,o,f,fʲ,s,sɥ,h,ɛ,ɔ,m,mʲ,n,nʲ,ɲ,ŋ,ŋ͡m,ŋ͡mʲ,a,l,lɥ,ɾ,ĩ,ũ,ɥ̥,ɛ̃,ɔ̃,w,ɥ,ã,] Some things that I would consider "the same": [ɛ̃] and [ɛ] [ɥ̥] and [ɥ] [p], [pʲ] and [pʷ]
|
# ? Nov 25, 2011 23:01 |
|
FoiledAgain posted:Unicode confuses the hell out of me for some reason, and I always have trouble with funny letters. See if this helps. I used to have a hard time with unicode as well.
|
# ? Nov 25, 2011 23:03 |
|
FoiledAgain posted:Thanks for the idea, but this is giving me a UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128) Sounds like you're not feeding in a fully fledged unicode string but rather a normal ASCII string where the bytes are stored as some other encoding. Edit: ahah, my function does indeed break when it's given ASCII strings containing high-bit chars when it tries to force it into a unicode string type. Try this update. code:
ynohtna fucked around with this message at 23:20 on Nov 25, 2011 |
# ? Nov 25, 2011 23:07 |
|
ynohtna posted:Sounds like you're not feeding in a fully fledged unicode string but rather a normal ASCII string where the bytes are stored as some other encoding. It does sound like you are doing this - if you have a string - its default encoding is ascii, so if you call unicode(foo) it is first decoded as ascii before being stored as unicode. so you must do "fooo".decode('text-encoding') to get a unicode string.
|
# ? Nov 25, 2011 23:16 |
|
ynohtna posted:Sounds like you're not feeding in a fully fledged unicode string but rather a normal ASCII string where the bytes are stored as some other encoding. So I need to convert it to some other encoding, right? I tried this: letter = letter.encode('UTF-8') But that throws the ssame error, so I guess I don't understand what it means to "encode" something. edit: AH! So you have to decode to get the unicode version. I thought that going that direction was encoding. Everything works fine now. Thanks! FoiledAgain fucked around with this message at 23:20 on Nov 25, 2011 |
# ? Nov 25, 2011 23:17 |
|
pastorrich posted:I have a school issue I would like to discuss. I'm currently enrolled in university as a writing minor and I would like to pursue a interactive media BA. To get in, though, I need to send a piece of work that I've done specificaly for the program. Pythons a fine language to learn on, and a great one to use as your primary weapon once you've learned. Its easy like basic is , but non-retarded and powerful enough to take you into fairly deep depths of computing.
|
# ? Nov 26, 2011 09:45 |
|
Has anyone had any luck getting PySide working on Lion? Downloaded latest QT sdk and installed it and PySide, and it just crashes and burns with code:
|
# ? Nov 27, 2011 08:57 |
|
Does anyone know if there's a generic Python-primitive to Tkinter Treeview parser? As in, if I hand a Python object to this hypothetical library, it descends through the object and turns all lists, dictionaries, into a tree for the Tkinter Treeview widget to display.
|
# ? Nov 27, 2011 20:43 |
|
qt hasn't been released for lion yet. 4.8 should be out real soon now.
|
# ? Nov 27, 2011 20:45 |
|
Kim Jong III posted:POST STUFF....FIREBUG Just wanted to follow up and say that your advice worked perfectly for me. Thanks again!
|
# ? Nov 27, 2011 21:15 |
|
Gothmog1065 posted:Is there a common separator programmers use? For example, I will have a plaintext file that will have lines that a script I'm writing will perform, and each line will have different functions (IE: open web, url, new/tab) etc. Okay, stupid question and check to make sure I'm not retarding something up here: To loop over new lines in the text file, it's: code:
Am I going about this completely wrong way? It's supposed to start off as a simple script.
|
# ? Nov 27, 2011 21:50 |
|
Use str.split:code:
|
# ? Nov 27, 2011 21:58 |
|
Suspicious Dish posted:Use str.split: That looks like what I need, one last question for now. Some of these line will have two arguments, some one. On top of that I was going to be passing different commands (Mapping a network drive, opening a browser, opening general programs, etc), if my functions have set defaults <def web_open(url = None, args = None) >, will passing on inforation <web_open(command,args)> pass on to the default commands (mapping(command,args) where the defaults are drive_letter,path), and will empty strings override the defaults? edit: Can I also call a function with a variable: code:
code:
Gothmog1065 fucked around with this message at 22:18 on Nov 27, 2011 |
# ? Nov 27, 2011 22:14 |
|
Gothmog1065 posted:That looks like what I need, one last question for now. Some of these line will have two arguments, some one. On top of that I was going to be passing different commands (Mapping a network drive, opening a browser, opening general programs, etc), if my functions have set defaults <def web_open(url = None, args = None) >, will passing on inforation <web_open(command,args)> pass on to the default commands (mapping(command,args) where the defaults are drive_letter,path), and will empty strings override the defaults? I recommend making all of your command functions take one argument, and doing the parsing inside each function. Gothmog1065 posted:
You can use the dispatch dict pattern: code:
|
# ? Nov 27, 2011 22:36 |
|
My only real question is why you are writing a scripting language inside of python rather than using python as a scripting language?code:
|
# ? Nov 28, 2011 00:58 |
|
tef posted:My only real question is why you are writing a scripting language inside of python rather than using python as a scripting language? Theres plenty of circumstances where a flexible workflow is necessary but a full blown programing language isn't a neat fit, particularly if that flexibility is user facing. Its *very* hard to make python eval() safe.
|
# ? Nov 28, 2011 01:27 |
|
tef posted:My only real question is why you are writing a scripting language inside of python rather than using python as a scripting language? I'm looking for something fairly self-contained, when I eventually get done, I want it to be on a flash drive and that's it so I don't have to install it, just run it off the drive. edit: Dish, I'm having a bit of an issue, I know you wrote that for 2.7, but other than the print function, are there any major changes? Gothmog1065 fucked around with this message at 07:20 on Nov 28, 2011 |
# ? Nov 28, 2011 05:58 |
|
Gothmog1065 posted:I'm looking for something fairly self-contained, when I eventually get done, I want it to be on a flash drive and that's it so I don't have to install it, just run it off the drive. You have three components when running your own custom script:
and only two when just using Python and your own set of custom libraries:
By just writing Python scripts like tef suggested, it's more self-contained. Gothmog1065 posted:edit: Dish, I'm having a bit of an issue, I know you wrote that for 2.7, but other than the print function, are there any major changes? What are you using? 3.2? I highly recommend using 2.7, not 3.2, for a large variety of reasons. I don't use 3.2 myself, but I can't think of any major issue. Post your traceback and code and I'll see what I can do.
|
# ? Nov 28, 2011 07:31 |
|
Suspicious Dish posted:By just writing Python scripts like tef suggested, it's more self-contained. Eventually I want this to be part of a larger program that will do some of the tasks at work to make things easier. For example, we use IPs a lot, I'd like to eventually have it where I can put an IP in to a box and have it do various functions depending on the need. quote:What are you using? 3.2? I highly recommend using 2.7, not 3.2, for a large variety of reasons. I don't use 3.2 myself, but I can't think of any major issue. Post your traceback and code and I'll see what I can do. I'm on 3.2.1 right now, going to update, I though I was on 3.1.3, but whatever. I keep hearing different reasons to either be updated or go back. I know 2.x has much better library and addon support, but for the basic stuff right now, will it make a difference? anyhoo: pre:Traceback (most recent call last): File "C:\Users\Gothmog\Desktop\Python\Script.py", line 61, in <module> main(0) File "C:\Users\Gothmog\Desktop\Python\Script.py", line 53, in main split_commands = command.split(',') UnboundLocalError: local variable 'command' referenced before assignment
|
# ? Nov 28, 2011 08:01 |
|
|
# ? May 9, 2024 13:12 |
|
Gothmog1065 posted:
As for 3.2 vs. 2.7, my decider for which to use is whether I'm making something self-contained and will be setting up the environment myself. If both of those are true I'll use 3.2, if I have to use external libraries that aren't 3.0 compatible I'll use 2.7, and if I don't know what version of python will be available I use 2.7 since that's what most distros ship with currently.
|
# ? Nov 28, 2011 08:09 |