|
suffix posted:Is that really "blah.txt" or is it "blah.txt.txt" with a hidden file extension? Not sure, but I did a ghetto work-around by just editing the file that I created within python and then using that to read from.
|
# ? Nov 15, 2013 23:42 |
|
|
# ? May 30, 2024 10:19 |
the posted:Not sure, but I did a ghetto work-around by just editing the file that I created within python and then using that to read from. Turn on display of file extensions! I find myself compulsively changing that setting as soon as I sit down a computer that isn't my own.
|
|
# ? Nov 15, 2013 23:44 |
|
fletcher posted:Turn on display of file extensions! I find myself compulsively changing that setting as soon as I sit down a computer that isn't my own. Yep that fixed it. I turned them on and created a "blah.txt" and it read from it. So I'm guessing it was the problem that ^that guy posted at the top of the page.
|
# ? Nov 15, 2013 23:49 |
|
Anyone know how to match a sequence of characters with regex, and do so with an or statement? I'd like the code below to match any 3 letters, 1 or more spaces, one of 'NDB', 'DME' or 'VOR', any number of characters, and 4 digits. I believe it's all working properly except for the [NDB|DME|VOR] bit. '[A-Z]{3}\s+[NDB|DME|VOR].*\d{4}' Is it just me, or are the Python docs (main and how-to) on Regexs poorly organized? The examples are too sparse to be useful too. Dominoes fucked around with this message at 00:27 on Nov 16, 2013 |
# ? Nov 16, 2013 00:23 |
|
Your problem is that [] is for single characters, so you're matching a single one of of BDEMNROV|. You want (NDB|DME|VOR). The Python documentation of the re module does assume a working knowledge of regexes, yeah. If you want a basic tutorial on regex syntax this one looks like it might be good.
|
# ? Nov 16, 2013 00:35 |
|
Also as written it matches capital letters and whitespace rather than letters and spaces which may or may not be important.
|
# ? Nov 16, 2013 00:45 |
|
Dominoes posted:Anyone know how to match a sequence of characters with regex, and do so with an or statement? I learned regexes pretty well from the regex howto in the python docs. http://docs.python.org/2/howto/regex.html
|
# ? Nov 16, 2013 01:01 |
|
Dominoes posted:Anyone know how to match a sequence of characters with regex, and do so with an or statement? They probably didn't put much thought into the Python regex documentation since there's already so much regex documentation elsewhere
|
# ? Nov 16, 2013 01:47 |
|
I have a question about sorting in Python. I have a list of lists, and each of the sublists contains only floats. Some of the lists have exactly one value of 1.0 and all other values are 0.0. Some of the lists have more variety in their values. Each list is the same length. I want to sort these lists such that all of the lists with exclusively 1.0 and 0.0 values come first, and all the other lists follow. Is this something I can do with the key keyword that sort() uses? Or do I need a more complicated solution? (A quick technical explanation is that these are values in a Markov chain's transition matrix before they get put into a numpy matrix. I want to sort all the absorbing states before the non-absorbing states.)
FoiledAgain fucked around with this message at 03:50 on Nov 16, 2013 |
# ? Nov 16, 2013 03:01 |
|
FoiledAgain posted:I have a question about sorting in Python. I have a list of lists, and each of the sublists contains only floats. Some of the lists have exactly one value of 1.0 and all other values are 0.0. Some of the lists have more variety in their values. Each list is the same length. I want to sort these lists such that all of the lists with exclusively 1.0 and 0.0 values come first, and all the other lists follow. Is this something I can do with the key keyword that sort() uses? Or do I need a more complicated solution? (A quick technical explanation is that these are values in a Markov chain's transition matrix before they get put into a numpy matrix. I want to sort all the absorbing states before the non-absorbing states.) Write a key function that returns 0 for the rows you want at the start and 1 for all the others.
|
# ? Nov 16, 2013 04:05 |
|
QuarkJets posted:They probably didn't put much thought into the Python regex documentation since there's already so much regex documentation elsewhere
|
# ? Nov 16, 2013 04:53 |
|
Forget building your own regexes, just use http://txt2re.com/
|
# ? Nov 16, 2013 05:27 |
|
http://regexpal.com/ is pretty handy too.
|
# ? Nov 16, 2013 07:12 |
|
Nippashish posted:Write a key function that returns 0 for the rows you want at the start and 1 for all the others. Ah, I didn't quite grasp what to do with that keyword. That's easy though. Thanks!
|
# ? Nov 16, 2013 07:14 |
|
BeefofAges posted:Forget building your own regexes, just use http://txt2re.com/ That's higher on the google search listings but http://gskinner.com/RegExr/ is Much Much better.
|
# ? Nov 16, 2013 08:13 |
|
Opinion Haver posted:Your problem is that [] is for single characters, so you're matching a single one of of BDEMNROV|. You want (NDB|DME|VOR). Word. Python code:
If I use compile, match/search, group() syntax, it seems to work properly. I'd like to use findall, since I'm finding multiple matches. Regarding the docs, the intent makes more sense in context of expecting prior RE knowledge. Dominoes fucked around with this message at 10:31 on Nov 16, 2013 |
# ? Nov 16, 2013 10:27 |
|
try:code:
If there are no capturing groups, then the entire regex is a group, so you don't need parens around the entire expression.
|
# ? Nov 16, 2013 11:13 |
|
evensevenone posted:try:
|
# ? Nov 16, 2013 11:37 |
|
Misogynist posted:It's more that the Python official documentation was written out of necessity and not to be consumed by any living being anywhere It varies module to module but I've found the official documentation to be really quite good. Some bits, like the overview of unicode, are excellent.
|
# ? Nov 16, 2013 20:10 |
|
Can the exec command handle multi-line conditional statements within a read text input? Let's say I'm reading an input file line by line, and I "exec" each line. Should that work? I don't think it is because I'm getting an "unexpected EOF while parsing," and I believe it's because it's hitting the end of an IF statement and not knowing what to do. ex: say I was going to exec(line) line by line for Python code:
the fucked around with this message at 21:04 on Nov 16, 2013 |
# ? Nov 16, 2013 20:55 |
|
the posted:Can the exec command handle multi-line conditional statements within a read text input? I guess the more important question is why you want to do this.
|
# ? Nov 16, 2013 21:17 |
|
If you're trying to execute a file that has a bunch of commands like that, why not just run the file itself?
|
# ? Nov 16, 2013 21:26 |
|
Modern Pragmatist posted:I guess the more important question is why you want to do this. It's part of an assignment There are portions in the file that are not code. So I can't run the file itself.
|
# ? Nov 16, 2013 22:13 |
|
I wonder if there are also assignments that have you intentionally allow SQL injection, or where you transmit usernames and passwords over the internet as plaintext.
|
# ? Nov 16, 2013 22:19 |
|
the posted:It's part of an assignment I'm going to guess that the approach you want is to do a pass over all of the lines in the file looking for the lines that are code and storing them all together. Then exec that. It's a stupid assignment because why would you ever?
|
# ? Nov 16, 2013 22:25 |
|
Dren posted:I'm going to guess that the approach you want is to do a pass over all of the lines in the file looking for the lines that are code and storing them all together. Then exec that. It's a stupid assignment because why would you ever? Yeah that's what I'm currently trying to figure out what to do (store them). And did I say assignment? I meant "Whiteboard problem given by prospective company to be completed over the weekend."
|
# ? Nov 16, 2013 22:31 |
|
In what real-world context would this ever be important or useful?
|
# ? Nov 16, 2013 22:37 |
|
Of course exec can handle multiline input, but it is immediately executing what you give it each time. If you give it "if whatever:" there is nothing to execute because "if whatever:" is incomplete and invalid. Re: what real-world context it would be useful in, it depends on the job and also we haven't really gotten a full description of the problem. It could just be a job where they do a lot of dumb poo poo, there are plenty of those. If it's a sciencey thing where writing Python is more of a side job and a means to an end, there may not be a strong emphasis on code quality. It could be the problem is actually something reasonable, or is a reduction of some more complicated reasonable task. If it's a dev job where code quality is important, assigning some easy tasks to be completed in whatever language is pretty common. They are often abstract, unrelated to the job, or they may even intentionally ask you to do some stupid poo poo to see what you say about it. The point is to 1) filter out the people who can't even solve the easy problems and have no business doing the job and 2) to provide your solution to some problems they are familiar with so that they can talk to you about why you made the decisions that you did, and thereby figure out if you actually know what you are doing. There are almost always some traps that they are looking for you to avoid.
|
# ? Nov 16, 2013 23:32 |
|
For an interview coding assignment I once got lazy and used an eval on some sample data instead of properly parsing it (or using ast.literal_eval), and when I went over my code with the interviewers they went straight to the eval and asked why I would ever do that. Surprisingly, they were satisfied with my answer that it was the weekend and I was feeling lazy and I would never do that in production code.
|
# ? Nov 16, 2013 23:32 |
|
Yeah I'm not giving too much information because that wouldn't really be ethical. I want to be able to do this myself.
|
# ? Nov 16, 2013 23:36 |
|
To give you a tiny bit of guidance; the process you should be thinking about for this is: Filter out what lines are code and what isn't code. Seperate it into "Chunks", so if there's garbage, 3 lines of code, garbage, then more code; it gets split into two chunks of code (alternatively; the it might be wanting you to strip away all the garbage and execute the entire remaining code all at once). There's a few things like ending with a semicolon that would make the line hopefully Not Garbage and that's mostly your goal; figuring out what dictates "code" and "not code".
|
# ? Nov 16, 2013 23:59 |
|
Yeah I think I've done that. My major problem is now that I'm scanning through it 'line by line' and either just printing it or executing it depending on what it is 'not code or code.' The problem is that when I encounter nested lines of code, like an if statement, it breaks because it's parsing it line by line. They want me to, at the end, output the entire thing with the code part executed. So this Python code:
code:
the fucked around with this message at 00:05 on Nov 17, 2013 |
# ? Nov 17, 2013 00:01 |
|
Mr. Wynand posted:Seriously though, if any of you would care to name a single legitimately good use of the metaclass, I am all ears. I really don't think there is a "good reason" for it, ever. (Except maybe "we can only use python 2.5 or older" - fine fine) Metaclasses are used when you need to "decorate" a whole class hierarchy. Metaclass is like an inheritable decorator in a way.
|
# ? Nov 17, 2013 00:03 |
|
the posted:My major problem is now that I'm scanning through it 'line by line' and either just printing it or executing it depending on what it is 'not code or code.' Well maybe try something else.
|
# ? Nov 17, 2013 00:07 |
|
the posted:Yeah I think I've done that. My major problem is now that I'm scanning through it 'line by line' and either just printing it or executing it depending on what it is 'not code or code.' Try moving up a level from lines of code and lines of garbage to chunks of code and chunks of garbage maybe?
|
# ? Nov 17, 2013 00:11 |
|
BeefofAges posted:I wonder if there are also assignments that have you intentionally allow SQL injection, or where you transmit usernames and passwords over the internet as plaintext.
|
# ? Nov 17, 2013 01:20 |
|
the posted:
You need to break the code up into chunks, not line by line. This is basically like a templating language. Stop trying to do everything at once, or as you process the file. Break the file up into chunks in one pass, and then work out what to do with them in the next, you'll find it easier.
|
# ? Nov 17, 2013 01:37 |
|
the posted:Yeah I'm not giving too much information because that wouldn't really be ethical. I want to be able to do this myself. What you're doing is asking for help, buy coyly. Asking us to reverse engineer the interview question doesn't make it any less unethical than you being forthright. tef fucked around with this message at 01:46 on Nov 17, 2013 |
# ? Nov 17, 2013 01:42 |
|
BeefofAges posted:Surprisingly, they were satisfied with my answer that it was the weekend and I was feeling lazy and I would never do that in production code. It shows that you know eval is a hazard. That's a much better resolution to the eval red flag than asking why it's a problem. If you were interviewing somebody and they gave that response, would you continue to be horrified, or would you just say "yeah, you get it, fine by us"
|
# ? Nov 17, 2013 02:26 |
|
|
# ? May 30, 2024 10:19 |
|
tef posted:What you're doing is asking for help, buy coyly. Asking us to reverse engineer the interview question doesn't make it any less unethical than you being forthright. Didn't intend to. I'll drop the issue now.
|
# ? Nov 17, 2013 02:38 |