问:
Spark saveAsTextFile() results in Mkdirs failed to create for half of the directory
I am currently running a Java Spark Application in tomcat and receiving the following exception:

```<code>Caused by: java.io.IOException: Mkdirs failed to create file:/opt/folder/tmp/file.json/_temporary/0/_temporary/attempt_201603031703_0001_m_000000_5
</code>```

on the line

text.saveAsTextFile("/opt/folder/tmp/file.json") //where text is a JavaRDD<String>

The issue is that /opt/folder/tmp/ already exists and successfully creates up to /opt/folder/tmp/file.json/_temporary/0/ and then it runs into what looks like a permission issue with the remaining part of the path _temporary/attempt_201603031703_0001_m_000000_5 itself, but I gave the tomcat user permissions (chown -R tomcat:tomcat tmp/ and chmod -R 755 tmp/) to the tmp/ directory. Does anyone know what could be happening?

Thanks

Edit for @javadba:

```<code>[root@ip tmp]# ls -lrta
total 12
drwxr-xr-x 4 tomcat tomcat 4096 Mar  3 16:44 ..
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 file.json
drwxrwxrwx 3 tomcat tomcat 4096 Mar  7 20:01 .

[root@ip tmp]# cd file.json/
[root@ip file.json]# ls -lrta
total 12
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 _temporary
drwxrwxrwx 3 tomcat tomcat 4096 Mar  7 20:01 ..
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 .

[root@ip file.json]# cd _temporary/
[root@ip _temporary]# ls -lrta
total 12
drwxr-xr-x 2 tomcat tomcat 4096 Mar  7 20:01 0
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 ..
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 .

[root@ip _temporary]# cd 0/
[root@ip 0]# ls -lrta
total 8
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 ..
drwxr-xr-x 2 tomcat tomcat 4096 Mar  7 20:01 .
</code>```

The exception in catalina.out

```<code>Caused by: java.io.IOException: Mkdirs failed to create file:/opt/folder/tmp/file.json/_temporary/0/_temporary/attempt_201603072001_0001_m_000000_5
    at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:438)
    at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:424)
    at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:906)
    at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:799)
    at org.apache.hadoop.mapred.TextOutputFormat.getRecordWriter(TextOutputFormat.java:123)
    at org.apache.spark.SparkHadoopWriter.open(SparkHadoopWriter.scala:91)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1$$anonfun$13.apply(PairRDDFunctions.scala:1193)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1$$anonfun$13.apply(PairRDDFunctions.scala:1185)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
    at org.apache.spark.scheduler.Task.run(Task.scala:89)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:213)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    ... 1 more
</code>```

答:
this is tricky one, but simple to solve. One must configure job.local.dir variable to point to working directory. Following code works fine with writing CSV file:</p>

```<code>def xmlConvert(spark):
    etl_time = time.time()
    df = spark.read.format('com.databricks.spark.xml').options(rowTag='HistoricalTextData').load(
        '/home/zangetsu/proj/prometheus-core/demo/demo-1-iot-predictive-maintainance/dataset/train/')
    df = df.withColumn("TimeStamp", df["TimeStamp"].cast("timestamp")).groupBy("TimeStamp").pivot("TagName").sum(
        "TagValue").na.fill(0)
    df.repartition(1).write.csv(
        path="/home/zangetsu/proj/prometheus-core/demo/demo-1-iot-predictive-maintainance/result/",
        mode="overwrite",
        header=True,
        sep=",")
    print("Time taken to do xml transformation: --- %s seconds ---" % (time.time() - etl_time))

if __name__ == '__main__':
    spark = SparkSession \
        .builder \
        .appName('XML ETL') \
        .master("local[*]") \
        .config('job.local.dir', '/home/zangetsu/proj/prometheus-core/demo/demo-1-iot-predictive-maintainance') \
        .config('spark.driver.memory','64g') \
        .config('spark.debug.maxToStringFields','200') \
        .config('spark.jars.packages', 'com.databricks:spark-xml_2.11:0.5.0') \
        .getOrCreate()

    print('Session created')

    try:
        xmlConvert(spark)

    finally:
        spark.stop()
</code>```

Body:
I am currently running a Java Spark Application in tomcat and receiving the following exception:

Caused by: java.io.IOException: Mkdirs failed to create file:/opt/folder/tmp/file.json/_temporary/0/_temporary/attempt_201603031703_0001_m_000000_5

on the line

text.saveAsTextFile("/opt/folder/tmp/file.json") //where text is a JavaRDD<String>

The issue is that /opt/folder/tmp/ already exists and successfully creates up to /opt/folder/tmp/file.json/_temporary/0/ and then it runs into what looks like a permission issue with the remaining part of the path _temporary/attempt_201603031703_0001_m_000000_5 itself, but I gave the tomcat user permissions (chown -R tomcat:tomcat tmp/ and chmod -R 755 tmp/) to the tmp/ directory. Does anyone know what could be happening?

Thanks

Edit for @javadba:

[root@ip tmp]# ls -lrta
total 12
drwxr-xr-x 4 tomcat tomcat 4096 Mar  3 16:44 ..
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 file.json
drwxrwxrwx 3 tomcat tomcat 4096 Mar  7 20:01 .

[root@ip tmp]# cd file.json/
[root@ip file.json]# ls -lrta
total 12
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 _temporary
drwxrwxrwx 3 tomcat tomcat 4096 Mar  7 20:01 ..
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 .

[root@ip file.json]# cd _temporary/
[root@ip _temporary]# ls -lrta
total 12
drwxr-xr-x 2 tomcat tomcat 4096 Mar  7 20:01 0
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 ..
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 .

[root@ip _temporary]# cd 0/
[root@ip 0]# ls -lrta
total 8
drwxr-xr-x 3 tomcat tomcat 4096 Mar  7 20:01 ..
drwxr-xr-x 2 tomcat tomcat 4096 Mar  7 20:01 .

The exception in catalina.out

Caused by: java.io.IOException: Mkdirs failed to create file:/opt/folder/tmp/file.json/_temporary/0/_temporary/attempt_201603072001_0001_m_000000_5
    at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:438)
    at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:424)
    at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:906)
    at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:799)
    at org.apache.hadoop.mapred.TextOutputFormat.getRecordWriter(TextOutputFormat.java:123)
    at org.apache.spark.SparkHadoopWriter.open(SparkHadoopWriter.scala:91)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1$$anonfun$13.apply(PairRDDFunctions.scala:1193)
    at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsHadoopDataset$1$$anonfun$13.apply(PairRDDFunctions.scala:1185)
    at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
    at org.apache.spark.scheduler.Task.run(Task.scala:89)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:213)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    ... 1 more

Body:
this is tricky one, but simple to solve. One must configure job.local.dir variable to point to working directory. Following code works fine with writing CSV file:

def xmlConvert(spark):
    etl_time = time.time()
    df = spark.read.format('com.databricks.spark.xml').options(rowTag='HistoricalTextData').load(
        '/home/zangetsu/proj/prometheus-core/demo/demo-1-iot-predictive-maintainance/dataset/train/')
    df = df.withColumn("TimeStamp", df["TimeStamp"].cast("timestamp")).groupBy("TimeStamp").pivot("TagName").sum(
        "TagValue").na.fill(0)
    df.repartition(1).write.csv(
        path="/home/zangetsu/proj/prometheus-core/demo/demo-1-iot-predictive-maintainance/result/",
        mode="overwrite",
        header=True,
        sep=",")
    print("Time taken to do xml transformation: --- %s seconds ---" % (time.time() - etl_time))

if __name__ == '__main__':
    spark = SparkSession \
        .builder \
        .appName('XML ETL') \
        .master("local[*]") \
        .config('job.local.dir', '/home/zangetsu/proj/prometheus-core/demo/demo-1-iot-predictive-maintainance') \
        .config('spark.driver.memory','64g') \
        .config('spark.debug.maxToStringFields','200') \
        .config('spark.jars.packages', 'com.databricks:spark-xml_2.11:0.5.0') \
        .getOrCreate()

    print('Session created')

    try:
        xmlConvert(spark)

    finally:
        spark.stop()


来源: stackexchange

问:
NPM browserstack alias installed by package.json for TestCafe not recognized
Installing 'testcafe-browser-provider-browserstack' by package.json causes Jenkins job not to recognize 'browserstack'

Tried removing from package.json and install from command line but dependencies cause npm install to error.

testcafe 1.1.4
testcafe-browser-provider-browserstack 1.8.0

npm install

node_modules/.bin/testcafe -e  browserstack:safari  auth-subscriber-access-myaccount.js

ERROR Unable to find the browser. "browserstack:safari" is not a browser alias or path to an executable file.

答:
I tried different paths and also moving to devDependencies in package.json but wasn't successful. I removed from package.json and installed command line at run time and it works.

Maybe I didn't find the right path to call it or it needs to be "local" installed but it now recognizes 'browserstack' this way.

ContentLicense: CC BY-SA 4.0


问:
Chrome-Extension: iterate through all tabs?
How would I iterate through all tabs a user has open and then check if they have a particular HTML item with id = 'item'?

答:
This is a not deprecated vanilla way (may 2019):</p>

```<code>chrome.tabs.query({}, function(tabs){
        tabs.forEach(tb => {
            chrome.tabs.sendMessage(tb.id, { action: "xxx" });
        });
    });
</code>```

ContentLicense: CC BY-SA 2.5

Body:
This is a not deprecated vanilla way (may 2019):

chrome.tabs.query({}, function(tabs){
        tabs.forEach(tb => {
            chrome.tabs.sendMessage(tb.id, { action: "xxx" });
        });
    });


问:
How to properly unsubscribe from an Observable Angular 7?
I am currently looking at making sure my application has no memory leaks. I am following this tutorial where he shows how to change from using subscribe() to async as well as showing how to explicitly unsubscribe during ngOnDestroy.

I am struggling to test whether my implementation worked and I wanted to know if my implementation of it in my project is actually unsubscribing.

Here is what is in the tutorial in his component:

