Re: Why Python When There Is Perl?

Liste des GroupesRevenir à col advocacy 
Sujet : Re: Why Python When There Is Perl?
De : nospam (at) *nospam* dfs.com (DFS)
Groupes : comp.os.linux.advocacy
Date : 21. Mar 2024, 00:31:00
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <utfo33$1o6mu$3@dont-email.me>
References : 1 2
User-Agent : Betterbird (Windows)
On 3/19/2024 7:47 PM, Physfitfreak wrote:

I think, if I had not relied on COLA to provide help on these matters, by now, I'd be months into fruitful learning of ... probably C++ and into several exciting programming projects.
First off, don't listen to ANYTHING Feeb says about programming.  He's incompetent, a liar, a nutcase, and makes contradictory statements all the time.
"Nothing but C from now on!"
"Only assembly for me from now on!"
"Python is for talentless idiots"
"Use Perl"
"C++ is for degenerate sissies"
"C++ is too complex"
"I despise OO programming"
"most programmers don't program.  They will use frameworks that
   literally produce the code for them."
That's all lies and idiocy.
I recently showed you 3 versions (C, python, VBA) of the same program. Python was the fewest lines of code, looked the cleanest, and was quickest to write.
After I learned enough python, I started working on the same problems with C.  C executes 5x to 10x faster than Python, but requires 3x to 5x the amt of time and code.  Note: you will NEVER recoup the extra C development time with savings in program execution time.
I'd say work on learning Python and C at the same time.  Leave C++ for later.

This place is wasteful. It is like a drinking parlor. You go there everyday and drink and joke and curse and argue, but in the background, it is making you an alcoholic, making you drift.
If you wanted to focus on programming, you would.  Don't blame cola for your moral shortcomings.
Here's something to get you started with python.  This code reads the Linux kernel CREDITS file, parses it, and posts the data to a SQLite database table.  I just now wrote the python, but the original program was in C 3 years ago (for yet another programming challenge that weasel Feeb couldn't handle).
======================================================================
Python
======================================================================
import sqlite3
#copy or concat lines into string
#if entry has 1 linetype (NEWPDS), copy line into var
#if entry has 2+ of the same linetype, concat lines
def processvals(val, data, sep):
if val[0] == ' ':
val = data
else:
val += sep
val += data
return val
#strings
entry,Nval,Eval,Wval,Pval,Dval,Sval = ' ',' ',' ',' ',' ',' ',' '
#create and open new SQLite db file
dbname = "kernel_credits_py.db";
conn = sqlite3.connect(dbname)
db = conn.cursor()
#create table
db.execute("DROP TABLE IF EXISTS CREDITS;")
db.execute("BEGIN TRANSACTION;")
sql =  "CREATE TABLE CREDITS(nameid INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL, email TEXT DEFAULT 'na' NOT NULL, web TEXT DEFAULT 'na' NOT NULL, pgp TEXT DEFAULT 'na' NOT NULL, description TEXT DEFAULT 'na' NOT NULL, address TEXT DEFAULT 'na' NOT NULL);";
db.execute(sql)
#read kernel CREDITS file, post data to db table
#each credit entry has one name, and 0+ other lines
creditsfile = "KERNEL_CREDITS.txt"
lines = open(creditsfile,'r').readlines()
for line in lines:
if len(line) > 3: entry = line[3:-1]  #drop NEWPDS prefixes
if line[0]=='N': Nval = entry
if line[0]=='E': Eval = processvals(Eval,entry," / ")
if line[0]=='W': Wval = processvals(Wval,entry," / ")
if line[0]=='P': Pval = processvals(Pval,entry," / ")
if line[0]=='D': Dval = processvals(Dval,entry," / ")
if line[0]=='S': Sval = processvals(Sval,entry,", ")

if line[0] == '\n':
sql = "INSERT INTO CREDITS (name,email,web,pgp,description,address) VALUES (?,?,?,?,?,?);";
db.execute(sql,(Nval, Eval, Wval, Pval, Dval, Sval))
entry,Nval,Eval,Wval,Pval,Dval,Sval = ' ',' ',' ',' ',' ',' ',' '

conn.commit()
#print data in table
rows = db.execute("SELECT * FROM CREDITS ORDER BY nameID;")
for row in rows:
for i,colname in enumerate(db.description):
print("%-12s = %s" % (colname[0], row[i]))
print()

