Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
the
Jul 18, 2004

by Cowcaster

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.

Adbot
ADBOT LOVES YOU

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

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.

the
Jul 18, 2004

by Cowcaster

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.

Dominoes
Sep 20, 2007

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

Opinion Haver
Apr 9, 2007

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.

breaks
May 12, 2001

Also as written it matches capital letters and whitespace rather than letters and spaces which may or may not be important.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Dominoes posted:

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.

I learned regexes pretty well from the regex howto in the python docs.

http://docs.python.org/2/howto/regex.html

QuarkJets
Sep 8, 2008

Dominoes posted:

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.

They probably didn't put much thought into the Python regex documentation since there's already so much regex documentation elsewhere

FoiledAgain
May 6, 2007

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

Nippashish
Nov 2, 2005

Let me see you dance!

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.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

QuarkJets posted:

They probably didn't put much thought into the Python regex documentation since there's already so much regex documentation elsewhere
It's more that the Python official documentation was written out of necessity and not to be consumed by any living being anywhere

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

Forget building your own regexes, just use http://txt2re.com/

evensevenone
May 12, 2001
Glass is a solid.
http://regexpal.com/ is pretty handy too.

FoiledAgain
May 6, 2007

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!

Jewel
May 2, 2009

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.

Dominoes
Sep 20, 2007

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).

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.

Word.
Python code:
import re
text = "    CPT   DME                 ETA 1100"
navaids = re.findall('[A-Z]{3}\s+(NDB|DME|VOR).*\d{4}', text)
print(navaids)
The output here is "['DME'],when it should be the whole line. If I replace (NDB|DME|VOR) with [A-Z]{3}, it works as expected.

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

evensevenone
May 12, 2001
Glass is a solid.
try:
code:
   re.findall(r"[A-Z]{3}\s+(?:NDB|DME|VOR).*\d{4}",navaids)
The reason is that parentheses are used to indicate groups, and findall returns a list of groups. Unfortunately, I don't know any way to get the | to work as we want without putting it in a group, so we need to put it in a non-capturing group with the ?: notation.

If there are no capturing groups, then the entire regex is a group, so you don't need parens around the entire expression.

Dominoes
Sep 20, 2007

evensevenone posted:

try:
code:
   re.findall(r"[A-Z]{3}\s+(?:NDB|DME|VOR).*\d{4}",navaids)
The reason is that parentheses are used to indicate groups, and findall returns a list of groups. Unfortunately, I don't know any way to get the | to work as we want without putting it in a group, so we need to put it in a non-capturing group with the ?: notation.

If there are no capturing groups, then the entire regex is a group, so you don't need parens around the entire expression.
Thanks dude; this works.

Dren
Jan 5, 2001

Pillbug

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.

the
Jul 18, 2004

by Cowcaster
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:
dog = 2
if dog == 2:
    print 'bark'
Currently I'd get an "unexpected EOF" at the end of the if statement.

the fucked around with this message at 21:04 on Nov 16, 2013

Modern Pragmatist
Aug 20, 2008

the posted:

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:
dog = 2
if dog == 2:
    print 'bark'

Currently I'd get an "unexpected EOF" at the end of the if statement.

I guess the more important question is why you want to do this.

Pollyanna
Mar 5, 2005

Milk's on them.


If you're trying to execute a file that has a bunch of commands like that, why not just run the file itself? :confused:

the
Jul 18, 2004

by Cowcaster

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.

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

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.

Dren
Jan 5, 2001

Pillbug

the posted:

It's part of an assignment :(

There are portions in the file that are not code. So I can't run the file itself.

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?

the
Jul 18, 2004

by Cowcaster

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."

Pollyanna
Mar 5, 2005

Milk's on them.


In what real-world context would this ever be important or useful?

breaks
May 12, 2001

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.

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

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.

the
Jul 18, 2004

by Cowcaster
Yeah I'm not giving too much information because that wouldn't really be ethical. I want to be able to do this myself.

Jewel
May 2, 2009

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".

the
Jul 18, 2004

by Cowcaster
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:
random bullshit

*code start*
print 'hey this is stupid'

*code end*
random bullshit
Would output

code:
random bullshit

hey this is stupid

random bullshit

the fucked around with this message at 00:05 on Nov 17, 2013

rvm
May 6, 2013

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.

fritz
Jul 26, 2003

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.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

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.'

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:
random bullshit

*code start*
print 'hey this is stupid'

*code end*
random bullshit
Would output

code:
random bullshit

hey this is stupid

random bullshit

Try moving up a level from lines of code and lines of garbage to chunks of code and chunks of garbage maybe?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

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.
I had an assignment in school that intentionally had buffer overflow bugs, but the next assignment was to craft exploits for those bugs.

tef
May 30, 2004

-> some l-system crap ->

the posted:

Python code:
random bullshit

*code start*
print 'hey this is stupid'

*code end*
random bullshit
Would output

code:
random bullshit

hey this is stupid

random bullshit

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.

tef
May 30, 2004

-> some l-system crap ->

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

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

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"

Adbot
ADBOT LOVES YOU

the
Jul 18, 2004

by Cowcaster

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.

  • Locked thread