|
Bonus posted:Here's a cool thing I've just found out while doing some stuff for school. The zip function works as its own inverse. code:
|
# ? Mar 14, 2008 19:00 |
|
|
# ? Jun 13, 2024 06:59 |
|
Hey guys, I could need a short example script for one of my projects. It's a Java program that uses Jython for scripting support. Here's what I have. There's this function convert(data, offset, size) where data is a byte array, offset is a position inside the byte array, and size is a number of bytes. Starting at position offset, the function takes size bytes from the data array and converts it into a string that looks like a MASM (Microsoft Assembler) array. Example Output: myArray db 030h, 0F0h, 000h, 057h, ... Note that each data line should have at max 10 entries, then I want a line break into the next line. Here's what I have so far. code:
|
# ? Mar 15, 2008 12:52 |
|
I'm writing a little script to telnet around to different routers, but unfortunately the routers default to a little user interface (Netopia 3386) when establishing a telnet connection. Pressing ctrl-N drops the telnet window into a command line, but I cannot figure out how to send this control character through python. I've been using telnetlib just for proof of concept, but probably will use pexpect in my final program. How do I send control characters through either telnetlib or pexpect?
|
# ? Mar 15, 2008 15:00 |
|
inveratulo posted:How do I send control characters through either telnetlib or pexpect? Ok, this is a slight case of RTFM, because pexpect has a sendcontrol()
|
# ? Mar 15, 2008 15:57 |
|
Alan Greenspan posted:This returns the entire array in one huge line. Is there a cute way to add the line breaks? Nothing matters but shortness of the source code. Is there a way to make the code I wrote shorter? Write it in perl.
|
# ? Mar 15, 2008 16:28 |
|
deimos posted:Ok, this is a slight case of RTFM, because pexpect has a sendcontrol() hmmm sendcontrol was the first thing I tried actually but I couldn't get it to work. I kept getting an attribute error: AttributeError: 'spawn' object has no attribute 'sendcontrol' Should I be creating my connection object differently? This is how I did it: conn = pexpect.spawn('telnet ' + host) edit: I had version 2.1 which did not have sendcontrol(). I updated and now it works, thanks! I propose a new acronym: UTFM - Update The loving Modules. inveratulo fucked around with this message at 18:29 on Mar 15, 2008 |
# ? Mar 15, 2008 17:13 |
|
inveratulo posted:hmmm sendcontrol was the first thing I tried actually but I couldn't get it to work. I kept getting an attribute error: update your pexpect
|
# ? Mar 15, 2008 18:16 |
|
Alan Greenspan posted:Is there a short way to get rid of the leading 0 for values between 00 and 9F? MASM requires the leading 0 only for values starting with a character (0xA0 - 0xFF). You could try the old C-style and-or trick to determine if it needs a leading 0: code:
code:
code:
In your code (with the earlier addition) it will look something like this (also non-table breaking): code:
EDIT: Actually, just tried it, this doesn't seem to work properly with the join function, it will have a leading ', ' at the start of each new line. Bum! EDIT 2: Oh, easy workaround, just move the insertion of a newline to the start, and trigger for the item after every tenth item. Uh, that's not very clear, but here's what I mean: code:
Moof Strydar fucked around with this message at 21:21 on Mar 17, 2008 |
# ? Mar 17, 2008 13:29 |
|
Just FYI, you don't need those line continuation characters as new lines and whitespace have no significance inside parentheses (or brackets, or braces).
|
# ? Mar 17, 2008 15:04 |
|
Moof Strydar posted:
Just want to pop in with the usual "be really careful with this tactic" warning, it's really easy to make a stupid mistake like the following: code:
|
# ? Mar 17, 2008 16:23 |
|
EDIT: Figured it out, giving a uri like "twitter.com" to the HTTPPasswordMgr.add_password method instead of the full path to what I was trying to access works. edit2 ahh, more urllib2 questions... I wrote a basic class: code:
code:
code:
edit: ah, some cat at google already wrote a python twitter api... but I wanted to write my own. http://code.google.com/p/python-twitter/ editx3: hooray, got it working by doing what he did in his example and I did in my second code sample and added the Basic auth header as well. ashgromnies fucked around with this message at 19:24 on Mar 17, 2008 |
# ? Mar 17, 2008 18:19 |
|
Milde posted:Just FYI, you don't need those line continuation characters as new lines and whitespace have no significance inside parentheses (or brackets, or braces). Not only that, they'll actually cause errors in the last example, because you can't have comments after them!
|
# ? Mar 17, 2008 21:20 |
|
Moof Strydar posted:
code:
|
# ? Mar 18, 2008 01:42 |
|
Habnabit posted:
Yeah, that is the prefered usage but I don't think Jython supports that yet. Does anyone know if they'll be changing yield to a function in Py3k? It seems kind of silly not to, because they're already changing print to a function and now yield can give you a return value so writing foo = (yield value) instead of foo = yield(value) just seems kind of hackish and like it doesn't go along with the rest of Python.
|
# ? Mar 18, 2008 09:28 |
|
Bonus posted:Does anyone know if they'll be changing yield to a function in Py3k? It seems kind of silly not to, because they're already changing print to a function and now yield can give you a return value so writing foo = (yield value) instead of foo = yield(value) just seems kind of hackish and like it doesn't go along with the rest of Python. It appears so. code:
|
# ? Mar 18, 2008 10:22 |
|
I've been fumbling around with Python the last couple of days and studying various tutorials for Python itself and libraries such as pygame. However, I'm quite unexperienced in coding and with only a minor Java background I find it pretty hard to really learn and apply my learnings in practice without doing "homework assignments". It would really help my learning if I'd be able to program and think for myself given problems that I know can be done with the skills I've learned so far. Are there that kind of tutorials available? For example a university course with open material or something.
|
# ? Mar 18, 2008 15:42 |
|
Moof Strydar posted:
Thanks man, that's really sweet. I managed to find another solution with a helper function. code:
|
# ? Mar 18, 2008 19:30 |
|
If you make helper functions just so they're used once, you can always cram them in a lambda if you want stuff in one line. So your example can become:code:
|
# ? Mar 18, 2008 19:33 |
|
Ick, no. Make it easier to read! I'm in favour of more helper functions. Remember you can nest them.code:
|
# ? Mar 18, 2008 19:59 |
|
Yeah, I'm all for readability and I'd suggest going that way too. It's the most important characteristic of code, apart from elegance. Make the code so people can read it first, then for computers. But he mentioned that he wants the code to be as short as possible, so that's why I posted that solution. Although that presents the question, why would anyone want shortness of code over readability, apart from maybe limited storage space, but I doubt that's the issue..
|
# ? Mar 18, 2008 20:04 |
|
The reason for why I need it shorter is unbelievably silly. I work in a small, very specialized part of the industry where people know each other quite well. So when I post a readable 10-liner on my blog that shows an example of what you can do using our software/library it's very likely that some guy from the competition chimes in and posts an unreadable 1-liner that does the same in their software/library. Playing code golf with the plugin support of our products is just a way to amuse ourselves. I have real, readable examples for the software/library too, but this is the one-liner to one-up the competition.
|
# ? Mar 18, 2008 20:31 |
|
JoeNotCharles posted:
One thing about this: defining the functions nested like that is actually a performance penalty in this case because neither function use any of the local variables in the parent function directly. Every time the code is run, the functions are regenerated.
|
# ? Mar 18, 2008 20:50 |
|
Ech. Performance, schmerformance!
|
# ? Mar 18, 2008 21:00 |
|
Contract Otter posted:I've been fumbling around with Python the last couple of days and studying various tutorials for Python itself and libraries such as pygame. However, I'm quite unexperienced in coding and with only a minor Java background I find it pretty hard to really learn and apply my learnings in practice without doing "homework assignments".
|
# ? Mar 20, 2008 03:58 |
|
do it posted:This might be too basic for what you're looking for, but Learning Python has exercises at the end of each chapter. Thanks, I'll look into it!
|
# ? Mar 20, 2008 06:37 |
|
How do I convert a 4bit binary number to a decimal without using anything like character strings or anything like int("10111", 2)? I never learned how to do it manually e: this is a "so I know how to do it in the future" type of question!
|
# ? Apr 3, 2008 01:54 |
|
Edit: I assume when you say "never learned how to do it manually" you were wondering how to do that, but maybe I misread. Well, here's how to do it manually I guess... Simple way is to just add the values of the columns set to 1. Like: code:
code:
npe fucked around with this message at 03:15 on Apr 3, 2008 |
# ? Apr 3, 2008 03:09 |
|
drcru posted:How do I convert a 4bit binary number to a decimal without using anything like character strings or anything like int("10111", 2)? Binary isn't really a format like a string or an int. Your binary is represented by one of the built-in types such as an array of strings or a long or whatever. The conversion from <random type> to int is different for every <random type>. (there is a possibility that the language du jour has a special binary type, then again there will be a built-in function to go from binary to decimal) edit: wait did I read the question wrong; do you want an explanation of the logic behind the conversion from binary to decimal? Google is better equipped to handle that question. (edit2: and yaoi prophet too )
|
# ? Apr 3, 2008 03:14 |
|
drcru posted:How do I convert a 4bit binary number to a decimal without using anything like character strings or anything like int("10111", 2)? code:
But to reiterate, why do you want to do this?
|
# ? Apr 3, 2008 03:15 |
|
Destroyenator posted:
The reason it works for this case is that the python interpreter pre-allocates objects for the first 256 or so small integers as singletons. Fake edit: code:
|
# ? Apr 3, 2008 03:39 |
|
Thanks for the quick replies! The reason I want to know this is because it will likely end up on one of my exams and since they never specified how to convert the numbers in our assignments... I just used the easiest/quickest way One more question: If I get a string like "1234" how do I use division to substring them? I did something like this instead: code:
|
# ? Apr 3, 2008 03:40 |
|
No Safe Word posted:Don't do this. You want !=. Yes, it will work for this case, but in general when comparing a variable and a constant number you want the equality test, not the identity test.
|
# ? Apr 3, 2008 06:00 |
|
I don't know if you're allowed to use it, but you can do this:code:
|
# ? Apr 3, 2008 08:11 |
|
Bonus posted:I don't know if you're allowed to use it, but you can do this: I was using the int(string, base) too but yeah, I don't know if it's actually allowed. As for the division by substring (I'm not sure if that's the proper term) but basically, I think we were supposed to do something like charAt on a string of numbers. eg. 9765 should print out 9, 7, 6, and 5 individually.
|
# ? Apr 3, 2008 10:41 |
|
Aha, well if you're doing that with a string, you can do it like this.code:
code:
|
# ? Apr 3, 2008 11:11 |
|
drcru posted:eg. 9765 should print out 9, 7, 6, and 5 individually. A string is a sequence, like a list or a tuple, so you can just iterate over it: code:
|
# ? Apr 3, 2008 11:36 |
|
And, of course l=[x for x in "1234"] Edit: I am a mong, fixed tef fucked around with this message at 16:00 on Apr 3, 2008 |
# ? Apr 3, 2008 11:50 |
|
Here's an interesting way to split an integer into digits without converting to strings.code:
hey mom its 420 fucked around with this message at 12:22 on Apr 3, 2008 |
# ? Apr 3, 2008 12:04 |
|
tef posted:And, of course list=[x for x in "1234"] And of course not picking variable names that override/shadow builtin functions like list is preferred
|
# ? Apr 3, 2008 13:57 |
|
|
# ? Jun 13, 2024 06:59 |
|
tef posted:And, of course l=[x for x in "1234"] Fixed your variable name for you. l = list("1234") is easier and clearer.
|
# ? Apr 3, 2008 15:29 |