问:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.Calendar.before(java.lang.Object)' on a null object reference
The user can press three buttons: One will bring up a clock to select the time, another will bring up a calendar to select the date. The third button is to be pressed last, and is meant to send out a notification.

When I press on the last button, I get the error:

```<code>java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.Calendar.before(java.lang.Object)' on a null object reference
        at com.example.meeldetuletuserakendus.MeeldetuletusActivity.startAlarm(MeeldetuletusActivity.java:105)
</code>```

Something goes wrong when trying to do the method startAlarm(c).
I know for certain that everything worked perfectly when the code only had a choice to choose a time, but something broke when I coded the date picking part of the code. So the problem probably has something to do with the date.

MeeldetuletusActivity.java:

```<code>public class MeeldetuletusActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener {
    String timeText;
    TextView textTime;
    Button nupp_vali, nupp_katkesta, nupp_kuupaev, nupp_alarm;
    Calendar c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meeldetuletus);

        nupp_vali = findViewById(R.id.button2);
        nupp_katkesta = findViewById(R.id.nupp_katkesta);
        nupp_kuupaev = findViewById(R.id.button);
        nupp_alarm = findViewById(R.id.button3);

        nupp_alarm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startAlarm(c);
            }
        });

        nupp_vali.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment timePicker = new TimePickerFragment();
                timePicker.show(getSupportFragmentManager(), "time picker");

            }
        });

        nupp_kuupaev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment kuupaevaValija = new KuupaevaFragment();
                kuupaevaValija.show(getSupportFragmentManager(), "date picker");
            }
        });

        nupp_katkesta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                katkestaAlarm();
                Toast.makeText(MeeldetuletusActivity.this, "Meeldetuletus unustatud", Toast.LENGTH_SHORT).show();

            }
        });

    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, 0);
    }

    public void onDateSet (DatePicker view, int aasta, int kuu, int paev) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, aasta);
        c.set(Calendar.MONTH, kuu);
        c.set(Calendar.DAY_OF_MONTH, paev);
    }

    private void startAlarm(Calendar c) {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, NotificationPublisher.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
        if (c.before(Calendar.getInstance())) {
            c.add(Calendar.DATE, 1);
        }
        Objects.requireNonNull(alarmManager).setExact(AlarmManager.RTC_WAKEUP,
                c.getTimeInMillis(), pendingIntent);
    }

    private void katkestaAlarm() {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent myIntent = new Intent(getApplicationContext(), NotificationPublisher.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getApplicationContext(), 1, myIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        alarmManager.cancel(pendingIntent);
    }
}
</code>```

Row 105 is:

```<code>if (c.before(Calendar.getInstance())) {
</code>```

KuupaevaFragment.java:

```<code>public class KuupaevaFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int aasta = c.get(Calendar.YEAR);
        int kuu = c.get(Calendar.MONTH);
        int paev = c.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(),
                aasta, kuu, paev);

    }

}
</code>```

TimePickerFragment.java: (This worked perfectly before, I'm only putting this here so you can see what KuupaevaFragment.java is meant to do aswell)

```<code>public class TimePickerFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(),
                hour, minute, DateFormat.is24HourFormat(getActivity()));

    }

}
</code>```

NotificationPublisher.java:

```<code>public class NotificationPublisher extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationHelper notificationHelper = new NotificationHelper(context);
        NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
        notificationHelper.getManager().notify(1, nb.build());
        Notification notification = nb.build();
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_SOUND;
    }
}
</code>```

NotificationHelper.Java:

```<code>class NotificationHelper extends ContextWrapper {
    public static final String channelID = "channelID";
    public static final String channelName = "Channel Name";
    private NotificationManager notificationManager;
    public NotificationHelper(Context base) {
        super(base);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel();
        }
    }
    @TargetApi(Build.VERSION_CODES.O)
    private void createChannel() {
        NotificationChannel channel = new NotificationChannel(channelID, channelName,
                NotificationManager.IMPORTANCE_HIGH);
        getManager().createNotificationChannel(channel);
    }
    public NotificationManager getManager() {
        if (notificationManager == null) {
            notificationManager = (NotificationManager)
                    getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return notificationManager;
    }
    public NotificationCompat.Builder getChannelNotification() {
        return new NotificationCompat.Builder(getApplicationContext(), channelID)
                .setContentTitle("Meeldetuletus")
                .setContentText("Teie valitud aeg on käes")
                .setSmallIcon(R.drawable.ic_baseline_notifications_active_24);
    }
}
</code>```

答:
Try to reference to the Activity's field c in onTimeSet and onDateSet:

```<code>@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);
}

public void onDateSet(DatePicker view, int aasta, int kuu, int paev) {
    c = Calendar.getInstance();
    c.set(Calendar.YEAR, aasta);
    c.set(Calendar.MONTH, kuu);
    c.set(Calendar.DAY_OF_MONTH, paev);
}
</code>```

So instead of Calendar c = Calendar.getInstance(); write c = Calendar.getInstance(); like in the code above.

Or you can try to initialize c property in Activity:

```<code>Calendar c = Calendar.getInstance();
</code>```

and use it to change Calendar's properties, for example in onTimeSet method:

```<code>@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);
}
</code>```

Body:
The user can press three buttons: One will bring up a clock to select the time, another will bring up a calendar to select the date. The third button is to be pressed last, and is meant to send out a notification.

When I press on the last button, I get the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.Calendar.before(java.lang.Object)' on a null object reference
        at com.example.meeldetuletuserakendus.MeeldetuletusActivity.startAlarm(MeeldetuletusActivity.java:105)

Something goes wrong when trying to do the method startAlarm(c).
I know for certain that everything worked perfectly when the code only had a choice to choose a time, but something broke when I coded the date picking part of the code. So the problem probably has something to do with the date.

MeeldetuletusActivity.java:

public class MeeldetuletusActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener {
    String timeText;
    TextView textTime;
    Button nupp_vali, nupp_katkesta, nupp_kuupaev, nupp_alarm;
    Calendar c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meeldetuletus);

        nupp_vali = findViewById(R.id.button2);
        nupp_katkesta = findViewById(R.id.nupp_katkesta);
        nupp_kuupaev = findViewById(R.id.button);
        nupp_alarm = findViewById(R.id.button3);

        nupp_alarm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startAlarm(c);
            }
        });

        nupp_vali.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment timePicker = new TimePickerFragment();
                timePicker.show(getSupportFragmentManager(), "time picker");

            }
        });

        nupp_kuupaev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment kuupaevaValija = new KuupaevaFragment();
                kuupaevaValija.show(getSupportFragmentManager(), "date picker");
            }
        });

        nupp_katkesta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                katkestaAlarm();
                Toast.makeText(MeeldetuletusActivity.this, "Meeldetuletus unustatud", Toast.LENGTH_SHORT).show();

            }
        });

    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, 0);
    }

    public void onDateSet (DatePicker view, int aasta, int kuu, int paev) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, aasta);
        c.set(Calendar.MONTH, kuu);
        c.set(Calendar.DAY_OF_MONTH, paev);
    }

    private void startAlarm(Calendar c) {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, NotificationPublisher.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
        if (c.before(Calendar.getInstance())) {
            c.add(Calendar.DATE, 1);
        }
        Objects.requireNonNull(alarmManager).setExact(AlarmManager.RTC_WAKEUP,
                c.getTimeInMillis(), pendingIntent);
    }

    private void katkestaAlarm() {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent myIntent = new Intent(getApplicationContext(), NotificationPublisher.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getApplicationContext(), 1, myIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        alarmManager.cancel(pendingIntent);
    }
}

Row 105 is:

if (c.before(Calendar.getInstance())) {

KuupaevaFragment.java:

public class KuupaevaFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int aasta = c.get(Calendar.YEAR);
        int kuu = c.get(Calendar.MONTH);
        int paev = c.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(),
                aasta, kuu, paev);

    }

}

TimePickerFragment.java: (This worked perfectly before, I'm only putting this here so you can see what KuupaevaFragment.java is meant to do aswell)

public class TimePickerFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(),
                hour, minute, DateFormat.is24HourFormat(getActivity()));

    }

}

NotificationPublisher.java:

public class NotificationPublisher extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationHelper notificationHelper = new NotificationHelper(context);
        NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
        notificationHelper.getManager().notify(1, nb.build());
        Notification notification = nb.build();
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_SOUND;
    }
}

NotificationHelper.Java:

class NotificationHelper extends ContextWrapper {
    public static final String channelID = "channelID";
    public static final String channelName = "Channel Name";
    private NotificationManager notificationManager;
    public NotificationHelper(Context base) {
        super(base);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel();
        }
    }
    @TargetApi(Build.VERSION_CODES.O)
    private void createChannel() {
        NotificationChannel channel = new NotificationChannel(channelID, channelName,
                NotificationManager.IMPORTANCE_HIGH);
        getManager().createNotificationChannel(channel);
    }
    public NotificationManager getManager() {
        if (notificationManager == null) {
            notificationManager = (NotificationManager)
                    getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return notificationManager;
    }
    public NotificationCompat.Builder getChannelNotification() {
        return new NotificationCompat.Builder(getApplicationContext(), channelID)
                .setContentTitle("Meeldetuletus")
                .setContentText("Teie valitud aeg on käes")
                .setSmallIcon(R.drawable.ic_baseline_notifications_active_24);
    }
}

Body:
Try to reference to the Activity's field c in onTimeSet and onDateSet:

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);
}

public void onDateSet(DatePicker view, int aasta, int kuu, int paev) {
    c = Calendar.getInstance();
    c.set(Calendar.YEAR, aasta);
    c.set(Calendar.MONTH, kuu);
    c.set(Calendar.DAY_OF_MONTH, paev);
}

So instead of Calendar c = Calendar.getInstance(); write c = Calendar.getInstance(); like in the code above.

Or you can try to initialize c property in Activity:

Calendar c = Calendar.getInstance();

and use it to change Calendar's properties, for example in onTimeSet method:

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);
}


来源: stackexchange