```<code>@Component({
  /* ... */
  template: `
    <ul *ngIf="todos.length > 0">
      <li *ngFor="let todo of todos">{{todo.name}}</li>
    </ul>
  `
})
export class TodosComponent implements OnInit, OnDestroy {
  private unsubscribe$ = new Subject<void>();

  todos: Todo[];

  constructor(private store: Store<State>) {}

  ngOnInit() {
    this.store
      .pipe(select(selectTodos), takeUntil(this.unsubscribe$)) // unsubscribe to prevent memory leak
      .subscribe(todos => this.todos = todos);            // unwrap observable
  }

  ngOnDestroy(): void {
    this.unsubscribe$.next();
    this.unsubscribe$.complete();
  }
}
</code>```

My ngOnDestroy looks exactly the same as his, but this is what my call to the service looks like:

```<code>this.insightsService.getLatestsInsights(5)
      .pipe(takeUntil(this.unsubscribe$))
      .subscribe(z => {
        if (z != null) {
          this.latestInsights = z;
          this.visibleSlides = z.slice(0, this.amountOfSlidesToShow);
        }
      })
</code>```

So the service works but I just want to know if my implementation is correct? Wasn't sure what the select function was as well or if that is only applicable with what he is using. Let me know if I can provide info.

答:
It's interesting but I can't find any complete answer for this question, I found three approaches, but I'm not sure are they absolutely correct or not, so if anybody knows better solution please provide it.
!!! IMPORTANT SYNTAX OF CODE BELOW IS NOT CHECKED

First solution is assigning subscription to a variable and then use it for unsubscribing.

```<code>public mySubscription: Observable<Array<Something>>;

export class someComponent OnInit, OnDestroy {

  constructor(private myService: MyService) {}

  ngOnInit() {

      this.mySubscription = this.myService.someMethod().subscribe(response => ... do something);
  }

  ngOnDestroy() {
     this.mySubscription.unsubscribe();
  }

.....
</code>```

Second solution is using RxJs's Subscription class. All subscriptions can be put together, so that a call to an unsubscribe() of one Subscription may unsubscribe multiple Subscriptions. We can achieve this behavior by "adding" one subscription into another:

```<code>....

import { Subscription } from 'rxjs/Subscription';

....

private subscription = new Subscription();

public mySubscription1: Observable<Array<Something>>;
public mySubscription2: Observable<Array<Something>>;

export class someComponent OnInit, OnDestroy {

    constructor(private myService: MyService) { }

    ngOnInit() {

        this.mySubscription1 = this.myService.someMethod().subscribe(response => ... do something);
        this.subscription.add(this.subscription.add(this.mySubscription1));

        this.mySubscription2 = this.myService.someMethod().subscribe(response => ... do something);
        this.subscription.add(this.subscription.add(this.mySubscription1));
    }

    ngOnDestroy() {

        if (this.subscription) {
            this.subscription.unsubscribe();
        }
    }

.....
</code>```

Third and final solution is using rxjs's takeUntil operator

```<code>....

import { Subject } from 'rxjs';

....

private unsubscribe = new Subject();

export class someComponent OnInit, OnDestroy {

    constructor(private myService: MyService) { }

    ngOnInit() {

        this.myService.someMethod()
            .pipe(takeUntil(this.unsubscribe))
            .subscribe(response => ... do something);

    }

    ngOnDestroy() {

        this.unsubscribe.next();
        this.unsubscribe.complete();
    }

.....
</code>```

Body:
I am currently looking at making sure my application has no memory leaks. I am following this tutorial where he shows how to change from using subscribe() to async as well as showing how to explicitly unsubscribe during ngOnDestroy.

I am struggling to test whether my implementation worked and I wanted to know if my implementation of it in my project is actually unsubscribing.

Here is what is in the tutorial in his component:

@Component({
  /* ... */
  template: `

 0">

{{todo.name}}

  `
})
export class TodosComponent implements OnInit, OnDestroy {
  private unsubscribe$ = new Subject<void>();

  todos: Todo[];

  constructor(private store: Store<State>) {}

  ngOnInit() {
    this.store
      .pipe(select(selectTodos), takeUntil(this.unsubscribe$)) // unsubscribe to prevent memory leak
      .subscribe(todos => this.todos = todos);            // unwrap observable
  }

  ngOnDestroy(): void {
    this.unsubscribe$.next();
    this.unsubscribe$.complete();
  }
}

My ngOnDestroy looks exactly the same as his, but this is what my call to the service looks like:

this.insightsService.getLatestsInsights(5)
      .pipe(takeUntil(this.unsubscribe$))
      .subscribe(z => {
        if (z != null) {
          this.latestInsights = z;
          this.visibleSlides = z.slice(0, this.amountOfSlidesToShow);
        }
      })

So the service works but I just want to know if my implementation is correct? Wasn't sure what the select function was as well or if that is only applicable with what he is using. Let me know if I can provide info.

Body:
It's interesting but I can't find any complete answer for this question, I found three approaches, but I'm not sure are they absolutely correct or not, so if anybody knows better solution please provide it.
!!! IMPORTANT SYNTAX OF CODE BELOW IS NOT CHECKED

First solution is assigning subscription to a variable and then use it for unsubscribing.

public mySubscription: Observable<Array<Something>>;

export class someComponent OnInit, OnDestroy {

  constructor(private myService: MyService) {}

  ngOnInit() {

      this.mySubscription = this.myService.someMethod().subscribe(response => ... do something);
  }

  ngOnDestroy() {
     this.mySubscription.unsubscribe();
  }

.....

Second solution is using RxJs's Subscription class. All subscriptions can be put together, so that a call to an unsubscribe() of one Subscription may unsubscribe multiple Subscriptions. We can achieve this behavior by "adding" one subscription into another:

....

import { Subscription } from 'rxjs/Subscription';

....

private subscription = new Subscription();

public mySubscription1: Observable<Array<Something>>;
public mySubscription2: Observable<Array<Something>>;

export class someComponent OnInit, OnDestroy {

    constructor(private myService: MyService) { }

    ngOnInit() {

        this.mySubscription1 = this.myService.someMethod().subscribe(response => ... do something);
        this.subscription.add(this.subscription.add(this.mySubscription1));

        this.mySubscription2 = this.myService.someMethod().subscribe(response => ... do something);
        this.subscription.add(this.subscription.add(this.mySubscription1));
    }

    ngOnDestroy() {

        if (this.subscription) {
            this.subscription.unsubscribe();
        }
    }

.....

Third and final solution is using rxjs's takeUntil operator

....

import { Subject } from 'rxjs';

....

private unsubscribe = new Subject();

export class someComponent OnInit, OnDestroy {

    constructor(private myService: MyService) { }

    ngOnInit() {

        this.myService.someMethod()
            .pipe(takeUntil(this.unsubscribe))
            .subscribe(response => ... do something);

    }

