Re: Why Python When There Is Perl?

Liste des GroupesRevenir à ol 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
19 Mar 24 * Why Python When There Is Perl?308Lester Thorpe
20 Mar 24 +* Re: Why Python When There Is Perl?10Lawrence D'Oliveiro
20 Mar 24 i+* Re: Why Python When There Is Perl?2rbowman
20 Mar 24 ii`- Re: Why Python When There Is Perl?1Lawrence D'Oliveiro
20 Mar 24 i`* Re: Why Python When There Is Perl?7Nuxxie
20 Mar 24 i +* Re: Why Python When There Is Perl?2Chris Ahlstrom
21 Mar 24 i i`- Re: Why Python When There Is Perl?1rbowman
21 Mar 24 i `* Re: Why Python When There Is Perl?4rbowman
21 Mar 24 i  `* Re: Why Python When There Is Perl?3Lawrence D'Oliveiro
21 Mar 24 i   `* Re: Why Python When There Is Perl?2Lawrence D'Oliveiro
22 Mar 24 i    `- Re: Why Python When There Is Perl?1rbowman
20 Mar 24 `* Re: Why Python When There Is Perl?297Physfitfreak
20 Mar 24  +- Re: Why Python When There Is Perl?1rbowman
20 Mar 24  +* Re: Why Python When There Is Perl?4DFS
20 Mar 24  i`* Re: Why Python When There Is Perl?3rbowman
21 Mar 24  i `* Re: Why Python When There Is Perl?2DFS
21 Mar 24  i  `- Re: Why Python When There Is Perl?1rbowman
20 Mar 24  +* Re: Why Python When There Is Perl?202Nuxxie
21 Mar 24  i+* Re: Why Python When There Is Perl?194Physfitfreak
21 Mar 24  ii+- Re: Why Python When There Is Perl?1DFS
21 Mar 24  ii+- Re: Why Python When There Is Perl?1DFS
21 Mar 24  ii`* Re: Why Python When There Is Perl?191Farley Flud
21 Mar 24  ii +- Re: Why Python When There Is Perl?1Lawrence D'Oliveiro
22 Mar 24  ii +* Re: Why Python When There Is Perl?187Physfitfreak
22 Mar 24  ii i+* Re: Why Python When There Is Perl?2Lawrence D'Oliveiro
22 Mar 24  ii ii`- Re: Why Python When There Is Perl?1Physfitfreak
22 Mar 24  ii i+* Re: Why Python When There Is Perl?12Farley Flud
22 Mar 24  ii ii`* Re: Why Python When There Is Perl?11DFS
22 Mar 24  ii ii +* Re: Why Python When There Is Perl?7rbowman
22 Mar 24  ii ii i`* Re: Why Python When There Is Perl?6DFS
23 Mar 24  ii ii i `* Re: Why Python When There Is Perl?5rbowman
23 Mar 24  ii ii i  `* Re: Why Python When There Is Perl?4Lawrence D'Oliveiro
23 Mar 24  ii ii i   `* Re: Why Python When There Is Perl?3rbowman
23 Mar 24  ii ii i    `* Re: Why Python When There Is Perl?2Lawrence D'Oliveiro
23 Mar 24  ii ii i     `- Re: Why Python When There Is Perl?1rbowman
24 Mar 24  ii ii `* Re: Why Python When There Is Perl?3Stéphane CARPENTIER
24 Mar 24  ii ii  `* Re: Why Python When There Is Perl?2DFS
24 Mar 24  ii ii   `- Re: Why Python When There Is Perl?1rbowman
22 Mar 24  ii i+* Re: Why Python When There Is Perl?169Farley Flud
23 Mar 24  ii ii`* Re: Why Python When There Is Perl?168Physfitfreak
23 Mar 24  ii ii +* Re: Why Python When There Is Perl?6Physfitfreak
23 Mar 24  ii ii i`* Re: Why Python When There Is Perl?5Physfitfreak
24 Mar 24  ii ii i `* Re: Why Python When There Is Perl?4Lawrence D'Oliveiro
24 Mar 24  ii ii i  `* Re: Why Python When There Is Perl?3Physfitfreak
24 Mar 24  ii ii i   +- Re: Why Python When There Is Perl?1Physfitfreak
24 Mar 24  ii ii i   `- Re: Why Python When There Is Perl?1rbowman
23 Mar 24  ii ii +* Re: Why Python When There Is Perl?160Farley Flud
23 Mar 24  ii ii i+- Re: Why Python When There Is Perl?1DFS
23 Mar 24  ii ii i+* Re: Why Python When There Is Perl?113rbowman
24 Mar 24  ii ii ii`* Re: Why Python When There Is Perl?112Physfitfreak
24 Mar 24  ii ii ii `* Re: Why Python When There Is Perl?111rbowman
25 Mar 24  ii ii ii  `* Re: Why Python When There Is Perl?110Physfitfreak
25 Mar 24  ii ii ii   +* Re: Why Python When There Is Perl?102rbowman
25 Mar 24  ii ii ii   i+- I control Afghanistan; they do what I say, when I say.1Relf
26 Mar 24  ii ii ii   i`* Re: Why Python When There Is Perl?100Physfitfreak
26 Mar 24  ii ii ii   i +* Re: Why Python When There Is Perl?98Physfitfreak
26 Mar 24  ii ii ii   i i+* Re: Why Python When There Is Perl?9Physfitfreak
2 Apr 24  ii ii ii   i ii`* Re: Why Python When There Is Perl?8Lawrence D'Oliveiro
2 Apr 24  ii ii ii   i ii `* Re: Why Python When There Is Perl?7rbowman
2 Apr 24  ii ii ii   i ii  +* Re: Why Python When There Is Perl?4Physfitfreak
3 Apr 24  ii ii ii   i ii  i`* Re: Why Python When There Is Perl?3rbowman
3 Apr 24  ii ii ii   i ii  i `* Re: Why Python When There Is Perl?2Physfitfreak
3 Apr 24  ii ii ii   i ii  i  `- Re: Why Python When There Is Perl?1Physfitfreak
6 Apr 24  ii ii ii   i ii  `* Re: Why Python When There Is Perl?2Stéphane CARPENTIER
6 Apr 24  ii ii ii   i ii   `- Re: Why Python When There Is Perl?1rbowman
26 Mar 24  ii ii ii   i i`* Re: Why Python When There Is Perl?88rbowman
26 Mar 24  ii ii ii   i i +* Re: Why Python When There Is Perl?2Chris Ahlstrom
26 Mar 24  ii ii ii   i i i`- Re: Why Python When There Is Perl?1rbowman
26 Mar 24  ii ii ii   i i +- Re: Why Python When There Is Perl?1rbowman
26 Mar 24  ii ii ii   i i +* Re: Why Python When There Is Perl?83Physfitfreak
26 Mar 24  ii ii ii   i i i+* Re: Why Python When There Is Perl?3rbowman
27 Mar 24  ii ii ii   i i ii+- Re: Why Python When There Is Perl?1Physfitfreak
27 Mar 24  ii ii ii   i i ii`- Re: Why Python When There Is Perl?1Chris Ahlstrom
2 Apr 24  ii ii ii   i i i`* Re: Why Python When There Is Perl?79Lawrence D'Oliveiro
2 Apr 24  ii ii ii   i i i +- Re: Why Python When There Is Perl?1Chris Ahlstrom
2 Apr 24  ii ii ii   i i i `* Re: Why Python When There Is Perl?77Physfitfreak
3 Apr 24  ii ii ii   i i i  `* Re: Why Python When There Is Perl?76Lawrence D'Oliveiro
3 Apr 24  ii ii ii   i i i   `* Re: Why Python When There Is Perl?75Physfitfreak
5 Apr 24  ii ii ii   i i i    `* Re: Why Python When There Is Perl?74Lawrence D'Oliveiro
5 Apr 24  ii ii ii   i i i     +* Re: Why Python When There Is Perl?72rbowman
5 Apr 24  ii ii ii   i i i     i+* Re: Why Python When There Is Perl?61Lawrence D'Oliveiro
5 Apr 24  ii ii ii   i i i     ii`* Re: Why Python When There Is Perl?60rbowman
5 Apr 24  ii ii ii   i i i     ii +* Re: Why Python When There Is Perl?3Lawrence D'Oliveiro
5 Apr 24  ii ii ii   i i i     ii i+- Re: Why Python When There Is Perl?1Physfitfreak
6 Apr 24  ii ii ii   i i i     ii i`- Re: Why Python When There Is Perl?1RonB
5 Apr 24  ii ii ii   i i i     ii `* Re: Why Python When There Is Perl?56Chris Ahlstrom
6 Apr 24  ii ii ii   i i i     ii  `* Re: Why Python When There Is Perl?55RonB
7 Apr 24  ii ii ii   i i i     ii   `* Re: Why Python When There Is Perl?54Chris Ahlstrom
7 Apr 24  ii ii ii   i i i     ii    +* Re: Why Python When There Is Perl?10Chris Ahlstrom
7 Apr 24  ii ii ii   i i i     ii    i+* Re: Why Python When There Is Perl?2rbowman
8 Apr 24  ii ii ii   i i i     ii    ii`- Re: Why Python When There Is Perl?1RonB
7 Apr 24  ii ii ii   i i i     ii    i+- Re: Why Python When There Is Perl?1Physfitfreak
8 Apr 24  ii ii ii   i i i     ii    i`* Re: Why Python When There Is Perl?6RonB
8 Apr 24  ii ii ii   i i i     ii    i `* Re: Why Python When There Is Perl?5Chris Ahlstrom
10 Apr 24  ii ii ii   i i i     ii    i  `* Re: Why Python When There Is Perl?4RonB
10 Apr 24  ii ii ii   i i i     ii    i   `* Re: Why Python When There Is Perl?3Chris Ahlstrom
10 Apr 24  ii ii ii   i i i     ii    i    +- Re: Why Python When There Is Perl?1Physfitfreak
11 Apr 24  ii ii ii   i i i     ii    i    `- Re: Why Python When There Is Perl?1RonB
7 Apr 24  ii ii ii   i i i     ii    +* Re: Why Python When There Is Perl?30rbowman
8 Apr 24  ii ii ii   i i i     ii    i+* Re: Why Python When There Is Perl?5RonB
8 Apr 24  ii ii ii   i i i     ii    ii`* Re: Why Python When There Is Perl?4Chris Ahlstrom
8 Apr 24  ii ii ii   i i i     ii    i`* Re: Why Python When There Is Perl?24Chris Ahlstrom
7 Apr 24  ii ii ii   i i i     ii    +- Re: Why Python When There Is Perl?1Physfitfreak
8 Apr 24  ii ii ii   i i i     ii    +* Re: Why Python When There Is Perl?9RonB
8 Apr 24  ii ii ii   i i i     ii    +- Re: Why Python When There Is Perl?1RonB
9 Apr 24  ii ii ii   i i i     ii    `* Re: Why Python When There Is Perl?2Lawrence D'Oliveiro
5 Apr 24  ii ii ii   i i i     i+- Re: Why Python When There Is Perl?1Chris Ahlstrom
6 Apr 24  ii ii ii   i i i     i`* Re: Why Python When There Is Perl?9RonB
5 Apr 24  ii ii ii   i i i     `- Re: Why Python When There Is Perl?1Physfitfreak
2 Apr 24  ii ii ii   i i `- Re: Why Python When There Is Perl?1Lawrence D'Oliveiro
26 Mar 24  ii ii ii   i `- Re: Why Python When There Is Perl?1rbowman
2 Apr 24  ii ii ii   +* Re: Why Python When There Is Perl?6Lawrence D'Oliveiro
2 Apr 24  ii ii ii   `- Re: Why Python When There Is Perl?1Lawrence D'Oliveiro
24 Mar 24  ii ii i`* Re: Why Python When There Is Perl?45Physfitfreak
23 Mar 24  ii ii `- Re: Why Python When There Is Perl?1DFS
23 Mar 24  ii i`* Re: Why Python When There Is Perl?3DFS
24 Mar 24  ii `* Re: Why Python When There Is Perl?2Stéphane CARPENTIER
21 Mar 24  i+- Re: Why Python When There Is Perl?1Lawrence D'Oliveiro
21 Mar 24  i+* Re: Why Python When There Is Perl?5DFS
21 Mar 24  i`- Re: Why Python When There Is Perl?1rbowman
20 Mar 24  +* Re: Why Python When There Is Perl?56Yaxley Peaks
21 Mar 24  +* Re: Why Python When There Is Perl?32DFS
22 Mar 24  `- Re: Why Python When There Is Perl?1DFS

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal