问:
c++ vectors and pointers
As i understand if i dont store pointers, everything in c++ gets copied, which can lead to bad performance (ignore the simplicity of my example). So i thought i store my objects as pointers instead of string object inside my vector, thats better for performance right? (assumining i have very long strings and lots of them).

The problem when i try to iterate over my vector of string pointers is i cant extract the actual value from them

```<code>string test = "my-name";
vector<string*> names(20);
names.push_back(&test);

vector<string*>::iterator iterator = names.begin();
while (iterator != names.end())
{
    std::cout << (*iterator) << ":" << std::endl;
    // std::cout << *(*iterator); // fails

    iterator++;
}
</code>```

See the commented line, i have no problem in receiving the string pointer. But when i try to get the string pointers value i get an error (i couldnt find what excatly the error is but the program just fails).

I also tried storing (iterator) in a new string variable and but it didnt help?

答:
```<code>string test = "my-name";
vector<string*> names(20);
</code>```

The size of vector is 20, meaning it can hold 20 string pointers.

```<code>names.push_back(&test);
</code>```

With the push_back operation, you are leaving out the first 20 elements and adding a new element to the vector which holds the address of test. First 20 elements of vector are uninitialized and might be pointing to garbage. And the while loop runs till the end of vector whose size is 21 and dereferencing uninitialized pointers is what causing the problem. Since the size of vector can be dynamically increased with a push_back operation, there is no need to explicitly mention the size.

```<code>vector<string*> names; // Not explicitly mentioning the size and the rest of
                       // the program should work as expected.
</code>```

Title: c++ vectors and pointers

Body:
As i understand if i dont store pointers, everything in c++ gets copied, which can lead to bad performance (ignore the simplicity of my example). So i thought i store my objects as pointers instead of string object inside my vector, thats better for performance right? (assumining i have very long strings and lots of them).

The problem when i try to iterate over my vector of string pointers is i cant extract the actual value from them

string test = "my-name";
vector<string*> names(20);
names.push_back(&test);

vector<string*>::iterator iterator = names.begin();
while (iterator != names.end())
{
    std::cout << (*iterator) << ":" << std::endl;
    // std::cout << *(*iterator); // fails

    iterator++;
}

See the commented line, i have no problem in receiving the string pointer. But when i try to get the string pointers value i get an error (i couldnt find what excatly the error is but the program just fails).

I also tried storing (iterator) in a new string variable and but it didnt help?

Body:
string test = "my-name";
vector<string*> names(20);

The size of vector is 20, meaning it can hold 20 string pointers.

names.push_back(&test);

With the push_back operation, you are leaving out the first 20 elements and adding a new element to the vector which holds the address of test. First 20 elements of vector are uninitialized and might be pointing to garbage. And the while loop runs till the end of vector whose size is 21 and dereferencing uninitialized pointers is what causing the problem. Since the size of vector can be dynamically increased with a push_back operation, there is no need to explicitly mention the size.

vector<string*> names; // Not explicitly mentioning the size and the rest of
                       // the program should work as expected.


来源: stackexchange

问:
Spring Boot Micro Service Tracing Options
I am having below requirement for which is there any open source library will cover all of them.</p>

```<code>1.We are building a distributed micro service architecture with Spring Boot.Which includes more than 100 micro services.
2.There is a lot if inter micro service communications possible to achieve single transaction.
3.We want to trace every micro service call and the trace should provide following information.
 a.Transaction ID/Trace ID
 b. Back end transaction status-HTTP status for REST.Like wise for SOAP as well.
 c.Time taken for that call.
 d.Request and Response payload.
Currently we are achieving this using indigenous tracing frame work.Is there any open source project will handle all this without any coding from developer.I know we have few options with spring Boot Cloud Zipkin,Seluth etc does this handle above requirements.
</code>```

答:
My project has similar requirements to yours. IMHO, Spring-cloud-sleuth + Zipkin work well in my case.

For any inter microservices communication, we are using Kafka, and Spring-cloud-sleuth + zipkin has no problem to trace all the call, from REST -> Kafka -> More Kafka -> REST.

To enable Kafka Tracing, just simply add

```<code>spring:
  sleuth:
    propagation-keys: some-key
    sampler:
      probability: 1
    messaging:
      kafka:
        enabled: true
</code>```

We are also using Azure ApplicationInsights to do centralized logging, which is well integrated with Spring Cloud.

Hope above give you some confidence of using Sleuth + Zipkin.

ContentLicense: CC BY-SA 3.0

Body:
I am having below requirement for which is there any open source library will cover all of them.

1.We are building a distributed micro service architecture with Spring Boot.Which includes more than 100 micro services.
2.There is a lot if inter micro service communications possible to achieve single transaction.
3.We want to trace every micro service call and the trace should provide following information.
 a.Transaction ID/Trace ID
 b. Back end transaction status-HTTP status for REST.Like wise for SOAP as well.
 c.Time taken for that call.
 d.Request and Response payload.
Currently we are achieving this using indigenous tracing frame work.Is there any open source project will handle all this without any coding from developer.I know we have few options with spring Boot Cloud Zipkin,Seluth etc does this handle above requirements.

Body:
My project has similar requirements to yours. IMHO, Spring-cloud-sleuth + Zipkin work well in my case.

For any inter microservices communication, we are using Kafka, and Spring-cloud-sleuth + zipkin has no problem to trace all the call, from REST -> Kafka -> More Kafka -> REST.

To enable Kafka Tracing, just simply add

spring:
  sleuth:
    propagation-keys: some-key
    sampler:
      probability: 1
    messaging:
      kafka:
        enabled: true

We are also using Azure ApplicationInsights to do centralized logging, which is well integrated with Spring Cloud.

Hope above give you some confidence of using Sleuth + Zipkin.

ContentLicense: CC BY-SA 4.0


问:
How can i check that demo data is loaded or not in a database by code
How can i check that demo data is loaded or not in a database from python code.I know other ways to check , but i need to check it by code.</p>

答:
I think the easiest way to do is by checking if the XML-ID is loaded :

         # checking if the demo user record exist in database
         # don't forget the put the full qualifying XML-ID (addon_name.xml_id)
         if not self.env.ref('base.user_demo', raise_if_not_found=False):
               # demo user is not loaded in database
```

Odoo maps the XML-ID with the real ID in database using this model ir.model.data if the record is loaded ref will return the record from the database else it will raise an exception or return None based on the second argument raise_if_not_found by default it's True

Body:
I think the easiest way to do is by checking if the XML-ID is loaded :

         # checking if the demo user record exist in database
         # don't forget the put the full qualifying XML-ID (addon_name.xml_id)
         if not self.env.ref('base.user_demo', raise_if_not_found=False):
               # demo user is not loaded in database

Odoo maps the XML-ID with the real ID in database using this model ir.model.data if the record is loaded ref will return the record from the database else it will raise an exception or return None based on the second argument raise_if_not_found by default it's True


问:
Differentiating between an Activity launch from home screen or from another activity from App
I need to know a generic way to distinguish between a call of activity from launcher and a call from another activity from inside my app, or a BACK on the activity stack

Anyone? this is bugging me for quite a while now and i need to put it to rest...

Thanks in advance
JQCorreia

答:
In addition to @advantej's answer, you can extend each start-call to that activity adding an extra to the starting intent (e.g. intent.putExtra("caller", this.getClass().getSimpleName());

In the activity's onCreate method you can check then what @advantej suggests.

If the initiator is not the home-screen icon, than you can check further if the intent.hasExtra("caller") returns true, and if so, what is it.


问:
Is there an R function to extract images based on a unique date and time stamp?
I have a set of photos from camera traps with multiple images of identical date and time stamps. I need to extract one photo for each timestamp in the set. I need every photo where there is only one photo with that date and time stamp. Then for any multiple date and time stamps I must always take only the second photo. Eg. One photo, X, timestamp 13:22:42, I want photo X; three photos, a, b and c, have timestamp 13:22:43, I want b. Seven photos, 1, 2, 3, 4, 5, 6 and 7 have timestamp 13:22:44, I want 2.

I am completely new to R, but thought it sounded like the kind of thing it might be able to do! I have tried googling options, but not much luck.

I am hoping I can either rename all the photos I want to easily copy and paste them in to a new album, or ideally have R extract them into a new folder for me. Is this possible?

答:
If you're trying to use the system "Created At" timestamp, this is one way to do it. First, we get a list of the file names. Then we use file.info to get the "Created At" time. We then group by timestamp, and create a grouping id with row_number. We then filter to the second image id == 2 where max(id) >= 2, otherwise if there's only one image we use id == 1. Then we can create a new folder and copy the selected images there.

```<code>library(tidyverse)

setwd(choose.dir())

# select the folder with your images in it

files <- list.files()

keep <- files %>% file.info() %>%
  select(ctime) %>%
  rownames_to_column("file") %>%
  group_by(ctime) %>%
  mutate(id = row_number()) %>%
  filter(id == min(max(id),2)) %>%
  pull(file)

# at this point `keep` is a list of file names meeting the specified criteria

dir.create("Keep")

