The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
I am adding functionality to my IRC dicebot that will allow persistent tracking of individual's experience, money, and items (which I'm calling a character account). The way the bot is set up is that it has a thread listening on a socket for input which uses a queue to pass to a master thread, which handles the input. If the socket input is a private message, it executes the private message handling logic.
Now, all actual interaction with the character accounts will therefore be in the private message function, as any requests to change around variables will be received there. The problem, then, is that I want an account to persist once opened, because it's being saved to a file and it'd be really stupid to receive "add ten dollars to desc's account" and then open a file, read it all into memory, fiddle with desc's bits, write the changed file, and send the message "you have successfully fiddled with desc's bits." So I would like the first call to have to be either "create" or "open," and calls afterwards all work on an object in memory.
Is there a way to do this? The main issue is that, since it's inside of a function, I can't really create an object and have it persist. Global variables are not great practice. I guess I could have a thread run on any given account, and use a SEPARATE queue to communicate with the master thread, but that seems extremely poor in terms of design.
How would you guys do this?
The code is in Python, by the way.
Firstly, that's not so stupid of a design. It's a light duty program, it's a hobby project, and computers automatically cache small, commonly opened files, so the open/read/modify/write cycle is going to be a lot faster than you imagine. Like less than 1ms. My first advice is just leave it the way it is. This is a simple and shockingly robust design. In network apps, this is kind of what the file system is for.
Next, the "more efficient" design, which may in practice not actually be more efficient, is to have a design where you have a user object that contains all their info. This object has a function for writing its self out to disk and loading off of disk. You store all these user objects in a collection in your main thread. When you get a new command, you check the user name and look in your collection for that user. If they aren't found, look on disk for a file. If a file is found, make a new instance of the user class with that file and put it in the collection. If no file is found, initialize a blank user with that name. Next, process the message, modify the class in-memory, and call the write-out to disk function. You could even have another thread where you queue up the write out to disk functions but then you need to worry about locking the user objects so you don't accidentally modify one while it is being written.
But like I said, this is classic premature optimization.
Thousands of hot, local singles are waiting to play at bubbulon.com.
it'd be really stupid to receive "add ten dollars to desc's account" and then open a file, read it all into memory, fiddle with desc's bits, write the changed file, and send the message "you have successfully fiddled with desc's bits."
how slow would this actually be?
Depends on whether you have one file containing the data for all players (in which case: depends on the number of players) or one file per players (in which case: not very long, unless that file is keeping a log of every single transaction or something).
My answer would be that the efficiency of the read/write times is not a concern, unless this bot is going to be servicing thousands of people simultaneously. Just typing out "fiddle with dec's bits" takes orders of magnitude longer than fiddling with desc's bits regardless of how you do it, so don't worry about it.
However, the question "where do I put data I need 'globally' if global variables is bad?" is important. Generally, you'd give the variable to an object, namely the master thread object. If I recall correctly, in Python this should be as simple as using self.ListOfOpenFiles or somesuch, I dunno. I don't like how wishy-washy Python is, honestly.
I am adding functionality to my IRC dicebot that will allow persistent tracking of individual's experience, money, and items (which I'm calling a character account). The way the bot is set up is that it has a thread listening on a socket for input which uses a queue to pass to a master thread, which handles the input. If the socket input is a private message, it executes the private message handling logic.
Now, all actual interaction with the character accounts will therefore be in the private message function, as any requests to change around variables will be received there. The problem, then, is that I want an account to persist once opened, because it's being saved to a file and it'd be really stupid to receive "add ten dollars to desc's account" and then open a file, read it all into memory, fiddle with desc's bits, write the changed file, and send the message "you have successfully fiddled with desc's bits." So I would like the first call to have to be either "create" or "open," and calls afterwards all work on an object in memory.
Is there a way to do this? The main issue is that, since it's inside of a function, I can't really create an object and have it persist. Global variables are not great practice. I guess I could have a thread run on any given account, and use a SEPARATE queue to communicate with the master thread, but that seems extremely poor in terms of design.
How would you guys do this?
The code is in Python, by the way.
First of all, the typical OO solution to that kind of problem (having an object that persists across multiple calls of a function) would be to wrap the whole thing--or at least most of it--in a class. Then your initialize function would check to see if your file exists, open up the file, and dump the info into a class variable. Since that variable is at the class rather than function scope, it would persist across multiple calls of a function.
Second of all, for this particular problem, you might want to look into a nosql solution like MongoDB. Depending on what exactly you're doing with your dicebot that might be a bit overkill, but probably worth looking into for future reference anyway.
The problem with the first thing is that I don't necessarily want to create it, and it seems inelegant to have a massive class with subobjects that I'm maybe not going to use. At that point it's barely different from a global variable, right?
I will check those in 2. Though yeah, probably total overkill :P
If it's very complicated, it's probably worthwhile to just do a class that represents characters, and handle individual updates there.
Everytime I watch this film, I feel sadness at the dearth of politicians and public figures who spend time critically examining their past actions in a way that's not mostly self serving.
0
ShivahnUnaware of her barrel shifter privilegeWestern coastal temptressRegistered User, Moderatormod
Thanks, DK
Yeah I suppose it is hella premature optimization, I just don't want to have to rewrite later and also figure this kind of question is something I could see later, and should learn more about. Your post answers that well (as well as pointing out I'm premature in worrying about it) and is appreciated.
0
Apothe0sisHave you ever questioned the nature of your reality?Registered Userregular
I am adding functionality to my IRC dicebot that will allow persistent tracking of individual's experience, money, and items (which I'm calling a character account). The way the bot is set up is that it has a thread listening on a socket for input which uses a queue to pass to a master thread, which handles the input. If the socket input is a private message, it executes the private message handling logic.
Now, all actual interaction with the character accounts will therefore be in the private message function, as any requests to change around variables will be received there. The problem, then, is that I want an account to persist once opened, because it's being saved to a file and it'd be really stupid to receive "add ten dollars to desc's account" and then open a file, read it all into memory, fiddle with desc's bits, write the changed file, and send the message "you have successfully fiddled with desc's bits." So I would like the first call to have to be either "create" or "open," and calls afterwards all work on an object in memory.
Is there a way to do this? The main issue is that, since it's inside of a function, I can't really create an object and have it persist. Global variables are not great practice. I guess I could have a thread run on any given account, and use a SEPARATE queue to communicate with the master thread, but that seems extremely poor in terms of design.
How would you guys do this?
The code is in Python, by the way.
First of all, the typical OO solution to that kind of problem (having an object that persists across multiple calls of a function) would be to wrap the whole thing--or at least most of it--in a class. Then your initialize function would check to see if your file exists, open up the file, and dump the info into a class variable. Since that variable is at the class rather than function scope, it would persist across multiple calls of a function.
Second of all, for this particular problem, you might want to look into a nosql solution like MongoDB. Depending on what exactly you're doing with your dicebot that might be a bit overkill, but probably worth looking into for future reference anyway.
The problem with the first thing is that I don't necessarily want to create it, and it seems inelegant to have a massive class with subobjects that I'm maybe not going to use. At that point it's barely different from a global variable, right?
I will check those in 2. Though yeah, probably total overkill :P
If it's very complicated, it's probably worthwhile to just do a class that represents characters, and handle individual updates there.
My idea was to have a class that represents a given game campaign and has a bunch of separate character accounts (which are their own class) in a list, which handles everything, so mostly I would just need to put that in the right spot so it persists. It sounds like, if I don't want to repeatedly open files, I should make a collection of these and put them in the main thread.
The only real problem with that design up to thousands of accounts and tens of queries per second is that data integrity might be an issue if your power goes out mid-write, and most filesystems will start by truncating the file and overwriting. You'll want to write the modified data out into a new file and then delete/rename
The only real problem with that design up to thousands of accounts and tens of queries per second is that data integrity might be an issue if your power goes out mid-write, and most filesystems will start by truncating the file and overwriting. You'll want to write the modified data out into a new file and then delete/rename
I was planning on that anyway - to a degree I'm overengineering this on purpose, so that I can learn more while doing it.
Like the addition of separate threads for handling console and socket input worked well, but it was totally just an excuse to get myself used to designing multithreaded programs.
If the forums allowed embedding javascript directly into posts, I would be like a god. And have all of your login credentials.
I always wanted to do a dynamic signature, which not only returned the user's IP address would check the current users page to narrow down the possibilities until it could assocate a user with an IP address.
They moistly come out at night, moistly.
0
Donkey KongPutting Nintendo out of business with AI nipsRegistered Userregular
Thousands of hot, local singles are waiting to play at bubbulon.com.
I am adding functionality to my IRC dicebot that will allow persistent tracking of individual's experience, money, and items (which I'm calling a character account). The way the bot is set up is that it has a thread listening on a socket for input which uses a queue to pass to a master thread, which handles the input. If the socket input is a private message, it executes the private message handling logic.
Now, all actual interaction with the character accounts will therefore be in the private message function, as any requests to change around variables will be received there. The problem, then, is that I want an account to persist once opened, because it's being saved to a file and it'd be really stupid to receive "add ten dollars to desc's account" and then open a file, read it all into memory, fiddle with desc's bits, write the changed file, and send the message "you have successfully fiddled with desc's bits." So I would like the first call to have to be either "create" or "open," and calls afterwards all work on an object in memory.
Is there a way to do this? The main issue is that, since it's inside of a function, I can't really create an object and have it persist. Global variables are not great practice. I guess I could have a thread run on any given account, and use a SEPARATE queue to communicate with the master thread, but that seems extremely poor in terms of design.
How would you guys do this?
The code is in Python, by the way.
First of all, the typical OO solution to that kind of problem (having an object that persists across multiple calls of a function) would be to wrap the whole thing--or at least most of it--in a class. Then your initialize function would check to see if your file exists, open up the file, and dump the info into a class variable. Since that variable is at the class rather than function scope, it would persist across multiple calls of a function.
Second of all, for this particular problem, you might want to look into a nosql solution like MongoDB. Depending on what exactly you're doing with your dicebot that might be a bit overkill, but probably worth looking into for future reference anyway.
The problem with the first thing is that I don't necessarily want to create it, and it seems inelegant to have a massive class with subobjects that I'm maybe not going to use. At that point it's barely different from a global variable, right?
I will check those in 2. Though yeah, probably total overkill :P
If it's very complicated, it's probably worthwhile to just do a class that represents characters, and handle individual updates there.
My idea was to have a class that represents a given game campaign and has a bunch of separate character accounts (which are their own class) in a list, which handles everything, so mostly I would just need to put that in the right spot so it persists. It sounds like, if I don't want to repeatedly open files, I should make a collection of these and put them in the main thread.
I would offload actually storing the data to something totally different -- I think that should be outside the scope of your bot. You could offload it to a sqllite server or something like Redis which is pretty much designed for this and is much hipper.
If the forums allowed embedding javascript directly into posts, I would be like a god. And have all of your login credentials.
still in gratitude for helping me catch the cheating cheater who cheated
you were as become death, destroyer of GPAs
Wait what
I want to hear this story.
Donkey Kong--and this is a real thing that actually happened--looked at a string of garbage characters and said "oh, yeah, I recognize that; it's what you get when you force open a certain type of image file in text-editor"
I would be perfectly fine with going back to totally static webpages if it meant we could get rid of Javascript. We can get rid of CSS, too, I never liked it all that much.
0
MrMisterJesus dying on the cross in pain? Morally better than us. One has to go "all in".Registered Userregular
Also, there were other things wrong with the document but that was the real come to jesus moment
If the forums allowed embedding javascript directly into posts, I would be like a god. And have all of your login credentials.
Yes, but then you'd have to use javascript, so it's still kind of a losing proposition for you.
In javascript, inside anonymous functions, the "this" keyword refers to the object calling the function, not the scope in which it was created.
In fact, scope is so sketchy in javascript that if you create a collection of anonymous functions inside of a for loop, for example, to process different pieces of data, every single one will process the last piece of data. Because scope only operates at a function-level and so all the anonymous functions share the same variable space owned by the enclosing function.
I have used javascript long enough that it can no longer hurt me because I am already dead.
Thousands of hot, local singles are waiting to play at bubbulon.com.
0
ShivahnUnaware of her barrel shifter privilegeWestern coastal temptressRegistered User, Moderatormod
TehSloth I'm watching a thing with Ell but may ask a q later
If the forums allowed embedding javascript directly into posts, I would be like a god. And have all of your login credentials.
still in gratitude for helping me catch the cheating cheater who cheated
you were as become death, destroyer of GPAs
Wait what
I want to hear this story.
Donkey Kong--and this is a real thing that actually happened--looked at a string of garbage characters and said "oh, yeah, I recognize that; it's what you get when you force open a certain type of image file in text-editor"
Posts
sorry cB hehehehe
how do you not know this
you're the wizard here
get your shit together
Firstly, that's not so stupid of a design. It's a light duty program, it's a hobby project, and computers automatically cache small, commonly opened files, so the open/read/modify/write cycle is going to be a lot faster than you imagine. Like less than 1ms. My first advice is just leave it the way it is. This is a simple and shockingly robust design. In network apps, this is kind of what the file system is for.
Next, the "more efficient" design, which may in practice not actually be more efficient, is to have a design where you have a user object that contains all their info. This object has a function for writing its self out to disk and loading off of disk. You store all these user objects in a collection in your main thread. When you get a new command, you check the user name and look in your collection for that user. If they aren't found, look on disk for a file. If a file is found, make a new instance of the user class with that file and put it in the collection. If no file is found, initialize a blank user with that name. Next, process the message, modify the class in-memory, and call the write-out to disk function. You could even have another thread where you queue up the write out to disk functions but then you need to worry about locking the user objects so you don't accidentally modify one while it is being written.
But like I said, this is classic premature optimization.
This writing :biggrin:
Graffiti in Singapore:
A traditional UK bike:
A traditional UK breakfast:
It’s not a very important country most of the time
http://steamcommunity.com/id/mortious
Depends on whether you have one file containing the data for all players (in which case: depends on the number of players) or one file per players (in which case: not very long, unless that file is keeping a log of every single transaction or something).
My answer would be that the efficiency of the read/write times is not a concern, unless this bot is going to be servicing thousands of people simultaneously. Just typing out "fiddle with dec's bits" takes orders of magnitude longer than fiddling with desc's bits regardless of how you do it, so don't worry about it.
However, the question "where do I put data I need 'globally' if global variables is bad?" is important. Generally, you'd give the variable to an object, namely the master thread object. If I recall correctly, in Python this should be as simple as using self.ListOfOpenFiles or somesuch, I dunno. I don't like how wishy-washy Python is, honestly.
If it's very complicated, it's probably worthwhile to just do a class that represents characters, and handle individual updates there.
Yeah I suppose it is hella premature optimization, I just don't want to have to rewrite later and also figure this kind of question is something I could see later, and should learn more about. Your post answers that well (as well as pointing out I'm premature in worrying about it) and is appreciated.
Has anyone got Hagrid yet?
My idea was to have a class that represents a given game campaign and has a bunch of separate character accounts (which are their own class) in a list, which handles everything, so mostly I would just need to put that in the right spot so it persists. It sounds like, if I don't want to repeatedly open files, I should make a collection of these and put them in the main thread.
The forums don't even use https
It gives me the Heebie jeebies
I was planning on that anyway - to a degree I'm overengineering this on purpose, so that I can learn more while doing it.
Like the addition of separate threads for handling console and socket input worked well, but it was totally just an excuse to get myself used to designing multithreaded programs.
Yes, but then you'd have to use javascript, so it's still kind of a losing proposition for you.
I always wanted to do a dynamic signature, which not only returned the user's IP address would check the current users page to narrow down the possibilities until it could assocate a user with an IP address.
still in gratitude for helping me catch the cheating cheater who cheated
you were as become death, destroyer of GPAs
I know.
I can't login either.
and it's crazy cuse facebook is like never down.
I am not even a Halo guy and yet I want to buy into the hype Just because
Wait what
I want to hear this story.
seems legit
Deetaaaails
I would offload actually storing the data to something totally different -- I think that should be outside the scope of your bot. You could offload it to a sqllite server or something like Redis which is pretty much designed for this and is much hipper.
twitch.tv/tehsloth
Donkey Kong--and this is a real thing that actually happened--looked at a string of garbage characters and said "oh, yeah, I recognize that; it's what you get when you force open a certain type of image file in text-editor"
In javascript, inside anonymous functions, the "this" keyword refers to the object calling the function, not the scope in which it was created.
In fact, scope is so sketchy in javascript that if you create a collection of anonymous functions inside of a for loop, for example, to process different pieces of data, every single one will process the last piece of data. Because scope only operates at a function-level and so all the anonymous functions share the same variable space owned by the enclosing function.
I have used javascript long enough that it can no longer hurt me because I am already dead.
Please take my document homework1.docx.exe