问:
Silverlight layout - unwanted spaces around the edge of the canvas
I used the Silverlight Business template and started making some changes. For some reason now I have the browser coming up with both horizontal and vertical scroll bars. There seems to be a tiny white space around the edge of the silverlight canvas/object, but I can't figure out where it's coming from.

The object element is set to 100% for both height and width as in:

```<code><object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
</code>```

The body of the document has its padding and margin set to zero.

I've made sure that all the HTML elements have borders, padding and margin all set to zero.

Anybody got an idea where the extra spacing on the sides is coming from?

Regards,
Jacques

答:
I managed to find 'a' solution. It seems that the iframe used for the history portion of the silverlight navigation technique was the cause of the problem. It has its visibility set to hidden, but when I added display = none, it removed the browser scrollbars and the small bits of white space around the edges of the sliverlight app.

This seems odd as Microsoft's Silverlight business application is virtually identical to mine and doesn't require this to be done.

Regards,
Jacques

ContentLicense: CC BY-SA 3.0

Body:
I used the Silverlight Business template and started making some changes. For some reason now I have the browser coming up with both horizontal and vertical scroll bars. There seems to be a tiny white space around the edge of the silverlight canvas/object, but I can't figure out where it's coming from.

The object element is set to 100% for both height and width as in:

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">

The body of the document has its padding and margin set to zero.

I've made sure that all the HTML elements have borders, padding and margin all set to zero.

Anybody got an idea where the extra spacing on the sides is coming from?

Regards,
Jacques


问:
What is Object Buffer in Node?
In nodejs, I have:

```<code>  const fileToUpload = fs.readFileSync(test_file_path);
  console.log("fileToUpload: type: ", typeof fileToUpload, ", content: ", fileToUpload);
</code>```

which prints:

```<code>type: object,  content:  <Buffer 50 4b 03 04 14 00 08 00 08 00 78 84 cb 50 00 00 00 00 00 00 00 00 24 ae 12 01 3e 00 20 00 31 2e 32 2e 38 34 30 2e 31 31 33 35 36 34 2e 31 30 2e 31 2e ... 10573784 more bytes>
</code>```

I get that the type of fileToUpload is object. This is basic javascript type.
But what is <Buffer 50 4b 03 04 ....>? Is it binary format?

In frontend built with ReactJS, how can I convert an attached zip file to this <Buffer ... > type?

The reason I'm asking is that I need to get a file in such format in reactjs client side to be able to upload it to AWS s3 bucket. More details on: Javascript on client side: how to upload a zip file to AWS S3 bucket through pre-signed URL?

答:
See Node.js docs:

https://nodejs.org/api/fs.html#fsreadfilesyncpath-options

If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.

So, to get a string you can write e.g.:

```<code>const text = fs.readFileSync(filePath, 'utf8');
</code>```

The Buffer class is a subclass of JavaScript's Uint8Array.

The Buffer (Uint8Array) has some converter methods as well:

https://nodejs.org/api/buffer.html#buffer

ContentLicense: CC BY-SA 4.0

Title: What is Object Buffer in Node?

Body:
In nodejs, I have:

  const fileToUpload = fs.readFileSync(test_file_path);
  console.log("fileToUpload: type: ", typeof fileToUpload, ", content: ", fileToUpload);

which prints:

type: object,  content:  <Buffer 50 4b 03 04 14 00 08 00 08 00 78 84 cb 50 00 00 00 00 00 00 00 00 24 ae 12 01 3e 00 20 00 31 2e 32 2e 38 34 30 2e 31 31 33 35 36 34 2e 31 30 2e 31 2e ... 10573784 more bytes>

I get that the type of fileToUpload is object. This is basic javascript type.
But what is <Buffer 50 4b 03 04 ....>? Is it binary format?

In frontend built with ReactJS, how can I convert an attached zip file to this <Buffer ... > type?

The reason I'm asking is that I need to get a file in such format in reactjs client side to be able to upload it to AWS s3 bucket. More details on: Javascript on client side: how to upload a zip file to AWS S3 bucket through pre-signed URL?

Body:
See Node.js docs:

https://nodejs.org/api/fs.html#fsreadfilesyncpath-options

If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.

So, to get a string you can write e.g.:

const text = fs.readFileSync(filePath, 'utf8');

The Buffer class is a subclass of JavaScript's Uint8Array.

The Buffer (Uint8Array) has some converter methods as well:

https://nodejs.org/api/buffer.html#buffer


问:
You may have an infinite update loop in a component render function in vue for iteration
I have an array with 4 objects, which I render as follows

```<code><div v-for="item in cant_objetivos_tipo">
        {{ item.__data__.nombre_monitor_tipo + ' : ' + item.__data__.cantidad_objetivos }}
</div>
</code>```

then when i try fill another array with the data as follows

```<code><div v-for="item in cant_objetivos_tipo">
        {{ datapie.push(item.__data__.nombre_monitor_tipo + ' : ' + item.__data__.cantidad_objetivos) }}
</div
</code>```

i get infinite loop error

datapie[] is declared before

```<code><script>
    import axios from 'axios'

    export default {
        data() {
            return {
                cant_objetivos_tipo: [],
                datapie: [],
          }
        },...
</code>```

I hope you can help me, thank you very much in advance

答:
<template>

        {{ item.__data__.nombre_monitor_tipo + ' : ' + item.__data__.cantidad_objetivos }}
  </div
</template>

```

Body:
I have an array with 4 objects, which I render as follows

        {{ item.__data__.nombre_monitor_tipo + ' : ' + item.__data__.cantidad_objetivos }}

then when i try fill another array with the data as follows

        {{ datapie.push(item.__data__.nombre_monitor_tipo + ' : ' + item.__data__.cantidad_objetivos) }}
</div

i get infinite loop error

datapie[] is declared before

Body:
<template>
  <div v-for="(item,index) in cant_objetivos_tipo" :key="index">
        {{ item.__data__.nombre_monitor_tipo + ' : ' + item.__data__.cantidad_objetivos }}
  </div
</template>

<script>
import axios from 'axios';

export default
{
  name: 'MyCustomComponent',
  data()
  {
    return {
      cant_objetivos_tipo: [],
    };
  },
  computed:
  {
    datapie()
    {
      return this.cant_objetivos_tip.map(item => item.__data__.nombre_monitor_tipo + ' : ' + item.__data__.cantidad_objetivos);
    }
  },
  created()
  {
    this.fetchData();
  }
  methods:
  {
    fetchData()
    {
      axios.get('/api/get_tipo_objectivos').then(response =>
      {
        this.cant_objetivos_tipo = response || [];
      });
    }
  }
}
</script>


问:
How to (quickly) batch process multiple images and run through tesseract
I've successfully extracted text from a single pdf using a combination of magick-r and tesseract, but have hit a roadblock when trying to process multiple images.(It is for a non-profit organization)

I welcome answers in bash, but ask that they are comprehensive and don't skip the tesseract component.

The answers to this question are for image cleaning without using OCR, so not sure how the first answer can be integrated here.

image data:

My process:

```<code>library(tesseract)
library(dplyr)
library(stringr)
library(pdftools)
library(readr)
library(magick)
library(purrr)
</code>```

```<code># original data
#pdf <- https://github.com/pembletonc/Project44_Text_Extraction/blob/master/test-data/001_0145.pdf

#image file (note that size here doesn't match processing below because of 2mb limit)[![enter image description here][2]][2]

file_name <- tools::list_files_with_exts(dir = "./test-data", exts = "pdf")
page_count <- pdf_info(file_name)$pages

multi_files <- list(pdftools::pdf_convert(file_name, page = 1:page_count,
                                          filenames = paste0("./test-data/", "page", 1:page_count, ".png"),dpi = 250))

#or just get the file extensions for the file if already created[![enter image description here][1]][1]
#multi_files <- list(tools::list_files_with_exts(dir = "./test-data", exts = "png"))

</code>```

To read the images as magick files:

```<code>multi_images <- map(multi_files, image_read)

which creates a tibble magick pointer object with the images sort of joined as a frame:

[[1]]
# A tibble: 5 x 7
  format width height colorspace matte filesize density
  <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>
1 PNG     3243   2010 sRGB       FALSE        0 98x98
2 PNG     3247   2013 sRGB       FALSE  4515441 98x98
3 PNG     3243   2013 sRGB       FALSE  4559229 98x98
4 PNG     3247   2010 sRGB       FALSE  4270145 98x98
5 PNG     3247   2010 sRGB       FALSE  3212528 98x98

</code>```

How do I access this on each PNG so I can clean and process in an OCR?

```<code>multi_text_clean <- function(images){

  Map(function(x) {
    x %>%
      image_crop(geometry_area(width = 2200, height = 1600, y_off = 500, x_off = 650)) %>%
      image_resize("2000x") %>%
      image_background("white", flatten = TRUE) %>%
      image_noise(noisetype = "Uniform") %>%          # Reduce noise in image using a noise peak elimination filter
      image_enhance() %>%                             # Enhance image (minimize noise)
      image_normalize() %>%
      image_convert(type = 'Grayscale') %>%
      image_trim(fuzz = 40) %>%
      image_contrast(sharpen = 1) %>%
      #image_deskew(threshold = 40) %>%
      image_write(format = 'png', density = '300x300') %>%
      tesseract::ocr(tesseract(options = list(preserve_interword_spaces = 1)))
  }, images)

}

</code>```

This only runs it on the first image:

```<code>text_list <-  multi_text_clean(multi_images)
(text_multi <- stringr::str_split(text_list, pattern = "\\s{5,}"))

