|
How do you run unit tests? I get errors about import directories and such, both from PyCharm, and from the terminal. I nav to the directory, then run in terminal: 'python -m unittest tests.py'. I receive ImportError: cannot import name 'my module', for any imports within my program. I can fix this by changing my imports in all files from from module import file to import file, but this breaks actually using the module vice testing it. Pycharm's unit test Run feature breaks in the same way. Any ideas? Using Django's unittests introduces a whole other world of DB errors... Not even going there! It looks like most projects on Github use third party tools, but I'm trying to do this straight from Python's docs.
|
# ? May 7, 2016 14:51 |
|
|
# ? May 9, 2024 09:44 |
|
Have you marked all your source directories as such in the PyCharm project? If you do, PyCharm will add the source directories to the Python path when running your tests. It could be a legitimate issue with your package structure and how you import modules from one another. Is this source posted anywhere so we can see how you've arranged things? Also, by "file" do you mean you're importing some class/object/whatever called "file" from a module, or that you're importing a file from a module? Because if it's the latter, you may need to back up a bit.
|
# ? May 9, 2016 14:52 |
|
Loving Africa Chaps posted:It wasn't and might explain my cool new errors. Got some more reading to do i guess The method probably looks something like Python code:
Anyway, both string and tuple are enumerable, so that for loop will happily consume either, member by member, so it will try to bind every character in a naked string (or code point in Python 3, I guess). The reason you need the (thing,) construct to pass a tuple instead of a string is that that's the special case syntax to create a one-element tuple. An empty tuple would just be empty matching parens. I would be a little surprised if a list didn't work just as well and the syntax there is a little more intuitive: [thing].
|
# ? May 9, 2016 15:27 |
|
GameCube posted:Have you marked all your source directories as such in the PyCharm project? If you do, PyCharm will add the source directories to the Python path when running your tests.
|
# ? May 9, 2016 16:17 |
|
Dominoes posted:I didn't have it marked in PyCharm, but marking it doesn't appear to help. I think I have it arranged in a normal way. Github of an example. By file - let's say I have a normal python file. It imports another, with 'from module import file' or 'from . import file'. This is the line that breaks the unit tests. Changing it to 'import file' fixes it for tests, but breaks it for normal use. A "module" is a .py file. A "package" is a folder with an __init__.py file. Say I want to use your saturn package. I can import the whole package: pre:import saturn x = saturn.today() pre:from saturn import today x = today() pre:import saturn.saturn x = saturn.saturn.today() pre:from saturn.saturn import today x = today()
|
# ? May 9, 2016 17:34 |
|
GameCube posted:So by 'from module import file' do you mean 'from package import module'? It sounds to me like you're either missing something in your __init__.py, or you need to add something to your Python path (which the PyCharm suggestion might have helped with, if that were the case). Dominoes fucked around with this message at 18:01 on May 9, 2016 |
# ? May 9, 2016 17:55 |
|
The top answer here should be what you need, then. The build-in Python unittest module also does the sys.path fuckery, because your unit tests should be written like any other code that might use your package.
|
# ? May 9, 2016 19:42 |
|
Thanks; you solved it! The solution was, as described in that SO answer, to move tests.py to a separate tests folder. Running the tests from that folder (either by just running the script, or using 'python -m unittest') works; still can't get Pycharm to run them for me though.
Dominoes fucked around with this message at 19:55 on May 9, 2016 |
# ? May 9, 2016 19:53 |
Hi. I'm trying to learn NumPy as a replacement for MATLAB. I'm implementing an image processing routine I wrote that detects copy-paste forgeries. image is an mxnx3 array of unsigned 8-bit integers. mask is an mxn array of booleans. separator is an mx8x3 array that's BLUE (i.e, each pixel is [0, 0, 255]) I want to create a masked image that is the original image on the left, the blue separator, and a psuedo-grayscale version on the right, eg: But I'm still having some trouble with basic array manipulation. Here's what I did in MATLAB. How would I do this in numpy? code:
Eela6 fucked around with this message at 20:38 on May 10, 2016 |
|
# ? May 10, 2016 19:31 |
|
sympy, numpy, and toolz have 3 very different functions all called diff!
|
# ? May 10, 2016 21:50 |
|
Eela6 posted:Hi. I'm trying to learn NumPy as a replacement for MATLAB. I'm implementing an image processing routine I wrote that detects copy-paste forgeries. What problems are you running into, specifically? Can you post your Python code? First thing I can point out is that Python (and most other languages) starts numbering from 0, rather than 1. So RED, GREEN, and BLUE should be 0, 1, 2 rather than 1, 2, 3 Matlab also tends to treat array shapes oppositely from Python; check the shape of your arrays by printing maskedImage.shape or inspecting it with a debugger. It really depends on what modules you're using to read your data Python uses square brackets [ ] instead of parens ( ) for array indexing. Numpy will keep things in uint8 if you're dealing with just uint8 arrays, whereas Matlab sometimes converts things to double unexpectedly. Be careful that you're not accidentally creating an overflow There are several equivalent functions for rgb2gray (there are many equally valid ways to convert from rgb to grayscale) but if you just want something quick and easy you can just take the average along the rgb axis: maskedImage_gs = maskedImage_rgb.mean(axis=0) or axis=2 depending on how the array was created I've never used bmat but it looks like it deals primarily with matrices, like you said. Numpy actually distinguishes between matrices and arrays, and you almost definitely want an array. If you're just trying to build a rectangular image of 3 inputs you can use concatenate, vstack, or hstack (all part of numpy). Python code:
QuarkJets fucked around with this message at 00:19 on May 11, 2016 |
# ? May 11, 2016 00:04 |
|
Here's also a helpful cheat sheet for converting between Matlab and Python syntax: http://mathesaurus.sourceforge.net/matlab-numpy.html
|
# ? May 11, 2016 00:06 |
QuarkJets posted:What problems are you running into, specifically? Can you post your Python code? QuarkJets posted:Here's also a helpful cheat sheet for converting between Matlab and Python syntax: Thank you for your advice. I am aware of the MATLAB-numPy cheat sheet & looked there first. Edit: No I didn't. I was aware of ~a~ MATLAB-numPy cheat sheet. Let me do some digging in this one. I seem to have the hang of 2d arrays in numpy (though I am still making the occasional off-by-one error adjusting to the zero-based indexing). My problem is that I don't seem to understand how to deal with 3+ dimension arrays. EG, code:
code:
code:
Eela6 fucked around with this message at 03:54 on May 11, 2016 |
|
# ? May 11, 2016 03:40 |
|
Eela6 posted:
The line I marked with ** needs to be code:
|
# ? May 11, 2016 04:00 |
|
Eela6 posted:
Happy to help. Your questions aren't silly, it's good to get syntax help when learning a new language I think what SurgicalOntologist posted should be enough to get you where you're going, just a small syntax change fixes your toy code. But I also noticed this: Python code:
Python code:
|
# ? May 11, 2016 07:28 |
|
Dominoes posted:How do you run unit tests? I get errors about import directories and such, both from PyCharm, and from the terminal. Put your tests in a sibling folder of your main project source folder and run `python -m unittest <your_test_dir> discover` from the root folder. Edit: beaten.
|
# ? May 11, 2016 07:53 |
Thank you for your help, everyone! I think I've got it.code:
Edit: I was able to fully re-implement my program. Thanks again for the help! Eela6 fucked around with this message at 20:34 on May 12, 2016 |
|
# ? May 11, 2016 17:28 |
|
Does anyone happen to know any good resources for asynchronous programs in Python? I'm working in a command line twitter client and I'd like to add some features that exist on the web version - for example, I'd like the be able to load tweets from various timelines and lists and read user input to the terminal as well as render any changes to the state (such as adding new tweets). I currently process things by loading tweets at the start and when a certain key combination is pressed. However, this is slow and not particularly user friendly. I'd love to be able to push tweets into my process but I don't know how to handle doing that AND responding to user input at the same time. My thought is to have a few processes running that all pipe any 'events' they receive into a queue that the main terminal can read from. However, the tutorials I've found on Queues and async in general for Python have been extremely simplistic and don't take into account this sort of continuous reading. Maybe I'm just missing something?
|
# ? May 14, 2016 07:35 |
|
IT BEGINS posted:Does anyone happen to know any good resources for asynchronous programs in Python? Sounds like you're looking for gevent: http://www.gevent.org Alternatively there's Celery but that sounds like overkill.
|
# ? May 14, 2016 13:00 |
|
Any recommended 'Pandas 101' books/tutorials? Doing some basic data manipulation in Excel, curious to see how Pandas matches up against it.
|
# ? May 15, 2016 05:33 |
|
I'm working on creating a project for the class I TA, and I'm not quite understanding what's going on with my dictionaries. My code reads in some files of EPA fuel test data, creating individual dictionaries where the keys are the manufacturers and the values are a list of [city_mpg, highway_mpg, combined_mpg, num_vehicles] and combines them into one larger dictionary:Python code:
Edit: This only happens in PyCharm; I tried using Spyder (which I hate), and it worked fine. Fantastic. Edit 2: The problem also happens on the command line. Super weird. hooah fucked around with this message at 14:25 on May 15, 2016 |
# ? May 15, 2016 14:09 |
|
Stringent posted:Sounds like you're looking for gevent: Thanks for the help. I ended up going towards the threads route for now, but the docs for gevent were very useful for figuring out the right architecture.
|
# ? May 15, 2016 14:27 |
|
hooah posted:The problem is that when the last line runs, eighties_dict gets changed. I've verified that master_dict and eighties_dict are different objects by both printing their IDs and printing the result of master_dict is eighties_dict. Why is this happening? When you run master_dict.update(eighties) it doesn't copy the values as new objects, it adds a reference to the original object Python code:
As for why it works in one thing and not another, that would come down to the environment right? Python version and any libraries you're using. Maybe one of them is making copies somewhere along the line
|
# ? May 15, 2016 16:55 |
|
Instead of passing objects to functions in order to be mutated, have them return a new object.
|
# ? May 15, 2016 17:09 |
|
Ah, I wasn't aware that update() is a shallow copy; thanks.
|
# ? May 15, 2016 19:10 |
|
IT BEGINS posted:Does anyone happen to know any good resources for asynchronous programs in Python? Python 3 has asyncio built-in.
|
# ? May 15, 2016 22:19 |
|
i know python hates functional programming, but is there really no filtermap in the standard library?
|
# ? May 17, 2016 01:43 |
|
the talent deficit posted:i know python hates functional programming, but is there really no filtermap in the standard library? x = [f(y) for y in c if y < 2] ?
|
# ? May 17, 2016 01:53 |
|
the talent deficit posted:i know python hates functional programming, but is there really no filtermap in the standard library? filter() map() reduce()???
|
# ? May 17, 2016 02:04 |
|
tef posted:x = [f(y) for y in c if y < 2] ? And you can even do this lazily with a generator comprehension. Tigren posted:filter() map() reduce()??? And these with the itertools versions! Not to mention getting into the toolz stuff.
|
# ? May 17, 2016 17:40 |
|
I am now trying to package my GUI as an .exe using the py2exe package. However I have encountered the following error:Python code:
|
# ? May 17, 2016 19:39 |
|
I don't know the answer to your quesiton, but my hunch is that the zip file is the problem; try extracting library.zip and pointing to the contents rather than stuff inside of the zip file e: unless you're supposed to create a .zip, or maybe the module did that? This exe creation business is so weird
|
# ? May 17, 2016 19:43 |
|
QuarkJets posted:I don't know the answer to your quesiton, but my hunch is that the zip file is the problem; try extracting library.zip and pointing to the contents rather than stuff inside of the zip file The py2exe package creates the library.zip folder as part of the dist folder (where to my understanding the dist folder contains all the files necessary to run the exe).
|
# ? May 17, 2016 20:42 |
|
The folder on the desktop changes name in your output from my_gui to tfsp_gui. Is that in the original output or did you change that when copying/pasting ?
|
# ? May 17, 2016 22:40 |
|
GameCube posted:The folder on the desktop changes name in your output from my_gui to tfsp_gui. Is that in the original output or did you change that when copying/pasting ? No, that's my fault when copy pasting, it should read: Python code:
|
# ? May 18, 2016 00:30 |
|
py2exe has been temperamental for me when packaging anything but really simple libraries. It's inelegant, but you can pass zipfile=None to your setup() call and it'll bundle everything into your exe itself instead of a library.zip archive.
|
# ? May 18, 2016 19:03 |
|
Reformed Pissboy posted:py2exe has been temperamental for me when packaging anything but really simple libraries. It's inelegant, but you can pass zipfile=None to your setup() call and it'll bundle everything into your exe itself instead of a library.zip archive. I found this suggestion in a StackOverflow post as well and tried it, but I still get the same error message. I guess I will try using cx_freeze instead as I cannot find anything online that points me to how to solve this issue. EDIT: I have managed to get cx_freeze to work, but I have another non-related general question. On this page, there is the line Python code:
Jose Cuervo fucked around with this message at 21:57 on May 18, 2016 |
# ? May 18, 2016 19:25 |
|
Thermopyle posted:Python 3 has asyncio built-in. Thanks for the link. I've been spending the last couple of days understanding it, helped in particular by this code. Is there a way to somewhat easily convert regular non-asyncio code to asyncio? Posts like this one lead me to believe there's not much I can do other than convert most everything in my codebase into a coroutine if I want to go in that direction. Maybe I'm tackling the wrong problem overall? I've started looking into this because I have a program that redraws the screen a bunch and adding a thread to control some extra stuff like making push notifications to my main thread has caused my screen rendering to drop in speed significantly (to the point where I can visually identify the re-render). Should I just be using processes to handle this problem?
|
# ? May 19, 2016 07:41 |
|
Why does Pycharm inspect spelling by default? Doesn't make sense to me!
|
# ? May 19, 2016 17:20 |
|
|
# ? May 9, 2024 09:44 |
|
Yeah I hate that, I always disable spellchecking in both code and comments (because sometimes my comments contain tiny bits of code mid-sentence!)
|
# ? May 19, 2016 18:57 |