    ngOnDestroy() {

        this.unsubscribe.next();
        this.unsubscribe.complete();
    }

.....


问:
Unable to remove active class in React NavLink
I have taken advantage of the NavLink styling to add the active class to the current page menu item link. I am also using "exact in the Navs. However, I am unable to remove the active class from the Home page. Can anyone please take a look at my code and see if I am missing something? Thank you. The code is as follows:

app.js -

```<code>import React, { Component } from 'react';
import './App.css';
import { HashRouter as Router, Route } from 'react-router-dom';

import Inicio from './pages/Inicio';
import Gamification from './pages/Gamification';
import TotalMultimedia from './pages/TotalMultimedia';
import ELearning from './pages/ELearning';
import AbsoluteEngagement from './pages/AbsoluteEngagement';
import Nosotros from './pages/Nosotros';
import Contacto from './pages/Contacto';

class App extends Component {
  render() {
    return (
      <Router basename="/">
        <div>
        <Route exact path='/' component={Inicio} />
        <Route exact path='/Gamification' component={Gamification} />
        <Route exact path='/TotalMultimedia' component={TotalMultimedia} />
        <Route exact path='/ELearning' component={ELearning} />
        <Route exact path='/AbsoluteEngagement' component=
{AbsoluteEngagement} />
        <Route exact path='/Nosotros' component={Nosotros} />
        <Route exact path='/Contacto' component={Contacto} />
        </div>
      </Router>
    );
  }
}

export default App;
</code>```

Navbar.js -

```<code>import React, { Component } from 'react';
import './Navbar.css';
import { NavLink } from 'react-router-dom';

const isActivefunc = (match, location) => {
    console.log(match, location)
    return match
}

class Navbar extends Component {
    render() {
        return (
            <div className="nav-wrapper">
            <nav className="navbar nav-container pb-3 navbar-expand-md navbar-dark overlay">
            <NavLink exact to="/"className="navbar-brand"></NavLink>
                <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
                    <span className="navbar-toggler-icon"></span>
                </button>

                <div className="collapse navbar-collapse" id="navbarsExampleDefault">
                <ul className="navbar-nav ml-auto">
                    <li className="nav-item active">
                    <NavLink
                        isActive={isActivefunc}
                        exact activeClassName="active"
                        className="nav-link text-uppercase"
                        to="/">Inicio</NavLink>
                    </li>
                    <li className="nav-item dropdown">
                    <NavLink exact to="/Servicios" activeClassName="active" className="nav-link text-uppercase dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Servicios</NavLink>
                    <div className="dropdown-menu" aria-labelledby="dropdown01">
                        <NavLink exact to="/Gamification" activeClassName="main-nav-active-dropdown" className="dropdown-item text-uppercase font-weight-bold">Gamification Projects</NavLink>
                        <NavLink exact to="/TotalMultimedia" activeClassName="main-nav-active-dropdown" className="dropdown-item text-uppercase font-weight-bold">Total Multimedia</NavLink>
                        <NavLink exact to="/ELearning" activeClassName="main-nav-active-dropdown" className="dropdown-item text-uppercase font-weight-bold">Full E-Learning Platform</NavLink>
                        <NavLink exact to="/AbsoluteEngagement" activeClassName="main-nav-active-dropdown" className="dropdown-item text-uppercase font-weight-bold">Absolute Engagement Program</NavLink>
                    </div>
                    </li>
                    <li className="nav-item">
                    <NavLink exact to="/Nosotros" activeClassName="active" className="nav-link text-uppercase">Nosotros</NavLink>
                    </li>
                    <li className="nav-item">
                    <NavLink exact to="/Contacto" activeClassName="active" className="nav-link text-uppercase">Contacto</NavLink>
                    </li>

                </ul>
                </div>
            </nav>
            </div>
        );
    }
}

export default Navbar
</code>```

Homepage -

```<code>import React, { Component } from 'react';
import './Inicio.css';
import { Link } from 'react-router-dom';

import Navbar from '../components/Navbar';
import Jumbotron1 from '../components/Jumbotron1';
import Footer from '../components/Footer';

class Inicio extends Component {
    render() {
       return (
        <div>
            <Navbar />
            <Jumbotron1 />
            <div className="content-container mt-3 mb-1 pt-5 pb-5">
                <div className="content-img-1">
                    <div className="image-inner">
                        <div className="caption-background">
                            <div className="caption caption text-center pt-1 text-uppercase">1. Total Multimedia</div>
                            <div className="sub-caption text-center pb-1">
                                <h3>Realizamos sus tutoriales, videos, descargables y más, la mejor experiencia en capacitación</h3>
                            </div>
                            <div className="button-holder">
                                <Link to='/TotalMultimedia' className="btn btn-primary text-uppercase" role="button">Ver Detalles</Link>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div className="content-container mb-2 pt-5 pb-5">
                <div className="content-img-2">
                    <div className="image-inner">
                        <div className="caption-background">
                            <div className="caption caption text-center pt-1 text-uppercase">2. Gamification Projects</div>
                            <div className="sub-caption text-center pb-1">
                                <h3>Desarrollamos el potencial de cada persona con juegos, aplicaciones y más, las herramientas más innovadoras en capacitación</h3>
                            </div>
                            <div className="button-holder">
                                <Link to='/Gamification' className="btn btn-primary text-uppercase" role="button">Ver Detalles</Link>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div className="content-container mb-2 pt-5 pb-5">
                <div className="content-img-3">
                    <div className="image-inner">
                        <div className="caption-background">
                            <div className="caption caption text-center pt-1 text-uppercase">3. Full E-Learning Platform</div>
                            <div className="sub-caption text-center pb-1">
                                <h3>Creamos una plataforma para todos sus materiales de formación, totalmente interactivos y atractivos</h3>
                            </div>
                            <div className="button-holder">
                                <Link to='/ELearning' className="btn btn-primary text-uppercase" role="button">Ver Detalles</Link>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div className="content-container mb-2 pt-5">
                <div className="content-img-4">
                    <div className="image-inner">
                        <div className="caption-background">
                            <div className="caption caption text-center pt-1 text-uppercase">4. ABSOLUTE ENGAGEMENT PROGRAM</div>
                            <div className="sub-caption text-center pb-1">
                                <h3>El poder de todas nuestras soluciones en un solo programa incomparable</h3>
                            </div>
                            <div className="button-holder">
                                <Link to='/AbsoluteEngagement' className="btn btn-primary text-uppercase" role="button">Ver Detalles</Link>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div className="content-container text-center mb-2 pt-5 pb-5">
                <div className="row">
                    <div className="col-lg-3 col-md-6 spacing">
                        <Link to='/TotalMultimedia' className="blue-links">Multimedia</Link>
                    </div>
                    <div className="col-lg-3 col-md-6 spacing">
                    <Link to='/Gamification' className="blue-links">Gamification</Link>
                    </div>
                    <div className="col-lg-3 col-md-6 spacing">
                    <Link to='/ELearning' className="blue-links">Platform</Link>
                    </div>
                    <div className="col-lg-3 col-md-6 spacing">
                    <Link to='/AbsoluteEngagement' className="blue-links">Absolute Program</Link>
                    </div>
                </div>
            </div>
            <div class="images-section pt-3 pb-3">
                <div class="container pt-5 pb-5">
                    <div class="owl-carousel owl-theme">
                        <div class="item"><img class="grey" src="/assets/img/Renault-e1437433948637.jpg" alt="" /></div>
                        <div class="item"><img class="grey" src="/assets/img/hsbc.jpg" alt="" /></div>
                        <div class="item"><img class="grey" src="/assets/img/hiab.jpg" alt="" /></div>
                        <div class="item"><img class="grey" src="/assets/img/ge.jpg" alt="" /></div>
                        <div class="item"><img class="grey" src="/assets/img/holiday-inn.jpg" alt="" /></div>
                        <div class="item"><img class="grey" src="/assets/img/toyota.jpg" alt="" /></div>
                    </div>
                </div>
            </div>
            <Footer />
        </div>
       );
    }
}

export default Inicio
</code>```

Demo site can be found at https://rrspark.com/lionsfieldcom

答: I found my problem. I simply forgot to remove the active class that shipped with the bootstrap example that I copied (dangers of cut and paste). The problem can be seen right after the nav-item class in the Navbar.js file.</p>

Body:
I have taken advantage of the NavLink styling to add the active class to the current page menu item link. I am also using "exact in the Navs. However, I am unable to remove the active class from the Home page. Can anyone please take a look at my code and see if I am missing something? Thank you. The code is as follows:

app.js -

import React, { Component } from 'react';
import './App.css';
import { HashRouter as Router, Route } from 'react-router-dom';

import Inicio from './pages/Inicio';
import Gamification from './pages/Gamification';
import TotalMultimedia from './pages/TotalMultimedia';
import ELearning from './pages/ELearning';
import AbsoluteEngagement from './pages/AbsoluteEngagement';
import Nosotros from './pages/Nosotros';
import Contacto from './pages/Contacto';

class App extends Component {
  render() {
    return (
      <Router basename="/">

        <Route exact path='/' component={Inicio} />
        <Route exact path='/Gamification' component={Gamification} />
        <Route exact path='/TotalMultimedia' component={TotalMultimedia} />
        <Route exact path='/ELearning' component={ELearning} />
        <Route exact path='/AbsoluteEngagement' component=
{AbsoluteEngagement} />
        <Route exact path='/Nosotros' component={Nosotros} />
        <Route exact path='/Contacto' component={Contacto} />

      </Router>
    );
  }
}

export default App;

Navbar.js -

import React, { Component } from 'react';
import './Navbar.css';
import { NavLink } from 'react-router-dom';

const isActivefunc = (match, location) => {
    console.log(match, location)
    return match
}

class Navbar extends Component {
    render() {
        return (

            <NavLink exact to="/"className="navbar-brand"></NavLink>
                <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">

                </button>

                    <NavLink
                        isActive={isActivefunc}
                        exact activeClassName="active"
                        className="nav-link text-uppercase"
                        to="/">Inicio</NavLink>

                    <NavLink exact to="/Servicios" activeClassName="active" className="nav-link text-uppercase dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Servicios</NavLink>

                        <NavLink exact to="/Gamification" activeClassName="main-nav-active-dropdown" className="dropdown-item text-uppercase font-weight-bold">Gamification Projects</NavLink>
                        <NavLink exact to="/TotalMultimedia" activeClassName="main-nav-active-dropdown" className="dropdown-item text-uppercase font-weight-bold">Total Multimedia</NavLink>
                        <NavLink exact to="/ELearning" activeClassName="main-nav-active-dropdown" className="dropdown-item text-uppercase font-weight-bold">Full E-Learning Platform</NavLink>
                        <NavLink exact to="/AbsoluteEngagement" activeClassName="main-nav-active-dropdown" className="dropdown-item text-uppercase font-weight-bold">Absolute Engagement Program</NavLink>

                    <NavLink exact to="/Nosotros" activeClassName="active" className="nav-link text-uppercase">Nosotros</NavLink>

                    <NavLink exact to="/Contacto" activeClassName="active" className="nav-link text-uppercase">Contacto</NavLink>

        );
    }
}

export default Navbar

Homepage -

import React, { Component } from 'react';
import './Inicio.css';
import { Link } from 'react-router-dom';

import Navbar from '../components/Navbar';
import Jumbotron1 from '../components/Jumbotron1';
import Footer from '../components/Footer';

class Inicio extends Component {
    render() {
       return (

            <Navbar />
            <Jumbotron1 />

1. Total Multimedia

Realizamos sus tutoriales, videos, descargables y más, la mejor experiencia en capacitación

                                <Link to='/TotalMultimedia' className="btn btn-primary text-uppercase" role="button">Ver Detalles</Link>

2. Gamification Projects

Desarrollamos el potencial de cada persona con juegos, aplicaciones y más, las herramientas más innovadoras en capacitación

                                <Link to='/Gamification' className="btn btn-primary text-uppercase" role="button">Ver Detalles</Link>

3. Full E-Learning Platform

Creamos una plataforma para todos sus materiales de formación, totalmente interactivos y atractivos

                                <Link to='/ELearning' className="btn btn-primary text-uppercase" role="button">Ver Detalles</Link>

4. ABSOLUTE ENGAGEMENT PROGRAM

El poder de todas nuestras soluciones en un solo programa incomparable

                                <Link to='/AbsoluteEngagement' className="btn btn-primary text-uppercase" role="button">Ver Detalles</Link>

                        <Link to='/TotalMultimedia' className="blue-links">Multimedia</Link>

                    <Link to='/Gamification' className="blue-links">Gamification</Link>

                    <Link to='/ELearning' className="blue-links">Platform</Link>

                    <Link to='/AbsoluteEngagement' className="blue-links">Absolute Program</Link>

       );
    }
}

export default Inicio

Demo site can be found at https://rrspark.com/lionsfieldcom


问:
Unable to run airflow scheduler
I have recently installed airflow on an AWS server by using this guide for ubuntu 16.04. After a painful and successful install started the webserver. I tried a sample dag as follows

```<code>from airflow.operators.python_operator import PythonOperator
from airflow.operators.dummy_operator import DummyOperator
from datetime import timedelta
from airflow import DAG
import airflow

# DEFAULT ARGS
default_args = {
'owner': 'airflow',
'start_date': airflow.utils.dates.days_ago(2),
'depends_on_past': False}

dag = DAG('init_run', default_args=default_args, description='DAG SAMPLE',
schedule_interval='@daily')

def print_something():
        print("HELLO AIRFLOW!")

with dag:
        task_1 = PythonOperator(task_id='do_it', python_callable=print_something)
        task_2 = DummyOperator(task_id='dummy')

        task_1 << task_2
</code>```

But when i open the UI the tasks in the dag are still in "No Status" no matter how many times i trigger manually or refresh the page.

Later i found out that airflow scheduler is not running and shows the following error:

```<code>{celery_executor.py:228} ERROR - Error sending Celery task:No module named 'MySQLdb'
Celery Task ID: ('init_run', 'dummy', datetime.datetime(2019, 5, 30, 18, 0, 24, 902499, tzinfo=<TimezoneInfo [UTC, GMT, +00:00:00, STD]>), 1)
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/airflow/executors/celery_executor.py", line 118, in send_task_to_executor
    result = task.apply_async(args=[command], queue=queue)
  File "/usr/local/lib/python3.7/site-packages/celery/app/task.py", line 535, in apply_async
    **options
  File "/usr/local/lib/python3.7/site-packages/celery/app/base.py", line 728, in send_task
    amqp.send_task_message(P, name, message, **options)
  File "/usr/local/lib/python3.7/site-packages/celery/app/amqp.py", line 552, in send_task_message
    **properties
  File "/usr/local/lib/python3.7/site-packages/kombu/messaging.py", line 181, in publish
    exchange_name, declare,
  File "/usr/local/lib/python3.7/site-packages/kombu/connection.py", line 510, in _ensured
    return fun(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/kombu/messaging.py", line 194, in _publish
    [maybe_declare(entity) for entity in declare]
  File "/usr/local/lib/python3.7/site-packages/kombu/messaging.py", line 194, in <listcomp>
    [maybe_declare(entity) for entity in declare]
  File "/usr/local/lib/python3.7/site-packages/kombu/messaging.py", line 102, in maybe_declare
    return maybe_declare(entity, self.channel, retry, **retry_policy)
  File "/usr/local/lib/python3.7/site-packages/kombu/common.py", line 121, in maybe_declare
    return _maybe_declare(entity, channel)
  File "/usr/local/lib/python3.7/site-packages/kombu/common.py", line 145, in _maybe_declare
    entity.declare(channel=channel)
  File "/usr/local/lib/python3.7/site-packages/kombu/entity.py", line 608, in declare
    self._create_queue(nowait=nowait, channel=channel)
  File "/usr/local/lib/python3.7/site-packages/kombu/entity.py", line 617, in _create_queue
    self.queue_declare(nowait=nowait, passive=False, channel=channel)
  File "/usr/local/lib/python3.7/site-packages/kombu/entity.py", line 652, in queue_declare
    nowait=nowait,
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/virtual/base.py", line 531, in queue_declare
    self._new_queue(queue, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/sqlalchemy/__init__.py", line 82, in _new_queue
    self._get_or_create(queue)
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/sqlalchemy/__init__.py", line 70, in _get_or_create
    obj = self.session.query(self.queue_cls) \
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/sqlalchemy/__init__.py", line 65, in session
    _, Session = self._open()
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/sqlalchemy/__init__.py", line 56, in _open
    engine = self._engine_from_config()
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/sqlalchemy/__init__.py", line 51, in _engine_from_config
    return create_engine(conninfo.hostname, **transport_options)
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 443, in create_engine
    return strategy.create(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 87, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 104, in dbapi
    return __import__("MySQLdb")
ModuleNotFoundError: No module named 'MySQLdb'
</code>```

Here is the setting in the config file (airflow.cfg):

```<code>sql_alchemy_conn = postgresql+psycopg2://airflow@localhost:5432/airflow
broker_url = sqla+mysql://airflow:airflow@localhost:3306/airflow
result_backend =  db+postgresql://airflow:airflow@localhost/airflow
</code>```

I been struggling with this issue for two days now, Please help

答:
I think the example you are following didnt told you to install mysql and it seems you are using it in broker URL.

you can install mysql and than configure it. (for python 3.5+)

```<code>pip install mysqlclient
</code>```

Alternatively, for a quick fix. You can also use rabbit MQ(Rabbitmq is a message broker, that you will require to rerun airflow dags with celery) guest user login

and then your broker_url will be

```<code>broker_url = amqp://guest:guest@localhost:5672//
</code>```

if not already installed, Rabbitmq can be installed with following command.

```<code>sudo apt install rabbitmq-server
</code>```

Change configuration NODE_IP_ADDRESS=0.0.0.0 in configuration file located at

```<code>/etc/rabbitmq/rabbitmq-env.conf
</code>```

start RabbitMQ service

```<code>sudo service rabbitmq-server start
</code>```

Title: Unable to run airflow scheduler

Body:
I have recently installed airflow on an AWS server by using this guide for ubuntu 16.04. After a painful and successful install started the webserver. I tried a sample dag as follows

from airflow.operators.python_operator import PythonOperator
from airflow.operators.dummy_operator import DummyOperator
from datetime import timedelta
from airflow import DAG
import airflow

# DEFAULT ARGS
default_args = {
'owner': 'airflow',
'start_date': airflow.utils.dates.days_ago(2),
'depends_on_past': False}

dag = DAG('init_run', default_args=default_args, description='DAG SAMPLE',
schedule_interval='@daily')

def print_something():
        print("HELLO AIRFLOW!")

with dag:
        task_1 = PythonOperator(task_id='do_it', python_callable=print_something)
        task_2 = DummyOperator(task_id='dummy')

