webdevRefinery Forum: IRC errors and shenanigans - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline Midareru 

  • Group: Members
  • Posts: 5
  • Joined: 28-April 12
  • Expertise:Python

Posted 28 April 2012 - 02:51 AM (#1)

IRC errors and shenanigans


jumped into the deep end again
after messing around with some python code i found for a overly basic and simple IRC bot in python, i have decided to flesh it out.

edit- new code :3

Spoiler


setup.ini
Spoiler


current problems as stated in last post:

Quote

what i really need to do is split the variable "data" into a few strings.

print (data)

returns
:Midareru!~androirc@119.224.2.55 PRIVMSG #OT :Hello World

what i'm trrying to do is make 6 new variables from "data".
the users nickname "Midareru", their indent "androirc", ip, "@119.224.2.55", message type "PRIVMSG", channel #OT, and finally the message "Hello World".

i have been sugested to use regular expressions to extract the data types, but i don't quite get it, and as i said before, i don't have the time at the moment...

once i get that sortet, i can get this hunk of code sorted out nicely.

#!/usr/local/bin/perl
use strict;
srand();
system('clear');
my $ihavenoidearwhatimdoing = int(rand(2));
if ($ihavenoidearwhatimdoing == 0) {$ihavenoidearwhatimdoing++}
if ($ihavenoidearwhatimdoing == 1) {
    print 'I really have no idear what i\'m doing...'}
elsif ($ihavenoidearwhatimdoing == 2) {
    print 'I know what i\'m doing... I hope..'}
else {
die {"What?"}
}
0


User is offline Mo3 

  • Brogrammer
  • Group: Moderators
  • Posts: 1950
  • Joined: 21-July 10
  • LocationStuttgart, Germany
  • Expertise:PHP,Java,Javascript,Python,Ruby on Rails,Node.js

Posted 28 April 2012 - 04:38 AM (#2)

You print all those variables to the command line. Either use

python bot.py >> log.txt

or remove the print statements and write them to the file instead.

//edit: disregard that. just woke up

Quote

A person who thinks all the time has nothing to think about except thoughts. So he loses touch with reality, and lives in a world of illusion called the past. Things are not explained by the past, they are explained by what happens right now. That creates the past, and it begins here. That's the birth of responsibility.
0


User is offline ianonavy 

  • Group: Members
  • Posts: 685
  • Joined: 14-April 10
  • Expertise:HTML,CSS,Java,Javascript,Python

Posted 30 April 2012 - 09:24 PM (#3)

Why do you open the file twice?
f = open('log.txt', 'a')
being called every loop is very inefficient. You should just use
t.write(data)
.

That said, your code could use quite a bit of cleaning up in terms of style. For example, you could use a better variable name such as
log_file
. I recommend the PEP 8 Style Guide to help clean up your code a bit.

This is how I would do it:
Spoiler


I'd like to note the following changes:
  • I put all reused strings and data in one configuration area.
  • I added lots of helpful comments.
  • I made the whitespace more consistent. e.g.
    irc.send('JOIN %s\r\n' % channel)
    instead of
    irc.send ( 'JOIN ' + channel + '\r\n' )

  • I used Python's method of string interpolation/formatting using the modulo operator.
  • I cleaned up the logic of the code making it only open the file once and wrapped it in a try-catch block.
  • I illustrated the use of Python's
    in
    keyword for finding strings.
  • I wrote a function that both prints to the console and writes to the log to make the code more DRY.
  • I added a shebang to the start of the script to make it more UNIX-based OS friendly.


I hope that I have helped you with your issue.
reputation += 1 if post.is_helpful else 0
0


User is offline Midareru 

  • Group: Members
  • Posts: 5
  • Joined: 28-April 12
  • Expertise:Python

Posted 11 May 2012 - 01:34 AM (#4)

well, i changed it to this.

i don't have much time to work on this due to college -.-;

Spoiler


setup.ini
Spoiler


what i really need to do is split the variable "data" into a few strings.

print (data)

returns
:Midareru!~androirc@119.224.2.55 PRIVMSG #OT :Hello World

what i'm trrying to do is make 6 new variables from "data".
the users nickname "Midareru", their indent "androirc", ip, "@119.224.2.55", message type "PRIVMSG", channel #OT, and finally the message "Hello World".

i have been sugested to use regular expressions to extract the data types, but i don't quite get it, and as i said before, i don't have the time at the moment...

i was sugested to try something like this:
nick = data.substr(1, data.find '!~' -1)



once i get that sortet, i can get this hunk of code sorted out nicely.

just a few notes:
i open the file, append to it, then close it.
this saves me the hassle of telling to bot to exit on the irc, looking at the log, then starting it up again when i just could open up the file without making the python script stop before it closes the file so i can see the text.

i use that form of whitspace as i's easyer to read for me.
#!/usr/local/bin/perl
use strict;
srand();
system('clear');
my $ihavenoidearwhatimdoing = int(rand(2));
if ($ihavenoidearwhatimdoing == 0) {$ihavenoidearwhatimdoing++}
if ($ihavenoidearwhatimdoing == 1) {
    print 'I really have no idear what i\'m doing...'}
elsif ($ihavenoidearwhatimdoing == 2) {
    print 'I know what i\'m doing... I hope..'}
else {
die {"What?"}
}
0


User is online callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 11 May 2012 - 03:09 AM (#5)

Try this regex:

^:([^!]+)!(~?[^!@]+)@([^ @]+) (PRIVMSG) ([^ ]+) :(.+)$


Note that the ident in the sample you gave isn't
androirc
, it is
~androirc
, meaning that you don't have an ident server installed.
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is offline Midareru 

  • Group: Members
  • Posts: 5
  • Joined: 28-April 12
  • Expertise:Python

Posted 11 May 2012 - 03:25 AM (#6)

ah, i see now.

so i would use it like so?
new = data.find(^:([^!]+)!(~?[^!@]+)@([^ @]+) (PRIVMSG) ([^ ]+) :(.+)$)


sorry if i'm getting this wrong, my brain has been fuzzy all day..
#!/usr/local/bin/perl
use strict;
srand();
system('clear');
my $ihavenoidearwhatimdoing = int(rand(2));
if ($ihavenoidearwhatimdoing == 0) {$ihavenoidearwhatimdoing++}
if ($ihavenoidearwhatimdoing == 1) {
    print 'I really have no idear what i\'m doing...'}
elsif ($ihavenoidearwhatimdoing == 2) {
    print 'I know what i\'m doing... I hope..'}
else {
die {"What?"}
}
0


User is offline Fike 

  • Group: Members
  • Posts: 340
  • Joined: 26-October 10
  • LocationIreland
  • Expertise:PHP,Javascript,Python,SQL

Posted 11 May 2012 - 01:21 PM (#7)

View PostMidareru, on 11 May 2012 - 03:25 AM, said:

ah, i see now.

so i would use it like so?
new = data.find(^:([^!]+)!(~?[^!@]+)@([^ @]+) (PRIVMSG) ([^ ]+) :(.+)$)


sorry if i'm getting this wrong, my brain has been fuzzy all day..


Something like

import re
new = re.match("^:([^!]+)!(~?[^!@]+)@([^ @]+) (PRIVMSG) ([^ ]+) :(.+)$", data)

web developer :: HTML, CSS, JavaScript (node), Python, PHP, MySQL, Mongo.
server admin :: experience with debian (and debian based distros), Gentoo, FreeBSD, OpenBSD.
social :: @nixhead (Twitter), Fudge (IRC), Github (FionnK), Personal Blog.
0


User is offline Midareru 

  • Group: Members
  • Posts: 5
  • Joined: 28-April 12
  • Expertise:Python

Posted 12 May 2012 - 12:05 AM (#8)

using this:
import re
data = ':Midareru!~androirc@119.224.2.55 PRIVMSG #OT :Hello World'
new = re.match("^:([^!]+)!(~?[^!@]+)@([^ @]+) (PRIVMSG) ([^ ]+) :(.+)$", data)
print (new)
pause = raw_input('press enter to exit')


i got his in return:
<_sre.SRE_Match object at 0x00DEC920>
press enter to exit


any idear what's going wrong/right?
#!/usr/local/bin/perl
use strict;
srand();
system('clear');
my $ihavenoidearwhatimdoing = int(rand(2));
if ($ihavenoidearwhatimdoing == 0) {$ihavenoidearwhatimdoing++}
if ($ihavenoidearwhatimdoing == 1) {
    print 'I really have no idear what i\'m doing...'}
elsif ($ihavenoidearwhatimdoing == 2) {
    print 'I know what i\'m doing... I hope..'}
else {
die {"What?"}
}
0


User is offline NeilHanlon 

  • Group: Members
  • Posts: 884
  • Joined: 08-July 10
  • LocationRowley, Massachusetts
  • Expertise:HTML,CSS,PHP,Java,Graphics

Posted 12 May 2012 - 07:35 AM (#9)

New is an object, and that's what it's telling you. I'm not familiar with python, but I know in PHP, if I did print() on an array or object, it would do the same thing.

Is there a python function equivalent to print_r() or var_dump()?
Thanks,
兄ニール

Website | Blog | @NeilHanlon | About.Me | Facebook | LinkedIn
0


User is offline Fike 

  • Group: Members
  • Posts: 340
  • Joined: 26-October 10
  • LocationIreland
  • Expertise:PHP,Javascript,Python,SQL

Posted 12 May 2012 - 08:05 AM (#10)

View PostNeilHanlon, on 12 May 2012 - 07:35 AM, said:

New is an object, and that's what it's telling you. I'm not familiar with python, but I know in PHP, if I did print() on an array or object, it would do the same thing.

Is there a python function equivalent to print_r() or var_dump()?


pprint?
web developer :: HTML, CSS, JavaScript (node), Python, PHP, MySQL, Mongo.
server admin :: experience with debian (and debian based distros), Gentoo, FreeBSD, OpenBSD.
social :: @nixhead (Twitter), Fudge (IRC), Github (FionnK), Personal Blog.
0


User is offline Sephern 

  • Group: Moderators
  • Posts: 967
  • Joined: 04-June 10
  • LocationReading, UK
  • Expertise:HTML,CSS,PHP,Javascript,Python

Posted 12 May 2012 - 08:43 AM (#11)

re.match returns a MatchObject. They always have a boolean value of true so the idea is that you call in an if, and if there's a match then it carries on (for example, if you're validating email). If you actually want to get the part of the string that is matched you can do
print(new.group(0))
.

Quote

group([group1, ...])
Returns one or more subgroups of the match. If there is a single argument, the result is a single string; if there are multiple arguments, the result is a tuple with one item per argument. Without arguments, group1 defaults to zero (the whole match is returned). If a groupN argument is zero, the corresponding return value is the entire matching string; if it is in the inclusive range [1..99], it is the string matching the corresponding parenthesized group. If a group number is negative or larger than the number of groups defined in the pattern, an IndexError exception is raised. If a group is contained in a part of the pattern that did not match, the corresponding result is None. If a group is contained in a part of the pattern that matched multiple times, the last match is returned.

>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0)       # The entire match
'Isaac Newton'
>>> m.group(1)       # The first parenthesized subgroup.
'Isaac'
>>> m.group(2)       # The second parenthesized subgroup.
'Newton'
>>> m.group(1, 2)    # Multiple arguments give us a tuple.
('Isaac', 'Newton')


0


User is offline Fike 

  • Group: Members
  • Posts: 340
  • Joined: 26-October 10
  • LocationIreland
  • Expertise:PHP,Javascript,Python,SQL

Posted 12 May 2012 - 10:30 AM (#12)

View PostMidareru, on 12 May 2012 - 12:05 AM, said:

using this:
import re
data = ':Midareru!~androirc@119.224.2.55 PRIVMSG #OT :Hello World'
new = re.match("^:([^!]+)!(~?[^!@]+)@([^ @]+) (PRIVMSG) ([^ ]+) :(.+)$", data)
print (new)
pause = raw_input('press enter to exit')


i got his in return:
<_sre.SRE_Match object at 0x00DEC920>
press enter to exit


any idear what's going wrong/right?


Here's an example I just made to demonstrate how re.match works.

import re

# Compile the regex incase you need to use it further (saves resources)
regex = re.compile("^:([^!]+)!(~?[^!@]+)@([^ @]+) (PRIVMSG) ([^ ]+) :(.+)$")

# This function takes the string and uses re.match() to check if its valid.
def privmsg_validate(string):
    valid = regex.match(string)
    if valid == None:
        return False
    else:
        return True

# Lets do some tests.
valid_line = privmsg_validate(":Midareru!~androirc@119.224.2.55 PRIVMSG #OT :Hello World")
invalid_line = privmsg_validate(":Midareru!~androirc@119.224.2.55 NOTICE #OT :Hello World")

print("Checking %s..." % valid_line)

if valid_line == True:
    print("Its valid!")
else:
    print("Doesn't appear valid.")

print("Checking %s..." % invalid_line)

if invalid_line == True:
    print("Its valid!")
else:
    print("Doesn't appear valid.")

web developer :: HTML, CSS, JavaScript (node), Python, PHP, MySQL, Mongo.
server admin :: experience with debian (and debian based distros), Gentoo, FreeBSD, OpenBSD.
social :: @nixhead (Twitter), Fudge (IRC), Github (FionnK), Personal Blog.
0


User is offline Midareru 

  • Group: Members
  • Posts: 5
  • Joined: 28-April 12
  • Expertise:Python

Posted 21 May 2012 - 12:55 AM (#13)

View Postianonavy, on 30 April 2012 - 09:24 PM, said:

Why do you open the file twice?
f = open('log.txt', 'a')
being called every loop is very inefficient. You should just use
t.write(data)
.


Yes, but if i don't keep setting that varable, it trys to write to a closed file, and will crash with an error.

instead of closing f i could just write to the file, then close the file ehen the bot is told to quit on the IRC, then i don't have any problems with writing to a closed file, and the program is more efficent.

Why i have done it the way i have is so the program logs in "real time", so that when someone says something in the channel the bot is in, it appears in CMD and in my log file (after reloading it), instead if i wanted to look at what the bot has loged, i have to shut down the bot via the IRC, and then restat it manually. also, if the bot gets an error and crashes, the program won't close the file, and the loged data will be lost.

Unless there is another way to do what i'm currently doing with the logging, this is the only way i want and can thing of logging for my bot.

View Postianonavy, on 30 April 2012 - 09:24 PM, said:

I'd like to note the following changes:
  • I put all reused strings and data in one configuration area.
  • I added lots of helpful comments.
  • I made the whitespace more consistent. e.g.
    irc.send('JOIN %s\r\n' % channel)
    instead of
    irc.send ( 'JOIN ' + channel + '\r\n' )

  • I used Python's method of string interpolation/formatting using the modulo operator.
  • I cleaned up the logic of the code making it only open the file once and wrapped it in a try-catch block.
  • I illustrated the use of Python's
    in
    keyword for finding strings.
  • I wrote a function that both prints to the console and writes to the log to make the code more DRY.
  • I added a shebang to the start of the script to make it more UNIX-based OS friendly.



The reason i use an external .ini file for this bot, is so that i can compile and run this bot on other machines without python installed (my hosting computer can't install it for some reason, and it runs faster compiled too), and not have permanant setting once the bot is compiled, so if i want to change the server to frenode, i can just edit my .ini file instead of chaning the py on a difrent computer, compiling, and then transefering the compiled bot to the soting computer.




another thing i have noticed since i have been programming this on my hosting laptop (it runs Lucid Puppy 5.2) and i have getting this once every 1-10 runs
socket.gaierror: [Errno -3] Temporary failure in name resolution


Does anyone know what this is about?
#!/usr/local/bin/perl
use strict;
srand();
system('clear');
my $ihavenoidearwhatimdoing = int(rand(2));
if ($ihavenoidearwhatimdoing == 0) {$ihavenoidearwhatimdoing++}
if ($ihavenoidearwhatimdoing == 1) {
    print 'I really have no idear what i\'m doing...'}
elsif ($ihavenoidearwhatimdoing == 2) {
    print 'I know what i\'m doing... I hope..'}
else {
die {"What?"}
}
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services