问:
PyCharm SQLAlchemy autocomplete
I started evaluating PyCharm 3 professional edition because I will be working on several Pyramid + SQLAlchemy projects.
One of the things I would really love to have is SQLAlchemy autocomplete.

I created a new starter project with the alchemy scaffold following  these instructions. I also installed the SQLAlchemy package for the interpreter and virtual environment I am using for this project.

Also, when I created a new pycharm project for this code, the IDE suggested me to install the pyramid, sqlalchemy and other packages. Of course I accepted the suggestion and let the IDE install all of those packages.

In the models.py file, the DBSession is declared as follows:

```<code>DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
</code>```

In the views.py file, the DBSession is used this way:

```<code>one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()
</code>```

So I started playing with the IDE and did something like this: typed DBSession. and the IDE just gave me some few suggestions, within which the 'query' function was not listed. Then I tried typing: DBSession.query(MyModel). and pressed Ctrl+Space to try to get suggestions and a 'No suggestions' message showed up.

I would really like to have the SQLAlchemy suggestions of functions that I could use on my DBSession variable (like filter, filter_by, first, etc). I would say that this is mandatory for me :)

Is there something I am missing? Or, PyCharm doesn't support this?

答:
I use a type declaration after a variable assignment:

from sqlalchemy import create_engine
from sqlalchemy.engine import Engine

...

engine = create_engine(connect_str, max_overflow=10)
engine: Engine
```

<p>As a use for variables in for loop, I used:        </p>

```for table, meta in tables.items():
    meta: Table
    pass
```

<p>in which, <code>tables</code> is <code>sqlalchemy.orm.mapper.Mapper</code>, and <code>table</code> is an imported type: </p>

```from sqlalchemy import create_engine, Table
```

Title: PyCharm SQLAlchemy autocomplete

Body:
I started evaluating PyCharm 3 professional edition because I will be working on several Pyramid + SQLAlchemy projects.
One of the things I would really love to have is SQLAlchemy autocomplete.

I created a new starter project with the alchemy scaffold following  these instructions. I also installed the SQLAlchemy package for the interpreter and virtual environment I am using for this project.

Also, when I created a new pycharm project for this code, the IDE suggested me to install the pyramid, sqlalchemy and other packages. Of course I accepted the suggestion and let the IDE install all of those packages.

In the models.py file, the DBSession is declared as follows:

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

In the views.py file, the DBSession is used this way:

one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()

So I started playing with the IDE and did something like this: typed DBSession. and the IDE just gave me some few suggestions, within which the 'query' function was not listed. Then I tried typing: DBSession.query(MyModel). and pressed Ctrl+Space to try to get suggestions and a 'No suggestions' message showed up.

I would really like to have the SQLAlchemy suggestions of functions that I could use on my DBSession variable (like filter, filter_by, first, etc). I would say that this is mandatory for me :)

Is there something I am missing? Or, PyCharm doesn't support this?

Body:
I use a type declaration after a variable assignment:

from sqlalchemy import create_engine
from sqlalchemy.engine import Engine

...

engine = create_engine(connect_str, max_overflow=10)
engine: Engine

As a use for variables in for loop, I used:

for table, meta in tables.items():
    meta: Table
    pass

in which, tables is sqlalchemy.orm.mapper.Mapper, and table is an imported type:

from sqlalchemy import create_engine, Table


来源: stackexchange

问:
Move all files in subdirectories contained in one parent directory to N sub-subdirectories
So I have a directory with 44 subdirectories. Each of these subdirectories contain 15,000 files.

What I would like to do is: for each subdirectory, create a sub-subdirectory with max N files, i.e. 5,000.

From: Parent -> Subdirectory w/ 15,000 files

To: Parent -> Subdirectory -> N Sub-sub directories (with names starting at 1), each with max. N files.

Any advice on how to approach this is greatly appreciated!

答:
Well, after a lot of tinkering around with the unfamiliar world of PS, I did manage to code a solution myself:

```<code>$filesperfolder = 3000
$sourcePath = "E:\parent\subdirectory"

$j = 0;
$folderNum = 1;

$range = 44

