Shinyshell Community Forums > Coding >
Python...


[1]


April 20 02009, 06:33 GMT
Nick^
merciful justice

Nick's avatar
Location: Brisbane, Australia
Post count: 78
au
Okay, seeing as I can't catch you over IM, here's what I'm having trouble with:

First, time stamps - how do I convert them to a readable and understandable format?

Second, what can I use as a Python alternative to PHP's "nl2br" function?

Next, how do I send e-mails from within a script? For instance, if I wanted to an e-mail to myself to alert me of the latest error report?

Finally, what would be the best way to code a search function?

Thannnk you. If you hadn't read my latest update, I've decided to finish the features first, and then the content and layouts :D

YAY
______________________________
Lazurane

April 21 02009, 21:01 GMT
lec**
Supra stultitiam

lec's avatar
Location: Varaždin, Croatia
Post count: 173

Timestamps
I believe I provided you with the timeformat() function (it should be among the generic functions like unicode_safe() and htmlspecialchars(). The first parameter is a formatting string (much like PHP's date() or gmdate() function that you might already be familar with). The format characters are slightly different though - they're in the table on this page.

So, for example, to print out "April 21 2009, 22:16", call the timeformat function with the following parameters: ("%B %d %Y, %H:%M", time.time() )

nl2br
The nl2br function just inserts a <br /> before any newlines in the provided string. Newlines are "\n" characters. You could just use the replace() method of string objects:

text = "your text with newline chars\nWoop!" 
print text.replace("\n", "<br />\n")
text = "your text with newline chars\nWoop!"
print text.replace("\n", "<br />\n")
]]>


Sending email
There's a cute little library for sending mail over SMTP - smtplib. Combine this with the MIMEText object from the email lib. Here's a demonstration

import smtplib 
from email.MIMEText import MIMEText

# set up a MIMEText object (it's a dictionary)
msg = MIMEText("Hello World")

# you can use add_header or set headers directly
msg["Subject"] = "Python mail test"

# these headers are useful to show the email
# correctly in your recipient's inbox, and to
# avoid being marked as spam. They are NOT
# essential to the sendmail call later
msg["From"] = "Nemo Nihil <nemo@nihil.com>"
msg["Reply-to"] = "Nemo Nihil <nemo@nihil.com>"
msg["To"] = "aliquis@aliquid.com"

# establish an SMTP object and connect to a
# mail server if the script isn't running ON
# the mail server itself
s = smtplib.SMTP()
s.connect("mail.eeveeshq.com")

# send the email - real from, real to,
# extra headers and content
s.sendmail(
"you@eeveeshq.com", # actual sender
"aliquis@aliquid.com", # actual recipient
msg.as_string()
)
s.close()
import smtplib
from email.MIMEText import MIMEText

# set up a MIMEText object (it's a dictionary)
msg = MIMEText("Hello World")

# you can use add_header or set headers directly
msg["Subject"] = "Python mail test"

# these headers are useful to show the email
# correctly in your recipient's inbox, and to
# avoid being marked as spam. They are NOT
# essential to the sendmail call later
msg["From"] = "Nemo Nihil <nemo@nihil.com>"
msg["Reply-to"] = "Nemo Nihil <nemo@nihil.com>"
msg["To"] = "aliquis@aliquid.com"

# establish an SMTP object and connect to a
# mail server if the script isn't running ON
# the mail server itself
s = smtplib.SMTP()
s.connect("mail.eeveeshq.com")

# send the email - real from, real to,
# extra headers and content
s.sendmail(
"you@eeveeshq.com", # actual sender
"aliquis@aliquid.com", # actual recipient
msg.as_string()
)
s.close()
]]>


You need to specify an SMTP server. Mail.eeveeshq.com might work while Kat hosts you. I haven't tested this yet.

Search
It's probably best to add a row called "tags" to content like articles and news entries. Then users can search by this keyword (you can actually list them).

Otherwise, since your database isn't going to be lord knows how large, I recommend just using SQL's LIKE condition to search content fields. Do this for every table you think is relevant, and list out the results if anything was found.

SELECT * FROM articles 
WHERE content LIKE '%search terms%'
SELECT * FROM articles
WHERE content LIKE '%search terms%'
]]>


I also recommend making a list of keywords that don't get searched for, stuff like "a, the, most, I, be, ..." - you get the drift.

April 22 02009, 11:22 GMT
Nick^
merciful justice

Nick's avatar
Location: Brisbane, Australia
Post count: 78
au
Thanks for all that.