#count rows in table
db.execute("SELECT COUNT(*) FROM CREDITS;")
print("Done.  %d records were posted to SQLite database '%s'" % (db.fetchone()[0],dbname))
#version
print("running SQLite %s" % sqlite3.sqlite_version)
db.close()
conn.close()
======================================================================
======================================================================
C
======================================================================
// 1. install libsqlite3-dev (sudo apt install libsqlite3-dev)
// 2. copy the Linux kernel CREDITS file to the folder this program is in
// 3. rename it KERNEL_CREDITS.txt
// compilation: gcc -Wall source.c -o executable -lsqlite3
#include <sqlite3.h>
#include <stdio.h>
#include <string.h>
//used when retrieving data from SQLite
int callback(void *na, int argc, char **argv, char **azColName)
{
     na = 0;
     for (int i = 0; i < argc; i++) {
         printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
     }
     printf("\n");
     return 0;
}
//right trim
void rtrim(char *s)
{
s[strlen(s)-1]='\0';
}
//copy or concat lines into string
//if entry has 1 linetype (NEWPDS), copy line into var
//if entry has 2+ of the same linetype, concat lines
void processvals(char *val, char *data, char *sep)
{
if(val[0] == '\0')
{
strncpy(val,data,strlen(data));
}
else
{ strncat(val,sep,3);
strncat(val,data,strlen(data));
}
}
int main(void) {
     sqlite3 *db;
     sqlite3_stmt *stmt;
     char *error_msg = 0;
int rc = 0;
rc += 1;  //without this nonsense, got compiler warning variable set but not used
char line[1024] = "", entry[1024];
char Nval[1024] = ""; char Eval[1024] = ""; char Wval[1024] = "";
char Pval[1024] = ""; char Dval[1024] = ""; char Sval[1024] = {"\0"};
//count lines in file
int lines = 0;
FILE *fin = fopen("KERNEL_CREDITS.txt","r");
while (fgets(line,sizeof line, fin)!= NULL) {lines++;}

     //create and open new db file
char *dbname = "kernel_credits.db";
     rc = sqlite3_open(dbname, &db);
     //create table
     char *sql = "DROP TABLE IF EXISTS CREDITS;"
"BEGIN TRANSACTION;"
                 "CREATE TABLE CREDITS(nameid INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL, email TEXT DEFAULT 'na' NOT NULL, web TEXT DEFAULT 'na' NOT NULL, pgp TEXT DEFAULT 'na' NOT NULL, description TEXT DEFAULT 'na' NOT NULL, address TEXT DEFAULT 'na' NOT NULL);";
     rc = sqlite3_exec(db, sql, 0, 0, &error_msg);

//read kernel CREDITS file, post data to db table
     //each credit entry has one name, and 0+ other lines
     int currentline = 0;
rewind(fin);
while (fgets(line,sizeof line, fin)!= NULL)
{
currentline++;
rtrim(line);
if(strlen(line) > 3) //drop NEWPDS prefixes
{
memset(entry,'\0',sizeof entry);
memcpy(entry, &line[3], strlen(line)-3);
entry[strlen(entry)] = '\0';
}
if(line[0]=='N') {strncpy(Nval,entry,strlen(entry));}
if(line[0]=='E') {processvals(Eval,entry," / ");}
if(line[0]=='W') {processvals(Wval,entry," / ");}
if(line[0]=='P') {processvals(Pval,entry," / ");}
if(line[0]=='D') {processvals(Dval,entry," / ");}
if(line[0]=='S') {processvals(Sval,entry,", ");}

if(line[0] == '\0' || currentline == lines)
{

sql = "INSERT INTO CREDITS (name,email,web,pgp,description,address) "
"VALUES (?,?,?,?,?,?);";
sqlite3_prepare_v2(db, sql, 2048, &stmt, NULL);
if(stmt != NULL) {

sqlite3_bind_text(stmt, 1, Nval, strlen(Nval), SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, Eval, strlen(Eval), SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, Wval, strlen(Wval), SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 4, Pval, strlen(Pval), SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 5, Dval, strlen(Dval), SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 6, Sval, strlen(Sval), SQLITE_TRANSIENT);

sqlite3_step(stmt);
sqlite3_finalize(stmt);

memset(entry,'\0',sizeof entry);
memset(Nval,'\0',sizeof Nval);
memset(Eval,'\0',sizeof Eval);
memset(Wval,'\0',sizeof Wval);
memset(Pval,'\0',sizeof Pval);
memset(Dval,'\0',sizeof Dval);
memset(Sval,'\0',sizeof Sval);

}
}
}
fclose(fin);
rc = sqlite3_exec(db, "COMMIT;", 0, 0, &error_msg);

     //print rows in table
     sql = "SELECT * FROM CREDITS ORDER BY nameID";
     rc = sqlite3_exec(db, sql, callback, 0, &error_msg);
//count rows in table
sql  = "SELECT COUNT(*) FROM CREDITS;";
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
rc = sqlite3_step(stmt);
int rows = sqlite3_column_int(stmt, 0);

     //finish
     sqlite3_finalize(stmt);
     sqlite3_close(db);
     printf("Done.  %d records were posted to SQLite database '%s'\n",rows,dbname);

     //version
     printf("running SQLite %s\n", sqlite3_libversion());
     return 0;
}
======================================================================

Date Sujet#  Auteur
5 Oct 24 o 

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal