|
Stabby McDamage posted:I'm surprised nobody's posted the underlying problem.
|
# ? Apr 7, 2010 03:35 |
|
|
# ? May 11, 2024 17:07 |
|
Avenging Dentist posted:I know why it's that way but it's beyond retarded that they don't check for it in the assignment, especially since it's trivially easy (A[3::-1].base is A). Do you mind elaborating why this is the case? Is it because you don't want to unnecessarily copy the entire array in memory? What benefits does the current way have?
|
# ? Apr 7, 2010 03:52 |
|
Modern Pragmatist posted:Do you mind elaborating why this is the case? Is it because you don't want to unnecessarily copy the entire array in memory? What benefits does the current way have? NumPy array slices are just views into the array and reference the same memory. It's faster and more memory-efficient. This is a dumb bug because it's actually something that C/C++ programmers have to deal with a lot (checking for self-assignment), and I'd have expected the NumPy folks to have predicted it, since they wrote it (mostly) in C.
|
# ? Apr 7, 2010 04:32 |
|
UberJumper posted:a. comtypes people get pissy, and can they basically tell me to take it down? It doesn't really matter. It's distributed under the MIT License, so they can't prevent you from modifying it to work for your particular case.
|
# ? Apr 7, 2010 09:09 |
|
I'm having to compose regular expressions, and I often need fairly complex subexpressions that i don't want to repeat. I can store them as a string, but then my regex looks like:code:
code:
|
# ? Apr 7, 2010 14:55 |
|
In the past I've used template strings to do stuff like this. you could then have the option of making your wrapper something like: code:
code:
|
# ? Apr 7, 2010 15:52 |
|
Scaevolus posted:It doesn't really matter. It's distributed under the MIT License, so they can't prevent you from modifying it to work for your particular case. thanks!
|
# ? Apr 7, 2010 16:32 |
|
Lurchington posted:In the past I've used template strings to do stuff like this. If I find myself adding more substitutions, I'll add this in. Thanks!
|
# ? Apr 8, 2010 03:23 |
|
UberJumper posted:thanks! When you're done, email the developers of comtypes and laugh at them for being completely retarded. It takes a special kind of stupid to use a BSD/MIT license and then tell people they can't modify your code.
|
# ? Apr 8, 2010 03:24 |
|
Concise method of creating a new list from every nth item of a list? I seem to write multiple lines of code for this and it seems kinda ridiculous. Example: Every 3rd item from list a makes list b. a = [1, 2, 3, 4, 5, 6, 7, 8, 9] b = [3, 6, 9]
|
# ? Apr 9, 2010 03:08 |
|
Thermopyle posted:Concise method of creating a new list from every nth item of a list? b = [x for x in a[::3]]
|
# ? Apr 9, 2010 03:10 |
|
UberJumper posted:b = [x for x in a[::3]] Any reason not to do b = a[::3]? a[2::3] actually gives [3,6,9]
|
# ? Apr 9, 2010 03:23 |
|
I was not aware of that syntax with slicing. Thanks guys. Another question: alist = ['a', 'b', 'c', 'd', 'e', 'f'] Given an index, let's say 2, what's a concise, yet readable, way to create a list that begins with alist[3] and wraps around to alist[2], so I end up with: blist = ['d', 'e', 'f', 'a', 'b', 'c']
|
# ? Apr 9, 2010 06:37 |
|
Thermopyle posted:I was not aware of that syntax with slicing. Thanks guys. blist = alist[idx:] + alist[:idx]
|
# ? Apr 9, 2010 06:39 |
|
Thermopyle posted:alist = ['a', 'b', 'c', 'd', 'e', 'f'] >>> alist[3:] + alist[0:3] ['d', 'e', 'f', 'a', 'b', 'c']
|
# ? Apr 9, 2010 06:41 |
|
king_kilr posted:blist = alist[idx:] + alist[:idx] God, I'm an idiot. I think I've stayed up too late.
|
# ? Apr 9, 2010 06:43 |
|
Thermopyle posted:God, I'm an idiot. I think I've stayed up too late. Don't feel too bad. List Edit: I'm just going to quite posting altogether :/ Captain Capacitor fucked around with this message at 23:20 on Apr 9, 2010 |
# ? Apr 9, 2010 22:39 |
|
Captain Capacitor posted:Don't feel too bad. List comprehensions are powerful and mysterious creatures. They're a feature I miss quite a bit when switching to other languages. What? Those are list comprehensions, just slicing.
|
# ? Apr 9, 2010 22:41 |
|
I now decided to try interfacing a 2x16 Character LCD module to my parallel port. This is my first program dealing with ASCII to binary conversion, in addition to accepting command line arguments. Any newbish mistakes I should know about? code:
|
# ? Apr 9, 2010 23:44 |
|
dv6speed posted:I now decided to try interfacing a 2x16 Character LCD module to my parallel port. This is my first program dealing with ASCII to binary conversion, in addition to accepting command line arguments. I think you're overcomplicating things a bit. You don't need hex-to-binary conversion for anything you're doing, and if you did, you don't need the binascii module to achieve it. This should be equivalent to what you had: code:
code:
|
# ? Apr 10, 2010 02:39 |
|
Stabby McDamage posted:Awesome stuff, thanks!
|
# ? Apr 10, 2010 03:56 |
|
I'm following along with the MIT Open CourseWare lectures on Python (they're great btw). I'm doing the first quiz now. One thing I've learned in the few cumulative weeks in my life I've spent doing Python is that just because it works doesn't mean it's most efficient. One of the problems on the quiz was to print the first n odd perfect squares. The below works (wrote it myself... /flex)... Can anyone point out some general things to optimize it? (Not looking for the code itself... just hints maybe?) code:
|
# ? Apr 10, 2010 05:41 |
|
code:
EDIT: here's how you do it without list comprehensions: code:
Avenging Dentist fucked around with this message at 05:51 on Apr 10, 2010 |
# ? Apr 10, 2010 05:48 |
|
Avenging Dentist posted:
drat, ok that's cool. I see what you did, there. Thanks.
|
# ? Apr 10, 2010 05:54 |
|
dv6speed posted:Awesome stuff, thanks! Just a little awesome tidbit, I use a project called cmdln to create interactive and non-interactive shells. You could use it to create a live shell to test sending messages to the LCD. It's basically a souped-up optparse setup.
|
# ? Apr 10, 2010 06:08 |
|
Google is failing me (or rather, it's just cluttered up with old old no longer maintained code). Anyone know a serviceable package for editing video files in Python? There was a promising looking thing called DVEdit, but it's nowhere to be found. PyMedia hasn't been touched since '06. I'm looking to open up some AVI files, copy some audio/video from certain timeslices into a new file and mix in some audio over it. Just some really primitive video-editing. Pyglet is a possibility, but the A/V support seems pretty limited from a brief look. Am I missing some sweet secret media package?
|
# ? Apr 10, 2010 22:56 |
|
Avenging Dentist posted:
I appreciate this example even more now. It showed me how list comprehensions worked better than the docs did. One of the other assignments was to write a script to find the nth prime. code:
|
# ? Apr 11, 2010 07:39 |
|
RobotEmpire posted:Try giving your functions more descriptive names (and a few optimizations) code:
This uses a list comprehension: code:
This uses a generator expression: code:
xrange() returns an iterator. range returns a list. Scaevolus fucked around with this message at 08:14 on Apr 11, 2010 |
# ? Apr 11, 2010 08:11 |
|
devilmouse posted:Google is failing me (or rather, it's just cluttered up with old old no longer maintained code). Anyone know a serviceable package for editing video files in Python? There was a promising looking thing called DVEdit, but it's nowhere to be found. PyMedia hasn't been touched since '06. mystes fucked around with this message at 15:10 on Apr 11, 2010 |
# ? Apr 11, 2010 15:07 |
|
Scaevolus posted:This uses a generator expression: So I assume a generator expression creates a tuple vice a list? Or are the outer parens for grouping?
|
# ? Apr 11, 2010 16:59 |
|
Scaevolus posted:This uses a generator expression: Wait, so if you take a list comprehension, make sure all its component parts are generators, and replace the brackets with parens, then the whole thing is also a generator?
|
# ? Apr 11, 2010 16:59 |
|
RobotEmpire posted:So I assume a generator expression creates a tuple vice a list? Or are the outer parens for grouping? They're for grouping. It works just the same as how a generator would work, so no tuples are made. All though, I think that syntax is 2.6/3.x only.
|
# ? Apr 11, 2010 17:25 |
|
mystes posted:The python bindings for gstreamer may be your best bet. It's too bad there isn't any good up-to-date ffmpeg module since that was my first thought too. Yeah, looks like this is gonna have to do. It's too bad about that DVEdit package, it really looked like the tits too.
|
# ? Apr 11, 2010 17:27 |
|
RobotEmpire posted:So I assume a generator expression creates a tuple vice a list? Or are the outer parens for grouping? This is really important: an expression inside parens but without commas (commas inside other parens don't count) aren't tuples. You have to type (foo,) for a one-element tuple. Also function calls aren't tuples either.
|
# ? Apr 11, 2010 17:45 |
|
nbv4 posted:All though, I think that syntax is 2.6/3.x only. 2.5+, I believe
|
# ? Apr 11, 2010 18:10 |
|
Generator comprehensions were added in 2.4.
|
# ? Apr 11, 2010 19:01 |
|
Avenging Dentist posted:This is really important: an expression inside parens but without commas (commas inside other parens don't count) aren't tuples. You have to type (foo,) for a one-element tuple. Also function calls aren't tuples either. Buh! I knew this. Thanks for the reminder. As I get deeper into this MIT edit: On the other hand, I feel like I learn something important every loving lecture. I just got a brain dump about linear/log/exp/quad algorithms... and I actually understood the concepts by the end. RobotEmpire fucked around with this message at 19:40 on Apr 11, 2010 |
# ? Apr 11, 2010 19:18 |
|
Stupid python question, I'm sorting a 2x20 list with a string and a % integer, and I want to sort by the absolute value and put the highest 5 entries into a list. I suck at this but this is what I came up with:code:
|
# ? Apr 14, 2010 21:18 |
|
LuckySevens posted:Stupid python question, I'm sorting a 2x20 list with a string and a % integer, and I want to sort by the absolute value and put the highest 5 entries into a list. I suck at this but this is what I came up with: That should work: >>> l = [['',5],['',-2],['',2],['',1]] >>> sorted(l, key=lambda x: abs(x[1])) [['', 1], ['', -2], ['', 2], ['', 5]]
|
# ? Apr 14, 2010 21:30 |
|
|
# ? May 11, 2024 17:07 |
|
ah, i think its screwing up because my values have % symbols, ill just parse those. duh :P
|
# ? Apr 14, 2010 21:41 |