|
How can I write to CSVs without getting a blank row between each real row? This page shows that the file needs to be written in binary mode to do this, but when I do it, I get the following error: 'TypeError: 'str' does not support the buffer infercace.'. Is there a way to convert a string to binary first? I've been able to work with CSVs successfully before when downloading raw CSVs from websites, which work in binary mode. Now that I'm trying to make my own to save data, I'm running into this. example: Python code:
edit: Solved. The correct open line is: Python code:
Dominoes fucked around with this message at 04:10 on May 11, 2013 |
# ¿ May 11, 2013 03:34 |
|
|
# ¿ May 9, 2024 01:19 |
|
I'm trying to delete dictionary entries based on values. I'm receiving "RuntimeError: dictionary changed size during iterations" errors despite making a copy first. Any ideas? Python3.Python code:
Dominoes fucked around with this message at 21:36 on May 11, 2013 |
# ¿ May 11, 2013 21:25 |
|
breaks posted:You aren't making a copy. mydict_copy = mydict means that you have two names pointing at the same object. OnceIWasAnOstrich posted:You aren't actually making a copy, just another reference to the same dict. Thanks. Replacing the first line with the following worked. code:
Python code:
Dominoes fucked around with this message at 22:13 on May 11, 2013 |
# ¿ May 11, 2013 22:07 |
|
Dren posted:I encourage you to carefully read this: http://docs.python.org/2/library/stdtypes.html#typesmapping Nippashish posted:You might as well just write mydict = {k:v for k,v in mydict.iteritems() if v.objectvalue != 0} and avoid this whole business of copying and deleting. QT issue: Anyone know how to pull data from input text and combo boxes? I've been trying to solve this one for weeks with no luck, and it's stopped development of the program. I can't find anything in a search that describes the problem, and most of what I find about text and combo boxes implies I'm doing it correctly. For text boxes, I receive a None result, and combo boxes I recieve the default selection, no matter what the current one is. Code: code:
Dominoes fucked around with this message at 16:20 on May 12, 2013 |
# ¿ May 11, 2013 23:14 |
|
PYQT question. I created a keypress event based on code I found in a google search. It works, but I don't know why. I added this function under my main window's class: Python code:
This function, keyPressEvent, is never called. Why does it work? Dominoes fucked around with this message at 02:08 on May 18, 2013 |
# ¿ May 18, 2013 02:04 |
|
So it's a special function name. I notice that changing its name makes it not work.
|
# ¿ May 18, 2013 03:45 |
|
What's the best way to get the current path a file is run from? I've been using abspath('')+ '\\folder', but this relies on the folder name not changing. Is there a way to get the actual path of the file, instead of one level up? (which abspath() seems to do) Using __file__ with dirname seems to work, but it causes cx_freeze to glitch out. Dominoes fucked around with this message at 23:49 on May 19, 2013 |
# ¿ May 19, 2013 23:46 |
|
BeefofAges posted:os.getcwd()? edit: Looks like abspath()'s doing the same thing cwd did. Oops - doublebroke. Perhaps dirname(__file__) is the answer, and I just need to troubleshoot the cx_freeze error. Dominoes fucked around with this message at 00:14 on May 20, 2013 |
# ¿ May 19, 2013 23:52 |
|
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 |
|
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 |
|
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 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 |
|
Thank you.
|
# ¿ May 22, 2013 13:22 |
|
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 |
|
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 |
|
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 |
|
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 |
|
accipter posted:Like this: Python code:
Dominoes fucked around with this message at 02:55 on May 23, 2013 |
# ¿ May 23, 2013 02:26 |
|
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 |
|
edit: nvm
Dominoes fucked around with this message at 02:49 on Jun 1, 2013 |
# ¿ Jun 1, 2013 02:41 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
Dren posted:Dominoes, I noticed you have the idea that you should write as little code as possible. That's not quite the right idea. The right idea is to avoid reusing code, not to write the shortest possible syntactically correct code. It's often better to write a little more code for the sake of clarity. e.g. os.path.join(dir, "filename") as opposed to dir + "filename". Not to mention that in the former case you get cross platform compatibility. For example: Python code:
|
# ¿ Jun 7, 2013 17:13 |
|
Thermopyle posted:
Dren posted:Pardon my language, but you have this rear end backwards. The appropriate way to approach making code more succinct is to stick to one or two operations per line the way Thermopyle showed, not by creating custom aliases for operations so that they take up less visual space on the line.
|
# ¿ Jun 7, 2013 21:34 |
|
Is there a trick to installing Numpy? I tried PIP, but get a list of errors, the final being "Command python setup.py egg_info failed with error code 1". Googling this brings up links to mostly Mac issues, but I'm using Windows/Python 3. edit: Got it working with a binary from this website. Kind of surprised Pip didn't work - it's been great so far. Dominoes fucked around with this message at 00:52 on Jun 9, 2013 |
# ¿ Jun 9, 2013 00:43 |
|
The Gripper posted:A friend of mine is currently in a job where his team leader uses sloc as a performance metric, giving pretty graphs straight to upper management. Currently nobody on his team is allowed to make changes to existing lines of code at all because if they do it's reported as "cosmetic changes" or "replacing existing working code" and looked on negatively, so their codebase in some parts has 5-10 copies of the same code smashed together to avoid touching existing variables/commenting anything out.
|
# ¿ Jun 9, 2013 14:52 |
|
fritz posted:Until PEP8 goes in the parser, you can use whatever names you drat well please. Dominoes fucked around with this message at 01:42 on Jun 12, 2013 |
# ¿ Jun 12, 2013 01:29 |
|
Jon Joe posted:Could I, perhaps, get a better tutorial?
|
# ¿ Jun 12, 2013 03:12 |
|
Do you need access to a machine running the target OS when making a binary? I've been making Windows x64 files using CX_freeze, but ideally would like mac, linux and win-32 ones. I got my program to work on Ubuntu running from source - everything works. (although the GUI fonts are too big for their buttons - easy fix) I looked up making a .deb file, and the instructions were complex, and seemed to require being on Linux to do it. It looks like to create a Mac version, I need to be on a Mac. For a 32-bit Win version, I think I can do it from a 64-bit OS, but need a 32-bit installation of Python. Is this accurate, or is there a way to make different binaries without jumping on different computers/OSes/versions of Python?
Dominoes fucked around with this message at 22:19 on Jun 12, 2013 |
# ¿ Jun 12, 2013 21:50 |
|
accipter posted:Why have you decided to distribute a compiled version of your program? Dominoes fucked around with this message at 22:21 on Jun 12, 2013 |
# ¿ Jun 12, 2013 22:18 |
|
Dren posted:If your intended result is cx freeze binaries packaged natively for various operating systems you will most likely need to get access to those platforms. Cross compiling is difficult. Cross-creating packages seems incredibly hard, if not impossible, since you generally need a platform specific toolchain to do that sort of thing. Besides, you'll need those access to those platforms in order to test the packages you create. Dominoes fucked around with this message at 22:36 on Jun 12, 2013 |
# ¿ Jun 12, 2013 22:34 |
|
madkapitolist posted:I'm trying to grab a column of values from a csv file, then pass those values as a parameter through to a url (my example url is madkapitolist.com). Then I want to hit those url's. I'm not clear on what you're asking, so here's a guess. Are you trying to copy text from the url? Download a file? JSON/XML data? Python code:
Dominoes fucked around with this message at 22:24 on Jun 16, 2013 |
# ¿ Jun 16, 2013 22:18 |
|
Jon Joe posted:I've learned the basics of python, how do I go about expanding my knowledge? Specifically, how do I learn more functions? For example: I started python (and programming in general) a few months ago. I learned the basics from the Codeacademy interactive tutorial. I decided I wanted to make a stock screener. I learned how to apply what I learned from Codeacademy, how to use Oauth authentication, how to use dates and times, HTTP requests, managing CSV and JSON data, and how to save data. When I later decided to make it automated, I learned how to run programs in continuous loops, how to use FIXML, when to split into multiple files, how to use exception handling, and how to set up built-in-tests. I decided I wanted to make a program to simplify downloading items from the internet. I learned how to merge PDF files, how to code a GUI, and how to compile code in a standalone program. I recently decided I'd like to make an instrument tuner and metronome, so I'm learning how to use arrays, FFT algorithms, sampling etc. I got a lot of help here and from Stack overflow.
|
# ¿ Jun 17, 2013 02:45 |
|
I looked up logarithms in the python docs, where it's listed as 'math.log(x[, base])' It seems like the actual syntax is 'math.log(x, base)', as the above doesn't work, and the docs later refer to 'math.log(x, base)'. What are the docs conveying with the bracketed notation?
|
# ¿ Jun 17, 2013 13:57 |
|
So "[, base]" means "either 'base' or blank" ? Thanks. Dominoes fucked around with this message at 14:26 on Jun 17, 2013 |
# ¿ Jun 17, 2013 14:15 |
|
I can't recommend LFD.UCI's pythonlibs website enough for Windows users. I recommend putting it in the OP. I've spend a lot of time over the past few days troubleshooting how to install PyAudio from source. (No win64 binary on their official page; 32 only) I ran into many errors, as always. Found a precompiled 64-bit version on this website; headache over.
Dominoes fucked around with this message at 14:54 on Jun 22, 2013 |
# ¿ Jun 22, 2013 14:51 |
|
|
# ¿ May 9, 2024 01:19 |
|
Is there a good reason the datetime module has a type named datetime? It seems confusing. Why not name the type date_time, or something else that isn't the exact name of its parent?
|
# ¿ Jun 25, 2013 02:56 |