[[1]]
 [1] "Weather clear all day. A small arms inspection held at 1400 hrs. A recce party went\njout consisting of Coy Comds and Lt Col Nicklin, I.0. and Asst Adjt. An Orders group\nheld in the evening. Pay parade for HQ and Bn HQ was at 1900 hrs. A movie was shown\nfor B Coy personnel by our YMCA Supervisor."
 [2] ")\nWeather clear and cold all day. Personnel packed equipment early in the morning and |~\nwere ready to move at 0830 hrs. Unit embussed at 0900 hrs and moved to Rochefort, MR\n2076, Sheet 105, 1/25000, arriving at 1390 hrs. Coys were in position at 1600 hrs. |,,\nPW brought in by A Coy at 1800 hrs. PW was a deserter from 304 Regt 2 Pz division.\nNo other activity during the day. Patrols were sent out during the night by all coys}) u\nCold all day. Very quiet all morning. A Coy moved forward. Coy HQ set up at Chateawv .\n\\Vieux de Rochefort. Slight opposition met by A Coy on advance. Opposition met at\n\\Croic St Jean. A Coy was in position at 1700 hrs. Advance started at 1500 hrs. OP\nset up at 1900 hrs at MR 207753. Patrols sent out by all Coys."
 [3] "“y\neather wet all day. Snowed most of the day. 1 Pl from C Coy guarding bridge MR\n204767. A Coy sent a fighting patrol to clear Powder Mill woods MR 2074. Recce\npatrols sent out byall coys."
 [4] "f\nWeather fair all day. No enemy was seen during the day. A Coy sent out patrols during\ntthe day and night but no opposition memt. B Coy moved forward to MR 195771. Orders\nGroup held at 2000 hrs and orders were given to have all personnel ready to move to\nnew location by 1200 hrs on the 6 of Jan 1945. YMCA was to show a movie in the evenp\nling but the CO cancelled it. Two Polish deserters from the German army walked into\n|A Coy lines."
 [5] "iz\nWeather clear all day. CO, Coy Comds, Sig Officer and Vickers Officer left to recce\nnew location at 0830 hrs. Unit started to move to new location at 1200 hrs, Unit   Bs\narrived at AYE MR 2683, Sheet 91, 1\" to mile at 1500 hrs. Personnel were shown to\ntheir areas and billets."
 [6] "| 9\neather clear all day. Observation Post set up by the Intelligence Sec at MR 253813.| |\nQuiet all day. No enemy activity during the day."
 [7] "|\neather overcast and snowing. Intelligence Section set up another OP at MR 268814.\nNo enemy activity during the day. At 2300 hrs orders were received that all personnel\nere to be ready to move to new area on the morning of the 9th Jan, 1945."
 [8] ":"
 [9] "‘\nWeather clear and cold, Bm started to move at 0830 hrs. Bn reached Champlon"
[10] "&\nFamenine, MR 3182 at 1230 hrs. Bn relieved the HLI. Coys immediately took up"
[11] ":\npositions for all around defence."
[12] "4\n"

</code>```

How can I run this through each image in that magick object?

答:
Here's a script I put together (using many examples from stack overflow), which processes multiple .pdf's in a directory (or just one..) -- maybe it'll be of some help?

You can download the script at: https://drive.google.com/file/d/1fB9P0TQchE6vEr2MBug47aJIPc4yag45/view?usp=sharing

#!/bin/bash
echo -ne "\033]0;CREATE SEARCHABLE PDF MULTIPLE .PDF FILES\007" # set terminal title

note to users

```<code># install ImageMagick
# install tesseract
# install pdftk
# install libtiff
</code>```

get current directory

```<code>current_dir=$(pwd)
</code>```

set temporary director name

```<code>temp_dir_nme=pdf_OCR_temp_nkAIumgy430qIRVn3Np6ZQx
</code>```

warn user no spaces in files; files will be renamed: \e[1;31m (red colour on) | \033[0m": colour off

```<code>echo -e "\e[1;31mTAKE NOTE that directory names containing spaces are unsupported!\033[0m"
printf "\n"

echo -e "\e[1;31mTAKE NOTE FURTHER that your .pdf file/files will be renamed to replace any spaces in their names with underscores!\033[0m"
printf "\n"
</code>```

give user an opportunity to back out...

```<code>read -p "Press enter to continue..."
printf "\n"
</code>```

rename .pdf files to replace spaces with "underscore"

```<code>for f in *.pdf
    do mv "$f" "${f// /_}"
done

# run script for each .pdf file in folder
for f in *.pdf
    do

    # establish path to input .pdf file
        path_to_file="$current_dir/"$f

    # make temp directory for operations
        mkdir $current_dir/$temp_dir_nme

    # copy .pdf file to temp directory
        cp $f $current_dir/$temp_dir_nme

    # change to temp directory to work the magick
        cd $current_dir/$temp_dir_nme

        no_pgs=$(pdftk $f dump_data | grep NumberOfPages | awk '{print $2}')
        pgs_per_vol=10 # for .pdf's of more than ten pages
        min_volumes=$(( no_pgs / pgs_per_vol )) # for .pdf's of more than ten pages
        fin_volume=$(( min_volumes+1 )) # for .pdf's of more than ten pages
        unlik_nme="nkAIumgy430qIRVn3Np6ZQx_" # give .pdf volumes unlikely names

        let "ss = $min_volumes * 10" 1> /dev/null
        let "tt = $ss + 1" 1> /dev/null

    # chop .pdf into volumes

    # chop .pdf into one volume with a new name if it has ten or less pages
        if [ $no_pgs -lt 11 ]
            then
            pdftk $f cat 1-$no_pgs output $unlik_nme.pdf
        fi

    # chop .pdf into multiple volumes if it has eleven or more pages, excluding the final volume
        if [ $no_pgs -gt 10 ]
            then
                echo Chopping $f into $fin_volume volumes, to a maximum of ten pages per volume...

                i=1
                j=1
                k=$pgs_per_vol

                pdftk $f cat 1-$pgs_per_vol output "${unlik_nme}${i}".pdf # concatenate variables

                while [ $i -ne $min_volumes ]
                    do
                        j=$(( $j + pgs_per_vol ))
                        k=$(( $k + pgs_per_vol ))
                        i=$(( $i + 1 ))

                        pdftk $f cat $j-$k output "${unlik_nme}${i}".pdf
                    done

        # create final volume of whatever number of pages
            pdftk $f cat $tt-end output "${fin_volume}${unlik_nme}".pdf 2> /dev/null
        fi

    # remove initial .pdf file
        rm $f

    # rename pdf volumes in directory sorted by modification time, oldest first: ls -tr
        n=0; ls -tr | while read a; do n=$(( n+i )); mv -- "$a" "$(printf '%03d' "$n")"_"$a"; done

        total_vols=$(( $min_volumes + 2 ))

    # loop over .pdf volumes in directory

        for files in *.pdf
            do
                echo Exporting $files to .png images...

            # export .pdf volume  to .png images
                pdftoppm -r 150 $files exported -png

            # delete .pdf volume
                rm $files
                echo Converting .png files to .jpg files...

            # convert .png files to .jpg files
                magick convert *.png %03d_converted.jpg

            # delete first .png images
                rm *.png

            # deskew images
                echo Deskewing text...
                magick convert *.jpg -deskew 90% %03d_deskewed.jpg

            # delete converted .jpf images
                rm *converted.jpg

            # enhance contrast
                echo Enhancing contrast...
                magick convert -brightness-contrast 0x10 *.jpg %03d_contrast.jpg

            # delete deskewed images
                rm *deskewed.jpg

            # crop and resize .jpg images to A4 ratio
                echo Resizing, and cropping...
                magick mogrify -format jpg -geometry "1680x2376^" -gravity center -extent 1680x2376 *.jpg

            # generate compressed .tiff image from .jpg images
                echo Converting resized and cropped .jpg"'s" to a compressed .tiff file...
                magick convert -compress lzw *.jpg images.tiff

            # delete .jpg images
                rm *.jpg

            # create .pdf
                magick convert images.tiff images.pdf
                printf "\n"
                echo Recognizing text...

            # recognize text
                tesseract images.tiff text -l eng -c textonly_pdf=1 pdf
                printf "\n"

            # delete .tiff to save space
                rm images.tiff

            # add "OCR_" to original .pdf name
                pdftk text.pdf multibackground images.pdf output "OC_"$files

            # compressing .pdf
                ps2pdf -dPDFSETTINGS=/ebook "OC_"$files "CR_"$files

        # end files loop
            done

# combine output .pdf files into "OCR"_original_file_name.pdf
    pdftk CR_*.pdf output "OCR_"$f

# copy "OCR"_original_file_name.pdf to initial directory
    cp "OCR_"$f $current_dir

# change to previous directory
    cd $current_dir

# delete temporary directory, and temporary files
    rm -r $current_dir/$temp_dir_nme

    done
