|
Drunk Badger posted:I think I need to add some more information now that I have a better idea what I'm doing. I'm not sure what it is you're trying to work towards, but here is something silly I put together quickly. Maybe it will help you, IDK. Python code:
|
# ¿ Feb 14, 2013 04:10 |
|
|
# ¿ May 9, 2024 00:58 |
|
The notion of having an "official style guide" is awful. The idea that you're not free to adopt your own set of formatting conventions for your project, no matter that they're consistent and work well for you, because some busybodies decided to codify what they think everyone else's code should look like.
|
# ¿ Feb 15, 2013 02:13 |
|
Thermopyle posted:I agree if you're not doing open source work. Why on Earth would that make a difference to anything? I think if I were to have read PEP 8 and discovered that quite by coincidence my preferred formatting conventions matched its prescriptions to a tee, I should have chosen to alter key elements so that they no longer matched, purely out of principle.
|
# ¿ Feb 15, 2013 02:19 |
|
Lysidas posted:What is wrong with you? A lot of the responses to what I posted (or comments that may have been partially prompted by what I wrote) have been along the lines of "look guy, you aren't obliged to follow the suggestions in PEP 8. They're a default for people to start out with". Or similar. If that's all it was then I wouldn't have a problem with it. But that's not all it is, because as soon as you endorse a default style you get people saying snarky things like xtal posted:Special cases aren't special enough to break the rules. My problem isn't with anyone who really does just see it as a suggestion. My problem is that people think it's a set of rules and that it's wrong to "break" those rules (as opposed to them merely being guidelines that you can disregard if you like). If you're not one of those people then great. Maybe my post was immature, but I guess I have a streak of bloody-mindedness that prompts me to resent being told I have to format my code in a certain way when I like another way better!
|
# ¿ Feb 15, 2013 05:12 |
|
Innocent Bystander posted:Can someone explain to me the way that Python variables behave in closure like situations? It looks like the functions stored in myLat evaluate their return values when called, not when defined. Since the variable "a" referenced in each function is still needed, a reference to that variable sticks around, and of course it has the last value it was given, which is 2. It looks like there is an accepted way to get Python to accept the value of "a" that was in place at the time the function was defined. You declare it as an argument with a default value. The default value is evaluated at function definition time. So your function would become code:
code:
|
# ¿ Feb 15, 2013 06:29 |
|
Suspicious Dish posted:No, it's: Oh, yeah you're right. It was late
|
# ¿ Feb 15, 2013 11:54 |
|
JetsGuy posted:I still don't understand why. What is the advantage of doing: I don't really know what the Pythonesque (TM) answer is but to me saying "if not <thing>" does a better job of expressing your intentions than "if <thing> == False" (or "is"). The "not" version makes it clear (in part because it reads like English) that you're checking for a negative response; whereas the "False" version ties us up in detail (that a negative response is given in the form of the constant False). Of course there are much worse ways you can obfuscate what code is doing but it's the same sort of thing, just on a trivial level. Edit: Suspicious Dish's post suggests a related point, which is that you wouldn't write "if x == True:"; you'd just write "if x:". By analogy you shouldn't write "if x == False:", just "if not x:". Hammerite fucked around with this message at 22:16 on Mar 4, 2013 |
# ¿ Mar 4, 2013 22:12 |
|
JetsGuy posted:EDIT: code:
edit: bastard
|
# ¿ Mar 11, 2013 22:22 |
|
Love Stole the Day posted:Hey again, I have a project that I kind of go back to every often that uses this too, I agree it gives you a nice way to build simple IRC bots. Watch out if Python3 support matters to you though, it isn't compatible and doesn't appear to be actively developed any longer.
|
# ¿ Mar 15, 2013 02:56 |
|
Dominoes posted:Is there a way to procedurally change a function based on variables, like in a loop? The best way I can describe it is with psuedocode: There's probably a better way to do this, and I'm tired or I would think about it and suggest one. But yes you can do that using __getattribute__. Python code:
|
# ¿ Apr 10, 2013 01:53 |
|
yaoi prophet posted:To expand, what it will actually do is evaluate as It will be either True (if indeed foo == 1) or 2 (which is a "true" value in a Boolean context) (otherwise).
|
# ¿ Apr 28, 2013 23:05 |
|
Popper posted:Even if foo is not 1 it will return True, it's completely useless. No, you have not understood what I said. If foo != 1 then it will return 2, which is not True, although it is a "true" value in a Boolean context. code:
|
# ¿ Apr 29, 2013 15:32 |
|
You can encode a set of Booleans as a number using something like this, though there might often be a better way of doing whatever you are trying to do:code:
|
# ¿ Apr 29, 2013 21:34 |
|
If all else fails, you can just write scripts and run them from the Python interactive command-line interpreter using import. You will want to add the directory where you are saving the scripts to your PYTHONPATH so that Python can find them. (On my version of Windows I can do this by going Control Panel -> Classic View -> System -> Advanced system settings -> Advanced -> Environment Variables and adding/editing the PYTHONPATH variable under "System variables".)
|
# ¿ May 1, 2013 21:08 |
|
You are already using the Python interactive interpreter, you are not using the OS's command line. You need to type Python commands, not commands that would be understood by Windows's command line. So to run the code in hello.py you need to type "import hello.py" "python hello.py" is not valid Python syntax, which is why you see that error.
|
# ¿ May 1, 2013 21:44 |
|
I realised that the correct version of the command is "import hello" (without the .py). However, I do not know whether that is the cause of your most recent error message. Try "import hello" and see whether that works better.
|
# ¿ May 1, 2013 21:51 |
|
dantheman650 posted:Well, no errors this time! However, it also had no output of any sort. This is probably because you entered the command in the same command-line window as before, and the hello module had been imported already. Once a module has been imported once, importing it again only serves to define variable names in the namespace in which the import statement appears. If you use "reload hello" you will see output, because that re-executes the script in full. edit: You should probably not get used to using reload, because once you are writing larger scripts that themselves import other scripts of yours, using reload will only re-execute the named script, and not the others nested inside. This is a downside to using the interactive command line. Hammerite fucked around with this message at 22:02 on May 1, 2013 |
# ¿ May 1, 2013 21:56 |
|
dantheman650 posted:Are you talking about the Windows command prompt or the cmd.py that is in my Python intall directory? Use the [ code ] tags when posting code, because otherwise people who read your post may not see spacing correctly. This is particularly important for Python because whitespace is syntactically significant. Did you close and reopen the interpreter, or use reload, as I mentioned? You might be getting that error from the Windows command line because Python is not in your PATH and so Windows can't find it. Follow the same steps you used to edit the PYTHONPATH variable but this time change the PATH variable (it might be capitalised differently) by adding the path to the directory where python.exe is at the end, after a semicolon.
|
# ¿ May 1, 2013 22:09 |
|
I feel like this is the sort of thing I'm going to slap myself on the forehead for not remembering by myself, but suppose I have a dictionary and I want to transform the values of that dictionary in some way while keeping their keys the same... how do I do that? I mean, obviously I can come up with a way to do that using a loop, but what's the idiomatic way? What I want to do is make a dictionary whose values are tuples to be passed to a class constructor, and I then want to run the class constructor across the dictionary so as to produce a dictionary of objects of the class. Python 3 if it matters. Python code:
|
# ¿ May 7, 2013 03:19 |
|
I see, I felt sure there must be a builtin that does this, but I guess I was mistaken. The following is what I have: Python code:
Python code:
edit: less misleading variable names Python code:
Hammerite fucked around with this message at 04:01 on May 7, 2013 |
# ¿ May 7, 2013 03:46 |
|
Lysidas posted:Hammerite, if you want to update your dict in place there probably isn't much room for improvement in your functions/loops. A few suggestions though: you may as well directly loop over key, value in my_dict.items() and you should check for isinstance(value, collections.abc.Mapping) instead of dict. I concluded that I had no need to update the dict in place after all. So I ended up with more or less what Dren suggested in the first place. I also decided I don't really need that code to cope with dictionaries right now, so it's out. Python code:
Python code:
|
# ¿ May 7, 2013 08:43 |
|
Master_Odin posted:Yeah, I just used self.deckStart[:] which makes a copy as I understand it. I guess I'm a bit confused as to when to know when something is passed by reference and when it's not. Running: Everything is passed by reference, absolutely everything. But ints and strings are immutable - can't be changed. Once you have a value of 4, that's it; that 4 can't be changed (to 5 say), though it can be replaced by a new value 5. The reason why += changes a list in place whereas for your integer it acts like "temp = temp + 1" is that the behaviour of += is dependent on the type on its left. It behaves in the one way for lists and in the other for ints, strings and so on. This might seem confusing at first and almost like "cheating", but in fact you can set the behaviour of += for your own types however you want (by defining the __iadd__() method).
|
# ¿ May 9, 2013 08:45 |
|
tef posted:It's more accurate to say that python uses call by object semantics. Every object is stored on the heap, and variables contain a reference to that object. When you pass in a list to a function, the reference is copied. Yeah, I knew I was most likely not using the correct terminology. I hoped to just get across the idea of how Python behaves and what's going on with Master_Odin's code.
|
# ¿ May 10, 2013 03:29 |
|
BeefofAges posted:Even if you import numpy twice, it's still just happening twice while loading your program. This shouldn't have any noticeable speed impact. It's not like you're reimporting numpy over and over in a loop. Correct me if I'm wrong, but the second time aren't you just adding a name to the namespace of the module you're in at the time? That's essentially no work.
|
# ¿ May 13, 2013 16:49 |
|
Is it considered poor form to use the fact that loop variables are still set after the loop? I have to repeatedly loop over elements of a dictionary and unset an element at each iteration, but can't unset in the loop because that's not allowed. So I came up with Python code:
|
# ¿ May 14, 2013 21:25 |
|
Misogynist posted:Why are you using an inner loop when each run through the loop will execute 0 or 1 times? 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 14, 2013 21:51 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
Thanks for the thoughts on coroutines, Dren. I did it that way and just put a brief explanation in the docstring. Added to that there is the fact that a ready made example is present as well due to the fact that I was writing it for a purpose (had a specific application for walking the filesystem). Question: Should a module export its custom exceptions? I found out that the help() function respects __all__. But it seems to add unnecessary clutter to have the help list the custom exception class and a long list of double-underscore methods it inherits. Blah, blah, blah. It is a class of exceptions. Who would want to read that? So I thought perhaps __all__ should exclude the exceptions.
|
# ¿ May 23, 2013 04:06 |
|
I have a Python module, let us call it x.py, I wrote for myself that I now want to be in version control. So I made a directory called simply x, moved x.py in there, and initialised a git repository. So far so simple. But now in order to import that module I have to type import x.x as x Which is kind of onerous. I could add the directory x to my PYTHONPATH, but I don't want to do that for every module I create. I could put a copy of x in my Python install directory, but I'd have to update it each time I make a change. But I see that I can rename x.py to __init__.py and I can once again just "import x". So now I have a directory containing a single file, "__init__.py" (and a .gitignore file and git/pycache nonsense). Is there a better way?
|
# ¿ May 28, 2013 07:09 |
|
|
# ¿ May 9, 2024 00:58 |
|
Thanks, I decided to bite the bullet and split the thing up into a half dozen files. After a bit of faffing around with import statements and so on the organisation of the code is now improved.
|
# ¿ May 28, 2013 10:47 |