Python time zone parsing
I have a little problem parsing a date in python
This is the date I have to parse:
Sun Sep 15, 2013 12:10pm EDT
And that is the code I'm using to parse it:
datetime.strptime( date, "%a %b %d, %Y %I:%M%p %Z")
Everything is fine but the time-zone parsing, which always return a
ValueError exception. I've also tried pytz but without any success.
So how can i parse this kind date using python?
Sunday, 15 September 2013
Sort by AVG(rating)
Sort by AVG(rating)
I am trying to write a mySQL-query that sorts by first suburb and then
AVG(rating_table.rating).
Here is the street_table:
id street_name suburb
0 streetone subone
1 streettwo subthree
2 streetthree subthree
3 streetfour subtwo
And here is the rating_table:
street_id rating
1 1
2 1
3 4
2 2
1 3
And this is the result I am looking for:
id suburb avarage_rating
0 subone (no rating)
1 subtwo 1 + 3 / 2 = 2
3 subthree 4 / 1 = 4 (Just one vote..)
2 subthree 2 + 1 / 2 = 1.5
(As you can see, #3 is before #2 because of the avarage_rating)
I am trying to write a mySQL-query that sorts by first suburb and then
AVG(rating_table.rating).
Here is the street_table:
id street_name suburb
0 streetone subone
1 streettwo subthree
2 streetthree subthree
3 streetfour subtwo
And here is the rating_table:
street_id rating
1 1
2 1
3 4
2 2
1 3
And this is the result I am looking for:
id suburb avarage_rating
0 subone (no rating)
1 subtwo 1 + 3 / 2 = 2
3 subthree 4 / 1 = 4 (Just one vote..)
2 subthree 2 + 1 / 2 = 1.5
(As you can see, #3 is before #2 because of the avarage_rating)
Duplicate the old data when the datagridview refreshed
Duplicate the old data when the datagridview refreshed
i already can add the new data to the database and display that database
through datagridview, and i command to refresh the datagridview whenever i
click "Add" button (Adding new data to the database). But it is duplicate
the old data with the new one.
Here is the screenshot that show it duplicate after adding new data and
refresh datagridview:
In the above image, there is duplicate data of ID "16", whenever i enter
the new value and insert it to the database, it add the new (current)
value to the database, but the old value will be duplicated. Therefore, i
have to quit the program and re-launch it again. But, is there any other
ways to solve this (Old data not being duplicated after add a new data to
the database and refresh the datagridview)?
Here is my code:
private void ViewDatabase(object sender, EventArgs e)
{
using (OleDbConnection conn = new
OleDbConnection(connectionString))
{
string query = "SELECT * FROM [Table]";
conn.Open();
using (OleDbDataAdapter adapter = new
OleDbDataAdapter(query, conn))
{
adapter.Fill(ds, "Table");
dataGridView1.DataSource = ds.Tables[0];
}
conn.Close();
}
}
private void Add(object sender, EventArgs e)
{
using (OleDbConnection conn = new
OleDbConnection(connectionString))
{
string query = "INSERT INTO [Table] ([ProductCode],
[Quantity], [Description], [Price]) VALUES (@ProductCode,
@Quantity, @Description, @Price)";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
cmd.Parameters.Add("@ProductCode",
System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["@ProductCode"].Value =
this.numericTextBox1.Text;
cmd.Parameters.Add("@Quantity",
System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["@Quantity"].Value =
this.numericUpDown1.Value;
cmd.Parameters.Add("@Description",
System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["@Description"].Value =
this.textBox3.Text;
cmd.Parameters.Add("@Price",
System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["@Price"].Value = this.textBox4.Text;
cmd.ExecuteNonQuery();
if (choice.comboBox1.Text == "English")
{
System.Media.SoundPlayer sound = new
System.Media.SoundPlayer(@"C:\Windows\Media\Windows
Exclamation.wav");
sound.Play();
DialogResult dialogResult = MessageBox.Show("Added
Successfully!", "Success", MessageBoxButtons.OK);
if (dialogResult == DialogResult.OK)
{
ViewDatabase(sender, e);
ClearTextBoxes(sender, e);
}
}
i already can add the new data to the database and display that database
through datagridview, and i command to refresh the datagridview whenever i
click "Add" button (Adding new data to the database). But it is duplicate
the old data with the new one.
Here is the screenshot that show it duplicate after adding new data and
refresh datagridview:
In the above image, there is duplicate data of ID "16", whenever i enter
the new value and insert it to the database, it add the new (current)
value to the database, but the old value will be duplicated. Therefore, i
have to quit the program and re-launch it again. But, is there any other
ways to solve this (Old data not being duplicated after add a new data to
the database and refresh the datagridview)?
Here is my code:
private void ViewDatabase(object sender, EventArgs e)
{
using (OleDbConnection conn = new
OleDbConnection(connectionString))
{
string query = "SELECT * FROM [Table]";
conn.Open();
using (OleDbDataAdapter adapter = new
OleDbDataAdapter(query, conn))
{
adapter.Fill(ds, "Table");
dataGridView1.DataSource = ds.Tables[0];
}
conn.Close();
}
}
private void Add(object sender, EventArgs e)
{
using (OleDbConnection conn = new
OleDbConnection(connectionString))
{
string query = "INSERT INTO [Table] ([ProductCode],
[Quantity], [Description], [Price]) VALUES (@ProductCode,
@Quantity, @Description, @Price)";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
cmd.Parameters.Add("@ProductCode",
System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["@ProductCode"].Value =
this.numericTextBox1.Text;
cmd.Parameters.Add("@Quantity",
System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["@Quantity"].Value =
this.numericUpDown1.Value;
cmd.Parameters.Add("@Description",
System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["@Description"].Value =
this.textBox3.Text;
cmd.Parameters.Add("@Price",
System.Data.OleDb.OleDbType.Integer);
cmd.Parameters["@Price"].Value = this.textBox4.Text;
cmd.ExecuteNonQuery();
if (choice.comboBox1.Text == "English")
{
System.Media.SoundPlayer sound = new
System.Media.SoundPlayer(@"C:\Windows\Media\Windows
Exclamation.wav");
sound.Play();
DialogResult dialogResult = MessageBox.Show("Added
Successfully!", "Success", MessageBoxButtons.OK);
if (dialogResult == DialogResult.OK)
{
ViewDatabase(sender, e);
ClearTextBoxes(sender, e);
}
}
How to draw multiple lines with finger? (Android)
How to draw multiple lines with finger? (Android)
I have tried to draw multiple lines like this:
`
l1 = new Path();
l2 = new Path();
l3 = new Path();
l4 = new Path();`
---
`mPathList.add(l1...l4);`
---
`public void onDraw(Canvas canvas) {
...
for (Path path : mPathList) {
canvas.drawPath(path, mOverlayPaint);
}
...
}`
---
`case MotionEvent.ACTION_MOVE:
int X = (int)me.getRawX();
int Y = (int)me.getRawY();
l1.moveTo(X, Y);
l2.moveTo(X + 5, Y);
l3.moveTo(X + 10, Y);
l4.moveTo(X + 15, Y);
break;`
But when I'm trying to draw something, FPS will slowly decrease. Any ideas
how to make it works fine? P.S. Sorry for my English, please
I have tried to draw multiple lines like this:
`
l1 = new Path();
l2 = new Path();
l3 = new Path();
l4 = new Path();`
---
`mPathList.add(l1...l4);`
---
`public void onDraw(Canvas canvas) {
...
for (Path path : mPathList) {
canvas.drawPath(path, mOverlayPaint);
}
...
}`
---
`case MotionEvent.ACTION_MOVE:
int X = (int)me.getRawX();
int Y = (int)me.getRawY();
l1.moveTo(X, Y);
l2.moveTo(X + 5, Y);
l3.moveTo(X + 10, Y);
l4.moveTo(X + 15, Y);
break;`
But when I'm trying to draw something, FPS will slowly decrease. Any ideas
how to make it works fine? P.S. Sorry for my English, please
Saturday, 14 September 2013
PERL Mechanize cannot print results when reading input data from a data file
PERL Mechanize cannot print results when reading input data from a data file
I created a PERL program to retrieve the stock exchanges from Yahoo
Finance, given a list of stock symbols.
The following code writes to a file
#!/usr/bin/perl
# program name: FindStockExchange.pl
use strict;
use warnings;
use WWW::Mechanize;
use Storable;
use Getopt::Long;
#cmd: clear; ./FindStockExchange.pl A AA AA.V AAA.TO -f ~/symbol_out.txt
# Find Stock Exchange for a given Stock Symbole
# Command line options:
# -s Symbol
# -f Output filename
# Initialize variables:
my $urlBase='http://finance.yahoo.com/q?s='; # Before symbol
my $urlSuffix='&ql=0'; # After symbol
my $url='';
my $oFile='';
my $symbol='';
my $c='';
# Read command line options.
GetOptions(
'f=s' => \$oFile #Output filename
) or die "Incorrect usage!\n";
# Ouptput file(s)
open(OUTSYM,">$oFile") || die "Couldn't open file $oFile, $!";
my $m = WWW::Mechanize->new( autocheck=>0 );
foreach $symbol ( @ARGV ){
$url=$urlBase . $symbol .$urlSuffix;
$m->get($url);
$c = $m->content; # Places html page source text into variable
# Text pattern: <div class="title"><h2>Electrolux AB (ELUXY)</h2> <span
class="rtq_exch"><span class="rtq_dash">-</span>OTC Markets
</span></div>
$c =~ m{rtq_dash\">-</span>(.*?)</span>}s or next;
print OUTSYM "$symbol\t$1\n"; # Write output file
print "$symbol\t$1\t" . "\n"; # Write to STDOUT
} # End foreach
close OUTFIL;
The following code reads from an input file and creates an empty data
file. The input file contained the following stock symbols:
A AA AA.V AAA.TO
#!/usr/bin/perl
# program name: FindStockExchange2.pl
use strict;
use warnings;
use WWW::Mechanize;
use Storable;
use Getopt::Long;
#cmd: clear; ./FindStockExchange2.pl -i ~/symbol_in.txt -o ~/symbol_out2.txt
# Find Stock Exchange for a given Stock Symbole
# Command line options:
# -i Input filename
# -o Output filename
# Initialize variables:
my $urlBase='http://finance.yahoo.com/q?s='; # Before symbol
my $urlSuffix='&ql=0'; # After symbol
my $url='';
my $oFile='';
my $iFile='';
my $symbol='';
my $c='';
# Read command line options.
GetOptions(
'o=s' => \$oFile, #Output filename
'i=s' => \$iFile #Input filename
) or die "Incorrect usage!\n";
# File(s)
open(OUTSYM,">$oFile") || die "Couldn't open file $oFile, $!";
open(INSYM,"<$iFile") || die "Couldn't open file $iFile, $!";
my $m = WWW::Mechanize->new( autocheck=>0 );
while ( <INSYM> ){
$symbol=chomp($_);
$url=$urlBase . $symbol .$urlSuffix;
$m->get($url);
$c = $m->content; # Places html page source text into variable
# Text pattern: <div class="title"><h2>Electrolux AB (ELUXY)</h2> <span
class="rtq_exch"><span class="rtq_dash">-</span>OTC Markets
</span></div>
$c =~ m{rtq_dash\">-</span>(.*?)</span>}s or next;
print OUTSYM "$symbol\t$1\n"; # Write output file
print "$symbol\t$1\t" . "\n"; # Write to STDOUT
} # End while
close INSYM;
close OUTSYM;
Why would changing from a foreach loop to reading an input file using a
while loop produce different results?
Foreeah code creates a file containing the following: A NYSE
AA NYSE
AA.V TSXV
AAA.TO Toronto
To-Air-Is:~ vlis
While loop creates an empty file.
I created a PERL program to retrieve the stock exchanges from Yahoo
Finance, given a list of stock symbols.
The following code writes to a file
#!/usr/bin/perl
# program name: FindStockExchange.pl
use strict;
use warnings;
use WWW::Mechanize;
use Storable;
use Getopt::Long;
#cmd: clear; ./FindStockExchange.pl A AA AA.V AAA.TO -f ~/symbol_out.txt
# Find Stock Exchange for a given Stock Symbole
# Command line options:
# -s Symbol
# -f Output filename
# Initialize variables:
my $urlBase='http://finance.yahoo.com/q?s='; # Before symbol
my $urlSuffix='&ql=0'; # After symbol
my $url='';
my $oFile='';
my $symbol='';
my $c='';
# Read command line options.
GetOptions(
'f=s' => \$oFile #Output filename
) or die "Incorrect usage!\n";
# Ouptput file(s)
open(OUTSYM,">$oFile") || die "Couldn't open file $oFile, $!";
my $m = WWW::Mechanize->new( autocheck=>0 );
foreach $symbol ( @ARGV ){
$url=$urlBase . $symbol .$urlSuffix;
$m->get($url);
$c = $m->content; # Places html page source text into variable
# Text pattern: <div class="title"><h2>Electrolux AB (ELUXY)</h2> <span
class="rtq_exch"><span class="rtq_dash">-</span>OTC Markets
</span></div>
$c =~ m{rtq_dash\">-</span>(.*?)</span>}s or next;
print OUTSYM "$symbol\t$1\n"; # Write output file
print "$symbol\t$1\t" . "\n"; # Write to STDOUT
} # End foreach
close OUTFIL;
The following code reads from an input file and creates an empty data
file. The input file contained the following stock symbols:
A AA AA.V AAA.TO
#!/usr/bin/perl
# program name: FindStockExchange2.pl
use strict;
use warnings;
use WWW::Mechanize;
use Storable;
use Getopt::Long;
#cmd: clear; ./FindStockExchange2.pl -i ~/symbol_in.txt -o ~/symbol_out2.txt
# Find Stock Exchange for a given Stock Symbole
# Command line options:
# -i Input filename
# -o Output filename
# Initialize variables:
my $urlBase='http://finance.yahoo.com/q?s='; # Before symbol
my $urlSuffix='&ql=0'; # After symbol
my $url='';
my $oFile='';
my $iFile='';
my $symbol='';
my $c='';
# Read command line options.
GetOptions(
'o=s' => \$oFile, #Output filename
'i=s' => \$iFile #Input filename
) or die "Incorrect usage!\n";
# File(s)
open(OUTSYM,">$oFile") || die "Couldn't open file $oFile, $!";
open(INSYM,"<$iFile") || die "Couldn't open file $iFile, $!";
my $m = WWW::Mechanize->new( autocheck=>0 );
while ( <INSYM> ){
$symbol=chomp($_);
$url=$urlBase . $symbol .$urlSuffix;
$m->get($url);
$c = $m->content; # Places html page source text into variable
# Text pattern: <div class="title"><h2>Electrolux AB (ELUXY)</h2> <span
class="rtq_exch"><span class="rtq_dash">-</span>OTC Markets
</span></div>
$c =~ m{rtq_dash\">-</span>(.*?)</span>}s or next;
print OUTSYM "$symbol\t$1\n"; # Write output file
print "$symbol\t$1\t" . "\n"; # Write to STDOUT
} # End while
close INSYM;
close OUTSYM;
Why would changing from a foreach loop to reading an input file using a
while loop produce different results?
Foreeah code creates a file containing the following: A NYSE
AA NYSE
AA.V TSXV
AAA.TO Toronto
To-Air-Is:~ vlis
While loop creates an empty file.
caesar cipher problems in python
caesar cipher problems in python
I have the following code which gives me error saying it cannot convert
'int' object to str implicitly. the if statements were used so if a letter
in WORD is ascii 125 and i had to shift it by 5, I wanted the shift to
goto ascii 32 and calculate. if any of the letters in word was less than
ascii 32 shift, then shift to 32.
word=input("enter something to encrypt")
offset=int(input("Enter a number to shift it by"))
for char in word:
newword= ord(char)
newword =newword+ chr(newword) + offset
if newwword<32:
result=result+95
elif newword >126:
result=result-95
I have the following code which gives me error saying it cannot convert
'int' object to str implicitly. the if statements were used so if a letter
in WORD is ascii 125 and i had to shift it by 5, I wanted the shift to
goto ascii 32 and calculate. if any of the letters in word was less than
ascii 32 shift, then shift to 32.
word=input("enter something to encrypt")
offset=int(input("Enter a number to shift it by"))
for char in word:
newword= ord(char)
newword =newword+ chr(newword) + offset
if newwword<32:
result=result+95
elif newword >126:
result=result-95
Cleaner Syntax for HTTP Object in AJAX?
Cleaner Syntax for HTTP Object in AJAX?
AJAX noob here... So this code below works for me which is called during
an onclick event:
function updateText() {
var http = getHTTPObject();
http.open("GET","ajax_info.asp",true);
http.onreadystatechange = function() {
if (http.readyState == 4) {
document.getElementById("myDiv").innerHTML=http.responseText;
}
}
http.send();
}
However, when I tried to clean it like below, it no longer works. Is the
code below not a correct callback syntax?
function handleHTTPResponse() {
if (http.readyState == 4) {
document.getElementById("myDiv").innerHTML=http.responseText;
}
}
function updateText() {
var http = getHTTPObject();
http.open("GET","ajax_info.asp",true);
http.onreadystatechange = handleHTTPResponse;
http.send();
}
AJAX noob here... So this code below works for me which is called during
an onclick event:
function updateText() {
var http = getHTTPObject();
http.open("GET","ajax_info.asp",true);
http.onreadystatechange = function() {
if (http.readyState == 4) {
document.getElementById("myDiv").innerHTML=http.responseText;
}
}
http.send();
}
However, when I tried to clean it like below, it no longer works. Is the
code below not a correct callback syntax?
function handleHTTPResponse() {
if (http.readyState == 4) {
document.getElementById("myDiv").innerHTML=http.responseText;
}
}
function updateText() {
var http = getHTTPObject();
http.open("GET","ajax_info.asp",true);
http.onreadystatechange = handleHTTPResponse;
http.send();
}
Subscribe to:
Posts (Atom)