|
Think about what you want in that case. If you're planning to store a configuration file or data file in the same place as the script, it might not make sense on some platforms. That folder may not even be writable for security reasons. Chances are the home directory might make more sense.
|
# ? May 20, 2013 00:22 |
|
|
# ? May 20, 2024 10:43 |
|
evensevenone posted:Think about what you want in that case. If you're planning to store a configuration file or data file in the same place as the script, it might not make sense on some platforms. That folder may not even be writable for security reasons. Chances are the home directory might make more sense. What I'm working with: -Single python script -Two configuration files (YAML) -One databasish file (YAML) -One icon -A temporary directory used for downloading and merging PDF files, prior to loading them to a user-specified save directory (QT dialog). I plan on distributing the program as a standalone folder, where the user can create a shortcut to the program and place it wherever, and move the whole folder wherever. It's a simple program, so I feel an installer's overkill. I'm open to restructuring the way I'm doing this, since I don't know wtf I'm doing. Where would you recommend placing the config files and temporary directory? I feel like keeping it together's good for portability. Dominoes fucked around with this message at 00:46 on May 20, 2013 |
# ? May 20, 2013 00:29 |
|
You want os.path.abspath(os.path.dirname(__file__)), that will always give you the full directory path for that file's location no matter where you're calling it from But it might make more sense to create and use a directory in the person's Documents folder, and it might also make sense to allow the user to set the temp directory path If this is causing a crash with cx_freeze, then I'm sure that others have encountered the same problem and you might try googling for the issue QuarkJets fucked around with this message at 03:33 on May 20, 2013 |
# ? May 20, 2013 03:29 |
|
Found a solution on Stack Overflow Uses a workaround where you locate a dummy module's directory, which is the same as the one it's in. Seems to find the correct directory, and not crash the frozen program. Am I correct assuming using the documents folder for config files would require using an installer? I don't want to leave pieces of the program lying around a user's drive without a way to cleanly uninstall it. The program provides a quick way to select, download and merge multiple documents from the FAA's website that are published each month. Since the program's goal is to save time and effort, I'm trying to keep things as simple as possible for the user. I do let the user select the directory to download the final product, and it defaults to their Home/Downloads folder. Dominoes fucked around with this message at 03:43 on May 20, 2013 |
# ? May 20, 2013 03:38 |
|
If the user selects the destination of the final product, then why do you need to know where this module is located?
|
# ? May 20, 2013 05:21 |
|
Use the tempfile module for temporary files. It'll handle creating a temp file/folder in a safe, user-writable place, not overwriting anything, and cleaning it up when done.
|
# ? May 20, 2013 06:02 |
|
Help me i'm trapped in a php hole and i cant get out.
|
# ? May 20, 2013 10:33 |
|
QuarkJets posted:If the user selects the destination of the final product, then why do you need to know where this module is located? I'd like to avoid having the user set the temporary directory; I'd like for this to be as simple and user-friendly as possible. "I just want to download approach plates, what's a temporary directly, and why do I have to set one? I don't know where to put it." The module locator workaround I got from Stack works by checking if the program's frozen. If so, uses dirname(sys.executable), if not, uses dirname(__file__). evensevenone posted:Use the tempfile module for temporary files. It'll handle creating a temp file/folder in a safe, user-writable place, not overwriting anything, and cleaning it up when done. Dominoes fucked around with this message at 11:46 on May 20, 2013 |
# ? May 20, 2013 11:11 |
|
I wrote some code that involves searching through directories and doing stuff to any files found whose filenames match a regular expression. I'm interested to know whether I've done it "the right way" or what I should have done differently. I wanted to provide the option to either (1) not search subdirectories, (2) search all subdirectories up to a specified depth, or (3) search all subdirectories regardless of depth. It seemed like the way you're supposed to navigate a file system is using os.walk(), so that's what I did. http://pastebin.com/Dzy8mNPs The relevant code is lines 202 through 258. In particular, I'm not sure what to do if an IOError gets thrown as a result of something the function tries to do. I mean, at the moment they aren't caught, but with the errors that I'm raising myself I attach information about which file was being processed and how many files were previously processed. It would be nice to have escaping IOErrors have that information attached to them, too, but I don't know what's the "correct" way to do such a thing.
|
# ? May 21, 2013 00:16 |
|
No-one answered my question but I think maybe I was overly complicating it. Anyway here's my question distilled into a more basic form. Suppose I have a function that makes a change or a series of changes to the filesystem. Some of the changes it attempts to make might result in an exception being raised (for example, it might try to write a file somewhere, but Python might not have the necessary privileges to do so). If that happens, I want to just allow the caller to deal with the exception, so I allow it to propagate out of the function. But, by the time an exception is raised, the function might already have made some changes to the filesystem, and it would be nice to have a way of communicating back to the caller what those changes were. What is the best way to do this?
|
# ? May 21, 2013 22:51 |
|
Well, there's always the option of attaching something to the exception. You catch the exception, and then either raise your own custom exception or monkeypatch and re-raise the original exception.
|
# ? May 21, 2013 23:44 |
|
Haystack posted:Well, there's always the option of attaching something to the exception. You catch the exception, and then either raise your own custom exception or monkeypatch and re-raise the original exception. See, both those things occurred to me, but I have no idea if there's a Correct Way of doing things that I don't know about because I'm a know-nothing newbie It occurred to me that I could raise my own exception and have it say something like 'While writing file "%s" after writing %d files already, this happened: ' followed by the __str__ of the existing exception, or the __str__ preceded by the class name, as when the exception is printed. But for all I knew that might discard some useful information that is carried by the original exception. It also occurred to me to interfere with the original exception, but then that might also have been frowned upon for reasons entirely unknown to me.
|
# ? May 22, 2013 01:08 |
|
I noticed that Python modules seem portable - I was able to install a module on Ubuntu that I couldn't get working by using build/install, by copying and pasting the folder from my Windows install's site-libs folder. Why aren't they distributed as stand-alone folders? Was it an exception that this one (PyPdf2) worked by copy/paste? I'm asking because the python setup build/install method gives me errors most of the time.
Dominoes fucked around with this message at 03:07 on May 22, 2013 |
# ? May 22, 2013 01:36 |
|
Hammerite I think the thing to do in your case is to store the state of the directory walk outside of the scope of the function doing the work. Something like this:Python code:
I actually prefer writing processing pipelines like this as coroutines but that's opening a whole can of worms.
|
# ? May 22, 2013 02:00 |
|
BigRedDot posted:No, sorry I shouldn't have been so coy. I gave the Numpy tutorial. .... I'm fairly convinced Numpy is going to change my life.
|
# ? May 22, 2013 02:36 |
|
Dren posted:Hammerite I think the thing to do in your case is to store the state of the directory walk outside of the scope of the function doing the work. Something like this: Thanks, that's really interesting. I hadn't heard of coroutines before and they seem complicated to wrap one's head around, but interesting.
|
# ? May 22, 2013 03:20 |
|
Dominoes posted:I noticed that Python modules seem portable - I was able to install a module on Ubuntu that I couldn't get working by using build/install, by copying and pasting the folder from my Windows install's site-libs folder. Why aren't they distributed as stand-alone folders? Was it an exception that this one (PyPdf2) worked by copy/paste? I'm asking because the python setup build/install method gives me errors most of the time. A) Sometimes there is compiled code that needs to be installed in order for the Python modules to work. For instance, parts of NumPy use compiled C and FORTRAN code. B) It's a lot easier to send out an RPM and have people install that than it is to give explicit instructions regarding where to place a stand-alone folder, or a set of stand-alone folders when it comes to larger projects. This also helps to ensure that most users will have a worry-free install process. If you use a Makefile, then that Makefile will probably come with some standard defaults and will at least help to minimize the pain. C) Using a standard install procedure creates uniformity between systems, which makes development more straightforward. If I log onto server X, then ideally I shouldn't have to set my PYTHONPATH to some special area because an admin decided to install it in a different place than on my home server. There are other reasons, too. On Ubuntu there's almost no reason to build/install Python modules yourself, just use apt-get unless you want to do something special
|
# ? May 22, 2013 06:06 |
|
Thank you.
|
# ? May 22, 2013 13:22 |
|
QuarkJets posted:There are other reasons, too. On Ubuntu there's almost no reason to build/install Python modules yourself, just use apt-get unless you want to do something special There are two very good reasons and they're called pip and virtualenv.
|
# ? May 22, 2013 14:22 |
|
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. I also think part of it for him is he expects to be creating full blown desktop applications, but you need to learn to walk before you can run. Most day-to-day users would poop themselves if they had to deal with the command line in any way. He was just texting me about it this morning as I was getting ready for work, so I didn't have a chance to sit down and try to learn anything, though I figured while he's taking this course I probably should too
|
# ? May 22, 2013 14:34 |
|
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. Sounds like he is working on Windows and is running into a limitation of the Windows command line. Basically, when you run a command line application on Windows, it will close the window as soon as the program exits, which is inconvenient if you wanted to see what the program did. If your program is waiting for user input then it will not close until it gets it (and goes on to exit), but probably your friend's newbie code contains a syntax error or hits some other error condition which causes an exception to be thrown, which isn't caught and causes the program to exit with a printed error message, which counts as exiting and causes Windows to close the window. There are ways around this. One way is to open a windows command line from the Start menu and run python from there, by typing "python <name of script>.py". If you are changing the script as you go, you can do this repeatedly without having to reopen the window. Note that for maximum convenience, the python install directory should be added to the Windows PATH environment variable, and the directory where he is saving his scripts should be added to the PYTHONPATH environment variable. This prevents errors where Windows can't find Python, or Python can't find the scripts. Your friend should also acquaint himself with the Python command line and how it differs from the Windows command line.
|
# ? May 22, 2013 15:30 |
|
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. There are windowing libraries available for python just like any other mature language. You're right about walking before you can run, though. The windowing stuff is, by and large, handled by external libraries. You have to learn to write code before you can start using them. Your first didactic project in engineering isn't going to be building a bridge just like your first one in programming isn't going to be writing a fully-featured web browser. Also (depending on where he's getting this CS degree, I suppose) CS is only tangentially about programming. Maybe he's looking for Software Engineering?
|
# ? May 22, 2013 15:33 |
|
Hammerite posted:Sounds like he is working on Windows and is running into a limitation of the Windows command line. Basically, when you run a command line application on Windows, it will close the window as soon as the program exits, which is inconvenient if you wanted to see what the program did. He was just doing some super primitive thing, forgive the syntax mistakes because I'm typing this from memory... x=raw_input("Whats your DOB?") y=raw_input("Whats your name?") print "Hi " + y + ", you were born on " + x So it was requiring input, which is why I thought it was weird it would close instantly. Thanks though, I believe the Windows command line of "python fileName.py" is more-or-less what he was looking for. He'll be attending University of Buffalo, Course List. I think he has to take all the 100 and 200 courses, then a 3 of the 300's and 3 of the 400's. Seems pretty open ended. I myself have a degree in Software Engineering, not CompSci. Maybe I just went to a lovely school but I'm quite unhappy with my education. I can build programs, sure, but I feel I lack a real technical vocabulary / understanding on some levels and definitely wish I had an actual CS degree. Like I am just awful with data structures and algorithm analysis. Sab669 fucked around with this message at 16:10 on May 22, 2013 |
# ? May 22, 2013 16:03 |
|
What your friend should do is launch a command shell and run his programs from there. Tell him to do:code:
I'm surprised he wants to write a GUI app, most teenage/college people I've talked to only care about writing web apps or phone apps.
|
# ? May 22, 2013 17:06 |
|
Python is often used for serverside applications and scripts that don't have a GUI or any user interaction at all, and therefore it doesn't matter that they're launched from the command line or as a service.
|
# ? May 22, 2013 17:10 |
|
Foiltha posted:There are two very good reasons and they're called pip and virtualenv. Yeah. I can't remember the last time I apt-getted a python module. I'm not sure about this, but I kind of think newbies should be using pip + virtualenv (really virtualenvwrapper) (or whatever you're supposed to do with Python3) from the very beginning.
|
# ? May 22, 2013 17:11 |
|
Dren posted:I'm surprised he wants to write a GUI app, most teenage/college people I've talked to only care about writing web apps or phone apps. I'm not really sure what his "end goal" is. Right now he has a Bachelor's in Business Management and after spending 2 years in an office he's miserable, decided to go back for a BS in CS. I know I personally want to get into mobile development, but I think he just wants something completely different
|
# ? May 22, 2013 17:18 |
|
Sab669 posted:"Friend" wants to start Python. A few thoughts... People have been suggesting running python from a cmd.exe window, which is a good idea. 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. I would also encourage you to suggest a good text editor (SublimeText2, Notepad++, or vim). Vim isn't probably the best suggestion at this point, but it is my preferred editor. Python provides an interactive interpreter, which is great for interacting with objects to understand their behavior or trying out code. The interpreter that ships with Python is okay, but IPython has a number of improvements. Installing Python Extensions on Windows can be a bit of hassle. I highly recommend this site: http://www.lfd.uci.edu/~gohlke/pythonlibs/ as it has pre-build binaries with Windows installers. Going from application to GUI is an order of magnitude increase in complexity.
|
# ? May 22, 2013 17:18 |
|
Dren posted:Hammerite I think the thing to do in your case is to store the state of the directory walk outside of the scope of the function doing the work. Something like this: So now suppose I want to do it like this: Python code:
|
# ? May 22, 2013 19:57 |
|
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. 1: Go through all of the Python tutorials on Codeacademy. They're very good. 2: Install a text editor that will run code automatically. I use Notepad++ with the NPP plugin. I can press alt+shift+f5, and the code will immediately run in a Windows command prompt that doesn't close after running. 3: Install Pip - this will make installing some packages painless. I don't remember how to do it offhand; I thought there was an installer, but I can't find it. 4: To make a program others can use, you need Cx_freeze and a GUI toolkit. Toolkits include TKinter, WxWidgets, QT and GTK. I've only used QT, but learning it was more difficult than learning Python itself, due to a lack of good or interactive tutorials like CodeAcademy. The official QT docs describe the properties of each GUI element without explaining what they do. You will need to package msvcp100.dll and msvcr100.dll, found somewhere in your windows directly with the program when distributing it, or ensure the user has Microsoft Visual C++ 2010 redistributable installed. Dominoes fucked around with this message at 22:51 on May 22, 2013 |
# ? May 22, 2013 20:07 |
|
Thanks for the links, I'll check them out and send them to him when I get out of work in 3 more painful hours
|
# ? May 22, 2013 20:09 |
|
Dominoes posted:The official QT docs describe the properties of each GUI element without explaining what they do. What docs are you using? The PySide documentation is pretty nice. Here is an example.
|
# ? May 22, 2013 22:06 |
|
accipter posted:What docs are you using? The PySide documentation is pretty nice. Here is an example. The docs are thorough, but there's no available tutorials on par with Codeacademy, and figuring out how to structure your documents, and how the QT code fits with python takes work. Dominoes fucked around with this message at 22:38 on May 22, 2013 |
# ? May 22, 2013 22:24 |
|
Hammerite posted:So now suppose I want to do it like this: Welp, got myself into this. First of all, check out the slides from david beazley's talk on coroutines. http://dabeaz.com/coroutines/Coroutines.pdf Read at least through slide 75. If you don't want to read that far then don't use coroutines. The code used in his presentation is available at http://dabeaz.com/coroutines/ for you to follow along. He's also got a handy decorator that keeps you from having to do setup on your coroutines. For your question about where to put the keyword arguments the answer is probably both places. It sounds like you're creating a coroutine to handle the data from your private API and a convenience function for your public API. Here is an example of how to setup a proper processing pipeline w/ coroutines that does most of what you need. You can wrap what is in my main up into a convenience function. Python code:
Python code:
code:
Dren fucked around with this message at 22:37 on May 22, 2013 |
# ? May 22, 2013 22:27 |
|
Moving everything but the body of the try blocks to a decorator would make the coroutine-based code look pretty similar to regular functions.
|
# ? May 23, 2013 00:03 |
|
evensevenone posted:Use the tempfile module for temporary files. It'll handle creating a temp file/folder in a safe, user-writable place, not overwriting anything, and cleaning it up when done. For example, Python code:
|
# ? May 23, 2013 00:17 |
|
Dominoes posted:Is there a way to use this with strings? Just work with tempfile.NamedTemporaryFile() and look at the name attribute for its name. This might not be a meaningful name, but that shouldn't make a difference because it is a temporary file.
|
# ? May 23, 2013 00:31 |
|
accipter posted:Just work with tempfile.NamedTemporaryFile() and look at the name attribute for its name. This might not be a meaningful name, but that shouldn't make a difference because it is a temporary file. Python code:
Example code using a temp directory I made. Dir is a global variable that represents the program's path. empty_temp is a function that deletes all .pdf files in my temporary directory. Omitted some unnecessary-for-example code. Is it possible/worthwhile to use the tempfile module for this case? Python code:
Dominoes fucked around with this message at 00:47 on May 23, 2013 |
# ? May 23, 2013 00:40 |
|
Dominoes posted:Like this? Like this: Python code:
|
# ? May 23, 2013 00:55 |
|
|
# ? May 20, 2024 10:43 |
|
accipter posted:Like this: Python code:
Dominoes fucked around with this message at 02:55 on May 23, 2013 |
# ? May 23, 2013 02:26 |