</code>```

print line

```<code>printf "\n"
printf '=%.s' {1..40}; echo
read -p "Press enter to exit..."
</code>```

Body:
I've successfully extracted text from a single pdf using a combination of magick-r and tesseract, but have hit a roadblock when trying to process multiple images.(It is for a non-profit organization)

I welcome answers in bash, but ask that they are comprehensive and don't skip the tesseract component.

The answers to this question are for image cleaning without using OCR, so not sure how the first answer can be integrated here.

image data:

My process:

library(tesseract)
library(dplyr)
library(stringr)
library(pdftools)
library(readr)
library(magick)
library(purrr)

# original data
#pdf <- https://github.com/pembletonc/Project44_Text_Extraction/blob/master/test-data/001_0145.pdf

#image file (note that size here doesn't match processing below because of 2mb limit)[![enter image description here][2]][2]

file_name <- tools::list_files_with_exts(dir = "./test-data", exts = "pdf")
page_count <- pdf_info(file_name)$pages

multi_files <- list(pdftools::pdf_convert(file_name, page = 1:page_count,
                                          filenames = paste0("./test-data/", "page", 1:page_count, ".png"),dpi = 250))

#or just get the file extensions for the file if already created[![enter image description here][1]][1]
#multi_files <- list(tools::list_files_with_exts(dir = "./test-data", exts = "png"))

To read the images as magick files:

multi_images <- map(multi_files, image_read)

which creates a tibble magick pointer object with the images sort of joined as a frame:

[[1]]
# A tibble: 5 x 7
  format width height colorspace matte filesize density
  <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>
1 PNG     3243   2010 sRGB       FALSE        0 98x98
2 PNG     3247   2013 sRGB       FALSE  4515441 98x98
3 PNG     3243   2013 sRGB       FALSE  4559229 98x98
4 PNG     3247   2010 sRGB       FALSE  4270145 98x98
5 PNG     3247   2010 sRGB       FALSE  3212528 98x98

How do I access this on each PNG so I can clean and process in an OCR?

multi_text_clean <- function(images){

  Map(function(x) {
    x %>%
      image_crop(geometry_area(width = 2200, height = 1600, y_off = 500, x_off = 650)) %>%
      image_resize("2000x") %>%
      image_background("white", flatten = TRUE) %>%
      image_noise(noisetype = "Uniform") %>%          # Reduce noise in image using a noise peak elimination filter
      image_enhance() %>%                             # Enhance image (minimize noise)
      image_normalize() %>%
      image_convert(type = 'Grayscale') %>%
      image_trim(fuzz = 40) %>%
      image_contrast(sharpen = 1) %>%
      #image_deskew(threshold = 40) %>%
      image_write(format = 'png', density = '300x300') %>%
      tesseract::ocr(tesseract(options = list(preserve_interword_spaces = 1)))
  }, images)

}

This only runs it on the first image:

text_list <-  multi_text_clean(multi_images)
(text_multi <- stringr::str_split(text_list, pattern = "\\s{5,}"))

[[1]]
 [1] "Weather clear all day. A small arms inspection held at 1400 hrs. A recce party went\njout consisting of Coy Comds and Lt Col Nicklin, I.0. and Asst Adjt. An Orders group\nheld in the evening. Pay parade for HQ and Bn HQ was at 1900 hrs. A movie was shown\nfor B Coy personnel by our YMCA Supervisor."
 [2] ")\nWeather clear and cold all day. Personnel packed equipment early in the morning and |~\nwere ready to move at 0830 hrs. Unit embussed at 0900 hrs and moved to Rochefort, MR\n2076, Sheet 105, 1/25000, arriving at 1390 hrs. Coys were in position at 1600 hrs. |,,\nPW brought in by A Coy at 1800 hrs. PW was a deserter from 304 Regt 2 Pz division.\nNo other activity during the day. Patrols were sent out during the night by all coys}) u\nCold all day. Very quiet all morning. A Coy moved forward. Coy HQ set up at Chateawv .\n\\Vieux de Rochefort. Slight opposition met by A Coy on advance. Opposition met at\n\\Croic St Jean. A Coy was in position at 1700 hrs. Advance started at 1500 hrs. OP\nset up at 1900 hrs at MR 207753. Patrols sent out by all Coys."
 [3] "“y\neather wet all day. Snowed most of the day. 1 Pl from C Coy guarding bridge MR\n204767. A Coy sent a fighting patrol to clear Powder Mill woods MR 2074. Recce\npatrols sent out byall coys."
 [4] "f\nWeather fair all day. No enemy was seen during the day. A Coy sent out patrols during\ntthe day and night but no opposition memt. B Coy moved forward to MR 195771. Orders\nGroup held at 2000 hrs and orders were given to have all personnel ready to move to\nnew location by 1200 hrs on the 6 of Jan 1945. YMCA was to show a movie in the evenp\nling but the CO cancelled it. Two Polish deserters from the German army walked into\n|A Coy lines."
 [5] "iz\nWeather clear all day. CO, Coy Comds, Sig Officer and Vickers Officer left to recce\nnew location at 0830 hrs. Unit started to move to new location at 1200 hrs, Unit   Bs\narrived at AYE MR 2683, Sheet 91, 1\" to mile at 1500 hrs. Personnel were shown to\ntheir areas and billets."
 [6] "| 9\neather clear all day. Observation Post set up by the Intelligence Sec at MR 253813.| |\nQuiet all day. No enemy activity during the day."
 [7] "|\neather overcast and snowing. Intelligence Section set up another OP at MR 268814.\nNo enemy activity during the day. At 2300 hrs orders were received that all personnel\nere to be ready to move to new area on the morning of the 9th Jan, 1945."
 [8] ":"
 [9] "‘\nWeather clear and cold, Bm started to move at 0830 hrs. Bn reached Champlon"
[10] "&\nFamenine, MR 3182 at 1230 hrs. Bn relieved the HLI. Coys immediately took up"
[11] ":\npositions for all around defence."
[12] "4\n"

How can I run this through each image in that magick object?

Body:
Here's a script I put together (using many examples from stack overflow), which processes multiple .pdf's in a directory (or just one..) -- maybe it'll be of some help?

You can download the script at: https://drive.google.com/file/d/1fB9P0TQchE6vEr2MBug47aJIPc4yag45/view?usp=sharing

#!/bin/bash
echo -ne "\033]0;CREATE SEARCHABLE PDF MULTIPLE .PDF FILES\007" # set terminal title

note to users

# install ImageMagick
# install tesseract
# install pdftk
# install libtiff

get current directory

current_dir=$(pwd)

set temporary director name

temp_dir_nme=pdf_OCR_temp_nkAIumgy430qIRVn3Np6ZQx

warn user no spaces in files; files will be renamed: \e[1;31m (red colour on) | \033[0m": colour off

echo -e "\e[1;31mTAKE NOTE that directory names containing spaces are unsupported!\033[0m"
printf "\n"

echo -e "\e[1;31mTAKE NOTE FURTHER that your .pdf file/files will be renamed to replace any spaces in their names with underscores!\033[0m"
printf "\n"

give user an opportunity to back out...

read -p "Press enter to continue..."
printf "\n"

rename .pdf files to replace spaces with "underscore"

for f in *.pdf
    do mv "$f" "${f// /_}"
done

# run script for each .pdf file in folder
for f in *.pdf
    do

    # establish path to input .pdf file
        path_to_file="$current_dir/"$f

    # make temp directory for operations
        mkdir $current_dir/$temp_dir_nme

    # copy .pdf file to temp directory
        cp $f $current_dir/$temp_dir_nme

    # change to temp directory to work the magick
        cd $current_dir/$temp_dir_nme

        no_pgs=$(pdftk $f dump_data | grep NumberOfPages | awk '{print $2}')
        pgs_per_vol=10 # for .pdf's of more than ten pages
        min_volumes=$(( no_pgs / pgs_per_vol )) # for .pdf's of more than ten pages
        fin_volume=$(( min_volumes+1 )) # for .pdf's of more than ten pages
        unlik_nme="nkAIumgy430qIRVn3Np6ZQx_" # give .pdf volumes unlikely names

        let "ss = $min_volumes * 10" 1> /dev/null
        let "tt = $ss + 1" 1> /dev/null

    # chop .pdf into volumes

    # chop .pdf into one volume with a new name if it has ten or less pages
        if [ $no_pgs -lt 11 ]
            then
            pdftk $f cat 1-$no_pgs output $unlik_nme.pdf
        fi

    # chop .pdf into multiple volumes if it has eleven or more pages, excluding the final volume
        if [ $no_pgs -gt 10 ]
            then
                echo Chopping $f into $fin_volume volumes, to a maximum of ten pages per volume...

                i=1
                j=1
                k=$pgs_per_vol

                pdftk $f cat 1-$pgs_per_vol output "${unlik_nme}${i}".pdf # concatenate variables

                while [ $i -ne $min_volumes ]
                    do
                        j=$(( $j + pgs_per_vol ))
                        k=$(( $k + pgs_per_vol ))
                        i=$(( $i + 1 ))

                        pdftk $f cat $j-$k output "${unlik_nme}${i}".pdf
                    done

        # create final volume of whatever number of pages
            pdftk $f cat $tt-end output "${fin_volume}${unlik_nme}".pdf 2> /dev/null
        fi

    # remove initial .pdf file
        rm $f

    # rename pdf volumes in directory sorted by modification time, oldest first: ls -tr
        n=0; ls -tr | while read a; do n=$(( n+i )); mv -- "$a" "$(printf '%03d' "$n")"_"$a"; done

        total_vols=$(( $min_volumes + 2 ))

    # loop over .pdf volumes in directory

        for files in *.pdf
            do
                echo Exporting $files to .png images...

            # export .pdf volume  to .png images
                pdftoppm -r 150 $files exported -png

            # delete .pdf volume
                rm $files
                echo Converting .png files to .jpg files...

            # convert .png files to .jpg files
                magick convert *.png %03d_converted.jpg

            # delete first .png images
                rm *.png

            # deskew images
                echo Deskewing text...
                magick convert *.jpg -deskew 90% %03d_deskewed.jpg

            # delete converted .jpf images
                rm *converted.jpg

            # enhance contrast
                echo Enhancing contrast...
                magick convert -brightness-contrast 0x10 *.jpg %03d_contrast.jpg

            # delete deskewed images
                rm *deskewed.jpg

            # crop and resize .jpg images to A4 ratio
                echo Resizing, and cropping...
                magick mogrify -format jpg -geometry "1680x2376^" -gravity center -extent 1680x2376 *.jpg

            # generate compressed .tiff image from .jpg images
                echo Converting resized and cropped .jpg"'s" to a compressed .tiff file...
                magick convert -compress lzw *.jpg images.tiff

            # delete .jpg images
                rm *.jpg

            # create .pdf
                magick convert images.tiff images.pdf
                printf "\n"
                echo Recognizing text...

            # recognize text
                tesseract images.tiff text -l eng -c textonly_pdf=1 pdf
                printf "\n"

            # delete .tiff to save space
                rm images.tiff

            # add "OCR_" to original .pdf name
                pdftk text.pdf multibackground images.pdf output "OC_"$files

            # compressing .pdf
                ps2pdf -dPDFSETTINGS=/ebook "OC_"$files "CR_"$files

        # end files loop
            done

# combine output .pdf files into "OCR"_original_file_name.pdf
    pdftk CR_*.pdf output "OCR_"$f

# copy "OCR"_original_file_name.pdf to initial directory
    cp "OCR_"$f $current_dir

# change to previous directory
    cd $current_dir

# delete temporary directory, and temporary files
    rm -r $current_dir/$temp_dir_nme

    done

print line

printf "\n"
printf '=%.s' {1..40}; echo
read -p "Press enter to exit..."


问:
Printing a zip from dictionary return Empty list
I am performing this simple zip in python from a dictionary and I am not sure why the second time I do it it gives me an empty list.

```<code>l1= {'a':6, 'b':5, 'c':3, 'd':2, 'e':1, 'f':7, 'g':4, 'h':9}
l2={'a':7, 'b':5, 'c':2, 'd':3, 'e':8}