file.copy(keep, paste0("./Keep/", keep))
</code>```

Body:
If you're trying to use the system "Created At" timestamp, this is one way to do it. First, we get a list of the file names. Then we use file.info to get the "Created At" time. We then group by timestamp, and create a grouping id with row_number. We then filter to the second image id == 2 where max(id) >= 2, otherwise if there's only one image we use id == 1. Then we can create a new folder and copy the selected images there.

library(tidyverse)

setwd(choose.dir())

# select the folder with your images in it

files <- list.files()

keep <- files %>% file.info() %>%
  select(ctime) %>%
  rownames_to_column("file") %>%
  group_by(ctime) %>%
  mutate(id = row_number()) %>%
  filter(id == min(max(id),2)) %>%
  pull(file)

# at this point `keep` is a list of file names meeting the specified criteria

dir.create("Keep")

file.copy(keep, paste0("./Keep/", keep))


问:
Create a service worker from a foreign origin
I'm building a web push notification system that can be integrated on any website. I control these sites, but I'm trying to make the integration as simple as possible so it can be done by someone other than me. Ideally, I could just give someone a single JavaScript or iframe tag to drop in and it would just work.

```<code><script src="https://foreign.com/main.js" />
</code>```

-- or --

```<code><iframe src="https://foreign.com/main.html" />
</code>```

I first tried to create a service worker from a foreign script - that threw an exception and I've since read up on why. I understand why this doesn't work.

```<code>  navigator.serviceWorker.register('https://foreign.com/sw.js')
</code>```

So I tried creating a very simple page that just runs the JavaScript to setup the service worker, and calling that from an iframe. This does appear to work - it asks for permission and creates the subscription. But if I try to send a message to that subscription, nothing happens. It just silently fails. Why is this?

答:
That cannot be done, it'll be a security issue, not in the Specs.
SW has to be downloaded from same origin, same path and https</p>


问:
ClosedChannelException exception when using Ehcache
I am having a problem with Ehcache. It usually works fine but occasionally I get a ClosedChannelException when try to access the cache, and once that occurs I can only get round the problem by recreating the cache

Here is my class

```<code>package com.jthink.songkong.cache;

    import com.jthink.acoustid.acoustidschema.Result;
    import com.jthink.songkong.analyse.general.Errors;
    import org.ehcache.CachePersistenceException;
    import org.ehcache.config.builders.CacheConfigurationBuilder;
    import org.ehcache.config.builders.ExpiryPolicyBuilder;
    import org.ehcache.config.builders.ResourcePoolsBuilder;
    import org.ehcache.config.units.EntryUnit;
    import org.ehcache.config.units.MemoryUnit;
    import org.ehcache.core.statistics.CacheStatistics;

    import java.time.Duration;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;

    /**
     * AcoustIdResult EhCache
     *
     */
    public class AcoustIdResultCache
    {
        protected org.ehcache.Cache<String, Result>           cache;

        public static final String ACOUSTID_RESULT_CACHE = "AcoustidResultCache";

        private static AcoustIdResultCache apc;

        private AcoustIdResultCache()
        {
           createCache();
        }

        static
        {
            apc = new AcoustIdResultCache();
        }

        public static AcoustIdResultCache getInstanceOf()
        {
            if(apc==null)
            {
                apc = new AcoustIdResultCache();
            }
            return apc;
        }

        /**
         * Create the Cache
         */
        private void createCache()
        {
            cache = SongKongCacheManager.getCacheManager().createCache(getCacheName(),
                    CacheConfigurationBuilder.newCacheConfigurationBuilder(
                            String.class,
                            Result.class,
                            ResourcePoolsBuilder.newResourcePoolsBuilder()
                                    .heap(300, EntryUnit.ENTRIES)
                                    .disk(500, MemoryUnit.MB, true)
                    )
                            .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofDays(10)) //Since Acoustid is Live Db
                            )
            );
        }

        protected AcoustIdResultCache(String cacheName)
        {
            createCache();
        }

        public void init()
        {

        }

        /**
         * Close Cache Manager
         */
        public static void close()
        {
            SongKongCacheManager.getCacheManager().close();
        }

        public void emptyCache()
        {
            try
            {
                SongKongCacheManager.getCacheManager().destroyCache(getCacheName());
                createCache();
            }
            catch(CachePersistenceException cpe)
            {
                Errors.addError("Problem emptying cache for"+getCacheName()+":"+cpe.getMessage(), cpe);
            }
        }

        public void clearCacheStatistics()
        {
            SongKongCacheManager.getStatistics().getCacheStatistics(getCacheName()).clear();
        }

        /**
         *
         * @return cache name
         */
        protected String getCacheName()
        {
            return ACOUSTID_RESULT_CACHE;
        }

        /**
         *
         * @return cache
         */

        protected org.ehcache.Cache<String, Result> getCache()
        {
            return cache;
        }

        /**
         *
         * @return statistics of cache
         */
        public String statusCache()
        {
            CacheStatistics ehCacheStat = SongKongCacheManager.getStatistics().getCacheStatistics(getCacheName());
            return "CacheName:" + getCacheName()
                    + ":Put:"   + ehCacheStat.getCachePuts()
                    + ":Hit:"   + ehCacheStat.getCacheHits()
                    + ":Miss:"  + ehCacheStat.getCacheMisses()
                    + ":Evictions:"  + ehCacheStat.getCacheEvictions()
                    + ":Removals:"  + ehCacheStat.getCacheRemovals();
        }

        /**
         * Add an item to cache
         *
         * @param item
         */
        public void add(Result item)
        {
            addToCache(item);
        }

        /**
         * Get Item from cache
         *
         * @param id
         * @return the item or null
         */
        public  Result get(String id)
        {
            return getFromCache(id);
        }

        /**
         * Is there an item with this id in cache
         * @param id
         * @return
         */
        public boolean isInCache(String id)
        {
            return getCache().containsKey(id);
        }

        /**
         *
         * @param id
         * @return item for this id or null if doesn't not exist
         */
        protected   Result getFromCache(String id)
        {
            return getCache().get(id);
        }

        /**
         * For adding multiple items to cache
         *
         * @param items
         * @return
         */
        public  boolean addToCache(Collection<Result> items)
        {
            try
            {
                Map<String, Result> map = new HashMap<String, Result>();
                for(Result acoustidResult:items)
                {
                    map.put(acoustidResult.getId(), acoustidResult);
                }
                getCache().putAll(map);
                return true;
            }
            catch(Exception e)
            {
                Errors.addError("Failed AddResultToCache:" + e.getMessage(), e);
                return false;
            }
        }

        /**
         * Add item to cache
         *
         * @param item
         */
        protected  boolean addToCache(Result item)
        {
            try
            {
                getCache().putIfAbsent(item.getId(), item);
                return true;
            }
            catch(Exception e)
            {
                Errors.addError("Failed AddAcoustidToCache:"+item.getId()+ ':' +e.getMessage(),e);
                return false;
            }
        }
    }
</code>```

and this is the stack trace

```<code>java.lang.RuntimeException: java.nio.channels.ClosedChannelException
    at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk$1.setLong(FileBackedStorageEngine.java:677)
    at org.ehcache.impl.internal.store.offheap.LazyOffHeapValueHolder.writeBack(LazyOffHeapValueHolder.java:82)
    at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore.lambda$flush$16(AbstractOffHeapStore.java:865)
    at org.ehcache.impl.internal.store.disk.EhcachePersistentConcurrentOffHeapClockCache.lambda$computeIfPinned$3(EhcachePersistentConcurrentOffHeapClockCache.java:213)
    at org.terracotta.offheapstore.OffHeapHashMap.computeIfPresentWithMetadata(OffHeapHashMap.java:2096)
    at org.terracotta.offheapstore.AbstractLockedOffHeapHashMap.computeIfPresentWithMetadata(AbstractLockedOffHeapHashMap.java:604)
    at org.terracotta.offheapstore.concurrent.AbstractConcurrentOffHeapMap.computeIfPresentWithMetadata(AbstractConcurrentOffHeapMap.java:781)
    at org.ehcache.impl.internal.store.disk.EhcachePersistentConcurrentOffHeapClockCache.computeIfPinned(EhcachePersistentConcurrentOffHeapClockCache.java:210)
    at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore.flush(AbstractOffHeapStore.java:858)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.lambda$setInvalidationListener$19(OnHeapStore.java:914)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.lambda$evict$28(OnHeapStore.java:1552)
    at org.ehcache.impl.internal.concurrent.ConcurrentHashMap.computeIfPresent(ConcurrentHashMap.java:1848)
    at org.ehcache.impl.internal.store.heap.SimpleBackend.computeIfPresent(SimpleBackend.java:130)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.evict(OnHeapStore.java:1547)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.enforceCapacity(OnHeapStore.java:1514)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.resolveFault(OnHeapStore.java:745)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.getOrComputeIfAbsent(OnHeapStore.java:692)
    at org.ehcache.impl.internal.store.tiering.TieredStore.get(TieredStore.java:88)
    at org.ehcache.core.Ehcache.doGet(Ehcache.java:90)
    at org.ehcache.core.EhcacheBase.get(EhcacheBase.java:127)
    at com.jthink.songkong.cache.AcoustIdResultCache.getFromCache(AcoustIdResultCache.java:177)
    at com.jthink.songkong.cache.AcoustIdResultCache.get(AcoustIdResultCache.java:157)
    at com.jthink.songkong.analyse.acoustid.AcoustidHelper.getMusicBrainzAcoustidResultsFromCacheOrDb(AcoustidHelper.java:136)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.getReleasesByAcoustid(MusicBrainzSingleSongMatcher.java:99)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.queryForCandidateReleasesByArtistTitleAndReleaseForSingleSong(MusicBrainzSingleSongMatcher.java:335)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.matchSingleSongToReleaseUsingExistingFileMetadata(MusicBrainzSingleSongMatcher.java:396)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.match(MusicBrainzSingleSongMatcher.java:72)
    at com.jthink.songkong.analyse.analyser.AbstractMusicBrainzGroupMatcher.matchSongs(AbstractMusicBrainzGroupMatcher.java:146)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.doTask(MusicBrainzSongGroupMatcher1.java:420)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher.call(MusicBrainzSongGroupMatcher.java:469)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.call(MusicBrainzSongGroupMatcher1.java:97)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.call(MusicBrainzSongGroupMatcher1.java:40)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.nio.channels.ClosedChannelException
    at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:110)
    at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:758)
    at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.writeBufferToChannel(FileBackedStorageEngine.java:393)
    at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.writeLongToChannel(FileBackedStorageEngine.java:388)
    at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.access$700(FileBackedStorageEngine.java:60)
    at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk$1.setLong(FileBackedStorageEngine.java:675)
    ... 35 more
</code>```

I don't see why happens, my first thought is I should modify

```<code>  protected org.ehcache.Cache<String, Result> getCache()
  {
     return cache;
  }
</code>```

to

```<code>  protected org.ehcache.Cache<String, Result> getCache()
  {
      if(cache==null)
      {
          cache = createCache();
      }
      return cache;
  }
</code>```

but why would cache ever be null ?

My second thought is there is some multithreading issue, but I assume Ehcache is thread safe ?

答:
There have been a couple of bugs in this area. I think that you are falling victim to a variant of: https://github.com/Terracotta-OSS/offheap-store/pull/53

If you could update to Ehcache 3.7.1 which contains the fixes for these issues and see if you can reproduce.  If that doesn't solve the issue then please file a bug at https://github.com/ehcache/ehcache3

Body:
I am having a problem with Ehcache. It usually works fine but occasionally I get a ClosedChannelException when try to access the cache, and once that occurs I can only get round the problem by recreating the cache

Here is my class

package com.jthink.songkong.cache;

    import com.jthink.acoustid.acoustidschema.Result;
    import com.jthink.songkong.analyse.general.Errors;
    import org.ehcache.CachePersistenceException;
    import org.ehcache.config.builders.CacheConfigurationBuilder;
    import org.ehcache.config.builders.ExpiryPolicyBuilder;
    import org.ehcache.config.builders.ResourcePoolsBuilder;
    import org.ehcache.config.units.EntryUnit;
    import org.ehcache.config.units.MemoryUnit;
    import org.ehcache.core.statistics.CacheStatistics;

    import java.time.Duration;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;

    /**
     * AcoustIdResult EhCache
     *
     */
    public class AcoustIdResultCache
    {
        protected org.ehcache.Cache<String, Result>           cache;

        public static final String ACOUSTID_RESULT_CACHE = "AcoustidResultCache";

        private static AcoustIdResultCache apc;

        private AcoustIdResultCache()
        {
           createCache();
        }

        static
        {
            apc = new AcoustIdResultCache();
        }

        public static AcoustIdResultCache getInstanceOf()
        {
            if(apc==null)
            {
                apc = new AcoustIdResultCache();
            }
            return apc;
        }

        /**
         * Create the Cache
         */
        private void createCache()
        {
            cache = SongKongCacheManager.getCacheManager().createCache(getCacheName(),
                    CacheConfigurationBuilder.newCacheConfigurationBuilder(
                            String.class,
                            Result.class,
                            ResourcePoolsBuilder.newResourcePoolsBuilder()
                                    .heap(300, EntryUnit.ENTRIES)
                                    .disk(500, MemoryUnit.MB, true)
                    )
                            .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofDays(10)) //Since Acoustid is Live Db
                            )
            );
        }

        protected AcoustIdResultCache(String cacheName)
        {
            createCache();
        }

        public void init()
        {

        }

        /**
         * Close Cache Manager
         */
        public static void close()
        {
            SongKongCacheManager.getCacheManager().close();
        }

        public void emptyCache()
        {
            try
            {
                SongKongCacheManager.getCacheManager().destroyCache(getCacheName());
                createCache();
            }
            catch(CachePersistenceException cpe)
            {
                Errors.addError("Problem emptying cache for"+getCacheName()+":"+cpe.getMessage(), cpe);
            }
        }

        public void clearCacheStatistics()
        {
            SongKongCacheManager.getStatistics().getCacheStatistics(getCacheName()).clear();
        }

        /**
         *
         * @return cache name
         */
        protected String getCacheName()
        {
            return ACOUSTID_RESULT_CACHE;
        }

        /**
         *
         * @return cache
         */

        protected org.ehcache.Cache<String, Result> getCache()
        {
            return cache;
        }

        /**
         *
         * @return statistics of cache
         */
        public String statusCache()
        {
            CacheStatistics ehCacheStat = SongKongCacheManager.getStatistics().getCacheStatistics(getCacheName());
            return "CacheName:" + getCacheName()
                    + ":Put:"   + ehCacheStat.getCachePuts()
                    + ":Hit:"   + ehCacheStat.getCacheHits()
                    + ":Miss:"  + ehCacheStat.getCacheMisses()
                    + ":Evictions:"  + ehCacheStat.getCacheEvictions()
                    + ":Removals:"  + ehCacheStat.getCacheRemovals();
        }

        /**
         * Add an item to cache
         *
         * @param item
         */
        public void add(Result item)
        {
            addToCache(item);
        }

        /**
         * Get Item from cache
         *
         * @param id
         * @return the item or null
         */
        public  Result get(String id)
        {
            return getFromCache(id);
        }

        /**
         * Is there an item with this id in cache
         * @param id
         * @return
         */
        public boolean isInCache(String id)
        {
            return getCache().containsKey(id);
        }

        /**
         *
         * @param id
         * @return item for this id or null if doesn't not exist
         */
        protected   Result getFromCache(String id)
        {
            return getCache().get(id);
        }

        /**
         * For adding multiple items to cache
         *
         * @param items
         * @return
         */
        public  boolean addToCache(Collection<Result> items)
        {
            try
            {
                Map<String, Result> map = new HashMap<String, Result>();
                for(Result acoustidResult:items)
                {
                    map.put(acoustidResult.getId(), acoustidResult);
                }
                getCache().putAll(map);
                return true;
            }
            catch(Exception e)
            {
                Errors.addError("Failed AddResultToCache:" + e.getMessage(), e);
                return false;
            }
        }

        /**
         * Add item to cache
         *
         * @param item
         */
        protected  boolean addToCache(Result item)
        {
            try
            {
                getCache().putIfAbsent(item.getId(), item);
                return true;
            }
            catch(Exception e)
            {
                Errors.addError("Failed AddAcoustidToCache:"+item.getId()+ ':' +e.getMessage(),e);
                return false;
            }
        }
    }

and this is the stack trace

java.lang.RuntimeException: java.nio.channels.ClosedChannelException
    at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk$1.setLong(FileBackedStorageEngine.java:677)
    at org.ehcache.impl.internal.store.offheap.LazyOffHeapValueHolder.writeBack(LazyOffHeapValueHolder.java:82)
    at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore.lambda$flush$16(AbstractOffHeapStore.java:865)
    at org.ehcache.impl.internal.store.disk.EhcachePersistentConcurrentOffHeapClockCache.lambda$computeIfPinned$3(EhcachePersistentConcurrentOffHeapClockCache.java:213)
    at org.terracotta.offheapstore.OffHeapHashMap.computeIfPresentWithMetadata(OffHeapHashMap.java:2096)
    at org.terracotta.offheapstore.AbstractLockedOffHeapHashMap.computeIfPresentWithMetadata(AbstractLockedOffHeapHashMap.java:604)
    at org.terracotta.offheapstore.concurrent.AbstractConcurrentOffHeapMap.computeIfPresentWithMetadata(AbstractConcurrentOffHeapMap.java:781)
    at org.ehcache.impl.internal.store.disk.EhcachePersistentConcurrentOffHeapClockCache.computeIfPinned(EhcachePersistentConcurrentOffHeapClockCache.java:210)
    at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore.flush(AbstractOffHeapStore.java:858)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.lambda$setInvalidationListener$19(OnHeapStore.java:914)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.lambda$evict$28(OnHeapStore.java:1552)
    at org.ehcache.impl.internal.concurrent.ConcurrentHashMap.computeIfPresent(ConcurrentHashMap.java:1848)
    at org.ehcache.impl.internal.store.heap.SimpleBackend.computeIfPresent(SimpleBackend.java:130)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.evict(OnHeapStore.java:1547)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.enforceCapacity(OnHeapStore.java:1514)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.resolveFault(OnHeapStore.java:745)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.getOrComputeIfAbsent(OnHeapStore.java:692)
    at org.ehcache.impl.internal.store.tiering.TieredStore.get(TieredStore.java:88)
    at org.ehcache.core.Ehcache.doGet(Ehcache.java:90)
    at org.ehcache.core.EhcacheBase.get(EhcacheBase.java:127)
    at com.jthink.songkong.cache.AcoustIdResultCache.getFromCache(AcoustIdResultCache.java:177)
    at com.jthink.songkong.cache.AcoustIdResultCache.get(AcoustIdResultCache.java:157)
    at com.jthink.songkong.analyse.acoustid.AcoustidHelper.getMusicBrainzAcoustidResultsFromCacheOrDb(AcoustidHelper.java:136)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.getReleasesByAcoustid(MusicBrainzSingleSongMatcher.java:99)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.queryForCandidateReleasesByArtistTitleAndReleaseForSingleSong(MusicBrainzSingleSongMatcher.java:335)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.matchSingleSongToReleaseUsingExistingFileMetadata(MusicBrainzSingleSongMatcher.java:396)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.match(MusicBrainzSingleSongMatcher.java:72)
    at com.jthink.songkong.analyse.analyser.AbstractMusicBrainzGroupMatcher.matchSongs(AbstractMusicBrainzGroupMatcher.java:146)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.doTask(MusicBrainzSongGroupMatcher1.java:420)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher.call(MusicBrainzSongGroupMatcher.java:469)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.call(MusicBrainzSongGroupMatcher1.java:97)
    at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.call(MusicBrainzSongGroupMatcher1.java:40)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.nio.channels.ClosedChannelException
    at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:110)
    at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:758)
    at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.writeBufferToChannel(FileBackedStorageEngine.java:393)
    at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.writeLongToChannel(FileBackedStorageEngine.java:388)
    at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.access$700(FileBackedStorageEngine.java:60)
    at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk$1.setLong(FileBackedStorageEngine.java:675)
    ... 35 more

I don't see why happens, my first thought is I should modify

  protected org.ehcache.Cache<String, Result> getCache()
  {
     return cache;
  }

to

  protected org.ehcache.Cache<String, Result> getCache()
  {
      if(cache==null)
      {
          cache = createCache();
      }
      return cache;
  }

but why would cache ever be null ?

My second thought is there is some multithreading issue, but I assume Ehcache is thread safe ?


问:
Jupyterlab: turn on tab completion for text editor as in Notebook?
In Jupyterlab, there is a text editor that we can open .py files, is it possible to also turn on tab completion, just like how it works in Notebook ?

答: By now, tab completion in the text editor of jupyter lab has been implemented in this pull request (see also discussion in this issue). However, for it to be working you need to open a console for the editor (right click in the editor window and select Create Console for Editor).


问:
Basic Global State with React Hooks in React Native
I've been jumping on the React Hooks bandwagon and decided to roll a React Native app together to try and give a full redux/global state implementation a shot. I've been following some articles on how to go about it, but I do have some questions.

I understand the concepts of useState and useReducer, but I'm getting hung up on the Context side of things.

First question: What is the best way to pass a state object down the tree without having to use props? Here's what I have so far. Normally, without Navigation, you would have a child component of DispatchContext that would receive props. But since I do have Navigation, I don't really want to pass the state down the tree. I feel that defeats the purpose of Context and hooks in general. I've seen some things where the value={dispatch} is value={{somePieceOfState}}. How can I pass both the state and the dispatch into the component tree?

```<code>import React, { useReducer } from 'react';
import Navigation from './Navigation';
import { DispatchContext } from './Context';
import { countReducer } from './reducers';

const App = () => {
  const [count, dispatchCount] = useReducer(countReducer, 0);

  const dispatch = action => [dispatchCount].forEach(fn => fn(action)); //provides one dispatch for the entire component tree

  const state = {
      count,
      //other pieces of state go here
  }

  return (
    <DispatchContext.Provider value={dispatch}>
      <Navigation />
      // <ChildComponent someStateProp={count} /> normal way to pass down props
    </DispatchContext.Provider>
  );
};

export default App;
</code>```

Second question: What's the best way to do actions and action creators? This is probably able to be answered in conjunction with the first question. Here is a component attached to my navigation.

```<code>import React, { useContext } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import { DispatchContext } from './Context';
import { countUp, countDown } from './actions';

const Comp1 = () => {
  const dispatch = useContext(DispatchContext);
  return (
    <View>
      <TouchableOpacity
        onPress={() => dispatch(countUp)}
      >
        <Text>Up</Text>
      </TouchableOpacity>
      <TouchableOpacity
        onPress={() => dispatch(countDown)}
      >
        <Text>Down</Text>
      </TouchableOpacity>
    </View>
  );
};

export default Comp1;
</code>```

The actions associated with it:

```<code>import {
  COUNT_UP,
  COUNT_DOWN
} from './types';

export const countUp = () => {
  return {
    type: COUNT_UP
  };
};

export const countDown = () => {
  return {
    type: COUNT_DOWN
  };
};
</code>```

And the reducer:

```<code>import {
  COUNT_UP,
  COUNT_DOWN
} from '../actions/types';

export const countReducer = (state, action) => {
  console.log(action);
  switch (action) {
    case COUNT_UP:
      return state + 1;
    case COUNT_DOWN:
      return state - 1;
    default:
      return state;
  }
};
</code>```

I know the actions and dispatch and all that works because I console.log() in my reducer and I get a response. But when I console.log(action), it shows that it's the function, the action itself instead of the type. If I console.log(action.type), it's undefined. What am I doing wrong here?

Last question: Is this a reasonable approach for complex state management? I know about useEffect() and stuff like that, but from anyone that has had some experience with hooks, is it good for api calls and location tracking and stuff like that? Is it an extensible approach? Basically what I'm asking is I don't want to get knee deep into this and have to refactor everything into class-based components because I can't do something with hooks that I need to do with classes. But I really like this hooks thing and I want to stick with it. Is it good to move forward with it?

Sorry for the long post, but I feel these questions that have answers would be useful for others that are looking to get into hooks with react native.

答:
Your error is that you are confusing an action creator with an action. An action creator, as the name implies, creates an action when it is invoked. In your case, you are dispatching an action creator instead of dispatching an action. You must invoke the function first.

```<code><TouchableOpacity
  onPress={() => dispatch(countUp())}
>
// ...
<TouchableOpacity
  onPress={() => dispatch(countDown())}
>
</code>```

In the above rework, the action creators are invoked () before being passed to dispatch.

Body:
I've been jumping on the React Hooks bandwagon and decided to roll a React Native app together to try and give a full redux/global state implementation a shot. I've been following some articles on how to go about it, but I do have some questions.

I understand the concepts of useState and useReducer, but I'm getting hung up on the Context side of things.

First question: What is the best way to pass a state object down the tree without having to use props? Here's what I have so far. Normally, without Navigation, you would have a child component of DispatchContext that would receive props. But since I do have Navigation, I don't really want to pass the state down the tree. I feel that defeats the purpose of Context and hooks in general. I've seen some things where the value={dispatch} is value={{somePieceOfState}}. How can I pass both the state and the dispatch into the component tree?

import React, { useReducer } from 'react';
import Navigation from './Navigation';
import { DispatchContext } from './Context';
import { countReducer } from './reducers';

const App = () => {
  const [count, dispatchCount] = useReducer(countReducer, 0);

  const dispatch = action => [dispatchCount].forEach(fn => fn(action)); //provides one dispatch for the entire component tree

  const state = {
      count,
      //other pieces of state go here
  }

  return (
    <DispatchContext.Provider value={dispatch}>
      <Navigation />
      // <ChildComponent someStateProp={count} /> normal way to pass down props
    </DispatchContext.Provider>
  );
};

export default App;

Second question: What's the best way to do actions and action creators? This is probably able to be answered in conjunction with the first question. Here is a component attached to my navigation.

import React, { useContext } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import { DispatchContext } from './Context';
import { countUp, countDown } from './actions';

const Comp1 = () => {
  const dispatch = useContext(DispatchContext);
  return (
    <View>
      <TouchableOpacity
        onPress={() => dispatch(countUp)}
      >
        <Text>Up</Text>
      </TouchableOpacity>
      <TouchableOpacity
        onPress={() => dispatch(countDown)}
      >
        <Text>Down</Text>
      </TouchableOpacity>
    </View>
  );
};

export default Comp1;

The actions associated with it:

import {
  COUNT_UP,
  COUNT_DOWN
} from './types';

export const countUp = () => {
  return {
    type: COUNT_UP
  };
};

export const countDown = () => {
  return {
    type: COUNT_DOWN
  };
};

And the reducer:

import {
  COUNT_UP,
  COUNT_DOWN
} from '../actions/types';

export const countReducer = (state, action) => {
  console.log(action);
  switch (action) {
    case COUNT_UP:
      return state + 1;
    case COUNT_DOWN:
      return state - 1;
    default:
      return state;
  }
};

I know the actions and dispatch and all that works because I console.log() in my reducer and I get a response. But when I console.log(action), it shows that it's the function, the action itself instead of the type. If I console.log(action.type), it's undefined. What am I doing wrong here?

Last question: Is this a reasonable approach for complex state management? I know about useEffect() and stuff like that, but from anyone that has had some experience with hooks, is it good for api calls and location tracking and stuff like that? Is it an extensible approach? Basically what I'm asking is I don't want to get knee deep into this and have to refactor everything into class-based components because I can't do something with hooks that I need to do with classes. But I really like this hooks thing and I want to stick with it. Is it good to move forward with it?

Sorry for the long post, but I feel these questions that have answers would be useful for others that are looking to get into hooks with react native.

Body:
Your error is that you are confusing an action creator with an action. An action creator, as the name implies, creates an action when it is invoked. In your case, you are dispatching an action creator instead of dispatching an action. You must invoke the function first.

<TouchableOpacity
  onPress={() => dispatch(countUp())}
>
// ...
<TouchableOpacity
  onPress={() => dispatch(countDown())}
>

In the above rework, the action creators are invoked () before being passed to dispatch.


问:
Using a Visual Sourcesafe login with no password
We use SourceSafe to prevent accidents, not fraud. Our SourceSafe User names are the same as our Windows user names, and none of our SourceSafe accounts (not even Admin) have a password. When we log in to SourceSafe, the login dialog doesn't even have a space for a password.

A few days ago, my co-worker accidentally checked out a file and then left for the day. I needed to make a change to the file and then check it in. My first attempt to solve this problem was to log in to SourceSafe with her UserID, so that I could "Undo check-out". (This is the first time I ever tried to do this.) However, when I tried to do this, a different login dialog appeared, asking me for the UserID and password. I left the password field blank and pressed OK, but then error message displayed: "Invalid password".

Eventually I figured out that I could log in as "Admin" with no password, and that Admin is able to Undo Checkout for any user.

My account also does not have a password, and yet my co-worker is not able to log in as me unless she is logged in to my Windows account.

My question is, why did this happen? Is there any way to set up several user accounts (other than Admin), so that anyone can log in to them without a password?

答:
Use the following environment variables:

```<code>SSUSER - username
SSPWD - password
</code>```

More and more info.

Body:
Use the following environment variables:

SSUSER - username
SSPWD - password

More and more info.


问:
Scraping youtube all the comments and its replies with selenium in python
I'm trying to scrape the youtube video comments and its replies, comment likes, comment dislikes, comment count, reply count.

First I tried to scrape the text data like comments and its reply with selenium google drivers in python based on id.

I can able to scrape only the comments which is available in the page and not its replies.

Replies are not able to achieve.

```<code>import time
import csv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chrome_path = "/Users/Downloads/chromedriver"
page_url = "https://www.youtube.com/watch?v=AJesAlohO6I&t="

driver = webdriver.Chrome(executable_path=chrome_path)
driver.get(page_url)
time.sleep(2)

title = driver.find_element_by_xpath('//*[@id="container"]/h1/yt-formatted-string').text
print(title)

SCROLL_PAUSE_TIME = 2
CYCLES = 100

html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)
html.send_keys(Keys.PAGE_DOWN)
time.sleep(SCROLL_PAUSE_TIME * 3)

for i in range(CYCLES):
    html.send_keys(Keys.END)
    time.sleep(SCROLL_PAUSE_TIME)

comment_elems = driver.find_elements_by_xpath('//*[@id="content-text"]')
all_comments = [elem.text for elem in comment_elems]
print(all_comments)

write_file = "output_testing.csv"
with open(write_file, "w") as output:
    for line in all_comments:
        output.write(line + '\n')
</code>```

With this above code I can able to scrape only the comments. How to scrape reply of those comments, likes, dislikes, date of those comments with selenium in python.

Can anyone please help me to suggest where I'm went wrong.

Updated Code ( Empty Array )

```<code>import time
import csv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chrome_path = "/Users/Downloads/chromedriver"
page_url = "https://www.youtube.com/watch?v=qBp1rCz_yQU"

driver = webdriver.Chrome(executable_path=chrome_path)
driver.get(page_url)
time.sleep(2)

title = driver.find_element_by_xpath('//*[@id="container"]/h1/yt-formatted-string').text
print(title)

SCROLL_PAUSE_TIME = 2
CYCLES = 100

html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)
html.send_keys(Keys.PAGE_DOWN)
time.sleep(SCROLL_PAUSE_TIME * 3)

for i in range(CYCLES):
    html.send_keys(Keys.END)
    time.sleep(SCROLL_PAUSE_TIME)

driver.find_elements_by_xpath('//div[@id="replies"]/ytd-comment-replies-renderer/ytd-expander/paper-button[@id="more"]')

comment_elems = driver.find_elements_by_xpath('//div[@id="loaded-replies"]//yt-formatted-string[@id="content-text"]')
all_comments = [elem.text for elem in comment_elems]
print(all_comments)

