code logs -> 2007 -> Sat, 24 Nov 2007< code.20071123.log - code.20071125.log >
--- Log opened Sat Nov 24 00:00:43 2007
00:02 Vornicus [~vorn@ServicesOp.Nightstar.Net] has quit [Quit: Leaving]
00:03 Vornicus [~vorn@64.252.25.ns-4028] has joined #code
00:03 mode/#code [+o Vornicus] by ChanServ
00:11 You're now known as TheWatcher[T-2]
00:15 You're now known as TheWatcher[zZzZ]
00:31
<@Vornicus>
Arg. Why do I randomly ponder I7 as a MUD language?
00:31
<@McMartin>
I blame ifMUD.
00:32
<@Vornicus>
A true thing.
00:35
<@Vornicus>
But you see how it could sorta work... you'd just need to replace the Z machine with something that can do MUD.
00:35
<@McMartin>
Glulx has file output that you can play pipe games with.
00:36
<@McMartin>
There's sample code for embedding RSS feeds into Glulx games.
00:36
<@McMartin>
(The actual ifMUD is a PerlMUD.)
--- Log closed Sat Nov 24 01:41:11 2007
--- Log opened Sat Nov 24 01:41:46 2007
01:41 TheWatcher[zZzZ] [~chris@Nightstar-29731.dsl.in-addr.zen.co.uk] has joined #code
01:41 Irssi: #code: Total of 17 nicks [7 ops, 0 halfops, 0 voices, 10 normal]
01:41 mode/#code [+o TheWatcher[zZzZ]] by ChanServ
01:42 Irssi: Join to #code was synced in 44 secs
01:43 Chalcy [~Chalcedon@Nightstar-10789.ue.woosh.co.nz] has quit [Quit: Gone]
01:47 Forjeh [~Forj@Nightstar-10789.ue.woosh.co.nz] has quit [Quit: Gone]
03:14 Syloqs-AFH [Syloq@Admin.Nightstar.Net] has quit [Connection reset by peer]
07:35 GeekSoldier|bed is now known as GeekSoldier
08:39 You're now known as TheWatcher
10:46 Vornicus is now known as Vornicus-Latens
11:04 * Reiver scratches his head.
11:04
< GeekSoldier>
what's wrong, Reiver?
11:06
< Reiver>
Mostly my complete lack of knowledge of Python keeps tripping me up in stupid ways. >.>
11:08
< GeekSoldier>
anything specific? I may be able to help.
11:10
< Reiver>
Well, I'll likely figure it out myself soon enough anyhow, but
11:11
< Reiver>
I'm trying to use the .find() method to find a specific text string in a string of text loaded from a file, with the goal of then modifying the contents of said string and outputting it to a new file.
11:12
< Reiver>
It would help if line.find('Starting Position Offset Z') didn't return every line in the file as true~
11:13
<@McMartin>
So, find returns -1 if it's not there.
11:13
<@McMartin>
You want line.startswith(), I believe.
11:13
<@McMartin>
The command dir(obj) will list every possible invokable method on obj
11:13
<@McMartin>
Including strings.
11:14
<@McMartin>
dir('hi'), for instance
11:14
< Reiver>
Aha. -1 resolves as Not False, correct?
11:14
<@McMartin>
You can then get docs on any method by using help with the method as an argument.
11:14
<@McMartin>
Quite
11:14
<@McMartin>
But then it should in fact be rejecting exactly those that match
11:14
<@McMartin>
Because, after all, they match starting at index 0.
11:14
<@McMartin>
Try help("hi".startswith)
11:14 * Reiver nods.
11:15
<@McMartin>
And actually, there's an even easier way, IIRC
11:15
<@McMartin>
if 'Starting Position Offset Z' in line:
11:15
<@McMartin>
# stuff
11:15
<@McMartin>
"in" being the membership test.
11:15
< Reiver>
...Oh nice!
11:16
< GeekSoldier>
yeah, McM got it. :)
11:16
<@McMartin>
But, IIRC, you actually *want* startswith, to ensure that, say "Thing That Is Not Starting Position Offset Z" does not match.
11:16
< Reiver>
(FWIW, "if line.startswith('Starting Position Offset Z'):" was resolving correctly also.)
11:16
<@McMartin>
That more closely captures the semantics you want
11:16
<@McMartin>
Though of course it will also end up catching 'Starting Position Offset Zebra'.
11:17
<@McMartin>
The real real solution is to write a parser. >_>
11:17
< Reiver>
While useful to be aware of, that is thankfully not an issue in this situation.
11:17
<@McMartin>
Yeah
11:17
< Reiver>
Yeeaah, I may resort to that eventually, but for this wee project I'd rather pass on that~
11:17
< Reiver>
(If not least because I haven't actually yet learned how you'd do that, or at least the proper method~)
11:18
<@McMartin>
(If I recall your file format right, the relevant method would be string.split())
11:18
<@McMartin>
(You can split on ':=' and that would give you a tuple of (left hand side, right hand side).
11:18
<@McMartin>
)
11:18
< Reiver>
(I can then throw int(leftHandSide) at it and it will clean off whitespace, or do I have to clean that first?
11:19
< Reiver>
...hm, it mentions some thingy called 'Pickle'
11:19
<@McMartin>
(You'd want rightHandSide, and it autocleans. You'll have to strip() the string)
11:19 * Reiver starts poking.
11:19
<@McMartin>
Leave Pickle alone.
11:19
<@McMartin>
It's for Magic Binary Storage.
11:19
< Reiver>
...Maybe not then?
11:19
< Reiver>
O-kay?
11:19
<@McMartin>
Basically, you have a shitload of objects in your program.
11:19
<@McMartin>
You want to shove them to disk and reload them later
11:20
<@McMartin>
You don't care what the file looks like
11:20
<@McMartin>
then you can use pickle.
11:20
< Reiver>
Serialisation ftw?
11:20
<@McMartin>
As long as you don't upgrade Python too far in between.
11:20
<@McMartin>
Yes, it's the opaque serialization interface.
11:20
<@McMartin>
And language-based serialization is a recipe for >_<.
11:21
< Reiver>
Lovely. So it is used for quick-and-dirty, and shouldn't really have been done the way it was?~
11:21
<@McMartin>
Ehn.
11:21
<@McMartin>
I think you can do IPC with it safely.
11:21
<@McMartin>
But it's a terrible long-term storage solution. Do that with text or a custom IFF FORM.
11:21
< Reiver>
Right.
11:21
< Reiver>
(IPC?)
11:21
<@McMartin>
Inter-Process Communication.
11:22
< Reiver>
Oh, right.
11:22
<@McMartin>
"Here, other instance of me. Have this wad of data." "Thanks!"
11:22 Thaqui [~Thaqui@Nightstar-13064.jetstream.xtra.co.nz] has quit [Quit: This computer has gone to sleep]
11:22
<@McMartin>
Argh, damnj00 popcap
11:22
<@McMartin>
Stop chewing on my hindbrain
11:22
< Reiver>
?
11:23 * McMartin has been burning through various PopCap demos, and they are chewing on his hindbrain.
11:23
<@McMartin>
Especially Bejeweled 2.
11:24 * McMartin notes http://www.penny-arcade.com/comic/2001/01/08 and http://www.penny-arcade.com/2006/12/01 -- the latter of which he recently beat.
11:27 * Reiver rofls
11:27
< Reiver>
>>> help split()
11:27
< Reiver>
Invalid syntax error
11:28
<@McMartin>
help("".split)
11:28 * Reiver realises, but still found it funny in an odd way.
11:32
<@McMartin>
... aw, they removed the easter egg.
11:33
<@McMartin>
>>> from __future__ import * File "<stdin>", line 1
11:33
<@McMartin>
SyntaxError: future feature * is not defined
11:33
<@McMartin>
That used to be
11:33
<@McMartin>
FutureError: You wish
11:33
< Reiver>
...pfffft
11:33
< Reiver>
That's... entirely appropriate~
11:34
<@McMartin>
That's because it trapped it to give the error
11:34
< Reiver>
?
11:38
<@McMartin>
The current error is just a generic interpreter error.
11:39
< EvilDarkLord>
They still have 'import this' though.
11:40
<@McMartin>
The old one was, for all practical purposes, an easter egg, looking for the line
11:40 * McMartin tries 'import this', cracks up
11:40
<@McMartin>
That contains great wisdom.
11:40
< GeekSoldier>
heh, wow.
11:41
<@McMartin>
I had never seen that. Thanks.
11:42
<@McMartin>
"Now is better than never -- though never is often better than *right* now" is actually directly relevant to my current projects.
11:47
< Reiver>
...I actually totally approve of that.
11:49 * EvilDarkLord thought this was common knowledge, but happy to help :)
13:09
< Reiver>
Hrn
13:20 * Kyrre PatPats Reiver.
13:22
< Reiver>
For some reason I am failing at tuples. How do I get the second string out? This is obvious as all getout, I know. >.>
13:23
< GeekSoldier>
k,v = (k1, v2)
13:23
< GeekSoldier>
discard k, use v.
13:24
<@McMartin>
The other way is (a, b)[1]
13:24
<@McMartin>
(1, 2)[1] returns 2.
13:24
<@McMartin>
(zero-indexed)
13:24 * GeekSoldier forgot about being able to index into tuples...
13:24
< GeekSoldier>
very useful, that.
13:24
<@McMartin>
Been hitting the Haskell lately, have we?
13:25
< GeekSoldier>
just a taste.
13:26
< Reiver>
...I was trying to get McM's approach, and somehow failing~
13:29 * Reiver was botching the syntax on zaxis = (line.split(':='))[1]
13:32
< Reiver>
...oho, should have thought of that one shouldn't I.
13:32
< Reiver>
The offset is not nessisarily an integer: ValueError: invalid literal for int() with base 10: '130.2\n'
13:36
< Reiver>
Do I need to be careful of anything when using float() in Python?
13:37 * Reiver is aware some languages do Odd Things with decimals in floats, with the occasional 12 being printed as 11.99997847921 or the like~
13:38
< GeekSoldier>
not that I have encountered yet, but I don't tend to go into deep floats or anything like that.
13:40
< Reiver>
I just want it to return 120.2 when I tell it to subtract 10 from 130.2 and print the result~
13:41
< GeekSoldier>
hmm. well, I'm getting Odd Things now...
13:43
< GeekSoldier>
print "%.1f" %(x)
13:43
< GeekSoldier>
hehe. dirty, but it prints the rightthing.
13:43
< GeekSoldier>
prior to that, it was 120.1999999999999
13:43
< Reiver>
Wootness, it works!
13:44
< GeekSoldier>
sweet.
13:44
< Reiver>
Now to simply cobble together the file writer, and I will be done.
13:46
< GeekSoldier>
excellent.
13:46
< Reiver>
And this will have taken... *stare*
13:47
< Reiver>
9 lines?
13:47
< GeekSoldier>
in what?
13:47
< Reiver>
Python.
13:48
< GeekSoldier>
ofcourse. I thought you were comparing to another lanuage.
13:48 * Reiver last coded in Java, is used to barely managing Hello World in that much~
13:49
< GeekSoldier>
ah. are you enjoying Python a bit more at least?
13:49
< Reiver>
Ye gods yes.
13:49
< Reiver>
Still holding a degree of caution about just how refactoring works out in the long run, buuut...
13:51
< GeekSoldier>
I have to do Java in my next class.
13:51
< Reiver>
Boilerplate ftl~
13:52
< GeekSoldier>
I am none all too pleased with that. though they do apparently emphasize Jython..
13:53
< Reiver>
?
13:54
< GeekSoldier>
Jython is Python bindings for Java classes.
13:54
< GeekSoldier>
aka sugar to help the medicine go down.
13:54
<@McMartin>
Jython is a Bad Idea. All the good stuff in Python comes from after where Jython forked. ={
13:54
<@McMartin>
=P
13:55
< Reiver>
What's the good stuff?
13:55
<@McMartin>
Proper lexical scoping, mainly.
13:55
<@McMartin>
Generators.
13:56
< Reiver>
OK
13:57
< Reiver>
Stupid Question Time #42: How do I get my lines which I am currently outputting with print to be outputted into a file?
13:59
<@McMartin>
Heh. Not a stupid question at all, as Python is Wacky (tm) about that.
13:59
<@McMartin>
First you open a file for writing.
13:59
<@McMartin>
Let's call it f.
13:59
<@McMartin>
instead of:
13:59
<@McMartin>
print "La la la"
13:59
<@McMartin>
You instead write:
13:59
<@McMartin>
print>>f, "La la la"
14:00
< Reiver>
...So throw in a declarative line up top, then a >>target onto each print and that's as good as done?
14:02
< GeekSoldier>
f = file.open("name", 'w') or something of the sort.
14:02
<@McMartin>
Yeah.
14:02
< Reiver>
source = open('Copy of XFileClasses_Stellar.txt', 'r')
14:02
< Reiver>
target = open('XFileClasses_Stellar.txt', 'w')
14:02
< Reiver>
Is what I have.
14:02
<@McMartin>
No
14:02
< Reiver>
Nein?
14:02
<@McMartin>
Don't open target until after you've read everything in
14:02
<@McMartin>
Otherwise you'll obliterate whatever's in target.
14:02
< Reiver>
...Is that bad?
14:02
< Reiver>
They're different files.
14:03
<@McMartin>
For now.
14:03
<@McMartin>
Eventually they may not be
14:03
< GeekSoldier>
he's opening a copy for reading, then writing to the original.
14:03
< GeekSoldier>
it's not that bad.
14:03
<@McMartin>
Until you go to make it be "in-place" and nuke the world before you get your data.
14:03 * McMartin belongs to the school of thought where many things that are safe as written still aren't safe.
14:03
< GeekSoldier>
true enough.
14:04 * Reiver nods.
14:04
< Reiver>
Hm. That would complicate my code somewhat. I was reading in, and then outputting, in the same place in the file.
14:04
<@McMartin>
Mmm.
14:04
< Reiver>
I was partly wondering about loading the whole file, modifying the whole thing in-memory, then dumping it out as a manouver to accelerate the code, though.
14:05
<@McMartin>
Yeah, that would be The Normal Way.
14:05
< GeekSoldier>
unless it's massive.
14:05
<@McMartin>
Yeah
14:05
< GeekSoldier>
naturally.
14:05
<@McMartin>
Actually, I lie
14:05
<@McMartin>
The "normal" way is to read from stdin and write to stdout, and use pipes for everything~
14:05
< Reiver>
1-9 MB is the most I've seen.
14:05
< Reiver>
So not massive.
14:06
< GeekSoldier>
is it likely that these datafiles will get large?
14:06
< Reiver>
Not much bigger than that, no.
14:06
< Reiver>
The current, bigger-than-default file I'm working on is 2.9MB.
14:07
<@McMartin>
Then don't worry about speed hacks
14:07
<@McMartin>
At least for now, you've got an Actual Answer You Need, so get it.
14:10
< Reiver>
Just a quick query: Why is 'Starting Position Offset Z := ' invalid syntax?
14:10
< Reiver>
When trying to print a string?
14:10
<@McMartin>
Give me the full line, please?
14:11
< Reiver>
print>>target 'Starting Position Offset Z := ', (float(zaxis))
14:11
<@McMartin>
You need a comma after target
14:11
< Reiver>
print>>target, ?
14:11
<@McMartin>
YEs
14:13
< Reiver>
Aha!
14:13
< Reiver>
What's the logic behind that, out of curiosity? It seems kind of arbitary to need it there.
14:14
<@McMartin>
There isn't much.
14:14
<@McMartin>
Guido von Russom likes the syntax. =P
14:14
<@McMartin>
The large number of keywords, plus the indentation rules, are to keep the language LL(1).
14:14
< Reiver>
Ah...hah~
14:15
< GeekSoldier>
it's like what Larry Wall said about perl's elsif command.
14:15 * Reiver ponders.
14:15
< Reiver>
Right, it works!
14:16
< GeekSoldier>
something along the lines of "if you miss the 'e', then make your own language then make it popular"
14:16
< Reiver>
Also, the main reason I wrote it to take one file and output another is so I can screw with the value of the Z-offset and keep it an absolute shift in value, rather than a relative one~
14:17
< Reiver>
(As it stands, it's applying a +/- x to the z-axis offset. If it opened and then ran on the file, running the file three times would mean it got offset 3x the amount.)
14:17
< Reiver>
Next step in Complicating The Code is indeed, to put in a comment that specifies the current offset, so the code can allow for it. (And to tell people what you've done. >.>)
14:20
< Reiver>
While I'm here I may as well work out how to load the origional file into memory before one starts writing the destination. Is that hard to do? I saw a readlines() function which suggests there's at least one way to skin this bunny. I'm wondering if it's the wise one~
14:22
< GeekSoldier>
if the file is layed out in lines like that, then it would be good.
14:22
< GeekSoldier>
lines = []
14:23
< GeekSoldier>
bah.. got my wires crossed.
14:23
< Reiver>
?
14:27
< GeekSoldier>
ok... that would work.
14:27
< GeekSoldier>
initialize a list (lines = [])
14:27
< GeekSoldier>
open the file for reading (f = open("name", 'r'))
14:28
< GeekSoldier>
then append them on:
14:28
< GeekSoldier>
lines.append(f.readlines())
14:28
< GeekSoldier>
that should populate lines with the file, indexed one per line.
14:29
< Reiver>
...hmn
14:30
< Reiver>
This is because readlines() creates a godawfulhuge tuple, rather than an array?
14:30
< GeekSoldier>
...no. it made a godawfulhuge list.
14:31
< Reiver>
Oh. Hrn. Whyfor the appending, then?
14:32
< GeekSoldier>
bah. did not see that...
14:32
< Reiver>
Sorry, not trying to be nitpicky. Just trying to understand why I'm doin' what I'm doing. >.>
14:32
< Reiver>
Also, uh. How does one force a newline to appear in a text statement rather than the actual physical letters /n?~
14:32
< GeekSoldier>
no, you were right.
14:32
< GeekSoldier>
lines = f.readlines()
14:33
< GeekSoldier>
double quotes.
14:33
< Reiver>
...Aha.
14:34
< Reiver>
...Apparently not.
14:35
< GeekSoldier>
it's \n
14:36
< Reiver>
...Gah!
14:36
< Reiver>
I keep doing that
14:36
< GeekSoldier>
also, single quotes will expand \n.
14:36 * GeekSoldier screwed that up, again.
14:39 * Reiver is currently distracted by making the produced file the same as the initial file.
14:40
< Reiver>
Is there a way to make a float not use decimals if there are no decimal places present?
14:40 GeekSoldier [~Rob@Nightstar-4455.pools.arcor-ip.net] has quit [Ping Timeout]
14:40
< Reiver>
This isn't a major, as the file types are compatable either way, but it'd be nice to have that final niggle clarified~
14:44 GeekSoldier [~Rob@Nightstar-4783.pools.arcor-ip.net] has joined #code
14:44
< GeekSoldier>
gah... what did I miss? last I had was when I said that I screwed it up again.
14:45
< Reiver>
Sun[03:43] * Reiver is currently distracted by making the produced file the same as the initial file.
14:45
< Reiver>
Sun[03:44] <Reiver> Is there a way to make a float not use decimals if there are no decimal places present?
14:54
< GeekSoldier>
make it an int?
14:56
< Reiver>
I have a small number of values that use single-point decimals.
15:01
< Reiver>
Does python support... switch statements, I think they were?
15:02
< Reiver>
You know, rather than having a giant set of nested ifs, you could have a single list of potential results in the same method.
15:07
< GeekSoldier>
no switch.
15:07
< Reiver>
Why on earth not?~
15:08
< GeekSoldier>
if, elif, elif, elif, else.
15:09
<@AnnoDomini>
That sounds like a drunken song.
15:38
<@ToxicFrog>
Reiver: because switch is traditionally a computed goto with fallthrough, which can easily result in bugs, and you can fake it with dictionaries + lambdas in any case.
15:39
<@ToxicFrog>
By "not use decimals", do you mean when displayed, or internally?
15:40
<@ToxicFrog>
Also, GeekSoldier. Concerning fstab.
15:40
<@ToxicFrog>
mount(8) has the gory details, but you need at least one of the umask, uid, or gid options.
15:41
<@ToxicFrog>
The default is uid=0,gid=0,umask=077 - ie, owned by root and not usable by anyone else.
15:44
< GeekSoldier>
ah, thanks, TF!
15:45
<@ToxicFrog>
The one I use is uid=500,gid=100,umask=027 - owned by me with full access, the "users" group can read it and no-one else can touch it.
15:46
<@ToxicFrog>
You might also want to look at ntfs-3g, a FUSE module which I find generally works better than kmod-ntfs.
15:46
<@ToxicFrog>
I've never been able to get it to work with fstab, though, I have to put it in rc.local.
15:52
< Reiver>
TF: What I mean is I don't want decimals displayed/outputted unless it actually needs a decimal value.
15:54
< EvilDarkLord>
if i == int(i): Output integer version?
17:06 You're now known as TheWatcher[afk]
18:15 You're now known as TheWatcher
19:08
<@Vornicus-Latens>
Switches in Python are accomplished using a dictionary.
19:09
<@Vornicus-Latens>
You can store functions in various structures by name, or you can create anonymous functions via lambdas
19:14
< GeekSoldier>
isn't it just easier to go the if, elsif, else route?
19:15
<@Vornicus-Latens>
GS: often, no. A dictionary-based switching routine can be a hell of a lot clearer.
19:15
<@ToxicFrog>
Depends on what you're doing.
19:16
<@ToxicFrog>
Case in point, my risc3380 assembler, which uses a mnemonic => function map to emit code.
19:16
<@ToxicFrog>
Rather than a 30+ clause if-elseif.
19:16
<@Vornicus-Latens>
Reiver: The format string %f will not place decimals if they're not necessary.
19:16
< GeekSoldier>
hmm. interesting. I shall look more into that, then.
19:16 * EvilDarkLord has never used dictionaries with lambdas, must try that sometime.
19:18
<@ToxicFrog>
So we have, for example, ops["sub"] = function(r3,r1,r2) return emit("00001"..r1.."0"..r2.."0"..r3) end
19:18 Vornicus-Latens is now known as Vornicus
19:19
<@ToxicFrog>
This also means you don't need all your branches in one place; using an if-chain, everything ends up in one huge function, but using a dictionary, you can fill it in in different places in the program.
19:19
<@ToxicFrog>
Eg, one module contains the math/logic instruction handlers, one contains the flow control handlers, one contains the load/store instructions, and one contains macros, and all of them create entries in the 'ops' dictionary.
19:24
<@Vornicus>
Schlockian used it for its operator data - because of the nature of the operators you end up with not only needing the function it calls, but how many operands it pulls off the stack, and what its priority is. It didn't make much sense to put all this data into a giant ifelse thing because it would be lines and lines of boilerplate for each operator.
19:24
<@Vornicus>
So I wrote the operator processing bits once, and had it talk to the dictionary to get the specifics of its design.
19:24
<@Vornicus>
Dictionaries are your friend.
19:25
<@ToxicFrog>
Indeed.
19:25
<@ToxicFrog>
What was the saying? "Smart data is easier to write, easier to debug, and easier to maintain than smart code"?
19:27
<@Vornicus>
Indeed.
20:13 Reiver [~reaverta@Admin.Nightstar.Net] has quit [Ping Timeout]
20:23 Orthia [~reaverta@118.90.63.ns-26976] has joined #Code
21:08 AnnoDomini [AnnoDomini@Nightstar-29051.neoplus.adsl.tpnet.pl] has quit [Ping Timeout]
21:09 AnnoDomini [AnnoDomini@Nightstar-29221.neoplus.adsl.tpnet.pl] has joined #Code
21:09 mode/#code [+o AnnoDomini] by ChanServ
21:55 Thaqui [~Thaqui@Nightstar-13064.jetstream.xtra.co.nz] has joined #code
21:55 mode/#code [+o Thaqui] by ChanServ
22:16 Orthia is now known as Reiver
22:18 Reiver is now known as Reiv
22:18 Reiv [~reaverta@118.90.63.ns-26976] has quit [Quit: Changing servers]
22:19 Reiver [~reaverta@118.90.63.ns-26976] has joined #Code
22:19 mode/#code [+o Reiver] by ChanServ
22:29
<@Reiver>
Hmm.
22:29
<@Reiver>
Now that's a little more interesting.
22:30
<@Reiver>
I have my code working pretty nicely for 'Starting Position Offset Y :=', indeed, testing shows a value of approximately 8 was all that was needed.
22:32
<@Reiver>
I now need to get 'Effect 1 Offset Position Y := 0' to do the same. The minor catch is, it's really Effect X, and said value must be preserved.
22:33
<@Reiver>
So I need to be able to search for the line (easy), then modify the end of the line (easy), whilst preserving part of the line I wasn't searching for?
22:35
<@Reiver>
...I could just start using split() more intelligently than I already am, I suppose...
22:38
< GeekSoldier>
store each line. if it turns out that you don't need it after testing, then just put it back in place.
22:38
<@Vornicus>
or you could parse it full-on and then regenerate it. I did that for parts of an SEIV component thingy.
22:39
<@Reiver>
I think I have it.
22:39
<@Reiver>
What used to be
22:39
<@Reiver>
if line.startswith('Starting Position Offset ' + axis):
22:39
<@Reiver>
print>>target, 'Starting Position Offset ' + axis + ' :=', (float((line.split(':='))[1]) + offset)
22:40
<@Reiver>
Is now
22:40
<@Reiver>
if line.startswith('Starting Position Offset ' + axis):
22:40
<@Reiver>
print>>target, line.split(':=')[0], (float((line.split(':='))[1]) + offset)
22:40
<@Vornicus>
Note that if you're automatically generating these, I don't think SE cares about space.
22:40
<@Reiver>
Technically speaking, SEV doesn't care, no
22:40
<@Reiver>
But its datafiles are constructed so that the information falls into tidy columns.
22:40
<@Vornicus>
But you're being a formatting nazi?
22:41
< GeekSoldier>
it seems that it is required in this case.
22:41
<@Reiver>
If you break the pattern, it would be substantially Messy(tm), and make eyeball parsing much harder.
22:41
<@ToxicFrog>
I note that printf() has formatting flags for that kind of alignment.
22:41
<@Reiver>
(Also you can theoretically occasionally get Odd Things in SEV if you don't, so it's best to leave it as is~)
22:41
<@ToxicFrog>
V. useful.
22:42
<@Reiver>
TF: Oh? Shiny.
22:42 * Vornicus fiddles
22:42
<@Vornicus>
printf() being the string % fields thing in Python
22:43
<@Reiver>
And vorn: What I'm actually doing is modifying a previously written file. >.>
22:43
<@Reiver>
It's just, uh, a 2.9 MB datafile I didn't really want to alter by hand~
22:43
<@Vornicus>
I don't know if there's one for strings though
22:44
<@ToxicFrog>
What kind of modifications are you doing, anyways?
22:45
<@Vornicus>
...ah, you can, and it right justifies it.
22:45
<@Reiver>
TF: You know how the planets, warp points, etc in SEV sit above the hex grid?
22:46
<@Reiver>
So if you zoom in (Which flattens the view to closer to paralell with the hex grid), you can end up with perspective errors, when a planet appears over one hex, but is really the one a couple hexes closer?
22:46
< GeekSoldier>
SEV?
22:46
<@Reiver>
Space Empires V
22:46
< GeekSoldier>
ah ha.
22:47
<@Reiver>
I'm trying to drop all the stellar objects in the game down by a small amount, so that their equators all line up with the hex.
22:47
<@Vornicus>
>>> print "%10s blarg" % "foo"
22:47
<@Vornicus>
foo blarg
22:48
<@Vornicus>
Not exactly what you want. Let me see.
22:48
<@ToxicFrog>
No, that looks right.
22:48
<@Vornicus>
no, it's wrong
22:48
<@Vornicus>
You want it /left/ justified.
22:48
<@ToxicFrog>
You can use %-10s for left-aligned, if you need it
22:48
<@Vornicus>
...really?
22:48 * GeekSoldier nods.
22:48
<@Vornicus>
...oh snap.
22:48
<@Vornicus>
>>> print "%-10s blarg" % "foo"
22:48
<@Vornicus>
foo blarg
22:49
<@Reiver>
Also has the minor aesthetic bonus of making the hex grid less of a chessboard and more of a galactic measurement system.
22:49
<@Reiver>
But really, I'm doing it because I need the practice and this seemed a good place to start coding again~
22:49
<@ToxicFrog>
I would probably just use awk here~
22:49
<@Reiver>
I wanted to learn Python, though.
22:49
<@Vornicus>
That's because you have Cthulhu For Brains.
22:50
<@Vornicus>
THE BRAIN FROM BEYOND SANITY
22:50
<@Reiver>
And the rest of SEV uses Python as the scripting language, so it has a double convinience.
22:50
<@ToxicFrog>
Vornicus: come on, it's like four lines in awk, if that
22:50
<@Reiver>
TF: ...?
22:51
<@ToxicFrog>
awk is a text processing language.
22:51
<@Vornicus>
one line of awk is too much, imo
22:51 * ToxicFrog eyes vorn
22:51
< Kyrre>
It just an R away from being Rawk.
22:51
<@ToxicFrog>
What do you use instead of | awk '{ print $2 }' to pick out fields in bash scripts?
22:52
<@Reiver>
TF: If it helps, it's largely four lines of code in Python too, except I'm trying to be a good boy and use seperate lines for explicitly stating load/save files, etc~
22:52
<@Vornicus>
I'm exaggerating - but I do find awk to be... eccentric to say the least.
22:55
<@ToxicFrog>
Well, it doesn't work the same way as <insert typical imperative language here>
22:55
<@ToxicFrog>
But it's damn useful.
22:58
<@Reiver>
brb
22:59
< EvilDarkLord>
numberdic[x] = numberdic.get(x, 0) + 1 <-- is there some more graceful way of doing this in Python?
23:01
< EvilDarkLord>
(It's something I do pretty regularly these days, so I thought I'd ask. Not that this is ugly really.)
23:01
<@ToxicFrog>
...what does that do?
23:01
<@ToxicFrog>
Increments numberdic[x] if it exists, sets it to 1 otherwise?
23:01
<@Vornicus>
Yes.
23:01
< EvilDarkLord>
Yeah.
23:01
<@Vornicus>
That's about how I'd do it.
23:02
<@Vornicus>
You might want to hunt for a defaulting dictionary thing, but it's how I'd do it.
23:02 * EvilDarkLord did it with an if clause for some time, then learned about this.
23:02
<@McMartin>
Likewise
23:02 Reiv [~reaverta@Nightstar-11723.xdsl.xnet.co.nz] has joined #Code
23:02
< EvilDarkLord>
Thanks for the confirmation then.
23:02 Reiver [~reaverta@118.90.63.ns-26976] has quit [Ping Timeout]
23:02
<@McMartin>
Though when I do it it's usually multibag.get(x, []).append(y)
23:03
< Reiv>
Argh!
23:03 Reiv [~reaverta@Nightstar-11723.xdsl.xnet.co.nz] has quit [Quit: Changing servers]
23:03
<@McMartin>
Hum. No, actually, that one needs the ifs.
23:03 * EvilDarkLord headdesk, for I think I've done that with +[y] until now for whatever brainfarty reason.
23:04 * EvilDarkLord goes test that.
23:04 Reiver [~reaverta@118.90.79.ns-4997] has joined #Code
23:04 mode/#code [+o Reiver] by ChanServ
23:05
< EvilDarkLord>
dic[x] = dic.get(x, []) + [y] is how I do it.
23:06
<@Vornicus>
+ makes a copy - in general with lists you want .append() because it's much faster, especially on larger lists.
23:06
< EvilDarkLord>
Hm. I thought Python could optimise that these days.
23:07
<@McMartin>
The problem with .append(), however, is that it returns None.
23:07
<@McMartin>
Which menas this form doesn't work.
23:09
<@Vornicus>
Using append means you need the if.
23:15
< EvilDarkLord>
Ah, you are right. Python knows how to optimise ginormouslist += [y], but not ginormouslist = ginormouslist + [y].
23:29
<@Reiver>
elseif line.startswith(' Offset Position ' + axis):
23:30
<@Reiver>
Throws an error.
23:30
<@Reiver>
Does elseif actually exist?
23:31
< EvilDarkLord>
elif is what you want.
23:31
<@Reiver>
Ah!
23:32
<@Reiver>
Cheers.
23:33
<@Reiver>
hrm
23:34
<@Reiver>
Is there any way to put a wildcard into a startswith() function?
23:35 * Reiver wants 'Effect [X] Starting Offset := [Y]'
23:35
< EvilDarkLord>
What are you using at the moment? Regular expressions?
23:36
<@Reiver>
if line.startswith('Starting Position Offset ' + axis):
23:36
<@Reiver>
Is finding me 'Starting Position Offset Y := [Y]'. I'm then modifying [Y].
23:37
< EvilDarkLord>
So you want to know if that string is in the line?
23:38
<@Reiver>
I want to find lines that say 'Effect 1 Starting Offset Y := [Y]', but also 'Effect 2 Starting Offset y := [Y]', etc.
23:40
< EvilDarkLord>
I'd use regexes, mebbe. import re, re.findall('Effect [0-9]* Starting Offset y := \[(Y)\]'.
23:40
< EvilDarkLord>
(But then, my first reaction to a lot of things is to use regex.)
23:42
< EvilDarkLord>
(Oh, and that is incorrect by me. You want reg = re.compile('Effect [0-9]* Starting Offset y := \[(Y)\]'); reg.findall(stringyousearchin) )
23:43 * EvilDarkLord is probably being confusing, sorry.
23:44 * Reiver hasn't used regexes much.
23:44
< EvilDarkLord>
Anyway, simpler method.
23:45
< EvilDarkLord>
position = searchable.find('Starting Offset')
23:46
< EvilDarkLord>
if line.startswith(searchable, start=position): do stuff
23:48 * EvilDarkLord ponders. That seems to just say that the string is to be found in the line at all. Is this sufficient for your needs?
23:48
< EvilDarkLord>
I think if you need to check for something specific at the beginning before the 'Starting position offset blah' part, your best bet is regexes.
23:49
< EvilDarkLord>
If you just need to check whether the string contains 'Starting position offset blah', then it's trivial with the 'in' keyword.
23:53
<@Reiver>
So what does [0-9]* do? The * specifically?
23:54
< EvilDarkLord>
Any amount of preceding bit of regex.
23:54
< EvilDarkLord>
* is 0 to infinity, + is 1 to infinity.
--- Log closed Sun Nov 25 00:00:52 2007
code logs -> 2007 -> Sat, 24 Nov 2007< code.20071123.log - code.20071125.log >