foreach($i in 1..$range) {
    $path = $sourcePath + "\" + $i
    Get-ChildItem "$path\*" -File | % {
        New-Item -Path ($path + "\" + $folderNum) -Type Directory -Force
        Move-Item $_ ($path + "\" + $folderNum);
        $j++;
        if ($j -eq $filesperfolder) {
            $folderNum++;
            $j = 0;
        }
    }
    $folderNum = 1;
}
</code>```

Which works really well for my situation! Each subdirectory I have is 15-17 GB large, and each took 1-1.5 minutes to process.

ContentLicense: CC BY-SA 4.0

Body:
Well, after a lot of tinkering around with the unfamiliar world of PS, I did manage to code a solution myself:

$filesperfolder = 3000
$sourcePath = "E:\parent\subdirectory"

$j = 0;
$folderNum = 1;

$range = 44

foreach($i in 1..$range) {
    $path = $sourcePath + "\" + $i
    Get-ChildItem "$path\*" -File | % {
        New-Item -Path ($path + "\" + $folderNum) -Type Directory -Force
        Move-Item $_ ($path + "\" + $folderNum);
        $j++;
        if ($j -eq $filesperfolder) {
            $folderNum++;
            $j = 0;
        }
    }
    $folderNum = 1;
}

Which works really well for my situation! Each subdirectory I have is 15-17 GB large, and each took 1-1.5 minutes to process.


问:
socket.io client Angular Cannot read property 'emit' of undefined
I have this component in Angular

```<code>import { Component, OnInit} from "@angular/core";
import * as io from "socket.io-client";
import { environment } from "src/environments/environment";

@Component({
  selector: "app-home",
  templateUrl: "./home.component.html",
  styleUrls: ["./home.component.css"]
})
export class HomeComponent implements OnInit {
  busy: boolean;
  username: string;
  socket: any;
  user = JSON.parse(localStorage.getItem("user"));
  token = localStorage.getItem("token");

  constructor() {
    // THIS WORKS
    this.socket = io.connect(environment.endpoint, {
      query: `username=${this.user.username}&token=${this.token}`
    });
  }

  ngOnInit() {
    this.socket.on("client-retrieve", function(data) { // <--- THIS WORKS
      console.log(data);
      this.socket.emit( // <---------- THIS FAILS, throws the error
        "request-response",
        {
          from: this.user.username,
          to: data.from,
          command: "SEND",
          payload: [{ 'something': "something" }]
        },
        (err, message) => {
          console.log(err);
          console.log(message);
        }
      );
    });
  }

  onSubmit(formData) {
    console.log("FormData: ", formData);
    this.socket.emit( // This works. Gets triggered by a form submit
      "request-forward",
      {
        command: "PULL",
        from: this.user.username,
        to: formData.username
      },
      (err, message) => {
        console.log(err);
        console.log(message);
      }
    );
  }
}
</code>```

The error thrown is basically:
core.js:5828 ERROR TypeError: Cannot read property 'emit' of undefined. at Socket.<anonymous> (home.component.ts:26)

What am I doing wrong? Because calling emit via the onSubmit(...) works.

答:
You'll want to change it to an arrow function so this will reference the lexical context and not the caller context.

On this line:

```<code>this.socket.on("client-retrieve", function(data) { // <--- THIS WORKS
</code>```

Change to this:

```<code> this.socket.on("client-retrieve", (data) => { // <--- THIS WORKS
</code>```

Your method should now look like:

```<code>  ngOnInit() {
    this.socket.on("client-retrieve", (data) => { // <--- THIS WORKS
      console.log(data);
      this.socket.emit( // <---------- THIS FAILS, throws the error
        "request-response",
        {
          from: this.user.username,
          to: data.from,
          command: "SEND",
          payload: [{ 'something': "something" }]
        },
        (err, message) => {
          console.log(err);
          console.log(message);
        }
      );
    });
  }
</code>```

https://www.typescriptlang.org/docs/handbook/functions.html#this-and-arrow-functions

Body:
I have this component in Angular

import { Component, OnInit} from "@angular/core";
import * as io from "socket.io-client";
import { environment } from "src/environments/environment";

@Component({
  selector: "app-home",
  templateUrl: "./home.component.html",
  styleUrls: ["./home.component.css"]
})
export class HomeComponent implements OnInit {
  busy: boolean;
  username: string;
  socket: any;
  user = JSON.parse(localStorage.getItem("user"));
  token = localStorage.getItem("token");

  constructor() {
    // THIS WORKS
    this.socket = io.connect(environment.endpoint, {
      query: `username=${this.user.username}&token=${this.token}`
    });
  }

  ngOnInit() {
    this.socket.on("client-retrieve", function(data) { // <--- THIS WORKS
      console.log(data);
      this.socket.emit( // <---------- THIS FAILS, throws the error
        "request-response",
        {
          from: this.user.username,
          to: data.from,
          command: "SEND",
          payload: [{ 'something': "something" }]
        },
        (err, message) => {
          console.log(err);
          console.log(message);
        }
      );
    });
  }

  onSubmit(formData) {
    console.log("FormData: ", formData);
    this.socket.emit( // This works. Gets triggered by a form submit
      "request-forward",
      {
        command: "PULL",
        from: this.user.username,
        to: formData.username
      },
      (err, message) => {
        console.log(err);
        console.log(message);
      }
    );
  }
}

The error thrown is basically:
core.js:5828 ERROR TypeError: Cannot read property 'emit' of undefined. at Socket.<anonymous> (home.component.ts:26)

What am I doing wrong? Because calling emit via the onSubmit(...) works.

Body:
You'll want to change it to an arrow function so this will reference the lexical context and not the caller context.

On this line:

this.socket.on("client-retrieve", function(data) { // <--- THIS WORKS

Change to this:

 this.socket.on("client-retrieve", (data) => { // <--- THIS WORKS

Your method should now look like:

  ngOnInit() {
    this.socket.on("client-retrieve", (data) => { // <--- THIS WORKS
      console.log(data);
      this.socket.emit( // <---------- THIS FAILS, throws the error
        "request-response",
        {
          from: this.user.username,
          to: data.from,
          command: "SEND",
          payload: [{ 'something': "something" }]
        },
        (err, message) => {
          console.log(err);
          console.log(message);
        }
      );
    });
  }

https://www.typescriptlang.org/docs/handbook/functions.html#this-and-arrow-functions


问:
Google sheet create pdf from another sheet ID
Hi how could this code be modified to create a PDF file from another worksheet instead of the active open worksheet.
Here's part of the code I'm using and it works but I would like to create a PDF from another worksheet ID

Thanks

```<code>   var source = SpreadsheetApp.getActiveSpreadsheet();
   var spreadsheet = spreadsheetId ? SpreadsheetApp.openById(spreadsheetId) : SpreadsheetApp.getActiveSpreadsheet();
   var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
   var ticketnum = source.getSheetByName("Print Page").getRange('X1').getValue();
   var body = 'Attached is your copy'
   var Client = source.getSheetByName("Print Page").getRange('O5').getValue();
   var Cons = source.getSheetByName("Print Page").getRange('T4').getValue() ;
   var jobN = source.getSheetByName("Ticket PDF").getRange('G9').getValue() ;
   var mailTo = source.getSheetByName("Print Page").getRange('Z1').getValue() ;
   var folder = DriveApp.getFoldersByName('Ticket').next();
   var token = ScriptApp.getOAuthToken();
   var ssID = spreadsheetId;
   var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
                                                    "?format=pdf&"+
                                                     '&portrait=true' +
                                                     '&fzr=true' +
                                                     '&fitw=True' +
                                                     //'&scale=4' +
                                                     '&top_margin=0.25' +
                                                     '&bottom_margin=0.25' +
                                                     '&left_margin=0.25' +
                                                     '&right_margin=0.25' +
                                                     '&horizontal_alignment=CENTER' +
                                                     '&gridlines=false'+
                                                     '&sheetnames=False';

     var response = UrlFetchApp.fetch(url, {
    headers: {
        'Authorization': 'Bearer ' + token
      }
    });

    var pdf = response.getBlob().setName( Client + "  " + ticketnum + " @ " + jobN + "  "  + '.PDF');
    folder.createFile(pdf)
</code>```

答:
How about this modification?

In this modification, it supposes that another worksheet ID in your question is the other sheet in the active Spreadsheet.

Modified script:

In order to export the specific sheet in the Spreadsheet as PDF file, you can use the query parameter of gid as follows.

From:

```<code>"?format=pdf&"+
</code>```

To:

```<code>"?format=pdf&gid=" + spreadsheet.getSheetByName("###").getSheetId() + "&" +
</code>```

In this case, please set the sheet name you want to export as a PDF to ###.

Note:

If you want to export several sheets in the Spreadsheet as a PDF file, this thread might be useful.

If I misunderstood your question and this was not the direction you want, I apologize.

Added

You want to export the specific Spreadsheet, which is not the active Spreadsheet, as a PDF file.

In your script, please give spreadsheetId. When spreadsheetId is not given the active Spreadsheet by var spreadsheet = spreadsheetId ? SpreadsheetApp.openById(spreadsheetId) : SpreadsheetApp.getActiveSpreadsheet();. And spreadsheetId is retrieved from the active Spreadsheet. So please modify as follows.

From:

```<code>var source = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheet = spreadsheetId ? SpreadsheetApp.openById(spreadsheetId) : SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
</code>```

To:

```<code>var spreadsheetId = "###"; // Please set the Spreadsheet ID.

var source = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheet = SpreadsheetApp.openById(spreadsheetId);
var spreadsheetId = spreadsheet.getId();
</code>```

If you want to export the specific sheet in the Spreadsheet, please use the query parameter gid. And if you want to export several sheets in the Spreadsheet, this thread might be useful.

Body:
Hi how could this code be modified to create a PDF file from another worksheet instead of the active open worksheet.
Here's part of the code I'm using and it works but I would like to create a PDF from another worksheet ID

Thanks

   var source = SpreadsheetApp.getActiveSpreadsheet();
   var spreadsheet = spreadsheetId ? SpreadsheetApp.openById(spreadsheetId) : SpreadsheetApp.getActiveSpreadsheet();
   var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
   var ticketnum = source.getSheetByName("Print Page").getRange('X1').getValue();
   var body = 'Attached is your copy'
   var Client = source.getSheetByName("Print Page").getRange('O5').getValue();
   var Cons = source.getSheetByName("Print Page").getRange('T4').getValue() ;
   var jobN = source.getSheetByName("Ticket PDF").getRange('G9').getValue() ;
   var mailTo = source.getSheetByName("Print Page").getRange('Z1').getValue() ;
   var folder = DriveApp.getFoldersByName('Ticket').next();
   var token = ScriptApp.getOAuthToken();
   var ssID = spreadsheetId;
   var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
                                                    "?format=pdf&"+
                                                     '&portrait=true' +
                                                     '&fzr=true' +
                                                     '&fitw=True' +
                                                     //'&scale=4' +
                                                     '&top_margin=0.25' +
                                                     '&bottom_margin=0.25' +
                                                     '&left_margin=0.25' +
                                                     '&right_margin=0.25' +
                                                     '&horizontal_alignment=CENTER' +
                                                     '&gridlines=false'+
                                                     '&sheetnames=False';

     var response = UrlFetchApp.fetch(url, {
    headers: {
        'Authorization': 'Bearer ' + token
      }
    });

    var pdf = response.getBlob().setName( Client + "  " + ticketnum + " @ " + jobN + "  "  + '.PDF');
    folder.createFile(pdf)

Body:
How about this modification?

In this modification, it supposes that another worksheet ID in your question is the other sheet in the active Spreadsheet.

Modified script:

In order to export the specific sheet in the Spreadsheet as PDF file, you can use the query parameter of gid as follows.

From:

"?format=pdf&"+

To:

"?format=pdf&gid=" + spreadsheet.getSheetByName("###").getSheetId() + "&" +

In this case, please set the sheet name you want to export as a PDF to ###.

Note:

If you want to export several sheets in the Spreadsheet as a PDF file, this thread might be useful.

If I misunderstood your question and this was not the direction you want, I apologize.

Added

You want to export the specific Spreadsheet, which is not the active Spreadsheet, as a PDF file.

In your script, please give spreadsheetId. When spreadsheetId is not given the active Spreadsheet by var spreadsheet = spreadsheetId ? SpreadsheetApp.openById(spreadsheetId) : SpreadsheetApp.getActiveSpreadsheet();. And spreadsheetId is retrieved from the active Spreadsheet. So please modify as follows.

From:

var source = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheet = spreadsheetId ? SpreadsheetApp.openById(spreadsheetId) : SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();

To:

var spreadsheetId = "###"; // Please set the Spreadsheet ID.

var source = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheet = SpreadsheetApp.openById(spreadsheetId);
var spreadsheetId = spreadsheet.getId();

If you want to export the specific sheet in the Spreadsheet, please use the query parameter gid. And if you want to export several sheets in the Spreadsheet, this thread might be useful.


问:
When do I use if/endif versus If{}?
Well the question is self explanatory.

In PHP when do I use the if/endif notation instead of the standard if(something){} notation?

Example:

```<code><?php if($a == 5): ?>
A is equal to 5
<?php endif; ?>
</code>```

Versus:

```<code><?php if($a == 5){ ?>
A is equal to 5
<?php } ?>
</code>```

答:
Others have given the answer "for templating", but haven't really explained why.  Curly braces are great for denoting blocks, but they kind of rely on indentation to be read clearly.  So this is fairly clear:

```<code><?php
if (1 == 2)
{
    while (1 < 2)
    {
        doSomething();
    }
}
</code>```

It's obvious which brace matches which.

If, however, you're going into an HTML block, you're probably going to stop indenting cleanly.  For instance:

```<code><?php
if (1 != 2) { ?>
<div>This always happens! <?php while (true) { ?>
  <p>This is going to cause an infinite loop!</p>
<?php }
}
</code>```

That's hard to follow.  If you use the endif style, it looks a little cleaner:

```<code><?php
if (1 != 2): ?>
<div>This always happens! <?php while (true): ?>
  <p>This is going to cause an infinite loop!</p>
<?php endwhile;
endif;
</code>```

The content of the code shows more clearly what's being done.  For this limited case, it's more legible.

ContentLicense: CC BY-SA 3.0

Title: When do I use if/endif versus If{}?

Body:
Well the question is self explanatory.

In PHP when do I use the if/endif notation instead of the standard if(something){} notation?

Example:

<?php if($a == 5): ?>
A is equal to 5
<?php endif; ?>

Versus:

<?php if($a == 5){ ?>
A is equal to 5
<?php } ?>

Body:
Others have given the answer "for templating", but haven't really explained why.  Curly braces are great for denoting blocks, but they kind of rely on indentation to be read clearly.  So this is fairly clear:

<?php
if (1 == 2)
{
    while (1 < 2)
    {
        doSomething();
    }
}

It's obvious which brace matches which.

If, however, you're going into an HTML block, you're probably going to stop indenting cleanly.  For instance:

<?php
if (1 != 2) { ?>
<div>This always happens! <?php while (true) { ?>
  <p>This is going to cause an infinite loop!</p>
<?php }
}

That's hard to follow.  If you use the endif style, it looks a little cleaner:

<?php
if (1 != 2): ?>
<div>This always happens! <?php while (true): ?>
  <p>This is going to cause an infinite loop!</p>
<?php endwhile;
endif;

The content of the code shows more clearly what's being done.  For this limited case, it's more legible.


问:
runtime/cgo: pthread_create failed: Resource temporarily unavailable
I currently have this function, that pipes multiple youtubedl commands thru ffmpeg, and then pipes the output of ffmpeg to an HTTP client.

```<code>func pipeThruFfmpegToMp4(vi *VideoInfo, rw web.ResponseWriter) error {
    var ffmpeg *exec.Cmd
    ffmpeg = exec.Command(
        "ffmpeg",
        "-i", "-",
        "-i", "pipe:3",
        "-c:v", "copy", "-c:a", "copy",
        "-preset", "veryfast",
        "-metadata", fmt.Sprintf(`title=%s`, vi.GetTitle()),
        "-movflags", "frag_keyframe+empty_moov",
        "-f", "mp4",
        "-")

    youtubevideo := exec.Command(YoutubeDLPath, "-c", "-f", fmt.Sprintf("%s/bestvideo[ext=mp4]/bestvideo/best", vi.GetFormat()), "--no-cache-dir", "--restrict-filenames", "--hls-prefer-native", "-o", "-", fmt.Sprintf("%s", vi.GetVideoUrl()))
    fmt.Println(youtubevideo)

    youtube := exec.Command(YoutubeDLPath, "-c", "-f", "bestaudio[ext=m4a]/bestaudio/best", "--no-cache-dir", "--restrict-filenames", "--hls-prefer-native", "-o", "-", fmt.Sprintf("%s", vi.GetVideoUrl()))
    fmt.Println(youtube)

    var ytvbuf, ytbuf, ffbuf bytes.Buffer
    youtubevideo.Stderr = &ytvbuf
    youtube.Stderr = &ytbuf
    ffmpeg.Stderr = &ffbuf

    video, err := youtubevideo.StdoutPipe()
    if err != nil {
        log.Printf("pipeThruFfmpegToMp4: %v\n", err)
        return err
    }

    pipe3, err := youtube.StdoutPipe()
    if err != nil {
        log.Printf("pipeThruFfmpegToMp4: %v\n", err)
        return err
    }

    ffmpeg.Stdin = video
    ffmpeg.ExtraFiles = []*os.File{pipe3.(*os.File)}
    ffmpeg.Stdout = rw

    // Headers sent, no turning back now
    rw.Header().Set("Content-Type", "video/mp4")
    rw.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=\"%s.mp4\"", vi.GetSlug()))
    rw.Flush()

    ffmpeg.Start()
    youtubevideo.Start()
    youtube.Start()
    ffmpeg.Wait()
    youtubevideo.Wait()
    youtube.Wait()

    // check ytvbuf, ytbuf, ffbuf for stderr errors

    if ffbuf.Len() != 0 {
        rollbar.Error(rollbar.ERR, err, &rollbar.Field{"stderr", ffbuf.String()})
        log.Printf("pipeThruFfmpegToMp4: %v\n", ffbuf.String())
    }
    if ytvbuf.Len() != 0 {
        rollbar.Error(rollbar.ERR, err, &rollbar.Field{"stderr", ytvbuf.String()})
        log.Printf("pipeThruYouTubevDLToMp4: %v\n", ytvbuf.String())
    }
    if ytbuf.Len() != 0 {
        rollbar.Error(rollbar.ERR, err, &rollbar.Field{"stderr", ytbuf.String()})
        log.Printf("pipeThruYouTubeDLToMp4: %v\n", ytbuf.String())
    }

    return nil
}
</code>```

The problem is that everything runs fine, but after a while the ram starts filling up on the server, till it gets to the point that it errors out with runtime/cgo: pthread_create failed: Resource temporarily unavailable

I'm unsure if it's a memory leak, or if either instance of youtube-dl is not closing right or if ffmpeg isn't closing right and just consuming more and more ram as the program runs more, until the program crashes with this error

```<code>runtime/cgo: pthread_create failed: Resource temporarily unavailable
SIGABRT: abort
PC=0x7f083501fe97 m=128 sigcode=18446744073709551610

goroutine 0 [idle]:
runtime: unknown pc 0x7f083501fe97
stack: frame={sp:0x7f05ff7fd7d0, fp:0x0} stack=[0x7f05feffe288,0x7f05ff7fde88)
00007f05ff7fd6d0:  00007f05ff7fd700  0000ffff00001fa0
00007f05ff7fd6e0:  00007f05ff7fdbe0  00007f05f8000da0
00007f05ff7fd6f0:  0000000000000000  000000000093032c
00007f05ff7fd700:  0000000000000000  00007f0835600ec3
00007f05ff7fd710:  0000000000000005  0000000000000000
00007f05ff7fd720:  000000c0000ce120  00007f0834ff1ce0
00007f05ff7fd730:  00007f05ff7fdaf0  00007f083560870a
00007f05ff7fd740:  0000000000000000  0000000000000000
00007f05ff7fd750:  0000000000000000  00007f05ff7fdbe0
00007f05ff7fd760:  2525252525252525  2525252525252525
00007f05ff7fd770:  000000ffffffffff  0000000000000000
00007f05ff7fd780:  000000ffffffffff  0000000000000000
00007f05ff7fd790:  000000c00010d1a0  000000c000953740
00007f05ff7fd7a0:  000000c0000ce120  000000c000cf2300
00007f05ff7fd7b0:  000000c00010d260  000000c001f4e180
00007f05ff7fd7c0:  000000c001f4e000  000000c00169f680
00007f05ff7fd7d0: <0000000000000000  000000c001f34180
00007f05ff7fd7e0:  6e75720000000000  6f67632f656d6974
00007f05ff7fd7f0:  0000000000000000  0000000000000000
00007f05ff7fd800:  000000c000cf2300  000000c00010d260
00007f05ff7fd810:  000000c001f4e180  000000c001f4e000
00007f05ff7fd820:  000000c00169f680  000000c00169f500
00007f05ff7fd830:  000000c001f34180  000000c001f34000
00007f05ff7fd840:  000000c000c92780  000000c001ec2600
00007f05ff7fd850:  fffffffe7fffffff  ffffffffffffffff
00007f05ff7fd860:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd870:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd880:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd890:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd8a0:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd8b0:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd8c0:  ffffffffffffffff  ffffffffffffffff
runtime: unknown pc 0x7f083501fe97
stack: frame={sp:0x7f05ff7fd7d0, fp:0x0} stack=[0x7f05feffe288,0x7f05ff7fde88)
00007f05ff7fd6d0:  00007f05ff7fd700  0000ffff00001fa0
00007f05ff7fd6e0:  00007f05ff7fdbe0  00007f05f8000da0
00007f05ff7fd6f0:  0000000000000000  000000000093032c
00007f05ff7fd700:  0000000000000000  00007f0835600ec3
00007f05ff7fd710:  0000000000000005  0000000000000000
</code>```

I've also tried building the binary with CGO_ENABLED=0 even though I don't even have a import "c" but that also ends up erroring out as well with

```<code>runtime: failed to create new OS thread (have 21 already; errno=11)
runtime: may need to increase max user processes (ulimit -u)
fatal error: newosproc
</code>```

my limit's are already very generouse, which maybe could also be a problem? Or do I maybe have to increase the pipe size?

```<code>$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 192907
max locked memory       (kbytes, -l) 16384
max memory size         (kbytes, -m) unlimited
open files                      (-n) 100000
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 65536
cpu time               (seconds, -t) unlimited
max user processes              (-u) 63883
virtual memory          (kbytes, -v) unlimited
</code>```

Either way, I apprecate any and all the help I can get on this.  Here is a link to the entire program and here is a thread in the golang-nuts forum regarding this questions

答:
So after some debugging and with the help of Kevin's answer.  It seems youtube-dl was not closing after it was successful.

Given that if ffmpeg finished, then it would not make any sense to have youtube-dl instance running, and since the Wait() function is waiting for youtube-dl to finish, but it never seems to send it's finish signal, I went ahead and had to add a kill to also kill the child sub processes.

So I had to add change this

```<code>    ffmpeg.Start()
    youtubevideo.Start()
    youtube.Start()
    ffmpeg.Wait()
    youtubevideo.Wait()
    youtube.Wait()
</code>```

To this

```<code>    youtube.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
    youtubevideo.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
    ffmpeg.Start()
    youtubevideo.Start()
    youtube.Start()
    ffmpeg.Wait()
    syscall.Kill(-youtube.Process.Pid, syscall.SIGKILL)
    syscall.Kill(-youtubevideo.Process.Pid, syscall.SIGKILL)
    youtubevideo.Wait()
    youtube.Wait()
</code>```

Please note, since it seems it makes some sort of child subprocess, youtube.Process.Kill() didn't actually kill the process, that's why I had to use syscall, and also Setpgid: true

Body:
I currently have this function, that pipes multiple youtubedl commands thru ffmpeg, and then pipes the output of ffmpeg to an HTTP client.

func pipeThruFfmpegToMp4(vi *VideoInfo, rw web.ResponseWriter) error {
    var ffmpeg *exec.Cmd
    ffmpeg = exec.Command(
        "ffmpeg",
        "-i", "-",
        "-i", "pipe:3",
        "-c:v", "copy", "-c:a", "copy",
        "-preset", "veryfast",
        "-metadata", fmt.Sprintf(`title=%s`, vi.GetTitle()),
        "-movflags", "frag_keyframe+empty_moov",
        "-f", "mp4",
        "-")

    youtubevideo := exec.Command(YoutubeDLPath, "-c", "-f", fmt.Sprintf("%s/bestvideo[ext=mp4]/bestvideo/best", vi.GetFormat()), "--no-cache-dir", "--restrict-filenames", "--hls-prefer-native", "-o", "-", fmt.Sprintf("%s", vi.GetVideoUrl()))
    fmt.Println(youtubevideo)

    youtube := exec.Command(YoutubeDLPath, "-c", "-f", "bestaudio[ext=m4a]/bestaudio/best", "--no-cache-dir", "--restrict-filenames", "--hls-prefer-native", "-o", "-", fmt.Sprintf("%s", vi.GetVideoUrl()))
    fmt.Println(youtube)

    var ytvbuf, ytbuf, ffbuf bytes.Buffer
    youtubevideo.Stderr = &ytvbuf
    youtube.Stderr = &ytbuf
    ffmpeg.Stderr = &ffbuf

    video, err := youtubevideo.StdoutPipe()
    if err != nil {
        log.Printf("pipeThruFfmpegToMp4: %v\n", err)
        return err
    }

    pipe3, err := youtube.StdoutPipe()
    if err != nil {
        log.Printf("pipeThruFfmpegToMp4: %v\n", err)
        return err
    }

    ffmpeg.Stdin = video
    ffmpeg.ExtraFiles = []*os.File{pipe3.(*os.File)}
    ffmpeg.Stdout = rw

    // Headers sent, no turning back now
    rw.Header().Set("Content-Type", "video/mp4")
    rw.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=\"%s.mp4\"", vi.GetSlug()))
    rw.Flush()

    ffmpeg.Start()
    youtubevideo.Start()
    youtube.Start()
    ffmpeg.Wait()
    youtubevideo.Wait()
    youtube.Wait()

    // check ytvbuf, ytbuf, ffbuf for stderr errors

    if ffbuf.Len() != 0 {
        rollbar.Error(rollbar.ERR, err, &rollbar.Field{"stderr", ffbuf.String()})
        log.Printf("pipeThruFfmpegToMp4: %v\n", ffbuf.String())
    }
    if ytvbuf.Len() != 0 {
        rollbar.Error(rollbar.ERR, err, &rollbar.Field{"stderr", ytvbuf.String()})
        log.Printf("pipeThruYouTubevDLToMp4: %v\n", ytvbuf.String())
    }
    if ytbuf.Len() != 0 {
        rollbar.Error(rollbar.ERR, err, &rollbar.Field{"stderr", ytbuf.String()})
        log.Printf("pipeThruYouTubeDLToMp4: %v\n", ytbuf.String())
    }

    return nil
}

The problem is that everything runs fine, but after a while the ram starts filling up on the server, till it gets to the point that it errors out with runtime/cgo: pthread_create failed: Resource temporarily unavailable

I'm unsure if it's a memory leak, or if either instance of youtube-dl is not closing right or if ffmpeg isn't closing right and just consuming more and more ram as the program runs more, until the program crashes with this error

runtime/cgo: pthread_create failed: Resource temporarily unavailable
SIGABRT: abort
PC=0x7f083501fe97 m=128 sigcode=18446744073709551610

goroutine 0 [idle]:
runtime: unknown pc 0x7f083501fe97
stack: frame={sp:0x7f05ff7fd7d0, fp:0x0} stack=[0x7f05feffe288,0x7f05ff7fde88)
00007f05ff7fd6d0:  00007f05ff7fd700  0000ffff00001fa0
00007f05ff7fd6e0:  00007f05ff7fdbe0  00007f05f8000da0
00007f05ff7fd6f0:  0000000000000000  000000000093032c
00007f05ff7fd700:  0000000000000000  00007f0835600ec3
00007f05ff7fd710:  0000000000000005  0000000000000000
00007f05ff7fd720:  000000c0000ce120  00007f0834ff1ce0
00007f05ff7fd730:  00007f05ff7fdaf0  00007f083560870a
00007f05ff7fd740:  0000000000000000  0000000000000000
00007f05ff7fd750:  0000000000000000  00007f05ff7fdbe0
00007f05ff7fd760:  2525252525252525  2525252525252525
00007f05ff7fd770:  000000ffffffffff  0000000000000000
00007f05ff7fd780:  000000ffffffffff  0000000000000000
00007f05ff7fd790:  000000c00010d1a0  000000c000953740
00007f05ff7fd7a0:  000000c0000ce120  000000c000cf2300
00007f05ff7fd7b0:  000000c00010d260  000000c001f4e180
00007f05ff7fd7c0:  000000c001f4e000  000000c00169f680
00007f05ff7fd7d0: <0000000000000000  000000c001f34180
00007f05ff7fd7e0:  6e75720000000000  6f67632f656d6974
00007f05ff7fd7f0:  0000000000000000  0000000000000000
00007f05ff7fd800:  000000c000cf2300  000000c00010d260
00007f05ff7fd810:  000000c001f4e180  000000c001f4e000
00007f05ff7fd820:  000000c00169f680  000000c00169f500
00007f05ff7fd830:  000000c001f34180  000000c001f34000
00007f05ff7fd840:  000000c000c92780  000000c001ec2600
00007f05ff7fd850:  fffffffe7fffffff  ffffffffffffffff
00007f05ff7fd860:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd870:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd880:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd890:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd8a0:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd8b0:  ffffffffffffffff  ffffffffffffffff
00007f05ff7fd8c0:  ffffffffffffffff  ffffffffffffffff
runtime: unknown pc 0x7f083501fe97
stack: frame={sp:0x7f05ff7fd7d0, fp:0x0} stack=[0x7f05feffe288,0x7f05ff7fde88)
00007f05ff7fd6d0:  00007f05ff7fd700  0000ffff00001fa0
00007f05ff7fd6e0:  00007f05ff7fdbe0  00007f05f8000da0
00007f05ff7fd6f0:  0000000000000000  000000000093032c
00007f05ff7fd700:  0000000000000000  00007f0835600ec3
00007f05ff7fd710:  0000000000000005  0000000000000000

I've also tried building the binary with CGO_ENABLED=0 even though I don't even have a import "c" but that also ends up erroring out as well with

runtime: failed to create new OS thread (have 21 already; errno=11)
runtime: may need to increase max user processes (ulimit -u)
fatal error: newosproc

my limit's are already very generouse, which maybe could also be a problem? Or do I maybe have to increase the pipe size?

$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 192907
max locked memory       (kbytes, -l) 16384
max memory size         (kbytes, -m) unlimited
open files                      (-n) 100000
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 65536
cpu time               (seconds, -t) unlimited
max user processes              (-u) 63883
virtual memory          (kbytes, -v) unlimited

Either way, I apprecate any and all the help I can get on this.  Here is a link to the entire program and here is a thread in the golang-nuts forum regarding this questions

Body:
So after some debugging and with the help of Kevin's answer.  It seems youtube-dl was not closing after it was successful.

Given that if ffmpeg finished, then it would not make any sense to have youtube-dl instance running, and since the Wait() function is waiting for youtube-dl to finish, but it never seems to send it's finish signal, I went ahead and had to add a kill to also kill the child sub processes.

So I had to add change this

    ffmpeg.Start()
    youtubevideo.Start()
    youtube.Start()
    ffmpeg.Wait()
    youtubevideo.Wait()
    youtube.Wait()

To this

    youtube.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
    youtubevideo.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
    ffmpeg.Start()
    youtubevideo.Start()
    youtube.Start()
    ffmpeg.Wait()
    syscall.Kill(-youtube.Process.Pid, syscall.SIGKILL)
    syscall.Kill(-youtubevideo.Process.Pid, syscall.SIGKILL)
    youtubevideo.Wait()
    youtube.Wait()

Please note, since it seems it makes some sort of child subprocess, youtube.Process.Kill() didn't actually kill the process, that's why I had to use syscall, and also Setpgid: true


问:
dropna remove all rows with valid values and only NA rows are left in pandas
I am trying to clean np values in an open sourced data.

I am using python3, Jupyter and pandas.

```<code> response = urllib.request.urlopen('https://resources.lendingclub.com/LoanStats3c.csv.zip')
 import shutil
 url = 'https://resources.lendingclub.com/LoanStats3c.csv.zip'
 file_name = 'LoanStats3c.csv.zip'

 with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
     shutil.copyfileobj(response, out_file)
     with zipfile.ZipFile(file_name) as zf:
         zf.extractall()

 loan=pd.read_csv(open('LoanStats3c.csv'), skiprows=1, parse_dates=True, index_col='id')
 loan.describe()

 # remove all columns with all NAs
 loan = loan.dropna(axis=1, how = 'all')
 loan.describe()

 # remove all rows with any NAs
 loan = loan.dropna(axis = 0)

 loan.describe()
</code>```

But, the results are all columns with all NAs:

```<code>  loan_amnt  funded_amnt  funded_amnt_inv  installment  annual_inc  dti  \
  count        0.0          0.0              0.0          0.0         0.0  0.0
  mean         NaN          NaN              NaN          NaN         NaN  NaN
  std          NaN          NaN              NaN          NaN         NaN  NaN
  min          NaN          NaN              NaN          NaN         NaN  NaN
  25%          NaN          NaN              NaN          NaN         NaN  NaN
  50%          NaN          NaN              NaN          NaN         NaN  NaN
  75%          NaN          NaN              NaN          NaN         NaN  NaN
  max          NaN          NaN              NaN          NaN         NaN  NaN
</code>```

Why all rows with valid values are gone and only the NA columns are left ?

thanks

答:
When you'r using .dropna() like that all ocurrences with NaN values are deleted from dataframe

```<code>loan.dropna(axis=1, how = 'all')
</code>```

Will delete the columns with all values in NaN

```<code>loan.dropna(axis = 0)
</code>```

Will delete the rows with a least one value in NaN

I saw the file and i'm pretty sure that every rows has at least one column in NaN

Finally when using .describe() the dataframe is empty and the values that are shown are a descriptive statistics of that, if you want to see the real DF use print(df) or in jupyter just let the variable at the end of the block

```<code>some code
some code
some code
variable = pd.DataFrame([])

#print(variable)
variable
</code>```

That would show you the value of the variable

Body:
I am trying to clean np values in an open sourced data.

I am using python3, Jupyter and pandas.

 response = urllib.request.urlopen('https://resources.lendingclub.com/LoanStats3c.csv.zip')
 import shutil
 url = 'https://resources.lendingclub.com/LoanStats3c.csv.zip'
 file_name = 'LoanStats3c.csv.zip'

 with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
     shutil.copyfileobj(response, out_file)
     with zipfile.ZipFile(file_name) as zf:
         zf.extractall()

 loan=pd.read_csv(open('LoanStats3c.csv'), skiprows=1, parse_dates=True, index_col='id')
 loan.describe()

 # remove all columns with all NAs
 loan = loan.dropna(axis=1, how = 'all')
 loan.describe()

 # remove all rows with any NAs
 loan = loan.dropna(axis = 0)

 loan.describe()

But, the results are all columns with all NAs:

  loan_amnt  funded_amnt  funded_amnt_inv  installment  annual_inc  dti  \
  count        0.0          0.0              0.0          0.0         0.0  0.0
  mean         NaN          NaN              NaN          NaN         NaN  NaN
  std          NaN          NaN              NaN          NaN         NaN  NaN
  min          NaN          NaN              NaN          NaN         NaN  NaN
  25%          NaN          NaN              NaN          NaN         NaN  NaN
  50%          NaN          NaN              NaN          NaN         NaN  NaN
  75%          NaN          NaN              NaN          NaN         NaN  NaN
  max          NaN          NaN              NaN          NaN         NaN  NaN

Why all rows with valid values are gone and only the NA columns are left ?

thanks

Body:
When you'r using .dropna() like that all ocurrences with NaN values are deleted from dataframe

loan.dropna(axis=1, how = 'all')

Will delete the columns with all values in NaN

loan.dropna(axis = 0)

Will delete the rows with a least one value in NaN

I saw the file and i'm pretty sure that every rows has at least one column in NaN

Finally when using .describe() the dataframe is empty and the values that are shown are a descriptive statistics of that, if you want to see the real DF use print(df) or in jupyter just let the variable at the end of the block

some code
some code
some code
variable = pd.DataFrame([])

#print(variable)
variable

That would show you the value of the variable


问:
Find the tags in a file using pointers
So I am quite new to using pointers, and as such maybe what I'm trying doesn't even make sense...? I am working on a project that asks me to find html tags in a file by searching for the start and end points of the tags (being read in from a file).

Currently, I have the entire file written into a 1 dimensional char array and I must search through the array finding any tag starting and ending with the identifiers '<' and  '>' strictly using pointers. If there is a '/' or white space between the identifiers then it is not a tag, and we do not need to keep a reference of it. The array is set up in such a way that it does keep the formatting of the initial file.

Once a tag is found we must search the array for matches and count how many of the same tags there are. I am restricted from storing the found tags in an array but I am able to store the pointer to the start and end of each tag in a pointer array. Any guidance would be appreciated. This is the code I have that has it working probably, but I want to get some practice with pointers...

```<code>#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    FILE *fptr;
    char maxLength[10] = "";// this is where each tag less than 10 characters long will be held
    char htags[100][10]; // Array that will be used to store each tag found
    int tagCounts[100]; // Array that will count the amount of times a tag shows up
    for (int i = 0; i < 100; i++)
        tagCounts[i] = 0;
    int n = 0;
    char *filename = argv[1]; // Input file to be read
    fptr = fopen(filename, "r"); // Read through the file
    if (fptr == NULL)
    {
        printf("File not found...");
        return 0;
    }
int c;
    if (fptr != NULL)
    {
        while (c = fgetc(fptr) != NULL)// making sure we are reading until the very end of the file
        {
            char c = fgetc(fptr); // reading character by character

            if (c == '<' && !feof(fptr))// searching for tags that start with '<'
            {
                char ch = fgetc(fptr);
                if (ch != '/') // checking to see if it is actually a tag
                {
                    int i = 0;
                    strcpy(maxLength, ""); // this is what we will use to store the tags when we find one
                    while (ch != 10 && ch != '>' && ch != ' ')
                    {
                        maxLength[i] = ch; // adds the next character to str[] until
                        // it gets to the end of the tag
                        ch = fgetc(fptr);
                        i++; // increment i so that the characters don't overlap in the array
                    }

                    maxLength[i] = '\0'; // Used to indicate the termination of the character string for a tag

                    int number = 1;

                    for (int i = 0; i < n; i++)// Checking to see if the tag has already
                                               // been found before in the file
                    {
                        if (strcmp(htags[i], maxLength) == 0) // tag has been found before
                        {
                            tagCounts[i]++; // increase the count by 1
                            number = 0;
                            break;
                        }
                    }

                    if (number == 1)
                    {
                        strcpy(htags[n], maxLength); // store the newly found tag in the array
                        tagCounts[n]++;
                        n++; // A new tag has been found, so we must create another element in the array and subsequently increase it's count
                    }
                }
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        printf("Tag-> %s -> Appeared %d time(s)\n", htags[i], tagCounts[i]);
    }
}
</code>```

Above is what the program should do (take in a text file and count the tags), but now I must strictly be using pointers to reference the elements in the array, get the string between and then count the similar ones...

The arrays I have initialized for the below program are:

```<code>char buffer[100000];
char *sTags[100], *eTags[100];
</code>```

This is what I was able to come up with, but I have just hit a wall in my understanding of the material.

```<code>    char *contents = buffer;

    if(buffer != NULL){
       char *c = contents;

        if(*c == '<'){
            char *ch = contents;

            if(*ch != '/'){
                while(*ch != '\0' && *ch != '>' && *ch != ' '){
                    contents++; //nothing is telling us it isn't a tag, so find the end
                    //found the tag, so get the location right before it?
                    if(*ch == '>'){
                        *sTags = contents--;
                    }
                    *sTags = '\0';
                }
                //This is where I am confused.
                //How would I go about comparing the string to other strings
                //throughout the file if it is not stored somewhere..?
            }
        }
    }
    printf("%s", sTags);
</code>```

答:
Two things to consider.

1) isspace(c)

You may want to use isspace(c) instead of c == ' '.
https://www.techonthenet.com/c_language/standard_library_functions/ctype_h/isspace.php

Because it seems like checking for (c == ' ') will allow tags that cross ends of lines.
For example, what would your code do with a four line file like this?

```<code><alpha>
<be
ta>
<gamma>
</code>```

Would it find find tags like "<alpha>", "<be\nta>", and "<gamma>" ?
Hmm... Is that what you're checking (c == 10) for ?

2) strncmp()

I don't completey understand the pointers-only requirement of not storing copies of tags.
But strncmp() might help. https://www.tutorialspoint.com/c_standard_library/c_function_strncmp.htm
If you have the beginning and end pointers of a tag then the lenth is (end-beg+1).

So in your first kind of longish main() method you're looping over known tags, if any, to check for matches.

Your pointer-based one can loop over your known tags and you only need to call strncmp()
if the candidate tag is the same length as a known tag.

Lastly

Lastly, bonus points for printf() debugging, I find those super useful in my code.
Don't hesitate to print things out.

And if you don't already have a small test file for debugging I'd encourage you to make one.

Good luck!

Title: Find the tags in a file using pointers

Body:
So I am quite new to using pointers, and as such maybe what I'm trying doesn't even make sense...? I am working on a project that asks me to find html tags in a file by searching for the start and end points of the tags (being read in from a file).

Currently, I have the entire file written into a 1 dimensional char array and I must search through the array finding any tag starting and ending with the identifiers '<' and  '>' strictly using pointers. If there is a '/' or white space between the identifiers then it is not a tag, and we do not need to keep a reference of it. The array is set up in such a way that it does keep the formatting of the initial file.

Once a tag is found we must search the array for matches and count how many of the same tags there are. I am restricted from storing the found tags in an array but I am able to store the pointer to the start and end of each tag in a pointer array. Any guidance would be appreciated. This is the code I have that has it working probably, but I want to get some practice with pointers...

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    FILE *fptr;
    char maxLength[10] = "";// this is where each tag less than 10 characters long will be held
    char htags[100][10]; // Array that will be used to store each tag found
    int tagCounts[100]; // Array that will count the amount of times a tag shows up
    for (int i = 0; i < 100; i++)
        tagCounts[i] = 0;
    int n = 0;
    char *filename = argv[1]; // Input file to be read
    fptr = fopen(filename, "r"); // Read through the file
    if (fptr == NULL)
    {
        printf("File not found...");
        return 0;
    }
int c;
    if (fptr != NULL)
    {
        while (c = fgetc(fptr) != NULL)// making sure we are reading until the very end of the file
        {
            char c = fgetc(fptr); // reading character by character

            if (c == '<' && !feof(fptr))// searching for tags that start with '<'
            {
                char ch = fgetc(fptr);
                if (ch != '/') // checking to see if it is actually a tag
                {
                    int i = 0;
                    strcpy(maxLength, ""); // this is what we will use to store the tags when we find one
                    while (ch != 10 && ch != '>' && ch != ' ')
                    {
                        maxLength[i] = ch; // adds the next character to str[] until
                        // it gets to the end of the tag
                        ch = fgetc(fptr);
                        i++; // increment i so that the characters don't overlap in the array
                    }

                    maxLength[i] = '\0'; // Used to indicate the termination of the character string for a tag

                    int number = 1;

                    for (int i = 0; i < n; i++)// Checking to see if the tag has already
                                               // been found before in the file
                    {
                        if (strcmp(htags[i], maxLength) == 0) // tag has been found before
                        {
                            tagCounts[i]++; // increase the count by 1
                            number = 0;
                            break;
                        }
                    }

                    if (number == 1)
                    {
                        strcpy(htags[n], maxLength); // store the newly found tag in the array
                        tagCounts[n]++;
                        n++; // A new tag has been found, so we must create another element in the array and subsequently increase it's count
                    }
                }
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        printf("Tag-> %s -> Appeared %d time(s)\n", htags[i], tagCounts[i]);
    }
}

Above is what the program should do (take in a text file and count the tags), but now I must strictly be using pointers to reference the elements in the array, get the string between and then count the similar ones...

The arrays I have initialized for the below program are:

char buffer[100000];
char *sTags[100], *eTags[100];

This is what I was able to come up with, but I have just hit a wall in my understanding of the material.

    char *contents = buffer;

    if(buffer != NULL){
       char *c = contents;

        if(*c == '<'){
            char *ch = contents;

            if(*ch != '/'){
                while(*ch != '\0' && *ch != '>' && *ch != ' '){
                    contents++; //nothing is telling us it isn't a tag, so find the end
                    //found the tag, so get the location right before it?
                    if(*ch == '>'){
                        *sTags = contents--;
                    }
                    *sTags = '\0';
                }
                //This is where I am confused.
                //How would I go about comparing the string to other strings
                //throughout the file if it is not stored somewhere..?
            }
        }
    }
    printf("%s", sTags);

Body:
Two things to consider.

1) isspace(c)

You may want to use isspace(c) instead of c == ' '.
https://www.techonthenet.com/c_language/standard_library_functions/ctype_h/isspace.php

Because it seems like checking for (c == ' ') will allow tags that cross ends of lines.
For example, what would your code do with a four line file like this?

<alpha>
<be
ta>
<gamma>

Would it find find tags like "<alpha>", "<be\nta>", and "<gamma>" ?
Hmm... Is that what you're checking (c == 10) for ?

2) strncmp()

I don't completey understand the pointers-only requirement of not storing copies of tags.
But strncmp() might help. https://www.tutorialspoint.com/c_standard_library/c_function_strncmp.htm
If you have the beginning and end pointers of a tag then the lenth is (end-beg+1).

So in your first kind of longish main() method you're looping over known tags, if any, to check for matches.

Your pointer-based one can loop over your known tags and you only need to call strncmp()
if the candidate tag is the same length as a known tag.

Lastly

Lastly, bonus points for printf() debugging, I find those super useful in my code.
Don't hesitate to print things out.

And if you don't already have a small test file for debugging I'd encourage you to make one.

Good luck!


问:
Update grouped rows with ID of newly inserted grouping row
I want to group several rows of table A and insert a new row into table B for each bunch of grouped rows.
Next to that I want to update the rows of table A with the ID of the newly inserted row.

Inserting the lines into the table with 'grouplines' is like:

```<code>INSERT INTO B(...,...,...)
SELECT col1, col2 FROM A
GROUP BY col1,col2
</code>```

This will produce a list of IDs in table B. I want to update the rows of table A with the ID of the corresponding group-row of table B.

Is there a possibilty to do this?

Some sample data:

After grouping table B looks like:

And then table A should look like:

答:
Deme on db<>fiddle

You can achieve table B by using Row_Number like below

```<code>Select ROW_NUMBER() OVER(ORDER BY Category) as ID, Name, Category
into #B
from #A
group by Name, Category
</code>```

Then Update the table A after joining with table B like

```<code>Update a
set a.ID_Of_Group_row = b.ID
from #A a
inner join #B b on a.Category = b.Category
</code>```

Output

Body:
I want to group several rows of table A and insert a new row into table B for each bunch of grouped rows.
Next to that I want to update the rows of table A with the ID of the newly inserted row.

Inserting the lines into the table with 'grouplines' is like:

INSERT INTO B(...,...,...)
SELECT col1, col2 FROM A
GROUP BY col1,col2

This will produce a list of IDs in table B. I want to update the rows of table A with the ID of the corresponding group-row of table B.

Is there a possibilty to do this?

Some sample data:

After grouping table B looks like:

And then table A should look like:

Body:
Deme on db<>fiddle

You can achieve table B by using Row_Number like below

Select ROW_NUMBER() OVER(ORDER BY Category) as ID, Name, Category
into #B
from #A
group by Name, Category

Then Update the table A after joining with table B like

Update a
set a.ID_Of_Group_row = b.ID
from #A a
inner join #B b on a.Category = b.Category

Output


问:
Casting a borrowed reference with a lifetime to a raw pointer in Rust
I am new to rust and am trying to wrap my head around lifetimes. Please consider the following code:

use jni::JNIEnv;

pub struct CameraAppEngine<'a> {
    _env: &'a JNIEnv<'a>,
    _width: i32,
    _height: i32
}

impl<'a> CameraAppEngine<'a> {
    pub fn new(_env: &'a JNIEnv<'a>, _width: i32, _height: i32) -> CameraAppEngine {
        CameraAppEngine { _env, _width, _height }
    }

    pub fn env(&'a self) -> JNIEnv<'a> {
        JNIEnv::from_raw(self._env).unwrap() // error!
    }
}
```

<p>The JNIEnv::from_raw method has a type signature of <code>from_raw(ptr: *mut JNIEnv) -> Result<Self></code>, so this results in the compile error:</p>

```|         JNIEnv::from_raw(self._env).unwrap()
|                          ^^^^^^^^^ types differ in mutability
|
= note: expected raw pointer `*mut *const jni::sys::JNINativeInterface_`
                found reference `&'a jni::JNIEnv<'a>`
```

<p>I then tried <code>JNIEnv::from_raw(self._env as *mut JNIEnv<'a>).unwrap()</code>, but this results in:</p>

```|         JNIEnv::from_raw(self._env as *mut JNIEnv<'a>).unwrap() // error!
|                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected *-ptr, found struct `jni::JNIEnv`
|
= note: expected raw pointer `*mut *const jni::sys::JNINativeInterface_`
            found raw pointer `*mut jni::JNIEnv<'a>`

error[E0606]: casting `&'a jni::JNIEnv<'a>` as `*mut jni::JNIEnv<'a>` is invalid
--> native_app/src/camera_engine.rs:16:26
|
|         JNIEnv::from_raw(self._env as *mut JNIEnv<'a>).unwrap() // error!
|                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

<p>How do I get a valid JNIEnv out of the reference in the struct?</p>

<p>Cheers and thank you for your time!</p>

<p><strong>Update</strong></p>

<p>Here is a less contrived way to demonstrate the prolem.</p>

<pre class="lang-rust prettyprint-override"><code>pub fn create_camera_session(&'a mut self, surface: jobject) {
    // error!
    let window = ffi::ANativeWindow_fromSurface(self._env as *mut JNIEnv<'a>, surface);
}
</code>```

This results in:

```<code>error[E0606]: casting `&'a jni::JNIEnv<'a>` as `*mut jni::JNIEnv<'a>` is invalid
</code>```

The type signature of ANativeWindow_fromSurface is ANativeWindow_fromSurface(env: *mut JNIEnv, surface: jobject) -> *mut ANativeWindow.

答:
There are two types named JNIEnv in the jni crate:

jni::sys::JNIEnv: This is a type alias: type JNIEnv = *const JNINativeInterface_;

jni::JNIEnv: This is a safe wrapper around jni::sys::JNIEnv.

You cannot simply use as to cast between the two, because they are unrelated types. In fact, in your env() method, you can just clone self._env. Furthermore, jni::JNIEnv is just a wrapper for a pointer, and cloning is cheap (it's not a deep copy), so you can store it by value rather by reference in self._env.

jni::JNIEnv exposes methods to convert between the two types. jni::JNIEnv::from_raw goes from jni::sys::JNIEnv to jni::JNIEnv, and jni::JNIEnv::get_native_interface goes the other way.

Body:
I am new to rust and am trying to wrap my head around lifetimes. Please consider the following code:

use jni::JNIEnv;

pub struct CameraAppEngine<'a> {
    _env: &'a JNIEnv<'a>,
    _width: i32,
    _height: i32
}

impl<'a> CameraAppEngine<'a> {
    pub fn new(_env: &'a JNIEnv<'a>, _width: i32, _height: i32) -> CameraAppEngine {
        CameraAppEngine { _env, _width, _height }
    }

    pub fn env(&'a self) -> JNIEnv<'a> {
        JNIEnv::from_raw(self._env).unwrap() // error!
    }
}

The JNIEnv::from_raw method has a type signature of from_raw(ptr: *mut JNIEnv) -> Result<Self>, so this results in the compile error:

|         JNIEnv::from_raw(self._env).unwrap()
|                          ^^^^^^^^^ types differ in mutability
|
= note: expected raw pointer `*mut *const jni::sys::JNINativeInterface_`
                found reference `&'a jni::JNIEnv<'a>`

I then tried JNIEnv::from_raw(self._env as *mut JNIEnv<'a>).unwrap(), but this results in:

|         JNIEnv::from_raw(self._env as *mut JNIEnv<'a>).unwrap() // error!
|                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected *-ptr, found struct `jni::JNIEnv`
|
= note: expected raw pointer `*mut *const jni::sys::JNINativeInterface_`
            found raw pointer `*mut jni::JNIEnv<'a>`

error[E0606]: casting `&'a jni::JNIEnv<'a>` as `*mut jni::JNIEnv<'a>` is invalid
--> native_app/src/camera_engine.rs:16:26
|
|         JNIEnv::from_raw(self._env as *mut JNIEnv<'a>).unwrap() // error!
|                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

How do I get a valid JNIEnv out of the reference in the struct?

Cheers and thank you for your time!

Update

Here is a less contrived way to demonstrate the prolem.

pub fn create_camera_session(&'a mut self, surface: jobject) {
    // error!
    let window = ffi::ANativeWindow_fromSurface(self._env as *mut JNIEnv<'a>, surface);
}

This results in:

error[E0606]: casting `&'a jni::JNIEnv<'a>` as `*mut jni::JNIEnv<'a>` is invalid

The type signature of ANativeWindow_fromSurface is ANativeWindow_fromSurface(env: *mut JNIEnv, surface: jobject) -> *mut ANativeWindow.


问:
How do you custom-format the first word/character from an html-markup MySQL field?
I did the following which works with simple text fields:

```<code>$field = "How are you doing?";
$arr = explode(' ',trim($field));
$first_word = $arr[0];
$balance = strstr("$field"," ");
</code>```

It didn't work because the field contains html markup, perhaps an image, video, div, div, paragraph, etc and resulted in all text within the html getting mixed in with the text.

I could possibly use strip_tags to strip out the html then obtain first word and reformat it, but then I would have to figure out how to add the html back into the data. I'm wondering if there is a php or custom function ready made for this purpose.

答:
You can use DOMDocument to parse the HTML, modify the contents, and save it back as HTML. Also, find the words is not always as simple as using space delimiters since not all languages delimit their words with spaces and not all words are necessarily delimited by spaces. For example: mother-in-law this could be viewed as one word or as 3 depending on how you define a word. Also, things like pancake do you consider this one word or two (pan and cake)? One simple solution is to use the IntlBreakIterator::createWordInstance class which implements the Unicode Standard for text segmentation A.K.A UAX #29.

Here's an example of how you might go about implementing this:

```<code>$html = <<<'HTML'
<div>some sample text here</div>
HTML;

/* Let's extend DOMDocument to include a walk method that can traverse the entire DOM tree */
class MyDOMDocument extends DOMDocument {
    public function walk(DOMNode $node, $skipParent = false) {
        if (!$skipParent) {
            yield $node;
        }
        if ($node->hasChildNodes()) {
            foreach ($node->childNodes as $n) {
                yield from $this->walk($n);
            }
        }
    }
}

$dom = new MyDOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

// Let's traverse the DOMTree to find the first text node
foreach ($dom->walk($dom->childNodes->item(0)) as $node) {
    if ($node->nodeName === "#text") {
        break;
    }
}

// Extract the first word from that text node
$iterator = IntlBreakIterator::createWordInstance();
$iterator->setText($node->nodeValue); // set the text in the word iterator
$it = $iterator->getPartsIterator(IntlPartsIterator::KEY_RIGHT);
foreach ($it as $offset => $word) {
    break;
}

// You can do whatever you want to $word here
$word .= "s"; // I'm going to append the letter s

// Replace the text node with the modification
$unmodifiedString = substr($node->nodeValue, $offset);
$modifiedString = $word . $unmodifiedString;
$oldNode = $node; // Keep a copy of the old node for reference
$node->nodeValue = $modifiedString;

// Replace the node back into the DOM tree
$node->parentNode->replaceChild($node, $oldNode);

// Save the HTML
$newHTML = $dom->saveHTML();

echo $newHTML;
</code>```

Outputs

```
<div>somes sample text here</div>
```

Body:
I did the following which works with simple text fields:

$field = "How are you doing?";
$arr = explode(' ',trim($field));
$first_word = $arr[0];
$balance = strstr("$field"," ");

It didn't work because the field contains html markup, perhaps an image, video, div, div, paragraph, etc and resulted in all text within the html getting mixed in with the text.

I could possibly use strip_tags to strip out the html then obtain first word and reformat it, but then I would have to figure out how to add the html back into the data. I'm wondering if there is a php or custom function ready made for this purpose.

Body:
You can use DOMDocument to parse the HTML, modify the contents, and save it back as HTML. Also, find the words is not always as simple as using space delimiters since not all languages delimit their words with spaces and not all words are necessarily delimited by spaces. For example: mother-in-law this could be viewed as one word or as 3 depending on how you define a word. Also, things like pancake do you consider this one word or two (pan and cake)? One simple solution is to use the IntlBreakIterator::createWordInstance class which implements the Unicode Standard for text segmentation A.K.A UAX #29.

Here's an example of how you might go about implementing this:

$html = <<<'HTML'
<div>some sample text here</div>
HTML;

/* Let's extend DOMDocument to include a walk method that can traverse the entire DOM tree */
class MyDOMDocument extends DOMDocument {
    public function walk(DOMNode $node, $skipParent = false) {
        if (!$skipParent) {
            yield $node;
        }
        if ($node->hasChildNodes()) {
            foreach ($node->childNodes as $n) {
                yield from $this->walk($n);
            }
        }
    }
}

$dom = new MyDOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

// Let's traverse the DOMTree to find the first text node
foreach ($dom->walk($dom->childNodes->item(0)) as $node) {
    if ($node->nodeName === "#text") {
        break;
    }
}

// Extract the first word from that text node
$iterator = IntlBreakIterator::createWordInstance();
$iterator->setText($node->nodeValue); // set the text in the word iterator
$it = $iterator->getPartsIterator(IntlPartsIterator::KEY_RIGHT);
foreach ($it as $offset => $word) {
    break;
}

// You can do whatever you want to $word here
$word .= "s"; // I'm going to append the letter s

// Replace the text node with the modification
$unmodifiedString = substr($node->nodeValue, $offset);
$modifiedString = $word . $unmodifiedString;
$oldNode = $node; // Keep a copy of the old node for reference
$node->nodeValue = $modifiedString;

// Replace the node back into the DOM tree
$node->parentNode->replaceChild($node, $oldNode);

// Save the HTML
$newHTML = $dom->saveHTML();

echo $newHTML;

Outputs

<div>somes sample text here</div>


问:
Android: log into website and preserve session/cookie using DefaultHttpClient
I've been through different tutorials and this website, but couldn't find a proper solution. On the other hand, I've seen apps logging into websites and requesting further information, so I'm sure there's a way to get this working, but maybe my approach is all wrong.

Here's what I'm trying to do: I want to log into a website that needs user authentication and then read and parse websites that are only accessible if the user is logged in.
The problem: after POSTing the credentials to the website, I receive a cookie which doesn't seem to be preserved in my HttpClient, even though the docs suggest that exactly that should happen.

Here's some of my code:

```<code>DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(LOGIN_URL);

List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair(USER_FIELD, login));
nvps.add(new BasicNameValuePair(PASS_FIELD, pw));
nvps.add(new BasicNameValuePair(REMEMBERME, "on"));

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();

if (entity != null) {
  entity.consumeContent();
}

List<Cookie> cookies = httpclient.getCookieStore().getCookies();
</code>```

When I output the contents of "cookies", everything seems fine (I receive a session):

- [version: 0][name: ASP.NET_SessionId][value: xxx][domain: xxx][path: /][expiry: null]

As I understood, the cookie/session will be preserved and used in my HttpClient as long as I don't close it.

When reading the next page (which is restricted), using this code:

```<code>HttpGet httpget2 = new HttpGet(RESTRICTED_URL);
response = httpclient.execute(httpget2);
entity = response.getEntity();
InputStream data = entity.getContent();
// data will be parsed here
if (entity != null) {
    entity.consumeContent();
}
// connection will be closed afterwards
</code>```

If I output the response of the GET-request (using response.getStatusLine()) I get a "200 OK" message, but parsing the site that is returned shows, that the login is lost (I only retrieve a login form).

Any help is appreciated.

答:
In an application that I have to login to. First i have to run a GET followed by a POST and then the GET again. The First get will instantiate a Jsession Id for my connection. The POST will authenticate my ID and then the original get GET will return the real content.

The code below is for an app running in JBoss

```<code>public boolean login() {
    HttpGet  httpGet = new HttpGet(  "http://localhost:8080/gwt-console-server/rs/identity/secure/sid/");
    HttpPost httpPost = new HttpPost("http://localhost:8080/gwt-console-server/rs/identity/secure/j_security_check");
    HttpResponse response = null;

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair(USER_FIELD, userName));
    nvps.add(new BasicNameValuePair(PASS_FIELD, password));

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpClient.execute(httpGet);
        EntityUtils.consume(response.getEntity());

        response = httpClient.execute(httpPost);
        EntityUtils.consume(response.getEntity());

        response = httpClient.execute(httpGet);
        String sessionId =EntityUtils.toString(response.getEntity());

        String cookieId ="";
        List<Cookie> cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();
        for (Cookie cookie: cookies){
            if (cookie.getName().equals("JSESSIONID")){
                cookieId = cookie.getValue();
            }
        }

        if(sessionId!= null && sessionId.equals(cookieId) ){
            return true;
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}
</code>```

ContentLicense: CC BY-SA 2.5

Body:
I've been through different tutorials and this website, but couldn't find a proper solution. On the other hand, I've seen apps logging into websites and requesting further information, so I'm sure there's a way to get this working, but maybe my approach is all wrong.

Here's what I'm trying to do: I want to log into a website that needs user authentication and then read and parse websites that are only accessible if the user is logged in.
The problem: after POSTing the credentials to the website, I receive a cookie which doesn't seem to be preserved in my HttpClient, even though the docs suggest that exactly that should happen.

Here's some of my code:

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(LOGIN_URL);

List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair(USER_FIELD, login));
nvps.add(new BasicNameValuePair(PASS_FIELD, pw));
nvps.add(new BasicNameValuePair(REMEMBERME, "on"));

httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();

if (entity != null) {
  entity.consumeContent();
}

List<Cookie> cookies = httpclient.getCookieStore().getCookies();

When I output the contents of "cookies", everything seems fine (I receive a session):

- [version: 0][name: ASP.NET_SessionId][value: xxx][domain: xxx][path: /][expiry: null]

As I understood, the cookie/session will be preserved and used in my HttpClient as long as I don't close it.

When reading the next page (which is restricted), using this code:

HttpGet httpget2 = new HttpGet(RESTRICTED_URL);
response = httpclient.execute(httpget2);
entity = response.getEntity();
InputStream data = entity.getContent();
// data will be parsed here
if (entity != null) {
    entity.consumeContent();
}
// connection will be closed afterwards

If I output the response of the GET-request (using response.getStatusLine()) I get a "200 OK" message, but parsing the site that is returned shows, that the login is lost (I only retrieve a login form).

Any help is appreciated.

Body:
In an application that I have to login to. First i have to run a GET followed by a POST and then the GET again. The First get will instantiate a Jsession Id for my connection. The POST will authenticate my ID and then the original get GET will return the real content.

The code below is for an app running in JBoss

public boolean login() {
    HttpGet  httpGet = new HttpGet(  "http://localhost:8080/gwt-console-server/rs/identity/secure/sid/");
    HttpPost httpPost = new HttpPost("http://localhost:8080/gwt-console-server/rs/identity/secure/j_security_check");
    HttpResponse response = null;

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair(USER_FIELD, userName));
    nvps.add(new BasicNameValuePair(PASS_FIELD, password));

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpClient.execute(httpGet);
        EntityUtils.consume(response.getEntity());

        response = httpClient.execute(httpPost);
        EntityUtils.consume(response.getEntity());

        response = httpClient.execute(httpGet);
        String sessionId =EntityUtils.toString(response.getEntity());

        String cookieId ="";
        List<Cookie> cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();
        for (Cookie cookie: cookies){
            if (cookie.getName().equals("JSESSIONID")){
                cookieId = cookie.getValue();
            }
        }

        if(sessionId!= null && sessionId.equals(cookieId) ){
            return true;
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}


问:
Is there a simpler way of calling JSON in a Jinja2 template?
I managed to call in a JSON file into my router and access the content in the template. However, I'm wondering if there's a simpler way of calling my data. Is there a way to just call {{ title }} for example, and not {{ data["title"] }}? Any help would be greatly appreciated.

router_article_02.py

@app.route('/article-02/welcome')
    def article_02_welcome():
        with app.open_resource("templates/article-02/data.json", "r" ) as data_file:
            data = json.load(data_file)

    return render_template("article-02/welcome.html", data = data)
```

<p><strong>article-02.html</strong></p>

<pre class="lang-html prettyprint-override"><code><h1>{{ data["title"] }}</h1>
<div class="overline">Category: {{ data["category-type"] }}</div>
</code>```

答:
It looks like I needed to unpack the dictionary using the double asterisks or dictionary unpacking operator (**) notation.

router_article_02.py

@app.route('/article-02/welcome')
def article_02_welcome():
    with app.open_resource("templates/article-02/data.json", "r" ) as data_file:
        data = json.load(data_file)

    return render_template("article-02/welcome.html", **data)
```

<p><strong>article-02.html</strong></p>

<pre class="lang-html prettyprint-override"><code><h1>{{ title }}</h1>
<div class="overline">Category: {{ category_type }}</div>
</code>```

Body:
I managed to call in a JSON file into my router and access the content in the template. However, I'm wondering if there's a simpler way of calling my data. Is there a way to just call {{ title }} for example, and not {{ data["title"] }}? Any help would be greatly appreciated.

router_article_02.py

@app.route('/article-02/welcome')
    def article_02_welcome():
        with app.open_resource("templates/article-02/data.json", "r" ) as data_file:
            data = json.load(data_file)

    return render_template("article-02/welcome.html", data = data)

article-02.html

{{ data["title"] }}

Category: {{ data["category-type"] }}

Body:
It looks like I needed to unpack the dictionary using the double asterisks or dictionary unpacking operator (**) notation.

router_article_02.py

@app.route('/article-02/welcome')
def article_02_welcome():
    with app.open_resource("templates/article-02/data.json", "r" ) as data_file:
        data = json.load(data_file)

    return render_template("article-02/welcome.html", **data)

article-02.html

<h1>{{ title }}</h1>
<div class="overline">Category: {{ category_type }}</div>


问:
Plot scatter chart by using loop command
the main body of the programming is shown as below:

```<code>ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "='7G'!$A$2"
ActiveChart.FullSeriesCollection(1).XValues = "='7G'!$B$2:$B$67"
ActiveChart.FullSeriesCollection(1).Values = "='7G'!$N$2:$N$67"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = "='7G'!$A$68"
ActiveChart.FullSeriesCollection(2).XValues = "='7G'!$B$68:$B$133"
ActiveChart.FullSeriesCollection(2).Values = "='7G'!$N$68:$N$133"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(3).Name = "='7G'!$A$134"
ActiveChart.FullSeriesCollection(3).XValues = "='7G'!$B$134:$B199"
ActiveChart.FullSeriesCollection(3).Values = "='7G'!$N$134:$N$199"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(4).Name = "='7G'!$A$200"
ActiveChart.FullSeriesCollection(4).XValues = "='7G'!$B$200:$B$265"
ActiveChart.FullSeriesCollection(4).Values = "='7G'!$N$200:$N$265"
</code>```

mainly, need to plot 50 curves with above script in Excel.  Wonder if there is a way to simplify above script by using a loop statement since all series there have a formula to repeat.

答:
Try it.</p>

```<code>Sub test()
    Dim Cht As Chart
    Dim Shs As Series
    Dim Ws As Worksheet
    Dim rngName As Range
    Dim i As Integer, n As Integer

    Set Cht = ActiveChart
    Set Ws = Sheets("7G")
    n = 2
    With Cht
        For i = 1 To 50
            Set rngName = Ws.Range("a" & n)
            Set Shs = .SeriesCollection.NewSeries
            With Shs
                .Name = rngName
                .XValues = rngName.Offset(, 1).Resize(66)
                .Values = rngName.Offset(, 3).Resize(66)
            End With
            n = n + 66
        Next i
    End with

End Sub
</code>```

Body:
the main body of the programming is shown as below:

ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "='7G'!$A$2"
ActiveChart.FullSeriesCollection(1).XValues = "='7G'!$B$2:$B$67"
ActiveChart.FullSeriesCollection(1).Values = "='7G'!$N$2:$N$67"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = "='7G'!$A$68"
ActiveChart.FullSeriesCollection(2).XValues = "='7G'!$B$68:$B$133"
ActiveChart.FullSeriesCollection(2).Values = "='7G'!$N$68:$N$133"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(3).Name = "='7G'!$A$134"
ActiveChart.FullSeriesCollection(3).XValues = "='7G'!$B$134:$B199"
ActiveChart.FullSeriesCollection(3).Values = "='7G'!$N$134:$N$199"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(4).Name = "='7G'!$A$200"
ActiveChart.FullSeriesCollection(4).XValues = "='7G'!$B$200:$B$265"
ActiveChart.FullSeriesCollection(4).Values = "='7G'!$N$200:$N$265"

mainly, need to plot 50 curves with above script in Excel.  Wonder if there is a way to simplify above script by using a loop statement since all series there have a formula to repeat.

Body:
Try it.

Sub test()
    Dim Cht As Chart
    Dim Shs As Series
    Dim Ws As Worksheet
    Dim rngName As Range
    Dim i As Integer, n As Integer

    Set Cht = ActiveChart
    Set Ws = Sheets("7G")
    n = 2
    With Cht
        For i = 1 To 50
            Set rngName = Ws.Range("a" & n)
            Set Shs = .SeriesCollection.NewSeries
            With Shs
                .Name = rngName
                .XValues = rngName.Offset(, 1).Resize(66)
                .Values = rngName.Offset(, 3).Resize(66)
            End With
            n = n + 66
        Next i
    End with

End Sub


问:
Is there a fixed point representation available in C or Assembly
As far as I know, representing a fraction in C relies on floats and doubles which are in floating point representation.

Assume I'm trying to represent 1.5 which is a fixed point number (only one digit to the right of the radix point). Is there a way to represent such number in C or even assembly using a fixed point data type?

Are there even any fixed point instructions on x86 (or other architectures) which would operate on such type?

答:
While some C compilers offer special fixed-point types as an extension (not part of the standard C language), there's really very little use for them. Fixed point is just integers, interpreted with a different unit. For example, fixed point currency in typical cent denominations is just using integers that represent cents instead of dollars (or whatever the whole currency unit is) for your unit. Likewise, you can think of 8-bit RGB as having units of 1/256 or 1/255 "full intensity".

Adding and subtracting fixed point values with the same unit is just adding and subtracting integers. This is just like arithmetic with units in the physical sciences. The only value in having the language track that they're "fixed point" would be ensuring that you can only add/subtract values with matching units.

For multiplication and division, the result will not have the same units as the operands so you have to either treat the result as a different fixed-point type, or renormalize. For example if you multiply two values representing 1/16 units, the result will have 1/256 units. You can then either scale the value down by a factor of 16 (rounding in whatever way is appropriate) to get back to a value with 1/16 units.


问:
Where can I learn ffmpeg?
I really want to get more into video/audio encoding and decoding, and I’ve heard that ffmpeg is a good tool, but I can’t find any good tutorials for it. Does anyone know a good way to learn it?</p>

答:
Browse trough these links:

ffmpeg Encoding and Editing Course by Werner Robitza

ffmpeg examples by hhsprings (bitbucket.io)

ffmpeg book by Michael Koch

ffmprovisr

Books:

FFmpeg Basics: Multimedia handling with a fast audio and video encoder by Frantisek Korbel

Learn to Produce Video with FFmpeg in 30 Minutes or Less by Jan Ozer

Title: Where can I learn ffmpeg?


问:
How can I find the count of semicolon separated values?
I have a list of all email ids which I have copied from the 'To' field, from an email I received in MS Outlook. These values (email ids) are separated by a semicolon. I have copied this big list of email ids into Excel. Now I want to find the number of email ids in this list; basically by counting the number of semi colons.

One way I can do this is by writing C code. i.e. store the big list as string buffer, and keep comparing the chars to ";" in a while(char == ';') loop.
But I want to do it quickly.

Is there any quick way to find that out using either:

1.) Regular expression (I use powergrep for processing the regexps)

2.) In excel itself (any excel macro/plugin for that?)

3.) DOS script method

4.) Any other quick way of getting it done?

答:
I believe the following should work in Excel:

```<code>= Len(A1) - Len(Substitute(A1, ";", "")) + 1
</code>```

/EDIT: if you've pasted the email addresses over several cells, you can count the cells with the following function:

```<code>= CountA(A1:BY1)
</code>```

CountA counts non-empty cells in a given range. You can specify the range by typing =CountA( into a cell and then selecting your cell range with the mouse cursor.

OwnerDisplayName: goldenmean

LastEditorDisplayName: Svante

Body:
I believe the following should work in Excel:

= Len(A1) - Len(Substitute(A1, ";", "")) + 1

/EDIT: if you've pasted the email addresses over several cells, you can count the cells with the following function:

= CountA(A1:BY1)

CountA counts non-empty cells in a given range. You can specify the range by typing =CountA( into a cell and then selecting your cell range with the mouse cursor.

OwnerDisplayName: Konrad Rudolph

LastEditorDisplayName: Konrad Rudolph


问:
PHP automatic array key quoter
I'm working on a large codebase that doesn't have quotes around any array keys, e.g. $_GET[page].

Does anyone have a script/regex that programmatically adds quotes to array keys for PHP?

update

Here's what I ended up doing:

```<code>egrep -lRZ "\.php" . | \
    xargs -0 -l sed -i -e "s/\$\([a-zA-Z_]\+\)\[\([a-zA-Z_]\+\)\]/$\1['\2']/g"
</code>```

This only handles one-dimensional arrays.

答:
I hated doing this in the past. This will cover all but severe edge cases and multidimentional arrays. Either use preg_replace or something that allows regex for mass replace in all files (like Dreamweaver).

```<code>\$(\w+)\[(\w+)\]
</code>```

Replace like so (\#, where # is the match offset):

```<code>$\1['\2']
</code>```

I believe Dreamweaver's replace syntax is:

```<code>$$1['$2']
</code>```

Multidimentional might work with (after you run the above):

```<code>\['(\w+)'\]\[(\w+)\]

['$1']['$2']
</code>```

And finally, inspired by @MarcB's comment, in theory you could use PHP::Tokenizer. See the example and change the case statment to trigger on T_ARRAY and alter the array to conform before outputting it (and every other part of the file). If all works well, you'd get a new file, which you could then loop into SPL::DirectoryIterator and update all files in one go (now and in the future).

Title: PHP automatic array key quoter

Body:
I'm working on a large codebase that doesn't have quotes around any array keys, e.g. $_GET[page].

Does anyone have a script/regex that programmatically adds quotes to array keys for PHP?

update

Here's what I ended up doing:

egrep -lRZ "\.php" . | \
    xargs -0 -l sed -i -e "s/\$\([a-zA-Z_]\+\)\[\([a-zA-Z_]\+\)\]/$\1['\2']/g"

This only handles one-dimensional arrays.

Body:
I hated doing this in the past. This will cover all but severe edge cases and multidimentional arrays. Either use preg_replace or something that allows regex for mass replace in all files (like Dreamweaver).

\$(\w+)\[(\w+)\]

Replace like so (\#, where # is the match offset):

$\1['\2']

I believe Dreamweaver's replace syntax is:

$$1['$2']

Multidimentional might work with (after you run the above):

\['(\w+)'\]\[(\w+)\]

['$1']['$2']

And finally, inspired by @MarcB's comment, in theory you could use PHP::Tokenizer. See the example and change the case statment to trigger on T_ARRAY and alter the array to conform before outputting it (and every other part of the file). If all works well, you'd get a new file, which you could then loop into SPL::DirectoryIterator and update all files in one go (now and in the future).


答:
Use join to convert list to single string

```<code>self.map = [[" " for i in range(34)]
             for i in range(40)]  # initialize the 2d array
for layer in self.map: #print the array
    print( "".join(layer) )
</code>```

Title: Printing just the elements in a 2D list

Body:
I am making a text based game and the map is a 2D list.  The way i have been doing until now is like this:

self.map = [[" " for i in range(34)]
                 for i in range(40)]  # initialize the 2d array
    for layer in self.map: #print the array
        print(layer)

However, this prints the commas and quote marks as well. Is there any way to print the array with just the elements in the list and the printed list should be in the same format as the text printed by the code above above.

EDIT
This is how i would like to print it, but without the speech marks and commas:

[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']

Body:
Use join to convert list to single string

self.map = [[" " for i in range(34)]
             for i in range(40)]  # initialize the 2d array
for layer in self.map: #print the array
    print( "".join(layer) )