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
VikingofRock
Aug 24, 2008




Is it possible to use anonymous pipes with subprocess? I have bash command which I would like to run from my python script, which looks like this:

Bash code:
command -arg1 file1 -arg2 -file2 -arg3 -file3
The expected contents of file1, file2, and file3 are determined from the rest of my script, and I'd rather not have to actually create those files if possible. In bash I would do

Bash code:
command -arg1 <(make_file1_contents) -arg2 <(make_file2_contents) -arg3 <(make_file3_contents)
Does anyone know how to do that with subprocess? I guess I could always use temporary files but I'm wondering if there is a more elegant way.

Adbot
ADBOT LOVES YOU

VikingofRock
Aug 24, 2008




Edison was a dick posted:

That works by bash either making a fifo or using the /dev/fd symlinks to turn an open file descriptor into a file path.


Python code:
arg1 = Popen(make_file1_contents, stdout=PIPE)
arg2 = Popen(make_file2_contents, stdout=PIPE)
arg3 = Popen(make_file3_contents, stdout=PIPE)
command = Popen('command', '-arg1', '/dev/fd/%d' % arg1.stdout.fileno()
                           '-arg2', '/dev/fd/%d' % arg2.stdout.fileno(),
                           '-arg3', '/dev/fd/%d' % arg3.stdout.fileno())
Substitute the final Popen for call, check_call or check_output as appropriate, and also wait for arg1, and remember to kill and poll the subprocesses.

Thanks, this is really helpful.

VikingofRock
Aug 24, 2008




So there are lots of times in Python when I find myself wishing I had enums. For example, let's say I'm writing a Blackjack game and I want to model the possible player actions. In C or something I would have an enum containing STAND, HIT, SPLIT, etc. But since Python doesn't have enums / ADTs / whatever, I find myself at a loss for how to model things like that. What's the idiomatic Python solution?

edit: Right after I posted this, my friend informed me of the fact that Python has enums. So that solves that, although I am curious how people accomplished this sort of thing before enums were added.

VikingofRock fucked around with this message at 00:04 on Aug 2, 2016

VikingofRock
Aug 24, 2008




Zero Gravitas posted:

Got a question for you fine folks.

I've got a function where I'm dealing with mach numbers and angles. I need to get the angle of the sum of these mach angles.

Mu (mach angle) = sin^-1 (1/M)

I'm having a hassle as you might expect where M is exactly equal to 1. Or at least, very very close to it.

The problem here is that when you have an M which is slightly less than 1, and you take arcsin(1/M), you get NaN since 1/M > 1 and arcsin isn't defined for values > 1. When you enter the very long floating representation, probably M gets rounded to exactly 1.0 and things work out. Depending on your application, a decent work around might be to just say that if M < 1.0, M = 1.0 (if M can be <= -1 you'll need to modify this slightly).

edit: Didn't see your edit. If you are getting in trouble with re-assigning M, you could just say M_prime = M if M >= 1.0 else 1.0. Then use M_prime in your formulae.

VikingofRock fucked around with this message at 23:48 on Aug 3, 2016

VikingofRock
Aug 24, 2008




LochNessMonster posted:

I noticed you mentioned my for loop overwriting the variable with each iteration but changed your reply so it's gone. That was a relevant remark as well though, I'm kinda ashamed I didn't notice that myself.

Half the code I post on this site has some stupid mistake like that, so don't sweat it. It happens.

VikingofRock
Aug 24, 2008




Is python 2.6 still the default python for CentOS? The only time I ever see it nowadays is on old CentOS machines.

VikingofRock
Aug 24, 2008




Eela6 posted:

Python code:
[new_remaining, dominated][dominates(candidate, other)].append(other)

Is this line common python style? I've never seen that before and it took me a minute or two to puzzle through what it does.

VikingofRock
Aug 24, 2008




Dominoes posted:

Hey dudes, running into issues scraping this image: http://www.feynmanlectures.caltech.edu/img/FLP_I/f16-01/f16-01_tc_big.svgz

Getting 403s, despite having a user agent header. Ie:

Python code:
url = 'http://www.feynmanlectures.caltech.edu/img/FLP_I/f16-01/f16-01_tc_big.svgz'
requests.get(url, headers={'User-agent': 'Mozilla/5.0'}, stream=True)
Any ideas?

I'm getting a 403 too. Maybe you need to be logged into something to see that image?

VikingofRock
Aug 24, 2008





Hey so I finally got around to watching this talk, and it was pretty good! Thanks for linking it.

VikingofRock
Aug 24, 2008




Hey so what's the state of the art way of doing packaging? I want something where I can run some command(s) on the command line and it'll download all the right dependencies for my script, download the right version of python, and run the script. But it seems like there are a million different tools that each do a part of this and it's not really clear to me how they intersect.

VikingofRock
Aug 24, 2008




Fergus Mac Roich posted:

I feel that half-open has been fairly universal in the languages that I've used. For example, Java's Arrays.sort() uses half-open indexing. Dijkstra essentially makes an argument for it here

Yeah the only exception to this in the languages I've used is in Haskell, where [1 .. 5] is the list [1, 2, 3, 4, 5]. Generally I like half open indices, and the only time it's bitten me is in languages where I can't easily express a max bound for some reason. For example in Rust, expressing the range (i .. 256u8) doesn't work because you can't have 256 as an unsigned 8-bit value. They've talked about adding an inclusive range syntax to rectify this, so I could do (i ... 255u8) instead.

VikingofRock
Aug 24, 2008




Dominoes posted:

Hey dudes. What are the best ways to learn Python, for someone with no experience? I learned from Codeacademy, which was OK. The OP looks out of date.

I've heard a lot of good things about Fluent Python.

VikingofRock
Aug 24, 2008




Dominoes posted:

Thread's up.

Still looking for additions and edits.

Maybe add some stuff about Python 2 vs. Python 3? Someone new to the language is bound to have questions about it, since it comes up fairly often in Python discussions, and there is still a lot of Python 2 code floating around out there (particularly in scientific circles). Even if the section is just "Python 3 is better and you should definitely use it and here is why", it's probably worth having that section IMO.

VikingofRock
Aug 24, 2008




Dominoes posted:

I deliberately omitted that; too political.If anything, it would be a note about missing features etc when working with old codebases.

Fair enough--I didn't realize this was still a political issue within the python community. I figured most serious python programmers had switched over to 3 at this point unless they were working with a legacy codebase which used python 2. Are there large numbers of people who advocate 2 over 3?

VikingofRock
Aug 24, 2008




QuarkJets posted:

For some reason Enthought (their product Canopy is basically like Anaconda) only supports Python2. I've asked their sales people in the past why they haven't moved to Python3 and their answer was basically "we think scientists still prefer and use 2"

I don't think that it's political any longer, now it's just personal preference / legacy vs new code

Okay, that's more similar to how I thought it was (I'm also in science academia). Mostly to me it seems like academics use python 2 because that's what's provided by default by Mac OS, CentOS, and Ubuntu, and that's like 99% of what your code is going to be running on.

Adbot
ADBOT LOVES YOU

VikingofRock
Aug 24, 2008




9-Volt Assault posted:

Love it that we now have 2 python threads. Feels just like real life!

Don't worry, I'm sure this thread will get end-of-lifed in five ten years.

  • Locked thread