|
onionradish posted:Unrelated to your issue, but something to be aware of because things like this can really bite you in the butt later: I agree, I was only using the word "map" here because the tutorial I initially based things off of used it. A pretty terrible practice which I'm glad to get away from. I've been implementing a GameMap class, but something odd happens with the indexing when I play around with it in the interpreter. Here's the class: Python code:
Python code:
list_of_maps[0][1][x][y].foo works to give me the info on a specific x, y coordinate. The first index is the GameMap object in the list_of_maps, that's clear, but I don't know what the second value is. That second index, if I put 0, just returns 0, and if I put 1, it returns the entire 2D array of map tiles so I think it's the map array. If I put 2 it is out of bounds. What the heck is that second value indexing? Writing: list_of_maps[0][1][0][0] and list_of_maps[0].level[1][0][0] both refer to the exact same map square at (0, 0) but I don't get why the self.level thing needs the [1]. Edit: Is it the self.level variable in the GameMap object? I didn't know those could be indexed. vvvvv I think you're right, make_surface_map was returning the map index as well as the new map. reading fucked around with this message at 19:22 on Jan 3, 2015 |
# ? Jan 3, 2015 19:11 |
|
|
# ? May 9, 2024 18:22 |
|
I'm going to guess the problem is wherever you construct the map. For some reason make_surface_map is returning [0, actual_map]. You may also consider using numpy for your maps, if you're doing any math with them you'll find it invaluable. If you're not it's probably overkill, the only advantage being that it will allow you to do indexing like game_map[0, 0] or even game_map[:, 0] (first column) which is a bit trickier with just a list of lists.
|
# ? Jan 3, 2015 19:18 |
|
Trying to solve one of the problems on Google Foobar and its failing all the tests. Heres the problem:quote:Origins and order And here is my code: Python code:
Any insight?? Am I missing something?? Has anyone done these before? edit: One thing that I might be missing is that for certain months, the days are not going to be 31, like for Feb and April for instance. So there is no 02/31/20. That might actually be a test case that I am failing. Tacos Al Pastor fucked around with this message at 06:26 on Jan 4, 2015 |
# ? Jan 4, 2015 06:11 |
|
I think a function that checks if a date is a valid date might be very helpful here.code:
|
# ? Jan 4, 2015 11:28 |
|
Yeah your logic doesn't look right - try taking your first test case (19, 19, 3) and reading through the code to see where that set of inputs takes you. Then change it to (12, 12, 3) - which is completely ambiguous - and see where that leads you in the code path. It's a good idea to write more tests! Think of a few examples of all the awkward situations you'd need to test for (you've already been doing that in writing your code) and add them as tests, so you can be sure everything's working the way you think it is. And that way, when something fails you can debug it and find out exactly why it's tripping up Also you spelled 'ambiguous' wrong in your return statement, so it's technically not producing the required output for those cases
|
# ? Jan 4, 2015 12:47 |
|
Is there a solution much cleaner than this? Is there any decent publicly available page with teasers like these?Python code:
Begall fucked around with this message at 17:49 on Jan 4, 2015 |
# ? Jan 4, 2015 17:46 |
|
Begall posted:Is there a solution much cleaner than this? Is there any decent publicly available page with teasers like these? Well, the problem statement says you can ignore leap years. I would hope this is because there are no test cases where it matters, but if there were a datetime based solution would be "wrong" for getting leap years correct. You also don't correctly handle the case (19, 19, 3), which is unambiguous. You could cut down on checks by sorting the input values before checking for valid dates, since we know that for an unambiguous date, month <= 12, 12 < day <= daysinmonth, year > daysinmonth. So the date is ambiguous if our middle value (day) is a valid month and not equal to the smallest value, or our highest value (year) is a valid day and not equal to the middle value. A solution taking advantage of the no leap years clause: Python code:
|
# ? Jan 4, 2015 19:00 |
|
Any things that can cause sorted() not to work as expected?Python code:
This works as expected: Python code:
Dominoes fucked around with this message at 21:34 on Jan 4, 2015 |
# ? Jan 4, 2015 21:23 |
|
supercrooky posted:
This looks great. Sorting it actually simplifies it a lot now that I looked at your solution and sorting it doesn't really affect the result of the date or whether its ambiguous.
|
# ? Jan 4, 2015 22:17 |
|
Dominoes posted:Any things that can cause sorted() not to work as expected? Can you describe your problem a little more clearly? That first chunk of code is just a list of dictionaries with no output shown
|
# ? Jan 5, 2015 07:42 |
|
Oops, this: sorted(x, key=lambda i: i['profit']) was meant to be inserted between the two lists. The second list is the output. The second code block demonstrates a simplified example of what should be the same logic, working. Sorted()'s not doing anything, and my only explanation is 'floats are weird'. I thought it might be because I tried to round the 'profit' values early, hence the weird 0-trailing decimals, but it turns out that's not it. Dominoes fucked around with this message at 07:55 on Jan 5, 2015 |
# ? Jan 5, 2015 07:52 |
|
Your issue is probably that your input includes NaNs, which are explictly defined as being unordered with respect to other floats. Try it:code:
|
# ? Jan 5, 2015 08:15 |
|
Yeah, floats aren't really weird, but NaN is weird.
|
# ? Jan 5, 2015 09:26 |
|
Hey, looks like y'all are right about nan, and Mike's explanation makes sense. Using sum() and len() instead of np.mean() to generate 'profit' fixed it.
Dominoes fucked around with this message at 20:43 on Jan 5, 2015 |
# ? Jan 5, 2015 20:10 |
|
What would be the easiest way to run a periodic (once every 24 hours) task within a flask app. It's literally 3 lines of code so I wouldn't want to use something advanced like celery
|
# ? Jan 6, 2015 02:28 |
|
superstepa posted:What would be the easiest way to run a periodic (once every 24 hours) task within a flask app. It's literally 3 lines of code so I wouldn't want to use something advanced like celery What are you trying to do? If it's something super simple I'd probably just set up a cron job.
|
# ? Jan 6, 2015 02:31 |
|
EAT THE EGGS RICOLA posted:What are you trying to do? If it's something super simple I'd probably just set up a cron job. I just want to use beautfulsoup to scrape data from a table once every 24 hours. I want it to work on windows and I'd like to have all the code in python within the app but at the moment cron does seem like the best option
|
# ? Jan 6, 2015 03:10 |
|
There's a Windows cron equivalent. Google "windows 7/8 schedule task." There will probably be a way to schedule it programmatically if that's what you're looking for.
|
# ? Jan 6, 2015 04:16 |
|
What's this thread think about threading vs multiprocessing vs pathos? I'm trying to count a large number of AWS S3 objects (in the order of millions) as quickly as possible. I started out with threading and then discovered that each thread wasn't run concurrently due to the Global Interpreter Lock and the implementation of threads. I'm playing around with multiprocessing now and through some debugging and problems pickling, I discovered pathos. I thought I'd find out what you guys thought before I went and (possibly) wasted more time.
|
# ? Jan 8, 2015 01:03 |
|
I just wanted to stop in and say that I've been using mypy for the last week (I've just played with it previously) and it's pretty super if you've thought that Python could use option static type checking. I won't use it all the time, but for larger programs its pretty nice (well it's still experimental so I won't use it for larger programs either yet).
|
# ? Jan 8, 2015 16:54 |
|
Thermopyle posted:I just wanted to stop in and say that I've been using mypy for the last week (I've just played with it previously) and it's pretty super if you've thought that Python could use option static type checking. Dominoes fucked around with this message at 19:40 on Jan 8, 2015 |
# ? Jan 8, 2015 19:32 |
|
Thermopyle posted:I just wanted to stop in and say that I've been using mypy for the last week (I've just played with it previously) and it's pretty super if you've thought that Python could use option static type checking. It's an interesting idea, but seems to go against a lot of pythonic practices, especially the centrality of the dict or list with non-uniform types. Dominoes posted:I like how the type signatures provide a quick insight of what the function's doing. It's especially useful when inputting or outputting complex/nested data structures. While that can be a plus when things are not otherwise well documented, but it doesn't replace proper documentation. I've been doing a fair bit of scala programming lately and it's amazing how many library authors think "type safe" means "don't have to write documentation."
|
# ? Jan 9, 2015 04:07 |
|
I'm having an issue with importing modules. classes.py: Python code:
Python code:
|
# ? Jan 9, 2015 04:16 |
|
You seem to have a circular dependency.
|
# ? Jan 9, 2015 04:23 |
|
That looks like the problem is a circular import, though in Python 3 those raise an ImportError rather than NameError but I assume that's a Python 2 thing. maincode relies on classes which relies on maincode etc. and the interpreter can't resolve that for you. There are cheap fixes (lazily import one of the modules inside a function where it's first needed) and there's the right way to do it, which is almost always to structure your code in a different way. A module called maincode should probably not be imported by anything else; it should probably only contain the script glue that sets all your other components in motion. If there's something that classes needs inside maincode, that something should probably go in a third module. This can get tricky, feel free to elaborate. e:f;b, and also don't get into the import * habit. It's much easier to type out classes. a few times than to inadvertently pollute your namespace or come back to your code later and wonder where the gently caress Foo comes from. KICK BAMA KICK fucked around with this message at 04:29 on Jan 9, 2015 |
# ? Jan 9, 2015 04:27 |
|
...or do from classes import SomeClass, SomeOtherClass and then every time you realize you need a new one stick it on the end of that import statement. (an alternative to from classes import *, not circular dependencies). I would say a reasonable circularity dependency solution is to take whatever piece of maincode that you're importing into classes and just stick it in classes. In general, "classes" vs. "not classes" is probably not a good way to split your code. SurgicalOntologist fucked around with this message at 04:41 on Jan 9, 2015 |
# ? Jan 9, 2015 04:38 |
|
Thanks, those are great ideas.
|
# ? Jan 9, 2015 04:58 |
|
In this case since MyClass is the only class you have to import, specifically say that, but even if you have multiple ones inside classes.py its just good practice to point out which one you want to use.
|
# ? Jan 9, 2015 17:53 |
|
I've tried to post this question multiple times, but it seemed like I came up with a new possible solution every time. I guess now I've diluted it to the essentials. Do I have to do this? code:
A similar method in a different place seems to work a little simpler: code:
|
# ? Jan 9, 2015 18:11 |
|
supermikhail posted:I've tried to post this question multiple times, but it seemed like I came up with a new possible solution every time. I guess now I've diluted it to the essentials. It'd be useful if we knew what we were looking at here. Can you show us an entire class definition? For instance: quote:Do I have to do this? Try calling type(answer) and seeing what kind of answer is being passed in. You probably don't need to use Decimal(str(...)) but you might need Decimal(). You definitely don't need to invoke str() on this line: code:
In order to know for sure whether you need to convert the value returned by OneOperandProblem.check to a string before invoking Decimal(), we'd first have to know how OneOperandProblem.check is defined, or at least what kind of value it returns. It's likely that you don't need to use str() there.
|
# ? Jan 9, 2015 19:55 |
|
Also, if you pass 80, 8.94 to either of the methods shown here, then the lines that looks like this:code:
|
# ? Jan 9, 2015 19:59 |
|
I am currently working on my first programming project ever, which is a tool for mining academic databases and automatically storing search matches in a local .txt file for later processing. I have no previous experience in programming whatsoever but after a LOT of reading and watching tutorials I managed to get an initial version of the script working. The script is very short and simple, but I will have to build a GUI for it since the potential users will very likely have no proficiency in programming. I have made some attempts at creating a GUI with Tkinter, but I'm finding it much more difficult to program than the main script. Is there an easier and faster way to build GUIs? What I'm attempting to create is akin to search form, with several search bars (the number of which is defined by the user), with a dropdown menu for search type besides each of them (e.g, "Topic", "Author", etc.), and another for boolean operators.
|
# ? Jan 9, 2015 22:40 |
|
Rlazgoth posted:I am currently working on my first programming project ever, which is a tool for mining academic databases and automatically storing search matches in a local .txt file for later processing. I have no previous experience in programming whatsoever but after a LOT of reading and watching tutorials I managed to get an initial version of the script working. The script is very short and simple, but I will have to build a GUI for it since the potential users will very likely have no proficiency in programming. There's a nice included WYSIWYG editor for it, but it's still a pain to learn. Dominoes fucked around with this message at 22:55 on Jan 9, 2015 |
# ? Jan 9, 2015 22:52 |
|
Whatever GUI library you use (seconding Dominoes) the most important thing is structuring your code to separate the user interface from the logic. Not doing that will bite you in the rear end the second you realize you need to refactor something. There's a few different patterns like MVC and MVP that you can implement to satisfy this but don't get hung up on which, just pick something that makes sense to you. Also, totally cool if you're just doing this to learn to code but just in case: is there something specific about your domain that makes writing a custom program a better solution to a problem that sounds like "search some databases" than just marshaling your data into a format usable by any of the myriad general-purpose database applications? KICK BAMA KICK fucked around with this message at 23:00 on Jan 9, 2015 |
# ? Jan 9, 2015 22:57 |
|
KICK BAMA KICK posted:Whatever GUI library you use (seconding Dominoes) the most important thing is structuring your code to separate the user interface from the logic. Not doing that will bite you in the rear end the second you realize you need to refactor something. There's a few different patterns like MVC and MVP that you can implement to satisfy this but don't get hung up on which, just pick something that makes sense to you. In part this is a good opportunity for me to learn programming, which I've wanted to for a while now, but there was also some demand at the office for this kind of script. The site that we want to extract publication data from actually has an API for that, but it only allows retrieval of basic data - Author and Title. Even the search results page on the site only shows limited information. In order to get the full publication metadata - which we need - you have to click a series of buttons on the search page which lets you save the data in a .txt file. Unfortunately, this option only lets you save up to 500 records at a time - and the searches we run usually yield upwards of hundreds of thousands of results. We actually had a research assistant scrap together a dataset by manually exporting something like 200.000 records, in batches of 500 at a time. The idea of this script is to automate the process by having the user just run his search, and the script will download as many batches are necessary, merge them together, and save the output in a format which can then be used in a statistics package. There might actually be an easier way to do this but I'm having a lot of fun programming so I don't mind. I completed the Python course on Codecademy before, but I think I'm learning much faster now that I'm actually working on something with a tangible result.
|
# ? Jan 10, 2015 02:22 |
|
I don't know if you're concerned about this, but it would be much more efficient to download the entire database (if that's possible) now, then download updates periodically.
|
# ? Jan 10, 2015 03:58 |
|
QuarkJets posted:Also, if you pass 80, 8.94 to either of the methods shown here, then the lines that looks like this: That could well be a newbie thing, but that checks that 'problem' and 'answer' are valid. OneOperandProblem is the superclass of several classes, each of which requires this operation (I think). For clarity: code:
QuarkJets posted:It'd be useful if we knew what we were looking at here. Can you show us an entire class definition? drat, I commented out the conversion of 'answer' into decimal and it works. (Type is 'Decimal'.) But I swear yesterday that broke the program... I don't even know what's going on anymore. But omitting "str" from code:
|
# ? Jan 10, 2015 07:51 |
|
supermikhail posted:That could well be a newbie thing, but that checks that 'problem' and 'answer' are valid. OneOperandProblem is the superclass of several classes, each of which requires this operation (I think). This is reasonable, I didn't expect you to be raising exceptions. However, a lot of Python programmers will frown at attempting to check the type of the input before doing something with it. Generally, the advice is to clearly document your function/method/whatever as expecting an integer (or something integer-like), and if a user provides something weird then it's on them. For instance, what if I wanted to be able to calculate the square root of a Decimal and check the answer? I'd pass Decimal(3.4) as the "problem" and your method would immediately raise an exception, because it's not an int, but otherwise I think that your code would work okay. quote:I don't think the entire class is going to be useful. It's a bunch of static methods which don't interact with each other. The one supremely confusing thing that I failed to clarify yesterday is above, and the class's name is RootProblem, so the idea of the method is to check that 'answer' is the correct root for the 'problem'. 'problem' is an int, by the way. That's because you're taking a float and then turning it into a Decimal. You need to work directly with Decimal objects instead. A lot of things in the math libraries will return a float. Have a look here: https://docs.python.org/2/library/decimal.html Here's an example of what you can do for this particular line: code:
|
# ? Jan 10, 2015 08:53 |
|
Uh... I'm too much of a noob for this thread. Okay, the input for the method comes from Django, and in fact the problem is randomly generated in the same model in a different method, so if the wrong types are submitted it's a bug I should fix... Aand I guess that's what tests are for. But for some reason it hasn't occurred to me previously... Or maybe not. Anyway, that's for the Django documentation. Thanks for that decimal info, I didn't catch that when I first learned about the module. Though I'm not sure why the second method in my original post works as intended (except I should re-converting 'answer' to Decimal). Presumably it compares a Decimal to a float in the end. supermikhail posted:
|
# ? Jan 10, 2015 11:01 |
|
|
# ? May 9, 2024 18:22 |
|
I'm still not clear what you're actually trying to do in general. Are you just writing a problem solver? How generic does it have to be? Do you want two operands? Three? n? What about operator types? It seems as though you're calling TwoOperandProblem.check() so it seems like you've at least got a handle on the fact that you want to check a problem with two operands, but what if someone passes three operands into this check method? You need to rethink your goal and then refactor the crap out of your code because it smells like you're trying to solve a problem that isn't actually the one you want to solve.
|
# ? Jan 10, 2015 14:42 |