write_file = "output_31may.csv"
with open(write_file, "w") as output:
    for line in all_comments:
        output.write(line + '\n')
</code>```

My updated code: (1-05-2019)

```<code>import time
import csv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chrome_path = "/Users/Downloads/chromedriver"
page_url = "https://www.youtube.com/watch?v=qBp1rCz_yQU"

driver = webdriver.Chrome(executable_path=chrome_path)
driver.get(page_url)
time.sleep(2)

title = driver.find_element_by_xpath('//*[@id="container"]/h1/yt-formatted-string').text
print(title)

SCROLL_PAUSE_TIME = 2
CYCLES = 100

html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)
html.send_keys(Keys.PAGE_DOWN)
time.sleep(SCROLL_PAUSE_TIME * 3)

for i in range(CYCLES):
    html.send_keys(Keys.END)
    time.sleep(SCROLL_PAUSE_TIME)

comment_elems = driver.find_elements_by_xpath('//*[@id="content-text"]')
all_comments = [elem.text for elem in comment_elems]
#print(all_comments)

replies_elems =driver.find_elements_by_xpath('//*[@id="replies"]')
all_replies = [elem.text for elem in replies_elems]
print(all_replies)

write_file = "output_replies.csv"
with open(write_file, "w") as output:
    for line in all_replies:
        output.write(line + '\n')
</code>```

My actual ouput:

```<code>['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View 39 replies', '', '', 'View 2 replies', '', '', '', 'View reply', '', '', '', '', '', 'View reply', '', '', '', '', '', '', '', '', 'View reply', '', '', 'View reply', '', '', '', '', 'View 43 replies', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View 2 replies', '', '', '', '', '', 'View 17 replies', '', '', '', '', 'View 13 replies', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View reply', '', 'View reply', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View 5 replies', '', '', '', '', '', 'View reply', '', 'View 28 replies', '', '', 'View 27 replies', '', '', 'View reply', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View reply', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View 9 replies', 'View reply', '', '', '', 'View reply', '', 'View 13 replies', '', '', '', 'View reply', 'View 9 replies', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View 11 replies', '', '', '', '', 'View 2 replies', '', '', '', '', '', 'View reply', '', '', '', '', '', '', 'View reply', '', '', '', '', '', '', '', 'View reply', '', '', '', 'View 2 replies', '', '', '', '']
</code>```

My expected output to get the replies content message. But I can able to get only for reply count.

答:
You need to click on View replay to scrape comment replies.

for clicking on that, you can do following:

```<code>driver.find_elements_by_xpath("//ytd-button-renderer[@id='more-replies']/a/paper-button[@id="button"]").click()
</code>```

and after that for scraping replies

```<code>driver.find_elements_by_xpath("//div[@id='loaded-replies']/ytd-comment-renderer//yt-formatted-string[@id='content-text']")
</code>```

Body:
I'm trying to scrape the youtube video comments and its replies, comment likes, comment dislikes, comment count, reply count.

First I tried to scrape the text data like comments and its reply with selenium google drivers in python based on id.

I can able to scrape only the comments which is available in the page and not its replies.

Replies are not able to achieve.

import time
import csv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chrome_path = "/Users/Downloads/chromedriver"
page_url = "https://www.youtube.com/watch?v=AJesAlohO6I&t="

driver = webdriver.Chrome(executable_path=chrome_path)
driver.get(page_url)
time.sleep(2)

title = driver.find_element_by_xpath('//*[@id="container"]/h1/yt-formatted-string').text
print(title)

SCROLL_PAUSE_TIME = 2
CYCLES = 100

html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)
html.send_keys(Keys.PAGE_DOWN)
time.sleep(SCROLL_PAUSE_TIME * 3)

for i in range(CYCLES):
    html.send_keys(Keys.END)
    time.sleep(SCROLL_PAUSE_TIME)

comment_elems = driver.find_elements_by_xpath('//*[@id="content-text"]')
all_comments = [elem.text for elem in comment_elems]
print(all_comments)

write_file = "output_testing.csv"
with open(write_file, "w") as output:
    for line in all_comments:
        output.write(line + '\n')

With this above code I can able to scrape only the comments. How to scrape reply of those comments, likes, dislikes, date of those comments with selenium in python.

Can anyone please help me to suggest where I'm went wrong.

Updated Code ( Empty Array )

import time
import csv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chrome_path = "/Users/Downloads/chromedriver"
page_url = "https://www.youtube.com/watch?v=qBp1rCz_yQU"

driver = webdriver.Chrome(executable_path=chrome_path)
driver.get(page_url)
time.sleep(2)

title = driver.find_element_by_xpath('//*[@id="container"]/h1/yt-formatted-string').text
print(title)

SCROLL_PAUSE_TIME = 2
CYCLES = 100

html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)
html.send_keys(Keys.PAGE_DOWN)
time.sleep(SCROLL_PAUSE_TIME * 3)

for i in range(CYCLES):
    html.send_keys(Keys.END)
    time.sleep(SCROLL_PAUSE_TIME)

driver.find_elements_by_xpath('//div[@id="replies"]/ytd-comment-replies-renderer/ytd-expander/paper-button[@id="more"]')

comment_elems = driver.find_elements_by_xpath('//div[@id="loaded-replies"]//yt-formatted-string[@id="content-text"]')
all_comments = [elem.text for elem in comment_elems]
print(all_comments)

write_file = "output_31may.csv"
with open(write_file, "w") as output:
    for line in all_comments:
        output.write(line + '\n')

My updated code: (1-05-2019)

import time
import csv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chrome_path = "/Users/Downloads/chromedriver"
page_url = "https://www.youtube.com/watch?v=qBp1rCz_yQU"

driver = webdriver.Chrome(executable_path=chrome_path)
driver.get(page_url)
time.sleep(2)

title = driver.find_element_by_xpath('//*[@id="container"]/h1/yt-formatted-string').text
print(title)

SCROLL_PAUSE_TIME = 2
CYCLES = 100

html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)
html.send_keys(Keys.PAGE_DOWN)
time.sleep(SCROLL_PAUSE_TIME * 3)

for i in range(CYCLES):
    html.send_keys(Keys.END)
    time.sleep(SCROLL_PAUSE_TIME)

comment_elems = driver.find_elements_by_xpath('//*[@id="content-text"]')
all_comments = [elem.text for elem in comment_elems]
#print(all_comments)

replies_elems =driver.find_elements_by_xpath('//*[@id="replies"]')
all_replies = [elem.text for elem in replies_elems]
print(all_replies)

write_file = "output_replies.csv"
with open(write_file, "w") as output:
    for line in all_replies:
        output.write(line + '\n')

My actual ouput:

['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View 39 replies', '', '', 'View 2 replies', '', '', '', 'View reply', '', '', '', '', '', 'View reply', '', '', '', '', '', '', '', '', 'View reply', '', '', 'View reply', '', '', '', '', 'View 43 replies', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View 2 replies', '', '', '', '', '', 'View 17 replies', '', '', '', '', 'View 13 replies', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View reply', '', 'View reply', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View 5 replies', '', '', '', '', '', 'View reply', '', 'View 28 replies', '', '', 'View 27 replies', '', '', 'View reply', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View reply', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View 9 replies', 'View reply', '', '', '', 'View reply', '', 'View 13 replies', '', '', '', 'View reply', 'View 9 replies', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'View 11 replies', '', '', '', '', 'View 2 replies', '', '', '', '', '', 'View reply', '', '', '', '', '', '', 'View reply', '', '', '', '', '', '', '', 'View reply', '', '', '', 'View 2 replies', '', '', '', '']

My expected output to get the replies content message. But I can able to get only for reply count.

Body:
You need to click on View replay to scrape comment replies.

for clicking on that, you can do following:

driver.find_elements_by_xpath("//ytd-button-renderer[@id='more-replies']/a/paper-button[@id="button"]").click()

and after that for scraping replies

driver.find_elements_by_xpath("//div[@id='loaded-replies']/ytd-comment-renderer//yt-formatted-string[@id='content-text']")


问:
mapping values are not allowed in this context at line 2 column 8 (Psych::SyntaxError)
I'm learning RoR by The Ruby on Rails Tutorial (Michael Hartl).

When I add to .gemrc

```<code>sublime ~/.gemrc
</code>```

(Sublime Text is installed) and I just check, for example

```<code>gem -v
</code>```

I get

```<code>Failed to load /home/gvyntyk/.gemrc because it doesn't contain valid YAML hash
</code>```

BTW:

```<code>gvyntyk@gvyntyk-r60:~$ ldconfig -p | grep libyaml
libyaml-0.so.2 (libc6) => /usr/lib/i386-linux-gnu/libyaml-0.so.2
</code>```

And when I add to .gemrc lines:

```<code>install: --no-rdoc --no-ri
update:  --no-rdoc --no-ri
</code>```

(Of course, I don't want to install docs)

I get error:

```<code>gvyntyk@gvyntyk-r60:~$ gem install rails --version 4.0.2
/home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/2.0.0/psych.rb:205:in `parse': (<unknown>): mapping values are not allowed in this context at line 2 column 8 (Psych::SyntaxError)
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/2.0.0/psych.rb:205:in `parse_stream'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/2.0.0/psych.rb:153:in `parse'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/2.0.0/psych.rb:129:in `load'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/config_file.rb:332:in `load_file'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/config_file.rb:197:in `initialize'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/gem_runner.rb:74:in `new'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/gem_runner.rb:74:in `do_configuration'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/gem_runner.rb:39:in `run'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/bin/gem:21:in `<main>'
</code>```

When I delete .gemrc, I don't have those troubles and I can install Rails.

What's wrong?

Ubuntu 14.04.1 LTS

答:
Summarizing what all the previous comments said, I also had this problem and it is a syntax issue with a .yml so I would suggest checking your code in any of the yaml checking web pages or try to play with it and see what solves your problem.

In my case my error was in my sidekiq.yml:

```<code>:verbose: true
  :concurrency: 25
  :queues:
    - [mailers, 7]
    - [default, 5]
</code>```

Solution:

```<code>:concurrency: 25
:queues:
  - [mailers, 7]
  - [default, 5]
</code>```

By the way for free heroku plans you should use:

```<code>:concurrency: 1
:queues:
  - [mailers, 7]
  - [default, 5]
</code>```

