%%
**Send Email through Gmail with Python**
%%(python)
"""
Send an email programmatically through Gmail.
REFERENCES
http://snippets.dzone.com/posts/show/757
http://www.nixtutor.com/linux/send-mail-through-gmail-with-python/
http://stackoverflow.com/questions/399129/#comment8985407_399240
"""
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os
from os.path import abspath, dirname, join
SMTP_SERVER = 'smtp.gmail.com:587'
SMTP_USER = 'USER@gmail.com'
SMTP_PASS = 'PASS'
EMAIL_FROM = 'USER <%s>' % (SMTP_USER)
def send_gmail(to, subject, text, files=[]):
assert type(to) == list
assert type(files) == list
from_ = EMAIL_FROM
msg = MIMEMultipart()
msg['From'] = EMAIL_FROM
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for file in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(file,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(file))
msg.attach(part)
smtp = smtplib.SMTP(SMTP_SERVER)
#smtp.set_debuglevel(1)
smtp.ehlo() # apparently this is required in python 2.5
smtp.starttls()
smtp.login(SMTP_USER, SMTP_PASS)
smtp.sendmail(from_, to, msg.as_string() )
smtp.close()
def test_email():
to = 'YOUR_RECEPIENT@some_domain.com'
subject = "Python Gmail Test"
text = 'This is simply a test.'
send_gmail([to], subject, text)
print 'simple test complete'
%%
**Method Decorator**
%%(python)
def check_some_flag(method):
def wrapper(self, *args, **kwargs):
if not self.some_flag:
raise Exception("some_flag not set")
else:
result = method(self, *args, **kwargs)
return result
return wrapper
%%
**Find Equation for Set of Points**
[[https://www.google.com/search?hl=en&output=search&sclient=psy-ab&q=python+find+equation&btnK= python find roots of equation]]
%%(python)
>>> import numpy
>>> x = [1,50,99]
>>> y = [2,1,0.5]
>>> a,b,c = numpy.polyfit(x,y,2)
>>> f = lambda x: a*x**2 + b*x + c
>>> f(1)
2.0
>>> f(20)
1.5528946272386506
>>> f(50)
1.0000000000000002
>>> f(90)
0.55435235318617249
>>> f(100)
0.49500208246563937
>>> f(99)
0.5
%%
Verizon Opt-Out Site: http://www.vzw.com/myprivacy
**Somewhat Improved Wikka Installer Routine**
Diff for wikka.php:
%%
diff -r c8f8537f5e21 -r 4e091af5eacf php/wikka/wikka.php
--- a/php/wikka/wikka.php Tue Jan 17 10:01:24 2012 -0800
+++ b/php/wikka/wikka.php Tue Jan 17 11:43:38 2012 -0800
@@ -509,11 +509,15 @@
/**
* Compare versions, start installer if necessary.
+ * Wait to start installer below if this is an upgrade so we can limit access
+ * to admin users.
*/
-if (!isset($wakkaConfig['wakka_version'])) $wakkaConfig['wakka_version'] = 0;
-if ($wakkaConfig['wakka_version'] !== WAKKA_VERSION)
-{
- /**
+$is_new_install = ! isset($wakkaConfig['wakka_version']);
+$is_upgrade = $wakkaConfig['wakka_version'] !== WAKKA_VERSION;
+
+if ( $is_new_install ) {
+ $wakkaConfig['wakka_version'] = 0;
+ /**
* Start installer.
*
* Data entered by the user is submitted in $_POST, next action for the
@@ -590,6 +594,7 @@
exit;
}
+
/**
* Save session ID
*/
@@ -611,6 +616,39 @@
}
/**
+ * Check for upgrade. If so an user is admin, show setup page, else show
+ * a maintenance message to all other visitors.
+ */
+$is_admin = $wakka->IsAdmin($user);
+$at_install_step = ($_GET['installAction'] == 'writeconfig') && (isset($_POST['config']));
+$upgrade_allowed = $is_admin || $at_install_step;
+
+if ( $is_upgrade && $upgrade_allowed ) {
+ $installAction = 'default';
+ if (isset($_GET['installAction'])) $installAction = trim($_GET['installAction']); #312
+ if (file_exists('setup'.DIRECTORY_SEPARATOR.'header.php'))
+ include('setup'.DIRECTORY_SEPARATOR.'header.php'); else print ''.ERROR_SETUP_HEADER_MISSING.''; #89
+ if
+ (file_exists('setup'.DIRECTORY_SEPARATOR.$installAction.'.php'))
+ include('setup'.DIRECTORY_SEPARATOR.$installAction.'.php'); else print ''.ERROR_SETUP_FILE_MISSING.''; #89
+ if (file_exists('setup'.DIRECTORY_SEPARATOR.'footer.php'))
+ include('setup'.DIRECTORY_SEPARATOR.'footer.php'); else print ''.ERROR_SETUP_FOOTER_MISSING.''; #89
+ exit;
+}
+elseif ( $is_upgrade && $_GET['install_help'] ) {
+ print " POST,GET, SESSION variables
";
+ print '';
+ var_dump($_POST);
+ var_dump($_GET);
+ var_dump($_SESSION);
+ print '
';
+}
+elseif ( $is_upgrade ) {
+ die('Site Undergoing Temporary Maintenance
Please check back shortly.
');
+}
+
+
+/**
* Run the engine.
*/
if (!isset($handler)) $handler='';
%%
**IKEA Stand-Up Desks**
http://news.ycombinator.com/item?id=3442809
http://www.ikea.com/us/en/catalog/products/00115992/
http://www.ikea.com/us/en/catalog/products/60111123/
----
[[CategorySpecial]]