z=zip(l1.values(),l2.values())

print(list(z)) #This gives me correct output.
print(list(z)) #This gives me EMPTY list. WHY?

[(6, 7), (5, 5), (3, 2), (2, 3), (1, 8)]
[]
</code>```

答:
you can use zip one time because it iter object

you can convert it to list object

```<code>import copy
l1= {'a':6, 'b':5, 'c':3, 'd':2, 'e':1, 'f':7, 'g':4, 'h':9}
l2={'a':7, 'b':5, 'c':2, 'd':3, 'e':8}

z=list(zip(l1.values(),l2.values()))
print(list(z)) #This gives me correct output.
print(list(z)) #This gives me EMPTY list. WHY?

</code>```

output

```<code>[(6, 7), (5, 5), (3, 2), (2, 3), (1, 8)]
[(6, 7), (5, 5), (3, 2), (2, 3), (1, 8)]
</code>```

Body:
I am performing this simple zip in python from a dictionary and I am not sure why the second time I do it it gives me an empty list.

l1= {'a':6, 'b':5, 'c':3, 'd':2, 'e':1, 'f':7, 'g':4, 'h':9}
l2={'a':7, 'b':5, 'c':2, 'd':3, 'e':8}

z=zip(l1.values(),l2.values())

print(list(z)) #This gives me correct output.
print(list(z)) #This gives me EMPTY list. WHY?

[(6, 7), (5, 5), (3, 2), (2, 3), (1, 8)]
[]

Body:
you can use zip one time because it iter object

you can convert it to list object

import copy
l1= {'a':6, 'b':5, 'c':3, 'd':2, 'e':1, 'f':7, 'g':4, 'h':9}
l2={'a':7, 'b':5, 'c':2, 'd':3, 'e':8}

z=list(zip(l1.values(),l2.values()))
print(list(z)) #This gives me correct output.
print(list(z)) #This gives me EMPTY list. WHY?

output

[(6, 7), (5, 5), (3, 2), (2, 3), (1, 8)]
[(6, 7), (5, 5), (3, 2), (2, 3), (1, 8)]


问:
select row in heavy csv
i search how can i select some row with word in line so i use this script

```<code>import pandas
import datetime

df = pandas.read_csv(
    r"C:StockEtablissement_utf8(1)\StockEtablissement_utf8.csv",
    sep=",",
)

communes = ["PERPIGNAN"]

print()

df = df[~df["libelleCommuneEtablissement"].isin(communes)]

