|
Hammerite posted:Is it considered poor form to use the fact that loop variables are still set after the loop? I'm not sure about that exact situation, but it's not utterly uncommon for me to break out of a loop and use the last set value of the loop. What's happening isn't completely explicit, either, so I don't do it all the time.
|
# ? May 14, 2013 23:02 |
|
|
# ? May 20, 2024 07:27 |
|
During my learning pursuits I got a brilliant idea of splitting part of rarely-used code (namely, the random level generator) into a module, partially to make browsing the code more convenient and partially just to learn. It resulted in a horrible clusterfuck of shamefully bad code. Before I try to tackle it again, there are some things that confused me (probably mainly due to my lack of experience with coding at all): 1) Do I have this right: to read a variable from another module I have to simply import it and slap moduleName. before it. To actually change the imported variable I have to slap "global" on it in the source module? 2) If I do the "from myModule import *", everything from the first point still applies to variables, except I'm spared the effort of writing moduleName. before them? What would happen if I did it while loving up and having a variable that's named exactly the same in each file? 3) How do classes and their instances work with modules? If I define a class and its particular instance, what do I need to set up to read one of that instance's attributes in another module? 4) Do I have this right: unless there's some magic involving the aforementioned instances, the only sane/proper way of sending some data to module and getting back some processed data without delving into some horrible circular import hell is to put everything we want into the return statement of the imported function. Dumping the results into a .txt file doesn't count.
|
# ? May 15, 2013 00:14 |
|
Lichtenstein posted:1) Do I have this right: to read a variable from another module I have to simply import it and slap moduleName. before it. To actually change the imported variable I have to slap "global" on it in the source module? Lichtenstein posted:2) If I do the "from myModule import *", everything from the first point still applies to variables, except I'm spared the effort of writing moduleName. before them? What would happen if I did it while loving up and having a variable that's named exactly the same in each file? Lichtenstein posted:3) How do classes and their instances work with modules? If I define a class and its particular instance, what do I need to set up to read one of that instance's attributes in another module? Lichtenstein posted:4) Do I have this right: unless there's some magic involving the aforementioned instances, the only sane/proper way of sending some data to module and getting back some processed data without delving into some horrible circular import hell is to put everything we want into the return statement of the imported function. Dumping the results into a .txt file doesn't count.
|
# ? May 15, 2013 01:41 |
|
QuarkJets posted:If by "pretty" you mean "readable" then shouldn't that be part of "make it work?" Most scientific programming is done in the style of "I'm going to get this to work, I don't care if it's fast or readable", and it's actually a huge problem when a change to the code needs to be made but the entire house of cards falls apart because the code has turned into a black box and no one knows what makes it work It can also have political implications. The climate gate frame up partly revolved around a series of inexperienced coders with no experience in dealing with scientist written bad code declaring it MUST be dodgy because it was so "obfuscated". No kids, that's not obfuscated, it's just garbage quality fortran and entirely functional and works too. Also, don't call your model calibrations "fudge factors". Loons will skewer you for it.
|
# ? May 15, 2013 01:46 |
|
duck monster posted:series of inexperienced coders I think I recall Eric Raymond being amongst them. While not inexperienced, he has his own set of issues.
|
# ? May 15, 2013 02:16 |
|
Lichtenstein posted:4) Do I have this right: unless there's some magic involving the aforementioned instances, the only sane/proper way of sending some data to module and getting back some processed data without delving into some horrible circular import hell is to put everything we want into the return statement of the imported function. Dumping the results into a .txt file doesn't count. What does this even mean?
|
# ? May 15, 2013 02:20 |
|
Question for the thread: what if I have a module defined like this:Python code:
Lichtenstein posted:During my learning pursuits I got a brilliant idea of splitting part of rarely-used code (namely, the random level generator) into a module, partially to make browsing the code more convenient and partially just to learn. It resulted in a horrible clusterfuck of shamefully bad code. Before I try to tackle it again, there are some things that confused me (probably mainly due to my lack of experience with coding at all): Your module should consist of classes and/or functions that are used in randomly generating a level. Maybe some other module somewhere creates an instance of one of the classes (in which case it has "from mymodule import classname in it", or maybe it calls one of the functions, but you certainly don't need to be using global variables or polluting your namespace with "from module import *" For your last question, you could send some data to a class or function within the module, and then that class or function could hold or return the processed data. No circular import hell, no hard drive access, just simple calls. From your questions, it sounds like you are basically writing your modules as scripts and then trying to get them to call each other. Don't do that. Put all of the code in your module into classes and functions within that module, and then import those classes and/or functions when they're needed elsewhere. QuarkJets fucked around with this message at 05:46 on May 15, 2013 |
# ? May 15, 2013 05:35 |
|
Lichtenstein posted:2) If I do the "from myModule import *", everything from the first point still applies to variables, except I'm spared the effort of writing moduleName. before them? What would happen if I did it while loving up and having a variable that's named exactly the same in each file? You would clobber the existing variable in the module where the import statement is.
|
# ? May 15, 2013 09:22 |
|
Dren posted:This seems like a job for a heapsort. I'm not sorting it. Here, it will be more straightforward if I just post my actual code. http://pastebin.com/ikaisXAA The relevant function is getPatternDict().
|
# ? May 15, 2013 10:44 |
|
Thanks a lot, it's much more understandable now and I managed to make it work.
|
# ? May 15, 2013 12:44 |
|
Lichtenstein posted:4) Do I have this right: unless there's some magic involving the aforementioned instances, the only sane/proper way of sending some data to module and getting back some processed data without delving into some horrible circular import hell is to put everything we want into the return statement of the imported function. Dumping the results into a .txt file doesn't count. Are you asking how do you get data returned from your module? Because it's pretty much going to return data in the same way it would if it was in your code to begin with (with the additional module_name. before your call).
|
# ? May 15, 2013 13:30 |
|
QuarkJets posted:Is it considered poor form to keep variables (which I'm treating as constants) in the base of a module like this? This seems useful as it allows me to import the constants in other modules (from myModule import const1), and if I ever change those constants in the future then I only have to change a single line in a single module That sounds completely fine, so long as the constants are all fairly independent. Big groups of constants (such as, say, application settings) are better off being grouped together in a dict or namedtuple or something.
|
# ? May 15, 2013 14:18 |
|
Hammerite posted:At each step in the process, at least one of the elements of the dictionary should satisfy the if clause (otherwise the dictionary shall be considered badly-formed by definition). However, I don't know which one(s).
|
# ? May 15, 2013 15:45 |
|
Hammerite posted:I'm not sorting it. Here, it will be more straightforward if I just post my actual code. I think it's a little strange that you transform the dictionary in place but if you're after efficiency I suppose it makes sense. Then again, if you're after efficiency you probably ought to write it all in C. Any particular reason you have these pre-schema files instead of plain-ol' json schema?
|
# ? May 15, 2013 15:49 |
|
Dren posted:I think it's a little strange that you transform the dictionary in place but if you're after efficiency I suppose it makes sense. Then again, if you're after efficiency you probably ought to write it all in C. The choice to transform the dictionary in place was not based on anything in particular. The main motivation is the following note from http://json-schema.org/latest/json-schema-validation.html#anchor6 quote:Furthermore, given the high disparity in regular expression constructs support, schema authors SHOULD limit themselves to the following regular expression tokens: I wanted to write regular expressions using named subexpressions and without significant whitespace. The algorithm that prompted me to ask the question is related to the construction of a dictionary of named subexpressions which might initially refer to one another. These references first need to be resolved so that each subexpression stands on its own. It could happen that some subexpression references by name another subexpression that does not in fact exist, or that a circular relationship exists between subexpressions. Hopefully it is obvious that collections of subexpressions like this are no good. The whitespace thing is permitted in a primitive way by allowing the pattern property to be a list, which is ''.join()'d. I took the opportunity to add a few bells and whistles, like the ability to write (for example) "_INTEGER": [4, 8] and have it be interpreted as "type": "integer", "minimum": 4, "maximum": 8. A regular expression in a preschema: code:
code:
Hammerite fucked around with this message at 17:31 on May 15, 2013 |
# ? May 15, 2013 16:51 |
|
What's the preferred way to parse XML now? I'm seeing lxml, BeautifulSoup, etc being mentioned. I don't need a ton of features, just something that will let me extract values from an XML structure that I provide quickly and easily.
|
# ? May 15, 2013 20:15 |
|
Use lxml. It's good.
|
# ? May 15, 2013 20:22 |
|
Suspicious Dish posted:Use lxml. It's good. Wrong. It's great! I haven't done anything needing lxml-ish abilities in a few years until recently. Last time I did it I was using BS, this time I tried out lxml and am quite pleased.
|
# ? May 16, 2013 01:26 |
|
Followup question to what I was talking about before: what is the correct abstract base class to use for lists (in Python 3)? I was using Sequence as an abstract base class for lists, but then ran into a bug where my recursing function would go beyond the recursion limit. I eventually worked out it was because Sequence matches strings as well as lists, so I changed it to MutableSequence.
|
# ? May 16, 2013 18:09 |
|
Hey y'all, I understand that PEP8 is a guideline and all, but I was just wondering if there was an accepted way to fix this. I have a feeling this just may be one of those cases where I have to just let it be. I have lines in a code that is very long, and really can't be cut down too much further without affecting the readability of the var names. E.g.: Python code:
Again, I realize that PEP8 is a style thing, but I was just curious if there was some pythonic trick to avoiding this. I can't really see much of a workaround.
|
# ? May 16, 2013 18:35 |
|
JetsGuy posted:Hey y'all, I understand that PEP8 is a guideline and all, but I was just wondering if there was an accepted way to fix this. I have a feeling this just may be one of those cases where I have to just let it be. Why not just make a function? Python code:
|
# ? May 16, 2013 18:41 |
|
JetsGuy posted:I have lines in a code that is very long, and really can't be cut down too much further without affecting the readability of the var names. E.g.: This is another potential solution: Python code:
|
# ? May 16, 2013 18:45 |
|
JetsGuy posted:Hey y'all, I understand that PEP8 is a guideline and all, but I was just wondering if there was an accepted way to fix this. I have a feeling this just may be one of those cases where I have to just let it be. In addition to the ideas already posted, Python code:
|
# ? May 16, 2013 18:59 |
|
All doable ideas guys, thanks a ton.
|
# ? May 16, 2013 20:29 |
|
I got tasked to 'make a webserver to do (this stuff)', but as I'm basically a data scientist on a suddenly short-staffed team, I don't really know what I'm doing. The thing I came up with is to use cherrypy and multiprocessing, when a POST request comes in, the cherrypy code does some preprocessing and : code:
code:
This works fine, everything's great, except when wrapper() finishes, I'm still seeing heavy cpu and memory usage by looking at ps. For example, I sent off four POST requests (on an eight core machine) and this is what ps looks like, a half hour after the jobs finished: code:
* what am I doing wrong? * what's the right way to do this? EDIT: a couple hours later, the %CPU is down around 20% but the total CPU time hasn't budged. Is ps giving me unreliable information? fritz fucked around with this message at 03:46 on May 17, 2013 |
# ? May 17, 2013 02:07 |
|
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 |
|
Dominoes posted:PYQT question. I created a keypress event based on code I found in a google search. It works, but I don't know why. keyPressEvent() is called whenever you press a key. Read about keyPressEvent here. You might find it interesting to add "print e" to that method and see what happens.
|
# ? May 18, 2013 03:26 |
|
So it's a special function name. I notice that changing its name makes it not work.
|
# ? May 18, 2013 03:45 |
|
Dominoes posted:So it's a special function name. I notice that changing its name makes it not work. It is a class method that you are overloading.
|
# ? May 19, 2013 02:08 |
|
It'll probably turn out to be something simple, but I'm at loss here:code:
What this code is meant to do is to check if player accumulated enough points to unlock a new item, then move the first item in unlocks list to a new place and increase future threshold (in a similar progression of that of D&D). Then check again until there's no more unlocks. The line "threshold += factor" is all hosed up, though. threshold doesn't increase, ever (I whipped up a second program to look up what's in the file). Yet for the second item in the unlock list the "if max_points >= threshold" seems to work more like "if max_points >= threshold+factor". It's some sort of "it's not a variable but a reference to one"-style voodoo, isn't it?
|
# ? May 19, 2013 18:22 |
|
Lichtenstein posted:It'll probably turn out to be something simple, but I'm at loss here: Does your external code check threshold or plik['unlock_threshold']? And what is BASE_FACTOR set to? Maybe you could explain a little bit more about what this external code checked. Have you tried just printing threshold before and after you increment it? Can you show us that output? Also, the closing of the file also calls sync(), which writes back all entries in the cache. So you'd need to either close the file or call sync() after setting all of those dictionary values.
|
# ? May 19, 2013 18:33 |
|
Right after resetting the file, factor, threshold and BASE_FACTOR are each 5.000. After hitting 5.000 max_points, threshold is left at the very same 5.000, while factor increases to 10.000. After hitting 10.000 max_points, nothing happens. After hitting 15.000 max_points, second item gets unlocked and factor increases to 15.000. The only other interactions with the file in external code is a simple random.choice() of the unlocked items to spawn, which works fine.
|
# ? May 19, 2013 18:59 |
|
Lichtenstein posted:Right after resetting the file, factor, threshold and BASE_FACTOR are each 5.000. What behaviour do you want? It sounds like you want the second unlock to occur at 10,000 points, which you can accomplish by swapping factor += BASE_FACTOR and threshold += factor. Alternatively, you may want to store a level number and then have a function that can generate the threshold for the next level. This would be easier to reason about than tracking the state of both factor and threshold. Another alternative is to just store the point thresholds in the item objects. I would also double-check your logging; threshold definitely increases when you hit 5,000 points. Here's the version I'm testing with: Python code:
code:
Met48 fucked around with this message at 19:56 on May 19, 2013 |
# ? May 19, 2013 19:52 |
|
It turns out everything was fine, the busted part was the code I used to peek on the file. Even though it literally consisted only of opening and closing the file and print() in between. Out of curiosity, instead of using .get() method like Met48 did I simply wrote print(shelf['whatever']). Why was this (apparently) a bad, retarded rookie mistake?
|
# ? May 19, 2013 20:53 |
|
.get() doesn't throw an exception when you try to get a non-existent key.
|
# ? May 19, 2013 20:56 |
|
Thermopyle posted:I think I recall Eric Raymond being amongst them. While not inexperienced, he has his own set of issues. batshit insane libertarian would the issue in his case.
|
# ? May 19, 2013 23:02 |
|
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 |
|
Thermopyle posted:.get() doesn't throw an exception when you try to get a non-existent key. Instead, it just returns None.
|
# ? May 19, 2013 23:49 |
|
Dominoes posted: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) os.getcwd()?
|
# ? May 19, 2013 23:50 |
|
|
# ? May 20, 2024 07:27 |
|
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 |