Timestamps
However, there's an error with your timeformat function:

said

<type 'exceptions.TypeError'> Python 2.5.4: /home/nickyn00/bin/python
Wed Apr 22 04:20:40 2009

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
/home/.keerokee/nickyn00/pokenova.com/index.py in ()
20
21 for story in news:
22 time = electron.misc.timeformat("%B %d %Y, %H:%M", story)
23
24 T_content += """<div class='newsParent'>
time undefined, electron = <module 'electron' from '/home/.keerokee/nickyn00/pokenova.com/electron/__init__.pyc'>, electron.misc = <module 'electron.misc' from '/home/.keerokee/nickyn00/pokenova.com/electron/misc.pyc'>, electron.misc.timeformat = <function timeformat at 0x4064c614>, story = {'id': 4L, 'news': 'Did I say 90%? Hah, I wrote like two pieces. HOW...elpful for monitoring progress. Okay, so yeah. :D', 'status': 1L, 'time': '1240146898.6104', 'title': 'Off Holidays...'}
/home/nickyn00/pokenova.com/electron/misc.py in timeformat(format='%B %d %Y, %H:%M', timestamp='1240146898.6104')
89 timestamp = time.time()
90
91 return datetime.datetime.utcfromtimestamp(timestamp).strftime(format)
92
93 def unicode_safe(object, type):
global datetime = <module 'datetime' from '/home/nickyn00/bin/build/lib.linux-i686-2.5/datetime.so'>, datetime.datetime = <type 'datetime.datetime'>, datetime.datetime.utcfromtimestamp = <built-in method utcfromtimestamp of type object at 0x40468180>, timestamp = '1240146898.6104', ).strftime undefined, format = '%B %d %Y, %H:%M'

<type 'exceptions.TypeError'>: a float is required
args = ('a float is required',)
message = 'a float is required'


nl2br
Working great, thank you. I've made my own linebreaks() function and everything!

Sending emails
Eurgh not working...! D:

The e-mail sends, and I receive it, but the e-mail is blank, with an attachment named "noname" (probably gmail's doing), which contains the message (which contains %s as opposed to the actual variable).
______________________________
Lazurane

April 23 02009, 10:48 GMT
lec**
Supra stultitiam

lec's avatar
Location: Varaždin, Croatia
Post count: 173

Timestamps
That's because the second argument has to be a float, as that error states. You're passing a string:

time = electron.misc.timeformat("%B %d %Y, %H:%M", '1240146898.6104') 
time = electron.misc.timeformat("%B %d %Y, %H:%M", '1240146898.6104')
]]>


it should be:

time = electron.misc.timeformat("%B %d %Y, %H:%M", 1240146898.6104) 
time = electron.misc.timeformat("%B %d %Y, %H:%M", 1240146898.6104)
]]>


A quick fix for this would be just to call the float() function on the second argument.

nl2br
Great :)

Sending emails
Hmm, I'm not sure exactly why that is happening. My guess is that you're not specifying a MIME-type for the attached document (text/plain or text/html) so Gmail isn't showing it as the contents.

I'll check this out a little more, I guess you should experiment with it a little, see if you can get it working before then.


[1]



Forum Information
  Currently Active Members [detailed] (0 members and ? guests)
-
Forum Statistics
Topics: 0, Posts: 0, Members: 108.
Welcome to our newest member, adamthephantump
Most members online was 5, on August 28 2009, at 21:49:28.
Legend
    Forums with unread topics in them are indicated by a strong yellow colouring around the forum icon.
    Forums with no unread topics have the standard pale yellow colouring around the forum icon.
    Forums with a blue arrow icon will redirect you to a non-forum page.
    Locked forums have a little padlock by their icon. You won't be able to post in these forums.
Shinyshell Home | Contact | Staff List | Archive | Top 

Conventional Login

Don't have an account? You may want to create one.

OpenID Login
OpenID login and registration is usable, but not finished.
What is OpenID?
Search

(advanced search)
Site Stats
  Total members: 108
  Latest member: adamthephantump
  Members currently online: 0
  Most online: 5 - Aug 28, 2009 (21:49)
  Front page hits: 88122
Developer info
  Site version: 3.5 Alpha
  16 queries - 9 templates
Under the Spotlight
Collide Site
Collide make fabulously dreamy electronic-industrial music, they're one of my favourite bands! Give them a chance to take control of your life - myspace | youtube - "Euphoria".

Collide Site - Hits: 4926

5/5 (2) | Rate this site?