        task_1 << task_2

But when i open the UI the tasks in the dag are still in "No Status" no matter how many times i trigger manually or refresh the page.

Later i found out that airflow scheduler is not running and shows the following error:

{celery_executor.py:228} ERROR - Error sending Celery task:No module named 'MySQLdb'
Celery Task ID: ('init_run', 'dummy', datetime.datetime(2019, 5, 30, 18, 0, 24, 902499, tzinfo=<TimezoneInfo [UTC, GMT, +00:00:00, STD]>), 1)
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/airflow/executors/celery_executor.py", line 118, in send_task_to_executor
    result = task.apply_async(args=[command], queue=queue)
  File "/usr/local/lib/python3.7/site-packages/celery/app/task.py", line 535, in apply_async
    **options
  File "/usr/local/lib/python3.7/site-packages/celery/app/base.py", line 728, in send_task
    amqp.send_task_message(P, name, message, **options)
  File "/usr/local/lib/python3.7/site-packages/celery/app/amqp.py", line 552, in send_task_message
    **properties
  File "/usr/local/lib/python3.7/site-packages/kombu/messaging.py", line 181, in publish
    exchange_name, declare,
  File "/usr/local/lib/python3.7/site-packages/kombu/connection.py", line 510, in _ensured
    return fun(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/kombu/messaging.py", line 194, in _publish
    [maybe_declare(entity) for entity in declare]
  File "/usr/local/lib/python3.7/site-packages/kombu/messaging.py", line 194, in <listcomp>
    [maybe_declare(entity) for entity in declare]
  File "/usr/local/lib/python3.7/site-packages/kombu/messaging.py", line 102, in maybe_declare
    return maybe_declare(entity, self.channel, retry, **retry_policy)
  File "/usr/local/lib/python3.7/site-packages/kombu/common.py", line 121, in maybe_declare
    return _maybe_declare(entity, channel)
  File "/usr/local/lib/python3.7/site-packages/kombu/common.py", line 145, in _maybe_declare
    entity.declare(channel=channel)
  File "/usr/local/lib/python3.7/site-packages/kombu/entity.py", line 608, in declare
    self._create_queue(nowait=nowait, channel=channel)
  File "/usr/local/lib/python3.7/site-packages/kombu/entity.py", line 617, in _create_queue
    self.queue_declare(nowait=nowait, passive=False, channel=channel)
  File "/usr/local/lib/python3.7/site-packages/kombu/entity.py", line 652, in queue_declare
    nowait=nowait,
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/virtual/base.py", line 531, in queue_declare
    self._new_queue(queue, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/sqlalchemy/__init__.py", line 82, in _new_queue
    self._get_or_create(queue)
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/sqlalchemy/__init__.py", line 70, in _get_or_create
    obj = self.session.query(self.queue_cls) \
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/sqlalchemy/__init__.py", line 65, in session
    _, Session = self._open()
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/sqlalchemy/__init__.py", line 56, in _open
    engine = self._engine_from_config()
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/sqlalchemy/__init__.py", line 51, in _engine_from_config
    return create_engine(conninfo.hostname, **transport_options)
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 443, in create_engine
    return strategy.create(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 87, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 104, in dbapi
    return __import__("MySQLdb")
ModuleNotFoundError: No module named 'MySQLdb'

Here is the setting in the config file (airflow.cfg):

sql_alchemy_conn = postgresql+psycopg2://airflow@localhost:5432/airflow
broker_url = sqla+mysql://airflow:airflow@localhost:3306/airflow
result_backend =  db+postgresql://airflow:airflow@localhost/airflow

I been struggling with this issue for two days now, Please help

Body:
I think the example you are following didnt told you to install mysql and it seems you are using it in broker URL.

you can install mysql and than configure it. (for python 3.5+)

pip install mysqlclient

Alternatively, for a quick fix. You can also use rabbit MQ(Rabbitmq is a message broker, that you will require to rerun airflow dags with celery) guest user login

and then your broker_url will be

broker_url = amqp://guest:guest@localhost:5672//

if not already installed, Rabbitmq can be installed with following command.

sudo apt install rabbitmq-server

Change configuration NODE_IP_ADDRESS=0.0.0.0 in configuration file located at

/etc/rabbitmq/rabbitmq-env.conf

start RabbitMQ service

sudo service rabbitmq-server start


问:
SQL query to get max of a column and display with every row
Here's my scenario...
Multiple Items with multiple item types which have multiple prices.
Want to select Items with all types showing the MAX price. Can't figure out how to get the max?

Input:

```<code>ProductId   ProductType Description     Price
</code>```

1           A           BAKED BEANS     1.29

1           B           BAKED BEANS     1.98

Output:

```<code>ProductId   ProductType Description     Price
</code>```

1           A           BAKED BEANS     1.98

1           B           BAKED BEANS     1.98

Any suggestions?

答:
Thomas, right, except for that you need to group by type:</p>

```<code>Select ProductId, ProductType, Description, MaxByDesc.MaxPrice
From Product
Join    (
        Select Description, Max(Price) As MaxPrice
        From Product
        Group By ProductType
        ) As MaxByDesc
    On MaxByDesc.ProductType = Product.ProductType
</code>```

ContentLicense: CC BY-SA 3.0

Body:
Here's my scenario...
Multiple Items with multiple item types which have multiple prices.
Want to select Items with all types showing the MAX price. Can't figure out how to get the max?

Input:

ProductId   ProductType Description     Price

1           A           BAKED BEANS     1.29

1           B           BAKED BEANS     1.98

Output:

ProductId   ProductType Description     Price

1           A           BAKED BEANS     1.98

1           B           BAKED BEANS     1.98

Any suggestions?

Body:
Thomas, right, except for that you need to group by type:

Select ProductId, ProductType, Description, MaxByDesc.MaxPrice
From Product
Join    (
        Select Description, Max(Price) As MaxPrice
        From Product
        Group By ProductType
        ) As MaxByDesc
    On MaxByDesc.ProductType = Product.ProductType


问:
awk complains about non-terminated string in command from concatenated strings
So the background is that I'm using date command inside awk. This command has differing flags on Linux/GNU from OSX. I'm storing correct command with flags in $date variable to work around this. The following awk command (that relies on string concatenation) fails:

```<code>awk '{
    cmd = "'$date'" substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\""
    if ( (cmd | getline dd) > 0 ) {
        $1 = dd
    }
    close(cmd)
    print
}'
</code>```

with error:

```<code>awk: non-terminated string date... at source line 2
 context is
         >>>  <<<
awk: giving up
 source line number 3
</code>```

When replacing awk with echo, the command outputs correctly:

```<code>{
    cmd = "date -r " substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\""
    if ( (cmd | getline dd) > 0 ) {
        $1 = dd
    }
    close(cmd)
    print
}
</code>```

When the above script is put into awk directly, it also parses dates correctly (it takes first argument from each line of stdin as timestamp, strips microseconds and converts date to human-readable format).

$date variable is populated as follows:

```<code>date="date -d @"
date -d @1550000000 &>/dev/null
if [ $? -eq 1 ]; then
    date="date -r "
fi
</code>```

答:
Other answer, which was helpful to me to unravel the mystery, is around adjusting invocation method of awk and sidestepping the shell scripting question of shelling out to awk in this fashion.

I think I solved your shell script syntax problem. The setup:

args.sh:

```<code>#!/bin/bash

# copypasta code that shoves $1, $2... into 0-indexed bash array and prints it out.
# store arguments in a special array
args=("$@")
# get number of elements
ELEMENTS=${#args[@]}

# echo each element in array
# for loop
for (( i=0;i<$ELEMENTS;i++)); do
    echo ARGS[${i}]: ${args[${i}]}
done
</code>```

test.sh:

```<code>date="date -r "
./args.sh '{
    cmd = "'$date'" substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\""
    if ( (cmd | getline dd) > 0 ) {
        $1 = dd
    }
    close(cmd)
    print
}'
</code>```

Execution:

```<code>❯ ./args.sh one two three
ARGS[0]: one
ARGS[1]: two
ARGS[2]: three

❯ bash test.sh
ARGS[0]: { cmd = "date
ARGS[1]: -r
ARGS[2]: " substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\"" if ( (cmd | getline dd) > 0 ) { $1 = dd } close(cmd) print }
</code>```

Explanation: spaces within your naively expanded un-doublequoted shell variable cause the test using echo to not reveal the actual root problem of awk receiving 3 args instead of 1 arg as expected. The first arg being the malformed incomplete awk program.

Here is my fix: I added doublequotes. The shell command looks pretty gnarly now with a great deal of quoting involved.

```<code>❯ cat test.sh
date="date -r "
./args.sh '{
    cmd = "'"$date"'" substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\""
    if ( (cmd | getline dd) > 0 ) {
        $1 = dd
    }
    close(cmd)
    print
}'
❯ bash test.sh
ARGS[0]: { cmd = "date -r " substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\"" if ( (cmd | getline dd) > 0 ) { $1 = dd } close(cmd) print }
</code>```

I will not comment on awk usage because I don't know how to use awk.

This type of code will be rather brittle but hey at least we don't have large backslash stacks in it yet. Anyone written a quine lately?

Body:
So the background is that I'm using date command inside awk. This command has differing flags on Linux/GNU from OSX. I'm storing correct command with flags in $date variable to work around this. The following awk command (that relies on string concatenation) fails:

awk '{
    cmd = "'$date'" substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\""
    if ( (cmd | getline dd) > 0 ) {
        $1 = dd
    }
    close(cmd)
    print
}'

with error:

awk: non-terminated string date... at source line 2
 context is
         >>>  <<<
awk: giving up
 source line number 3

When replacing awk with echo, the command outputs correctly:

{
    cmd = "date -r " substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\""
    if ( (cmd | getline dd) > 0 ) {
        $1 = dd
    }
    close(cmd)
    print
}

When the above script is put into awk directly, it also parses dates correctly (it takes first argument from each line of stdin as timestamp, strips microseconds and converts date to human-readable format).

$date variable is populated as follows:

date="date -d @"
date -d @1550000000 &>/dev/null
if [ $? -eq 1 ]; then
    date="date -r "
fi

Body:
Other answer, which was helpful to me to unravel the mystery, is around adjusting invocation method of awk and sidestepping the shell scripting question of shelling out to awk in this fashion.

I think I solved your shell script syntax problem. The setup:

args.sh:

#!/bin/bash

# copypasta code that shoves $1, $2... into 0-indexed bash array and prints it out.
# store arguments in a special array
args=("$@")
# get number of elements
ELEMENTS=${#args[@]}

# echo each element in array
# for loop
for (( i=0;i<$ELEMENTS;i++)); do
    echo ARGS[${i}]: ${args[${i}]}
done

test.sh:

date="date -r "
./args.sh '{
    cmd = "'$date'" substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\""
    if ( (cmd | getline dd) > 0 ) {
        $1 = dd
    }
    close(cmd)
    print
}'

Execution:

❯ ./args.sh one two three
ARGS[0]: one
ARGS[1]: two
ARGS[2]: three

❯ bash test.sh
ARGS[0]: { cmd = "date
ARGS[1]: -r
ARGS[2]: " substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\"" if ( (cmd | getline dd) > 0 ) { $1 = dd } close(cmd) print }

Explanation: spaces within your naively expanded un-doublequoted shell variable cause the test using echo to not reveal the actual root problem of awk receiving 3 args instead of 1 arg as expected. The first arg being the malformed incomplete awk program.

Here is my fix: I added doublequotes. The shell command looks pretty gnarly now with a great deal of quoting involved.

❯ cat test.sh
date="date -r "
./args.sh '{
    cmd = "'"$date"'" substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\""
    if ( (cmd | getline dd) > 0 ) {
        $1 = dd
    }
    close(cmd)
    print
}'
❯ bash test.sh
ARGS[0]: { cmd = "date -r " substr( $1, 1, length($1) - 3 ) " +\"%Y-%m-%d %H:%M\"" if ( (cmd | getline dd) > 0 ) { $1 = dd } close(cmd) print }

I will not comment on awk usage because I don't know how to use awk.

This type of code will be rather brittle but hey at least we don't have large backslash stacks in it yet. Anyone written a quine lately?


问:
how to remove Microsoft.CodeContracts reference from a solution?
A have a large web service solution (written in C# targetting .NET 3.5) which is comprised of 16 individual projects (many class libraries, a few web applications and setup projects). It is developed in visual studio 2010.

I was recently experimenting with Microsoft.CodeContracts, and after evaluating it decided that it was not suitable for my needs. I removed all of the code that referenced it, and removed the assembly references from all of the projects that referenced it.

However my web service has now started to randomly return this error message:

```<code>"Could not load file or assembly 'Microsoft.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=736440c9b414ea16' or one of its dependencies. The system cannot find the file specified."
</code>```

How/Why can it still be trying to load that assembly when I have removed all the code & references to it?

On investigating further, I am still finding that Microsoft.Contracts.dll is showing up in the /bin/ folder of my web application, even though no projects reference it. It is also listed as a "detected dependency" in my setup project - but I am 100% certain NONE OF MY PROJECTS ARE USING IT!

This is driving me absolutely nuts - there is obviously something lurking around thats holding the reference. How can I remove all traces of Microsoft.CodeContracts from my solution?

答:
There are numerous XML elements within CSharp project files that start with CodeContracts.

Remove them if you feel you need to do so.

There will be as many sections as you have configurations {Release, Debug} x {Any CPU, Mixed Platforms} = 4.  Do this for each *.csproj file in your solution.

Keep in mind that CodeContracts conditionally generate/weave code, so you app might behave differently.

Body:
A have a large web service solution (written in C# targetting .NET 3.5) which is comprised of 16 individual projects (many class libraries, a few web applications and setup projects). It is developed in visual studio 2010.

I was recently experimenting with Microsoft.CodeContracts, and after evaluating it decided that it was not suitable for my needs. I removed all of the code that referenced it, and removed the assembly references from all of the projects that referenced it.

However my web service has now started to randomly return this error message:

"Could not load file or assembly 'Microsoft.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=736440c9b414ea16' or one of its dependencies. The system cannot find the file specified."

How/Why can it still be trying to load that assembly when I have removed all the code & references to it?

On investigating further, I am still finding that Microsoft.Contracts.dll is showing up in the /bin/ folder of my web application, even though no projects reference it. It is also listed as a "detected dependency" in my setup project - but I am 100% certain NONE OF MY PROJECTS ARE USING IT!

This is driving me absolutely nuts - there is obviously something lurking around thats holding the reference. How can I remove all traces of Microsoft.CodeContracts from my solution?


问:
readiness check for google cloud run - how?
I've searched quite extensively in the documentation at https://cloud.google.com/run/docs/how-to. I also found the YAML in the console.cloud.google.com, but I can't edit it. Is there a way to set it up using a command I might have missed?

EDIT:
I couldn't find anything in https://cloud.google.com/sdk/gcloud/reference/beta/container/clusters/create about it either.

EDIT2:

I'm looking for a way to make Google cloud run have a readiness check for my app in a container. The same way that kubernetes does it - example here: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/. The problem is I don't want to have my service down for 30-60 seconds while the app in the container is still spinning up. Google instantly redirects the traffic causing users to wait for a long time when I push a new build.

EDIT3:
Here's the time it takes to make the first initial request after I've deployed a new version.

EDIT4:
The app I'm trying to start is in Python. It's a flask app serving a tensorflow model. I needs to load in several files into memory. This takes only 5-10seconds on my computer, but as you can it takes longer on cloud run.

答:
Cloud Run does not have a readiness check other than confirming your service is listening on the specified port. Once that is done traffic starts routing to the new revision and previous serving revisions are scaled down as they wrap up in-progress requests.

If your goal is to ensure the service is ready ASAP after deployment, you might make a heavier entrypoint that takes care of more setup tasks.

A "heavier" entrypoint like this will help post-deploy responsiveness, at the cost of slower cold-starts.

Examples of things you can front-load in the entrypoint (whether in BASH scripts or in your service before turning on the HTTP server):

Perform all necessary setup tasks such as loading files into memory.

Establish and preserve in global state any clients or connections to backing services.

Perform via your service code any healthchecks that backing services and resources are available.

Warm up in-container caches to minimize the first response.

Again, this optimizes for post-deploy response by penalizing all cold starts.

https://cloud.google.com/run/docs/tips#optimizing_performance


问:
safe enough 8-character short unique random string
I am trying to compute 8-character short unique random filenames for, let's say, thousands of files without probable name collision. Is this method safe enough?

```<code>base64.urlsafe_b64encode(hashlib.md5(os.urandom(128)).digest())[:8]
</code>```

Edit

To be clearer, I am trying to achieve simplest possible obfuscation of filenames being uploaded to a storage.

I figured out that 8-character string, random enough, would be very efficient and simple way to store tens of thousands of files without probable collision, when implemented right. I don't need guaranteed uniqueness, only high-enough improbability of name collision (talking about only thousands of names).

Files are being stored in concurrent environment, so incrementing shared counter is achievable, but complicated. Storing counter in database would be inefficient.

I am also facing the fact that random() under some circumstances returns same pseudorandom sequences in different processes.

答:
Which method has less collisions, is faster and easier to read?

TLDR

The random_choice is the fastest, has fewer collisions but is IMO slightly harder to read.

The most readable is shortuuid_random but is an external dependency and is slightly slower and has 6x the collisions.

The methods

alphabet = string.ascii_lowercase + string.digits
su = shortuuid.ShortUUID(alphabet=alphabet)

def random_choice():
    return ''.join(random.choices(alphabet, k=8))

def truncated_uuid4():
    return str(uuid.uuid4())[:8]

def shortuuid_random():
    return su.random(length=8)

def secrets_random_choice():
    return ''.join(secrets.choice(alphabet) for _ in range(8))

```
<h3>Results</h3>
<p>All methods generate 8-character UUIDs from the <code>abcdefghijklmnopqrstuvwxyz0123456789</code> alphabet. Collisions are calculated from a single run with 10 million draws. Time is reported in seconds as average function execution ± standard deviation, both calculated over 100 runs of 1,000 draws. Total time is the total execution time of the collision testing.</p>
```random_choice: collisions 22 - time (s) 0.00229 ± 0.00016 - total (s) 29.70518
truncated_uuid4: collisions 11711 - time (s) 0.00439 ± 0.00021 - total (s) 54.03649
shortuuid_random: collisions 124 - time (s) 0.00482 ± 0.00029 - total (s) 51.19624
secrets_random_choice: collisions 15 - time (s) 0.02113 ± 0.00072 - total (s) 228.23106
```
<h3>Notes</h3>
<ul>
<li>the default <code>shortuuid</code> alphabet has uppercase characters, hence creating fewer collision. To make it a fair comparison we need to select the same alphabet as the other methods.</li>
<li>the <code>secrets</code> methods <code>token_hex</code> and <code>token_urlsafe</code> while possibly faster, have different alphabets, hence not eligible for the comparison.</li>
<li>the <code>alphabet</code> and class-based <code>shortuuid</code> methods are factored out as module variables, hence speeding up the method execution. This should not affect the TLDR.</li>
</ul>
<h3>Full testing details</h3>
```import random
import secrets
from statistics import mean
from statistics import stdev
import string
import time
import timeit
import uuid

import shortuuid

alphabet = string.ascii_lowercase + string.digits
su = shortuuid.ShortUUID(alphabet=alphabet)

def random_choice():
    return ''.join(random.choices(alphabet, k=8))

def truncated_uuid4():
    return str(uuid.uuid4())[:8]

def shortuuid_random():
    return su.random(length=8)

def secrets_random_choice():
    return ''.join(secrets.choice(alphabet) for _ in range(8))

def test_collisions(fun):
    out = set()
    count = 0
    for _ in range(10_000_000):
        new = fun()
        if new in out:
            count += 1
        else:
            out.add(new)
    return count

def run_and_print_results(fun):
    round_digits = 5
    now = time.time()
    collisions = test_collisions(fun)
    total_time = round(time.time() - now, round_digits)

    trials = 1_000
    runs = 100
    func_time = timeit.repeat(fun, repeat=runs, number=trials)
    avg = round(mean(func_time), round_digits)
    std = round(stdev(func_time), round_digits)

    print(f'{fun.__name__}: collisions {collisions} - '
          f'time (s) {avg} ± {std} - '
          f'total (s) {total_time}')

if __name__ == '__main__':
    run_and_print_results(random_choice)
    run_and_print_results(truncated_uuid4)
    run_and_print_results(shortuuid_random)
    run_and_print_results(secrets_random_choice)
```

Body:
I am trying to compute 8-character short unique random filenames for, let's say, thousands of files without probable name collision. Is this method safe enough?

base64.urlsafe_b64encode(hashlib.md5(os.urandom(128)).digest())[:8]

Edit

To be clearer, I am trying to achieve simplest possible obfuscation of filenames being uploaded to a storage.

I figured out that 8-character string, random enough, would be very efficient and simple way to store tens of thousands of files without probable collision, when implemented right. I don't need guaranteed uniqueness, only high-enough improbability of name collision (talking about only thousands of names).

Files are being stored in concurrent environment, so incrementing shared counter is achievable, but complicated. Storing counter in database would be inefficient.

I am also facing the fact that random() under some circumstances returns same pseudorandom sequences in different processes.

Body:
Which method has less collisions, is faster and easier to read?

TLDR

The random_choice is the fastest, has fewer collisions but is IMO slightly harder to read.

The most readable is shortuuid_random but is an external dependency and is slightly slower and has 6x the collisions.

The methods

alphabet = string.ascii_lowercase + string.digits
su = shortuuid.ShortUUID(alphabet=alphabet)

def random_choice():
    return ''.join(random.choices(alphabet, k=8))

def truncated_uuid4():
    return str(uuid.uuid4())[:8]

def shortuuid_random():
    return su.random(length=8)

def secrets_random_choice():
    return ''.join(secrets.choice(alphabet) for _ in range(8))

Results

All methods generate 8-character UUIDs from the abcdefghijklmnopqrstuvwxyz0123456789 alphabet. Collisions are calculated from a single run with 10 million draws. Time is reported in seconds as average function execution ± standard deviation, both calculated over 100 runs of 1,000 draws. Total time is the total execution time of the collision testing.

random_choice: collisions 22 - time (s) 0.00229 ± 0.00016 - total (s) 29.70518
truncated_uuid4: collisions 11711 - time (s) 0.00439 ± 0.00021 - total (s) 54.03649
shortuuid_random: collisions 124 - time (s) 0.00482 ± 0.00029 - total (s) 51.19624
secrets_random_choice: collisions 15 - time (s) 0.02113 ± 0.00072 - total (s) 228.23106

Notes

the default shortuuid alphabet has uppercase characters, hence creating fewer collision. To make it a fair comparison we need to select the same alphabet as the other methods.

the secrets methods token_hex and token_urlsafe while possibly faster, have different alphabets, hence not eligible for the comparison.

the alphabet and class-based shortuuid methods are factored out as module variables, hence speeding up the method execution. This should not affect the TLDR.

Full testing details

import random
import secrets
from statistics import mean
from statistics import stdev
import string
import time
import timeit
import uuid

import shortuuid

alphabet = string.ascii_lowercase + string.digits
su = shortuuid.ShortUUID(alphabet=alphabet)

def random_choice():
    return ''.join(random.choices(alphabet, k=8))

def truncated_uuid4():
    return str(uuid.uuid4())[:8]

def shortuuid_random():
    return su.random(length=8)

def secrets_random_choice():
    return ''.join(secrets.choice(alphabet) for _ in range(8))

def test_collisions(fun):
    out = set()
    count = 0
    for _ in range(10_000_000):
        new = fun()
        if new in out:
            count += 1
        else:
            out.add(new)
    return count

def run_and_print_results(fun):
    round_digits = 5
    now = time.time()
    collisions = test_collisions(fun)
    total_time = round(time.time() - now, round_digits)

    trials = 1_000
    runs = 100
    func_time = timeit.repeat(fun, repeat=runs, number=trials)
    avg = round(mean(func_time), round_digits)
    std = round(stdev(func_time), round_digits)

    print(f'{fun.__name__}: collisions {collisions} - '
          f'time (s) {avg} ± {std} - '
          f'total (s) {total_time}')

if __name__ == '__main__':
    run_and_print_results(random_choice)
    run_and_print_results(truncated_uuid4)
    run_and_print_results(shortuuid_random)
    run_and_print_results(secrets_random_choice)


问:
Installed Python from Package, Terminal didn't update?
Terminal is still showing Python 2.7.2 after an install of 3.3.0

I'm new to python- just want to get a good development environment working on Mac 10.8.

答:
You have few options

In your bash ~/.bash_profile add alias python='python3'

Instead of using python command use python3 instead.

Install python3 via homebrew


问:
How to change postgresql.conf from Postgres docker image
I have a Rails app that I'm deploying using Docker containers. I'm using PostgreSQL for the database, and I'm trying to set up WAL-E to do continuous backups to an S3 bucket. The setup for this involves making a few changes to the postgresql.conf file.

I am using a postgres official image as a base image, and am passing in a postgresql.conf file. Here is my Dockerfile:

```<code>FROM postgres:9.4
MAINTAINER David Ham <me@example.com>

RUN apt-get update -y && apt-get install -y \
    git \
    python-virtualenv \
    python-dev \
    python-pip \
    libevent-dev \
    pv \
    lzop

RUN virtualenv /opt/wale \
    && . /opt/wale/bin/activate \
    && pip install git+https://github.com/wal-e/wal-e

COPY postgresql.conf /postgresql-wal-e/postgresql-wal-e.conf

RUN chown -R postgres:postgres /postgresql-wal-e

CMD ["postgres", "-c", "config-file=/postgresql-wal-e/postgresql-wal-e.conf"]
</code>```

Is this right? It seems to have copied in correctly, but is this the right way to configure Postgres with their official images?

答:
yes, your way to configure should work.

you can find other ways in answers of this question
How to customize the configuration file of the official PostgreSQL Docker image?

Body:
I have a Rails app that I'm deploying using Docker containers. I'm using PostgreSQL for the database, and I'm trying to set up WAL-E to do continuous backups to an S3 bucket. The setup for this involves making a few changes to the postgresql.conf file.

I am using a postgres official image as a base image, and am passing in a postgresql.conf file. Here is my Dockerfile:

FROM postgres:9.4
MAINTAINER David Ham <me@example.com>

RUN apt-get update -y && apt-get install -y \
    git \
    python-virtualenv \
    python-dev \
    python-pip \
    libevent-dev \
    pv \
    lzop

RUN virtualenv /opt/wale \
    && . /opt/wale/bin/activate \
    && pip install git+https://github.com/wal-e/wal-e

COPY postgresql.conf /postgresql-wal-e/postgresql-wal-e.conf

RUN chown -R postgres:postgres /postgresql-wal-e

CMD ["postgres", "-c", "config-file=/postgresql-wal-e/postgresql-wal-e.conf"]

Is this right? It seems to have copied in correctly, but is this the right way to configure Postgres with their official images?


问:
python selenium xpath find get the second element
The problem of constructing the path to the second element.
How to construct a path?

I'm trying to find the second element with xPath.

```<code>getOpis = driver.find_element_by_xpath("(//article[2]/div/div/descendant-or-self::*)").text
</code>```

This method does not work.

The below does not work either.

```<code>getOpis = driver.find_element_by_xpath("//article[2]/div/div/descendant-or-self::*)[2]").text
</code>```

For the first element, using:

```<code>getOpis = driver.find_element_by_xpath("//article/div/div/descendant-or-self::*)").text
</code>```

The element is downloaded correctly for first element.
What I am doing wrong that I can not get the second item.
The path must retrieve the current text from the second article element

```<code>File "C:\Users\media\PycharmProjects\test\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"(//article[2]/div/div/descendant-or-self::*)"}
  (Session info: chrome=74.0.3729.169)
</code>```

答:
I agree with John Gordon and King Stone. It's hard to tell without more detail, but you could try:

```<code>getOpis = driver.find_elements_by_xpath("//article/div/div/descendant-or-self::*").text
</code>```

Using elements instead of element will return a list of the elements. You can then use:

```<code>getOpis[1]

</code>```

to get the second element.

Body:
The problem of constructing the path to the second element.
How to construct a path?

I'm trying to find the second element with xPath.

getOpis = driver.find_element_by_xpath("(//article[2]/div/div/descendant-or-self::*)").text

This method does not work.

The below does not work either.

getOpis = driver.find_element_by_xpath("//article[2]/div/div/descendant-or-self::*)[2]").text

For the first element, using:

getOpis = driver.find_element_by_xpath("//article/div/div/descendant-or-self::*)").text

The element is downloaded correctly for first element.
What I am doing wrong that I can not get the second item.
The path must retrieve the current text from the second article element

File "C:\Users\media\PycharmProjects\test\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"(//article[2]/div/div/descendant-or-self::*)"}
  (Session info: chrome=74.0.3729.169)

Body:
I agree with John Gordon and King Stone. It's hard to tell without more detail, but you could try:

getOpis = driver.find_elements_by_xpath("//article/div/div/descendant-or-self::*").text

Using elements instead of element will return a list of the elements. You can then use:

getOpis[1]

to get the second element.


问:
Paypal JS SDK and WebHooks - get payer details
I'm integrating the new Paypal smart payment button

```<code><script src="https://www.paypal.com/sdk/js?...>
paypal.Buttons({
createOrder(data, actions) {
// ...
onApprove: function(data, actions) {
// Capture the funds from the transaction
 return actions.order.capture().then(function(details) {
 // Show a success message to your buyer
 alert('Transaction completed by ' + details.payer.name.given_name);
 });
 }
).render('#paypal-button');
</code>```

I created an app in Sandbox and registered Webhooks to all events.
When a payment is triggered I get everything working and a webhook event of type PAYMENT.CAPTURE.COMPLETED is fired.

I'm sure I'm missing something in the process but I can't link this payment to the payer details. I don't know where the payer details are so I can't process the order on my side.

I searched the whole Google and PP documentation without success, anyone can help me?
Thanks!!

答:
Hey! Sorry I haven't been able to be of much help, but here's a link to another question similar to yours that seems to hold the answer.

The answer is to make (unfortunately) a second API call:

The only answer I found was to follow this up with a second call, this time to the GetExpressCheckoutDetails NVP API

Found here: PayPal REST API - How can I get email address returned in the webhook?

I hope I've been of some use!


问:
Storing word documents
I'm working on job site (using asp.net 3.5) in which a user can upload one or more resumes.
Database is sql server 2005.
Whats the recommendation regarding storage of these documents?
Shall i store it as a blob in the database or just store the path in database and keep the document in the physical folder?
Can someone please mention the pros and cons of each wrt performance and scalability?

Thanks!

答:
I'd go with using the best tool for each job:

the best database type for storing and querying data is a relational database.

the best database for managing files is the file system.

Using the filesystem will allow you to use the right tools for managing files: you can use the OS's compression features, use incremental backups tools, etc and not bloat your database with sometimes poorly constructed Word Documents that may contain large images.

As the number of files gets large, the problem is easy to solve from the point of view of the filesystem: store the files into relative path directories  so you can keep the number of file per directory under control (for instance, creating a weekly folder or creating a new folder when the file count is over a certain limit).

Then you only need to keep the relative path of the file in the database.

I would also normalise the filenames at the time of submission to keep them consistent, like 20090219-001.doc.

As other suggested, you could parse the doc file to extract the pure textual information and store it in the database to get the benefits of text search.

OwnerDisplayName: Patrick

Title: Storing word documents

OwnerDisplayName: Renaud Bompuis


问:
Ethernet Shield Not Appearing on Network
I am currently working on a project that involves an arduino uno and an ethernet shield. I am using a sketch from Randomnerdtutorials.com (with slight changes to fit my router) which I will provide. The server that the ethernet shield creates does show up and work when I put the IP address in the URL bar (192.168.1.178:8080) The ethernet shield itself however does not show up on my list of attached devices to my Netgear router. It does occasionally but then disappears. Any ideas? I looked at another thread that stated to replace

```<code>Ethernet.begin(mac, ip, gateway, subnet);
</code>```

with

```<code>Ethernet.begin(mac);
</code>```

but that did not work. Any ideas are welcome. Here is the full code

/*
 Created by Rui Santos
 Visit: http://randomnerdtutorials.com for more arduino projects

 Arduino with Ethernet Shield
 */

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
int led = 4;
Servo microservo;
int pos = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 130 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(8080);                             //server port
String readString;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  microservo.attach(7);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
 // Ethernet.begin(mac);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {
           Serial.println(readString); //print to serial monitor for debuging

           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();
           client.println("");
           client.println("");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("");
           client.println("");
           client.println("
Random Nerd Tutorials Project
");
           client.println("
");
           client.println("
");
           client.println("
Arduino with Ethernet Shield
");
           client.println("
");
           client.println("Turn On LED");
           client.println("Turn Off LED
");
           client.println("
");
           client.println("
");
           client.println("Rotate Left");
           client.println("Rotate Right
");
           client.println("
Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!
");
           client.println("
");
           client.println("");
           client.println("");

           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led, LOW);
           }
           if (readString.indexOf("?button2on") >0){
                for(pos = 0; pos < 180; pos += 3)  // goes from 0 degrees to 180 degrees
                {                                  // in steps of 1 degree
                  microservo.write(pos);              // tell servo to go to position in variable 'pos'
                  delay(15);                       // waits 15ms for the servo to reach the position
                }
           }
           if (readString.indexOf("?button2off") >0){
                for(pos = 180; pos>=1; pos-=3)     // goes from 180 degrees to 0 degrees
                {
                  microservo.write(pos);              // tell servo to go to position in variable 'pos'
                  delay(15);                       // waits 15ms for the servo to reach the position
                }
           }
            //clearing string for next read
            readString="";

         }
       }
    }
}
}```

答:
Looks like the problem is with DHCP. Try

if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
}
else{Serial.println("DHCP OK!");
```

and see if it gives DHCP failure

Body:
I am currently working on a project that involves an arduino uno and an ethernet shield. I am using a sketch from Randomnerdtutorials.com (with slight changes to fit my router) which I will provide. The server that the ethernet shield creates does show up and work when I put the IP address in the URL bar (192.168.1.178:8080) The ethernet shield itself however does not show up on my list of attached devices to my Netgear router. It does occasionally but then disappears. Any ideas? I looked at another thread that stated to replace

Ethernet.begin(mac, ip, gateway, subnet);

with

Ethernet.begin(mac);

but that did not work. Any ideas are welcome. Here is the full code

/*
 Created by Rui Santos
 Visit: http://randomnerdtutorials.com for more arduino projects

 Arduino with Ethernet Shield
 */

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
int led = 4;
Servo microservo;
int pos = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 1, 178 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 1, 130 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(8080);                             //server port
String readString;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(led, OUTPUT);
  microservo.attach(7);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
 // Ethernet.begin(mac);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {
           Serial.println(readString); //print to serial monitor for debuging

           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println();
           client.println("");
           client.println("");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("");
           client.println("");
           client.println("
Random Nerd Tutorials Project
");
           client.println("
");
           client.println("
");
           client.println("
Arduino with Ethernet Shield
");
           client.println("
");
           client.println("Turn On LED");
           client.println("Turn Off LED
");
           client.println("
");
           client.println("
");
           client.println("Rotate Left");
           client.println("Rotate Right
");
           client.println("
Created by Rui Santos. Visit http://randomnerdtutorials.com for more projects!
");
           client.println("
");
           client.println("");
           client.println("");

           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0){
               digitalWrite(led, HIGH);
           }
           if (readString.indexOf("?button1off") >0){
               digitalWrite(led, LOW);
           }
           if (readString.indexOf("?button2on") >0){
                for(pos = 0; pos < 180; pos += 3)  // goes from 0 degrees to 180 degrees
                {                                  // in steps of 1 degree
                  microservo.write(pos);              // tell servo to go to position in variable 'pos'
                  delay(15);                       // waits 15ms for the servo to reach the position
                }
           }
           if (readString.indexOf("?button2off") >0){
                for(pos = 180; pos>=1; pos-=3)     // goes from 180 degrees to 0 degrees
                {
                  microservo.write(pos);              // tell servo to go to position in variable 'pos'
                  delay(15);                       // waits 15ms for the servo to reach the position
                }
           }
            //clearing string for next read
            readString="";

         }
       }
    }
}
}

Body:
Looks like the problem is with DHCP. Try

if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
}
else{Serial.println("DHCP OK!");

and see if it gives DHCP failure


问:
Powershell - Custom enum as a parameter for a script
I have a script with a set of functions and instructions and declared enumeration:

Add-Type -TypeDefinition @"
    public enum CleanUpPolicy {
        NoCleanUp,
        CleanUpByExpiration,
        CleanUpByMaxCount
    }
"@
```

<p>and a script parameters with this enum like:</p>

<pre class="lang-bsh prettyprint-override"><code>param (
    [CleanUpPolicy] $cleanUp = [CleanUpPolicy]::NoCleanUp
)
</code>```

The script works fine when I run it locally. However this script should be downloaded and executed on remote machine and here come issues.
When I'm doing downloading and execution, I got an error iex : Unable to find type [CleanUpPolicy]
Here is script which downloads and executes the script:

iwr ("https://<some_url>/test.ps1") | iex
```

What I'm doing wrong?
Thanks in advance for your help

答:
I think add-type works by compiling the type definition to C#.  Maybe something is going wrong with that.  But in PS 5 you can make enums without using add-type (flags enums too).  https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_enum?view=powershell-5.1</p>

```<code>Enum CleanUpPolicy {
  NoCleanUp
  CleanUpByExpiration
  CleanUpByMaxCount
}

[CleanupPolicy]0
NoCleanUp
</code>```

Body:
I have a script with a set of functions and instructions and declared enumeration:

Add-Type -TypeDefinition @"
    public enum CleanUpPolicy {
        NoCleanUp,
        CleanUpByExpiration,
        CleanUpByMaxCount
    }
"@

and a script parameters with this enum like:

param (
    [CleanUpPolicy] $cleanUp = [CleanUpPolicy]::NoCleanUp
)

The script works fine when I run it locally. However this script should be downloaded and executed on remote machine and here come issues.
When I'm doing downloading and execution, I got an error iex : Unable to find type [CleanUpPolicy]
Here is script which downloads and executes the script:

iwr ("https://<some_url>/test.ps1") | iex

What I'm doing wrong?
Thanks in advance for your help

Body:
I think add-type works by compiling the type definition to C#.  Maybe something is going wrong with that.  But in PS 5 you can make enums without using add-type (flags enums too).  https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_enum?view=powershell-5.1

Enum CleanUpPolicy {
  NoCleanUp
  CleanUpByExpiration
  CleanUpByMaxCount
}

[CleanupPolicy]0
NoCleanUp


问:
Saving multiple files with group name Photoshop
My PSD structure

```<code>Group myname
layer -> Visible
Layer -> Hidden

Group 2randomname
layer -> Visible
Layer -> Hidden
</code>```

and goes on ...

I am trying to save export groups as png, it works file when using file -> Exports -> Layers to File but my problem is its not using the group name, I want to include group name for each file, this is working when selecting all the groups and right click -> export as then selecting PNG the layers have the group names but the problem is its automatically trimming the canvas and there is no option to avoid trimming.

Is there any code or script that can do this ? any directions or help appreciated.

答:
I guess something like this?

Result:

```<code>function main()
{

    var topLayers = activeDocument.layers, // will export all top level layers (groups, backgorund layer, etc)
        exportFolder = Folder.selectDialog();

    if (exportFolder == null) return false;;

    for (var i = 0; i < topLayers.length; i++)
    {
        activeDocument.activeLayer = topLayers[i];
        toggleVisibility();
        savePng24(topLayers[i].name);
        toggleVisibility();
    }

    function savePng24(fileName)
    {
        var pngOpts = new ExportOptionsSaveForWeb;
        pngOpts.format = SaveDocumentType.PNG
        pngOpts.PNG8 = false;
        pngOpts.transparency = true;
        activeDocument.exportDocument(new File(exportFolder + "/" + fileName + ".png"), ExportType.SAVEFORWEB, pngOpts);
    }; // end of savePng24()

    function toggleVisibility()
    {
        var desc = new ActionDescriptor();
        var list = new ActionList();
        var ref = new ActionReference();
        ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
        list.putReference(ref);
        desc.putList(cTID('null'), list);
        desc.putBoolean(cTID('TglO'), true);
        executeAction(cTID('Shw '), desc, DialogModes.NO);
    }; // end of toggleVisibility()

    function cTID(s)
    {
        return app.charIDToTypeID(s);
    };

    function sTID(s)
    {
        return app.stringIDToTypeID(s);
    };

}
app.activeDocument.suspendHistory("temp", "main()");
</code>```

Body:
My PSD structure

Group myname
layer -> Visible
Layer -> Hidden

Group 2randomname
layer -> Visible
Layer -> Hidden

and goes on ...

I am trying to save export groups as png, it works file when using file -> Exports -> Layers to File but my problem is its not using the group name, I want to include group name for each file, this is working when selecting all the groups and right click -> export as then selecting PNG the layers have the group names but the problem is its automatically trimming the canvas and there is no option to avoid trimming.

Is there any code or script that can do this ? any directions or help appreciated.

Body:
I guess something like this?

Result:

function main()
{

    var topLayers = activeDocument.layers, // will export all top level layers (groups, backgorund layer, etc)
        exportFolder = Folder.selectDialog();

    if (exportFolder == null) return false;;

    for (var i = 0; i < topLayers.length; i++)
    {
        activeDocument.activeLayer = topLayers[i];
        toggleVisibility();
        savePng24(topLayers[i].name);
        toggleVisibility();
    }

    function savePng24(fileName)
    {
        var pngOpts = new ExportOptionsSaveForWeb;
        pngOpts.format = SaveDocumentType.PNG
        pngOpts.PNG8 = false;
        pngOpts.transparency = true;
        activeDocument.exportDocument(new File(exportFolder + "/" + fileName + ".png"), ExportType.SAVEFORWEB, pngOpts);
    }; // end of savePng24()

    function toggleVisibility()
    {
        var desc = new ActionDescriptor();
        var list = new ActionList();
        var ref = new ActionReference();
        ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
        list.putReference(ref);
        desc.putList(cTID('null'), list);
        desc.putBoolean(cTID('TglO'), true);
        executeAction(cTID('Shw '), desc, DialogModes.NO);
    }; // end of toggleVisibility()

    function cTID(s)
    {
        return app.charIDToTypeID(s);
    };

    function sTID(s)
    {
        return app.stringIDToTypeID(s);
    };

}
app.activeDocument.suspendHistory("temp", "main()");


问:
jQuery dialog box not in center in Chrome
in FF and Internet Explorer the dialog box shws up in the center, but in chrome it ends up on the left side.

How can i fix this?

code (self is the object this is in):

```<code>     $('form').live('submit',function(e){
            e.preventDefault();
            var $this = this;
            console.log('click submit')
            $('<div>',
                  {html: 'Are you sure you want to submit this table?<br/> All undo information will be lost.'})
             .dialog({
                modal: true,
                title: 'Are You Sure?',
                buttons: {
                    ok: function(){
                        $.get($this.action,$($this).serialize()+'&page='+self.pageOn,function(data){
                            console.log(data);
                            self.pageChanged = false;
                            self.origPage = $('#page').clone();
                            self.lastClick = $('#page').clone();
                        })
                        $(this).dialog('close');
                    },
                    cancel: function(){
                        $(this).dialog('close');
                    }
                },
                beforeClose: function(){
                    $(this).remove();
                }
            })

            return false;
        })
</code>```

答:
Perhaps assign a class to the dialog's div e.g:

```<code> $('<div class="ui-dialogue">',
              {html: 'Are you sure you want to submit this table?<br/> All undo information will be lost.'})
         .dialog({
            modal: true,
            title: 'Are You Sure?',
            buttons: {
</code>```

And then create include a css rule that will center the dialogue on the page like so:

```<code>.ui-dialogue{margin:0 auto;}
</code>```

Or somesuch css that will center the dialogue...
It's a guess, but might force it to work on most browsers.

Body:
in FF and Internet Explorer the dialog box shws up in the center, but in chrome it ends up on the left side.

How can i fix this?

code (self is the object this is in):

     $('form').live('submit',function(e){
            e.preventDefault();
            var $this = this;
            console.log('click submit')
            $('
',
                  {html: 'Are you sure you want to submit this table?
 All undo information will be lost.'})
             .dialog({
                modal: true,
                title: 'Are You Sure?',
                buttons: {
                    ok: function(){
                        $.get($this.action,$($this).serialize()+'&page='+self.pageOn,function(data){
                            console.log(data);
                            self.pageChanged = false;
                            self.origPage = $('#page').clone();
                            self.lastClick = $('#page').clone();
                        })
                        $(this).dialog('close');
                    },
                    cancel: function(){
                        $(this).dialog('close');
                    }
                },
                beforeClose: function(){
                    $(this).remove();
                }
            })

            return false;
        })

Body:
Perhaps assign a class to the dialog's div e.g:

 $('<div class="ui-dialogue">',
              {html: 'Are you sure you want to submit this table?<br/> All undo information will be lost.'})
         .dialog({
            modal: true,
            title: 'Are You Sure?',
            buttons: {

And then create include a css rule that will center the dialogue on the page like so:

.ui-dialogue{margin:0 auto;}

Or somesuch css that will center the dialogue...
It's a guess, but might force it to work on most browsers.