|
You can also use os.path.join to join paths instead of having to deal with escaped slashes.
|
# ? May 23, 2013 03:40 |
|
|
# ? May 9, 2024 14:38 |
|
Thanks for the thoughts on coroutines, Dren. I did it that way and just put a brief explanation in the docstring. Added to that there is the fact that a ready made example is present as well due to the fact that I was writing it for a purpose (had a specific application for walking the filesystem). Question: Should a module export its custom exceptions? I found out that the help() function respects __all__. But it seems to add unnecessary clutter to have the help list the custom exception class and a long list of double-underscore methods it inherits. Blah, blah, blah. It is a class of exceptions. Who would want to read that? So I thought perhaps __all__ should exclude the exceptions.
|
# ? May 23, 2013 04:06 |
|
Plorkyeran posted:Moving everything but the body of the try blocks to a decorator would make the coroutine-based code look pretty similar to regular functions. For most coroutines you could get by with two decorators, @coroutine and @coroutine_sink where the latter does not do target.close() after handling a GeneratorExit. Coroutines have a feature that precludes putting everything except the inside of the try block into the decorator, that you can do stuff before you start up the main while loop. E.g. consider a coroutine to do a grep. Python code:
Another issue is that a decorator that does everything but the contents of the try block would create an additional layer of function calls. This is what the current decorator looks like: Python code:
The type of decorator you're proposing would have to stick around permanently as another layer of function call.
|
# ? May 23, 2013 16:54 |
|
accipter posted:A nice shortcut to starting this to browse to the directory with Windows Explorer, and then type "cmd" in the location bar and hit enter. That way you don't have to navigate to the required directory. holy poo poo
|
# ? May 23, 2013 17:39 |
|
Bunny Cuddlin posted:holy poo poo You can also shift+right-click a folder and select 'Open Command Window Here'.
|
# ? May 24, 2013 04:32 |
|
bonds0097 posted:You can also shift+right-click a folder and select 'Open Command Window Here'. You can also do this by right clicking inside a folder if you have nothing selected.
|
# ? May 24, 2013 09:23 |
|
Does lxml's ElementTree have anything similar to BeatifulSoup's find_previous_sibling()? I've been looking at its docs and playing around with it, I can't see anything I can use for this but I'm a newbie so I could be missing something. I'm using lxml to parse the xml for BeatifulSoup anyway so I thought I could perhaps cut out a dependency.
|
# ? May 24, 2013 21:32 |
|
Newbie here! Doing Udacity's CS101 class and I'm getting a syntax error.code:
|
# ? May 24, 2013 22:09 |
|
You're missing a closing parenthesis at the end of the twoP assignment.
|
# ? May 24, 2013 22:11 |
|
I feel dumb.
|
# ? May 24, 2013 22:11 |
|
I have a Python module, let us call it x.py, I wrote for myself that I now want to be in version control. So I made a directory called simply x, moved x.py in there, and initialised a git repository. So far so simple. But now in order to import that module I have to type import x.x as x Which is kind of onerous. I could add the directory x to my PYTHONPATH, but I don't want to do that for every module I create. I could put a copy of x in my Python install directory, but I'd have to update it each time I make a change. But I see that I can rename x.py to __init__.py and I can once again just "import x". So now I have a directory containing a single file, "__init__.py" (and a .gitignore file and git/pycache nonsense). Is there a better way?
|
# ? May 28, 2013 07:09 |
|
Hammerite posted:I have a Python module, let us call it x.py, I wrote for myself that I now want to be in version control. So I made a directory called simply x, moved x.py in there, and initialised a git repository. So far so simple. But now in order to import that module I have to type You could do "from x import x" Or you could just create a single python directory, turn it into a git repository, point your PYTHONPATH at it, and then drop x.py and any other python modules into it. If there are some modules that you don't want to version control, then just don't add them to the repository
|
# ? May 28, 2013 08:41 |
|
You can put from x import * into __init__.py. This will let you import x and then access its members like x.fancy_things().
|
# ? May 28, 2013 08:51 |
|
Thanks, I decided to bite the bullet and split the thing up into a half dozen files. After a bit of faffing around with import statements and so on the organisation of the code is now improved.
|
# ? May 28, 2013 10:47 |
|
Issue/question with PyQT / resources / cx_freeze. I'm using QT's resource system to set an application icon. (Goes in the top left corner of the Window for Windows programs) I created the resource in Designer, then used pyrcc4 to create a rc.py file. It works properly on my uncompiled program, but fails to show (Shows the generic windows program icon instead) when compiling the script with cx_freeze. Note that I am not referring to the icon you click to launch the program - that's not handled by QT, and works properly. Any ideas? This is my setup.py. Python code:
edit: Solved, from Stack Overflow. The solution is to copy the imageformats folder from the PyQT directory to my program's directory. The image folder was (Windows) in Python33\Lib\site-packages\PyQt4\plugins. Dominoes fucked around with this message at 01:47 on May 31, 2013 |
# ? May 28, 2013 23:32 |
|
I have an python XML parsing problem that I can't seem to figure out. I have the following XML: code:
code:
|
# ? May 30, 2013 00:55 |
|
watwat posted:I have an python XML parsing problem that I can't seem to figure out. Python code:
The Gripper fucked around with this message at 11:45 on May 30, 2013 |
# ? May 30, 2013 11:35 |
|
edit: nvm
Dominoes fucked around with this message at 02:49 on Jun 1, 2013 |
# ? Jun 1, 2013 02:41 |
|
Sab669 posted:So a buddy of mine is going back to college to get a BS in Comp Sci in a few months, and I have a really dumb question. He decided to start with the MIT lecture last night and I guess it starts off having him use Python. I don't know the first thing about Python or the Python Shell application it was having him use (Or any scripting language for that matter, I'm a newbie C# guy). He was having a problem trying to "understand" how he would actually use this in the real world. By that I mean, if he saved a file and ran it, a command window would pop up and immediately disappear, even though the script requests user input. http://kivy.org/ Its got a few rough edges in terms of the fact its a young project and evolving fast, but its very actively maintained and actually achieves cross platform (including ios(!) and android) really well. And the apps look pretty nice if you like black things.
|
# ? Jun 2, 2013 19:17 |
|
That kivy framework looks pretty neat. I've been looking for something like this for a small side project I'm working on and it might just fit what I need.
|
# ? Jun 3, 2013 18:28 |
|
Just a end-of-day frustration I just found in our code:code:
code:
|
# ? Jun 3, 2013 22:05 |
|
What library would be recommended for doing some 3D geometry stuff? I need to calculate dihedral angles and best fit planes for a list of points, checking coplanarity and combining coplanar triangles into polygon faces.
|
# ? Jun 3, 2013 23:52 |
|
I'm getting ready to release my first program. Since I'm new to programming and don't know what the hell I'm doing, I'm looking for feedback on the overall layout of my code. It seems functional, but may appear hideously disfigured on the inside to an experienced coder. Let me know what you think, and if you have any suggestions. Main Python file (should open directly in browser) Standalone Windows binary Dominoes fucked around with this message at 00:27 on Jun 4, 2013 |
# ? Jun 4, 2013 00:22 |
|
Dominoes posted:I'm getting ready to release my first program. Since I'm new to programming and don't know what the hell I'm doing, I'm looking for feedback on the overall layout of my code. It seems functional, but may appear hideously disfigured on the inside to an experienced coder. Let me know what you think, and if you have any suggestions. Some general thoughts here, but none if it's that big of a deal: I try to avoid doing "from X import Y". Throughout your code where you use Y, it's better if you know that it came from X. Explicit is better than impicit. code:
code:
code:
code:
code:
|
# ? Jun 4, 2013 01:02 |
|
Thermopyle posted:Some general thoughts here, but none if it's that big of a deal: Thank you. I've indented the function descriptor comments. I thought I read somewhere that docstring notation should only be used for long strings, not comments. Is this valid, or did I misinterpret something? I've been using 'from x import y' to make the code easier to read. I have an easier time comprehending shorter lines, especially in cases like this where the additional code is always the same. I've been looking at it similar to functions' importance: It allows me to reuse code instead of typing the same thing multiple times. Is my reasoning valid/common, or should I use 'import x' for everything? I also have a counter-reason for the downside of not knowing where an expression in the program body came from: Not knowing what a module from the import list is used for. Along the same reasoning with shorter code, what's the reason for using os.path.join() instead of + ? I've changed the names of the save() Main class methods to save_gui. I don't have a good grasp on the layout of QT code, and agree that having separate methods and functions for save() is messy. It's set up that way, because things involving input from the GUI seem to only work if they're in the Main() QT class. So, I send the input to these class methods, then have them call my main program's functions, which do most of the work. I like to keep as much of my code as possible in my own functions and classes. Dominoes fucked around with this message at 03:04 on Jun 4, 2013 |
# ? Jun 4, 2013 02:05 |
|
Dominoes posted:Thank you. I've indented the function descriptor comments. I thought I read somewhere that docstring notation should only be used for long strings, not comments. Is this valid, or did I misinterpret something? code:
Dominoes posted:I've been using 'from x import y' to make the code easier to read. I have an easier time comprehending shorter lines, especially in cases like this where the additional code is always the same. I've been looking at it similar to functions' importance: It allows me to reuse code instead of typing the same thing multiple times. Is my reasoning valid/common, or should I use 'import x' for everything? I also have a counter-reason for the downside of not knowing where an expression in the program body came from: Not knowing what a module from the import list is used for. I almost always use from module import whatever -- this is personal style. If I want to see where some name is defined, I control-click it in PyCharm or use grep. Dominoes posted:Along the same reasoning with shorter code, what's the reason for using os.path.join() instead of + ? I was unable to run your code on my (Linux) laptop since you've hard-coded Windows path separators. os.path.join uses / or \ as appropriate on different platforms and has a few other advantages over manually concatenating filesystem paths. I also do from os.path import join as ospj to use a shorter (but still descriptive and greppable) function name.
|
# ? Jun 4, 2013 02:50 |
|
Lysidas posted:The term "docstring" specifically refers to a string literal that is the first statement in a module, function, class or method definition. These strings serve as documentation for the user of the function/class/module and are accessible programmatically through __doc__ attributes. IPython lets you easily view these: Lysidas posted:I was unable to run your code on my (Linux) laptop since you've hard-coded Windows path separators. os.path.join uses / or \ as appropriate on different platforms and has a few other advantages over manually concatenating filesystem paths. I also do from os.path import join as ospj to use a shorter (but still descriptive and greppable) function name. Dominoes fucked around with this message at 03:06 on Jun 4, 2013 |
# ? Jun 4, 2013 02:57 |
|
I like 'from X import Y', it explicitly defines (at the top of your code) what imported names will be present in your code and I find that very helpful compared to just an 'import X' (what exactly is the author using from X? what's its purpose? I have to read/grep through to get a clue). As noted, this certainly seems appropriately subjective and can be chalked up to personal style.
|
# ? Jun 4, 2013 03:07 |
|
Dominoes posted:Thank you. I've indented the function descriptor comments. I thought I read somewhere that docstring notation should only be used for long strings, not comments. Is this valid, or did I misinterpret something? Note what PEP-257 says about one-line docstrings. I prefer the multi-line format for single-line docstrings despite what 257 says about it not looking as good, but it's not a big deal. Dominoes posted:I've been using 'from x import y' to make the code easier to read. I have an easier time comprehending shorter lines, especially in cases like this where the additional code is always the same. I've been looking at it similar to functions' importance: It allows me to reuse code instead of typing the same thing multiple times. Is my reasoning valid/common, or should I use 'import x' for everything? Here's what Guido has to say on the matter when someone said that they preferred to do it your way: quote:You would be wrong (unless you got your examples swapped around :-). It's not as big of an issue on a small file like yours, especially when it's all fresh in your mind. However, in 12 months, after the code has quadrupled in size, seeing a reference to load() in the middle of your code instead of yaml.load() is going to be confusing. Where did this load() come from? Wait, why is there a loads() in here as well? ITS SO CONFUSING! Dominoes posted:I also have a counter-reason for the downside of not knowing where an expression in the program body came from: Not knowing what a module from the import list is used for. Even if you have a good reason for caring (I can't think of a reason off the top of my head), it's going to be much less frequent that you care about this rather than having to scroll to the top of the file for the bajillionth time to see where this load() function came from. Dominoes posted:Along the same reasoning with shorter code, what's the reason for using os.path.join() instead of + ? Basically, this: http://stackoverflow.com/questions/13944387/why-use-os-path-join-over-string-concatenation
|
# ? Jun 4, 2013 03:07 |
|
Thermopyle posted:It's not as big of an issue on a small file like yours, especially when it's all fresh in your mind. However, in 12 months, after the code has quadrupled in size, seeing a reference to load() in the middle of your code instead of yaml.load() is going to be confusing. Where did this load() come from? Wait, why is there a loads() in here as well? ITS SO CONFUSING! For this reason, json is one module that I always import in its entirety. You're right that load() is too general when you're looking at the file a few months from now. A lot of the standard library classes and functions are named descriptively enough, though. These are the stdlib imports from one of my relatively recent scripts: Python code:
Guido's absolutely right in that it's good to avoid ambiguity, but lots of the functions/classes in the standard library are clear enough on their own.
|
# ? Jun 4, 2013 03:53 |
|
You can also use os.sep instead of hardcoding separators.
|
# ? Jun 4, 2013 03:58 |
|
Thermopyle posted:It's not as big of an issue on a small file like yours, especially when it's all fresh in your mind. However, in 12 months, after the code has quadrupled in size, seeing a reference to load() in the middle of your code instead of yaml.load() is going to be confusing. Where did this load() come from? Wait, why is there a loads() in here as well? ITS SO CONFUSING! Lysidas posted:For this reason, json is one module that I always import in its entirety. You're right that load() is too general when you're looking at the file a few months from now. Dominoes fucked around with this message at 04:23 on Jun 4, 2013 |
# ? Jun 4, 2013 04:16 |
|
I also prefer "from x import y" in most circumstances, only using "import x" if I use a lot of different features from that module. But there's a reason that you can do both: personal preference Thermopyle, why is a """single-line docstring""" better than #single-line docstring? They serve the same purpose and do the same thing but # is a lot cleaner looking when you only have one line Nybble posted:Just a end-of-day frustration I just found in our code: That's why I don't bother loving around with MySQLdb's bizarre variable substitution nonsense and just do it myself using Python code:
|
# ? Jun 4, 2013 07:30 |
|
QuarkJets posted:Seriously, is there any reason to use MySQLdb's variable substitution mechanisms instead of just building the strings yourself? Not being vulnerable to SQL injection attacks?
|
# ? Jun 4, 2013 07:47 |
|
yaoi prophet posted:Not being vulnerable to SQL injection attacks? Yeah, let the database library deal with working out whether quotes should go around a thing, how a string should be escaped, etc. Don't write PHP in Python As for importing names from a module, if you want it to be clear from the import statement what you are using something for _and_ you want the thing to have a descriptive name, you can always do from someModule import someName as descriptiveName (I'm sure someone will criticise me for this because omg someone who sees descriptiveName in use will have to go look for the line where it is imported, how could you do that!!! but I'll just say in advance that I think the upsides outweigh that, as long as the file is not enormous (which it shouldn't be))
|
# ? Jun 4, 2013 09:50 |
|
yaoi prophet posted:Not being vulnerable to SQL injection attacks? Seems like a good enough reason, but only for queries that use user inputs, right? Are there others?
|
# ? Jun 4, 2013 09:52 |
|
QuarkJets posted:Seems like a good enough reason, but only for queries that use user input, right? Are there others? If you use it for queries that use user input (and I agree you should), then IMO for consistency reasons alone you should do it everywhere.
|
# ? Jun 4, 2013 09:53 |
|
Going crazy with Python GUI crap. I've switched from TKinter to PyQt (using pyside) and cannot seem to figure out how to approach the below program concept. It could be that the concept is all wrong as well, so if it is, be gentle and provide some direction on how re-evaluate the concept. I'm creating a class for the GUI components and am trying to create buttons to switch between one layout and another. I was able to do this in TKinter by obfuscating the create/destroy of the frames that contained the various functions of each menu. main python file: -Class --Intro Menu --Player Select Menu --Difficulty Settings Menu --Main Game Window --etc... -main --Create class -if name main --main Through out my googling, I've not really found any good tutorials on how to handle multiple windowed applications. I understand that Python might not be the best programming language for this but its the language I prefer right now. I've found references to QStackedlayout and setlayout, however I have not found anything to help with the concept of how to best build multiple windowed apps like above. Also reading the QT libraries are difficult for me because they tend to be in a C/C++ style for function use. Reasoning: I'm attempting to remake a very old game as a way to dig myself in to Python and GUI programming. The repetition of programming the game should help the language stick a bit, as well as allow me to further improve on the application as my programming progresses. The game is Jones In The Fastlane and all I've done so far is load up the original and collect all the data points and GUI windows in to notes. I know what needs to go in and have an idea of what to expect. I was planning on building all of the menu components, creating all the variables in the process, and then putting it all together with the engine. n0manarmy fucked around with this message at 14:57 on Jun 4, 2013 |
# ? Jun 4, 2013 14:45 |
|
n0manarmy posted:Going crazy with Python GUI crap. I've switched from TKinter to PyQt (using pyside) and cannot seem to figure out how to approach the below program concept. It could be that the concept is all wrong as well, so if it is, be gentle and provide some direction on how re-evaluate the concept. Main program file: -QT class that is an instance of QtGui.QMainWindow. Looks like this: code:
-Separate classes for each window. ie: code:
-boilerplate stuff at the end. Also put global variables that affect the GUI, and anything that needs to happen with the GUI at startup here. code:
Example I posted last night. Has a link to the main program file, and the full, compiled Windows program with source code. Dominoes fucked around with this message at 15:06 on Jun 4, 2013 |
# ? Jun 4, 2013 15:00 |
|
|
# ? May 9, 2024 14:38 |
|
QuarkJets posted:Thermopyle, why is a """single-line docstring""" better than #single-line docstring? They serve the same purpose and do the same thing but # is a lot cleaner looking when you only have one line
|
# ? Jun 4, 2013 15:19 |