Re: [Tutor] Removing Content from Lines....
Hello everyone, First time using the mailing list to give a suggestion, apologies in advance if it's not appropriate :) Considering you're looking for specific string, I'd recommend maybe looking into the Regular Expressions. You could use something like: #At the top of the file import re #Within your function target = open(filename, 'rU') # This will read the file as a whole string, careful, it loads the file into memory so if it's a big file, it might cause troubles text = target.read() # The regular expression should match every single tag and its content within the file, using '.*?' will match each character without being greedy. match = re.findall(r'.*?', text) Afterwards, you can use something like: for i in match: print i This should print out all the matches to Stdout so you can verify the RegEx behaves as expected. To write the matches in a new file, you could do: new = open('Matches', 'w') ## This will create a new file called 'Matches' for writing for i in match: new.write(i + '\n') ## Each match of the RegEx will be written on a new line in the Matches file. At the end, my function looks like this: def textfind(filename): target = open(filename, 'rU') text = target.read() match = re.findall(r'.*?', text) new = open('Matches', 'w') for i in match: new.write(i + '\n') Regards, Dimitar -- Thanks, Dimitar *Twitter:* @winterchillz *Facebook: */dimitarxivanov ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Creating an object which is an extension of a dictionary
I'm attempting to create an object which can have elements which can be referenced like a dictionary but can also have normal methods and attributes like a conventional object but can also be referenced with key items using d[key] The dictionary I have is below: UPPERCASE = "ucase" LOWERCASE = "lcase" DEFAULT_TYPING_SETTINGS = {"allow letters" : True, "default case" : LOWERCASE, "allow uppercase" : True, "allow lowercase" : True, "allow shift case change" : True, "allow caps lock uppercase" : True, "allow numbers" : True, "allow standard numbers" : True, "allow keypad numbers" : True, "allow spaces" : True, "space keys" : K_SPACE, "allow backspace" : True, "backspace keys" : K_BACKSPACE, "allow newline" : False, "newline keys" : K_RETURN, "allow tab" : False, "tab keys" : K_TAB, "tab stop" : 4, "allow punctuation" : True, "allow standard punctuation" : True, "allow shift punctuation" : True, "allow number key punctuation" : True, "allow keypad punctuation" : True, "punctuation allowed" : None, "punctuation disallowed" : None, "show cursor" : False, "allow navigation" : False, "allow horizontal navigation" : False, "allow vertical navigation" : False, "allow arrow key navigation" : False, "allow keypad navigation" : False} I want to set an attribute for an object's typing settings which I can change any of these individually but I also want to be able to reference methods which can be called to perform operations such as allow all letters. Ideally I want a typing settings object where I can get and set attributes using for example: typing_settings["allow uppercase"] = True but I also want to create methods I can all using dot notation so I need something which is a cross between a dictionary and a conventional object. It isn't absolutely necessary but I would also like to validate when setting the dictionary attributes such as converting necessary values to boolean when setting and ensure the default case attribute is either UPPER or LOWER Thanks for any suggestions Ian ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Removing Content from Lines....
On 26/03/16 10:08, Dimitar Ivanov wrote: > First time using the mailing list to give a suggestion, apologies in > advance if it's not appropriate :) Join the gang, any suggestions are appropriate since even when they aren't completely correct they offer learning opportunities. So all contributions are welcome. > Considering you're looking for specific string, I'd recommend maybe looking > into the Regular Expressions. You could use something like: The regex approach is good if the pattern is not a simple string. And it also has the advantage that you can findall() in one operation. The downside is that regex are notoriously hard to get right for anything other than trivial. > target = open(filename, 'rU') You should probably use the 'with' style with open(filename, 'rU') as target: # code here > # The regular expression should match every single tag and its content > within the file, using '.*?' will match each character without being greedy. > match = re.findall(r'.*?', text) You need a slash for the second tag: match = re.findall(r'.*?', text) > def textfind(filename): > target = open(filename, 'rU') > text = target.read() > match = re.findall(r'.*?', text) > new = open('Matches', 'w') > for i in match: > new.write(i + '\n') You should either close() the file after writing or use the 'with' style (which closes automatically). Otherwise if your program terminated suddenly your data might not make it from the buffer to the disk. The 'with' style is preferred. with open('Matches', 'w') as new: for m in matches: new.write(m + '\n') But your overall point that a regex can be used is correct, especially if the pattern were not consistent (for example if the tag sometimes had spaces or different cased letters). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Creating an object which is an extension of a dictionary
On 26/03/16 11:35, Ian Winstanley wrote: > I'm attempting to create an object which can have elements which can be > referenced like a dictionary but can also have normal methods and > attributes like a conventional object but can also be referenced with key > items using d[key] I think you are looking for UserDict (in the UserDict module) which is specifically designed with subclassing in mind. > It isn't absolutely necessary but I would also like to validate when > setting the dictionary attributes such as converting necessary values to > boolean when setting and ensure the default case attribute is either UPPER > or LOWER You can do that by overriding one of the dunder methods. I think __setitem__() is the one you need but the docs will clarify. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python smtplib 'Message denied - From address spoofing attempt detected via SMTP
So I requested the error_logs once again, and this is what was given by the services provider. Still no luck. Apache Log : send: 'ehlo cp-in-9.webhostbox.net\r\n' reply: '250-cp-in-9.webhostbox.net Hello cp-in-9.webhostbox.net [111.118.215.222]\r\n' reply: '250-SIZE 52428800\r\n' reply: '250-8BITMIME\r\n' reply: '250-PIPELINING\r\n' reply: '250-AUTH PLAIN LOGIN\r\n' reply: '250-STARTTLS\r\n' reply: '250 HELP\r\n' reply: retcode (250); Msg: cp-in-9.webhostbox.net Hello cp-in-9.webhostbox.net [111.118.215.222] SIZE 52428800 8BITMIME PIPELINING AUTH PLAIN LOGIN STARTTLS HELP send: 'AUTH PLAIN AG5vLXJlcGx5QGpzc2hzLm9yZwByZXBseTEwSnNzaHM=\r\n' reply: '235 Authentication succeeded\r\n' reply: retcode (235); Msg: Authentication succeeded send: 'mail FROM: size=483\r\n' reply: '250 OK\r\n' reply: retcode (250); Msg: OK send: 'rcpt TO:\r\n' reply: '250 Accepted\r\n' reply: retcode (250); Msg: Accepted send: 'rcpt TO:\r\n' reply: '250 Accepted\r\n' reply: retcode (250); Msg: Accepted send: 'data\r\n' reply: '354 Enter message, ending with "." on a line by itself\r\n' reply: retcode (354); Msg: Enter message, ending with "." on a line by itself data: (354, 'Enter message, ending with "." on a line by itself') send: "\r\nFrom : no-re...@jsshs.org\r\nTo : ['mailhostingser...@gmail.com', 'ni...@chandrainformatics.com']\r\nSubject : Registration confirmation with JSSHS\r\n\r\n\r\nApplicationID : jsshs34\r\nFirstNAME : test\r\nLastNAME : email\r\nGender : male\r\nDoB : 10/11/2015\r\nAge : 0\r\nCategory : general\r\nPost : jrResident\r\nPhone M : 1234567890\r\nPhone LL : 01234567890\r\nEmail : mailhostingser...@gmail.com\r\nSubmission DT : 25/03/2016\r\nSubmission Time : 07:58AM\r\n\r\n.\r\n" reply: '250 Message denied - From address spoofing attempt detected via SMTP ( >From address: IP: 111.118.215.222 AuthenticatedID: no-re...@jsshs.org Protocol: esmtpa)\r\n' reply: retcode (250); Msg: Message denied - >From address spoofing attempt detected via SMTP ( From address: IP: 111.118.215.222 AuthenticatedID: no-re...@jsshs.org Protocol: esmtpa) data: (250, 'Message denied - From address spoofing attempt detected via SMTP ( From address: IP: 111.118.215.222 AuthenticatedID: no-re...@jsshs.org Protocol: esmtpa)') send: 'quit\r\n' reply: '221 cp-in-9.webhostbox.net closing connection\r\n' reply: retcode (221); Msg: cp-in-9.webhostbox.net closing connection Exim Log : 2016-03-26 04:22:38 SMTP connection from [111.118.215.222]:51490 (TCP/IP connection count = 5) 2016-03-26 04:22:38 H=cp-in-9.webhostbox.net [111.118.215.222]:51490 Warning: Sender rate 1.0 / 1h 2016-03-26 04:22:38 1ajfkE-000jLZ-Js <= no-re...@jsshs.org H=cp-in-9.webhostbox.net [111.118.215.222]:51490 P=esmtpa A=dovecot_plain:no-re...@jsshs.org S=692 for mailhostingser...@gmail.com ni...@chandrainformatics.com 2016-03-26 04:22:38 1ajfkE-000jLZ-Js => blackhole (DATA ACL discarded recipients): Message denied - From address spoofing attempt detected via SMTP ( From address: IP: 111.118.215.222 AuthenticatedID: no-re...@jsshs.org Protocol: esmtpa) 2016-03-26 04:22:38 1ajfkE-000jLZ-Js Completed 2016-03-26 04:22:38 SMTP connection from cp-in-9.webhostbox.net [111.118.215.222]:51490 closed by QUIT On 26 March 2016 at 03:48, Bill Campbell wrote: > On Sat, Mar 26, 2016, nitin chandra wrote: >>Hi All, >> >>I am trying to script a confirmation mail on submission of a form. >> >>Till now the form submission and summery page is working. >> >>BUT the following 'section' is not working. >>-- This is a python(CGI) script >>-- hosted on a shared server on hostgator.in >> >>-- This is giving "250 Spoofing error ", below copy/pasted error >>log of apache with respect to my website > > The problem isn't with the python usage, but basic SMTP checking to prevent > spamming and such. > > It's indicating that it thinks the 'From:' address, > is bogus. > > There's no MX record for jss.hs.org, and no IP address for jss.hs.org > either. > > The From: address should be legitimate and deliverable. It might > also accept a legitimate 'Reply-To:' address. > > Bill > -- > INTERNET: b...@celestial.com Bill Campbell; Celestial Software LLC > URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way > Voice: (206) 236-1676 Mercer Island, WA 98040-0820 > Fax:(206) 232-9186 Skype: jwccsllc (206) 855-5792 > > A Democracy cannot exist as a permanent form of government. It can only > exist until the voters discover they can vote themselves largesse out of > the public treasury. From the moment on the majority always votes for the > candidate promising the most benefits from the public treasury with the > result that Democracy always collapses over a loose fiscal policy, always > to be followed by a Dictatorship. > Professor Alexander Fraser Tytler. 1801 > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___