Why can a matrix 'protect' itself and how can I implement real
restrictions to custom classes?
I have been trying to get my ahead around validity of objects. I have read
Hadley's advanced programming and get what he says about he says about
aiming at your feet (with a gun):
"R doesn't protect you from yourself: you can easily shoot yourself in the
foot, but if
you don't aim the gun at your foot and pull the trigger, you won't have a
problem"
So this holds for S3. Looking for a more rigorous implementation I looked
into S4. The man page to setValidity brought up the following:
setClass("track",
representation(x="numeric", y = "numeric"))
t1 <- new("track", x=1:10, y=sort(stats::rnorm(10)))
## A valid "track" object has the same number of x, y values
validTrackObject <- function(object) {
if(length(object@x) == length(object@y)) TRUE
else paste("Unequal x,y lengths: ", length(object@x), ", ",
length(object@y), sep="")
}
## assign the function as the validity method for the class
setValidity("track", validTrackObject)
## t1 should be a valid "track" object
validObject(t1)
## Now we do something bad
t2 <- t1
t2@x <- 1:20
## This should generate an error
## Not run: try(validObject(t2))
Bottom line: If I do not add validObject to the initializer or constructor
there's little I can do. Also this post from Martin Morgan and
bioconductor's Seth Falcon was interesting, still though I could always
t2@x <- 1:1111.
I guess there's not much I can do about this? Though the matrix class for
example makes me wonder if there's an option.
a <- matrix(c(1:12),3,4)
cbind(a,"somechar")
# or similarily
a[1,1] <- "b"
Obviously all elements of a matrix have to be of the same class. So that's
why once a character is added all elements are coerced to the common
denominator, which is class character.
So my question is: How is this possible? In which way is the matrix class
defined, that it can protect the restriction "some class for all elements"
by any means? And is there a way to implement such a restriction to a
custom class, too?
E.g.: class of class 'customlist' that has to be a named list and names
being restricted to only be two chars long.
Bolduc
Thursday, 3 October 2013
Wednesday, 2 October 2013
importing 20k details in php using csv
importing 20k details in php using csv
I'm trying to insert a csv file with at least 20k lines, and it prompts my
browser to kill it self, I guess its taking up a lot of memory and its
processing it really slow, I'm also doing this for export and still the
same problem it takes too much time that the browser wanna kill it self.
Do you have any suggestions on how am I supposed to make it easier and
faster?
here are my codes:
Import:
if($_POST) {
$error = 0;
$tmpName = $_SESSION["csv_file"];
$fileExtension = $_SESSION["csv_ext"];
$fieldset = explode(",", trim($_REQUEST["csv_listfields"], ","));
unset($_SESSION["csv_file"]); unset($_SESSION["csv_ext"]);
if($tmpName){
if($fileExtension == 'csv'){
$fp = fopen($tmpName, 'r');
$fr = fread($fp, filesize($tmpName));
$line = explode("\n", $fr);
$field_pairs = array();
$csvpos=array();
$csvpos=$_POST['csv_pos'];
$getCsvPos=array();
$ifNotempty=0;
for($i=0;$i<count($csvpos);$i++){
if($csvpos[$i]!=-1){
$getCsvPos[$ifNotempty] = $csvpos[$i];
$ifNotempty++;
}
}
$fldcolumns = $line[0];
$fldcolumns = array_map("trim_field", explode(",",
$fldcolumns));
$forIndexValue=0;
foreach($fieldset as $fld){
$f = explode("=", $fld);
list($dbcol, $colcsv) = explode("=",$fld);
$field_pairs[$dbcol] = $getCsvPos[$forIndexValue] ;
$forIndexValue++;
}
$csvfile = fopen($tmpName, 'r');
$ctr = 0;
$total_uploaded = 0;
while (($datax = fgetcsv($csvfile, 1000, ",")) !== FALSE) {
$insert_crm = array();
$row_hascrm_assigned = false;
if($ctr != 0){
$ins_tbl = array();
$has_val = false;
foreach($field_pairs as $field => $colkey){
if( $datax[$colkey] != '' ) $has_val = true;
if($field != 'crm_group'){
if($field == 'password'){
$ins_tbl[$field] =
(strlen($datax[$colkey]) != 64) ?
hash("sha256", $datax[$colkey]) :
$datax[$colkey];
}elseif($field == 'birthdate' || $field ==
'dateIN'){
if($field=="dateIN"){
if($datax[$colkey] == ""){
$date = date("Y-m-d");
}else{
$date = $datax[$colkey];
}
$ins_tbl[$field] = $date;
}
}elseif($field == 'email'){
$ins_tbl[$field] =
strtolower($datax[$colkey]);
}else{
$ins_tbl[$field] =
mysql_real_escape_string($datax[$colkey]);
}
if($field != "dateIN"){
$ins_tbl["dateIN"] = date("Y-m-d");
}
if($field == "birthdate"){
$ins_tbl[$field] = $datax[$colkey];
}
}else{
foreach( explode(";",
$datax[count($fldcolumns) - 1]) as $cg ){
$cg = ($cg == "")?$datax[$colkey]:$cg;
$cg = htmlentities($cg);
$crm_sql = mysql_query("SELECT crm_gid
FROM tbl_crm_groups WHERE
crm_group_name = '".trim($cg,
"'")."'");
if(mysql_num_rows($crm_sql) < 1){
mysql_query("INSERT INTO
tbl_crm_groups (crm_group_name,
crm_date_created, custom) VALUES
('".$cg."', '".date('Y/m/d
H:i:s')."', 1)") or
die("</br>Error Message:
".mysql_error());
$crm_gid = mysql_insert_id();
}else{
$crm_gid = ($cg != "" &&
mysql_num_rows($crm_sql) > 0) ?
mysql_result($crm_sql, 0) : 1;
}
if(mysql_num_rows(mysql_query("SELECT
* FROM tbl_crm_members WHERE
crm_groupid = {$crm_gid} AND crm_uid =
{$crm_uid}")) < 1){
if(!in_array("INSERT INTO
tbl_crm_members(crm_groupid,
crm_uid, datejoined)
VALUES('{$crm_gid}',
'[give_me_uid]',
'".date("Y-m-d")."')",
$insert_crm))
$insert_crm[] = "INSERT INTO
tbl_crm_members(crm_groupid,
crm_uid, datejoined)
VALUES('{$crm_gid}',
'[give_me_uid]',
'".date("Y-m-d")."')";
}
}
}
}
if($has_val){
if(mysql_query("INSERT INTO tbl_members
(".implode(',',array_keys($ins_tbl)).") VALUES
(\"".implode('","',$ins_tbl)."\")")){
$last_member_inserted = mysql_insert_id();
$total_uploaded++;
if(count($insert_crm) > 0){
foreach($insert_crm as $ic){
mysql_query(
str_replace("[give_me_uid]",
$last_member_inserted, $ic) );
}
}else{
mysql_query( "INSERT INTO
tbl_crm_members(crm_groupid, crm_uid,
datejoined) VALUES('1',
".mysql_insert_id().",
'".date("Y-m-d")."')" );
}
}
}
}
$ctr++;
}
fclose($fp);
echo "<div style='color: green; margin: 10px;'>STATUS:
".$total_uploaded." record(s) successfully imported.
<br/>This page will reload in a couple of seconds.</div>";
}else{
exit("Not a valid csv file uploaded.");
}
unlink($tmpName);
echo "<script
type='text/javascript'>setTimeout(function(){parent.location.reload(true);},
2000);</script>";
}else{
exit("File uploaded improperly.");
}
}
Export:
if(IS_AJAX){
$output = array();
$sql_getcustomers = $_POST['val'];
/* CREATE CSV FILE FOR DOWNLOAD */
$filename2 = "csv/leads_".date("M-d-Y",time()).".csv";
$fp2 = fopen($filename2, 'w') or die("can't open file");
$sql2 = $sql_getcustomers;
$res2 = mysql_query($sql2);
// fetch a row and write the column names out to the file
$row2 = mysql_fetch_assoc($res2);
$line = "";
$comma = "";
if($row2){
foreach($row2 as $name => $value) {
$line .= $comma . '"' . str_replace('"', '""', $name)
. '"';
$comma = ",";
}
$line .= ",crm_group";
$line .= "\n";
fwrite($fp2, $line);
// remove the result pointer back to the start
mysql_data_seek($res2, 0);
// and loop through the actual data
while($row2 = mysql_fetch_assoc($res2)) {
$line = "";
$comma = "";
foreach($row2 as $index => $value) {
$line .= $comma . '"' . str_replace('"', '""',
utf8_decode($value)) . '"';
$comma = ",";
}
//** GET THE CRM GROUPS
$sql_get_group = "SELECT a.crm_group_name, b.* FROM
tbl_crm_members b JOIN tbl_crm_groups a ON (a.crm_gid
= b.crm_groupid) WHERE crm_uid = ".$row2["uid"];
$sql_get_groups = mysql_query($sql_get_group);
$res_get_groups = "";
while($sgg = mysql_fetch_object($sql_get_groups))
$res_get_groups .= $sgg->crm_group_name.";";
$line .= ",".trim($res_get_groups, ";");
$line .= "\n";
fwrite($fp2, $line);
}
fclose($fp2);
$output['data'] = 1;
$output['file'] = $filename2;
}else{
$output['data'] = 0;
}
}else{
$output['data'] = 0;
}
I'm trying to insert a csv file with at least 20k lines, and it prompts my
browser to kill it self, I guess its taking up a lot of memory and its
processing it really slow, I'm also doing this for export and still the
same problem it takes too much time that the browser wanna kill it self.
Do you have any suggestions on how am I supposed to make it easier and
faster?
here are my codes:
Import:
if($_POST) {
$error = 0;
$tmpName = $_SESSION["csv_file"];
$fileExtension = $_SESSION["csv_ext"];
$fieldset = explode(",", trim($_REQUEST["csv_listfields"], ","));
unset($_SESSION["csv_file"]); unset($_SESSION["csv_ext"]);
if($tmpName){
if($fileExtension == 'csv'){
$fp = fopen($tmpName, 'r');
$fr = fread($fp, filesize($tmpName));
$line = explode("\n", $fr);
$field_pairs = array();
$csvpos=array();
$csvpos=$_POST['csv_pos'];
$getCsvPos=array();
$ifNotempty=0;
for($i=0;$i<count($csvpos);$i++){
if($csvpos[$i]!=-1){
$getCsvPos[$ifNotempty] = $csvpos[$i];
$ifNotempty++;
}
}
$fldcolumns = $line[0];
$fldcolumns = array_map("trim_field", explode(",",
$fldcolumns));
$forIndexValue=0;
foreach($fieldset as $fld){
$f = explode("=", $fld);
list($dbcol, $colcsv) = explode("=",$fld);
$field_pairs[$dbcol] = $getCsvPos[$forIndexValue] ;
$forIndexValue++;
}
$csvfile = fopen($tmpName, 'r');
$ctr = 0;
$total_uploaded = 0;
while (($datax = fgetcsv($csvfile, 1000, ",")) !== FALSE) {
$insert_crm = array();
$row_hascrm_assigned = false;
if($ctr != 0){
$ins_tbl = array();
$has_val = false;
foreach($field_pairs as $field => $colkey){
if( $datax[$colkey] != '' ) $has_val = true;
if($field != 'crm_group'){
if($field == 'password'){
$ins_tbl[$field] =
(strlen($datax[$colkey]) != 64) ?
hash("sha256", $datax[$colkey]) :
$datax[$colkey];
}elseif($field == 'birthdate' || $field ==
'dateIN'){
if($field=="dateIN"){
if($datax[$colkey] == ""){
$date = date("Y-m-d");
}else{
$date = $datax[$colkey];
}
$ins_tbl[$field] = $date;
}
}elseif($field == 'email'){
$ins_tbl[$field] =
strtolower($datax[$colkey]);
}else{
$ins_tbl[$field] =
mysql_real_escape_string($datax[$colkey]);
}
if($field != "dateIN"){
$ins_tbl["dateIN"] = date("Y-m-d");
}
if($field == "birthdate"){
$ins_tbl[$field] = $datax[$colkey];
}
}else{
foreach( explode(";",
$datax[count($fldcolumns) - 1]) as $cg ){
$cg = ($cg == "")?$datax[$colkey]:$cg;
$cg = htmlentities($cg);
$crm_sql = mysql_query("SELECT crm_gid
FROM tbl_crm_groups WHERE
crm_group_name = '".trim($cg,
"'")."'");
if(mysql_num_rows($crm_sql) < 1){
mysql_query("INSERT INTO
tbl_crm_groups (crm_group_name,
crm_date_created, custom) VALUES
('".$cg."', '".date('Y/m/d
H:i:s')."', 1)") or
die("</br>Error Message:
".mysql_error());
$crm_gid = mysql_insert_id();
}else{
$crm_gid = ($cg != "" &&
mysql_num_rows($crm_sql) > 0) ?
mysql_result($crm_sql, 0) : 1;
}
if(mysql_num_rows(mysql_query("SELECT
* FROM tbl_crm_members WHERE
crm_groupid = {$crm_gid} AND crm_uid =
{$crm_uid}")) < 1){
if(!in_array("INSERT INTO
tbl_crm_members(crm_groupid,
crm_uid, datejoined)
VALUES('{$crm_gid}',
'[give_me_uid]',
'".date("Y-m-d")."')",
$insert_crm))
$insert_crm[] = "INSERT INTO
tbl_crm_members(crm_groupid,
crm_uid, datejoined)
VALUES('{$crm_gid}',
'[give_me_uid]',
'".date("Y-m-d")."')";
}
}
}
}
if($has_val){
if(mysql_query("INSERT INTO tbl_members
(".implode(',',array_keys($ins_tbl)).") VALUES
(\"".implode('","',$ins_tbl)."\")")){
$last_member_inserted = mysql_insert_id();
$total_uploaded++;
if(count($insert_crm) > 0){
foreach($insert_crm as $ic){
mysql_query(
str_replace("[give_me_uid]",
$last_member_inserted, $ic) );
}
}else{
mysql_query( "INSERT INTO
tbl_crm_members(crm_groupid, crm_uid,
datejoined) VALUES('1',
".mysql_insert_id().",
'".date("Y-m-d")."')" );
}
}
}
}
$ctr++;
}
fclose($fp);
echo "<div style='color: green; margin: 10px;'>STATUS:
".$total_uploaded." record(s) successfully imported.
<br/>This page will reload in a couple of seconds.</div>";
}else{
exit("Not a valid csv file uploaded.");
}
unlink($tmpName);
echo "<script
type='text/javascript'>setTimeout(function(){parent.location.reload(true);},
2000);</script>";
}else{
exit("File uploaded improperly.");
}
}
Export:
if(IS_AJAX){
$output = array();
$sql_getcustomers = $_POST['val'];
/* CREATE CSV FILE FOR DOWNLOAD */
$filename2 = "csv/leads_".date("M-d-Y",time()).".csv";
$fp2 = fopen($filename2, 'w') or die("can't open file");
$sql2 = $sql_getcustomers;
$res2 = mysql_query($sql2);
// fetch a row and write the column names out to the file
$row2 = mysql_fetch_assoc($res2);
$line = "";
$comma = "";
if($row2){
foreach($row2 as $name => $value) {
$line .= $comma . '"' . str_replace('"', '""', $name)
. '"';
$comma = ",";
}
$line .= ",crm_group";
$line .= "\n";
fwrite($fp2, $line);
// remove the result pointer back to the start
mysql_data_seek($res2, 0);
// and loop through the actual data
while($row2 = mysql_fetch_assoc($res2)) {
$line = "";
$comma = "";
foreach($row2 as $index => $value) {
$line .= $comma . '"' . str_replace('"', '""',
utf8_decode($value)) . '"';
$comma = ",";
}
//** GET THE CRM GROUPS
$sql_get_group = "SELECT a.crm_group_name, b.* FROM
tbl_crm_members b JOIN tbl_crm_groups a ON (a.crm_gid
= b.crm_groupid) WHERE crm_uid = ".$row2["uid"];
$sql_get_groups = mysql_query($sql_get_group);
$res_get_groups = "";
while($sgg = mysql_fetch_object($sql_get_groups))
$res_get_groups .= $sgg->crm_group_name.";";
$line .= ",".trim($res_get_groups, ";");
$line .= "\n";
fwrite($fp2, $line);
}
fclose($fp2);
$output['data'] = 1;
$output['file'] = $filename2;
}else{
$output['data'] = 0;
}
}else{
$output['data'] = 0;
}
Xcode - Use multiple SDKs on XCode 5
Xcode - Use multiple SDKs on XCode 5
I have a project which requires iOS 6.1 SDK and I have upgraded to XCode5
and dont see iOS 6.1 SDK in Build Settings -> Base SDKs. I have earlier
SDK saved separately and tried copy-pasting it in
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs
Directory. But it doesn't show up in Build settings -> Base SDKS. How can
I use multiple SDKs in Xcode 5?
I have a project which requires iOS 6.1 SDK and I have upgraded to XCode5
and dont see iOS 6.1 SDK in Build Settings -> Base SDKs. I have earlier
SDK saved separately and tried copy-pasting it in
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs
Directory. But it doesn't show up in Build settings -> Base SDKS. How can
I use multiple SDKs in Xcode 5?
Nginx+Tornado static files aren't being handled by nginx, why?
Nginx+Tornado static files aren't being handled by nginx, why?
I'm trying to set up Tornado server behind nginx proxy, here're the
relevant bits of the configuration:
server {
listen 80;
server_name localhost;
location html/ {
root /srv/www/intj.com/html;
index login.html;
if ($query_string) {
expires max;
}
}
location = /favicon.ico {
rewrite (.*) /html/favicon.ico;
}
location = /robots.txt {
rewrite (.*) /html/robots.txt;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://localhost:8888;
}
}
I can get to my Python server through nginx, but when I request a static
pages, such as, say login.html, which is located in
/srv/www/intj.com/html/login.html, instead of loading the static file, the
request is forwarded to Tornado, which doesn't know what to make of it.
What did I do wrong?
I'm trying to set up Tornado server behind nginx proxy, here're the
relevant bits of the configuration:
server {
listen 80;
server_name localhost;
location html/ {
root /srv/www/intj.com/html;
index login.html;
if ($query_string) {
expires max;
}
}
location = /favicon.ico {
rewrite (.*) /html/favicon.ico;
}
location = /robots.txt {
rewrite (.*) /html/robots.txt;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://localhost:8888;
}
}
I can get to my Python server through nginx, but when I request a static
pages, such as, say login.html, which is located in
/srv/www/intj.com/html/login.html, instead of loading the static file, the
request is forwarded to Tornado, which doesn't know what to make of it.
What did I do wrong?
Tuesday, 1 October 2013
IndexController newly added method give 404 error
IndexController newly added method give 404 error
I am new to the zf so I guess this is a basic level problem for you guys.I
have a zf1 project and I am studying the code.In my IndexController all
the old methods are working except my new method,It gives me a 404
error.Do i need to specify the method names anywhere? or what can I do for
this error ?
my controller path -
application/modules/default/controllers/IndexController.php
my newly added method that is not work in IndexController.php
public function mytestAction() { }
Old method that is work in IndexController.php
public function termsconditionsAction() { }
views path - application/modules/default/views/index/mytest.phtml
I am new to the zf so I guess this is a basic level problem for you guys.I
have a zf1 project and I am studying the code.In my IndexController all
the old methods are working except my new method,It gives me a 404
error.Do i need to specify the method names anywhere? or what can I do for
this error ?
my controller path -
application/modules/default/controllers/IndexController.php
my newly added method that is not work in IndexController.php
public function mytestAction() { }
Old method that is work in IndexController.php
public function termsconditionsAction() { }
views path - application/modules/default/views/index/mytest.phtml
Web client works on GlassFish 4, but not on GlassFish 3
Web client works on GlassFish 4, but not on GlassFish 3
I'm developing some enterprise web site using Java EE 6 technology. My
collegue was assigned working on web client and he develops it on his PC
using JSF 2.1 and GlassFish 4.0. When I tried running that web client on
my PC, which at first had GlassFish 3.1 instaled, it didn't work. Not a
single line of xhtml code was rendered on my web browser.
Later on, I installed GlassFish 4 and deployed application on it and look
at miracle - it worked! So basicaly, my question is simple: Why does that
happend? Why can't I run web client on older version of GlassFish server?
P.S. No error or warning is recorded on server's log when I try to run it
on GlassFish 3.1.
I'm developing some enterprise web site using Java EE 6 technology. My
collegue was assigned working on web client and he develops it on his PC
using JSF 2.1 and GlassFish 4.0. When I tried running that web client on
my PC, which at first had GlassFish 3.1 instaled, it didn't work. Not a
single line of xhtml code was rendered on my web browser.
Later on, I installed GlassFish 4 and deployed application on it and look
at miracle - it worked! So basicaly, my question is simple: Why does that
happend? Why can't I run web client on older version of GlassFish server?
P.S. No error or warning is recorded on server's log when I try to run it
on GlassFish 3.1.
Should graduate applicants follow instructions by department NOT to contact potential advisors=?iso-8859-1?Q?=3F_=96_academia.stackexchange.com?=
Should graduate applicants follow instructions by department NOT to
contact potential advisors? – academia.stackexchange.com
I am applying for doctoral programs in biostatistics and have read
multiple places to attempt to set up contact with a potential advisor
before applying. What if the department website specifically …
contact potential advisors? – academia.stackexchange.com
I am applying for doctoral programs in biostatistics and have read
multiple places to attempt to set up contact with a potential advisor
before applying. What if the department website specifically …
Subscribe to:
Comments (Atom)