Found that after many alerts from Heroku went up, any way have a great day!

Body:
I'm learning RoR by The Ruby on Rails Tutorial (Michael Hartl).

When I add to .gemrc

sublime ~/.gemrc

(Sublime Text is installed) and I just check, for example

gem -v

I get

Failed to load /home/gvyntyk/.gemrc because it doesn't contain valid YAML hash

BTW:

gvyntyk@gvyntyk-r60:~$ ldconfig -p | grep libyaml
libyaml-0.so.2 (libc6) => /usr/lib/i386-linux-gnu/libyaml-0.so.2

And when I add to .gemrc lines:

install: --no-rdoc --no-ri
update:  --no-rdoc --no-ri

(Of course, I don't want to install docs)

I get error:

gvyntyk@gvyntyk-r60:~$ gem install rails --version 4.0.2
/home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/2.0.0/psych.rb:205:in `parse': (<unknown>): mapping values are not allowed in this context at line 2 column 8 (Psych::SyntaxError)
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/2.0.0/psych.rb:205:in `parse_stream'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/2.0.0/psych.rb:153:in `parse'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/2.0.0/psych.rb:129:in `load'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/config_file.rb:332:in `load_file'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/config_file.rb:197:in `initialize'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/gem_runner.rb:74:in `new'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/gem_runner.rb:74:in `do_configuration'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/gem_runner.rb:39:in `run'
    from /home/gvyntyk/.rvm/rubies/ruby-2.0.0-p598/bin/gem:21:in `'

When I delete .gemrc, I don't have those troubles and I can install Rails.

What's wrong?

Ubuntu 14.04.1 LTS

Body:
Summarizing what all the previous comments said, I also had this problem and it is a syntax issue with a .yml so I would suggest checking your code in any of the yaml checking web pages or try to play with it and see what solves your problem.

In my case my error was in my sidekiq.yml:

:verbose: true
  :concurrency: 25
  :queues:
    - [mailers, 7]
    - [default, 5]

Solution:

:concurrency: 25
:queues:
  - [mailers, 7]
  - [default, 5]

By the way for free heroku plans you should use:

:concurrency: 1
:queues:
  - [mailers, 7]
  - [default, 5]

Found that after many alerts from Heroku went up, any way have a great day!


问:
Can't remove virtual directory (.NET MVC 5 + IIS Express)
In trying to have http://localhost:12345 redirect to http://localhost:12345/user/login, I created a virtual directory, which I now realize just remaps  http://localhost:12345 to show as  http://localhost:12345/user/login rather than going to that view itself.

So I added a virtual directory, but can't find it anywhere to remove it (IISExpress/config files, the .NET project files, Properties menu for the project in VS). My understanding is that clicking "Create Virtual Directory" adds an entry to the IISExpress/config/redirection.xml file, but I don't see anything in there for a virtual directory, much less the one I created. I also tried restoring all the config xml files in IISExpress/config/ from a backup but to no avail. I also searched the entire solution for "/user/login".. I can't find where this is set anywhere.

The issue is that I have to use the URL  http://localhost:12345/user/login/user/login to get to the login page at /user/login (using Attribute Routing). I removed Attribute Routing from RouteConfig and the controllers where I was using it (and undeleted the AreaRegistration.cs files for those controllers/areas), and now I can only reach /User/Login by prefixing with /user/login, so the issue is NOT Attribute Routing. It has to be that Virtual Directory.

I get this when browsing to the root http://localhost:12345 - The view '/user/login' or its master was not found or no view engine supports the searched locations. The following locations were searched:
/user/login. So it's definitely interpreting localhost:12345/ as localhost:12345/user/login/...

I am on Windows Server 2016 with the full IIS, but there's no reason it would be using IIS rather than IIS Express. It's set to use IIS Express in Properties of the project.

EDIT: The issue carried over to a fresh install of Windows and VS via TFS, so it seems like the the issue is in the .NET project itself. Also, the issue is only with the /user/login page, in another Area, the pages load as expected without needing /user/login prefix. But again, I don't see anything anomalous in RouteConfig or WebConfig or anywhere else... In any event, I rolled back the changeset and all is well, so the issue is technically "solved", but I'd still like to know what is causing the "virtual directory-esque" behavior in the newer changeset.

答:
Right click on the virtual directory and select "Convert to Application".

Then right click on the application and select "Remove".


问:
Run a function on custom button click in woocommerce admin order page
Based on "Add a button on top of admin orders list in woocommerce" answer code, I was able to add a custom button on woocommerce admin orders list.

Here is that code (lightly customized):

```<code>add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
function admin_order_list_top_bar_button( $which ) {
    global $typenow;

    if ( 'shop_order' === $typenow && 'top' === $which ) {
        ?>
        <div class="alignleft actions custom">
            <button type="submit" name="custom_" style="height:32px;" class="button" value=""><?php
                echo __( 'Import Couriers', 'woocommerce' ); ?></button>
        </div>
        <?php
    }
}
</code>```

Now I need to run a the following function when this custom button is clicked:

```<code>function update_shipping_couriers_meta_field() {
    $dir = __DIR__;
    $couriers = file( $dir . '/import-couriers.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
    $count = count(couriers);
    $i = 1;

    do {
        if ( !empty( $couriers ) ) {
            foreach ( $couriers as $a ) {
                if ( !empty( $a ) ) {
                    $rows = explode(';', $a);

                    $id = $rows[0];
                    $id = int($id);
                    $couriers = $rows[1];

                    update_post_meta( $id, '_shipping_couriers', $couriers );
                }
                $i++;
            }
        }
    }
    while ( $i <= $count );
}
</code>```

In practice, the function updates a "_shipping_couriers" custom field based on a specific order ID. The two values are present in a csv file.

I've already tested it and it's working. I "just" have it run when I click on the button I created with the function above.

How can I run my function when the button is clicked?

答:
There are some missing things in your code and an error in your last function where count(couriers); need to be instead count($couriers);.

```<code>// Display an action button in admin order list header
add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
function admin_order_list_top_bar_button( $which ) {
    global $pagenow, $typenow;

    if ( 'shop_order' === $typenow && 'edit.php' === $pagenow && 'top' === $which ) {
        ?>
        <div class="alignleft actions custom">
            <button type="submit" name="import_courier" style="height:32px;" class="button" value="yes"><?php
                echo __( 'Import Couriers', 'woocommerce' ); ?></button>
        </div>
        <?php
    }
}

// Trigger an action (or run some code) when the button is pressed
add_action( 'restrict_manage_posts', 'display_admin_shop_order_language_filter' );
function display_admin_shop_order_language_filter() {
    global $pagenow, $typenow;

    if ( 'shop_order' === $typenow && 'edit.php' === $pagenow &&
    isset($_GET['import_courier']) && $_GET['import_courier'] === 'yes' ) {

        ## -------- The code to be trigered -------- ##

        update_shipping_couriers_meta_field();

        ## -------------- End of code -------------- ##
    }
}

// Your function that will be triggered on button press
function update_shipping_couriers_meta_field() {
    $dir = __DIR__;
    $couriers = file( $dir . '/import-couriers.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
    $count = count($couriers);
    $i = 1;

    do {
        if ( ! empty( $couriers ) ) {
            foreach ( $couriers as $a ) {
                if ( ! empty( $a ) ) {
                    $rows = explode(';', $a);

                    update_post_meta( intval($rows[0]), '_shipping_couriers', $rows[1] );
                }
                $i++;
            }
        }
    }
    while ( $i <= $count );
}
</code>```

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Based from: Add a button on top of admin orders list in woocommerce

Body:
Based on "Add a button on top of admin orders list in woocommerce" answer code, I was able to add a custom button on woocommerce admin orders list.

Here is that code (lightly customized):

add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
function admin_order_list_top_bar_button( $which ) {
    global $typenow;

    if ( 'shop_order' === $typenow && 'top' === $which ) {
        ?>

            <button type="submit" name="custom_" style="height:32px;" class="button" value=""><?php
                echo __( 'Import Couriers', 'woocommerce' ); ?></button>

        <?php
    }
}

Now I need to run a the following function when this custom button is clicked:

function update_shipping_couriers_meta_field() {
    $dir = __DIR__;
    $couriers = file( $dir . '/import-couriers.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
    $count = count(couriers);
    $i = 1;

    do {
        if ( !empty( $couriers ) ) {
            foreach ( $couriers as $a ) {
                if ( !empty( $a ) ) {
                    $rows = explode(';', $a);

                    $id = $rows[0];
                    $id = int($id);
                    $couriers = $rows[1];

                    update_post_meta( $id, '_shipping_couriers', $couriers );
                }
                $i++;
            }
        }
    }
    while ( $i <= $count );
}

In practice, the function updates a "_shipping_couriers" custom field based on a specific order ID. The two values are present in a csv file.

I've already tested it and it's working. I "just" have it run when I click on the button I created with the function above.

How can I run my function when the button is clicked?

Body:
There are some missing things in your code and an error in your last function where count(couriers); need to be instead count($couriers);.

// Display an action button in admin order list header
add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
function admin_order_list_top_bar_button( $which ) {
    global $pagenow, $typenow;

    if ( 'shop_order' === $typenow && 'edit.php' === $pagenow && 'top' === $which ) {
        ?>
        <div class="alignleft actions custom">
            <button type="submit" name="import_courier" style="height:32px;" class="button" value="yes"><?php
                echo __( 'Import Couriers', 'woocommerce' ); ?></button>
        </div>
        <?php
    }
}

// Trigger an action (or run some code) when the button is pressed
add_action( 'restrict_manage_posts', 'display_admin_shop_order_language_filter' );
function display_admin_shop_order_language_filter() {
    global $pagenow, $typenow;

    if ( 'shop_order' === $typenow && 'edit.php' === $pagenow &&
    isset($_GET['import_courier']) && $_GET['import_courier'] === 'yes' ) {

        ## -------- The code to be trigered -------- ##

        update_shipping_couriers_meta_field();

        ## -------------- End of code -------------- ##
    }
}

// Your function that will be triggered on button press
function update_shipping_couriers_meta_field() {
    $dir = __DIR__;
    $couriers = file( $dir . '/import-couriers.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
    $count = count($couriers);
    $i = 1;

    do {
        if ( ! empty( $couriers ) ) {
            foreach ( $couriers as $a ) {
                if ( ! empty( $a ) ) {
                    $rows = explode(';', $a);

                    update_post_meta( intval($rows[0]), '_shipping_couriers', $rows[1] );
                }
                $i++;
            }
        }
    }
    while ( $i <= $count );
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Based from: Add a button on top of admin orders list in woocommerce


问:
Child component props are empty though they're passed from a parent component
I try to get the value of openMenu from Parent to Child function but the props.value is empty in Child function and I don´t understand why.

```<code>function Child({ routes }, props) {

  return (

    <div>{props.value}</div>        /*This is empty*/
  )
}
</code>```

```<code>function Parent() {
  const [isOpen, setOpen] = useState({
    isOpen: false
  });

  const handleClick = e => {
    e.preventDefault();
    setOpen(isOpen => !isOpen);

    if(isOpen === true) {
      const openMenu = 'open';
      return <Child value={openMenu}/>;
    }
    else {
      const openMenu = 'close';
      return <Child value={openMenu} />
    }

  };
}
</code>```

I want to get the value of openMenu, either open or close, to the Child component.

答:
Props is the first argument in a function component.

```<code>function Child(props) {
  return (
    <div>{props.value}</div>
  )
}
</code>```

If you are trying destructure routes from props you can use the rest pattern (...) to get all other props.

```<code>function Child({ routes, ...props }) {
  return (
    <div>{props.value}</div>
  )
}
</code>```

Body:
I try to get the value of openMenu from Parent to Child function but the props.value is empty in Child function and I don´t understand why.

function Child({ routes }, props) {

  return (

{props.value}
        /*This is empty*/
  )
}

function Parent() {
  const [isOpen, setOpen] = useState({
    isOpen: false
  });

  const handleClick = e => {
    e.preventDefault();
    setOpen(isOpen => !isOpen);

    if(isOpen === true) {
      const openMenu = 'open';
      return <Child value={openMenu}/>;
    }
    else {
      const openMenu = 'close';
      return <Child value={openMenu} />
    }

  };
}

I want to get the value of openMenu, either open or close, to the Child component.

Body:
Props is the first argument in a function component.

function Child(props) {
  return (
    <div>{props.value}</div>
  )
}

If you are trying destructure routes from props you can use the rest pattern (...) to get all other props.

function Child({ routes, ...props }) {
  return (
    <div>{props.value}</div>
  )
}


问:
render :json does not accept options
I'd love to use render :json but it seems its not as flexible.  Whats the right way to do this?

```<code>respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @things }

  #This is great
  format.json { render :text => @things.to_json(:include => :photos) }

  #This doesn't include photos
  format.json { render :json => @things, :include => :photos }
end
</code>```

答:
I guess this article can be useful for you - Rails to_json or as_json? by Jonathan Julian.

The main thought is that you should avoid using to_json in controllers. It is much more flexible to define as_json method in your model.

For instance:

In your Thing model

```<code>def as_json(options={})
  super(:include => :photos)
end
</code>```

And then you can write in your controller just

```<code>render :json => @things
</code>```

OwnerDisplayName: Squeegy

ContentLicense: CC BY-SA 2.5

Title: render :json does not accept options

Body:
I'd love to use render :json but it seems its not as flexible.  Whats the right way to do this?

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @things }

  #This is great
  format.json { render :text => @things.to_json(:include => :photos) }

  #This doesn't include photos
  format.json { render :json => @things, :include => :photos }
end

LastEditorDisplayName: Rich B

Body:
I guess this article can be useful for you - Rails to_json or as_json? by Jonathan Julian.

The main thought is that you should avoid using to_json in controllers. It is much more flexible to define as_json method in your model.

For instance:

In your Thing model

def as_json(options={})
  super(:include => :photos)
end

And then you can write in your controller just

render :json => @things


问:
ag-grid context menu shortcut key not working
hopefully this will be helpful to someone else. I'm using ag-grid enterprise's context menu feature. I wanted to have a shortcut key corresponding to the menu items, but nothing happened when I pressed the key combination.

To make it more confusing (to me) their own example of how to do this doesn't work:

https://www.ag-grid.com/javascript-grid-context-menu/

答:
The problem is that the shortcut attribute only lists the shortcut but doesn't fire anything. You must also write an event handler for the cellKeyDown event to watch for the shortcut:</p>

```<code>import { Component } from '@angular/core';
import { GetContextMenuItemsParams } from 'ag-grid-community';

@Component({
  selector: 'app-root',
  template:
  `
    <ag-grid-angular style="width: 500px; height: 500px;" class="ag-theme-balham"
    [rowData]="rowData" [columnDefs]="columnDefs"
    [getContextMenuItems]="getContextMenuItems"
    (cellKeyDown)="onCellKeyDown($event)">
    </ag-grid-angular>
  `,
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'ag-grid-import-excel';

  columnDefs = [
      {headerName: 'Make', field: 'make' },
      {headerName: 'Model', field: 'model' },
      {headerName: 'Price', field: 'price'}
  ];

  rowData = [
      { make: 'Toyota', model: 'Celica', price: 35000 },
      { make: 'Ford', model: 'Mondeo', price: 32000 },
      { make: 'Porsche', model: 'Boxter', price: 72000 }
  ];

  getContextMenuItems(params:GetContextMenuItemsParams ) {
    return [
      'copy',
      'copyWithHeaders',
      'paste',
      'separator',
      {
        name: "insert",
        shortcut: "Alt + i",
        action: function() {
          console.log("insert");
        },
        icon: '<i class="fas fa-plus"></i>'
      },
      'export',
    ];
  }

  onCellKeyDown($event) {
    let event:KeyboardEvent = $event.event;
    if (event.altKey && event.key === "i") {
      console.log('alt i pressed')
    }
  }
}
</code>```

Body:
The problem is that the shortcut attribute only lists the shortcut but doesn't fire anything. You must also write an event handler for the cellKeyDown event to watch for the shortcut:

import { Component } from '@angular/core';
import { GetContextMenuItemsParams } from 'ag-grid-community';

@Component({
  selector: 'app-root',
  template:
  `
    <ag-grid-angular style="width: 500px; height: 500px;" class="ag-theme-balham"
    [rowData]="rowData" [columnDefs]="columnDefs"
    [getContextMenuItems]="getContextMenuItems"
    (cellKeyDown)="onCellKeyDown($event)">
    </ag-grid-angular>
  `,
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'ag-grid-import-excel';

  columnDefs = [
      {headerName: 'Make', field: 'make' },
      {headerName: 'Model', field: 'model' },
      {headerName: 'Price', field: 'price'}
  ];

  rowData = [
      { make: 'Toyota', model: 'Celica', price: 35000 },
      { make: 'Ford', model: 'Mondeo', price: 32000 },
      { make: 'Porsche', model: 'Boxter', price: 72000 }
  ];

  getContextMenuItems(params:GetContextMenuItemsParams ) {
    return [
      'copy',
      'copyWithHeaders',
      'paste',
      'separator',
      {
        name: "insert",
        shortcut: "Alt + i",
        action: function() {
          console.log("insert");
        },
        icon: '<i class="fas fa-plus"></i>'
      },
      'export',
    ];
  }

  onCellKeyDown($event) {
    let event:KeyboardEvent = $event.event;
    if (event.altKey && event.key === "i") {
      console.log('alt i pressed')
    }
  }
}


问:
how to use puppeteer click() with xpath, web testing
I am trying to click on element with this code:

```<code>test('no existed user try', async() => {
   await page.click($x('//*[contains (text(), "people")]'))
})
</code>```

It returns me an error $x is not a function

how to use xpath correct with click ?

答:
```<code> test('no existed user try', async() => {
    const elements = await page.$x('//*[contains (text(), "people")]');
    await elements[0].click();
 });
</code>```

Body:
I am trying to click on element with this code:

test('no existed user try', async() => {
   await page.click($x('//*[contains (text(), "people")]'))
})

It returns me an error $x is not a function

how to use xpath correct with click ?


问:
how to surround a word using System.Text.RegularExpressions.Regex
I've seen this topic:
How to surround text with brackets using regex?
but that's on ruby and I don't know the analog for C#
I tried

```<code>text = System.Text.RegularExpressions.Regex.Replace(text, ' '  + SpecialWord + ' ', " \"\0\" ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
</code>```

but that didn't insert my matched word. So how do I surround my matched word with quotes?

答:
Try using \b to match the word boundary, rather than a space.

You need to use $0 instead of \0 too.

```<code>text = Regex.Replace(text, @"\b" + SpecialWord + @"\b", @" ""$0"" ", RegexOptions.IgnoreCase);
</code>```

Body:
I've seen this topic:
How to surround text with brackets using regex?
but that's on ruby and I don't know the analog for C#
I tried

text = System.Text.RegularExpressions.Regex.Replace(text, ' '  + SpecialWord + ' ', " \"\0\" ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

but that didn't insert my matched word. So how do I surround my matched word with quotes?

Body:
Try using \b to match the word boundary, rather than a space.

You need to use $0 instead of \0 too.

text = Regex.Replace(text, @"\b" + SpecialWord + @"\b", @" ""$0"" ", RegexOptions.IgnoreCase);


问:
How to scan and remove maclious file when uploading?
When uploading malicious file in this time need scanning and remove that file. any special package is there in NPM? Can you help me on this any one thanks advance.</p>

答:
Following 2 steps are basic steps.

In frontend only allow specific file extensions like(.pdf,.png etc.) with limitations like size. (Don't forget front end code can be manipulated).

2.You shoul also need to check file extenstions & sizes in backend(if you are using node you can use multer to achieve this.)

What more we can do in backend?

If we only rely on checking with extensions it doesn't help. (anyone can modify name of sample.exe to sample.jpg & upload).

For example if you check whether file uploaded image or not in backend other than checking with file extension you can follow below approach also.

The first eight bytes of a PNG file always contain the following (decimal) values: 137 80 78 71 13 10 26 10

If you want to check whether uploaded file is png or not above condition will work. Not only that if you want to check files uploaded properly or not you can follow some approaches like mentioned above. (for .pdf, .doc some rules might be there) You can check MIME signature data which is the best practice.

Don't save uploaded files in backend code repository. Store them some other workspace. (optional)

Following links might help.

Cloud Storages

Other than storing files in local server you can save uploaded files on cloud like amazon s3 bucket. After every time any file is uploaded to that s3 bucket you can trigger scanner using lambdas(automatic file scanners on amazon).

Other than amazon you can also use google drive for files upload (not optimal one). But when someone downloads uploaded file google will automatically scan for viruses.

amazon's s3 bucket file's scan links::

amazon s3 bucket files scan SO

amazon s3 bucket files reddit

s3 files scanning using lambda & clamav

For local server::

checking MIME signature offical docs

check file types plugin

clam scan npm

check image content without extension SO 1

check image content without extensions SO 2


问:
How to start powershell with a title and execute a command at the same time
I want to start a powershell window with a title and execute a command at the same time. My line of code starts a powershell window with a title but does not execute the command in powershell (execute is in the cmd-window).

How can I fix the problem to get the execution in the powershell window I created with the title?

Thanks for helping me out

```<code>    start powershell -command "$Host.UI.RawUI.WindowTitle = 'WebWolf'" & java -jar .\webwolf-8.0.0.M25.jar --server.port=8090
</code>```

答:
The following syntax should work for you. Just swap out the -Command text with your java commands.

```<code>start "WebWolf" powershell.exe -NoExit -Command "reg.exe /?"
</code>```

Body:
I want to start a powershell window with a title and execute a command at the same time. My line of code starts a powershell window with a title but does not execute the command in powershell (execute is in the cmd-window).

How can I fix the problem to get the execution in the powershell window I created with the title?

Thanks for helping me out

    start powershell -command "$Host.UI.RawUI.WindowTitle = 'WebWolf'" & java -jar .\webwolf-8.0.0.M25.jar --server.port=8090

Body:
The following syntax should work for you. Just swap out the -Command text with your java commands.

start "WebWolf" powershell.exe -NoExit -Command "reg.exe /?"


问:
How to check for double entries in my sql table?
i'm a newbie in programming and still learning.
I've created a database in mySQL to store a fictional restaurant's reservations. I got my site's form up and running, when you enter your info (email/seats needed/date/hour) it registers and is entered into the database successfully. But now i'm trying to check and see if i have double entries in my database to then send a message to my customer saying "sorry, that table is already booked, please choose another time or date"

just to make it simple, all i want to check for now is if the "seating table" in question is already in the database more than 2 times, if a third attempt is made i want to error message to appear, but i can't seem to figure out how to check my sql for that, here is the code i have for now...  the column i'm checking is the seating table "5-8" people,   my selections are 1-2, 3-4 and 5-8.
5-8 is what is written in the sql table column as "text"

```<code>require_once("config.php.inc");

$idConnexion = new mysqli($serveur, $utilisateur, $motDePasse, $bd, $port);

if ($idConnexion->connect_error) {
    die('Erreur de connexion (' . $idConnexion->connect_errno . ') ' .$idConnexion->connect_error);
}

$requete = $idConnexion->query("SELECT * FROM tables") or die("La requete a echouee!");
$tableau = $requete->fetch_array();
$heure_res = $tableau["heure"];
$date_res = $tableau["date_res"];
$nombre_res = $tableau["nombre_personnes"];

if (($tableau = $requete->fetch_array()) !=FALSE) {
    $nombre_table = $nombre_res == "5-8";
    if (count($nombre_table) >= 2) {
        echo "Il n'y a plus de place, choisissez une autre heure ou date.";
    } else {
        $resultats = $idConnexion->query("INSERT INTO `tables` SET email = '$_GET[email]', nombre_personnes ='$_GET[nombre_personne]', date_res = '$_GET[date_res]', heure = '$_GET[heure_preference]'") or die("La requête a échouée! : (" . $idConnexion->errno . ") " . $idConnexion->error);
        $message = "L'ajout de la réservation a été fait avec succès!";
        include("succes_interface.php");
    }
}
</code>```

once i fetch the array, i'm trying to see if the "5-8" is in the database more than twice, to return an error, and if it is less than 2, then PHP can continue and add the reservation into the database.

I really hope i'm asking the right questions, I have searched StackOverflow for an answer, but didn't have any luck.

Thank you in advance.

答:
First, your code only seems to be looking at the first row returned by the SELECT statement. Second, the following statement:

```<code>$nombre_table = $nombre_res == "5-8";
</code>```

assigns to $nombre_table a value of True or False. You cannot apply the PHP count function to such a value, so the next statement will throw a warning. In any case, you will not be counting the number of times "5-8" appears in column nombre_personnes. To do that, a query such as the following could be used:

```<code>$requete = $idConnexion->query("SELECT count(*) AS cnt from tables WHERE nombre_personnes = '5-8'");
</code>```

Of course, between the time you read the count and insert a new row, is it possible that another process has modified the database such that the count you read is no longer current and you still end up with too many occurrences of '5-8'? You may need to first lock the table against writes before proceeding.

Body:
i'm a newbie in programming and still learning.
I've created a database in mySQL to store a fictional restaurant's reservations. I got my site's form up and running, when you enter your info (email/seats needed/date/hour) it registers and is entered into the database successfully. But now i'm trying to check and see if i have double entries in my database to then send a message to my customer saying "sorry, that table is already booked, please choose another time or date"

just to make it simple, all i want to check for now is if the "seating table" in question is already in the database more than 2 times, if a third attempt is made i want to error message to appear, but i can't seem to figure out how to check my sql for that, here is the code i have for now...  the column i'm checking is the seating table "5-8" people,   my selections are 1-2, 3-4 and 5-8.
5-8 is what is written in the sql table column as "text"

require_once("config.php.inc");

$idConnexion = new mysqli($serveur, $utilisateur, $motDePasse, $bd, $port);

if ($idConnexion->connect_error) {
    die('Erreur de connexion (' . $idConnexion->connect_errno . ') ' .$idConnexion->connect_error);
}

$requete = $idConnexion->query("SELECT * FROM tables") or die("La requete a echouee!");
$tableau = $requete->fetch_array();
$heure_res = $tableau["heure"];
$date_res = $tableau["date_res"];
$nombre_res = $tableau["nombre_personnes"];

if (($tableau = $requete->fetch_array()) !=FALSE) {
    $nombre_table = $nombre_res == "5-8";
    if (count($nombre_table) >= 2) {
        echo "Il n'y a plus de place, choisissez une autre heure ou date.";
    } else {
        $resultats = $idConnexion->query("INSERT INTO `tables` SET email = '$_GET[email]', nombre_personnes ='$_GET[nombre_personne]', date_res = '$_GET[date_res]', heure = '$_GET[heure_preference]'") or die("La requête a échouée! : (" . $idConnexion->errno . ") " . $idConnexion->error);
        $message = "L'ajout de la réservation a été fait avec succès!";
        include("succes_interface.php");
    }
}

once i fetch the array, i'm trying to see if the "5-8" is in the database more than twice, to return an error, and if it is less than 2, then PHP can continue and add the reservation into the database.

I really hope i'm asking the right questions, I have searched StackOverflow for an answer, but didn't have any luck.

Thank you in advance.

Body:
First, your code only seems to be looking at the first row returned by the SELECT statement. Second, the following statement:

$nombre_table = $nombre_res == "5-8";

assigns to $nombre_table a value of True or False. You cannot apply the PHP count function to such a value, so the next statement will throw a warning. In any case, you will not be counting the number of times "5-8" appears in column nombre_personnes. To do that, a query such as the following could be used:

$requete = $idConnexion->query("SELECT count(*) AS cnt from tables WHERE nombre_personnes = '5-8'");

Of course, between the time you read the count and insert a new row, is it possible that another process has modified the database such that the count you read is no longer current and you still end up with too many occurrences of '5-8'? You may need to first lock the table against writes before proceeding.


问:
When working with access and mysql is there a way to view which queries are used to generate a pivot table?
How can I determine which sql queries are being used to generate the data?</p>

答: MySql has a "processlist" command which can show you the running queries. If you can catch the query as it runs you could see what it's doing.</p>


问:
Java HTML rendering "pt" vs. "px" sizes
I ran into an issue with specifying a html element's width in a JLabel and thought I would run it up the flag pole here and see if anyone had any advice.

When I specify the width of an element using a "px" unit value, the resulting size is actually ~133%, whereas if I don't use a unit, or use "pt", I get the exact size I specified.

In the below example, if you change the "width: 100px" to "width: 100pt", you will get the right size.

This answer https://stackoverflow.com/a/6257861/131795 seems to be related, and the 72 dpi adjustment seems to match with the mismatch that I'm seeing in in my example.

I might be raging against an ancient piece of code here, but why is an absolute px value being converted and a pt value being treated as absolute?

```<code>public class test {
    public static void main(String args[]) {
        JFrame frame = new JFrame();

        JLabel label = new JLabel("<html><div style='width: 100px; background-color: red;'>test</div>");
        frame.getContentPane().add(label);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 200);
        frame.setVisible(true);
    }
}
</code>```

答:
Ok, I think I've found the 'reason' (or culprit) for java scaling pixel values.

In the HTMLEditorKit implementation, there is a CSS class that handles css stuff.  It has a table with scaling values for each supported unit.  (starting on line 2843 in CSS.class)

For "pt", it is 1-to-1 mapping.  For "px", its 1-to-1.3 mapping.
There is even a comment that says:

// Not sure about 1.3, determined by experiementation.

There are actually two mapping tables, where the second one is used if the w3c mode is turned on.  It has scaling modes that are backwards from the first table:

For "pt", it is 1-to-screenres/72dpi.  For "px", its 1-to-1 mapping.

I'm not sure what activates the w3c mode, but beware of the issue.

Title: Java HTML rendering "pt" vs. "px" sizes

Body:
I ran into an issue with specifying a html element's width in a JLabel and thought I would run it up the flag pole here and see if anyone had any advice.

When I specify the width of an element using a "px" unit value, the resulting size is actually ~133%, whereas if I don't use a unit, or use "pt", I get the exact size I specified.

In the below example, if you change the "width: 100px" to "width: 100pt", you will get the right size.

This answer https://stackoverflow.com/a/6257861/131795 seems to be related, and the 72 dpi adjustment seems to match with the mismatch that I'm seeing in in my example.

I might be raging against an ancient piece of code here, but why is an absolute px value being converted and a pt value being treated as absolute?

public class test {
    public static void main(String args[]) {
        JFrame frame = new JFrame();

        JLabel label = new JLabel("
test
");
        frame.getContentPane().add(label);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 200);
        frame.setVisible(true);
    }
}