print()
</code>```

so my script work well with a normal csv

but with a heavy Csv (4Go) the scipt say :

```<code>Traceback (most recent call last):
  File "C:lafinessedufiness.py", line 5, in <module>
    df = pandas.read_csv(r'C:StockEtablissement_utf8(1)\StockEtablissement_utf8.csv',
  File "C:\Users\\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers\readers.py", line 680, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers\readers.py", line 581, in _read
    return parser.read(nrows)
  File "C:\Users\\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers\readers.py", line 1250, in read
    index, columns, col_dict = self._engine.read(nrows)
  File "C:\Users\\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 225, in read
    chunks = self._reader.read_low_memory(nrows)
  File "pandas\_libs\parsers.pyx", line 805, in pandas._libs.parsers.TextReader.read_low_memory
  File "pandas\_libs\parsers.pyx", line 883, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 1026, in pandas._libs.parsers.TextReader._convert_column_data
  File "pandas\_libs\parsers.pyx", line 1072, in pandas._libs.parsers.TextReader._convert_tokens
  File "pandas\_libs\parsers.pyx", line 1172, in pandas._libs.parsers.TextReader._convert_with_dtype
  File "pandas\_libs\parsers.pyx", line 1731, in pandas._libs.parsers._try_int64
MemoryError: Unable to allocate 128. KiB for an array with shape (16384,) and data type int64
</code>```

do you know how can i fix this error please?

答:
As you can see, you don't have enough memory available for Pandas to load that file entirely into memory.

One reason is that based on Python38-32 in the traceback, you're running a 32-bit version of Python, where 4 gigabytes (or is it 3 gigabytes?) is the limit for memory allocations anyway. If your system is 64-bit, you should switch to the 64-bit version of Python, so that's one obstacle less.

If that doesn't help, you'll just also need more memory. You could configure Windows's virtual memory, or buy more actual memory and install it in your system.

If those don't help, then you'll have to come up with a better approach than to load the big CSV entirely into memory.

For one, if you really only care about rows with the string PERPIGNAN (no matter the column; you can really filter it again in your code), you could do grep PERPIGNAN data.csv > data_perpignan.csv and work with that (assuming you have grep; you can do the same filtering with a short Python script).

Since read_csv() accepts any iterable of lines, you can also just do something like

```<code>def lines_from_file_including_strings(file, strings):
    for i, line in enumerate(file):
        if i == 0 or any(string in line for string in strings):
            yield line

communes = ["PERPIGNAN", "PARIS"]

with open("StockEtablissement_utf8.csv") as f:
  df = pd.read_csv(lines_from_file_including_strings(f, communes), sep=",")
</code>```

for an initial filter.

Title: select row in heavy csv

Body:
i search how can i select some row with word in line so i use this script

import pandas
import datetime

df = pandas.read_csv(
    r"C:StockEtablissement_utf8(1)\StockEtablissement_utf8.csv",
    sep=",",
)

communes = ["PERPIGNAN"]

print()

df = df[~df["libelleCommuneEtablissement"].isin(communes)]

print()

so my script work well with a normal csv

but with a heavy Csv (4Go) the scipt say :

Traceback (most recent call last):
  File "C:lafinessedufiness.py", line 5, in <module>
    df = pandas.read_csv(r'C:StockEtablissement_utf8(1)\StockEtablissement_utf8.csv',
  File "C:\Users\\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers\readers.py", line 680, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers\readers.py", line 581, in _read
    return parser.read(nrows)
  File "C:\Users\\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers\readers.py", line 1250, in read
    index, columns, col_dict = self._engine.read(nrows)
  File "C:\Users\\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 225, in read
    chunks = self._reader.read_low_memory(nrows)
  File "pandas\_libs\parsers.pyx", line 805, in pandas._libs.parsers.TextReader.read_low_memory
  File "pandas\_libs\parsers.pyx", line 883, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 1026, in pandas._libs.parsers.TextReader._convert_column_data
  File "pandas\_libs\parsers.pyx", line 1072, in pandas._libs.parsers.TextReader._convert_tokens
  File "pandas\_libs\parsers.pyx", line 1172, in pandas._libs.parsers.TextReader._convert_with_dtype
  File "pandas\_libs\parsers.pyx", line 1731, in pandas._libs.parsers._try_int64
MemoryError: Unable to allocate 128. KiB for an array with shape (16384,) and data type int64

do you know how can i fix this error please?

Body:
As you can see, you don't have enough memory available for Pandas to load that file entirely into memory.

One reason is that based on Python38-32 in the traceback, you're running a 32-bit version of Python, where 4 gigabytes (or is it 3 gigabytes?) is the limit for memory allocations anyway. If your system is 64-bit, you should switch to the 64-bit version of Python, so that's one obstacle less.

If that doesn't help, you'll just also need more memory. You could configure Windows's virtual memory, or buy more actual memory and install it in your system.

If those don't help, then you'll have to come up with a better approach than to load the big CSV entirely into memory.

For one, if you really only care about rows with the string PERPIGNAN (no matter the column; you can really filter it again in your code), you could do grep PERPIGNAN data.csv > data_perpignan.csv and work with that (assuming you have grep; you can do the same filtering with a short Python script).

Since read_csv() accepts any iterable of lines, you can also just do something like

def lines_from_file_including_strings(file, strings):
    for i, line in enumerate(file):
        if i == 0 or any(string in line for string in strings):
            yield line

communes = ["PERPIGNAN", "PARIS"]

with open("StockEtablissement_utf8.csv") as f:
  df = pd.read_csv(lines_from_file_including_strings(f, communes), sep=",")

for an initial filter.


答:
Use-case for str.rstrip:

B = [item.rstrip('%') for item in A]
```

Title: Spliting integers from string in python

Body:
Suppose I have a numpy array like -

A = ['83.56%' '2.74%' '2.74%' '4.11%' '4.11%' '19.18%' '76.71%' '20.55%'
 '34.25%' '54.79%']

and I want to split this array as integers array only like -

B = ['83.56' '2.74' '2.74' '4.11' '4.11' '19.18' '76.71' '20.55'
 '34.25' '54.79']

How should I do it using Python codes ?


问:
How to define a function for ode in Scilab?
It shows that function f is not defined when executing u1 = ode

How can I define f correctly?

```<code>function xp,f(r,x,E)

    e = 3.795;
    m = 0.5111 * 10 ^ 6;
    h = 1973;
    xp(1) = x(2);
    xp(2) = (((2*m) / (h*h)) * (-((e*e/r) - E))) * x(1);

endfunction

E1 = input ("Enter the 1st guess for energy :");

E2 = input ("Enter the 2nd guess for energy :");

rmin = 0.01;

rmax = 20;

h = 0.01;

for i = 1:100

    r = rmin : h : rmax;
    u1 = ode([0.01 ; 1], rmin, r, list(f,E1));
    u2 = ode([0.01 ; 1], rmin, r, list(f,E2));

    E3 = (E1 + E2)/2;

    u3 = ode([0.01 ; 1], rmin, r, list(f,E3));
</code>```

答:
The definition of the function has to be fixed like this : output argument = f(input arguments)

```<code>function xp = f(r,x,E)

    e = 3.795;
    m = 0.5111 * 10 ^ 6;
    h = 1973;
    xp(1) = x(2);
    xp(2) = (((2*m) / (h*h)) * (-((e*e/r) - E))) * x(1);

endfunction
</code>```

and add an end to close your loop at the end. BW, the endfunction shows and the argument list for ode() show that you are using Scilab, not Octave (title and tags have been edited accordingly).

Body:
It shows that function f is not defined when executing u1 = ode

How can I define f correctly?

function xp,f(r,x,E)

    e = 3.795;
    m = 0.5111 * 10 ^ 6;
    h = 1973;
    xp(1) = x(2);
    xp(2) = (((2*m) / (h*h)) * (-((e*e/r) - E))) * x(1);

endfunction

E1 = input ("Enter the 1st guess for energy :");

E2 = input ("Enter the 2nd guess for energy :");

rmin = 0.01;

rmax = 20;

h = 0.01;

for i = 1:100

    r = rmin : h : rmax;
    u1 = ode([0.01 ; 1], rmin, r, list(f,E1));
    u2 = ode([0.01 ; 1], rmin, r, list(f,E2));

    E3 = (E1 + E2)/2;

    u3 = ode([0.01 ; 1], rmin, r, list(f,E3));

Body:
The definition of the function has to be fixed like this : output argument = f(input arguments)

function xp = f(r,x,E)

    e = 3.795;
    m = 0.5111 * 10 ^ 6;
    h = 1973;
    xp(1) = x(2);
    xp(2) = (((2*m) / (h*h)) * (-((e*e/r) - E))) * x(1);

endfunction

and add an end to close your loop at the end. BW, the endfunction shows and the argument list for ode() show that you are using Scilab, not Octave (title and tags have been edited accordingly).


问:
Flutter Vertical and Horizontal overflow issue
I'm trying to create a fully responsive design. I'm facing the overflow issue when I resize my screen from bottom to top(Vertically) or top to bottom in desktop view.  And I face horizontal responsiveness overflow from right and bottom sides, if I shrink the window size to much. There are two things 1rst I think the issue will be resolve with scrollbars, but I'm not able to add scrollbars. 2nd I would like to have a solution(or method, or widgets) for all type of responsiveness(horizontal and vertical) ,as soon as possible.

Normal Desktop View

Bottom Over Flow Issue when I resize Vertically

Normal Mobile View

Vertical and Horizontal Over Flow Issue after Resizing

Flutter Doctor

```<code>Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.10.0, on Microsoft Windows [Version 10.0.22000.318], locale en-PK)
[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Professional 2022 17.0.5)
[√] Android Studio (version 2021.1)
[√] VS Code (version 1.64.0)
[√] Connected device (3 available)
[√] HTTP Host Availability

• No issues found!
</code>```

Web_Screen Code

```<code>import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:g_res_p/colors/colors.dart';
import 'package:g_res_p/widgets/search.dart';
import 'package:g_res_p/widgets/webButtons/search_button1.dart';
import 'package:g_res_p/widgets/webButtons/translation_btn.dart';
import 'package:g_res_p/widgets/webButtons/upper_web_footer.dart';
import 'package:g_res_p/widgets/webButtons/web_footer.dart';

class WebScreenLayout extends StatelessWidget {
  const WebScreenLayout({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: backgroundColor,
        elevation: 0,
        actions: [
          TextButton(
            onPressed: () {},
            child: const Text(
              'Gmail',
              style: TextStyle(
                color: primaryColor,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          const SizedBox(
            width: 10,
          ),
          TextButton(
            onPressed: () {},
            child: const Text(
              'Images',
              style: TextStyle(
                color: primaryColor,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          const SizedBox(
            width: 10,
          ),
          IconButton(
            onPressed: () {},
            icon: SvgPicture.asset(
              'assets/images/more-apps.svg',
              color: primaryColor,
            ),
          ),
          const SizedBox(
            width: 10,
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 10.0).copyWith(
              right: 10,
            ),
            child: MaterialButton(
              onPressed: () {},
              color: const Color(
                0xff1A73EB,
              ),
              child: const Text(
                'Sign in',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.only(),
        child: Column(
          children: [
            SizedBox(
              height: size.height * 0.25,
            ),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    children: const [
                      Search(),
                      SizedBox(
                        height: 20,
                      ),
                      SearchButton1(),
                      SizedBox(
                        height: 20,
                      ),
                      TranslationButtons(),
                    ],
                  ),
                  Column(
                    children: const [
                      UpperWebFooter(),
                      Divider(
                        height: 1,
                        color: dividerColor,
                      ),
                      WebFooter(),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
</code>```

Mobile_Screen Code

```<code>import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:g_res_p/colors/colors.dart';
import 'package:g_res_p/widgets/mobile/mobiel_footer.dart';
import 'package:g_res_p/widgets/search.dart';
import 'package:g_res_p/widgets/webButtons/search_button1.dart';
import 'package:g_res_p/widgets/webButtons/translation_btn.dart';
import 'package:g_res_p/widgets/webButtons/upper_web_footer.dart';

class MobileScreenLayout extends StatelessWidget {
  const MobileScreenLayout({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: backgroundColor,
        elevation: 0,
        leading: IconButton(
          onPressed: () {},
          icon: const Icon(
            Icons.menu,
            color: Colors.grey,
          ),
        ),
        title: SizedBox(
          width: size.width * 0.34,
          child: const DefaultTabController(
            length: 2,
            child: TabBar(
              indicatorColor: blueColor,
              unselectedLabelColor: Colors.grey,
              labelColor: blueColor,
              indicatorSize: TabBarIndicatorSize.label,
              tabs: [
                Tab(
                  text: 'All',
                ),
                Tab(
                  text: 'Images',
                ),
              ],
            ),
          ),
        ),
        actions: [
          IconButton(
            onPressed: () {},
            icon: SvgPicture.asset(
              'assets/images/more-apps.svg',
              color: primaryColor,
            ),
          ),
          const SizedBox(
            width: 10,
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 10.0).copyWith(
              right: 10,
            ),
            child: MaterialButton(
              onPressed: () {},
              color: const Color(
                0xff1A73EB,
              ),
              child: const Text(
                'Sign in',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.only(),
        child: Column(
          children: [
            SizedBox(
              height: size.height * 0.25,
            ),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    children: const [
                      Search(),
                      SizedBox(
                        height: 20,
                      ),
                      SearchButton1(),
                      SizedBox(
                        height: 20,
                      ),
                      TranslationButtons(),
                    ],
                  ),
                  Column(
                    children: const [
                      UpperWebFooter(),
                      Divider(
                        height: 1,
                        color: dividerColor,
                      ),
                      MobileFooter(),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
</code>```

答:
use SingleChildScrollView:</p>
```<code>body: SingleChildScrollView(
        child:...
      )
</code>```

Body:
I'm trying to create a fully responsive design. I'm facing the overflow issue when I resize my screen from bottom to top(Vertically) or top to bottom in desktop view.  And I face horizontal responsiveness overflow from right and bottom sides, if I shrink the window size to much. There are two things 1rst I think the issue will be resolve with scrollbars, but I'm not able to add scrollbars. 2nd I would like to have a solution(or method, or widgets) for all type of responsiveness(horizontal and vertical) ,as soon as possible.

Normal Desktop View

Bottom Over Flow Issue when I resize Vertically

Normal Mobile View

Vertical and Horizontal Over Flow Issue after Resizing

Flutter Doctor

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.10.0, on Microsoft Windows [Version 10.0.22000.318], locale en-PK)
[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Professional 2022 17.0.5)
[√] Android Studio (version 2021.1)
[√] VS Code (version 1.64.0)
[√] Connected device (3 available)
[√] HTTP Host Availability

• No issues found!

Web_Screen Code

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:g_res_p/colors/colors.dart';
import 'package:g_res_p/widgets/search.dart';
import 'package:g_res_p/widgets/webButtons/search_button1.dart';
import 'package:g_res_p/widgets/webButtons/translation_btn.dart';
import 'package:g_res_p/widgets/webButtons/upper_web_footer.dart';
import 'package:g_res_p/widgets/webButtons/web_footer.dart';

class WebScreenLayout extends StatelessWidget {
  const WebScreenLayout({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: backgroundColor,
        elevation: 0,
        actions: [
          TextButton(
            onPressed: () {},
            child: const Text(
              'Gmail',
              style: TextStyle(
                color: primaryColor,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          const SizedBox(
            width: 10,
          ),
          TextButton(
            onPressed: () {},
            child: const Text(
              'Images',
              style: TextStyle(
                color: primaryColor,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          const SizedBox(
            width: 10,
          ),
          IconButton(
            onPressed: () {},
            icon: SvgPicture.asset(
              'assets/images/more-apps.svg',
              color: primaryColor,
            ),
          ),
          const SizedBox(
            width: 10,
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 10.0).copyWith(
              right: 10,
            ),
            child: MaterialButton(
              onPressed: () {},
              color: const Color(
                0xff1A73EB,
              ),
              child: const Text(
                'Sign in',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.only(),
        child: Column(
          children: [
            SizedBox(
              height: size.height * 0.25,
            ),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    children: const [
                      Search(),
                      SizedBox(
                        height: 20,
                      ),
                      SearchButton1(),
                      SizedBox(
                        height: 20,
                      ),
                      TranslationButtons(),
                    ],
                  ),
                  Column(
                    children: const [
                      UpperWebFooter(),
                      Divider(
                        height: 1,
                        color: dividerColor,
                      ),
                      WebFooter(),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Mobile_Screen Code

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:g_res_p/colors/colors.dart';
import 'package:g_res_p/widgets/mobile/mobiel_footer.dart';
import 'package:g_res_p/widgets/search.dart';
import 'package:g_res_p/widgets/webButtons/search_button1.dart';
import 'package:g_res_p/widgets/webButtons/translation_btn.dart';
import 'package:g_res_p/widgets/webButtons/upper_web_footer.dart';

class MobileScreenLayout extends StatelessWidget {
  const MobileScreenLayout({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: backgroundColor,
        elevation: 0,
        leading: IconButton(
          onPressed: () {},
          icon: const Icon(
            Icons.menu,
            color: Colors.grey,
          ),
        ),
        title: SizedBox(
          width: size.width * 0.34,
          child: const DefaultTabController(
            length: 2,
            child: TabBar(
              indicatorColor: blueColor,
              unselectedLabelColor: Colors.grey,
              labelColor: blueColor,
              indicatorSize: TabBarIndicatorSize.label,
              tabs: [
                Tab(
                  text: 'All',
                ),
                Tab(
                  text: 'Images',
                ),
              ],
            ),
          ),
        ),
        actions: [
          IconButton(
            onPressed: () {},
            icon: SvgPicture.asset(
              'assets/images/more-apps.svg',
              color: primaryColor,
            ),
          ),
          const SizedBox(
            width: 10,
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 10.0).copyWith(
              right: 10,
            ),
            child: MaterialButton(
              onPressed: () {},
              color: const Color(
                0xff1A73EB,
              ),
              child: const Text(
                'Sign in',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.only(),
        child: Column(
          children: [
            SizedBox(
              height: size.height * 0.25,
            ),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    children: const [
                      Search(),
                      SizedBox(
                        height: 20,
                      ),
                      SearchButton1(),
                      SizedBox(
                        height: 20,
                      ),
                      TranslationButtons(),
                    ],
                  ),
                  Column(
                    children: const [
                      UpperWebFooter(),
                      Divider(
                        height: 1,
                        color: dividerColor,
                      ),
                      MobileFooter(),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Body:
use SingleChildScrollView:

body: SingleChildScrollView(
        child:...
      )


问:
Is it possible to tell if a java ResultSet is empty without affecting the record?
I do not want to use next() since if the ResultSet is not empty, I do not want the cursor to be advanced. Is there some sort of peek() method or equivalent that will return false if the ResultSet is empty and true otherwise without affecting the record?

Edit:

I cannot call beforeFirst because I get the following

Operation requires a scrollable ResultSet, but this ResultSet is
  FORWARD_ONLY

I need FORWARD_ONLY because I'm using postgreSQL cursors

答:
isBeforeFirst() returns true if the cursor is before the first row or false if it is in any other position or the resultset is empty.

Check out the Java API for ResultSet for isBeforeFirst()


问:
How to obtain twitter follow count history
I want an API, or a way to obtain the Twitter follower count, of several twitter counts, for example, Twitter follower count of several Twitter accounts, from 1st September 2021 to today(9th February 2022),whether paid or not, if anyone can help me.</p>

答: If you're willing to pay, the Social Blade API could be worth looking into. Social Blade has historical Twitter stats stored; you can search their website to see if the accounts that you're looking for are there. You could also scrape their website in Python with methods like this.</p>


问:
Drop a layer in raster stack
I have issue using drop layer/subset function to remove raster layer from raster stack that have min and max value==0. Please help me correcting this syntax.

Ls_AOI_3_r_sel_stk_v1 <- dropLayer(Ls_AOI_3_r_sel_stk_crop, (maxValue(Ls_AOI_3_r_sel_stk_crop)==0) & (maxValue(Ls_AOI_3_r_sel_stk_crop)==0))
```

答:
Please find below one possible solution using the terra library (which is an improved version of the raster library)

Reprex

1 - Creating a dummy raster r of class SpatRaster

library(terra)

A <- rast( nrows=10, ncols=10, xmin=0, xmax=10 )
B <- rast( nrows=10, ncols=10, xmin=0, xmax=10 )
C <- rast( nrows=10, ncols=10, xmin=0, xmax=10 )

values(A) <- 1:100
values(B) <- 0:99
values(C) <- rep(0,100)

r <- rast(list(A,B,C))

names(r) <- c("red","green","blue")

# To what looks like 'r'
r
#> class       : SpatRaster
#> dimensions  : 10, 10, 3  (nrow, ncol, nlyr)
#> resolution  : 1, 18  (x, y)
#> extent      : 0, 10, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> sources     : memory
#>               memory
#>               memory
#> names       : red, green, blue
#> min values  :   1,     0,    0
#> max values  : 100,    99,    0
```
<ul>
<li>2 - Code to select layers where min and max are different from 0</li>
</ul>
<pre class="lang-r prettyprint-override"><code>r[[minmax(r)[1,] != 0 & minmax(r)[2,] != 0]]
</code>```

3 - Output (only the "red" layer has been selected)

#> class       : SpatRaster
#> dimensions  : 10, 10, 1  (nrow, ncol, nlyr)
#> resolution  : 1, 18  (x, y)
#> extent      : 0, 10, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> source      : memory
#> name        : red
#> min value   :   1
#> max value   : 100
```

Created on 2022-02-09 by the reprex package (v2.0.1)

Title: Drop a layer in raster stack

Body:
Please find below one possible solution using the terra library (which is an improved version of the raster library)

Reprex

1 - Creating a dummy raster r of class SpatRaster

library(terra)

A <- rast( nrows=10, ncols=10, xmin=0, xmax=10 )
B <- rast( nrows=10, ncols=10, xmin=0, xmax=10 )
C <- rast( nrows=10, ncols=10, xmin=0, xmax=10 )

values(A) <- 1:100
values(B) <- 0:99
values(C) <- rep(0,100)

r <- rast(list(A,B,C))

names(r) <- c("red","green","blue")

# To what looks like 'r'
r
#> class       : SpatRaster
#> dimensions  : 10, 10, 3  (nrow, ncol, nlyr)
#> resolution  : 1, 18  (x, y)
#> extent      : 0, 10, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> sources     : memory
#>               memory
#>               memory
#> names       : red, green, blue
#> min values  :   1,     0,    0
#> max values  : 100,    99,    0

2 - Code to select layers where min and max are different from 0

r[[minmax(r)[1,] != 0 & minmax(r)[2,] != 0]]

3 - Output (only the "red" layer has been selected)

#> class       : SpatRaster
#> dimensions  : 10, 10, 1  (nrow, ncol, nlyr)
#> resolution  : 1, 18  (x, y)
#> extent      : 0, 10, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> source      : memory
#> name        : red
#> min value   :   1
#> max value   : 100

Created on 2022-02-09 by the reprex package (v2.0.1)


问:
UnexpectedTagNameException: Element should have been "select" but was "a" while trying to get the texts of dropdown menu using Selenium and Java
While trying to get the menu list, I'm getting this error message:

```<code>Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "a".
</code>```

Here below is the code:

```<code>public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "D:\\selenium files\\chromedriver_win32_new\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.tutorialspoint.com/tutor_connect/index.php");

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        WebElement ele = driver.findElement(By.xpath("//*[@id=\"logo-menu\"]/div/div[1]/div/a"));

        Select s = new Select(ele);

    //getting list of menu
         List <WebElement> op = s.getOptions();
          int size = op.size();
          for(int i =0; i<size ; i++){
             String options = op.get(i).getText();
             System.out.println(options);
          }
    }
}
</code>```

答:
That is because the element you are trying to cast is a link tag and not a select tag.
You need to give the Xpath or CSS of the correct Select element and then cast it from WebElement into a Select ojbect.

In the example you are using there is not real selector, you first need to click on the buttons that says "Categories" and later take the options that appear:

```<code>WebElement button = driver.findElementByCSS("div[class='mui-dropdown']");
button.click();
WebElement SelectObj = driver.findElementByCSS("ul[class*='mui--is-open']");
Select s = new Select(SelectObj);
</code>```

Body:
While trying to get the menu list, I'm getting this error message:

Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "a".

Here below is the code:

public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "D:\\selenium files\\chromedriver_win32_new\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.tutorialspoint.com/tutor_connect/index.php");

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        WebElement ele = driver.findElement(By.xpath("//*[@id=\"logo-menu\"]/div/div[1]/div/a"));

        Select s = new Select(ele);

    //getting list of menu
         List <WebElement> op = s.getOptions();
          int size = op.size();
          for(int i =0; i<size ; i++){
             String options = op.get(i).getText();
             System.out.println(options);
          }
    }
}

Body:
That is because the element you are trying to cast is a link tag and not a select tag.
You need to give the Xpath or CSS of the correct Select element and then cast it from WebElement into a Select ojbect.

In the example you are using there is not real selector, you first need to click on the buttons that says "Categories" and later take the options that appear:

WebElement button = driver.findElementByCSS("div[class='mui-dropdown']");
button.click();
WebElement SelectObj = driver.findElementByCSS("ul[class*='mui--is-open']");
Select s = new Select(SelectObj);


问:
How to disable scrolling outside of polygon in react leaflet
I created a simple map in the react project with the help of the leaflet library.
Now I need to make the map be focused on one specific country so it won't be possible to scroll the map outside its borders.

So far, I have found solutions that apply only to a standard Java Script / HTML project and not something that integrates with the leaflet library for react.

Also, even these solutions only need to define an area where only the map is loaded and the other tiles are not loaded, and as can be understood from the description at the beginning of my question, This is not exactly what I need and I realized there is another option.

(However, if you are familiar with another option to make the map be focused on a specific country in some way, even if it doesn't exactly do what I described, feel free to list below. I'd be happy for any advice)

Anyone have an idea?

Here is my code:

```<code>import './App.css';
import { MapContainer, TileLayer} from 'react-leaflet';
import 'leaflet/dist/leaflet.css';

function App() {
  return (
    <div>
       <MapContainer
        style={{ height: "400vh" }}
        center={[51.505, -0.09]}
        zoom={13}
        scrollWheelZoom={false}
      >
        <TileLayer
          attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        />
      </MapContainer>
    </div>
  );
}

export default App;
</code>```

答: To load tiles only in a defined area you can use the bounds option on the TileLayer. And to restrict the "scroll" / moving outside of the area you can use the maxBounds option on the map.

Body:
I created a simple map in the react project with the help of the leaflet library.
Now I need to make the map be focused on one specific country so it won't be possible to scroll the map outside its borders.

So far, I have found solutions that apply only to a standard Java Script / HTML project and not something that integrates with the leaflet library for react.

Also, even these solutions only need to define an area where only the map is loaded and the other tiles are not loaded, and as can be understood from the description at the beginning of my question, This is not exactly what I need and I realized there is another option.

(However, if you are familiar with another option to make the map be focused on a specific country in some way, even if it doesn't exactly do what I described, feel free to list below. I'd be happy for any advice)

Anyone have an idea?

Here is my code:

import './App.css';
import { MapContainer, TileLayer} from 'react-leaflet';
import 'leaflet/dist/leaflet.css';

function App() {
  return (

       <MapContainer
        style={{ height: "400vh" }}
        center={[51.505, -0.09]}
        zoom={13}
        scrollWheelZoom={false}
      >
        <TileLayer
          attribution='© OpenStreetMap contributors'
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        />
      </MapContainer>

  );
}

export default App;


问:
Group by null in MariaDB

I have a table with data like the one below.

```<code>**  ID  TYP       NAME      END**
     1  *null*  ADEPLAST    S.R.L.
     2  *null*  ADEPLAST    S.A.
     3   S.C.   ADEPLAST    S.R.L.
     4   A.B.   ADEPLAST    S.R.L.
</code>```

Is there a way in MariaDB to group it so that i get a result like:

```<code>**  ID  TYP       NAME      END**
    1,3 S.C.   ADEPLAST    S.R.L.
    2  *null*  ADEPLAST    S.A.
    4   A.B.   ADEPLAST    S.R.L.
</code>```

I have tried the classical GROUP BY, but as i have read somewhere, the NULL value is a value in itself so that won't work

Thank you :)

答:
DBFIDDLE</p>
```<code>SELECT
   GROUP_CONCAT(ID) as `ID`,
   `TYP`,
   `NAME`,
   `END`
FROM (
   -- this will find the TYP when it is NULL, and the same name with
   -- a TYPE exists
   SELECT
      ID,
      CASE WHEN `TYP` IS NULL THEN (SELECT TYP
                                  FROM table1 t1
                                  WHERE t1.name=t0.name
                                    and t1.`END`=t0.`END`
                                    and NOT t1.TYP IS NULL
                                    LIMIT 1)
                            ELSE TYP END as `TYP`,
      Name,
      `END`
   FROM table1 t0

) x
GROUP BY `TYP`,`Name`,`END`
ORDER BY `ID`
</code>```

Title: Group by null in MariaDB

Body:
I have a table with data like the one below.

**  ID  TYP       NAME      END**
     1  *null*  ADEPLAST    S.R.L.
     2  *null*  ADEPLAST    S.A.
     3   S.C.   ADEPLAST    S.R.L.
     4   A.B.   ADEPLAST    S.R.L.

Is there a way in MariaDB to group it so that i get a result like:

**  ID  TYP       NAME      END**
    1,3 S.C.   ADEPLAST    S.R.L.
    2  *null*  ADEPLAST    S.A.
    4   A.B.   ADEPLAST    S.R.L.

I have tried the classical GROUP BY, but as i have read somewhere, the NULL value is a value in itself so that won't work

Thank you :)

Body:
DBFIDDLE

SELECT
   GROUP_CONCAT(ID) as `ID`,
   `TYP`,
   `NAME`,
   `END`
FROM (
   -- this will find the TYP when it is NULL, and the same name with
   -- a TYPE exists
   SELECT
      ID,
      CASE WHEN `TYP` IS NULL THEN (SELECT TYP
                                  FROM table1 t1
                                  WHERE t1.name=t0.name
                                    and t1.`END`=t0.`END`
                                    and NOT t1.TYP IS NULL
                                    LIMIT 1)
                            ELSE TYP END as `TYP`,
      Name,
      `END`
   FROM table1 t0

) x
GROUP BY `TYP`,`Name`,`END`
ORDER BY `ID`


问:
How fast is mprotect
My question is how fast is mprotect. What will be the difference between mprotecting say 1 MB of contiguous memory as compared to 1 GB of contiguous memory? Of course I can measure the time, but I want to know what goes under the hood.

答:
A quick check on the source seems to indicate that it iterates over the process mappings in the selected region and change their flags. If you protect less than a whole mapping it'll split it into two or three.

So in short it's O(n) where n is the number of times you've called mmap.

You can see all the current maps in /proc/pid/maps

Title: How fast is mprotect


问:
How to cancel valueChanges in Angular and set the old value again?
I subscribed to valueChanges of a mat-selection-list to be notified when the user selects a new list item. In some cases I ask the user if he/she really wants to select the new item via a confirmation dialog. If the user clicks "No" in this dialog, I need to cancel the selection of the new item. This is how I attempted to do this:

```<code>this.createForm.controls.selectionList.valueChanges.subscribe((value: number[]) => {
  const prevValue = this.createForm.controls.selectionList.value;

  if (<condition>) {
    const confirmDialogRef = this.dialog.open(ConfirmationDialogComponent, {
      data: "Do you really want to do this?",
    });

    confirmDialogRef.afterClosed().subscribe((confirmResult) => {
      if (confirmResult === true) {
        // User clicked yes, continue...
      } else {
        // User clicked no, set the previous value again...
        this.createForm.controls.selectionList.setValue(prevValue, { emitEvent: false });
      }
    });
  } else {
    // No user confirmation required, just continue...
  }
});
</code>```

Unfortunately, this does not seem to work because prevValue seems to already be the new value when I try to set it as current value. What can I do instead to set the previous value again and basically just pretend that the user never even selected a new list item to begin with? Do I really have to cache the previous value manually, or is there a better way to do this?

答:
You subscribe() to a form value change - why? Where is the unsubscribe() for this?

If you try to get prevValue inside the subscribe() then it's true, that this.createForm.controls.selectionList.value is already the new value.

I would bind the data and then use ngOnChanges to read previous and current value. We could then place the if (<condition>) block into ngOnChanges. We also then can get rid of the subscription.

If possible I would call Event.stopPropagation() to cancel value change event or reassign selectionList.value to be previousValue from simpleChange.

That or use pairwise and just use the previous value.

observable // [1, 5, 2, 3]
  .pipe(
    pairwise() // [[1, 5], [5, 2], [2, 3]]
  )
```

Body:
I subscribed to valueChanges of a mat-selection-list to be notified when the user selects a new list item. In some cases I ask the user if he/she really wants to select the new item via a confirmation dialog. If the user clicks "No" in this dialog, I need to cancel the selection of the new item. This is how I attempted to do this:

this.createForm.controls.selectionList.valueChanges.subscribe((value: number[]) => {
  const prevValue = this.createForm.controls.selectionList.value;

  if (<condition>) {
    const confirmDialogRef = this.dialog.open(ConfirmationDialogComponent, {
      data: "Do you really want to do this?",
    });

    confirmDialogRef.afterClosed().subscribe((confirmResult) => {
      if (confirmResult === true) {
        // User clicked yes, continue...
      } else {
        // User clicked no, set the previous value again...
        this.createForm.controls.selectionList.setValue(prevValue, { emitEvent: false });
      }
    });
  } else {
    // No user confirmation required, just continue...
  }
});

Unfortunately, this does not seem to work because prevValue seems to already be the new value when I try to set it as current value. What can I do instead to set the previous value again and basically just pretend that the user never even selected a new list item to begin with? Do I really have to cache the previous value manually, or is there a better way to do this?


问:
Clarification on overloading
When the parent class have an add method with 2 parameters, If we add new add method with 3 parameters in child class, shall we call this as over loading?

Thanks in advance.

答:
Yes, this is overloading. It would be overloading even if the method were in the same class as the method with two parameters.

Note that when there are different numbers of parameters (and no varargs parameters) overloading is reasonably simple. It gets a lot more complicated when you have methods with the same number of parameters - at that point the compiler has to choose the "best" method out of applicable candidate methods.

Also note that overloading is determined at compile time whereas which override is executed is determined at execution time based on the actual type of the object the method is called on.

Title: Clarification on overloading


问:
vim substitution expand to uppercase
I insert the name of my file into its skeleton when i open it with this command into my ~/.vimrc file :

```<code>function! HPPFile()
    silent! 0r $HOME/.vim/templates/skeleton.hpp
    %s/FileName/\=expand("%:t:r")/g
    %s/FILENAME/\U\=expand("%:t:r")\E/g
endfunction
autocmd BufNewFile *.hpp call HPPFile()
</code>```

the first expansion line works well %s/FileName/\=expand("%:t:r")/g : all instances of FileName get replaced by the file name

but the second expansion line doesn't works %s/FILENAME/\U\=expand("%:t:r")\E/g -> it seems i cannot use both the uppercase substitution and the expand action

I've tried some variations but without chance, how could i do that ?

答:
From :help sub-replace-\=:

```<code>When the substitute string starts with "\=" the remainder is interpreted as an
expression.
</code>```

You can use :help substitute() to uppercase your filename:

```<code>function! HPPFile()
    silent! 0r $HOME/.vim/templates/skeleton.hpp
    %s/FileName/\=expand("%:t:r")/g
    %s/FILENAME/\=substitute(expand("%:t:r"),'.*','\U&','')/g
endfunction
</code>```

Title: vim substitution expand to uppercase

Body:
I insert the name of my file into its skeleton when i open it with this command into my ~/.vimrc file :

function! HPPFile()
    silent! 0r $HOME/.vim/templates/skeleton.hpp
    %s/FileName/\=expand("%:t:r")/g
    %s/FILENAME/\U\=expand("%:t:r")\E/g
endfunction
autocmd BufNewFile *.hpp call HPPFile()

the first expansion line works well %s/FileName/\=expand("%:t:r")/g : all instances of FileName get replaced by the file name

but the second expansion line doesn't works %s/FILENAME/\U\=expand("%:t:r")\E/g -> it seems i cannot use both the uppercase substitution and the expand action

I've tried some variations but without chance, how could i do that ?

Body:
From :help sub-replace-\=:

When the substitute string starts with "\=" the remainder is interpreted as an
expression.

You can use :help substitute() to uppercase your filename:

function! HPPFile()
    silent! 0r $HOME/.vim/templates/skeleton.hpp
    %s/FileName/\=expand("%:t:r")/g
    %s/FILENAME/\=substitute(expand("%:t:r"),'.*','\U&','')/g
endfunction